graph_element.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**************************************************************************/
  2. /* graph_element.cpp */
  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. #include "graph_element.h"
  31. #include "core/string/translation.h"
  32. #include "scene/gui/graph_edit.h"
  33. #ifdef TOOLS_ENABLED
  34. void GraphElement::_edit_set_position(const Point2 &p_position) {
  35. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  36. if (graph) {
  37. Point2 offset = (p_position + graph->get_scroll_offset()) * graph->get_zoom();
  38. set_position_offset(offset);
  39. }
  40. set_position(p_position);
  41. }
  42. #endif
  43. void GraphElement::_resort() {
  44. Size2 size = get_size();
  45. for (int i = 0; i < get_child_count(); i++) {
  46. Control *child = Object::cast_to<Control>(get_child(i));
  47. if (!child || !child->is_visible_in_tree()) {
  48. continue;
  49. }
  50. if (child->is_set_as_top_level()) {
  51. continue;
  52. }
  53. fit_child_in_rect(child, Rect2(Point2(), size));
  54. }
  55. }
  56. Size2 GraphElement::get_minimum_size() const {
  57. Size2 minsize;
  58. for (int i = 0; i < get_child_count(); i++) {
  59. Control *child = Object::cast_to<Control>(get_child(i));
  60. if (!child) {
  61. continue;
  62. }
  63. if (child->is_set_as_top_level()) {
  64. continue;
  65. }
  66. Size2i size = child->get_combined_minimum_size();
  67. minsize.width = MAX(minsize.width, size.width);
  68. minsize.height = MAX(minsize.height, size.height);
  69. }
  70. return minsize;
  71. }
  72. void GraphElement::_notification(int p_what) {
  73. switch (p_what) {
  74. case NOTIFICATION_SORT_CHILDREN: {
  75. _resort();
  76. } break;
  77. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  78. case NOTIFICATION_TRANSLATION_CHANGED:
  79. case NOTIFICATION_THEME_CHANGED: {
  80. update_minimum_size();
  81. queue_redraw();
  82. } break;
  83. }
  84. }
  85. void GraphElement::_validate_property(PropertyInfo &p_property) const {
  86. Control::_validate_property(p_property);
  87. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  88. if (graph) {
  89. if (p_property.name == "position") {
  90. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  91. }
  92. }
  93. }
  94. void GraphElement::set_position_offset(const Vector2 &p_offset) {
  95. if (position_offset == p_offset) {
  96. return;
  97. }
  98. position_offset = p_offset;
  99. emit_signal(SNAME("position_offset_changed"));
  100. queue_redraw();
  101. }
  102. Vector2 GraphElement::get_position_offset() const {
  103. return position_offset;
  104. }
  105. void GraphElement::set_selected(bool p_selected) {
  106. if (!is_selectable() || selected == p_selected) {
  107. return;
  108. }
  109. selected = p_selected;
  110. emit_signal(p_selected ? SNAME("node_selected") : SNAME("node_deselected"));
  111. queue_redraw();
  112. }
  113. bool GraphElement::is_selected() {
  114. return selected;
  115. }
  116. void GraphElement::set_drag(bool p_drag) {
  117. if (p_drag) {
  118. drag_from = get_position_offset();
  119. } else {
  120. emit_signal(SNAME("dragged"), drag_from, get_position_offset()); // Required for undo/redo.
  121. }
  122. }
  123. Vector2 GraphElement::get_drag_from() {
  124. return drag_from;
  125. }
  126. void GraphElement::gui_input(const Ref<InputEvent> &p_ev) {
  127. ERR_FAIL_COND(p_ev.is_null());
  128. Ref<InputEventMouseButton> mb = p_ev;
  129. if (mb.is_valid()) {
  130. ERR_FAIL_COND_MSG(get_parent_control() == nullptr, "GraphElement must be the child of a GraphEdit node.");
  131. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  132. Vector2 mpos = mb->get_position();
  133. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  134. if (resizable && mpos.x > get_size().x - resizer->get_width() && mpos.y > get_size().y - resizer->get_height()) {
  135. resizing = true;
  136. resizing_from = mpos;
  137. resizing_from_size = get_size();
  138. accept_event();
  139. return;
  140. }
  141. emit_signal(SNAME("raise_request"));
  142. }
  143. if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  144. resizing = false;
  145. }
  146. }
  147. Ref<InputEventMouseMotion> mm = p_ev;
  148. if (resizing && mm.is_valid()) {
  149. Vector2 mpos = mm->get_position();
  150. Vector2 diff = mpos - resizing_from;
  151. emit_signal(SNAME("resize_request"), resizing_from_size + diff);
  152. }
  153. }
  154. void GraphElement::set_resizable(bool p_enable) {
  155. if (resizable == p_enable) {
  156. return;
  157. }
  158. resizable = p_enable;
  159. queue_redraw();
  160. }
  161. bool GraphElement::is_resizable() const {
  162. return resizable;
  163. }
  164. void GraphElement::set_draggable(bool p_draggable) {
  165. draggable = p_draggable;
  166. }
  167. bool GraphElement::is_draggable() {
  168. return draggable;
  169. }
  170. void GraphElement::set_selectable(bool p_selectable) {
  171. if (!p_selectable) {
  172. set_selected(false);
  173. }
  174. selectable = p_selectable;
  175. }
  176. bool GraphElement::is_selectable() {
  177. return selectable;
  178. }
  179. void GraphElement::_bind_methods() {
  180. ClassDB::bind_method(D_METHOD("set_resizable", "resizable"), &GraphElement::set_resizable);
  181. ClassDB::bind_method(D_METHOD("is_resizable"), &GraphElement::is_resizable);
  182. ClassDB::bind_method(D_METHOD("set_draggable", "draggable"), &GraphElement::set_draggable);
  183. ClassDB::bind_method(D_METHOD("is_draggable"), &GraphElement::is_draggable);
  184. ClassDB::bind_method(D_METHOD("set_selectable", "selectable"), &GraphElement::set_selectable);
  185. ClassDB::bind_method(D_METHOD("is_selectable"), &GraphElement::is_selectable);
  186. ClassDB::bind_method(D_METHOD("set_selected", "selected"), &GraphElement::set_selected);
  187. ClassDB::bind_method(D_METHOD("is_selected"), &GraphElement::is_selected);
  188. ClassDB::bind_method(D_METHOD("set_position_offset", "offset"), &GraphElement::set_position_offset);
  189. ClassDB::bind_method(D_METHOD("get_position_offset"), &GraphElement::get_position_offset);
  190. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position_offset"), "set_position_offset", "get_position_offset");
  191. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable"), "set_resizable", "is_resizable");
  192. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draggable"), "set_draggable", "is_draggable");
  193. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selectable"), "set_selectable", "is_selectable");
  194. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selected"), "set_selected", "is_selected");
  195. ADD_SIGNAL(MethodInfo("position_offset_changed"));
  196. ADD_SIGNAL(MethodInfo("node_selected"));
  197. ADD_SIGNAL(MethodInfo("node_deselected"));
  198. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::VECTOR2, "from"), PropertyInfo(Variant::VECTOR2, "to")));
  199. ADD_SIGNAL(MethodInfo("raise_request"));
  200. ADD_SIGNAL(MethodInfo("close_request"));
  201. ADD_SIGNAL(MethodInfo("resize_request", PropertyInfo(Variant::VECTOR2, "new_minsize")));
  202. }