2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* popup.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
2020-01-01 18:16:22 +08:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 09:10:30 +08: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 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "popup.h"
|
2017-08-19 07:02:56 +08:00
|
|
|
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/engine.h"
|
|
|
|
#include "core/os/keyboard.h"
|
2020-03-12 20:37:40 +08:00
|
|
|
#include "scene/gui/panel.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
void Popup::_input_from_window(const Ref<InputEvent> &p_event) {
|
|
|
|
Ref<InputEventKey> key = p_event;
|
|
|
|
if (key.is_valid() && key->is_pressed() && key->get_keycode() == KEY_ESCAPE) {
|
|
|
|
_close_pressed();
|
2015-12-29 20:26:17 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
void Popup::_parent_focused() {
|
2020-03-20 10:32:09 +08:00
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
_close_pressed();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2020-03-12 20:37:40 +08:00
|
|
|
void Popup::_notification(int p_what) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
if (is_visible()) {
|
|
|
|
|
|
|
|
parent_visible = get_parent_visible_window();
|
|
|
|
if (parent_visible) {
|
|
|
|
parent_visible->connect("focus_entered", callable_mp(this, &Popup::_parent_focused));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (parent_visible) {
|
|
|
|
parent_visible->disconnect("focus_entered", callable_mp(this, &Popup::_parent_focused));
|
|
|
|
parent_visible = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit_signal("popup_hide");
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
if (parent_visible) {
|
|
|
|
parent_visible->disconnect("focus_entered", callable_mp(this, &Popup::_parent_focused));
|
|
|
|
parent_visible = nullptr;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_WM_CLOSE_REQUEST: {
|
|
|
|
_close_pressed();
|
2020-03-20 10:32:09 +08:00
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
} break;
|
2015-06-14 09:12:53 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
void Popup::_close_pressed() {
|
2019-06-25 08:37:32 +08:00
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
Window *parent_window = parent_visible;
|
|
|
|
if (parent_visible) {
|
|
|
|
parent_visible->disconnect("focus_entered", callable_mp(this, &Popup::_parent_focused));
|
|
|
|
parent_visible = nullptr;
|
2017-03-03 05:43:56 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
call_deferred("hide");
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
emit_signal("cancelled");
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
if (parent_window) {
|
|
|
|
//parent_window->grab_focus();
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
void Popup::set_as_minsize() {
|
|
|
|
set_size(get_contents_minimum_size());
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
void Popup::_bind_methods() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ADD_SIGNAL(MethodInfo("popup_hide"));
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2020-03-20 10:32:09 +08: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
Popup::Popup() {
|
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
parent_visible = nullptr;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
set_wrap_controls(true);
|
|
|
|
set_visible(false);
|
|
|
|
set_transient(true);
|
|
|
|
set_flag(FLAG_BORDERLESS, true);
|
|
|
|
set_flag(FLAG_RESIZE_DISABLED, true);
|
2016-05-18 05:27:15 +08:00
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
connect("window_input", callable_mp(this, &Popup::_input_from_window));
|
2016-05-18 05:27:15 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Popup::~Popup() {
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
Size2 PopupPanel::_get_contents_minimum_size() const {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-03-21 04:51:53 +08:00
|
|
|
Ref<StyleBox> p = get_theme_stylebox("panel", get_class_name());
|
2018-09-08 00:49:10 +08:00
|
|
|
|
|
|
|
Size2 ms;
|
|
|
|
|
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
2020-03-12 20:37:40 +08:00
|
|
|
if (!c || c == panel)
|
2018-09-08 00:49:10 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (c->is_set_as_toplevel())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Size2 cms = c->get_combined_minimum_size();
|
|
|
|
ms.x = MAX(cms.x, ms.x);
|
|
|
|
ms.y = MAX(cms.y, ms.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ms + p->get_minimum_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopupPanel::_update_child_rects() {
|
|
|
|
|
2020-03-21 04:51:53 +08:00
|
|
|
Ref<StyleBox> p = get_theme_stylebox("panel", get_class_name());
|
2018-09-08 00:49:10 +08:00
|
|
|
|
|
|
|
Vector2 cpos(p->get_offset());
|
|
|
|
Vector2 csize(get_size() - p->get_minimum_size());
|
|
|
|
|
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (c->is_set_as_toplevel())
|
|
|
|
continue;
|
|
|
|
|
2020-03-12 20:37:40 +08:00
|
|
|
if (c == panel) {
|
|
|
|
c->set_position(Vector2());
|
|
|
|
c->set_size(get_size());
|
|
|
|
} else {
|
|
|
|
c->set_position(cpos);
|
|
|
|
c->set_size(csize);
|
|
|
|
}
|
2018-09-08 00:49:10 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PopupPanel::_notification(int p_what) {
|
|
|
|
|
2020-03-20 10:32:09 +08:00
|
|
|
if (p_what == NOTIFICATION_THEME_CHANGED) {
|
2020-03-21 04:51:53 +08:00
|
|
|
panel->add_theme_style_override("panel", get_theme_stylebox("panel", get_class_name()));
|
2020-03-20 10:32:09 +08:00
|
|
|
} else if (p_what == NOTIFICATION_READY || p_what == NOTIFICATION_ENTER_TREE) {
|
2018-09-08 00:49:10 +08:00
|
|
|
|
2020-03-21 04:51:53 +08:00
|
|
|
panel->add_theme_style_override("panel", get_theme_stylebox("panel", get_class_name()));
|
2018-09-08 00:49:10 +08:00
|
|
|
_update_child_rects();
|
2020-03-12 20:37:40 +08:00
|
|
|
} else if (p_what == NOTIFICATION_WM_SIZE_CHANGED) {
|
2018-09-08 00:49:10 +08:00
|
|
|
|
|
|
|
_update_child_rects();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PopupPanel::PopupPanel() {
|
2020-03-12 20:37:40 +08:00
|
|
|
|
|
|
|
panel = memnew(Panel);
|
|
|
|
add_child(panel);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|