author | Edouard Tisserant |
Mon, 10 Aug 2020 14:00:20 +0200 | |
branch | svghmi |
changeset 3020 | 895bbeced72d |
parent 3018 | 22b969b409b0 |
child 3021 | 49799de67540 |
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; |
|
9 |
drag = false; |
|
10 |
enTimer = false; |
|
3020
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
11 |
svg_dist = 0 |
3012 | 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 |
|
3020
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
17 |
this.handle_position(value); |
3012 | 18 |
} |
19 |
||
20 |
handle_position(value){ |
|
21 |
let [min,max,start,totallength] = this.range; |
|
22 |
let length = Math.max(0,Math.min(totallength,(Number(value)-min)*totallength/(max-min))); |
|
23 |
let tip = this.range_elt.getPointAtLength(length); |
|
24 |
this.handle_elt.setAttribute('transform',"translate("+(tip.x-start.x)+","+(tip.y-start.y)+")"); |
|
25 |
} |
|
26 |
||
27 |
on_release(evt) { |
|
28 |
if(this.drag){ |
|
29 |
this.drag = false; |
|
30 |
} |
|
31 |
} |
|
32 |
||
33 |
update_position(evt){ |
|
3020
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
34 |
if(this.drag){ |
3012 | 35 |
var html_dist = 0; |
36 |
||
37 |
//calculate size of widget in html |
|
38 |
var range_borders = this.range_elt.getBoundingClientRect(); |
|
39 |
var range_length = Math.sqrt( range_borders.height*range_borders.height + range_borders.width*range_borders.width ); |
|
40 |
var [minX,minY,maxX,maxY] = [range_borders.left,range_borders.bottom,range_borders.right,range_borders.top]; |
|
41 |
||
42 |
//get range and mouse coordinates |
|
43 |
var mouseX = undefined; |
|
44 |
var mouseY = undefined; |
|
45 |
if (evt.type.startsWith("touch")){ |
|
46 |
mouseX = Math.ceil(evt.touches[0].clientX); |
|
47 |
mouseY = Math.ceil(evt.touches[0].clientY); |
|
48 |
} |
|
49 |
else{ |
|
50 |
mouseX = evt.pageX; |
|
51 |
mouseY = evt.pageY; |
|
52 |
} |
|
53 |
||
54 |
//get handle distance from mouse position |
|
55 |
if (minX > mouseX && minY < mouseY){ |
|
56 |
html_dist = 0; |
|
57 |
} |
|
58 |
else if (maxX < mouseX && maxY > mouseY){ |
|
59 |
html_dist = range_length; |
|
60 |
} |
|
61 |
else{ |
|
62 |
// calculate distace |
|
63 |
if(this.fi > 0.7){ |
|
64 |
html_dist = (minY - mouseY)/Math.sin(this.fi); |
|
65 |
} |
|
66 |
else{ |
|
67 |
html_dist = (mouseX - minX)/Math.cos(this.fi); |
|
68 |
} |
|
69 |
||
70 |
//check if in range |
|
71 |
if (html_dist > range_length){ |
|
72 |
html_dist = range_length; |
|
73 |
} |
|
74 |
else if (html_dist < 0){ |
|
75 |
html_dist = 0; |
|
76 |
} |
|
77 |
} |
|
3020
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
78 |
|
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
79 |
this.svg_dist=(html_dist/range_length)*this.range[1]; |
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
80 |
|
3012 | 81 |
//redraw handle |
3020
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
82 |
//this.handle_position(svg_dist=(html_dist/range_length)*this.range[1]); |
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
83 |
//this.value_elt.textContent = String(Math.ceil(svg_dist)); |
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
84 |
|
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
85 |
if(this.enTimer){ |
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
86 |
this.apply_hmi_value(0, Math.ceil(this.svg_dist)); |
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
87 |
|
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
88 |
// TODO : update ghost cursor and call this.request_animate() |
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
89 |
|
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
90 |
//reset timer |
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
91 |
this.enTimer = false; |
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
92 |
setTimeout("{hmi_widgets['"+this.element_id+"'].enTimer = true;}", 100); |
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
93 |
} |
3012 | 94 |
} |
95 |
} |
|
96 |
||
97 |
on_select(evt){ |
|
98 |
this.drag = true; |
|
99 |
this.enTimer = true; |
|
100 |
this.update_position(evt); |
|
101 |
} |
|
102 |
||
103 |
init() { |
|
104 |
let min = this.min_elt ? |
|
105 |
Number(this.min_elt.textContent) : |
|
106 |
this.args.length >= 1 ? this.args[0] : 0; |
|
107 |
let max = this.max_elt ? |
|
108 |
Number(this.max_elt.textContent) : |
|
109 |
this.args.length >= 2 ? this.args[1] : 100; |
|
110 |
||
111 |
this.range = [min, max, this.range_elt.getPointAtLength(0),this.range_elt.getTotalLength()]; |
|
112 |
let start = this.range_elt.getPointAtLength(0); |
|
113 |
let end = this.range_elt.getPointAtLength(this.range_elt.getTotalLength()); |
|
114 |
this.fi = Math.atan2(start.y-end.y, end.x-start.x); |
|
115 |
||
116 |
||
117 |
this.handle_elt.addEventListener("touchstart", hmi_widgets[this.element_id].on_select.bind(this)); |
|
118 |
this.handle_elt.addEventListener("mousedown", hmi_widgets[this.element_id].on_select.bind(this)); |
|
119 |
this.element.addEventListener("mousedown", hmi_widgets[this.element_id].on_select.bind(this)); |
|
120 |
||
121 |
window.addEventListener("touchmove", hmi_widgets[this.element_id].update_position.bind(this)); |
|
122 |
window.addEventListener("mousemove", hmi_widgets[this.element_id].update_position.bind(this)); |
|
123 |
||
124 |
window.addEventListener("mouseup", hmi_widgets[this.element_id].on_release.bind(this)) |
|
125 |
window.addEventListener("touchend", hmi_widgets[this.element_id].on_release.bind(this)); |
|
126 |
window.addEventListener("touchcancel", hmi_widgets[this.element_id].on_release.bind(this)); |
|
127 |
||
128 |
} |
|
129 |
} |
|
130 |
|| |
|
131 |
||
132 |
template "widget[@type='Slider']", mode="widget_defs" { |
|
133 |
param "hmi_element"; |
|
134 |
labels("handle range"); |
|
135 |
optional_labels("value min max"); |
|
136 |
|, |
|
3018 | 137 |
} |