graph_edit.h 6.6 KB

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