graph_edit.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/graph_node.h"
  34. #include "scene/gui/label.h"
  35. #include "scene/gui/scroll_bar.h"
  36. #include "scene/gui/slider.h"
  37. #include "scene/gui/spin_box.h"
  38. #include "scene/gui/texture_rect.h"
  39. #include "scene/gui/tool_button.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;
  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. static void _bind_methods();
  57. public:
  58. GraphEditMinimap(GraphEdit *p_edit);
  59. void update_minimap();
  60. Rect2 get_camera_rect();
  61. private:
  62. Vector2 minimap_padding;
  63. Vector2 minimap_offset;
  64. Vector2 graph_proportions;
  65. Vector2 graph_padding;
  66. Vector2 camera_position;
  67. Vector2 camera_size;
  68. bool is_pressing;
  69. bool is_resizing;
  70. Vector2 _get_render_size();
  71. Vector2 _get_graph_offset();
  72. Vector2 _get_graph_size();
  73. Vector2 _convert_from_graph_position(const Vector2 &p_position);
  74. Vector2 _convert_to_graph_position(const Vector2 &p_position);
  75. void _gui_input(const Ref<InputEvent> &p_ev);
  76. void _adjust_graph_scroll(const Vector2 &p_offset);
  77. };
  78. class GraphEdit : public Control {
  79. GDCLASS(GraphEdit, Control);
  80. public:
  81. struct Connection {
  82. StringName from;
  83. StringName to;
  84. int from_port;
  85. int to_port;
  86. float activity;
  87. };
  88. private:
  89. Label *zoom_label;
  90. ToolButton *zoom_minus;
  91. ToolButton *zoom_reset;
  92. ToolButton *zoom_plus;
  93. ToolButton *snap_button;
  94. SpinBox *snap_amount;
  95. Button *minimap_button;
  96. HScrollBar *h_scroll;
  97. VScrollBar *v_scroll;
  98. float port_grab_distance_horizontal;
  99. float port_grab_distance_vertical;
  100. bool connecting = false;
  101. String connecting_from;
  102. bool connecting_out = false;
  103. int connecting_index;
  104. int connecting_type;
  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.0f;
  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. void _bake_segment2d(Vector<Vector2> &points, Vector<Color> &colors, float p_begin, float p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_min_depth, int p_max_depth, float p_tol, const Color &p_color, const Color &p_to_color, int &lines) const;
  137. void _draw_cos_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color, float p_width = 2.0, float p_bezier_ratio = 1.0);
  138. void _graph_node_raised(Node *p_gn);
  139. void _graph_node_moved(Node *p_gn);
  140. void _graph_node_slot_updated(int p_index, Node *p_gn);
  141. void _update_scroll();
  142. void _scroll_moved(double);
  143. void _gui_input(const Ref<InputEvent> &p_ev);
  144. Control *connections_layer;
  145. GraphEditFilter *top_layer;
  146. GraphEditMinimap *minimap;
  147. void _top_layer_input(const Ref<InputEvent> &p_ev);
  148. bool is_in_hot_zone(const Vector2 &pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left);
  149. void _top_layer_draw();
  150. void _connections_layer_draw();
  151. void _minimap_draw();
  152. void _update_scroll_offset();
  153. Array _get_connection_list() const;
  154. bool lines_on_bg;
  155. struct ConnType {
  156. union {
  157. struct {
  158. uint32_t type_a;
  159. uint32_t type_b;
  160. };
  161. uint64_t key;
  162. };
  163. bool operator<(const ConnType &p_type) const {
  164. return key < p_type.key;
  165. }
  166. ConnType(uint32_t a = 0, uint32_t b = 0) {
  167. type_a = a;
  168. type_b = b;
  169. }
  170. };
  171. Set<ConnType> valid_connection_types;
  172. Set<int> valid_left_disconnect_types;
  173. Set<int> valid_right_disconnect_types;
  174. HBoxContainer *zoom_hb;
  175. friend class GraphEditFilter;
  176. bool _filter_input(const Point2 &p_point);
  177. void _snap_toggled();
  178. void _snap_value_changed(double);
  179. friend class GraphEditMinimap;
  180. void _minimap_toggled();
  181. bool _check_clickable_control(Control *p_control, const Vector2 &pos);
  182. protected:
  183. static void _bind_methods();
  184. virtual void add_child_notify(Node *p_child);
  185. virtual void remove_child_notify(Node *p_child);
  186. void _notification(int p_what);
  187. virtual bool clips_input() const;
  188. public:
  189. Error connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  190. bool is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  191. void disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  192. void clear_connections();
  193. void set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity);
  194. void add_valid_connection_type(int p_type, int p_with_type);
  195. void remove_valid_connection_type(int p_type, int p_with_type);
  196. bool is_valid_connection_type(int p_type, int p_with_type) const;
  197. void set_zoom(float p_zoom);
  198. void set_zoom_custom(float p_zoom, const Vector2 &p_center);
  199. float get_zoom() const;
  200. void set_zoom_min(float p_zoom_min);
  201. float get_zoom_min() const;
  202. void set_zoom_max(float p_zoom_max);
  203. float get_zoom_max() const;
  204. void set_zoom_step(float p_zoom_step);
  205. float get_zoom_step() const;
  206. void set_show_zoom_label(bool p_enable);
  207. bool is_showing_zoom_label() const;
  208. void set_minimap_size(Vector2 p_size);
  209. Vector2 get_minimap_size() const;
  210. void set_minimap_opacity(float p_opacity);
  211. float get_minimap_opacity() const;
  212. void set_minimap_enabled(bool p_enable);
  213. bool is_minimap_enabled() const;
  214. GraphEditFilter *get_top_layer() const { return top_layer; }
  215. GraphEditMinimap *get_minimap() const { return minimap; }
  216. void get_connection_list(List<Connection> *r_connections) const;
  217. void set_right_disconnects(bool p_enable);
  218. bool is_right_disconnects_enabled() const;
  219. void add_valid_right_disconnect_type(int p_type);
  220. void remove_valid_right_disconnect_type(int p_type);
  221. void add_valid_left_disconnect_type(int p_type);
  222. void remove_valid_left_disconnect_type(int p_type);
  223. void set_scroll_ofs(const Vector2 &p_ofs);
  224. Vector2 get_scroll_ofs() const;
  225. void set_selected(Node *p_child);
  226. void set_use_snap(bool p_enable);
  227. bool is_using_snap() const;
  228. int get_snap() const;
  229. void set_snap(int p_snap);
  230. HBoxContainer *get_zoom_hbox();
  231. GraphEdit();
  232. };
  233. #endif // GRAPHEdit_H