graph_edit.h 17 KB

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