Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/window.cpp
9902 views
1
/**************************************************************************/
2
/* window.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "window.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/debugger/engine_debugger.h"
35
#include "core/string/translation_server.h"
36
#include "scene/gui/control.h"
37
#include "scene/theme/theme_db.h"
38
#include "scene/theme/theme_owner.h"
39
40
// Editor integration.
41
42
int Window::root_layout_direction = 0;
43
44
void Window::set_root_layout_direction(int p_root_dir) {
45
root_layout_direction = p_root_dir;
46
}
47
48
// Dynamic properties.
49
50
Window *Window::focused_window = nullptr;
51
52
bool Window::_set(const StringName &p_name, const Variant &p_value) {
53
ERR_MAIN_THREAD_GUARD_V(false);
54
String name = p_name;
55
56
if (!name.begins_with("theme_override")) {
57
return false;
58
}
59
60
if (p_value.get_type() == Variant::NIL || (p_value.get_type() == Variant::OBJECT && (Object *)p_value == nullptr)) {
61
if (name.begins_with("theme_override_icons/")) {
62
String dname = name.get_slicec('/', 1);
63
if (theme_icon_override.has(dname)) {
64
theme_icon_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
65
}
66
theme_icon_override.erase(dname);
67
_notify_theme_override_changed();
68
} else if (name.begins_with("theme_override_styles/")) {
69
String dname = name.get_slicec('/', 1);
70
if (theme_style_override.has(dname)) {
71
theme_style_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
72
}
73
theme_style_override.erase(dname);
74
_notify_theme_override_changed();
75
} else if (name.begins_with("theme_override_fonts/")) {
76
String dname = name.get_slicec('/', 1);
77
if (theme_font_override.has(dname)) {
78
theme_font_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
79
}
80
theme_font_override.erase(dname);
81
_notify_theme_override_changed();
82
} else if (name.begins_with("theme_override_font_sizes/")) {
83
String dname = name.get_slicec('/', 1);
84
theme_font_size_override.erase(dname);
85
_notify_theme_override_changed();
86
} else if (name.begins_with("theme_override_colors/")) {
87
String dname = name.get_slicec('/', 1);
88
theme_color_override.erase(dname);
89
_notify_theme_override_changed();
90
} else if (name.begins_with("theme_override_constants/")) {
91
String dname = name.get_slicec('/', 1);
92
theme_constant_override.erase(dname);
93
_notify_theme_override_changed();
94
} else {
95
return false;
96
}
97
} else {
98
if (name.begins_with("theme_override_icons/")) {
99
String dname = name.get_slicec('/', 1);
100
add_theme_icon_override(dname, p_value);
101
} else if (name.begins_with("theme_override_styles/")) {
102
String dname = name.get_slicec('/', 1);
103
add_theme_style_override(dname, p_value);
104
} else if (name.begins_with("theme_override_fonts/")) {
105
String dname = name.get_slicec('/', 1);
106
add_theme_font_override(dname, p_value);
107
} else if (name.begins_with("theme_override_font_sizes/")) {
108
String dname = name.get_slicec('/', 1);
109
add_theme_font_size_override(dname, p_value);
110
} else if (name.begins_with("theme_override_colors/")) {
111
String dname = name.get_slicec('/', 1);
112
add_theme_color_override(dname, p_value);
113
} else if (name.begins_with("theme_override_constants/")) {
114
String dname = name.get_slicec('/', 1);
115
add_theme_constant_override(dname, p_value);
116
} else {
117
return false;
118
}
119
}
120
return true;
121
}
122
123
bool Window::_get(const StringName &p_name, Variant &r_ret) const {
124
ERR_READ_THREAD_GUARD_V(false);
125
String sname = p_name;
126
127
if (!sname.begins_with("theme_override")) {
128
return false;
129
}
130
131
if (sname.begins_with("theme_override_icons/")) {
132
String name = sname.get_slicec('/', 1);
133
r_ret = theme_icon_override.has(name) ? Variant(theme_icon_override[name]) : Variant();
134
} else if (sname.begins_with("theme_override_styles/")) {
135
String name = sname.get_slicec('/', 1);
136
r_ret = theme_style_override.has(name) ? Variant(theme_style_override[name]) : Variant();
137
} else if (sname.begins_with("theme_override_fonts/")) {
138
String name = sname.get_slicec('/', 1);
139
r_ret = theme_font_override.has(name) ? Variant(theme_font_override[name]) : Variant();
140
} else if (sname.begins_with("theme_override_font_sizes/")) {
141
String name = sname.get_slicec('/', 1);
142
r_ret = theme_font_size_override.has(name) ? Variant(theme_font_size_override[name]) : Variant();
143
} else if (sname.begins_with("theme_override_colors/")) {
144
String name = sname.get_slicec('/', 1);
145
r_ret = theme_color_override.has(name) ? Variant(theme_color_override[name]) : Variant();
146
} else if (sname.begins_with("theme_override_constants/")) {
147
String name = sname.get_slicec('/', 1);
148
r_ret = theme_constant_override.has(name) ? Variant(theme_constant_override[name]) : Variant();
149
} else {
150
return false;
151
}
152
153
return true;
154
}
155
156
void Window::_get_property_list(List<PropertyInfo> *p_list) const {
157
ERR_READ_THREAD_GUARD;
158
159
Ref<Theme> default_theme = ThemeDB::get_singleton()->get_default_theme();
160
161
p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Theme Overrides", "theme_override_"), PROPERTY_HINT_NONE, "theme_override_", PROPERTY_USAGE_GROUP));
162
163
{
164
List<StringName> names;
165
default_theme->get_color_list(get_class_name(), &names);
166
for (const StringName &E : names) {
167
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
168
if (theme_color_override.has(E)) {
169
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
170
}
171
172
p_list->push_back(PropertyInfo(Variant::COLOR, PNAME("theme_override_colors") + String("/") + E, PROPERTY_HINT_NONE, "", usage));
173
}
174
}
175
{
176
List<StringName> names;
177
default_theme->get_constant_list(get_class_name(), &names);
178
for (const StringName &E : names) {
179
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
180
if (theme_constant_override.has(E)) {
181
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
182
}
183
184
p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_constants") + String("/") + E, PROPERTY_HINT_RANGE, "-16384,16384", usage));
185
}
186
}
187
{
188
List<StringName> names;
189
default_theme->get_font_list(get_class_name(), &names);
190
for (const StringName &E : names) {
191
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
192
if (theme_font_override.has(E)) {
193
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
194
}
195
196
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_fonts") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "Font", usage));
197
}
198
}
199
{
200
List<StringName> names;
201
default_theme->get_font_size_list(get_class_name(), &names);
202
for (const StringName &E : names) {
203
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
204
if (theme_font_size_override.has(E)) {
205
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
206
}
207
208
p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_font_sizes") + String("/") + E, PROPERTY_HINT_RANGE, "1,256,1,or_greater,suffix:px", usage));
209
}
210
}
211
{
212
List<StringName> names;
213
default_theme->get_icon_list(get_class_name(), &names);
214
for (const StringName &E : names) {
215
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
216
if (theme_icon_override.has(E)) {
217
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
218
}
219
220
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_icons") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", usage));
221
}
222
}
223
{
224
List<StringName> names;
225
default_theme->get_stylebox_list(get_class_name(), &names);
226
for (const StringName &E : names) {
227
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
228
if (theme_style_override.has(E)) {
229
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
230
}
231
232
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_styles") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", usage));
233
}
234
}
235
}
236
237
void Window::_validate_property(PropertyInfo &p_property) const {
238
if (p_property.name == "position") {
239
if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
240
p_property.usage = PROPERTY_USAGE_NONE;
241
}
242
} else if (p_property.name == "current_screen") {
243
if (initial_position != WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN) {
244
p_property.usage = PROPERTY_USAGE_NONE;
245
}
246
} else if (Engine::get_singleton()->is_editor_hint() && p_property.name == "theme_type_variation") {
247
List<StringName> names;
248
249
ThemeDB::get_singleton()->get_default_theme()->get_type_variation_list(get_class_name(), &names);
250
251
// Iterate to find all themes.
252
Control *tmp_control = Object::cast_to<Control>(get_parent());
253
Window *tmp_window = Object::cast_to<Window>(get_parent());
254
while (tmp_control || tmp_window) {
255
// We go up and any non Control/Window will break the chain.
256
if (tmp_control) {
257
if (tmp_control->get_theme().is_valid()) {
258
tmp_control->get_theme()->get_type_variation_list(get_class_name(), &names);
259
}
260
tmp_window = Object::cast_to<Window>(tmp_control->get_parent());
261
tmp_control = Object::cast_to<Control>(tmp_control->get_parent());
262
} else { // Window.
263
if (tmp_window->get_theme().is_valid()) {
264
tmp_window->get_theme()->get_type_variation_list(get_class_name(), &names);
265
}
266
tmp_control = Object::cast_to<Control>(tmp_window->get_parent());
267
tmp_window = Object::cast_to<Window>(tmp_window->get_parent());
268
}
269
}
270
if (get_theme().is_valid()) {
271
get_theme()->get_type_variation_list(get_class_name(), &names);
272
}
273
if (ThemeDB::get_singleton()->get_project_theme().is_valid()) {
274
ThemeDB::get_singleton()->get_project_theme()->get_type_variation_list(get_class_name(), &names);
275
}
276
names.sort_custom<StringName::AlphCompare>();
277
278
Vector<StringName> unique_names;
279
String hint_string;
280
for (const StringName &E : names) {
281
// Skip duplicate values.
282
if (unique_names.has(E)) {
283
continue;
284
}
285
286
hint_string += String(E) + ",";
287
unique_names.append(E);
288
}
289
290
p_property.hint_string = hint_string;
291
}
292
}
293
294
//
295
296
Window *Window::get_from_id(DisplayServer::WindowID p_window_id) {
297
if (p_window_id == DisplayServer::INVALID_WINDOW_ID) {
298
return nullptr;
299
}
300
return ObjectDB::get_instance<Window>(DisplayServer::get_singleton()->window_get_attached_instance_id(p_window_id));
301
}
302
303
void Window::set_title(const String &p_title) {
304
ERR_MAIN_THREAD_GUARD;
305
306
title = p_title;
307
_update_displayed_title();
308
309
emit_signal("title_changed");
310
}
311
312
String Window::get_title() const {
313
ERR_READ_THREAD_GUARD_V(String());
314
return title;
315
}
316
317
String Window::get_displayed_title() const {
318
ERR_READ_THREAD_GUARD_V(String());
319
return displayed_title;
320
}
321
322
void Window::_settings_changed() {
323
if (visible && initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE && is_in_edited_scene_root()) {
324
Size2 screen_size = Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
325
position = (screen_size - size) / 2;
326
if (embedder) {
327
embedder->_sub_window_update(this);
328
}
329
}
330
}
331
332
void Window::set_initial_position(Window::WindowInitialPosition p_initial_position) {
333
ERR_MAIN_THREAD_GUARD;
334
335
initial_position = p_initial_position;
336
_settings_changed();
337
notify_property_list_changed();
338
}
339
340
Window::WindowInitialPosition Window::get_initial_position() const {
341
ERR_READ_THREAD_GUARD_V(WINDOW_INITIAL_POSITION_ABSOLUTE);
342
return initial_position;
343
}
344
345
void Window::set_current_screen(int p_screen) {
346
ERR_MAIN_THREAD_GUARD;
347
348
current_screen = p_screen;
349
if (window_id == DisplayServer::INVALID_WINDOW_ID) {
350
return;
351
}
352
DisplayServer::get_singleton()->window_set_current_screen(p_screen, window_id);
353
}
354
355
int Window::get_current_screen() const {
356
ERR_READ_THREAD_GUARD_V(0);
357
358
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
359
current_screen = DisplayServer::get_singleton()->window_get_current_screen(window_id);
360
}
361
return current_screen;
362
}
363
364
void Window::set_position(const Point2i &p_position) {
365
ERR_MAIN_THREAD_GUARD;
366
367
position = p_position;
368
369
if (embedder) {
370
embedder->_sub_window_update(this);
371
372
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
373
DisplayServer::get_singleton()->window_set_position(p_position, window_id);
374
}
375
}
376
377
Point2i Window::get_position() const {
378
ERR_READ_THREAD_GUARD_V(Point2i());
379
380
return position;
381
}
382
383
void Window::move_to_center() {
384
ERR_MAIN_THREAD_GUARD;
385
ERR_FAIL_COND(!is_inside_tree());
386
387
Rect2 parent_rect;
388
389
if (is_embedded()) {
390
parent_rect = get_embedder()->get_visible_rect();
391
} else {
392
int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(get_window_id());
393
parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
394
parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
395
}
396
397
if (parent_rect != Rect2()) {
398
set_position(parent_rect.position + (parent_rect.size - get_size()) / 2);
399
}
400
}
401
402
void Window::set_size(const Size2i &p_size) {
403
ERR_MAIN_THREAD_GUARD;
404
#if defined(ANDROID_ENABLED)
405
if (!get_parent() && is_inside_tree()) {
406
// Can't set root window size on Android.
407
return;
408
}
409
#endif
410
411
size = p_size;
412
_update_window_size();
413
_settings_changed();
414
}
415
416
Size2i Window::get_size() const {
417
ERR_READ_THREAD_GUARD_V(Size2i());
418
return size;
419
}
420
421
void Window::reset_size() {
422
ERR_MAIN_THREAD_GUARD;
423
set_size(Size2i());
424
}
425
426
Point2i Window::get_position_with_decorations() const {
427
ERR_READ_THREAD_GUARD_V(Point2i());
428
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
429
return DisplayServer::get_singleton()->window_get_position_with_decorations(window_id);
430
}
431
if (visible && is_embedded() && !get_flag(Window::FLAG_BORDERLESS)) {
432
Size2 border_offset;
433
if (theme_cache.embedded_border.is_valid()) {
434
border_offset = theme_cache.embedded_border->get_offset();
435
}
436
if (theme_cache.embedded_unfocused_border.is_valid()) {
437
border_offset = border_offset.max(theme_cache.embedded_unfocused_border->get_offset());
438
}
439
return position - border_offset;
440
}
441
return position;
442
}
443
444
Size2i Window::get_size_with_decorations() const {
445
ERR_READ_THREAD_GUARD_V(Size2i());
446
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
447
return DisplayServer::get_singleton()->window_get_size_with_decorations(window_id);
448
}
449
if (visible && is_embedded() && !get_flag(Window::FLAG_BORDERLESS)) {
450
Size2 border_size;
451
if (theme_cache.embedded_border.is_valid()) {
452
border_size = theme_cache.embedded_border->get_minimum_size();
453
}
454
if (theme_cache.embedded_unfocused_border.is_valid()) {
455
border_size = border_size.max(theme_cache.embedded_unfocused_border->get_minimum_size());
456
}
457
return size + border_size;
458
}
459
return size;
460
}
461
462
Size2i Window::_clamp_limit_size(const Size2i &p_limit_size) {
463
// Force window limits to respect size limitations of rendering server.
464
Size2i max_window_size = RS::get_singleton()->get_maximum_viewport_size();
465
if (max_window_size != Size2i()) {
466
return p_limit_size.clamp(Vector2i(), max_window_size);
467
} else {
468
return p_limit_size.maxi(0);
469
}
470
}
471
472
void Window::_validate_limit_size() {
473
// When max_size is invalid, max_size_used falls back to respect size limitations of rendering server.
474
bool max_size_valid = (max_size.x > 0 || max_size.y > 0) && max_size.x >= min_size.x && max_size.y >= min_size.y;
475
max_size_used = max_size_valid ? max_size : RS::get_singleton()->get_maximum_viewport_size();
476
}
477
478
void Window::set_max_size(const Size2i &p_max_size) {
479
ERR_MAIN_THREAD_GUARD;
480
#if defined(ANDROID_ENABLED)
481
if (!get_parent() && is_inside_tree()) {
482
// Can't set root window size on Android.
483
return;
484
}
485
#endif
486
Size2i max_size_clamped = _clamp_limit_size(p_max_size);
487
if (max_size == max_size_clamped) {
488
return;
489
}
490
max_size = max_size_clamped;
491
492
_validate_limit_size();
493
_update_window_size();
494
}
495
496
Size2i Window::get_max_size() const {
497
ERR_READ_THREAD_GUARD_V(Size2i());
498
return max_size;
499
}
500
501
void Window::set_min_size(const Size2i &p_min_size) {
502
ERR_MAIN_THREAD_GUARD;
503
#if defined(ANDROID_ENABLED)
504
if (!get_parent() && is_inside_tree()) {
505
// Can't set root window size on Android.
506
return;
507
}
508
#endif
509
Size2i min_size_clamped = _clamp_limit_size(p_min_size);
510
if (min_size == min_size_clamped) {
511
return;
512
}
513
min_size = min_size_clamped;
514
515
_validate_limit_size();
516
_update_window_size();
517
}
518
519
Size2i Window::get_min_size() const {
520
ERR_READ_THREAD_GUARD_V(Size2i());
521
return min_size;
522
}
523
524
void Window::set_mode(Mode p_mode) {
525
ERR_MAIN_THREAD_GUARD;
526
mode = p_mode;
527
528
if (embedder) {
529
embedder->_sub_window_update(this);
530
531
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
532
DisplayServer::get_singleton()->window_set_mode(DisplayServer::WindowMode(p_mode), window_id);
533
}
534
}
535
536
Window::Mode Window::get_mode() const {
537
ERR_READ_THREAD_GUARD_V(MODE_WINDOWED);
538
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
539
mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
540
}
541
return mode;
542
}
543
544
void Window::set_flag(Flags p_flag, bool p_enabled) {
545
ERR_MAIN_THREAD_GUARD;
546
ERR_FAIL_INDEX(p_flag, FLAG_MAX);
547
flags[p_flag] = p_enabled;
548
549
if (p_flag == FLAG_TRANSPARENT) {
550
set_transparent_background(p_enabled);
551
}
552
553
if (embedder) {
554
embedder->_sub_window_update(this);
555
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
556
if (!is_in_edited_scene_root()) {
557
DisplayServer::get_singleton()->window_set_flag(DisplayServer::WindowFlags(p_flag), p_enabled, window_id);
558
}
559
}
560
}
561
562
bool Window::get_flag(Flags p_flag) const {
563
ERR_READ_THREAD_GUARD_V(false);
564
ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
565
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
566
if (!is_in_edited_scene_root()) {
567
flags[p_flag] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(p_flag), window_id);
568
}
569
}
570
return flags[p_flag];
571
}
572
573
bool Window::is_popup() const {
574
return get_flag(Window::FLAG_POPUP) || get_flag(Window::FLAG_NO_FOCUS);
575
}
576
577
bool Window::is_maximize_allowed() const {
578
ERR_READ_THREAD_GUARD_V(false);
579
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
580
return DisplayServer::get_singleton()->window_is_maximize_allowed(window_id);
581
}
582
return true;
583
}
584
585
void Window::request_attention() {
586
ERR_MAIN_THREAD_GUARD;
587
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
588
DisplayServer::get_singleton()->window_request_attention(window_id);
589
}
590
}
591
592
#ifndef DISABLE_DEPRECATED
593
void Window::move_to_foreground() {
594
WARN_DEPRECATED_MSG(R"*(The "move_to_foreground()" method is deprecated, use "grab_focus()" instead.)*");
595
grab_focus();
596
}
597
#endif // DISABLE_DEPRECATED
598
599
bool Window::can_draw() const {
600
ERR_READ_THREAD_GUARD_V(false);
601
if (!is_inside_tree()) {
602
return false;
603
}
604
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
605
return DisplayServer::get_singleton()->window_can_draw(window_id);
606
}
607
608
return visible;
609
}
610
611
void Window::set_ime_active(bool p_active) {
612
ERR_MAIN_THREAD_GUARD;
613
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
614
DisplayServer::get_singleton()->window_set_ime_active(p_active, window_id);
615
}
616
}
617
618
void Window::set_ime_position(const Point2i &p_pos) {
619
ERR_MAIN_THREAD_GUARD;
620
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
621
DisplayServer::get_singleton()->window_set_ime_position(p_pos, window_id);
622
}
623
}
624
625
bool Window::is_embedded() const {
626
ERR_READ_THREAD_GUARD_V(false);
627
return get_embedder() != nullptr;
628
}
629
630
bool Window::is_in_edited_scene_root() const {
631
ERR_READ_THREAD_GUARD_V(false);
632
#ifdef TOOLS_ENABLED
633
return is_part_of_edited_scene();
634
#else
635
return false;
636
#endif
637
}
638
639
void Window::_make_window() {
640
ERR_FAIL_COND(window_id != DisplayServer::INVALID_WINDOW_ID);
641
642
if (transient && transient_to_focused) {
643
_make_transient();
644
}
645
646
uint32_t f = 0;
647
for (int i = 0; i < FLAG_MAX; i++) {
648
if (flags[i]) {
649
f |= (1 << i);
650
}
651
}
652
653
DisplayServer::VSyncMode vsync_mode = DisplayServer::get_singleton()->window_get_vsync_mode(DisplayServer::MAIN_WINDOW_ID);
654
Rect2i window_rect;
655
if (initial_position == WINDOW_INITIAL_POSITION_ABSOLUTE) {
656
window_rect = Rect2i(position, size);
657
} else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN) {
658
window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_PRIMARY) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_PRIMARY) - size) / 2, size);
659
} else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN) {
660
window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_OF_MAIN_WINDOW) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_OF_MAIN_WINDOW) - size) / 2, size);
661
} else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN) {
662
window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(current_screen) + (DisplayServer::get_singleton()->screen_get_size(current_screen) - size) / 2, size);
663
} else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS) {
664
window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_WITH_MOUSE_FOCUS) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_WITH_MOUSE_FOCUS) - size) / 2, size);
665
} else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS) {
666
window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_WITH_KEYBOARD_FOCUS) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_WITH_KEYBOARD_FOCUS) - size) / 2, size);
667
}
668
669
window_id = DisplayServer::get_singleton()->create_sub_window(DisplayServer::WindowMode(mode), vsync_mode, f, window_rect, is_in_edited_scene_root() ? false : exclusive, transient_parent ? transient_parent->window_id : DisplayServer::INVALID_WINDOW_ID);
670
ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
671
DisplayServer::get_singleton()->window_set_max_size(Size2i(), window_id);
672
DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id);
673
DisplayServer::get_singleton()->window_set_mouse_passthrough(mpath, window_id);
674
DisplayServer::get_singleton()->window_set_title(displayed_title, window_id);
675
DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
676
677
_update_window_size();
678
679
if (transient_parent) {
680
for (const Window *E : transient_children) {
681
if (E->window_id != DisplayServer::INVALID_WINDOW_ID) {
682
DisplayServer::get_singleton()->window_set_transient(E->window_id, transient_parent->window_id);
683
}
684
}
685
}
686
687
_update_window_callbacks();
688
689
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
690
DisplayServer::get_singleton()->show_window(window_id);
691
}
692
693
void Window::_update_from_window() {
694
ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
695
mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
696
for (int i = 0; i < FLAG_MAX; i++) {
697
flags[i] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(i), window_id);
698
}
699
}
700
701
void Window::_clear_window() {
702
ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
703
704
bool had_focus = has_focus();
705
706
if (transient_parent && transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID) {
707
DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
708
}
709
710
for (const Window *E : transient_children) {
711
if (E->window_id != DisplayServer::INVALID_WINDOW_ID) {
712
DisplayServer::get_singleton()->window_set_transient(E->window_id, DisplayServer::INVALID_WINDOW_ID);
713
}
714
}
715
716
_update_from_window();
717
718
DisplayServer::get_singleton()->delete_sub_window(window_id);
719
window_id = DisplayServer::INVALID_WINDOW_ID;
720
721
// If closing window was focused and has a parent, return focus.
722
if (had_focus && transient_parent) {
723
transient_parent->grab_focus();
724
}
725
726
_update_viewport_size();
727
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
728
729
if (transient && transient_to_focused) {
730
_clear_transient();
731
}
732
}
733
734
void Window::_rect_changed_callback(const Rect2i &p_callback) {
735
//we must always accept this as the truth
736
if (size == p_callback.size && position == p_callback.position) {
737
return;
738
}
739
740
if (position != p_callback.position) {
741
position = p_callback.position;
742
_propagate_window_notification(this, NOTIFICATION_WM_POSITION_CHANGED);
743
}
744
745
if (size != p_callback.size) {
746
size = p_callback.size;
747
_update_viewport_size();
748
}
749
if (window_id != DisplayServer::INVALID_WINDOW_ID && !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS)) {
750
Vector2 sz_out = DisplayServer::get_singleton()->window_get_size_with_decorations(window_id);
751
Vector2 pos_out = DisplayServer::get_singleton()->window_get_position_with_decorations(window_id);
752
Vector2 sz_in = DisplayServer::get_singleton()->window_get_size(window_id);
753
Vector2 pos_in = DisplayServer::get_singleton()->window_get_position(window_id);
754
DisplayServer::get_singleton()->accessibility_set_window_rect(window_id, Rect2(pos_out, sz_out), Rect2(pos_in, sz_in));
755
}
756
queue_accessibility_update();
757
}
758
759
void Window::_propagate_window_notification(Node *p_node, int p_notification) {
760
p_node->notification(p_notification);
761
for (int i = 0; i < p_node->get_child_count(); i++) {
762
Node *child = p_node->get_child(i);
763
Window *window = Object::cast_to<Window>(child);
764
if (window) {
765
continue;
766
}
767
_propagate_window_notification(child, p_notification);
768
}
769
}
770
771
void Window::_event_callback(DisplayServer::WindowEvent p_event) {
772
switch (p_event) {
773
case DisplayServer::WINDOW_EVENT_MOUSE_ENTER: {
774
if (!is_inside_tree()) {
775
return;
776
}
777
Window *root = get_tree()->get_root();
778
if (mouse_in_window && root->gui.windowmanager_window_over == this) {
779
return;
780
}
781
if (root->gui.windowmanager_window_over) {
782
#ifdef DEV_ENABLED
783
WARN_PRINT_ONCE("Entering a window while a window is hovered should never happen in DisplayServer.");
784
#endif // DEV_ENABLED
785
root->gui.windowmanager_window_over->_event_callback(DisplayServer::WINDOW_EVENT_MOUSE_EXIT);
786
}
787
_propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
788
root->gui.windowmanager_window_over = this;
789
mouse_in_window = true;
790
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CURSOR_SHAPE)) {
791
DisplayServer::get_singleton()->cursor_set_shape(DisplayServer::CURSOR_ARROW); //restore cursor shape
792
}
793
} break;
794
case DisplayServer::WINDOW_EVENT_MOUSE_EXIT: {
795
if (!is_inside_tree()) {
796
return;
797
}
798
// Ensure keeping the order of input events and window events when input events are buffered or accumulated.
799
Input::get_singleton()->flush_buffered_events();
800
801
Window *root = get_tree()->get_root();
802
if (!root->gui.windowmanager_window_over) {
803
#ifdef DEV_ENABLED
804
WARN_PRINT_ONCE("Exiting a window while no window is hovered should never happen in DisplayServer.");
805
#endif // DEV_ENABLED
806
return;
807
}
808
mouse_in_window = false;
809
root->gui.windowmanager_window_over->_mouse_leave_viewport();
810
root->gui.windowmanager_window_over = nullptr;
811
_propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
812
} break;
813
case DisplayServer::WINDOW_EVENT_FOCUS_IN: {
814
focused = true;
815
focused_window = this;
816
_propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_IN);
817
emit_signal(SceneStringName(focus_entered));
818
} break;
819
case DisplayServer::WINDOW_EVENT_FOCUS_OUT: {
820
focused = false;
821
if (focused_window == this) {
822
focused_window = nullptr;
823
}
824
_propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_OUT);
825
emit_signal(SceneStringName(focus_exited));
826
} break;
827
case DisplayServer::WINDOW_EVENT_CLOSE_REQUEST: {
828
if (exclusive_child != nullptr) {
829
break; //has an exclusive child, can't get events until child is closed
830
}
831
_propagate_window_notification(this, NOTIFICATION_WM_CLOSE_REQUEST);
832
emit_signal(SNAME("close_requested"));
833
} break;
834
case DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST: {
835
_propagate_window_notification(this, NOTIFICATION_WM_GO_BACK_REQUEST);
836
emit_signal(SNAME("go_back_requested"));
837
} break;
838
case DisplayServer::WINDOW_EVENT_DPI_CHANGE: {
839
_update_viewport_size();
840
_propagate_window_notification(this, NOTIFICATION_WM_DPI_CHANGE);
841
emit_signal(SNAME("dpi_changed"));
842
} break;
843
case DisplayServer::WINDOW_EVENT_TITLEBAR_CHANGE: {
844
emit_signal(SNAME("titlebar_changed"));
845
} break;
846
case DisplayServer::WINDOW_EVENT_FORCE_CLOSE: {
847
hide();
848
} break;
849
}
850
}
851
852
void Window::update_mouse_cursor_state() {
853
ERR_MAIN_THREAD_GUARD;
854
// Update states based on mouse cursor position.
855
// This includes updated mouse_enter or mouse_exit signals or the current mouse cursor shape.
856
// These details are set in Viewport::_gui_input_event. To instantly
857
// see the changes in the viewport, we need to trigger a mouse motion event.
858
// This function should be called whenever scene tree changes affect the mouse cursor.
859
Ref<InputEventMouseMotion> mm;
860
Vector2 pos = get_mouse_position();
861
Transform2D xform = get_global_canvas_transform().affine_inverse();
862
mm.instantiate();
863
mm->set_position(pos);
864
mm->set_global_position(xform.xform(pos));
865
mm->set_device(InputEvent::DEVICE_ID_INTERNAL);
866
push_input(mm, true);
867
}
868
869
void Window::show() {
870
ERR_MAIN_THREAD_GUARD;
871
set_visible(true);
872
}
873
874
void Window::hide() {
875
ERR_MAIN_THREAD_GUARD;
876
set_visible(false);
877
}
878
879
void Window::_accessibility_notify_enter(Node *p_node) {
880
p_node->queue_accessibility_update();
881
882
if (p_node != this) {
883
const Window *window = Object::cast_to<Window>(p_node);
884
if (window) {
885
return;
886
}
887
}
888
889
for (int i = 0; i < p_node->get_child_count(); i++) {
890
_accessibility_notify_enter(p_node->get_child(i));
891
}
892
}
893
894
void Window::_accessibility_notify_exit(Node *p_node) {
895
p_node->notification(Node::NOTIFICATION_ACCESSIBILITY_INVALIDATE);
896
897
if (p_node != this) {
898
const Window *window = Object::cast_to<Window>(p_node);
899
if (window) {
900
return;
901
}
902
}
903
904
for (int i = 0; i < p_node->get_child_count(); i++) {
905
_accessibility_notify_exit(p_node->get_child(i));
906
}
907
}
908
909
void Window::set_visible(bool p_visible) {
910
ERR_MAIN_THREAD_GUARD;
911
if (visible == p_visible) {
912
return;
913
}
914
915
if (!is_inside_tree()) {
916
visible = p_visible;
917
return;
918
}
919
920
ERR_FAIL_NULL_MSG(get_parent(), "Can't change visibility of main window.");
921
922
visible = p_visible;
923
924
// Stop any queued resizing, as the window will be resized right now.
925
updating_child_controls = false;
926
927
Viewport *embedder_vp = get_embedder();
928
929
if (!embedder_vp) {
930
if (!p_visible && window_id != DisplayServer::INVALID_WINDOW_ID) {
931
_clear_window();
932
}
933
if (p_visible && window_id == DisplayServer::INVALID_WINDOW_ID) {
934
_make_window();
935
}
936
} else {
937
if (visible) {
938
embedder = embedder_vp;
939
if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
940
if (is_in_edited_scene_root()) {
941
Size2 screen_size = Size2(GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_width"), GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_height"));
942
position = (screen_size - size) / 2;
943
} else {
944
position = (embedder->get_visible_rect().size - size) / 2;
945
}
946
}
947
embedder->_sub_window_register(this);
948
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
949
} else {
950
embedder->_sub_window_remove(this);
951
embedder = nullptr;
952
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
953
}
954
_update_window_size();
955
}
956
957
if (visible) {
958
if (get_tree() && get_tree()->is_accessibility_supported()) {
959
get_tree()->_accessibility_force_update();
960
_accessibility_notify_enter(this);
961
}
962
} else {
963
if (get_tree() && get_tree()->is_accessibility_supported()) {
964
_accessibility_notify_exit(this);
965
}
966
focused = false;
967
if (focused_window == this) {
968
focused_window = nullptr;
969
}
970
}
971
if (get_parent()) {
972
get_parent()->queue_accessibility_update();
973
}
974
if (embedder) {
975
embedder->queue_accessibility_update();
976
}
977
978
notification(NOTIFICATION_VISIBILITY_CHANGED);
979
emit_signal(SceneStringName(visibility_changed));
980
981
RS::get_singleton()->viewport_set_active(get_viewport_rid(), visible);
982
983
//update transient exclusive
984
if (transient_parent) {
985
_set_transient_exclusive_child(true);
986
}
987
}
988
989
void Window::_clear_transient() {
990
if (transient_parent) {
991
if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
992
DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
993
}
994
transient_parent->transient_children.erase(this);
995
if (transient_parent->exclusive_child == this) {
996
transient_parent->exclusive_child = nullptr;
997
}
998
transient_parent = nullptr;
999
}
1000
}
1001
1002
void Window::_make_transient() {
1003
if (!get_parent()) {
1004
//main window, can't be transient
1005
return;
1006
}
1007
//find transient parent
1008
1009
Window *window = nullptr;
1010
1011
if (!is_embedded() && transient_to_focused) {
1012
DisplayServer::WindowID focused_window_id = DisplayServer::get_singleton()->get_focused_window();
1013
if (focused_window_id != DisplayServer::INVALID_WINDOW_ID) {
1014
window = Window::get_from_id(focused_window_id);
1015
}
1016
}
1017
1018
if (!window) {
1019
Viewport *vp = get_parent()->get_viewport();
1020
while (vp) {
1021
window = Object::cast_to<Window>(vp);
1022
if (window) {
1023
break;
1024
}
1025
if (!vp->get_parent()) {
1026
break;
1027
}
1028
1029
vp = vp->get_parent()->get_viewport();
1030
}
1031
}
1032
1033
if (window) {
1034
transient_parent = window;
1035
window->transient_children.insert(this);
1036
_set_transient_exclusive_child();
1037
}
1038
1039
//see if we can make transient
1040
if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
1041
DisplayServer::get_singleton()->window_set_transient(window_id, transient_parent->window_id);
1042
}
1043
}
1044
1045
void Window::_set_transient_exclusive_child(bool p_clear_invalid) {
1046
if (exclusive && visible && is_inside_tree()) {
1047
if (!is_in_edited_scene_root()) {
1048
// Transient parent has another exclusive child.
1049
if (transient_parent->exclusive_child && transient_parent->exclusive_child != this) {
1050
ERR_PRINT(vformat("Attempting to make child window exclusive, but the parent window already has another exclusive child. This window: %s, parent window: %s, current exclusive child window: %s", get_description(), transient_parent->get_description(), transient_parent->exclusive_child->get_description()));
1051
}
1052
transient_parent->exclusive_child = this;
1053
}
1054
} else if (p_clear_invalid) {
1055
if (transient_parent->exclusive_child == this) {
1056
transient_parent->exclusive_child = nullptr;
1057
}
1058
}
1059
}
1060
1061
void Window::set_transient(bool p_transient) {
1062
ERR_MAIN_THREAD_GUARD;
1063
if (transient == p_transient) {
1064
return;
1065
}
1066
1067
transient = p_transient;
1068
1069
if (!is_inside_tree()) {
1070
return;
1071
}
1072
1073
if (transient) {
1074
if (!transient_to_focused) {
1075
_make_transient();
1076
}
1077
} else {
1078
_clear_transient();
1079
}
1080
}
1081
1082
bool Window::is_transient() const {
1083
return transient;
1084
}
1085
1086
void Window::set_transient_to_focused(bool p_transient_to_focused) {
1087
ERR_MAIN_THREAD_GUARD;
1088
if (transient_to_focused == p_transient_to_focused) {
1089
return;
1090
}
1091
1092
transient_to_focused = p_transient_to_focused;
1093
}
1094
1095
bool Window::is_transient_to_focused() const {
1096
ERR_READ_THREAD_GUARD_V(false);
1097
return transient_to_focused;
1098
}
1099
1100
void Window::set_exclusive(bool p_exclusive) {
1101
ERR_MAIN_THREAD_GUARD;
1102
if (exclusive == p_exclusive) {
1103
return;
1104
}
1105
1106
exclusive = p_exclusive;
1107
1108
if (!embedder && window_id != DisplayServer::INVALID_WINDOW_ID) {
1109
if (is_in_edited_scene_root()) {
1110
DisplayServer::get_singleton()->window_set_exclusive(window_id, false);
1111
} else {
1112
DisplayServer::get_singleton()->window_set_exclusive(window_id, exclusive);
1113
}
1114
}
1115
1116
if (transient_parent) {
1117
_set_transient_exclusive_child(true);
1118
}
1119
}
1120
1121
bool Window::is_exclusive() const {
1122
ERR_READ_THREAD_GUARD_V(false);
1123
return exclusive;
1124
}
1125
1126
bool Window::is_visible() const {
1127
ERR_READ_THREAD_GUARD_V(false);
1128
return visible;
1129
}
1130
1131
Size2i Window::_clamp_window_size(const Size2i &p_size) {
1132
Size2i window_size_clamped = p_size;
1133
Size2 minsize = get_clamped_minimum_size();
1134
window_size_clamped = window_size_clamped.max(minsize);
1135
1136
if (max_size_used != Size2i()) {
1137
window_size_clamped = window_size_clamped.min(max_size_used);
1138
}
1139
1140
return window_size_clamped;
1141
}
1142
1143
void Window::_update_window_size() {
1144
Size2i size_limit = get_clamped_minimum_size();
1145
if (!embedder && window_id != DisplayServer::INVALID_WINDOW_ID && keep_title_visible) {
1146
Size2i title_size = DisplayServer::get_singleton()->window_get_title_size(displayed_title, window_id);
1147
size_limit = size_limit.max(title_size);
1148
}
1149
1150
size = size.max(size_limit);
1151
1152
bool reset_min_first = false;
1153
1154
if (max_size_used != Size2i()) {
1155
// Force window size to respect size limitations of max_size_used.
1156
size = size.min(max_size_used);
1157
1158
if (size_limit.x > max_size_used.x) {
1159
size_limit.x = max_size_used.x;
1160
reset_min_first = true;
1161
}
1162
if (size_limit.y > max_size_used.y) {
1163
size_limit.y = max_size_used.y;
1164
reset_min_first = true;
1165
}
1166
}
1167
1168
if (embedder) {
1169
size = size.maxi(1);
1170
1171
embedder->_sub_window_update(this);
1172
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
1173
// When main window embedded in the editor, we can't resize the main window.
1174
if (window_id != DisplayServer::MAIN_WINDOW_ID || !Engine::get_singleton()->is_embedded_in_editor()) {
1175
if (reset_min_first && wrap_controls) {
1176
// Avoid an error if setting max_size to a value between min_size and the previous size_limit.
1177
DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id);
1178
}
1179
1180
DisplayServer::get_singleton()->window_set_max_size(max_size_used, window_id);
1181
DisplayServer::get_singleton()->window_set_min_size(size_limit, window_id);
1182
DisplayServer::get_singleton()->window_set_size(size, window_id);
1183
}
1184
}
1185
1186
//update the viewport
1187
_update_viewport_size();
1188
}
1189
1190
void Window::_update_viewport_size() {
1191
//update the viewport part
1192
1193
Size2i final_size;
1194
Size2 final_size_override;
1195
Rect2i attach_to_screen_rect(Point2i(), size);
1196
window_transform = Transform2D();
1197
1198
if (content_scale_stretch == Window::CONTENT_SCALE_STRETCH_INTEGER) {
1199
// We always want to make sure that the content scale factor is a whole
1200
// number, else there will be pixel wobble no matter what.
1201
content_scale_factor = Math::floor(content_scale_factor);
1202
1203
// A content scale factor of zero is pretty useless.
1204
if (content_scale_factor < 1) {
1205
content_scale_factor = 1;
1206
}
1207
}
1208
1209
if (content_scale_mode == CONTENT_SCALE_MODE_DISABLED || content_scale_size.x == 0 || content_scale_size.y == 0) {
1210
final_size = size;
1211
final_size_override = Size2(size) / content_scale_factor;
1212
} else {
1213
//actual screen video mode
1214
Size2 video_mode = size;
1215
Size2 desired_res = content_scale_size;
1216
1217
Size2 viewport_size;
1218
Size2 screen_size;
1219
1220
float viewport_aspect = desired_res.aspect();
1221
float video_mode_aspect = video_mode.aspect();
1222
1223
if (content_scale_aspect == CONTENT_SCALE_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
1224
//same aspect or ignore aspect
1225
viewport_size = desired_res;
1226
screen_size = video_mode;
1227
} else if (viewport_aspect < video_mode_aspect) {
1228
// screen ratio is smaller vertically
1229
1230
if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_HEIGHT || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
1231
//will stretch horizontally
1232
viewport_size.x = desired_res.y * video_mode_aspect;
1233
viewport_size.y = desired_res.y;
1234
screen_size = video_mode;
1235
1236
} else {
1237
//will need black bars
1238
viewport_size = desired_res;
1239
screen_size.x = video_mode.y * viewport_aspect;
1240
screen_size.y = video_mode.y;
1241
}
1242
} else {
1243
//screen ratio is smaller horizontally
1244
if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_WIDTH || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
1245
//will stretch horizontally
1246
viewport_size.x = desired_res.x;
1247
viewport_size.y = desired_res.x / video_mode_aspect;
1248
screen_size = video_mode;
1249
1250
} else {
1251
//will need black bars
1252
viewport_size = desired_res;
1253
screen_size.x = video_mode.x;
1254
screen_size.y = video_mode.x / viewport_aspect;
1255
}
1256
}
1257
1258
screen_size = screen_size.floor();
1259
viewport_size = viewport_size.floor();
1260
1261
if (content_scale_stretch == Window::CONTENT_SCALE_STRETCH_INTEGER) {
1262
Size2i screen_scale = (screen_size / viewport_size).floor();
1263
int scale_factor = MIN(screen_scale.x, screen_scale.y);
1264
1265
if (scale_factor < 1) {
1266
scale_factor = 1;
1267
}
1268
1269
screen_size = viewport_size * scale_factor;
1270
}
1271
1272
Size2 margin;
1273
Size2 offset;
1274
1275
if (screen_size.x < video_mode.x) {
1276
margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
1277
offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
1278
}
1279
1280
if (screen_size.y < video_mode.y) {
1281
margin.y = Math::round((video_mode.y - screen_size.y) / 2.0);
1282
offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
1283
}
1284
1285
switch (content_scale_mode) {
1286
case CONTENT_SCALE_MODE_DISABLED: {
1287
} break;
1288
case CONTENT_SCALE_MODE_CANVAS_ITEMS: {
1289
final_size = screen_size;
1290
final_size_override = viewport_size / content_scale_factor;
1291
attach_to_screen_rect = Rect2(margin, screen_size);
1292
1293
window_transform.translate_local(margin);
1294
} break;
1295
case CONTENT_SCALE_MODE_VIEWPORT: {
1296
final_size = (viewport_size / content_scale_factor).floor();
1297
attach_to_screen_rect = Rect2(margin, screen_size);
1298
1299
window_transform.translate_local(margin);
1300
if (final_size.x != 0 && final_size.y != 0) {
1301
Transform2D scale_transform;
1302
scale_transform.scale(Vector2(attach_to_screen_rect.size) / Vector2(final_size));
1303
window_transform *= scale_transform;
1304
}
1305
} break;
1306
}
1307
}
1308
1309
bool allocate = is_inside_tree() && visible && (window_id != DisplayServer::INVALID_WINDOW_ID || embedder != nullptr);
1310
_set_size(final_size, final_size_override, allocate);
1311
1312
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
1313
RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), attach_to_screen_rect, window_id);
1314
} else if (!is_embedded()) {
1315
RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), Rect2i(), DisplayServer::INVALID_WINDOW_ID);
1316
}
1317
1318
notification(NOTIFICATION_WM_SIZE_CHANGED);
1319
1320
if (embedder) {
1321
float scale = MIN(embedder->stretch_transform.get_scale().width, embedder->stretch_transform.get_scale().height);
1322
Viewport::set_oversampling_override(scale);
1323
Size2 s = Size2(final_size.width * scale, final_size.height * scale).ceil();
1324
RS::get_singleton()->viewport_set_global_canvas_transform(get_viewport_rid(), global_canvas_transform * scale * content_scale_factor);
1325
RS::get_singleton()->viewport_set_size(get_viewport_rid(), s.width, s.height);
1326
embedder->_sub_window_update(this);
1327
}
1328
}
1329
1330
void Window::_update_window_callbacks() {
1331
DisplayServer::get_singleton()->window_set_rect_changed_callback(callable_mp(this, &Window::_rect_changed_callback), window_id);
1332
DisplayServer::get_singleton()->window_set_window_event_callback(callable_mp(this, &Window::_event_callback), window_id);
1333
DisplayServer::get_singleton()->window_set_input_event_callback(callable_mp(this, &Window::_window_input), window_id);
1334
DisplayServer::get_singleton()->window_set_input_text_callback(callable_mp(this, &Window::_window_input_text), window_id);
1335
DisplayServer::get_singleton()->window_set_drop_files_callback(callable_mp(this, &Window::_window_drop_files), window_id);
1336
}
1337
1338
void Window::set_force_native(bool p_force_native) {
1339
if (force_native == p_force_native) {
1340
return;
1341
}
1342
if (is_visible() && !is_in_edited_scene_root()) {
1343
ERR_FAIL_MSG("Can't change \"force_native\" while a window is displayed. Consider hiding window before changing this value.");
1344
}
1345
if (window_id == DisplayServer::MAIN_WINDOW_ID) {
1346
return;
1347
}
1348
force_native = p_force_native;
1349
if (!is_in_edited_scene_root() && is_inside_tree() && get_tree()->get_root()->is_embedding_subwindows()) {
1350
set_embedding_subwindows(force_native);
1351
}
1352
}
1353
1354
bool Window::get_force_native() const {
1355
return force_native;
1356
}
1357
1358
Viewport *Window::get_embedder() const {
1359
ERR_READ_THREAD_GUARD_V(nullptr);
1360
if (force_native && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SUBWINDOWS) && !is_in_edited_scene_root()) {
1361
return nullptr;
1362
}
1363
1364
Viewport *vp = get_parent_viewport();
1365
1366
while (vp) {
1367
if (vp->is_embedding_subwindows()) {
1368
return vp;
1369
}
1370
1371
if (vp->get_parent()) {
1372
vp = vp->get_parent()->get_viewport();
1373
} else {
1374
vp = nullptr;
1375
}
1376
}
1377
return nullptr;
1378
}
1379
1380
RID Window::get_accessibility_element() const {
1381
if (!visible || is_part_of_edited_scene()) {
1382
return RID();
1383
}
1384
if (get_embedder() || is_popup()) {
1385
return Node::get_accessibility_element();
1386
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
1387
return DisplayServer::get_singleton()->accessibility_get_window_root(window_id);
1388
} else {
1389
return RID();
1390
}
1391
}
1392
1393
RID Window::get_focused_accessibility_element() const {
1394
if (window_id == DisplayServer::MAIN_WINDOW_ID) {
1395
if (get_child_count() > 0) {
1396
return get_child(0)->get_focused_accessibility_element(); // Try scene tree root node.
1397
}
1398
}
1399
return Node::get_focused_accessibility_element();
1400
}
1401
1402
void Window::_notification(int p_what) {
1403
ERR_MAIN_THREAD_GUARD;
1404
switch (p_what) {
1405
case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
1406
accessibility_title_element = RID();
1407
accessibility_announcement_element = RID();
1408
} break;
1409
1410
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
1411
RID ae = get_accessibility_element();
1412
ERR_FAIL_COND(ae.is_null());
1413
1414
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_WINDOW);
1415
if (accessibility_name.is_empty()) {
1416
DisplayServer::get_singleton()->accessibility_update_set_name(ae, displayed_title);
1417
} else {
1418
DisplayServer::get_singleton()->accessibility_update_set_name(ae, accessibility_name);
1419
}
1420
DisplayServer::get_singleton()->accessibility_update_set_description(ae, accessibility_description);
1421
DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_MODAL, exclusive);
1422
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &Window::_accessibility_action_grab_focus));
1423
DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, !visible);
1424
1425
if (get_embedder() || is_popup()) {
1426
Control *parent_ctrl = Object::cast_to<Control>(get_parent());
1427
Transform2D parent_tr = parent_ctrl ? parent_ctrl->get_global_transform() : Transform2D();
1428
Transform2D tr;
1429
if (window_id == DisplayServer::INVALID_WINDOW_ID) {
1430
tr.set_origin(position);
1431
} else {
1432
Window *np = get_non_popup_window();
1433
if (np) {
1434
tr.set_origin(get_position() - np->get_position());
1435
}
1436
}
1437
DisplayServer::get_singleton()->accessibility_update_set_transform(ae, parent_tr.affine_inverse() * tr);
1438
DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, Rect2(Point2(), size));
1439
1440
if (accessibility_title_element.is_null()) {
1441
accessibility_title_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_TITLE_BAR);
1442
}
1443
1444
int w = get_theme_constant(SNAME("title_height"));
1445
DisplayServer::get_singleton()->accessibility_update_set_name(accessibility_title_element, displayed_title);
1446
DisplayServer::get_singleton()->accessibility_update_set_bounds(accessibility_title_element, Rect2(Vector2(0, -w), Size2(size.x, w)));
1447
} else {
1448
DisplayServer::get_singleton()->accessibility_update_set_transform(ae, get_final_transform());
1449
if (_get_size_2d_override() != Size2()) {
1450
DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, Rect2(Point2(), _get_size_2d_override()));
1451
} else {
1452
DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, Rect2(Point2(), _get_size()));
1453
}
1454
1455
if (accessibility_announcement_element.is_null()) {
1456
accessibility_announcement_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);
1457
}
1458
1459
if (announcement.is_empty()) {
1460
DisplayServer::get_singleton()->accessibility_update_set_live(accessibility_announcement_element, DisplayServer::LIVE_OFF);
1461
} else {
1462
DisplayServer::get_singleton()->accessibility_update_set_name(accessibility_announcement_element, announcement);
1463
DisplayServer::get_singleton()->accessibility_update_set_live(accessibility_announcement_element, DisplayServer::LIVE_ASSERTIVE);
1464
}
1465
}
1466
} break;
1467
1468
case NOTIFICATION_POSTINITIALIZE: {
1469
initialized = true;
1470
1471
_invalidate_theme_cache();
1472
_update_theme_item_cache();
1473
} break;
1474
1475
case NOTIFICATION_PARENTED: {
1476
theme_owner->assign_theme_on_parented(this);
1477
} break;
1478
1479
case NOTIFICATION_UNPARENTED: {
1480
theme_owner->clear_theme_on_unparented(this);
1481
} break;
1482
1483
case NOTIFICATION_ENTER_TREE: {
1484
if (is_in_edited_scene_root()) {
1485
if (!ProjectSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &Window::_settings_changed))) {
1486
ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Window::_settings_changed));
1487
}
1488
} else if (get_parent() && get_tree()->get_root()->is_embedding_subwindows()) {
1489
// Is not the main window and main window is embedding.
1490
set_embedding_subwindows(force_native);
1491
}
1492
1493
bool embedded = false;
1494
{
1495
embedder = get_embedder();
1496
if (embedder) {
1497
embedded = true;
1498
if (!visible) {
1499
embedder = nullptr; // Not yet since not visible.
1500
}
1501
}
1502
}
1503
1504
if (embedded) {
1505
// Create as embedded.
1506
if (embedder) {
1507
if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
1508
if (is_in_edited_scene_root()) {
1509
Size2 screen_size = Size2(GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_width"), GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_height"));
1510
position = (screen_size - size) / 2;
1511
} else {
1512
position = (embedder->get_visible_rect().size - size) / 2;
1513
}
1514
}
1515
embedder->_sub_window_register(this);
1516
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
1517
_update_window_size();
1518
}
1519
1520
} else {
1521
if (!get_parent()) {
1522
// It's the root window!
1523
visible = true; // Always visible.
1524
window_id = DisplayServer::MAIN_WINDOW_ID;
1525
focused_window = this;
1526
DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
1527
_update_from_window();
1528
// Since this window already exists (created on start), we must update pos and size from it.
1529
{
1530
position = DisplayServer::get_singleton()->window_get_position(window_id);
1531
size = DisplayServer::get_singleton()->window_get_size(window_id);
1532
focused = DisplayServer::get_singleton()->window_is_focused(window_id);
1533
}
1534
_update_window_size(); // Inform DisplayServer of minimum and maximum size.
1535
_update_viewport_size(); // Then feed back to the viewport.
1536
_update_window_callbacks();
1537
// Simulate mouse-enter event when mouse is over the window, since OS event might arrive before setting callbacks.
1538
if (!mouse_in_window && Rect2(position, size).has_point(DisplayServer::get_singleton()->mouse_get_position())) {
1539
_event_callback(DisplayServer::WINDOW_EVENT_MOUSE_ENTER);
1540
}
1541
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
1542
if (DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(FLAG_TRANSPARENT), window_id)) {
1543
set_transparent_background(true);
1544
}
1545
} else {
1546
// Create.
1547
if (visible) {
1548
_make_window();
1549
}
1550
}
1551
}
1552
1553
if (transient && !transient_to_focused) {
1554
_make_transient();
1555
}
1556
if (visible) {
1557
if (window_id != DisplayServer::MAIN_WINDOW_ID && get_tree() && get_tree()->is_accessibility_supported()) {
1558
get_tree()->_accessibility_force_update();
1559
_accessibility_notify_enter(this);
1560
}
1561
notification(NOTIFICATION_VISIBILITY_CHANGED);
1562
emit_signal(SceneStringName(visibility_changed));
1563
RS::get_singleton()->viewport_set_active(get_viewport_rid(), true);
1564
if (get_parent()) {
1565
get_parent()->queue_accessibility_update();
1566
}
1567
if (embedder) {
1568
embedder->queue_accessibility_update();
1569
}
1570
}
1571
1572
// Emits NOTIFICATION_THEME_CHANGED internally.
1573
set_theme_context(ThemeDB::get_singleton()->get_nearest_theme_context(this));
1574
} break;
1575
1576
case NOTIFICATION_READY: {
1577
if (wrap_controls) {
1578
// Finish any resizing immediately so it doesn't interfere on stuff overriding _ready().
1579
_update_child_controls();
1580
}
1581
} break;
1582
1583
case NOTIFICATION_THEME_CHANGED: {
1584
emit_signal(SceneStringName(theme_changed));
1585
_invalidate_theme_cache();
1586
_update_theme_item_cache();
1587
} break;
1588
1589
case NOTIFICATION_TRANSLATION_CHANGED: {
1590
_invalidate_theme_cache();
1591
_update_theme_item_cache();
1592
_update_displayed_title();
1593
} break;
1594
1595
case NOTIFICATION_VISIBILITY_CHANGED: {
1596
if (unparent_when_invisible && !is_visible()) {
1597
Node *p = get_parent();
1598
if (p) {
1599
p->remove_child(this);
1600
}
1601
}
1602
} break;
1603
1604
case NOTIFICATION_EXIT_TREE: {
1605
if (ProjectSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &Window::_settings_changed))) {
1606
ProjectSettings::get_singleton()->disconnect("settings_changed", callable_mp(this, &Window::_settings_changed));
1607
}
1608
1609
set_theme_context(nullptr, false);
1610
1611
if (visible && window_id != DisplayServer::MAIN_WINDOW_ID) {
1612
if (get_tree() && get_tree()->is_accessibility_supported()) {
1613
_accessibility_notify_exit(this);
1614
if (get_parent()) {
1615
get_parent()->queue_accessibility_update();
1616
}
1617
if (embedder) {
1618
embedder->queue_accessibility_update();
1619
}
1620
}
1621
}
1622
1623
accessibility_title_element = RID();
1624
accessibility_announcement_element = RID();
1625
1626
if (transient) {
1627
_clear_transient();
1628
}
1629
1630
if (!is_embedded() && window_id != DisplayServer::INVALID_WINDOW_ID) {
1631
if (window_id == DisplayServer::MAIN_WINDOW_ID) {
1632
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
1633
_update_window_callbacks();
1634
} else {
1635
_clear_window();
1636
}
1637
} else {
1638
if (embedder) {
1639
embedder->_sub_window_remove(this);
1640
embedder = nullptr;
1641
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
1642
}
1643
_update_viewport_size(); //called by clear and make, which does not happen here
1644
}
1645
1646
RS::get_singleton()->viewport_set_active(get_viewport_rid(), false);
1647
} break;
1648
1649
case NOTIFICATION_VP_MOUSE_ENTER: {
1650
emit_signal(SceneStringName(mouse_entered));
1651
} break;
1652
1653
case NOTIFICATION_VP_MOUSE_EXIT: {
1654
emit_signal(SceneStringName(mouse_exited));
1655
} break;
1656
}
1657
}
1658
1659
void Window::set_content_scale_size(const Size2i &p_size) {
1660
ERR_MAIN_THREAD_GUARD;
1661
ERR_FAIL_COND(p_size.x < 0);
1662
ERR_FAIL_COND(p_size.y < 0);
1663
content_scale_size = p_size;
1664
_update_viewport_size();
1665
}
1666
1667
Size2i Window::get_content_scale_size() const {
1668
ERR_READ_THREAD_GUARD_V(Size2i());
1669
return content_scale_size;
1670
}
1671
1672
void Window::set_content_scale_mode(ContentScaleMode p_mode) {
1673
ERR_MAIN_THREAD_GUARD;
1674
content_scale_mode = p_mode;
1675
_update_viewport_size();
1676
}
1677
1678
Window::ContentScaleMode Window::get_content_scale_mode() const {
1679
ERR_READ_THREAD_GUARD_V(CONTENT_SCALE_MODE_DISABLED);
1680
return content_scale_mode;
1681
}
1682
1683
void Window::set_content_scale_aspect(ContentScaleAspect p_aspect) {
1684
ERR_MAIN_THREAD_GUARD;
1685
content_scale_aspect = p_aspect;
1686
_update_viewport_size();
1687
}
1688
1689
Window::ContentScaleAspect Window::get_content_scale_aspect() const {
1690
ERR_READ_THREAD_GUARD_V(CONTENT_SCALE_ASPECT_IGNORE);
1691
return content_scale_aspect;
1692
}
1693
1694
void Window::set_content_scale_stretch(ContentScaleStretch p_stretch) {
1695
content_scale_stretch = p_stretch;
1696
_update_viewport_size();
1697
}
1698
1699
Window::ContentScaleStretch Window::get_content_scale_stretch() const {
1700
return content_scale_stretch;
1701
}
1702
1703
void Window::set_keep_title_visible(bool p_title_visible) {
1704
if (keep_title_visible == p_title_visible) {
1705
return;
1706
}
1707
keep_title_visible = p_title_visible;
1708
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
1709
_update_window_size();
1710
}
1711
}
1712
1713
bool Window::get_keep_title_visible() const {
1714
return keep_title_visible;
1715
}
1716
1717
void Window::set_content_scale_factor(real_t p_factor) {
1718
ERR_MAIN_THREAD_GUARD;
1719
ERR_FAIL_COND(p_factor <= 0);
1720
content_scale_factor = p_factor;
1721
_update_viewport_size();
1722
}
1723
1724
real_t Window::get_content_scale_factor() const {
1725
ERR_READ_THREAD_GUARD_V(0);
1726
return content_scale_factor;
1727
}
1728
1729
DisplayServer::WindowID Window::get_window_id() const {
1730
ERR_READ_THREAD_GUARD_V(DisplayServer::INVALID_WINDOW_ID);
1731
if (get_embedder()) {
1732
#ifdef TOOLS_ENABLED
1733
if (is_part_of_edited_scene()) {
1734
return DisplayServer::MAIN_WINDOW_ID;
1735
}
1736
#endif
1737
return parent->get_window_id();
1738
}
1739
return window_id;
1740
}
1741
1742
void Window::set_mouse_passthrough_polygon(const Vector<Vector2> &p_region) {
1743
ERR_MAIN_THREAD_GUARD;
1744
mpath = p_region;
1745
if (window_id == DisplayServer::INVALID_WINDOW_ID) {
1746
return;
1747
}
1748
DisplayServer::get_singleton()->window_set_mouse_passthrough(mpath, window_id);
1749
}
1750
1751
Vector<Vector2> Window::get_mouse_passthrough_polygon() const {
1752
return mpath;
1753
}
1754
1755
void Window::set_wrap_controls(bool p_enable) {
1756
ERR_MAIN_THREAD_GUARD;
1757
wrap_controls = p_enable;
1758
1759
if (!is_inside_tree()) {
1760
return;
1761
}
1762
1763
if (updating_child_controls) {
1764
_update_child_controls();
1765
} else {
1766
_update_window_size();
1767
}
1768
}
1769
1770
bool Window::is_wrapping_controls() const {
1771
ERR_READ_THREAD_GUARD_V(false);
1772
return wrap_controls;
1773
}
1774
1775
Size2 Window::_get_contents_minimum_size() const {
1776
Size2 max;
1777
1778
for (int i = 0; i < get_child_count(); i++) {
1779
Control *c = Object::cast_to<Control>(get_child(i));
1780
if (c) {
1781
Point2i pos = c->get_position();
1782
Size2i min = c->get_combined_minimum_size();
1783
1784
max = max.max(pos + min);
1785
}
1786
}
1787
1788
return max * content_scale_factor;
1789
}
1790
1791
void Window::child_controls_changed() {
1792
ERR_MAIN_THREAD_GUARD;
1793
if (!is_inside_tree() || !visible || updating_child_controls) {
1794
return;
1795
}
1796
1797
updating_child_controls = true;
1798
callable_mp(this, &Window::_update_child_controls).call_deferred();
1799
}
1800
1801
void Window::_update_child_controls() {
1802
if (!updating_child_controls) {
1803
return;
1804
}
1805
1806
_update_window_size();
1807
1808
updating_child_controls = false;
1809
}
1810
1811
bool Window::_can_consume_input_events() const {
1812
return exclusive_child == nullptr;
1813
}
1814
1815
void Window::_window_input(const Ref<InputEvent> &p_ev) {
1816
ERR_MAIN_THREAD_GUARD;
1817
1818
if (exclusive_child != nullptr) {
1819
if (!is_embedding_subwindows()) { // Not embedding, no need for event.
1820
return;
1821
}
1822
}
1823
1824
// If the event needs to be handled in a Window-derived class, then it should overwrite
1825
// `_input_from_window` instead of subscribing to the `window_input` signal, because the signal
1826
// filters out internal events.
1827
_input_from_window(p_ev);
1828
1829
if (p_ev->get_device() != InputEvent::DEVICE_ID_INTERNAL && is_inside_tree()) {
1830
emit_signal(SceneStringName(window_input), p_ev);
1831
}
1832
1833
if (is_inside_tree()) {
1834
push_input(p_ev);
1835
}
1836
}
1837
1838
void Window::_window_input_text(const String &p_text) {
1839
push_text_input(p_text);
1840
}
1841
1842
void Window::_window_drop_files(const Vector<String> &p_files) {
1843
emit_signal(SNAME("files_dropped"), p_files);
1844
}
1845
1846
Viewport *Window::get_parent_viewport() const {
1847
ERR_READ_THREAD_GUARD_V(nullptr);
1848
if (get_parent()) {
1849
return get_parent()->get_viewport();
1850
} else {
1851
return nullptr;
1852
}
1853
}
1854
1855
Window *Window::get_non_popup_window() const {
1856
Window *w = const_cast<Window *>(this);
1857
while (w && w->is_popup()) {
1858
w = w->get_parent_visible_window();
1859
}
1860
return w;
1861
}
1862
1863
Window *Window::get_parent_visible_window() const {
1864
ERR_READ_THREAD_GUARD_V(nullptr);
1865
Viewport *vp = get_parent_viewport();
1866
Window *window = nullptr;
1867
while (vp) {
1868
window = Object::cast_to<Window>(vp);
1869
if (window && window->visible) {
1870
break;
1871
}
1872
if (!vp->get_parent()) {
1873
break;
1874
}
1875
1876
vp = vp->get_parent()->get_viewport();
1877
}
1878
return window;
1879
}
1880
1881
void Window::popup_on_parent(const Rect2i &p_parent_rect) {
1882
ERR_MAIN_THREAD_GUARD;
1883
ERR_FAIL_COND(!is_inside_tree());
1884
ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
1885
1886
if (!is_embedded()) {
1887
Window *window = get_parent_visible_window();
1888
1889
if (!window) {
1890
popup(p_parent_rect);
1891
} else {
1892
popup(Rect2i(window->get_position() + p_parent_rect.position, p_parent_rect.size));
1893
}
1894
} else {
1895
popup(p_parent_rect);
1896
}
1897
}
1898
1899
void Window::popup_centered_clamped(const Size2i &p_size, float p_fallback_ratio) {
1900
ERR_MAIN_THREAD_GUARD;
1901
ERR_FAIL_COND(!is_inside_tree());
1902
ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
1903
1904
// Consider the current size when calling with the default value.
1905
Size2i expected_size = p_size == Size2i() ? size : p_size;
1906
1907
Rect2 parent_rect;
1908
1909
if (is_embedded()) {
1910
parent_rect = get_embedder()->get_visible_rect();
1911
} else {
1912
DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
1913
int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
1914
parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
1915
parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
1916
}
1917
1918
Vector2i size_ratio = parent_rect.size * p_fallback_ratio;
1919
1920
Rect2i popup_rect;
1921
popup_rect.size = size_ratio.min(expected_size);
1922
popup_rect.size = _clamp_window_size(popup_rect.size);
1923
1924
if (parent_rect != Rect2()) {
1925
popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
1926
}
1927
1928
popup(popup_rect);
1929
}
1930
1931
void Window::popup_centered(const Size2i &p_minsize) {
1932
ERR_MAIN_THREAD_GUARD;
1933
ERR_FAIL_COND(!is_inside_tree());
1934
ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
1935
1936
// Consider the current size when calling with the default value.
1937
Size2i expected_size = p_minsize == Size2i() ? size : p_minsize;
1938
1939
Rect2 parent_rect;
1940
1941
if (is_embedded()) {
1942
parent_rect = get_embedder()->get_visible_rect();
1943
} else {
1944
DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
1945
int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
1946
parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
1947
parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
1948
}
1949
1950
Rect2i popup_rect;
1951
popup_rect.size = _clamp_window_size(expected_size);
1952
1953
if (parent_rect != Rect2()) {
1954
popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
1955
}
1956
1957
popup(popup_rect);
1958
}
1959
1960
void Window::popup_centered_ratio(float p_ratio) {
1961
ERR_MAIN_THREAD_GUARD;
1962
ERR_FAIL_COND(!is_inside_tree());
1963
ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
1964
ERR_FAIL_COND_MSG(p_ratio <= 0.0 || p_ratio > 1.0, "Ratio must be between 0.0 and 1.0!");
1965
1966
Rect2 parent_rect;
1967
1968
if (is_embedded()) {
1969
parent_rect = get_embedder()->get_visible_rect();
1970
} else {
1971
DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
1972
int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
1973
parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
1974
parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
1975
}
1976
1977
Rect2i popup_rect;
1978
if (parent_rect != Rect2()) {
1979
popup_rect.size = parent_rect.size * p_ratio;
1980
popup_rect.size = _clamp_window_size(popup_rect.size);
1981
popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
1982
}
1983
1984
popup(popup_rect);
1985
}
1986
1987
void Window::popup(const Rect2i &p_screen_rect) {
1988
ERR_MAIN_THREAD_GUARD;
1989
emit_signal(SNAME("about_to_popup"));
1990
1991
_pre_popup();
1992
1993
if (!get_embedder() && get_flag(FLAG_POPUP)) {
1994
// Send a focus-out notification when opening a Window Manager Popup.
1995
SceneTree *scene_tree = get_tree();
1996
if (scene_tree) {
1997
scene_tree->notify_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_viewports", NOTIFICATION_WM_WINDOW_FOCUS_OUT);
1998
}
1999
}
2000
2001
// Update window size to calculate the actual window size based on contents minimum size and minimum size.
2002
_update_window_size();
2003
2004
bool should_fit = is_embedded() || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS);
2005
2006
if (p_screen_rect != Rect2i()) {
2007
set_position(p_screen_rect.position);
2008
2009
if (should_fit) {
2010
int screen_id = DisplayServer::get_singleton()->get_screen_from_rect(p_screen_rect);
2011
Size2i screen_size = DisplayServer::get_singleton()->screen_get_usable_rect(screen_id).size;
2012
Size2i new_size = p_screen_rect.size.min(screen_size);
2013
set_size(new_size);
2014
} else {
2015
set_size(p_screen_rect.size);
2016
}
2017
}
2018
2019
Rect2i adjust = _popup_adjust_rect();
2020
if (adjust != Rect2i()) {
2021
set_position(adjust.position);
2022
set_size(adjust.size);
2023
}
2024
2025
int scr = DisplayServer::get_singleton()->get_screen_count();
2026
for (int i = 0; i < scr; i++) {
2027
Rect2i r = DisplayServer::get_singleton()->screen_get_usable_rect(i);
2028
if (r.has_point(position)) {
2029
current_screen = i;
2030
break;
2031
}
2032
}
2033
2034
set_transient(true);
2035
set_visible(true);
2036
2037
Rect2i parent_rect;
2038
if (is_embedded()) {
2039
parent_rect = get_embedder()->get_visible_rect();
2040
} else {
2041
int screen_id = DisplayServer::get_singleton()->window_get_current_screen(get_window_id());
2042
parent_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen_id);
2043
}
2044
if (should_fit && parent_rect != Rect2i() && !parent_rect.intersects(Rect2i(position, size))) {
2045
ERR_PRINT(vformat("Window %d spawned at invalid position: %s.", get_window_id(), position));
2046
set_position((parent_rect.size - size) / 2);
2047
}
2048
if (parent_rect != Rect2i() && is_clamped_to_embedder() && is_embedded()) {
2049
Rect2i new_rect = fit_rect_in_parent(Rect2i(position, size), parent_rect);
2050
set_position(new_rect.position);
2051
set_size(new_rect.size);
2052
}
2053
2054
_post_popup();
2055
notification(NOTIFICATION_POST_POPUP);
2056
}
2057
2058
bool Window::_try_parent_dialog(Node *p_from_node) {
2059
ERR_FAIL_NULL_V(p_from_node, false);
2060
ERR_FAIL_COND_V_MSG(is_inside_tree(), false, "Attempting to parent and popup a dialog that already has a parent.");
2061
2062
Window *w = p_from_node->get_last_exclusive_window();
2063
if (w && w != this) {
2064
w->add_child(this);
2065
return true;
2066
}
2067
return false;
2068
}
2069
2070
void Window::popup_exclusive(Node *p_from_node, const Rect2i &p_screen_rect) {
2071
if (_try_parent_dialog(p_from_node)) {
2072
popup(p_screen_rect);
2073
}
2074
}
2075
2076
void Window::popup_exclusive_on_parent(Node *p_from_node, const Rect2i &p_parent_rect) {
2077
if (_try_parent_dialog(p_from_node)) {
2078
popup_on_parent(p_parent_rect);
2079
}
2080
}
2081
2082
void Window::popup_exclusive_centered(Node *p_from_node, const Size2i &p_minsize) {
2083
if (_try_parent_dialog(p_from_node)) {
2084
popup_centered(p_minsize);
2085
}
2086
}
2087
2088
void Window::popup_exclusive_centered_ratio(Node *p_from_node, float p_ratio) {
2089
if (_try_parent_dialog(p_from_node)) {
2090
popup_centered_ratio(p_ratio);
2091
}
2092
}
2093
2094
void Window::popup_exclusive_centered_clamped(Node *p_from_node, const Size2i &p_size, float p_fallback_ratio) {
2095
if (_try_parent_dialog(p_from_node)) {
2096
popup_centered_clamped(p_size, p_fallback_ratio);
2097
}
2098
}
2099
2100
Rect2i Window::fit_rect_in_parent(Rect2i p_rect, const Rect2i &p_parent_rect) const {
2101
ERR_READ_THREAD_GUARD_V(Rect2i());
2102
Size2i limit = p_parent_rect.size;
2103
if (p_rect.position.x + p_rect.size.x > limit.x) {
2104
p_rect.position.x = limit.x - p_rect.size.x;
2105
}
2106
if (p_rect.position.y + p_rect.size.y > limit.y) {
2107
p_rect.position.y = limit.y - p_rect.size.y;
2108
}
2109
2110
if (p_rect.position.x < 0) {
2111
p_rect.position.x = 0;
2112
}
2113
2114
int title_height = get_flag(Window::FLAG_BORDERLESS) ? 0 : theme_cache.title_height;
2115
2116
if (p_rect.position.y < title_height) {
2117
p_rect.position.y = title_height;
2118
}
2119
2120
return p_rect;
2121
}
2122
2123
Size2 Window::get_contents_minimum_size() const {
2124
ERR_READ_THREAD_GUARD_V(Size2());
2125
Vector2 ms;
2126
if (GDVIRTUAL_CALL(_get_contents_minimum_size, ms)) {
2127
return ms;
2128
}
2129
return _get_contents_minimum_size();
2130
}
2131
2132
Size2 Window::get_clamped_minimum_size() const {
2133
ERR_READ_THREAD_GUARD_V(Size2());
2134
if (!wrap_controls) {
2135
return min_size;
2136
}
2137
2138
return min_size.max(get_contents_minimum_size() * get_content_scale_factor());
2139
}
2140
2141
void Window::grab_focus() {
2142
ERR_MAIN_THREAD_GUARD;
2143
if (embedder) {
2144
embedder->_sub_window_grab_focus(this);
2145
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2146
DisplayServer::get_singleton()->window_move_to_foreground(window_id);
2147
}
2148
}
2149
2150
bool Window::has_focus() const {
2151
ERR_READ_THREAD_GUARD_V(false);
2152
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2153
return DisplayServer::get_singleton()->window_is_focused(window_id);
2154
}
2155
return focused;
2156
}
2157
2158
bool Window::has_focus_or_active_popup() const {
2159
ERR_READ_THREAD_GUARD_V(false);
2160
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2161
return DisplayServer::get_singleton()->window_is_focused(window_id) || (DisplayServer::get_singleton()->window_get_active_popup() == window_id);
2162
}
2163
return focused;
2164
}
2165
2166
void Window::start_drag() {
2167
ERR_MAIN_THREAD_GUARD;
2168
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2169
DisplayServer::get_singleton()->window_start_drag(window_id);
2170
} else if (embedder) {
2171
embedder->_window_start_drag(this);
2172
}
2173
}
2174
2175
void Window::start_resize(DisplayServer::WindowResizeEdge p_edge) {
2176
ERR_MAIN_THREAD_GUARD;
2177
if (get_flag(FLAG_RESIZE_DISABLED)) {
2178
return;
2179
}
2180
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2181
DisplayServer::get_singleton()->window_start_resize(p_edge, window_id);
2182
} else if (embedder) {
2183
switch (p_edge) {
2184
case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
2185
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_TOP_LEFT, this);
2186
} break;
2187
case DisplayServer::WINDOW_EDGE_TOP: {
2188
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_TOP, this);
2189
} break;
2190
case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
2191
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_TOP_RIGHT, this);
2192
} break;
2193
case DisplayServer::WINDOW_EDGE_LEFT: {
2194
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_LEFT, this);
2195
} break;
2196
case DisplayServer::WINDOW_EDGE_RIGHT: {
2197
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_RIGHT, this);
2198
} break;
2199
case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
2200
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_BOTTOM_LEFT, this);
2201
} break;
2202
case DisplayServer::WINDOW_EDGE_BOTTOM: {
2203
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_BOTTOM, this);
2204
} break;
2205
case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
2206
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_BOTTOM_RIGHT, this);
2207
} break;
2208
default:
2209
break;
2210
}
2211
}
2212
}
2213
2214
Rect2i Window::get_usable_parent_rect() const {
2215
ERR_READ_THREAD_GUARD_V(Rect2i());
2216
ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
2217
Rect2i parent_rect;
2218
if (is_embedded()) {
2219
parent_rect = get_embedder()->get_visible_rect();
2220
} else {
2221
const Window *w = is_visible() ? this : get_parent_visible_window();
2222
//find a parent that can contain us
2223
ERR_FAIL_NULL_V(w, Rect2());
2224
2225
parent_rect = DisplayServer::get_singleton()->screen_get_usable_rect(DisplayServer::get_singleton()->window_get_current_screen(w->get_window_id()));
2226
}
2227
return parent_rect;
2228
}
2229
2230
void Window::set_accessibility_name(const String &p_name) {
2231
ERR_MAIN_THREAD_GUARD;
2232
if (accessibility_name != p_name) {
2233
accessibility_name = p_name;
2234
queue_accessibility_update();
2235
update_configuration_warnings();
2236
}
2237
}
2238
2239
String Window::get_accessibility_name() const {
2240
return tr(accessibility_name);
2241
}
2242
2243
void Window::set_accessibility_description(const String &p_description) {
2244
ERR_MAIN_THREAD_GUARD;
2245
if (accessibility_description != p_description) {
2246
accessibility_description = p_description;
2247
queue_accessibility_update();
2248
}
2249
}
2250
2251
String Window::get_accessibility_description() const {
2252
return tr(accessibility_description);
2253
}
2254
2255
void Window::accessibility_announcement(const String &p_announcement) {
2256
ERR_MAIN_THREAD_GUARD;
2257
announcement = p_announcement;
2258
queue_accessibility_update();
2259
}
2260
2261
void Window::add_child_notify(Node *p_child) {
2262
if (is_inside_tree() && wrap_controls) {
2263
child_controls_changed();
2264
}
2265
}
2266
2267
void Window::remove_child_notify(Node *p_child) {
2268
if (is_inside_tree() && wrap_controls) {
2269
child_controls_changed();
2270
}
2271
}
2272
2273
// Theming.
2274
2275
void Window::set_theme_owner_node(Node *p_node) {
2276
ERR_MAIN_THREAD_GUARD;
2277
theme_owner->set_owner_node(p_node);
2278
}
2279
2280
Node *Window::get_theme_owner_node() const {
2281
ERR_READ_THREAD_GUARD_V(nullptr);
2282
return theme_owner->get_owner_node();
2283
}
2284
2285
bool Window::has_theme_owner_node() const {
2286
ERR_READ_THREAD_GUARD_V(false);
2287
return theme_owner->has_owner_node();
2288
}
2289
2290
void Window::set_theme_context(ThemeContext *p_context, bool p_propagate) {
2291
ERR_MAIN_THREAD_GUARD;
2292
theme_owner->set_owner_context(p_context, p_propagate);
2293
}
2294
2295
void Window::set_theme(const Ref<Theme> &p_theme) {
2296
ERR_MAIN_THREAD_GUARD;
2297
if (theme == p_theme) {
2298
return;
2299
}
2300
2301
if (theme.is_valid()) {
2302
theme->disconnect_changed(callable_mp(this, &Window::_theme_changed));
2303
}
2304
2305
theme = p_theme;
2306
if (theme.is_valid()) {
2307
theme_owner->propagate_theme_changed(this, this, is_inside_tree(), true);
2308
theme->connect_changed(callable_mp(this, &Window::_theme_changed), CONNECT_DEFERRED);
2309
return;
2310
}
2311
2312
Control *parent_c = Object::cast_to<Control>(get_parent());
2313
if (parent_c && parent_c->has_theme_owner_node()) {
2314
theme_owner->propagate_theme_changed(this, parent_c->get_theme_owner_node(), is_inside_tree(), true);
2315
return;
2316
}
2317
2318
Window *parent_w = cast_to<Window>(get_parent());
2319
if (parent_w && parent_w->has_theme_owner_node()) {
2320
theme_owner->propagate_theme_changed(this, parent_w->get_theme_owner_node(), is_inside_tree(), true);
2321
return;
2322
}
2323
2324
theme_owner->propagate_theme_changed(this, nullptr, is_inside_tree(), true);
2325
}
2326
2327
Ref<Theme> Window::get_theme() const {
2328
ERR_READ_THREAD_GUARD_V(Ref<Theme>());
2329
return theme;
2330
}
2331
2332
void Window::_theme_changed() {
2333
if (is_inside_tree()) {
2334
theme_owner->propagate_theme_changed(this, this, true, false);
2335
}
2336
}
2337
2338
void Window::_notify_theme_override_changed() {
2339
if (!bulk_theme_override && is_inside_tree()) {
2340
notification(NOTIFICATION_THEME_CHANGED);
2341
}
2342
}
2343
2344
void Window::_invalidate_theme_cache() {
2345
theme_icon_cache.clear();
2346
theme_style_cache.clear();
2347
theme_font_cache.clear();
2348
theme_font_size_cache.clear();
2349
theme_color_cache.clear();
2350
theme_constant_cache.clear();
2351
}
2352
2353
void Window::_update_theme_item_cache() {
2354
// Request an update on the next frame to reflect theme changes.
2355
// Updating without a delay can cause a lot of lag.
2356
if (!wrap_controls) {
2357
updating_embedded_window = true;
2358
callable_mp(this, &Window::_update_embedded_window).call_deferred();
2359
} else {
2360
child_controls_changed();
2361
}
2362
2363
ThemeDB::get_singleton()->update_class_instance_items(this);
2364
}
2365
2366
void Window::_update_embedded_window() {
2367
if (!updating_embedded_window) {
2368
return;
2369
}
2370
2371
if (embedder) {
2372
embedder->_sub_window_update(this);
2373
};
2374
2375
updating_embedded_window = false;
2376
}
2377
2378
void Window::set_theme_type_variation(const StringName &p_theme_type) {
2379
ERR_MAIN_THREAD_GUARD;
2380
theme_type_variation = p_theme_type;
2381
if (is_inside_tree()) {
2382
notification(NOTIFICATION_THEME_CHANGED);
2383
}
2384
}
2385
2386
StringName Window::get_theme_type_variation() const {
2387
ERR_READ_THREAD_GUARD_V(StringName());
2388
return theme_type_variation;
2389
}
2390
2391
/// Theme property lookup.
2392
2393
Ref<Texture2D> Window::get_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
2394
ERR_READ_THREAD_GUARD_V(Ref<Texture2D>());
2395
if (!initialized) {
2396
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2397
}
2398
2399
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2400
const Ref<Texture2D> *tex = theme_icon_override.getptr(p_name);
2401
if (tex) {
2402
return *tex;
2403
}
2404
}
2405
2406
if (theme_icon_cache.has(p_theme_type) && theme_icon_cache[p_theme_type].has(p_name)) {
2407
return theme_icon_cache[p_theme_type][p_name];
2408
}
2409
2410
Vector<StringName> theme_types;
2411
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2412
Ref<Texture2D> icon = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
2413
theme_icon_cache[p_theme_type][p_name] = icon;
2414
return icon;
2415
}
2416
2417
Ref<StyleBox> Window::get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
2418
ERR_READ_THREAD_GUARD_V(Ref<StyleBox>());
2419
if (!initialized) {
2420
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2421
}
2422
2423
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2424
const Ref<StyleBox> *style = theme_style_override.getptr(p_name);
2425
if (style) {
2426
return *style;
2427
}
2428
}
2429
2430
if (theme_style_cache.has(p_theme_type) && theme_style_cache[p_theme_type].has(p_name)) {
2431
return theme_style_cache[p_theme_type][p_name];
2432
}
2433
2434
Vector<StringName> theme_types;
2435
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2436
Ref<StyleBox> style = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
2437
theme_style_cache[p_theme_type][p_name] = style;
2438
return style;
2439
}
2440
2441
Ref<Font> Window::get_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
2442
ERR_READ_THREAD_GUARD_V(Ref<Font>());
2443
if (!initialized) {
2444
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2445
}
2446
2447
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2448
const Ref<Font> *font = theme_font_override.getptr(p_name);
2449
if (font) {
2450
return *font;
2451
}
2452
}
2453
2454
if (theme_font_cache.has(p_theme_type) && theme_font_cache[p_theme_type].has(p_name)) {
2455
return theme_font_cache[p_theme_type][p_name];
2456
}
2457
2458
Vector<StringName> theme_types;
2459
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2460
Ref<Font> font = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
2461
theme_font_cache[p_theme_type][p_name] = font;
2462
return font;
2463
}
2464
2465
int Window::get_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
2466
ERR_READ_THREAD_GUARD_V(0);
2467
if (!initialized) {
2468
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2469
}
2470
2471
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2472
const int *font_size = theme_font_size_override.getptr(p_name);
2473
if (font_size && (*font_size) > 0) {
2474
return *font_size;
2475
}
2476
}
2477
2478
if (theme_font_size_cache.has(p_theme_type) && theme_font_size_cache[p_theme_type].has(p_name)) {
2479
return theme_font_size_cache[p_theme_type][p_name];
2480
}
2481
2482
Vector<StringName> theme_types;
2483
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2484
int font_size = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
2485
theme_font_size_cache[p_theme_type][p_name] = font_size;
2486
return font_size;
2487
}
2488
2489
Color Window::get_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
2490
ERR_READ_THREAD_GUARD_V(Color());
2491
if (!initialized) {
2492
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2493
}
2494
2495
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2496
const Color *color = theme_color_override.getptr(p_name);
2497
if (color) {
2498
return *color;
2499
}
2500
}
2501
2502
if (theme_color_cache.has(p_theme_type) && theme_color_cache[p_theme_type].has(p_name)) {
2503
return theme_color_cache[p_theme_type][p_name];
2504
}
2505
2506
Vector<StringName> theme_types;
2507
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2508
Color color = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
2509
theme_color_cache[p_theme_type][p_name] = color;
2510
return color;
2511
}
2512
2513
int Window::get_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
2514
ERR_READ_THREAD_GUARD_V(0);
2515
if (!initialized) {
2516
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2517
}
2518
2519
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2520
const int *constant = theme_constant_override.getptr(p_name);
2521
if (constant) {
2522
return *constant;
2523
}
2524
}
2525
2526
if (theme_constant_cache.has(p_theme_type) && theme_constant_cache[p_theme_type].has(p_name)) {
2527
return theme_constant_cache[p_theme_type][p_name];
2528
}
2529
2530
Vector<StringName> theme_types;
2531
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2532
int constant = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
2533
theme_constant_cache[p_theme_type][p_name] = constant;
2534
return constant;
2535
}
2536
2537
Variant Window::get_theme_item(Theme::DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
2538
switch (p_data_type) {
2539
case Theme::DATA_TYPE_COLOR:
2540
return get_theme_color(p_name, p_theme_type);
2541
case Theme::DATA_TYPE_CONSTANT:
2542
return get_theme_constant(p_name, p_theme_type);
2543
case Theme::DATA_TYPE_FONT:
2544
return get_theme_font(p_name, p_theme_type);
2545
case Theme::DATA_TYPE_FONT_SIZE:
2546
return get_theme_font_size(p_name, p_theme_type);
2547
case Theme::DATA_TYPE_ICON:
2548
return get_theme_icon(p_name, p_theme_type);
2549
case Theme::DATA_TYPE_STYLEBOX:
2550
return get_theme_stylebox(p_name, p_theme_type);
2551
case Theme::DATA_TYPE_MAX:
2552
break; // Can't happen, but silences warning.
2553
}
2554
2555
return Variant();
2556
}
2557
2558
#ifdef TOOLS_ENABLED
2559
Ref<Texture2D> Window::get_editor_theme_icon(const StringName &p_name) const {
2560
return get_theme_icon(p_name, SNAME("EditorIcons"));
2561
}
2562
2563
Ref<Texture2D> Window::get_editor_theme_native_menu_icon(const StringName &p_name, bool p_global_menu, bool p_dark_mode) const {
2564
if (!p_global_menu) {
2565
return get_theme_icon(p_name, SNAME("EditorIcons"));
2566
}
2567
if (p_dark_mode && has_theme_icon(String(p_name) + "Dark", SNAME("EditorIcons"))) {
2568
return get_theme_icon(String(p_name) + "Dark", SNAME("EditorIcons"));
2569
} else if (!p_dark_mode && has_theme_icon(String(p_name) + "Light", SNAME("EditorIcons"))) {
2570
return get_theme_icon(String(p_name) + "Light", SNAME("EditorIcons"));
2571
}
2572
return get_theme_icon(p_name, SNAME("EditorIcons"));
2573
}
2574
#endif
2575
2576
bool Window::has_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
2577
ERR_READ_THREAD_GUARD_V(false);
2578
if (!initialized) {
2579
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2580
}
2581
2582
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2583
if (has_theme_icon_override(p_name)) {
2584
return true;
2585
}
2586
}
2587
2588
Vector<StringName> theme_types;
2589
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2590
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
2591
}
2592
2593
bool Window::has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
2594
ERR_READ_THREAD_GUARD_V(false);
2595
if (!initialized) {
2596
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2597
}
2598
2599
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2600
if (has_theme_stylebox_override(p_name)) {
2601
return true;
2602
}
2603
}
2604
2605
Vector<StringName> theme_types;
2606
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2607
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
2608
}
2609
2610
bool Window::has_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
2611
ERR_READ_THREAD_GUARD_V(false);
2612
if (!initialized) {
2613
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2614
}
2615
2616
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2617
if (has_theme_font_override(p_name)) {
2618
return true;
2619
}
2620
}
2621
2622
Vector<StringName> theme_types;
2623
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2624
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
2625
}
2626
2627
bool Window::has_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
2628
ERR_READ_THREAD_GUARD_V(false);
2629
if (!initialized) {
2630
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2631
}
2632
2633
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2634
if (has_theme_font_size_override(p_name)) {
2635
return true;
2636
}
2637
}
2638
2639
Vector<StringName> theme_types;
2640
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2641
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
2642
}
2643
2644
bool Window::has_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
2645
ERR_READ_THREAD_GUARD_V(false);
2646
if (!initialized) {
2647
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2648
}
2649
2650
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2651
if (has_theme_color_override(p_name)) {
2652
return true;
2653
}
2654
}
2655
2656
Vector<StringName> theme_types;
2657
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2658
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
2659
}
2660
2661
bool Window::has_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
2662
ERR_READ_THREAD_GUARD_V(false);
2663
if (!initialized) {
2664
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2665
}
2666
2667
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2668
if (has_theme_constant_override(p_name)) {
2669
return true;
2670
}
2671
}
2672
2673
Vector<StringName> theme_types;
2674
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2675
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
2676
}
2677
2678
/// Local property overrides.
2679
2680
void Window::add_theme_icon_override(const StringName &p_name, const Ref<Texture2D> &p_icon) {
2681
ERR_MAIN_THREAD_GUARD;
2682
ERR_FAIL_COND(p_icon.is_null());
2683
2684
if (theme_icon_override.has(p_name)) {
2685
theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2686
}
2687
2688
theme_icon_override[p_name] = p_icon;
2689
theme_icon_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
2690
_notify_theme_override_changed();
2691
}
2692
2693
void Window::add_theme_style_override(const StringName &p_name, const Ref<StyleBox> &p_style) {
2694
ERR_MAIN_THREAD_GUARD;
2695
ERR_FAIL_COND(p_style.is_null());
2696
2697
if (theme_style_override.has(p_name)) {
2698
theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2699
}
2700
2701
theme_style_override[p_name] = p_style;
2702
theme_style_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
2703
_notify_theme_override_changed();
2704
}
2705
2706
void Window::add_theme_font_override(const StringName &p_name, const Ref<Font> &p_font) {
2707
ERR_MAIN_THREAD_GUARD;
2708
ERR_FAIL_COND(p_font.is_null());
2709
2710
if (theme_font_override.has(p_name)) {
2711
theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2712
}
2713
2714
theme_font_override[p_name] = p_font;
2715
theme_font_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
2716
_notify_theme_override_changed();
2717
}
2718
2719
void Window::add_theme_font_size_override(const StringName &p_name, int p_font_size) {
2720
ERR_MAIN_THREAD_GUARD;
2721
theme_font_size_override[p_name] = p_font_size;
2722
_notify_theme_override_changed();
2723
}
2724
2725
void Window::add_theme_color_override(const StringName &p_name, const Color &p_color) {
2726
ERR_MAIN_THREAD_GUARD;
2727
theme_color_override[p_name] = p_color;
2728
_notify_theme_override_changed();
2729
}
2730
2731
void Window::add_theme_constant_override(const StringName &p_name, int p_constant) {
2732
ERR_MAIN_THREAD_GUARD;
2733
theme_constant_override[p_name] = p_constant;
2734
_notify_theme_override_changed();
2735
}
2736
2737
void Window::remove_theme_icon_override(const StringName &p_name) {
2738
ERR_MAIN_THREAD_GUARD;
2739
if (theme_icon_override.has(p_name)) {
2740
theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2741
}
2742
2743
theme_icon_override.erase(p_name);
2744
_notify_theme_override_changed();
2745
}
2746
2747
void Window::remove_theme_style_override(const StringName &p_name) {
2748
ERR_MAIN_THREAD_GUARD;
2749
if (theme_style_override.has(p_name)) {
2750
theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2751
}
2752
2753
theme_style_override.erase(p_name);
2754
_notify_theme_override_changed();
2755
}
2756
2757
void Window::remove_theme_font_override(const StringName &p_name) {
2758
ERR_MAIN_THREAD_GUARD;
2759
if (theme_font_override.has(p_name)) {
2760
theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2761
}
2762
2763
theme_font_override.erase(p_name);
2764
_notify_theme_override_changed();
2765
}
2766
2767
void Window::remove_theme_font_size_override(const StringName &p_name) {
2768
ERR_MAIN_THREAD_GUARD;
2769
theme_font_size_override.erase(p_name);
2770
_notify_theme_override_changed();
2771
}
2772
2773
void Window::remove_theme_color_override(const StringName &p_name) {
2774
ERR_MAIN_THREAD_GUARD;
2775
theme_color_override.erase(p_name);
2776
_notify_theme_override_changed();
2777
}
2778
2779
void Window::remove_theme_constant_override(const StringName &p_name) {
2780
ERR_MAIN_THREAD_GUARD;
2781
theme_constant_override.erase(p_name);
2782
_notify_theme_override_changed();
2783
}
2784
2785
bool Window::has_theme_icon_override(const StringName &p_name) const {
2786
ERR_READ_THREAD_GUARD_V(false);
2787
const Ref<Texture2D> *tex = theme_icon_override.getptr(p_name);
2788
return tex != nullptr;
2789
}
2790
2791
bool Window::has_theme_stylebox_override(const StringName &p_name) const {
2792
ERR_READ_THREAD_GUARD_V(false);
2793
const Ref<StyleBox> *style = theme_style_override.getptr(p_name);
2794
return style != nullptr;
2795
}
2796
2797
bool Window::has_theme_font_override(const StringName &p_name) const {
2798
ERR_READ_THREAD_GUARD_V(false);
2799
const Ref<Font> *font = theme_font_override.getptr(p_name);
2800
return font != nullptr;
2801
}
2802
2803
bool Window::has_theme_font_size_override(const StringName &p_name) const {
2804
ERR_READ_THREAD_GUARD_V(false);
2805
const int *font_size = theme_font_size_override.getptr(p_name);
2806
return font_size != nullptr;
2807
}
2808
2809
bool Window::has_theme_color_override(const StringName &p_name) const {
2810
ERR_READ_THREAD_GUARD_V(false);
2811
const Color *color = theme_color_override.getptr(p_name);
2812
return color != nullptr;
2813
}
2814
2815
bool Window::has_theme_constant_override(const StringName &p_name) const {
2816
ERR_READ_THREAD_GUARD_V(false);
2817
const int *constant = theme_constant_override.getptr(p_name);
2818
return constant != nullptr;
2819
}
2820
2821
/// Default theme properties.
2822
2823
float Window::get_theme_default_base_scale() const {
2824
ERR_READ_THREAD_GUARD_V(0);
2825
return theme_owner->get_theme_default_base_scale();
2826
}
2827
2828
Ref<Font> Window::get_theme_default_font() const {
2829
ERR_READ_THREAD_GUARD_V(Ref<Font>());
2830
return theme_owner->get_theme_default_font();
2831
}
2832
2833
int Window::get_theme_default_font_size() const {
2834
ERR_READ_THREAD_GUARD_V(0);
2835
return theme_owner->get_theme_default_font_size();
2836
}
2837
2838
/// Bulk actions.
2839
2840
void Window::begin_bulk_theme_override() {
2841
ERR_MAIN_THREAD_GUARD;
2842
bulk_theme_override = true;
2843
}
2844
2845
void Window::end_bulk_theme_override() {
2846
ERR_MAIN_THREAD_GUARD;
2847
ERR_FAIL_COND(!bulk_theme_override);
2848
2849
bulk_theme_override = false;
2850
_notify_theme_override_changed();
2851
}
2852
2853
//
2854
2855
Rect2i Window::get_parent_rect() const {
2856
ERR_READ_THREAD_GUARD_V(Rect2i());
2857
ERR_FAIL_COND_V(!is_inside_tree(), Rect2i());
2858
if (is_embedded()) {
2859
//viewport
2860
Node *n = get_parent();
2861
ERR_FAIL_NULL_V(n, Rect2i());
2862
Viewport *p = n->get_viewport();
2863
ERR_FAIL_NULL_V(p, Rect2i());
2864
2865
return p->get_visible_rect();
2866
} else {
2867
int x = get_position().x;
2868
int closest_dist = 0x7FFFFFFF;
2869
Rect2i closest_rect;
2870
for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
2871
Rect2i s(DisplayServer::get_singleton()->screen_get_position(i), DisplayServer::get_singleton()->screen_get_size(i));
2872
int d;
2873
if (x >= s.position.x && x < s.size.x) {
2874
//contained
2875
closest_rect = s;
2876
break;
2877
} else if (x < s.position.x) {
2878
d = s.position.x - x;
2879
} else {
2880
d = x - (s.position.x + s.size.x);
2881
}
2882
2883
if (d < closest_dist) {
2884
closest_dist = d;
2885
closest_rect = s;
2886
}
2887
}
2888
return closest_rect;
2889
}
2890
}
2891
2892
void Window::set_clamp_to_embedder(bool p_enable) {
2893
ERR_MAIN_THREAD_GUARD;
2894
clamp_to_embedder = p_enable;
2895
}
2896
2897
bool Window::is_clamped_to_embedder() const {
2898
ERR_READ_THREAD_GUARD_V(false);
2899
return clamp_to_embedder;
2900
}
2901
2902
void Window::set_unparent_when_invisible(bool p_unparent) {
2903
unparent_when_invisible = p_unparent;
2904
}
2905
2906
void Window::set_layout_direction(Window::LayoutDirection p_direction) {
2907
ERR_MAIN_THREAD_GUARD;
2908
ERR_FAIL_INDEX(p_direction, LAYOUT_DIRECTION_MAX);
2909
2910
layout_dir = p_direction;
2911
propagate_notification(Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
2912
}
2913
2914
Window::LayoutDirection Window::get_layout_direction() const {
2915
ERR_READ_THREAD_GUARD_V(LAYOUT_DIRECTION_INHERITED);
2916
return layout_dir;
2917
}
2918
2919
bool Window::is_layout_rtl() const {
2920
ERR_READ_THREAD_GUARD_V(false);
2921
if (layout_dir == LAYOUT_DIRECTION_INHERITED) {
2922
#ifdef TOOLS_ENABLED
2923
if (is_part_of_edited_scene() && GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
2924
return true;
2925
}
2926
if (is_inside_tree()) {
2927
Node *edited_scene_root = get_tree()->get_edited_scene_root();
2928
if (edited_scene_root == this) {
2929
int proj_root_layout_direction = GLOBAL_GET_CACHED(int, "internationalization/rendering/root_node_layout_direction");
2930
if (proj_root_layout_direction == 1) {
2931
return false;
2932
} else if (proj_root_layout_direction == 2) {
2933
return true;
2934
} else if (proj_root_layout_direction == 3) {
2935
String locale = OS::get_singleton()->get_locale();
2936
return TS->is_locale_right_to_left(locale);
2937
} else {
2938
const Ref<Translation> &t = TranslationServer::get_singleton()->get_translation_object(TranslationServer::get_singleton()->get_locale());
2939
String locale = t.is_valid() ? t->get_locale() : TranslationServer::get_singleton()->get_fallback_locale();
2940
return TS->is_locale_right_to_left(locale);
2941
}
2942
}
2943
}
2944
#else
2945
if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
2946
return true;
2947
}
2948
#endif
2949
Node *parent_node = get_parent();
2950
while (parent_node) {
2951
Control *parent_control = Object::cast_to<Control>(parent_node);
2952
if (parent_control) {
2953
return parent_control->is_layout_rtl();
2954
}
2955
2956
Window *parent_window = Object::cast_to<Window>(parent_node);
2957
if (parent_window) {
2958
return parent_window->is_layout_rtl();
2959
}
2960
parent_node = parent_node->get_parent();
2961
}
2962
2963
if (root_layout_direction == 1) {
2964
return false;
2965
} else if (root_layout_direction == 2) {
2966
return true;
2967
} else if (root_layout_direction == 3) {
2968
String locale = OS::get_singleton()->get_locale();
2969
return TS->is_locale_right_to_left(locale);
2970
} else {
2971
String locale = TranslationServer::get_singleton()->get_tool_locale();
2972
return TS->is_locale_right_to_left(locale);
2973
}
2974
} else if (layout_dir == LAYOUT_DIRECTION_APPLICATION_LOCALE) {
2975
if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
2976
return true;
2977
} else {
2978
String locale = TranslationServer::get_singleton()->get_tool_locale();
2979
return TS->is_locale_right_to_left(locale);
2980
}
2981
} else if (layout_dir == LAYOUT_DIRECTION_SYSTEM_LOCALE) {
2982
if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
2983
return true;
2984
} else {
2985
String locale = OS::get_singleton()->get_locale();
2986
return TS->is_locale_right_to_left(locale);
2987
}
2988
} else {
2989
return (layout_dir == LAYOUT_DIRECTION_RTL);
2990
}
2991
}
2992
2993
#ifndef DISABLE_DEPRECATED
2994
2995
void Window::set_use_font_oversampling(bool p_oversampling) {
2996
Viewport::set_use_oversampling(p_oversampling);
2997
}
2998
2999
bool Window::is_using_font_oversampling() const {
3000
return Viewport::is_using_oversampling();
3001
}
3002
3003
void Window::set_auto_translate(bool p_enable) {
3004
ERR_MAIN_THREAD_GUARD;
3005
set_auto_translate_mode(p_enable ? AUTO_TRANSLATE_MODE_ALWAYS : AUTO_TRANSLATE_MODE_DISABLED);
3006
}
3007
3008
bool Window::is_auto_translating() const {
3009
ERR_READ_THREAD_GUARD_V(false);
3010
return can_auto_translate();
3011
}
3012
#endif
3013
3014
Transform2D Window::get_final_transform() const {
3015
ERR_READ_THREAD_GUARD_V(Transform2D());
3016
return window_transform * stretch_transform * global_canvas_transform;
3017
}
3018
3019
Transform2D Window::get_screen_transform_internal(bool p_absolute_position) const {
3020
ERR_READ_THREAD_GUARD_V(Transform2D());
3021
Transform2D embedder_transform;
3022
if (get_embedder()) {
3023
embedder_transform.translate_local(get_position());
3024
embedder_transform = get_embedder()->get_screen_transform_internal(p_absolute_position) * embedder_transform;
3025
} else if (p_absolute_position) {
3026
embedder_transform.translate_local(get_position());
3027
}
3028
return embedder_transform * get_final_transform();
3029
}
3030
3031
Transform2D Window::get_popup_base_transform_native() const {
3032
ERR_READ_THREAD_GUARD_V(Transform2D());
3033
if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SUBWINDOWS)) {
3034
return Transform2D();
3035
}
3036
Transform2D popup_base_transform;
3037
popup_base_transform.set_origin(get_position());
3038
popup_base_transform *= get_final_transform();
3039
if (get_embedder()) {
3040
return get_embedder()->get_popup_base_transform_native() * popup_base_transform;
3041
}
3042
return popup_base_transform;
3043
}
3044
3045
Transform2D Window::get_popup_base_transform() const {
3046
ERR_READ_THREAD_GUARD_V(Transform2D());
3047
if (is_embedding_subwindows()) {
3048
return Transform2D();
3049
}
3050
Transform2D popup_base_transform;
3051
popup_base_transform.set_origin(get_position());
3052
popup_base_transform *= get_final_transform();
3053
if (get_embedder()) {
3054
return get_embedder()->get_popup_base_transform() * popup_base_transform;
3055
}
3056
return popup_base_transform;
3057
}
3058
3059
Viewport *Window::get_section_root_viewport() const {
3060
if (get_embedder()) {
3061
return get_embedder()->get_section_root_viewport();
3062
}
3063
if (is_inside_tree()) {
3064
// Native window.
3065
return SceneTree::get_singleton()->get_root();
3066
}
3067
Window *vp = const_cast<Window *>(this);
3068
return vp;
3069
}
3070
3071
bool Window::is_attached_in_viewport() const {
3072
return get_embedder();
3073
}
3074
3075
void Window::_update_mouse_over(Vector2 p_pos) {
3076
if (!mouse_in_window) {
3077
if (is_embedded()) {
3078
mouse_in_window = true;
3079
_propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
3080
}
3081
}
3082
3083
bool new_in = get_visible_rect().has_point(p_pos);
3084
if (new_in == gui.mouse_in_viewport) {
3085
if (new_in) {
3086
Viewport::_update_mouse_over(p_pos);
3087
}
3088
return;
3089
}
3090
3091
if (new_in) {
3092
notification(NOTIFICATION_VP_MOUSE_ENTER);
3093
Viewport::_update_mouse_over(p_pos);
3094
} else {
3095
Viewport::_mouse_leave_viewport();
3096
}
3097
}
3098
3099
void Window::_mouse_leave_viewport() {
3100
Viewport::_mouse_leave_viewport();
3101
if (is_embedded()) {
3102
mouse_in_window = false;
3103
_propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
3104
}
3105
}
3106
3107
void Window::_update_displayed_title() {
3108
displayed_title = atr(title);
3109
3110
#ifdef DEBUG_ENABLED
3111
if (window_id == DisplayServer::MAIN_WINDOW_ID && !Engine::get_singleton()->is_project_manager_hint()) {
3112
// Append a suffix to the window title to denote that the project is running
3113
// from a debug build (including the editor, excluding the project manager).
3114
// Since this results in lower performance, this should be clearly presented
3115
// to the user.
3116
displayed_title = vformat("%s (DEBUG)", displayed_title);
3117
}
3118
#endif
3119
3120
if (embedder) {
3121
embedder->_sub_window_update(this);
3122
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
3123
DisplayServer::get_singleton()->window_set_title(displayed_title, window_id);
3124
if (keep_title_visible) {
3125
Size2i title_size = DisplayServer::get_singleton()->window_get_title_size(displayed_title, window_id);
3126
Size2i size_limit = get_clamped_minimum_size();
3127
if (title_size.x > size_limit.x || title_size.y > size_limit.y) {
3128
_update_window_size();
3129
}
3130
}
3131
}
3132
3133
#ifdef DEBUG_ENABLED
3134
if (EngineDebugger::get_singleton() && window_id == DisplayServer::MAIN_WINDOW_ID && !Engine::get_singleton()->is_project_manager_hint()) {
3135
Array arr = { displayed_title };
3136
EngineDebugger::get_singleton()->send_message("window:title", arr);
3137
}
3138
#endif
3139
3140
queue_accessibility_update();
3141
}
3142
3143
void Window::_bind_methods() {
3144
ClassDB::bind_method(D_METHOD("set_title", "title"), &Window::set_title);
3145
ClassDB::bind_method(D_METHOD("get_title"), &Window::get_title);
3146
3147
ClassDB::bind_method(D_METHOD("set_initial_position", "initial_position"), &Window::set_initial_position);
3148
ClassDB::bind_method(D_METHOD("get_initial_position"), &Window::get_initial_position);
3149
3150
ClassDB::bind_method(D_METHOD("set_current_screen", "index"), &Window::set_current_screen);
3151
ClassDB::bind_method(D_METHOD("get_current_screen"), &Window::get_current_screen);
3152
3153
ClassDB::bind_method(D_METHOD("set_position", "position"), &Window::set_position);
3154
ClassDB::bind_method(D_METHOD("get_position"), &Window::get_position);
3155
ClassDB::bind_method(D_METHOD("move_to_center"), &Window::move_to_center);
3156
3157
ClassDB::bind_method(D_METHOD("set_size", "size"), &Window::set_size);
3158
ClassDB::bind_method(D_METHOD("get_size"), &Window::get_size);
3159
ClassDB::bind_method(D_METHOD("reset_size"), &Window::reset_size);
3160
3161
ClassDB::bind_method(D_METHOD("get_position_with_decorations"), &Window::get_position_with_decorations);
3162
ClassDB::bind_method(D_METHOD("get_size_with_decorations"), &Window::get_size_with_decorations);
3163
3164
ClassDB::bind_method(D_METHOD("set_max_size", "max_size"), &Window::set_max_size);
3165
ClassDB::bind_method(D_METHOD("get_max_size"), &Window::get_max_size);
3166
3167
ClassDB::bind_method(D_METHOD("set_min_size", "min_size"), &Window::set_min_size);
3168
ClassDB::bind_method(D_METHOD("get_min_size"), &Window::get_min_size);
3169
3170
ClassDB::bind_method(D_METHOD("set_mode", "mode"), &Window::set_mode);
3171
ClassDB::bind_method(D_METHOD("get_mode"), &Window::get_mode);
3172
3173
ClassDB::bind_method(D_METHOD("set_flag", "flag", "enabled"), &Window::set_flag);
3174
ClassDB::bind_method(D_METHOD("get_flag", "flag"), &Window::get_flag);
3175
3176
ClassDB::bind_method(D_METHOD("is_maximize_allowed"), &Window::is_maximize_allowed);
3177
3178
ClassDB::bind_method(D_METHOD("request_attention"), &Window::request_attention);
3179
3180
#ifndef DISABLE_DEPRECATED
3181
ClassDB::bind_method(D_METHOD("move_to_foreground"), &Window::move_to_foreground);
3182
#endif // DISABLE_DEPRECATED
3183
3184
ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Window::set_visible);
3185
ClassDB::bind_method(D_METHOD("is_visible"), &Window::is_visible);
3186
3187
ClassDB::bind_method(D_METHOD("hide"), &Window::hide);
3188
ClassDB::bind_method(D_METHOD("show"), &Window::show);
3189
3190
ClassDB::bind_method(D_METHOD("set_transient", "transient"), &Window::set_transient);
3191
ClassDB::bind_method(D_METHOD("is_transient"), &Window::is_transient);
3192
3193
ClassDB::bind_method(D_METHOD("set_transient_to_focused", "enable"), &Window::set_transient_to_focused);
3194
ClassDB::bind_method(D_METHOD("is_transient_to_focused"), &Window::is_transient_to_focused);
3195
3196
ClassDB::bind_method(D_METHOD("set_exclusive", "exclusive"), &Window::set_exclusive);
3197
ClassDB::bind_method(D_METHOD("is_exclusive"), &Window::is_exclusive);
3198
3199
ClassDB::bind_method(D_METHOD("set_unparent_when_invisible", "unparent"), &Window::set_unparent_when_invisible);
3200
3201
ClassDB::bind_method(D_METHOD("can_draw"), &Window::can_draw);
3202
ClassDB::bind_method(D_METHOD("has_focus"), &Window::has_focus);
3203
ClassDB::bind_method(D_METHOD("grab_focus"), &Window::grab_focus);
3204
3205
ClassDB::bind_method(D_METHOD("start_drag"), &Window::start_drag);
3206
ClassDB::bind_method(D_METHOD("start_resize", "edge"), &Window::start_resize);
3207
3208
ClassDB::bind_method(D_METHOD("set_ime_active", "active"), &Window::set_ime_active);
3209
ClassDB::bind_method(D_METHOD("set_ime_position", "position"), &Window::set_ime_position);
3210
3211
ClassDB::bind_method(D_METHOD("is_embedded"), &Window::is_embedded);
3212
3213
ClassDB::bind_method(D_METHOD("get_contents_minimum_size"), &Window::get_contents_minimum_size);
3214
3215
ClassDB::bind_method(D_METHOD("set_force_native", "force_native"), &Window::set_force_native);
3216
ClassDB::bind_method(D_METHOD("get_force_native"), &Window::get_force_native);
3217
3218
ClassDB::bind_method(D_METHOD("set_content_scale_size", "size"), &Window::set_content_scale_size);
3219
ClassDB::bind_method(D_METHOD("get_content_scale_size"), &Window::get_content_scale_size);
3220
3221
ClassDB::bind_method(D_METHOD("set_content_scale_mode", "mode"), &Window::set_content_scale_mode);
3222
ClassDB::bind_method(D_METHOD("get_content_scale_mode"), &Window::get_content_scale_mode);
3223
3224
ClassDB::bind_method(D_METHOD("set_content_scale_aspect", "aspect"), &Window::set_content_scale_aspect);
3225
ClassDB::bind_method(D_METHOD("get_content_scale_aspect"), &Window::get_content_scale_aspect);
3226
3227
ClassDB::bind_method(D_METHOD("set_content_scale_stretch", "stretch"), &Window::set_content_scale_stretch);
3228
ClassDB::bind_method(D_METHOD("get_content_scale_stretch"), &Window::get_content_scale_stretch);
3229
3230
ClassDB::bind_method(D_METHOD("set_keep_title_visible", "title_visible"), &Window::set_keep_title_visible);
3231
ClassDB::bind_method(D_METHOD("get_keep_title_visible"), &Window::get_keep_title_visible);
3232
3233
ClassDB::bind_method(D_METHOD("set_content_scale_factor", "factor"), &Window::set_content_scale_factor);
3234
ClassDB::bind_method(D_METHOD("get_content_scale_factor"), &Window::get_content_scale_factor);
3235
3236
ClassDB::bind_method(D_METHOD("set_mouse_passthrough_polygon", "polygon"), &Window::set_mouse_passthrough_polygon);
3237
ClassDB::bind_method(D_METHOD("get_mouse_passthrough_polygon"), &Window::get_mouse_passthrough_polygon);
3238
3239
ClassDB::bind_method(D_METHOD("set_wrap_controls", "enable"), &Window::set_wrap_controls);
3240
ClassDB::bind_method(D_METHOD("is_wrapping_controls"), &Window::is_wrapping_controls);
3241
ClassDB::bind_method(D_METHOD("child_controls_changed"), &Window::child_controls_changed);
3242
3243
ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Window::set_theme);
3244
ClassDB::bind_method(D_METHOD("get_theme"), &Window::get_theme);
3245
3246
ClassDB::bind_method(D_METHOD("set_theme_type_variation", "theme_type"), &Window::set_theme_type_variation);
3247
ClassDB::bind_method(D_METHOD("get_theme_type_variation"), &Window::get_theme_type_variation);
3248
3249
ClassDB::bind_method(D_METHOD("begin_bulk_theme_override"), &Window::begin_bulk_theme_override);
3250
ClassDB::bind_method(D_METHOD("end_bulk_theme_override"), &Window::end_bulk_theme_override);
3251
3252
ClassDB::bind_method(D_METHOD("add_theme_icon_override", "name", "texture"), &Window::add_theme_icon_override);
3253
ClassDB::bind_method(D_METHOD("add_theme_stylebox_override", "name", "stylebox"), &Window::add_theme_style_override);
3254
ClassDB::bind_method(D_METHOD("add_theme_font_override", "name", "font"), &Window::add_theme_font_override);
3255
ClassDB::bind_method(D_METHOD("add_theme_font_size_override", "name", "font_size"), &Window::add_theme_font_size_override);
3256
ClassDB::bind_method(D_METHOD("add_theme_color_override", "name", "color"), &Window::add_theme_color_override);
3257
ClassDB::bind_method(D_METHOD("add_theme_constant_override", "name", "constant"), &Window::add_theme_constant_override);
3258
3259
ClassDB::bind_method(D_METHOD("remove_theme_icon_override", "name"), &Window::remove_theme_icon_override);
3260
ClassDB::bind_method(D_METHOD("remove_theme_stylebox_override", "name"), &Window::remove_theme_style_override);
3261
ClassDB::bind_method(D_METHOD("remove_theme_font_override", "name"), &Window::remove_theme_font_override);
3262
ClassDB::bind_method(D_METHOD("remove_theme_font_size_override", "name"), &Window::remove_theme_font_size_override);
3263
ClassDB::bind_method(D_METHOD("remove_theme_color_override", "name"), &Window::remove_theme_color_override);
3264
ClassDB::bind_method(D_METHOD("remove_theme_constant_override", "name"), &Window::remove_theme_constant_override);
3265
3266
ClassDB::bind_method(D_METHOD("get_theme_icon", "name", "theme_type"), &Window::get_theme_icon, DEFVAL(StringName()));
3267
ClassDB::bind_method(D_METHOD("get_theme_stylebox", "name", "theme_type"), &Window::get_theme_stylebox, DEFVAL(StringName()));
3268
ClassDB::bind_method(D_METHOD("get_theme_font", "name", "theme_type"), &Window::get_theme_font, DEFVAL(StringName()));
3269
ClassDB::bind_method(D_METHOD("get_theme_font_size", "name", "theme_type"), &Window::get_theme_font_size, DEFVAL(StringName()));
3270
ClassDB::bind_method(D_METHOD("get_theme_color", "name", "theme_type"), &Window::get_theme_color, DEFVAL(StringName()));
3271
ClassDB::bind_method(D_METHOD("get_theme_constant", "name", "theme_type"), &Window::get_theme_constant, DEFVAL(StringName()));
3272
3273
ClassDB::bind_method(D_METHOD("has_theme_icon_override", "name"), &Window::has_theme_icon_override);
3274
ClassDB::bind_method(D_METHOD("has_theme_stylebox_override", "name"), &Window::has_theme_stylebox_override);
3275
ClassDB::bind_method(D_METHOD("has_theme_font_override", "name"), &Window::has_theme_font_override);
3276
ClassDB::bind_method(D_METHOD("has_theme_font_size_override", "name"), &Window::has_theme_font_size_override);
3277
ClassDB::bind_method(D_METHOD("has_theme_color_override", "name"), &Window::has_theme_color_override);
3278
ClassDB::bind_method(D_METHOD("has_theme_constant_override", "name"), &Window::has_theme_constant_override);
3279
3280
ClassDB::bind_method(D_METHOD("has_theme_icon", "name", "theme_type"), &Window::has_theme_icon, DEFVAL(StringName()));
3281
ClassDB::bind_method(D_METHOD("has_theme_stylebox", "name", "theme_type"), &Window::has_theme_stylebox, DEFVAL(StringName()));
3282
ClassDB::bind_method(D_METHOD("has_theme_font", "name", "theme_type"), &Window::has_theme_font, DEFVAL(StringName()));
3283
ClassDB::bind_method(D_METHOD("has_theme_font_size", "name", "theme_type"), &Window::has_theme_font_size, DEFVAL(StringName()));
3284
ClassDB::bind_method(D_METHOD("has_theme_color", "name", "theme_type"), &Window::has_theme_color, DEFVAL(StringName()));
3285
ClassDB::bind_method(D_METHOD("has_theme_constant", "name", "theme_type"), &Window::has_theme_constant, DEFVAL(StringName()));
3286
3287
ClassDB::bind_method(D_METHOD("get_theme_default_base_scale"), &Window::get_theme_default_base_scale);
3288
ClassDB::bind_method(D_METHOD("get_theme_default_font"), &Window::get_theme_default_font);
3289
ClassDB::bind_method(D_METHOD("get_theme_default_font_size"), &Window::get_theme_default_font_size);
3290
3291
ClassDB::bind_method(D_METHOD("get_window_id"), &Window::get_window_id);
3292
3293
ClassDB::bind_method(D_METHOD("set_accessibility_name", "name"), &Window::set_accessibility_name);
3294
ClassDB::bind_method(D_METHOD("get_accessibility_name"), &Window::get_accessibility_name);
3295
ClassDB::bind_method(D_METHOD("set_accessibility_description", "description"), &Window::set_accessibility_description);
3296
ClassDB::bind_method(D_METHOD("get_accessibility_description"), &Window::get_accessibility_description);
3297
3298
ClassDB::bind_static_method("Window", D_METHOD("get_focused_window"), &Window::get_focused_window);
3299
3300
ClassDB::bind_method(D_METHOD("set_layout_direction", "direction"), &Window::set_layout_direction);
3301
ClassDB::bind_method(D_METHOD("get_layout_direction"), &Window::get_layout_direction);
3302
ClassDB::bind_method(D_METHOD("is_layout_rtl"), &Window::is_layout_rtl);
3303
3304
#ifndef DISABLE_DEPRECATED
3305
ClassDB::bind_method(D_METHOD("set_auto_translate", "enable"), &Window::set_auto_translate);
3306
ClassDB::bind_method(D_METHOD("is_auto_translating"), &Window::is_auto_translating);
3307
3308
ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &Window::set_use_font_oversampling);
3309
ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &Window::is_using_font_oversampling);
3310
#endif
3311
3312
ClassDB::bind_method(D_METHOD("popup", "rect"), &Window::popup, DEFVAL(Rect2i()));
3313
ClassDB::bind_method(D_METHOD("popup_on_parent", "parent_rect"), &Window::popup_on_parent);
3314
ClassDB::bind_method(D_METHOD("popup_centered", "minsize"), &Window::popup_centered, DEFVAL(Size2i()));
3315
ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Window::popup_centered_ratio, DEFVAL(0.8));
3316
ClassDB::bind_method(D_METHOD("popup_centered_clamped", "minsize", "fallback_ratio"), &Window::popup_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
3317
3318
ClassDB::bind_method(D_METHOD("popup_exclusive", "from_node", "rect"), &Window::popup_exclusive, DEFVAL(Rect2i()));
3319
ClassDB::bind_method(D_METHOD("popup_exclusive_on_parent", "from_node", "parent_rect"), &Window::popup_exclusive_on_parent);
3320
ClassDB::bind_method(D_METHOD("popup_exclusive_centered", "from_node", "minsize"), &Window::popup_exclusive_centered, DEFVAL(Size2i()));
3321
ClassDB::bind_method(D_METHOD("popup_exclusive_centered_ratio", "from_node", "ratio"), &Window::popup_exclusive_centered_ratio, DEFVAL(0.8));
3322
ClassDB::bind_method(D_METHOD("popup_exclusive_centered_clamped", "from_node", "minsize", "fallback_ratio"), &Window::popup_exclusive_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
3323
3324
// Keep the enum values in sync with the `Mode` enum.
3325
ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Windowed,Minimized,Maximized,Fullscreen,Exclusive Fullscreen"), "set_mode", "get_mode");
3326
3327
ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
3328
3329
// Keep the enum values in sync with the `WindowInitialPosition` enum.
3330
ADD_PROPERTY(PropertyInfo(Variant::INT, "initial_position", PROPERTY_HINT_ENUM, "Absolute,Center of Primary Screen,Center of Main Window Screen,Center of Other Screen,Center of Screen With Mouse Pointer,Center of Screen With Keyboard Focus"), "set_initial_position", "get_initial_position");
3331
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
3332
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
3333
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_screen", PROPERTY_HINT_RANGE, "0,64,1,or_greater"), "set_current_screen", "get_current_screen");
3334
3335
ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "mouse_passthrough_polygon"), "set_mouse_passthrough_polygon", "get_mouse_passthrough_polygon");
3336
3337
ADD_GROUP("Flags", "");
3338
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
3339
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wrap_controls"), "set_wrap_controls", "is_wrapping_controls");
3340
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient"), "set_transient", "is_transient");
3341
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient_to_focused"), "set_transient_to_focused", "is_transient_to_focused");
3342
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclusive"), "set_exclusive", "is_exclusive");
3343
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unresizable"), "set_flag", "get_flag", FLAG_RESIZE_DISABLED);
3344
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "borderless"), "set_flag", "get_flag", FLAG_BORDERLESS);
3345
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "always_on_top"), "set_flag", "get_flag", FLAG_ALWAYS_ON_TOP);
3346
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_flag", "get_flag", FLAG_TRANSPARENT);
3347
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unfocusable"), "set_flag", "get_flag", FLAG_NO_FOCUS);
3348
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "popup_window"), "set_flag", "get_flag", FLAG_POPUP);
3349
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "extend_to_title"), "set_flag", "get_flag", FLAG_EXTEND_TO_TITLE);
3350
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "mouse_passthrough"), "set_flag", "get_flag", FLAG_MOUSE_PASSTHROUGH);
3351
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "sharp_corners"), "set_flag", "get_flag", FLAG_SHARP_CORNERS);
3352
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "exclude_from_capture"), "set_flag", "get_flag", FLAG_EXCLUDE_FROM_CAPTURE);
3353
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "popup_wm_hint"), "set_flag", "get_flag", FLAG_POPUP_WM_HINT);
3354
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "minimize_disabled"), "set_flag", "get_flag", FLAG_MINIMIZE_DISABLED);
3355
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "maximize_disabled"), "set_flag", "get_flag", FLAG_MAXIMIZE_DISABLED);
3356
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_native"), "set_force_native", "get_force_native");
3357
3358
ADD_GROUP("Limits", "");
3359
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "min_size", PROPERTY_HINT_NONE, "suffix:px"), "set_min_size", "get_min_size");
3360
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "max_size", PROPERTY_HINT_NONE, "suffix:px"), "set_max_size", "get_max_size");
3361
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_title_visible"), "set_keep_title_visible", "get_keep_title_visible");
3362
3363
ADD_GROUP("Content Scale", "content_scale_");
3364
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "content_scale_size"), "set_content_scale_size", "get_content_scale_size");
3365
ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_mode", PROPERTY_HINT_ENUM, "Disabled,Canvas Items,Viewport"), "set_content_scale_mode", "get_content_scale_mode");
3366
ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_aspect", PROPERTY_HINT_ENUM, "Ignore,Keep,Keep Width,Keep Height,Expand"), "set_content_scale_aspect", "get_content_scale_aspect");
3367
ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_stretch", PROPERTY_HINT_ENUM, "Fractional,Integer"), "set_content_scale_stretch", "get_content_scale_stretch");
3368
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "content_scale_factor", PROPERTY_HINT_RANGE, "0.5,8.0,0.01"), "set_content_scale_factor", "get_content_scale_factor");
3369
3370
#ifndef DISABLE_DEPRECATED
3371
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_translate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_auto_translate", "is_auto_translating");
3372
#endif
3373
3374
ADD_GROUP("Accessibility", "accessibility_");
3375
ADD_PROPERTY(PropertyInfo(Variant::STRING, "accessibility_name"), "set_accessibility_name", "get_accessibility_name");
3376
ADD_PROPERTY(PropertyInfo(Variant::STRING, "accessibility_description"), "set_accessibility_description", "get_accessibility_description");
3377
3378
ADD_GROUP("Theme", "theme_");
3379
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
3380
ADD_PROPERTY(PropertyInfo(Variant::STRING, "theme_type_variation", PROPERTY_HINT_ENUM_SUGGESTION), "set_theme_type_variation", "get_theme_type_variation");
3381
3382
ADD_SIGNAL(MethodInfo("window_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
3383
ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));
3384
ADD_SIGNAL(MethodInfo("mouse_entered"));
3385
ADD_SIGNAL(MethodInfo("mouse_exited"));
3386
ADD_SIGNAL(MethodInfo("focus_entered"));
3387
ADD_SIGNAL(MethodInfo("focus_exited"));
3388
ADD_SIGNAL(MethodInfo("close_requested"));
3389
ADD_SIGNAL(MethodInfo("go_back_requested"));
3390
ADD_SIGNAL(MethodInfo("visibility_changed"));
3391
ADD_SIGNAL(MethodInfo("about_to_popup"));
3392
ADD_SIGNAL(MethodInfo("theme_changed"));
3393
ADD_SIGNAL(MethodInfo("dpi_changed"));
3394
ADD_SIGNAL(MethodInfo("titlebar_changed"));
3395
ADD_SIGNAL(MethodInfo("title_changed"));
3396
3397
BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
3398
BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);
3399
3400
BIND_ENUM_CONSTANT(MODE_WINDOWED);
3401
BIND_ENUM_CONSTANT(MODE_MINIMIZED);
3402
BIND_ENUM_CONSTANT(MODE_MAXIMIZED);
3403
BIND_ENUM_CONSTANT(MODE_FULLSCREEN);
3404
BIND_ENUM_CONSTANT(MODE_EXCLUSIVE_FULLSCREEN);
3405
3406
BIND_ENUM_CONSTANT(FLAG_RESIZE_DISABLED);
3407
BIND_ENUM_CONSTANT(FLAG_BORDERLESS);
3408
BIND_ENUM_CONSTANT(FLAG_ALWAYS_ON_TOP);
3409
BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
3410
BIND_ENUM_CONSTANT(FLAG_NO_FOCUS);
3411
BIND_ENUM_CONSTANT(FLAG_POPUP);
3412
BIND_ENUM_CONSTANT(FLAG_EXTEND_TO_TITLE);
3413
BIND_ENUM_CONSTANT(FLAG_MOUSE_PASSTHROUGH);
3414
BIND_ENUM_CONSTANT(FLAG_SHARP_CORNERS);
3415
BIND_ENUM_CONSTANT(FLAG_EXCLUDE_FROM_CAPTURE);
3416
BIND_ENUM_CONSTANT(FLAG_POPUP_WM_HINT);
3417
BIND_ENUM_CONSTANT(FLAG_MINIMIZE_DISABLED);
3418
BIND_ENUM_CONSTANT(FLAG_MAXIMIZE_DISABLED);
3419
BIND_ENUM_CONSTANT(FLAG_MAX);
3420
3421
BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_DISABLED);
3422
BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_CANVAS_ITEMS);
3423
BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_VIEWPORT);
3424
3425
BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_IGNORE);
3426
BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP);
3427
BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_WIDTH);
3428
BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_HEIGHT);
3429
BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_EXPAND);
3430
3431
BIND_ENUM_CONSTANT(CONTENT_SCALE_STRETCH_FRACTIONAL);
3432
BIND_ENUM_CONSTANT(CONTENT_SCALE_STRETCH_INTEGER);
3433
3434
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_INHERITED);
3435
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_APPLICATION_LOCALE);
3436
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LTR);
3437
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_RTL);
3438
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_SYSTEM_LOCALE);
3439
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_MAX);
3440
#ifndef DISABLE_DEPRECATED
3441
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LOCALE);
3442
#endif // DISABLE_DEPRECATED
3443
3444
BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_ABSOLUTE);
3445
BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN);
3446
BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN);
3447
BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN);
3448
BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS);
3449
BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS);
3450
3451
GDVIRTUAL_BIND(_get_contents_minimum_size);
3452
3453
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Window, embedded_border);
3454
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Window, embedded_unfocused_border);
3455
3456
BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Window, title_font);
3457
BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Window, title_font_size);
3458
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Window, title_color);
3459
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, title_height);
3460
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Window, title_outline_modulate);
3461
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, title_outline_size);
3462
3463
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Window, close);
3464
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Window, close_pressed);
3465
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, close_h_offset);
3466
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, close_v_offset);
3467
3468
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, resize_margin);
3469
}
3470
3471
Window::Window() {
3472
RenderingServer *rendering_server = RenderingServer::get_singleton();
3473
if (rendering_server) {
3474
max_size = rendering_server->get_maximum_viewport_size();
3475
max_size_used = max_size; // Update max_size_used.
3476
}
3477
3478
theme_owner = memnew(ThemeOwner(this));
3479
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
3480
}
3481
3482
Window::~Window() {
3483
if (focused_window == this) {
3484
focused_window = nullptr;
3485
}
3486
memdelete(theme_owner);
3487
3488
// Resources need to be disconnected.
3489
for (KeyValue<StringName, Ref<Texture2D>> &E : theme_icon_override) {
3490
E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
3491
}
3492
for (KeyValue<StringName, Ref<StyleBox>> &E : theme_style_override) {
3493
E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
3494
}
3495
for (KeyValue<StringName, Ref<Font>> &E : theme_font_override) {
3496
E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
3497
}
3498
3499
// Then override maps can be simply cleared.
3500
theme_icon_override.clear();
3501
theme_style_override.clear();
3502
theme_font_override.clear();
3503
theme_font_size_override.clear();
3504
theme_color_override.clear();
3505
theme_constant_override.clear();
3506
}
3507
3508