2
0

graph_element.cpp 8.2 KB

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