Debian Codegraph @

scene/main/viewport.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* viewport.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 "viewport.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/debugger/engine_debugger.h"
35 #include "core/templates/pair.h"
36 #include "core/templates/sort_array.h"
37 #include "scene/gui/control.h"
38 #include "scene/gui/label.h"
39 #include "scene/gui/popup.h"
40 #include "scene/gui/popup_menu.h"
41 #include "scene/gui/subviewport_container.h"
42 #include "scene/main/canvas_layer.h"
43 #include "scene/main/window.h"
44 #include "scene/resources/dpi_texture.h"
45 #include "scene/resources/mesh.h"
46 #include "scene/resources/text_line.h"
47 #include "scene/resources/world_2d.h"
48 #include "servers/audio/audio_server.h"
49 #include "servers/rendering/rendering_server_globals.h"
50
51 // 2D.
52 #include "scene/2d/audio_listener_2d.h"
53 #include "scene/2d/camera_2d.h"
54
55 #ifndef _3D_DISABLED
56 #include "scene/3d/audio_listener_3d.h"
57 #include "scene/3d/camera_3d.h"
58 #include "scene/3d/world_environment.h"
59 #endif // _3D_DISABLED
60
61 #ifndef PHYSICS_2D_DISABLED
62 #include "scene/2d/physics/collision_object_2d.h"
63 #endif // PHYSICS_2D_DISABLED
64
65 #ifndef PHYSICS_3D_DISABLED
66 #include "scene/3d/physics/collision_object_3d.h"
67 #endif // PHYSICS_3D_DISABLED
68
69 void ViewportTexture::setup_local_to_scene() {
70 // For the same target viewport, setup is only allowed once to prevent multiple free or multiple creations.
71 if (!vp_changed) {
72 return;
73 }
74
75 if (vp_pending) {
76 return;
77 }
78
79 Node *loc_scene = get_local_scene();
80 if (!loc_scene) {
81 return;
82 }
83
84 if (vp) {
85 vp->viewport_textures.erase(this);
86 vp = nullptr;
87 }
88
89 if (loc_scene->is_ready()) {
90 _setup_local_to_scene(loc_scene);
91 } else {
92 loc_scene->connect(SceneStringName(ready), callable_mp(this, &ViewportTexture::_setup_local_to_scene).bind(loc_scene), CONNECT_ONE_SHOT);
93 vp_pending = true;
94 }
95 }
96
97 void ViewportTexture::reset_local_to_scene() {
98 vp_changed = true;
99
100 if (vp) {
101 vp->viewport_textures.erase(this);
102 vp = nullptr;
103 }
104
105 if (proxy.is_valid() && proxy_ph.is_null()) {
106 proxy_ph = RS::get_singleton()->texture_2d_placeholder_create();
107 RS::get_singleton()->texture_proxy_update(proxy, proxy_ph);
108 }
109 }
110
111 void ViewportTexture::set_viewport_path_in_scene(const NodePath &p_path) {
112 if (path == p_path) {
113 return;
114 }
115
116 path = p_path;
117
118 reset_local_to_scene();
119
120 if (get_local_scene() && !path.is_empty()) {
121 setup_local_to_scene();
122 } else {
123 if (path.is_empty()) {
124 vp_changed = false;
125 }
126 emit_changed();
127 }
128 }
129
130 NodePath ViewportTexture::get_viewport_path_in_scene() const {
131 return path;
132 }
133
134 int ViewportTexture::get_width() const {
135 if (!vp) {
136 _err_print_viewport_not_set();
137 return 0;
138 }
139 if (vp->is_sub_viewport()) {
140 return vp->size.width;
141 }
142 return vp->size.width * vp->get_stretch_transform().get_scale().width;
143 }
144
145 int ViewportTexture::get_height() const {
146 if (!vp) {
147 _err_print_viewport_not_set();
148 return 0;
149 }
150 if (vp->is_sub_viewport()) {
151 return vp->size.height;
152 }
153 return vp->size.height * vp->get_stretch_transform().get_scale().height;
154 }
155
156 Size2 ViewportTexture::get_size() const {
157 if (!vp) {
158 _err_print_viewport_not_set();
159 return Size2();
160 }
161 if (vp->is_sub_viewport()) {
162 return vp->size;
163 }
164 Size2 scale = vp->get_stretch_transform().get_scale();
165 return Size2(vp->size.width * scale.width, vp->size.height * scale.height).ceil();
166 }
167
168 RID ViewportTexture::get_rid() const {
169 if (proxy.is_null()) {
170 proxy_ph = RS::get_singleton()->texture_2d_placeholder_create();
171 proxy = RS::get_singleton()->texture_proxy_create(proxy_ph);
172 }
173 return proxy;
174 }
175
176 bool ViewportTexture::has_alpha() const {
177 return false;
178 }
179
180 Ref<Image> ViewportTexture::get_image() const {
181 if (!vp) {
182 _err_print_viewport_not_set();
183 return Ref<Image>();
184 }
185 return RS::get_singleton()->texture_2d_get(vp->texture_rid);
186 }
187
188 void ViewportTexture::_err_print_viewport_not_set() const {
189 if (!vp_pending && !vp_changed) {
190 ERR_PRINT("Viewport Texture must be set to use it.");
191 }
192 }
193
194 void ViewportTexture::_setup_local_to_scene(const Node *p_loc_scene) {
195 // Always reset this, even if this call fails with an error.
196 vp_pending = false;
197
198 Node *vpn = p_loc_scene->get_node_or_null(path);
199 ERR_FAIL_NULL_MSG(vpn, "Path to node is invalid: '" + String(path) + "'.");
200 vp = Object::cast_to<Viewport>(vpn);
201 ERR_FAIL_NULL_MSG(vp, "Path to node does not point to a viewport: '" + String(path) + "'.");
202
203 vp->viewport_textures.insert(this);
204
205 ERR_FAIL_NULL(RenderingServer::get_singleton());
206 if (proxy_ph.is_valid()) {
207 RS::get_singleton()->texture_proxy_update(proxy, vp->texture_rid);
208 RS::get_singleton()->free_rid(proxy_ph);
209 proxy_ph = RID();
210 } else {
211 ERR_FAIL_COND(proxy.is_valid()); // Should be invalid.
212 proxy = RS::get_singleton()->texture_proxy_create(vp->texture_rid);
213 }
214 vp_changed = false;
215
216 emit_changed();
217 }
218
219 void ViewportTexture::_bind_methods() {
220 ClassDB::bind_method(D_METHOD("set_viewport_path_in_scene", "path"), &ViewportTexture::set_viewport_path_in_scene);
221 ClassDB::bind_method(D_METHOD("get_viewport_path_in_scene"), &ViewportTexture::get_viewport_path_in_scene);
222
223 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "viewport_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Viewport", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT), "set_viewport_path_in_scene", "get_viewport_path_in_scene");
224 }
225
226 ViewportTexture::ViewportTexture() {
227 set_local_to_scene(true);
228 }
229
230 ViewportTexture::~ViewportTexture() {
231 if (vp) {
232 vp->viewport_textures.erase(this);
233 }
234
235 ERR_FAIL_NULL(RenderingServer::get_singleton());
236
237 if (proxy_ph.is_valid()) {
238 RS::get_singleton()->free_rid(proxy_ph);
239 }
240 if (proxy.is_valid()) {
241 RS::get_singleton()->free_rid(proxy);
242 }
243 }
244
245 void Viewport::_process_dirty_canvas_parent_orders() {
246 for (const ObjectID &id : gui.canvas_parents_with_dirty_order) {
247 Object *obj = ObjectDB::get_instance(id);
248 if (!obj) {
249 continue; // May have been deleted.
250 }
251
252 Node *n = static_cast<Node *>(obj);
253 for (int i = 0; i < n->get_child_count(); i++) {
254 Node *c = n->get_child(i);
255 CanvasItem *ci = Object::cast_to<CanvasItem>(c);
256 if (ci) {
257 ci->update_draw_order();
258 continue;
259 }
260 CanvasLayer *cl = Object::cast_to<CanvasLayer>(c);
261 if (cl) {
262 cl->update_draw_order();
263 }
264 }
265 }
266
267 gui.canvas_parents_with_dirty_order.clear();
268 }
269
270 void Viewport::_sub_window_update_order() {
271 if (gui.sub_windows.size() < 2) {
272 return;
273 }
274
275 // Reorder 'always on top' windows.
276 int last_index = gui.sub_windows.size() - 1;
277 for (int index = last_index, insert_index = last_index; index >= 0; index--) {
278 SubWindow sw = gui.sub_windows[index];
279 Window *parent_window = sw.window->get_parent_visible_window();
280 bool parent_is_always_on_top = (parent_window != nullptr) && parent_window->get_flag(Window::FLAG_ALWAYS_ON_TOP);
281 if (sw.window->get_flag(Window::FLAG_ALWAYS_ON_TOP) || (parent_is_always_on_top && sw.window->is_exclusive())) {
282 if (index != insert_index) {
283 gui.sub_windows.remove_at(index);
284 gui.sub_windows.insert(insert_index, sw);
285 }
286 insert_index--;
287 }
288 }
289
290 // Reorder exclusive children.
291 for (int parent_index = 0; parent_index < gui.sub_windows.size(); parent_index++) {
292 Window *exclusive_child = gui.sub_windows[parent_index].window->get_exclusive_child();
293 if (exclusive_child != nullptr && exclusive_child->is_visible()) {
294 int child_index = _sub_window_find(exclusive_child);
295 if (child_index < parent_index) {
296 SubWindow sw = gui.sub_windows[child_index];
297 gui.sub_windows.remove_at(child_index);
298 gui.sub_windows.insert(parent_index, sw);
299 }
300 }
301 }
302
303 for (int i = 0; i < gui.sub_windows.size(); i++) {
304 RS::get_singleton()->canvas_item_set_draw_index(gui.sub_windows[i].canvas_item, i);
305 }
306 }
307
308 void Viewport::_sub_window_register(Window *p_window) {
309 ERR_FAIL_COND(!is_inside_tree());
310 for (int i = 0; i < gui.sub_windows.size(); i++) {
311 ERR_FAIL_COND(gui.sub_windows[i].window == p_window);
312 }
313
314 if (gui.sub_windows.is_empty()) {
315 subwindow_canvas = RS::get_singleton()->canvas_create();
316 RS::get_singleton()->viewport_attach_canvas(viewport, subwindow_canvas);
317 RS::get_singleton()->viewport_set_canvas_stacking(viewport, subwindow_canvas, SUBWINDOW_CANVAS_LAYER, 0);
318 }
319 SubWindow sw;
320 sw.canvas_item = RS::get_singleton()->canvas_item_create();
321 RS::get_singleton()->canvas_item_set_parent(sw.canvas_item, subwindow_canvas);
322 sw.window = p_window;
323 gui.sub_windows.push_back(sw);
324
325 if (gui.subwindow_drag == SUB_WINDOW_DRAG_DISABLED) {
326 if (p_window->get_flag(Window::FLAG_NO_FOCUS)) {
327 _sub_window_update_order();
328 } else {
329 _sub_window_grab_focus(p_window);
330 }
331 } else {
332 int index = _sub_window_find(gui.currently_dragged_subwindow);
333 sw = gui.sub_windows[index];
334 gui.sub_windows.remove_at(index);
335 gui.sub_windows.push_back(sw);
336 _sub_window_update_order();
337 }
338
339 RenderingServer::get_singleton()->viewport_set_parent_viewport(p_window->viewport, viewport);
340 }
341
342 void Viewport::_sub_window_update(Window *p_window) {
343 int index = _sub_window_find(p_window);
344
345 // _sub_window_update is sometimes called deferred, and the window may have been closed since then.
346 // For example, when the user resizes the game window.
347 // In that case, _sub_window_find will not find it, which is expected.
348 if (index == -1) {
349 return;
350 }
351
352 SubWindow &sw = gui.sub_windows.write[index];
353 sw.pending_window_update = false;
354
355 RS::get_singleton()->canvas_item_clear(sw.canvas_item);
356 const Rect2i r = Rect2i(p_window->get_position(), p_window->get_size());
357
358 if (!p_window->get_flag(Window::FLAG_BORDERLESS)) {
359 TextServer::set_current_drawn_item_oversampling(get_oversampling());
360
361 Ref<StyleBox> panel = gui.subwindow_focused == p_window ? p_window->theme_cache.embedded_border : p_window->theme_cache.embedded_unfocused_border;
362 panel->draw(sw.canvas_item, r);
363
364 // Draw the title bar text.
365 Ref<Font> title_font = p_window->theme_cache.title_font;
366 int font_size = p_window->theme_cache.title_font_size;
367 Color title_color = p_window->theme_cache.title_color;
368 int title_height = p_window->theme_cache.title_height;
369 int close_h_ofs = p_window->theme_cache.close_h_offset;
370 int close_v_ofs = p_window->theme_cache.close_v_offset;
371
372 const real_t title_space = r.size.width - panel->get_minimum_size().x - close_h_ofs;
373 if (title_space > 0) {
374 TextLine title_text = TextLine(p_window->get_displayed_title(), title_font, font_size);
375 title_text.set_width(title_space);
376 title_text.set_direction(p_window->is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
377 int x = (r.size.width - title_text.get_size().x) / 2;
378 int y = (-title_height - title_text.get_size().y) / 2;
379
380 Color font_outline_color = p_window->theme_cache.title_outline_modulate;
381 int outline_size = p_window->theme_cache.title_outline_size;
382 if (outline_size > 0 && font_outline_color.a > 0) {
383 title_text.draw_outline(sw.canvas_item, r.position + Point2(x, y), outline_size, font_outline_color);
384 }
385 title_text.draw(sw.canvas_item, r.position + Point2(x, y), title_color);
386 }
387
388 bool pressed = gui.subwindow_focused == sw.window && gui.subwindow_drag == SUB_WINDOW_DRAG_CLOSE && gui.subwindow_drag_close_inside;
389 Ref<Texture2D> close_icon = pressed ? p_window->theme_cache.close_pressed : p_window->theme_cache.close;
390 close_icon->draw(sw.canvas_item, r.position + Vector2(r.size.width - close_h_ofs, -close_v_ofs));
391
392 TextServer::set_current_drawn_item_oversampling(0.0);
393 }
394
395 const Transform2D xform = sw.window->window_transform * sw.window->stretch_transform;
396 Rect2 vr = xform.xform(sw.window->get_visible_rect());
397 vr.position += p_window->get_position();
398 if (vr != r) {
399 RS::get_singleton()->canvas_item_add_rect(sw.canvas_item, r, Color());
400 }
401 RS::get_singleton()->canvas_item_add_texture_rect(sw.canvas_item, vr, sw.window->get_texture()->get_rid());
402 }
403
404 void Viewport::_sub_window_grab_focus(Window *p_window) {
405 if (p_window == nullptr) {
406 // Release current focus.
407 if (gui.subwindow_focused) {
408 gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
409 gui.subwindow_focused = nullptr;
410 gui.subwindow_drag = SUB_WINDOW_DRAG_DISABLED;
411 }
412
413 Window *this_window = Object::cast_to<Window>(this);
414 if (this_window) {
415 this_window->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_IN);
416 }
417
418 return;
419 }
420
421 // The index needs to be update before every usage in case an event callback changed the window list.
422 int index = _sub_window_find(p_window);
423 ERR_FAIL_COND(index == -1);
424
425 if (p_window->get_flag(Window::FLAG_NO_FOCUS)) {
426 // Release current focus.
427 if (gui.subwindow_focused) {
428 gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
429 gui.subwindow_focused = nullptr;
430 gui.subwindow_drag = SUB_WINDOW_DRAG_DISABLED;
431 }
432 // Can only move to foreground, but no focus granted.
433 index = _sub_window_find(p_window);
434 ERR_FAIL_COND(index == -1);
435 SubWindow sw = gui.sub_windows[index];
436 gui.sub_windows.remove_at(index);
437 gui.sub_windows.push_back(sw);
438 _sub_window_update_order();
439 return;
440 }
441
442 if (gui.subwindow_focused) {
443 if (gui.subwindow_focused == p_window) {
444 return; // Nothing to do.
445 }
446 gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
447 gui.subwindow_drag = SUB_WINDOW_DRAG_DISABLED;
448 } else {
449 Window *this_window = Object::cast_to<Window>(this);
450 if (this_window) {
451 this_window->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
452 }
453 }
454
455 Window *old_focus = gui.subwindow_focused;
456
457 gui.subwindow_focused = p_window;
458
459 gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_IN);
460
461 { // Move to foreground.
462 index = _sub_window_find(p_window);
463 ERR_FAIL_COND(index == -1);
464 SubWindow sw = gui.sub_windows[index];
465 gui.sub_windows.remove_at(index);
466 gui.sub_windows.push_back(sw);
467 index = gui.sub_windows.size() - 1;
468 _sub_window_update_order();
469 }
470
471 if (old_focus) {
472 _sub_window_update(old_focus);
473 }
474
475 _sub_window_update(p_window);
476 }
477
478 void Viewport::_sub_window_remove(Window *p_window) {
479 int index = _sub_window_find(p_window);
480 ERR_FAIL_COND(index == -1);
481
482 ERR_FAIL_NULL(RenderingServer::get_singleton());
483
484 SubWindow sw = gui.sub_windows[index];
485 if (gui.subwindow_over == sw.window) {
486 sw.window->_mouse_leave_viewport();
487 gui.subwindow_over = nullptr;
488 }
489 RS::get_singleton()->free_rid(sw.canvas_item);
490 gui.sub_windows.remove_at(index);
491
492 if (gui.sub_windows.is_empty()) {
493 RS::get_singleton()->free_rid(subwindow_canvas);
494 subwindow_canvas = RID();
495 }
496
497 if (gui.currently_dragged_subwindow == p_window) {
498 gui.subwindow_drag = SUB_WINDOW_DRAG_DISABLED;
499 gui.currently_dragged_subwindow = nullptr;
500 }
501
502 if (gui.subwindow_focused == p_window) {
503 Window *new_focused_window;
504 Window *parent_visible = p_window->get_parent_visible_window();
505
506 gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
507
508 if (parent_visible) {
509 new_focused_window = parent_visible;
510 } else {
511 new_focused_window = Object::cast_to<Window>(this);
512 }
513
514 if (new_focused_window) {
515 int new_focused_index = _sub_window_find(new_focused_window);
516 if (new_focused_index != -1) {
517 gui.subwindow_focused = new_focused_window;
518 } else {
519 gui.subwindow_focused = nullptr;
520 }
521
522 new_focused_window->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_IN);
523 } else {
524 gui.subwindow_focused = nullptr;
525 }
526 }
527
528 RenderingServer::get_singleton()->viewport_set_parent_viewport(p_window->viewport, p_window->parent ? p_window->parent->viewport : RID());
529 }
530
531 int Viewport::_sub_window_find(Window *p_window) const {
532 for (int i = 0; i < gui.sub_windows.size(); i++) {
533 if (gui.sub_windows[i].window == p_window) {
534 return i;
535 }
536 }
537
538 return -1;
539 }
540
541 void Viewport::_update_viewport_path() {
542 if (!is_inside_tree()) {
543 return;
544 }
545
546 for (ViewportTexture *E : viewport_textures) {
547 Node *loc_scene = E->get_local_scene();
548 if (loc_scene) {
549 E->path = loc_scene->get_path_to(this);
550 }
551 }
552 }
553
554 bool Viewport::_can_hide_focus_state() {
555 return Engine::get_singleton()->is_editor_hint() || GLOBAL_GET_CACHED(int, "gui/common/show_focus_state_on_pointer_event") < 2;
556 }
557
558 void Viewport::_on_settings_changed() {
559 if (!gui.hide_focus || _can_hide_focus_state()) {
560 return;
561 }
562
563 gui.hide_focus = false;
564 // Show previously hidden focus.
565 if (gui.key_focus) {
566 gui.key_focus->queue_redraw();
567 }
568 }
569
570 void Viewport::_notification(int p_what) {
571 ERR_MAIN_THREAD_GUARD;
572
573 switch (p_what) {
574 case NOTIFICATION_ENTER_TREE: {
575 _update_viewport_path();
576
577 if (get_parent()) {
578 parent = get_parent()->get_viewport();
579 RenderingServer::get_singleton()->viewport_set_parent_viewport(viewport, parent->get_viewport_rid());
580 } else {
581 parent = nullptr;
582 }
583
584 current_canvas = find_world_2d()->get_canvas();
585 RenderingServer::get_singleton()->viewport_attach_canvas(viewport, current_canvas);
586 RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, current_canvas, canvas_transform);
587 RenderingServer::get_singleton()->viewport_set_canvas_cull_mask(viewport, canvas_cull_mask);
588 _update_audio_listener_2d();
589 #ifndef _3D_DISABLED
590 RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario());
591 _update_audio_listener_3d();
592 #endif // _3D_DISABLED
593
594 add_to_group("_viewports");
595 #if !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
596 if (get_tree()->is_debugging_collisions_hint()) {
597 #ifndef PHYSICS_2D_DISABLED
598 PhysicsServer2D::get_singleton()->space_set_debug_contacts(find_world_2d()->get_space(), get_tree()->get_collision_debug_contact_count());
599 contact_2d_debug = RenderingServer::get_singleton()->canvas_item_create();
600 RenderingServer::get_singleton()->canvas_item_set_parent(contact_2d_debug, current_canvas);
601 #endif // PHYSICS_2D_DISABLED
602 #ifndef PHYSICS_3D_DISABLED
603 PhysicsServer3D::get_singleton()->space_set_debug_contacts(find_world_3d()->get_space(), get_tree()->get_collision_debug_contact_count());
604 contact_3d_debug_multimesh = RenderingServer::get_singleton()->multimesh_create();
605 RenderingServer::get_singleton()->multimesh_allocate_data(contact_3d_debug_multimesh, get_tree()->get_collision_debug_contact_count(), RS::MULTIMESH_TRANSFORM_3D, false);
606 RenderingServer::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh, 0);
607 RenderingServer::get_singleton()->multimesh_set_mesh(contact_3d_debug_multimesh, get_tree()->get_debug_contact_mesh()->get_rid());
608 contact_3d_debug_instance = RenderingServer::get_singleton()->instance_create();
609 RenderingServer::get_singleton()->instance_set_base(contact_3d_debug_instance, contact_3d_debug_multimesh);
610 RenderingServer::get_singleton()->instance_set_scenario(contact_3d_debug_instance, find_world_3d()->get_scenario());
611 RenderingServer::get_singleton()->instance_geometry_set_flag(contact_3d_debug_instance, RS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, true);
612 #endif // PHYSICS_3D_DISABLED
613 set_physics_process_internal(true);
614 }
615 #endif // !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
616 } break;
617
618 case NOTIFICATION_READY: {
619 #ifndef _3D_DISABLED
620 if (audio_listener_3d_set.size() && !audio_listener_3d) {
621 AudioListener3D *first = nullptr;
622 for (AudioListener3D *E : audio_listener_3d_set) {
623 if (first == nullptr || first->is_greater_than(E)) {
624 first = E;
625 }
626 }
627
628 if (first) {
629 first->make_current();
630 }
631 }
632
633 if (camera_3d_set.size() && !camera_3d) {
634 // There are cameras but no current camera, pick first in tree and make it current.
635 Camera3D *first = nullptr;
636 for (Camera3D *E : camera_3d_set) {
637 if (first == nullptr || first->is_greater_than(E)) {
638 first = E;
639 }
640 }
641
642 if (first) {
643 first->make_current();
644 }
645 }
646 #endif // _3D_DISABLED
647 } break;
648
649 case NOTIFICATION_EXIT_TREE: {
650 _gui_cancel_tooltip();
651
652 RenderingServer::get_singleton()->viewport_set_scenario(viewport, RID());
653 RenderingServer::get_singleton()->viewport_remove_canvas(viewport, current_canvas);
654 #ifndef PHYSICS_2D_DISABLED
655 if (contact_2d_debug.is_valid()) {
656 RenderingServer::get_singleton()->free_rid(contact_2d_debug);
657 contact_2d_debug = RID();
658 }
659 #endif // PHYSICS_2D_DISABLED
660
661 #ifndef PHYSICS_3D_DISABLED
662 if (contact_3d_debug_multimesh.is_valid()) {
663 RenderingServer::get_singleton()->free_rid(contact_3d_debug_multimesh);
664 RenderingServer::get_singleton()->free_rid(contact_3d_debug_instance);
665 contact_3d_debug_instance = RID();
666 contact_3d_debug_multimesh = RID();
667 }
668 #endif // PHYSICS_3D_DISABLED
669
670 remove_from_group("_viewports");
671 set_physics_process_internal(false);
672
673 RS::get_singleton()->viewport_set_active(viewport, false);
674 RenderingServer::get_singleton()->viewport_set_parent_viewport(viewport, RID());
675 } break;
676
677 case NOTIFICATION_PATH_RENAMED: {
678 _update_viewport_path();
679 } break;
680
681 #if !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
682 case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
683 if (!get_tree()) {
684 return;
685 }
686
687 #ifndef PHYSICS_2D_DISABLED
688 if (get_tree()->is_debugging_collisions_hint() && contact_2d_debug.is_valid()) {
689 RenderingServer::get_singleton()->canvas_item_clear(contact_2d_debug);
690 RenderingServer::get_singleton()->canvas_item_set_draw_index(contact_2d_debug, 0xFFFFF); //very high index
691
692 Vector<Vector2> points = PhysicsServer2D::get_singleton()->space_get_contacts(find_world_2d()->get_space());
693 int point_count = PhysicsServer2D::get_singleton()->space_get_contact_count(find_world_2d()->get_space());
694 Color ccol = get_tree()->get_debug_collision_contact_color();
695
696 for (int i = 0; i < point_count; i++) {
697 RenderingServer::get_singleton()->canvas_item_add_rect(contact_2d_debug, Rect2(points[i] - Vector2(2, 2), Vector2(5, 5)), ccol);
698 }
699 }
700 #endif // PHYSICS_2D_DISABLED
701 #ifndef PHYSICS_3D_DISABLED
702 if (get_tree()->is_debugging_collisions_hint() && contact_3d_debug_multimesh.is_valid()) {
703 Vector<Vector3> points = PhysicsServer3D::get_singleton()->space_get_contacts(find_world_3d()->get_space());
704 int point_count = PhysicsServer3D::get_singleton()->space_get_contact_count(find_world_3d()->get_space());
705
706 RS::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh, point_count);
707
708 for (int i = 0; i < point_count; i++) {
709 Transform3D point_transform;
710 point_transform.origin = points[i];
711 RS::get_singleton()->multimesh_instance_set_transform(contact_3d_debug_multimesh, i, point_transform);
712 }
713 }
714 #endif // PHYSICS_3D_DISABLED
715 } break;
716 #endif // !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
717
718 case NOTIFICATION_VP_MOUSE_ENTER: {
719 gui.mouse_in_viewport = true;
720 } break;
721
722 case NOTIFICATION_VP_MOUSE_EXIT: {
723 gui.mouse_in_viewport = false;
724 _drop_physics_mouseover();
725 // When the mouse exits the viewport, we don't want to end
726 // mouse_focus, because, for example, we want to continue
727 // dragging a scrollbar even if the mouse has left the viewport.
728 } break;
729
730 case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
731 _gui_cancel_tooltip();
732 _drop_physics_mouseover();
733 _drop_mouse_focus();
734 // When the window focus changes, we want to end mouse_focus, but
735 // not the mouse_over. Note: The OS will trigger a separate mouse
736 // exit event if the change in focus results in the mouse exiting
737 // the window.
738 } break;
739
740 case NOTIFICATION_PREDELETE: {
741 if (gui_parent) {
742 gui_parent->gui.tooltip_popup = nullptr;
743 gui_parent->gui.tooltip_label = nullptr;
744 }
745 } break;
746 }
747 }
748
749 #if !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
750 void Viewport::_process_picking() {
751 if (!is_inside_tree()) {
752 return;
753 }
754 if (!physics_object_picking) {
755 return;
756 }
757 if (Object::cast_to<Window>(this) && Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
758 return;
759 }
760 if (!gui.mouse_in_viewport || gui.subwindow_over) {
761 // Clear picking events if the mouse has left the viewport or is over an embedded window.
762 // These are locations, that are expected to not trigger physics picking.
763 physics_picking_events.clear();
764 return;
765 }
766 #ifndef XR_DISABLED
767 if (use_xr) {
768 if (XRServer::get_singleton() != nullptr) {
769 Ref<XRInterface> xr_interface = XRServer::get_singleton()->get_primary_interface();
770 if (xr_interface.is_valid() && xr_interface->is_initialized() && xr_interface->get_view_count() > 1) {
771 WARN_PRINT_ONCE("Object picking can't be used when stereo rendering, this will be turned off!");
772 physics_object_picking = false; // don't try again.
773 return;
774 }
775 }
776 }
777 #endif // XR_DISABLED
778
779 _drop_physics_mouseover(true);
780
781 #ifndef PHYSICS_3D_DISABLED
782 Vector2 last_pos(1e20, 1e20);
783 CollisionObject3D *last_object = nullptr;
784 ObjectID last_id;
785 PhysicsDirectSpaceState3D::RayResult result;
786 #endif // PHYSICS_3D_DISABLED
787
788 #ifndef PHYSICS_2D_DISABLED
789 PhysicsDirectSpaceState2D *ss2d = PhysicsServer2D::get_singleton()->space_get_direct_state(find_world_2d()->get_space());
790 #endif // PHYSICS_2D_DISABLED
791
792 SubViewportContainer *parent_svc = Object::cast_to<SubViewportContainer>(get_parent());
793 bool parent_ignore_mouse = (parent_svc && parent_svc->get_mouse_filter_with_override() == Control::MOUSE_FILTER_IGNORE);
794 bool create_passive_hover_event = true;
795 if (gui.mouse_over.is_valid() || parent_ignore_mouse) {
796 // When the mouse is over a Control node, passive hovering would cause input events for Colliders, that are behind Control nodes.
797 // When parent SubViewportContainer ignores mouse, that setting should be respected.
798 create_passive_hover_event = false;
799 } else {
800 for (const Ref<InputEvent> &e : physics_picking_events) {
801 Ref<InputEventMouse> m = e;
802 if (m.is_valid()) {
803 // A mouse event exists, so passive hovering isn't necessary.
804 create_passive_hover_event = false;
805 break;
806 }
807 }
808 }
809
810 if (create_passive_hover_event) {
811 // Create a mouse motion event. This is necessary because objects or camera may have moved.
812 // While this extra event is sent, it is checked if both camera and last object and last ID did not move.
813 // If nothing changed, the event is discarded to avoid flooding with unnecessary motion events every frame.
814 Ref<InputEventMouseMotion> mm;
815 mm.instantiate();
816
817 mm->set_device(InputEvent::DEVICE_ID_INTERNAL);
818 mm->set_position(get_mouse_position());
819 mm->set_global_position(mm->get_position());
820 mm->set_alt_pressed(Input::get_singleton()->is_key_pressed(Key::ALT));
821 mm->set_shift_pressed(Input::get_singleton()->is_key_pressed(Key::SHIFT));
822 mm->set_ctrl_pressed(Input::get_singleton()->is_key_pressed(Key::CTRL));
823 mm->set_meta_pressed(Input::get_singleton()->is_key_pressed(Key::META));
824 mm->set_button_mask(Input::get_singleton()->get_mouse_button_mask());
825 physics_picking_events.push_back(mm);
826 }
827
828 while (physics_picking_events.size()) {
829 local_input_handled = false;
830 if (!handle_input_locally) {
831 Viewport *vp = this;
832 while (!Object::cast_to<Window>(vp) && vp->get_parent()) {
833 vp = vp->get_parent()->get_viewport();
834 }
835 vp->local_input_handled = false;
836 }
837
838 Ref<InputEvent> ev = physics_picking_events.front()->get();
839 physics_picking_events.pop_front();
840
841 Vector2 pos;
842 bool is_mouse = false;
843
844 Ref<InputEventMouseMotion> mm = ev;
845
846 if (mm.is_valid()) {
847 pos = mm->get_position();
848 is_mouse = true;
849 }
850
851 Ref<InputEventMouseButton> mb = ev;
852
853 if (mb.is_valid()) {
854 pos = mb->get_position();
855 is_mouse = true;
856 }
857
858 Ref<InputEventScreenDrag> sd = ev;
859
860 if (sd.is_valid()) {
861 pos = sd->get_position();
862 }
863
864 Ref<InputEventScreenTouch> st = ev;
865
866 if (st.is_valid()) {
867 pos = st->get_position();
868 }
869
870 #ifndef PHYSICS_2D_DISABLED
871 if (ss2d) {
872 // Send to 2D.
873
874 uint64_t frame = get_tree()->get_frame();
875
876 PhysicsDirectSpaceState2D::ShapeResult res[64];
877 for (const CanvasLayer *E : canvas_layers) {
878 Transform2D canvas_layer_transform;
879 ObjectID canvas_layer_id;
880 if (E) {
881 // A descendant CanvasLayer.
882 canvas_layer_transform = E->get_final_transform();
883 canvas_layer_id = E->get_instance_id();
884 } else {
885 // This Viewport's builtin canvas.
886 canvas_layer_transform = get_canvas_transform();
887 canvas_layer_id = ObjectID();
888 }
889
890 Vector2 point = canvas_layer_transform.affine_inverse().xform(pos);
891
892 PhysicsDirectSpaceState2D::PointParameters point_params;
893 point_params.position = point;
894 point_params.canvas_instance_id = canvas_layer_id;
895 point_params.collide_with_areas = true;
896 point_params.pick_point = true;
897
898 int rc = ss2d->intersect_point(point_params, res, 64);
899 if (physics_object_picking_sort) {
900 struct ComparatorCollisionObjects {
901 bool operator()(const PhysicsDirectSpaceState2D::ShapeResult &p_a, const PhysicsDirectSpaceState2D::ShapeResult &p_b) const {
902 CollisionObject2D *a = Object::cast_to<CollisionObject2D>(p_a.collider);
903 CollisionObject2D *b = Object::cast_to<CollisionObject2D>(p_b.collider);
904 if (!a || !b) {
905 return false;
906 }
907 int za = a->get_effective_z_index();
908 int zb = b->get_effective_z_index();
909 if (za != zb) {
910 return zb < za;
911 }
912 return a->is_greater_than(b);
913 }
914 };
915 SortArray<PhysicsDirectSpaceState2D::ShapeResult, ComparatorCollisionObjects> sorter;
916 sorter.sort(res, rc);
917 }
918 for (int i = 0; i < rc; i++) {
919 if (is_input_handled()) {
920 break;
921 }
922 if (res[i].collider_id.is_valid() && res[i].collider) {
923 CollisionObject2D *co = Object::cast_to<CollisionObject2D>(res[i].collider);
924 if (co && co->can_process()) {
925 bool send_event = true;
926 if (is_mouse) {
927 HashMap<ObjectID, uint64_t>::Iterator F = physics_2d_mouseover.find(res[i].collider_id);
928 if (!F) {
929 physics_2d_mouseover.insert(res[i].collider_id, frame);
930 co->_mouse_enter();
931 } else {
932 F->value = frame;
933 // It was already hovered, so don't send the event if it's faked.
934 if (mm.is_valid() && mm->get_device() == InputEvent::DEVICE_ID_INTERNAL) {
935 send_event = false;
936 }
937 }
938 HashMap<Pair<ObjectID, int>, uint64_t>::Iterator SF = physics_2d_shape_mouseover.find(Pair(res[i].collider_id, res[i].shape));
939 if (!SF) {
940 physics_2d_shape_mouseover.insert(Pair(res[i].collider_id, res[i].shape), frame);
941 co->_mouse_shape_enter(res[i].shape);
942 } else {
943 SF->value = frame;
944 }
945 }
946
947 if (send_event) {
948 co->_input_event_call(this, ev, res[i].shape);
949 }
950
951 if (physics_object_picking_first_only) {
952 break;
953 }
954 }
955 }
956 }
957 }
958
959 if (is_mouse) {
960 _cleanup_mouseover_colliders(false, false, frame);
961 }
962 }
963 #endif // PHYSICS_2D_DISABLED
964
965 #ifndef PHYSICS_3D_DISABLED
966 if (physics_object_picking_first_only && is_input_handled()) {
967 continue;
968 }
969
970 CollisionObject3D *capture_object = nullptr;
971 if (physics_object_capture.is_valid()) {
972 capture_object = ObjectDB::get_instance<CollisionObject3D>(physics_object_capture);
973 if (!capture_object || !camera_3d || (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed())) {
974 physics_object_capture = ObjectID();
975 } else {
976 last_id = physics_object_capture;
977 last_object = capture_object;
978 }
979 }
980
981 if (pos == last_pos) {
982 if (last_id.is_valid()) {
983 if (ObjectDB::get_instance(last_id) && last_object) {
984 // Good, exists.
985 _collision_object_3d_input_event(last_object, camera_3d, ev, result.position, result.normal, result.shape);
986 if (last_object->get_capture_input_on_drag() && mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
987 physics_object_capture = last_id;
988 }
989 }
990 }
991 } else {
992 if (camera_3d) {
993 Vector3 from = camera_3d->project_ray_origin(pos);
994 Vector3 dir = camera_3d->project_ray_normal(pos);
995 real_t depth_far = camera_3d->get_far();
996
997 PhysicsDirectSpaceState3D *space = PhysicsServer3D::get_singleton()->space_get_direct_state(find_world_3d()->get_space());
998 if (space) {
999 PhysicsDirectSpaceState3D::RayParameters ray_params;
1000 ray_params.from = from;
1001 ray_params.to = from + dir * depth_far;
1002 ray_params.collide_with_areas = true;
1003 ray_params.pick_ray = true;
1004
1005 bool col = space->intersect_ray(ray_params, result);
1006 ObjectID new_collider;
1007 CollisionObject3D *co = col ? Object::cast_to<CollisionObject3D>(result.collider) : nullptr;
1008 if (co && co->can_process()) {
1009 new_collider = result.collider_id;
1010 if (!capture_object) {
1011 last_object = co;
1012 last_id = result.collider_id;
1013 if (co->get_capture_input_on_drag() && mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
1014 physics_object_capture = last_id;
1015 }
1016 }
1017 }
1018
1019 if (is_mouse && new_collider != physics_object_over) {
1020 if (physics_object_over.is_valid()) {
1021 CollisionObject3D *previous_co = ObjectDB::get_instance<CollisionObject3D>(physics_object_over);
1022 if (previous_co) {
1023 previous_co->_mouse_exit();
1024 }
1025 }
1026
1027 if (new_collider.is_valid()) {
1028 DEV_ASSERT(co);
1029 co->_mouse_enter();
1030 }
1031
1032 physics_object_over = new_collider;
1033 }
1034 if (capture_object) {
1035 _collision_object_3d_input_event(capture_object, camera_3d, ev, result.position, result.normal, result.shape);
1036 } else if (new_collider.is_valid()) {
1037 _collision_object_3d_input_event(co, camera_3d, ev, result.position, result.normal, result.shape);
1038 }
1039 }
1040
1041 last_pos = pos;
1042 }
1043 }
1044 #endif // PHYSICS_3D_DISABLED
1045 }
1046 }
1047 #endif // !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
1048
1049 RID Viewport::get_viewport_rid() const {
1050 ERR_READ_THREAD_GUARD_V(RID());
1051 return viewport;
1052 }
1053
1054 void Viewport::update_canvas_items() {
1055 ERR_MAIN_THREAD_GUARD;
1056 if (!is_inside_tree()) {
1057 return;
1058 }
1059
1060 if (is_embedding_subwindows()) {
1061 for (Viewport::SubWindow w : gui.sub_windows) {
1062 if (w.window && !w.pending_window_update) {
1063 w.pending_window_update = true;
1064 w.window->_update_viewport_size();
1065 callable_mp(this, &Viewport::_sub_window_update).call_deferred(w.window);
1066 }
1067 }
1068 }
1069 _update_canvas_items(this);
1070 }
1071
1072 void Viewport::set_use_oversampling(bool p_oversampling) {
1073 ERR_MAIN_THREAD_GUARD;
1074 if (use_font_oversampling == p_oversampling) {
1075 return;
1076 }
1077 use_font_oversampling = p_oversampling;
1078 _set_size(_get_size(), _get_size_2d_override(), _is_size_allocated());
1079 }
1080
1081 bool Viewport::is_using_oversampling() const {
1082 ERR_READ_THREAD_GUARD_V(false);
1083 return use_font_oversampling;
1084 }
1085
1086 void Viewport::set_oversampling_override(float p_oversampling) {
1087 ERR_MAIN_THREAD_GUARD;
1088 if (font_oversampling_override == p_oversampling) {
1089 return;
1090 }
1091 font_oversampling_override = p_oversampling;
1092 _set_size(_get_size(), _get_size_2d_override(), _is_size_allocated());
1093 }
1094
1095 float Viewport::get_oversampling_override() const {
1096 ERR_READ_THREAD_GUARD_V(0.0);
1097 return font_oversampling_override;
1098 }
1099
1100 bool Viewport::_set_size(const Size2i &p_size, const Size2 &p_size_2d_override, bool p_allocated) {
1101 Transform2D stretch_transform_new = Transform2D();
1102 float new_font_oversampling = 1.0;
1103 if (is_size_2d_override_stretch_enabled() && p_size_2d_override.width > 0 && p_size_2d_override.height > 0) {
1104 Size2 scale = Size2(p_size) / p_size_2d_override;
1105 stretch_transform_new.scale(scale);
1106
1107 if (use_font_oversampling) {
1108 if (font_oversampling_override <= 0.0) {
1109 new_font_oversampling = MAX(scale.x, scale.y);
1110 } else {
1111 new_font_oversampling = font_oversampling_override;
1112 }
1113 } else {
1114 new_font_oversampling = 1.0;
1115 }
1116 } else if (use_font_oversampling && font_oversampling_override > 0.0) {
1117 new_font_oversampling = font_oversampling_override;
1118 }
1119
1120 Size2i new_size = p_size.maxi(2);
1121 if (size == new_size && size_allocated == p_allocated && stretch_transform == stretch_transform_new && p_size_2d_override == size_2d_override && new_font_oversampling == font_oversampling) {
1122 return false;
1123 }
1124
1125 if (new_font_oversampling != font_oversampling) {
1126 TS->reference_oversampling_level(new_font_oversampling);
1127 TS->unreference_oversampling_level(font_oversampling);
1128
1129 DPITexture::reference_scaling_level(new_font_oversampling);
1130 DPITexture::unreference_scaling_level(font_oversampling);
1131 }
1132
1133 size = new_size;
1134 size_allocated = p_allocated;
1135 size_2d_override = p_size_2d_override;
1136 stretch_transform = stretch_transform_new;
1137 font_oversampling = new_font_oversampling;
1138
1139 #ifndef XR_DISABLED
1140 if (!use_xr) {
1141 #endif
1142
1143 if (p_allocated) {
1144 RS::get_singleton()->viewport_set_size(viewport, size.width, size.height);
1145 } else {
1146 RS::get_singleton()->viewport_set_size(viewport, 0, 0);
1147 }
1148
1149 #ifndef XR_DISABLED
1150 } // if (!use_xr)
1151 #endif
1152
1153 _update_global_transform();
1154 update_configuration_warnings();
1155
1156 update_canvas_items();
1157
1158 for (ViewportTexture *E : viewport_textures) {
1159 E->emit_changed();
1160 }
1161
1162 emit_signal(SNAME("size_changed"));
1163
1164 Rect2i limit = get_visible_rect();
1165 for (int i = 0; i < gui.sub_windows.size(); ++i) {
1166 Window *sw = gui.sub_windows[i].window;
1167 Rect2i rect = Rect2i(sw->position, sw->size);
1168 Rect2i new_rect = sw->fit_rect_in_parent(rect, limit);
1169 if (new_rect != rect) {
1170 sw->set_position(new_rect.position);
1171 sw->set_size(new_rect.size);
1172 }
1173 }
1174 return true;
1175 }
1176
1177 Size2i Viewport::_get_size() const {
1178 #ifndef XR_DISABLED
1179 if (use_xr) {
1180 if (XRServer::get_singleton() != nullptr) {
1181 Ref<XRInterface> xr_interface = XRServer::get_singleton()->get_primary_interface();
1182 if (xr_interface.is_valid() && xr_interface->is_initialized()) {
1183 Size2 xr_size = xr_interface->get_render_target_size();
1184 return (Size2i)xr_size;
1185 }
1186 }
1187 return Size2i();
1188 }
1189 #endif // XR_DISABLED
1190
1191 return size;
1192 }
1193
1194 Size2 Viewport::_get_size_2d_override() const {
1195 return size_2d_override;
1196 }
1197
1198 bool Viewport::_is_size_allocated() const {
1199 return size_allocated;
1200 }
1201
1202 Rect2 Viewport::get_visible_rect() const {
1203 ERR_READ_THREAD_GUARD_V(Rect2());
1204 Rect2 r;
1205
1206 if (size == Size2()) {
1207 r = Rect2(Point2(), DisplayServer::get_singleton()->window_get_size());
1208 } else {
1209 r = Rect2(Point2(), size);
1210 }
1211
1212 if (size_2d_override != Size2()) {
1213 r.size = size_2d_override;
1214 }
1215
1216 return r;
1217 }
1218
1219 void Viewport::canvas_parent_mark_dirty(Node *p_node) {
1220 ERR_MAIN_THREAD_GUARD;
1221 bool request_update = gui.canvas_parents_with_dirty_order.is_empty();
1222 gui.canvas_parents_with_dirty_order.insert(p_node->get_instance_id());
1223 if (request_update) {
1224 callable_mp(this, &Viewport::_process_dirty_canvas_parent_orders).call_deferred();
1225 }
1226 }
1227
1228 void Viewport::set_canvas_transform(const Transform2D &p_transform) {
1229 ERR_MAIN_THREAD_GUARD;
1230 canvas_transform = p_transform;
1231
1232 RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform);
1233 }
1234
1235 Transform2D Viewport::get_canvas_transform() const {
1236 ERR_READ_THREAD_GUARD_V(Transform2D());
1237 return canvas_transform;
1238 }
1239
1240 void Viewport::_update_global_transform() {
1241 Transform2D sxform = stretch_transform * global_canvas_transform;
1242
1243 RenderingServer::get_singleton()->viewport_set_global_canvas_transform(viewport, sxform);
1244 }
1245
1246 void Viewport::set_global_canvas_transform(const Transform2D &p_transform) {
1247 ERR_MAIN_THREAD_GUARD;
1248 global_canvas_transform = p_transform;
1249
1250 _update_global_transform();
1251 }
1252
1253 Transform2D Viewport::get_global_canvas_transform() const {
1254 ERR_READ_THREAD_GUARD_V(Transform2D());
1255 return global_canvas_transform;
1256 }
1257
1258 void Viewport::_canvas_layer_add(CanvasLayer *p_canvas_layer) {
1259 canvas_layers.insert(p_canvas_layer);
1260 }
1261
1262 void Viewport::_canvas_layer_remove(CanvasLayer *p_canvas_layer) {
1263 canvas_layers.erase(p_canvas_layer);
1264 }
1265
1266 void Viewport::set_transparent_background(bool p_enable) {
1267 ERR_MAIN_THREAD_GUARD;
1268 transparent_bg = p_enable;
1269 RS::get_singleton()->viewport_set_transparent_background(viewport, p_enable);
1270 }
1271
1272 bool Viewport::has_transparent_background() const {
1273 ERR_READ_THREAD_GUARD_V(false);
1274 return transparent_bg;
1275 }
1276
1277 void Viewport::set_use_hdr_2d(bool p_enable) {
1278 ERR_MAIN_THREAD_GUARD;
1279 use_hdr_2d = p_enable;
1280 RS::get_singleton()->viewport_set_use_hdr_2d(viewport, p_enable);
1281 }
1282
1283 bool Viewport::is_using_hdr_2d() const {
1284 ERR_READ_THREAD_GUARD_V(false);
1285 return use_hdr_2d;
1286 }
1287
1288 void Viewport::set_world_2d(const Ref<World2D> &p_world_2d) {
1289 ERR_MAIN_THREAD_GUARD;
1290 if (world_2d == p_world_2d) {
1291 return;
1292 }
1293
1294 if (is_inside_tree()) {
1295 RenderingServer::get_singleton()->viewport_remove_canvas(viewport, current_canvas);
1296 }
1297
1298 if (world_2d.is_valid()) {
1299 world_2d->remove_viewport(this);
1300 }
1301
1302 if (p_world_2d.is_valid()) {
1303 bool do_propagate = world_2d.is_valid() && is_inside_tree();
1304 world_2d = p_world_2d;
1305 if (do_propagate) {
1306 _propagate_world_2d_changed(this);
1307 }
1308 } else {
1309 WARN_PRINT("Invalid world_2d");
1310 world_2d.instantiate();
1311 }
1312
1313 world_2d->register_viewport(this);
1314 _update_audio_listener_2d();
1315
1316 if (is_inside_tree()) {
1317 current_canvas = find_world_2d()->get_canvas();
1318 RenderingServer::get_singleton()->viewport_attach_canvas(viewport, current_canvas);
1319 }
1320 }
1321
1322 Ref<World2D> Viewport::find_world_2d() const {
1323 ERR_READ_THREAD_GUARD_V(Ref<World2D>());
1324 if (world_2d.is_valid()) {
1325 return world_2d;
1326 } else if (parent) {
1327 return parent->find_world_2d();
1328 } else {
1329 return Ref<World2D>();
1330 }
1331 }
1332
1333 void Viewport::_propagate_drag_notification(Node *p_node, int p_what) {
1334 // Send notification to p_node and all children and descendant nodes of p_node, except to SubViewports which are not children of a SubViewportContainer.
1335 p_node->notification(p_what);
1336 bool is_svc = Object::cast_to<SubViewportContainer>(p_node);
1337 for (int i = 0; i < p_node->get_child_count(); i++) {
1338 Node *c = p_node->get_child(i);
1339 if (!is_svc && Object::cast_to<SubViewport>(c)) {
1340 continue;
1341 }
1342 Viewport::_propagate_drag_notification(c, p_what);
1343 }
1344 }
1345
1346 Ref<World2D> Viewport::get_world_2d() const {
1347 ERR_READ_THREAD_GUARD_V(Ref<World2D>());
1348 return world_2d;
1349 }
1350
1351 Transform2D Viewport::get_stretch_transform() const {
1352 return stretch_transform;
1353 }
1354
1355 Transform2D Viewport::get_final_transform() const {
1356 ERR_READ_THREAD_GUARD_V(Transform2D());
1357 return stretch_transform * global_canvas_transform;
1358 }
1359
1360 void Viewport::_update_canvas_items(Node *p_node) {
1361 if (p_node != this) {
1362 Window *w = Object::cast_to<Window>(p_node);
1363 if (w && (!w->is_inside_tree() || !w->is_embedded())) {
1364 return;
1365 }
1366
1367 CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
1368 if (ci) {
1369 ci->queue_redraw();
1370 }
1371 }
1372
1373 int cc = p_node->get_child_count();
1374
1375 for (int i = 0; i < cc; i++) {
1376 _update_canvas_items(p_node->get_child(i));
1377 }
1378 }
1379
1380 Ref<ViewportTexture> Viewport::get_texture() const {
1381 ERR_READ_THREAD_GUARD_V(Ref<ViewportTexture>());
1382 return default_texture;
1383 }
1384
1385 void Viewport::set_positional_shadow_atlas_size(int p_size) {
1386 ERR_MAIN_THREAD_GUARD;
1387 positional_shadow_atlas_size = p_size;
1388 RS::get_singleton()->viewport_set_positional_shadow_atlas_size(viewport, p_size, positional_shadow_atlas_16_bits);
1389 }
1390
1391 int Viewport::get_positional_shadow_atlas_size() const {
1392 ERR_READ_THREAD_GUARD_V(0);
1393 return positional_shadow_atlas_size;
1394 }
1395
1396 void Viewport::set_positional_shadow_atlas_16_bits(bool p_16_bits) {
1397 ERR_MAIN_THREAD_GUARD;
1398 if (positional_shadow_atlas_16_bits == p_16_bits) {
1399 return;
1400 }
1401
1402 positional_shadow_atlas_16_bits = p_16_bits;
1403 RS::get_singleton()->viewport_set_positional_shadow_atlas_size(viewport, positional_shadow_atlas_size, positional_shadow_atlas_16_bits);
1404 }
1405
1406 bool Viewport::get_positional_shadow_atlas_16_bits() const {
1407 ERR_READ_THREAD_GUARD_V(false);
1408 return positional_shadow_atlas_16_bits;
1409 }
1410 void Viewport::set_positional_shadow_atlas_quadrant_subdiv(int p_quadrant, PositionalShadowAtlasQuadrantSubdiv p_subdiv) {
1411 ERR_MAIN_THREAD_GUARD;
1412 ERR_FAIL_INDEX(p_quadrant, 4);
1413 ERR_FAIL_INDEX(p_subdiv, SHADOW_ATLAS_QUADRANT_SUBDIV_MAX);
1414
1415 if (positional_shadow_atlas_quadrant_subdiv[p_quadrant] == p_subdiv) {
1416 return;
1417 }
1418
1419 positional_shadow_atlas_quadrant_subdiv[p_quadrant] = p_subdiv;
1420 static const int subdiv[SHADOW_ATLAS_QUADRANT_SUBDIV_MAX] = { 0, 1, 4, 16, 64, 256, 1024 };
1421
1422 RS::get_singleton()->viewport_set_positional_shadow_atlas_quadrant_subdivision(viewport, p_quadrant, subdiv[p_subdiv]);
1423 }
1424
1425 Viewport::PositionalShadowAtlasQuadrantSubdiv Viewport::get_positional_shadow_atlas_quadrant_subdiv(int p_quadrant) const {
1426 ERR_READ_THREAD_GUARD_V(SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED);
1427 ERR_FAIL_INDEX_V(p_quadrant, 4, SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED);
1428 return positional_shadow_atlas_quadrant_subdiv[p_quadrant];
1429 }
1430
1431 Ref<InputEvent> Viewport::_make_input_local(const Ref<InputEvent> &ev) {
1432 if (ev.is_null()) {
1433 return ev; // No transformation defined for null event
1434 }
1435
1436 Transform2D ai = get_final_transform().affine_inverse();
1437 Ref<InputEventMouse> me = ev;
1438 if (me.is_valid()) {
1439 me = me->xformed_by(ai);
1440 // For InputEventMouse, the global position is not adjusted by ev->xformed_by() and needs to be set separately.
1441 me->set_global_position(me->get_position());
1442 return me;
1443 }
1444 return ev->xformed_by(ai);
1445 }
1446
1447 Vector2 Viewport::get_mouse_position() const {
1448 ERR_READ_THREAD_GUARD_V(Vector2());
1449 if (get_section_root_viewport() != SceneTree::get_singleton()->get_root()) {
1450 // Rely on the most recent mouse coordinate from an InputEventMouse in push_input.
1451 // In this case get_screen_transform is not applicable, because it is ambiguous.
1452 return gui.last_mouse_pos;
1453 } else if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_MOUSE)) {
1454 Transform2D xform = get_screen_transform_internal(true);
1455 if (xform.determinant() == 0) {
1456 // Screen transform can be non-invertible when the Window is minimized.
1457 return Vector2();
1458 }
1459 return xform.affine_inverse().xform(DisplayServer::get_singleton()->mouse_get_position());
1460 } else {
1461 // Fallback to Input for getting mouse position in case of emulated mouse.
1462 return get_screen_transform_internal().affine_inverse().xform(Input::get_singleton()->get_mouse_position());
1463 }
1464 }
1465
1466 void Viewport::warp_mouse(const Vector2 &p_position) {
1467 ERR_MAIN_THREAD_GUARD;
1468 Transform2D xform = get_screen_transform_internal();
1469 Vector2 gpos = xform.xform(p_position);
1470 Input::get_singleton()->warp_mouse(gpos);
1471 }
1472
1473 Point2 Viewport::wrap_mouse_in_rect(const Vector2 &p_relative, const Rect2 &p_rect) {
1474 // Move the mouse cursor from its current position to a location bounded by `p_rect`
1475 // in accordance with a heuristic that takes the traveled distance `p_relative` of the mouse
1476 // into account.
1477
1478 // All parameters are in viewport coordinates.
1479 // p_relative denotes the distance to the previous mouse position.
1480 // p_rect denotes the area, in which the mouse should be confined in.
1481
1482 // The relative distance reported for the next event after a warp is in the boundaries of the
1483 // size of the rect on that axis, but it may be greater, in which case there's no problem as
1484 // fmod() will warp it, but if the pointer has moved in the opposite direction between the
1485 // pointer relocation and the subsequent event, the reported relative distance will be less
1486 // than the size of the rect and thus fmod() will be disabled for handling the situation.
1487 // And due to this mouse warping mechanism being stateless, we need to apply some heuristics
1488 // to detect the warp: if the relative distance is greater than the half of the size of the
1489 // relevant rect (checked per each axis), it will be considered as the consequence of a former
1490 // pointer warp.
1491
1492 const Point2 rel_sign(p_relative.x >= 0.0f ? 1 : -1, p_relative.y >= 0.0 ? 1 : -1);
1493 const Size2 warp_margin = p_rect.size * 0.5f;
1494 const Point2 rel_warped(
1495 Math::fmod(p_relative.x + rel_sign.x * warp_margin.x, p_rect.size.x) - rel_sign.x * warp_margin.x,
1496 Math::fmod(p_relative.y + rel_sign.y * warp_margin.y, p_rect.size.y) - rel_sign.y * warp_margin.y);
1497
1498 const Point2 pos_local = get_mouse_position() - p_rect.position;
1499 const Point2 pos_warped(Math::fposmod(pos_local.x, p_rect.size.x), Math::fposmod(pos_local.y, p_rect.size.y));
1500 if (pos_warped != pos_local) {
1501 warp_mouse(pos_warped + p_rect.position);
1502 }
1503
1504 return rel_warped;
1505 }
1506
1507 void Viewport::_gui_sort_roots() {
1508 if (!gui.roots_order_dirty) {
1509 return;
1510 }
1511
1512 gui.roots.sort_custom<Control::CComparator>();
1513
1514 gui.roots_order_dirty = false;
1515 }
1516
1517 void Viewport::_gui_cancel_tooltip() {
1518 gui.tooltip_control = nullptr;
1519 gui.tooltip_text = "";
1520
1521 if (gui.tooltip_timer.is_valid()) {
1522 gui.tooltip_timer->release_connections();
1523 gui.tooltip_timer = Ref<SceneTreeTimer>();
1524 }
1525 if (gui.tooltip_popup) {
1526 gui.tooltip_popup->queue_free();
1527 }
1528 }
1529
1530 String Viewport::_gui_get_tooltip(Control *p_control, const Vector2 &p_pos, Control **r_tooltip_owner) {
1531 Vector2 pos = p_pos;
1532 String tooltip;
1533
1534 while (p_control) {
1535 tooltip = p_control->get_tooltip(pos);
1536
1537 // Temporary solution for PopupMenus.
1538 PopupMenu *menu = Object::cast_to<PopupMenu>(this);
1539 if (menu) {
1540 Ref<StyleBox> sb = menu->get_theme_stylebox(SceneStringName(panel));
1541 if (sb.is_valid()) {
1542 pos.y += sb->get_margin(SIDE_TOP);
1543 }
1544
1545 tooltip = menu->get_tooltip(pos);
1546 }
1547
1548 if (r_tooltip_owner) {
1549 *r_tooltip_owner = p_control;
1550 }
1551
1552 // If we found a tooltip, we stop here.
1553 if (!tooltip.is_empty()) {
1554 break;
1555 }
1556
1557 // Otherwise, we check parent controls unless some conditions prevent it.
1558
1559 if (p_control->get_mouse_filter_with_override() == Control::MOUSE_FILTER_STOP) {
1560 break;
1561 }
1562 if (p_control->is_set_as_top_level()) {
1563 break;
1564 }
1565
1566 // Transform cursor pos for parent control.
1567 pos = p_control->get_transform().xform(pos);
1568
1569 p_control = p_control->get_parent_control();
1570 }
1571
1572 return tooltip;
1573 }
1574
1575 void Viewport::cancel_tooltip() {
1576 _gui_cancel_tooltip();
1577 }
1578
1579 void Viewport::show_tooltip(Control *p_control) {
1580 if (!p_control) {
1581 return;
1582 }
1583
1584 if (gui.tooltip_timer.is_valid()) {
1585 gui.tooltip_timer->release_connections();
1586 gui.tooltip_timer = Ref<SceneTreeTimer>();
1587 }
1588 gui.tooltip_control = p_control;
1589 _gui_show_tooltip_at(p_control->get_size() / 2);
1590 }
1591
1592 void Viewport::_gui_show_tooltip() {
1593 _gui_show_tooltip_at(gui.last_mouse_pos);
1594 }
1595
1596 void Viewport::_gui_show_tooltip_at(const Point2i &p_pos) {
1597 if (!gui.tooltip_control) {
1598 return;
1599 }
1600
1601 // Get the Control under cursor and the relevant tooltip text, if any.
1602 Control *tooltip_owner = nullptr;
1603 gui.tooltip_text = _gui_get_tooltip(
1604 gui.tooltip_control,
1605 gui.tooltip_control->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos),
1606 &tooltip_owner);
1607 gui.tooltip_text = gui.tooltip_text.strip_edges();
1608
1609 // Controls can implement `make_custom_tooltip` to provide their own tooltip.
1610 // This should be a Control node which will be added as child to a TooltipPanel.
1611 Control *base_tooltip = tooltip_owner->make_custom_tooltip(gui.tooltip_text);
1612
1613 // When the custom control is not visible, don't show any tooltip.
1614 // This way, the custom tooltip from `ConnectionsDockTree` can create
1615 // its own tooltip without conflicting with the default one, even an empty tooltip.
1616 if (base_tooltip && !base_tooltip->is_visible()) {
1617 memdelete(base_tooltip);
1618 return;
1619 }
1620
1621 if (gui.tooltip_text.is_empty() && !base_tooltip) {
1622 return; // Nothing to show.
1623 }
1624
1625 // Remove previous popup if we change something.
1626 if (gui.tooltip_popup) {
1627 memdelete(gui.tooltip_popup);
1628 gui.tooltip_popup = nullptr;
1629 }
1630
1631 if (!tooltip_owner) {
1632 return;
1633 }
1634
1635 // Popup window which houses the tooltip content.
1636 PopupPanel *panel = memnew(PopupPanel);
1637 panel->set_theme_type_variation(SNAME("TooltipPanel"));
1638
1639 // If no custom tooltip is given, use a default implementation.
1640 if (!base_tooltip) {
1641 gui.tooltip_label = memnew(Label);
1642 gui.tooltip_label->set_theme_type_variation(SNAME("TooltipLabel"));
1643 gui.tooltip_label->set_text(gui.tooltip_text);
1644 gui.tooltip_label->set_auto_translate_mode(tooltip_owner->get_tooltip_auto_translate_mode());
1645 base_tooltip = gui.tooltip_label;
1646 panel->connect(SceneStringName(mouse_entered), callable_mp(this, &Viewport::_gui_cancel_tooltip));
1647 }
1648
1649 base_tooltip->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
1650
1651 panel->set_flag(Window::FLAG_NO_FOCUS, true);
1652 panel->set_flag(Window::FLAG_POPUP, false);
1653 panel->set_flag(Window::FLAG_MOUSE_PASSTHROUGH, true);
1654 panel->set_wrap_controls(true);
1655 panel->set_default_canvas_item_texture_filter(get_default_canvas_item_texture_filter());
1656 panel->set_default_canvas_item_texture_repeat(get_default_canvas_item_texture_repeat());
1657 panel->add_child(base_tooltip);
1658 panel->gui_parent = this;
1659
1660 gui.tooltip_popup = panel;
1661
1662 tooltip_owner->add_child(gui.tooltip_popup);
1663
1664 Window *window = Object::cast_to<Window>(gui.tooltip_popup->get_embedder());
1665 if (!window) { // Not embedded.
1666 window = gui.tooltip_popup->get_parent_visible_window();
1667 }
1668 Size2 scale = get_popup_base_transform().get_scale();
1669 real_t popup_scale = MIN(scale.x, scale.y);
1670 Point2 tooltip_offset = GLOBAL_GET_CACHED(Point2, "display/mouse_cursor/tooltip_position_offset");
1671 tooltip_offset *= popup_scale;
1672 Rect2 r(gui.tooltip_pos + tooltip_offset, gui.tooltip_popup->get_contents_minimum_size());
1673 Rect2i vr;
1674 if (gui.tooltip_popup->is_embedded()) {
1675 vr = gui.tooltip_popup->get_embedder()->get_visible_rect();
1676 } else {
1677 vr = window->get_usable_parent_rect();
1678 }
1679 panel->content_scale_factor = popup_scale;
1680 r.size *= popup_scale;
1681 r.size = r.size.ceil();
1682 r.size = r.size.min(panel->get_max_size());
1683
1684 if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS) || gui.tooltip_popup->is_embedded()) {
1685 if (r.size.x + r.position.x > vr.size.x + vr.position.x) {
1686 // Place it in the opposite direction. If it fails, just hug the border.
1687 r.position.x = gui.tooltip_pos.x - r.size.x - tooltip_offset.x;
1688
1689 if (r.position.x < vr.position.x) {
1690 r.position.x = vr.position.x + vr.size.x - r.size.x;
1691 }
1692 } else if (r.position.x < vr.position.x) {
1693 r.position.x = vr.position.x;
1694 }
1695
1696 if (r.size.y + r.position.y > vr.size.y + vr.position.y) {
1697 // Same as above.
1698 r.position.y = gui.tooltip_pos.y - r.size.y - tooltip_offset.y;
1699
1700 if (r.position.y < vr.position.y) {
1701 r.position.y = vr.position.y + vr.size.y - r.size.y;
1702 }
1703 } else if (r.position.y < vr.position.y) {
1704 r.position.y = vr.position.y;
1705 }
1706 }
1707
1708 DisplayServer::WindowID active_popup = DisplayServer::get_singleton()->window_get_active_popup();
1709 if (active_popup == DisplayServer::INVALID_WINDOW_ID || active_popup == window->get_window_id()) {
1710 gui.tooltip_popup->popup(r);
1711 }
1712 gui.tooltip_popup->child_controls_changed();
1713 }
1714
1715 void Viewport::_gui_call_input(Control *p_control, const Ref<InputEvent> &p_input) {
1716 Ref<InputEvent> ev = p_input;
1717
1718 // Returns true if an event should be impacted by a control's mouse filter.
1719 bool is_pointer_event = Ref<InputEventMouse>(p_input).is_valid() || Ref<InputEventScreenDrag>(p_input).is_valid() || Ref<InputEventScreenTouch>(p_input).is_valid();
1720
1721 Ref<InputEventMouseButton> mb = p_input;
1722 bool is_scroll_event = mb.is_valid() &&
1723 (mb->get_button_index() == MouseButton::WHEEL_DOWN ||
1724 mb->get_button_index() == MouseButton::WHEEL_UP ||
1725 mb->get_button_index() == MouseButton::WHEEL_LEFT ||
1726 mb->get_button_index() == MouseButton::WHEEL_RIGHT);
1727
1728 CanvasItem *ci = p_control;
1729 while (ci) {
1730 Control *control = Object::cast_to<Control>(ci);
1731 if (control) {
1732 if (control->get_mouse_filter_with_override() != Control::MOUSE_FILTER_IGNORE) {
1733 control->_call_gui_input(ev);
1734 }
1735
1736 if (!control->is_inside_tree() || control->is_set_as_top_level()) {
1737 break;
1738 }
1739 if (control->get_mouse_filter_with_override() == Control::MOUSE_FILTER_STOP && is_pointer_event && !(is_scroll_event && control->data.force_pass_scroll_events)) {
1740 // Mouse, ScreenDrag and ScreenTouch events are stopped by default with MOUSE_FILTER_STOP, unless we have a scroll event and force_pass_scroll_events set to true
1741 set_input_as_handled();
1742 break;
1743 }
1744 }
1745
1746 if (is_input_handled()) {
1747 // Break when the event is set to handled in a child Control node or after physics picking in SubViewport.
1748 break;
1749 }
1750
1751 if (ci->is_set_as_top_level()) {
1752 break;
1753 }
1754
1755 ev = ev->xformed_by(ci->get_transform()); // Transform event upwards.
1756 ci = ci->get_parent_item();
1757 }
1758 }
1759
1760 void Viewport::_gui_call_notification(Control *p_control, int p_what) {
1761 CanvasItem *ci = p_control;
1762 while (ci) {
1763 Control *control = Object::cast_to<Control>(ci);
1764 if (control) {
1765 if (control->get_mouse_filter_with_override() != Control::MOUSE_FILTER_IGNORE) {
1766 control->notification(p_what);
1767 }
1768
1769 if (!control->is_inside_tree()) {
1770 break;
1771 }
1772
1773 if (!control->is_inside_tree() || control->is_set_as_top_level()) {
1774 break;
1775 }
1776 if (control->get_mouse_filter_with_override() == Control::MOUSE_FILTER_STOP) {
1777 break;
1778 }
1779 }
1780
1781 if (ci->is_set_as_top_level()) {
1782 break;
1783 }
1784
1785 ci = ci->get_parent_item();
1786 }
1787 }
1788
1789 // `gui_find_control` doesn't take embedded windows into account. So the caller of this function
1790 // needs to make sure, that there is no embedded window at the specified position.
1791 Control *Viewport::gui_find_control(const Point2 &p_global) {
1792 ERR_MAIN_THREAD_GUARD_V(nullptr);
1793
1794 _gui_sort_roots();
1795
1796 for (List<Control *>::Element *E = gui.roots.back(); E; E = E->prev()) {
1797 Control *sw = E->get();
1798 if (!sw->is_visible_in_tree()) {
1799 continue;
1800 }
1801
1802 Transform2D xform;
1803 CanvasItem *pci = sw->get_parent_item();
1804 if (pci) {
1805 xform = pci->get_global_transform_with_canvas();
1806 } else {
1807 xform = sw->get_canvas_transform();
1808 }
1809
1810 Control *ret = _gui_find_control_at_pos(sw, p_global, xform);
1811 if (ret) {
1812 return ret;
1813 }
1814 }
1815
1816 return nullptr;
1817 }
1818
1819 Control *Viewport::_gui_find_control_at_pos(CanvasItem *p_node, const Point2 &p_global, const Transform2D &p_xform) {
1820 if (!p_node->is_visible()) {
1821 return nullptr; // Canvas item hidden, discard.
1822 }
1823
1824 Transform2D matrix = p_xform * p_node->get_transform();
1825 // matrix.determinant() == 0.0f implies that node does not exist on scene
1826 if (matrix.determinant() == 0.0f) {
1827 return nullptr;
1828 }
1829
1830 Control *c = Object::cast_to<Control>(p_node);
1831
1832 if (!c || !c->is_clipping_contents() || c->has_point(matrix.affine_inverse().xform(p_global))) {
1833 for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
1834 CanvasItem *ci = Object::cast_to<CanvasItem>(p_node->get_child(i));
1835 if (!ci || ci->is_set_as_top_level()) {
1836 continue;
1837 }
1838
1839 Control *ret = _gui_find_control_at_pos(ci, p_global, matrix);
1840 if (ret) {
1841 return ret;
1842 }
1843 }
1844 }
1845
1846 if (!c || c->get_mouse_filter_with_override() == Control::MOUSE_FILTER_IGNORE) {
1847 return nullptr;
1848 }
1849
1850 matrix.affine_invert();
1851 if (!c->has_point(matrix.xform(p_global))) {
1852 return nullptr;
1853 }
1854
1855 Control *drag_preview = _gui_get_drag_preview();
1856 if (!drag_preview || (c != drag_preview && !drag_preview->is_ancestor_of(c))) {
1857 return c;
1858 }
1859
1860 return nullptr;
1861 }
1862
1863 bool Viewport::_gui_drop(Control *p_at_control, Point2 p_at_pos, bool p_just_check) {
1864 // Attempt drop, try parent controls too.
1865 CanvasItem *ci = p_at_control;
1866 Viewport *section_root = get_section_root_viewport();
1867 while (ci) {
1868 Control *control = Object::cast_to<Control>(ci);
1869 if (control) {
1870 if (control->can_drop_data(p_at_pos, section_root->gui.drag_data)) {
1871 if (!p_just_check) {
1872 control->drop_data(p_at_pos, section_root->gui.drag_data);
1873 }
1874
1875 return true;
1876 }
1877
1878 if (control->get_mouse_filter_with_override() == Control::MOUSE_FILTER_STOP) {
1879 break;
1880 }
1881 }
1882
1883 p_at_pos = ci->get_transform().xform(p_at_pos);
1884
1885 if (ci->is_set_as_top_level()) {
1886 break;
1887 }
1888
1889 ci = ci->get_parent_item();
1890 }
1891
1892 return false;
1893 }
1894
1895 void Viewport::_gui_input_event(Ref<InputEvent> p_event) {
1896 ERR_FAIL_COND(p_event.is_null());
1897
1898 Ref<InputEventMouseButton> mb = p_event;
1899 if (mb.is_valid()) {
1900 Point2 mpos = mb->get_position();
1901 if (mb->is_pressed()) {
1902 if (gui.dragging && mb->get_button_index() == MouseButton::RIGHT) {
1903 _perform_drop();
1904 set_input_as_handled();
1905 return;
1906 }
1907 MouseButtonMask button_mask = mouse_button_to_mask(mb->get_button_index());
1908 if (!gui.mouse_focus_mask.is_empty() && !gui.mouse_focus_mask.has_flag(button_mask)) {
1909 if (!gui.mouse_focus) {
1910 return;
1911 }
1912
1913 // Do not steal mouse focus and stuff while a focus mask without the current mouse button exists.
1914 gui.mouse_focus_mask.set_flag(button_mask);
1915 } else {
1916 gui.mouse_focus = gui_find_control(mpos);
1917
1918 if (!gui.mouse_focus) {
1919 // Focus should be hidden on click even if the focus holder didn't change.
1920 if (gui.key_focus && mb->get_button_index() == MouseButton::LEFT && _can_hide_focus_state()) {
1921 gui.hide_focus = true;
1922 gui.key_focus->queue_redraw();
1923 }
1924
1925 return;
1926 }
1927
1928 gui.mouse_focus_mask.set_flag(mouse_button_to_mask(mb->get_button_index()));
1929
1930 if (mb->get_button_index() == MouseButton::LEFT) {
1931 gui.drag_accum = Vector2();
1932 gui.drag_attempted = false;
1933 }
1934 }
1935 DEV_ASSERT(gui.mouse_focus);
1936
1937 mb = mb->xformed_by(Transform2D()); // Make a copy of the event.
1938
1939 Point2 pos = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(mpos);
1940 mb->set_position(pos);
1941
1942 #ifdef DEBUG_ENABLED
1943 if (EngineDebugger::get_singleton()) {
1944 Array arr = { gui.mouse_focus->get_path(), gui.mouse_focus->get_class() };
1945 EngineDebugger::get_singleton()->send_message("scene:click_ctrl", arr);
1946 }
1947 #endif
1948
1949 if (mb->get_button_index() == MouseButton::LEFT) { // Assign focus.
1950 CanvasItem *ci = gui.mouse_focus;
1951 while (ci) {
1952 Control *control = Object::cast_to<Control>(ci);
1953 if (control) {
1954 if (control->_is_focusable()) {
1955 // Grabbing unhovered focus can cause issues when mouse is dragged
1956 // with another button held down.
1957 if (gui.mouse_over_hierarchy.has(control->get_instance_id())) {
1958 // Hide the focus when it comes from a click.
1959 control->grab_focus(true);
1960 }
1961 break;
1962 }
1963
1964 if (control->get_mouse_filter_with_override() == Control::MOUSE_FILTER_STOP) {
1965 break;
1966 }
1967 }
1968
1969 if (ci->is_set_as_top_level()) {
1970 break;
1971 }
1972
1973 ci = ci->get_parent_item();
1974 }
1975 }
1976
1977 if (gui.mouse_focus && gui.mouse_focus->can_process()) {
1978 _gui_call_input(gui.mouse_focus, mb);
1979 }
1980
1981 if (gui.dragging && mb->get_button_index() == MouseButton::LEFT) {
1982 // Alternate drop use (when using force_drag(), as proposed by #5342).
1983 _perform_drop(gui.mouse_focus);
1984 }
1985
1986 _gui_cancel_tooltip();
1987 } else {
1988 if (gui.dragging && mb->get_button_index() == MouseButton::LEFT) {
1989 _perform_drop(gui.drag_mouse_over);
1990 }
1991
1992 gui.mouse_focus_mask.clear_flag(mouse_button_to_mask(mb->get_button_index())); // Remove from mask.
1993
1994 if (!gui.mouse_focus) {
1995 // Release event is only sent if a mouse focus (previously pressed button) exists.
1996 return;
1997 }
1998
1999 mb = mb->xformed_by(Transform2D()); // Make a copy.
2000 Point2 pos = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(mpos);
2001 mb->set_position(pos);
2002
2003 Control *mouse_focus = gui.mouse_focus;
2004
2005 // Disable mouse focus if needed before calling input,
2006 // this makes popups on mouse press event work better,
2007 // as the release will never be received otherwise.
2008 if (gui.mouse_focus_mask.is_empty()) {
2009 gui.mouse_focus = nullptr;
2010 }
2011
2012 if (mouse_focus && mouse_focus->can_process()) {
2013 _gui_call_input(mouse_focus, mb);
2014 }
2015 }
2016 }
2017
2018 Ref<InputEventMouseMotion> mm = p_event;
2019 if (mm.is_valid()) {
2020 Point2 mpos = mm->get_position();
2021
2022 // Drag & drop.
2023 Viewport *section_root = get_section_root_viewport();
2024 if (!gui.drag_attempted && gui.mouse_focus && section_root && !section_root->gui.global_dragging && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
2025 gui.drag_accum += mm->get_relative();
2026 float len = gui.drag_accum.length();
2027 if (len > gui.drag_threshold) {
2028 { // Attempt grab, try parent controls too.
2029 CanvasItem *ci = gui.mouse_focus;
2030 while (ci) {
2031 Control *control = Object::cast_to<Control>(ci);
2032 if (control) {
2033 section_root->gui.global_dragging = true;
2034 section_root->gui.drag_data = control->get_drag_data(control->get_global_transform_with_canvas().affine_inverse().xform(mpos - gui.drag_accum));
2035 if (section_root->gui.drag_data.get_type() != Variant::NIL) {
2036 gui.mouse_focus = nullptr;
2037 gui.mouse_focus_mask.clear();
2038 gui.dragging = true;
2039 break;
2040 } else {
2041 Control *drag_preview = _gui_get_drag_preview();
2042 if (drag_preview) {
2043 ERR_PRINT("Don't set a drag preview and return null data. Preview was deleted and drag request ignored.");
2044 memdelete(drag_preview);
2045 gui.drag_preview_id = ObjectID();
2046 }
2047 section_root->gui.global_dragging = false;
2048 }
2049
2050 if (control->get_mouse_filter_with_override() == Control::MOUSE_FILTER_STOP) {
2051 break;
2052 }
2053 }
2054
2055 if (ci->is_set_as_top_level()) {
2056 break;
2057 }
2058
2059 ci = ci->get_parent_item();
2060 }
2061 }
2062
2063 gui.drag_attempted = true;
2064 if (gui.dragging) {
2065 Viewport::_propagate_drag_notification(section_root, NOTIFICATION_DRAG_BEGIN);
2066 }
2067 }
2068 }
2069
2070 Control *over = nullptr;
2071 if (gui.mouse_focus) {
2072 over = gui.mouse_focus;
2073 } else if (gui.mouse_in_viewport) {
2074 over = gui_find_control(mpos);
2075 }
2076
2077 DisplayServer::CursorShape ds_cursor_shape = (DisplayServer::CursorShape)Input::get_singleton()->get_default_cursor_shape();
2078
2079 if (over) {
2080 Transform2D localizer = over->get_global_transform_with_canvas().affine_inverse();
2081 Size2 pos = localizer.xform(mpos);
2082 Vector2 velocity = localizer.basis_xform(mm->get_velocity());
2083 Vector2 rel = localizer.basis_xform(mm->get_relative());
2084
2085 mm = mm->xformed_by(Transform2D()); // Make a copy.
2086
2087 mm->set_global_position(mpos);
2088 mm->set_velocity(velocity);
2089 mm->set_relative(rel);
2090
2091 // Nothing pressed.
2092 if (mm->get_button_mask().is_empty()) {
2093 bool is_tooltip_shown = false;
2094
2095 if (gui.tooltip_popup) {
2096 if (gui.tooltip_control) {
2097 String tooltip = _gui_get_tooltip(over, gui.tooltip_control->get_global_transform_with_canvas().affine_inverse().xform(mpos));
2098 tooltip = tooltip.strip_edges();
2099
2100 if (tooltip != gui.tooltip_text) {
2101 _gui_cancel_tooltip();
2102 } else {
2103 is_tooltip_shown = true;
2104 }
2105 } else {
2106 _gui_cancel_tooltip();
2107 }
2108 }
2109
2110 // Reset the timer if the mouse has moved more than 5 pixels or has entered a new control.
2111 if (!is_tooltip_shown && over->can_process()) {
2112 Vector2 new_tooltip_pos = over->get_screen_transform().xform(pos);
2113 if (over != gui.tooltip_control || gui.tooltip_pos.distance_squared_to(new_tooltip_pos) > 25) {
2114 if (gui.tooltip_timer.is_valid()) {
2115 gui.tooltip_timer->release_connections();
2116 }
2117 gui.tooltip_control = over;
2118 gui.tooltip_pos = new_tooltip_pos;
2119 gui.tooltip_timer = get_tree()->create_timer(gui.tooltip_delay);
2120 gui.tooltip_timer->set_ignore_time_scale(true);
2121 gui.tooltip_timer->connect("timeout", callable_mp(this, &Viewport::_gui_show_tooltip));
2122 }
2123 }
2124 }
2125
2126 mm->set_position(pos);
2127
2128 Control::CursorShape cursor_shape = Control::CURSOR_ARROW;
2129 {
2130 Control *c = over;
2131 Vector2 cpos = pos;
2132 while (c) {
2133 if (!gui.mouse_focus_mask.is_empty() || c->has_point(cpos)) {
2134 cursor_shape = c->get_cursor_shape(cpos);
2135 } else {
2136 cursor_shape = Control::CURSOR_ARROW;
2137 }
2138 cpos = c->get_transform().xform(cpos);
2139 if (cursor_shape != Control::CURSOR_ARROW) {
2140 break;
2141 }
2142 if (c->get_mouse_filter_with_override() == Control::MOUSE_FILTER_STOP) {
2143 break;
2144 }
2145 if (c->is_set_as_top_level()) {
2146 break;
2147 }
2148 c = c->get_parent_control();
2149 }
2150 }
2151
2152 ds_cursor_shape = (DisplayServer::CursorShape)cursor_shape;
2153
2154 if (over->can_process()) {
2155 _gui_call_input(over, mm);
2156 }
2157 }
2158
2159 if (gui.dragging) {
2160 // Handle drag & drop. This happens in the viewport where dragging started.
2161
2162 Control *drag_preview = _gui_get_drag_preview();
2163 if (drag_preview) {
2164 Vector2 pos = drag_preview->get_canvas_transform().affine_inverse().xform(mpos);
2165 drag_preview->set_position(pos);
2166 }
2167
2168 gui.drag_mouse_over = section_root->gui.target_control;
2169 if (gui.drag_mouse_over) {
2170 if (!_gui_drop(gui.drag_mouse_over, gui.drag_mouse_over->get_local_mouse_position(), true)) {
2171 gui.drag_mouse_over = nullptr;
2172 }
2173 if (gui.drag_mouse_over) {
2174 ds_cursor_shape = DisplayServer::CURSOR_CAN_DROP;
2175 } else {
2176 ds_cursor_shape = DisplayServer::CURSOR_FORBIDDEN;
2177 }
2178 }
2179 }
2180
2181 if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CURSOR_SHAPE) && (gui.dragging || (!section_root->gui.global_dragging && !Object::cast_to<SubViewportContainer>(over)))) {
2182 // If dragging is active, then set the cursor shape only from the Viewport where dragging started.
2183 // If dragging is inactive, then set the cursor shape only when not over a SubViewportContainer.
2184 DisplayServer::get_singleton()->cursor_set_shape(ds_cursor_shape);
2185 }
2186 }
2187
2188 Ref<InputEventScreenTouch> touch_event = p_event;
2189 if (touch_event.is_valid()) {
2190 Size2 pos = touch_event->get_position();
2191 const int touch_index = touch_event->get_index();
2192 if (touch_event->is_pressed()) {
2193 Control *over = gui_find_control(pos);
2194 if (over) {
2195 gui.touch_focus[touch_index] = over->get_instance_id();
2196 if (over->can_process()) {
2197 touch_event = touch_event->xformed_by(Transform2D()); // Make a copy.
2198 pos = over->get_global_transform_with_canvas().affine_inverse().xform(pos);
2199 touch_event->set_position(pos);
2200 _gui_call_input(over, touch_event);
2201 }
2202 return;
2203 }
2204 } else {
2205 ObjectID control_id = gui.touch_focus[touch_index];
2206 Control *over = control_id.is_valid() ? ObjectDB::get_instance<Control>(control_id) : nullptr;
2207 if (over && over->can_process()) {
2208 touch_event = touch_event->xformed_by(Transform2D()); // Make a copy.
2209 pos = over->get_global_transform_with_canvas().affine_inverse().xform(pos);
2210 touch_event->set_position(pos);
2211
2212 _gui_call_input(over, touch_event);
2213 }
2214 gui.touch_focus.erase(touch_index);
2215 return;
2216 }
2217 }
2218
2219 Ref<InputEventGesture> gesture_event = p_event;
2220 if (gesture_event.is_valid()) {
2221 _gui_cancel_tooltip();
2222
2223 Size2 pos = gesture_event->get_position();
2224
2225 Control *over = gui_find_control(pos);
2226 if (over) {
2227 if (over->can_process()) {
2228 gesture_event = gesture_event->xformed_by(Transform2D()); // Make a copy.
2229 pos = over->get_global_transform_with_canvas().affine_inverse().xform(pos);
2230 gesture_event->set_position(pos);
2231 _gui_call_input(over, gesture_event);
2232 }
2233 return;
2234 }
2235 }
2236
2237 Ref<InputEventScreenDrag> drag_event = p_event;
2238 if (drag_event.is_valid()) {
2239 const int drag_event_index = drag_event->get_index();
2240 ObjectID control_id = gui.touch_focus[drag_event_index];
2241 Control *over = control_id.is_valid() ? ObjectDB::get_instance<Control>(control_id) : nullptr;
2242 if (!over) {
2243 over = gui_find_control(drag_event->get_position());
2244 }
2245 if (over) {
2246 if (over->can_process()) {
2247 Transform2D localizer = over->get_global_transform_with_canvas().affine_inverse();
2248 Size2 pos = localizer.xform(drag_event->get_position());
2249 Vector2 velocity = localizer.basis_xform(drag_event->get_velocity());
2250 Vector2 rel = localizer.basis_xform(drag_event->get_relative());
2251
2252 drag_event = drag_event->xformed_by(Transform2D()); // Make a copy.
2253
2254 drag_event->set_velocity(velocity);
2255 drag_event->set_relative(rel);
2256 drag_event->set_position(pos);
2257
2258 _gui_call_input(over, drag_event);
2259 }
2260
2261 return;
2262 }
2263 }
2264
2265 if (mm.is_null() && mb.is_null() && p_event->is_action_type()) {
2266 if (gui.dragging && p_event->is_action_pressed(SNAME("ui_cancel")) && Input::get_singleton()->is_action_just_pressed(SNAME("ui_cancel"))) {
2267 _perform_drop();
2268 set_input_as_handled();
2269 return;
2270 }
2271
2272 if (p_event->is_action_pressed(SNAME("ui_cancel"))) {
2273 // Cancel tooltip timer or hide tooltip when pressing Escape (this is standard behavior in most applications).
2274 _gui_cancel_tooltip();
2275 if (gui.tooltip_popup) {
2276 // If a tooltip was hidden, prevent other actions associated with `ui_cancel` from occurring.
2277 // For instance, this prevents the node from being deselected when pressing Escape
2278 // to hide a documentation tooltip in the inspector.
2279 set_input_as_handled();
2280 return;
2281 }
2282 }
2283
2284 if (gui.key_focus && !gui.key_focus->is_visible_in_tree()) {
2285 gui.key_focus->release_focus();
2286 }
2287
2288 if (gui.key_focus) {
2289 if (gui.key_focus->can_process()) {
2290 gui.key_focus->_call_gui_input(p_event);
2291 }
2292
2293 if (is_input_handled()) {
2294 return;
2295 }
2296 }
2297
2298 Control *from = gui.key_focus ? gui.key_focus : nullptr;
2299 if (!from) {
2300 for (int i = 0; i < get_child_count(true); i++) {
2301 Control *c = Object::cast_to<Control>(get_child(i, true));
2302 if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
2303 continue;
2304 }
2305
2306 from = c;
2307 break;
2308 }
2309 }
2310
2311 if (from && p_event->is_pressed()) {
2312 Control *next = nullptr;
2313 bool show_focus = false;
2314
2315 Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
2316 if (joypadmotion_event.is_valid()) {
2317 Input *input = Input::get_singleton();
2318
2319 if (p_event->is_action_pressed(SNAME("ui_focus_next")) && input->is_action_just_pressed_by_event(SNAME("ui_focus_next"), p_event)) {
2320 next = from->find_next_valid_focus();
2321 show_focus = true;
2322 }
2323
2324 if (p_event->is_action_pressed(SNAME("ui_focus_prev")) && input->is_action_just_pressed_by_event(SNAME("ui_focus_prev"), p_event)) {
2325 next = from->find_prev_valid_focus();
2326 show_focus = true;
2327 }
2328
2329 if (p_event->is_action_pressed(SNAME("ui_accessibility_drag_and_drop")) && input->is_action_just_pressed_by_event(SNAME("ui_accessibility_drag_and_drop"), p_event)) {
2330 if (gui_is_dragging()) {
2331 from->accessibility_drop();
2332 } else {
2333 from->accessibility_drag();
2334 }
2335 }
2336
2337 if (p_event->is_action_pressed(SNAME("ui_up")) && input->is_action_just_pressed_by_event(SNAME("ui_up"), p_event)) {
2338 next = from->_get_focus_neighbor(SIDE_TOP);
2339 show_focus = true;
2340 }
2341
2342 if (p_event->is_action_pressed(SNAME("ui_left")) && input->is_action_just_pressed_by_event(SNAME("ui_left"), p_event)) {
2343 next = from->_get_focus_neighbor(SIDE_LEFT);
2344 show_focus = true;
2345 }
2346
2347 if (p_event->is_action_pressed(SNAME("ui_right")) && input->is_action_just_pressed_by_event(SNAME("ui_right"), p_event)) {
2348 next = from->_get_focus_neighbor(SIDE_RIGHT);
2349 show_focus = true;
2350 }
2351
2352 if (p_event->is_action_pressed(SNAME("ui_down")) && input->is_action_just_pressed_by_event(SNAME("ui_down"), p_event)) {
2353 next = from->_get_focus_neighbor(SIDE_BOTTOM);
2354 show_focus = true;
2355 }
2356 } else {
2357 if (p_event->is_action_pressed(SNAME("ui_focus_next"), true, true)) {
2358 next = from->find_next_valid_focus();
2359 show_focus = true;
2360 }
2361
2362 if (p_event->is_action_pressed(SNAME("ui_focus_prev"), true, true)) {
2363 next = from->find_prev_valid_focus();
2364 show_focus = true;
2365 }
2366
2367 if (p_event->is_action_pressed(SNAME("ui_accessibility_drag_and_drop"), true, true)) {
2368 if (gui_is_dragging()) {
2369 from->accessibility_drop();
2370 } else {
2371 from->accessibility_drag();
2372 }
2373 }
2374
2375 if (p_event->is_action_pressed(SNAME("ui_up"), true, true)) {
2376 next = from->_get_focus_neighbor(SIDE_TOP);
2377 show_focus = true;
2378 }
2379
2380 if (p_event->is_action_pressed(SNAME("ui_left"), true, true)) {
2381 next = from->_get_focus_neighbor(SIDE_LEFT);
2382 show_focus = true;
2383 }
2384
2385 if (p_event->is_action_pressed(SNAME("ui_right"), true, true)) {
2386 next = from->_get_focus_neighbor(SIDE_RIGHT);
2387 show_focus = true;
2388 }
2389
2390 if (p_event->is_action_pressed(SNAME("ui_down"), true, true)) {
2391 next = from->_get_focus_neighbor(SIDE_BOTTOM);
2392 show_focus = true;
2393 }
2394 }
2395
2396 if (next) {
2397 next->grab_focus();
2398 set_input_as_handled();
2399 } else if (show_focus && gui.hide_focus && gui.key_focus) {
2400 // Show focus even it the holder didn't change, as visual feedback.
2401 gui.hide_focus = false;
2402 gui.key_focus->queue_redraw();
2403 }
2404 }
2405 }
2406 }
2407
2408 void Viewport::_perform_drop(Control *p_control) {
2409 gui_perform_drop_at(p_control ? p_control->get_local_mouse_position() : Vector2(), p_control);
2410 }
2411
2412 void Viewport::gui_perform_drop_at(const Point2 &p_pos, Control *p_control) {
2413 // Without any arguments, simply cancel Drag and Drop.
2414 if (p_control) {
2415 gui.drag_successful = _gui_drop(p_control, p_pos, false);
2416 } else {
2417 gui.drag_successful = false;
2418 }
2419
2420 Control *drag_preview = _gui_get_drag_preview();
2421 if (drag_preview) {
2422 memdelete(drag_preview);
2423 gui.drag_preview_id = ObjectID();
2424 }
2425
2426 Viewport *section_root = get_section_root_viewport();
2427 section_root->gui.drag_data = Variant();
2428 gui.dragging = false;
2429 gui.drag_description = String();
2430 section_root->gui.global_dragging = false;
2431 gui.drag_mouse_over = nullptr;
2432 Viewport::_propagate_drag_notification(section_root, NOTIFICATION_DRAG_END);
2433 // Display the new cursor shape instantly.
2434 update_mouse_cursor_state();
2435 }
2436
2437 void Viewport::_gui_cleanup_internal_state(Ref<InputEvent> p_event) {
2438 ERR_FAIL_COND(p_event.is_null());
2439
2440 Ref<InputEventMouseButton> mb = p_event;
2441 if (mb.is_valid()) {
2442 if (!mb->is_pressed()) {
2443 gui.mouse_focus_mask.clear_flag(mouse_button_to_mask(mb->get_button_index())); // Remove from mask.
2444 }
2445 }
2446 }
2447
2448 List<Control *>::Element *Viewport::_gui_add_root_control(Control *p_control) {
2449 gui.roots_order_dirty = true;
2450 return gui.roots.push_back(p_control);
2451 }
2452
2453 void Viewport::gui_set_root_order_dirty() {
2454 ERR_MAIN_THREAD_GUARD;
2455 gui.roots_order_dirty = true;
2456 }
2457
2458 void Viewport::_gui_force_drag_start() {
2459 Viewport *section_root = get_section_root_viewport();
2460 section_root->gui.global_dragging = true;
2461 }
2462
2463 void Viewport::_gui_force_drag_cancel() {
2464 Viewport *section_root = get_section_root_viewport();
2465 section_root->gui.global_dragging = false;
2466 }
2467
2468 void Viewport::_gui_force_drag(Control *p_base, const Variant &p_data, Control *p_control) {
2469 ERR_FAIL_COND_MSG(p_data.get_type() == Variant::NIL, "Drag data must be a value.");
2470
2471 Viewport *section_root = get_section_root_viewport();
2472 ERR_FAIL_COND(!section_root->gui.global_dragging);
2473
2474 gui.dragging = true;
2475 section_root->gui.drag_data = p_data;
2476 gui.mouse_focus = nullptr;
2477 gui.mouse_focus_mask.clear();
2478
2479 if (p_control) {
2480 _gui_set_drag_preview(p_base, p_control);
2481 }
2482 Viewport::_propagate_drag_notification(section_root, NOTIFICATION_DRAG_BEGIN);
2483 }
2484
2485 void Viewport::_gui_set_drag_preview(Control *p_base, Control *p_control) {
2486 ERR_FAIL_NULL(p_control);
2487 ERR_FAIL_COND(p_control->is_inside_tree());
2488 ERR_FAIL_COND(p_control->get_parent() != nullptr);
2489
2490 Control *drag_preview = _gui_get_drag_preview();
2491 if (drag_preview) {
2492 memdelete(drag_preview);
2493 }
2494 p_control->set_as_top_level(true);
2495 p_control->set_position(gui.last_mouse_pos);
2496 p_base->get_root_parent_control()->add_child(p_control); // Add as child of viewport.
2497 p_control->move_to_front();
2498
2499 gui.drag_preview_id = p_control->get_instance_id();
2500 }
2501
2502 Control *Viewport::_gui_get_drag_preview() {
2503 if (gui.drag_preview_id.is_null()) {
2504 return nullptr;
2505 } else {
2506 Control *drag_preview = ObjectDB::get_instance<Control>(gui.drag_preview_id);
2507 if (!drag_preview) {
2508 ERR_PRINT("Don't free the control set as drag preview.");
2509 gui.drag_preview_id = ObjectID();
2510 }
2511 return drag_preview;
2512 }
2513 }
2514
2515 void Viewport::_gui_remove_root_control(List<Control *>::Element *RI) {
2516 gui.roots.erase(RI);
2517 }
2518
2519 void Viewport::_gui_unfocus_control(Control *p_control) {
2520 if (gui.key_focus == p_control) {
2521 gui.key_focus->release_focus();
2522 }
2523 }
2524
2525 void Viewport::_gui_hide_control(Control *p_control) {
2526 if (gui.mouse_focus == p_control) {
2527 _drop_mouse_focus();
2528 }
2529
2530 if (gui.key_focus == p_control) {
2531 gui_release_focus();
2532 }
2533 ObjectID over_id = p_control ? p_control->get_instance_id() : ObjectID();
2534 if (gui.mouse_over == over_id || gui.mouse_over_hierarchy.has(over_id)) {
2535 _drop_mouse_over(p_control->get_parent_control());
2536 }
2537 if (gui.drag_mouse_over == p_control) {
2538 gui.drag_mouse_over = nullptr;
2539 }
2540 if (gui.tooltip_control == p_control) {
2541 _gui_cancel_tooltip();
2542 }
2543 }
2544
2545 void Viewport::_gui_remove_control(Control *p_control) {
2546 if (gui.mouse_focus == p_control) {
2547 gui.mouse_focus = nullptr;
2548 gui.mouse_focus_mask.clear();
2549 }
2550 if (gui.key_focus == p_control) {
2551 gui.key_focus = nullptr;
2552 }
2553 ObjectID over_id = p_control ? p_control->get_instance_id() : ObjectID();
2554 if (gui.mouse_over == over_id || gui.mouse_over_hierarchy.has(over_id)) {
2555 _drop_mouse_over(p_control->get_parent_control());
2556 }
2557 if (gui.drag_mouse_over == p_control) {
2558 gui.drag_mouse_over = nullptr;
2559 }
2560 if (gui.tooltip_control == p_control) {
2561 gui.tooltip_control = nullptr;
2562 }
2563 }
2564
2565 void Viewport::canvas_item_top_level_changed() {
2566 _gui_update_mouse_over();
2567 }
2568
2569 void Viewport::_gui_update_mouse_over() {
2570 if (gui.mouse_over.is_null() || gui.mouse_over_hierarchy.is_empty()) {
2571 return;
2572 }
2573
2574 if (gui.sending_mouse_enter_exit_notifications) {
2575 // If notifications are already being sent, delay call to next frame.
2576 if (get_tree() && !get_tree()->is_connected(SNAME("process_frame"), callable_mp(this, &Viewport::_gui_update_mouse_over))) {
2577 get_tree()->connect(SNAME("process_frame"), callable_mp(this, &Viewport::_gui_update_mouse_over), CONNECT_ONE_SHOT);
2578 }
2579 return;
2580 }
2581
2582 // Rebuild the mouse over hierarchy.
2583 LocalVector<ObjectID> new_mouse_over_hierarchy;
2584 LocalVector<ObjectID> needs_enter;
2585 LocalVector<int> needs_exit;
2586
2587 CanvasItem *over = ObjectDB::get_instance<CanvasItem>(gui.mouse_over);
2588 CanvasItem *ancestor = over;
2589 bool removing = false;
2590 bool reached_top = false;
2591 while (ancestor) {
2592 Control *ancestor_control = Object::cast_to<Control>(ancestor);
2593 if (ancestor_control) {
2594 ObjectID ancestor_control_id = ancestor_control->get_instance_id();
2595 int found = gui.mouse_over_hierarchy.find(ancestor_control_id);
2596 if (found >= 0) {
2597 // Remove the node if the propagation chain has been broken or it is now MOUSE_FILTER_IGNORE.
2598 if (removing || ancestor_control->get_mouse_filter_with_override() == Control::MOUSE_FILTER_IGNORE) {
2599 needs_exit.push_back(found);
2600 }
2601 }
2602 if (found == 0) {
2603 if (removing) {
2604 // Stop if the chain has been broken and the top of the hierarchy has been reached.
2605 break;
2606 }
2607 reached_top = true;
2608 }
2609 if (!removing && ancestor_control->get_mouse_filter_with_override() != Control::MOUSE_FILTER_IGNORE) {
2610 new_mouse_over_hierarchy.push_back(ancestor_control_id);
2611 // Add the node if it was not found and it is now not MOUSE_FILTER_IGNORE.
2612 if (found < 0) {
2613 needs_enter.push_back(ancestor_control_id);
2614 }
2615 }
2616 if (ancestor_control->get_mouse_filter_with_override() == Control::MOUSE_FILTER_STOP) {
2617 // MOUSE_FILTER_STOP breaks the propagation chain.
2618 if (reached_top) {
2619 break;
2620 }
2621 removing = true;
2622 }
2623 }
2624 if (ancestor->is_set_as_top_level()) {
2625 // Top level breaks the propagation chain.
2626 if (reached_top) {
2627 break;
2628 } else {
2629 removing = true;
2630 ancestor = Object::cast_to<CanvasItem>(ancestor->get_parent());
2631 continue;
2632 }
2633 }
2634 ancestor = ancestor->get_parent_item();
2635 }
2636 if (needs_exit.is_empty() && needs_enter.is_empty()) {
2637 return;
2638 }
2639
2640 gui.sending_mouse_enter_exit_notifications = true;
2641
2642 // Send Mouse Exit Self notification.
2643 if (over && !needs_exit.is_empty() && needs_exit[0] == (int)gui.mouse_over_hierarchy.size() - 1) {
2644 over->notification(Control::NOTIFICATION_MOUSE_EXIT_SELF);
2645 gui.mouse_over = ObjectID();
2646 }
2647
2648 // Send Mouse Exit notifications.
2649 for (int exit_control_index : needs_exit) {
2650 Control *ctrl = ObjectDB::get_instance<Control>(gui.mouse_over_hierarchy[exit_control_index]);
2651 if (ctrl) {
2652 ctrl->notification(Control::NOTIFICATION_MOUSE_EXIT);
2653 ctrl->emit_signal(SceneStringName(mouse_exited));
2654 }
2655 }
2656
2657 // Update the mouse over hierarchy.
2658 gui.mouse_over_hierarchy.resize(new_mouse_over_hierarchy.size());
2659 for (int i = 0; i < (int)new_mouse_over_hierarchy.size(); i++) {
2660 gui.mouse_over_hierarchy[i] = new_mouse_over_hierarchy[new_mouse_over_hierarchy.size() - 1 - i];
2661 }
2662
2663 // Send Mouse Enter notifications.
2664 for (int i = needs_enter.size() - 1; i >= 0; i--) {
2665 Control *ctrl = ObjectDB::get_instance<Control>(needs_enter[i]);
2666 if (ctrl) {
2667 ctrl->notification(Control::NOTIFICATION_MOUSE_ENTER);
2668 ctrl->emit_signal(SceneStringName(mouse_entered));
2669 }
2670 }
2671
2672 gui.sending_mouse_enter_exit_notifications = false;
2673 }
2674
2675 Window *Viewport::get_base_window() {
2676 ERR_READ_THREAD_GUARD_V(nullptr);
2677 ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
2678
2679 Viewport *v = this;
2680 Window *w = Object::cast_to<Window>(v);
2681 while (!w) {
2682 v = v->get_parent_viewport();
2683 w = Object::cast_to<Window>(v);
2684 }
2685
2686 return w;
2687 }
2688
2689 void Viewport::_gui_remove_focus_for_window(Node *p_window) {
2690 if (get_base_window() == p_window) {
2691 gui_release_focus();
2692 }
2693 }
2694
2695 bool Viewport::_gui_control_has_focus(const Control *p_control, bool p_ignore_hidden_focus) {
2696 return (!p_ignore_hidden_focus || !gui.hide_focus) && gui.key_focus == p_control;
2697 }
2698
2699 void Viewport::_gui_control_grab_focus(Control *p_control, bool p_hide_focus) {
2700 if (gui.key_focus && gui.key_focus == p_control) {
2701 // Only worry about the focus visibility change.
2702 if (p_hide_focus != gui.hide_focus && _can_hide_focus_state()) {
2703 gui.hide_focus = p_hide_focus;
2704 p_control->queue_redraw();
2705 }
2706 return;
2707 }
2708
2709 get_tree()->call_group("_viewports", "_gui_remove_focus_for_window", get_base_window());
2710 if (p_control->is_inside_tree() && p_control->get_viewport() == this) {
2711 gui.key_focus = p_control;
2712 if (_can_hide_focus_state()) {
2713 gui.hide_focus = p_hide_focus;
2714 }
2715 emit_signal(SNAME("gui_focus_changed"), p_control);
2716 p_control->notification(Control::NOTIFICATION_FOCUS_ENTER);
2717 p_control->queue_redraw();
2718 }
2719 }
2720
2721 void Viewport::_gui_accept_event() {
2722 if (is_inside_tree()) {
2723 set_input_as_handled();
2724 }
2725 }
2726
2727 void Viewport::_drop_mouse_focus() {
2728 Control *c = gui.mouse_focus;
2729 BitField<MouseButtonMask> mask = gui.mouse_focus_mask;
2730 gui.mouse_focus = nullptr;
2731 gui.mouse_focus_mask.clear();
2732
2733 if (!c) {
2734 return;
2735 }
2736
2737 for (int i = 0; i < 3; i++) {
2738 if ((int)mask & (1 << i)) {
2739 Ref<InputEventMouseButton> mb;
2740 mb.instantiate();
2741 mb->set_position(c->get_local_mouse_position());
2742 mb->set_global_position(c->get_local_mouse_position());
2743 mb->set_button_index(MouseButton(i + 1));
2744 mb->set_pressed(false);
2745 mb->set_device(InputEvent::DEVICE_ID_INTERNAL);
2746 c->_call_gui_input(mb);
2747 }
2748 }
2749 }
2750
2751 void Viewport::_drop_physics_mouseover(bool p_paused_only) {
2752 #ifndef PHYSICS_2D_DISABLED
2753 _cleanup_mouseover_colliders(true, p_paused_only);
2754 #endif // PHYSICS_2D_DISABLED
2755
2756 #ifndef PHYSICS_3D_DISABLED
2757 if (physics_object_over.is_valid()) {
2758 CollisionObject3D *co = ObjectDB::get_instance<CollisionObject3D>(physics_object_over);
2759 if (co) {
2760 if (!co->is_inside_tree()) {
2761 physics_object_over = ObjectID();
2762 physics_object_capture = ObjectID();
2763 } else if (!(p_paused_only && co->can_process())) {
2764 co->_mouse_exit();
2765 physics_object_over = ObjectID();
2766 physics_object_capture = ObjectID();
2767 }
2768 }
2769 }
2770 #endif // PHYSICS_3D_DISABLED
2771 }
2772
2773 void Viewport::_gui_grab_click_focus(Control *p_control) {
2774 gui.mouse_click_grabber = p_control;
2775 callable_mp(this, &Viewport::_post_gui_grab_click_focus).call_deferred();
2776 }
2777
2778 void Viewport::_post_gui_grab_click_focus() {
2779 Control *focus_grabber = gui.mouse_click_grabber;
2780 if (!focus_grabber) {
2781 // Redundant grab requests were made.
2782 return;
2783 }
2784 gui.mouse_click_grabber = nullptr;
2785
2786 if (gui.mouse_focus) {
2787 if (gui.mouse_focus == focus_grabber) {
2788 return;
2789 }
2790
2791 BitField<MouseButtonMask> mask = gui.mouse_focus_mask;
2792 Point2 click = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos);
2793
2794 for (int i = 0; i < 3; i++) {
2795 if ((int)mask & (1 << i)) {
2796 Ref<InputEventMouseButton> mb;
2797 mb.instantiate();
2798
2799 // Send unclick.
2800
2801 mb->set_position(click);
2802 mb->set_button_index(MouseButton(i + 1));
2803 mb->set_pressed(false);
2804 mb->set_device(InputEvent::DEVICE_ID_INTERNAL);
2805 gui.mouse_focus->_call_gui_input(mb);
2806 }
2807 }
2808
2809 gui.mouse_focus = focus_grabber;
2810 click = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos);
2811
2812 for (int i = 0; i < 3; i++) {
2813 if ((int)mask & (1 << i)) {
2814 Ref<InputEventMouseButton> mb;
2815 mb.instantiate();
2816
2817 // Send click.
2818
2819 mb->set_position(click);
2820 mb->set_button_index(MouseButton(i + 1));
2821 mb->set_pressed(true);
2822 mb->set_device(InputEvent::DEVICE_ID_INTERNAL);
2823 callable_mp(gui.mouse_focus, &Control::_call_gui_input).call_deferred(mb);
2824 }
2825 }
2826 }
2827 }
2828
2829 ///////////////////////////////
2830
2831 void Viewport::_push_text_input(const String &p_text, bool p_emit_signal) {
2832 ERR_MAIN_THREAD_GUARD;
2833 if (gui.subwindow_focused) {
2834 gui.subwindow_focused->push_text_input(p_text);
2835 return;
2836 }
2837
2838 StringName set_text_method = SNAME("_set_text");
2839 if (!gui.key_focus || !gui.key_focus->has_method(set_text_method)) {
2840 return;
2841 }
2842 gui.key_focus->call(set_text_method, p_text, p_emit_signal);
2843 }
2844
2845 void Viewport::push_text_input(const String &p_text) {
2846 _push_text_input(p_text, false);
2847 }
2848
2849 Viewport::SubWindowResize Viewport::_sub_window_get_resize_margin(Window *p_subwindow, const Point2 &p_point) {
2850 if (p_subwindow->get_flag(Window::FLAG_BORDERLESS) || p_subwindow->get_flag(Window::FLAG_RESIZE_DISABLED)) {
2851 return SUB_WINDOW_RESIZE_DISABLED;
2852 }
2853
2854 Rect2i r = Rect2i(p_subwindow->get_position(), p_subwindow->get_size());
2855
2856 int title_height = p_subwindow->theme_cache.title_height;
2857
2858 r.position.y -= title_height;
2859 r.size.y += title_height;
2860
2861 if (r.has_point(p_point)) {
2862 return SUB_WINDOW_RESIZE_DISABLED; // It's inside, so no resize.
2863 }
2864
2865 int dist_x = p_point.x < r.position.x ? (p_point.x - r.position.x) : (p_point.x > (r.position.x + r.size.x) ? (p_point.x - (r.position.x + r.size.x)) : 0);
2866 int dist_y = p_point.y < r.position.y ? (p_point.y - r.position.y) : (p_point.y > (r.position.y + r.size.y) ? (p_point.y - (r.position.y + r.size.y)) : 0);
2867
2868 int limit = p_subwindow->theme_cache.resize_margin;
2869
2870 if (Math::abs(dist_x) > limit) {
2871 return SUB_WINDOW_RESIZE_DISABLED;
2872 }
2873
2874 if (Math::abs(dist_y) > limit) {
2875 return SUB_WINDOW_RESIZE_DISABLED;
2876 }
2877
2878 if (dist_x < 0 && dist_y < 0) {
2879 return SUB_WINDOW_RESIZE_TOP_LEFT;
2880 }
2881
2882 if (dist_x == 0 && dist_y < 0) {
2883 return SUB_WINDOW_RESIZE_TOP;
2884 }
2885
2886 if (dist_x > 0 && dist_y < 0) {
2887 return SUB_WINDOW_RESIZE_TOP_RIGHT;
2888 }
2889
2890 if (dist_x < 0 && dist_y == 0) {
2891 return SUB_WINDOW_RESIZE_LEFT;
2892 }
2893
2894 if (dist_x > 0 && dist_y == 0) {
2895 return SUB_WINDOW_RESIZE_RIGHT;
2896 }
2897
2898 if (dist_x < 0 && dist_y > 0) {
2899 return SUB_WINDOW_RESIZE_BOTTOM_LEFT;
2900 }
2901
2902 if (dist_x == 0 && dist_y > 0) {
2903 return SUB_WINDOW_RESIZE_BOTTOM;
2904 }
2905
2906 if (dist_x > 0 && dist_y > 0) {
2907 return SUB_WINDOW_RESIZE_BOTTOM_RIGHT;
2908 }
2909
2910 return SUB_WINDOW_RESIZE_DISABLED;
2911 }
2912
2913 bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) {
2914 if (gui.subwindow_drag != SUB_WINDOW_DRAG_DISABLED) {
2915 ERR_FAIL_NULL_V(gui.currently_dragged_subwindow, false);
2916
2917 Ref<InputEventMouseButton> mb = p_event;
2918 if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
2919 if (gui.subwindow_drag == SUB_WINDOW_DRAG_CLOSE) {
2920 if (gui.subwindow_drag_close_rect.has_point(mb->get_position())) {
2921 // Close window.
2922 gui.currently_dragged_subwindow->_event_callback(DisplayServer::WINDOW_EVENT_CLOSE_REQUEST);
2923 }
2924 }
2925 gui.subwindow_drag = SUB_WINDOW_DRAG_DISABLED;
2926 if (gui.currently_dragged_subwindow != nullptr) { // May have been erased.
2927 _sub_window_update(gui.currently_dragged_subwindow);
2928 gui.currently_dragged_subwindow = nullptr;
2929 }
2930 }
2931
2932 Ref<InputEventMouseMotion> mm = p_event;
2933 if (mm.is_valid()) {
2934 if (gui.subwindow_drag == SUB_WINDOW_DRAG_MOVE) {
2935 Vector2 diff = mm->get_position() - gui.subwindow_drag_from;
2936 Rect2i new_rect(gui.subwindow_drag_pos + diff, gui.currently_dragged_subwindow->get_size());
2937
2938 if (gui.currently_dragged_subwindow->is_clamped_to_embedder()) {
2939 new_rect = gui.currently_dragged_subwindow->fit_rect_in_parent(new_rect, get_visible_rect());
2940 }
2941
2942 gui.currently_dragged_subwindow->_rect_changed_callback(new_rect);
2943
2944 if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CURSOR_SHAPE)) {
2945 DisplayServer::get_singleton()->cursor_set_shape(DisplayServer::CURSOR_MOVE);
2946 }
2947 }
2948 if (gui.subwindow_drag == SUB_WINDOW_DRAG_CLOSE) {
2949 gui.subwindow_drag_close_inside = gui.subwindow_drag_close_rect.has_point(mm->get_position());
2950 }
2951 if (gui.subwindow_drag == SUB_WINDOW_DRAG_RESIZE) {
2952 Vector2i diff = mm->get_position() - gui.subwindow_drag_from;
2953 Size2i min_size = gui.currently_dragged_subwindow->get_min_size();
2954 Size2i min_size_clamped = gui.currently_dragged_subwindow->get_clamped_minimum_size();
2955
2956 min_size_clamped = min_size_clamped.maxi(1);
2957
2958 Rect2i r = gui.subwindow_resize_from_rect;
2959
2960 Size2i limit = r.size - min_size_clamped;
2961
2962 switch (gui.subwindow_resize_mode) {
2963 case SUB_WINDOW_RESIZE_TOP_LEFT: {
2964 diff.x = MIN(diff.x, limit.x);
2965 diff.y = MIN(diff.y, limit.y);
2966 r.position += diff;
2967 r.size -= diff;
2968 } break;
2969 case SUB_WINDOW_RESIZE_TOP: {
2970 diff.x = 0;
2971 diff.y = MIN(diff.y, limit.y);
2972 r.position += diff;
2973 r.size -= diff;
2974 } break;
2975 case SUB_WINDOW_RESIZE_TOP_RIGHT: {
2976 diff.x = MAX(diff.x, -limit.x);
2977 diff.y = MIN(diff.y, limit.y);
2978 r.position.y += diff.y;
2979 r.size.y -= diff.y;
2980 r.size.x += diff.x;
2981 } break;
2982 case SUB_WINDOW_RESIZE_LEFT: {
2983 diff.x = MIN(diff.x, limit.x);
2984 diff.y = 0;
2985 r.position += diff;
2986 r.size -= diff;
2987
2988 } break;
2989 case SUB_WINDOW_RESIZE_RIGHT: {
2990 diff.x = MAX(diff.x, -limit.x);
2991 r.size.x += diff.x;
2992 } break;
2993 case SUB_WINDOW_RESIZE_BOTTOM_LEFT: {
2994 diff.x = MIN(diff.x, limit.x);
2995 diff.y = MAX(diff.y, -limit.y);
2996 r.position.x += diff.x;
2997 r.size.x -= diff.x;
2998 r.size.y += diff.y;
2999
3000 } break;
3001 case SUB_WINDOW_RESIZE_BOTTOM: {
3002 diff.y = MAX(diff.y, -limit.y);
3003 r.size.y += diff.y;
3004 } break;
3005 case SUB_WINDOW_RESIZE_BOTTOM_RIGHT: {
3006 diff.x = MAX(diff.x, -limit.x);
3007 diff.y = MAX(diff.y, -limit.y);
3008 r.size += diff;
3009
3010 } break;
3011 default: {
3012 }
3013 }
3014
3015 Size2i max_size = gui.currently_dragged_subwindow->get_max_size();
3016 if ((max_size.x > 0 || max_size.y > 0) && (max_size.x >= min_size.x && max_size.y >= min_size.y)) {
3017 max_size = max_size.maxi(1);
3018
3019 if (r.size.x > max_size.x) {
3020 r.size.x = max_size.x;
3021 }
3022 if (r.size.y > max_size.y) {
3023 r.size.y = max_size.y;
3024 }
3025 }
3026
3027 gui.currently_dragged_subwindow->_rect_changed_callback(r);
3028 }
3029
3030 if (gui.currently_dragged_subwindow) { // May have been erased.
3031 _sub_window_update(gui.currently_dragged_subwindow);
3032 }
3033 }
3034
3035 return true; // Handled.
3036 }
3037 Ref<InputEventMouseButton> mb = p_event;
3038 // If the event is a mouse button, we need to check whether another window was clicked.
3039
3040 if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
3041 Window *click_on_window = nullptr;
3042 for (int i = gui.sub_windows.size() - 1; i >= 0; i--) {
3043 SubWindow sw = gui.sub_windows.write[i];
3044
3045 // Clicked inside window?
3046
3047 Rect2i r = Rect2i(sw.window->get_position(), sw.window->get_size());
3048
3049 if (!sw.window->get_flag(Window::FLAG_BORDERLESS)) {
3050 // Check top bar.
3051 int title_height = sw.window->theme_cache.title_height;
3052 Rect2i title_bar = r;
3053 title_bar.position.y -= title_height;
3054 title_bar.size.y = title_height;
3055
3056 if (title_bar.size.y > 0 && title_bar.has_point(mb->get_position())) {
3057 click_on_window = sw.window;
3058
3059 int close_h_ofs = sw.window->theme_cache.close_h_offset;
3060 int close_v_ofs = sw.window->theme_cache.close_v_offset;
3061 bool pressed = gui.subwindow_focused == sw.window && gui.subwindow_drag == SUB_WINDOW_DRAG_CLOSE && gui.subwindow_drag_close_inside;
3062 Ref<Texture2D> close_icon = pressed ? sw.window->theme_cache.close_pressed : sw.window->theme_cache.close;
3063
3064 Rect2 close_rect;
3065 close_rect.position = Vector2(r.position.x + r.size.x - close_h_ofs, r.position.y - close_v_ofs);
3066 close_rect.size = close_icon->get_size();
3067
3068 if (gui.subwindow_focused != sw.window) {
3069 // Refocus.
3070 _sub_window_grab_focus(sw.window);
3071 }
3072
3073 if (close_rect.has_point(mb->get_position())) {
3074 gui.subwindow_drag = SUB_WINDOW_DRAG_CLOSE;
3075 gui.subwindow_drag_close_inside = true; // Starts inside.
3076 gui.subwindow_drag_close_rect = close_rect;
3077 } else {
3078 gui.subwindow_drag = SUB_WINDOW_DRAG_MOVE;
3079 }
3080
3081 gui.subwindow_drag_from = mb->get_position();
3082 gui.subwindow_drag_pos = sw.window->get_position();
3083
3084 _sub_window_update(sw.window);
3085 } else {
3086 gui.subwindow_resize_mode = _sub_window_get_resize_margin(sw.window, mb->get_position());
3087 if (gui.subwindow_resize_mode != SUB_WINDOW_RESIZE_DISABLED) {
3088 if (gui.subwindow_focused != sw.window) {
3089 // Refocus.
3090 _sub_window_grab_focus(sw.window);
3091 }
3092
3093 gui.subwindow_resize_from_rect = r;
3094 gui.subwindow_drag_from = mb->get_position();
3095 gui.subwindow_drag = SUB_WINDOW_DRAG_RESIZE;
3096 click_on_window = sw.window;
3097 }
3098 }
3099 }
3100 if (!click_on_window && r.has_point(mb->get_position())) {
3101 // Clicked, see if it needs to fetch focus.
3102 if (gui.subwindow_focused != sw.window) {
3103 // Refocus.
3104 _sub_window_grab_focus(sw.window);
3105 }
3106
3107 click_on_window = sw.window;
3108 }
3109
3110 if (click_on_window) {
3111 break;
3112 }
3113 }
3114
3115 gui.currently_dragged_subwindow = click_on_window;
3116
3117 if (!click_on_window && gui.subwindow_focused) {
3118 // No window found and clicked, remove focus.
3119 _sub_window_grab_focus(nullptr);
3120 }
3121 }
3122
3123 if (gui.subwindow_focused) {
3124 Ref<InputEventMouseMotion> mm = p_event;
3125 if (mm.is_valid()) {
3126 SubWindowResize resize = _sub_window_get_resize_margin(gui.subwindow_focused, mm->get_position());
3127 if (resize != SUB_WINDOW_RESIZE_DISABLED) {
3128 DisplayServer::CursorShape shapes[SUB_WINDOW_RESIZE_MAX] = {
3129 DisplayServer::CURSOR_ARROW,
3130 DisplayServer::CURSOR_FDIAGSIZE,
3131 DisplayServer::CURSOR_VSIZE,
3132 DisplayServer::CURSOR_BDIAGSIZE,
3133 DisplayServer::CURSOR_HSIZE,
3134 DisplayServer::CURSOR_HSIZE,
3135 DisplayServer::CURSOR_BDIAGSIZE,
3136 DisplayServer::CURSOR_VSIZE,
3137 DisplayServer::CURSOR_FDIAGSIZE
3138 };
3139
3140 if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CURSOR_SHAPE)) {
3141 DisplayServer::get_singleton()->cursor_set_shape(shapes[resize]);
3142 }
3143
3144 return true; // Reserved for showing the resize cursor.
3145 }
3146 }
3147 }
3148
3149 if (gui.subwindow_drag != SUB_WINDOW_DRAG_DISABLED) {
3150 return true; // Dragging, don't pass the event.
3151 }
3152
3153 if (!gui.subwindow_focused) {
3154 // No window focus, check for unfocusable windows under the cursor.
3155 Ref<InputEventMouse> me = p_event;
3156 if (me.is_valid()) {
3157 for (int i = gui.sub_windows.size() - 1; i >= 0; i--) {
3158 const SubWindow &sw = gui.sub_windows[i];
3159 if (!sw.window->get_flag(Window::FLAG_NO_FOCUS) || sw.window->get_flag(Window::FLAG_MOUSE_PASSTHROUGH)) {
3160 continue;
3161 }
3162 Rect2i r = Rect2i(sw.window->get_position(), sw.window->get_size());
3163 if (r.has_point(me->get_position())) {
3164 Transform2D window_ofs;
3165 window_ofs.set_origin(-sw.window->get_position());
3166 Ref<InputEvent> ev = p_event->xformed_by(window_ofs);
3167 sw.window->_window_input(ev);
3168 return true;
3169 }
3170 }
3171 }
3172 return false;
3173 }
3174
3175 Transform2D window_ofs;
3176 window_ofs.set_origin(-gui.subwindow_focused->get_position());
3177
3178 Ref<InputEvent> ev = p_event->xformed_by(window_ofs);
3179
3180 gui.subwindow_focused->_window_input(ev);
3181
3182 return true;
3183 }
3184
3185 void Viewport::_window_start_drag(Window *p_window) {
3186 int index = _sub_window_find(p_window);
3187 ERR_FAIL_COND(index == -1);
3188
3189 SubWindow sw = gui.sub_windows.write[index];
3190
3191 if (gui.subwindow_focused != sw.window) {
3192 // Refocus.
3193 _sub_window_grab_focus(sw.window);
3194 }
3195
3196 gui.subwindow_drag = SUB_WINDOW_DRAG_MOVE;
3197 gui.subwindow_drag_from = get_mouse_position();
3198 gui.subwindow_drag_pos = sw.window->get_position();
3199 gui.currently_dragged_subwindow = sw.window;
3200
3201 _sub_window_update(sw.window);
3202 }
3203
3204 void Viewport::_window_start_resize(SubWindowResize p_edge, Window *p_window) {
3205 int index = _sub_window_find(p_window);
3206 ERR_FAIL_COND(index == -1);
3207
3208 SubWindow sw = gui.sub_windows.write[index];
3209 Rect2i r = Rect2i(sw.window->get_position(), sw.window->get_size());
3210
3211 if (gui.subwindow_focused != sw.window) {
3212 // Refocus.
3213 _sub_window_grab_focus(sw.window);
3214 }
3215
3216 gui.subwindow_drag = SUB_WINDOW_DRAG_RESIZE;
3217 gui.subwindow_resize_mode = p_edge;
3218 gui.subwindow_resize_from_rect = r;
3219 gui.subwindow_drag_from = get_mouse_position();
3220 gui.subwindow_drag_pos = sw.window->get_position();
3221 gui.currently_dragged_subwindow = sw.window;
3222
3223 _sub_window_update(sw.window);
3224 }
3225
3226 void Viewport::_update_mouse_over(const Ref<InputEventMouse> &p_mm) {
3227 // Update gui.mouse_over and gui.subwindow_over in all Viewports.
3228 // Send necessary mouse_enter/mouse_exit signals and the MOUSE_ENTER/MOUSE_EXIT notifications for every Viewport in the SceneTree.
3229
3230 if (is_attached_in_viewport()) {
3231 // Execute this function only, when it is processed by a native Window or a SubViewport, that has no SubViewportContainer as parent.
3232 return;
3233 }
3234
3235 if (get_tree()->get_root()->is_embedding_subwindows() || is_sub_viewport()) {
3236 // Use embedder logic for calculating mouse position.
3237 _update_mouse_over(p_mm->get_position());
3238 } else {
3239 // Native Window: Use DisplayServer logic for calculating mouse position.
3240 Window *receiving_window = get_tree()->get_root()->gui.windowmanager_window_over;
3241 if (!receiving_window) {
3242 return;
3243 }
3244 if (receiving_window->get_window_id() != p_mm->get_window_id()) {
3245 Vector2 pos = DisplayServer::get_singleton()->mouse_get_position() - receiving_window->get_position();
3246 pos = receiving_window->get_final_transform().affine_inverse().xform(pos);
3247 receiving_window->_update_mouse_over(pos);
3248 } else {
3249 receiving_window->_update_mouse_over(p_mm->get_position());
3250 }
3251 }
3252 }
3253
3254 void Viewport::_update_mouse_over(Vector2 p_pos) {
3255 gui.last_mouse_pos = p_pos; // Necessary, because mouse cursor can be over Viewports that are not reached by the InputEvent.
3256 // Look for embedded windows at mouse position.
3257 if (is_embedding_subwindows()) {
3258 for (int i = gui.sub_windows.size() - 1; i >= 0; i--) {
3259 Window *sw = gui.sub_windows[i].window;
3260 Rect2 swrect = Rect2(sw->get_position(), sw->get_size());
3261 Rect2 swrect_border = swrect;
3262
3263 if (!sw->get_flag(Window::FLAG_BORDERLESS)) {
3264 int title_height = sw->theme_cache.title_height;
3265 int margin = sw->theme_cache.resize_margin;
3266 swrect_border.position.y -= title_height + margin;
3267 swrect_border.size.y += title_height + margin * 2;
3268 swrect_border.position.x -= margin;
3269 swrect_border.size.x += margin * 2;
3270 }
3271
3272 if (swrect_border.has_point(p_pos)) {
3273 if (gui.mouse_over.is_valid()) {
3274 _drop_mouse_over();
3275 } else if (!gui.subwindow_over) {
3276 _drop_physics_mouseover();
3277 }
3278 if (swrect.has_point(p_pos)) {
3279 if (sw != gui.subwindow_over) {
3280 if (gui.subwindow_over) {
3281 gui.subwindow_over->_mouse_leave_viewport();
3282 }
3283 gui.subwindow_over = sw;
3284 }
3285 if (!sw->is_input_disabled()) {
3286 sw->_update_mouse_over(sw->get_final_transform().affine_inverse().xform(p_pos - sw->get_position()));
3287 }
3288 } else {
3289 if (gui.subwindow_over) {
3290 gui.subwindow_over->_mouse_leave_viewport();
3291 gui.subwindow_over = nullptr;
3292 }
3293 }
3294 return;
3295 }
3296 }
3297
3298 if (gui.subwindow_over) {
3299 // Take care of moving mouse out of any embedded Window.
3300 gui.subwindow_over->_mouse_leave_viewport();
3301 gui.subwindow_over = nullptr;
3302 }
3303 }
3304
3305 // Look for Controls at mouse position.
3306 Control *over = gui_find_control(p_pos);
3307 ObjectID over_id = over ? over->get_instance_id() : ObjectID();
3308 get_section_root_viewport()->gui.target_control = over;
3309 bool notify_embedded_viewports = false;
3310 if (over_id != gui.mouse_over || (!over && !gui.mouse_over_hierarchy.is_empty())) {
3311 // Find the common ancestor of `gui.mouse_over` and `over`.
3312 Control *common_ancestor = nullptr;
3313 LocalVector<ObjectID> over_ancestors;
3314
3315 if (over) {
3316 // Get all ancestors that the mouse is currently over and need an enter signal.
3317 CanvasItem *ancestor = over;
3318 while (ancestor) {
3319 Control *ancestor_control = Object::cast_to<Control>(ancestor);
3320 if (ancestor_control) {
3321 if (ancestor_control->get_mouse_filter_with_override() != Control::MOUSE_FILTER_IGNORE) {
3322 int found = gui.mouse_over_hierarchy.find(ancestor_control->get_instance_id());
3323 if (found >= 0) {
3324 common_ancestor = ObjectDB::get_instance<Control>(gui.mouse_over_hierarchy[found]);
3325 break;
3326 }
3327 over_ancestors.push_back(ancestor_control->get_instance_id());
3328 }
3329 if (ancestor_control->get_mouse_filter_with_override() == Control::MOUSE_FILTER_STOP) {
3330 // MOUSE_FILTER_STOP breaks the propagation chain.
3331 break;
3332 }
3333 }
3334 if (ancestor->is_set_as_top_level()) {
3335 // Top level breaks the propagation chain.
3336 break;
3337 }
3338 ancestor = ancestor->get_parent_item();
3339 }
3340 }
3341
3342 if (gui.mouse_over.is_valid() || !gui.mouse_over_hierarchy.is_empty()) {
3343 // Send Mouse Exit Self and Mouse Exit notifications.
3344 _drop_mouse_over(common_ancestor);
3345 } else {
3346 _drop_physics_mouseover();
3347 }
3348
3349 if (over) {
3350 gui.mouse_over = over_id;
3351 gui.mouse_over_hierarchy.reserve(gui.mouse_over_hierarchy.size() + over_ancestors.size());
3352
3353 gui.sending_mouse_enter_exit_notifications = true;
3354
3355 // Send Mouse Enter notifications to parents first.
3356 for (int i = over_ancestors.size() - 1; i >= 0; i--) {
3357 Control *ctrl = ObjectDB::get_instance<Control>(over_ancestors[i]);
3358 if (ctrl) {
3359 gui.mouse_over_hierarchy.push_back(over_ancestors[i]);
3360 ctrl->notification(Control::NOTIFICATION_MOUSE_ENTER);
3361 ctrl->emit_signal(SceneStringName(mouse_entered));
3362 }
3363 }
3364
3365 // Send Mouse Enter Self notification.
3366 CanvasItem *ctrl = ObjectDB::get_instance<CanvasItem>(gui.mouse_over);
3367 if (ctrl) {
3368 ctrl->notification(Control::NOTIFICATION_MOUSE_ENTER_SELF);
3369 }
3370
3371 gui.sending_mouse_enter_exit_notifications = false;
3372
3373 notify_embedded_viewports = true;
3374 }
3375 }
3376
3377 if (over) {
3378 SubViewportContainer *c = Object::cast_to<SubViewportContainer>(over);
3379 if (!c) {
3380 return;
3381 }
3382 Vector2 pos = c->get_global_transform_with_canvas().affine_inverse().xform(p_pos);
3383 if (c->is_stretch_enabled()) {
3384 pos /= c->get_stretch_shrink();
3385 }
3386
3387 for (int i = 0; i < c->get_child_count(); i++) {
3388 SubViewport *v = Object::cast_to<SubViewport>(c->get_child(i));
3389 if (!v || v->is_input_disabled()) {
3390 continue;
3391 }
3392 if (notify_embedded_viewports) {
3393 v->notification(NOTIFICATION_VP_MOUSE_ENTER);
3394 }
3395 v->_update_mouse_over(v->get_final_transform().affine_inverse().xform(pos));
3396 }
3397
3398 Viewport *section_root = get_section_root_viewport();
3399 if (section_root && c->is_mouse_target_enabled()) {
3400 // Evaluating `mouse_target` and adjusting target_control needs to happen
3401 // after `_update_mouse_over` in the SubViewports, because otherwise physics picking
3402 // would not work inside SubViewports.
3403 section_root->gui.target_control = over;
3404 }
3405 }
3406 }
3407
3408 void Viewport::_mouse_leave_viewport() {
3409 if (!is_inside_tree() || is_input_disabled()) {
3410 return;
3411 }
3412 if (gui.subwindow_over) {
3413 gui.subwindow_over->_mouse_leave_viewport();
3414 gui.subwindow_over = nullptr;
3415 } else if (gui.mouse_over.is_valid()) {
3416 _drop_mouse_over();
3417 }
3418 notification(NOTIFICATION_VP_MOUSE_EXIT);
3419 }
3420
3421 void Viewport::_drop_mouse_over(Control *p_until_control) {
3422 if (gui.sending_mouse_enter_exit_notifications) {
3423 // If notifications are already being sent, defer call.
3424 callable_mp(this, &Viewport::_drop_mouse_over).call_deferred(p_until_control);
3425 return;
3426 }
3427
3428 _gui_cancel_tooltip();
3429 SubViewportContainer *c = ObjectDB::get_instance<SubViewportContainer>(gui.mouse_over);
3430 if (c) {
3431 for (int i = 0; i < c->get_child_count(); i++) {
3432 SubViewport *v = Object::cast_to<SubViewport>(c->get_child(i));
3433 if (!v) {
3434 continue;
3435 }
3436 v->_mouse_leave_viewport();
3437 }
3438 }
3439
3440 gui.sending_mouse_enter_exit_notifications = true;
3441 CanvasItem *over = ObjectDB::get_instance<CanvasItem>(gui.mouse_over);
3442 if (over && over->is_inside_tree()) {
3443 over->notification(Control::NOTIFICATION_MOUSE_EXIT_SELF);
3444 }
3445 Viewport *section_root = get_section_root_viewport();
3446 if (section_root && section_root->gui.target_control == over) {
3447 section_root->gui.target_control = nullptr;
3448 }
3449 gui.mouse_over = ObjectID();
3450
3451 // Send Mouse Exit notifications to children first. Don't send to p_until_control or above.
3452 int notification_until = p_until_control ? gui.mouse_over_hierarchy.find(p_until_control->get_instance_id()) + 1 : 0;
3453 for (int i = gui.mouse_over_hierarchy.size() - 1; i >= notification_until; i--) {
3454 Control *ctrl = ObjectDB::get_instance<Control>(gui.mouse_over_hierarchy[i]);
3455 if (ctrl && ctrl->is_inside_tree()) {
3456 ctrl->notification(Control::NOTIFICATION_MOUSE_EXIT);
3457 ctrl->emit_signal(SceneStringName(mouse_exited));
3458 }
3459 }
3460 gui.mouse_over_hierarchy.resize(notification_until);
3461 gui.sending_mouse_enter_exit_notifications = false;
3462 }
3463
3464 void Viewport::push_input(RequiredParam<InputEvent> rp_event, bool p_local_coords) {
3465 ERR_MAIN_THREAD_GUARD;
3466 ERR_FAIL_COND(!is_inside_tree());
3467 EXTRACT_PARAM_OR_FAIL(p_event, rp_event);
3468
3469 if (disable_input || disable_input_override) {
3470 return;
3471 }
3472
3473 if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_ancestor_of(this)) {
3474 return;
3475 }
3476
3477 local_input_handled = false;
3478 if (!handle_input_locally) {
3479 Viewport *vp = this;
3480 while (true) {
3481 if (Object::cast_to<Window>(vp) || !vp->get_parent()) {
3482 break;
3483 }
3484 vp = vp->get_parent()->get_viewport();
3485 }
3486 vp->local_input_handled = false;
3487 }
3488
3489 Ref<InputEvent> ev;
3490 if (!p_local_coords) {
3491 ev = _make_input_local(p_event);
3492 } else {
3493 ev = p_event;
3494 }
3495
3496 Ref<InputEventMouse> me = ev;
3497 if (me.is_valid()) {
3498 _update_mouse_over(me);
3499 }
3500
3501 if (is_embedding_subwindows() && _sub_windows_forward_input(ev)) {
3502 set_input_as_handled();
3503 return;
3504 }
3505
3506 if (!_can_consume_input_events()) {
3507 return;
3508 }
3509
3510 if (!is_input_handled()) {
3511 ERR_FAIL_COND(!is_inside_tree());
3512 get_tree()->_call_input_pause(input_group, SceneTree::CALL_INPUT_TYPE_INPUT, ev, this); //not a bug, must happen before GUI, order is _input -> gui input -> _unhandled input
3513 }
3514
3515 if (!is_input_handled()) {
3516 ERR_FAIL_COND(!is_inside_tree());
3517 _gui_input_event(ev);
3518 } else {
3519 // Cleanup internal GUI state after accepting event during _input().
3520 _gui_cleanup_internal_state(ev);
3521 }
3522
3523 if (!is_input_handled()) {
3524 _push_unhandled_input_internal(ev);
3525 }
3526
3527 event_count++;
3528 }
3529
3530 #ifndef DISABLE_DEPRECATED
3531 void Viewport::push_unhandled_input(RequiredParam<InputEvent> rp_event, bool p_local_coords) {
3532 ERR_MAIN_THREAD_GUARD;
3533 WARN_DEPRECATED_MSG(R"*(The "push_unhandled_input()" method is deprecated, use "push_input()" instead.)*");
3534 ERR_FAIL_COND(!is_inside_tree());
3535 EXTRACT_PARAM_OR_FAIL(p_event, rp_event);
3536
3537 local_input_handled = false;
3538
3539 if (disable_input || disable_input_override || !_can_consume_input_events()) {
3540 return;
3541 }
3542
3543 if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_ancestor_of(this)) {
3544 return;
3545 }
3546
3547 Ref<InputEvent> ev;
3548 if (!p_local_coords) {
3549 ev = _make_input_local(p_event);
3550 } else {
3551 ev = p_event;
3552 }
3553
3554 _push_unhandled_input_internal(ev);
3555 }
3556 #endif // DISABLE_DEPRECATED
3557
3558 void Viewport::_push_unhandled_input_internal(const Ref<InputEvent> &p_event) {
3559 // Shortcut Input.
3560 if (Object::cast_to<InputEventKey>(*p_event) != nullptr || Object::cast_to<InputEventShortcut>(*p_event) != nullptr || Object::cast_to<InputEventJoypadButton>(*p_event) != nullptr) {
3561 ERR_FAIL_COND(!is_inside_tree());
3562 get_tree()->_call_input_pause(shortcut_input_group, SceneTree::CALL_INPUT_TYPE_SHORTCUT_INPUT, p_event, this);
3563 }
3564
3565 // Unhandled key Input - Used for performance reasons - This is called a lot less than _unhandled_input since it ignores MouseMotion, and to handle Unicode input with Alt / Ctrl modifiers after handling shortcuts.
3566 if (!is_input_handled() && (Object::cast_to<InputEventKey>(*p_event) != nullptr)) {
3567 ERR_FAIL_COND(!is_inside_tree());
3568 get_tree()->_call_input_pause(unhandled_key_input_group, SceneTree::CALL_INPUT_TYPE_UNHANDLED_KEY_INPUT, p_event, this);
3569 }
3570
3571 // Unhandled Input.
3572 if (!is_input_handled()) {
3573 ERR_FAIL_COND(!is_inside_tree());
3574 get_tree()->_call_input_pause(unhandled_input_group, SceneTree::CALL_INPUT_TYPE_UNHANDLED_INPUT, p_event, this);
3575 }
3576
3577 #if !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
3578 if (physics_object_picking && !is_input_handled()) {
3579 if (Input::get_singleton()->get_mouse_mode() != Input::MOUSE_MODE_CAPTURED &&
3580 (Object::cast_to<InputEventMouse>(*p_event) ||
3581 Object::cast_to<InputEventScreenDrag>(*p_event) ||
3582 Object::cast_to<InputEventScreenTouch>(*p_event)
3583
3584 )) {
3585 physics_picking_events.push_back(p_event);
3586 set_input_as_handled();
3587 }
3588 }
3589 #endif // !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
3590 }
3591
3592 void Viewport::notify_mouse_entered() {
3593 if (gui.mouse_in_viewport) {
3594 WARN_PRINT_ED("The Viewport was previously notified that the mouse is in its area. There is no need to notify it at this time.");
3595 return;
3596 }
3597 notification(NOTIFICATION_VP_MOUSE_ENTER);
3598 }
3599
3600 void Viewport::notify_mouse_exited() {
3601 if (!gui.mouse_in_viewport) {
3602 WARN_PRINT_ED("The Viewport was previously notified that the mouse has left its area. There is no need to notify it at this time.");
3603 return;
3604 }
3605 _mouse_leave_viewport();
3606 }
3607
3608 #if !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
3609 void Viewport::set_physics_object_picking(bool p_enable) {
3610 ERR_MAIN_THREAD_GUARD;
3611 physics_object_picking = p_enable;
3612 if (physics_object_picking) {
3613 add_to_group("_picking_viewports");
3614 } else {
3615 physics_picking_events.clear();
3616 if (is_in_group("_picking_viewports")) {
3617 remove_from_group("_picking_viewports");
3618 }
3619 }
3620 }
3621
3622 bool Viewport::get_physics_object_picking() {
3623 ERR_READ_THREAD_GUARD_V(false);
3624 return physics_object_picking;
3625 }
3626
3627 void Viewport::set_physics_object_picking_sort(bool p_enable) {
3628 ERR_MAIN_THREAD_GUARD;
3629 physics_object_picking_sort = p_enable;
3630 }
3631
3632 bool Viewport::get_physics_object_picking_sort() {
3633 ERR_READ_THREAD_GUARD_V(false);
3634 return physics_object_picking_sort;
3635 }
3636
3637 void Viewport::set_physics_object_picking_first_only(bool p_enable) {
3638 physics_object_picking_first_only = p_enable;
3639 }
3640
3641 bool Viewport::get_physics_object_picking_first_only() {
3642 return physics_object_picking_first_only;
3643 }
3644 #endif // !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
3645
3646 Vector2 Viewport::get_camera_coords(const Vector2 &p_viewport_coords) const {
3647 ERR_READ_THREAD_GUARD_V(Vector2());
3648 Transform2D xf = stretch_transform * global_canvas_transform;
3649 return xf.xform(p_viewport_coords);
3650 }
3651
3652 Vector2 Viewport::get_camera_rect_size() const {
3653 ERR_READ_THREAD_GUARD_V(Vector2());
3654 return size;
3655 }
3656
3657 void Viewport::set_disable_input(bool p_disable) {
3658 ERR_MAIN_THREAD_GUARD;
3659 if (p_disable == disable_input) {
3660 return;
3661 }
3662 if (p_disable && !disable_input_override) {
3663 _drop_mouse_focus();
3664 _mouse_leave_viewport();
3665 _gui_cancel_tooltip();
3666 }
3667 disable_input = p_disable;
3668 }
3669
3670 bool Viewport::is_input_disabled() const {
3671 ERR_READ_THREAD_GUARD_V(false);
3672 return disable_input;
3673 }
3674
3675 void Viewport::set_disable_input_override(bool p_disable) {
3676 ERR_MAIN_THREAD_GUARD;
3677 if (p_disable == disable_input_override) {
3678 return;
3679 }
3680 if (p_disable && !disable_input) {
3681 _drop_mouse_focus();
3682 _mouse_leave_viewport();
3683 _gui_cancel_tooltip();
3684 }
3685 disable_input_override = p_disable;
3686 }
3687
3688 String Viewport::gui_get_drag_description() const {
3689 ERR_READ_THREAD_GUARD_V(String());
3690 if (get_section_root_viewport()->gui.drag_description.is_empty()) {
3691 return RTR("Drag-and-drop data");
3692 } else {
3693 return get_section_root_viewport()->gui.drag_description;
3694 }
3695 }
3696
3697 void Viewport::gui_set_drag_description(const String &p_description) {
3698 gui.drag_description = p_description;
3699 }
3700
3701 Variant Viewport::gui_get_drag_data() const {
3702 ERR_READ_THREAD_GUARD_V(Variant());
3703 return get_section_root_viewport()->gui.drag_data;
3704 }
3705
3706 PackedStringArray Viewport::get_configuration_warnings() const {
3707 ERR_MAIN_THREAD_GUARD_V(PackedStringArray());
3708 PackedStringArray warnings = Node::get_configuration_warnings();
3709
3710 if (size.x <= 1 || size.y <= 1) {
3711 warnings.push_back(RTR("The Viewport size must be greater than or equal to 2 pixels on both dimensions to render anything."));
3712 }
3713 return warnings;
3714 }
3715
3716 void Viewport::gui_reset_canvas_sort_index() {
3717 ERR_MAIN_THREAD_GUARD;
3718 gui.canvas_sort_index = 0;
3719 }
3720
3721 int Viewport::gui_get_canvas_sort_index() {
3722 ERR_MAIN_THREAD_GUARD_V(0);
3723 return gui.canvas_sort_index++;
3724 }
3725
3726 void Viewport::gui_release_focus() {
3727 ERR_MAIN_THREAD_GUARD;
3728 if (gui.key_focus) {
3729 Control *f = gui.key_focus;
3730 gui.key_focus = nullptr;
3731 f->notification(Control::NOTIFICATION_FOCUS_EXIT, true);
3732 f->queue_redraw();
3733 }
3734 }
3735
3736 Control *Viewport::gui_get_focus_owner() const {
3737 ERR_READ_THREAD_GUARD_V(nullptr);
3738 return gui.key_focus;
3739 }
3740
3741 Control *Viewport::gui_get_hovered_control() const {
3742 ERR_READ_THREAD_GUARD_V(nullptr);
3743 return ObjectDB::get_instance<Control>(gui.mouse_over);
3744 }
3745
3746 void Viewport::set_msaa_2d(MSAA p_msaa) {
3747 ERR_MAIN_THREAD_GUARD;
3748 ERR_FAIL_INDEX(p_msaa, MSAA_MAX);
3749 if (msaa_2d == p_msaa) {
3750 return;
3751 }
3752 msaa_2d = p_msaa;
3753 RS::get_singleton()->viewport_set_msaa_2d(viewport, RS::ViewportMSAA(p_msaa));
3754 }
3755
3756 Viewport::MSAA Viewport::get_msaa_2d() const {
3757 ERR_READ_THREAD_GUARD_V(MSAA_DISABLED);
3758 return msaa_2d;
3759 }
3760
3761 void Viewport::set_msaa_3d(MSAA p_msaa) {
3762 ERR_MAIN_THREAD_GUARD;
3763 ERR_FAIL_INDEX(p_msaa, MSAA_MAX);
3764 if (msaa_3d == p_msaa) {
3765 return;
3766 }
3767 msaa_3d = p_msaa;
3768 RS::get_singleton()->viewport_set_msaa_3d(viewport, RS::ViewportMSAA(p_msaa));
3769 }
3770
3771 Viewport::MSAA Viewport::get_msaa_3d() const {
3772 ERR_READ_THREAD_GUARD_V(MSAA_DISABLED);
3773 return msaa_3d;
3774 }
3775
3776 void Viewport::set_screen_space_aa(ScreenSpaceAA p_screen_space_aa) {
3777 ERR_MAIN_THREAD_GUARD;
3778 ERR_FAIL_INDEX(p_screen_space_aa, SCREEN_SPACE_AA_MAX);
3779 if (screen_space_aa == p_screen_space_aa) {
3780 return;
3781 }
3782 screen_space_aa = p_screen_space_aa;
3783 RS::get_singleton()->viewport_set_screen_space_aa(viewport, RS::ViewportScreenSpaceAA(p_screen_space_aa));
3784 }
3785
3786 Viewport::ScreenSpaceAA Viewport::get_screen_space_aa() const {
3787 ERR_READ_THREAD_GUARD_V(SCREEN_SPACE_AA_DISABLED);
3788 return screen_space_aa;
3789 }
3790
3791 void Viewport::set_use_taa(bool p_use_taa) {
3792 ERR_MAIN_THREAD_GUARD;
3793 if (use_taa == p_use_taa) {
3794 return;
3795 }
3796 use_taa = p_use_taa;
3797 RS::get_singleton()->viewport_set_use_taa(viewport, p_use_taa);
3798 }
3799
3800 bool Viewport::is_using_taa() const {
3801 ERR_READ_THREAD_GUARD_V(false);
3802 return use_taa;
3803 }
3804
3805 void Viewport::set_use_debanding(bool p_use_debanding) {
3806 ERR_MAIN_THREAD_GUARD;
3807 if (use_debanding == p_use_debanding) {
3808 return;
3809 }
3810 use_debanding = p_use_debanding;
3811 RS::get_singleton()->viewport_set_use_debanding(viewport, p_use_debanding);
3812 }
3813
3814 bool Viewport::is_using_debanding() const {
3815 ERR_READ_THREAD_GUARD_V(false);
3816 return use_debanding;
3817 }
3818
3819 void Viewport::set_mesh_lod_threshold(float p_pixels) {
3820 ERR_MAIN_THREAD_GUARD;
3821 mesh_lod_threshold = p_pixels;
3822 RS::get_singleton()->viewport_set_mesh_lod_threshold(viewport, mesh_lod_threshold);
3823 }
3824
3825 float Viewport::get_mesh_lod_threshold() const {
3826 ERR_READ_THREAD_GUARD_V(0);
3827 return mesh_lod_threshold;
3828 }
3829
3830 void Viewport::set_use_occlusion_culling(bool p_use_occlusion_culling) {
3831 ERR_MAIN_THREAD_GUARD;
3832 if (use_occlusion_culling == p_use_occlusion_culling) {
3833 return;
3834 }
3835
3836 use_occlusion_culling = p_use_occlusion_culling;
3837 RS::get_singleton()->viewport_set_use_occlusion_culling(viewport, p_use_occlusion_culling);
3838
3839 notify_property_list_changed();
3840 }
3841
3842 bool Viewport::is_using_occlusion_culling() const {
3843 ERR_READ_THREAD_GUARD_V(false);
3844 return use_occlusion_culling;
3845 }
3846
3847 void Viewport::set_debug_draw(DebugDraw p_debug_draw) {
3848 ERR_MAIN_THREAD_GUARD;
3849 debug_draw = p_debug_draw;
3850 RS::get_singleton()->viewport_set_debug_draw(viewport, RS::ViewportDebugDraw(p_debug_draw));
3851 }
3852
3853 Viewport::DebugDraw Viewport::get_debug_draw() const {
3854 ERR_READ_THREAD_GUARD_V(DEBUG_DRAW_DISABLED);
3855 return debug_draw;
3856 }
3857
3858 int Viewport::get_render_info(RenderInfoType p_type, RenderInfo p_info) {
3859 ERR_READ_THREAD_GUARD_V(0);
3860 return RS::get_singleton()->viewport_get_render_info(viewport, RS::ViewportRenderInfoType(p_type), RS::ViewportRenderInfo(p_info));
3861 }
3862
3863 void Viewport::set_snap_controls_to_pixels(bool p_enable) {
3864 ERR_MAIN_THREAD_GUARD;
3865 snap_controls_to_pixels = p_enable;
3866 }
3867
3868 bool Viewport::is_snap_controls_to_pixels_enabled() const {
3869 ERR_READ_THREAD_GUARD_V(false);
3870 return snap_controls_to_pixels;
3871 }
3872
3873 void Viewport::set_snap_2d_transforms_to_pixel(bool p_enable) {
3874 ERR_MAIN_THREAD_GUARD;
3875 snap_2d_transforms_to_pixel = p_enable;
3876 RS::get_singleton()->viewport_set_snap_2d_transforms_to_pixel(viewport, snap_2d_transforms_to_pixel);
3877 }
3878
3879 bool Viewport::is_snap_2d_transforms_to_pixel_enabled() const {
3880 ERR_READ_THREAD_GUARD_V(false);
3881 return snap_2d_transforms_to_pixel;
3882 }
3883
3884 void Viewport::set_snap_2d_vertices_to_pixel(bool p_enable) {
3885 ERR_MAIN_THREAD_GUARD;
3886 snap_2d_vertices_to_pixel = p_enable;
3887 RS::get_singleton()->viewport_set_snap_2d_vertices_to_pixel(viewport, snap_2d_vertices_to_pixel);
3888 }
3889
3890 bool Viewport::is_snap_2d_vertices_to_pixel_enabled() const {
3891 ERR_READ_THREAD_GUARD_V(false);
3892 return snap_2d_vertices_to_pixel;
3893 }
3894
3895 bool Viewport::gui_is_dragging() const {
3896 ERR_READ_THREAD_GUARD_V(false);
3897 return get_section_root_viewport()->gui.global_dragging;
3898 }
3899
3900 bool Viewport::gui_is_drag_successful() const {
3901 ERR_READ_THREAD_GUARD_V(false);
3902 return gui.drag_successful;
3903 }
3904
3905 void Viewport::gui_cancel_drag() {
3906 ERR_MAIN_THREAD_GUARD;
3907 if (gui_is_dragging()) {
3908 _perform_drop();
3909 }
3910 }
3911
3912 void Viewport::set_input_as_handled() {
3913 ERR_MAIN_THREAD_GUARD;
3914 if (!handle_input_locally) {
3915 ERR_FAIL_COND(!is_inside_tree());
3916 Viewport *vp = this;
3917 while (true) {
3918 if (Object::cast_to<Window>(vp)) {
3919 break;
3920 }
3921 if (!vp->get_parent()) {
3922 break;
3923 }
3924 vp = vp->get_parent()->get_viewport();
3925 }
3926 if (vp != this) {
3927 vp->set_input_as_handled();
3928 return;
3929 }
3930 }
3931
3932 local_input_handled = true;
3933 }
3934
3935 bool Viewport::is_input_handled() const {
3936 ERR_READ_THREAD_GUARD_V(false);
3937 if (!handle_input_locally) {
3938 ERR_FAIL_COND_V(!is_inside_tree(), false);
3939 const Viewport *vp = this;
3940 while (true) {
3941 if (Object::cast_to<Window>(vp)) {
3942 break;
3943 }
3944 if (!vp->get_parent()) {
3945 break;
3946 }
3947 vp = vp->get_parent()->get_viewport();
3948 }
3949 if (vp != this) {
3950 return vp->is_input_handled();
3951 }
3952 }
3953 return local_input_handled;
3954 }
3955
3956 void Viewport::set_handle_input_locally(bool p_enable) {
3957 ERR_MAIN_THREAD_GUARD;
3958 handle_input_locally = p_enable;
3959 }
3960
3961 bool Viewport::is_handling_input_locally() const {
3962 ERR_READ_THREAD_GUARD_V(false);
3963 return handle_input_locally;
3964 }
3965
3966 void Viewport::set_default_canvas_item_texture_filter(DefaultCanvasItemTextureFilter p_filter) {
3967 ERR_MAIN_THREAD_GUARD;
3968 ERR_FAIL_INDEX(p_filter, DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_MAX);
3969
3970 if (default_canvas_item_texture_filter == p_filter) {
3971 return;
3972 }
3973 default_canvas_item_texture_filter = p_filter;
3974 switch (default_canvas_item_texture_filter) {
3975 case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST:
3976 RS::get_singleton()->viewport_set_default_canvas_item_texture_filter(viewport, RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
3977 break;
3978 case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR:
3979 RS::get_singleton()->viewport_set_default_canvas_item_texture_filter(viewport, RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR);
3980 break;
3981 case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
3982 RS::get_singleton()->viewport_set_default_canvas_item_texture_filter(viewport, RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS);
3983 break;
3984 case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
3985 RS::get_singleton()->viewport_set_default_canvas_item_texture_filter(viewport, RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
3986 break;
3987 default: {
3988 }
3989 }
3990 }
3991
3992 Viewport::DefaultCanvasItemTextureFilter Viewport::get_default_canvas_item_texture_filter() const {
3993 ERR_READ_THREAD_GUARD_V(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
3994 return default_canvas_item_texture_filter;
3995 }
3996
3997 void Viewport::set_default_canvas_item_texture_repeat(DefaultCanvasItemTextureRepeat p_repeat) {
3998 ERR_MAIN_THREAD_GUARD;
3999 ERR_FAIL_INDEX(p_repeat, DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_MAX);
4000
4001 if (default_canvas_item_texture_repeat == p_repeat) {
4002 return;
4003 }
4004
4005 default_canvas_item_texture_repeat = p_repeat;
4006
4007 switch (default_canvas_item_texture_repeat) {
4008 case DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_DISABLED:
4009 RS::get_singleton()->viewport_set_default_canvas_item_texture_repeat(viewport, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
4010 break;
4011 case DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_ENABLED:
4012 RS::get_singleton()->viewport_set_default_canvas_item_texture_repeat(viewport, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
4013 break;
4014 case DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_MIRROR:
4015 RS::get_singleton()->viewport_set_default_canvas_item_texture_repeat(viewport, RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR);
4016 break;
4017 default: {
4018 }
4019 }
4020 }
4021
4022 Viewport::DefaultCanvasItemTextureRepeat Viewport::get_default_canvas_item_texture_repeat() const {
4023 ERR_READ_THREAD_GUARD_V(DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
4024 return default_canvas_item_texture_repeat;
4025 }
4026
4027 void Viewport::set_vrs_mode(Viewport::VRSMode p_vrs_mode) {
4028 ERR_MAIN_THREAD_GUARD;
4029 // Note, set this even if not supported on this hardware, it will only be used if it is but we want to save the value as set by the user.
4030 vrs_mode = p_vrs_mode;
4031
4032 switch (p_vrs_mode) {
4033 case VRS_TEXTURE: {
4034 RS::get_singleton()->viewport_set_vrs_mode(viewport, RS::VIEWPORT_VRS_TEXTURE);
4035 } break;
4036 case VRS_XR: {
4037 RS::get_singleton()->viewport_set_vrs_mode(viewport, RS::VIEWPORT_VRS_XR);
4038 } break;
4039 default: {
4040 RS::get_singleton()->viewport_set_vrs_mode(viewport, RS::VIEWPORT_VRS_DISABLED);
4041 } break;
4042 }
4043
4044 notify_property_list_changed();
4045 }
4046
4047 Viewport::VRSMode Viewport::get_vrs_mode() const {
4048 ERR_READ_THREAD_GUARD_V(VRS_DISABLED);
4049 return vrs_mode;
4050 }
4051
4052 void Viewport::set_vrs_update_mode(VRSUpdateMode p_vrs_update_mode) {
4053 ERR_MAIN_THREAD_GUARD;
4054
4055 vrs_update_mode = p_vrs_update_mode;
4056 switch (p_vrs_update_mode) {
4057 case VRS_UPDATE_ONCE: {
4058 RS::get_singleton()->viewport_set_vrs_update_mode(viewport, RS::VIEWPORT_VRS_UPDATE_ONCE);
4059 } break;
4060 case VRS_UPDATE_ALWAYS: {
4061 RS::get_singleton()->viewport_set_vrs_update_mode(viewport, RS::VIEWPORT_VRS_UPDATE_ALWAYS);
4062 } break;
4063 default: {
4064 RS::get_singleton()->viewport_set_vrs_update_mode(viewport, RS::VIEWPORT_VRS_UPDATE_DISABLED);
4065 } break;
4066 }
4067 }
4068
4069 Viewport::VRSUpdateMode Viewport::get_vrs_update_mode() const {
4070 ERR_READ_THREAD_GUARD_V(VRS_UPDATE_DISABLED);
4071 return vrs_update_mode;
4072 }
4073
4074 void Viewport::set_vrs_texture(Ref<Texture2D> p_texture) {
4075 ERR_MAIN_THREAD_GUARD;
4076 vrs_texture = p_texture;
4077
4078 // TODO need to add something here in case the RID changes
4079 RID tex = p_texture.is_valid() ? p_texture->get_rid() : RID();
4080 RS::get_singleton()->viewport_set_vrs_texture(viewport, tex);
4081 }
4082
4083 Ref<Texture2D> Viewport::get_vrs_texture() const {
4084 ERR_READ_THREAD_GUARD_V(Ref<Texture2D>());
4085 return vrs_texture;
4086 }
4087
4088 DisplayServer::WindowID Viewport::get_window_id() const {
4089 ERR_READ_THREAD_GUARD_V(DisplayServer::INVALID_WINDOW_ID);
4090 return DisplayServer::MAIN_WINDOW_ID;
4091 }
4092
4093 Viewport *Viewport::get_parent_viewport() const {
4094 ERR_READ_THREAD_GUARD_V(nullptr);
4095 ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
4096 if (!get_parent()) {
4097 return nullptr; //root viewport
4098 }
4099
4100 return get_parent()->get_viewport();
4101 }
4102
4103 void Viewport::set_embedding_subwindows(bool p_embed) {
4104 ERR_THREAD_GUARD;
4105 if (gui.embed_subwindows_hint == p_embed) {
4106 return;
4107 }
4108
4109 bool allow_change = true;
4110
4111 if (!is_inside_tree()) {
4112 // Change can happen since no child window is displayed.
4113 } else if (gui.embed_subwindows_hint) {
4114 if (!gui.sub_windows.is_empty()) {
4115 // Prevent change when this viewport has embedded windows.
4116 allow_change = false;
4117 }
4118 } else {
4119 Viewport *vp = this;
4120 while (true) {
4121 if (!vp->get_parent()) {
4122 // Root window reached.
4123 break;
4124 }
4125 vp = vp->get_parent()->get_viewport();
4126 if (vp->is_embedding_subwindows()) {
4127 for (int i = 0; i < vp->gui.sub_windows.size(); i++) {
4128 if (is_ancestor_of(vp->gui.sub_windows[i].window)) {
4129 // Prevent change when this viewport has child windows that are displayed in an ancestor viewport.
4130 allow_change = false;
4131 break;
4132 }
4133 }
4134 }
4135 }
4136
4137 if (allow_change) {
4138 Vector<DisplayServer::WindowID> wl = DisplayServer::get_singleton()->get_window_list();
4139 for (const DisplayServer::WindowID &window_id : wl) {
4140 const Window *w = Window::get_from_id(window_id);
4141 if (w && is_ancestor_of(w)) {
4142 // Prevent change when this viewport has child windows that are displayed as native windows.
4143 allow_change = false;
4144 break;
4145 }
4146 }
4147 }
4148 }
4149
4150 if (allow_change) {
4151 gui.embed_subwindows_hint = p_embed;
4152 } else {
4153 WARN_PRINT("Can't change \"gui_embed_subwindows\" while a child window is displayed. Consider hiding all child windows before changing this value.");
4154 }
4155 }
4156
4157 bool Viewport::is_embedding_subwindows() const {
4158 ERR_READ_THREAD_GUARD_V(false);
4159 return gui.embed_subwindows_hint;
4160 }
4161
4162 TypedArray<Window> Viewport::get_embedded_subwindows() const {
4163 TypedArray<Window> windows;
4164 for (int i = 0; i < gui.sub_windows.size(); i++) {
4165 windows.append(gui.sub_windows[i].window);
4166 }
4167
4168 return windows;
4169 }
4170
4171 void Viewport::set_drag_threshold(int p_threshold) {
4172 ERR_MAIN_THREAD_GUARD
4173 gui.drag_threshold = p_threshold;
4174 }
4175
4176 int Viewport::get_drag_threshold() const {
4177 ERR_READ_THREAD_GUARD_V(10);
4178 return gui.drag_threshold;
4179 }
4180
4181 void Viewport::subwindow_set_popup_safe_rect(Window *p_window, const Rect2i &p_rect) {
4182 int index = _sub_window_find(p_window);
4183 ERR_FAIL_COND(index == -1);
4184
4185 gui.sub_windows.write[index].parent_safe_rect = p_rect;
4186 }
4187
4188 Rect2i Viewport::subwindow_get_popup_safe_rect(Window *p_window) const {
4189 int index = _sub_window_find(p_window);
4190 // FIXME: Re-enable ERR_FAIL_COND after rewriting embedded window popup closing.
4191 // Currently it is expected, that index == -1 can happen.
4192 if (index == -1) {
4193 return Rect2i();
4194 }
4195 // ERR_FAIL_COND_V(index == -1, Rect2i());
4196
4197 return gui.sub_windows[index].parent_safe_rect;
4198 }
4199
4200 void Viewport::set_sdf_oversize(SDFOversize p_sdf_oversize) {
4201 ERR_MAIN_THREAD_GUARD;
4202 ERR_FAIL_INDEX(p_sdf_oversize, SDF_OVERSIZE_MAX);
4203 sdf_oversize = p_sdf_oversize;
4204 RS::get_singleton()->viewport_set_sdf_oversize_and_scale(viewport, RS::ViewportSDFOversize(sdf_oversize), RS::ViewportSDFScale(sdf_scale));
4205 }
4206
4207 Viewport::SDFOversize Viewport::get_sdf_oversize() const {
4208 ERR_READ_THREAD_GUARD_V(SDF_OVERSIZE_100_PERCENT);
4209 return sdf_oversize;
4210 }
4211
4212 void Viewport::set_sdf_scale(SDFScale p_sdf_scale) {
4213 ERR_MAIN_THREAD_GUARD;
4214 ERR_FAIL_INDEX(p_sdf_scale, SDF_SCALE_MAX);
4215 sdf_scale = p_sdf_scale;
4216 RS::get_singleton()->viewport_set_sdf_oversize_and_scale(viewport, RS::ViewportSDFOversize(sdf_oversize), RS::ViewportSDFScale(sdf_scale));
4217 }
4218
4219 Viewport::SDFScale Viewport::get_sdf_scale() const {
4220 ERR_READ_THREAD_GUARD_V(SDF_SCALE_100_PERCENT);
4221 return sdf_scale;
4222 }
4223
4224 Transform2D Viewport::get_screen_transform() const {
4225 ERR_READ_THREAD_GUARD_V(Transform2D());
4226 return get_screen_transform_internal();
4227 }
4228
4229 Transform2D Viewport::get_screen_transform_internal(bool p_absolute_position) const {
4230 ERR_READ_THREAD_GUARD_V(Transform2D());
4231 return get_final_transform();
4232 }
4233
4234 void Viewport::update_mouse_cursor_state() {
4235 // Updates need to happen in Window, because SubViewportContainers might be hidden behind other Controls.
4236 Window *base_window = get_base_window();
4237 if (base_window) {
4238 base_window->update_mouse_cursor_state();
4239 }
4240 }
4241
4242 void Viewport::set_canvas_cull_mask(uint32_t p_canvas_cull_mask) {
4243 ERR_MAIN_THREAD_GUARD;
4244 canvas_cull_mask = p_canvas_cull_mask;
4245 RenderingServer::get_singleton()->viewport_set_canvas_cull_mask(viewport, canvas_cull_mask);
4246 }
4247
4248 uint32_t Viewport::get_canvas_cull_mask() const {
4249 ERR_READ_THREAD_GUARD_V(0);
4250 return canvas_cull_mask;
4251 }
4252
4253 void Viewport::set_canvas_cull_mask_bit(uint32_t p_layer, bool p_enable) {
4254 ERR_MAIN_THREAD_GUARD;
4255 ERR_FAIL_UNSIGNED_INDEX(p_layer, 32);
4256 if (p_enable) {
4257 set_canvas_cull_mask(canvas_cull_mask | (1 << p_layer));
4258 } else {
4259 set_canvas_cull_mask(canvas_cull_mask & (~(1 << p_layer)));
4260 }
4261 }
4262
4263 bool Viewport::get_canvas_cull_mask_bit(uint32_t p_layer) const {
4264 ERR_READ_THREAD_GUARD_V(false);
4265 ERR_FAIL_UNSIGNED_INDEX_V(p_layer, 32, false);
4266 return (canvas_cull_mask & (1 << p_layer));
4267 }
4268
4269 #ifdef TOOLS_ENABLED
4270 bool Viewport::is_visible_subviewport() const {
4271 if (!is_sub_viewport()) {
4272 return true;
4273 }
4274 SubViewportContainer *container = Object::cast_to<SubViewportContainer>(get_parent());
4275 return container && container->is_visible_in_tree();
4276 }
4277 #endif // TOOLS_ENABLED
4278
4279 void Viewport::_update_audio_listener_2d() {
4280 if (AudioServer::get_singleton()) {
4281 AudioServer::get_singleton()->notify_listener_changed();
4282 }
4283 }
4284
4285 void Viewport::_audio_listener_2d_set(AudioListener2D *p_audio_listener) {
4286 if (audio_listener_2d == p_audio_listener) {
4287 return;
4288 } else if (audio_listener_2d) {
4289 audio_listener_2d->clear_current();
4290 }
4291 audio_listener_2d = p_audio_listener;
4292 }
4293
4294 void Viewport::_audio_listener_2d_remove(AudioListener2D *p_audio_listener) {
4295 if (audio_listener_2d == p_audio_listener) {
4296 audio_listener_2d = nullptr;
4297 }
4298 }
4299
4300 void Viewport::_camera_2d_set(Camera2D *p_camera_2d) {
4301 #if DEBUG_ENABLED
4302 if (is_camera_2d_override_enabled()) {
4303 camera_2d_override.set_overridden_camera(p_camera_2d);
4304 return;
4305 }
4306 #endif // DEBUG_ENABLED
4307
4308 camera_2d = p_camera_2d;
4309 }
4310
4311 #ifndef PHYSICS_2D_DISABLED
4312 void Viewport::_cleanup_mouseover_colliders(bool p_clean_all_frames, bool p_paused_only, uint64_t p_frame_reference) {
4313 List<ObjectID> to_erase;
4314 List<ObjectID> to_mouse_exit;
4315
4316 for (const KeyValue<ObjectID, uint64_t> &E : physics_2d_mouseover) {
4317 if (!p_clean_all_frames && E.value == p_frame_reference) {
4318 continue;
4319 }
4320
4321 Object *o = ObjectDB::get_instance(E.key);
4322 if (o) {
4323 CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
4324 if (co && co->is_inside_tree()) {
4325 if (p_clean_all_frames && p_paused_only && co->can_process()) {
4326 continue;
4327 }
4328 to_mouse_exit.push_back(E.key);
4329 }
4330 }
4331 to_erase.push_back(E.key);
4332 }
4333
4334 while (to_erase.size()) {
4335 physics_2d_mouseover.erase(to_erase.front()->get());
4336 to_erase.pop_front();
4337 }
4338
4339 // Per-shape.
4340 List<Pair<ObjectID, int>> shapes_to_erase;
4341 List<Pair<ObjectID, int>> shapes_to_mouse_exit;
4342
4343 for (KeyValue<Pair<ObjectID, int>, uint64_t> &E : physics_2d_shape_mouseover) {
4344 if (!p_clean_all_frames && E.value == p_frame_reference) {
4345 continue;
4346 }
4347
4348 Object *o = ObjectDB::get_instance(E.key.first);
4349 if (o) {
4350 CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
4351 if (co && co->is_inside_tree()) {
4352 if (p_clean_all_frames && p_paused_only && co->can_process()) {
4353 continue;
4354 }
4355 shapes_to_mouse_exit.push_back(E.key);
4356 }
4357 }
4358 shapes_to_erase.push_back(E.key);
4359 }
4360
4361 while (shapes_to_erase.size()) {
4362 physics_2d_shape_mouseover.erase(shapes_to_erase.front()->get());
4363 shapes_to_erase.pop_front();
4364 }
4365
4366 while (to_mouse_exit.size()) {
4367 Object *o = ObjectDB::get_instance(to_mouse_exit.front()->get());
4368 CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
4369 co->_mouse_exit();
4370 to_mouse_exit.pop_front();
4371 }
4372
4373 while (shapes_to_mouse_exit.size()) {
4374 Pair<ObjectID, int> e = shapes_to_mouse_exit.front()->get();
4375 Object *o = ObjectDB::get_instance(e.first);
4376 CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
4377 co->_mouse_shape_exit(e.second);
4378 shapes_to_mouse_exit.pop_front();
4379 }
4380 }
4381 #endif // PHYSICS_2D_DISABLED
4382
4383 AudioListener2D *Viewport::get_audio_listener_2d() const {
4384 ERR_READ_THREAD_GUARD_V(nullptr);
4385 return audio_listener_2d;
4386 }
4387
4388 void Viewport::set_as_audio_listener_2d(bool p_enable) {
4389 ERR_MAIN_THREAD_GUARD;
4390 if (p_enable == is_audio_listener_2d_enabled) {
4391 return;
4392 }
4393
4394 is_audio_listener_2d_enabled = p_enable;
4395 _update_audio_listener_2d();
4396 }
4397
4398 bool Viewport::is_audio_listener_2d() const {
4399 ERR_READ_THREAD_GUARD_V(false);
4400 return is_audio_listener_2d_enabled;
4401 }
4402
4403 Camera2D *Viewport::get_camera_2d() const {
4404 ERR_READ_THREAD_GUARD_V(nullptr);
4405 return camera_2d;
4406 }
4407
4408 void Viewport::assign_next_enabled_camera_2d(const StringName &p_camera_group) {
4409 ERR_MAIN_THREAD_GUARD;
4410 Vector<Node *> camera_list = get_tree()->get_nodes_in_group(p_camera_group);
4411
4412 Camera2D *new_camera = nullptr;
4413 for (Node *E : camera_list) {
4414 Camera2D *cam = Object::cast_to<Camera2D>(E);
4415 if (!cam) {
4416 continue; // Non-camera node (e.g. ParallaxBackground).
4417 }
4418
4419 if (cam->is_enabled()) {
4420 new_camera = cam;
4421 break;
4422 }
4423 }
4424
4425 _camera_2d_set(new_camera);
4426 if (!camera_2d) {
4427 set_canvas_transform(Transform2D());
4428 }
4429 }
4430
4431 #if DEBUG_ENABLED
4432 void Viewport::enable_camera_2d_override(bool p_enable) {
4433 ERR_MAIN_THREAD_GUARD;
4434
4435 if (p_enable) {
4436 camera_2d_override.enable(this, camera_2d);
4437 } else {
4438 camera_2d_override.disable(camera_2d);
4439 }
4440 }
4441
4442 bool Viewport::is_camera_2d_override_enabled() const {
4443 ERR_READ_THREAD_GUARD_V(false);
4444 return camera_2d_override.is_enabled();
4445 }
4446
4447 Camera2D *Viewport::get_overridden_camera_2d() const {
4448 ERR_READ_THREAD_GUARD_V(nullptr);
4449 ERR_FAIL_COND_V(!camera_2d_override.is_enabled(), nullptr);
4450 return camera_2d_override.get_overridden_camera();
4451 }
4452
4453 Camera2D *Viewport::get_override_camera_2d() const {
4454 ERR_READ_THREAD_GUARD_V(nullptr);
4455 ERR_FAIL_COND_V(!camera_2d_override.is_enabled(), nullptr);
4456 return camera_2d_override.is_enabled() ? get_camera_2d() : nullptr;
4457 }
4458 #endif // DEBUG_ENABLED
4459
4460 #ifndef _3D_DISABLED
4461 AudioListener3D *Viewport::get_audio_listener_3d() const {
4462 ERR_READ_THREAD_GUARD_V(nullptr);
4463 return audio_listener_3d;
4464 }
4465
4466 void Viewport::set_as_audio_listener_3d(bool p_enable) {
4467 ERR_MAIN_THREAD_GUARD;
4468 if (p_enable == is_audio_listener_3d_enabled) {
4469 return;
4470 }
4471
4472 is_audio_listener_3d_enabled = p_enable;
4473 _update_audio_listener_3d();
4474 }
4475
4476 bool Viewport::is_audio_listener_3d() const {
4477 ERR_READ_THREAD_GUARD_V(false);
4478 return is_audio_listener_3d_enabled;
4479 }
4480
4481 void Viewport::_update_audio_listener_3d() {
4482 if (AudioServer::get_singleton()) {
4483 AudioServer::get_singleton()->notify_listener_changed();
4484 }
4485 }
4486
4487 void Viewport::_listener_transform_3d_changed_notify() {
4488 }
4489
4490 void Viewport::_audio_listener_3d_set(AudioListener3D *p_listener) {
4491 if (audio_listener_3d == p_listener) {
4492 return;
4493 }
4494
4495 audio_listener_3d = p_listener;
4496
4497 _update_audio_listener_3d();
4498 _listener_transform_3d_changed_notify();
4499 }
4500
4501 bool Viewport::_audio_listener_3d_add(AudioListener3D *p_listener) {
4502 audio_listener_3d_set.insert(p_listener);
4503 return audio_listener_3d_set.size() == 1;
4504 }
4505
4506 void Viewport::_audio_listener_3d_remove(AudioListener3D *p_listener) {
4507 audio_listener_3d_set.erase(p_listener);
4508 if (audio_listener_3d == p_listener) {
4509 audio_listener_3d = nullptr;
4510 }
4511 }
4512
4513 void Viewport::_audio_listener_3d_make_next_current(AudioListener3D *p_exclude) {
4514 if (audio_listener_3d_set.size() > 0) {
4515 for (AudioListener3D *E : audio_listener_3d_set) {
4516 if (p_exclude == E) {
4517 continue;
4518 }
4519 if (!E->is_inside_tree()) {
4520 continue;
4521 }
4522 if (audio_listener_3d != nullptr) {
4523 return;
4524 }
4525
4526 E->make_current();
4527 }
4528 } else {
4529 // Attempt to reset listener to the camera position.
4530 if (camera_3d != nullptr) {
4531 _update_audio_listener_3d();
4532 _camera_3d_transform_changed_notify();
4533 }
4534 }
4535 }
4536
4537 #ifndef PHYSICS_3D_DISABLED
4538 void Viewport::_collision_object_3d_input_event(CollisionObject3D *p_object, Camera3D *p_camera, const Ref<InputEvent> &p_input_event, const Vector3 &p_pos, const Vector3 &p_normal, int p_shape) {
4539 Transform3D object_transform = p_object->get_global_transform();
4540 Transform3D camera_transform = p_camera->get_global_transform();
4541 ObjectID id = p_object->get_instance_id();
4542
4543 // Avoid sending the fake event unnecessarily if nothing really changed in the context.
4544 if (object_transform == physics_last_object_transform && camera_transform == physics_last_camera_transform && physics_last_id == id) {
4545 Ref<InputEventMouseMotion> mm = p_input_event;
4546 if (mm.is_valid() && mm->get_device() == InputEvent::DEVICE_ID_INTERNAL) {
4547 return; // Discarded.
4548 }
4549 }
4550 p_object->_input_event_call(camera_3d, p_input_event, p_pos, p_normal, p_shape);
4551 physics_last_object_transform = object_transform;
4552 physics_last_camera_transform = camera_transform;
4553 physics_last_id = id;
4554 }
4555 #endif // PHYSICS_3D_DISABLED
4556
4557 Camera3D *Viewport::get_camera_3d() const {
4558 ERR_READ_THREAD_GUARD_V(nullptr);
4559 return camera_3d;
4560 }
4561
4562 void Viewport::_camera_3d_transform_changed_notify() {
4563 }
4564
4565 void Viewport::_camera_3d_set(Camera3D *p_camera) {
4566 if (camera_3d == p_camera) {
4567 return;
4568 }
4569
4570 #if DEBUG_ENABLED
4571 if (is_camera_3d_override_enabled()) {
4572 camera_3d_override.set_overridden_camera(p_camera);
4573 return;
4574 }
4575 #endif // DEBUG_ENABLED
4576
4577 if (camera_3d) {
4578 camera_3d->notification(Camera3D::NOTIFICATION_LOST_CURRENT);
4579 }
4580
4581 camera_3d = p_camera;
4582
4583 if (camera_3d) {
4584 RenderingServer::get_singleton()->viewport_attach_camera(viewport, camera_3d->get_camera());
4585 } else {
4586 RenderingServer::get_singleton()->viewport_attach_camera(viewport, RID());
4587 }
4588
4589 if (camera_3d) {
4590 camera_3d->notification(Camera3D::NOTIFICATION_BECAME_CURRENT);
4591 }
4592
4593 _update_audio_listener_3d();
4594 _camera_3d_transform_changed_notify();
4595 }
4596
4597 bool Viewport::_camera_3d_add(Camera3D *p_camera) {
4598 camera_3d_set.insert(p_camera);
4599 return camera_3d_set.size() == 1;
4600 }
4601
4602 void Viewport::_camera_3d_remove(Camera3D *p_camera) {
4603 camera_3d_set.erase(p_camera);
4604 if (camera_3d == p_camera) {
4605 _camera_3d_set(nullptr);
4606 }
4607 }
4608
4609 void Viewport::_camera_3d_make_next_current(Camera3D *p_exclude) {
4610 for (Camera3D *E : camera_3d_set) {
4611 if (p_exclude == E) {
4612 continue;
4613 }
4614 if (!E->is_inside_tree()) {
4615 continue;
4616 }
4617 if (camera_3d != nullptr) {
4618 return;
4619 }
4620
4621 E->make_current();
4622 }
4623 }
4624
4625 #if DEBUG_ENABLED
4626 void Viewport::enable_camera_3d_override(bool p_enable) {
4627 ERR_MAIN_THREAD_GUARD;
4628
4629 if (p_enable) {
4630 camera_3d_override.enable(this, camera_3d);
4631 } else {
4632 camera_3d_override.disable(camera_3d);
4633 }
4634 }
4635
4636 bool Viewport::is_camera_3d_override_enabled() const {
4637 ERR_READ_THREAD_GUARD_V(false);
4638 return camera_3d_override.is_enabled();
4639 }
4640
4641 Camera3D *Viewport::get_overridden_camera_3d() const {
4642 ERR_READ_THREAD_GUARD_V(nullptr);
4643 ERR_FAIL_COND_V(!camera_3d_override.is_enabled(), nullptr);
4644 return camera_3d_override.get_overridden_camera();
4645 }
4646
4647 Camera3D *Viewport::get_override_camera_3d() const {
4648 ERR_READ_THREAD_GUARD_V(nullptr);
4649 ERR_FAIL_COND_V(!camera_3d_override.is_enabled(), nullptr);
4650 return get_camera_3d();
4651 }
4652 #endif //DEBUG_ENABLED
4653
4654 void Viewport::set_disable_3d(bool p_disable) {
4655 ERR_MAIN_THREAD_GUARD;
4656 disable_3d = p_disable;
4657 RenderingServer::get_singleton()->viewport_set_disable_3d(viewport, disable_3d);
4658 }
4659
4660 bool Viewport::is_3d_disabled() const {
4661 ERR_READ_THREAD_GUARD_V(false);
4662 return disable_3d;
4663 }
4664
4665 Ref<World3D> Viewport::get_world_3d() const {
4666 ERR_READ_THREAD_GUARD_V(Ref<World3D>());
4667 return world_3d;
4668 }
4669
4670 Ref<World3D> Viewport::find_world_3d() const {
4671 ERR_READ_THREAD_GUARD_V(Ref<World3D>());
4672 if (own_world_3d.is_valid()) {
4673 return own_world_3d;
4674 } else if (world_3d.is_valid()) {
4675 return world_3d;
4676 } else if (parent) {
4677 return parent->find_world_3d();
4678 } else {
4679 return Ref<World3D>();
4680 }
4681 }
4682
4683 void Viewport::set_world_3d(const Ref<World3D> &p_world_3d) {
4684 ERR_MAIN_THREAD_GUARD;
4685 if (world_3d == p_world_3d) {
4686 return;
4687 }
4688
4689 if (is_inside_tree()) {
4690 _propagate_exit_world_3d(this);
4691 }
4692
4693 if (own_world_3d.is_valid() && world_3d.is_valid()) {
4694 world_3d->disconnect_changed(callable_mp(this, &Viewport::_own_world_3d_changed));
4695 }
4696
4697 world_3d = p_world_3d;
4698
4699 if (own_world_3d.is_valid()) {
4700 if (world_3d.is_valid()) {
4701 own_world_3d = world_3d->duplicate();
4702 world_3d->connect_changed(callable_mp(this, &Viewport::_own_world_3d_changed));
4703 } else {
4704 own_world_3d.instantiate();
4705 }
4706 }
4707
4708 if (is_inside_tree()) {
4709 _propagate_enter_world_3d(this);
4710 }
4711
4712 if (is_inside_tree()) {
4713 RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario());
4714 }
4715
4716 _update_audio_listener_3d();
4717 }
4718
4719 void Viewport::_own_world_3d_changed() {
4720 ERR_FAIL_COND(world_3d.is_null());
4721 ERR_FAIL_COND(own_world_3d.is_null());
4722
4723 if (is_inside_tree()) {
4724 _propagate_exit_world_3d(this);
4725 }
4726
4727 own_world_3d = world_3d->duplicate();
4728
4729 if (is_inside_tree()) {
4730 _propagate_enter_world_3d(this);
4731 }
4732
4733 if (is_inside_tree()) {
4734 RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario());
4735 }
4736
4737 _update_audio_listener_3d();
4738 }
4739
4740 void Viewport::set_use_own_world_3d(bool p_use_own_world_3d) {
4741 ERR_MAIN_THREAD_GUARD;
4742 if (p_use_own_world_3d == own_world_3d.is_valid()) {
4743 return;
4744 }
4745
4746 if (is_inside_tree()) {
4747 _propagate_exit_world_3d(this);
4748 }
4749
4750 if (p_use_own_world_3d) {
4751 if (world_3d.is_valid()) {
4752 own_world_3d = world_3d->duplicate();
4753 world_3d->connect_changed(callable_mp(this, &Viewport::_own_world_3d_changed));
4754 } else {
4755 own_world_3d.instantiate();
4756 }
4757 } else {
4758 own_world_3d = Ref<World3D>();
4759 if (world_3d.is_valid()) {
4760 world_3d->disconnect_changed(callable_mp(this, &Viewport::_own_world_3d_changed));
4761 }
4762 }
4763
4764 if (is_inside_tree()) {
4765 _propagate_enter_world_3d(this);
4766 }
4767
4768 if (is_inside_tree()) {
4769 RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario());
4770 }
4771
4772 _update_audio_listener_3d();
4773 }
4774
4775 bool Viewport::is_using_own_world_3d() const {
4776 ERR_READ_THREAD_GUARD_V(false);
4777 return own_world_3d.is_valid();
4778 }
4779
4780 void Viewport::_propagate_enter_world_3d(Node *p_node) {
4781 if (p_node != this) {
4782 if (!p_node->is_inside_tree()) { //may not have entered scene yet
4783 return;
4784 }
4785
4786 if (Object::cast_to<Node3D>(p_node) || Object::cast_to<WorldEnvironment>(p_node)) {
4787 p_node->notification(Node3D::NOTIFICATION_ENTER_WORLD);
4788 } else {
4789 Viewport *v = Object::cast_to<Viewport>(p_node);
4790 if (v) {
4791 if (v->world_3d.is_valid() || v->own_world_3d.is_valid()) {
4792 return;
4793 }
4794 }
4795 }
4796 }
4797
4798 for (int i = 0; i < p_node->get_child_count(); i++) {
4799 _propagate_enter_world_3d(p_node->get_child(i));
4800 }
4801 }
4802
4803 void Viewport::_propagate_exit_world_3d(Node *p_node) {
4804 if (p_node != this) {
4805 if (!p_node->is_inside_tree()) { //may have exited scene already
4806 return;
4807 }
4808
4809 if (Object::cast_to<Node3D>(p_node) || Object::cast_to<WorldEnvironment>(p_node)) {
4810 p_node->notification(Node3D::NOTIFICATION_EXIT_WORLD);
4811 } else {
4812 Viewport *v = Object::cast_to<Viewport>(p_node);
4813 if (v) {
4814 if (v->world_3d.is_valid() || v->own_world_3d.is_valid()) {
4815 return;
4816 }
4817 }
4818 }
4819 }
4820
4821 for (int i = 0; i < p_node->get_child_count(); i++) {
4822 _propagate_exit_world_3d(p_node->get_child(i));
4823 }
4824 }
4825
4826 #ifndef XR_DISABLED
4827 void Viewport::set_use_xr(bool p_use_xr) {
4828 ERR_MAIN_THREAD_GUARD;
4829 if (use_xr != p_use_xr) {
4830 use_xr = p_use_xr;
4831
4832 RS::get_singleton()->viewport_set_use_xr(viewport, use_xr);
4833
4834 if (!use_xr) {
4835 // Set viewport to previous size when exiting XR.
4836 if (size_allocated) {
4837 RS::get_singleton()->viewport_set_size(viewport, size.width, size.height);
4838 } else {
4839 RS::get_singleton()->viewport_set_size(viewport, 0, 0);
4840 }
4841
4842 // Reset render target override textures.
4843 RID rt = RS::get_singleton()->viewport_get_render_target(viewport);
4844 RSG::texture_storage->render_target_set_override(rt, RID(), RID(), RID(), RID());
4845 }
4846 }
4847 }
4848
4849 bool Viewport::is_using_xr() {
4850 ERR_READ_THREAD_GUARD_V(false);
4851 return use_xr;
4852 }
4853 #endif // XR_DISABLED
4854
4855 void Viewport::set_scaling_3d_mode(Scaling3DMode p_scaling_3d_mode) {
4856 ERR_MAIN_THREAD_GUARD;
4857 if (scaling_3d_mode == p_scaling_3d_mode) {
4858 return;
4859 }
4860
4861 scaling_3d_mode = p_scaling_3d_mode;
4862 RS::get_singleton()->viewport_set_scaling_3d_mode(viewport, (RS::ViewportScaling3DMode)(int)p_scaling_3d_mode);
4863 }
4864
4865 Viewport::Scaling3DMode Viewport::get_scaling_3d_mode() const {
4866 ERR_READ_THREAD_GUARD_V(SCALING_3D_MODE_BILINEAR);
4867 return scaling_3d_mode;
4868 }
4869
4870 void Viewport::set_scaling_3d_scale(float p_scaling_3d_scale) {
4871 ERR_MAIN_THREAD_GUARD;
4872 // Clamp to reasonable values that are actually useful.
4873 // Values above 2.0 don't serve a practical purpose since the viewport
4874 // isn't displayed with mipmaps.
4875 scaling_3d_scale = CLAMP(p_scaling_3d_scale, 0.1, 2.0);
4876
4877 RS::get_singleton()->viewport_set_scaling_3d_scale(viewport, scaling_3d_scale);
4878 }
4879
4880 float Viewport::get_scaling_3d_scale() const {
4881 ERR_READ_THREAD_GUARD_V(0);
4882 return scaling_3d_scale;
4883 }
4884
4885 void Viewport::set_fsr_sharpness(float p_fsr_sharpness) {
4886 ERR_MAIN_THREAD_GUARD;
4887 if (fsr_sharpness == p_fsr_sharpness) {
4888 return;
4889 }
4890
4891 if (p_fsr_sharpness < 0.0f) {
4892 p_fsr_sharpness = 0.0f;
4893 }
4894
4895 fsr_sharpness = p_fsr_sharpness;
4896 RS::get_singleton()->viewport_set_fsr_sharpness(viewport, p_fsr_sharpness);
4897 }
4898
4899 float Viewport::get_fsr_sharpness() const {
4900 ERR_READ_THREAD_GUARD_V(0);
4901 return fsr_sharpness;
4902 }
4903
4904 void Viewport::set_texture_mipmap_bias(float p_texture_mipmap_bias) {
4905 ERR_MAIN_THREAD_GUARD;
4906 if (texture_mipmap_bias == p_texture_mipmap_bias) {
4907 return;
4908 }
4909
4910 texture_mipmap_bias = p_texture_mipmap_bias;
4911 RS::get_singleton()->viewport_set_texture_mipmap_bias(viewport, p_texture_mipmap_bias);
4912 }
4913
4914 float Viewport::get_texture_mipmap_bias() const {
4915 ERR_READ_THREAD_GUARD_V(0);
4916 return texture_mipmap_bias;
4917 }
4918
4919 void Viewport::set_anisotropic_filtering_level(AnisotropicFiltering p_anisotropic_filtering_level) {
4920 ERR_MAIN_THREAD_GUARD;
4921 if (anisotropic_filtering_level == p_anisotropic_filtering_level) {
4922 return;
4923 }
4924
4925 anisotropic_filtering_level = p_anisotropic_filtering_level;
4926 RS::get_singleton()->viewport_set_anisotropic_filtering_level(viewport, (RS::ViewportAnisotropicFiltering)(int)p_anisotropic_filtering_level);
4927 }
4928
4929 Viewport::AnisotropicFiltering Viewport::get_anisotropic_filtering_level() const {
4930 ERR_READ_THREAD_GUARD_V(ANISOTROPY_DISABLED);
4931 return anisotropic_filtering_level;
4932 }
4933
4934 #endif // _3D_DISABLED
4935
4936 void Viewport::_propagate_world_2d_changed(Node *p_node) {
4937 if (p_node != this) {
4938 if (Object::cast_to<CanvasItem>(p_node)) {
4939 p_node->notification(CanvasItem::NOTIFICATION_WORLD_2D_CHANGED);
4940 } else {
4941 Viewport *v = Object::cast_to<Viewport>(p_node);
4942 if (v) {
4943 if (v->world_2d.is_valid()) {
4944 return;
4945 }
4946 }
4947 }
4948 }
4949
4950 for (int i = 0; i < p_node->get_child_count(); ++i) {
4951 _propagate_world_2d_changed(p_node->get_child(i));
4952 }
4953 }
4954
4955 void Viewport::_bind_methods() {
4956 ClassDB::bind_method(D_METHOD("set_world_2d", "world_2d"), &Viewport::set_world_2d);
4957 ClassDB::bind_method(D_METHOD("get_world_2d"), &Viewport::get_world_2d);
4958 ClassDB::bind_method(D_METHOD("find_world_2d"), &Viewport::find_world_2d);
4959
4960 ClassDB::bind_method(D_METHOD("set_canvas_transform", "xform"), &Viewport::set_canvas_transform);
4961 ClassDB::bind_method(D_METHOD("get_canvas_transform"), &Viewport::get_canvas_transform);
4962
4963 ClassDB::bind_method(D_METHOD("set_global_canvas_transform", "xform"), &Viewport::set_global_canvas_transform);
4964 ClassDB::bind_method(D_METHOD("get_global_canvas_transform"), &Viewport::get_global_canvas_transform);
4965 ClassDB::bind_method(D_METHOD("get_stretch_transform"), &Viewport::get_stretch_transform);
4966 ClassDB::bind_method(D_METHOD("get_final_transform"), &Viewport::get_final_transform);
4967 ClassDB::bind_method(D_METHOD("get_screen_transform"), &Viewport::get_screen_transform);
4968
4969 ClassDB::bind_method(D_METHOD("get_visible_rect"), &Viewport::get_visible_rect);
4970 ClassDB::bind_method(D_METHOD("set_transparent_background", "enable"), &Viewport::set_transparent_background);
4971 ClassDB::bind_method(D_METHOD("has_transparent_background"), &Viewport::has_transparent_background);
4972 ClassDB::bind_method(D_METHOD("set_use_hdr_2d", "enable"), &Viewport::set_use_hdr_2d);
4973 ClassDB::bind_method(D_METHOD("is_using_hdr_2d"), &Viewport::is_using_hdr_2d);
4974
4975 ClassDB::bind_method(D_METHOD("set_msaa_2d", "msaa"), &Viewport::set_msaa_2d);
4976 ClassDB::bind_method(D_METHOD("get_msaa_2d"), &Viewport::get_msaa_2d);
4977
4978 ClassDB::bind_method(D_METHOD("set_msaa_3d", "msaa"), &Viewport::set_msaa_3d);
4979 ClassDB::bind_method(D_METHOD("get_msaa_3d"), &Viewport::get_msaa_3d);
4980
4981 ClassDB::bind_method(D_METHOD("set_screen_space_aa", "screen_space_aa"), &Viewport::set_screen_space_aa);
4982 ClassDB::bind_method(D_METHOD("get_screen_space_aa"), &Viewport::get_screen_space_aa);
4983
4984 ClassDB::bind_method(D_METHOD("set_use_taa", "enable"), &Viewport::set_use_taa);
4985 ClassDB::bind_method(D_METHOD("is_using_taa"), &Viewport::is_using_taa);
4986
4987 ClassDB::bind_method(D_METHOD("set_use_debanding", "enable"), &Viewport::set_use_debanding);
4988 ClassDB::bind_method(D_METHOD("is_using_debanding"), &Viewport::is_using_debanding);
4989
4990 ClassDB::bind_method(D_METHOD("set_use_occlusion_culling", "enable"), &Viewport::set_use_occlusion_culling);
4991 ClassDB::bind_method(D_METHOD("is_using_occlusion_culling"), &Viewport::is_using_occlusion_culling);
4992
4993 ClassDB::bind_method(D_METHOD("set_debug_draw", "debug_draw"), &Viewport::set_debug_draw);
4994 ClassDB::bind_method(D_METHOD("get_debug_draw"), &Viewport::get_debug_draw);
4995
4996 ClassDB::bind_method(D_METHOD("set_use_oversampling", "enable"), &Viewport::set_use_oversampling);
4997 ClassDB::bind_method(D_METHOD("is_using_oversampling"), &Viewport::is_using_oversampling);
4998
4999 ClassDB::bind_method(D_METHOD("set_oversampling_override", "oversampling"), &Viewport::set_oversampling_override);
5000 ClassDB::bind_method(D_METHOD("get_oversampling_override"), &Viewport::get_oversampling_override);
5001
5002 ClassDB::bind_method(D_METHOD("get_oversampling"), &Viewport::get_oversampling);
5003
5004 ClassDB::bind_method(D_METHOD("get_render_info", "type", "info"), &Viewport::get_render_info);
5005
5006 ClassDB::bind_method(D_METHOD("get_texture"), &Viewport::get_texture);
5007
5008 #if !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
5009 ClassDB::bind_method(D_METHOD("set_physics_object_picking", "enable"), &Viewport::set_physics_object_picking);
5010 ClassDB::bind_method(D_METHOD("get_physics_object_picking"), &Viewport::get_physics_object_picking);
5011 ClassDB::bind_method(D_METHOD("set_physics_object_picking_sort", "enable"), &Viewport::set_physics_object_picking_sort);
5012 ClassDB::bind_method(D_METHOD("get_physics_object_picking_sort"), &Viewport::get_physics_object_picking_sort);
5013 ClassDB::bind_method(D_METHOD("set_physics_object_picking_first_only", "enable"), &Viewport::set_physics_object_picking_first_only);
5014 ClassDB::bind_method(D_METHOD("get_physics_object_picking_first_only"), &Viewport::get_physics_object_picking_first_only);
5015 #endif // !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
5016
5017 ClassDB::bind_method(D_METHOD("get_viewport_rid"), &Viewport::get_viewport_rid);
5018 ClassDB::bind_method(D_METHOD("push_text_input", "text"), &Viewport::push_text_input);
5019 ClassDB::bind_method(D_METHOD("push_input", "event", "in_local_coords"), &Viewport::push_input, DEFVAL(false));
5020 #ifndef DISABLE_DEPRECATED
5021 ClassDB::bind_method(D_METHOD("push_unhandled_input", "event", "in_local_coords"), &Viewport::push_unhandled_input, DEFVAL(false));
5022 #endif // DISABLE_DEPRECATED
5023 ClassDB::bind_method(D_METHOD("notify_mouse_entered"), &Viewport::notify_mouse_entered);
5024 ClassDB::bind_method(D_METHOD("notify_mouse_exited"), &Viewport::notify_mouse_exited);
5025
5026 ClassDB::bind_method(D_METHOD("get_mouse_position"), &Viewport::get_mouse_position);
5027 ClassDB::bind_method(D_METHOD("warp_mouse", "position"), &Viewport::warp_mouse);
5028 ClassDB::bind_method(D_METHOD("update_mouse_cursor_state"), &Viewport::update_mouse_cursor_state);
5029
5030 ClassDB::bind_method(D_METHOD("gui_cancel_drag"), &Viewport::gui_cancel_drag);
5031 ClassDB::bind_method(D_METHOD("gui_get_drag_data"), &Viewport::gui_get_drag_data);
5032 ClassDB::bind_method(D_METHOD("gui_get_drag_description"), &Viewport::gui_get_drag_description);
5033 ClassDB::bind_method(D_METHOD("gui_set_drag_description", "description"), &Viewport::gui_set_drag_description);
5034 ClassDB::bind_method(D_METHOD("gui_is_dragging"), &Viewport::gui_is_dragging);
5035 ClassDB::bind_method(D_METHOD("gui_is_drag_successful"), &Viewport::gui_is_drag_successful);
5036
5037 ClassDB::bind_method(D_METHOD("gui_release_focus"), &Viewport::gui_release_focus);
5038 ClassDB::bind_method(D_METHOD("gui_get_focus_owner"), &Viewport::gui_get_focus_owner);
5039 ClassDB::bind_method(D_METHOD("gui_get_hovered_control"), &Viewport::gui_get_hovered_control);
5040
5041 ClassDB::bind_method(D_METHOD("set_disable_input", "disable"), &Viewport::set_disable_input);
5042 ClassDB::bind_method(D_METHOD("is_input_disabled"), &Viewport::is_input_disabled);
5043
5044 ClassDB::bind_method(D_METHOD("_gui_remove_focus_for_window"), &Viewport::_gui_remove_focus_for_window);
5045
5046 ClassDB::bind_method(D_METHOD("set_positional_shadow_atlas_size", "size"), &Viewport::set_positional_shadow_atlas_size);
5047 ClassDB::bind_method(D_METHOD("get_positional_shadow_atlas_size"), &Viewport::get_positional_shadow_atlas_size);
5048
5049 ClassDB::bind_method(D_METHOD("set_positional_shadow_atlas_16_bits", "enable"), &Viewport::set_positional_shadow_atlas_16_bits);
5050 ClassDB::bind_method(D_METHOD("get_positional_shadow_atlas_16_bits"), &Viewport::get_positional_shadow_atlas_16_bits);
5051
5052 ClassDB::bind_method(D_METHOD("set_snap_controls_to_pixels", "enabled"), &Viewport::set_snap_controls_to_pixels);
5053 ClassDB::bind_method(D_METHOD("is_snap_controls_to_pixels_enabled"), &Viewport::is_snap_controls_to_pixels_enabled);
5054
5055 ClassDB::bind_method(D_METHOD("set_snap_2d_transforms_to_pixel", "enabled"), &Viewport::set_snap_2d_transforms_to_pixel);
5056 ClassDB::bind_method(D_METHOD("is_snap_2d_transforms_to_pixel_enabled"), &Viewport::is_snap_2d_transforms_to_pixel_enabled);
5057
5058 ClassDB::bind_method(D_METHOD("set_snap_2d_vertices_to_pixel", "enabled"), &Viewport::set_snap_2d_vertices_to_pixel);
5059 ClassDB::bind_method(D_METHOD("is_snap_2d_vertices_to_pixel_enabled"), &Viewport::is_snap_2d_vertices_to_pixel_enabled);
5060
5061 ClassDB::bind_method(D_METHOD("set_positional_shadow_atlas_quadrant_subdiv", "quadrant", "subdiv"), &Viewport::set_positional_shadow_atlas_quadrant_subdiv);
5062 ClassDB::bind_method(D_METHOD("get_positional_shadow_atlas_quadrant_subdiv", "quadrant"), &Viewport::get_positional_shadow_atlas_quadrant_subdiv);
5063
5064 ClassDB::bind_method(D_METHOD("set_input_as_handled"), &Viewport::set_input_as_handled);
5065 ClassDB::bind_method(D_METHOD("is_input_handled"), &Viewport::is_input_handled);
5066
5067 ClassDB::bind_method(D_METHOD("set_handle_input_locally", "enable"), &Viewport::set_handle_input_locally);
5068 ClassDB::bind_method(D_METHOD("is_handling_input_locally"), &Viewport::is_handling_input_locally);
5069
5070 ClassDB::bind_method(D_METHOD("set_default_canvas_item_texture_filter", "mode"), &Viewport::set_default_canvas_item_texture_filter);
5071 ClassDB::bind_method(D_METHOD("get_default_canvas_item_texture_filter"), &Viewport::get_default_canvas_item_texture_filter);
5072
5073 ClassDB::bind_method(D_METHOD("set_embedding_subwindows", "enable"), &Viewport::set_embedding_subwindows);
5074 ClassDB::bind_method(D_METHOD("is_embedding_subwindows"), &Viewport::is_embedding_subwindows);
5075 ClassDB::bind_method(D_METHOD("get_embedded_subwindows"), &Viewport::get_embedded_subwindows);
5076
5077 ClassDB::bind_method(D_METHOD("set_drag_threshold", "threshold"), &Viewport::set_drag_threshold);
5078 ClassDB::bind_method(D_METHOD("get_drag_threshold"), &Viewport::get_drag_threshold);
5079
5080 ClassDB::bind_method(D_METHOD("set_canvas_cull_mask", "mask"), &Viewport::set_canvas_cull_mask);
5081 ClassDB::bind_method(D_METHOD("get_canvas_cull_mask"), &Viewport::get_canvas_cull_mask);
5082
5083 ClassDB::bind_method(D_METHOD("set_canvas_cull_mask_bit", "layer", "enable"), &Viewport::set_canvas_cull_mask_bit);
5084 ClassDB::bind_method(D_METHOD("get_canvas_cull_mask_bit", "layer"), &Viewport::get_canvas_cull_mask_bit);
5085
5086 ClassDB::bind_method(D_METHOD("set_default_canvas_item_texture_repeat", "mode"), &Viewport::set_default_canvas_item_texture_repeat);
5087 ClassDB::bind_method(D_METHOD("get_default_canvas_item_texture_repeat"), &Viewport::get_default_canvas_item_texture_repeat);
5088
5089 ClassDB::bind_method(D_METHOD("set_sdf_oversize", "oversize"), &Viewport::set_sdf_oversize);
5090 ClassDB::bind_method(D_METHOD("get_sdf_oversize"), &Viewport::get_sdf_oversize);
5091
5092 ClassDB::bind_method(D_METHOD("set_sdf_scale", "scale"), &Viewport::set_sdf_scale);
5093 ClassDB::bind_method(D_METHOD("get_sdf_scale"), &Viewport::get_sdf_scale);
5094
5095 ClassDB::bind_method(D_METHOD("set_mesh_lod_threshold", "pixels"), &Viewport::set_mesh_lod_threshold);
5096 ClassDB::bind_method(D_METHOD("get_mesh_lod_threshold"), &Viewport::get_mesh_lod_threshold);
5097
5098 #if !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
5099 ClassDB::bind_method(D_METHOD("_process_picking"), &Viewport::_process_picking);
5100 #endif // !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
5101
5102 ClassDB::bind_method(D_METHOD("set_as_audio_listener_2d", "enable"), &Viewport::set_as_audio_listener_2d);
5103 ClassDB::bind_method(D_METHOD("is_audio_listener_2d"), &Viewport::is_audio_listener_2d);
5104 ClassDB::bind_method(D_METHOD("get_audio_listener_2d"), &Viewport::get_audio_listener_2d);
5105 ClassDB::bind_method(D_METHOD("get_camera_2d"), &Viewport::get_camera_2d);
5106
5107 #ifndef _3D_DISABLED
5108 ClassDB::bind_method(D_METHOD("set_world_3d", "world_3d"), &Viewport::set_world_3d);
5109 ClassDB::bind_method(D_METHOD("get_world_3d"), &Viewport::get_world_3d);
5110 ClassDB::bind_method(D_METHOD("find_world_3d"), &Viewport::find_world_3d);
5111
5112 ClassDB::bind_method(D_METHOD("set_use_own_world_3d", "enable"), &Viewport::set_use_own_world_3d);
5113 ClassDB::bind_method(D_METHOD("is_using_own_world_3d"), &Viewport::is_using_own_world_3d);
5114
5115 ClassDB::bind_method(D_METHOD("get_audio_listener_3d"), &Viewport::get_audio_listener_3d);
5116 ClassDB::bind_method(D_METHOD("get_camera_3d"), &Viewport::get_camera_3d);
5117 ClassDB::bind_method(D_METHOD("set_as_audio_listener_3d", "enable"), &Viewport::set_as_audio_listener_3d);
5118 ClassDB::bind_method(D_METHOD("is_audio_listener_3d"), &Viewport::is_audio_listener_3d);
5119
5120 ClassDB::bind_method(D_METHOD("set_disable_3d", "disable"), &Viewport::set_disable_3d);
5121 ClassDB::bind_method(D_METHOD("is_3d_disabled"), &Viewport::is_3d_disabled);
5122
5123 #ifndef XR_DISABLED
5124 ClassDB::bind_method(D_METHOD("set_use_xr", "use"), &Viewport::set_use_xr);
5125 ClassDB::bind_method(D_METHOD("is_using_xr"), &Viewport::is_using_xr);
5126 #endif // XR_DISABLED
5127
5128 ClassDB::bind_method(D_METHOD("set_scaling_3d_mode", "scaling_3d_mode"), &Viewport::set_scaling_3d_mode);
5129 ClassDB::bind_method(D_METHOD("get_scaling_3d_mode"), &Viewport::get_scaling_3d_mode);
5130
5131 ClassDB::bind_method(D_METHOD("set_scaling_3d_scale", "scale"), &Viewport::set_scaling_3d_scale);
5132 ClassDB::bind_method(D_METHOD("get_scaling_3d_scale"), &Viewport::get_scaling_3d_scale);
5133
5134 ClassDB::bind_method(D_METHOD("set_fsr_sharpness", "fsr_sharpness"), &Viewport::set_fsr_sharpness);
5135 ClassDB::bind_method(D_METHOD("get_fsr_sharpness"), &Viewport::get_fsr_sharpness);
5136
5137 ClassDB::bind_method(D_METHOD("set_texture_mipmap_bias", "texture_mipmap_bias"), &Viewport::set_texture_mipmap_bias);
5138 ClassDB::bind_method(D_METHOD("get_texture_mipmap_bias"), &Viewport::get_texture_mipmap_bias);
5139
5140 ClassDB::bind_method(D_METHOD("set_anisotropic_filtering_level", "anisotropic_filtering_level"), &Viewport::set_anisotropic_filtering_level);
5141 ClassDB::bind_method(D_METHOD("get_anisotropic_filtering_level"), &Viewport::get_anisotropic_filtering_level);
5142
5143 ClassDB::bind_method(D_METHOD("set_vrs_mode", "mode"), &Viewport::set_vrs_mode);
5144 ClassDB::bind_method(D_METHOD("get_vrs_mode"), &Viewport::get_vrs_mode);
5145
5146 ClassDB::bind_method(D_METHOD("set_vrs_update_mode", "mode"), &Viewport::set_vrs_update_mode);
5147 ClassDB::bind_method(D_METHOD("get_vrs_update_mode"), &Viewport::get_vrs_update_mode);
5148
5149 ClassDB::bind_method(D_METHOD("set_vrs_texture", "texture"), &Viewport::set_vrs_texture);
5150 ClassDB::bind_method(D_METHOD("get_vrs_texture"), &Viewport::get_vrs_texture);
5151
5152 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_3d"), "set_disable_3d", "is_3d_disabled");
5153 #ifndef XR_DISABLED
5154 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_xr"), "set_use_xr", "is_using_xr");
5155 #endif // XR_DISABLED
5156 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "own_world_3d"), "set_use_own_world_3d", "is_using_own_world_3d");
5157 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_3d", PROPERTY_HINT_RESOURCE_TYPE, "World3D"), "set_world_3d", "get_world_3d");
5158 #endif // _3D_DISABLED
5159 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_2d", PROPERTY_HINT_RESOURCE_TYPE, "World2D", PROPERTY_USAGE_NONE), "set_world_2d", "get_world_2d");
5160 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent_bg"), "set_transparent_background", "has_transparent_background");
5161 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "handle_input_locally"), "set_handle_input_locally", "is_handling_input_locally");
5162 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snap_2d_transforms_to_pixel"), "set_snap_2d_transforms_to_pixel", "is_snap_2d_transforms_to_pixel_enabled");
5163 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snap_2d_vertices_to_pixel"), "set_snap_2d_vertices_to_pixel", "is_snap_2d_vertices_to_pixel_enabled");
5164 ADD_GROUP("Rendering", "");
5165 ADD_PROPERTY(PropertyInfo(Variant::INT, "msaa_2d", PROPERTY_HINT_ENUM, String::utf8("Disabled (Fastest),2× (Average),4× (Slow),8× (Slowest)")), "set_msaa_2d", "get_msaa_2d");
5166 ADD_PROPERTY(PropertyInfo(Variant::INT, "msaa_3d", PROPERTY_HINT_ENUM, String::utf8("Disabled (Fastest),2× (Average),4× (Slow),8× (Slowest)")), "set_msaa_3d", "get_msaa_3d");
5167 ADD_PROPERTY(PropertyInfo(Variant::INT, "screen_space_aa", PROPERTY_HINT_ENUM, "Disabled (Fastest),FXAA (Fast),SMAA (Average)"), "set_screen_space_aa", "get_screen_space_aa");
5168 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_taa"), "set_use_taa", "is_using_taa");
5169 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_debanding"), "set_use_debanding", "is_using_debanding");
5170 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_occlusion_culling"), "set_use_occlusion_culling", "is_using_occlusion_culling");
5171 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mesh_lod_threshold", PROPERTY_HINT_RANGE, "0,1024,0.1"), "set_mesh_lod_threshold", "get_mesh_lod_threshold");
5172 ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_draw", PROPERTY_HINT_ENUM, "Disabled,Unshaded,Lighting,Overdraw,Wireframe,Normal Buffer,VoxelGI Albedo,VoxelGI Lighting,VoxelGI Emission,Shadow Atlas,Directional Shadow Map,Scene Luminance,SSAO,SSIL,Directional Shadow Splits,Decal Atlas,SDFGI Cascades,SDFGI Probes,VoxelGI/SDFGI Buffer,Disable Mesh LOD,OmniLight3D Cluster,SpotLight3D Cluster,Decal Cluster,ReflectionProbe Cluster,Occlusion Culling Buffer,Motion Vectors,Internal Buffer"), "set_debug_draw", "get_debug_draw");
5173 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr_2d"), "set_use_hdr_2d", "is_using_hdr_2d");
5174
5175 #ifndef _3D_DISABLED
5176 ADD_GROUP("Scaling 3D", "");
5177 ADD_PROPERTY(PropertyInfo(Variant::INT, "scaling_3d_mode", PROPERTY_HINT_ENUM, "Bilinear (Fastest),FSR 1.0 (Fast),FSR 2.2 (Slow),MetalFX (Spatial),MetalFX (Temporal)"), "set_scaling_3d_mode", "get_scaling_3d_mode");
5178 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scaling_3d_scale", PROPERTY_HINT_RANGE, "0.25,2.0,0.01"), "set_scaling_3d_scale", "get_scaling_3d_scale");
5179 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "texture_mipmap_bias", PROPERTY_HINT_RANGE, "-2,2,0.001"), "set_texture_mipmap_bias", "get_texture_mipmap_bias");
5180 ADD_PROPERTY(PropertyInfo(Variant::INT, "anisotropic_filtering_level", PROPERTY_HINT_ENUM, String::utf8("Disabled (Fastest),2× (Faster),4× (Fast),8× (Average),16x (Slow)")), "set_anisotropic_filtering_level", "get_anisotropic_filtering_level");
5181 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fsr_sharpness", PROPERTY_HINT_RANGE, "0,2,0.1"), "set_fsr_sharpness", "get_fsr_sharpness");
5182 ADD_GROUP("Variable Rate Shading", "vrs_");
5183 ADD_PROPERTY(PropertyInfo(Variant::INT, "vrs_mode", PROPERTY_HINT_ENUM, "Disabled,Texture,XR"), "set_vrs_mode", "get_vrs_mode");
5184 ADD_PROPERTY(PropertyInfo(Variant::INT, "vrs_update_mode", PROPERTY_HINT_ENUM, "Disabled,Once,Always"), "set_vrs_update_mode", "get_vrs_update_mode");
5185 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "vrs_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_vrs_texture", "get_vrs_texture");
5186 #endif
5187 ADD_GROUP("Canvas Items", "canvas_item_");
5188 ADD_PROPERTY(PropertyInfo(Variant::INT, "canvas_item_default_texture_filter", PROPERTY_HINT_ENUM, "Nearest,Linear,Linear Mipmap,Nearest Mipmap"), "set_default_canvas_item_texture_filter", "get_default_canvas_item_texture_filter");
5189 ADD_PROPERTY(PropertyInfo(Variant::INT, "canvas_item_default_texture_repeat", PROPERTY_HINT_ENUM, "Disabled,Enabled,Mirror"), "set_default_canvas_item_texture_repeat", "get_default_canvas_item_texture_repeat");
5190 ADD_GROUP("Audio Listener", "audio_listener_");
5191 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_listener_enable_2d"), "set_as_audio_listener_2d", "is_audio_listener_2d");
5192 #ifndef _3D_DISABLED
5193 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_listener_enable_3d"), "set_as_audio_listener_3d", "is_audio_listener_3d");
5194 #endif // _3D_DISABLED
5195 #if !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
5196 ADD_GROUP("Physics", "physics_");
5197 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "physics_object_picking"), "set_physics_object_picking", "get_physics_object_picking");
5198 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "physics_object_picking_sort"), "set_physics_object_picking_sort", "get_physics_object_picking_sort");
5199 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "physics_object_picking_first_only"), "set_physics_object_picking_first_only", "get_physics_object_picking_first_only");
5200 #endif // !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
5201 ADD_GROUP("GUI", "gui_");
5202 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gui_disable_input"), "set_disable_input", "is_input_disabled");
5203 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gui_snap_controls_to_pixels"), "set_snap_controls_to_pixels", "is_snap_controls_to_pixels_enabled");
5204 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gui_embed_subwindows"), "set_embedding_subwindows", "is_embedding_subwindows");
5205 ADD_PROPERTY(PropertyInfo(Variant::INT, "gui_drag_threshold"), "set_drag_threshold", "get_drag_threshold");
5206 ADD_GROUP("SDF", "sdf_");
5207 ADD_PROPERTY(PropertyInfo(Variant::INT, "sdf_oversize", PROPERTY_HINT_ENUM, "100%,120%,150%,200%"), "set_sdf_oversize", "get_sdf_oversize");
5208 ADD_PROPERTY(PropertyInfo(Variant::INT, "sdf_scale", PROPERTY_HINT_ENUM, "100%,50%,25%"), "set_sdf_scale", "get_sdf_scale");
5209 ADD_GROUP("Positional Shadow Atlas", "positional_shadow_atlas_");
5210 ADD_PROPERTY(PropertyInfo(Variant::INT, "positional_shadow_atlas_size"), "set_positional_shadow_atlas_size", "get_positional_shadow_atlas_size");
5211 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "positional_shadow_atlas_16_bits"), "set_positional_shadow_atlas_16_bits", "get_positional_shadow_atlas_16_bits");
5212 ADD_PROPERTYI(PropertyInfo(Variant::INT, "positional_shadow_atlas_quad_0", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), "set_positional_shadow_atlas_quadrant_subdiv", "get_positional_shadow_atlas_quadrant_subdiv", 0);
5213 ADD_PROPERTYI(PropertyInfo(Variant::INT, "positional_shadow_atlas_quad_1", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), "set_positional_shadow_atlas_quadrant_subdiv", "get_positional_shadow_atlas_quadrant_subdiv", 1);
5214 ADD_PROPERTYI(PropertyInfo(Variant::INT, "positional_shadow_atlas_quad_2", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), "set_positional_shadow_atlas_quadrant_subdiv", "get_positional_shadow_atlas_quadrant_subdiv", 2);
5215 ADD_PROPERTYI(PropertyInfo(Variant::INT, "positional_shadow_atlas_quad_3", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), "set_positional_shadow_atlas_quadrant_subdiv", "get_positional_shadow_atlas_quadrant_subdiv", 3);
5216 ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "canvas_transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_canvas_transform", "get_canvas_transform");
5217 ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "global_canvas_transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_canvas_transform", "get_global_canvas_transform");
5218 ADD_PROPERTY(PropertyInfo(Variant::INT, "canvas_cull_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_canvas_cull_mask", "get_canvas_cull_mask");
5219
5220 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "oversampling"), "set_use_oversampling", "is_using_oversampling");
5221 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "oversampling_override", PROPERTY_HINT_RANGE, "0,16,0.0001,or_greater"), "set_oversampling_override", "get_oversampling_override");
5222
5223 ADD_SIGNAL(MethodInfo("size_changed"));
5224 ADD_SIGNAL(MethodInfo("gui_focus_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Control")));
5225
5226 BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED);
5227 BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_1);
5228 BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_4);
5229 BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_16);
5230 BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_64);
5231 BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_256);
5232 BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_1024);
5233 BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_MAX);
5234
5235 BIND_ENUM_CONSTANT(SCALING_3D_MODE_BILINEAR);
5236 BIND_ENUM_CONSTANT(SCALING_3D_MODE_FSR);
5237 BIND_ENUM_CONSTANT(SCALING_3D_MODE_FSR2);
5238 BIND_ENUM_CONSTANT(SCALING_3D_MODE_METALFX_SPATIAL);
5239 BIND_ENUM_CONSTANT(SCALING_3D_MODE_METALFX_TEMPORAL);
5240 BIND_ENUM_CONSTANT(SCALING_3D_MODE_MAX);
5241
5242 BIND_ENUM_CONSTANT(MSAA_DISABLED);
5243 BIND_ENUM_CONSTANT(MSAA_2X);
5244 BIND_ENUM_CONSTANT(MSAA_4X);
5245 BIND_ENUM_CONSTANT(MSAA_8X);
5246 BIND_ENUM_CONSTANT(MSAA_MAX);
5247
5248 BIND_ENUM_CONSTANT(ANISOTROPY_DISABLED);
5249 BIND_ENUM_CONSTANT(ANISOTROPY_2X);
5250 BIND_ENUM_CONSTANT(ANISOTROPY_4X);
5251 BIND_ENUM_CONSTANT(ANISOTROPY_8X);
5252 BIND_ENUM_CONSTANT(ANISOTROPY_16X);
5253 BIND_ENUM_CONSTANT(ANISOTROPY_MAX);
5254
5255 BIND_ENUM_CONSTANT(SCREEN_SPACE_AA_DISABLED);
5256 BIND_ENUM_CONSTANT(SCREEN_SPACE_AA_FXAA);
5257 BIND_ENUM_CONSTANT(SCREEN_SPACE_AA_SMAA);
5258 BIND_ENUM_CONSTANT(SCREEN_SPACE_AA_MAX);
5259
5260 BIND_ENUM_CONSTANT(RENDER_INFO_OBJECTS_IN_FRAME);
5261 BIND_ENUM_CONSTANT(RENDER_INFO_PRIMITIVES_IN_FRAME);
5262 BIND_ENUM_CONSTANT(RENDER_INFO_DRAW_CALLS_IN_FRAME);
5263 BIND_ENUM_CONSTANT(RENDER_INFO_MAX);
5264
5265 BIND_ENUM_CONSTANT(RENDER_INFO_TYPE_VISIBLE);
5266 BIND_ENUM_CONSTANT(RENDER_INFO_TYPE_SHADOW);
5267 BIND_ENUM_CONSTANT(RENDER_INFO_TYPE_CANVAS);
5268 BIND_ENUM_CONSTANT(RENDER_INFO_TYPE_MAX);
5269
5270 BIND_ENUM_CONSTANT(DEBUG_DRAW_DISABLED);
5271 BIND_ENUM_CONSTANT(DEBUG_DRAW_UNSHADED);
5272 BIND_ENUM_CONSTANT(DEBUG_DRAW_LIGHTING);
5273 BIND_ENUM_CONSTANT(DEBUG_DRAW_OVERDRAW);
5274 BIND_ENUM_CONSTANT(DEBUG_DRAW_WIREFRAME);
5275 BIND_ENUM_CONSTANT(DEBUG_DRAW_NORMAL_BUFFER);
5276 BIND_ENUM_CONSTANT(DEBUG_DRAW_VOXEL_GI_ALBEDO);
5277 BIND_ENUM_CONSTANT(DEBUG_DRAW_VOXEL_GI_LIGHTING);
5278 BIND_ENUM_CONSTANT(DEBUG_DRAW_VOXEL_GI_EMISSION);
5279 BIND_ENUM_CONSTANT(DEBUG_DRAW_SHADOW_ATLAS);
5280 BIND_ENUM_CONSTANT(DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS);
5281 BIND_ENUM_CONSTANT(DEBUG_DRAW_SCENE_LUMINANCE);
5282 BIND_ENUM_CONSTANT(DEBUG_DRAW_SSAO);
5283 BIND_ENUM_CONSTANT(DEBUG_DRAW_SSIL);
5284 BIND_ENUM_CONSTANT(DEBUG_DRAW_PSSM_SPLITS);
5285 BIND_ENUM_CONSTANT(DEBUG_DRAW_DECAL_ATLAS);
5286 BIND_ENUM_CONSTANT(DEBUG_DRAW_SDFGI);
5287 BIND_ENUM_CONSTANT(DEBUG_DRAW_SDFGI_PROBES);
5288 BIND_ENUM_CONSTANT(DEBUG_DRAW_GI_BUFFER);
5289 BIND_ENUM_CONSTANT(DEBUG_DRAW_DISABLE_LOD);
5290 BIND_ENUM_CONSTANT(DEBUG_DRAW_CLUSTER_OMNI_LIGHTS);
5291 BIND_ENUM_CONSTANT(DEBUG_DRAW_CLUSTER_SPOT_LIGHTS);
5292 BIND_ENUM_CONSTANT(DEBUG_DRAW_CLUSTER_DECALS);
5293 BIND_ENUM_CONSTANT(DEBUG_DRAW_CLUSTER_REFLECTION_PROBES);
5294 BIND_ENUM_CONSTANT(DEBUG_DRAW_OCCLUDERS)
5295 BIND_ENUM_CONSTANT(DEBUG_DRAW_MOTION_VECTORS)
5296 BIND_ENUM_CONSTANT(DEBUG_DRAW_INTERNAL_BUFFER);
5297
5298 BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
5299 BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR);
5300 BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS);
5301 BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
5302 BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_MAX);
5303
5304 BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
5305 BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
5306 BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_MIRROR);
5307 BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_MAX);
5308
5309 BIND_ENUM_CONSTANT(SDF_OVERSIZE_100_PERCENT);
5310 BIND_ENUM_CONSTANT(SDF_OVERSIZE_120_PERCENT);
5311 BIND_ENUM_CONSTANT(SDF_OVERSIZE_150_PERCENT);
5312 BIND_ENUM_CONSTANT(SDF_OVERSIZE_200_PERCENT);
5313 BIND_ENUM_CONSTANT(SDF_OVERSIZE_MAX);
5314
5315 BIND_ENUM_CONSTANT(SDF_SCALE_100_PERCENT);
5316 BIND_ENUM_CONSTANT(SDF_SCALE_50_PERCENT);
5317 BIND_ENUM_CONSTANT(SDF_SCALE_25_PERCENT);
5318 BIND_ENUM_CONSTANT(SDF_SCALE_MAX);
5319
5320 BIND_ENUM_CONSTANT(VRS_DISABLED);
5321 BIND_ENUM_CONSTANT(VRS_TEXTURE);
5322 BIND_ENUM_CONSTANT(VRS_XR);
5323 BIND_ENUM_CONSTANT(VRS_MAX);
5324
5325 BIND_ENUM_CONSTANT(VRS_UPDATE_DISABLED);
5326 BIND_ENUM_CONSTANT(VRS_UPDATE_ONCE);
5327 BIND_ENUM_CONSTANT(VRS_UPDATE_ALWAYS);
5328 BIND_ENUM_CONSTANT(VRS_UPDATE_MAX);
5329 }
5330
5331 void Viewport::_validate_property(PropertyInfo &p_property) const {
5332 if (!Engine::get_singleton()->is_editor_hint()) {
5333 return;
5334 }
5335 if (vrs_mode != VRS_TEXTURE && (p_property.name == "vrs_texture")) {
5336 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
5337 }
5338
5339 if (vrs_mode == VRS_DISABLED && (p_property.name == "vrs_update_mode")) {
5340 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
5341 }
5342 }
5343
5344 Viewport::Viewport() {
5345 world_2d.instantiate();
5346 world_2d->register_viewport(this);
5347
5348 viewport = RenderingServer::get_singleton()->viewport_create();
5349 texture_rid = RenderingServer::get_singleton()->viewport_get_texture(viewport);
5350
5351 default_texture.instantiate();
5352 default_texture->vp = this;
5353 viewport_textures.insert(default_texture.ptr());
5354 default_texture->proxy = RS::get_singleton()->texture_proxy_create(texture_rid);
5355
5356 canvas_layers.insert(nullptr); // This eases picking code (interpreted as the canvas of the Viewport).
5357
5358 set_positional_shadow_atlas_size(positional_shadow_atlas_size);
5359
5360 for (int i = 0; i < 4; i++) {
5361 positional_shadow_atlas_quadrant_subdiv[i] = SHADOW_ATLAS_QUADRANT_SUBDIV_MAX;
5362 }
5363 set_positional_shadow_atlas_quadrant_subdiv(0, SHADOW_ATLAS_QUADRANT_SUBDIV_4);
5364 set_positional_shadow_atlas_quadrant_subdiv(1, SHADOW_ATLAS_QUADRANT_SUBDIV_4);
5365 set_positional_shadow_atlas_quadrant_subdiv(2, SHADOW_ATLAS_QUADRANT_SUBDIV_16);
5366 set_positional_shadow_atlas_quadrant_subdiv(3, SHADOW_ATLAS_QUADRANT_SUBDIV_64);
5367
5368 set_mesh_lod_threshold(mesh_lod_threshold);
5369
5370 String id = itos(get_instance_id());
5371 input_group = "_vp_input" + id;
5372 unhandled_input_group = "_vp_unhandled_input" + id;
5373 shortcut_input_group = "_vp_shortcut_input" + id;
5374 unhandled_key_input_group = "_vp_unhandled_key_input" + id;
5375
5376 // Window tooltip.
5377 gui.tooltip_delay = GLOBAL_GET("gui/timers/tooltip_delay_sec");
5378
5379 #ifndef _3D_DISABLED
5380 set_scaling_3d_mode((Viewport::Scaling3DMode)(int)GLOBAL_GET("rendering/scaling_3d/mode"));
5381 set_scaling_3d_scale(GLOBAL_GET("rendering/scaling_3d/scale"));
5382 set_fsr_sharpness((float)GLOBAL_GET("rendering/scaling_3d/fsr_sharpness"));
5383 set_texture_mipmap_bias((float)GLOBAL_GET("rendering/textures/default_filters/texture_mipmap_bias"));
5384 set_anisotropic_filtering_level((Viewport::AnisotropicFiltering)(int)GLOBAL_GET("rendering/textures/default_filters/anisotropic_filtering_level"));
5385 #endif // _3D_DISABLED
5386
5387 set_sdf_oversize(sdf_oversize); // Set to server.
5388
5389 // Physics interpolation mode for viewports is a special case.
5390 // Typically viewports will be housed within Controls,
5391 // and Controls default to PHYSICS_INTERPOLATION_MODE_OFF.
5392 // Viewports can thus inherit physics interpolation OFF, which is unexpected.
5393 // Setting to ON allows each viewport to have a fresh interpolation state.
5394 set_physics_interpolation_mode(Node::PHYSICS_INTERPOLATION_MODE_ON);
5395
5396 ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Viewport::_on_settings_changed));
5397 }
5398
5399 Viewport::~Viewport() {
5400 // Erase itself from viewport textures.
5401 for (ViewportTexture *E : viewport_textures) {
5402 E->vp = nullptr;
5403 }
5404 if (world_2d.is_valid()) {
5405 world_2d->remove_viewport(this);
5406 }
5407 ERR_FAIL_NULL(RenderingServer::get_singleton());
5408 RenderingServer::get_singleton()->free_rid(viewport);
5409 }
5410
5411 /////////////////////////////////
5412
5413 void SubViewport::set_size(const Size2i &p_size) {
5414 ERR_MAIN_THREAD_GUARD;
5415 _internal_set_size(p_size);
5416 }
5417
5418 void SubViewport::set_size_force(const Size2i &p_size) {
5419 ERR_MAIN_THREAD_GUARD;
5420 // Use only for setting the size from the parent SubViewportContainer with enabled stretch mode.
5421 // Don't expose function to scripting.
5422 _internal_set_size(p_size, true);
5423 }
5424
5425 void SubViewport::_internal_set_size(const Size2i &p_size, bool p_force) {
5426 SubViewportContainer *c = Object::cast_to<SubViewportContainer>(get_parent());
5427 if (!p_force && c && c->is_stretch_enabled()) {
5428 #ifdef DEBUG_ENABLED
5429 WARN_PRINT("Can't change the size of a `SubViewport` with a `SubViewportContainer` parent that has `stretch` enabled. Set `SubViewportContainer.stretch` to `false` to allow changing the size manually.");
5430 #endif // DEBUG_ENABLED
5431 return;
5432 }
5433
5434 _set_size(p_size, _get_size_2d_override(), true);
5435
5436 if (c) {
5437 c->update_minimum_size();
5438 c->queue_redraw();
5439 }
5440 }
5441
5442 Size2i SubViewport::get_size() const {
5443 ERR_READ_THREAD_GUARD_V(Size2());
5444 return _get_size();
5445 }
5446
5447 void SubViewport::set_size_2d_override(const Size2i &p_size) {
5448 ERR_MAIN_THREAD_GUARD;
5449 _set_size(_get_size(), p_size, true);
5450 }
5451
5452 Size2i SubViewport::get_size_2d_override() const {
5453 ERR_READ_THREAD_GUARD_V(Size2i());
5454 // Rounding will cause offset issues with the
5455 // exact positioning of subwindows, but changing the
5456 // type of size_2d_override would break compatibility.
5457 return Size2i((_get_size_2d_override() + Size2(0.5, 0.5)).floor());
5458 }
5459
5460 void SubViewport::set_size_2d_override_stretch(bool p_enable) {
5461 ERR_MAIN_THREAD_GUARD;
5462 if (p_enable == size_2d_override_stretch) {
5463 return;
5464 }
5465
5466 size_2d_override_stretch = p_enable;
5467 _set_size(_get_size(), _get_size_2d_override(), true);
5468 }
5469
5470 bool SubViewport::is_size_2d_override_stretch_enabled() const {
5471 ERR_READ_THREAD_GUARD_V(false);
5472 return size_2d_override_stretch;
5473 }
5474
5475 void SubViewport::set_update_mode(UpdateMode p_mode) {
5476 ERR_MAIN_THREAD_GUARD;
5477 update_mode = p_mode;
5478 RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::ViewportUpdateMode(p_mode));
5479 }
5480
5481 SubViewport::UpdateMode SubViewport::get_update_mode() const {
5482 ERR_READ_THREAD_GUARD_V(UPDATE_DISABLED);
5483 return update_mode;
5484 }
5485
5486 void SubViewport::set_clear_mode(ClearMode p_mode) {
5487 ERR_MAIN_THREAD_GUARD;
5488 clear_mode = p_mode;
5489 RS::get_singleton()->viewport_set_clear_mode(get_viewport_rid(), RS::ViewportClearMode(p_mode));
5490 }
5491
5492 SubViewport::ClearMode SubViewport::get_clear_mode() const {
5493 ERR_READ_THREAD_GUARD_V(CLEAR_MODE_ALWAYS);
5494 return clear_mode;
5495 }
5496
5497 DisplayServer::WindowID SubViewport::get_window_id() const {
5498 ERR_READ_THREAD_GUARD_V(DisplayServer::INVALID_WINDOW_ID);
5499 return DisplayServer::INVALID_WINDOW_ID;
5500 }
5501
5502 Transform2D SubViewport::get_screen_transform_internal(bool p_absolute_position) const {
5503 ERR_READ_THREAD_GUARD_V(Transform2D());
5504 Transform2D container_transform;
5505 SubViewportContainer *c = Object::cast_to<SubViewportContainer>(get_parent());
5506 if (c) {
5507 if (c->is_stretch_enabled()) {
5508 container_transform.scale(Vector2(c->get_stretch_shrink(), c->get_stretch_shrink()));
5509 }
5510 container_transform = c->get_viewport()->get_screen_transform_internal(p_absolute_position) * c->get_global_transform_with_canvas() * container_transform;
5511 } else {
5512 WARN_PRINT_ONCE("SubViewport is not a child of a SubViewportContainer. get_screen_transform doesn't return the actual screen position.");
5513 }
5514 return container_transform * get_final_transform();
5515 }
5516
5517 Transform2D SubViewport::get_popup_base_transform() const {
5518 ERR_READ_THREAD_GUARD_V(Transform2D());
5519 if (is_embedding_subwindows()) {
5520 return Transform2D();
5521 }
5522 SubViewportContainer *c = Object::cast_to<SubViewportContainer>(get_parent());
5523 if (!c) {
5524 return get_final_transform();
5525 }
5526 Transform2D container_transform;
5527 if (c->is_stretch_enabled()) {
5528 container_transform.scale(Vector2(c->get_stretch_shrink(), c->get_stretch_shrink()));
5529 }
5530 return c->get_screen_transform() * container_transform * get_final_transform();
5531 }
5532
5533 Viewport *SubViewport::get_section_root_viewport() const {
5534 if (Object::cast_to<SubViewportContainer>(get_parent()) && get_parent()->get_viewport()) {
5535 return get_parent()->get_viewport()->get_section_root_viewport();
5536 }
5537 SubViewport *vp = const_cast<SubViewport *>(this);
5538 return vp;
5539 }
5540
5541 bool SubViewport::is_attached_in_viewport() const {
5542 return Object::cast_to<SubViewportContainer>(get_parent());
5543 }
5544
5545 void SubViewport::_notification(int p_what) {
5546 ERR_MAIN_THREAD_GUARD;
5547 switch (p_what) {
5548 case NOTIFICATION_ENTER_TREE: {
5549 RS::get_singleton()->viewport_set_active(get_viewport_rid(), true);
5550
5551 SubViewportContainer *parent_svc = Object::cast_to<SubViewportContainer>(get_parent());
5552 if (parent_svc) {
5553 parent_svc->recalc_force_viewport_sizes();
5554 }
5555 } break;
5556
5557 case NOTIFICATION_EXIT_TREE: {
5558 RS::get_singleton()->viewport_set_active(get_viewport_rid(), false);
5559 } break;
5560 }
5561 }
5562
5563 void SubViewport::_bind_methods() {
5564 ClassDB::bind_method(D_METHOD("set_size", "size"), &SubViewport::set_size);
5565 ClassDB::bind_method(D_METHOD("get_size"), &SubViewport::get_size);
5566
5567 ClassDB::bind_method(D_METHOD("set_size_2d_override", "size"), &SubViewport::set_size_2d_override);
5568 ClassDB::bind_method(D_METHOD("get_size_2d_override"), &SubViewport::get_size_2d_override);
5569
5570 ClassDB::bind_method(D_METHOD("set_size_2d_override_stretch", "enable"), &SubViewport::set_size_2d_override_stretch);
5571 ClassDB::bind_method(D_METHOD("is_size_2d_override_stretch_enabled"), &SubViewport::is_size_2d_override_stretch_enabled);
5572
5573 ClassDB::bind_method(D_METHOD("set_update_mode", "mode"), &SubViewport::set_update_mode);
5574 ClassDB::bind_method(D_METHOD("get_update_mode"), &SubViewport::get_update_mode);
5575
5576 ClassDB::bind_method(D_METHOD("set_clear_mode", "mode"), &SubViewport::set_clear_mode);
5577 ClassDB::bind_method(D_METHOD("get_clear_mode"), &SubViewport::get_clear_mode);
5578
5579 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
5580 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size_2d_override", PROPERTY_HINT_NONE, "suffix:px"), "set_size_2d_override", "get_size_2d_override");
5581 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "size_2d_override_stretch"), "set_size_2d_override_stretch", "is_size_2d_override_stretch_enabled");
5582 ADD_GROUP("Render Target", "render_target_");
5583 ADD_PROPERTY(PropertyInfo(Variant::INT, "render_target_clear_mode", PROPERTY_HINT_ENUM, "Always,Never,Next Frame"), "set_clear_mode", "get_clear_mode");
5584 ADD_PROPERTY(PropertyInfo(Variant::INT, "render_target_update_mode", PROPERTY_HINT_ENUM, "Disabled,Once,When Visible,When Parent Visible,Always"), "set_update_mode", "get_update_mode");
5585
5586 BIND_ENUM_CONSTANT(CLEAR_MODE_ALWAYS);
5587 BIND_ENUM_CONSTANT(CLEAR_MODE_NEVER);
5588 BIND_ENUM_CONSTANT(CLEAR_MODE_ONCE);
5589
5590 BIND_ENUM_CONSTANT(UPDATE_DISABLED);
5591 BIND_ENUM_CONSTANT(UPDATE_ONCE);
5592 BIND_ENUM_CONSTANT(UPDATE_WHEN_VISIBLE);
5593 BIND_ENUM_CONSTANT(UPDATE_WHEN_PARENT_VISIBLE);
5594 BIND_ENUM_CONSTANT(UPDATE_ALWAYS);
5595 }
5596
5597 void SubViewport::_validate_property(PropertyInfo &p_property) const {
5598 if (!Engine::get_singleton()->is_editor_hint()) {
5599 return;
5600 }
5601 if (p_property.name == "size") {
5602 SubViewportContainer *parent_svc = Object::cast_to<SubViewportContainer>(get_parent());
5603 if (parent_svc && parent_svc->is_stretch_enabled()) {
5604 p_property.usage = PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY;
5605 } else {
5606 p_property.usage = PROPERTY_USAGE_DEFAULT;
5607 }
5608 }
5609 }
5610
5611 SubViewport::SubViewport() {
5612 RS::get_singleton()->viewport_set_size(get_viewport_rid(), get_size().width, get_size().height);
5613 }
5614
5615 /////////////////////////////////
5616
5617 #if DEBUG_ENABLED
5618 template <class T>
5619 bool Viewport::CameraOverride<T>::is_enabled() const {
5620 return enabled;
5621 }
5622
5623 template <class T>
5624 void Viewport::CameraOverride<T>::enable(Viewport *p_viewport, const T *p_current_camera) {
5625 if (enabled) {
5626 return;
5627 }
5628
5629 T *override_camera = memnew(T);
5630 override_camera->set_name(vformat("Override%s", T ::get_class_static()));
5631 p_viewport->add_child(override_camera, false, Node::INTERNAL_MODE_BACK);
5632
5633 override_camera->make_current();
5634 set_overridden_camera(p_current_camera);
5635
5636 // Call to make the override camera current must happen before we enable the override to prevent the override mechanism from kicking in.
5637 enabled = true;
5638 }
5639
5640 template <class T>
5641 void Viewport::CameraOverride<T>::disable(T *p_current_camera) {
5642 if (!enabled) {
5643 return;
5644 }
5645
5646 // Call to make the overridden camera current must happen after we disable the override to prevent the override mechanism from kicking in.
5647 enabled = false;
5648
5649 T *overridden_camera = get_overridden_camera();
5650 if (overridden_camera) {
5651 overridden_camera->make_current();
5652 } else {
5653 p_current_camera->clear_current();
5654 }
5655
5656 p_current_camera->queue_free();
5657 overridden_camera_id = ObjectID();
5658 }
5659
5660 template <class T>
5661 void Viewport::CameraOverride<T>::set_overridden_camera(const T *p_camera) {
5662 overridden_camera_id = p_camera ? p_camera->get_instance_id() : ObjectID();
5663 }
5664
5665 template <class T>
5666 T *Viewport::CameraOverride<T>::get_overridden_camera() const {
5667 return ObjectDB::get_instance<T>(overridden_camera_id);
5668 }
5669
5670 // Explicit template instantiation to allow template definitions inside cpp file
5671 // and prevent instantiation using other than the desired camera types.
5672 template class Viewport::CameraOverride<Camera2D>;
5673 #ifndef _3D_DISABLED
5674 template class Viewport::CameraOverride<Camera3D>;
5675 #endif // _3D_DISABLED
5676 #endif // DEBUG_ENABLED