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