This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvector_graphics_editor.cpp
1599 lines (1230 loc) · 40.6 KB
/
vector_graphics_editor.cpp
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************/
/* vg_editor.cpp */
/*************************************************************************/
#include "vector_graphics_editor.h"
#include "editor/plugins/canvas_item_editor_plugin.h"
#include "core/os/keyboard.h"
#include "tove2d/src/cpp/mesh/meshifier.h"
static Array subpath_points_array(const tove::SubpathRef &subpath) {
const int n = subpath->getNumPoints();
const float *p = subpath->getPoints();
Array a;
a.resize(n);
for (int i = 0; i < n; i++) {
a[i] = Vector2(p[2 * i + 0], p[2 * i + 1]);
}
return a;
}
inline bool is_control_point(int pt) {
return (pt % 3) != 0;
}
Vector2 VGTool::global_to_local(CanvasItem *p_node, const Vector2 p_where) const {
return p_node->get_global_transform().affine_inverse().xform(
canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(p_where)));
}
class GaussJordan {
private:
Vector3 A[3];
Vector3 b;
void swap_rows(int i, int j) {
if (i != j) {
{
Vector3 t = A[i];
A[i] = A[j];
A[j] = t;
}
{
float t = b[i];
b[i] = b[j];
b[j] = t;
}
}
}
int find_pivot(int h) {
int i_max = h;
float m = 0.0f;
for (int i = h; i < 3; i++) {
float t = Math::abs(A[i][h]);
if (t > m) {
m = t;
i_max = i;
}
}
return i_max;
}
Vector3 find_x() {
Vector3 x;
for (int i = 2; i >= 0; i--) {
float t = b[i];
for (int j = 2; j > i; j--) {
t -= x[j] * A[i][j];
}
x[i] = t / A[i][i];
}
return x;
}
public:
GaussJordan(Vector3 A1, Vector3 A2, Vector3 A3, Vector3 b) : b(b) {
A[0] = A1;
A[1] = A2;
A[2] = A3;
}
Vector3 solve() {
for (int d = 0; d < 3; d++) {
swap_rows(d, find_pivot(d));
for (int i = d + 1; i < 3; i++) {
const float t = A[i][d] / A[d][d];
A[i][d] = 0;
for (int j = d + 1; j < 3; j++) {
A[i][j] = A[i][j] - A[d][j] * t;
}
b[i] -= t * b[d];
}
}
return find_x();
}
};
Transform2D mapping_to_transform(const Vector2 &u1, const Vector2 &v1, const Vector2 &u2, const Vector2 &v2, const Vector2 &u3, const Vector2 &v3) {
GaussJordan gx(
Vector3(u1.x, u1.y, 1),
Vector3(u2.x, u2.y, 1),
Vector3(u3.x, u3.y, 1),
Vector3(v1.x, v2.x, v3.x)
);
Vector3 x = gx.solve();
GaussJordan gy(
Vector3(u1.x, u1.y, 1),
Vector3(u2.x, u2.y, 1),
Vector3(u3.x, u3.y, 1),
Vector3(v1.y, v2.y, v3.y)
);
Vector3 y = gy.solve();
return Transform2D(x[0], y[0], x[1], y[1], x[2], y[2]);
}
Vector2 ray_ray(const Vector2 &s1, const Vector2 &r1, const Vector2 &s2, const Vector2 &r2) {
float t = -((-(r2.y*s1.x) + r2.y*s2.x + r2.x*s1.y - r2.x*s2.y)/(r2.x*r1.y - r1.x*r2.y));
return s1 + t * r1;
}
VGTool::VGTool() : canvas_item_editor(CanvasItemEditor::get_singleton()) {
}
class VGTransformTool : public VGTool {
VGEditor *vg_editor;
VGPath *path;
UndoRedo *undo_redo;
Transform2D transform0;
Transform2D transform1;
struct Pos {
Vector2 local;
Vector2 global;
};
typedef Pos Pos4[4];
enum Widget {
WIDGET_NONE,
WIDGET_CORNER,
WIDGET_MIDPOINT,
WIDGET_ROTATE
};
Widget clicked;
Vector2 offset;
struct {
Pos corner;
Pos opposite;
Pos between;
Vector2 u, v;
Transform2D t;
void init(const Pos4 &p, int i) {
corner = p[i];
opposite = p[(i + 2) % 4];
between = p[(i + 1) % 4];
u = (between.global - opposite.global).normalized();
v = (between.global - corner.global).normalized();
}
} corner_widget;
struct {
Pos corner_0;
Pos corner_1;
Pos opposite_0;
Pos opposite_1;
Pos opposite_2;
Vector2 u;
Transform2D t;
void init(const Pos4 &p, int i) {
corner_0 = p[i];
corner_1 = p[(i + 1) % 4];
opposite_0 = p[(i + 3) % 4];
opposite_1 = p[(i + 2) % 4];
opposite_2 = p[(i + 1) % 4];
u = (opposite_2.global - opposite_1.global).normalized();
}
} midpoint_widget;
struct {
Vector2 drag_from;
Vector2 drag_rotation_center;
float initial_angle;
} rotate_widget;
void get_corners(Pos4 &r_pos, const Transform2D &p_xform) {
Rect2 r = tove_bounds_to_rect2(path->get_subtree_graphics()->getBounds());
Vector2 p[4];
for (int i = 0; i < 4; i++) {
const int j = i ^ (i >> 1);
r_pos[i].local = Point2(r.position.x + ((j & 1) ? r.size.x : 0.0f), r.position.y + ((j & 2) ? r.size.y : 0.0f));
r_pos[i].global = p_xform.xform(r_pos[i].local);
}
}
Vector2 get_rotation_handle_position(const Pos4 &p_pos, Vector2 *r_origin = NULL) {
float x00 = p_pos[0].global.x, y00 = p_pos[0].global.y;
float x10 = p_pos[1].global.x, y10 = p_pos[1].global.y;
float x11 = p_pos[2].global.x, y11 = p_pos[2].global.y;
float phi = Math::atan2(y11 - y10, x11 - x10) + M_PI;
float mx = (x00 + x10) / 2, my = (y00 + y10) / 2;
const float toplen = 50;
if (r_origin) {
*r_origin = Vector2(mx, my);
}
return Vector2(mx + toplen * Math::cos(phi), my + toplen * Math::sin(phi));
}
void update(const Vector2 &u1, const Vector2 &v1, const Vector2 &u2, const Vector2 &v2, const Vector2 &u3, const Vector2 &v3) {
Transform2D t = mapping_to_transform(u1, v1, u2, v2, u3, v3);
const float eps = 1e-3;
if (t.elements[0].length() > eps && t.elements[1].length() > eps) {
transform1 = t;
path->set_transform(t);
}
}
void _commit_action() {
undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
undo_redo->commit_action();
}
public:
VGTransformTool(VGEditor *p_vg_editor, VGPath *p_path) :vg_editor(p_vg_editor), path(p_path) {
clicked = WIDGET_NONE;
undo_redo = vg_editor->get_editor_node()->get_undo_redo();
}
virtual bool forward_gui_input(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid()) {
if (mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
const CanvasItem *pi = path->get_parent_item();
const Transform2D xform = canvas_item_editor->get_canvas_transform() * (pi ? pi->get_global_transform() : Transform2D());
const real_t grab_threshold = EDITOR_DEF("editors/poly_editor/point_grab_radius", 8);
transform0 = path->get_transform();
transform1 = transform0;
clicked = WIDGET_NONE;
const Point2 q_canvas = mb->get_position();
Pos4 p_canvas;
get_corners(p_canvas, xform * path->get_transform());
for (int i = 0; i < 4; i++) {
if ((p_canvas[i].global - q_canvas).length() < grab_threshold) {
Pos4 p;
get_corners(p, path->get_transform());
const Point2 q_inv = xform.affine_inverse().xform(q_canvas);
offset = p[i].global - q_inv;
corner_widget.init(p, i);
corner_widget.t = xform;
clicked = WIDGET_CORNER;
return true;
}
Vector2 midpoint = (p_canvas[i].global + p_canvas[(i + 1) % 4].global) / 2;
if ((midpoint - q_canvas).length() < grab_threshold) {
Pos4 p;
get_corners(p, path->get_transform());
const Point2 q_inv = xform.affine_inverse().xform(q_canvas);
Vector2 midpoint_inv = (p[i].global + p[(i + 1) % 4].global) / 2;
offset = midpoint_inv - q_inv;
midpoint_widget.init(p, i);
midpoint_widget.t = xform;
clicked = WIDGET_MIDPOINT;
return true;
}
}
//
Vector2 rot_pos = get_rotation_handle_position(p_canvas);
if ((rot_pos - q_canvas).length() < grab_threshold) {
/*Pos4 p;
get_corners(p, path->get_transform());
const Point2 q_inv = xform.affine_inverse().xform(q_canvas);
Vector2 rot_pos_inv = get_rotation_handle_position(p);
offset = rot_pos_inv - q_inv;*/
offset = Vector2(0, 0); // not used for rotation
rotate_widget.drag_from = canvas_item_editor->get_canvas_transform().affine_inverse().xform(q_canvas);
Vector2 drag_rotation_center;
if (path->_edit_use_pivot()) {
drag_rotation_center = path->get_global_transform().xform(path->_edit_get_pivot());
} else {
drag_rotation_center = path->get_global_transform().get_origin();
}
rotate_widget.drag_rotation_center = drag_rotation_center;
rotate_widget.initial_angle = path->_edit_get_rotation();
clicked = WIDGET_ROTATE;
return true;
}
} else if (mb->get_button_index() == BUTTON_LEFT && clicked != WIDGET_NONE) {
if (clicked != WIDGET_NONE) {
if (clicked != WIDGET_ROTATE) {
undo_redo->create_action(TTR("Transform Path"));
undo_redo->add_do_method(path, "set_transform", transform1);
undo_redo->add_do_method(path, "recenter");
undo_redo->add_undo_method(path, "set_transform", transform0);
undo_redo->add_undo_method(path, "recenter");
_commit_action();
} else {
undo_redo->create_action(TTR("Rotate Path"));
undo_redo->add_do_method(path, "_edit_set_rotation", path->_edit_get_rotation());
undo_redo->add_undo_method(path, "_edit_set_rotation", rotate_widget.initial_angle);
_commit_action();
}
clicked = WIDGET_NONE;
}
return true;
}
}
Ref<InputEventMouseMotion> mm = p_event;
if (clicked == WIDGET_NONE && path && mm.is_valid() && (mm->get_button_mask() & BUTTON_MASK_LEFT)) {
return false;
}
if (clicked != WIDGET_NONE && path && mm.is_valid()) {
if (mm->get_button_mask() & BUTTON_MASK_LEFT) {
Vector2 g = mm->get_position();
const CanvasItem *pi = path->get_parent_item();
Transform2D xform = canvas_item_editor->get_canvas_transform() * (pi ? pi->get_global_transform() : Transform2D());
g = xform.affine_inverse().xform(g);
g = g + offset;
switch (clicked) {
case WIDGET_CORNER: {
Vector2 p = ray_ray(corner_widget.opposite.global, corner_widget.u, g, corner_widget.v);
update(
corner_widget.between.local, p,
corner_widget.opposite.local, corner_widget.opposite.global,
corner_widget.corner.local, g);
} break;
case WIDGET_MIDPOINT: {
Vector2 m = (midpoint_widget.opposite_0.global + midpoint_widget.opposite_1.global) / 2;
Vector2 u = midpoint_widget.u;
float l = (g - m).dot(u);
update(
midpoint_widget.opposite_0.local, midpoint_widget.opposite_0.global,
midpoint_widget.opposite_1.local, midpoint_widget.opposite_1.global,
(midpoint_widget.corner_0.local + midpoint_widget.corner_1.local) / 2, m + u * l);
} break;
case WIDGET_ROTATE: {
Vector2 drag_rotation_center = rotate_widget.drag_rotation_center;
Vector2 drag_from = rotate_widget.drag_from;
Vector2 drag_to = canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position());
float phi = (drag_from - drag_rotation_center).angle_to(drag_to - drag_rotation_center);
rotate_widget.drag_from = drag_to;
path->_edit_set_rotation(canvas_item_editor->snap_angle(path->_edit_get_rotation() + phi, path->_edit_get_rotation()));
} break;
case WIDGET_NONE: {
} break;
}
canvas_item_editor->get_viewport_control()->update();
vg_editor->update();
return true;
}
}
return false;
}
virtual void forward_canvas_draw_over_viewport(Control *p_overlay) {
Transform2D xform = canvas_item_editor->get_canvas_transform() * path->get_global_transform();
const Ref<Texture> handle = vg_editor->get_icon("EditorHandle", "EditorIcons");
Control *vpc = canvas_item_editor->get_viewport_control();
Pos4 p;
get_corners(p, xform);
Vector2 rot_org;
Vector2 rot_pos = get_rotation_handle_position(p, &rot_org);
const Color line_color = Color(0.5, 0.5, 0.5);
for (int i = 0; i < 4; i++) {
vpc->draw_line(p[i].global, p[(i + 1) % 4].global, line_color, 2 * EDSCALE);
}
vpc->draw_line(rot_org, rot_pos, line_color, 2 * EDSCALE);
for (int i = 0; i < 4; i++) {
vpc->draw_texture(handle, p[i].global - handle->get_size() * 0.5);
}
for (int i = 0; i < 4; i++) {
Vector2 q = (p[i].global + p[(i + 1) % 4].global) / 2;
vpc->draw_texture(handle, q - handle->get_size() * 0.5);
}
vpc->draw_texture(handle, rot_pos - handle->get_size() * 0.5);
}
};
class VGEllipseTool : public VGTool {
VGPath *root;
VGPath *path;
tove::PathRef tove_path;
Vector2 point0;
public:
VGEllipseTool(VGPath *p_root) : root(p_root) {
path = NULL;
}
virtual bool forward_gui_input(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid()) {
if (mb->get_button_index() == BUTTON_LEFT) {
if (mb->is_pressed() && path == NULL) {
path = memnew(VGPath);
root->add_child(path);
if (root->get_owner()) {
path->set_owner(root->get_owner());
}
tove::SubpathRef tove_subpath = std::make_shared<tove::Subpath>();
tove_subpath->drawEllipse(0, 0, 0, 0);
tove_path = std::make_shared<tove::Path>();
tove_path->addSubpath(tove_subpath);
tove_path->setFillColor(std::make_shared<tove::Color>(0.8, 0.1, 0.1));
tove_path->setLineColor(std::make_shared<tove::Color>(0, 0, 0));
path->set_tove_path(tove_path);
point0 = global_to_local(root, mb->get_position());
} else if (!mb->is_pressed()) {
if (path && (global_to_local(root, mb->get_position()) - point0).length() < 2.0f) {
root->remove_child(path);
memdelete(path);
path = NULL;
} else if (path) {
// commit
//undo_redo->add_do_method(root, "add_vgpath");
//undo_redo->add_undo_method(root, "remove_vgpath", root->get_child_count());
//undo_redo->add_do_method(root, "set_child_fill_color", n, subpath_points_array(tove_subpath));
//undo_redo->add_do_method(root, "set_child_line_color", n, subpath_points_array(tove_subpath));
//undo_redo->add_do_method(root, "set_child_points", n, subpath_points_array(tove_subpath));
//undo_redo->add_undo_method(root, "remove_child", n);
//undo_redo->commit_action();
}
path = NULL;
}
}
return true;
}
Ref<InputEventMouseMotion> mm = p_event;
if (path && mm.is_valid()) {
if (mm->get_button_mask() & BUTTON_MASK_LEFT) {
// Vector2 gpoint = mm->get_position();
Vector2 point1 = global_to_local(root, mm->get_position());
Vector2 center = (point0 + point1) / 2;
Vector2 radius = (point1 - center).abs();
//float tx, ty = cx, cy
tove::SubpathRef tove_subpath = std::make_shared<tove::Subpath>();
tove_subpath->drawEllipse(0, 0, radius.x, radius.y);
//tove_path->getSubpath(0)->set(tove_subpath);
path->set_points(0, subpath_points_array(tove_subpath));
path->set_position(center);
//const float *bounds = path->get_tove_path()->getBounds();
//printf("%f %f\n", bounds[0], bounds[1]);
}
return true;
}
return false;
}
};
struct SubpathId {
SubpathId();
SubpathId(int p_subpath);
bool operator==(const SubpathId &p_id) const;
bool operator!=(const SubpathId &p_id) const;
bool valid() const;
int subpath;
};
struct Vertex : public SubpathId {
Vertex();
Vertex(int p_pt);
Vertex(int p_subpath, int p_pt);
bool operator==(const Vertex &p_vertex) const;
bool operator!=(const Vertex &p_vertex) const;
bool valid() const;
int pt;
};
struct PosVertex : public Vertex {
PosVertex();
PosVertex(const Vertex &p_vertex, const Vector2 &p_pos);
PosVertex(int p_subpath, int p_pt, const Vector2 &p_pos);
Vector2 pos;
};
struct SubpathPos : public SubpathId {
SubpathPos();
SubpathPos(int p_subpath, float p_t);
bool valid() const;
float t;
};
class PointIterator {
private:
tove::PathRef path_ref;
int n_subpaths;
int subpath;
tove::SubpathRef subpath_ref;
int pt;
int n_pts;
bool closed;
public:
PointIterator(
const tove::PathRef &p_path_ref);
bool next();
PosVertex get_pos_vertex() const;
Vertex get_vertex() const;
Vector2 get_pos() const;
};
class ControlPointIterator {
private:
const Vertex &selected_point;
const Vertex &edited_point;
int edited_pt0;
// int path;
tove::PathRef path_ref;
int n_subpaths;
int subpath;
tove::SubpathRef subpath_ref;
int pt;
int d_pt;
int n_pts;
bool closed;
bool is_visible() const;
public:
ControlPointIterator(
const tove::PathRef &p_path_ref,
const Vertex &p_selected_point,
const Vertex &p_edited_point);
bool next();
PosVertex get_pos_vertex() const;
Vector2 get_pos() const;
Vector2 get_knot_pos() const;
};
class VGCurveTool : public VGTool {
// VGPath *root;
VGPath *node_vg;
VGEditor *vg_editor;
EditorNode *editor;
UndoRedo *undo_redo;
PosVertex edited_point;
SubpathId edited_path;
Vertex hover_point; // point under mouse cursor
Vertex selected_point; // currently selected
Array pre_move_edit;
uint64_t mb_down_time;
Vector2 mb_down_where;
SubpathPos mb_down_at;
bool aligned_move;
void remove_point(const Vertex &p_vertex);
Vertex get_active_point() const;
PosVertex closest_point(const Vector2 &p_pos, bool p_cp = false) const;
SubpathPos closest_subpath_point(const Vector2 &p_pos) const;
Array _get_points(const SubpathId &p_id);
void _commit_action();
bool _is_empty() const;
VGPath *_get_node() const {
return node_vg;
}
public:
VGCurveTool(VGEditor *p_vg_editor, VGPath *p_path) :
vg_editor(p_vg_editor), editor(p_vg_editor->get_editor_node()) {
node_vg = p_path;
undo_redo = editor->get_undo_redo();
edited_point = PosVertex();
edited_path = SubpathId();
hover_point = Vertex();
selected_point = Vertex();
}
virtual bool forward_gui_input(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid()) {
Transform2D xform = canvas_item_editor->get_canvas_transform() * node_vg->get_global_transform();
if (mb->get_button_index() == BUTTON_LEFT && mb->is_pressed() && mb->is_doubleclick()) {
Point2 p = xform.affine_inverse().xform(mb->get_position());
VGPath *clicked = node_vg->find_clicked_child(p);
if (clicked) {
editor->push_item(clicked);
}
return true;
}
Vector2 gpoint = mb->get_position();
// Vector2 cpoint = node_vg->get_global_transform().affine_inverse().xform(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mb->get_position())));
if (true) {
if (mb->get_button_index() == BUTTON_LEFT) {
if (mb->is_pressed()) {
const PosVertex closest = closest_point(gpoint, true);
const SubpathPos insert = closest.valid() ? SubpathPos() : closest_subpath_point(gpoint);
edited_point = PosVertex();
edited_path = SubpathId();
hover_point = Vertex();
aligned_move = !mb->get_alt();
if (insert.valid()) {
pre_move_edit = _get_points(insert);
mb_down_time = OS::get_singleton()->get_ticks_msec();
mb_down_where = gpoint;
mb_down_at = insert;
return true;
} else if (closest.valid()) {
pre_move_edit = _get_points(closest);
edited_point = PosVertex(closest, xform.affine_inverse().xform(closest.pos));
edited_path = edited_point;
mb_down_time = 0;
if (!is_control_point(closest.pt)) {
selected_point = closest;
}
canvas_item_editor->get_viewport_control()->update();
return true;
} else {
if (selected_point.valid()) {
selected_point = Vertex();
canvas_item_editor->get_viewport_control()->update();
}
return false;
}
} else {
const bool clicked = !edited_path.valid() && mb_down_time > 0 &&
OS::get_singleton()->get_ticks_msec() - mb_down_time < 500;
mb_down_time = 0;
edited_point = PosVertex();
if (clicked) {
undo_redo->create_action(TTR("Insert Curve"));
const SubpathPos insert = mb_down_at;
undo_redo->add_do_method(node_vg, "insert_curve",
insert.subpath, insert.t);
undo_redo->add_undo_method(node_vg, "set_points",
insert.subpath, pre_move_edit);
_commit_action();
selected_point = Vertex(insert.subpath, 3 * (1 + int(Math::floor(insert.t))));
return true;
} else if (edited_path.valid()) {
undo_redo->create_action(TTR("Edit Path"));
undo_redo->add_do_method(node_vg, "set_points",
edited_path.subpath, _get_points(edited_path));
undo_redo->add_undo_method(node_vg, "set_points",
edited_path.subpath, pre_move_edit);
edited_path = SubpathId();
_commit_action();
return true;
}
}
} else if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed() && !edited_point.valid()) {
const PosVertex closest = closest_point(gpoint);
if (closest.valid()) {
remove_point(closest);
return true;
}
}
} /*else if (mode == MODE_DELETE) {
if (mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
const PosVertex closest = closest_point(gpoint);
if (closest.valid()) {
remove_point(closest);
return true;
}
}
}*/
if (mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
return true;
}
}
Ref<InputEventMouseMotion> mm = p_event;
if (mm.is_valid()) {
Vector2 gpoint = mm->get_position();
if (mm->get_button_mask() & BUTTON_MASK_LEFT) {
if (mb_down_time > 0 && gpoint.distance_to(mb_down_where) > 2) {
edited_path = mb_down_at;
mb_down_time = 0;
}
if (edited_point.valid()) {
Vector2 cpoint = _get_node()->get_global_transform().affine_inverse().xform(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint)));
//cpoint = node_vg->get_vg_transform().affine_inverse().xform(cpoint);
edited_point = PosVertex(edited_point, cpoint);
tove::SubpathRef subpath = node_vg->get_subpath(edited_point.subpath);
subpath->move(edited_point.pt, cpoint.x, cpoint.y, aligned_move ? TOVE_HANDLE_ALIGNED : TOVE_HANDLE_FREE);
node_vg->set_dirty();
canvas_item_editor->get_viewport_control()->update();
} else if (edited_path.valid()) {
Vector2 cpoint = _get_node()->get_global_transform().affine_inverse().xform(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint)));
//cpoint = node_vg->get_vg_transform().affine_inverse().xform(cpoint);
tove::SubpathRef subpath = node_vg->get_subpath(mb_down_at.subpath);
subpath->mould(mb_down_at.t, cpoint.x, cpoint.y);
node_vg->set_dirty();
canvas_item_editor->get_viewport_control()->update();
}
} else if (true) {
const PosVertex new_hover_point = closest_point(gpoint);
if (hover_point != new_hover_point) {
hover_point = new_hover_point;
canvas_item_editor->get_viewport_control()->update();
}
}
}
Ref<InputEventKey> k = p_event;
if (k.is_valid() && k->is_pressed()) {
if (k->get_scancode() == KEY_DELETE || k->get_scancode() == KEY_BACKSPACE) {
const Vertex active_point = get_active_point();
if (active_point.valid()) {
remove_point(active_point);
return true;
}
}
}
return false;
}
void forward_canvas_draw_over_viewport(Control *p_overlay)
{
Control *vpc = canvas_item_editor->get_viewport_control();
Transform2D xform = canvas_item_editor->get_canvas_transform() *
_get_node()->get_global_transform();
const Ref<Texture> handle = vg_editor->get_icon("EditorHandle", "EditorIcons");
const tove::PathRef path = node_vg->get_tove_path();
{
const Color line_color = Color(0.5, 0.5, 0.5);
ControlPointIterator cp_iterator(path, selected_point, edited_point);
while (cp_iterator.next()) {
Vector2 p = xform.xform(cp_iterator.get_knot_pos());
Vector2 q = xform.xform(cp_iterator.get_pos());
vpc->draw_line(p, q, line_color, 2 * EDSCALE);
}
}
{
const Vertex active_point = get_active_point();
PointIterator pt_iterator(path);
while (pt_iterator.next()) {
Vector2 p = xform.xform(pt_iterator.get_pos());
const Color modulate = (pt_iterator.get_vertex() == active_point) ?
Color(0.5, 1, 2) : Color(1, 1, 1);
vpc->draw_texture(handle, p - handle->get_size() * 0.5, modulate);
}
}
{
ControlPointIterator cp_iterator(path, selected_point, edited_point);
const Size2 s = handle->get_size() * 0.75;
while (cp_iterator.next()) {
Vector2 p = xform.xform(cp_iterator.get_pos());
Rect2 r = Rect2(p - s / 2, s);
vpc->draw_texture_rect(handle, r, false, Color(1, 1, 1));
}
}
}
};
/* static void reown_children(Node *p_node) {
const int n = p_node->get_child_count();
for (int i = 0; i < n; i++) {
Node *child = p_node->get_child(i);
child->force_parent_owned();
reown_children(child);
}
} */
Array VGCurveTool::_get_points(const SubpathId &p_id) {
tove::PathRef path = node_vg->get_tove_path();
return subpath_points_array(path->getSubpath(p_id.subpath));
}
Node2D *VGEditor::_get_node() const {
return node_vg;
}
void VGEditor::_set_node(Node *p_node_vg) {
if (node_vg) {
node_vg->remove_change_receptor(this);
}
node_vg = Object::cast_to<VGPath>(p_node_vg);
if (node_vg) {
node_vg->add_change_receptor(this);
}
if (!node_vg) {
tool = Ref<VGTool>();
}
}
SubpathId::SubpathId() :
subpath(-1) {
}
SubpathId::SubpathId(int p_subpath) :
subpath(p_subpath) {
}
bool SubpathId::operator==(const SubpathId &p_id) const {
return subpath == p_id.subpath;
}
bool SubpathId::operator!=(const SubpathId &p_id) const {
return !(*this == p_id);
}
bool SubpathId::valid() const {
return subpath >= 0;
}
Vertex::Vertex() :
pt(-1) {
// invalid vertex
}
Vertex::Vertex(int p_pt) :
pt(p_pt) {
}
Vertex::Vertex(int p_subpath, int p_pt) :
SubpathId(p_subpath),
pt(p_pt) {
}
bool Vertex::operator==(const Vertex &p_vertex) const {
return subpath == p_vertex.subpath && pt == p_vertex.pt;
}
bool Vertex::operator!=(const Vertex &p_vertex) const {
return !(*this == p_vertex);
}
bool Vertex::valid() const {
return pt >= 0;
}
PosVertex::PosVertex() {
// invalid vertex
}
PosVertex::PosVertex(const Vertex &p_vertex, const Vector2 &p_pos) :
Vertex(p_vertex.subpath, p_vertex.pt),
pos(p_pos) {
}
PosVertex::PosVertex(int p_subpath, int p_pt, const Vector2 &p_pos) :
Vertex(p_subpath, p_pt),
pos(p_pos) {
}
SubpathPos::SubpathPos() :
t(-1) {
// invalid vertex
}
SubpathPos::SubpathPos(int p_subpath, float p_t) :
SubpathId(p_subpath),
t(p_t) {
// vertex p_vertex of polygon p_polygon
}
bool SubpathPos::valid() const {
return t >= 0;