/**************************************************************************/ /* dialogs.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* 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. */ /**************************************************************************/ #include "dialogs.h" #include "dialogs.compat.inc" #include "core/os/keyboard.h" #include "core/string/print_string.h" #include "core/string/translation.h" #include "scene/gui/line_edit.h" #include "scene/theme/theme_db.h" // AcceptDialog void AcceptDialog::_input_from_window(const Ref &p_event) { if (close_on_escape && p_event->is_action_pressed(SNAME("ui_cancel"), false, true)) { _cancel_pressed(); } Window::_input_from_window(p_event); } void AcceptDialog::_parent_focused() { if (popped_up && !is_exclusive() && get_flag(FLAG_POPUP)) { _cancel_pressed(); } } void AcceptDialog::_notification(int p_what) { switch (p_what) { case NOTIFICATION_POST_ENTER_TREE: { if (is_visible()) { get_ok_button()->grab_focus(); } } break; case NOTIFICATION_VISIBILITY_CHANGED: { if (is_visible()) { if (get_ok_button()->is_inside_tree()) { get_ok_button()->grab_focus(); } _update_child_rects(); parent_visible = get_parent_visible_window(); if (parent_visible) { parent_visible->connect(SceneStringName(focus_entered), callable_mp(this, &AcceptDialog::_parent_focused)); } } else { popped_up = false; if (parent_visible) { parent_visible->disconnect(SceneStringName(focus_entered), callable_mp(this, &AcceptDialog::_parent_focused)); parent_visible = nullptr; } } } break; case NOTIFICATION_WM_WINDOW_FOCUS_IN: { if (!is_in_edited_scene_root()) { if (has_focus()) { popped_up = true; } } } break; case NOTIFICATION_THEME_CHANGED: { bg_panel->add_theme_style_override(SceneStringName(panel), theme_cache.panel_style); child_controls_changed(); if (is_visible()) { _update_child_rects(); } } break; case NOTIFICATION_EXIT_TREE: { if (parent_visible) { parent_visible->disconnect(SceneStringName(focus_entered), callable_mp(this, &AcceptDialog::_parent_focused)); parent_visible = nullptr; } } break; case NOTIFICATION_READY: case NOTIFICATION_WM_SIZE_CHANGED: { if (is_visible()) { _update_child_rects(); } } break; case NOTIFICATION_WM_CLOSE_REQUEST: { _cancel_pressed(); } break; } } void AcceptDialog::_text_submitted(const String &p_text) { if (get_ok_button() && get_ok_button()->is_disabled()) { return; // Do not allow submission if OK button is disabled. } _ok_pressed(); } void AcceptDialog::_post_popup() { Window::_post_popup(); popped_up = true; } void AcceptDialog::_ok_pressed() { if (hide_on_ok) { popped_up = false; set_visible(false); } ok_pressed(); emit_signal(SceneStringName(confirmed)); set_input_as_handled(); } void AcceptDialog::_cancel_pressed() { popped_up = false; Window *parent_window = parent_visible; if (parent_visible) { parent_visible->disconnect(SceneStringName(focus_entered), callable_mp(this, &AcceptDialog::_parent_focused)); parent_visible = nullptr; } callable_mp((Window *)this, &Window::hide).call_deferred(); emit_signal(SNAME("canceled")); cancel_pressed(); if (parent_window) { //parent_window->grab_focus(); } set_input_as_handled(); } String AcceptDialog::get_text() const { return message_label->get_text(); } void AcceptDialog::set_text(String p_text) { if (message_label->get_text() == p_text) { return; } message_label->set_text(p_text); child_controls_changed(); if (is_visible()) { _update_child_rects(); } } void AcceptDialog::set_hide_on_ok(bool p_hide) { hide_on_ok = p_hide; } bool AcceptDialog::get_hide_on_ok() const { return hide_on_ok; } void AcceptDialog::set_close_on_escape(bool p_hide) { close_on_escape = p_hide; } bool AcceptDialog::get_close_on_escape() const { return close_on_escape; } void AcceptDialog::set_autowrap(bool p_autowrap) { message_label->set_autowrap_mode(p_autowrap ? TextServer::AUTOWRAP_WORD : TextServer::AUTOWRAP_OFF); } bool AcceptDialog::has_autowrap() { return message_label->get_autowrap_mode() != TextServer::AUTOWRAP_OFF; } void AcceptDialog::set_ok_button_text(String p_ok_button_text) { ok_button->set_text(p_ok_button_text); child_controls_changed(); if (is_visible()) { _update_child_rects(); } } String AcceptDialog::get_ok_button_text() const { return ok_button->get_text(); } void AcceptDialog::register_text_enter(LineEdit *p_line_edit) { ERR_FAIL_NULL(p_line_edit); p_line_edit->connect("text_submitted", callable_mp(this, &AcceptDialog::_text_submitted)); } void AcceptDialog::_update_child_rects() { Size2 dlg_size = Vector2(get_size()) / get_content_scale_factor(); float h_margins = theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.panel_style->get_margin(SIDE_RIGHT); float v_margins = theme_cache.panel_style->get_margin(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_BOTTOM); // Fill the entire size of the window with the background. bg_panel->set_position(Point2()); bg_panel->set_size(dlg_size); for (int i = 0; i < buttons_hbox->get_child_count(); i++) { Button *b = Object::cast_to