svghmi/widget_button.ysl2
author Edouard Tisserant
Thu, 11 Mar 2021 20:46:05 +0100
branchsvghmi
changeset 3186 1e9b9d7451cd
parent 3086 a70a97196654
child 3232 7bdb766c2a4d
permissions -rw-r--r--
SVGHMI: Ensure that when a HMI:List is refered, none of its refered elements are discarded.
2944
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
     1
// widget_button.ysl2
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
     2
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
     3
// Finite state machine
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
     4
decl fsm(name);
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
     5
decl state(name);
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
     6
decl on_mouse(position);
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
     7
decl on_dispatch(value);
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
     8
decl jump(state);
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
     9
decl show(eltname);
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    10
decl hmi_value(value);
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    11
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    12
// State machine to drive HMI_BOOL on a potentially laggy connection
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    13
const "_button_fsm" fsm {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    14
    state "init" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    15
        on_dispatch "false" jump "released";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    16
        on_dispatch "true" jump "pressed";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    17
    }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    18
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    19
    state "pressing" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    20
        // show "waitactive";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    21
        hmi_value "true";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    22
        on_dispatch "true" jump "pressed";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    23
        on_mouse "up" jump "shortpress";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    24
    }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    25
    state "pressed" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    26
        show "active";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    27
        on_mouse "up" jump "releasing";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    28
        on_dispatch "false" jump "released";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    29
    }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    30
    state "shortpress" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    31
        on_dispatch "true" jump "releasing";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    32
        on_mouse "down" jump "pressing";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    33
    }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    34
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    35
    state "releasing" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    36
        // show "waitinactive";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    37
        hmi_value "false";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    38
        on_dispatch "false" jump "released";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    39
        on_mouse "down" jump "shortrelease";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    40
    }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    41
    state "released" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    42
        show "inactive";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    43
        on_mouse "down" jump "pressing";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    44
        on_dispatch "true" jump "pressed";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    45
    }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    46
    state "shortrelease" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    47
        on_dispatch "false" jump "pressing";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    48
        on_mouse "up" jump "releasing";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    49
    }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    50
}
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    51
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    52
template "fsm", mode="dispatch_transition" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    53
    |         switch (this.state) {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    54
    apply "state", mode="dispatch_transition";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    55
    |         }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    56
}
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    57
template "state", mode="dispatch_transition" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    58
    |           case "«@name»":
3086
a70a97196654 SVGHMI: cosmetic fixes in button widget
Edouard Tisserant
parents: 3085
diff changeset
    59
       apply "on-dispatch";
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    60
    |             break;
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    61
}
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    62
template "on-dispatch" {
3086
a70a97196654 SVGHMI: cosmetic fixes in button widget
Edouard Tisserant
parents: 3085
diff changeset
    63
    |             if(value ==  «@value») {
a70a97196654 SVGHMI: cosmetic fixes in button widget
Edouard Tisserant
parents: 3085
diff changeset
    64
    apply "jump", mode="transition";
a70a97196654 SVGHMI: cosmetic fixes in button widget
Edouard Tisserant
parents: 3085
diff changeset
    65
    |             }
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    66
}
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    67
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    68
template "fsm", mode="mouse_transition" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    69
    param "position";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    70
    |         switch (this.state) {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    71
    apply "state", mode="mouse_transition" with "position", "$position";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    72
    |         }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    73
}
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    74
template "state", mode="mouse_transition" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    75
    param "position";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    76
    |           case "«@name»":
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    77
    apply "on-mouse[@position = $position]";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    78
    |             break;
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    79
}
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    80
template "on-mouse" {
3086
a70a97196654 SVGHMI: cosmetic fixes in button widget
Edouard Tisserant
parents: 3085
diff changeset
    81
    // up or down state is already assumed because apply statement filters it
a70a97196654 SVGHMI: cosmetic fixes in button widget
Edouard Tisserant
parents: 3085
diff changeset
    82
    apply "jump", mode="transition";
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    83
}
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    84
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    85
template "jump", mode="transition" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    86
    |             this.state = "«@state»";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    87
    |             this.«@state»_action();
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    88
}
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    89
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    90
template "fsm", mode="actions" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    91
    apply "state", mode="actions";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    92
}
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    93
template "state", mode="actions" {
3086
a70a97196654 SVGHMI: cosmetic fixes in button widget
Edouard Tisserant
parents: 3085
diff changeset
    94
    |     «@name»_action(){
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    95
    //| console.log("Entering state «@name»");
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    96
    apply "*", mode="actions";
3086
a70a97196654 SVGHMI: cosmetic fixes in button widget
Edouard Tisserant
parents: 3085
diff changeset
    97
    |     }
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    98
}
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
    99
