graph_element.cpp 8.1 KB

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