From 80e9e93e27622e1c03a4da0ef130367191a7b996 Mon Sep 17 00:00:00 2001 From: homer666 Date: Sat, 27 Apr 2019 05:36:44 +1000 Subject: [PATCH] Add Popup::popup_centered_clamped method - Also replace redundant duplicate code in editor dialogs with calls to popup_centered_clamped() --- doc/classes/Popup.xml | 11 +++++++++++ editor/create_dialog.cpp | 9 +-------- editor/editor_node.cpp | 9 +-------- editor/project_export.cpp | 9 +-------- editor/project_settings_editor.cpp | 10 ++-------- editor/scene_tree_dock.cpp | 8 +------- editor/settings_config_dialog.cpp | 9 +-------- scene/gui/popup.cpp | 13 +++++++++++++ scene/gui/popup.h | 1 + 9 files changed, 32 insertions(+), 47 deletions(-) diff --git a/doc/classes/Popup.xml b/doc/classes/Popup.xml index c3256f2f5b5..e1b51463b21 100644 --- a/doc/classes/Popup.xml +++ b/doc/classes/Popup.xml @@ -27,6 +27,17 @@ Popup (show the control in modal form) in the center of the screen relative to its current canvas transform, at the current size, or at a size determined by "size". + + + + + + + + + Popup (show the control in modal form) in the center of the screen relative to the current canvas transform, clamping the size to [code]size[/code], then ensuring the popup is no larger than the viewport size multiplied by [code]fallback_ratio[/code]. + + diff --git a/editor/create_dialog.cpp b/editor/create_dialog.cpp index 26bd651c2ba..765fd023fad 100644 --- a/editor/create_dialog.cpp +++ b/editor/create_dialog.cpp @@ -93,14 +93,7 @@ void CreateDialog::popup_create(bool p_dont_clear, bool p_replace_mode) { if (saved_size != Rect2()) { popup(saved_size); } else { - - Size2 popup_size = Size2(900, 700) * editor_get_scale(); - Size2 window_size = get_viewport_rect().size; - - popup_size.x = MIN(window_size.x * 0.8, popup_size.x); - popup_size.y = MIN(window_size.y * 0.8, popup_size.y); - - popup_centered(popup_size); + popup_centered_clamped(Size2(900, 700) * EDSCALE, 0.8); } if (p_dont_clear) { diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index df653d6d03c..8aa8a32d7d8 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -2451,14 +2451,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) { } break; case SETTINGS_MANAGE_FEATURE_PROFILES: { - Size2 popup_size = Size2(900, 800) * editor_get_scale(); - Size2 window_size = get_viewport()->get_size(); - - popup_size.x = MIN(window_size.x * 0.8, popup_size.x); - popup_size.y = MIN(window_size.y * 0.8, popup_size.y); - - feature_profile_manager->popup_centered(popup_size); - + feature_profile_manager->popup_centered_clamped(Size2(900, 800) * EDSCALE, 0.8); } break; case SETTINGS_TOGGLE_FULLSCREEN: { diff --git a/editor/project_export.cpp b/editor/project_export.cpp index b9cf7ec10ac..d56d4e6cf16 100644 --- a/editor/project_export.cpp +++ b/editor/project_export.cpp @@ -88,14 +88,7 @@ void ProjectExportDialog::popup_export() { if (saved_size != Rect2()) { popup(saved_size); } else { - - Size2 popup_size = Size2(900, 700) * editor_get_scale(); - Size2 window_size = get_viewport_rect().size; - - popup_size.x = MIN(window_size.x * 0.8, popup_size.x); - popup_size.y = MIN(window_size.y * 0.8, popup_size.y); - - popup_centered(popup_size); + popup_centered_clamped(Size2(900, 700) * EDSCALE, 0.8); } } diff --git a/editor/project_settings_editor.cpp b/editor/project_settings_editor.cpp index 2d7ad8bc042..0dd9807fbf6 100644 --- a/editor/project_settings_editor.cpp +++ b/editor/project_settings_editor.cpp @@ -793,15 +793,9 @@ void ProjectSettingsEditor::popup_project_settings() { if (saved_size != Rect2()) { popup(saved_size); } else { - - Size2 popup_size = Size2(900, 700) * editor_get_scale(); - Size2 window_size = get_viewport_rect().size; - - popup_size.x = MIN(window_size.x * 0.8, popup_size.x); - popup_size.y = MIN(window_size.y * 0.8, popup_size.y); - - popup_centered(popup_size); + popup_centered_clamped(Size2(900, 700) * EDSCALE, 0.8); } + globals_editor->update_category_list(); _update_translations(); autoload_settings->update_autoload(); diff --git a/editor/scene_tree_dock.cpp b/editor/scene_tree_dock.cpp index 1dca5421381..4bb18f3ed97 100644 --- a/editor/scene_tree_dock.cpp +++ b/editor/scene_tree_dock.cpp @@ -1940,13 +1940,7 @@ void SceneTreeDock::set_selected(Node *p_node, bool p_emit_selected) { void SceneTreeDock::import_subscene() { - Size2 popup_size = Size2(500, 800) * editor_get_scale(); - Size2 window_size = get_viewport_rect().size; - - popup_size.x = MIN(window_size.x * 0.8, popup_size.x); - popup_size.y = MIN(window_size.y * 0.8, popup_size.y); - - import_subscene_dialog->popup_centered(popup_size); + import_subscene_dialog->popup_centered_clamped(Size2(500, 800) * EDSCALE, 0.8); } void SceneTreeDock::_import_subscene() { diff --git a/editor/settings_config_dialog.cpp b/editor/settings_config_dialog.cpp index d6756b2bb3e..6b1e53aa2e1 100644 --- a/editor/settings_config_dialog.cpp +++ b/editor/settings_config_dialog.cpp @@ -98,14 +98,7 @@ void EditorSettingsDialog::popup_edit_settings() { if (saved_size != Rect2()) { popup(saved_size); } else { - - Size2 popup_size = Size2(900, 700) * editor_get_scale(); - Size2 window_size = get_viewport_rect().size; - - popup_size.x = MIN(window_size.x * 0.8, popup_size.x); - popup_size.y = MIN(window_size.y * 0.8, popup_size.y); - - popup_centered(popup_size); + popup_centered_clamped(Size2(900, 700) * EDSCALE, 0.8); } _focus_current_search_box(); diff --git a/scene/gui/popup.cpp b/scene/gui/popup.cpp index 80ec7049fc7..bd9584f3db8 100644 --- a/scene/gui/popup.cpp +++ b/scene/gui/popup.cpp @@ -115,6 +115,18 @@ void Popup::set_as_minsize() { set_size(total_minsize); } +void Popup::popup_centered_clamped(const Size2 &p_size, float p_fallback_ratio) { + + Size2 popup_size = p_size; + Size2 window_size = get_viewport_rect().size; + + // clamp popup size in each dimension if window size is too small (using fallback ratio) + popup_size.x = MIN(window_size.x * p_fallback_ratio, popup_size.x); + popup_size.y = MIN(window_size.y * p_fallback_ratio, popup_size.y); + + popup_centered(popup_size); +} + void Popup::popup_centered_minsize(const Size2 &p_minsize) { set_custom_minimum_size(p_minsize); @@ -179,6 +191,7 @@ 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_centered_clamped", "size", "fallback_ratio"), &Popup::popup_centered_clamped, DEFVAL(Size2()), DEFVAL(0.75)); 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); diff --git a/scene/gui/popup.h b/scene/gui/popup.h index 7ccefe1d757..6615c51ec5f 100644 --- a/scene/gui/popup.h +++ b/scene/gui/popup.h @@ -64,6 +64,7 @@ public: void popup_centered(const Size2 &p_size = Size2()); void popup_centered_minsize(const Size2 &p_minsize = Size2()); void set_as_minsize(); + void popup_centered_clamped(const Size2 &p_size = Size2(), float p_fallback_ratio = 0.75); virtual void popup(const Rect2 &p_bounds = Rect2()); virtual String get_configuration_warning() const;