template "show", mode="actions" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   100
    |         this.display = "«@eltname»";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   101
    |         this.request_animate();
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   102
}
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   103
template "hmi-value", mode="actions" {
3086
a70a97196654 SVGHMI: cosmetic fixes in button widget
Edouard Tisserant
parents: 3085
diff changeset
   104
    |         this.apply_hmi_value(0, «@value»);
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   105
}
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   106
3024
Edouard Tisserant
parents: 3018
diff changeset
   107
template "widget[@type='Button']", mode="widget_class"{
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   108
    const "fsm","exsl:node-set($_button_fsm)";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   109
    | class ButtonWidget extends Widget{
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   110
    |     frequency = 5;
3009
7c6960f09881 Reworked button widget so it uses classes.
usveticic
parents: 3004
diff changeset
   111
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   112
    |     display = "inactive";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   113
    |     state = "init";
3059
e0db3f6a5f39 Button and toggle reworked to use animate and dispatch
usveticic
parents: 3058
diff changeset
   114
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   115
    |     dispatch(value) {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   116
    // |         console.log("dispatch"+value);
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   117
    apply "$fsm", mode="dispatch_transition";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   118
    |     }
3056
827bf284feec Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents: 3024
diff changeset
   119
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   120
    |     onmouseup(evt) {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   121
    |         svg_root.removeEventListener("pointerup", this.bound_onmouseup, true);
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   122
    // |         console.log("onmouseup");
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   123
    apply "$fsm", mode="mouse_transition" with "position", "'up'";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   124
    |     }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   125
    |     onmousedown(evt) {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   126
    |         svg_root.addEventListener("pointerup", this.bound_onmouseup, true);
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   127
    // |         console.log("onmousedown");
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   128
    apply "$fsm", mode="mouse_transition" with "position", "'down'";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   129
    |     }
3009
7c6960f09881 Reworked button widget so it uses classes.
usveticic
parents: 3004
diff changeset
   130
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   131
    apply "$fsm", mode="actions";
3056
827bf284feec Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents: 3024
diff changeset
   132
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   133
    |     animate(){
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   134
    |         if (this.active_elt && this.inactive_elt) {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   135
    foreach "str:split('active inactive')" {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   136
    |             if(this.display == "«.»")
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   137
    |                 this.«.»_elt.style.display = "";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   138
    |             else
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   139
    |                 this.«.»_elt.style.display = "none";
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   140
    }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   141
    |         }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   142
    |     }
3009
7c6960f09881 Reworked button widget so it uses classes.
usveticic
parents: 3004
diff changeset
   143
3085
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   144
    |     init() {
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   145
    |         this.bound_onmouseup = this.onmouseup.bind(this);
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   146
    |         this.element.addEventListener("pointerdown", this.onmousedown.bind(this));
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   147
    |     }
6b1b23971960 SVGHMI: Rewrote button widget.
Edouard Tisserant
parents: 3062
diff changeset
   148
    | }
3024
Edouard Tisserant
parents: 3018
diff changeset
   149
}
3009
7c6960f09881 Reworked button widget so it uses classes.
usveticic
parents: 3004
diff changeset
   150
7c6960f09881 Reworked button widget so it uses classes.
usveticic
parents: 3004
diff changeset
   151
2976
99c4521bb844 SVGHMI: Changed widget button handler to element attribute.
dgaberscek
parents: 2961
diff changeset
   152
template "widget[@type='Button']", mode="widget_defs" {
99c4521bb844 SVGHMI: Changed widget button handler to element attribute.
dgaberscek
parents: 2961
diff changeset
   153
    param "hmi_element";
99c4521bb844 SVGHMI: Changed widget button handler to element attribute.
dgaberscek
parents: 2961
diff changeset
   154
    optional_labels("active inactive");
3000
a9a45977bac0 SVGHMI: prefer apply_hmi_value() to change_hmi_value() when possible
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2976
diff changeset
   155
}