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