graph_edit.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*************************************************************************/
  2. /* graph_edit.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. GraphEdit *ge;
  44. virtual bool has_point(const Point2 &p_point) const;
  45. public:
  46. GraphEditFilter(GraphEdit *p_edit);
  47. };
  48. class GraphEdit : public Control {
  49. GDCLASS(GraphEdit, Control);
  50. public:
  51. struct Connection {
  52. StringName from;
  53. StringName to;
  54. int from_port;
  55. int to_port;
  56. float activity;
  57. };
  58. private:
  59. ToolButton *zoom_minus;
  60. ToolButton *zoom_reset;
  61. ToolButton *zoom_plus;
  62. ToolButton *snap_button;
  63. SpinBox *snap_amount;
  64. void _zoom_minus();
  65. void _zoom_reset();
  66. void _zoom_plus();
  67. HScrollBar *h_scroll;
  68. VScrollBar *v_scroll;
  69. float port_grab_distance_horizontal;
  70. float port_grab_distance_vertical;
  71. bool connecting;
  72. String connecting_from;
  73. bool connecting_out;
  74. int connecting_index;
  75. int connecting_type;
  76. Color connecting_color;
  77. bool connecting_target;
  78. Vector2 connecting_to;
  79. String connecting_target_to;
  80. int connecting_target_index;
  81. bool just_disconnected;
  82. bool dragging;
  83. bool just_selected;
  84. Vector2 drag_accum;
  85. Point2 drag_origin; // Workaround for GH-5907
  86. float zoom;
  87. bool box_selecting;
  88. bool box_selection_mode_aditive;
  89. Point2 box_selecting_from;
  90. Point2 box_selecting_to;
  91. Rect2 box_selecting_rect;
  92. List<GraphNode *> previus_selected;
  93. bool setting_scroll_ofs;
  94. bool right_disconnects;
  95. bool updating;
  96. bool awaiting_scroll_offset_update;
  97. List<Connection> connections;
  98. 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;
  99. void _draw_cos_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color);
  100. void _graph_node_raised(Node *p_gn);
  101. void _graph_node_moved(Node *p_gn);
  102. void _update_scroll();
  103. void _scroll_moved(double);
  104. void _gui_input(const Ref<InputEvent> &p_ev);
  105. Control *connections_layer;
  106. GraphEditFilter *top_layer;
  107. void _top_layer_input(const Ref<InputEvent> &p_ev);
  108. bool is_in_hot_zone(const Vector2 &pos, const Vector2 &p_mouse_pos);
  109. void _top_layer_draw();
  110. void _connections_layer_draw();
  111. void _update_scroll_offset();
  112. Array _get_connection_list() const;
  113. bool lines_on_bg;
  114. struct ConnType {
  115. union {
  116. struct {
  117. uint32_t type_a;
  118. uint32_t type_b;
  119. };
  120. uint64_t key;
  121. };
  122. bool operator<(const ConnType &p_type) const {
  123. return key < p_type.key;
  124. }
  125. ConnType(uint32_t a = 0, uint32_t b = 0) {
  126. type_a = a;
  127. type_b = b;
  128. }
  129. };
  130. Set<ConnType> valid_connection_types;
  131. Set<int> valid_left_disconnect_types;
  132. Set<int> valid_right_disconnect_types;
  133. HBoxContainer *zoom_hb;
  134. friend class GraphEditFilter;
  135. bool _filter_input(const Point2 &p_point);
  136. void _snap_toggled();
  137. void _snap_value_changed(double);
  138. bool _check_clickable_control(Control *p_control, const Vector2 &pos);
  139. protected:
  140. static void _bind_methods();
  141. virtual void add_child_notify(Node *p_child);
  142. virtual void remove_child_notify(Node *p_child);
  143. void _notification(int p_what);
  144. virtual bool clips_input() const;
  145. public:
  146. Error connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  147. bool is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  148. void disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
  149. void clear_connections();
  150. void set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity);
  151. void add_valid_connection_type(int p_type, int p_with_type);
  152. void remove_valid_connection_type(int p_type, int p_with_type);
  153. bool is_valid_connection_type(int p_type, int p_with_type) const;
  154. void set_zoom(float p_zoom);
  155. void set_zoom_custom(float p_zoom, const Vector2 &p_center);
  156. float get_zoom() const;
  157. GraphEditFilter *get_top_layer() const { return top_layer; }
  158. void get_connection_list(List<Connection> *r_connections) const;
  159. void set_right_disconnects(bool p_enable);
  160. bool is_right_disconnects_enabled() const;
  161. void add_valid_right_disconnect_type(int p_type);
  162. void remove_valid_right_disconnect_type(int p_type);
  163. void add_valid_left_disconnect_type(int p_type);
  164. void remove_valid_left_disconnect_type(int p_type);
  165. void set_scroll_ofs(const Vector2 &p_ofs);
  166. Vector2 get_scroll_ofs() const;
  167. void set_selected(Node *p_child);
  168. void set_use_snap(bool p_enable);
  169. bool is_using_snap() const;
  170. int get_snap() const;
  171. void set_snap(int p_snap);
  172. HBoxContainer *get_zoom_hbox();
  173. GraphEdit();
  174. };
  175. #endif // GRAPHEdit_H