Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/graph_frame.cpp
9903 views
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_size() + sb_titlebar->get_minimum_size());
107
Size2 body_size = get_size();
108
body_size.y -= titlebar_rect.size.height;
109
Rect2 body_rect(Point2(0, titlebar_rect.size.height), body_size);
110
111
// Draw body stylebox.
112
if (tint_color_enabled) {
113
if (sb_panel_flat.is_valid()) {
114
Color original_border_color = sb_panel_flat->get_border_color();
115
sb_panel_flat = sb_panel_flat->duplicate();
116
sb_panel_flat->set_bg_color(tint_color);
117
sb_panel_flat->set_border_color(selected ? original_border_color : tint_color.lightened(0.3));
118
draw_style_box(sb_panel_flat, body_rect);
119
} else if (sb_panel_texture.is_valid()) {
120
sb_panel_texture = sb_panel_texture->duplicate();
121
sb_panel_texture->set_modulate(tint_color);
122
draw_style_box(sb_panel_texture, body_rect);
123
}
124
} else {
125
draw_style_box(sb_panel_flat, body_rect);
126
}
127
128
// Draw title bar stylebox above.
129
draw_style_box(sb_to_draw_titlebar, titlebar_rect);
130
131
// Only draw the resize handle if the frame is not auto-resizing.
132
if (resizable && !autoshrink_enabled) {
133
Ref<Texture2D> resizer = theme_cache.resizer;
134
Color resizer_color = theme_cache.resizer_color;
135
if (resizable) {
136
draw_texture(resizer, get_size() - resizer->get_size(), resizer_color);
137
}
138
}
139
140
} break;
141
}
142
}
143
144
void GraphFrame::_resort() {
145
Ref<StyleBox> sb_panel = theme_cache.panel;
146
Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
147
148
// Resort titlebar first.
149
Size2 titlebar_size = Size2(get_size().width, titlebar_hbox->get_size().height);
150
titlebar_size -= sb_titlebar->get_minimum_size();
151
Rect2 titlebar_rect = Rect2(sb_titlebar->get_offset(), titlebar_size);
152
153
fit_child_in_rect(titlebar_hbox, titlebar_rect);
154
155
// After resort the children of the titlebar container may have changed their height (e.g. Label autowrap).
156
Size2i titlebar_min_size = titlebar_hbox->get_combined_minimum_size();
157
158
Size2 size = get_size() - sb_panel->get_minimum_size() - Size2(0, titlebar_min_size.height + sb_titlebar->get_minimum_size().height);
159
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);
160
161
for (int i = 0; i < get_child_count(false); i++) {
162
Control *child = as_sortable_control(get_child(i, false));
163
if (!child) {
164
continue;
165
}
166
fit_child_in_rect(child, Rect2(offset, size));
167
}
168
}
169
170
void GraphFrame::_bind_methods() {
171
ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphFrame::set_title);
172
ClassDB::bind_method(D_METHOD("get_title"), &GraphFrame::get_title);
173
174
ClassDB::bind_method(D_METHOD("get_titlebar_hbox"), &GraphFrame::get_titlebar_hbox);
175
176
ClassDB::bind_method(D_METHOD("set_autoshrink_enabled", "shrink"), &GraphFrame::set_autoshrink_enabled);
177
ClassDB::bind_method(D_METHOD("is_autoshrink_enabled"), &GraphFrame::is_autoshrink_enabled);
178
179
ClassDB::bind_method(D_METHOD("set_autoshrink_margin", "autoshrink_margin"), &GraphFrame::set_autoshrink_margin);
180
ClassDB::bind_method(D_METHOD("get_autoshrink_margin"), &GraphFrame::get_autoshrink_margin);
181
182
ClassDB::bind_method(D_METHOD("set_drag_margin", "drag_margin"), &GraphFrame::set_drag_margin);
183
ClassDB::bind_method(D_METHOD("get_drag_margin"), &GraphFrame::get_drag_margin);
184
185
ClassDB::bind_method(D_METHOD("set_tint_color_enabled", "enable"), &GraphFrame::set_tint_color_enabled);
186
ClassDB::bind_method(D_METHOD("is_tint_color_enabled"), &GraphFrame::is_tint_color_enabled);
187
188
ClassDB::bind_method(D_METHOD("set_tint_color", "color"), &GraphFrame::set_tint_color);
189
ClassDB::bind_method(D_METHOD("get_tint_color"), &GraphFrame::get_tint_color);
190
191
ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
192
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoshrink_enabled"), "set_autoshrink_enabled", "is_autoshrink_enabled");
193
ADD_PROPERTY(PropertyInfo(Variant::INT, "autoshrink_margin", PROPERTY_HINT_RANGE, "0,128,1"), "set_autoshrink_margin", "get_autoshrink_margin");
194
ADD_PROPERTY(PropertyInfo(Variant::INT, "drag_margin", PROPERTY_HINT_RANGE, "0,128,1"), "set_drag_margin", "get_drag_margin");
195
196
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tint_color_enabled"), "set_tint_color_enabled", "is_tint_color_enabled");
197
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_color"), "set_tint_color", "get_tint_color");
198
199
ADD_SIGNAL(MethodInfo(SNAME("autoshrink_changed")));
200
201
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphFrame, panel);
202
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphFrame, panel_selected);
203
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphFrame, titlebar);
204
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphFrame, titlebar_selected);
205
206
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphFrame, resizer);
207
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphFrame, resizer_color);
208
}
209
210
void GraphFrame::_validate_property(PropertyInfo &p_property) const {
211
if (!Engine::get_singleton()->is_editor_hint()) {
212
return;
213
}
214
if (p_property.name == "resizable") {
215
p_property.usage = PROPERTY_USAGE_NO_EDITOR;
216
}
217
}
218
219
void GraphFrame::set_title(const String &p_title) {
220
if (title == p_title) {
221
return;
222
}
223
title = p_title;
224
if (title_label) {
225
title_label->set_text(title);
226
}
227
update_minimum_size();
228
}
229
230
String GraphFrame::get_title() const {
231
return title;
232
}
233
234
void GraphFrame::set_autoshrink_enabled(bool p_shrink) {
235
if (autoshrink_enabled == p_shrink) {
236
return;
237
}
238
239
autoshrink_enabled = p_shrink;
240
241
emit_signal("autoshrink_changed", get_size());
242
queue_redraw();
243
}
244
245
bool GraphFrame::is_autoshrink_enabled() const {
246
return autoshrink_enabled;
247
}
248
249
void GraphFrame::set_autoshrink_margin(const int &p_margin) {
250
if (autoshrink_margin == p_margin) {
251
return;
252
}
253
254
autoshrink_margin = p_margin;
255
256
emit_signal("autoshrink_changed", get_size());
257
}
258
259
int GraphFrame::get_autoshrink_margin() const {
260
return autoshrink_margin;
261
}
262
263
HBoxContainer *GraphFrame::get_titlebar_hbox() {
264
return titlebar_hbox;
265
}
266
267
Size2 GraphFrame::get_titlebar_size() const {
268
return titlebar_hbox->get_size() + theme_cache.titlebar->get_minimum_size();
269
}
270
271
void GraphFrame::set_drag_margin(int p_margin) {
272
drag_margin = p_margin;
273
}
274
275
int GraphFrame::get_drag_margin() const {
276
return drag_margin;
277
}
278
279
void GraphFrame::set_tint_color_enabled(bool p_enable) {
280
tint_color_enabled = p_enable;
281
queue_redraw();
282
}
283
284
bool GraphFrame::is_tint_color_enabled() const {
285
return tint_color_enabled;
286
}
287
288
void GraphFrame::set_tint_color(const Color &p_color) {
289
tint_color = p_color;
290
queue_redraw();
291
}
292
293
Color GraphFrame::get_tint_color() const {
294
return tint_color;
295
}
296
297
bool GraphFrame::has_point(const Point2 &p_point) const {
298
Ref<StyleBox> sb_panel = theme_cache.panel;
299
Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
300
Ref<Texture2D> resizer = theme_cache.resizer;
301
302
if (Rect2(get_size() - resizer->get_size(), resizer->get_size()).has_point(p_point)) {
303
return true;
304
}
305
306
// For grabbing on the titlebar.
307
int titlebar_height = titlebar_hbox->get_size().height + sb_titlebar->get_minimum_size().height;
308
if (Rect2(0, 0, get_size().width, titlebar_height).has_point(p_point)) {
309
return true;
310
}
311
312
// Allow grabbing on all sides of the frame.
313
Rect2 frame_rect = Rect2(0, 0, get_size().width, get_size().height);
314
Rect2 no_drag_rect = frame_rect.grow(-drag_margin);
315
316
if (frame_rect.has_point(p_point) && !no_drag_rect.has_point(p_point)) {
317
return true;
318
}
319
320
return false;
321
}
322
323
Size2 GraphFrame::get_minimum_size() const {
324
Ref<StyleBox> sb_panel = theme_cache.panel;
325
Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
326
327
Size2 minsize = titlebar_hbox->get_minimum_size() + sb_titlebar->get_minimum_size();
328
329
for (int i = 0; i < get_child_count(false); i++) {
330
Control *child = as_sortable_control(get_child(i, false));
331
if (!child) {
332
continue;
333
}
334
335
Size2i size = child->get_combined_minimum_size();
336
size.width += sb_panel->get_minimum_size().width;
337
338
minsize.x = MAX(minsize.x, size.x);
339
minsize.y += MAX(minsize.y, size.y);
340
}
341
342
minsize.height += sb_panel->get_minimum_size().height;
343
344
return minsize;
345
}
346
347
GraphFrame::GraphFrame() {
348
titlebar_hbox = memnew(HBoxContainer);
349
titlebar_hbox->set_h_size_flags(SIZE_EXPAND_FILL);
350
add_child(titlebar_hbox, false, INTERNAL_MODE_FRONT);
351
352
title_label = memnew(Label);
353
title_label->set_theme_type_variation("GraphFrameTitleLabel");
354
title_label->set_h_size_flags(SIZE_EXPAND_FILL);
355
title_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
356
titlebar_hbox->add_child(title_label);
357
358
set_mouse_filter(MOUSE_FILTER_STOP);
359
}
360
361