graph_edit.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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_node.h"
  34. class Button;
  35. class GraphEdit;
  36. class GraphEditArranger;
  37. class HScrollBar;
  38. class Label;
  39. class PanelContainer;
  40. class SpinBox;
  41. class ViewPanner;
  42. class VScrollBar;
  43. class GraphEditFilter : public Control {
  44. GDCLASS(GraphEditFilter, Control);
  45. friend class GraphEdit;
  46. friend class GraphEditMinimap;
  47. GraphEdit *ge = nullptr;
  48. virtual bool has_point(const Point2 &p_point) const override;
  49. public:
  50. GraphEditFilter(GraphEdit *p_edit);
  51. };
  52. class GraphEditMinimap : public Control {
  53. GDCLASS(GraphEditMinimap, Control);
  54. friend class GraphEdit;
  55. friend class GraphEditFilter;
  56. GraphEdit *ge = nullptr;
  57. Vector2 minimap_padding;
  58. Vector2 minimap_offset;
  59. Vector2 graph_proportions = Vector2(1, 1);
  60. Vector2 graph_padding = Vector2(0, 0);
  61. Vector2 camera_position = Vector2(100, 50);
  62. Vector2 camera_size = Vector2(200, 200);
  63. bool is_pressing = false;
  64. bool is_resizing = false;
  65. struct ThemeCache {
  66. Ref<StyleBox> panel;
  67. Ref<StyleBox> node_style;
  68. Ref<StyleBox> camera_style;
  69. Ref<Texture2D> resizer;
  70. Color resizer_color;
  71. } theme_cache;
  72. Vector2 _get_render_size();
  73. Vector2 _get_graph_offset();
  74. Vector2 _get_graph_size();
  75. Vector2 _convert_from_graph_position(const Vector2 &p_position);
  76. Vector2 _convert_to_graph_position(const Vector2 &p_position);
  77. virtual void gui_input(const Ref<InputEvent> &p_ev) override;
  78. void _adjust_graph_scroll(const Vector2 &p_offset);
  79. protected:
  80. static void _bind_methods();
  81. public:
  82. virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
  83. void update_minimap();
  84. Rect2 get_camera_rect();
  85. GraphEditMinimap(GraphEdit *p_edit);
  86. };
  87. class GraphEdit : public Control {
  88. GDCLASS(GraphEdit, Control);
  89. public:
  90. struct Connection {
  91. StringName from_node;
  92. StringName to_node;
  93. int from_port = 0;
  94. int to_port = 0;
  95. float activity = 0.0;
  96. };
  97. // Should be in sync with ControlScheme in ViewPanner.
  98. enum PanningScheme {
  99. SCROLL_ZOOMS,
  100. SCROLL_PANS,
  101. };
  102. private:
  103. struct ConnectionType {
  104. union {
  105. struct {
  106. uint32_t type_a;
  107. uint32_t type_b;
  108. };
  109. uint64_t key = 0;
  110. };
  111. static uint32_t hash(const ConnectionType &p_conn) {
  112. return hash_one_uint64(p_conn.key);
  113. }
  114. bool operator==(const ConnectionType &p_type) const {
  115. return key == p_type.key;
  116. }
  117. ConnectionType(uint32_t a = 0, uint32_t b = 0) {
  118. type_a = a;
  119. type_b = b;
  120. }
  121. };
  122. Label *zoom_label = nullptr;
  123. Button *zoom_minus_button = nullptr;
  124. Button *zoom_reset_button = nullptr;
  125. Button *zoom_plus_button = nullptr;
  126. Button *toggle_snapping_button = nullptr;
  127. SpinBox *snapping_distance_spinbox = nullptr;
  128. Button *toggle_grid_button = nullptr;
  129. Button *minimap_button = nullptr;
  130. Button *arrange_button = nullptr;
  131. HScrollBar *h_scrollbar = nullptr;
  132. VScrollBar *v_scrollbar = nullptr;
  133. Ref<ViewPanner> panner;
  134. bool warped_panning = true;
  135. bool show_menu = true;
  136. bool show_zoom_label = false;
  137. bool show_grid_buttons = true;
  138. bool show_zoom_buttons = true;
  139. bool show_minimap_button = true;
  140. bool show_arrange_button = true;
  141. bool snapping_enabled = true;
  142. int snapping_distance = 20;
  143. bool show_grid = true;
  144. bool connecting = false;
  145. String connecting_from;
  146. bool connecting_out = false;
  147. int connecting_index = 0;
  148. int connecting_type = 0;
  149. Color connecting_color;
  150. bool connecting_target = false;
  151. Vector2 connecting_to;
  152. StringName connecting_target_to;
  153. int connecting_target_index = 0;
  154. bool just_disconnected = false;
  155. bool connecting_valid = false;
  156. Vector2 click_pos;
  157. PanningScheme panning_scheme = SCROLL_ZOOMS;
  158. bool dragging = false;
  159. bool just_selected = false;
  160. bool moving_selection = false;
  161. Vector2 drag_accum;
  162. float zoom = 1.0;
  163. float zoom_step = 1.2;
  164. // Proper values set in constructor.
  165. float zoom_min = 0.0;
  166. float zoom_max = 0.0;
  167. bool box_selecting = false;
  168. bool box_selection_mode_additive = false;
  169. Point2 box_selecting_from;
  170. Point2 box_selecting_to;
  171. Rect2 box_selecting_rect;
  172. List<GraphElement *> prev_selected;
  173. bool setting_scroll_offset = false;
  174. bool right_disconnects = false;
  175. bool updating = false;
  176. bool awaiting_scroll_offset_update = false;
  177. List<Connection> connections;
  178. float lines_thickness = 2.0f;
  179. float lines_curvature = 0.5f;
  180. bool lines_antialiased = true;
  181. PanelContainer *menu_panel = nullptr;
  182. HBoxContainer *menu_hbox = nullptr;
  183. Control *connections_layer = nullptr;
  184. GraphEditFilter *top_layer = nullptr;
  185. GraphEditMinimap *minimap = nullptr;
  186. Ref<GraphEditArranger> arranger;
  187. HashSet<ConnectionType, ConnectionType> valid_connection_types;
  188. HashSet<int> valid_left_disconnect_types;
  189. HashSet<int> valid_right_disconnect_types;
  190. struct ThemeCache {
  191. float base_scale = 1.0;
  192. Ref<StyleBox> panel;
  193. Color grid_major;
  194. Color grid_minor;
  195. Color activity_color;
  196. Color selection_fill;
  197. Color selection_stroke;
  198. Ref<StyleBox> menu_panel;
  199. Ref<Texture2D> zoom_in;
  200. Ref<Texture2D> zoom_out;
  201. Ref<Texture2D> zoom_reset;
  202. Ref<Texture2D> snapping_toggle;
  203. Ref<Texture2D> grid_toggle;
  204. Ref<Texture2D> minimap_toggle;
  205. Ref<Texture2D> layout;
  206. float port_hotzone_inner_extent = 0.0;
  207. float port_hotzone_outer_extent = 0.0;
  208. } theme_cache;
  209. void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  210. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  211. void _zoom_minus();
  212. void _zoom_reset();
  213. void _zoom_plus();
  214. void _update_zoom_label();
  215. 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);
  216. void _graph_element_selected(Node *p_node);
  217. void _graph_element_deselected(Node *p_node);
  218. void _graph_element_moved_to_front(Node *p_node);
  219. void _graph_element_resized(Vector2 p_new_minsize, Node *p_node);
  220. void _graph_element_moved(Node *p_node);
  221. void _graph_node_slot_updated(int p_index, Node *p_node);
  222. void _update_scroll();
  223. void _update_scroll_offset();
  224. void _scroll_moved(double);
  225. virtual void gui_input(const Ref<InputEvent> &p_ev) override;
  226. void _top_layer_input(const Ref<InputEvent> &p_ev);
  227. bool is_in_port_hotzone(const Vector2 &p_pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left);
  228. void _top_layer_draw();
  229. void _connections_layer_draw();
  230. void _minimap_draw();
  231. TypedArray<Dictionary> _get_connection_list() const;
  232. friend class GraphEditFilter;
  233. bool _filter_input(const Point2 &p_point);
  234. void _snapping_toggled();
  235. void _snapping_distance_changed(double);
  236. void _show_grid_toggled();
  237. friend class GraphEditMinimap;
  238. void _minimap_toggled();
  239. bool _check_clickable_control(Control *p_control, const Vector2 &r_mouse_pos, const Vector2 &p_offset);
  240. #ifndef DISABLE_DEPRECATED
  241. bool _is_arrange_nodes_button_hidden_bind_compat_81582() const;
  242. void _set_arrange_nodes_button_hidden_bind_compat_81582(bool p_enable);
  243. #endif
  244. protected:
  245. virtual void _update_theme_item_cache() override;
  246. virtual void add_child_notify(Node *p_child) override;
  247. virtual void remove_child_notify(Node *p_child) override;
  248. void _notification(int p_what);
  249. static void _bind_methods();
  250. #ifndef DISABLE_DEPRECATED
  251. static void _bind_compatibility_methods();
  252. #endif
  253. virtual bool is_in_input_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size);
  254. virtual bool is_in_output_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size);
  255. GDVIRTUAL2RC(Vector<Vector2>, _get_connection_line, Vector2, Vector2)
  256. GDVIRTUAL3R(bool, _is_in_input_hotzone, Object *, int, Vector2)
  257. GDVIRTUAL3R(bool, _is_in_output_hotzone, Object *, int, Vector2)
  258. GDVIRTUAL4R(bool, _is_node_hover_valid, StringName, int, StringName, int);
  259. public:
  260. virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
  261. PackedStringArray get_configuration_warnings() const override;
  262. Error connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  263. bool is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  264. void disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  265. void clear_connections();
  266. void force_connection_drag_end();
  267. virtual PackedVector2Array get_connection_line(const Vector2 &p_from, const Vector2 &p_to);
  268. virtual bool is_node_hover_valid(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  269. void set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity);
  270. void add_valid_connection_type(int p_type, int p_with_type);
  271. void remove_valid_connection_type(int p_type, int p_with_type);
  272. bool is_valid_connection_type(int p_type, int p_with_type) const;
  273. void set_panning_scheme(PanningScheme p_scheme);
  274. PanningScheme get_panning_scheme() const;
  275. void set_zoom(float p_zoom);
  276. void set_zoom_custom(float p_zoom, const Vector2 &p_center);
  277. float get_zoom() const;
  278. void set_zoom_min(float p_zoom_min);
  279. float get_zoom_min() const;
  280. void set_zoom_max(float p_zoom_max);
  281. float get_zoom_max() const;
  282. void set_zoom_step(float p_zoom_step);
  283. float get_zoom_step() const;
  284. void set_minimap_size(Vector2 p_size);
  285. Vector2 get_minimap_size() const;
  286. void set_minimap_opacity(float p_opacity);
  287. float get_minimap_opacity() const;
  288. void set_minimap_enabled(bool p_enable);
  289. bool is_minimap_enabled() const;
  290. void set_show_menu(bool p_hidden);
  291. bool is_showing_menu() const;
  292. void set_show_zoom_label(bool p_hidden);
  293. bool is_showing_zoom_label() const;
  294. void set_show_grid_buttons(bool p_hidden);
  295. bool is_showing_grid_buttons() const;
  296. void set_show_zoom_buttons(bool p_hidden);
  297. bool is_showing_zoom_buttons() const;
  298. void set_show_minimap_button(bool p_hidden);
  299. bool is_showing_minimap_button() const;
  300. void set_show_arrange_button(bool p_hidden);
  301. bool is_showing_arrange_button() const;
  302. GraphEditFilter *get_top_layer() const { return top_layer; }
  303. GraphEditMinimap *get_minimap() const { return minimap; }
  304. void get_connection_list(List<Connection> *r_connections) const;
  305. void set_right_disconnects(bool p_enable);
  306. bool is_right_disconnects_enabled() const;
  307. void add_valid_right_disconnect_type(int p_type);
  308. void remove_valid_right_disconnect_type(int p_type);
  309. void add_valid_left_disconnect_type(int p_type);
  310. void remove_valid_left_disconnect_type(int p_type);
  311. void set_scroll_offset(const Vector2 &p_ofs);
  312. Vector2 get_scroll_offset() const;
  313. void set_selected(Node *p_child);
  314. void set_snapping_enabled(bool p_enable);
  315. bool is_snapping_enabled() const;
  316. void set_snapping_distance(int p_snapping_distance);
  317. int get_snapping_distance() const;
  318. void set_show_grid(bool p_enable);
  319. bool is_showing_grid() const;
  320. void set_connection_lines_curvature(float p_curvature);
  321. float get_connection_lines_curvature() const;
  322. void set_connection_lines_thickness(float p_thickness);
  323. float get_connection_lines_thickness() const;
  324. void set_connection_lines_antialiased(bool p_antialiased);
  325. bool is_connection_lines_antialiased() const;
  326. HBoxContainer *get_menu_hbox();
  327. Ref<ViewPanner> get_panner();
  328. void set_warped_panning(bool p_warped);
  329. void arrange_nodes();
  330. GraphEdit();
  331. };
  332. VARIANT_ENUM_CAST(GraphEdit::PanningScheme);
  333. #endif // GRAPH_EDIT_H