You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-14 13:41:12 +00:00
Added OS::center_window to center the window precisely on desktop platforms
This commit is contained in:
committed by
Marcelo Fernandez
parent
7983fb95b0
commit
129e0f34d0
@@ -261,6 +261,10 @@ Size2 _OS::get_window_size() const {
|
|||||||
return OS::get_singleton()->get_window_size();
|
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) {
|
void _OS::set_window_size(const Size2 &p_size) {
|
||||||
OS::get_singleton()->set_window_size(p_size);
|
OS::get_singleton()->set_window_size(p_size);
|
||||||
}
|
}
|
||||||
@@ -935,6 +939,11 @@ void _OS::request_attention() {
|
|||||||
OS::get_singleton()->request_attention();
|
OS::get_singleton()->request_attention();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _OS::center_window() {
|
||||||
|
|
||||||
|
OS::get_singleton()->center_window();
|
||||||
|
}
|
||||||
|
|
||||||
bool _OS::is_debug_build() const {
|
bool _OS::is_debug_build() const {
|
||||||
|
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
@@ -1032,6 +1041,8 @@ void _OS::_bind_methods() {
|
|||||||
ObjectTypeDB::bind_method(_MD("set_window_maximized", "enabled"), &_OS::set_window_maximized);
|
ObjectTypeDB::bind_method(_MD("set_window_maximized", "enabled"), &_OS::set_window_maximized);
|
||||||
ObjectTypeDB::bind_method(_MD("is_window_maximized"), &_OS::is_window_maximized);
|
ObjectTypeDB::bind_method(_MD("is_window_maximized"), &_OS::is_window_maximized);
|
||||||
ObjectTypeDB::bind_method(_MD("request_attention"), &_OS::request_attention);
|
ObjectTypeDB::bind_method(_MD("request_attention"), &_OS::request_attention);
|
||||||
|
ObjectTypeDB::bind_method(_MD("get_real_window_size"), &_OS::get_real_window_size);
|
||||||
|
ObjectTypeDB::bind_method(_MD("center_window"), &_OS::center_window);
|
||||||
|
|
||||||
ObjectTypeDB::bind_method(_MD("set_borderless_window", "borderless"), &_OS::set_borderless_window);
|
ObjectTypeDB::bind_method(_MD("set_borderless_window", "borderless"), &_OS::set_borderless_window);
|
||||||
ObjectTypeDB::bind_method(_MD("get_borderless_window"), &_OS::get_borderless_window);
|
ObjectTypeDB::bind_method(_MD("get_borderless_window"), &_OS::get_borderless_window);
|
||||||
|
|||||||
@@ -142,6 +142,7 @@ public:
|
|||||||
virtual Point2 get_window_position() const;
|
virtual Point2 get_window_position() const;
|
||||||
virtual void set_window_position(const Point2 &p_position);
|
virtual void set_window_position(const Point2 &p_position);
|
||||||
virtual Size2 get_window_size() const;
|
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_size(const Size2 &p_size);
|
||||||
virtual void set_window_fullscreen(bool p_enabled);
|
virtual void set_window_fullscreen(bool p_enabled);
|
||||||
virtual bool is_window_fullscreen() const;
|
virtual bool is_window_fullscreen() const;
|
||||||
@@ -152,6 +153,7 @@ public:
|
|||||||
virtual void set_window_maximized(bool p_enabled);
|
virtual void set_window_maximized(bool p_enabled);
|
||||||
virtual bool is_window_maximized() const;
|
virtual bool is_window_maximized() const;
|
||||||
virtual void request_attention();
|
virtual void request_attention();
|
||||||
|
virtual void center_window();
|
||||||
|
|
||||||
virtual void set_borderless_window(bool p_borderless);
|
virtual void set_borderless_window(bool p_borderless);
|
||||||
virtual bool get_borderless_window() const;
|
virtual bool get_borderless_window() const;
|
||||||
|
|||||||
@@ -546,6 +546,17 @@ Dictionary OS::get_engine_version() const {
|
|||||||
return dict;
|
return dict;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
OS::OS() {
|
||||||
last_error = NULL;
|
last_error = NULL;
|
||||||
frames_drawn = 0;
|
frames_drawn = 0;
|
||||||
|
|||||||
@@ -168,6 +168,7 @@ public:
|
|||||||
virtual Point2 get_window_position() const { return Vector2(); }
|
virtual Point2 get_window_position() const { return Vector2(); }
|
||||||
virtual void set_window_position(const Point2 &p_position) {}
|
virtual void set_window_position(const Point2 &p_position) {}
|
||||||
virtual Size2 get_window_size() const = 0;
|
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_size(const Size2 p_size) {}
|
||||||
virtual void set_window_fullscreen(bool p_enabled) {}
|
virtual void set_window_fullscreen(bool p_enabled) {}
|
||||||
virtual bool is_window_fullscreen() const { return true; }
|
virtual bool is_window_fullscreen() const { return true; }
|
||||||
@@ -178,6 +179,7 @@ public:
|
|||||||
virtual void set_window_maximized(bool p_enabled) {}
|
virtual void set_window_maximized(bool p_enabled) {}
|
||||||
virtual bool is_window_maximized() const { return true; }
|
virtual bool is_window_maximized() const { return true; }
|
||||||
virtual void request_attention() {}
|
virtual void request_attention() {}
|
||||||
|
virtual void center_window();
|
||||||
|
|
||||||
virtual void set_borderless_window(int p_borderless) {}
|
virtual void set_borderless_window(int p_borderless) {}
|
||||||
virtual bool get_borderless_window() { return 0; }
|
virtual bool get_borderless_window() { return 0; }
|
||||||
|
|||||||
@@ -159,6 +159,7 @@ public:
|
|||||||
virtual void set_window_title(const String &p_title);
|
virtual void set_window_title(const String &p_title);
|
||||||
|
|
||||||
virtual Size2 get_window_size() const;
|
virtual Size2 get_window_size() const;
|
||||||
|
virtual Size2 get_real_window_size() const;
|
||||||
|
|
||||||
virtual void set_icon(const Image &p_icon);
|
virtual void set_icon(const Image &p_icon);
|
||||||
|
|
||||||
|
|||||||
@@ -1549,6 +1549,12 @@ Size2 OS_OSX::get_window_size() const {
|
|||||||
return window_size;
|
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) {
|
void OS_OSX::set_window_size(const Size2 p_size) {
|
||||||
|
|
||||||
Size2 size = p_size;
|
Size2 size = p_size;
|
||||||
|
|||||||
@@ -1486,6 +1486,12 @@ Size2 OS_Windows::get_window_size() const {
|
|||||||
GetClientRect(hWnd, &r);
|
GetClientRect(hWnd, &r);
|
||||||
return Vector2(r.right - r.left, r.bottom - r.top);
|
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) {
|
void OS_Windows::set_window_size(const Size2 p_size) {
|
||||||
|
|
||||||
video_mode.width = p_size.width;
|
video_mode.width = p_size.width;
|
||||||
|
|||||||
@@ -211,6 +211,7 @@ public:
|
|||||||
virtual Point2 get_window_position() const;
|
virtual Point2 get_window_position() const;
|
||||||
virtual void set_window_position(const Point2 &p_position);
|
virtual void set_window_position(const Point2 &p_position);
|
||||||
virtual Size2 get_window_size() const;
|
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_size(const Size2 p_size);
|
||||||
virtual void set_window_fullscreen(bool p_enabled);
|
virtual void set_window_fullscreen(bool p_enabled);
|
||||||
virtual bool is_window_fullscreen() const;
|
virtual bool is_window_fullscreen() const;
|
||||||
|
|||||||
@@ -801,6 +801,26 @@ Size2 OS_X11::get_window_size() const {
|
|||||||
return Size2i(current_videomode.width, current_videomode.height);
|
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) {
|
void OS_X11::set_window_size(const Size2 p_size) {
|
||||||
// If window resizable is disabled we need to update the attributes first
|
// If window resizable is disabled we need to update the attributes first
|
||||||
if (is_window_resizable() == false) {
|
if (is_window_resizable() == false) {
|
||||||
|
|||||||
@@ -246,6 +246,7 @@ public:
|
|||||||
virtual Point2 get_window_position() const;
|
virtual Point2 get_window_position() const;
|
||||||
virtual void set_window_position(const Point2 &p_position);
|
virtual void set_window_position(const Point2 &p_position);
|
||||||
virtual Size2 get_window_size() const;
|
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_size(const Size2 p_size);
|
||||||
virtual void set_window_fullscreen(bool p_enabled);
|
virtual void set_window_fullscreen(bool p_enabled);
|
||||||
virtual bool is_window_fullscreen() const;
|
virtual bool is_window_fullscreen() const;
|
||||||
|
|||||||
Reference in New Issue
Block a user