-
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathPathCursor.js
More file actions
350 lines (293 loc) · 13.1 KB
/
Copy pathPathCursor.js
File metadata and controls
350 lines (293 loc) · 13.1 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright 2020 WICKLETS LLC
*
* This file is part of Wick Engine.
*
* Wick Engine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wick Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Wick Engine. If not, see <https://www.gnu.org/licenses/>.
*/
Wick.Tools.PathCursor = class extends Wick.Tool {
constructor () {
super();
this.name = 'pathcursor';
this.SELECTION_TOLERANCE = 3;
this.CURSOR_DEFAULT = 'cursors/default.png';
this.CURSOR_SEGMENT = 'cursors/segment.png';
this.CURSOR_CURVE = 'cursors/curve.png';
this.HOVER_PREVIEW_SEGMENT_STROKE_COLOR = 'rgba(100,150,255,1.0)';
this.HOVER_PREVIEW_SEGMENT_STROKE_WIDTH = 1.5;
this.HOVER_PREVIEW_SEGMENT_FILL_COLOR = '#ffffff';
this.HOVER_PREVIEW_SEGMENT_RADIUS = 5;
this.HOVER_PREVIEW_CURVE_STROKE_WIDTH = 2;
this.HOVER_PREVIEW_CURVE_STROKE_COLOR = this.HOVER_PREVIEW_SEGMENT_STROKE_COLOR;
this.hitResult = new this.paper.HitResult();
this.draggingCurve = new this.paper.Curve();
this.draggingSegment = new this.paper.Segment();
this.hoverPreview = new this.paper.Item({insert:false});
this.detailedEditing = null;
this.currentCursorIcon = '';
}
get doubleClickEnabled () {
return true;
}
get cursor () {
return 'url("'+this.currentCursorIcon+'") 32 32, auto';
}
onActivate (e) {
}
onDeactivate (e) {
this._leaveDetailedEditing();
}
onMouseMove (e) {
super.onMouseMove(e);
// Remove the hover preview, a new one will be generated if needed
this.hoverPreview.remove();
// Find the thing that is currently under the cursor.
this.hitResult = this._updateHitResult(e);
// Update the image being used for the cursor
this._setCursor(this._getCursor());
// Regen hover preview
if(this.hitResult.type === 'segment' && !this.hitResult.item.data.isSelectionBoxGUI) {
// Hovering over a segment, draw a circle where the segment is
this.hoverPreview = new this.paper.Path.Circle(this.hitResult.segment.point, this.HOVER_PREVIEW_SEGMENT_RADIUS/this.paper.view.zoom);
this.hoverPreview.strokeColor = this.HOVER_PREVIEW_SEGMENT_STROKE_COLOR;
this.hoverPreview.strokeWidth = this.HOVER_PREVIEW_SEGMENT_STROKE_WIDTH;
this.hoverPreview.fillColor = this.HOVER_PREVIEW_SEGMENT_FILL_COLOR;
} else if (this.hitResult.type === 'curve' && !this.hitResult.item.data.isSelectionBoxGUI) {
// Hovering over a curve, render a copy of the curve that can be bent
this.hoverPreview = new this.paper.Path();
this.hoverPreview.strokeWidth = this.HOVER_PREVIEW_CURVE_STROKE_WIDTH;
this.hoverPreview.strokeColor = this.HOVER_PREVIEW_CURVE_STROKE_COLOR;
this.hoverPreview.add(new this.paper.Point(this.hitResult.location.curve.point1));
this.hoverPreview.add(new this.paper.Point(this.hitResult.location.curve.point2));
this.hoverPreview.segments[0].handleOut = this.hitResult.location.curve.handle1;
this.hoverPreview.segments[1].handleIn = this.hitResult.location.curve.handle2;
}
this.hoverPreview.data.wickType = 'gui';
}
onMouseDown (e) {
super.onMouseDown(e);
if(!e.modifiers) e.modifiers = {};
this.hitResult = this._updateHitResult(e);
if (this.detailedEditing !== null && !(
this.hitResult.item || (
this.hitResult.type && this.hitResult.type.startsWith('handle')))) {
// Clicked neither on the currently edited path nor on a handle.
this._leaveDetailedEditing();
}
if (this.hitResult.item && this.hitResult.type === 'curve') {
// Clicked a curve, start dragging it
this.draggingCurve = this.hitResult.location.curve;
} else if (this.hitResult.item && this.hitResult.type === 'segment') {
if(e.modifiers.alt ||
e.modifiers.command ||
e.modifiers.control ||
e.modifiers.option ||
e.modifiers.shift) {
this.hitResult.segment.remove();
}
}
}
onDoubleClick (e) {
this.hitResult = this._updateHitResult(e);
if (this.detailedEditing == null) {
// If detailed editing is off, turn it on for this path.
this.detailedEditing = this.hitResult.item;
this.detailedEditing.setFullySelected(true);
} else if (!this.hitResult.item) {
// If detailed editing is on for some path, but the user
// double clicked somewhere else, turn it off.
this._leaveDetailedEditing();
} else if (this.hitResult.item && this.hitResult.type === 'curve') {
var location = this.hitResult.location;
var path = this.hitResult.item;
var addedPoint = path.insert(location.index + 1, e.point);
if (!e.modifiers.shift) {
addedPoint.smooth()
var handleInMag = Math.sqrt(
addedPoint.handleIn.x*addedPoint.handleIn.x+
addedPoint.handleIn.y+addedPoint.handleIn.y)
var handleOutMag = Math.sqrt(
addedPoint.handleOut.x*addedPoint.handleOut.x+
addedPoint.handleOut.y+addedPoint.handleOut.y)
if(handleInMag > handleOutMag) {
var avgMag = handleOutMag;
addedPoint.handleIn.x = -addedPoint.handleOut.x*1.5;
addedPoint.handleIn.y = -addedPoint.handleOut.y*1.5;
addedPoint.handleOut.x *= 1.5;
addedPoint.handleOut.y *= 1.5;
} else {
var avgMag = handleInMag;
addedPoint.handleOut.x = -addedPoint.handleIn.x*1.5;
addedPoint.handleOut.y = -addedPoint.handleIn.y*1.5;
addedPoint.handleIn.x *= 1.5;
addedPoint.handleIn.y *= 1.5;
}
}
if (this.detailedEditing !== null) {
path.setFullySelected(true);
}
} else if (this.hitResult.item && this.hitResult.type === 'segment') {
var hix = this.hitResult.segment.handleIn.x;
var hiy = this.hitResult.segment.handleIn.y;
var hox = this.hitResult.segment.handleOut.x;
var hoy = this.hitResult.segment.handleOut.y;
if(hix === 0 && hiy === 0 && hix === 0 && hiy === 0) {
this.hitResult.segment.smooth();
} else {
this.hitResult.segment.handleIn.x = 0;
this.hitResult.segment.handleIn.y = 0;
this.hitResult.segment.handleOut.x = 0;
this.hitResult.segment.handleOut.y = 0;
}
}
}
onMouseDrag (e) {
if(!e.modifiers) e.modifiers = {};
if(this.hitResult.item && this.hitResult.type === 'segment') {
// We're dragging an individual point, so move the point.
this.hitResult.segment.point = this.hitResult.segment.point.add(e.delta);
this.hoverPreview.position = this.hitResult.segment.point;
} else if(this.hitResult.item && this.hitResult.type === 'curve') {
// We're dragging a curve, so bend the curve.
var segment1 = this.draggingCurve.segment1;
var segment2 = this.draggingCurve.segment2;
var handleIn = segment1.handleOut;
var handleOut = segment2.handleIn;
if(handleIn.x === 0 && handleIn.y === 0) {
handleIn.x = (segment2.point.x - segment1.point.x) / 4;
handleIn.y = (segment2.point.y - segment1.point.y) / 4;
}
if(handleOut.x === 0 && handleOut.y === 0) {
handleOut.x = (segment1.point.x - segment2.point.x) / 4;
handleOut.y = (segment1.point.y - segment2.point.y) / 4;
}
handleIn.x += e.delta.x;
handleIn.y += e.delta.y;
handleOut.x += e.delta.x;
handleOut.y += e.delta.y;
// Update the hover preview to match the curve we just changed
this.hoverPreview.segments[0].handleOut = this.draggingCurve.handle1;
this.hoverPreview.segments[1].handleIn = this.draggingCurve.handle2;
}
if (this.hitResult.type && this.hitResult.type.startsWith('handle')) {
var otherHandle;
var handle;
if(this.hitResult.type === 'handle-in') {
handle = this.hitResult.segment.handleIn;
otherHandle = this.hitResult.segment.handleOut;
} else if (this.hitResult.type === 'handle-out') {
handle = this.hitResult.segment.handleOut;
otherHandle = this.hitResult.segment.handleIn;
}
handle.x += e.delta.x;
handle.y += e.delta.y;
if (!e.modifiers.shift) {
otherHandle.x -= e.delta.x;
otherHandle.y -= e.delta.y;
}
}
}
onMouseUp (e) {
if (this.hitResult.type === 'segment' || this.hitResult.type === 'curve') {
this.fireEvent({eventName: 'canvasModified', actionName:'pathcursor'});
}
}
onKeyDown(e) {
if (this.detailedEditing !== null && e.key == "<") {
var wick = Wick.ObjectCache.getObjectByUUID(
this._getWickUUID(this.detailedEditing));
var path = wick._view._item;
path.closed = !path.closed;
this.fireEvent('canvasModified');
}
}
_updateHitResult (e) {
var newHitResult = this.paper.project.hitTest(e.point, {
fill: true,
stroke: true,
curves: true,
segments: true,
handles: this.detailedEditing !== null,
tolerance: this.SELECTION_TOLERANCE,
match: (result => {
return result.item !== this.hoverPreview
&& !result.item.data.isBorder;
}),
});
if(!newHitResult) newHitResult = new this.paper.HitResult();
if (this.detailedEditing !== null) {
if (this._getWickUUID(newHitResult.item) !== this._getWickUUID(this.detailedEditing)) {
// Hits an item, but not the one currently in detail edit - handle as a click with no hit.
return new this.paper.HitResult();
}
if (newHitResult.item && newHitResult.type.startsWith('handle')) {
// If this a click on a handle, do not apply hit type prediction below.
return newHitResult;
}
}
if(newHitResult.item && !newHitResult.item.data.isSelectionBoxGUI) {
// You can't select children of compound paths, you can only select the whole thing.
if (newHitResult.item.parent.className === 'CompoundPath') {
newHitResult.item = newHitResult.item.parent;
}
// You can't select individual children in a group, you can only select the whole thing.
if (newHitResult.item.parent.parent) {
newHitResult.type = 'fill';
while (newHitResult.item.parent.parent) {
newHitResult.item = newHitResult.item.parent;
}
}
// this.paper.js has two names for strokes+curves, we don't need that extra info
if(newHitResult.type === 'stroke') {
newHitResult.type = 'curve';
}
// Mousing over rasters acts the same as mousing over fills.
if(newHitResult.type === 'pixel') {
newHitResult.type = 'fill';
}
}
return newHitResult;
}
_getCursor () {
if(!this.hitResult.item) {
return this.CURSOR_DEFAULT;
} else if (this.hitResult.type === 'curve') {
return this.CURSOR_CURVE;
} else if (this.hitResult.type === 'segment') {
return this.CURSOR_SEGMENT;
}
}
_setCursor (cursor) {
this.currentCursorIcon = cursor;
}
_leaveDetailedEditing () {
if (this.detailedEditing !== null) {
this.paper.project.deselectAll();
this.paper.project.activeLayer.children.forEach(function (child) {
if (child.wick && !child.wick.isSymbol) {
child.fullySelected = false;
}
});
this.detailedEditing = null;
this.fireEvent('canvasModified');
}
}
_getWickUUID (item) {
if (item) {
return item.data.wickUUID;
} else {
return undefined;
}
}
}