graph_edit.h 18 KB

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