graph_edit.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*************************************************************************/
  2. /* graph_edit.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/slider.h"
  38. #include "scene/gui/spin_box.h"
  39. #include "scene/gui/texture_rect.h"
  40. class GraphEdit;
  41. class GraphEditFilter : public Control {
  42. GDCLASS(GraphEditFilter, Control);
  43. friend class GraphEdit;
  44. friend class GraphEditMinimap;
  45. GraphEdit *ge;
  46. virtual bool has_point(const Point2 &p_point) const override;
  47. public:
  48. GraphEditFilter(GraphEdit *p_edit);
  49. };
  50. class GraphEditMinimap : public Control {
  51. GDCLASS(GraphEditMinimap, Control);
  52. friend class GraphEdit;
  53. friend class GraphEditFilter;
  54. GraphEdit *ge;
  55. protected:
  56. public:
  57. GraphEditMinimap(GraphEdit *p_edit);
  58. void update_minimap();
  59. Rect2 get_camera_rect();
  60. private:
  61. Vector2 minimap_padding;
  62. Vector2 minimap_offset;
  63. Vector2 graph_proportions;
  64. Vector2 graph_padding;
  65. Vector2 camera_position;
  66. Vector2 camera_size;
  67. bool is_pressing;
  68. bool is_resizing;
  69. Vector2 _get_render_size();
  70. Vector2 _get_graph_offset();
  71. Vector2 _get_graph_size();
  72. Vector2 _convert_from_graph_position(const Vector2 &p_position);
  73. Vector2 _convert_to_graph_position(const Vector2 &p_position);
  74. virtual void gui_input(const Ref<InputEvent> &p_ev) override;
  75. void _adjust_graph_scroll(const Vector2 &p_offset);
  76. };
  77. class GraphEdit : public Control {
  78. GDCLASS(GraphEdit, Control);
  79. public:
  80. struct Connection {
  81. StringName from;
  82. StringName to;
  83. int from_port = 0;
  84. int to_port = 0;
  85. float activity = 0.0;
  86. };
  87. private:
  88. Label *zoom_label;
  89. Button *zoom_minus;
  90. Button *zoom_reset;
  91. Button *zoom_plus;
  92. Button *snap_button;
  93. SpinBox *snap_amount;
  94. Button *minimap_button;
  95. Button *layout_button;
  96. HScrollBar *h_scroll;
  97. VScrollBar *v_scroll;
  98. float port_grab_distance_horizontal = 0.0;
  99. float port_grab_distance_vertical;
  100. bool connecting = false;
  101. String connecting_from;
  102. bool connecting_out = false;
  103. int connecting_index = 0;
  104. int connecting_type = 0;
  105. Color connecting_color;
  106. bool connecting_target = false;
  107. Vector2 connecting_to;
  108. String connecting_target_to;
  109. int connecting_target_index;
  110. bool just_disconnected = false;
  111. bool connecting_valid = false;
  112. Vector2 click_pos;
  113. bool dragging = false;
  114. bool just_selected = false;
  115. bool moving_selection = false;
  116. Vector2 drag_accum;
  117. float zoom = 1.0;
  118. float zoom_step = 1.2;
  119. float zoom_min;
  120. float zoom_max;
  121. void _zoom_minus();
  122. void _zoom_reset();
  123. void _zoom_plus();
  124. void _update_zoom_label();
  125. bool box_selecting = false;
  126. bool box_selection_mode_additive = false;
  127. Point2 box_selecting_from;
  128. Point2 box_selecting_to;
  129. Rect2 box_selecting_rect;
  130. List<GraphNode *> previous_selected;
  131. bool setting_scroll_ofs = false;
  132. bool right_disconnects = false;
  133. bool updating = false;
  134. bool awaiting_scroll_offset_update = false;
  135. List<Connection> connections;
  136. float lines_thickness = 2.0f;
  137. bool lines_antialiased = true;
  138. PackedVector2Array get_connection_line(const Vector2 &p_from, const Vector2 &p_to);
  139. 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);
  140. void _graph_node_raised(Node *p_gn);
  141. void _graph_node_moved(Node *p_gn);
  142. void _graph_node_slot_updated(int p_index, Node *p_gn);
  143. void _update_scroll();
  144. void _scroll_moved(double);
  145. virtual void gui_input(const Ref<InputEvent> &p_ev) override;
  146. Control *connections_layer;
  147. GraphEditFilter *top_layer;
  148. GraphEditMinimap *minimap;
  149. void _top_layer_input(const Ref<InputEvent> &p_ev);
  150. bool is_in_hot_zone(const Vector2 &pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left);
  151. void _top_layer_draw();
  152. void _connections_layer_draw();
  153. void _minimap_draw();
  154. void _update_scroll_offset();
  155. Array _get_connection_list() const;
  156. bool lines_on_bg;
  157. struct ConnType {
  158. union {
  159. struct {
  160. uint32_t type_a;
  161. uint32_t type_b;
  162. };
  163. uint64_t key = 0;
  164. };
  165. bool operator<(const ConnType &p_type) const {
  166. return key < p_type.key;
  167. }
  168. ConnType(uint32_t a = 0, uint32_t b = 0) {
  169. type_a = a;
  170. type_b = b;
  171. }
  172. };
  173. Set<ConnType> valid_connection_types;
  174. Set<int> valid_left_disconnect_types;
  175. Set<int> valid_right_disconnect_types;
  176. HBoxContainer *zoom_hb;
  177. friend class GraphEditFilter;
  178. bool _filter_input(const Point2 &p_point);
  179. void _snap_toggled();
  180. void _snap_value_changed(double);
  181. friend class GraphEditMinimap;
  182. void _minimap_toggled();
  183. bool _check_clickable_control(Control *p_control, const Vector2 &pos);
  184. bool arranging_graph = false;
  185. enum SET_OPERATIONS {
  186. IS_EQUAL,
  187. IS_SUBSET,
  188. DIFFERENCE,
  189. UNION,
  190. };
  191. int _set_operations(SET_OPERATIONS p_operation, Set<StringName> &r_u, const Set<StringName> &r_v);
  192. HashMap<int, Vector<StringName>> _layering(const Set<StringName> &r_selected_nodes, const HashMap<StringName, Set<StringName>> &r_upper_neighbours);
  193. Vector<StringName> _split(const Vector<StringName> &r_layer, const HashMap<StringName, Dictionary> &r_crossings);
  194. void _horizontal_alignment(Dictionary &r_root, Dictionary &r_align, const HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, Set<StringName>> &r_upper_neighbours, const Set<StringName> &r_selected_nodes);
  195. void _crossing_minimisation(HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, Set<StringName>> &r_upper_neighbours);
  196. void _calculate_inner_shifts(Dictionary &r_inner_shifts, const Dictionary &r_root, const Dictionary &r_node_names, const Dictionary &r_align, const Set<StringName> &r_block_heads, const HashMap<StringName, Pair<int, int>> &r_port_info);
  197. 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);
  198. 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);
  199. protected:
  200. static void _bind_methods();
  201. virtual void add_child_notify(Node *p_child) override;
  202. virtual void remove_child_notify(Node *p_child) override;
  203. void _notification(int p_what);
  204. GDVIRTUAL2RC(Vector<Vector2>, _get_connection_line, Vector2, Vector2)
  205. public:
  206. Error connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  207. bool is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  208. void disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  209. void clear_connections();
  210. void set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity);
  211. void add_valid_connection_type(int p_type, int p_with_type);
  212. void remove_valid_connection_type(int p_type, int p_with_type);
  213. bool is_valid_connection_type(int p_type, int p_with_type) const;
  214. void set_zoom(float p_zoom);
  215. void set_zoom_custom(float p_zoom, const Vector2 &p_center);
  216. float get_zoom() const;
  217. void set_zoom_min(float p_zoom_min);
  218. float get_zoom_min() const;
  219. void set_zoom_max(float p_zoom_max);
  220. float get_zoom_max() const;
  221. void set_zoom_step(float p_zoom_step);
  222. float get_zoom_step() const;
  223. void set_show_zoom_label(bool p_enable);
  224. bool is_showing_zoom_label() const;
  225. void set_minimap_size(Vector2 p_size);
  226. Vector2 get_minimap_size() const;
  227. void set_minimap_opacity(float p_opacity);
  228. float get_minimap_opacity() const;
  229. void set_minimap_enabled(bool p_enable);
  230. bool is_minimap_enabled() const;
  231. GraphEditFilter *get_top_layer() const { return top_layer; }
  232. GraphEditMinimap *get_minimap() const { return minimap; }
  233. void get_connection_list(List<Connection> *r_connections) const;
  234. void set_right_disconnects(bool p_enable);
  235. bool is_right_disconnects_enabled() const;
  236. void add_valid_right_disconnect_type(int p_type);
  237. void remove_valid_right_disconnect_type(int p_type);
  238. void add_valid_left_disconnect_type(int p_type);
  239. void remove_valid_left_disconnect_type(int p_type);
  240. void set_scroll_ofs(const Vector2 &p_ofs);
  241. Vector2 get_scroll_ofs() const;
  242. void set_selected(Node *p_child);
  243. void set_use_snap(bool p_enable);
  244. bool is_using_snap() const;
  245. int get_snap() const;
  246. void set_snap(int p_snap);
  247. void set_connection_lines_thickness(float p_thickness);
  248. float get_connection_lines_thickness() const;
  249. void set_connection_lines_antialiased(bool p_antialiased);
  250. bool is_connection_lines_antialiased() const;
  251. HBoxContainer *get_zoom_hbox();
  252. void arrange_nodes();
  253. GraphEdit();
  254. };
  255. #endif // GRAPHEdit_H