Debian Codegraph @

scene/gui/graph_frame.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* graph_frame.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_frame.h"
32
33 #include "scene/gui/box_container.h"
34 #include "scene/gui/label.h"
35 #include "scene/resources/style_box_flat.h"
36 #include "scene/resources/style_box_texture.h"
37 #include "scene/theme/theme_db.h"
38
39 void GraphFrame::gui_input(const Ref<InputEvent> &p_ev) {
40 ERR_FAIL_COND(p_ev.is_null());
41
42 Ref<InputEventMouseButton> mb = p_ev;
43 if (mb.is_valid()) {
44 ERR_FAIL_NULL_MSG(get_parent_control(), "GraphFrame must be the child of a GraphEdit node.");
45
46 if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
47 Vector2 mpos = mb->get_position();
48
49 Ref<Texture2D> resizer = theme_cache.resizer;
50
51 if (resizable && mpos.x > get_size().x - resizer->get_width() && mpos.y > get_size().y - resizer->get_height()) {
52 resizing = true;
53 resizing_from = mpos;
54 resizing_from_size = get_size();
55 accept_event();
56 return;
57 }
58
59 emit_signal(SNAME("raise_request"));
60 }
61
62 if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
63 if (resizing) {
64 resizing = false;
65 emit_signal(SNAME("resize_end"), get_size());
66 return;
67 }
68 }
69 }
70
71 Ref<InputEventMouseMotion> mm = p_ev;
72
73 // Only resize if the frame is not auto-resizing based on linked nodes.
74 if (resizing && !autoshrink_enabled && mm.is_valid()) {
75 Vector2 mpos = mm->get_position();
76
77 Vector2 diff = mpos - resizing_from;
78
79 emit_signal(SNAME("resize_request"), resizing_from_size + diff);
80 }
81 }
82
83 Control::CursorShape GraphFrame::get_cursor_shape(const Point2 &p_pos) const {
84 if (resizable && !autoshrink_enabled) {
85 if (resizing || (p_pos.x > get_size().x - theme_cache.resizer->get_width() && p_pos.y > get_size().y - theme_cache.resizer->get_height())) {
86 return CURSOR_FDIAGSIZE;
87 }
88 }
89
90 return Control::get_cursor_shape(p_pos);
91 }
92
93 void GraphFrame::_notification(int p_what) {
94 switch (p_what) {
95 case NOTIFICATION_DRAW: {
96 // Used for layout calculations.
97 Ref<StyleBox> sb_panel = theme_cache.panel;
98 Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
99
100 // Used for drawing.
101 Ref<StyleBox> sb_to_draw_panel = selected ? theme_cache.panel_selected : sb_panel;
102 Ref<StyleBox> sb_to_draw_titlebar = selected ? theme_cache.titlebar_selected : sb_titlebar;
103 Ref<StyleBoxFlat> sb_panel_flat = sb_to_draw_panel;
104 Ref<StyleBoxTexture> sb_panel_texture = sb_to_draw_panel;
105
106 Rect2 titlebar_rect(Point2(), titlebar_hbox->get_combined_minimum_size() + sb_titlebar->get_minimum_size());
107 Size2 body_size = get_size();
108 titlebar_rect.size.width = body_size.width;
109 body_size.y -= titlebar_rect.size.height;
110 Rect2 body_rect(Point2(0, titlebar_rect.size.height), body_size);
111
112 // Draw body stylebox.
113 if (tint_color_enabled) {
114 if (sb_panel_flat.is_valid()) {
115 Color original_border_color = sb_panel_flat->get_border_color();
116 sb_panel_flat = sb_panel_flat->duplicate();
117 sb_panel_flat->set_bg_color(tint_color);
118 sb_panel_flat->set_border_color(selected ? original_border_color : tint_color.lightened(0.3));
119 draw_style_box(sb_panel_flat, body_rect);
120 } else if (sb_panel_texture.is_valid()) {
121 sb_panel_texture = sb_panel_texture->duplicate();
122 sb_panel_texture->set_modulate(tint_color);
123 draw_style_box(sb_panel_texture, body_rect);
124 }
125 } else {
126 draw_style_box(sb_panel_flat, body_rect);
127 }
128
129 // Draw title bar stylebox above.
130 draw_style_box(sb_to_draw_titlebar, titlebar_rect);
131
132 // Only draw the resize handle if the frame is not auto-resizing.
133 if (resizable && !autoshrink_enabled) {
134 Ref<Texture2D> resizer = theme_cache.resizer;
135 Color resizer_color = theme_cache.resizer_color;
136 if (resizable) {
137 draw_texture(resizer, get_size() - resizer->get_size(), resizer_color);
138 }
139 }
140
141 } break;
142 }
143 }
144
145 void GraphFrame::_resort() {
146 Ref<StyleBox> sb_panel = theme_cache.panel;
147 Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
148
149 // Resort titlebar first.
150 Size2 titlebar_size = Size2(get_size().width, titlebar_hbox->get_size().height);
151 titlebar_size -= sb_titlebar->get_minimum_size();
152 Rect2 titlebar_rect = Rect2(sb_titlebar->get_offset(), titlebar_size);
153
154 fit_child_in_rect(titlebar_hbox, titlebar_rect);
155
156 // After resort the children of the titlebar container may have changed their height (e.g. Label autowrap).
157 Size2i titlebar_min_size = titlebar_hbox->get_combined_minimum_size();
158
159 Size2 size = get_size() - sb_panel->get_minimum_size() - Size2(0, titlebar_min_size.height + sb_titlebar->get_minimum_size().height);
160 Point2 offset = Point2(sb_panel->get_margin(SIDE_LEFT), sb_panel->get_margin(SIDE_TOP) + titlebar_min_size.height + sb_titlebar->get_minimum_size().height);
161
162 for (int i = 0; i < get_child_count(false); i++) {
163 Control *child = as_sortable_control(get_child(i, false));
164 if (!child) {
165 continue;
166 }
167 fit_child_in_rect(child, Rect2(offset, size));
168 }
169 }
170
171 void GraphFrame::_bind_methods() {
172 ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphFrame::set_title);
173 ClassDB::bind_method(D_METHOD("get_title"), &GraphFrame::get_title);
174
175 ClassDB::bind_method(D_METHOD("get_titlebar_hbox"), &GraphFrame::get_titlebar_hbox);
176
177 ClassDB::bind_method(D_METHOD("set_autoshrink_enabled", "shrink"), &GraphFrame::set_autoshrink_enabled);
178 ClassDB::bind_method(D_METHOD("is_autoshrink_enabled"), &GraphFrame::is_autoshrink_enabled);
179
180 ClassDB::bind_method(D_METHOD("set_autoshrink_margin", "autoshrink_margin"), &GraphFrame::set_autoshrink_margin);
181 ClassDB::bind_method(D_METHOD("get_autoshrink_margin"), &GraphFrame::get_autoshrink_margin);
182
183 ClassDB::bind_method(D_METHOD("set_drag_margin", "drag_margin"), &GraphFrame::set_drag_margin);
184 ClassDB::bind_method(D_METHOD("get_drag_margin"), &GraphFrame::get_drag_margin);
185
186 ClassDB::bind_method(D_METHOD("set_tint_color_enabled", "enable"), &GraphFrame::set_tint_color_enabled);
187 ClassDB::bind_method(D_METHOD("is_tint_color_enabled"), &GraphFrame::is_tint_color_enabled);
188
189 ClassDB::bind_method(D_METHOD("set_tint_color", "color"), &GraphFrame::set_tint_color);
190 ClassDB::bind_method(D_METHOD("get_tint_color"), &GraphFrame::get_tint_color);
191
192 ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
193 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoshrink_enabled"), "set_autoshrink_enabled", "is_autoshrink_enabled");
194 ADD_PROPERTY(PropertyInfo(Variant::INT, "autoshrink_margin", PROPERTY_HINT_RANGE, "0,128,1"), "set_autoshrink_margin", "get_autoshrink_margin");
195 ADD_PROPERTY(PropertyInfo(Variant::INT, "drag_margin", PROPERTY_HINT_RANGE, "0,128,1"), "set_drag_margin", "get_drag_margin");
196
197 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tint_color_enabled"), "set_tint_color_enabled", "is_tint_color_enabled");
198 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_color"), "set_tint_color", "get_tint_color");
199
200 ADD_SIGNAL(MethodInfo(SNAME("autoshrink_changed")));
201
202 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphFrame, panel);
203 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphFrame, panel_selected);
204 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphFrame, titlebar);
205 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphFrame, titlebar_selected);
206
207 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphFrame, resizer);
208 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphFrame, resizer_color);
209 }
210
211 void GraphFrame::_validate_property(PropertyInfo &p_property) const {
212 if (!Engine::get_singleton()->is_editor_hint()) {
213 return;
214 }
215 if (p_property.name == "resizable") {
216 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
217 }
218 }
219
220 void GraphFrame::set_title(const String &p_title) {
221 if (title == p_title) {
222 return;
223 }
224 title = p_title;
225 if (title_label) {
226 title_label->set_text(title);
227 }
228 update_minimum_size();
229 }
230
231 String GraphFrame::get_title() const {
232 return title;
233 }
234
235 void GraphFrame::set_autoshrink_enabled(bool p_shrink) {
236 if (autoshrink_enabled == p_shrink) {
237 return;
238 }
239
240 autoshrink_enabled = p_shrink;
241
242 emit_signal("autoshrink_changed", get_size());
243 queue_redraw();
244 }
245
246 bool GraphFrame::is_autoshrink_enabled() const {
247 return autoshrink_enabled;
248 }
249
250 void GraphFrame::set_autoshrink_margin(const int &p_margin) {
251 if (autoshrink_margin == p_margin) {
252 return;
253 }
254
255 autoshrink_margin = p_margin;
256
257 emit_signal("autoshrink_changed", get_size());
258 }
259
260 int GraphFrame::get_autoshrink_margin() const {
261 return autoshrink_margin;
262 }
263
264 HBoxContainer *GraphFrame::get_titlebar_hbox() {
265 return titlebar_hbox;
266 }
267
268 Size2 GraphFrame::get_titlebar_size() const {
269 return titlebar_hbox->get_combined_minimum_size() + theme_cache.titlebar->get_minimum_size();
270 }
271
272 void GraphFrame::set_drag_margin(int p_margin) {
273 drag_margin = p_margin;
274 }
275
276 int GraphFrame::get_drag_margin() const {
277 return drag_margin;
278 }
279
280 void GraphFrame::set_tint_color_enabled(bool p_enable) {
281 tint_color_enabled = p_enable;
282 queue_redraw();
283 }
284
285 bool GraphFrame::is_tint_color_enabled() const {
286 return tint_color_enabled;
287 }
288
289 void GraphFrame::set_tint_color(const Color &p_color) {
290 tint_color = p_color;
291 queue_redraw();
292 }
293
294 Color GraphFrame::get_tint_color() const {
295 return tint_color;
296 }
297
298 bool GraphFrame::has_point(const Point2 &p_point) const {
299 Ref<StyleBox> sb_panel = theme_cache.panel;
300 Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
301 Ref<Texture2D> resizer = theme_cache.resizer;
302
303 if (Rect2(get_size() - resizer->get_size(), resizer->get_size()).has_point(p_point)) {
304 return true;
305 }
306
307 // For grabbing on the titlebar.
308 int titlebar_height = titlebar_hbox->get_combined_minimum_size().height + sb_titlebar->get_minimum_size().height;
309 if (Rect2(0, 0, get_size().width, titlebar_height).has_point(p_point)) {
310 return true;
311 }
312
313 // Allow grabbing on all sides of the frame.
314 Rect2 frame_rect = Rect2(0, 0, get_size().width, get_size().height);
315 Rect2 no_drag_rect = frame_rect.grow(-drag_margin);
316
317 if (frame_rect.has_point(p_point) && !no_drag_rect.has_point(p_point)) {
318 return true;
319 }
320
321 return false;
322 }
323
324 Size2 GraphFrame::get_minimum_size() const {
325 Ref<StyleBox> sb_panel = theme_cache.panel;
326 Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
327
328 Size2 minsize = titlebar_hbox->get_minimum_size() + sb_titlebar->get_minimum_size();
329
330 for (int i = 0; i < get_child_count(false); i++) {
331 Control *child = as_sortable_control(get_child(i, false));
332 if (!child) {
333 continue;
334 }
335
336 Size2i size = child->get_combined_minimum_size();
337 size.width += sb_panel->get_minimum_size().width;
338
339 minsize.x = MAX(minsize.x, size.x);
340 minsize.y += MAX(minsize.y, size.y);
341 }
342
343 minsize.height += sb_panel->get_minimum_size().height;
344
345 return minsize;
346 }
347
348 GraphFrame::GraphFrame() {
349 titlebar_hbox = memnew(HBoxContainer);
350 titlebar_hbox->set_h_size_flags(SIZE_EXPAND_FILL);
351 add_child(titlebar_hbox, false, INTERNAL_MODE_FRONT);
352
353 title_label = memnew(Label);
354 title_label->set_theme_type_variation("GraphFrameTitleLabel");
355 title_label->set_h_size_flags(SIZE_EXPAND_FILL);
356 title_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
357 titlebar_hbox->add_child(title_label);
358
359 set_mouse_filter(MOUSE_FILTER_STOP);
360 }