Repository navigation
Expand file tree
/
Copy pathselect.js
More file actions
520 lines (470 loc) · 14 KB
/
Copy pathselect.js
File metadata and controls
520 lines (470 loc) · 14 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import app from './../app.js';
import config from './../config.js';
import Base_tools_class from './../core/base-tools.js';
import Base_layers_class from './../core/base-layers.js';
import Base_selection_class from './../core/base-selection.js';
import Helper_class from './../libs/helpers.js';
import Dialog_class from './../libs/popup.js';
import alertify from './../../../node_modules/alertifyjs/build/alertify.min.js';
class Select_tool_class extends Base_tools_class {
constructor(ctx) {
super();
this.Base_layers = new Base_layers_class();
this.POP = new Dialog_class();
this.Helper = new Helper_class();
this.ctx = ctx;
this.name = 'select';
this.saved = false;
this.mousedown_dimensions = { x: null, y: null, width: null, height: null };
this.keyboard_move_start_position = null;
this.moving = false;
this.resizing = false;
this.snap_line_info = {x: null, y: null};
var sel_config = {
enable_background: false,
enable_borders: true,
enable_controls: true,
keep_ratio: true,
data_function: function () {
return config.layer;
},
};
this.Base_selection = new Base_selection_class(ctx, sel_config, this.name);
}
load() {
var _this = this;
//mouse events
document.addEventListener('mousedown', function (e) {
_this.dragStart(e);
});
document.addEventListener('mousemove', function (e) {
_this.dragMove(e);
});
document.addEventListener('mouseup', function (e) {
_this.dragEnd(e);
});
// collect touch events
document.addEventListener('touchstart', function (e) {
_this.dragStart(e);
});
document.addEventListener('touchmove', function (e) {
_this.dragMove(e);
});
document.addEventListener('touchend', function (e) {
_this.dragEnd(e);
});
//keyboard actions
document.addEventListener('keydown', (event) => {
if (config.TOOL.name != this.name)
return;
if (this.POP.active == true)
return;
if (this.Helper.is_input(event.target))
return;
var k = event.key;
if (k == "ArrowUp") {
this.move(0, -1, event);
}
else if (k == "ArrowDown") {
this.move(0, 1, event);
}
else if (k == "ArrowRight") {
this.move(1, 0, event);
}
else if (k == "ArrowLeft") {
this.move(-1, 0, event);
}
if (k == "Delete") {
if (config.TOOL.name == this.name) {
app.State.do_action(
new app.Actions.Delete_layer_action(config.layer.id)
);
}
}
});
document.addEventListener('keyup', (event) => {
if (config.TOOL.name != this.name)
return;
if (this.POP.active == true)
return;
if (this.Helper.is_input(event.target))
return;
var k = event.key;
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(k)) {
if (this.keyboard_move_start_position) {
let x = config.layer.x;
let y = config.layer.y;
config.layer.x = this.keyboard_move_start_position.x;
config.layer.y = this.keyboard_move_start_position.y;
app.State.do_action(
new app.Actions.Update_layer_action(config.layer.id, { x, y })
);
this.keyboard_move_start_position = null;
}
}
});
}
dragStart(event) {
if (config.TOOL.name != this.name)
return;
this.mousedown(event);
}
dragMove(event) {
if (config.TOOL.name != this.name)
return;
this.mousemove(event);
}
dragEnd(event) {
if (config.TOOL.name != this.name)
return;
this.mouseup(event);
this.Base_layers.render();
}
async mousedown(e) {
var mouse = this.get_mouse_info(e);
if (mouse.click_valid == false)
return;
if (this.Base_selection.mouse_lock != null) {
this.resizing = true;
this.Base_selection.find_settings().keep_ratio = config.layer.type === 'image';
if (config.layer.type === 'text' && config.layer.params && config.layer.params.boundary === 'dynamic') {
config.layer.params.boundary = 'box';
}
}
else {
this.moving = true;
await this.auto_select_object(e);
this.Base_selection.find_settings().keep_ratio = config.layer.type === 'image';
this.saved = false;
}
this.mousedown_dimensions = {
x: Math.round(config.layer.x),
y: Math.round(config.layer.y),
width: Math.round(config.layer.width),
height: Math.round(config.layer.height)
};
}
mousemove(e) {
var mouse = this.get_mouse_info(e);
if (mouse.is_drag == false)
return;
if (mouse.click_valid == false) {
return;
}
if (this.resizing) {
return;
}
else if (this.moving) {
//move object
config.layer.x = Math.round(mouse.x - mouse.click_x + this.mousedown_dimensions.x);
config.layer.y = Math.round(mouse.y - mouse.click_y + this.mousedown_dimensions.y);
//apply snap
var snap_info = this.calc_snap(e, config.layer.x, config.layer.y);
if(snap_info != null){
if(snap_info.x != null) {
config.layer.x = snap_info.x;
}
if(snap_info.y != null) {
config.layer.y = snap_info.y;
}
}
config.need_render = true;
}
}
mouseup(e) {
var mouse = this.get_mouse_info(e);
if (mouse.click_valid == false) {
return;
}
if (this.resizing) {
let x = config.layer.x;
let y = config.layer.y;
let width = config.layer.width;
let height = config.layer.height;
config.layer.x = this.mousedown_dimensions.x;
config.layer.y = this.mousedown_dimensions.y;
config.layer.width = this.mousedown_dimensions.width;
config.layer.height = this.mousedown_dimensions.height;
if (
this.mousedown_dimensions.x !== x || this.mousedown_dimensions.y !== y ||
this.mousedown_dimensions.width !== width || this.mousedown_dimensions.height !== height
) {
app.State.do_action(
new app.Actions.Bundle_action('resize_layer', 'Resize Layer', [
new app.Actions.Update_layer_action(config.layer.id, {
x, y, width, height
})
])
);
}
}
else if (this.moving) {
var new_x = Math.round(mouse.x - mouse.click_x + this.mousedown_dimensions.x);
var new_y = Math.round(mouse.y - mouse.click_y + this.mousedown_dimensions.y);
config.layer.x = this.mousedown_dimensions.x;
config.layer.y = this.mousedown_dimensions.y;
if(mouse.x - mouse.click_x || mouse.y - mouse.click_y) {
var snap_info = this.calc_snap(e, new_x, new_y);
if (snap_info != null) {
if (snap_info.x != null) {
new_x = snap_info.x;
}
if (snap_info.y != null) {
new_y = snap_info.y;
}
}
}
if (this.mousedown_dimensions.x !== new_x || this.mousedown_dimensions.y !== new_y) {
app.State.do_action(
new app.Actions.Bundle_action('move_layer', 'Move Layer', [
new app.Actions.Update_layer_action(config.layer.id, {
x: new_x,
y: new_y
})
])
);
}
}
this.moving = false;
this.resizing = false;
}
render_overlay(ctx){
var ctx = this.Base_layers.ctx;
var mouse = this.get_mouse_info(event);
if (mouse.is_drag == false)
return;
this.render_overlay_parent(ctx);
}
/**
* calculates current object snap coordinates and returns it. One of coordinates can be null.
*
* @param event
* @param pos_x
* @param pos_y
* @returns object|null
*/
calc_snap(event, pos_x, pos_y) {
var snap_position = { x: null, y: null };
var params = this.getParams();
if(config.SNAP === false || event.shiftKey == true){
this.snap_line_info = {x: null, y: null};
return null;
}
//settings
var sensitivity = 0.01;
var max_distance = (config.WIDTH + config.HEIGHT) / 2 * sensitivity / config.ZOOM;
//collect snap positions
var snap_positions = this.get_snap_positions(config.layer.id);
//find closest snap positions
var min_group = {
x: {
start: null,
center: null,
end: null,
},
y: {
start: null,
center: null,
end: null,
},
};
var min_group_distance = {
x: {
start: null,
center: null,
end: null,
},
y: {
start: null,
center: null,
end: null,
},
};
//x
for(var i in snap_positions.x){
var distance = Math.abs(pos_x - snap_positions.x[i]);
if(distance < max_distance && (distance < min_group_distance.x.start || min_group_distance.x.start === null)){
min_group_distance.x.start = distance;
min_group.x.start = snap_positions.x[i];
}
var distance = Math.abs(pos_x + config.layer.width/2 - snap_positions.x[i]);
if(distance < max_distance && (distance < min_group_distance.x.center || min_group_distance.x.center === null)){
min_group_distance.x.center = distance;
min_group.x.center = snap_positions.x[i];
}
var distance = Math.abs(pos_x + config.layer.width - snap_positions.x[i]);
if(distance < max_distance && (distance < min_group_distance.x.end || min_group_distance.x.end === null)){
min_group_distance.x.end = distance;
min_group.x.end = snap_positions.x[i];
}
}
//y
for(var i in snap_positions.y){
var distance = Math.abs(pos_y - snap_positions.y[i]);
if(distance < max_distance && (distance < min_group_distance.y.start || min_group_distance.y.start === null)){
min_group_distance.y.start = distance;
min_group.y.start = snap_positions.y[i];
}
var distance = Math.abs(pos_y + config.layer.height/2 - snap_positions.y[i]);
if(distance < max_distance && (distance < min_group_distance.y.center || min_group_distance.y.center === null)){
min_group_distance.y.center = distance;
min_group.y.center = snap_positions.y[i];
}
var distance = Math.abs(pos_y + config.layer.height - snap_positions.y[i]);
if(distance < max_distance && (distance < min_group_distance.y.end || min_group_distance.y.end === null)){
min_group_distance.y.end = distance;
min_group.y.end = snap_positions.y[i];
}
}
//find best begin, center, end
var min_distance = {
x: null,
y: null,
};
//x
if(min_group_distance.x.start != null)
min_distance.x = min_group_distance.x.start;
if(min_group_distance.x.center != null && (min_group_distance.x.center < min_distance.x || min_distance.x === null))
min_distance.x = min_group_distance.x.center;
if(min_group_distance.x.end != null && (min_group_distance.x.end < min_distance.x || min_distance.x === null))
min_distance.x = min_group_distance.x.end;
//y
if(min_group_distance.y.start != null)
min_distance.y = min_group_distance.y.start;
if(min_group_distance.y.center != null && (min_group_distance.y.center < min_distance.y || min_distance.y === null))
min_distance.y = min_group_distance.y.center;
if(min_group_distance.y.end != null && (min_group_distance.y.end < min_distance.y || min_distance.y === null))
min_distance.y = min_group_distance.y.end;
//apply snap
var success = false;
//x
if(min_group.x.center != null && min_group_distance.x.center == min_distance.x) {
snap_position.x = Math.round(min_group.x.center - config.layer.width / 2);
success = true;
this.snap_line_info.x = {
start_x: min_group.x.center,
start_y: 0,
end_x: min_group.x.center,
end_y: config.HEIGHT
};
}
else if(min_group.x.start != null && min_group_distance.x.start == min_distance.x) {
snap_position.x = Math.round(min_group.x.start);
success = true;
this.snap_line_info.x = {
start_x: min_group.x.start,
start_y: 0,
end_x: min_group.x.start,
end_y: config.HEIGHT,
};
}
else if(min_group.x.end != null && min_group_distance.x.end == min_distance.x) {
snap_position.x = Math.round(min_group.x.end - config.layer.width);
success = true;
this.snap_line_info.x = {
start_x: min_group.x.end,
start_y: 0,
end_x: min_group.x.end,
end_y: config.HEIGHT
};
}
else{
this.snap_line_info.x = null;
}
//y
if(min_group.y.center != null && min_group_distance.y.center == min_distance.y) {
snap_position.y = Math.round(min_group.y.center - config.layer.height / 2);
success = true;
this.snap_line_info.y = {
start_x: 0,
start_y: min_group.y.center,
end_x: config.WIDTH,
end_y: min_group.y.center,
};
}
else if(min_group.y.start != null && min_group_distance.y.start == min_distance.y) {
snap_position.y = Math.round(min_group.y.start);
success = true;
this.snap_line_info.y = {
start_x: 0,
start_y: min_group.y.start,
end_x: config.WIDTH,
end_y: min_group.y.start,
};
}
else if(min_group.y.end != null && min_group_distance.y.end == min_distance.y) {
snap_position.y = Math.round(min_group.y.end - config.layer.height);
success = true;
this.snap_line_info.y = {
start_x: 0,
start_y: min_group.y.end,
end_x: config.WIDTH,
end_y: min_group.y.end,
};
}
else{
this.snap_line_info.y = null;
}
if(success) {
return snap_position;
}
return null;
}
move(direction_x, direction_y, event) {
if (!this.keyboard_move_start_position) {
this.keyboard_move_start_position = {
x: config.layer.x,
y: config.layer.y
}
}
var power = 10;
if (event.ctrlKey == true || event.metaKey)
power = 50;
if (event.shiftKey == true)
power = 1;
config.layer.x += direction_x * power;
config.layer.y += direction_y * power;
config.need_render = true;
}
async auto_select_object(e) {
var params = this.getParams();
if (params.auto_select == false)
return;
var layers_sorted = this.Base_layers.get_sorted_layers();
//render main canvas
for (var i = 0; i < layers_sorted.length; i++) {
var value = layers_sorted[i];
var canvas = this.Base_layers.convert_layer_to_canvas(value.id, null, false);
if (this.check_hit_region(e, canvas.getContext("2d"), value) == true) {
await app.State.do_action(
new app.Actions.Select_layer_action(value.id)
);
break;
}
}
}
check_hit_region(e, ctx, layer) {
var mouse = this.get_mouse_info(e);
if(layer.type == 'image' && Math.abs(layer.width * layer.height / 1000000) > 5){
//too big to check using getImageData - use simple way
if (mouse.x > layer.x && mouse.x < layer.x + layer.width &&
mouse.y > layer.y && mouse.y < layer.y + layer.height) {
//hit
return true;
}
return false;
}
var data = ctx.getImageData(mouse.x, mouse.y, 1, 1).data;
var blank = [0, 0, 0, 0];
if (config.TRANSPARENCY == false) {
blank = [0, 0, 0, 0];
}
if (data[0] != blank[0] || data[1] != blank[1] || data[2] != blank[2]
|| data[3] != blank[3]) {
//hit
return true;
}
return false;
}
}
export default Select_tool_class;