graph_edit.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*************************************************************************/
  2. /* graph_edit.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifndef GRAPH_EDIT_H
  31. #define GRAPH_EDIT_H
  32. #include "scene/gui/box_container.h"
  33. #include "scene/gui/button.h"
  34. #include "scene/gui/graph_node.h"
  35. #include "scene/gui/label.h"
  36. #include "scene/gui/scroll_bar.h"
  37. #include "scene/gui/spin_box.h"
  38. class GraphEdit;
  39. class ViewPanner;
  40. class GraphEditFilter : public Control {
  41. GDCLASS(GraphEditFilter, Control);
  42. friend class GraphEdit;
  43. friend class GraphEditMinimap;
  44. GraphEdit *ge = nullptr;
  45. virtual bool has_point(const Point2 &p_point) const override;
  46. public:
  47. GraphEditFilter(GraphEdit *p_edit);
  48. };
  49. class GraphEditMinimap : public Control {
  50. GDCLASS(GraphEditMinimap, Control);
  51. friend class GraphEdit;
  52. friend class GraphEditFilter;
  53. GraphEdit *ge = nullptr;
  54. protected:
  55. public:
  56. GraphEditMinimap(GraphEdit *p_edit);
  57. void update_minimap();
  58. Rect2 get_camera_rect();
  59. private:
  60. Vector2 minimap_padding;
  61. Vector2 minimap_offset;
  62. Vector2 graph_proportions;
  63. Vector2 graph_padding;
  64. Vector2 camera_position;
  65. Vector2 camera_size;
  66. bool is_pressing;
  67. bool is_resizing;
  68. Vector2 _get_render_size();
  69. Vector2 _get_graph_offset();
  70. Vector2 _get_graph_size();
  71. Vector2 _convert_from_graph_position(const Vector2 &p_position);
  72. Vector2 _convert_to_graph_position(const Vector2 &p_position);
  73. virtual void gui_input(const Ref<InputEvent> &p_ev) override;
  74. void _adjust_graph_scroll(const Vector2 &p_offset);
  75. };
  76. class GraphEdit : public Control {
  77. GDCLASS(GraphEdit, Control);
  78. public:
  79. struct Connection {
  80. StringName from;
  81. StringName to;
  82. int from_port = 0;
  83. int to_port = 0;
  84. float activity = 0.0;
  85. };
  86. // Should be in sync with ControlScheme in ViewPanner.
  87. enum PanningScheme {
  88. SCROLL_ZOOMS,
  89. SCROLL_PANS,
  90. };
  91. private:
  92. Label *zoom_label = nullptr;
  93. Button *zoom_minus = nullptr;
  94. Button *zoom_reset = nullptr;
  95. Button *zoom_plus = nullptr;
  96. Button *snap_button = nullptr;
  97. SpinBox *snap_amount = nullptr;
  98. Button *minimap_button = nullptr;
  99. Button *layout_button = nullptr;
  100. HScrollBar *h_scroll = nullptr;
  101. VScrollBar *v_scroll = nullptr;
  102. float port_hotzone_inner_extent = 0.0;
  103. float port_hotzone_outer_extent = 0.0;
  104. Ref<ViewPanner> panner;
  105. bool warped_panning = true;
  106. void _scroll_callback(Vector2 p_scroll_vec, bool p_alt);
  107. void _pan_callback(Vector2 p_scroll_vec);
  108. void _zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt);
  109. bool arrange_nodes_button_hidden = false;
  110. bool connecting = false;
  111. String connecting_from;
  112. bool connecting_out = false;
  113. int connecting_index = 0;
  114. int connecting_type = 0;
  115. Color connecting_color;
  116. bool connecting_target = false;
  117. Vector2 connecting_to;
  118. String connecting_target_to;
  119. int connecting_target_index = 0;
  120. bool just_disconnected = false;
  121. bool connecting_valid = false;
  122. Vector2 click_pos;
  123. PanningScheme panning_scheme = SCROLL_ZOOMS;
  124. bool dragging = false;
  125. bool just_selected = false;
  126. bool moving_selection = false;
  127. Vector2 drag_accum;
  128. float zoom = 1.0;
  129. float zoom_step = 1.2;
  130. // Proper values set in constructor.
  131. float zoom_min = 0.0;
  132. float zoom_max = 0.0;
  133. void _zoom_minus();
  134. void _zoom_reset();
  135. void _zoom_plus();
  136. void _update_zoom_label();
  137. bool box_selecting = false;
  138. bool box_selection_mode_additive = false;
  139. Point2 box_selecting_from;
  140. Point2 box_selecting_to;
  141. Rect2 box_selecting_rect;
  142. List<GraphNode *> previous_selected;
  143. bool setting_scroll_ofs = false;
  144. bool right_disconnects = false;
  145. bool updating = false;
  146. bool awaiting_scroll_offset_update = false;
  147. List<Connection> connections;
  148. float lines_thickness = 2.0f;
  149. float lines_curvature = 0.5f;
  150. bool lines_antialiased = true;
  151. PackedVector2Array get_connection_line(const Vector2 &p_from, const Vector2 &p_to);
  152. void _draw_connection_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color, float p_width, float p_zoom);
  153. void _graph_node_raised(Node *p_gn);
  154. void _graph_node_moved(Node *p_gn);
  155. void _graph_node_slot_updated(int p_index, Node *p_gn);
  156. void _update_scroll();
  157. void _scroll_moved(double);
  158. virtual void gui_input(const Ref<InputEvent> &p_ev) override;
  159. Control *connections_layer = nullptr;
  160. GraphEditFilter *top_layer = nullptr;
  161. GraphEditMinimap *minimap = nullptr;
  162. void _top_layer_input(const Ref<InputEvent> &p_ev);
  163. bool is_in_input_hotzone(GraphNode *p_graph_node, int p_slot_index, const Vector2 &p_mouse_pos, const Vector2i &p_port_size);
  164. bool is_in_output_hotzone(GraphNode *p_graph_node, int p_slot_index, const Vector2 &p_mouse_pos, const Vector2i &p_port_size);
  165. bool is_in_port_hotzone(const Vector2 &pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left);
  166. void _top_layer_draw();
  167. void _connections_layer_draw();
  168. void _minimap_draw();
  169. void _update_scroll_offset();
  170. TypedArray<Dictionary> _get_connection_list() const;
  171. bool lines_on_bg = false;
  172. struct ConnType {
  173. union {
  174. struct {
  175. uint32_t type_a;
  176. uint32_t type_b;
  177. };
  178. uint64_t key = 0;
  179. };
  180. static uint32_t hash(const ConnType &p_conn) {
  181. return hash_one_uint64(p_conn.key);
  182. }
  183. bool operator==(const ConnType &p_type) const {
  184. return key == p_type.key;
  185. }
  186. ConnType(uint32_t a = 0, uint32_t b = 0) {
  187. type_a = a;
  188. type_b = b;
  189. }
  190. };
  191. HashSet<ConnType, ConnType> valid_connection_types;
  192. HashSet<int> valid_left_disconnect_types;
  193. HashSet<int> valid_right_disconnect_types;
  194. HashMap<StringName, Vector<GraphNode *>> comment_enclosed_nodes;
  195. void _update_comment_enclosed_nodes_list(GraphNode *p_node, HashMap<StringName, Vector<GraphNode *>> &p_comment_enclosed_nodes);
  196. void _set_drag_comment_enclosed_nodes(GraphNode *p_node, HashMap<StringName, Vector<GraphNode *>> &p_comment_enclosed_nodes, bool p_drag);
  197. void _set_position_of_comment_enclosed_nodes(GraphNode *p_node, HashMap<StringName, Vector<GraphNode *>> &p_comment_enclosed_nodes, Vector2 p_pos);
  198. HBoxContainer *zoom_hb = nullptr;
  199. friend class GraphEditFilter;
  200. bool _filter_input(const Point2 &p_point);
  201. void _snap_toggled();
  202. void _snap_value_changed(double);
  203. friend class GraphEditMinimap;
  204. void _minimap_toggled();
  205. bool _check_clickable_control(Control *p_control, const Vector2 &r_mouse_pos, const Vector2 &p_offset);
  206. bool arranging_graph = false;
  207. enum SET_OPERATIONS {
  208. IS_EQUAL,
  209. IS_SUBSET,
  210. DIFFERENCE,
  211. UNION,
  212. };
  213. int _set_operations(SET_OPERATIONS p_operation, HashSet<StringName> &r_u, const HashSet<StringName> &r_v);
  214. HashMap<int, Vector<StringName>> _layering(const HashSet<StringName> &r_selected_nodes, const HashMap<StringName, HashSet<StringName>> &r_upper_neighbours);
  215. Vector<StringName> _split(const Vector<StringName> &r_layer, const HashMap<StringName, Dictionary> &r_crossings);
  216. void _horizontal_alignment(Dictionary &r_root, Dictionary &r_align, const HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, HashSet<StringName>> &r_upper_neighbours, const HashSet<StringName> &r_selected_nodes);
  217. void _crossing_minimisation(HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, HashSet<StringName>> &r_upper_neighbours);
  218. void _calculate_inner_shifts(Dictionary &r_inner_shifts, const Dictionary &r_root, const Dictionary &r_node_names, const Dictionary &r_align, const HashSet<StringName> &r_block_heads, const HashMap<StringName, Pair<int, int>> &r_port_info);
  219. float _calculate_threshold(StringName p_v, StringName p_w, const Dictionary &r_node_names, const HashMap<int, Vector<StringName>> &r_layers, const Dictionary &r_root, const Dictionary &r_align, const Dictionary &r_inner_shift, real_t p_current_threshold, const HashMap<StringName, Vector2> &r_node_positions);
  220. void _place_block(StringName p_v, float p_delta, const HashMap<int, Vector<StringName>> &r_layers, const Dictionary &r_root, const Dictionary &r_align, const Dictionary &r_node_name, const Dictionary &r_inner_shift, Dictionary &r_sink, Dictionary &r_shift, HashMap<StringName, Vector2> &r_node_positions);
  221. protected:
  222. static void _bind_methods();
  223. virtual void add_child_notify(Node *p_child) override;
  224. virtual void remove_child_notify(Node *p_child) override;
  225. void _notification(int p_what);
  226. GDVIRTUAL2RC(Vector<Vector2>, _get_connection_line, Vector2, Vector2)
  227. GDVIRTUAL3R(bool, _is_in_input_hotzone, Object *, int, Vector2)
  228. GDVIRTUAL3R(bool, _is_in_output_hotzone, Object *, int, Vector2)
  229. GDVIRTUAL4R(bool, _is_node_hover_valid, StringName, int, StringName, int);
  230. public:
  231. Error connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  232. bool is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  233. void disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  234. void clear_connections();
  235. void force_connection_drag_end();
  236. virtual bool is_node_hover_valid(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  237. void set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity);
  238. void add_valid_connection_type(int p_type, int p_with_type);
  239. void remove_valid_connection_type(int p_type, int p_with_type);
  240. bool is_valid_connection_type(int p_type, int p_with_type) const;
  241. void set_panning_scheme(PanningScheme p_scheme);
  242. PanningScheme get_panning_scheme() const;
  243. void set_zoom(float p_zoom);
  244. void set_zoom_custom(float p_zoom, const Vector2 &p_center);
  245. float get_zoom() const;
  246. void set_zoom_min(float p_zoom_min);
  247. float get_zoom_min() const;
  248. void set_zoom_max(float p_zoom_max);
  249. float get_zoom_max() const;
  250. void set_zoom_step(float p_zoom_step);
  251. float get_zoom_step() const;
  252. void set_show_zoom_label(bool p_enable);
  253. bool is_showing_zoom_label() const;
  254. void set_minimap_size(Vector2 p_size);
  255. Vector2 get_minimap_size() const;
  256. void set_minimap_opacity(float p_opacity);
  257. float get_minimap_opacity() const;
  258. void set_minimap_enabled(bool p_enable);
  259. bool is_minimap_enabled() const;
  260. void set_arrange_nodes_button_hidden(bool p_enable);
  261. bool is_arrange_nodes_button_hidden() const;
  262. GraphEditFilter *get_top_layer() const { return top_layer; }
  263. GraphEditMinimap *get_minimap() const { return minimap; }
  264. void get_connection_list(List<Connection> *r_connections) const;
  265. void set_right_disconnects(bool p_enable);
  266. bool is_right_disconnects_enabled() const;
  267. void add_valid_right_disconnect_type(int p_type);
  268. void remove_valid_right_disconnect_type(int p_type);
  269. void add_valid_left_disconnect_type(int p_type);
  270. void remove_valid_left_disconnect_type(int p_type);
  271. void set_scroll_ofs(const Vector2 &p_ofs);
  272. Vector2 get_scroll_ofs() const;
  273. void set_selected(Node *p_child);
  274. void set_use_snap(bool p_enable);
  275. bool is_using_snap() const;
  276. int get_snap() const;
  277. void set_snap(int p_snap);
  278. void set_connection_lines_curvature(float p_curvature);
  279. float get_connection_lines_curvature() const;
  280. void set_connection_lines_thickness(float p_thickness);
  281. float get_connection_lines_thickness() const;
  282. void set_connection_lines_antialiased(bool p_antialiased);
  283. bool is_connection_lines_antialiased() const;
  284. HBoxContainer *get_zoom_hbox();
  285. Ref<ViewPanner> get_panner();
  286. void set_warped_panning(bool p_warped);
  287. void arrange_nodes();
  288. GraphEdit();
  289. };
  290. VARIANT_ENUM_CAST(GraphEdit::PanningScheme);
  291. #endif // GRAPH_EDIT_H