svghmi/widget_circularbar.ysl2
author Edouard Tisserant <edouard.tisserant@gmail.com>
Wed, 05 Aug 2020 18:49:29 +0200
branchsvghmi
changeset 3005 ff9ae4f4e3be
parent 2944 2a20038fbea9
child 3045 f6d428330e04
permissions -rw-r--r--
SVGHMI: widgets are not anymore binary relative or absolute, but have a "relativeness".

Because of allowing multiple variables per widget, we must distinguish if individual variables is relative to page, not the whole widget.
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
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
     3
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
     4
template "widget[@type='CircularBar']", mode="widget_defs" {
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
     5
    param "hmi_element";
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
     6
    | frequency: 10,
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
     7
    labels("path");
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
     8
    optional_labels("value min max");
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
     9
    | dispatch: function(value) {
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    10
    |     if(this.value_elt)
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    11
    |         this.value_elt.textContent = String(value);
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    12
    |     let [min,max,start,end] = this.range;
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    13
    |     let [cx,cy] = this.center;
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    14
    |     let [rx,ry] = this.proportions;
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    15
    |     let tip = start + (end-start)*Number(value)/(max-min);
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    16
    |     let size = 0;
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    17
    |     if (tip-start > Math.PI) {
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    18
    |         size = 1;
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    19
    |     } else {
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    20
    |         size = 0;
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    21
    |     }
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    22
    |     this.path_elt.setAttribute('d', "M "+(cx+rx*Math.cos(start))+","+(cy+ry*Math.sin(start))+" A "+rx+","+ry+" 0 "+size+" 1 "+(cx+rx*Math.cos(tip))+","+(cy+ry*Math.sin(tip)));
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    23
    | },
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    24
    | range: undefined,
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    25
    | init: function() {
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    26
    |     let start = Number(this.path_elt.getAttribute('sodipodi:start'));
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    27
    |     let end = Number(this.path_elt.getAttribute('sodipodi:end'));
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    28
    |     let cx = Number(this.path_elt.getAttribute('sodipodi:cx'));
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    29
    |     let cy = Number(this.path_elt.getAttribute('sodipodi:cy'));
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    30
    |     let rx = Number(this.path_elt.getAttribute('sodipodi:rx'));
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    31
    |     let ry = Number(this.path_elt.getAttribute('sodipodi:ry'));
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    32
    |     if (ry == 0) {
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    33
    |         ry = rx;
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    34
    |     }
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    35
    |     if (start > end) {
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    36
    |         end = end + 2*Math.PI;
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    37
    |     }
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    38
    |     let min = this.min_elt ?
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    39
    |               Number(this.min_elt.textContent) :
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    40
    |               this.args.length >= 1 ? this.args[0] : 0;
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    41
    |     let max = this.max_elt ?
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    42
    |               Number(this.max_elt.textContent) :
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    43
    |               this.args.length >= 2 ? this.args[1] : 100;
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    44
    |     this.range = [min, max, start, end];
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    45
    |     this.center = [cx, cy];
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    46
    |     this.proportions = [rx, ry];
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    47
    | },
2a20038fbea9 Added button and circular bar widgets.
dgaberscek
parents:
diff changeset
    48
}