-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnapPoint.qml
321 lines (306 loc) · 8.69 KB
/
SnapPoint.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import QtQuick 2.12
import QtQuick.Controls 1.4
import QtQuick.Shapes 1.15
Item{
id: anchor
property var objColor: "red"
property var name: null
property var type: null
property var index: null
property var rMax: 1000
property var snappedPoi: null
property var origin: null
property var container: null
property var target: null
property var done: []
property var doneSim: []
property var action: container.action
property var time: null
//Scaling for variable display
property var k: map.width/2500
z:30
opacity: .5
Rectangle{
opacity: .5
id: dragPoint
x:-width/2
y:-height/2
width: mouseArea.enabled ? 50*k : 25*k
height: width
radius: width/2
color: "red"
border.color: objColor
border.width:width/3
}
onSnappedPoiChanged: {
if(snappedPoi !== null){
name = snappedPoi.name
index = snappedPoi.index
type = snappedPoi.type
updateParam()
}
}
onActionChanged: {
if(action.includes("Move")){
mouseArea.enabled = true
}
else{
mouseArea.enabled = false
snapRect.visible = false
snappedPoi=origin
doSnap(origin)
//movedPois.removePoi(origin.type, origin.index, objColor)
}
}
Component.onDestruction: {
//if(origin !== null)
//movedPois.removePoi(origin.type, origin.index, objColor)
}
Component.onCompleted: {
snappedPoi = origin
}
MouseArea {
id:mouseArea
anchors.fill: dragPoint
enabled: false
drag.target: parent
drag.axis: Drag.XAndYAxis
onPressed: {
if(time === null || time === -1){
var d = new Date();
time = d.getTime();
}
container.selected(true)
}
onReleased: {
doSnap()
}
}
Item{
id: snapPoint
x:x
y:y
}
Rectangle{
id: snapRect
color: objColor
width: 35*k
height: width
radius: width/2
x:snapPoint.x-width/2
y:snapPoint.y-height/2
z:-11
opacity: .8
visible: false
}
/*
Label{
z:30
id: actionDisplay
text:action+" "+param.replace("_"," ")
x:snapPoint.x-snapRect.width
y:snapPoint.y-3*snapRect.height
font.bold: true
font.pixelSize: 40
style: Text.Outline
styleColor: "black"
color: "white"
}*/
Item{
id: startPoint
x:origin.x-anchor.x-width/2
y:origin.y-anchor.y-height/2
}
Rectangle{
id: startRect
color: "black"
width: 10*k
height: width
radius: width/2
x:startPoint.x-width/2
y:startPoint.y-height/2
z:-1
opacity: 1
visible: true
}
Shape {
anchors.fill: parent
z: -10
ShapePath {
strokeWidth: 5*k
strokeColor: objColor
strokeStyle: ShapePath.DashLine
dashPattern: [ 1, 3 ]
startX: startPoint.x; startY: startPoint.y
PathLine { x: dragPoint.x+dragPoint.width/2; y: dragPoint.y+dragPoint.height/2}
}
}
Shape{
id: p
anchors.fill: parent
z: -10
visible: ((startPoint.x - endX)**2 + (startPoint.y - endY)**2) > d**2
property var endX: dragPoint.x+dragPoint.width/2
property var endY: dragPoint.y+dragPoint.height/2
property var angle: Math.atan2(startPoint.y-endY,startPoint.x-endX)
property var d: 40*k
ShapePath {
strokeWidth: 5*k
strokeColor: "black"
fillColor: objColor
startX: p.endX+20*k*Math.cos(p.angle); startY: p.endY+20*k*Math.sin(p.angle)
PathLine { x: p.endX+p.d*Math.cos(p.angle+Math.PI/12); y: p.endY+p.d*Math.sin(p.angle+Math.PI/12)}
PathLine { x: p.endX+p.d*Math.cos(p.angle-Math.PI/12); y: p.endY+p.d*Math.sin(p.angle-Math.PI/12)}
PathLine { x: p.endX+15*k*Math.cos(p.angle); y: p.endY+15*k*Math.sin(p.angle)}
}
}
onXChanged: {
if (mouseArea.pressed){
checkSnap()
}
}
onYChanged: {
if (mouseArea.pressed){
checkSnap()
}
}
function getCoord(){
var imx = x/pixScale // /map.paintedWidth * map.sourceSize.width;
var imy = y/pixScale + map.offset // /map.paintedHeight * map.sourceSize.height;
return parseInt(imx)+','+parseInt(imy)
}
function checkSnap(){
var dMin=rMax*rMax
var parentPois = []
for(var i=0;i<parent.listPoints.length;i++){
if(snappedPoi === null){
parentPois.push(parent.listPoints[i].name)
}
}
var tempSnap = null
for (var i=0;i<pois.children.length;i++){
if(parentPois.includes(pois.children[i].name) || pois.children[i].type === "screw")
continue
var d = Math.pow(pois.children[i].x-x,2)+Math.pow(pois.children[i].y-y,2)
if(d < dMin){
dMin=d
snapRect.visible = true
snapPoint.x = pois.children[i].x-x
snapPoint.y = pois.children[i].y-y
tempSnap = pois.children[i]
}
}
if(dMin === rMax*rMax){
snapRect.visible = false
snappedPoi=null
//movedPois.removePoi(origin.type, origin.index,objColor)
}
else{
if(tempSnap !== snappedPoi){
snappedPoi = tempSnap
}
//movedPois.updatePoi(origin.type, origin.index,snappedPoi.x,snappedPoi.y,objColor)
}
}
function doSnap(){
var targetx = x + snapPoint.x
var targety = y + snapPoint.y
snapRect.visible = false
if (snappedPoi !== null){
x = snappedPoi.x
y = snappedPoi.y
}
}
function updateParam(){
timerUpdateActions.restart()
/*if(action === "Move"){
param = origin.name +" to "+snappedPoi.name
}
else
param = snappedPoi.name*/
}
onOriginChanged:{
updateParam()
if(origin === null){
//movedPois.removePoi(type, index, objColor)
try{
anchor.destroy()
}
catch(err) {
console.log(err.message)
}
return
}
}
function getAction(){
var actions=[]
target = snappedPoi.name
for(var i=0;i<action.length;i++){
var a={}
a.img1 = "none_ "
a.name = action[i]
if(a.name.includes("Move")){
if (origin.name === snappedPoi.name){
continue //time = -1
}
target = origin.name +"-"+snappedPoi.name
a.img1 = origin.name
a.img3 = snappedPoi.name
}
else{
target = origin.name
a.img3 = target
}
if(a.name.includes("Inspect")){
a.img2 = "Inspect"
if(!a.name.includes("Move")){
a.img1 = a.name.split("-")[1]+"_ "
}
}
else
a.img2 = a.name
a.target = target
a.targetDisplay = target.replace(/_/g," ").replace('-',' to ')
a.order = i
a.color = container.objColor
a.done = false
if(done.includes(a.name) || doneSim.includes(a.name))
a.done = true
if (time === -1 || time === null){
var d = new Date()
time = d.getTime()
}
a.time = time
actions.push(a)
}
return actions
}
function testDone(act, t){
if(action.includes(act) && target.split("-").includes(t.split("-")[0])){
if(act === "Pull")
origin.pulled = true
if(act === "Push")
origin.pulled = false
if(globalStates.state === "simulation")
doneSim.push(act)
else
done.push(act)
return true
}
return false
}
function poiUpdated(){
if(!mouseArea.pressed){
x = snappedPoi.x
y = snappedPoi.y
}
}
function testDelete(){
if(action.includes("Move"))
if (origin.name === snappedPoi.name)
return true
if(done.length>0)
return true
return false
}
}