diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2023-02-07 16:30:41 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2023-02-07 16:30:41 +0100 |
commit | 929333fe267f488638c76564237faff9d5d572fc (patch) | |
tree | 676f6fef50eb021edde9a4659659c78a5621d8a5 /scene/main | |
parent | 743e26d4f93ba277e6af33c75de6b65b8c592084 (diff) | |
parent | c73844f1171861c9ff694c8253356047321e65b4 (diff) |
Merge pull request #62179 from Rindbee/fix-popup_center-off-center
Use min_size/max_size to limit window size and position while popup center
Diffstat (limited to 'scene/main')
-rw-r--r-- | scene/main/viewport.cpp | 14 | ||||
-rw-r--r-- | scene/main/window.cpp | 100 | ||||
-rw-r--r-- | scene/main/window.h | 6 |
3 files changed, 72 insertions, 48 deletions
diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 89f0fd2039..a1e7555a5d 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -2561,20 +2561,14 @@ bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) { if (gui.subwindow_drag == SUB_WINDOW_DRAG_RESIZE) { Vector2i diff = mm->get_position() - gui.subwindow_drag_from; Size2i min_size = gui.subwindow_focused->get_min_size(); + Size2i min_size_clamped = gui.subwindow_focused->get_clamped_minimum_size(); - Size2i min_size_adjusted = min_size; - if (gui.subwindow_focused->is_wrapping_controls()) { - Size2i cms = gui.subwindow_focused->get_contents_minimum_size(); - min_size_adjusted.x = MAX(cms.x, min_size.x); - min_size_adjusted.y = MAX(cms.y, min_size.y); - } - - min_size_adjusted.x = MAX(min_size_adjusted.x, 1); - min_size_adjusted.y = MAX(min_size_adjusted.y, 1); + min_size_clamped.x = MAX(min_size_clamped.x, 1); + min_size_clamped.y = MAX(min_size_clamped.y, 1); Rect2i r = gui.subwindow_resize_from_rect; - Size2i limit = r.size - min_size_adjusted; + Size2i limit = r.size - min_size_clamped; switch (gui.subwindow_resize_mode) { case SUB_WINDOW_RESIZE_TOP_LEFT: { diff --git a/scene/main/window.cpp b/scene/main/window.cpp index 4cdd447235..44df648552 100644 --- a/scene/main/window.cpp +++ b/scene/main/window.cpp @@ -349,8 +349,30 @@ Size2i Window::get_size_with_decorations() const { return size; } +Size2i Window::_clamp_limit_size(const Size2i &p_limit_size) { + // Force window limits to respect size limitations of rendering server. + Size2i max_window_size = RS::get_singleton()->get_maximum_viewport_size(); + if (max_window_size != Size2i()) { + return p_limit_size.clamp(Vector2i(), max_window_size); + } else { + return p_limit_size.max(Vector2i()); + } +} + +void Window::_validate_limit_size() { + // When max_size is invalid, max_size_used falls back to respect size limitations of rendering server. + bool max_size_valid = (max_size.x > 0 || max_size.y > 0) && max_size.x >= min_size.x && max_size.y >= min_size.y; + max_size_used = max_size_valid ? max_size : RS::get_singleton()->get_maximum_viewport_size(); +} + void Window::set_max_size(const Size2i &p_max_size) { - max_size = p_max_size; + Size2i max_size_clamped = _clamp_limit_size(p_max_size); + if (max_size == max_size_clamped) { + return; + } + max_size = max_size_clamped; + + _validate_limit_size(); _update_window_size(); } @@ -359,7 +381,13 @@ Size2i Window::get_max_size() const { } void Window::set_min_size(const Size2i &p_min_size) { - min_size = p_min_size; + Size2i min_size_clamped = _clamp_limit_size(p_min_size); + if (min_size == min_size_clamped) { + return; + } + min_size = min_size_clamped; + + _validate_limit_size(); _update_window_size(); } @@ -833,49 +861,35 @@ bool Window::is_visible() const { return visible; } -void Window::_update_window_size() { - // Force window to respect size limitations of rendering server. - RenderingServer *rendering_server = RenderingServer::get_singleton(); - if (rendering_server) { - Size2i max_window_size = rendering_server->get_maximum_viewport_size(); +Size2i Window::_clamp_window_size(const Size2i &p_size) { + Size2i window_size_clamped = p_size; + Size2 minsize = get_clamped_minimum_size(); + window_size_clamped = window_size_clamped.max(minsize); - if (max_window_size != Size2i()) { - size = size.min(max_window_size); - min_size = min_size.min(max_window_size); - max_size = max_size.min(max_window_size); - } + if (max_size_used != Size2i()) { + window_size_clamped = window_size_clamped.min(max_size_used); } - Size2i size_limit; - if (wrap_controls) { - size_limit = get_contents_minimum_size(); - } + return window_size_clamped; +} - size_limit.x = MAX(size_limit.x, min_size.x); - size_limit.y = MAX(size_limit.y, min_size.y); +void Window::_update_window_size() { + Size2i size_limit = get_clamped_minimum_size(); - size.x = MAX(size_limit.x, size.x); - size.y = MAX(size_limit.y, size.y); + size = size.max(size_limit); bool reset_min_first = false; - bool max_size_valid = false; - if ((max_size.x > 0 || max_size.y > 0) && (max_size.x >= min_size.x && max_size.y >= min_size.y)) { - max_size_valid = true; + if (max_size_used != Size2i()) { + // Force window size to respect size limitations of max_size_used. + size = size.min(max_size_used); - if (size.x > max_size.x) { - size.x = max_size.x; - } - if (size_limit.x > max_size.x) { - size_limit.x = max_size.x; + if (size_limit.x > max_size_used.x) { + size_limit.x = max_size_used.x; reset_min_first = true; } - - if (size.y > max_size.y) { - size.y = max_size.y; - } - if (size_limit.y > max_size.y) { - size_limit.y = max_size.y; + if (size_limit.y > max_size_used.y) { + size_limit.y = max_size_used.y; reset_min_first = true; } } @@ -891,7 +905,7 @@ void Window::_update_window_size() { DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id); } - DisplayServer::get_singleton()->window_set_max_size(max_size_valid ? max_size : Size2i(), window_id); + DisplayServer::get_singleton()->window_set_max_size(max_size_used, window_id); DisplayServer::get_singleton()->window_set_min_size(size_limit, window_id); DisplayServer::get_singleton()->window_set_size(size, window_id); } @@ -1423,6 +1437,8 @@ void Window::popup_centered_clamped(const Size2i &p_size, float p_fallback_ratio Rect2i popup_rect; popup_rect.size = Vector2i(MIN(size_ratio.x, p_size.x), MIN(size_ratio.y, p_size.y)); + popup_rect.size = _clamp_window_size(popup_rect.size); + if (parent_rect != Rect2()) { popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2; } @@ -1446,9 +1462,7 @@ void Window::popup_centered(const Size2i &p_minsize) { } Rect2i popup_rect; - Size2 contents_minsize = _get_contents_minimum_size(); - popup_rect.size.x = MAX(p_minsize.x, contents_minsize.x); - popup_rect.size.y = MAX(p_minsize.y, contents_minsize.y); + popup_rect.size = _clamp_window_size(p_minsize); if (parent_rect != Rect2()) { popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2; @@ -1476,6 +1490,7 @@ void Window::popup_centered_ratio(float p_ratio) { Rect2i popup_rect; if (parent_rect != Rect2()) { popup_rect.size = parent_rect.size * p_ratio; + popup_rect.size = _clamp_window_size(popup_rect.size); popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2; } @@ -1539,6 +1554,14 @@ Size2 Window::get_contents_minimum_size() const { return _get_contents_minimum_size(); } +Size2 Window::get_clamped_minimum_size() const { + if (!wrap_controls) { + return min_size; + } + + return min_size.max(get_contents_minimum_size()); +} + void Window::grab_focus() { if (embedder) { embedder->_sub_window_grab_focus(this); @@ -2391,6 +2414,7 @@ Window::Window() { RenderingServer *rendering_server = RenderingServer::get_singleton(); if (rendering_server) { max_size = rendering_server->get_maximum_viewport_size(); + max_size_used = max_size; // Update max_size_used. } theme_owner = memnew(ThemeOwner); diff --git a/scene/main/window.h b/scene/main/window.h index 1920aae0fe..5359c37e9a 100644 --- a/scene/main/window.h +++ b/scene/main/window.h @@ -135,6 +135,11 @@ private: void _clear_window(); void _update_from_window(); + Size2i max_size_used; + + Size2i _clamp_limit_size(const Size2i &p_limit_size); + Size2i _clamp_window_size(const Size2i &p_size); + void _validate_limit_size(); void _update_viewport_size(); void _update_window_size(); @@ -306,6 +311,7 @@ public: void popup_centered_clamped(const Size2i &p_size = Size2i(), float p_fallback_ratio = 0.75); Size2 get_contents_minimum_size() const; + Size2 get_clamped_minimum_size() const; void grab_focus(); bool has_focus() const; |