svghmi/widget_meter.ysl2
author Edouard Tisserant <edouard.tisserant@gmail.com>
Sun, 02 May 2021 23:01:08 +0200
branchsvghmi
changeset 3232 7bdb766c2a4d
parent 3104 14d15712fcca
child 3241 fe945f1f48b7
permissions -rw-r--r--
SVGHMI: In order to allow widget signature and description to coexist in same ysl2 file, introduced widget_class, widget_defs to declare widget codegen templates and gen_index_xhtml to mark templates that are only usefull in gen_index_xhtml.xslt.
2883
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
     1
// widget_meter.ysl2
2792
0c0d3895b036 SVGHMI: moved/fixed some templates, avoided namespace problems, added parsing of HMI:* inkscape labels
Edouard Tisserant
parents: 2791
diff changeset
     2
3232
7bdb766c2a4d SVGHMI: In order to allow widget signature and description to coexist in same ysl2 file, introduced widget_class, widget_defs to declare widget codegen templates and gen_index_xhtml to mark templates that are only usefull in gen_index_xhtml.xslt.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3104
diff changeset
     3
widget_class("Metter"){
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
     4
    ||
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
     5
        frequency = 10;
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
     6
        origin = undefined;
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
     7
        range = undefined;
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
     8
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
     9
        dispatch(value) {
3104
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    10
            this.display_val = value;
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    11
            this.request_animate();
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    12
        }
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    13
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    14
        animate(){
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    15
            if(this.value_elt)
3104
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    16
                this.value_elt.textContent = String(this.display_val);
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    17
            let [min,max,totallength] = this.range;
3104
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    18
            let length = Math.max(0,Math.min(totallength,(Number(this.display_val)-min)*totallength/(max-min)));
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    19
            let tip = this.range_elt.getPointAtLength(length);
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    20
            this.needle_elt.setAttribute('d', "M "+this.origin.x+","+this.origin.y+" "+tip.x+","+tip.y);
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    21
        }
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    22
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    23
        init() {
3104
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    24
            let [min,max] = [[this.min_elt,0],[this.max_elt,100]].map(([elt,def],i)=>elt?
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    25
                Number(elt.textContent) :
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    26
                this.args.length >= i+1 ? this.args[i] : def);
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    27
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    28
            this.range = [min, max, this.range_elt.getTotalLength()]
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    29
            this.origin = this.needle_elt.getPointAtLength(0);
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    30
        }
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    31
    ||
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    32
}
2790
8fab1886ebec SVGHI: compute hmitree variables ordered index in xslt
Edouard Tisserant
parents: 2789
diff changeset
    33
3232
7bdb766c2a4d SVGHMI: In order to allow widget signature and description to coexist in same ysl2 file, introduced widget_class, widget_defs to declare widget codegen templates and gen_index_xhtml to mark templates that are only usefull in gen_index_xhtml.xslt.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3104
diff changeset
    34
widget_defs("Meter") {
2883
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
    35
    param "hmi_element";
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
    36
    labels("needle range");
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
    37
    optional_labels("value min max");
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
    38
}
2810
63b9a37b73c7 SVGHMI: various insignificant code moves, commenting and typos fixes.
Edouard Tisserant
parents: 2808
diff changeset
    39
63b9a37b73c7 SVGHMI: various insignificant code moves, commenting and typos fixes.
Edouard Tisserant
parents: 2808
diff changeset
    40