123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- #include "popup.h"
- #include "core/engine.h"
- #include "core/os/keyboard.h"
- void Popup::_gui_input(Ref<InputEvent> p_event) {
- }
- void Popup::_notification(int p_what) {
- if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
- if (popped_up && !is_visible_in_tree()) {
- popped_up = false;
- notification(NOTIFICATION_POPUP_HIDE);
- emit_signal("popup_hide");
- }
- update_configuration_warning();
- }
- if (p_what == NOTIFICATION_ENTER_TREE) {
- #ifdef TOOLS_ENABLED
- if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_a_parent_of(this)) {
- set_as_toplevel(false);
- }
- #endif
- }
- }
- void Popup::_fix_size() {
- Point2 pos = get_global_position();
- Size2 size = get_size();
- Point2 window_size = get_viewport_rect().size;
- if (pos.x + size.width > window_size.width)
- pos.x = window_size.width - size.width;
- if (pos.x < 0)
- pos.x = 0;
- if (pos.y + size.height > window_size.height)
- pos.y = window_size.height - size.height;
- if (pos.y < 0)
- pos.y = 0;
- if (pos != get_position())
- set_global_position(pos);
- }
- void Popup::set_as_minsize() {
- Size2 total_minsize;
- for (int i = 0; i < get_child_count(); i++) {
- Control *c = Object::cast_to<Control>(get_child(i));
- if (!c)
- continue;
- if (!c->is_visible())
- continue;
- Size2 minsize = c->get_combined_minimum_size();
- for (int j = 0; j < 2; j++) {
- Margin m_beg = Margin(0 + j);
- Margin m_end = Margin(2 + j);
- float margin_begin = c->get_margin(m_beg);
- float margin_end = c->get_margin(m_end);
- float anchor_begin = c->get_anchor(m_beg);
- float anchor_end = c->get_anchor(m_end);
- minsize[j] += margin_begin * (ANCHOR_END - anchor_begin) + margin_end * anchor_end;
- }
- total_minsize.width = MAX(total_minsize.width, minsize.width);
- total_minsize.height = MAX(total_minsize.height, minsize.height);
- }
- set_size(total_minsize);
- }
- void Popup::popup_centered_minsize(const Size2 &p_minsize) {
- set_custom_minimum_size(p_minsize);
- _fix_size();
- popup_centered();
- }
- void Popup::popup_centered(const Size2 &p_size) {
- Point2 window_size = get_viewport_rect().size;
- emit_signal("about_to_show");
- Rect2 rect;
- rect.size = p_size == Size2() ? get_size() : p_size;
- rect.position = ((window_size - rect.size) / 2.0).floor();
- set_position(rect.position);
- set_size(rect.size);
- show_modal(exclusive);
- _fix_size();
- Control *focusable = find_next_valid_focus();
- if (focusable)
- focusable->grab_focus();
- _post_popup();
- notification(NOTIFICATION_POST_POPUP);
- popped_up = true;
- }
- void Popup::popup_centered_ratio(float p_screen_ratio) {
- emit_signal("about_to_show");
- Rect2 rect;
- Point2 window_size = get_viewport_rect().size;
- rect.size = (window_size * p_screen_ratio).floor();
- rect.position = ((window_size - rect.size) / 2.0).floor();
- set_position(rect.position);
- set_size(rect.size);
- show_modal(exclusive);
- _fix_size();
- Control *focusable = find_next_valid_focus();
- if (focusable)
- focusable->grab_focus();
- _post_popup();
- notification(NOTIFICATION_POST_POPUP);
- popped_up = true;
- }
- void Popup::popup(const Rect2 &p_bounds) {
- emit_signal("about_to_show");
- show_modal(exclusive);
-
- if (!p_bounds.has_no_area()) {
- set_position(p_bounds.position);
- set_size(p_bounds.size);
- }
- _fix_size();
- Control *focusable = find_next_valid_focus();
- if (focusable)
- focusable->grab_focus();
- _post_popup();
- notification(NOTIFICATION_POST_POPUP);
- popped_up = true;
- }
- void Popup::set_exclusive(bool p_exclusive) {
- exclusive = p_exclusive;
- }
- bool Popup::is_exclusive() const {
- return exclusive;
- }
- void Popup::_bind_methods() {
- ClassDB::bind_method(D_METHOD("popup_centered", "size"), &Popup::popup_centered, DEFVAL(Size2()));
- ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Popup::popup_centered_ratio, DEFVAL(0.75));
- ClassDB::bind_method(D_METHOD("popup_centered_minsize", "minsize"), &Popup::popup_centered_minsize, DEFVAL(Size2()));
- ClassDB::bind_method(D_METHOD("popup", "bounds"), &Popup::popup, DEFVAL(Rect2()));
- ClassDB::bind_method(D_METHOD("set_exclusive", "enable"), &Popup::set_exclusive);
- ClassDB::bind_method(D_METHOD("is_exclusive"), &Popup::is_exclusive);
- ADD_SIGNAL(MethodInfo("about_to_show"));
- ADD_SIGNAL(MethodInfo("popup_hide"));
- ADD_GROUP("Popup", "popup_");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "popup_exclusive"), "set_exclusive", "is_exclusive");
- BIND_CONSTANT(NOTIFICATION_POST_POPUP);
- BIND_CONSTANT(NOTIFICATION_POPUP_HIDE);
- }
- Popup::Popup() {
- set_as_toplevel(true);
- exclusive = false;
- popped_up = false;
- hide();
- }
- String Popup::get_configuration_warning() const {
- if (is_visible_in_tree()) {
- return TTR("Popups will hide by default unless you call popup() or any of the popup*() functions. Making them visible for editing is fine though, but they will hide upon running.");
- }
- return String();
- }
- Popup::~Popup() {
- }
- Size2 PopupPanel::get_minimum_size() const {
- Ref<StyleBox> p = get_stylebox("panel");
- Size2 ms;
- 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;
- 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() {
- Ref<StyleBox> p = get_stylebox("panel");
- 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;
- c->set_position(cpos);
- c->set_size(csize);
- }
- }
- void PopupPanel::_notification(int p_what) {
- if (p_what == NOTIFICATION_DRAW) {
- get_stylebox("panel")->draw(get_canvas_item(), Rect2(Point2(), get_size()));
- } else if (p_what == NOTIFICATION_READY) {
- _update_child_rects();
- } else if (p_what == NOTIFICATION_RESIZED) {
- _update_child_rects();
- }
- }
- PopupPanel::PopupPanel() {
- }
|