graph_edit.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/graph_node.h"
  34. #include "scene/gui/scroll_bar.h"
  35. #include "scene/gui/slider.h"
  36. #include "scene/gui/spin_box.h"
  37. #include "scene/gui/texture_rect.h"
  38. #include "scene/gui/tool_button.h"
  39. class GraphEdit;
  40. class GraphEditFilter : public Control {
  41. GDCLASS(GraphEditFilter, Control);
  42. friend class GraphEdit;
  43. friend class GraphEditMinimap;
  44. GraphEdit *ge;
  45. virtual bool has_point(const Point2 &p_point) const;
  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;
  54. protected:
  55. static void _bind_methods();
  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. void _gui_input(const Ref<InputEvent> &p_ev);
  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;
  84. int to_port;
  85. float activity;
  86. };
  87. private:
  88. ToolButton *zoom_minus;
  89. ToolButton *zoom_reset;
  90. ToolButton *zoom_plus;
  91. ToolButton *snap_button;
  92. SpinBox *snap_amount;
  93. Button *minimap_button;
  94. void _zoom_minus();
  95. void _zoom_reset();
  96. void _zoom_plus();
  97. HScrollBar *h_scroll;
  98. VScrollBar *v_scroll;
  99. float port_grab_distance_horizontal;
  100. float port_grab_distance_vertical;
  101. bool connecting;
  102. String connecting_from;
  103. bool connecting_out;
  104. int connecting_index;
  105. int connecting_type;
  106. Color connecting_color;
  107. bool connecting_target;
  108. Vector2 connecting_to;
  109. String connecting_target_to;
  110. int connecting_target_index;
  111. bool just_disconnected;
  112. bool dragging;
  113. bool just_selected;
  114. bool moving_selection;
  115. Vector2 drag_accum;
  116. float zoom;
  117. bool box_selecting;
  118. bool box_selection_mode_additive;
  119. Point2 box_selecting_from;
  120. Point2 box_selecting_to;
  121. Rect2 box_selecting_rect;
  122. List<GraphNode *> previous_selected;
  123. bool setting_scroll_ofs;
  124. bool right_disconnects;
  125. bool updating;
  126. bool awaiting_scroll_offset_update;
  127. List<Connection> connections;
  128. 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;
  129. 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, float p_bezier_ratio);
  130. void _graph_node_raised(Node *p_gn);
  131. void _graph_node_moved(Node *p_gn);
  132. void _graph_node_slot_updated(int p_index, Node *p_gn);
  133. void _update_scroll();
  134. void _scroll_moved(double);
  135. void _gui_input(const Ref<InputEvent> &p_ev);
  136. Control *connections_layer;
  137. GraphEditFilter *top_layer;
  138. GraphEditMinimap *minimap;
  139. void _top_layer_input(const Ref<InputEvent> &p_ev);
  140. bool is_in_hot_zone(const Vector2 &pos, const Vector2 &p_mouse_pos);
  141. void _top_layer_draw();
  142. void _connections_layer_draw();
  143. void _minimap_draw();
  144. void _update_scroll_offset();
  145. Array _get_connection_list() const;
  146. bool lines_on_bg;
  147. struct ConnType {
  148. union {
  149. struct {
  150. uint32_t type_a;
  151. uint32_t type_b;
  152. };
  153. uint64_t key;
  154. };
  155. bool operator<(const ConnType &p_type) const {
  156. return key < p_type.key;
  157. }
  158. ConnType(uint32_t a = 0, uint32_t b = 0) {
  159. type_a = a;
  160. type_b = b;
  161. }
  162. };
  163. Set<ConnType> valid_connection_types;
  164. Set<int> valid_left_disconnect_types;
  165. Set<int> valid_right_disconnect_types;
  166. HBoxContainer *zoom_hb;
  167. friend class GraphEditFilter;
  168. bool _filter_input(const Point2 &p_point);
  169. void _snap_toggled();
  170. void _snap_value_changed(double);
  171. friend class GraphEditMinimap;
  172. void _minimap_toggled();
  173. bool _check_clickable_control(Control *p_control, const Vector2 &pos);
  174. protected:
  175. static void _bind_methods();
  176. virtual void add_child_notify(Node *p_child);
  177. virtual void remove_child_notify(Node *p_child);
  178. void _notification(int p_what);
  179. virtual bool clips_input() const;
  180. public:
  181. Error connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  182. bool is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  183. void disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  184. void clear_connections();
  185. void set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity);
  186. void add_valid_connection_type(int p_type, int p_with_type);
  187. void remove_valid_connection_type(int p_type, int p_with_type);
  188. bool is_valid_connection_type(int p_type, int p_with_type) const;
  189. void set_zoom(float p_zoom);
  190. void set_zoom_custom(float p_zoom, const Vector2 &p_center);
  191. float get_zoom() const;
  192. void set_minimap_size(Vector2 p_size);
  193. Vector2 get_minimap_size() const;
  194. void set_minimap_opacity(float p_opacity);
  195. float get_minimap_opacity() const;
  196. void set_minimap_enabled(bool p_enable);
  197. bool is_minimap_enabled() const;
  198. GraphEditFilter *get_top_layer() const { return top_layer; }
  199. GraphEditMinimap *get_minimap() const { return minimap; }
  200. void get_connection_list(List<Connection> *r_connections) const;
  201. void set_right_disconnects(bool p_enable);
  202. bool is_right_disconnects_enabled() const;
  203. void add_valid_right_disconnect_type(int p_type);
  204. void remove_valid_right_disconnect_type(int p_type);
  205. void add_valid_left_disconnect_type(int p_type);
  206. void remove_valid_left_disconnect_type(int p_type);
  207. void set_scroll_ofs(const Vector2 &p_ofs);
  208. Vector2 get_scroll_ofs() const;
  209. void set_selected(Node *p_child);
  210. void set_use_snap(bool p_enable);
  211. bool is_using_snap() const;
  212. int get_snap() const;
  213. void set_snap(int p_snap);
  214. HBoxContainer *get_zoom_hbox();
  215. GraphEdit();
  216. };
  217. #endif // GRAPHEdit_H