svghmi/widget_meter.ysl2
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 09 Feb 2021 08:11:22 +0100
branchsvghmi
changeset 3132 a1e2eff0bc58
parent 3104 14d15712fcca
child 3232 7bdb766c2a4d
permissions -rw-r--r--
SVGHMI: Add a simple i18n test
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
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
     3
template "widget[@type='Meter']", mode="widget_class"{
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
    class MeterWidget extends Widget{
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
     6
        frequency = 10;
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
     7
        origin = undefined;
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
     8
        range = undefined;
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
     9
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    10
        dispatch(value) {
3104
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    11
            this.display_val = value;
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    12
            this.request_animate();
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
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    15
        animate(){
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    16
            if(this.value_elt)
3104
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    17
                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
    18
            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
    19
            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
    20
            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
    21
            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
    22
        }
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    23
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    24
        init() {
3104
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    25
            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
    26
                Number(elt.textContent) :
14d15712fcca SVGHMI: Meter widget: use animate() + cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    27
                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
    28
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    29
            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
    30
            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
    31
        }
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    32
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    33
    }
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    34
    ||
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2883
diff changeset
    35
}
2790
8fab1886ebec SVGHI: compute hmitree variables ordered index in xslt
Edouard Tisserant
parents: 2789
diff changeset
    36
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
    37
template "widget[@type='Meter']", mode="widget_defs" {
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
    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
    39
    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
    40
    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
    41
}
2810
63b9a37b73c7 SVGHMI: various insignificant code moves, commenting and typos fixes.
Edouard Tisserant
parents: 2808
diff changeset
    42
63b9a37b73c7 SVGHMI: various insignificant code moves, commenting and typos fixes.
Edouard Tisserant
parents: 2808
diff changeset
    43