graph_edit.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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/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 _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  107. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  108. bool arrange_nodes_button_hidden = false;
  109. bool connecting = false;
  110. StringName connecting_from;
  111. bool connecting_out = false;
  112. int connecting_index = 0;
  113. int connecting_type = 0;
  114. Color connecting_color;
  115. bool connecting_target = false;
  116. Vector2 connecting_to;
  117. StringName connecting_target_to;
  118. int connecting_target_index = 0;
  119. bool just_disconnected = false;
  120. bool connecting_valid = false;
  121. Vector2 click_pos;
  122. PanningScheme panning_scheme = SCROLL_ZOOMS;
  123. bool dragging = false;
  124. bool just_selected = false;
  125. bool moving_selection = false;
  126. Vector2 drag_accum;
  127. float zoom = 1.0;
  128. float zoom_step = 1.2;
  129. // Proper values set in constructor.
  130. float zoom_min = 0.0;
  131. float zoom_max = 0.0;
  132. void _zoom_minus();
  133. void _zoom_reset();
  134. void _zoom_plus();
  135. void _update_zoom_label();
  136. bool box_selecting = false;
  137. bool box_selection_mode_additive = false;
  138. Point2 box_selecting_from;
  139. Point2 box_selecting_to;
  140. Rect2 box_selecting_rect;
  141. List<GraphNode *> previous_selected;
  142. bool setting_scroll_ofs = false;
  143. bool right_disconnects = false;
  144. bool updating = false;
  145. bool awaiting_scroll_offset_update = false;
  146. List<Connection> connections;
  147. float lines_thickness = 2.0f;
  148. float lines_curvature = 0.5f;
  149. bool lines_antialiased = true;
  150. PackedVector2Array get_connection_line(const Vector2 &p_from, const Vector2 &p_to);
  151. 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);
  152. void _graph_node_selected(Node *p_gn);
  153. void _graph_node_deselected(Node *p_gn);
  154. void _graph_node_raised(Node *p_gn);
  155. void _graph_node_moved(Node *p_gn);
  156. void _graph_node_slot_updated(int p_index, Node *p_gn);
  157. void _update_scroll();
  158. void _scroll_moved(double);
  159. virtual void gui_input(const Ref<InputEvent> &p_ev) override;
  160. Control *connections_layer = nullptr;
  161. GraphEditFilter *top_layer = nullptr;
  162. GraphEditMinimap *minimap = nullptr;
  163. void _top_layer_input(const Ref<InputEvent> &p_ev);
  164. bool is_in_input_hotzone(GraphNode *p_node, int p_port, const Vector2 &p_mouse_pos, const Vector2i &p_port_size);
  165. bool is_in_output_hotzone(GraphNode *p_node, int p_port, const Vector2 &p_mouse_pos, const Vector2i &p_port_size);
  166. bool is_in_port_hotzone(const Vector2 &pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left);
  167. void _top_layer_draw();
  168. void _connections_layer_draw();
  169. void _minimap_draw();
  170. void _update_scroll_offset();
  171. TypedArray<Dictionary> _get_connection_list() const;
  172. bool lines_on_bg = false;
  173. struct ConnType {
  174. union {
  175. struct {
  176. uint32_t type_a;
  177. uint32_t type_b;
  178. };
  179. uint64_t key = 0;
  180. };
  181. static uint32_t hash(const ConnType &p_conn) {
  182. return hash_one_uint64(p_conn.key);
  183. }
  184. bool operator==(const ConnType &p_type) const {
  185. return key == p_type.key;
  186. }
  187. ConnType(uint32_t a = 0, uint32_t b = 0) {
  188. type_a = a;
  189. type_b = b;
  190. }
  191. };
  192. HashSet<ConnType, ConnType> valid_connection_types;
  193. HashSet<int> valid_left_disconnect_types;
  194. HashSet<int> valid_right_disconnect_types;
  195. HashMap<StringName, Vector<GraphNode *>> comment_enclosed_nodes;
  196. void _update_comment_enclosed_nodes_list(GraphNode *p_node, HashMap<StringName, Vector<GraphNode *>> &p_comment_enclosed_nodes);
  197. void _set_drag_comment_enclosed_nodes(GraphNode *p_node, HashMap<StringName, Vector<GraphNode *>> &p_comment_enclosed_nodes, bool p_drag);
  198. void _set_position_of_comment_enclosed_nodes(GraphNode *p_node, HashMap<StringName, Vector<GraphNode *>> &p_comment_enclosed_nodes, Vector2 p_pos);
  199. HBoxContainer *zoom_hb = nullptr;
  200. friend class GraphEditFilter;
  201. bool _filter_input(const Point2 &p_point);
  202. void _snap_toggled();
  203. void _snap_value_changed(double);
  204. friend class GraphEditMinimap;
  205. void _minimap_toggled();
  206. bool _check_clickable_control(Control *p_control, const Vector2 &r_mouse_pos, const Vector2 &p_offset);
  207. bool arranging_graph = false;
  208. enum SET_OPERATIONS {
  209. IS_EQUAL,
  210. IS_SUBSET,
  211. DIFFERENCE,
  212. UNION,
  213. };
  214. int _set_operations(SET_OPERATIONS p_operation, HashSet<StringName> &r_u, const HashSet<StringName> &r_v);
  215. HashMap<int, Vector<StringName>> _layering(const HashSet<StringName> &r_selected_nodes, const HashMap<StringName, HashSet<StringName>> &r_upper_neighbours);
  216. Vector<StringName> _split(const Vector<StringName> &r_layer, const HashMap<StringName, Dictionary> &r_crossings);
  217. 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);
  218. void _crossing_minimisation(HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, HashSet<StringName>> &r_upper_neighbours);
  219. 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);
  220. 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);
  221. 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);
  222. protected:
  223. static void _bind_methods();
  224. virtual void add_child_notify(Node *p_child) override;
  225. virtual void remove_child_notify(Node *p_child) override;
  226. void _notification(int p_what);
  227. GDVIRTUAL2RC(Vector<Vector2>, _get_connection_line, Vector2, Vector2)
  228. GDVIRTUAL3R(bool, _is_in_input_hotzone, Object *, int, Vector2)
  229. GDVIRTUAL3R(bool, _is_in_output_hotzone, Object *, int, Vector2)
  230. GDVIRTUAL4R(bool, _is_node_hover_valid, StringName, int, StringName, int);
  231. public:
  232. PackedStringArray get_configuration_warnings() const override;
  233. Error connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  234. bool is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  235. void disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  236. void clear_connections();
  237. void force_connection_drag_end();
  238. virtual bool is_node_hover_valid(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  239. void set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity);
  240. void add_valid_connection_type(int p_type, int p_with_type);
  241. void remove_valid_connection_type(int p_type, int p_with_type);
  242. bool is_valid_connection_type(int p_type, int p_with_type) const;
  243. void set_panning_scheme(PanningScheme p_scheme);
  244. PanningScheme get_panning_scheme() const;
  245. void set_zoom(float p_zoom);
  246. void set_zoom_custom(float p_zoom, const Vector2 &p_center);
  247. float get_zoom() const;
  248. void set_zoom_min(float p_zoom_min);
  249. float get_zoom_min() const;
  250. void set_zoom_max(float p_zoom_max);
  251. float get_zoom_max() const;
  252. void set_zoom_step(float p_zoom_step);
  253. float get_zoom_step() const;
  254. void set_show_zoom_label(bool p_enable);
  255. bool is_showing_zoom_label() const;
  256. void set_minimap_size(Vector2 p_size);
  257. Vector2 get_minimap_size() const;
  258. void set_minimap_opacity(float p_opacity);
  259. float get_minimap_opacity() const;
  260. void set_minimap_enabled(bool p_enable);
  261. bool is_minimap_enabled() const;
  262. void set_arrange_nodes_button_hidden(bool p_enable);
  263. bool is_arrange_nodes_button_hidden() const;
  264. GraphEditFilter *get_top_layer() const { return top_layer; }
  265. GraphEditMinimap *get_minimap() const { return minimap; }
  266. void get_connection_list(List<Connection> *r_connections) const;
  267. void set_right_disconnects(bool p_enable);
  268. bool is_right_disconnects_enabled() const;
  269. void add_valid_right_disconnect_type(int p_type);
  270. void remove_valid_right_disconnect_type(int p_type);
  271. void add_valid_left_disconnect_type(int p_type);
  272. void remove_valid_left_disconnect_type(int p_type);
  273. void set_scroll_ofs(const Vector2 &p_ofs);
  274. Vector2 get_scroll_ofs() const;
  275. void set_selected(Node *p_child);
  276. void set_use_snap(bool p_enable);
  277. bool is_using_snap() const;
  278. int get_snap() const;
  279. void set_snap(int p_snap);
  280. void set_connection_lines_curvature(float p_curvature);
  281. float get_connection_lines_curvature() const;
  282. void set_connection_lines_thickness(float p_thickness);
  283. float get_connection_lines_thickness() const;
  284. void set_connection_lines_antialiased(bool p_antialiased);
  285. bool is_connection_lines_antialiased() const;
  286. HBoxContainer *get_zoom_hbox();
  287. Ref<ViewPanner> get_panner();
  288. void set_warped_panning(bool p_warped);
  289. void arrange_nodes();
  290. GraphEdit();
  291. };
  292. VARIANT_ENUM_CAST(GraphEdit::PanningScheme);
  293. #endif // GRAPH_EDIT_H