author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Thu, 07 Apr 2022 07:40:32 +0200 | |
branch | wxPython4 |
changeset 3447 | 65c5f66e9298 |
parent 3324 | 13779d34293b |
child 3454 | 0b5ab53007a9 |
permissions | -rw-r--r-- |
3324
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
1 |
// widget_pathslider.ysl2 |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
2 |
widget_desc("PathSlider") { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
3 |
longdesc |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
4 |
|| |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
5 |
PathSlider - |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
6 |
|| |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
7 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
8 |
shortdesc > Slide an SVG element along a path by dragging it |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
9 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
10 |
path name="value" accepts="HMI_INT,HMI_REAL" > value |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
11 |
path name="min" count="optional" accepts="HMI_INT,HMI_REAL" > min |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
12 |
path name="max" count="optional" accepts="HMI_INT,HMI_REAL" > max |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
13 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
14 |
arg name="min" count="optional" accepts="int,real" > minimum value |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
15 |
arg name="max" count="optional" accepts="int,real" > maximum value |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
16 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
17 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
18 |
widget_class("PathSlider") { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
19 |
|| |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
20 |
frequency = 10; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
21 |
position = undefined; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
22 |
min = 0; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
23 |
max = 100; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
24 |
scannedPoints = []; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
25 |
pathLength = undefined; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
26 |
precision = undefined; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
27 |
origPt = undefined; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
28 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
29 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
30 |
scanPath() { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
31 |
this.pathLength = this.path_elt.getTotalLength(); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
32 |
this.precision = Math.floor(this.pathLength / 10); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
33 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
34 |
// save linear scan for coarse approximation |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
35 |
for (var scanLength = 0; scanLength <= this.pathLength; scanLength += this.precision) { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
36 |
this.scannedPoints.push([this.path_elt.getPointAtLength(scanLength), scanLength]); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
37 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
38 |
[this.origPt,] = this.scannedPoints[0]; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
39 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
40 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
41 |
closestPoint(point) { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
42 |
var bestPoint, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
43 |
bestLength, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
44 |
bestDistance = Infinity, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
45 |
scanDistance; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
46 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
47 |
// use linear scan for coarse approximation |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
48 |
for (let [scanPoint, scanLength] of this.scannedPoints){ |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
49 |
if ((scanDistance = distance2(scanPoint)) < bestDistance) { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
50 |
bestPoint = scanPoint, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
51 |
bestLength = scanLength, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
52 |
bestDistance = scanDistance; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
53 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
54 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
55 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
56 |
// binary search for more precise estimate |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
57 |
let precision = this.precision / 2; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
58 |
while (precision > 0.5) { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
59 |
var beforePoint, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
60 |
afterPoint, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
61 |
beforeLength, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
62 |
afterLength, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
63 |
beforeDistance, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
64 |
afterDistance; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
65 |
if ((beforeLength = bestLength - precision) >= 0 && |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
66 |
(beforeDistance = distance2(beforePoint = this.path_elt.getPointAtLength(beforeLength))) < bestDistance) { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
67 |
bestPoint = beforePoint, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
68 |
bestLength = beforeLength, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
69 |
bestDistance = beforeDistance; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
70 |
} else if ((afterLength = bestLength + precision) <= this.pathLength && |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
71 |
(afterDistance = distance2(afterPoint = this.path_elt.getPointAtLength(afterLength))) < bestDistance) { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
72 |
bestPoint = afterPoint, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
73 |
bestLength = afterLength, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
74 |
bestDistance = afterDistance; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
75 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
76 |
precision /= 2; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
77 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
78 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
79 |
return [bestPoint, bestLength]; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
80 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
81 |
function distance2(p) { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
82 |
var dx = p.x - point.x, |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
83 |
dy = p.y - point.y; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
84 |
return dx * dx + dy * dy; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
85 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
86 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
87 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
88 |
dispatch(value,oldval, index) { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
89 |
switch(index) { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
90 |
case 0: |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
91 |
this.position = value; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
92 |
break; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
93 |
case 1: |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
94 |
this.min = value; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
95 |
break; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
96 |
case 2: |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
97 |
this.max = value; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
98 |
break; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
99 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
100 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
101 |
this.request_animate(); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
102 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
103 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
104 |
get_current_point(){ |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
105 |
let currLength = this.pathLength * (this.position - this.min) / (this.max - this.min) |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
106 |
return this.path_elt.getPointAtLength(currLength); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
107 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
108 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
109 |
animate(){ |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
110 |
if(this.position == undefined) |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
111 |
return; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
112 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
113 |
let currPt = this.get_current_point(); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
114 |
this.cursor_transform.setTranslate(currPt.x - this.origPt.x, currPt.y - this.origPt.y); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
115 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
116 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
117 |
init() { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
118 |
if(this.args.length == 2) |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
119 |
[this.min, this.max]=this.args; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
120 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
121 |
this.scanPath(); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
122 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
123 |
this.cursor_transform = svg_root.createSVGTransform(); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
124 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
125 |
this.cursor_elt.transform.baseVal.appendItem(this.cursor_transform); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
126 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
127 |
this.cursor_elt.onpointerdown = (e) => this.on_cursor_down(e); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
128 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
129 |
this.bound_drag = this.drag.bind(this); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
130 |
this.bound_drop = this.drop.bind(this); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
131 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
132 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
133 |
start_dragging_from_event(e){ |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
134 |
let clientPoint = new DOMPoint(e.clientX, e.clientY); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
135 |
let point = clientPoint.matrixTransform(this.invctm); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
136 |
let currPt = this.get_current_point(); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
137 |
this.draggingOffset = new DOMPoint(point.x - currPt.x , point.y - currPt.y); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
138 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
139 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
140 |
apply_position_from_event(e){ |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
141 |
let clientPoint = new DOMPoint(e.clientX, e.clientY); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
142 |
let rawPoint = clientPoint.matrixTransform(this.invctm); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
143 |
let point = new DOMPoint(rawPoint.x - this.draggingOffset.x , rawPoint.y - this.draggingOffset.y); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
144 |
let [closestPoint, closestLength] = this.closestPoint(point); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
145 |
let new_position = this.min + (this.max - this.min) * closestLength / this.pathLength; |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
146 |
this.position = Math.round(Math.max(Math.min(new_position, this.max), this.min)); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
147 |
this.apply_hmi_value(0, this.position); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
148 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
149 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
150 |
on_cursor_down(e){ |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
151 |
// get scrollbar -> root transform |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
152 |
let ctm = this.path_elt.getCTM(); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
153 |
// root -> path transform |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
154 |
this.invctm = ctm.inverse(); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
155 |
this.start_dragging_from_event(e); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
156 |
svg_root.addEventListener("pointerup", this.bound_drop, true); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
157 |
svg_root.addEventListener("pointermove", this.bound_drag, true); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
158 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
159 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
160 |
drop(e) { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
161 |
svg_root.removeEventListener("pointerup", this.bound_drop, true); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
162 |
svg_root.removeEventListener("pointermove", this.bound_drag, true); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
163 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
164 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
165 |
drag(e) { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
166 |
this.apply_position_from_event(e); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
167 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
168 |
|| |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
169 |
} |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
170 |
|
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
171 |
widget_defs("PathSlider") { |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
172 |
labels("cursor path"); |
13779d34293b
SVGHMI: Added PathSlider widget
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
173 |
} |