diff options
Diffstat (limited to 'scene/main/viewport.cpp')
-rw-r--r-- | scene/main/viewport.cpp | 920 |
1 files changed, 427 insertions, 493 deletions
diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index b95e81a702..8544d67ecc 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -32,7 +32,7 @@ #include "core/core_string_names.h" #include "core/debugger/engine_debugger.h" -#include "core/input/input_filter.h" +#include "core/input/input.h" #include "core/os/os.h" #include "core/project_settings.h" #include "scene/2d/collision_object_2d.h" @@ -56,12 +56,11 @@ #include "servers/physics_server_2d.h" void ViewportTexture::setup_local_to_scene() { - if (vp) { vp->viewport_textures.erase(this); } - vp = NULL; + vp = nullptr; Node *local_scene = get_local_scene(); if (!local_scene) { @@ -87,9 +86,9 @@ void ViewportTexture::setup_local_to_scene() { } void ViewportTexture::set_viewport_path_in_scene(const NodePath &p_path) { - - if (path == p_path) + if (path == p_path) { return; + } path = p_path; @@ -99,27 +98,25 @@ void ViewportTexture::set_viewport_path_in_scene(const NodePath &p_path) { } NodePath ViewportTexture::get_viewport_path_in_scene() const { - return path; } int ViewportTexture::get_width() const { - ERR_FAIL_COND_V_MSG(!vp, 0, "Viewport Texture must be set to use it."); return vp->size.width; } -int ViewportTexture::get_height() const { +int ViewportTexture::get_height() const { ERR_FAIL_COND_V_MSG(!vp, 0, "Viewport Texture must be set to use it."); return vp->size.height; } -Size2 ViewportTexture::get_size() const { +Size2 ViewportTexture::get_size() const { ERR_FAIL_COND_V_MSG(!vp, Size2(), "Viewport Texture must be set to use it."); return vp->size; } -RID ViewportTexture::get_rid() const { +RID ViewportTexture::get_rid() const { //ERR_FAIL_COND_V_MSG(!vp, RID(), "Viewport Texture must be set to use it."); if (proxy.is_null()) { proxy_ph = RS::get_singleton()->texture_2d_placeholder_create(); @@ -129,17 +126,15 @@ RID ViewportTexture::get_rid() const { } bool ViewportTexture::has_alpha() const { - return false; } -Ref<Image> ViewportTexture::get_data() const { +Ref<Image> ViewportTexture::get_data() const { ERR_FAIL_COND_V_MSG(!vp, Ref<Image>(), "Viewport Texture must be set to use it."); return RS::get_singleton()->texture_2d_get(vp->texture_rid); } void ViewportTexture::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_viewport_path_in_scene", "path"), &ViewportTexture::set_viewport_path_in_scene); ClassDB::bind_method(D_METHOD("get_viewport_path_in_scene"), &ViewportTexture::get_viewport_path_in_scene); @@ -147,13 +142,11 @@ void ViewportTexture::_bind_methods() { } ViewportTexture::ViewportTexture() { - - vp = NULL; + vp = nullptr; set_local_to_scene(true); } ViewportTexture::~ViewportTexture() { - if (vp) { vp->viewport_textures.erase(this); } @@ -169,57 +162,53 @@ ViewportTexture::~ViewportTexture() { ///////////////////////////////////// class TooltipPanel : public PopupPanel { - GDCLASS(TooltipPanel, PopupPanel); public: - TooltipPanel(){}; + TooltipPanel() {} }; class TooltipLabel : public Label { - GDCLASS(TooltipLabel, Label); public: - TooltipLabel(){}; + TooltipLabel() {} }; Viewport::GUI::GUI() { - embed_subwindows_hint = false; embedding_subwindows = false; dragging = false; - mouse_focus = NULL; + mouse_focus = nullptr; forced_mouse_focus = false; - mouse_click_grabber = NULL; + mouse_click_grabber = nullptr; mouse_focus_mask = 0; - key_focus = NULL; - mouse_over = NULL; - drag_mouse_over = NULL; + key_focus = nullptr; + mouse_over = nullptr; + drag_mouse_over = nullptr; - tooltip = NULL; - tooltip_popup = NULL; - tooltip_label = NULL; + tooltip = nullptr; + tooltip_popup = nullptr; + tooltip_label = nullptr; } ///////////////////////////////////// void Viewport::update_worlds() { - - if (!is_inside_tree()) + if (!is_inside_tree()) { return; + } Rect2 abstracted_rect = Rect2(Vector2(), get_visible_rect().size); Rect2 xformed_rect = (global_canvas_transform * canvas_transform).affine_inverse().xform(abstracted_rect); find_world_2d()->_update_viewport(this, xformed_rect); find_world_2d()->_update(); - find_world()->_update(get_tree()->get_frame()); + find_world_3d()->_update(get_tree()->get_frame()); } void Viewport::_collision_object_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) { - Transform object_transform = p_object->get_global_transform(); Transform camera_transform = p_camera->get_global_transform(); ObjectID id = p_object->get_instance_id(); @@ -238,14 +227,12 @@ void Viewport::_collision_object_input_event(CollisionObject3D *p_object, Camera } void Viewport::_sub_window_update_order() { - for (int i = 0; i < gui.sub_windows.size(); i++) { RS::get_singleton()->canvas_item_set_draw_index(gui.sub_windows[i].canvas_item, i); } } void Viewport::_sub_window_register(Window *p_window) { - ERR_FAIL_COND(!is_inside_tree()); for (int i = 0; i < gui.sub_windows.size(); i++) { ERR_FAIL_COND(gui.sub_windows[i].window == p_window); @@ -268,7 +255,6 @@ void Viewport::_sub_window_register(Window *p_window) { } void Viewport::_sub_window_update(Window *p_window) { - int index = -1; for (int i = 0; i < gui.sub_windows.size(); i++) { if (gui.sub_windows[i].window == p_window) { @@ -313,7 +299,6 @@ void Viewport::_sub_window_update(Window *p_window) { } void Viewport::_sub_window_grab_focus(Window *p_window) { - if (p_window == nullptr) { //release current focus if (gui.subwindow_focused) { @@ -385,7 +370,6 @@ void Viewport::_sub_window_grab_focus(Window *p_window) { } void Viewport::_sub_window_remove(Window *p_window) { - for (int i = 0; i < gui.sub_windows.size(); i++) { if (gui.sub_windows[i].window == p_window) { RS::get_singleton()->free(gui.sub_windows[i].canvas_item); @@ -407,7 +391,6 @@ void Viewport::_sub_window_remove(Window *p_window) { gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_OUT); if (parent_visible && parent_visible != this) { - gui.subwindow_focused = parent_visible; gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_IN); } else { @@ -422,44 +405,41 @@ void Viewport::_sub_window_remove(Window *p_window) { RenderingServer::get_singleton()->viewport_set_parent_viewport(p_window->viewport, p_window->parent ? p_window->parent->viewport : RID()); } -void Viewport::_own_world_changed() { - ERR_FAIL_COND(world.is_null()); - ERR_FAIL_COND(own_world.is_null()); +void Viewport::_own_world_3d_changed() { + ERR_FAIL_COND(world_3d.is_null()); + ERR_FAIL_COND(own_world_3d.is_null()); if (is_inside_tree()) { _propagate_exit_world(this); } - own_world = world->duplicate(); + own_world_3d = world_3d->duplicate(); if (is_inside_tree()) { _propagate_enter_world(this); } if (is_inside_tree()) { - RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world()->get_scenario()); + RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario()); } _update_listener(); } void Viewport::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_ENTER_TREE: { - gui.embedding_subwindows = gui.embed_subwindows_hint; if (get_parent()) { parent = get_parent()->get_viewport(); RenderingServer::get_singleton()->viewport_set_parent_viewport(viewport, parent->get_viewport_rid()); } else { - parent = NULL; + parent = nullptr; } current_canvas = find_world_2d()->get_canvas(); - RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world()->get_scenario()); + RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario()); RenderingServer::get_singleton()->viewport_attach_canvas(viewport, current_canvas); _update_listener(); @@ -474,14 +454,14 @@ void Viewport::_notification(int p_what) { contact_2d_debug = RenderingServer::get_singleton()->canvas_item_create(); RenderingServer::get_singleton()->canvas_item_set_parent(contact_2d_debug, find_world_2d()->get_canvas()); //3D - PhysicsServer3D::get_singleton()->space_set_debug_contacts(find_world()->get_space(), get_tree()->get_collision_debug_contact_count()); + PhysicsServer3D::get_singleton()->space_set_debug_contacts(find_world_3d()->get_space(), get_tree()->get_collision_debug_contact_count()); contact_3d_debug_multimesh = RenderingServer::get_singleton()->multimesh_create(); RenderingServer::get_singleton()->multimesh_allocate(contact_3d_debug_multimesh, get_tree()->get_collision_debug_contact_count(), RS::MULTIMESH_TRANSFORM_3D, true); RenderingServer::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh, 0); RenderingServer::get_singleton()->multimesh_set_mesh(contact_3d_debug_multimesh, get_tree()->get_debug_contact_mesh()->get_rid()); contact_3d_debug_instance = RenderingServer::get_singleton()->instance_create(); RenderingServer::get_singleton()->instance_set_base(contact_3d_debug_instance, contact_3d_debug_multimesh); - RenderingServer::get_singleton()->instance_set_scenario(contact_3d_debug_instance, find_world()->get_scenario()); + RenderingServer::get_singleton()->instance_set_scenario(contact_3d_debug_instance, find_world_3d()->get_scenario()); //RenderingServer::get_singleton()->instance_geometry_set_flag(contact_3d_debug_instance, RS::INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS, true); } @@ -489,30 +469,30 @@ void Viewport::_notification(int p_what) { case NOTIFICATION_READY: { #ifndef _3D_DISABLED if (listeners.size() && !listener) { - Listener3D *first = NULL; + Listener3D *first = nullptr; for (Set<Listener3D *>::Element *E = listeners.front(); E; E = E->next()) { - - if (first == NULL || first->is_greater_than(E->get())) { + if (first == nullptr || first->is_greater_than(E->get())) { first = E->get(); } } - if (first) + if (first) { first->make_current(); + } } if (cameras.size() && !camera) { //there are cameras but no current camera, pick first in tree and make it current - Camera3D *first = NULL; + Camera3D *first = nullptr; for (Set<Camera3D *>::Element *E = cameras.front(); E; E = E->next()) { - - if (first == NULL || first->is_greater_than(E->get())) { + if (first == nullptr || first->is_greater_than(E->get())) { first = E->get(); } } - if (first) + if (first) { first->make_current(); + } } #endif @@ -522,10 +502,10 @@ void Viewport::_notification(int p_what) { } break; case NOTIFICATION_EXIT_TREE: { - _gui_cancel_tooltip(); - if (world_2d.is_valid()) + if (world_2d.is_valid()) { world_2d->_remove_viewport(this); + } RenderingServer::get_singleton()->viewport_set_scenario(viewport, RID()); RenderingServer::get_singleton()->viewport_remove_canvas(viewport, current_canvas); @@ -548,7 +528,6 @@ void Viewport::_notification(int p_what) { } break; case NOTIFICATION_INTERNAL_PROCESS: { - if (gui.tooltip_timer >= 0) { gui.tooltip_timer -= get_process_delta_time(); if (gui.tooltip_timer < 0) { @@ -558,9 +537,7 @@ void Viewport::_notification(int p_what) { } break; case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { - if (get_tree()->is_debugging_collisions_hint() && contact_2d_debug.is_valid()) { - RenderingServer::get_singleton()->canvas_item_clear(contact_2d_debug); RenderingServer::get_singleton()->canvas_item_set_draw_index(contact_2d_debug, 0xFFFFF); //very high index @@ -569,24 +546,21 @@ void Viewport::_notification(int p_what) { Color ccol = get_tree()->get_debug_collision_contact_color(); for (int i = 0; i < point_count; i++) { - RenderingServer::get_singleton()->canvas_item_add_rect(contact_2d_debug, Rect2(points[i] - Vector2(2, 2), Vector2(5, 5)), ccol); } } if (get_tree()->is_debugging_collisions_hint() && contact_3d_debug_multimesh.is_valid()) { - - Vector<Vector3> points = PhysicsServer3D::get_singleton()->space_get_contacts(find_world()->get_space()); - int point_count = PhysicsServer3D::get_singleton()->space_get_contact_count(find_world()->get_space()); + Vector<Vector3> points = PhysicsServer3D::get_singleton()->space_get_contacts(find_world_3d()->get_space()); + int point_count = PhysicsServer3D::get_singleton()->space_get_contact_count(find_world_3d()->get_space()); RS::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh, point_count); } - if (physics_object_picking && (to_screen_rect == Rect2i() || InputFilter::get_singleton()->get_mouse_mode() != InputFilter::MOUSE_MODE_CAPTURED)) { - + if (physics_object_picking && (to_screen_rect == Rect2i() || Input::get_singleton()->get_mouse_mode() != Input::MOUSE_MODE_CAPTURED)) { #ifndef _3D_DISABLED Vector2 last_pos(1e20, 1e20); - CollisionObject3D *last_object = NULL; + CollisionObject3D *last_object = nullptr; ObjectID last_id; #endif PhysicsDirectSpaceState3D::RayResult result; @@ -621,7 +595,6 @@ void Viewport::_notification(int p_what) { } while (physics_picking_events.size()) { - Ref<InputEvent> ev = physics_picking_events.front()->get(); physics_picking_events.pop_front(); @@ -631,7 +604,6 @@ void Viewport::_notification(int p_what) { Ref<InputEventMouseMotion> mm = ev; if (mm.is_valid()) { - pos = mm->get_position(); is_mouse = true; @@ -647,7 +619,6 @@ void Viewport::_notification(int p_what) { Ref<InputEventMouseButton> mb = ev; if (mb.is_valid()) { - pos = mb->get_position(); is_mouse = true; @@ -715,7 +686,6 @@ void Viewport::_notification(int p_what) { int rc = ss2d->intersect_point_on_canvas(point, canvas_layer_id, res, 64, Set<RID>(), 0xFFFFFFFF, true, true, true); for (int i = 0; i < rc; i++) { - if (res[i].collider_id.is_valid() && res[i].collider) { CollisionObject2D *co = Object::cast_to<CollisionObject2D>(res[i].collider); if (co) { @@ -750,7 +720,6 @@ void Viewport::_notification(int p_what) { if (E->get() != frame) { Object *o = ObjectDB::get_instance(E->key()); if (o) { - CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o); if (co) { co->_mouse_exit(); @@ -771,7 +740,6 @@ void Viewport::_notification(int p_what) { bool captured = false; if (physics_object_capture.is_valid()) { - CollisionObject3D *co = Object::cast_to<CollisionObject3D>(ObjectDB::get_instance(physics_object_capture)); if (co && camera) { _collision_object_input_event(co, camera, ev, Vector3(), Vector3(), 0); @@ -788,7 +756,6 @@ void Viewport::_notification(int p_what) { if (captured) { //none } else if (pos == last_pos) { - if (last_id.is_valid()) { if (ObjectDB::get_instance(last_id) && last_object) { //good, exists @@ -799,22 +766,17 @@ void Viewport::_notification(int p_what) { } } } else { - if (camera) { - Vector3 from = camera->project_ray_origin(pos); Vector3 dir = camera->project_ray_normal(pos); - PhysicsDirectSpaceState3D *space = PhysicsServer3D::get_singleton()->space_get_direct_state(find_world()->get_space()); + PhysicsDirectSpaceState3D *space = PhysicsServer3D::get_singleton()->space_get_direct_state(find_world_3d()->get_space()); if (space) { - bool col = space->intersect_ray(from, from + dir * 10000, result, Set<RID>(), 0xFFFFFFFF, true, true, true); ObjectID new_collider; if (col) { - CollisionObject3D *co = Object::cast_to<CollisionObject3D>(result.collider); if (co) { - _collision_object_input_event(co, camera, ev, result.position, result.normal, result.shape); last_object = co; last_id = result.collider_id; @@ -826,9 +788,7 @@ void Viewport::_notification(int p_what) { } if (is_mouse && new_collider != physics_object_over) { - if (physics_object_over.is_valid()) { - CollisionObject3D *co = Object::cast_to<CollisionObject3D>(ObjectDB::get_instance(physics_object_over)); if (co) { co->_mouse_exit(); @@ -836,7 +796,6 @@ void Viewport::_notification(int p_what) { } if (new_collider.is_valid()) { - CollisionObject3D *co = Object::cast_to<CollisionObject3D>(ObjectDB::get_instance(new_collider)); if (co) { co->_mouse_enter(); @@ -857,7 +816,6 @@ void Viewport::_notification(int p_what) { } break; case NOTIFICATION_WM_MOUSE_EXIT: case NOTIFICATION_WM_FOCUS_OUT: { - _drop_physics_mouseover(); if (gui.mouse_focus && !gui.forced_mouse_focus) { @@ -868,24 +826,25 @@ void Viewport::_notification(int p_what) { } RID Viewport::get_viewport_rid() const { - return viewport; } void Viewport::update_canvas_items() { - if (!is_inside_tree()) + if (!is_inside_tree()) { return; + } _update_canvas_items(this); } -void Viewport::_set_size(const Size2i &p_size, const Size2i &p_size_override, const Rect2i &p_to_screen_rect, const Transform2D &p_stretch_transform, bool p_allocated) { - - if (size == p_size && size_allocated == p_allocated && stretch_transform == p_stretch_transform && p_size_override == size_override && to_screen_rect != p_to_screen_rect) +void Viewport::_set_size(const Size2i &p_size, const Size2i &p_size_2d_override, const Rect2i &p_to_screen_rect, const Transform2D &p_stretch_transform, bool p_allocated) { + if (size == p_size && size_allocated == p_allocated && stretch_transform == p_stretch_transform && p_size_2d_override == size_2d_override && to_screen_rect != p_to_screen_rect) { return; + } + size = p_size; size_allocated = p_allocated; - size_override = p_size_override; + size_2d_override = p_size_2d_override; stretch_transform = p_stretch_transform; to_screen_rect = p_to_screen_rect; @@ -904,12 +863,16 @@ void Viewport::_set_size(const Size2i &p_size, const Size2i &p_size_override, co Size2i Viewport::_get_size() const { return size; } + +Size2i Viewport::_get_size_2d_override() const { + return size_2d_override; +} + bool Viewport::_is_size_allocated() const { return size_allocated; } Rect2 Viewport::get_visible_rect() const { - Rect2 r; if (size == Size2()) { @@ -918,8 +881,8 @@ Rect2 Viewport::get_visible_rect() const { r = Rect2(Point2(), size); } - if (size_override != Size2i()) { - r.size = size_override; + if (size_2d_override != Size2i()) { + r.size = size_2d_override; } return r; @@ -929,7 +892,6 @@ void Viewport::_update_listener() { } void Viewport::_update_listener_2d() { - /* if (is_inside_tree() && audio_listener && (!get_parent() || (Object::cast_to<Control>(get_parent()) && Object::cast_to<Control>(get_parent())->is_visible_in_tree()))) SpatialSound2DServer::get_singleton()->listener_set_space(internal_listener_2d, find_world_2d()->get_sound_space()); @@ -939,23 +901,22 @@ void Viewport::_update_listener_2d() { } void Viewport::set_as_audio_listener(bool p_enable) { - - if (p_enable == audio_listener) + if (p_enable == audio_listener) { return; + } audio_listener = p_enable; _update_listener(); } bool Viewport::is_audio_listener() const { - return audio_listener; } void Viewport::set_as_audio_listener_2d(bool p_enable) { - - if (p_enable == audio_listener_2d) + if (p_enable == audio_listener_2d) { return; + } audio_listener_2d = p_enable; @@ -963,7 +924,6 @@ void Viewport::set_as_audio_listener_2d(bool p_enable) { } bool Viewport::is_audio_listener_2d() const { - return audio_listener_2d; } @@ -1000,7 +960,6 @@ Transform2D Viewport::get_canvas_transform_override() const { } void Viewport::set_canvas_transform(const Transform2D &p_transform) { - canvas_transform = p_transform; if (!override_canvas_transform) { @@ -1009,26 +968,22 @@ void Viewport::set_canvas_transform(const Transform2D &p_transform) { } Transform2D Viewport::get_canvas_transform() const { - return canvas_transform; } void Viewport::_update_global_transform() { - Transform2D sxform = stretch_transform * global_canvas_transform; RenderingServer::get_singleton()->viewport_set_global_canvas_transform(viewport, sxform); } void Viewport::set_global_canvas_transform(const Transform2D &p_transform) { - global_canvas_transform = p_transform; _update_global_transform(); } Transform2D Viewport::get_global_canvas_transform() const { - return global_canvas_transform; } @@ -1036,11 +991,11 @@ void Viewport::_listener_transform_changed_notify() { } void Viewport::_listener_set(Listener3D *p_listener) { - #ifndef _3D_DISABLED - if (listener == p_listener) + if (listener == p_listener) { return; + } listener = p_listener; @@ -1050,37 +1005,36 @@ void Viewport::_listener_set(Listener3D *p_listener) { } bool Viewport::_listener_add(Listener3D *p_listener) { - listeners.insert(p_listener); return listeners.size() == 1; } void Viewport::_listener_remove(Listener3D *p_listener) { - listeners.erase(p_listener); if (listener == p_listener) { - listener = NULL; + listener = nullptr; } } #ifndef _3D_DISABLED void Viewport::_listener_make_next_current(Listener3D *p_exclude) { - if (listeners.size() > 0) { for (Set<Listener3D *>::Element *E = listeners.front(); E; E = E->next()) { - - if (p_exclude == E->get()) + if (p_exclude == E->get()) { continue; - if (!E->get()->is_inside_tree()) + } + if (!E->get()->is_inside_tree()) { continue; - if (listener != NULL) + } + if (listener != nullptr) { return; + } E->get()->make_current(); } } else { // Attempt to reset listener to the camera position - if (camera != NULL) { + if (camera != nullptr) { _update_listener(); _camera_transform_changed_notify(); } @@ -1089,17 +1043,16 @@ void Viewport::_listener_make_next_current(Listener3D *p_exclude) { #endif void Viewport::_camera_transform_changed_notify() { - #ifndef _3D_DISABLED #endif } void Viewport::_camera_set(Camera3D *p_camera) { - #ifndef _3D_DISABLED - if (camera == p_camera) + if (camera == p_camera) { return; + } if (camera) { camera->notification(Camera3D::NOTIFICATION_LOST_CURRENT); @@ -1108,10 +1061,11 @@ void Viewport::_camera_set(Camera3D *p_camera) { camera = p_camera; if (!camera_override) { - if (camera) + if (camera) { RenderingServer::get_singleton()->viewport_attach_camera(viewport, camera->get_camera()); - else + } else { RenderingServer::get_singleton()->viewport_attach_camera(viewport, RID()); + } } if (camera) { @@ -1124,31 +1078,30 @@ void Viewport::_camera_set(Camera3D *p_camera) { } bool Viewport::_camera_add(Camera3D *p_camera) { - cameras.insert(p_camera); return cameras.size() == 1; } void Viewport::_camera_remove(Camera3D *p_camera) { - cameras.erase(p_camera); if (camera == p_camera) { camera->notification(Camera3D::NOTIFICATION_LOST_CURRENT); - camera = NULL; + camera = nullptr; } } #ifndef _3D_DISABLED void Viewport::_camera_make_next_current(Camera3D *p_exclude) { - for (Set<Camera3D *>::Element *E = cameras.front(); E; E = E->next()) { - - if (p_exclude == E->get()) + if (p_exclude == E->get()) { continue; - if (!E->get()->is_inside_tree()) + } + if (!E->get()->is_inside_tree()) { continue; - if (camera != NULL) + } + if (camera != nullptr) { return; + } E->get()->make_current(); } @@ -1156,32 +1109,29 @@ void Viewport::_camera_make_next_current(Camera3D *p_exclude) { #endif void Viewport::_canvas_layer_add(CanvasLayer *p_canvas_layer) { - canvas_layers.insert(p_canvas_layer); } void Viewport::_canvas_layer_remove(CanvasLayer *p_canvas_layer) { - canvas_layers.erase(p_canvas_layer); } void Viewport::set_transparent_background(bool p_enable) { - transparent_bg = p_enable; RS::get_singleton()->viewport_set_transparent_background(viewport, p_enable); } bool Viewport::has_transparent_background() const { - return transparent_bg; } void Viewport::set_world_2d(const Ref<World2D> &p_world_2d) { - if (world_2d == p_world_2d) + if (world_2d == p_world_2d) { return; + } if (parent && parent->find_world_2d() == p_world_2d) { - WARN_PRINT("Unable to use parent world as world_2d"); + WARN_PRINT("Unable to use parent world_3d as world_2d"); return; } @@ -1190,10 +1140,10 @@ void Viewport::set_world_2d(const Ref<World2D> &p_world_2d) { RenderingServer::get_singleton()->viewport_remove_canvas(viewport, current_canvas); } - if (p_world_2d.is_valid()) + if (p_world_2d.is_valid()) { world_2d = p_world_2d; - else { - WARN_PRINT("Invalid world"); + } else { + WARN_PRINT("Invalid world_3d"); world_2d = Ref<World2D>(memnew(World2D)); } @@ -1207,21 +1157,20 @@ void Viewport::set_world_2d(const Ref<World2D> &p_world_2d) { } Ref<World2D> Viewport::find_world_2d() const { - - if (world_2d.is_valid()) + if (world_2d.is_valid()) { return world_2d; - else if (parent) + } else if (parent) { return parent->find_world_2d(); - else + } else { return Ref<World2D>(); + } } void Viewport::_propagate_enter_world(Node *p_node) { - if (p_node != this) { - - if (!p_node->is_inside_tree()) //may not have entered scene yet + if (!p_node->is_inside_tree()) { //may not have entered scene yet return; + } #ifndef _3D_DISABLED if (Object::cast_to<Node3D>(p_node) || Object::cast_to<WorldEnvironment>(p_node)) { @@ -1230,9 +1179,9 @@ void Viewport::_propagate_enter_world(Node *p_node) { #endif Viewport *v = Object::cast_to<Viewport>(p_node); if (v) { - - if (v->world.is_valid() || v->own_world.is_valid()) + if (v->world_3d.is_valid() || v->own_world_3d.is_valid()) { return; + } } #ifndef _3D_DISABLED } @@ -1240,28 +1189,26 @@ void Viewport::_propagate_enter_world(Node *p_node) { } for (int i = 0; i < p_node->get_child_count(); i++) { - _propagate_enter_world(p_node->get_child(i)); } } void Viewport::_propagate_viewport_notification(Node *p_node, int p_what) { - p_node->notification(p_what); for (int i = 0; i < p_node->get_child_count(); i++) { Node *c = p_node->get_child(i); - if (Object::cast_to<Viewport>(c)) + if (Object::cast_to<Viewport>(c)) { continue; + } _propagate_viewport_notification(c, p_what); } } void Viewport::_propagate_exit_world(Node *p_node) { - if (p_node != this) { - - if (!p_node->is_inside_tree()) //may have exited scene already + if (!p_node->is_inside_tree()) { //may have exited scene already return; + } #ifndef _3D_DISABLED if (Object::cast_to<Node3D>(p_node) || Object::cast_to<WorldEnvironment>(p_node)) { @@ -1270,9 +1217,9 @@ void Viewport::_propagate_exit_world(Node *p_node) { #endif Viewport *v = Object::cast_to<Viewport>(p_node); if (v) { - - if (v->world.is_valid() || v->own_world.is_valid()) + if (v->world_3d.is_valid() || v->own_world_3d.is_valid()) { return; + } } #ifndef _3D_DISABLED } @@ -1280,68 +1227,66 @@ void Viewport::_propagate_exit_world(Node *p_node) { } for (int i = 0; i < p_node->get_child_count(); i++) { - _propagate_exit_world(p_node->get_child(i)); } } -void Viewport::set_world(const Ref<World3D> &p_world) { - - if (world == p_world) +void Viewport::set_world_3d(const Ref<World3D> &p_world_3d) { + if (world_3d == p_world_3d) { return; + } - if (is_inside_tree()) + if (is_inside_tree()) { _propagate_exit_world(this); + } - if (own_world.is_valid() && world.is_valid()) { - world->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Viewport::_own_world_changed)); + if (own_world_3d.is_valid() && world_3d.is_valid()) { + world_3d->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Viewport::_own_world_3d_changed)); } - world = p_world; + world_3d = p_world_3d; - if (own_world.is_valid()) { - if (world.is_valid()) { - own_world = world->duplicate(); - world->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Viewport::_own_world_changed)); + if (own_world_3d.is_valid()) { + if (world_3d.is_valid()) { + own_world_3d = world_3d->duplicate(); + world_3d->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Viewport::_own_world_3d_changed)); } else { - own_world = Ref<World3D>(memnew(World3D)); + own_world_3d = Ref<World3D>(memnew(World3D)); } } - if (is_inside_tree()) + if (is_inside_tree()) { _propagate_enter_world(this); + } if (is_inside_tree()) { - RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world()->get_scenario()); + RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario()); } _update_listener(); } -Ref<World3D> Viewport::get_world() const { - - return world; +Ref<World3D> Viewport::get_world_3d() const { + return world_3d; } Ref<World2D> Viewport::get_world_2d() const { - return world_2d; } -Ref<World3D> Viewport::find_world() const { - - if (own_world.is_valid()) - return own_world; - else if (world.is_valid()) - return world; - else if (parent) - return parent->find_world(); - else +Ref<World3D> Viewport::find_world_3d() const { + if (own_world_3d.is_valid()) { + return own_world_3d; + } else if (world_3d.is_valid()) { + return world_3d; + } else if (parent) { + return parent->find_world_3d(); + } else { return Ref<World3D>(); + } } Listener3D *Viewport::get_listener() const { - return listener; } @@ -1350,7 +1295,6 @@ Camera3D *Viewport::get_camera() const { } void Viewport::enable_camera_override(bool p_enable) { - #ifndef _3D_DISABLED if (p_enable == camera_override) { return; @@ -1395,8 +1339,9 @@ Transform Viewport::get_camera_override_transform() const { void Viewport::set_camera_override_perspective(float p_fovy_degrees, float p_z_near, float p_z_far) { if (camera_override) { if (camera_override.fov == p_fovy_degrees && camera_override.z_near == p_z_near && - camera_override.z_far == p_z_far && camera_override.projection == CameraOverrideData::PROJECTION_PERSPECTIVE) + camera_override.z_far == p_z_far && camera_override.projection == CameraOverrideData::PROJECTION_PERSPECTIVE) { return; + } camera_override.fov = p_fovy_degrees; camera_override.z_near = p_z_near; @@ -1410,8 +1355,9 @@ void Viewport::set_camera_override_perspective(float p_fovy_degrees, float p_z_n void Viewport::set_camera_override_orthogonal(float p_size, float p_z_near, float p_z_far) { if (camera_override) { if (camera_override.size == p_size && camera_override.z_near == p_z_near && - camera_override.z_far == p_z_far && camera_override.projection == CameraOverrideData::PROJECTION_ORTHOGONAL) + camera_override.z_far == p_z_far && camera_override.projection == CameraOverrideData::PROJECTION_ORTHOGONAL) { return; + } camera_override.size = p_size; camera_override.z_near = p_z_near; @@ -1423,16 +1369,15 @@ void Viewport::set_camera_override_orthogonal(float p_size, float p_z_near, floa } Transform2D Viewport::get_final_transform() const { - return stretch_transform * global_canvas_transform; } void Viewport::_update_canvas_items(Node *p_node) { if (p_node != this) { - Viewport *vp = Object::cast_to<Viewport>(p_node); - if (vp) + if (vp) { return; + } CanvasItem *ci = Object::cast_to<CanvasItem>(p_node); if (ci) { @@ -1448,49 +1393,45 @@ void Viewport::_update_canvas_items(Node *p_node) { } Ref<ViewportTexture> Viewport::get_texture() const { - return default_texture; } void Viewport::set_shadow_atlas_size(int p_size) { - - if (shadow_atlas_size == p_size) + if (shadow_atlas_size == p_size) { return; + } shadow_atlas_size = p_size; RS::get_singleton()->viewport_set_shadow_atlas_size(viewport, p_size); } int Viewport::get_shadow_atlas_size() const { - return shadow_atlas_size; } void Viewport::set_shadow_atlas_quadrant_subdiv(int p_quadrant, ShadowAtlasQuadrantSubdiv p_subdiv) { - ERR_FAIL_INDEX(p_quadrant, 4); ERR_FAIL_INDEX(p_subdiv, SHADOW_ATLAS_QUADRANT_SUBDIV_MAX); - if (shadow_atlas_quadrant_subdiv[p_quadrant] == p_subdiv) + if (shadow_atlas_quadrant_subdiv[p_quadrant] == p_subdiv) { return; + } shadow_atlas_quadrant_subdiv[p_quadrant] = p_subdiv; static const int subdiv[SHADOW_ATLAS_QUADRANT_SUBDIV_MAX] = { 0, 1, 4, 16, 64, 256, 1024 }; RS::get_singleton()->viewport_set_shadow_atlas_quadrant_subdivision(viewport, p_quadrant, subdiv[p_subdiv]); } -Viewport::ShadowAtlasQuadrantSubdiv Viewport::get_shadow_atlas_quadrant_subdiv(int p_quadrant) const { +Viewport::ShadowAtlasQuadrantSubdiv Viewport::get_shadow_atlas_quadrant_subdiv(int p_quadrant) const { ERR_FAIL_INDEX_V(p_quadrant, 4, SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED); return shadow_atlas_quadrant_subdiv[p_quadrant]; } Transform2D Viewport::_get_input_pre_xform() const { - Transform2D pre_xf; if (to_screen_rect.size.x != 0 && to_screen_rect.size.y != 0) { - pre_xf.elements[2] = -to_screen_rect.position; pre_xf.scale(size / to_screen_rect.size); } @@ -1499,27 +1440,24 @@ Transform2D Viewport::_get_input_pre_xform() const { } Ref<InputEvent> Viewport::_make_input_local(const Ref<InputEvent> &ev) { - Transform2D ai = get_final_transform().affine_inverse() * _get_input_pre_xform(); return ev->xformed_by(ai); } Vector2 Viewport::get_mouse_position() const { - return gui.last_mouse_pos; } void Viewport::warp_mouse(const Vector2 &p_pos) { - Vector2 gpos = (get_final_transform().affine_inverse() * _get_input_pre_xform()).affine_inverse().xform(p_pos); - InputFilter::get_singleton()->warp_mouse_position(gpos); + Input::get_singleton()->warp_mouse_position(gpos); } void Viewport::_gui_sort_roots() { - - if (!gui.roots_order_dirty) + if (!gui.roots_order_dirty) { return; + } gui.roots.sort_custom<Control::CComparator>(); @@ -1527,37 +1465,37 @@ void Viewport::_gui_sort_roots() { } void Viewport::_gui_cancel_tooltip() { - - gui.tooltip = NULL; + gui.tooltip = nullptr; gui.tooltip_timer = -1; if (gui.tooltip_popup) { gui.tooltip_popup->queue_delete(); - gui.tooltip_popup = NULL; - gui.tooltip_label = NULL; + gui.tooltip_popup = nullptr; + gui.tooltip_label = nullptr; } } String Viewport::_gui_get_tooltip(Control *p_control, const Vector2 &p_pos, Control **r_which) { - Vector2 pos = p_pos; String tooltip; while (p_control) { - tooltip = p_control->get_tooltip(pos); if (r_which) { *r_which = p_control; } - if (tooltip != String()) + if (tooltip != String()) { break; + } pos = p_control->get_transform().xform(pos); - if (p_control->data.mouse_filter == Control::MOUSE_FILTER_STOP) + if (p_control->data.mouse_filter == Control::MOUSE_FILTER_STOP) { break; - if (p_control->is_set_as_toplevel()) + } + if (p_control->is_set_as_toplevel()) { break; + } p_control = p_control->get_parent_control(); } @@ -1566,21 +1504,21 @@ String Viewport::_gui_get_tooltip(Control *p_control, const Vector2 &p_pos, Cont } void Viewport::_gui_show_tooltip() { - if (!gui.tooltip) { return; } - Control *which = NULL; + Control *which = nullptr; String tooltip = _gui_get_tooltip(gui.tooltip, gui.tooltip->get_global_transform().xform_inv(gui.tooltip_pos), &which); tooltip = tooltip.strip_edges(); - if (tooltip.length() == 0) + if (tooltip.length() == 0) { return; // bye + } if (gui.tooltip_popup) { memdelete(gui.tooltip_popup); - gui.tooltip_popup = NULL; - gui.tooltip_label = NULL; + gui.tooltip_popup = nullptr; + gui.tooltip_label = nullptr; } if (!which) { @@ -1617,15 +1555,17 @@ void Viewport::_gui_show_tooltip() { Rect2i vr = gui.tooltip_popup->get_parent_visible_window()->get_usable_parent_rect(); - if (r.size.x + r.position.x > vr.size.x + vr.position.x) + if (r.size.x + r.position.x > vr.size.x + vr.position.x) { r.position.x = vr.position.x + vr.size.x - r.size.x; - else if (r.position.x < vr.position.x) + } else if (r.position.x < vr.position.x) { r.position.x = vr.position.x; + } - if (r.size.y + r.position.y > vr.size.y + vr.position.y) + if (r.size.y + r.position.y > vr.size.y + vr.position.y) { r.position.y = vr.position.y + vr.size.y - r.size.y; - else if (r.position.y < vr.position.y) + } else if (r.position.y < vr.position.y) { r.position.y = vr.position.y; + } gui.tooltip_popup->set_position(r.position); gui.tooltip_popup->set_size(r.size); @@ -1635,7 +1575,6 @@ void Viewport::_gui_show_tooltip() { } void Viewport::_gui_call_input(Control *p_control, const Ref<InputEvent> &p_input) { - //_block(); Ref<InputEvent> ev = p_input; @@ -1651,36 +1590,40 @@ void Viewport::_gui_call_input(Control *p_control, const Ref<InputEvent> &p_inpu Ref<InputEventPanGesture> pn = p_input; cant_stop_me_now = pn.is_valid() || cant_stop_me_now; - bool ismouse = ev.is_valid() || Object::cast_to<InputEventMouseMotion>(*p_input) != NULL; + bool ismouse = ev.is_valid() || Object::cast_to<InputEventMouseMotion>(*p_input) != nullptr; CanvasItem *ci = p_control; while (ci) { - Control *control = Object::cast_to<Control>(ci); if (control) { - if (control->data.mouse_filter != Control::MOUSE_FILTER_IGNORE) { control->emit_signal(SceneStringNames::get_singleton()->gui_input, ev); //signal should be first, so it's possible to override an event (and then accept it) } - if (gui.key_event_accepted) + if (gui.key_event_accepted) { break; - if (!control->is_inside_tree()) + } + if (!control->is_inside_tree()) { break; + } if (control->data.mouse_filter != Control::MOUSE_FILTER_IGNORE) { control->call_multilevel(SceneStringNames::get_singleton()->_gui_input, ev); } - if (!control->is_inside_tree() || control->is_set_as_toplevel()) + if (!control->is_inside_tree() || control->is_set_as_toplevel()) { break; - if (gui.key_event_accepted) + } + if (gui.key_event_accepted) { break; - if (!cant_stop_me_now && control->data.mouse_filter == Control::MOUSE_FILTER_STOP && ismouse) + } + if (!cant_stop_me_now && control->data.mouse_filter == Control::MOUSE_FILTER_STOP && ismouse) { break; + } } - if (ci->is_set_as_toplevel()) + if (ci->is_set_as_toplevel()) { break; + } ev = ev->xformed_by(ci->get_transform()); //transform event upwards ci = ci->get_parent_item(); @@ -1690,93 +1633,98 @@ void Viewport::_gui_call_input(Control *p_control, const Ref<InputEvent> &p_inpu } void Viewport::_gui_call_notification(Control *p_control, int p_what) { - CanvasItem *ci = p_control; while (ci) { - Control *control = Object::cast_to<Control>(ci); if (control) { - if (control->data.mouse_filter != Control::MOUSE_FILTER_IGNORE) { control->notification(p_what); } - if (!control->is_inside_tree()) + if (!control->is_inside_tree()) { break; + } - if (!control->is_inside_tree() || control->is_set_as_toplevel()) + if (!control->is_inside_tree() || control->is_set_as_toplevel()) { break; - if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) + } + if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) { break; + } } - if (ci->is_set_as_toplevel()) + if (ci->is_set_as_toplevel()) { break; + } ci = ci->get_parent_item(); } //_unblock(); } -Control *Viewport::_gui_find_control(const Point2 &p_global) { +Control *Viewport::_gui_find_control(const Point2 &p_global) { //aca va subwindows _gui_sort_roots(); for (List<Control *>::Element *E = gui.roots.back(); E; E = E->prev()) { - Control *sw = E->get(); - if (!sw->is_visible_in_tree()) + if (!sw->is_visible_in_tree()) { continue; + } Transform2D xform; CanvasItem *pci = sw->get_parent_item(); - if (pci) + if (pci) { xform = pci->get_global_transform_with_canvas(); - else + } else { xform = sw->get_canvas_transform(); + } Control *ret = _gui_find_control_at_pos(sw, p_global, xform, gui.focus_inv_xform); - if (ret) + if (ret) { return ret; + } } - return NULL; + return nullptr; } Control *Viewport::_gui_find_control_at_pos(CanvasItem *p_node, const Point2 &p_global, const Transform2D &p_xform, Transform2D &r_inv_xform) { - - if (Object::cast_to<Viewport>(p_node)) - return NULL; + if (Object::cast_to<Viewport>(p_node)) { + return nullptr; + } if (!p_node->is_visible()) { //return _find_next_visible_control_at_pos(p_node,p_global,r_inv_xform); - return NULL; //canvas item hidden, discard + return nullptr; //canvas item hidden, discard } Transform2D matrix = p_xform * p_node->get_transform(); // matrix.basis_determinant() == 0.0f implies that node does not exist on scene - if (matrix.basis_determinant() == 0.0f) - return NULL; + if (matrix.basis_determinant() == 0.0f) { + return nullptr; + } Control *c = Object::cast_to<Control>(p_node); if (!c || !c->clips_input() || c->has_point(matrix.affine_inverse().xform(p_global))) { - for (int i = p_node->get_child_count() - 1; i >= 0; i--) { - CanvasItem *ci = Object::cast_to<CanvasItem>(p_node->get_child(i)); - if (!ci || ci->is_set_as_toplevel()) + if (!ci || ci->is_set_as_toplevel()) { continue; + } Control *ret = _gui_find_control_at_pos(ci, p_global, matrix, r_inv_xform); - if (ret) + if (ret) { return ret; + } } } - if (!c) - return NULL; + if (!c) { + return nullptr; + } matrix.affine_invert(); @@ -1784,19 +1732,17 @@ Control *Viewport::_gui_find_control_at_pos(CanvasItem *p_node, const Point2 &p_ if (c->data.mouse_filter != Control::MOUSE_FILTER_IGNORE && c->has_point(matrix.xform(p_global)) && (!gui.drag_preview || (c != gui.drag_preview && !gui.drag_preview->is_a_parent_of(c)))) { r_inv_xform = matrix; return c; - } else - return NULL; + } else { + return nullptr; + } } bool Viewport::_gui_drop(Control *p_at_control, Point2 p_at_pos, bool p_just_check) { - { //attempt grab, try parent controls too CanvasItem *ci = p_at_control; while (ci) { - Control *control = Object::cast_to<Control>(ci); if (control) { - if (control->can_drop_data(p_at_pos, gui.drag_data)) { if (!p_just_check) { control->drop_data(p_at_pos, gui.drag_data); @@ -1805,14 +1751,16 @@ bool Viewport::_gui_drop(Control *p_at_control, Point2 p_at_pos, bool p_just_che return true; } - if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) + if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) { break; + } } p_at_pos = ci->get_transform().xform(p_at_pos); - if (ci->is_set_as_toplevel()) + if (ci->is_set_as_toplevel()) { break; + } ci = ci->get_parent_item(); } @@ -1822,7 +1770,6 @@ bool Viewport::_gui_drop(Control *p_at_control, Point2 p_at_pos, bool p_just_che } void Viewport::_gui_input_event(Ref<InputEvent> p_event) { - ERR_FAIL_COND(p_event.is_null()); //? @@ -1835,19 +1782,15 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { Ref<InputEventMouseButton> mb = p_event; if (mb.is_valid()) { - gui.key_event_accepted = false; Point2 mpos = mb->get_position(); if (mb->is_pressed()) { - Size2 pos = mpos; if (gui.mouse_focus_mask) { - //do not steal mouse focus and stuff while a focus mask exists gui.mouse_focus_mask |= 1 << (mb->get_button_index() - 1); //add the button to the mask } else { - bool is_handled = false; if (is_handled) { @@ -1888,7 +1831,6 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { #ifdef DEBUG_ENABLED if (EngineDebugger::get_singleton() && gui.mouse_focus) { - Array arr; arr.push_back(gui.mouse_focus->get_path()); arr.push_back(gui.mouse_focus->get_class()); @@ -1899,7 +1841,6 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { if (mb->get_button_index() == BUTTON_LEFT) { //assign focus CanvasItem *ci = gui.mouse_focus; while (ci) { - Control *control = Object::cast_to<Control>(ci); if (control) { if (control->get_focus_mode() != Control::FOCUS_NONE) { @@ -1909,12 +1850,14 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { break; } - if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) + if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) { break; + } } - if (ci->is_set_as_toplevel()) + if (ci->is_set_as_toplevel()) { break; + } ci = ci->get_parent_item(); } @@ -1927,7 +1870,6 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { set_input_as_handled(); if (gui.drag_data.get_type() != Variant::NIL && mb->get_button_index() == BUTTON_LEFT) { - //alternate drop use (when using force_drag(), as proposed by #5342 if (gui.mouse_focus) { _gui_drop(gui.mouse_focus, pos, false); @@ -1938,7 +1880,7 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { if (gui.drag_preview) { memdelete(gui.drag_preview); - gui.drag_preview = NULL; + gui.drag_preview = nullptr; } _propagate_viewport_notification(this, NOTIFICATION_DRAG_END); //change mouse accordingly @@ -1948,16 +1890,14 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { //gui.tooltip_popup->hide(); } else { - if (gui.drag_data.get_type() != Variant::NIL && mb->get_button_index() == BUTTON_LEFT) { - if (gui.drag_mouse_over) { _gui_drop(gui.drag_mouse_over, gui.drag_mouse_over_pos, false); } if (gui.drag_preview && mb->get_button_index() == BUTTON_LEFT) { memdelete(gui.drag_preview); - gui.drag_preview = NULL; + gui.drag_preview = nullptr; } gui.drag_data = Variant(); @@ -1985,7 +1925,7 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { //disable mouse focus if needed before calling input, this makes popups on mouse press event work better, as the release will never be received otherwise if (gui.mouse_focus_mask == 0) { - gui.mouse_focus = NULL; + gui.mouse_focus = nullptr; gui.forced_mouse_focus = false; } @@ -2005,51 +1945,47 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { Ref<InputEventMouseMotion> mm = p_event; if (mm.is_valid()) { - gui.key_event_accepted = false; Point2 mpos = mm->get_position(); gui.last_mouse_pos = mpos; - Control *over = NULL; + Control *over = nullptr; // D&D if (!gui.drag_attempted && gui.mouse_focus && mm->get_button_mask() & BUTTON_MASK_LEFT) { - gui.drag_accum += mm->get_relative(); float len = gui.drag_accum.length(); if (len > 10) { - { //attempt grab, try parent controls too CanvasItem *ci = gui.mouse_focus; while (ci) { - Control *control = Object::cast_to<Control>(ci); if (control) { - gui.dragging = true; gui.drag_data = control->get_drag_data(control->get_global_transform_with_canvas().affine_inverse().xform(mpos) - gui.drag_accum); if (gui.drag_data.get_type() != Variant::NIL) { - - gui.mouse_focus = NULL; + gui.mouse_focus = nullptr; gui.forced_mouse_focus = false; gui.mouse_focus_mask = 0; break; } else { - if (gui.drag_preview != NULL) { + if (gui.drag_preview != nullptr) { ERR_PRINT("Don't set a drag preview and return null data. Preview was deleted and drag request ignored."); memdelete(gui.drag_preview); - gui.drag_preview = NULL; + gui.drag_preview = nullptr; } gui.dragging = false; } - if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) + if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) { break; + } } - if (ci->is_set_as_toplevel()) + if (ci->is_set_as_toplevel()) { break; + } ci = ci->get_parent_item(); } @@ -2057,7 +1993,6 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { gui.drag_attempted = true; if (gui.drag_data.get_type() != Variant::NIL) { - _propagate_viewport_notification(this, NOTIFICATION_DRAG_BEGIN); } } @@ -2068,12 +2003,10 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { //recompute focus_inv_xform again here } else { - over = _gui_find_control(mpos); } if (over != gui.mouse_over) { - if (gui.mouse_over) { _gui_call_notification(gui.mouse_over, Control::NOTIFICATION_MOUSE_EXIT); } @@ -2087,10 +2020,9 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { gui.mouse_over = over; - DisplayServer::CursorShape ds_cursor_shape = (DisplayServer::CursorShape)InputFilter::get_singleton()->get_default_cursor_shape(); + DisplayServer::CursorShape ds_cursor_shape = (DisplayServer::CursorShape)Input::get_singleton()->get_default_cursor_shape(); if (over) { - Transform2D localizer = over->get_global_transform_with_canvas().affine_inverse(); Size2 pos = localizer.xform(mpos); Vector2 speed = localizer.basis_xform(mm->get_speed()); @@ -2113,14 +2045,13 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { if (can_tooltip && gui.tooltip) { String tooltip = _gui_get_tooltip(over, gui.tooltip->get_global_transform().xform_inv(mpos)); - if (tooltip.length() == 0) + if (tooltip.length() == 0) { _gui_cancel_tooltip(); - else if (gui.tooltip_label) { + } else if (gui.tooltip_label) { if (tooltip == gui.tooltip_label->get_text()) { is_tooltip_shown = true; } } else { - Variant t = gui.tooltip_popup->call("get_tooltip_text"); if (t.get_type() == Variant::STRING) { @@ -2131,12 +2062,12 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { is_tooltip_shown = true; //well, nothing to compare against, likely using custom control, so if it changes there is nothing we can do } } - } else + } else { _gui_cancel_tooltip(); + } } if (can_tooltip && !is_tooltip_shown) { - gui.tooltip = over; gui.tooltip_pos = over->get_screen_transform().xform(pos); //(parent_xform * get_transform()).affine_inverse().xform(pos); gui.tooltip_timer = gui.tooltip_delay; @@ -2154,12 +2085,15 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { while (c) { cursor_shape = c->get_cursor_shape(cpos); cpos = c->get_transform().xform(cpos); - if (cursor_shape != Control::CURSOR_ARROW) + if (cursor_shape != Control::CURSOR_ARROW) { break; - if (c->data.mouse_filter == Control::MOUSE_FILTER_STOP) + } + if (c->data.mouse_filter == Control::MOUSE_FILTER_STOP) { break; - if (c->is_set_as_toplevel()) + } + if (c->is_set_as_toplevel()) { break; + } c = c->get_parent_control(); } } @@ -2259,7 +2193,6 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { gui.drag_mouse_over_pos = localizer.xform(viewport_pos); if (mm->get_button_mask() & BUTTON_MASK_LEFT) { - bool can_drop = _gui_drop(gui.drag_mouse_over, gui.drag_mouse_over_pos, true); if (!can_drop) { @@ -2280,15 +2213,11 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { Ref<InputEventScreenTouch> touch_event = p_event; if (touch_event.is_valid()) { - Size2 pos = touch_event->get_position(); if (touch_event->is_pressed()) { - Control *over = _gui_find_control(pos); if (over) { - if (over->can_process()) { - touch_event = touch_event->xformed_by(Transform2D()); //make a copy if (over == gui.mouse_focus) { pos = gui.focus_inv_xform.xform(pos); @@ -2302,9 +2231,7 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { return; } } else if (touch_event->get_index() == 0 && gui.last_mouse_focus) { - if (gui.last_mouse_focus->can_process()) { - touch_event = touch_event->xformed_by(Transform2D()); //make a copy touch_event->set_position(gui.focus_inv_xform.xform(pos)); @@ -2317,7 +2244,6 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { Ref<InputEventGesture> gesture_event = p_event; if (gesture_event.is_valid()) { - gui.key_event_accepted = false; _gui_cancel_tooltip(); @@ -2326,9 +2252,7 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { Control *over = _gui_find_control(pos); if (over) { - if (over->can_process()) { - gesture_event = gesture_event->xformed_by(Transform2D()); //make a copy if (over == gui.mouse_focus) { pos = gui.focus_inv_xform.xform(pos); @@ -2345,15 +2269,12 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { Ref<InputEventScreenDrag> drag_event = p_event; if (drag_event.is_valid()) { - Control *over = gui.mouse_focus; if (!over) { over = _gui_find_control(drag_event->get_position()); } if (over) { - if (over->can_process()) { - Transform2D localizer = over->get_global_transform_with_canvas().affine_inverse(); Size2 pos = localizer.xform(drag_event->get_position()); Vector2 speed = localizer.basis_xform(drag_event->get_speed()); @@ -2374,28 +2295,26 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { } if (mm.is_null() && mb.is_null() && p_event->is_action_type()) { - if (gui.key_focus && !gui.key_focus->is_visible_in_tree()) { gui.key_focus->release_focus(); } if (gui.key_focus) { - gui.key_event_accepted = false; if (gui.key_focus->can_process()) { gui.key_focus->call_multilevel(SceneStringNames::get_singleton()->_gui_input, p_event); - if (gui.key_focus) //maybe lost it + if (gui.key_focus) { //maybe lost it gui.key_focus->emit_signal(SceneStringNames::get_singleton()->gui_input, p_event); + } } if (gui.key_event_accepted) { - set_input_as_handled(); return; } } - Control *from = gui.key_focus ? gui.key_focus : NULL; //hmm + Control *from = gui.key_focus ? gui.key_focus : nullptr; //hmm //keyboard focus //if (from && p_event->is_pressed() && !p_event->get_alt() && !p_event->get_metakey() && !p_event->key->get_command()) { @@ -2405,37 +2324,31 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { bool mods = k.is_valid() && (k->get_control() || k->get_alt() || k->get_shift() || k->get_metakey()); if (from && p_event->is_pressed()) { - Control *next = NULL; + Control *next = nullptr; - InputFilter *input = InputFilter::get_singleton(); + Input *input = Input::get_singleton(); if (p_event->is_action_pressed("ui_focus_next") && input->is_action_just_pressed("ui_focus_next")) { - next = from->find_next_valid_focus(); } if (p_event->is_action_pressed("ui_focus_prev") && input->is_action_just_pressed("ui_focus_prev")) { - next = from->find_prev_valid_focus(); } if (!mods && p_event->is_action_pressed("ui_up") && input->is_action_just_pressed("ui_up")) { - next = from->_get_focus_neighbour(MARGIN_TOP); } if (!mods && p_event->is_action_pressed("ui_left") && input->is_action_just_pressed("ui_left")) { - next = from->_get_focus_neighbour(MARGIN_LEFT); } if (!mods && p_event->is_action_pressed("ui_right") && input->is_action_just_pressed("ui_right")) { - next = from->_get_focus_neighbour(MARGIN_RIGHT); } if (!mods && p_event->is_action_pressed("ui_down") && input->is_action_just_pressed("ui_down")) { - next = from->_get_focus_neighbour(MARGIN_BOTTOM); } @@ -2448,7 +2361,6 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { } List<Control *>::Element *Viewport::_gui_add_root_control(Control *p_control) { - gui.roots_order_dirty = true; return gui.roots.push_back(p_control); } @@ -2458,12 +2370,11 @@ void Viewport::_gui_set_root_order_dirty() { } void Viewport::_gui_force_drag(Control *p_base, const Variant &p_data, Control *p_control) { - ERR_FAIL_COND_MSG(p_data.get_type() == Variant::NIL, "Drag data must be a value."); gui.dragging = true; gui.drag_data = p_data; - gui.mouse_focus = NULL; + gui.mouse_focus = nullptr; if (p_control) { _gui_set_drag_preview(p_base, p_control); @@ -2471,11 +2382,10 @@ void Viewport::_gui_force_drag(Control *p_base, const Variant &p_data, Control * } void Viewport::_gui_set_drag_preview(Control *p_base, Control *p_control) { - ERR_FAIL_NULL(p_control); ERR_FAIL_COND(!Object::cast_to<Control>((Object *)p_control)); ERR_FAIL_COND(p_control->is_inside_tree()); - ERR_FAIL_COND(p_control->get_parent() != NULL); + ERR_FAIL_COND(p_control->get_parent() != nullptr); if (gui.drag_preview) { memdelete(gui.drag_preview); @@ -2489,82 +2399,74 @@ void Viewport::_gui_set_drag_preview(Control *p_base, Control *p_control) { } void Viewport::_gui_remove_root_control(List<Control *>::Element *RI) { - gui.roots.erase(RI); } void Viewport::_gui_unfocus_control(Control *p_control) { - if (gui.key_focus == p_control) { gui.key_focus->release_focus(); } } void Viewport::_gui_hid_control(Control *p_control) { - if (gui.mouse_focus == p_control) { _drop_mouse_focus(); } - /* ??? - if (data.window==p_control) { - window->drag_data=Variant(); - if (window->drag_preview) { - memdelete( window->drag_preview); - window->drag_preview=NULL; - } - } - */ - - if (gui.key_focus == p_control) + if (gui.key_focus == p_control) { _gui_remove_focus(); - if (gui.mouse_over == p_control) - gui.mouse_over = NULL; - if (gui.drag_mouse_over == p_control) - gui.drag_mouse_over = NULL; - if (gui.tooltip == p_control) + } + if (gui.mouse_over == p_control) { + gui.mouse_over = nullptr; + } + if (gui.drag_mouse_over == p_control) { + gui.drag_mouse_over = nullptr; + } + if (gui.tooltip == p_control) { _gui_cancel_tooltip(); + } } void Viewport::_gui_remove_control(Control *p_control) { - if (gui.mouse_focus == p_control) { - gui.mouse_focus = NULL; + gui.mouse_focus = nullptr; gui.forced_mouse_focus = false; gui.mouse_focus_mask = 0; } if (gui.last_mouse_focus == p_control) { - gui.last_mouse_focus = NULL; + gui.last_mouse_focus = nullptr; + } + if (gui.key_focus == p_control) { + gui.key_focus = nullptr; + } + if (gui.mouse_over == p_control) { + gui.mouse_over = nullptr; + } + if (gui.drag_mouse_over == p_control) { + gui.drag_mouse_over = nullptr; + } + if (gui.tooltip == p_control) { + gui.tooltip = nullptr; } - if (gui.key_focus == p_control) - gui.key_focus = NULL; - if (gui.mouse_over == p_control) - gui.mouse_over = NULL; - if (gui.drag_mouse_over == p_control) - gui.drag_mouse_over = NULL; - if (gui.tooltip == p_control) - gui.tooltip = NULL; } void Viewport::_gui_remove_focus() { - if (gui.key_focus) { Node *f = gui.key_focus; - gui.key_focus = NULL; + gui.key_focus = nullptr; f->notification(Control::NOTIFICATION_FOCUS_EXIT, true); } } bool Viewport::_gui_control_has_focus(const Control *p_control) { - return gui.key_focus == p_control; } void Viewport::_gui_control_grab_focus(Control *p_control) { - //no need for change - if (gui.key_focus && gui.key_focus == p_control) + if (gui.key_focus && gui.key_focus == p_control) { return; + } get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, "_viewports", "_gui_remove_focus"); gui.key_focus = p_control; emit_signal("gui_focus_changed", p_control); @@ -2573,22 +2475,20 @@ void Viewport::_gui_control_grab_focus(Control *p_control) { } void Viewport::_gui_accept_event() { - gui.key_event_accepted = true; - if (is_inside_tree()) + if (is_inside_tree()) { set_input_as_handled(); + } } void Viewport::_drop_mouse_focus() { - Control *c = gui.mouse_focus; int mask = gui.mouse_focus_mask; - gui.mouse_focus = NULL; + gui.mouse_focus = nullptr; gui.forced_mouse_focus = false; gui.mouse_focus_mask = 0; for (int i = 0; i < 3; i++) { - if (mask & (1 << i)) { Ref<InputEventMouseButton> mb; mb.instance(); @@ -2602,7 +2502,6 @@ void Viewport::_drop_mouse_focus() { } void Viewport::_drop_physics_mouseover() { - physics_has_last_mousepos = false; while (physics_2d_mouseover.size()) { @@ -2628,37 +2527,32 @@ void Viewport::_drop_physics_mouseover() { } Control *Viewport::_gui_get_focus_owner() { - return gui.key_focus; } void Viewport::_gui_grab_click_focus(Control *p_control) { - gui.mouse_click_grabber = p_control; call_deferred("_post_gui_grab_click_focus"); } void Viewport::_post_gui_grab_click_focus() { - Control *focus_grabber = gui.mouse_click_grabber; if (!focus_grabber) { // Redundant grab requests were made return; } - gui.mouse_click_grabber = NULL; + gui.mouse_click_grabber = nullptr; if (gui.mouse_focus) { - - if (gui.mouse_focus == focus_grabber) + if (gui.mouse_focus == focus_grabber) { return; + } int mask = gui.mouse_focus_mask; Point2 click = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos); for (int i = 0; i < 3; i++) { - if (mask & (1 << i)) { - Ref<InputEventMouseButton> mb; mb.instance(); @@ -2676,9 +2570,7 @@ void Viewport::_post_gui_grab_click_focus() { click = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos); for (int i = 0; i < 3; i++) { - if (mask & (1 << i)) { - Ref<InputEventMouseButton> mb; mb.instance(); @@ -2696,7 +2588,6 @@ void Viewport::_post_gui_grab_click_focus() { /////////////////////////////// void Viewport::input_text(const String &p_text) { - if (gui.subwindow_focused) { gui.subwindow_focused->input_text(p_text); return; @@ -2706,8 +2597,8 @@ void Viewport::input_text(const String &p_text) { gui.key_focus->call("set_text", p_text); } } -Viewport::SubWindowResize Viewport::_sub_window_get_resize_margin(Window *p_subwindow, const Point2 &p_point) { +Viewport::SubWindowResize Viewport::_sub_window_get_resize_margin(Window *p_subwindow, const Point2 &p_point) { if (p_subwindow->get_flag(Window::FLAG_BORDERLESS)) { return SUB_WINDOW_RESIZE_DISABLED; } @@ -2770,15 +2661,13 @@ Viewport::SubWindowResize Viewport::_sub_window_get_resize_margin(Window *p_subw return SUB_WINDOW_RESIZE_DISABLED; } -bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) { +bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) { if (gui.subwindow_drag != SUB_WINDOW_DRAG_DISABLED) { - ERR_FAIL_COND_V(gui.subwindow_focused == nullptr, false); Ref<InputEventMouseButton> mb = p_event; if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) { - if (gui.subwindow_drag == SUB_WINDOW_DRAG_CLOSE) { if (gui.subwindow_drag_close_rect.has_point(mb->get_position())) { //close window @@ -2793,7 +2682,6 @@ bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) { Ref<InputEventMouseMotion> mm = p_event; if (mm.is_valid()) { - if (gui.subwindow_drag == SUB_WINDOW_DRAG_MOVE) { Vector2 diff = mm->get_position() - gui.subwindow_drag_from; Rect2i new_rect(gui.subwindow_drag_pos + diff, gui.subwindow_focused->get_size()); @@ -2819,7 +2707,6 @@ bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) { switch (gui.subwindow_resize_mode) { case SUB_WINDOW_RESIZE_TOP_LEFT: { - diff.x = MIN(diff.x, limit.x); diff.y = MIN(diff.y, limit.y); r.position += diff; @@ -2885,7 +2772,6 @@ bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) { //if the event is a mouse button, we need to check whether another window was clicked if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) { - bool click_on_window = false; for (int i = gui.sub_windows.size() - 1; i >= 0; i--) { SubWindow &sw = gui.sub_windows.write[i]; @@ -2902,7 +2788,6 @@ bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) { title_bar.size.y = title_height; if (title_bar.has_point(mb->get_position())) { - click_on_window = true; int close_h_ofs = sw.window->get_theme_constant("close_h_ofs"); @@ -2919,12 +2804,10 @@ bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) { } if (close_rect.has_point(mb->get_position())) { - gui.subwindow_drag = SUB_WINDOW_DRAG_CLOSE; gui.subwindow_drag_close_inside = true; //starts inside gui.subwindow_drag_close_rect = close_rect; } else { - gui.subwindow_drag = SUB_WINDOW_DRAG_MOVE; } @@ -2964,13 +2847,10 @@ bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) { } if (gui.subwindow_focused) { - Ref<InputEventMouseMotion> mm = p_event; if (mm.is_valid()) { - SubWindowResize resize = _sub_window_get_resize_margin(gui.subwindow_focused, mm->get_position()); if (resize != SUB_WINDOW_RESIZE_DISABLED) { - DisplayServer::CursorShape shapes[SUB_WINDOW_RESIZE_MAX] = { DisplayServer::CURSOR_ARROW, DisplayServer::CURSOR_FDIAGSIZE, @@ -3009,11 +2889,11 @@ bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) { } void Viewport::input(const Ref<InputEvent> &p_event, bool p_local_coords) { - ERR_FAIL_COND(!is_inside_tree()); - if (disable_input) + if (disable_input) { return; + } if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_a_parent_of(this)) { return; @@ -3044,11 +2924,11 @@ void Viewport::input(const Ref<InputEvent> &p_event, bool p_local_coords) { } void Viewport::unhandled_input(const Ref<InputEvent> &p_event, bool p_local_coords) { - ERR_FAIL_COND(!is_inside_tree()); - if (disable_input) + if (disable_input) { return; + } if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_a_parent_of(this)) { return; @@ -3063,14 +2943,13 @@ void Viewport::unhandled_input(const Ref<InputEvent> &p_event, bool p_local_coor get_tree()->_call_input_pause(unhandled_input_group, "_unhandled_input", ev, this); //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_input","_unhandled_input",ev); - if (!is_input_handled() && Object::cast_to<InputEventKey>(*ev) != NULL) { + if (!is_input_handled() && Object::cast_to<InputEventKey>(*ev) != nullptr) { get_tree()->_call_input_pause(unhandled_key_input_group, "_unhandled_key_input", ev, this); //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_key_input","_unhandled_key_input",ev); } if (physics_object_picking && !is_input_handled()) { - - if (InputFilter::get_singleton()->get_mouse_mode() != InputFilter::MOUSE_MODE_CAPTURED && + if (Input::get_singleton()->get_mouse_mode() != Input::MOUSE_MODE_CAPTURED && (Object::cast_to<InputEventMouseButton>(*ev) || Object::cast_to<InputEventMouseMotion>(*ev) || Object::cast_to<InputEventScreenDrag>(*ev) || @@ -3083,45 +2962,45 @@ void Viewport::unhandled_input(const Ref<InputEvent> &p_event, bool p_local_coor } } -void Viewport::set_use_own_world(bool p_world) { - - if (p_world == own_world.is_valid()) +void Viewport::set_use_own_world_3d(bool p_world_3d) { + if (p_world_3d == own_world_3d.is_valid()) { return; + } - if (is_inside_tree()) + if (is_inside_tree()) { _propagate_exit_world(this); + } - if (!p_world) { - own_world = Ref<World3D>(); - if (world.is_valid()) { - world->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Viewport::_own_world_changed)); + if (!p_world_3d) { + own_world_3d = Ref<World3D>(); + if (world_3d.is_valid()) { + world_3d->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Viewport::_own_world_3d_changed)); } } else { - if (world.is_valid()) { - own_world = world->duplicate(); - world->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Viewport::_own_world_changed)); + if (world_3d.is_valid()) { + own_world_3d = world_3d->duplicate(); + world_3d->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Viewport::_own_world_3d_changed)); } else { - own_world = Ref<World3D>(memnew(World3D)); + own_world_3d = Ref<World3D>(memnew(World3D)); } } - if (is_inside_tree()) + if (is_inside_tree()) { _propagate_enter_world(this); + } if (is_inside_tree()) { - RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world()->get_scenario()); + RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario()); } _update_listener(); } -bool Viewport::is_using_own_world() const { - - return own_world.is_valid(); +bool Viewport::is_using_own_world_3d() const { + return own_world_3d.is_valid(); } void Viewport::set_physics_object_picking(bool p_enable) { - physics_object_picking = p_enable; if (!physics_object_picking) { physics_picking_events.clear(); @@ -3129,18 +3008,15 @@ void Viewport::set_physics_object_picking(bool p_enable) { } bool Viewport::get_physics_object_picking() { - return physics_object_picking; } Vector2 Viewport::get_camera_coords(const Vector2 &p_viewport_coords) const { - Transform2D xf = get_final_transform(); return xf.xform(p_viewport_coords); } Vector2 Viewport::get_camera_rect_size() const { - return size; } @@ -3149,7 +3025,6 @@ void Viewport::set_disable_input(bool p_disable) { } bool Viewport::is_input_disabled() const { - return disable_input; } @@ -3172,48 +3047,55 @@ String Viewport::get_configuration_warning() const { void Viewport::gui_reset_canvas_sort_index() { gui.canvas_sort_index = 0; } -int Viewport::gui_get_canvas_sort_index() { +int Viewport::gui_get_canvas_sort_index() { return gui.canvas_sort_index++; } void Viewport::set_msaa(MSAA p_msaa) { - - ERR_FAIL_INDEX(p_msaa, 7); - if (msaa == p_msaa) + ERR_FAIL_INDEX(p_msaa, MSAA_MAX); + if (msaa == p_msaa) { return; + } msaa = p_msaa; RS::get_singleton()->viewport_set_msaa(viewport, RS::ViewportMSAA(p_msaa)); } Viewport::MSAA Viewport::get_msaa() const { - return msaa; } -void Viewport::set_debug_draw(DebugDraw p_debug_draw) { +void Viewport::set_screen_space_aa(ScreenSpaceAA p_screen_space_aa) { + ERR_FAIL_INDEX(p_screen_space_aa, SCREEN_SPACE_AA_MAX); + if (screen_space_aa == p_screen_space_aa) { + return; + } + screen_space_aa = p_screen_space_aa; + RS::get_singleton()->viewport_set_screen_space_aa(viewport, RS::ViewportScreenSpaceAA(p_screen_space_aa)); +} + +Viewport::ScreenSpaceAA Viewport::get_screen_space_aa() const { + return screen_space_aa; +} +void Viewport::set_debug_draw(DebugDraw p_debug_draw) { debug_draw = p_debug_draw; RS::get_singleton()->viewport_set_debug_draw(viewport, RS::ViewportDebugDraw(p_debug_draw)); } Viewport::DebugDraw Viewport::get_debug_draw() const { - return debug_draw; } int Viewport::get_render_info(RenderInfo p_info) { - return RS::get_singleton()->viewport_get_render_info(viewport, RS::ViewportRenderInfo(p_info)); } void Viewport::set_snap_controls_to_pixels(bool p_enable) { - snap_controls_to_pixels = p_enable; } bool Viewport::is_snap_controls_to_pixels_enabled() const { - return snap_controls_to_pixels; } @@ -3289,6 +3171,7 @@ void Viewport::set_default_canvas_item_texture_repeat(DefaultCanvasItemTextureRe default_canvas_item_texture_repeat = p_repeat; _propagate_update_default_repeat(this); } + Viewport::DefaultCanvasItemTextureRepeat Viewport::get_default_canvas_item_texture_repeat() const { return default_canvas_item_texture_repeat; } @@ -3331,9 +3214,11 @@ Viewport *Viewport::get_parent_viewport() const { void Viewport::set_embed_subwindows_hint(bool p_embed) { gui.embed_subwindows_hint = p_embed; } + bool Viewport::get_embed_subwindows_hint() const { return gui.embed_subwindows_hint; } + bool Viewport::is_embedding_subwindows() const { return gui.embed_subwindows_hint; } @@ -3355,13 +3240,12 @@ void Viewport::pass_mouse_focus_to(Viewport *p_viewport, Control *p_control) { } void Viewport::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_world_2d", "world_2d"), &Viewport::set_world_2d); ClassDB::bind_method(D_METHOD("get_world_2d"), &Viewport::get_world_2d); ClassDB::bind_method(D_METHOD("find_world_2d"), &Viewport::find_world_2d); - ClassDB::bind_method(D_METHOD("set_world", "world"), &Viewport::set_world); - ClassDB::bind_method(D_METHOD("get_world"), &Viewport::get_world); - ClassDB::bind_method(D_METHOD("find_world"), &Viewport::find_world); + ClassDB::bind_method(D_METHOD("set_world_3d", "world_3d"), &Viewport::set_world_3d); + ClassDB::bind_method(D_METHOD("get_world_3d"), &Viewport::get_world_3d); + ClassDB::bind_method(D_METHOD("find_world_3d"), &Viewport::find_world_3d); ClassDB::bind_method(D_METHOD("set_canvas_transform", "xform"), &Viewport::set_canvas_transform); ClassDB::bind_method(D_METHOD("get_canvas_transform"), &Viewport::get_canvas_transform); @@ -3377,6 +3261,9 @@ void Viewport::_bind_methods() { ClassDB::bind_method(D_METHOD("set_msaa", "msaa"), &Viewport::set_msaa); ClassDB::bind_method(D_METHOD("get_msaa"), &Viewport::get_msaa); + ClassDB::bind_method(D_METHOD("set_screen_space_aa", "screen_space_aa"), &Viewport::set_screen_space_aa); + ClassDB::bind_method(D_METHOD("get_screen_space_aa"), &Viewport::get_screen_space_aa); + ClassDB::bind_method(D_METHOD("set_debug_draw", "debug_draw"), &Viewport::set_debug_draw); ClassDB::bind_method(D_METHOD("get_debug_draw"), &Viewport::get_debug_draw); @@ -3394,8 +3281,8 @@ void Viewport::_bind_methods() { ClassDB::bind_method(D_METHOD("update_worlds"), &Viewport::update_worlds); - ClassDB::bind_method(D_METHOD("set_use_own_world", "enable"), &Viewport::set_use_own_world); - ClassDB::bind_method(D_METHOD("is_using_own_world"), &Viewport::is_using_own_world); + ClassDB::bind_method(D_METHOD("set_use_own_world_3d", "enable"), &Viewport::set_use_own_world_3d); + ClassDB::bind_method(D_METHOD("is_using_own_world_3d"), &Viewport::is_using_own_world_3d); ClassDB::bind_method(D_METHOD("get_camera"), &Viewport::get_camera); @@ -3443,13 +3330,14 @@ void Viewport::_bind_methods() { ClassDB::bind_method(D_METHOD("set_default_canvas_item_texture_repeat", "mode"), &Viewport::set_default_canvas_item_texture_repeat); ClassDB::bind_method(D_METHOD("get_default_canvas_item_texture_repeat"), &Viewport::get_default_canvas_item_texture_repeat); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "own_world"), "set_use_own_world", "is_using_own_world"); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world", PROPERTY_HINT_RESOURCE_TYPE, "World"), "set_world", "get_world"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "own_world_3d"), "set_use_own_world_3d", "is_using_own_world_3d"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_3d", PROPERTY_HINT_RESOURCE_TYPE, "World3D"), "set_world_3d", "get_world_3d"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_2d", PROPERTY_HINT_RESOURCE_TYPE, "World2D", 0), "set_world_2d", "get_world_2d"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent_bg"), "set_transparent_background", "has_transparent_background"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "handle_input_locally"), "set_handle_input_locally", "is_handling_input_locally"); ADD_GROUP("Rendering", ""); ADD_PROPERTY(PropertyInfo(Variant::INT, "msaa", PROPERTY_HINT_ENUM, "Disabled,2x,4x,8x,16x,AndroidVR 2x,AndroidVR 4x"), "set_msaa", "get_msaa"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "screen_space_aa", PROPERTY_HINT_ENUM, "Disabled,FXAA"), "set_screen_space_aa", "get_screen_space_aa"); ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_draw", PROPERTY_HINT_ENUM, "Disabled,Unshaded,Overdraw,Wireframe"), "set_debug_draw", "get_debug_draw"); ADD_GROUP("Canvas Items", "canvas_item_"); ADD_PROPERTY(PropertyInfo(Variant::INT, "canvas_item_default_texture_filter", PROPERTY_HINT_ENUM, "Nearest,Linear,MipmapLinear,MipmapNearest"), "set_default_canvas_item_texture_filter", "get_default_canvas_item_texture_filter"); @@ -3484,6 +3372,17 @@ void Viewport::_bind_methods() { BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_1024); BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_MAX); + BIND_ENUM_CONSTANT(MSAA_DISABLED); + BIND_ENUM_CONSTANT(MSAA_2X); + BIND_ENUM_CONSTANT(MSAA_4X); + BIND_ENUM_CONSTANT(MSAA_8X); + BIND_ENUM_CONSTANT(MSAA_16X); + BIND_ENUM_CONSTANT(MSAA_MAX); + + BIND_ENUM_CONSTANT(SCREEN_SPACE_AA_DISABLED); + BIND_ENUM_CONSTANT(SCREEN_SPACE_AA_FXAA); + BIND_ENUM_CONSTANT(SCREEN_SPACE_AA_MAX); + BIND_ENUM_CONSTANT(RENDER_INFO_OBJECTS_IN_FRAME); BIND_ENUM_CONSTANT(RENDER_INFO_VERTICES_IN_FRAME); BIND_ENUM_CONSTANT(RENDER_INFO_MATERIAL_CHANGES_IN_FRAME); @@ -3494,9 +3393,10 @@ void Viewport::_bind_methods() { BIND_ENUM_CONSTANT(DEBUG_DRAW_DISABLED); BIND_ENUM_CONSTANT(DEBUG_DRAW_UNSHADED); + BIND_ENUM_CONSTANT(DEBUG_DRAW_LIGHTING); BIND_ENUM_CONSTANT(DEBUG_DRAW_OVERDRAW); BIND_ENUM_CONSTANT(DEBUG_DRAW_WIREFRAME); - + BIND_ENUM_CONSTANT(DEBUG_DRAW_NORMAL_BUFFER); BIND_ENUM_CONSTANT(DEBUG_DRAW_GI_PROBE_ALBEDO); BIND_ENUM_CONSTANT(DEBUG_DRAW_GI_PROBE_LIGHTING); BIND_ENUM_CONSTANT(DEBUG_DRAW_GI_PROBE_EMISSION); @@ -3504,19 +3404,16 @@ void Viewport::_bind_methods() { BIND_ENUM_CONSTANT(DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS); BIND_ENUM_CONSTANT(DEBUG_DRAW_SCENE_LUMINANCE); BIND_ENUM_CONSTANT(DEBUG_DRAW_SSAO); - - BIND_ENUM_CONSTANT(MSAA_DISABLED); - BIND_ENUM_CONSTANT(MSAA_2X); - BIND_ENUM_CONSTANT(MSAA_4X); - BIND_ENUM_CONSTANT(MSAA_8X); - BIND_ENUM_CONSTANT(MSAA_16X); + BIND_ENUM_CONSTANT(DEBUG_DRAW_ROUGHNESS_LIMITER); + BIND_ENUM_CONSTANT(DEBUG_DRAW_PSSM_SPLITS); + BIND_ENUM_CONSTANT(DEBUG_DRAW_DECAL_ATLAS); BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST); BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR); BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS); BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS); - BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_MAX); + BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_ENABLED); BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_MIRROR); @@ -3524,7 +3421,6 @@ void Viewport::_bind_methods() { } Viewport::Viewport() { - world_2d = Ref<World2D>(memnew(World2D)); viewport = RenderingServer::get_singleton()->viewport_create(); @@ -3539,11 +3435,11 @@ Viewport::Viewport() { //internal_listener_2d = SpatialSound2DServer::get_singleton()->listener_create(); audio_listener_2d = false; transparent_bg = false; - parent = NULL; - listener = NULL; - camera = NULL; + parent = nullptr; + listener = nullptr; + camera = nullptr; override_canvas_transform = false; - canvas_layers.insert(NULL); // This eases picking code (interpreted as the canvas of the Viewport) + canvas_layers.insert(nullptr); // This eases picking code (interpreted as the canvas of the Viewport) gen_mipmaps = false; @@ -3577,20 +3473,20 @@ Viewport::Viewport() { gui.tooltip_delay = GLOBAL_DEF("gui/timers/tooltip_delay_sec", 0.5); ProjectSettings::get_singleton()->set_custom_property_info("gui/timers/tooltip_delay_sec", PropertyInfo(Variant::FLOAT, "gui/timers/tooltip_delay_sec", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater")); // No negative numbers - gui.tooltip = NULL; - gui.tooltip_label = NULL; - gui.drag_preview = NULL; + gui.tooltip = nullptr; + gui.tooltip_label = nullptr; + gui.drag_preview = nullptr; gui.drag_attempted = false; gui.canvas_sort_index = 0; gui.roots_order_dirty = false; - gui.mouse_focus = NULL; + gui.mouse_focus = nullptr; gui.forced_mouse_focus = false; - gui.last_mouse_focus = NULL; + gui.last_mouse_focus = nullptr; gui.subwindow_focused = nullptr; gui.subwindow_drag = SUB_WINDOW_DRAG_DISABLED; msaa = MSAA_DISABLED; - + screen_space_aa = SCREEN_SPACE_AA_DISABLED; debug_draw = DEBUG_DRAW_DISABLED; snap_controls_to_pixels = true; @@ -3609,51 +3505,69 @@ Viewport::Viewport() { } Viewport::~Viewport() { - //erase itself from viewport textures for (Set<ViewportTexture *>::Element *E = viewport_textures.front(); E; E = E->next()) { - E->get()->vp = NULL; + E->get()->vp = nullptr; } RenderingServer::get_singleton()->free(viewport); } ///////////////////////////////// -void SubViewport::set_use_arvr(bool p_use_arvr) { - arvr = p_use_arvr; +void SubViewport::set_use_xr(bool p_use_xr) { + xr = p_use_xr; - RS::get_singleton()->viewport_set_use_arvr(get_viewport_rid(), arvr); + RS::get_singleton()->viewport_set_use_xr(get_viewport_rid(), xr); } -bool SubViewport::is_using_arvr() { - return arvr; +bool SubViewport::is_using_xr() { + return xr; } void SubViewport::set_size(const Size2i &p_size) { - _set_size(p_size, Size2i(), Rect2i(), Transform2D(), true); + _set_size(p_size, _get_size_2d_override(), Rect2i(), _stretch_transform(), true); } + Size2i SubViewport::get_size() const { return _get_size(); } -void SubViewport::set_update_mode(UpdateMode p_mode) { +void SubViewport::set_size_2d_override(const Size2i &p_size) { + _set_size(_get_size(), p_size, Rect2i(), _stretch_transform(), true); +} + +Size2i SubViewport::get_size_2d_override() const { + return _get_size_2d_override(); +} +void SubViewport::set_size_2d_override_stretch(bool p_enable) { + if (p_enable == size_2d_override_stretch) { + return; + } + + size_2d_override_stretch = p_enable; + _set_size(_get_size(), _get_size_2d_override(), Rect2i(), _stretch_transform(), true); +} + +bool SubViewport::is_size_2d_override_stretch_enabled() const { + return size_2d_override_stretch; +} + +void SubViewport::set_update_mode(UpdateMode p_mode) { update_mode = p_mode; RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::ViewportUpdateMode(p_mode)); } -SubViewport::UpdateMode SubViewport::get_update_mode() const { +SubViewport::UpdateMode SubViewport::get_update_mode() const { return update_mode; } void SubViewport::set_clear_mode(ClearMode p_mode) { - clear_mode = p_mode; RS::get_singleton()->viewport_set_clear_mode(get_viewport_rid(), RS::ViewportClearMode(p_mode)); } SubViewport::ClearMode SubViewport::get_clear_mode() const { - return clear_mode; } @@ -3661,8 +3575,18 @@ DisplayServer::WindowID SubViewport::get_window_id() const { return DisplayServer::INVALID_WINDOW_ID; } -void SubViewport::_notification(int p_what) { +Transform2D SubViewport::_stretch_transform() { + Transform2D transform = Transform2D(); + Size2i view_size_2d_override = _get_size_2d_override(); + if (size_2d_override_stretch && view_size_2d_override.width > 0 && view_size_2d_override.height > 0) { + Size2 scale = _get_size() / view_size_2d_override; + transform.scale(scale); + } + return transform; +} + +void SubViewport::_notification(int p_what) { if (p_what == NOTIFICATION_ENTER_TREE) { RS::get_singleton()->viewport_set_active(get_viewport_rid(), true); } @@ -3672,36 +3596,46 @@ void SubViewport::_notification(int p_what) { } void SubViewport::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_use_arvr", "use"), &SubViewport::set_use_arvr); - ClassDB::bind_method(D_METHOD("is_using_arvr"), &SubViewport::is_using_arvr); + ClassDB::bind_method(D_METHOD("set_use_xr", "use"), &SubViewport::set_use_xr); + ClassDB::bind_method(D_METHOD("is_using_xr"), &SubViewport::is_using_xr); ClassDB::bind_method(D_METHOD("set_size", "size"), &SubViewport::set_size); ClassDB::bind_method(D_METHOD("get_size"), &SubViewport::get_size); + ClassDB::bind_method(D_METHOD("set_size_2d_override", "size"), &SubViewport::set_size_2d_override); + ClassDB::bind_method(D_METHOD("get_size_2d_override"), &SubViewport::get_size_2d_override); + + ClassDB::bind_method(D_METHOD("set_size_2d_override_stretch", "enable"), &SubViewport::set_size_2d_override_stretch); + ClassDB::bind_method(D_METHOD("is_size_2d_override_stretch_enabled"), &SubViewport::is_size_2d_override_stretch_enabled); + ClassDB::bind_method(D_METHOD("set_update_mode", "mode"), &SubViewport::set_update_mode); ClassDB::bind_method(D_METHOD("get_update_mode"), &SubViewport::get_update_mode); ClassDB::bind_method(D_METHOD("set_clear_mode", "mode"), &SubViewport::set_clear_mode); ClassDB::bind_method(D_METHOD("get_clear_mode"), &SubViewport::get_clear_mode); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "arvr"), "set_use_arvr", "is_using_arvr"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "xr"), "set_use_xr", "is_using_xr"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size_2d_override"), "set_size_2d_override", "get_size_2d_override"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "size_2d_override_stretch"), "set_size_2d_override_stretch", "is_size_2d_override_stretch_enabled"); ADD_GROUP("Render Target", "render_target_"); ADD_PROPERTY(PropertyInfo(Variant::INT, "render_target_clear_mode", PROPERTY_HINT_ENUM, "Always,Never,Next Frame"), "set_clear_mode", "get_clear_mode"); ADD_PROPERTY(PropertyInfo(Variant::INT, "render_target_update_mode", PROPERTY_HINT_ENUM, "Disabled,Once,When Visible,Always"), "set_update_mode", "get_update_mode"); + BIND_ENUM_CONSTANT(CLEAR_MODE_ALWAYS); + BIND_ENUM_CONSTANT(CLEAR_MODE_NEVER); + BIND_ENUM_CONSTANT(CLEAR_MODE_ONLY_NEXT_FRAME); + BIND_ENUM_CONSTANT(UPDATE_DISABLED); BIND_ENUM_CONSTANT(UPDATE_ONCE); BIND_ENUM_CONSTANT(UPDATE_WHEN_VISIBLE); BIND_ENUM_CONSTANT(UPDATE_WHEN_PARENT_VISIBLE); BIND_ENUM_CONSTANT(UPDATE_ALWAYS); - - BIND_ENUM_CONSTANT(CLEAR_MODE_ALWAYS); - BIND_ENUM_CONSTANT(CLEAR_MODE_NEVER); - BIND_ENUM_CONSTANT(CLEAR_MODE_ONLY_NEXT_FRAME); } SubViewport::SubViewport() { - arvr = false; + xr = false; + size_2d_override_stretch = false; update_mode = UPDATE_WHEN_VISIBLE; clear_mode = CLEAR_MODE_ALWAYS; } |