Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/window.h
21152 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/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
Rect2i nonclient_area;
175
176
Size2i _clamp_limit_size(const Size2i &p_limit_size);
177
Size2i _clamp_window_size(const Size2i &p_size);
178
void _validate_limit_size();
179
void _update_viewport_size();
180
void _update_window_size();
181
182
void _propagate_window_notification(Node *p_node, int p_notification);
183
184
void _update_window_callbacks();
185
186
Window *transient_parent = nullptr;
187
Window *exclusive_child = nullptr;
188
HashSet<Window *> transient_children;
189
190
void _clear_transient();
191
void _make_transient();
192
void _set_transient_exclusive_child(bool p_clear_invalid = false);
193
194
static Window *focused_window;
195
196
ThemeOwner *theme_owner = nullptr;
197
Ref<Theme> theme;
198
StringName theme_type_variation;
199
200
bool bulk_theme_override = false;
201
Theme::ThemeIconMap theme_icon_override;
202
Theme::ThemeStyleMap theme_style_override;
203
Theme::ThemeFontMap theme_font_override;
204
Theme::ThemeFontSizeMap theme_font_size_override;
205
Theme::ThemeColorMap theme_color_override;
206
Theme::ThemeConstantMap theme_constant_override;
207
208
mutable HashMap<StringName, Theme::ThemeIconMap> theme_icon_cache;
209
mutable HashMap<StringName, Theme::ThemeStyleMap> theme_style_cache;
210
mutable HashMap<StringName, Theme::ThemeFontMap> theme_font_cache;
211
mutable HashMap<StringName, Theme::ThemeFontSizeMap> theme_font_size_cache;
212
mutable HashMap<StringName, Theme::ThemeColorMap> theme_color_cache;
213
mutable HashMap<StringName, Theme::ThemeConstantMap> theme_constant_cache;
214
215
void _theme_changed();
216
void _notify_theme_override_changed();
217
void _invalidate_theme_cache();
218
219
struct ThemeCache {
220
Ref<StyleBox> embedded_border;
221
Ref<StyleBox> embedded_unfocused_border;
222
223
Ref<Font> title_font;
224
int title_font_size = 0;
225
Color title_color;
226
int title_height = 0;
227
Color title_outline_modulate;
228
int title_outline_size = 0;
229
230
Ref<Texture2D> close;
231
Ref<Texture2D> close_pressed;
232
int close_h_offset = 0;
233
int close_v_offset = 0;
234
235
int resize_margin = 0;
236
} theme_cache;
237
238
void _settings_changed();
239
240
Viewport *embedder = nullptr;
241
242
Transform2D window_transform;
243
244
friend class Viewport; //friend back, can call the methods below
245
246
void _window_input(const Ref<InputEvent> &p_ev);
247
void _window_input_text(const String &p_text, bool p_emit_signal = false);
248
void _window_drop_files(const Vector<String> &p_files);
249
void _rect_changed_callback(const Rect2i &p_callback);
250
void _event_callback(DisplayServer::WindowEvent p_event);
251
virtual bool _can_consume_input_events() const override;
252
253
bool mouse_in_window = false;
254
void _update_mouse_over(Vector2 p_pos) override;
255
void _mouse_leave_viewport() override;
256
257
void _update_displayed_title();
258
259
static int root_layout_direction;
260
261
protected:
262
virtual void _popup_base(const Rect2i &p_screen_rect = Rect2i());
263
virtual void _pre_popup() {} // Called after "about_to_popup", but before window is shown.
264
virtual Rect2i _popup_adjust_rect() const { return Rect2i(); }
265
virtual void _post_popup() {}
266
267
virtual void _update_theme_item_cache();
268
virtual void _input_from_window(const Ref<InputEvent> &p_event) {}
269
270
void _notification(int p_what);
271
static void _bind_methods();
272
273
bool _set(const StringName &p_name, const Variant &p_value);
274
bool _get(const StringName &p_name, Variant &r_ret) const;
275
void _get_property_list(List<PropertyInfo> *p_list) const;
276
void _validate_property(PropertyInfo &p_property) const;
277
278
void _accessibility_action_grab_focus(const Variant &p_data) {
279
grab_focus();
280
}
281
282
virtual void add_child_notify(Node *p_child) override;
283
virtual void remove_child_notify(Node *p_child) override;
284
285
GDVIRTUAL0RC(Vector2, _get_contents_minimum_size)
286
287
public:
288
enum {
289
NOTIFICATION_VISIBILITY_CHANGED = 30,
290
NOTIFICATION_POST_POPUP = 31,
291
NOTIFICATION_THEME_CHANGED = 32
292
};
293
294
static void set_root_layout_direction(int p_root_dir);
295
static Window *get_from_id(DisplayServer::WindowID p_window_id);
296
297
RID get_accessibility_element() const override;
298
virtual RID get_focused_accessibility_element() const override;
299
300
void set_title(const String &p_title);
301
String get_title() const;
302
String get_displayed_title() const;
303
304
void set_initial_position(WindowInitialPosition p_initial_position);
305
WindowInitialPosition get_initial_position() const;
306
307
void set_force_native(bool p_force_native);
308
bool get_force_native() const;
309
310
void set_current_screen(int p_screen);
311
int get_current_screen() const;
312
313
void set_position(const Point2i &p_position);
314
Point2i get_position() const;
315
void move_to_center();
316
317
void set_size(const Size2i &p_size);
318
Size2i get_size() const;
319
void reset_size();
320
321
Point2i get_position_with_decorations() const;
322
Size2i get_size_with_decorations() const;
323
324
void set_max_size(const Size2i &p_max_size);
325
Size2i get_max_size() const;
326
327
void set_min_size(const Size2i &p_min_size);
328
Size2i get_min_size() const;
329
330
void set_mode(Mode p_mode);
331
Mode get_mode() const;
332
333
void set_flag(Flags p_flag, bool p_enabled);
334
bool get_flag(Flags p_flag) const;
335
336
bool is_popup() const;
337
338
bool is_maximize_allowed() const;
339
340
void request_attention();
341
#ifndef DISABLE_DEPRECATED
342
void move_to_foreground();
343
#endif // DISABLE_DEPRECATED
344
345
virtual void set_visible(bool p_visible);
346
bool is_visible() const;
347
348
void update_mouse_cursor_state() override;
349
350
void show();
351
void hide();
352
353
void set_transient(bool p_transient);
354
bool is_transient() const;
355
356
void set_transient_to_focused(bool p_transient_to_focused);
357
bool is_transient_to_focused() const;
358
359
void set_exclusive(bool p_exclusive);
360
bool is_exclusive() const;
361
362
void set_clamp_to_embedder(bool p_enable);
363
bool is_clamped_to_embedder() const;
364
365
void set_unparent_when_invisible(bool p_unparent);
366
367
bool is_in_edited_scene_root() const;
368
369
bool can_draw() const;
370
371
void set_ime_active(bool p_active);
372
void set_ime_position(const Point2i &p_pos);
373
374
bool is_embedded() const;
375
Viewport *get_embedder() const;
376
377
void set_content_scale_size(const Size2i &p_size);
378
Size2i get_content_scale_size() const;
379
380
void set_content_scale_mode(ContentScaleMode p_mode);
381
ContentScaleMode get_content_scale_mode() const;
382
383
void set_content_scale_aspect(ContentScaleAspect p_aspect);
384
ContentScaleAspect get_content_scale_aspect() const;
385
386
void set_content_scale_stretch(ContentScaleStretch p_stretch);
387
ContentScaleStretch get_content_scale_stretch() const;
388
389
void set_keep_title_visible(bool p_title_visible);
390
bool get_keep_title_visible() const;
391
392
void set_content_scale_factor(real_t p_factor);
393
real_t get_content_scale_factor() const;
394
395
void set_nonclient_area(const Rect2i &p_rect);
396
Rect2i get_nonclient_area() const;
397
398
void set_mouse_passthrough_polygon(const Vector<Vector2> &p_region);
399
Vector<Vector2> get_mouse_passthrough_polygon() const;
400
401
void set_wrap_controls(bool p_enable);
402
bool is_wrapping_controls() const;
403
void child_controls_changed();
404
405
Window *get_exclusive_child() const { return exclusive_child; }
406
HashSet<Window *> get_transient_children() const { return transient_children; }
407
Window *get_parent_visible_window() const;
408
Window *get_non_popup_window() const;
409
Viewport *get_parent_viewport() const;
410
411
void popup(const Rect2i &p_screen_rect = Rect2i());
412
void popup_on_parent(const Rect2i &p_parent_rect);
413
void popup_centered(const Size2i &p_minsize = Size2i());
414
void popup_centered_ratio(float p_ratio = 0.8);
415
void popup_centered_clamped(const Size2i &p_size = Size2i(), float p_fallback_ratio = 0.75);
416
417
void popup_exclusive(Node *p_from_node, const Rect2i &p_screen_rect = Rect2i());
418
void popup_exclusive_on_parent(Node *p_from_node, const Rect2i &p_parent_rect);
419
void popup_exclusive_centered(Node *p_from_node, const Size2i &p_minsize = Size2i());
420
void popup_exclusive_centered_ratio(Node *p_from_node, float p_ratio = 0.8);
421
void popup_exclusive_centered_clamped(Node *p_from_node, const Size2i &p_size = Size2i(), float p_fallback_ratio = 0.75);
422
423
Rect2i fit_rect_in_parent(Rect2i p_rect, const Rect2i &p_parent_rect) const;
424
Size2 get_contents_minimum_size() const;
425
Size2 get_clamped_minimum_size() const;
426
427
void grab_focus();
428
bool has_focus() const;
429
bool has_focus_or_active_popup() const;
430
431
void start_drag();
432
void start_resize(DisplayServer::WindowResizeEdge p_edge);
433
434
Rect2i get_usable_parent_rect() const;
435
436
void set_accessibility_name(const String &p_name);
437
String get_accessibility_name() const;
438
439
void set_accessibility_description(const String &p_description);
440
String get_accessibility_description() const;
441
442
void accessibility_announcement(const String &p_announcement);
443
444
// Internationalization.
445
446
void set_layout_direction(LayoutDirection p_direction);
447
LayoutDirection get_layout_direction() const;
448
bool is_layout_rtl() const;
449
450
#ifndef DISABLE_DEPRECATED
451
void set_use_font_oversampling(bool p_oversampling);
452
bool is_using_font_oversampling() const;
453
454
void set_auto_translate(bool p_enable);
455
bool is_auto_translating() const;
456
#endif
457
458
// Theming.
459
460
void set_theme_owner_node(Node *p_node);
461
Node *get_theme_owner_node() const;
462
bool has_theme_owner_node() const;
463
464
void set_theme_context(ThemeContext *p_context, bool p_propagate = true);
465
466
void set_theme(const Ref<Theme> &p_theme);
467
Ref<Theme> get_theme() const;
468
469
void set_theme_type_variation(const StringName &p_theme_type);
470
StringName get_theme_type_variation() const;
471
472
void begin_bulk_theme_override();
473
void end_bulk_theme_override();
474
475
void add_theme_icon_override(const StringName &p_name, const Ref<Texture2D> &p_icon);
476
void add_theme_style_override(const StringName &p_name, const Ref<StyleBox> &p_style);
477
void add_theme_font_override(const StringName &p_name, const Ref<Font> &p_font);
478
void add_theme_font_size_override(const StringName &p_name, int p_font_size);
479
void add_theme_color_override(const StringName &p_name, const Color &p_color);
480
void add_theme_constant_override(const StringName &p_name, int p_constant);
481
482
void remove_theme_icon_override(const StringName &p_name);
483
void remove_theme_style_override(const StringName &p_name);
484
void remove_theme_font_override(const StringName &p_name);
485
void remove_theme_font_size_override(const StringName &p_name);
486
void remove_theme_color_override(const StringName &p_name);
487
void remove_theme_constant_override(const StringName &p_name);
488
489
Ref<Texture2D> get_theme_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
490
Ref<StyleBox> get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
491
Ref<Font> get_theme_font(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
492
int get_theme_font_size(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
493
Color get_theme_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
494
int get_theme_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
495
Variant get_theme_item(Theme::DataType p_data_type, const StringName &p_name, const StringName &p_theme_type = StringName()) const;
496
#ifdef TOOLS_ENABLED
497
Ref<Texture2D> get_editor_theme_icon(const StringName &p_name) const;
498
#endif
499
500
bool has_theme_icon_override(const StringName &p_name) const;
501
bool has_theme_stylebox_override(const StringName &p_name) const;
502
bool has_theme_font_override(const StringName &p_name) const;
503
bool has_theme_font_size_override(const StringName &p_name) const;
504
bool has_theme_color_override(const StringName &p_name) const;
505
bool has_theme_constant_override(const StringName &p_name) const;
506
507
bool has_theme_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
508
bool has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
509
bool has_theme_font(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
510
bool has_theme_font_size(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
511
bool has_theme_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
512
bool has_theme_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
513
514
float get_theme_default_base_scale() const;
515
Ref<Font> get_theme_default_font() const;
516
int get_theme_default_font_size() const;
517
518
virtual Transform2D get_final_transform() const override;
519
virtual Transform2D get_screen_transform_internal(bool p_absolute_position = false) const override;
520
virtual Transform2D get_popup_base_transform() const override;
521
virtual Transform2D get_popup_base_transform_native() const override;
522
virtual Viewport *get_section_root_viewport() const override;
523
virtual bool is_attached_in_viewport() const override;
524
525
Rect2i get_parent_rect() const;
526
virtual DisplayServer::WindowID get_window_id() const override;
527
static Window *get_focused_window() { return focused_window; }
528
529
virtual Size2 _get_contents_minimum_size() const;
530
531
Window();
532
~Window();
533
};
534
535
VARIANT_ENUM_CAST(Window::Mode);
536
VARIANT_ENUM_CAST(Window::Flags);
537
VARIANT_ENUM_CAST(Window::ContentScaleMode);
538
VARIANT_ENUM_CAST(Window::ContentScaleAspect);
539
VARIANT_ENUM_CAST(Window::ContentScaleStretch);
540
VARIANT_ENUM_CAST(Window::LayoutDirection);
541
VARIANT_ENUM_CAST(Window::WindowInitialPosition);
542
543