1 /*************************************************************************/
2 /* graph_edit.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30
31 #include "graph_edit.h"
32
33 #include "core/os/input.h"
34 #include "core/os/keyboard.h"
35 #include "scene/gui/box_container.h"
36
37 #ifdef TOOLS_ENABLED
38 #include "editor/editor_scale.h"
39 #endif
40
41 #define ZOOM_SCALE 1.2
42
43 #define MIN_ZOOM (((1 / ZOOM_SCALE) / ZOOM_SCALE) / ZOOM_SCALE)
44 #define MAX_ZOOM (1 * ZOOM_SCALE * ZOOM_SCALE * ZOOM_SCALE)
45
has_point(const Point2 & p_point) const46 bool GraphEditFilter::has_point(const Point2 &p_point) const {
47
48 return ge->_filter_input(p_point);
49 }
50
GraphEditFilter(GraphEdit * p_edit)51 GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
52
53 ge = p_edit;
54 }
55
connect_node(const StringName & p_from,int p_from_port,const StringName & p_to,int p_to_port)56 Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
57
58 if (is_node_connected(p_from, p_from_port, p_to, p_to_port))
59 return OK;
60 Connection c;
61 c.from = p_from;
62 c.from_port = p_from_port;
63 c.to = p_to;
64 c.to_port = p_to_port;
65 c.activity = 0;
66 connections.push_back(c);
67 top_layer->update();
68 update();
69 connections_layer->update();
70
71 return OK;
72 }
73
is_node_connected(const StringName & p_from,int p_from_port,const StringName & p_to,int p_to_port)74 bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
75
76 for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
77
78 if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port)
79 return true;
80 }
81
82 return false;
83 }
84
disconnect_node(const StringName & p_from,int p_from_port,const StringName & p_to,int p_to_port)85 void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
86
87 for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
88
89 if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) {
90
91 connections.erase(E);
92 top_layer->update();
93 update();
94 connections_layer->update();
95 return;
96 }
97 }
98 }
99
clips_input() const100 bool GraphEdit::clips_input() const {
101
102 return true;
103 }
104
get_connection_list(List<Connection> * r_connections) const105 void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
106
107 *r_connections = connections;
108 }
109
set_scroll_ofs(const Vector2 & p_ofs)110 void GraphEdit::set_scroll_ofs(const Vector2 &p_ofs) {
111
112 setting_scroll_ofs = true;
113 h_scroll->set_value(p_ofs.x);
114 v_scroll->set_value(p_ofs.y);
115 _update_scroll();
116 setting_scroll_ofs = false;
117 }
118
get_scroll_ofs() const119 Vector2 GraphEdit::get_scroll_ofs() const {
120
121 return Vector2(h_scroll->get_value(), v_scroll->get_value());
122 }
123
_scroll_moved(double)124 void GraphEdit::_scroll_moved(double) {
125
126 if (!awaiting_scroll_offset_update) {
127 call_deferred("_update_scroll_offset");
128 awaiting_scroll_offset_update = true;
129 }
130 top_layer->update();
131 update();
132
133 if (!setting_scroll_ofs) { //in godot, signals on change value are avoided as a convention
134 emit_signal("scroll_offset_changed", get_scroll_ofs());
135 }
136 }
137
_update_scroll_offset()138 void GraphEdit::_update_scroll_offset() {
139
140 set_block_minimum_size_adjust(true);
141
142 for (int i = 0; i < get_child_count(); i++) {
143
144 GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
145 if (!gn)
146 continue;
147
148 Point2 pos = gn->get_offset() * zoom;
149 pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
150 gn->set_position(pos);
151 if (gn->get_scale() != Vector2(zoom, zoom)) {
152 gn->set_scale(Vector2(zoom, zoom));
153 }
154 }
155
156 connections_layer->set_position(-Point2(h_scroll->get_value(), v_scroll->get_value()));
157 set_block_minimum_size_adjust(false);
158 awaiting_scroll_offset_update = false;
159 }
160
_update_scroll()161 void GraphEdit::_update_scroll() {
162
163 if (updating)
164 return;
165
166 updating = true;
167
168 set_block_minimum_size_adjust(true);
169
170 Rect2 screen;
171 for (int i = 0; i < get_child_count(); i++) {
172
173 GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
174 if (!gn)
175 continue;
176
177 Rect2 r;
178 r.position = gn->get_offset() * zoom;
179 r.size = gn->get_size() * zoom;
180 screen = screen.merge(r);
181 }
182
183 screen.position -= get_size();
184 screen.size += get_size() * 2.0;
185
186 h_scroll->set_min(screen.position.x);
187 h_scroll->set_max(screen.position.x + screen.size.x);
188 h_scroll->set_page(get_size().x);
189 if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page())
190 h_scroll->hide();
191 else
192 h_scroll->show();
193
194 v_scroll->set_min(screen.position.y);
195 v_scroll->set_max(screen.position.y + screen.size.y);
196 v_scroll->set_page(get_size().y);
197
198 if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page())
199 v_scroll->hide();
200 else
201 v_scroll->show();
202
203 Size2 hmin = h_scroll->get_combined_minimum_size();
204 Size2 vmin = v_scroll->get_combined_minimum_size();
205
206 // Avoid scrollbar overlapping.
207 h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, v_scroll->is_visible() ? -vmin.width : 0);
208 v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, h_scroll->is_visible() ? -hmin.height : 0);
209
210 set_block_minimum_size_adjust(false);
211
212 if (!awaiting_scroll_offset_update) {
213 call_deferred("_update_scroll_offset");
214 awaiting_scroll_offset_update = true;
215 }
216
217 updating = false;
218 }
219
_graph_node_raised(Node * p_gn)220 void GraphEdit::_graph_node_raised(Node *p_gn) {
221
222 GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
223 ERR_FAIL_COND(!gn);
224 if (gn->is_comment()) {
225 move_child(gn, 0);
226 } else {
227 gn->raise();
228 }
229 int first_not_comment = 0;
230 for (int i = 0; i < get_child_count(); i++) {
231 GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
232 if (gn2 && !gn2->is_comment()) {
233 first_not_comment = i;
234 break;
235 }
236 }
237
238 move_child(connections_layer, first_not_comment);
239 top_layer->raise();
240 emit_signal("node_selected", p_gn);
241 }
242
_graph_node_moved(Node * p_gn)243 void GraphEdit::_graph_node_moved(Node *p_gn) {
244
245 GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
246 ERR_FAIL_COND(!gn);
247 top_layer->update();
248 update();
249 connections_layer->update();
250 }
251
add_child_notify(Node * p_child)252 void GraphEdit::add_child_notify(Node *p_child) {
253
254 Control::add_child_notify(p_child);
255
256 top_layer->call_deferred("raise"); //top layer always on top!
257 GraphNode *gn = Object::cast_to<GraphNode>(p_child);
258 if (gn) {
259 gn->set_scale(Vector2(zoom, zoom));
260 gn->connect("offset_changed", this, "_graph_node_moved", varray(gn));
261 gn->connect("raise_request", this, "_graph_node_raised", varray(gn));
262 gn->connect("item_rect_changed", connections_layer, "update");
263 _graph_node_moved(gn);
264 gn->set_mouse_filter(MOUSE_FILTER_PASS);
265 }
266 }
267
remove_child_notify(Node * p_child)268 void GraphEdit::remove_child_notify(Node *p_child) {
269
270 Control::remove_child_notify(p_child);
271 if (is_inside_tree()) {
272 top_layer->call_deferred("raise"); //top layer always on top!
273 }
274 GraphNode *gn = Object::cast_to<GraphNode>(p_child);
275 if (gn) {
276 gn->disconnect("offset_changed", this, "_graph_node_moved");
277 gn->disconnect("raise_request", this, "_graph_node_raised");
278 gn->disconnect("item_rect_changed", connections_layer, "update");
279 }
280 }
281
_notification(int p_what)282 void GraphEdit::_notification(int p_what) {
283
284 if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
285 port_grab_distance_horizontal = get_constant("port_grab_distance_horizontal");
286 port_grab_distance_vertical = get_constant("port_grab_distance_vertical");
287
288 zoom_minus->set_icon(get_icon("minus"));
289 zoom_reset->set_icon(get_icon("reset"));
290 zoom_plus->set_icon(get_icon("more"));
291 snap_button->set_icon(get_icon("snap"));
292 }
293 if (p_what == NOTIFICATION_READY) {
294 Size2 hmin = h_scroll->get_combined_minimum_size();
295 Size2 vmin = v_scroll->get_combined_minimum_size();
296
297 h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0);
298 h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
299 h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height);
300 h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
301
302 v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width);
303 v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
304 v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
305 v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
306 }
307 if (p_what == NOTIFICATION_DRAW) {
308
309 draw_style_box(get_stylebox("bg"), Rect2(Point2(), get_size()));
310
311 if (is_using_snap()) {
312 //draw grid
313
314 int snap = get_snap();
315
316 Vector2 offset = get_scroll_ofs() / zoom;
317 Size2 size = get_size() / zoom;
318
319 Point2i from = (offset / float(snap)).floor();
320 Point2i len = (size / float(snap)).floor() + Vector2(1, 1);
321
322 Color grid_minor = get_color("grid_minor");
323 Color grid_major = get_color("grid_major");
324
325 for (int i = from.x; i < from.x + len.x; i++) {
326
327 Color color;
328
329 if (ABS(i) % 10 == 0)
330 color = grid_major;
331 else
332 color = grid_minor;
333
334 float base_ofs = i * snap * zoom - offset.x * zoom;
335 draw_line(Vector2(base_ofs, 0), Vector2(base_ofs, get_size().height), color);
336 }
337
338 for (int i = from.y; i < from.y + len.y; i++) {
339
340 Color color;
341
342 if (ABS(i) % 10 == 0)
343 color = grid_major;
344 else
345 color = grid_minor;
346
347 float base_ofs = i * snap * zoom - offset.y * zoom;
348 draw_line(Vector2(0, base_ofs), Vector2(get_size().width, base_ofs), color);
349 }
350 }
351 }
352
353 if (p_what == NOTIFICATION_RESIZED) {
354 _update_scroll();
355 top_layer->update();
356 }
357 }
358
_filter_input(const Point2 & p_point)359 bool GraphEdit::_filter_input(const Point2 &p_point) {
360
361 Ref<Texture> port = get_icon("port", "GraphNode");
362
363 for (int i = get_child_count() - 1; i >= 0; i--) {
364
365 GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
366 if (!gn)
367 continue;
368
369 for (int j = 0; j < gn->get_connection_output_count(); j++) {
370
371 Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
372 if (is_in_hot_zone(pos, p_point))
373 return true;
374 }
375
376 for (int j = 0; j < gn->get_connection_input_count(); j++) {
377
378 Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
379 if (is_in_hot_zone(pos, p_point)) {
380 return true;
381 }
382 }
383 }
384
385 return false;
386 }
387
_top_layer_input(const Ref<InputEvent> & p_ev)388 void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
389
390 Ref<InputEventMouseButton> mb = p_ev;
391 if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
392
393 Ref<Texture> port = get_icon("port", "GraphNode");
394 Vector2 mpos(mb->get_position().x, mb->get_position().y);
395 for (int i = get_child_count() - 1; i >= 0; i--) {
396
397 GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
398 if (!gn)
399 continue;
400
401 for (int j = 0; j < gn->get_connection_output_count(); j++) {
402
403 Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
404 if (is_in_hot_zone(pos, mpos)) {
405
406 if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) {
407 //check disconnect
408 for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
409
410 if (E->get().from == gn->get_name() && E->get().from_port == j) {
411
412 Node *to = get_node(String(E->get().to));
413 if (Object::cast_to<GraphNode>(to)) {
414
415 connecting_from = E->get().to;
416 connecting_index = E->get().to_port;
417 connecting_out = false;
418 connecting_type = Object::cast_to<GraphNode>(to)->get_connection_input_type(E->get().to_port);
419 connecting_color = Object::cast_to<GraphNode>(to)->get_connection_input_color(E->get().to_port);
420 connecting_target = false;
421 connecting_to = pos;
422 just_disconnected = true;
423
424 emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
425 to = get_node(String(connecting_from)); //maybe it was erased
426 if (Object::cast_to<GraphNode>(to)) {
427 connecting = true;
428 }
429 return;
430 }
431 }
432 }
433 }
434
435 connecting = true;
436 connecting_from = gn->get_name();
437 connecting_index = j;
438 connecting_out = true;
439 connecting_type = gn->get_connection_output_type(j);
440 connecting_color = gn->get_connection_output_color(j);
441 connecting_target = false;
442 connecting_to = pos;
443 just_disconnected = false;
444 return;
445 }
446 }
447
448 for (int j = 0; j < gn->get_connection_input_count(); j++) {
449
450 Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
451 if (is_in_hot_zone(pos, mpos)) {
452
453 if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) {
454 //check disconnect
455 for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
456
457 if (E->get().to == gn->get_name() && E->get().to_port == j) {
458
459 Node *fr = get_node(String(E->get().from));
460 if (Object::cast_to<GraphNode>(fr)) {
461
462 connecting_from = E->get().from;
463 connecting_index = E->get().from_port;
464 connecting_out = true;
465 connecting_type = Object::cast_to<GraphNode>(fr)->get_connection_output_type(E->get().from_port);
466 connecting_color = Object::cast_to<GraphNode>(fr)->get_connection_output_color(E->get().from_port);
467 connecting_target = false;
468 connecting_to = pos;
469 just_disconnected = true;
470
471 emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
472 fr = get_node(String(connecting_from)); //maybe it was erased
473 if (Object::cast_to<GraphNode>(fr)) {
474 connecting = true;
475 }
476 return;
477 }
478 }
479 }
480 }
481
482 connecting = true;
483 connecting_from = gn->get_name();
484 connecting_index = j;
485 connecting_out = false;
486 connecting_type = gn->get_connection_input_type(j);
487 connecting_color = gn->get_connection_input_color(j);
488 connecting_target = false;
489 connecting_to = pos;
490 just_disconnected = false;
491
492 return;
493 }
494 }
495 }
496 }
497
498 Ref<InputEventMouseMotion> mm = p_ev;
499 if (mm.is_valid() && connecting) {
500
501 connecting_to = mm->get_position();
502 connecting_target = false;
503 top_layer->update();
504
505 Ref<Texture> port = get_icon("port", "GraphNode");
506 Vector2 mpos = mm->get_position();
507 for (int i = get_child_count() - 1; i >= 0; i--) {
508
509 GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
510 if (!gn)
511 continue;
512
513 if (!connecting_out) {
514 for (int j = 0; j < gn->get_connection_output_count(); j++) {
515
516 Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
517 int type = gn->get_connection_output_type(j);
518 if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) {
519
520 connecting_target = true;
521 connecting_to = pos;
522 connecting_target_to = gn->get_name();
523 connecting_target_index = j;
524 return;
525 }
526 }
527 } else {
528
529 for (int j = 0; j < gn->get_connection_input_count(); j++) {
530
531 Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
532 int type = gn->get_connection_input_type(j);
533 if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) {
534 connecting_target = true;
535 connecting_to = pos;
536 connecting_target_to = gn->get_name();
537 connecting_target_index = j;
538 return;
539 }
540 }
541 }
542 }
543 }
544
545 if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) {
546
547 if (connecting && connecting_target) {
548
549 String from = connecting_from;
550 int from_slot = connecting_index;
551 String to = connecting_target_to;
552 int to_slot = connecting_target_index;
553
554 if (!connecting_out) {
555 SWAP(from, to);
556 SWAP(from_slot, to_slot);
557 }
558 emit_signal("connection_request", from, from_slot, to, to_slot);
559
560 } else if (!just_disconnected) {
561
562 String from = connecting_from;
563 int from_slot = connecting_index;
564 Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y);
565
566 if (!connecting_out) {
567 emit_signal("connection_from_empty", from, from_slot, ofs);
568 } else {
569 emit_signal("connection_to_empty", from, from_slot, ofs);
570 }
571 }
572
573 connecting = false;
574 top_layer->update();
575 update();
576 connections_layer->update();
577 }
578 }
579
_check_clickable_control(Control * p_control,const Vector2 & pos)580 bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &pos) {
581
582 if (p_control->is_set_as_toplevel() || !p_control->is_visible())
583 return false;
584
585 if (!p_control->has_point(pos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
586 //test children
587 for (int i = 0; i < p_control->get_child_count(); i++) {
588 Control *subchild = Object::cast_to<Control>(p_control->get_child(i));
589 if (!subchild)
590 continue;
591 if (_check_clickable_control(subchild, pos - subchild->get_position())) {
592 return true;
593 }
594 }
595
596 return false;
597 } else {
598 return true;
599 }
600 }
601
is_in_hot_zone(const Vector2 & pos,const Vector2 & p_mouse_pos)602 bool GraphEdit::is_in_hot_zone(const Vector2 &pos, const Vector2 &p_mouse_pos) {
603 if (!Rect2(pos.x - port_grab_distance_horizontal, pos.y - port_grab_distance_vertical, port_grab_distance_horizontal * 2, port_grab_distance_vertical * 2).has_point(p_mouse_pos))
604 return false;
605
606 for (int i = 0; i < get_child_count(); i++) {
607 Control *child = Object::cast_to<Control>(get_child(i));
608 if (!child)
609 continue;
610 Rect2 rect = child->get_rect();
611 if (rect.has_point(p_mouse_pos)) {
612
613 //check sub-controls
614 Vector2 subpos = p_mouse_pos - rect.position;
615
616 for (int j = 0; j < child->get_child_count(); j++) {
617 Control *subchild = Object::cast_to<Control>(child->get_child(j));
618 if (!subchild)
619 continue;
620
621 if (_check_clickable_control(subchild, subpos - subchild->get_position())) {
622 return false;
623 }
624 }
625 }
626 }
627
628 return true;
629 }
630
631 template <class Vector2>
_bezier_interp(real_t t,Vector2 start,Vector2 control_1,Vector2 control_2,Vector2 end)632 static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, Vector2 start, Vector2 control_1, Vector2 control_2, Vector2 end) {
633 /* Formula from Wikipedia article on Bezier curves. */
634 real_t omt = (1.0 - t);
635 real_t omt2 = omt * omt;
636 real_t omt3 = omt2 * omt;
637 real_t t2 = t * t;
638 real_t t3 = t2 * t;
639
640 return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
641 }
642
_bake_segment2d(Vector<Vector2> & points,Vector<Color> & colors,float p_begin,float p_end,const Vector2 & p_a,const Vector2 & p_out,const Vector2 & p_b,const Vector2 & p_in,int p_depth,int p_min_depth,int p_max_depth,float p_tol,const Color & p_color,const Color & p_to_color,int & lines) const643 void GraphEdit::_bake_segment2d(Vector<Vector2> &points, Vector<Color> &colors, float p_begin, float p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_min_depth, int p_max_depth, float p_tol, const Color &p_color, const Color &p_to_color, int &lines) const {
644
645 float mp = p_begin + (p_end - p_begin) * 0.5;
646 Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
647 Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
648 Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
649
650 Vector2 na = (mid - beg).normalized();
651 Vector2 nb = (end - mid).normalized();
652 float dp = Math::rad2deg(Math::acos(na.dot(nb)));
653
654 if (p_depth >= p_min_depth && (dp < p_tol || p_depth >= p_max_depth)) {
655
656 points.push_back((beg + end) * 0.5);
657 colors.push_back(p_color.linear_interpolate(p_to_color, mp));
658 lines++;
659 } else {
660 _bake_segment2d(points, colors, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_min_depth, p_max_depth, p_tol, p_color, p_to_color, lines);
661 _bake_segment2d(points, colors, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_min_depth, p_max_depth, p_tol, p_color, p_to_color, lines);
662 }
663 }
664
_draw_cos_line(CanvasItem * p_where,const Vector2 & p_from,const Vector2 & p_to,const Color & p_color,const Color & p_to_color)665 void GraphEdit::_draw_cos_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color) {
666
667 //cubic bezier code
668 float diff = p_to.x - p_from.x;
669 float cp_offset;
670 int cp_len = get_constant("bezier_len_pos");
671 int cp_neg_len = get_constant("bezier_len_neg");
672
673 if (diff > 0) {
674 cp_offset = MIN(cp_len, diff * 0.5);
675 } else {
676 cp_offset = MAX(MIN(cp_len - diff, cp_neg_len), -diff * 0.5);
677 }
678
679 Vector2 c1 = Vector2(cp_offset * zoom, 0);
680 Vector2 c2 = Vector2(-cp_offset * zoom, 0);
681
682 int lines = 0;
683
684 Vector<Point2> points;
685 Vector<Color> colors;
686 points.push_back(p_from);
687 colors.push_back(p_color);
688 _bake_segment2d(points, colors, 0, 1, p_from, c1, p_to, c2, 0, 3, 9, 3, p_color, p_to_color, lines);
689 points.push_back(p_to);
690 colors.push_back(p_to_color);
691
692 #ifdef TOOLS_ENABLED
693 p_where->draw_polyline_colors(points, colors, Math::floor(2 * EDSCALE), true);
694 #else
695 p_where->draw_polyline_colors(points, colors, 2, true);
696 #endif
697 }
698
_connections_layer_draw()699 void GraphEdit::_connections_layer_draw() {
700
701 Color activity_color = get_color("activity");
702 //draw connections
703 List<List<Connection>::Element *> to_erase;
704 for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
705
706 NodePath fromnp(E->get().from);
707
708 Node *from = get_node(fromnp);
709 if (!from) {
710 to_erase.push_back(E);
711 continue;
712 }
713
714 GraphNode *gfrom = Object::cast_to<GraphNode>(from);
715
716 if (!gfrom) {
717 to_erase.push_back(E);
718 continue;
719 }
720
721 NodePath tonp(E->get().to);
722 Node *to = get_node(tonp);
723 if (!to) {
724 to_erase.push_back(E);
725 continue;
726 }
727
728 GraphNode *gto = Object::cast_to<GraphNode>(to);
729
730 if (!gto) {
731 to_erase.push_back(E);
732 continue;
733 }
734
735 Vector2 frompos = gfrom->get_connection_output_position(E->get().from_port) + gfrom->get_offset() * zoom;
736 Color color = gfrom->get_connection_output_color(E->get().from_port);
737 Vector2 topos = gto->get_connection_input_position(E->get().to_port) + gto->get_offset() * zoom;
738 Color tocolor = gto->get_connection_input_color(E->get().to_port);
739
740 if (E->get().activity > 0) {
741 color = color.linear_interpolate(activity_color, E->get().activity);
742 tocolor = tocolor.linear_interpolate(activity_color, E->get().activity);
743 }
744 _draw_cos_line(connections_layer, frompos, topos, color, tocolor);
745 }
746
747 while (to_erase.size()) {
748 connections.erase(to_erase.front()->get());
749 to_erase.pop_front();
750 }
751 }
752
_top_layer_draw()753 void GraphEdit::_top_layer_draw() {
754
755 _update_scroll();
756
757 if (connecting) {
758
759 Node *fromn = get_node(connecting_from);
760 ERR_FAIL_COND(!fromn);
761 GraphNode *from = Object::cast_to<GraphNode>(fromn);
762 ERR_FAIL_COND(!from);
763 Vector2 pos;
764 if (connecting_out)
765 pos = from->get_connection_output_position(connecting_index);
766 else
767 pos = from->get_connection_input_position(connecting_index);
768 pos += from->get_position();
769
770 Vector2 topos;
771 topos = connecting_to;
772
773 Color col = connecting_color;
774
775 if (connecting_target) {
776 col.r += 0.4;
777 col.g += 0.4;
778 col.b += 0.4;
779 }
780
781 if (!connecting_out) {
782 SWAP(pos, topos);
783 }
784 _draw_cos_line(top_layer, pos, topos, col, col);
785 }
786
787 if (box_selecting) {
788 top_layer->draw_rect(box_selecting_rect, get_color("selection_fill"));
789 top_layer->draw_rect(box_selecting_rect, get_color("selection_stroke"), false);
790 }
791 }
792
set_selected(Node * p_child)793 void GraphEdit::set_selected(Node *p_child) {
794
795 for (int i = get_child_count() - 1; i >= 0; i--) {
796
797 GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
798 if (!gn)
799 continue;
800
801 gn->set_selected(gn == p_child);
802 }
803 }
804
_gui_input(const Ref<InputEvent> & p_ev)805 void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) {
806
807 Ref<InputEventMouseMotion> mm = p_ev;
808 if (mm.is_valid() && (mm->get_button_mask() & BUTTON_MASK_MIDDLE || (mm->get_button_mask() & BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) {
809 h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
810 v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
811 }
812
813 if (mm.is_valid() && dragging) {
814
815 just_selected = true;
816 drag_accum += mm->get_relative();
817 for (int i = get_child_count() - 1; i >= 0; i--) {
818 GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
819 if (gn && gn->is_selected()) {
820
821 Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
822
823 // Snapping can be toggled temporarily by holding down Ctrl.
824 // This is done here as to not toggle the grid when holding down Ctrl.
825 if (is_using_snap() ^ Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
826 const int snap = get_snap();
827 pos = pos.snapped(Vector2(snap, snap));
828 }
829
830 gn->set_offset(pos);
831 }
832 }
833 }
834
835 if (mm.is_valid() && box_selecting) {
836 box_selecting_to = mm->get_position();
837
838 box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
839 MIN(box_selecting_from.y, box_selecting_to.y),
840 ABS(box_selecting_from.x - box_selecting_to.x),
841 ABS(box_selecting_from.y - box_selecting_to.y));
842
843 for (int i = get_child_count() - 1; i >= 0; i--) {
844
845 GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
846 if (!gn)
847 continue;
848
849 Rect2 r = gn->get_rect();
850 r.size *= zoom;
851 bool in_box = r.intersects(box_selecting_rect);
852
853 if (in_box) {
854 if (!gn->is_selected() && box_selection_mode_additive) {
855 emit_signal("node_selected", gn);
856 } else if (gn->is_selected() && !box_selection_mode_additive) {
857 emit_signal("node_unselected", gn);
858 }
859 gn->set_selected(box_selection_mode_additive);
860 } else {
861 bool select = (previus_selected.find(gn) != NULL);
862 if (gn->is_selected() && !select) {
863 emit_signal("node_unselected", gn);
864 } else if (!gn->is_selected() && select) {
865 emit_signal("node_selected", gn);
866 }
867 gn->set_selected(select);
868 }
869 }
870
871 top_layer->update();
872 }
873
874 Ref<InputEventMouseButton> b = p_ev;
875 if (b.is_valid()) {
876
877 if (b->get_button_index() == BUTTON_RIGHT && b->is_pressed()) {
878 if (box_selecting) {
879 box_selecting = false;
880 for (int i = get_child_count() - 1; i >= 0; i--) {
881
882 GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
883 if (!gn)
884 continue;
885
886 bool select = (previus_selected.find(gn) != NULL);
887 if (gn->is_selected() && !select) {
888 emit_signal("node_unselected", gn);
889 } else if (!gn->is_selected() && select) {
890 emit_signal("node_selected", gn);
891 }
892 gn->set_selected(select);
893 }
894 top_layer->update();
895 } else {
896 if (connecting) {
897 connecting = false;
898 top_layer->update();
899 } else {
900 emit_signal("popup_request", b->get_global_position());
901 }
902 }
903 }
904
905 if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && dragging) {
906 if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
907 //deselect current node
908 for (int i = get_child_count() - 1; i >= 0; i--) {
909 GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
910
911 if (gn) {
912 Rect2 r = gn->get_rect();
913 r.size *= zoom;
914 if (r.has_point(b->get_position())) {
915 emit_signal("node_unselected", gn);
916 gn->set_selected(false);
917 }
918 }
919 }
920 }
921
922 if (drag_accum != Vector2()) {
923
924 emit_signal("_begin_node_move");
925
926 for (int i = get_child_count() - 1; i >= 0; i--) {
927 GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
928 if (gn && gn->is_selected())
929 gn->set_drag(false);
930 }
931
932 emit_signal("_end_node_move");
933 }
934
935 dragging = false;
936
937 top_layer->update();
938 update();
939 connections_layer->update();
940 }
941
942 if (b->get_button_index() == BUTTON_LEFT && b->is_pressed()) {
943
944 GraphNode *gn = NULL;
945
946 for (int i = get_child_count() - 1; i >= 0; i--) {
947
948 GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));
949
950 if (gn_selected) {
951 if (gn_selected->is_resizing())
952 continue;
953
954 if (gn_selected->has_point(b->get_position() - gn_selected->get_position())) {
955 gn = gn_selected;
956 break;
957 }
958 }
959 }
960
961 if (gn) {
962
963 if (_filter_input(b->get_position()))
964 return;
965
966 dragging = true;
967 drag_accum = Vector2();
968 just_selected = !gn->is_selected();
969 if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
970 for (int i = 0; i < get_child_count(); i++) {
971 GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
972 if (o_gn) {
973 if (o_gn == gn) {
974 o_gn->set_selected(true);
975 } else {
976 if (o_gn->is_selected()) {
977 emit_signal("node_unselected", o_gn);
978 }
979 o_gn->set_selected(false);
980 }
981 }
982 }
983 }
984
985 gn->set_selected(true);
986 for (int i = 0; i < get_child_count(); i++) {
987 GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
988 if (!o_gn)
989 continue;
990 if (o_gn->is_selected())
991 o_gn->set_drag(true);
992 }
993
994 } else {
995 if (_filter_input(b->get_position()))
996 return;
997 if (Input::get_singleton()->is_key_pressed(KEY_SPACE))
998 return;
999
1000 box_selecting = true;
1001 box_selecting_from = b->get_position();
1002 if (b->get_control()) {
1003 box_selection_mode_additive = true;
1004 previus_selected.clear();
1005 for (int i = get_child_count() - 1; i >= 0; i--) {
1006
1007 GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
1008 if (!gn2 || !gn2->is_selected())
1009 continue;
1010
1011 previus_selected.push_back(gn2);
1012 }
1013 } else if (b->get_shift()) {
1014 box_selection_mode_additive = false;
1015 previus_selected.clear();
1016 for (int i = get_child_count() - 1; i >= 0; i--) {
1017
1018 GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
1019 if (!gn2 || !gn2->is_selected())
1020 continue;
1021
1022 previus_selected.push_back(gn2);
1023 }
1024 } else {
1025 box_selection_mode_additive = true;
1026 previus_selected.clear();
1027 for (int i = get_child_count() - 1; i >= 0; i--) {
1028
1029 GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
1030 if (!gn2)
1031 continue;
1032 if (gn2->is_selected()) {
1033 emit_signal("node_unselected", gn2);
1034 }
1035 gn2->set_selected(false);
1036 }
1037 }
1038 }
1039 }
1040
1041 if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && box_selecting) {
1042 box_selecting = false;
1043 previus_selected.clear();
1044 top_layer->update();
1045 }
1046
1047 if (b->get_button_index() == BUTTON_WHEEL_UP && b->is_pressed()) {
1048 //too difficult to get right
1049 //set_zoom(zoom*ZOOM_SCALE);
1050 }
1051
1052 if (b->get_button_index() == BUTTON_WHEEL_DOWN && b->is_pressed()) {
1053 //too difficult to get right
1054 //set_zoom(zoom/ZOOM_SCALE);
1055 }
1056 if (b->get_button_index() == BUTTON_WHEEL_UP && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
1057 v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b->get_factor() / 8);
1058 }
1059 if (b->get_button_index() == BUTTON_WHEEL_DOWN && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
1060 v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b->get_factor() / 8);
1061 }
1062 if (b->get_button_index() == BUTTON_WHEEL_RIGHT || (b->get_button_index() == BUTTON_WHEEL_DOWN && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
1063 h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * b->get_factor() / 8);
1064 }
1065 if (b->get_button_index() == BUTTON_WHEEL_LEFT || (b->get_button_index() == BUTTON_WHEEL_UP && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
1066 h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * b->get_factor() / 8);
1067 }
1068 }
1069
1070 Ref<InputEventKey> k = p_ev;
1071
1072 if (k.is_valid()) {
1073
1074 if (k->get_scancode() == KEY_D && k->is_pressed() && k->get_command()) {
1075 emit_signal("duplicate_nodes_request");
1076 accept_event();
1077 }
1078
1079 if (k->get_scancode() == KEY_C && k->is_pressed() && k->get_command()) {
1080 emit_signal("copy_nodes_request");
1081 accept_event();
1082 }
1083
1084 if (k->get_scancode() == KEY_V && k->is_pressed() && k->get_command()) {
1085 emit_signal("paste_nodes_request");
1086 accept_event();
1087 }
1088
1089 if (k->get_scancode() == KEY_DELETE && k->is_pressed()) {
1090 emit_signal("delete_nodes_request");
1091 accept_event();
1092 }
1093 }
1094
1095 Ref<InputEventMagnifyGesture> magnify_gesture = p_ev;
1096 if (magnify_gesture.is_valid()) {
1097
1098 set_zoom_custom(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
1099 }
1100
1101 Ref<InputEventPanGesture> pan_gesture = p_ev;
1102 if (pan_gesture.is_valid()) {
1103
1104 h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
1105 v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
1106 }
1107 }
1108
set_connection_activity(const StringName & p_from,int p_from_port,const StringName & p_to,int p_to_port,float p_activity)1109 void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
1110
1111 for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
1112
1113 if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) {
1114
1115 if (Math::is_equal_approx(E->get().activity, p_activity)) {
1116 //update only if changed
1117 top_layer->update();
1118 connections_layer->update();
1119 }
1120 E->get().activity = p_activity;
1121 return;
1122 }
1123 }
1124 }
1125
clear_connections()1126 void GraphEdit::clear_connections() {
1127
1128 connections.clear();
1129 update();
1130 connections_layer->update();
1131 }
1132
set_zoom(float p_zoom)1133 void GraphEdit::set_zoom(float p_zoom) {
1134
1135 set_zoom_custom(p_zoom, get_size() / 2);
1136 }
1137
set_zoom_custom(float p_zoom,const Vector2 & p_center)1138 void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
1139
1140 p_zoom = CLAMP(p_zoom, MIN_ZOOM, MAX_ZOOM);
1141 if (zoom == p_zoom)
1142 return;
1143
1144 zoom_minus->set_disabled(zoom == MIN_ZOOM);
1145 zoom_plus->set_disabled(zoom == MAX_ZOOM);
1146
1147 Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + p_center) / zoom;
1148
1149 zoom = p_zoom;
1150 top_layer->update();
1151
1152 _update_scroll();
1153 connections_layer->update();
1154
1155 if (is_visible_in_tree()) {
1156
1157 Vector2 ofs = sbofs * zoom - p_center;
1158 h_scroll->set_value(ofs.x);
1159 v_scroll->set_value(ofs.y);
1160 }
1161
1162 update();
1163 }
1164
get_zoom() const1165 float GraphEdit::get_zoom() const {
1166 return zoom;
1167 }
1168
set_right_disconnects(bool p_enable)1169 void GraphEdit::set_right_disconnects(bool p_enable) {
1170
1171 right_disconnects = p_enable;
1172 }
1173
is_right_disconnects_enabled() const1174 bool GraphEdit::is_right_disconnects_enabled() const {
1175
1176 return right_disconnects;
1177 }
1178
add_valid_right_disconnect_type(int p_type)1179 void GraphEdit::add_valid_right_disconnect_type(int p_type) {
1180
1181 valid_right_disconnect_types.insert(p_type);
1182 }
1183
remove_valid_right_disconnect_type(int p_type)1184 void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
1185
1186 valid_right_disconnect_types.erase(p_type);
1187 }
1188
add_valid_left_disconnect_type(int p_type)1189 void GraphEdit::add_valid_left_disconnect_type(int p_type) {
1190
1191 valid_left_disconnect_types.insert(p_type);
1192 }
1193
remove_valid_left_disconnect_type(int p_type)1194 void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
1195
1196 valid_left_disconnect_types.erase(p_type);
1197 }
1198
_get_connection_list() const1199 Array GraphEdit::_get_connection_list() const {
1200
1201 List<Connection> conns;
1202 get_connection_list(&conns);
1203 Array arr;
1204 for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
1205 Dictionary d;
1206 d["from"] = E->get().from;
1207 d["from_port"] = E->get().from_port;
1208 d["to"] = E->get().to;
1209 d["to_port"] = E->get().to_port;
1210 arr.push_back(d);
1211 }
1212 return arr;
1213 }
1214
_zoom_minus()1215 void GraphEdit::_zoom_minus() {
1216
1217 set_zoom(zoom / ZOOM_SCALE);
1218 }
_zoom_reset()1219 void GraphEdit::_zoom_reset() {
1220
1221 set_zoom(1);
1222 }
1223
_zoom_plus()1224 void GraphEdit::_zoom_plus() {
1225
1226 set_zoom(zoom * ZOOM_SCALE);
1227 }
1228
add_valid_connection_type(int p_type,int p_with_type)1229 void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
1230
1231 ConnType ct;
1232 ct.type_a = p_type;
1233 ct.type_b = p_with_type;
1234
1235 valid_connection_types.insert(ct);
1236 }
1237
remove_valid_connection_type(int p_type,int p_with_type)1238 void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
1239
1240 ConnType ct;
1241 ct.type_a = p_type;
1242 ct.type_b = p_with_type;
1243
1244 valid_connection_types.erase(ct);
1245 }
1246
is_valid_connection_type(int p_type,int p_with_type) const1247 bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
1248
1249 ConnType ct;
1250 ct.type_a = p_type;
1251 ct.type_b = p_with_type;
1252
1253 return valid_connection_types.has(ct);
1254 }
1255
set_use_snap(bool p_enable)1256 void GraphEdit::set_use_snap(bool p_enable) {
1257
1258 snap_button->set_pressed(p_enable);
1259 update();
1260 }
1261
is_using_snap() const1262 bool GraphEdit::is_using_snap() const {
1263
1264 return snap_button->is_pressed();
1265 }
1266
get_snap() const1267 int GraphEdit::get_snap() const {
1268
1269 return snap_amount->get_value();
1270 }
1271
set_snap(int p_snap)1272 void GraphEdit::set_snap(int p_snap) {
1273
1274 ERR_FAIL_COND(p_snap < 5);
1275 snap_amount->set_value(p_snap);
1276 update();
1277 }
_snap_toggled()1278 void GraphEdit::_snap_toggled() {
1279 update();
1280 }
1281
_snap_value_changed(double)1282 void GraphEdit::_snap_value_changed(double) {
1283
1284 update();
1285 }
1286
get_zoom_hbox()1287 HBoxContainer *GraphEdit::get_zoom_hbox() {
1288 return zoom_hb;
1289 }
1290
_bind_methods()1291 void GraphEdit::_bind_methods() {
1292
1293 ClassDB::bind_method(D_METHOD("connect_node", "from", "from_port", "to", "to_port"), &GraphEdit::connect_node);
1294 ClassDB::bind_method(D_METHOD("is_node_connected", "from", "from_port", "to", "to_port"), &GraphEdit::is_node_connected);
1295 ClassDB::bind_method(D_METHOD("disconnect_node", "from", "from_port", "to", "to_port"), &GraphEdit::disconnect_node);
1296 ClassDB::bind_method(D_METHOD("set_connection_activity", "from", "from_port", "to", "to_port", "amount"), &GraphEdit::set_connection_activity);
1297 ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
1298 ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
1299 ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
1300 ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs);
1301
1302 ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
1303 ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
1304 ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
1305 ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
1306 ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
1307 ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
1308 ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
1309
1310 ClassDB::bind_method(D_METHOD("set_zoom", "p_zoom"), &GraphEdit::set_zoom);
1311 ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
1312
1313 ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
1314 ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
1315
1316 ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
1317 ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
1318
1319 ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
1320 ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
1321
1322 ClassDB::bind_method(D_METHOD("_graph_node_moved"), &GraphEdit::_graph_node_moved);
1323 ClassDB::bind_method(D_METHOD("_graph_node_raised"), &GraphEdit::_graph_node_raised);
1324
1325 ClassDB::bind_method(D_METHOD("_top_layer_input"), &GraphEdit::_top_layer_input);
1326 ClassDB::bind_method(D_METHOD("_top_layer_draw"), &GraphEdit::_top_layer_draw);
1327 ClassDB::bind_method(D_METHOD("_scroll_moved"), &GraphEdit::_scroll_moved);
1328 ClassDB::bind_method(D_METHOD("_zoom_minus"), &GraphEdit::_zoom_minus);
1329 ClassDB::bind_method(D_METHOD("_zoom_reset"), &GraphEdit::_zoom_reset);
1330 ClassDB::bind_method(D_METHOD("_zoom_plus"), &GraphEdit::_zoom_plus);
1331 ClassDB::bind_method(D_METHOD("_snap_toggled"), &GraphEdit::_snap_toggled);
1332 ClassDB::bind_method(D_METHOD("_snap_value_changed"), &GraphEdit::_snap_value_changed);
1333
1334 ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEdit::_gui_input);
1335 ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
1336 ClassDB::bind_method(D_METHOD("_connections_layer_draw"), &GraphEdit::_connections_layer_draw);
1337
1338 ClassDB::bind_method(D_METHOD("get_zoom_hbox"), &GraphEdit::get_zoom_hbox);
1339
1340 ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
1341
1342 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
1343 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset"), "set_scroll_ofs", "get_scroll_ofs");
1344 ADD_PROPERTY(PropertyInfo(Variant::INT, "snap_distance"), "set_snap", "get_snap");
1345 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_snap"), "set_use_snap", "is_using_snap");
1346 ADD_PROPERTY(PropertyInfo(Variant::REAL, "zoom"), "set_zoom", "get_zoom");
1347
1348 ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot")));
1349 ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot")));
1350 ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
1351 ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
1352 ADD_SIGNAL(MethodInfo("copy_nodes_request"));
1353 ADD_SIGNAL(MethodInfo("paste_nodes_request"));
1354 ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
1355 ADD_SIGNAL(MethodInfo("node_unselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
1356 ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
1357 ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
1358 ADD_SIGNAL(MethodInfo("delete_nodes_request"));
1359 ADD_SIGNAL(MethodInfo("_begin_node_move"));
1360 ADD_SIGNAL(MethodInfo("_end_node_move"));
1361 ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs")));
1362 }
1363
GraphEdit()1364 GraphEdit::GraphEdit() {
1365 set_focus_mode(FOCUS_ALL);
1366
1367 awaiting_scroll_offset_update = false;
1368 top_layer = NULL;
1369 top_layer = memnew(GraphEditFilter(this));
1370 add_child(top_layer);
1371 top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
1372 top_layer->set_anchors_and_margins_preset(Control::PRESET_WIDE);
1373 top_layer->connect("draw", this, "_top_layer_draw");
1374 top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
1375 top_layer->connect("gui_input", this, "_top_layer_input");
1376
1377 connections_layer = memnew(Control);
1378 add_child(connections_layer);
1379 connections_layer->connect("draw", this, "_connections_layer_draw");
1380 connections_layer->set_name("CLAYER");
1381 connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offset
1382 connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
1383
1384 h_scroll = memnew(HScrollBar);
1385 h_scroll->set_name("_h_scroll");
1386 top_layer->add_child(h_scroll);
1387
1388 v_scroll = memnew(VScrollBar);
1389 v_scroll->set_name("_v_scroll");
1390 top_layer->add_child(v_scroll);
1391
1392 updating = false;
1393 connecting = false;
1394 right_disconnects = false;
1395
1396 box_selecting = false;
1397 dragging = false;
1398
1399 //set large minmax so it can scroll even if not resized yet
1400 h_scroll->set_min(-10000);
1401 h_scroll->set_max(10000);
1402
1403 v_scroll->set_min(-10000);
1404 v_scroll->set_max(10000);
1405
1406 h_scroll->connect("value_changed", this, "_scroll_moved");
1407 v_scroll->connect("value_changed", this, "_scroll_moved");
1408
1409 zoom = 1;
1410
1411 zoom_hb = memnew(HBoxContainer);
1412 top_layer->add_child(zoom_hb);
1413 zoom_hb->set_position(Vector2(10, 10));
1414
1415 zoom_minus = memnew(ToolButton);
1416 zoom_hb->add_child(zoom_minus);
1417 zoom_minus->set_tooltip(RTR("Zoom Out"));
1418 zoom_minus->connect("pressed", this, "_zoom_minus");
1419 zoom_minus->set_focus_mode(FOCUS_NONE);
1420
1421 zoom_reset = memnew(ToolButton);
1422 zoom_hb->add_child(zoom_reset);
1423 zoom_reset->set_tooltip(RTR("Zoom Reset"));
1424 zoom_reset->connect("pressed", this, "_zoom_reset");
1425 zoom_reset->set_focus_mode(FOCUS_NONE);
1426
1427 zoom_plus = memnew(ToolButton);
1428 zoom_hb->add_child(zoom_plus);
1429 zoom_plus->set_tooltip(RTR("Zoom In"));
1430 zoom_plus->connect("pressed", this, "_zoom_plus");
1431 zoom_plus->set_focus_mode(FOCUS_NONE);
1432
1433 snap_button = memnew(ToolButton);
1434 snap_button->set_toggle_mode(true);
1435 snap_button->set_tooltip(RTR("Enable snap and show grid."));
1436 snap_button->connect("pressed", this, "_snap_toggled");
1437 snap_button->set_pressed(true);
1438 snap_button->set_focus_mode(FOCUS_NONE);
1439 zoom_hb->add_child(snap_button);
1440
1441 snap_amount = memnew(SpinBox);
1442 snap_amount->set_min(5);
1443 snap_amount->set_max(100);
1444 snap_amount->set_step(1);
1445 snap_amount->set_value(20);
1446 snap_amount->connect("value_changed", this, "_snap_value_changed");
1447 zoom_hb->add_child(snap_amount);
1448
1449 setting_scroll_ofs = false;
1450 just_disconnected = false;
1451 set_clip_contents(true);
1452 }
1453