summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2023-08-28 12:07:27 +0200
committerRémi Verschelde <rverschelde@gmail.com>2023-08-28 12:07:27 +0200
commit072ba70424df59841cfce727a426b79b6ff549fc (patch)
treeef3e7c2298985301218d57bf8e6edd78f71f5075
parent8f076448676ed920f477dd35ae1788ebd28cd929 (diff)
parent9819ffb166a0c9bba671e0c7ed3cf2271c3addd0 (diff)
downloadredot-engine-072ba70424df59841cfce727a426b79b6ff549fc.tar.gz
Merge pull request #81012 from Jordyfel/center-window
Implement center window function.
-rw-r--r--doc/classes/Window.xml6
-rw-r--r--scene/main/window.cpp20
-rw-r--r--scene/main/window.h1
3 files changed, 27 insertions, 0 deletions
diff --git a/doc/classes/Window.xml b/doc/classes/Window.xml
index 92cd11d720..b03ef3ab4e 100644
--- a/doc/classes/Window.xml
+++ b/doc/classes/Window.xml
@@ -351,6 +351,12 @@
Returns [code]true[/code] if font oversampling is enabled. See [method set_use_font_oversampling].
</description>
</method>
+ <method name="move_to_center">
+ <return type="void" />
+ <description>
+ Centers a native window on the current screen and an embedded window on its embedder [Viewport].
+ </description>
+ </method>
<method name="move_to_foreground">
<return type="void" />
<description>
diff --git a/scene/main/window.cpp b/scene/main/window.cpp
index 1af279d94c..cffbb794c5 100644
--- a/scene/main/window.cpp
+++ b/scene/main/window.cpp
@@ -342,6 +342,25 @@ Point2i Window::get_position() const {
return position;
}
+void Window::move_to_center() {
+ ERR_MAIN_THREAD_GUARD;
+ ERR_FAIL_COND(!is_inside_tree());
+
+ Rect2 parent_rect;
+
+ if (is_embedded()) {
+ parent_rect = get_embedder()->get_visible_rect();
+ } else {
+ int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(get_window_id());
+ parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
+ parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
+ }
+
+ if (parent_rect != Rect2()) {
+ set_position(parent_rect.position + (parent_rect.size - get_size()) / 2);
+ }
+}
+
void Window::set_size(const Size2i &p_size) {
ERR_MAIN_THREAD_GUARD;
@@ -2602,6 +2621,7 @@ void Window::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_position", "position"), &Window::set_position);
ClassDB::bind_method(D_METHOD("get_position"), &Window::get_position);
+ ClassDB::bind_method(D_METHOD("move_to_center"), &Window::move_to_center);
ClassDB::bind_method(D_METHOD("set_size", "size"), &Window::set_size);
ClassDB::bind_method(D_METHOD("get_size"), &Window::get_size);
diff --git a/scene/main/window.h b/scene/main/window.h
index c387ffa92a..d781f228d2 100644
--- a/scene/main/window.h
+++ b/scene/main/window.h
@@ -246,6 +246,7 @@ public:
void set_position(const Point2i &p_position);
Point2i get_position() const;
+ void move_to_center();
void set_size(const Size2i &p_size);
Size2i get_size() const;