Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/window.h
9896 views
1
/**************************************************************************/
2
/* window.h */
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
#pragma once
32
33
#include "scene/main/viewport.h"
34
#include "scene/resources/theme.h"
35
#include "servers/display_server.h"
36
37
class Font;
38
class Shortcut;
39
class StyleBox;
40
class ThemeOwner;
41
class ThemeContext;
42
43
class Window : public Viewport {
44
GDCLASS(Window, Viewport);
45
46
public:
47
// Keep synced with enum hint for `mode` property.
48
enum Mode {
49
MODE_WINDOWED = DisplayServer::WINDOW_MODE_WINDOWED,
50
MODE_MINIMIZED = DisplayServer::WINDOW_MODE_MINIMIZED,
51
MODE_MAXIMIZED = DisplayServer::WINDOW_MODE_MAXIMIZED,
52
MODE_FULLSCREEN = DisplayServer::WINDOW_MODE_FULLSCREEN,
53
MODE_EXCLUSIVE_FULLSCREEN = DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN,
54
};
55
56
enum Flags {
57
FLAG_RESIZE_DISABLED = DisplayServer::WINDOW_FLAG_RESIZE_DISABLED,
58
FLAG_BORDERLESS = DisplayServer::WINDOW_FLAG_BORDERLESS,
59
FLAG_ALWAYS_ON_TOP = DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP,
60
FLAG_TRANSPARENT = DisplayServer::WINDOW_FLAG_TRANSPARENT,
61
FLAG_NO_FOCUS = DisplayServer::WINDOW_FLAG_NO_FOCUS,
62
FLAG_POPUP = DisplayServer::WINDOW_FLAG_POPUP,
63
FLAG_EXTEND_TO_TITLE = DisplayServer::WINDOW_FLAG_EXTEND_TO_TITLE,
64
FLAG_MOUSE_PASSTHROUGH = DisplayServer::WINDOW_FLAG_MOUSE_PASSTHROUGH,
65
FLAG_SHARP_CORNERS = DisplayServer::WINDOW_FLAG_SHARP_CORNERS,
66
FLAG_EXCLUDE_FROM_CAPTURE = DisplayServer::WINDOW_FLAG_EXCLUDE_FROM_CAPTURE,
67
FLAG_POPUP_WM_HINT = DisplayServer::WINDOW_FLAG_POPUP_WM_HINT,
68
FLAG_MINIMIZE_DISABLED = DisplayServer::WINDOW_FLAG_MINIMIZE_DISABLED,
69
FLAG_MAXIMIZE_DISABLED = DisplayServer::WINDOW_FLAG_MAXIMIZE_DISABLED,
70
FLAG_MAX = DisplayServer::WINDOW_FLAG_MAX,
71
};
72
73
enum ContentScaleMode {
74
CONTENT_SCALE_MODE_DISABLED,
75
CONTENT_SCALE_MODE_CANVAS_ITEMS,
76
CONTENT_SCALE_MODE_VIEWPORT,
77
};
78
79
enum ContentScaleAspect {
80
CONTENT_SCALE_ASPECT_IGNORE,
81
CONTENT_SCALE_ASPECT_KEEP,
82
CONTENT_SCALE_ASPECT_KEEP_WIDTH,
83
CONTENT_SCALE_ASPECT_KEEP_HEIGHT,
84
CONTENT_SCALE_ASPECT_EXPAND,
85
};
86
87
enum ContentScaleStretch {
88
CONTENT_SCALE_STRETCH_FRACTIONAL,
89
CONTENT_SCALE_STRETCH_INTEGER,
90
};
91
92
enum LayoutDirection {
93
LAYOUT_DIRECTION_INHERITED,
94
LAYOUT_DIRECTION_APPLICATION_LOCALE,
95
LAYOUT_DIRECTION_LTR,
96
LAYOUT_DIRECTION_RTL,
97
LAYOUT_DIRECTION_SYSTEM_LOCALE,
98
LAYOUT_DIRECTION_MAX,
99
#ifndef DISABLE_DEPRECATED
100
LAYOUT_DIRECTION_LOCALE = LAYOUT_DIRECTION_APPLICATION_LOCALE,
101
#endif // DISABLE_DEPRECATED
102
};
103
104
enum {
105
DEFAULT_WINDOW_SIZE = 100,
106
};
107
108
// Keep synced with enum hint for `initial_position` property and `display/window/size/initial_position_type` project setting.
109
enum WindowInitialPosition {
110
WINDOW_INITIAL_POSITION_ABSOLUTE,
111
WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN,
112
WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN,
113
WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN,
114
WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS,
115
WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS,
116
};
117
118
private:
119
DisplayServer::WindowID window_id = DisplayServer::INVALID_WINDOW_ID;
120
bool initialized = false;
121
122
String title;
123
String displayed_title;
124
mutable int current_screen = 0;
125
mutable Point2i position;
126
mutable Size2i size = Size2i(DEFAULT_WINDOW_SIZE, DEFAULT_WINDOW_SIZE);
127
mutable Size2i min_size;
128
mutable Size2i max_size;
129
mutable Vector<Vector2> mpath;
130
mutable Mode mode = MODE_WINDOWED;
131
mutable bool flags[FLAG_MAX] = {};
132
bool visible = true;
133
bool focused = false;
134
WindowInitialPosition initial_position = WINDOW_INITIAL_POSITION_ABSOLUTE;
135
bool force_native = false;
136
137
bool transient = false;
138
bool transient_to_focused = false;
139
bool exclusive = false;
140
bool wrap_controls = false;
141
bool updating_child_controls = false;
142
bool updating_embedded_window = false;
143
bool clamp_to_embedder = false;
144
bool unparent_when_invisible = false;
145
bool keep_title_visible = false;
146
147
LayoutDirection layout_dir = LAYOUT_DIRECTION_INHERITED;
148
149
void _update_child_controls();
150
void _update_embedded_window();
151
152
Size2i content_scale_size;
153
ContentScaleMode content_scale_mode = CONTENT_SCALE_MODE_DISABLED;
154
ContentScaleAspect content_scale_aspect = CONTENT_SCALE_ASPECT_IGNORE;
155
ContentScaleStretch content_scale_stretch = CONTENT_SCALE_STRETCH_FRACTIONAL;
156
real_t content_scale_factor = 1.0;
157
158
RID accessibility_title_element;
159
RID accessibility_announcement_element;
160
String announcement;
161
String accessibility_name;
162
String accessibility_description;
163
164
void _make_window();
165
void _clear_window();
166
void _update_from_window();
167
void _accessibility_notify_enter(Node *p_node);
168
void _accessibility_notify_exit(Node *p_node);
169
170
bool _try_parent_dialog(Node *p_from_node);
171
172
Size2i max_size_used;
173
174
Size2i _clamp_limit_size(const Size2i &p_limit_size);
175
Size2i _clamp_window_size(const Size2i &p_size);
176
void _validate_limit_size();
177
void _update_viewport_size();
178
void _update_window_size();
179
180
void _propagate_window_notification(Node *p_node, int p_notification);
181
182
void _update_window_callbacks();
183
184
Window *transient_parent = nullptr;
185
Window *exclusive_child = nullptr;
186
HashSet<Window *> transient_children;
187
188
void _clear_transient();
189
void _make_transient();
190
void _set_transient_exclusive_child(bool p_clear_invalid = false);
191
192
static Window *focused_window;
193
194
ThemeOwner *theme_owner = nullptr;
195
Ref<Theme> theme;
196
StringName theme_type_variation;
197
198
bool bulk_theme_override = false;
199
Theme::ThemeIconMap theme_icon_override;
200
Theme::ThemeStyleMap theme_style_override;
201
Theme::ThemeFontMap theme_font_override;
202
Theme::ThemeFontSizeMap theme_font_size_override;
203
Theme::ThemeColorMap theme_color_override;
204
Theme::ThemeConstantMap theme_constant_override;
205
206
mutable HashMap<StringName, Theme::ThemeIconMap> theme_icon_cache;
207
mutable HashMap<StringName, Theme::ThemeStyleMap> theme_style_cache;
208
mutable HashMap<StringName, Theme::ThemeFontMap> theme_font_cache;
209
mutable HashMap<StringName, Theme::ThemeFontSizeMap> theme_font_size_cache;
210
mutable HashMap<StringName, Theme::ThemeColorMap> theme_color_cache;
211
mutable HashMap<StringName, Theme::ThemeConstantMap> theme_constant_cache;
212
213
void _theme_changed();
214
void _notify_theme_override_changed();
215
void _invalidate_theme_cache();
216
217
struct ThemeCache {
218
Ref<StyleBox> embedded_border;
219
Ref<StyleBox> embedded_unfocused_border;
220
221
Ref<Font> title_font;
222
int title_font_size = 0;
223
Color title_color;
224
int title_height = 0;
225
Color title_outline_modulate;
226
int title_outline_size = 0;
227
228
Ref<Texture2D> close;
229
Ref<Texture2D> close_pressed;
230
int close_h_offset = 0;
231
int close_v_offset = 0;
232
233
int resize_margin = 0;
234
} theme_cache;
235
236
void _settings_changed();
237
238
Viewport *embedder = nullptr;
239
240
Transform2D window_transform;
241
242
friend class Viewport; //friend back, can call the methods below
243
244
void _window_input(const Ref<InputEvent> &p_ev);
245
void _window_input_text(const String &p_text);
246
void _window_drop_files(const Vector<String> &p_files);
247
void _rect_changed_callback(const Rect2i &p_callback);
248
void _event_callback(DisplayServer::WindowEvent p_event);
249
virtual bool _can_consume_input_events() const override;
250
251
bool mouse_in_window = false;
252
void _update_mouse_over(Vector2 p_pos) override;
253
void _mouse_leave_viewport() override;
254
255
void _update_displayed_title();
256
257
Ref<Shortcut> debugger_stop_shortcut;
258
259
static int root_layout_direction;
260
261
protected:
262
virtual void _pre_popup() {} // Called after "about_to_popup", but before window is shown.
263
virtual Rect2i _popup_adjust_rect() const { return Rect2i(); }
264
virtual void _post_popup() {}
265
266
virtual void _update_theme_item_cache();
267
virtual void _input_from_window(const Ref<InputEvent> &p_event) {}
268
269
void _notification(int p_what);
270
static void _bind_methods();
271
272
bool _set(const StringName &p_name, const Variant &p_value);
273
bool _get(const StringName &p_name, Variant &r_ret) const;
274
void _get_property_list(List<PropertyInfo> *p_list) const;
275
void _validate_property(PropertyInfo &p_property) const;
276
277
void _accessibility_action_grab_focus(const Variant &p_data) {
278
grab_focus();
279
}
280
281
virtual void add_child_notify(Node *p_child) override;
282
virtual void remove_child_notify(Node *p_child) override;
283
284
GDVIRTUAL0RC(Vector2, _get_contents_minimum_size)
285
286
public:
287
enum {
288
NOTIFICATION_VISIBILITY_CHANGED = 30,
289
NOTIFICATION_POST_POPUP = 31,
290
NOTIFICATION_THEME_CHANGED = 32
291
};
292
293
static void set_root_layout_direction(int p_root_dir);
294
static Window *get_from_id(DisplayServer::WindowID p_window_id);
295
296
RID get_accessibility_element() const override;
297
virtual RID get_focused_accessibility_element() const override;
298
299
void set_title(const String &p_title);
300
String get_title() const;
301
String get_displayed_title() const;
302
303
void set_initial_position(WindowInitialPosition p_initial_position);
304
WindowInitialPosition get_initial_position() const;
305
306
void set_force_native(bool p_force_native);
307
bool get_force_native() const;
308
309
void set_current_screen(int p_screen);
310
int get_current_screen() const;
311
312
void set_position(const Point2i &p_position);
313
Point2i get_position() const;
314
void move_to_center();
315
316
void set_size(const Size2i &p_size);
317
Size2i get_size() const;
318
void reset_size();
319
320
Point2i get_position_with_decorations() const;
321
Size2i get_size_with_decorations() const;
322
323
void set_max_size(const Size2i &p_max_size);
324
Size2i get_max_size() const;
325
326
void set_min_size(const Size2i &p_min_size);
327
Size2i get_min_size() const;
328
329
void set_mode(Mode p_mode);
330
Mode get_mode() const;
331
332
void set_flag(Flags p_flag, bool p_enabled);
333
bool get_flag(Flags p_flag) const;
334
335
bool is_popup() const;
336
337
bool is_maximize_allowed() const;
338
339
void request_attention();
340
#ifndef DISABLE_DEPRECATED
341
void move_to_foreground();
342
#endif // DISABLE_DEPRECATED
343
344
virtual void set_visible(bool p_visible);
345
bool is_visible() const;
346
347
void update_mouse_cursor_state() override;
348
349
void show();
350
void hide();
351
352
void set_transient(bool p_transient);
353
bool is_transient() const;
354
355
void set_transient_to_focused(bool p_transient_to_focused);
356
bool is_transient_to_focused() const;
357
358
void set_exclusive(bool p_exclusive);
359
bool is_exclusive() const;
360
361
void set_clamp_to_embedder(bool p_enable);
362
bool is_clamped_to_embedder() const;
363
364
void set_unparent_when_invisible(bool p_unparent);
365
366
bool is_in_edited_scene_root() const;
367
368
bool can_draw() const;
369
370
void set_ime_active(bool p_active);
371
void set_ime_position(const Point2i &p_pos);
372
373
bool is_embedded() const;
374
Viewport *get_embedder() const;
375
376
void set_content_scale_size(const Size2i &p_size);
377
Size2i get_content_scale_size() const;
378
379
void set_content_scale_mode(ContentScaleMode p_mode);
380
ContentScaleMode get_content_scale_mode() const;
381
382
void set_content_scale_aspect(ContentScaleAspect p_aspect);
383
ContentScaleAspect get_content_scale_aspect() const;
384
385
void set_content_scale_stretch(ContentScaleStretch p_stretch);
386
ContentScaleStretch get_content_scale_stretch() const;
387
388
void set_keep_title_visible(bool p_title_visible);
389
bool get_keep_title_visible() const;
390
391
void set_content_scale_factor(real_t p_factor);
392
real_t get_content_scale_factor() const;
393
394
void set_mouse_passthrough_polygon(const Vector<Vector2> &p_region);
395
Vector<Vector2> get_mouse_passthrough_polygon() const;
396
397
void set_wrap_controls(bool p_enable);
398
bool is_wrapping_controls() const;
399
void child_controls_changed();
400
401
Window *get_exclusive_child() const { return exclusive_child; }
402
HashSet<Window *> get_transient_children() const { return transient_children; }
403
Window *get_parent_visible_window() const;
404
Window *get_non_popup_window() const;
405
Viewport *get_parent_viewport() const;
406
407
virtual void popup(const Rect2i &p_screen_rect = Rect2i());
408
void popup_on_parent(const Rect2i &p_parent_rect);
409
void popup_centered(const Size2i &p_minsize = Size2i());
410
void popup_centered_ratio(float p_ratio = 0.8);
411
void popup_centered_clamped(const Size2i &p_size = Size2i(), float p_fallback_ratio = 0.75);
412
413
void popup_exclusive(Node *p_from_node, const Rect2i &p_screen_rect = Rect2i());
414
void popup_exclusive_on_parent(Node *p_from_node, const Rect2i &p_parent_rect);
415
void popup_exclusive_centered(Node *p_from_node, const Size2i &p_minsize = Size2i());
416
void popup_exclusive_centered_ratio(Node *p_from_node, float p_ratio = 0.8);
417
void popup_exclusive_centered_clamped(Node *p_from_node, const Size2i &p_size = Size2i(), float p_fallback_ratio = 0.75);
418
419
Rect2i fit_rect_in_parent(Rect2i p_rect, const Rect2i &p_parent_rect) const;
420
Size2 get_contents_minimum_size() const;
421
Size2 get_clamped_minimum_size() const;
422
423
void grab_focus();
424
bool has_focus() const;
425
bool has_focus_or_active_popup() const;
426
427
void start_drag();
428
void start_resize(DisplayServer::WindowResizeEdge p_edge);
429
430
Rect2i get_usable_parent_rect() const;
431
432
void set_accessibility_name(const String &p_name);
433
String get_accessibility_name() const;
434
435
void set_accessibility_description(const String &p_description);
436
String get_accessibility_description() const;
437
438
void accessibility_announcement(const String &p_announcement);
439
440
// Internationalization.
441
442
void set_layout_direction(LayoutDirection p_direction);
443
LayoutDirection get_layout_direction() const;
444
bool is_layout_rtl() const;
445
446
#ifndef DISABLE_DEPRECATED
447
void set_use_font_oversampling(bool p_oversampling);
448
bool is_using_font_oversampling() const;
449
450
void set_auto_translate(bool p_enable);
451
bool is_auto_translating() const;
452
#endif
453
454
// Theming.
455
456
void set_theme_owner_node(Node *p_node);
457
Node *get_theme_owner_node() const;
458
bool has_theme_owner_node() const;
459
460
void set_theme_context(ThemeContext *p_context, bool p_propagate = true);
461
462
void set_theme(const Ref<Theme> &p_theme);
463
Ref<Theme> get_theme() const;
464
465
void set_theme_type_variation(const StringName &p_theme_type);
466
StringName get_theme_type_variation() const;
467
468
void begin_bulk_theme_override();
469
void end_bulk_theme_override();
470
471
void add_theme_icon_override(const StringName &p_name, const Ref<Texture2D> &p_icon);
472
void add_theme_style_override(const StringName &p_name, const Ref<StyleBox> &p_style);
473
void add_theme_font_override(const StringName &p_name, const Ref<Font> &p_font);
474
void add_theme_font_size_override(const StringName &p_name, int p_font_size);
475
void add_theme_color_override(const StringName &p_name, const Color &p_color);
476
void add_theme_constant_override(const StringName &p_name, int p_constant);
477
478
void remove_theme_icon_override(const StringName &p_name);
479
void remove_theme_style_override(const StringName &p_name);
480
void remove_theme_font_override(const StringName &p_name);
481
void remove_theme_font_size_override(const StringName &p_name);
482
void remove_theme_color_override(const StringName &p_name);
483
void remove_theme_constant_override(const StringName &p_name);
484
485
Ref<Texture2D> get_theme_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
486
Ref<StyleBox> get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
487
Ref<Font> get_theme_font(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
488
int get_theme_font_size(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
489
Color get_theme_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
490
int get_theme_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
491
Variant get_theme_item(Theme::DataType p_data_type, const StringName &p_name, const StringName &p_theme_type = StringName()) const;
492
#ifdef TOOLS_ENABLED
493
Ref<Texture2D> get_editor_theme_icon(const StringName &p_name) const;
494
Ref<Texture2D> get_editor_theme_native_menu_icon(const StringName &p_name, bool p_global_menu, bool p_dark_mode) const;
495
#endif
496
497
bool has_theme_icon_override(const StringName &p_name) const;
498
bool has_theme_stylebox_override(const StringName &p_name) const;
499
bool has_theme_font_override(const StringName &p_name) const;
500
bool has_theme_font_size_override(const StringName &p_name) const;
501
bool has_theme_color_override(const StringName &p_name) const;
502
bool has_theme_constant_override(const StringName &p_name) const;
503
504
bool has_theme_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
505
bool has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
506
bool has_theme_font(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
507
bool has_theme_font_size(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
508
bool has_theme_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
509
bool has_theme_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
510
511
float get_theme_default_base_scale() const;
512
Ref<Font> get_theme_default_font() const;
513
int get_theme_default_font_size() const;
514
515
virtual Transform2D get_final_transform() const override;
516
virtual Transform2D get_screen_transform_internal(bool p_absolute_position = false) const override;
517
virtual Transform2D get_popup_base_transform() const override;
518
virtual Transform2D get_popup_base_transform_native() const override;
519
virtual Viewport *get_section_root_viewport() const override;
520
virtual bool is_attached_in_viewport() const override;
521
522
Rect2i get_parent_rect() const;
523
virtual DisplayServer::WindowID get_window_id() const override;
524
static Window *get_focused_window() { return focused_window; }
525
526
virtual Size2 _get_contents_minimum_size() const;
527
528
Window();
529
~Window();
530
};
531
532
VARIANT_ENUM_CAST(Window::Mode);
533
VARIANT_ENUM_CAST(Window::Flags);
534
VARIANT_ENUM_CAST(Window::ContentScaleMode);
535
VARIANT_ENUM_CAST(Window::ContentScaleAspect);
536
VARIANT_ENUM_CAST(Window::ContentScaleStretch);
537
VARIANT_ENUM_CAST(Window::LayoutDirection);
538
VARIANT_ENUM_CAST(Window::WindowInitialPosition);
539
540