edouard@2883: // widget_meter.ysl2
Edouard@2792: 
edouard@3241: widget_desc("Meter") {
edouard@3241:     longdesc
edouard@3241:     ||
edouard@3241:     Meter widget moves the end of "needle" labeled path along "range" labeled
edouard@3241:     path, according to value of the single accepted variable.
edouard@3241: 
edouard@3241:     Needle is reduced to a single segment. If "min" a "max" labeled texts
edouard@3241:     are provided, or if first and second argument are given, then they are used
edouard@3241:     as respective minimum and maximum value. Otherwise, value is expected to be
edouard@3241:     in between 0 and 100.
edouard@3241:     ||
edouard@3241: 
edouard@3241:     shortdesc > Moves "needle" along "range"
edouard@3241: 
edouard@3241:     arg name="min" count="optional" accepts="int,real" > minimum value
edouard@3241: 
edouard@3241:     arg name="max" count="optional" accepts="int,real" > maximum value
edouard@3241: 
edouard@3241:     path name="value" accepts="HMI_INT,HMI_REAL" > Value to display
edouard@3241:     
edouard@3241: }
edouard@3241: 
edouard@3275: widget_class("Meter"){
usveticic@3045:     ||
usveticic@3045:         frequency = 10;
usveticic@3045:         origin = undefined;
usveticic@3045:         range = undefined;
usveticic@3045: 
usveticic@3045:         dispatch(value) {
edouard@3104:             this.display_val = value;
edouard@3104:             this.request_animate();
edouard@3104:         }
edouard@3104: 
edouard@3104:         animate(){
usveticic@3045:             if(this.value_elt)
edouard@3104:                 this.value_elt.textContent = String(this.display_val);
usveticic@3045:             let [min,max,totallength] = this.range;
edouard@3104:             let length = Math.max(0,Math.min(totallength,(Number(this.display_val)-min)*totallength/(max-min)));
usveticic@3045:             let tip = this.range_elt.getPointAtLength(length);
usveticic@3045:             this.needle_elt.setAttribute('d', "M "+this.origin.x+","+this.origin.y+" "+tip.x+","+tip.y);
usveticic@3045:         }
usveticic@3045: 
usveticic@3045:         init() {
edouard@3104:             let [min,max] = [[this.min_elt,0],[this.max_elt,100]].map(([elt,def],i)=>elt?
edouard@3104:                 Number(elt.textContent) :
edouard@3104:                 this.args.length >= i+1 ? this.args[i] : def);
edouard@3104: 
usveticic@3045:             this.range = [min, max, this.range_elt.getTotalLength()]
usveticic@3045:             this.origin = this.needle_elt.getPointAtLength(0);
usveticic@3045:         }
usveticic@3045:     ||
usveticic@3045: }
Edouard@2790: 
edouard@3232: widget_defs("Meter") {
edouard@2883:     labels("needle range");
edouard@3361:     optional_labels("min max");
edouard@2883: }
Edouard@2810: 
Edouard@2810: