author | Edouard Tisserant |
Thu, 03 Sep 2020 11:16:08 +0200 | |
branch | svghmi |
changeset 3052 | ffce85221ea5 |
parent 3045 | f6d428330e04 |
child 3056 | 827bf284feec |
permissions | -rw-r--r-- |
3012 | 1 |
// widget_slider.ysl2 |
2 |
||
3 |
template "widget[@type='Slider']", mode="widget_class" |
|
4 |
|| |
|
5 |
class SliderWidget extends Widget{ |
|
6 |
frequency = 5; |
|
7 |
range = undefined; |
|
8 |
fi = undefined; |
|
3045
f6d428330e04
All widgets reworked to use widget class and animate function if needed
usveticic
parents:
3021
diff
changeset
|
9 |
svg_dist = undefined; |
3012 | 10 |
drag = false; |
11 |
enTimer = false; |
|
12 |
||
13 |
dispatch(value) { |
|
3020
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
14 |
if(this.value_elt) |
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
15 |
this.value_elt.textContent = String(value); |
3012 | 16 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
17 |
this.update_DOM(value, this.handle_elt); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
18 |
|
3012 | 19 |
} |
20 |
||
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
21 |
last_drag = false; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
22 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
23 |
update_DOM(value, elt){ |
3012 | 24 |
let [min,max,start,totallength] = this.range; |
25 |
let length = Math.max(0,Math.min(totallength,(Number(value)-min)*totallength/(max-min))); |
|
26 |
let tip = this.range_elt.getPointAtLength(length); |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
27 |
elt.setAttribute('transform',"translate("+(tip.x-start.x)+","+(tip.y-start.y)+")"); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
28 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
29 |
if(this.setpoint_elt != undefined){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
30 |
if(this.last_drag!= this.drag){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
31 |
if(this.drag){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
32 |
this.setpoint_elt.setAttribute("style", this.setpoint_style); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
33 |
}else{ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
34 |
this.setpoint_elt.setAttribute("style", "display:none"); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
35 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
36 |
this.last_drag = this.drag; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
37 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
38 |
} |
3012 | 39 |
} |
40 |
||
41 |
on_release(evt) { |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
42 |
window.removeEventListener("touchmove", this.on_bound_drag, true); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
43 |
window.removeEventListener("mousemove", this.on_bound_drag, true); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
44 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
45 |
window.removeEventListener("mouseup", this.bound_on_release, true) |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
46 |
window.removeEventListener("touchend", this.bound_on_release, true); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
47 |
window.removeEventListener("touchcancel", this.bound_on_release, true); |
3012 | 48 |
if(this.drag){ |
49 |
this.drag = false; |
|
50 |
} |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
51 |
this.update_position(evt); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
52 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
53 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
54 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
55 |
on_drag(evt){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
56 |
if(this.enTimer && this.drag){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
57 |
this.update_position(evt); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
58 |
//reset timer |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
59 |
this.enTimer = false; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
60 |
setTimeout("{hmi_widgets['"+this.element_id+"'].enTimer = true;}", 100); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
61 |
} |
3012 | 62 |
} |
63 |
||
64 |
update_position(evt){ |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
65 |
var html_dist = 0; |
3012 | 66 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
67 |
//calculate size of widget in html |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
68 |
var range_borders = this.range_elt.getBoundingClientRect(); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
69 |
var range_length = Math.sqrt( range_borders.height*range_borders.height + range_borders.width*range_borders.width ); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
70 |
var [minX,minY,maxX,maxY] = [range_borders.left,range_borders.bottom,range_borders.right,range_borders.top]; |
3012 | 71 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
72 |
//get range and mouse coordinates |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
73 |
var mouseX = undefined; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
74 |
var mouseY = undefined; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
75 |
if (evt.type.startsWith("touch")){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
76 |
mouseX = Math.ceil(evt.touches[0].clientX); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
77 |
mouseY = Math.ceil(evt.touches[0].clientY); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
78 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
79 |
else{ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
80 |
mouseX = evt.pageX; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
81 |
mouseY = evt.pageY; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
82 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
83 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
84 |
//get handle distance from mouse position |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
85 |
if (minX > mouseX && minY < mouseY){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
86 |
html_dist = 0; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
87 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
88 |
else if (maxX < mouseX && maxY > mouseY){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
89 |
html_dist = range_length; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
90 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
91 |
else{ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
92 |
// calculate distace |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
93 |
if(this.fi > 0.7){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
94 |
html_dist = (minY - mouseY)/Math.sin(this.fi); |
3012 | 95 |
} |
96 |
else{ |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
97 |
html_dist = (mouseX - minX)/Math.cos(this.fi); |
3012 | 98 |
} |
99 |
||
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
100 |
//check if in range |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
101 |
if (html_dist > range_length){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
102 |
html_dist = range_length; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
103 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
104 |
else if (html_dist < 0){ |
3012 | 105 |
html_dist = 0; |
106 |
} |
|
107 |
||
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
108 |
} |
3020
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
109 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
110 |
this.svg_dist=Math.ceil((html_dist/range_length)*this.range[1]); |
3020
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
111 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
112 |
this.apply_hmi_value(0, this.svg_dist); |
3020
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
113 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
114 |
// update ghost cursor |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
115 |
if(this.setpoint_elt != undefined){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
116 |
this.request_animate(); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
117 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
118 |
} |
3020
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
119 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
120 |
animate(){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
121 |
this.update_DOM(this.svg_dist, this.setpoint_elt); |
3012 | 122 |
} |
123 |
||
124 |
on_select(evt){ |
|
125 |
this.drag = true; |
|
126 |
this.enTimer = true; |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
127 |
window.addEventListener("touchmove", this.on_bound_drag, true); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
128 |
window.addEventListener("mousemove", this.on_bound_drag, true); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
129 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
130 |
window.addEventListener("mouseup", this.bound_on_release, true) |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
131 |
window.addEventListener("touchend", this.bound_on_release, true); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
132 |
window.addEventListener("touchcancel", this.bound_on_release, true); |
3012 | 133 |
this.update_position(evt); |
134 |
} |
|
135 |
||
136 |
init() { |
|
137 |
let min = this.min_elt ? |
|
138 |
Number(this.min_elt.textContent) : |
|
139 |
this.args.length >= 1 ? this.args[0] : 0; |
|
140 |
let max = this.max_elt ? |
|
141 |
Number(this.max_elt.textContent) : |
|
142 |
this.args.length >= 2 ? this.args[1] : 100; |
|
143 |
||
144 |
this.range = [min, max, this.range_elt.getPointAtLength(0),this.range_elt.getTotalLength()]; |
|
145 |
let start = this.range_elt.getPointAtLength(0); |
|
146 |
let end = this.range_elt.getPointAtLength(this.range_elt.getTotalLength()); |
|
147 |
this.fi = Math.atan2(start.y-end.y, end.x-start.x); |
|
148 |
||
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
149 |
this.bound_on_select = this.on_select.bind(this); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
150 |
this.bound_on_release = this.on_release.bind(this); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
151 |
this.on_bound_drag = this.on_drag.bind(this); |
3012 | 152 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
153 |
this.element.addEventListener("mousedown", this.bound_on_select); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
154 |
this.element.addEventListener("touchstart", this.bound_on_select); |
3012 | 155 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
156 |
if(this.setpoint_elt != undefined){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
157 |
this.setpoint_style = this.setpoint_elt.getAttribute("style"); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
158 |
this.setpoint_elt.setAttribute("style", "display:none"); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
159 |
} |
3012 | 160 |
|
161 |
} |
|
162 |
} |
|
163 |
|| |
|
164 |
||
165 |
template "widget[@type='Slider']", mode="widget_defs" { |
|
166 |
param "hmi_element"; |
|
167 |
labels("handle range"); |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
168 |
optional_labels("value min max setpoint"); |
3012 | 169 |
|, |
3018 | 170 |
} |