2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* popup.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "popup.h"
|
2017-08-19 01:02:56 +02:00
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/config/engine.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/keyboard.h"
|
2020-03-12 13:37:40 +01:00
|
|
|
#include "scene/gui/panel.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-12 13:37:40 +01:00
|
|
|
void Popup::_input_from_window(const Ref<InputEvent> &p_event) {
|
|
|
|
Ref<InputEventKey> key = p_event;
|
2021-08-13 23:31:57 +02:00
|
|
|
if (key.is_valid() && key->is_pressed() && key->get_keycode() == Key::ESCAPE) {
|
2020-03-12 13:37:40 +01:00
|
|
|
_close_pressed();
|
2015-12-29 13:26:17 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-08-21 16:11:20 +02:00
|
|
|
void Popup::_initialize_visible_parents() {
|
2022-02-24 10:21:23 +01:00
|
|
|
if (is_embedded()) {
|
|
|
|
visible_parents.clear();
|
|
|
|
|
|
|
|
Window *parent_window = this;
|
|
|
|
while (parent_window) {
|
|
|
|
parent_window = parent_window->get_parent_visible_window();
|
|
|
|
if (parent_window) {
|
|
|
|
visible_parents.push_back(parent_window);
|
|
|
|
parent_window->connect("focus_entered", callable_mp(this, &Popup::_parent_focused));
|
|
|
|
parent_window->connect("tree_exited", callable_mp(this, &Popup::_deinitialize_visible_parents));
|
|
|
|
}
|
2020-08-21 16:11:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Popup::_deinitialize_visible_parents() {
|
2022-02-24 10:21:23 +01:00
|
|
|
if (is_embedded()) {
|
|
|
|
for (uint32_t i = 0; i < visible_parents.size(); ++i) {
|
|
|
|
visible_parents[i]->disconnect("focus_entered", callable_mp(this, &Popup::_parent_focused));
|
|
|
|
visible_parents[i]->disconnect("tree_exited", callable_mp(this, &Popup::_deinitialize_visible_parents));
|
|
|
|
}
|
2020-08-21 16:11:20 +02:00
|
|
|
|
2022-02-24 10:21:23 +01:00
|
|
|
visible_parents.clear();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2022-09-01 12:38:08 +02:00
|
|
|
void Popup::_update_theme_item_cache() {
|
|
|
|
Window::_update_theme_item_cache();
|
|
|
|
|
|
|
|
theme_cache.panel_style = get_theme_stylebox(SNAME("panel"));
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:37:40 +01:00
|
|
|
void Popup::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
if (is_visible()) {
|
2020-08-21 16:11:20 +02:00
|
|
|
_initialize_visible_parents();
|
2020-03-12 13:37:40 +01:00
|
|
|
} else {
|
2020-08-21 16:11:20 +02:00
|
|
|
_deinitialize_visible_parents();
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("popup_hide"));
|
2021-07-21 21:03:03 +02:00
|
|
|
popped_up = false;
|
2020-03-12 13:37:40 +01:00
|
|
|
}
|
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2020-08-21 16:11:20 +02:00
|
|
|
case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
|
|
|
|
if (has_focus()) {
|
|
|
|
popped_up = true;
|
2020-03-12 13:37:40 +01:00
|
|
|
}
|
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2020-08-21 16:11:20 +02:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
_deinitialize_visible_parents();
|
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
|
|
|
case NOTIFICATION_WM_CLOSE_REQUEST:
|
2020-08-21 16:11:20 +02:00
|
|
|
case NOTIFICATION_APPLICATION_FOCUS_OUT: {
|
|
|
|
_close_pressed();
|
2020-03-12 13:37:40 +01:00
|
|
|
} break;
|
2015-06-14 03:12:53 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-08-21 16:11:20 +02:00
|
|
|
void Popup::_parent_focused() {
|
2022-02-24 10:21:23 +01:00
|
|
|
if (popped_up && get_flag(FLAG_POPUP)) {
|
2020-08-21 16:11:20 +02:00
|
|
|
_close_pressed();
|
2017-03-02 22:43:56 +01:00
|
|
|
}
|
2020-08-21 16:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Popup::_close_pressed() {
|
|
|
|
popped_up = false;
|
|
|
|
|
|
|
|
_deinitialize_visible_parents();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-06-09 10:59:01 +02:00
|
|
|
call_deferred(SNAME("hide"));
|
2022-05-13 14:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Popup::_post_popup() {
|
|
|
|
Window::_post_popup();
|
|
|
|
popped_up = true;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void Popup::_bind_methods() {
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("popup_hide"));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-20 03:32:09 +01:00
|
|
|
Rect2i Popup::_popup_adjust_rect() const {
|
|
|
|
ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
|
|
|
|
Rect2i parent = get_usable_parent_rect();
|
|
|
|
|
|
|
|
if (parent == Rect2i()) {
|
|
|
|
return Rect2i();
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect2i current(get_position(), get_size());
|
|
|
|
|
|
|
|
if (current.position.x + current.size.x > parent.position.x + parent.size.x) {
|
|
|
|
current.position.x = parent.position.x + parent.size.x - current.size.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current.position.x < parent.position.x) {
|
|
|
|
current.position.x = parent.position.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current.position.y + current.size.y > parent.position.y + parent.size.y) {
|
|
|
|
current.position.y = parent.position.y + parent.size.y - current.size.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current.position.y < parent.position.y) {
|
|
|
|
current.position.y = parent.position.y;
|
|
|
|
}
|
|
|
|
|
2020-08-29 14:05:59 +02:00
|
|
|
if (current.size.y > parent.size.y) {
|
|
|
|
current.size.y = parent.size.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current.size.x > parent.size.x) {
|
|
|
|
current.size.x = parent.size.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Early out if max size not set.
|
|
|
|
Size2i max_size = get_max_size();
|
|
|
|
if (max_size <= Size2()) {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current.size.x > max_size.x) {
|
|
|
|
current.size.x = max_size.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current.size.y > max_size.y) {
|
|
|
|
current.size.y = max_size.y;
|
|
|
|
}
|
|
|
|
|
2020-03-20 03:32:09 +01:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Popup::Popup() {
|
2020-03-12 13:37:40 +01:00
|
|
|
set_wrap_controls(true);
|
|
|
|
set_visible(false);
|
|
|
|
set_transient(true);
|
|
|
|
set_flag(FLAG_BORDERLESS, true);
|
|
|
|
set_flag(FLAG_RESIZE_DISABLED, true);
|
2022-02-24 10:21:23 +01:00
|
|
|
set_flag(FLAG_POPUP, true);
|
2016-05-17 23:27:15 +02:00
|
|
|
|
2020-03-12 13:37:40 +01:00
|
|
|
connect("window_input", callable_mp(this, &Popup::_input_from_window));
|
2016-05-17 23:27:15 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Popup::~Popup() {
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:37:40 +01:00
|
|
|
Size2 PopupPanel::_get_contents_minimum_size() const {
|
2018-09-07 18:49:10 +02:00
|
|
|
Size2 ms;
|
|
|
|
|
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!c || c == panel) {
|
2018-09-07 18:49:10 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-07 18:49:10 +02:00
|
|
|
|
2020-10-01 09:17:33 +02:00
|
|
|
if (c->is_set_as_top_level()) {
|
2018-09-07 18:49:10 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-07 18:49:10 +02:00
|
|
|
|
|
|
|
Size2 cms = c->get_combined_minimum_size();
|
|
|
|
ms.x = MAX(cms.x, ms.x);
|
|
|
|
ms.y = MAX(cms.y, ms.y);
|
|
|
|
}
|
|
|
|
|
2022-09-01 12:38:08 +02:00
|
|
|
return ms + theme_cache.panel_style->get_minimum_size();
|
2018-09-07 18:49:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PopupPanel::_update_child_rects() {
|
2022-09-01 12:38:08 +02:00
|
|
|
Vector2 cpos(theme_cache.panel_style->get_offset());
|
|
|
|
Vector2 csize(get_size() - theme_cache.panel_style->get_minimum_size());
|
2018-09-07 18:49:10 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!c) {
|
2018-09-07 18:49:10 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-07 18:49:10 +02:00
|
|
|
|
2020-10-01 09:17:33 +02:00
|
|
|
if (c->is_set_as_top_level()) {
|
2018-09-07 18:49:10 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-09-07 18:49:10 +02:00
|
|
|
|
2020-03-12 13:37:40 +01:00
|
|
|
if (c == panel) {
|
|
|
|
c->set_position(Vector2());
|
|
|
|
c->set_size(get_size());
|
|
|
|
} else {
|
|
|
|
c->set_position(cpos);
|
|
|
|
c->set_size(csize);
|
|
|
|
}
|
2018-09-07 18:49:10 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-09-01 12:38:08 +02:00
|
|
|
void PopupPanel::_update_theme_item_cache() {
|
|
|
|
Popup::_update_theme_item_cache();
|
|
|
|
|
|
|
|
theme_cache.panel_style = get_theme_stylebox(SNAME("panel"));
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void PopupPanel::_notification(int p_what) {
|
2022-02-15 18:06:48 +01:00
|
|
|
switch (p_what) {
|
2022-09-01 12:38:08 +02:00
|
|
|
case NOTIFICATION_READY:
|
2022-02-15 18:06:48 +01:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2022-09-01 12:38:08 +02:00
|
|
|
panel->add_theme_style_override("panel", theme_cache.panel_style);
|
2022-02-15 18:06:48 +01:00
|
|
|
_update_child_rects();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_WM_SIZE_CHANGED: {
|
|
|
|
_update_child_rects();
|
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PopupPanel::PopupPanel() {
|
2020-03-12 13:37:40 +01:00
|
|
|
panel = memnew(Panel);
|
2021-08-25 15:49:30 +02:00
|
|
|
add_child(panel, false, INTERNAL_MODE_FRONT);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|