Skip to content

Commit ae0b7ff

Browse files
committed
Fix Embedded Game Size
1 parent 1b7b009 commit ae0b7ff

11 files changed

+343
-151
lines changed

editor/editor_run.cpp

+100-91
Original file line numberDiff line numberDiff line change
@@ -104,99 +104,16 @@ Error EditorRun::run(const String &p_scene, const String &p_write_movie, const V
104104
}
105105
}
106106

107-
int screen = EDITOR_GET("run/window_placement/screen");
108-
if (screen == -5) {
109-
// Same as editor
110-
screen = DisplayServer::get_singleton()->window_get_current_screen();
111-
} else if (screen == -4) {
112-
// Previous monitor (wrap to the other end if needed)
113-
screen = Math::wrapi(
114-
DisplayServer::get_singleton()->window_get_current_screen() - 1,
115-
0,
116-
DisplayServer::get_singleton()->get_screen_count());
117-
} else if (screen == -3) {
118-
// Next monitor (wrap to the other end if needed)
119-
screen = Math::wrapi(
120-
DisplayServer::get_singleton()->window_get_current_screen() + 1,
121-
0,
122-
DisplayServer::get_singleton()->get_screen_count());
107+
WindowPlacement window_placement = get_window_placement();
108+
if (window_placement.position != Point2i(INT_MAX, INT_MAX)) {
109+
args.push_back("--position");
110+
args.push_back(itos(window_placement.position.x) + "," + itos(window_placement.position.y));
123111
}
124112

125-
Rect2 screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
126-
127-
int window_placement = EDITOR_GET("run/window_placement/rect");
128-
if (screen_rect != Rect2()) {
129-
Size2 window_size;
130-
window_size.x = GLOBAL_GET("display/window/size/viewport_width");
131-
window_size.y = GLOBAL_GET("display/window/size/viewport_height");
132-
133-
Size2 desired_size;
134-
desired_size.x = GLOBAL_GET("display/window/size/window_width_override");
135-
desired_size.y = GLOBAL_GET("display/window/size/window_height_override");
136-
if (desired_size.x > 0 && desired_size.y > 0) {
137-
window_size = desired_size;
138-
}
139-
140-
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_HIDPI)) {
141-
bool hidpi_proj = GLOBAL_GET("display/window/dpi/allow_hidpi");
142-
int display_scale = 1;
143-
144-
if (OS::get_singleton()->is_hidpi_allowed()) {
145-
if (hidpi_proj) {
146-
display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale.
147-
} else {
148-
display_scale = DisplayServer::get_singleton()->screen_get_max_scale(); // Editor is in hiDPI mode, project is not, scale down.
149-
}
150-
} else {
151-
if (hidpi_proj) {
152-
display_scale = (1.f / DisplayServer::get_singleton()->screen_get_max_scale()); // Editor is not in hiDPI mode, project is, scale up.
153-
} else {
154-
display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale.
155-
}
156-
}
157-
screen_rect.position /= display_scale;
158-
screen_rect.size /= display_scale;
159-
}
160-
161-
switch (window_placement) {
162-
case 0: { // top left
163-
args.push_back("--position");
164-
args.push_back(itos(screen_rect.position.x) + "," + itos(screen_rect.position.y));
165-
} break;
166-
case 1: { // centered
167-
Vector2 pos = (screen_rect.position) + ((screen_rect.size - window_size) / 2).floor();
168-
args.push_back("--position");
169-
args.push_back(itos(pos.x) + "," + itos(pos.y));
170-
} break;
171-
case 2: { // custom pos
172-
Vector2 pos = EDITOR_GET("run/window_placement/rect_custom_position");
173-
pos += screen_rect.position;
174-
args.push_back("--position");
175-
args.push_back(itos(pos.x) + "," + itos(pos.y));
176-
} break;
177-
case 3: { // force maximized
178-
Vector2 pos = screen_rect.position + screen_rect.size / 2;
179-
args.push_back("--position");
180-
args.push_back(itos(pos.x) + "," + itos(pos.y));
181-
args.push_back("--maximized");
182-
} break;
183-
case 4: { // force fullscreen
184-
Vector2 pos = screen_rect.position + screen_rect.size / 2;
185-
args.push_back("--position");
186-
args.push_back(itos(pos.x) + "," + itos(pos.y));
187-
args.push_back("--fullscreen");
188-
} break;
189-
}
190-
} else {
191-
// Unable to get screen info, skip setting position.
192-
switch (window_placement) {
193-
case 3: { // force maximized
194-
args.push_back("--maximized");
195-
} break;
196-
case 4: { // force fullscreen
197-
args.push_back("--fullscreen");
198-
} break;
199-
}
113+
if (window_placement.force_maximized) {
114+
args.push_back("--maximized");
115+
} else if (window_placement.force_fullscreen) {
116+
args.push_back("--fullscreen");
200117
}
201118

202119
List<String> breakpoints;
@@ -297,6 +214,98 @@ OS::ProcessID EditorRun::get_current_process() const {
297214
return pids.front()->get();
298215
}
299216

217+
EditorRun::WindowPlacement EditorRun::get_window_placement() {
218+
WindowPlacement placement = WindowPlacement();
219+
placement.screen = EDITOR_GET("run/window_placement/screen");
220+
if (placement.screen == -5) {
221+
// Same as editor
222+
placement.screen = DisplayServer::get_singleton()->window_get_current_screen();
223+
} else if (placement.screen == -4) {
224+
// Previous monitor (wrap to the other end if needed)
225+
placement.screen = Math::wrapi(
226+
DisplayServer::get_singleton()->window_get_current_screen() - 1,
227+
0,
228+
DisplayServer::get_singleton()->get_screen_count());
229+
} else if (placement.screen == -3) {
230+
// Next monitor (wrap to the other end if needed)
231+
placement.screen = Math::wrapi(
232+
DisplayServer::get_singleton()->window_get_current_screen() + 1,
233+
0,
234+
DisplayServer::get_singleton()->get_screen_count());
235+
} else if (placement.screen == -2) {
236+
// Primary screen
237+
placement.screen = DisplayServer::get_singleton()->get_primary_screen();
238+
}
239+
240+
placement.size.x = GLOBAL_GET("display/window/size/viewport_width");
241+
placement.size.y = GLOBAL_GET("display/window/size/viewport_height");
242+
243+
Size2 desired_size;
244+
desired_size.x = GLOBAL_GET("display/window/size/window_width_override");
245+
desired_size.y = GLOBAL_GET("display/window/size/window_height_override");
246+
if (desired_size.x > 0 && desired_size.y > 0) {
247+
placement.size = desired_size;
248+
}
249+
250+
Rect2 screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(placement.screen);
251+
252+
int window_placement = EDITOR_GET("run/window_placement/rect");
253+
if (screen_rect != Rect2()) {
254+
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_HIDPI)) {
255+
bool hidpi_proj = GLOBAL_GET("display/window/dpi/allow_hidpi");
256+
int display_scale = 1;
257+
258+
if (OS::get_singleton()->is_hidpi_allowed()) {
259+
if (hidpi_proj) {
260+
display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale.
261+
} else {
262+
display_scale = DisplayServer::get_singleton()->screen_get_max_scale(); // Editor is in hiDPI mode, project is not, scale down.
263+
}
264+
} else {
265+
if (hidpi_proj) {
266+
display_scale = (1.f / DisplayServer::get_singleton()->screen_get_max_scale()); // Editor is not in hiDPI mode, project is, scale up.
267+
} else {
268+
display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale.
269+
}
270+
}
271+
screen_rect.position /= display_scale;
272+
screen_rect.size /= display_scale;
273+
}
274+
275+
switch (window_placement) {
276+
case 0: { // top left
277+
placement.position = screen_rect.position;
278+
} break;
279+
case 1: { // centered
280+
placement.position = (screen_rect.position) + ((screen_rect.size - placement.size) / 2).floor();
281+
} break;
282+
case 2: { // custom pos
283+
Vector2 pos = EDITOR_GET("run/window_placement/rect_custom_position");
284+
pos += screen_rect.position;
285+
placement.position = pos;
286+
} break;
287+
case 3: { // force maximized
288+
placement.force_maximized = true;
289+
} break;
290+
case 4: { // force fullscreen
291+
placement.force_fullscreen = true;
292+
} break;
293+
}
294+
} else {
295+
// Unable to get screen info, skip setting position.
296+
switch (window_placement) {
297+
case 3: { // force maximized
298+
placement.force_maximized = true;
299+
} break;
300+
case 4: { // force fullscreen
301+
placement.force_fullscreen = true;
302+
} break;
303+
}
304+
}
305+
306+
return placement;
307+
}
308+
300309
EditorRun::EditorRun() {
301310
status = STATUS_STOP;
302311
running_scene = "";

editor/editor_run.h

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ class EditorRun {
4545

4646
List<OS::ProcessID> pids;
4747

48+
struct WindowPlacement {
49+
int screen = 0;
50+
Point2i position = Point2i(INT_MAX, INT_MAX);
51+
Size2i size;
52+
bool force_maximized = false;
53+
bool force_fullscreen = false;
54+
};
55+
4856
private:
4957
Status status;
5058
String running_scene;
@@ -64,6 +72,8 @@ class EditorRun {
6472
int get_child_process_count() const { return pids.size(); }
6573
OS::ProcessID get_current_process() const;
6674

75+
static WindowPlacement get_window_placement();
76+
6777
EditorRun();
6878
};
6979

editor/icons/FixedSize.svg

+1
Loading

editor/icons/KeepAspect.svg

+1-1
Loading

editor/icons/Stretch.svg

+1
Loading

editor/plugins/embedded_process.cpp

+41-15
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void EmbeddedProcess::_notification(int p_what) {
5050
Rect2i new_global_rect = get_global_rect();
5151
if (last_global_rect != new_global_rect) {
5252
last_global_rect = new_global_rect;
53-
_queue_update_embedded_process();
53+
queue_update_embedded_process();
5454
}
5555

5656
} break;
@@ -60,7 +60,7 @@ void EmbeddedProcess::_notification(int p_what) {
6060
case NOTIFICATION_RESIZED:
6161
case NOTIFICATION_VISIBILITY_CHANGED:
6262
case NOTIFICATION_WM_POSITION_CHANGED: {
63-
_queue_update_embedded_process();
63+
queue_update_embedded_process();
6464
} break;
6565
case NOTIFICATION_THEME_CHANGED: {
6666
focus_style_box = get_theme_stylebox(SNAME("FocusViewport"), EditorStringName(EditorStyles));
@@ -77,7 +77,7 @@ void EmbeddedProcess::_notification(int p_what) {
7777
}
7878
} break;
7979
case NOTIFICATION_FOCUS_ENTER: {
80-
_queue_update_embedded_process();
80+
queue_update_embedded_process();
8181
} break;
8282
case NOTIFICATION_APPLICATION_FOCUS_IN: {
8383
application_has_focus = true;
@@ -88,7 +88,7 @@ void EmbeddedProcess::_notification(int p_what) {
8888
// or if the current window is a different popup or secondary window.
8989
if (embedding_completed && current_process_id != focused_process_id && window && window->has_focus()) {
9090
grab_focus();
91-
_queue_update_embedded_process();
91+
queue_update_embedded_process();
9292
}
9393
}
9494
} break;
@@ -102,27 +102,33 @@ void EmbeddedProcess::_notification(int p_what) {
102102
void EmbeddedProcess::set_window_size(const Size2i p_window_size) {
103103
if (window_size != p_window_size) {
104104
window_size = p_window_size;
105-
_queue_update_embedded_process();
105+
queue_update_embedded_process();
106106
}
107107
}
108108

109109
void EmbeddedProcess::set_keep_aspect(bool p_keep_aspect) {
110110
if (keep_aspect != p_keep_aspect) {
111111
keep_aspect = p_keep_aspect;
112-
_queue_update_embedded_process();
112+
queue_update_embedded_process();
113113
}
114114
}
115115

116116
Rect2i EmbeddedProcess::_get_global_embedded_window_rect() {
117117
Rect2i control_rect = get_global_rect();
118-
control_rect = Rect2i(control_rect.position, control_rect.size.maxi(1));
119-
if (keep_aspect) {
120-
Rect2i desired_rect = control_rect;
121-
float ratio = MIN((float)control_rect.size.x / window_size.x, (float)control_rect.size.y / window_size.y);
122-
desired_rect.size = Size2i(window_size.x * ratio, window_size.y * ratio).maxi(1);
118+
control_rect = Rect2i(control_rect.position + margin_top_left, (control_rect.size - get_margins_size()).maxi(1));
119+
if (window_size != Size2i()) {
120+
Rect2i desired_rect = Rect2i();
121+
if (!keep_aspect && control_rect.size.x >= window_size.x && control_rect.size.y >= window_size.y) {
122+
// Fixed at the desired size.
123+
desired_rect.size = window_size;
124+
} else {
125+
float ratio = MIN((float)control_rect.size.x / window_size.x, (float)control_rect.size.y / window_size.y);
126+
desired_rect.size = Size2i(window_size.x * ratio, window_size.y * ratio).maxi(1);
127+
}
123128
desired_rect.position = Size2i(control_rect.position.x + ((control_rect.size.x - desired_rect.size.x) / 2), control_rect.position.y + ((control_rect.size.y - desired_rect.size.y) / 2));
124129
return desired_rect;
125130
} else {
131+
// Stretch, use all the control area.
126132
return control_rect;
127133
}
128134
}
@@ -133,8 +139,28 @@ Rect2i EmbeddedProcess::get_screen_embedded_window_rect() {
133139
rect.position += window->get_position();
134140
}
135141

136-
// Removing margins to make space for the focus border style.
137-
return Rect2i(rect.position.x + margin_top_left.x, rect.position.y + margin_top_left.y, MAX(rect.size.x - (margin_top_left.x + margin_bottom_right.x), 1), MAX(rect.size.y - (margin_top_left.y + margin_bottom_right.y), 1));
142+
return rect;
143+
}
144+
145+
int EmbeddedProcess::get_margin_size(Side p_side) const {
146+
ERR_FAIL_INDEX_V((int)p_side, 4, 0);
147+
148+
switch (p_side) {
149+
case SIDE_LEFT:
150+
return margin_top_left.x;
151+
case SIDE_RIGHT:
152+
return margin_bottom_right.x;
153+
case SIDE_TOP:
154+
return margin_top_left.y;
155+
case SIDE_BOTTOM:
156+
return margin_bottom_right.y;
157+
}
158+
159+
return 0;
160+
}
161+
162+
Size2 EmbeddedProcess::get_margins_size() {
163+
return margin_top_left + margin_bottom_right;
138164
}
139165

140166
bool EmbeddedProcess::is_embedding_in_progress() {
@@ -215,7 +241,7 @@ bool EmbeddedProcess::_is_embedded_process_updatable() {
215241
return window && current_process_id != 0 && embedding_completed;
216242
}
217243

218-
void EmbeddedProcess::_queue_update_embedded_process() {
244+
void EmbeddedProcess::queue_update_embedded_process() {
219245
if (updated_embedded_process_queued || !_is_embedded_process_updatable()) {
220246
return;
221247
}
@@ -287,7 +313,7 @@ void EmbeddedProcess::_check_mouse_over() {
287313
// When we already have the focus and the user moves the mouse over the embedded process,
288314
// we just need to refocus the process.
289315
if (focused) {
290-
_queue_update_embedded_process();
316+
queue_update_embedded_process();
291317
} else {
292318
grab_focus();
293319
queue_redraw();

editor/plugins/embedded_process.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class EmbeddedProcess : public Control {
5959
Rect2i last_global_rect;
6060

6161
void _try_embed_process();
62-
void _queue_update_embedded_process();
6362
void _update_embedded_process();
6463
void _timer_embedding_timeout();
6564
void _draw();
@@ -78,8 +77,11 @@ class EmbeddedProcess : public Control {
7877

7978
void set_window_size(const Size2i p_window_size);
8079
void set_keep_aspect(bool p_keep_aspect);
80+
void queue_update_embedded_process();
8181

8282
Rect2i get_screen_embedded_window_rect();
83+
int get_margin_size(Side p_side) const;
84+
Size2 get_margins_size();
8385
bool is_embedding_in_progress();
8486
bool is_embedding_completed();
8587
int get_embedded_pid() const;

0 commit comments

Comments
 (0)