Debian Codegraph @

scene/gui/popup.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* popup.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 "popup.h"
32
33 #ifdef TOOLS_ENABLED
34 #include "core/config/project_settings.h"
35 #endif
36 #include "scene/gui/panel.h"
37 #include "scene/resources/style_box_flat.h"
38 #include "scene/theme/theme_db.h"
39
40 void Popup::_input_from_window(const Ref<InputEvent> &p_event) {
41 if (get_flag(FLAG_POPUP) && p_event->is_action_pressed(SNAME("ui_cancel"), false, true)) {
42 hide_reason = HIDE_REASON_CANCELED; // ESC pressed, mark as canceled unconditionally.
43 _close_pressed();
44 }
45 Window::_input_from_window(p_event);
46 }
47
48 void Popup::_initialize_visible_parents() {
49 if (is_embedded()) {
50 visible_parents.clear();
51
52 Window *parent_window = this;
53 while (parent_window) {
54 parent_window = parent_window->get_parent_visible_window();
55 if (parent_window) {
56 visible_parents.push_back(parent_window);
57 parent_window->connect(SceneStringName(focus_entered), callable_mp(this, &Popup::_parent_focused));
58 parent_window->connect(SceneStringName(tree_exited), callable_mp(this, &Popup::_deinitialize_visible_parents));
59 }
60 }
61 }
62 }
63
64 void Popup::_deinitialize_visible_parents() {
65 if (is_embedded()) {
66 for (Window *parent_window : visible_parents) {
67 parent_window->disconnect(SceneStringName(focus_entered), callable_mp(this, &Popup::_parent_focused));
68 parent_window->disconnect(SceneStringName(tree_exited), callable_mp(this, &Popup::_deinitialize_visible_parents));
69 }
70
71 visible_parents.clear();
72 }
73 }
74
75 void Popup::_notification(int p_what) {
76 switch (p_what) {
77 case NOTIFICATION_VISIBILITY_CHANGED: {
78 if (!is_in_edited_scene_root()) {
79 if (is_visible()) {
80 _initialize_visible_parents();
81 popped_up = true;
82 hide_reason = HIDE_REASON_NONE;
83 } else {
84 _deinitialize_visible_parents();
85 if (hide_reason == HIDE_REASON_NONE) {
86 hide_reason = HIDE_REASON_CANCELED;
87 }
88 emit_signal(SNAME("popup_hide"));
89 popped_up = false;
90 }
91 }
92 } break;
93
94 case NOTIFICATION_UNPARENTED:
95 case NOTIFICATION_EXIT_TREE: {
96 if (!is_in_edited_scene_root()) {
97 _deinitialize_visible_parents();
98 }
99 } break;
100
101 case NOTIFICATION_WM_CLOSE_REQUEST: {
102 if (!is_in_edited_scene_root()) {
103 if (hide_reason == HIDE_REASON_NONE) {
104 hide_reason = HIDE_REASON_UNFOCUSED;
105 }
106 _close_pressed();
107 }
108 } break;
109
110 case NOTIFICATION_APPLICATION_FOCUS_OUT: {
111 if (!is_in_edited_scene_root() && get_flag(FLAG_POPUP)) {
112 if (hide_reason == HIDE_REASON_NONE) {
113 hide_reason = HIDE_REASON_UNFOCUSED;
114 }
115 _close_pressed();
116 }
117 } break;
118 }
119 }
120
121 void Popup::_parent_focused() {
122 if (popped_up && get_flag(FLAG_POPUP)) {
123 if (hide_reason == HIDE_REASON_NONE) {
124 hide_reason = HIDE_REASON_UNFOCUSED;
125 }
126 _close_pressed();
127 }
128 }
129
130 void Popup::_close_pressed() {
131 popped_up = false;
132
133 _deinitialize_visible_parents();
134
135 callable_mp((Window *)this, &Window::hide).call_deferred();
136 }
137
138 void Popup::_post_popup() {
139 Window::_post_popup();
140 popped_up = true;
141 }
142
143 void Popup::_validate_property(PropertyInfo &p_property) const {
144 if (!Engine::get_singleton()->is_editor_hint()) {
145 return;
146 }
147 if (
148 p_property.name == "transient" ||
149 p_property.name == "exclusive" ||
150 p_property.name == "popup_window" ||
151 p_property.name == "unfocusable") {
152 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
153 }
154 }
155
156 Rect2i Popup::_popup_adjust_rect() const {
157 ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
158 Rect2i parent_rect = get_usable_parent_rect();
159
160 if (parent_rect == Rect2i()) {
161 return Rect2i();
162 }
163
164 Rect2i current(get_position(), get_size());
165
166 if (!is_embedded() && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS)) {
167 // We're fine as is, the Display Server will take care of that for us.
168 return current;
169 }
170
171 if (current.position.x + current.size.x > parent_rect.position.x + parent_rect.size.x) {
172 current.position.x = parent_rect.position.x + parent_rect.size.x - current.size.x;
173 }
174
175 if (current.position.x < parent_rect.position.x) {
176 current.position.x = parent_rect.position.x;
177 }
178
179 if (current.position.y + current.size.y > parent_rect.position.y + parent_rect.size.y) {
180 current.position.y = parent_rect.position.y + parent_rect.size.y - current.size.y;
181 }
182
183 if (current.position.y < parent_rect.position.y) {
184 current.position.y = parent_rect.position.y;
185 }
186
187 if (current.size.y > parent_rect.size.y) {
188 current.size.y = parent_rect.size.y;
189 }
190
191 if (current.size.x > parent_rect.size.x) {
192 current.size.x = parent_rect.size.x;
193 }
194
195 // Early out if max size not set.
196 Size2i popup_max_size = get_max_size();
197 if (popup_max_size <= Size2()) {
198 return current;
199 }
200
201 if (current.size.x > popup_max_size.x) {
202 current.size.x = popup_max_size.x;
203 }
204
205 if (current.size.y > popup_max_size.y) {
206 current.size.y = popup_max_size.y;
207 }
208
209 return current;
210 }
211
212 void Popup::_bind_methods() {
213 ADD_SIGNAL(MethodInfo("popup_hide"));
214 }
215
216 Popup::Popup() {
217 set_wrap_controls(true);
218 set_visible(false);
219 set_transient(true);
220 set_flag(FLAG_BORDERLESS, true);
221 set_flag(FLAG_RESIZE_DISABLED, true);
222 set_flag(FLAG_MINIMIZE_DISABLED, true);
223 set_flag(FLAG_MAXIMIZE_DISABLED, true);
224 set_flag(FLAG_POPUP, true);
225 set_flag(FLAG_POPUP_WM_HINT, true);
226 }
227
228 Popup::~Popup() {
229 }
230
231 #ifdef TOOLS_ENABLED
232 PackedStringArray PopupPanel::get_configuration_warnings() const {
233 PackedStringArray warnings = Popup::get_configuration_warnings();
234
235 if (!DisplayServer::get_singleton()->is_window_transparency_available() && !GLOBAL_GET_CACHED(bool, "display/window/subwindows/embed_subwindows")) {
236 Ref<StyleBoxFlat> sb = theme_cache.panel_style;
237 if (sb.is_valid() && (sb->get_shadow_size() > 0 || sb->get_corner_radius(CORNER_TOP_LEFT) > 0 || sb->get_corner_radius(CORNER_TOP_RIGHT) > 0 || sb->get_corner_radius(CORNER_BOTTOM_LEFT) > 0 || sb->get_corner_radius(CORNER_BOTTOM_RIGHT) > 0)) {
238 warnings.push_back(RTR("The current theme style has shadows and/or rounded corners for popups, but those won't display correctly if \"display/window/per_pixel_transparency/allowed\" isn't enabled in the Project Settings, nor if it isn't supported."));
239 }
240 }
241
242 return warnings;
243 }
244 #endif
245
246 void PopupPanel::_input_from_window(const Ref<InputEvent> &p_event) {
247 if (p_event.is_valid()) {
248 if (!get_flag(FLAG_POPUP)) {
249 return;
250 }
251
252 Ref<InputEventMouseButton> b = p_event;
253 // Hide it if the shadows have been clicked.
254 if (b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
255 Rect2 panel_area = panel->get_global_rect();
256 float win_scale = get_content_scale_factor();
257 panel_area.position *= win_scale;
258 panel_area.size *= win_scale;
259 if (!panel_area.has_point(b->get_position())) {
260 _close_pressed();
261 }
262 }
263 } else {
264 WARN_PRINT_ONCE("PopupPanel has received an invalid InputEvent. Consider filtering out invalid events.");
265 }
266
267 Popup::_input_from_window(p_event);
268 }
269
270 Size2 PopupPanel::_get_contents_minimum_size() const {
271 Size2 ms;
272
273 for (int i = 0; i < get_child_count(); i++) {
274 Control *c = Object::cast_to<Control>(get_child(i));
275 if (!c || c == panel) {
276 continue;
277 }
278
279 if (c->is_set_as_top_level()) {
280 continue;
281 }
282
283 Size2 cms = c->get_combined_minimum_size();
284 ms = cms.max(ms);
285 }
286
287 // Take shadows into account.
288 ms.width += panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT);
289 ms.height += panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM);
290
291 return ms + theme_cache.panel_style->get_minimum_size();
292 }
293
294 Rect2i PopupPanel::_popup_adjust_rect() const {
295 Rect2i current = Popup::_popup_adjust_rect();
296 if (current == Rect2i()) {
297 return current;
298 }
299
300 pre_popup_rect = current;
301
302 _update_shadow_offsets();
303 _update_child_rects();
304
305 if (is_layout_rtl()) {
306 current.position -= Vector2(-panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor();
307 } else {
308 current.position -= Vector2(panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor();
309 }
310 current.size += Vector2(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM)) * get_content_scale_factor();
311
312 return current;
313 }
314
315 void PopupPanel::_update_shadow_offsets() const {
316 if (!DisplayServer::get_singleton()->is_window_transparency_available() && !is_embedded()) {
317 panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
318 return;
319 }
320
321 const Ref<StyleBoxFlat> sb = theme_cache.panel_style;
322 if (sb.is_null()) {
323 panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
324 return;
325 }
326
327 const int shadow_size = sb->get_shadow_size();
328 if (shadow_size == 0) {
329 panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
330 return;
331 }
332
333 // Offset the background panel so it leaves space inside the window for the shadows to be drawn.
334 const Point2 shadow_offset = sb->get_shadow_offset();
335 if (is_layout_rtl()) {
336 panel->set_offset(SIDE_LEFT, MAX(0, shadow_size + shadow_offset.x));
337 panel->set_offset(SIDE_RIGHT, MIN(0, -shadow_size + shadow_offset.x));
338 } else {
339 panel->set_offset(SIDE_LEFT, MAX(0, shadow_size - shadow_offset.x));
340 panel->set_offset(SIDE_RIGHT, MIN(0, -shadow_size - shadow_offset.x));
341 }
342 panel->set_offset(SIDE_TOP, MAX(0, shadow_size - shadow_offset.y));
343 panel->set_offset(SIDE_BOTTOM, MIN(0, -shadow_size - shadow_offset.y));
344 }
345
346 void PopupPanel::_update_child_rects() const {
347 Vector2 cpos(theme_cache.panel_style->get_offset());
348 cpos += Vector2(is_layout_rtl() ? -panel->get_offset(SIDE_RIGHT) : panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP));
349
350 Vector2 csize = Vector2(get_size()) / get_content_scale_factor() - theme_cache.panel_style->get_minimum_size();
351 // Trim shadows.
352 csize.width -= panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT);
353 csize.height -= panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM);
354
355 for (int i = 0; i < get_child_count(); i++) {
356 Control *c = Object::cast_to<Control>(get_child(i));
357 if (!c || c == panel) {
358 continue;
359 }
360
361 if (c->is_set_as_top_level()) {
362 continue;
363 }
364
365 c->set_position(cpos);
366 c->set_size(csize);
367 }
368 }
369
370 void PopupPanel::_notification(int p_what) {
371 switch (p_what) {
372 case NOTIFICATION_THEME_CHANGED: {
373 panel->add_theme_style_override(SceneStringName(panel), theme_cache.panel_style);
374
375 if (is_visible()) {
376 _update_shadow_offsets();
377 }
378
379 _update_child_rects();
380
381 #ifdef TOOLS_ENABLED
382 update_configuration_warnings();
383 #endif
384 } break;
385
386 case Control::NOTIFICATION_TRANSLATION_CHANGED:
387 case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
388 if (is_visible()) {
389 _update_shadow_offsets();
390 }
391 } break;
392
393 case NOTIFICATION_VISIBILITY_CHANGED: {
394 if (!is_visible()) {
395 // Remove the extra space used by the shadows, so they can be ignored when the popup is hidden.
396 panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
397 _update_child_rects();
398
399 if (pre_popup_rect != Rect2i()) {
400 set_position(pre_popup_rect.position);
401 set_size(pre_popup_rect.size);
402
403 pre_popup_rect = Rect2i();
404 }
405 } else if (pre_popup_rect == Rect2i()) {
406 // The popup was made visible directly (without `popup_*()`), so just update the offsets without touching the rect.
407 _update_shadow_offsets();
408 _update_child_rects();
409 }
410 } break;
411
412 case NOTIFICATION_WM_SIZE_CHANGED: {
413 _update_child_rects();
414
415 if (is_visible()) {
416 const Vector2i offsets = Vector2i(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM));
417 // Check if the size actually changed.
418 if (pre_popup_rect.size + offsets != get_size()) {
419 // Play safe, and stick with the new size.
420 pre_popup_rect = Rect2i();
421 }
422 }
423 } break;
424 }
425 }
426
427 void PopupPanel::_bind_methods() {
428 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupPanel, panel_style, "panel");
429 }
430
431 PopupPanel::PopupPanel() {
432 set_flag(FLAG_TRANSPARENT, true);
433
434 panel = memnew(Panel);
435 panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
436 add_child(panel, false, INTERNAL_MODE_FRONT);
437
438 #ifdef TOOLS_ENABLED
439 ProjectSettings::get_singleton()->connect("settings_changed", callable_mp((Node *)this, &Node::update_configuration_warnings));
440 #endif
441 }