diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2018-02-14 00:14:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-14 00:14:59 +0100 |
commit | 71ea00a358d430a110ae142ef548b5e720b622b0 (patch) | |
tree | b7674b9f4f93f95fda5c536595e98f13d33d8ac5 | |
parent | ba106ae51c01c45ccca2c599357d4304482b78fb (diff) | |
parent | ea1d726a4603fdd6bd4dfa6c1fa3128cfb2915c7 (diff) |
Merge pull request #16648 from marcelofg55/center_window
Added OS::center_window to center the window precisely on desktop platforms
-rw-r--r-- | core/bind/core_bind.cpp | 11 | ||||
-rw-r--r-- | core/bind/core_bind.h | 2 | ||||
-rw-r--r-- | core/os/os.cpp | 11 | ||||
-rw-r--r-- | core/os/os.h | 2 | ||||
-rw-r--r-- | platform/osx/os_osx.h | 1 | ||||
-rw-r--r-- | platform/osx/os_osx.mm | 6 | ||||
-rw-r--r-- | platform/windows/os_windows.cpp | 6 | ||||
-rw-r--r-- | platform/windows/os_windows.h | 1 | ||||
-rw-r--r-- | platform/x11/os_x11.cpp | 20 | ||||
-rw-r--r-- | platform/x11/os_x11.h | 1 |
10 files changed, 61 insertions, 0 deletions
diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 32b94b9b02..8f1a57e363 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -264,6 +264,10 @@ Size2 _OS::get_window_size() const { return OS::get_singleton()->get_window_size(); } +Size2 _OS::get_real_window_size() const { + return OS::get_singleton()->get_real_window_size(); +} + void _OS::set_window_size(const Size2 &p_size) { OS::get_singleton()->set_window_size(p_size); } @@ -929,6 +933,11 @@ void _OS::request_attention() { OS::get_singleton()->request_attention(); } +void _OS::center_window() { + + OS::get_singleton()->center_window(); +} + bool _OS::is_debug_build() const { #ifdef DEBUG_ENABLED @@ -1017,6 +1026,8 @@ void _OS::_bind_methods() { ClassDB::bind_method(D_METHOD("set_window_maximized", "enabled"), &_OS::set_window_maximized); ClassDB::bind_method(D_METHOD("is_window_maximized"), &_OS::is_window_maximized); ClassDB::bind_method(D_METHOD("request_attention"), &_OS::request_attention); + ClassDB::bind_method(D_METHOD("get_real_window_size"), &_OS::get_real_window_size); + ClassDB::bind_method(D_METHOD("center_window"), &_OS::center_window); ClassDB::bind_method(D_METHOD("set_borderless_window", "borderless"), &_OS::set_borderless_window); ClassDB::bind_method(D_METHOD("get_borderless_window"), &_OS::get_borderless_window); diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index 2353b6d09f..6b688a65f8 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -155,6 +155,7 @@ public: virtual Point2 get_window_position() const; virtual void set_window_position(const Point2 &p_position); virtual Size2 get_window_size() const; + virtual Size2 get_real_window_size() const; virtual void set_window_size(const Size2 &p_size); virtual void set_window_fullscreen(bool p_enabled); virtual bool is_window_fullscreen() const; @@ -165,6 +166,7 @@ public: virtual void set_window_maximized(bool p_enabled); virtual bool is_window_maximized() const; virtual void request_attention(); + virtual void center_window(); virtual void set_borderless_window(bool p_borderless); virtual bool get_borderless_window() const; diff --git a/core/os/os.cpp b/core/os/os.cpp index c6e5de703c..422acf95dc 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -616,6 +616,17 @@ bool OS::has_feature(const String &p_feature) { return false; } +void OS::center_window() { + + if (is_window_fullscreen()) return; + + Size2 scr = get_screen_size(get_current_screen()); + Size2 wnd = get_real_window_size(); + int x = scr.width / 2 - wnd.width / 2; + int y = scr.height / 2 - wnd.height / 2; + set_window_position(Vector2(x, y)); +} + OS::OS() { void *volatile stack_bottom; diff --git a/core/os/os.h b/core/os/os.h index 248e1dbefa..1ec488df06 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -182,6 +182,7 @@ public: virtual Point2 get_window_position() const { return Vector2(); } virtual void set_window_position(const Point2 &p_position) {} virtual Size2 get_window_size() const = 0; + virtual Size2 get_real_window_size() const { return get_window_size(); } virtual void set_window_size(const Size2 p_size) {} virtual void set_window_fullscreen(bool p_enabled) {} virtual bool is_window_fullscreen() const { return true; } @@ -192,6 +193,7 @@ public: virtual void set_window_maximized(bool p_enabled) {} virtual bool is_window_maximized() const { return true; } virtual void request_attention() {} + virtual void center_window(); virtual void set_borderless_window(bool p_borderless) {} virtual bool get_borderless_window() { return 0; } diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index d9ad0a7db8..0199bf0fc1 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -167,6 +167,7 @@ public: virtual void set_window_title(const String &p_title); virtual Size2 get_window_size() const; + virtual Size2 get_real_window_size() const; virtual void set_icon(const Ref<Image> &p_icon); diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index ab54f62045..c469af410e 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -1845,6 +1845,12 @@ Size2 OS_OSX::get_window_size() const { return window_size; }; +Size2 OS_OSX::get_real_window_size() const { + + NSRect frame = [window_object frame]; + return Size2(frame.size.width, frame.size.height); +} + void OS_OSX::set_window_size(const Size2 p_size) { Size2 size = p_size; diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index a2a51f10a7..f4b20b6981 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -1487,6 +1487,12 @@ Size2 OS_Windows::get_window_size() const { GetClientRect(hWnd, &r); return Vector2(r.right - r.left, r.bottom - r.top); } +Size2 OS_Windows::get_real_window_size() const { + + RECT r; + GetWindowRect(hWnd, &r); + return Vector2(r.right - r.left, r.bottom - r.top); +} void OS_Windows::set_window_size(const Size2 p_size) { video_mode.width = p_size.width; diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index 7308650695..543abe147d 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -201,6 +201,7 @@ public: virtual Point2 get_window_position() const; virtual void set_window_position(const Point2 &p_position); virtual Size2 get_window_size() const; + virtual Size2 get_real_window_size() const; virtual void set_window_size(const Size2 p_size); virtual void set_window_fullscreen(bool p_enabled); virtual bool is_window_fullscreen() const; diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index eaf72d4dbf..bd2abc1094 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -924,6 +924,26 @@ Size2 OS_X11::get_window_size() const { return Size2i(current_videomode.width, current_videomode.height); } +Size2 OS_X11::get_real_window_size() const { + XWindowAttributes xwa; + XSync(x11_display, False); + XGetWindowAttributes(x11_display, x11_window, &xwa); + int w = xwa.width; + int h = xwa.height; + Atom prop = XInternAtom(x11_display, "_NET_FRAME_EXTENTS", True); + Atom type; + int format; + unsigned long len; + unsigned long remaining; + unsigned char *data = NULL; + if (XGetWindowProperty(x11_display, x11_window, prop, 0, 4, False, AnyPropertyType, &type, &format, &len, &remaining, &data) == Success) { + long *extents = (long *)data; + w += extents[0] + extents[1]; // left, right + h += extents[2] + extents[3]; // top, bottom + } + return Size2(w, h); +} + void OS_X11::set_window_size(const Size2 p_size) { // If window resizable is disabled we need to update the attributes first if (is_window_resizable() == false) { diff --git a/platform/x11/os_x11.h b/platform/x11/os_x11.h index ee62b89227..3347b37f47 100644 --- a/platform/x11/os_x11.h +++ b/platform/x11/os_x11.h @@ -251,6 +251,7 @@ public: virtual Point2 get_window_position() const; virtual void set_window_position(const Point2 &p_position); virtual Size2 get_window_size() const; + virtual Size2 get_real_window_size() const; virtual void set_window_size(const Size2 p_size); virtual void set_window_fullscreen(bool p_enabled); virtual bool is_window_fullscreen() const; |