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