diff options
Diffstat (limited to 'scene/gui/popup.cpp')
-rw-r--r-- | scene/gui/popup.cpp | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/scene/gui/popup.cpp b/scene/gui/popup.cpp index b7601bdd3e..492e379440 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); @@ -129,7 +141,7 @@ void Popup::popup_centered(const Size2 &p_size) { rect.size = p_size == Size2() ? get_size() : p_size; rect.position = ((window_size - rect.size) / 2.0).floor(); - popup(rect); + _popup(rect, true); } void Popup::popup_centered_ratio(float p_screen_ratio) { @@ -139,18 +151,29 @@ void Popup::popup_centered_ratio(float p_screen_ratio) { rect.size = (window_size * p_screen_ratio).floor(); rect.position = ((window_size - rect.size) / 2.0).floor(); - popup(rect); + _popup(rect, true); } void Popup::popup(const Rect2 &p_bounds) { + _popup(p_bounds); +} + +void Popup::_popup(const Rect2 &p_bounds, const bool p_centered) { + emit_signal("about_to_show"); show_modal(exclusive); // Fit the popup into the optionally provided bounds. if (!p_bounds.has_no_area()) { - set_position(p_bounds.position); set_size(p_bounds.size); + + // check if p_bounds.size was using an outdated cached values + if (p_centered && p_bounds.size != get_size()) { + set_position(p_bounds.position - ((get_size() - p_bounds.size) / 2.0).floor()); + } else { + set_position(p_bounds.position); + } } _fix_size(); @@ -179,6 +202,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); |