-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.js
More file actions
429 lines (402 loc) · 12.2 KB
/
tools.js
File metadata and controls
429 lines (402 loc) · 12.2 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
class Tool {
constructor(editor) {
this.editor = editor;
}
start(ptr, target, x, y) {}
move(ptr, target, x, y) {}
end(ptr, target, x, y) {}
drawWidget(targets) {}
}
// this tool performs the actions of another tool without hiding the widgets of the original
class TemporaryTool extends Tool {
constructor(editor, style, substance, autoRestore = true) {
super(editor);
this.style = style;
this.substance = substance;
this.autoRestore = autoRestore;
}
start(ptr, target, x, y) {
this.substance.start(ptr, target, x, y);
}
move(ptr, target, x, y) {
this.substance.move(ptr, target, x, y);
}
end(ptr, target, x, y) {
this.substance.end(ptr, target, x, y);
if (this.autoRestore) {
this.editor.tool = this.style;
}
}
drawWidget(targets) {
this.style.drawWidget(targets);
}
}
class OrbitTool extends Tool {
constructor(editor) {
super(editor);
this.rotateStart = null;
}
start(ptr, target, x, y) {
this.rotateStart = this.editor.scene.rotate.copy();
}
move(ptr, target, x, y) {
let displaySize = Math.min( this.editor.scene.width, this.editor.scene.height );
let moveRY = x / displaySize * Math.PI * Zdog.TAU;
let moveRX = y / displaySize * Math.PI * Zdog.TAU;
this.editor.scene.rotate.x = this.rotateStart.x - moveRX;
this.editor.scene.rotate.y = this.rotateStart.y - moveRY;
this.editor.syncLayers();
}
}
class TranslateTool extends Tool {
constructor(editor) {
super(editor);
this.targets = null;
this.startTranslate = null;
this.mode = TranslateTool.MODE_NONE;
this.widget = null;
}
start(ptr, target, x, y) {
this.widget = target;
this.targets = this.editor.selection.slice(0);
this.mode = TranslateTool.MODE_NONE;
if (!this.targets.length || target.layer !== Zoodle.LAYER_UI || !target.color) {
return;
}
// Ensure our widget target is the base and not the tip.
if (this.widget.diameter)
this.widget = this.widget.addTo;
this.startTranslate = this.targets.map((t) => t.translate.copy());
switch (target.color) {
case rose:
this.mode = TranslateTool.MODE_X;
break;
case lime:
this.mode = TranslateTool.MODE_Y;
break;
case blueberry:
this.mode = TranslateTool.MODE_Z;
break;
default:
this.mode = TranslateTool.MODE_NONE;
break;
}
}
move(ptr, target, x, y) {
if (!this.mode) { return; }
let direction = this.widget.renderNormal; // TODO: Break out into a function.
let delta = this.editor.getAxisDistance(x, y, Math.atan2(direction.y, direction.x));
delta /= -this.editor.scene.zoom; // TODO: Include pixel ratio as well.
delta *= this.widget.addTo.addTo.scale.x;
delta *= Math.abs(this.widget.renderNormal.magnitude2d());
// TODO: Oh I know where this is going wrong, we're not including how the view rotation is going to shorten the distance.
// We need some sines or cosines or something or both in here.
this.targets.forEach((t, i) => {
t.translate[this.mode] = this.startTranslate[i][this.mode] + delta;
});
this.editor.updateHighlights();
this.editor.updateUI();
this.editor.props.updatePanel();
}
end(ptr, target, x, y) {
if (!this.mode) { return; }
// ensure any final adjustments are applied.
this.move(ptr, target, x, y);
let direction = this.widget.renderNormal; // TODO: Break out into a function.
let delta = this.editor.getAxisDistance(x, y, Math.atan2(direction.y, direction.x));
delta /= -this.editor.scene.zoom;
delta *= this.widget.addTo.addTo.scale.x;
delta /= Math.abs(this.widget.renderNormal.magnitude2d());
let command = new TranslateCommand(this.editor, this.targets, new Zdog.Vector({[this.mode]: delta}), this.startTranslate);
this.editor.did(command);
}
drawWidget(targets) {
// Create anchors matching selected objects.
targets = targets.map((target) => {
// TODO: Double check this is correct.
let parentTransforms = this.editor.getWorldTransforms(target.addTo);
let childTranslate = target.translate.copy().rotate(parentTransforms.rotate);
parentTransforms.translate.add(childTranslate);
parentTransforms.translate.multiply(parentTransforms.scale);
return new Zdog.Anchor({
addTo: this.editor.ui,
...parentTransforms,
});
});
let origin = new Zdog.Shape({
stroke: .5,
color: lace,
});
let base = new Zdog.Shape({
path: [ { z: -1.5 }, { z: 1.5 } ],
stroke: 1,
translate: { z: 3 },
});
new Zdog.Cone({
addTo: base,
diameter: 2,
length: 1.5,
stroke: .5,
translate: { z: 1.5 },
});
let z = base.copyGraph({
color: blueberry,
});
z.children[0].color = blueberry;
let y = base.copyGraph({
color: lime,
rotate: { x: -TAU/4 },
translate: { y: 3 },
});
y.children[0].color = lime;
let x = base.copyGraph({
color: rose,
rotate: { y: -TAU/4 },
translate: { x: 3 },
});
x.children[0].color = rose;
targets.forEach(t => {
origin.copyGraph({ addTo: t, scale: 1/t.scale.x });
z.copyGraph({
addTo: t,
scale: 1/t.scale.x,
translate: { z: 3/t.scale.x },
});
y.copyGraph({
addTo: t,
scale: 1/t.scale.x,
translate: { y: 3/t.scale.x },
});
x.copyGraph({
addTo: t,
scale: 1/t.scale.x,
translate: { x: 3/t.scale.x },
});
});
}
static get MODE_NONE() { return ''; }
static get MODE_X() { return 'x'; }
static get MODE_Y() { return 'y'; }
static get MODE_Z() { return 'z'; }
static get MODE_VIEW() { return 'v'; }
}
// TODO: I think this is going to need to run on basis vectors like getWorldTransforms.
class RotateTool extends Tool {
constructor(editor) {
super(editor);
this.targets = null;
this.startRotate = null;
this.mode = RotateTool.MODE_NONE;
this.widget = null;
}
start(ptr, target, x, y) {
this.widget = target;
this.targets = this.editor.selection.slice(0);
this.mode = RotateTool.MODE_NONE;
if (!this.targets.length || target.layer !== Zoodle.LAYER_UI || !target.color) {
return;
}
this.startRotate = this.targets.map( t => t.rotate.copy() );
switch (target.color) {
case rose:
this.mode = RotateTool.MODE_X;
break;
case lime:
this.mode = RotateTool.MODE_Y;
break;
case blueberry:
this.mode = RotateTool.MODE_Z;
break;
}
}
move( ptr, target, x, y ) {
if (!this.mode) { return; }
let displaySize = Math.min( this.editor.scene.width, this.editor.scene.height );
x /= displaySize / Math.PI * Zdog.TAU;
y /= displaySize / Math.PI * Zdog.TAU;
let direction = this.widget.renderNormal;
let delta = this.editor.getAxisDistance(x, y, Math.atan2(direction.y, direction.x) + TAU/4 );
this.targets.forEach((t, i) => {
t.rotate[this.mode] = this.startRotate[i][this.mode] + delta;
});
this.editor.updateHighlights();
this.editor.updateUI();
this.editor.props.updatePanel();
}
// TODO: Let's stash `delta` somewhere so we don't have to recalculate it in end()
end( ptr, target, x, y ) {
if (!this.mode) { return; }
// ensure any final adjustments are applied.
this.move( ptr, target, x, y );
let displaySize = Math.min( this.editor.scene.width, this.editor.scene.height );
x /= displaySize * Math.PI * Zdog.TAU;
y /= displaySize * Math.PI * Zdog.TAU;
let direction = this.widget.renderNormal;
let delta = this.editor.getAxisDistance( x, y, Math.atan2(direction.y, direction.x) + TAU/4 );
let command = new RotateCommand(this.editor, this.targets, new Zdog.Vector({[this.mode]: delta}));
this.editor.did(command);
}
drawWidget(targets) {
// Create anchors matching selected objects.
targets = targets.map((target) => {
return new Zdog.Anchor({
addTo: this.editor.ui,
...this.editor.getWorldTransforms(target),
});
});
const widgetDiameter = 10;
const widgetStroke = 0.75;
let origin = new Zdog.Shape({
stroke: .5,
color: lace,
});
let zRing = new Zdog.Ellipse({
diameter: widgetDiameter,
stroke: widgetStroke,
color: blueberry,
});
let yRing = zRing.copyGraph({
rotate: { x: TAU/4 },
color: lime,
});
let xRing = zRing.copyGraph({
rotate: { y: TAU/4 },
color: rose,
});
targets.forEach(t => {
origin.copyGraph({ addTo: t, scale: 1/t.scale.x });
zRing.copyGraph({ addTo: t, scale: 1/t.scale.x });
yRing.copyGraph({ addTo: t, scale: 1/t.scale.x });
xRing.copyGraph({ addTo: t, scale: 1/t.scale.x });
});
}
static get MODE_NONE() { return ''; }
static get MODE_X() { return 'x'; }
static get MODE_Y() { return 'y'; }
static get MODE_Z() { return 'z'; }
}
class Command {
constructor(editor) {
this.editor = editor;
}
do() {}
undo() {}
}
class SelectCommand extends Command {
constructor(editor, target, replace = false) {
super(editor);
this.replace = replace;
this.oldSelection = null;
// If this is a compositeChild, find its parent
while (target && target.compositeChild) {
target = target.addTo;
}
// Don't select root elements.
if (target && !target.addTo) {
target = null;
}
this.target = target;
}
do() {
this.oldSelection = this.editor.selection.slice( 0 );
if (!this.target) {
this.editor.clearSelection();
} else if (this.replace) {
this.editor.setSelection(this.target);
} else {
this.editor.toggleSelection(this.target);
}
this.refresh();
}
undo() {
this.editor.selection = this.oldSelection;
this.refresh();
}
refresh() {
this.editor.updateHighlights();
this.editor.updateUI();
this.editor.props.updatePanel();
}
}
class TranslateCommand extends Command {
constructor(editor, target, delta, oldTranslate = null) {
super(editor);
if (!Array.isArray(target)) {
target = [target];
}
this.target = target;
this.delta = delta;
this.oldTranslate = oldTranslate || target.map((t) => t.translate.copy());
}
do() {
if (!this.target) return console.error("Doing TranslateCommand with no target.");
this.target.forEach((t, i) => {
t.translate.set(this.oldTranslate[i]).add(this.delta);
});
this.refresh();
}
undo() {
if (!this.target) return console.error("Undoing TranslateCommand with no target.");
this.target.forEach((t, i) => {
t.translate.set(this.oldTranslate[i]);
});
this.refresh();
}
refresh() {
this.editor.updateHighlights();
this.editor.updateUI();
this.editor.props.updatePanel();
}
}
class RotateCommand extends Command {
// TODO: Add oldTranslate and oldRotate to the constructor, or maybe a flag to tell it if it's getting new or old transforms.
constructor(editor, target, delta) {
super(editor);
if (!Array.isArray(target)) {
target = [target];
}
this.target = target;
this.delta = delta;
this.oldRotate = target.map((t) => t.rotate.copy());
}
do() {
// TODO: Probably better to just throw in the constructor.
if (!this.target) return console.error("Doing RotateCommand with no target.");
this.target.forEach((t, i) => {
t.rotate.set(this.oldRotate[i]).add(this.delta);
});
}
undo() {
if (!this.target) return console.error("Undoing RotateCommand with no target.");
this.target.forEach((t, i) => {
t.rotate.set(this.oldRotate[i]);
});
}
}
class EditCommand extends Command {
constructor(editor, target, propId, value, oldValue = null) {
super(editor);
if (!target) {
throw new Error("No target specified for EditCommand");
}
if (!Array.isArray(target)) {
target = [target];
}
this.target = target;
this.propId = propId;
this.value = value;
this.oldValue = oldValue || target.map( (t) => t[propId]);
}
do() {
this.target.forEach( (t) => {
t[this.propId] = this.value;
if (t.updatePath) t.updatePath();
});
}
undo() {
this.target.forEach( (t, i) => {
t[this.propId] = this.oldValue[i];
if (t.updatePath) t.updatePath();
});
}
}