Debian Codegraph @

scene/gui/graph_edit.h

raw symbols SCIP
1 /**************************************************************************/
2 /* graph_edit.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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 #pragma once
32
33 #include "core/variant/typed_dictionary.h"
34 #include "scene/gui/box_container.h"
35 #include "scene/gui/graph_frame.h"
36 #include "scene/gui/graph_node.h"
37 #include "scene/resources/shader.h"
38
39 class Button;
40 class GraphEdit;
41 class GraphEditArranger;
42 class HScrollBar;
43 class Label;
44 class Line2D;
45 class PanelContainer;
46 class SpinBox;
47 class ViewPanner;
48 class VScrollBar;
49
50 class GraphEditFilter : public Control {
51 GDCLASS(GraphEditFilter, Control);
52
53 friend class GraphEdit;
54 friend class GraphEditMinimap;
55
56 GraphEdit *ge = nullptr;
57
58 virtual bool has_point(const Point2 &p_point) const override;
59
60 public:
61 GraphEditFilter(GraphEdit *p_edit);
62 };
63
64 class GraphEditMinimap : public Control {
65 GDCLASS(GraphEditMinimap, Control);
66
67 friend class GraphEdit;
68 friend class GraphEditFilter;
69
70 GraphEdit *ge = nullptr;
71
72 Vector2 minimap_padding;
73 Vector2 minimap_offset;
74 Vector2 graph_proportions = Vector2(1, 1);
75 Vector2 graph_padding = Vector2(0, 0);
76 Vector2 camera_position = Vector2(100, 50);
77 Vector2 camera_size = Vector2(200, 200);
78
79 bool is_pressing = false;
80 bool is_resizing = false;
81
82 struct ThemeCache {
83 Ref<StyleBox> panel;
84 Ref<StyleBox> node_style;
85 Ref<StyleBox> camera_style;
86
87 Ref<Texture2D> resizer;
88 Color resizer_color;
89 } theme_cache;
90
91 Vector2 _get_render_size();
92 Vector2 _get_graph_offset();
93 Vector2 _get_graph_size();
94
95 Vector2 _convert_from_graph_position(const Vector2 &p_position);
96 Vector2 _convert_to_graph_position(const Vector2 &p_position);
97
98 virtual void gui_input(const Ref<InputEvent> &p_ev) override;
99
100 void _adjust_graph_scroll(const Vector2 &p_offset);
101
102 protected:
103 static void _bind_methods();
104
105 public:
106 virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
107
108 void update_minimap();
109 Rect2 get_camera_rect();
110
111 GraphEditMinimap(GraphEdit *p_edit);
112 };
113
114 class GraphEdit : public Control {
115 GDCLASS(GraphEdit, Control);
116
117 public:
118 struct Connection : RefCounted {
119 StringName from_node;
120 StringName to_node;
121 int from_port = 0;
122 int to_port = 0;
123 float activity = 0.0;
124 bool keep_alive = true;
125
126 private:
127 struct Cache {
128 bool dirty = true;
129 Vector2 from_pos; // In graph space.
130 Vector2 to_pos; // In graph space.
131 Color from_color;
132 Color to_color;
133 Rect2 aabb; // In local screen space.
134 Line2D *line = nullptr; // In local screen space.
135 } _cache;
136
137 friend class GraphEdit;
138 };
139
140 // Should be in sync with ControlScheme in ViewPanner.
141 enum PanningScheme {
142 SCROLL_ZOOMS,
143 SCROLL_PANS,
144 };
145
146 enum GridPattern {
147 GRID_PATTERN_LINES,
148 GRID_PATTERN_DOTS
149 };
150
151 private:
152 struct ConnectionType {
153 union {
154 uint64_t key = 0;
155 struct {
156 uint32_t type_a;
157 uint32_t type_b;
158 };
159 };
160
161 static uint32_t hash(const ConnectionType &p_conn) {
162 return hash_one_uint64(p_conn.key);
163 }
164 bool operator==(const ConnectionType &p_type) const {
165 return key == p_type.key;
166 }
167
168 ConnectionType(uint32_t a = 0, uint32_t b = 0) {
169 type_a = a;
170 type_b = b;
171 }
172 };
173
174 Label *zoom_label = nullptr;
175 Button *zoom_minus_button = nullptr;
176 Button *zoom_reset_button = nullptr;
177 Button *zoom_plus_button = nullptr;
178
179 Button *toggle_snapping_button = nullptr;
180 SpinBox *snapping_distance_spinbox = nullptr;
181 Button *toggle_grid_button = nullptr;
182 Button *minimap_button = nullptr;
183 Button *arrange_button = nullptr;
184
185 HScrollBar *h_scrollbar = nullptr;
186 VScrollBar *v_scrollbar = nullptr;
187
188 Ref<ViewPanner> panner;
189 bool warped_panning = true;
190
191 bool show_menu = true;
192 bool show_zoom_label = false;
193 bool show_grid_buttons = true;
194 bool show_zoom_buttons = true;
195 bool show_minimap_button = true;
196 bool show_arrange_button = true;
197
198 bool snapping_enabled = true;
199 int snapping_distance = 20;
200 bool show_grid = true;
201 GridPattern grid_pattern = GRID_PATTERN_LINES;
202
203 bool keyboard_connecting = false;
204 bool connecting = false;
205 StringName connecting_from_node;
206 bool connecting_from_output = false;
207 int connecting_type = 0;
208 Color connecting_color;
209 Vector2 connecting_to_point; // In local screen space.
210 bool connecting_target_valid = false;
211 StringName connecting_target_node;
212 int connecting_from_port_index = 0;
213 int connecting_target_port_index = 0;
214
215 bool just_disconnected = false;
216 bool connecting_valid = false;
217
218 Vector2 click_pos;
219
220 PanningScheme panning_scheme = SCROLL_ZOOMS;
221 bool dragging = false;
222 bool just_selected = false;
223 bool moving_selection = false;
224 Vector2 drag_accum;
225
226 float zoom = 1.0;
227 float zoom_step = 1.2;
228 // Proper values set in constructor.
229 float zoom_min = 0.0;
230 float zoom_max = 0.0;
231
232 Vector2 min_scroll_offset;
233 Vector2 max_scroll_offset;
234 Vector2 scroll_offset;
235
236 bool box_selecting = false;
237 bool box_selection_mode_additive = false;
238 Point2 box_selecting_from;
239 Point2 box_selecting_to;
240 Rect2 box_selecting_rect;
241 List<GraphElement *> prev_selected;
242
243 bool setting_scroll_offset = false;
244 bool right_disconnects = false;
245 bool updating = false;
246 bool awaiting_scroll_offset_update = false;
247
248 Vector<Ref<Connection>> connections;
249 HashMap<StringName, List<Ref<Connection>>> connection_map;
250 Ref<Connection> hovered_connection;
251
252 float lines_thickness = 4.0f;
253 float lines_curvature = 0.5f;
254 bool lines_antialiased = true;
255
256 PanelContainer *menu_panel = nullptr;
257 HBoxContainer *menu_hbox = nullptr;
258 Control *connections_layer = nullptr;
259
260 GraphEditFilter *top_connection_layer = nullptr; // Draws a dragged connection. Necessary since the connection line shader can't be applied to the whole top layer.
261 Line2D *dragged_connection_line = nullptr;
262 Control *top_layer = nullptr; // Used for drawing the box selection rect. Contains the minimap, menu panel and the scrollbars.
263
264 GraphEditMinimap *minimap = nullptr;
265
266 static Ref<Shader> default_connections_shader;
267 Ref<Shader> connections_shader;
268
269 Ref<GraphEditArranger> arranger;
270
271 HashSet<ConnectionType, ConnectionType> valid_connection_types;
272 HashSet<int> valid_left_disconnect_types;
273 HashSet<int> valid_right_disconnect_types;
274
275 struct ThemeCache {
276 float base_scale = 1.0;
277
278 Ref<StyleBox> panel;
279 Ref<StyleBox> panel_focus;
280 Color grid_major;
281 Color grid_minor;
282
283 Color activity_color;
284 Color connection_hover_tint_color;
285 int connection_hover_thickness;
286 Color connection_valid_target_tint_color;
287 Color connection_rim_color;
288
289 Color selection_fill;
290 Color selection_stroke;
291
292 Ref<StyleBox> menu_panel;
293
294 Ref<Texture2D> zoom_in;
295 Ref<Texture2D> zoom_out;
296 Ref<Texture2D> zoom_reset;
297
298 Ref<Texture2D> snapping_toggle;
299 Ref<Texture2D> grid_toggle;
300 Ref<Texture2D> minimap_toggle;
301 Ref<Texture2D> layout;
302
303 float port_hotzone_inner_extent = 0.0;
304 float port_hotzone_outer_extent = 0.0;
305 } theme_cache;
306
307 // This separates the children in two layers to ensure the order
308 // of both background nodes (e.g frame nodes) and foreground nodes (connectable nodes).
309 int background_nodes_separator_idx = 0;
310
311 HashMap<StringName, HashSet<StringName>> frame_attached_nodes;
312 HashMap<StringName, StringName> linked_parent_map;
313
314 Dictionary type_names;
315
316 void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
317 void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
318
319 void _zoom_minus();
320 void _zoom_reset();
321 void _zoom_plus();
322 void _update_zoom_label();
323
324 void _graph_element_selected(Node *p_node);
325 void _graph_element_deselected(Node *p_node);
326 void _graph_element_visibility_changed(GraphElement *p_graph_element);
327 void _graph_element_resize_request(const Vector2 &p_new_minsize, Node *p_node);
328 void _graph_frame_autoshrink_changed(const Vector2 &p_new_minsize, GraphFrame *p_frame);
329 void _graph_element_moved(Node *p_node);
330 void _graph_node_slot_updated(int p_index, Node *p_node);
331 void _graph_node_rect_changed(GraphNode *p_node);
332
333 void _ensure_node_order_from_root(const StringName &p_node);
334 void _ensure_node_order_from(Node *p_node);
335
336 void _update_scrollbars();
337 void _update_scroll_offset();
338 void _scrollbar_moved(double);
339 virtual void gui_input(const Ref<InputEvent> &p_ev) override;
340 void _top_connection_layer_input(const Ref<InputEvent> &p_ev);
341
342 float _get_shader_line_width();
343 void _draw_minimap_connection_line(const Vector2 &p_from_graph_position, const Vector2 &p_to_graph_position, const Color &p_from_color, const Color &p_to_color);
344 void _invalidate_connection_line_cache();
345 void _update_top_connection_layer();
346 void _update_connections();
347
348 void _top_layer_draw();
349 void _minimap_draw();
350 void _draw_grid();
351
352 bool is_in_port_hotzone(const Vector2 &p_pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left);
353
354 void set_connections(const TypedArray<Dictionary> &p_connections);
355 TypedArray<Dictionary> _get_connection_list() const;
356 Dictionary _get_closest_connection_at_point(const Vector2 &p_point, float p_max_distance = 4.0) const;
357 TypedArray<Dictionary> _get_connections_intersecting_with_rect(const Rect2 &p_rect) const;
358 TypedArray<Dictionary> _get_connection_list_from_node(const StringName &p_node) const;
359
360 Rect2 _compute_shrinked_frame_rect(const GraphFrame *p_frame);
361 void _set_drag_frame_attached_nodes(GraphFrame *p_frame, bool p_drag);
362 void _set_position_of_frame_attached_nodes(GraphFrame *p_frame, const Vector2 &p_pos);
363
364 friend class GraphEditFilter;
365 bool _filter_input(const Point2 &p_point);
366 void _snapping_toggled();
367 void _snapping_distance_changed(double);
368 void _show_grid_toggled();
369
370 friend class GraphEditMinimap;
371 void _minimap_toggled();
372
373 bool _check_clickable_control(Control *p_control, const Vector2 &r_mouse_pos, const Vector2 &p_offset);
374
375 #ifndef DISABLE_DEPRECATED
376 bool _is_arrange_nodes_button_hidden_bind_compat_81582() const;
377 void _set_arrange_nodes_button_hidden_bind_compat_81582(bool p_enable);
378 PackedVector2Array _get_connection_line_bind_compat_86158(const Vector2 &p_from, const Vector2 &p_to);
379 Error _connect_node_bind_compat_97449(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
380 #endif
381
382 protected:
383 virtual void _update_theme_item_cache() override;
384
385 virtual void add_child_notify(Node *p_child) override;
386 virtual void remove_child_notify(Node *p_child) override;
387
388 void _notification(int p_what);
389 static void _bind_methods();
390 #ifndef DISABLE_DEPRECATED
391 static void _bind_compatibility_methods();
392 #endif
393
394 virtual bool is_in_input_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size);
395 virtual bool is_in_output_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size);
396
397 GDVIRTUAL2RC(Vector<Vector2>, _get_connection_line, Vector2, Vector2)
398 GDVIRTUAL3R(bool, _is_in_input_hotzone, Object *, int, Vector2)
399 GDVIRTUAL3R(bool, _is_in_output_hotzone, Object *, int, Vector2)
400 GDVIRTUAL4R(bool, _is_node_hover_valid, StringName, int, StringName, int);
401
402 public:
403 static void init_shaders();
404 static void finish_shaders();
405
406 virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
407
408 void key_input(const Ref<InputEvent> &p_ev);
409
410 // This method has to be public (for undo redo).
411 // TODO: Find a better way to do this.
412 void _update_graph_frame(GraphFrame *p_frame);
413
414 // Connection related methods.
415 Error connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, bool keep_alive = false);
416 bool is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
417 int get_connection_count(const StringName &p_node, int p_port);
418 GraphNode *get_input_connection_target(const StringName &p_node, int p_port);
419 GraphNode *get_output_connection_target(const StringName &p_node, int p_port);
420 String get_connections_description(const StringName &p_node, int p_port);
421 void disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
422
423 void force_connection_drag_end();
424 const Vector<Ref<Connection>> &get_connections() const;
425 void clear_connections();
426 virtual PackedVector2Array get_connection_line(const Vector2 &p_from, const Vector2 &p_to) const;
427 Ref<Connection> get_closest_connection_at_point(const Vector2 &p_point, float p_max_distance = 4.0) const;
428 List<Ref<Connection>> get_connections_intersecting_with_rect(const Rect2 &p_rect) const;
429
430 bool is_keyboard_connecting() const { return keyboard_connecting; }
431 void start_keyboard_connecting(GraphNode *p_node, int p_in_port, int p_out_port);
432 void end_keyboard_connecting(GraphNode *p_node, int p_in_port, int p_out_port);
433
434 Dictionary get_type_names() const;
435 void set_type_names(const Dictionary &p_names);
436
437 virtual bool is_node_hover_valid(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
438
439 void set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity);
440 void reset_all_connection_activity();
441
442 void add_valid_connection_type(int p_type, int p_with_type);
443 void remove_valid_connection_type(int p_type, int p_with_type);
444 bool is_valid_connection_type(int p_type, int p_with_type) const;
445
446 // GraphFrame related methods.
447 void attach_graph_element_to_frame(const StringName &p_graph_element, const StringName &p_parent_frame);
448 void detach_graph_element_from_frame(const StringName &p_graph_element);
449 GraphFrame *get_element_frame(const StringName &p_attached_graph_element);
450 TypedArray<StringName> get_attached_nodes_of_frame(const StringName &p_graph_frame);
451
452 void set_panning_scheme(PanningScheme p_scheme);
453 PanningScheme get_panning_scheme() const;
454
455 void set_zoom(float p_zoom);
456 void set_zoom_custom(float p_zoom, const Vector2 &p_center);
457 float get_zoom() const;
458
459 void set_zoom_min(float p_zoom_min);
460 float get_zoom_min() const;
461
462 void set_zoom_max(float p_zoom_max);
463 float get_zoom_max() const;
464
465 void set_zoom_step(float p_zoom_step);
466 float get_zoom_step() const;
467
468 void set_minimap_size(Vector2 p_size);
469 Vector2 get_minimap_size() const;
470 void set_minimap_opacity(float p_opacity);
471 float get_minimap_opacity() const;
472
473 void set_minimap_enabled(bool p_enable);
474 bool is_minimap_enabled() const;
475
476 void set_show_menu(bool p_hidden);
477 bool is_showing_menu() const;
478 void set_show_zoom_label(bool p_hidden);
479 bool is_showing_zoom_label() const;
480 void set_show_grid_buttons(bool p_hidden);
481 bool is_showing_grid_buttons() const;
482 void set_show_zoom_buttons(bool p_hidden);
483 bool is_showing_zoom_buttons() const;
484 void set_show_minimap_button(bool p_hidden);
485 bool is_showing_minimap_button() const;
486 void set_show_arrange_button(bool p_hidden);
487 bool is_showing_arrange_button() const;
488
489 Control *get_top_layer() const { return top_layer; }
490 GraphEditMinimap *get_minimap() const { return minimap; }
491
492 void override_connections_shader(const Ref<Shader> &p_shader);
493
494 void set_right_disconnects(bool p_enable);
495 bool is_right_disconnects_enabled() const;
496
497 void add_valid_right_disconnect_type(int p_type);
498 void remove_valid_right_disconnect_type(int p_type);
499
500 void add_valid_left_disconnect_type(int p_type);
501 void remove_valid_left_disconnect_type(int p_type);
502
503 void set_scroll_offset(const Vector2 &p_ofs);
504 Vector2 get_scroll_offset() const;
505
506 void set_selected(Node *p_child);
507
508 void set_snapping_enabled(bool p_enable);
509 bool is_snapping_enabled() const;
510
511 void set_snapping_distance(int p_snapping_distance);
512 int get_snapping_distance() const;
513
514 void set_show_grid(bool p_enable);
515 bool is_showing_grid() const;
516
517 void set_grid_pattern(GridPattern p_pattern);
518 GridPattern get_grid_pattern() const;
519
520 void set_connection_lines_curvature(float p_curvature);
521 float get_connection_lines_curvature() const;
522
523 void set_connection_lines_thickness(float p_thickness);
524 float get_connection_lines_thickness() const;
525
526 void set_connection_lines_antialiased(bool p_antialiased);
527 bool is_connection_lines_antialiased() const;
528
529 HBoxContainer *get_menu_hbox();
530 Ref<ViewPanner> get_panner();
531 void set_warped_panning(bool p_warped);
532 void update_warped_panning();
533
534 void arrange_nodes();
535
536 GraphEdit();
537 };
538
539 VARIANT_ENUM_CAST(GraphEdit::PanningScheme);
540 VARIANT_ENUM_CAST(GraphEdit::GridPattern);