svghmi/widget_circularbar.ysl2
author Edouard Tisserant <edouard.tisserant@gmail.com>
Sun, 02 May 2021 23:01:08 +0200
branchsvghmi
changeset 3232 7bdb766c2a4d
parent 3105 8819c8b66a42
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.
2944
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
     1
// widget_circularbar.ysl2
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
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: 3105
diff changeset
     3
widget_class("CircularBar") {
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
     4
    ||
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
     5
        frequency = 10;
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
     6
        range = undefined;
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
     7
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
     8
        dispatch(value) {
3105
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
     9
            this.display_val = value;
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    10
            this.request_animate();
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    11
        }
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    12
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    13
        animate(){
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    14
            if(this.value_elt)
3105
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    15
                this.value_elt.textContent = String(this.display_val);
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    16
            let [min,max,start,end] = this.range;
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    17
            let [cx,cy] = this.center;
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    18
            let [rx,ry] = this.proportions;
3105
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    19
            let tip = start + (end-start)*Number(this.display_val)/(max-min);
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    20
            let size = 0;
3105
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    21
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    22
            if (tip-start > Math.PI)
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    23
                size = 1;
3105
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    24
            else
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    25
                size = 0;
3105
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    26
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    27
            this.path_elt.setAttribute('d', "M "+(cx+rx*Math.cos(start))+","+(cy+ry*Math.sin(start))+
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    28
                                            " A "+rx+","+ry+
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    29
                                            " 0 "+size+
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    30
                                            " 1 "+(cx+rx*Math.cos(tip))+","+(cy+ry*Math.sin(tip)));
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    31
        }
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    32
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    33
        init() {
3105
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    34
            let [start, end, cx, cy, rx, ry] = ["start", "end", "cx", "cy", "rx", "ry"].
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    35
                map(tag=>Number(this.path_elt.getAttribute('sodipodi:'+tag)))
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    36
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    37
            if (ry == 0) 
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    38
                ry = rx;
3105
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    39
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    40
            if (start > end)
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    41
                end = end + 2*Math.PI;
3105
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    42
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    43
            let [min,max] = [[this.min_elt,0],[this.max_elt,100]].map(([elt,def],i)=>elt?
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    44
                Number(elt.textContent) :
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    45
                this.args.length >= i+1 ? this.args[i] : def);
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    46
3045
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    47
            this.range = [min, max, start, end];
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    48
            this.center = [cx, cy];
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    49
            this.proportions = [rx, ry];
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    50
        }
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    51
    ||
f6d428330e04 All widgets reworked to use widget class and animate function if needed
usveticic
parents: 2944
diff changeset
    52
}
2944
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    53
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: 3105
diff changeset
    54
widget_defs("CircularBar") {
2944
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    55
    param "hmi_element";
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    56
    labels("path");
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    57
    optional_labels("value min max");
3105
8819c8b66a42 SVGHMI: CircularBar: use animate() + cormetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3045
diff changeset
    58
}