From 9bdb8b6bce7cb109aa54d0845b73e5daeb9740bd Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 30 Aug 2022 15:47:22 +0200 Subject: [PATCH] Now only one override is allowed per World, as there is really no reason to have support for more than one per world pair. This fixes lots of issues with the system. --- scene/main/viewport.cpp | 61 ++++++++++----------- scene/main/world.cpp | 115 ++++++++++++---------------------------- scene/main/world.h | 19 +++++-- 3 files changed, 81 insertions(+), 114 deletions(-) diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index bbc57af41..e7a194bb5 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -678,8 +678,8 @@ void Viewport::_process_picking(bool p_ignore_paused) { } RID Viewport::get_viewport_rid() const { - if (_override_world) { - return _override_world->get_viewport_rid(); + if (_overriding_world) { + return _overriding_world->get_viewport_rid(); } return viewport; @@ -712,8 +712,8 @@ void Viewport::set_size(const Size2 &p_size) { } Rect2 Viewport::get_visible_rect() const { - if (_override_world) { - return _override_world->get_visible_rect(); + if (_overriding_world) { + return _overriding_world->get_visible_rect(); } Rect2 r; @@ -836,8 +836,9 @@ void Viewport::_on_after_world_override_changed() { } void Viewport::enable_canvas_transform_override(bool p_enable) { - if (_override_world) { - _override_world->enable_canvas_transform_override(p_enable); + if (_overriding_world) { + _overriding_world->enable_canvas_transform_override(p_enable); + return; } if (override_canvas_transform == p_enable) { @@ -853,16 +854,17 @@ void Viewport::enable_canvas_transform_override(bool p_enable) { } bool Viewport::is_canvas_transform_override_enbled() const { - if (_override_world) { - return _override_world->is_canvas_transform_override_enbled(); + if (_overriding_world) { + return _overriding_world->is_canvas_transform_override_enbled(); } return override_canvas_transform; } void Viewport::set_canvas_transform_override(const Transform2D &p_transform) { - if (_override_world) { - _override_world->set_canvas_transform_override(p_transform); + if (_overriding_world) { + _overriding_world->set_canvas_transform_override(p_transform); + return; } if (canvas_transform_override == p_transform) { @@ -876,20 +878,17 @@ void Viewport::set_canvas_transform_override(const Transform2D &p_transform) { } Transform2D Viewport::get_canvas_transform_override() const { - if (_override_world) { - return _override_world->get_canvas_transform_override(); - } - - if (_override_world) { - return _override_world->get_canvas_transform_override(); + if (_overriding_world) { + return _overriding_world->get_canvas_transform_override(); } return canvas_transform_override; } void Viewport::set_canvas_transform(const Transform2D &p_transform) { - if (_override_world) { - _override_world->set_canvas_transform(p_transform); + if (_overriding_world) { + _overriding_world->set_canvas_transform(p_transform); + return; } canvas_transform = p_transform; @@ -900,8 +899,8 @@ void Viewport::set_canvas_transform(const Transform2D &p_transform) { } Transform2D Viewport::get_canvas_transform() const { - if (_override_world) { - return _override_world->get_canvas_transform(); + if (_overriding_world) { + return _overriding_world->get_canvas_transform(); } return canvas_transform; @@ -914,8 +913,9 @@ void Viewport::_update_global_transform() { } void Viewport::set_global_canvas_transform(const Transform2D &p_transform) { - if (_override_world) { - _override_world->set_global_canvas_transform(p_transform); + if (_overriding_world) { + _overriding_world->set_global_canvas_transform(p_transform); + return; } global_canvas_transform = p_transform; @@ -924,8 +924,8 @@ void Viewport::set_global_canvas_transform(const Transform2D &p_transform) { } Transform2D Viewport::get_global_canvas_transform() const { - if (_override_world) { - return _override_world->get_global_canvas_transform(); + if (_overriding_world) { + return _overriding_world->get_global_canvas_transform(); } return global_canvas_transform; @@ -1148,8 +1148,8 @@ void Viewport::set_camera_override_orthogonal(float p_size, float p_z_near, floa } Transform2D Viewport::get_final_transform() const { - if (_override_world) { - return _override_world->get_final_transform(); + if (_overriding_world) { + return _overriding_world->get_final_transform(); } return stretch_transform * global_canvas_transform; @@ -2889,15 +2889,16 @@ String Viewport::get_configuration_warning() const { } void Viewport::gui_reset_canvas_sort_index() { - if (_override_world) { - _override_world->gui_reset_canvas_sort_index(); + if (_overriding_world) { + _overriding_world->gui_reset_canvas_sort_index(); + return; } gui.canvas_sort_index = 0; } int Viewport::gui_get_canvas_sort_index() { - if (_override_world) { - return _override_world->gui_get_canvas_sort_index(); + if (_overriding_world) { + return _overriding_world->gui_get_canvas_sort_index(); } return gui.canvas_sort_index++; diff --git a/scene/main/world.cpp b/scene/main/world.cpp index 366f5172b..afaf66fed 100644 --- a/scene/main/world.cpp +++ b/scene/main/world.cpp @@ -210,13 +210,11 @@ void World::set_override_world(World *p_world) { return; } - World *old_world = get_override_world(); - Ref old_world_3d = find_world_3d(); - _on_before_world_override_changed(); - if (old_world) { - old_world->_remove_overridden_world(this); + if (_overriding_world) { + _overriding_world->set_override_world(NULL); + _overriding_world = NULL; } _clear_override_cameras(); @@ -224,25 +222,7 @@ void World::set_override_world(World *p_world) { _override_world = p_world; if (_override_world) { - _override_world->_add_overridden_world(this); - } - - if (old_world_3d.is_valid()) { - Ref new_world_3d = find_world_3d(); - - if (old_world_3d != new_world_3d) { - if (new_world_3d.is_valid()) { - old_world_3d->move_cameras_into(new_world_3d); - - } else { - ERR_PRINT("!new_world_3d.is_valid()"); - } - } - } else { - ERR_PRINT("!old_world_3d.is_valid()"); - } - - if (_override_world) { + _override_world->_overriding_world = this; _add_override_cameras_into(_override_world); } @@ -257,97 +237,97 @@ void World::set_override_world_bind(Node *p_world) { } void World::gui_reset_canvas_sort_index() { - if (_override_world) { - _override_world->gui_reset_canvas_sort_index(); + if (_overriding_world) { + _overriding_world->gui_reset_canvas_sort_index(); } } int World::gui_get_canvas_sort_index() { - if (_override_world) { - return _override_world->gui_get_canvas_sort_index(); + if (_overriding_world) { + return _overriding_world->gui_get_canvas_sort_index(); } return 0; } void World::enable_canvas_transform_override(bool p_enable) { - if (_override_world) { - _override_world->enable_canvas_transform_override(p_enable); + if (_overriding_world) { + _overriding_world->enable_canvas_transform_override(p_enable); } } bool World::is_canvas_transform_override_enbled() const { - if (_override_world) { - return _override_world->is_canvas_transform_override_enbled(); + if (_overriding_world) { + return _overriding_world->is_canvas_transform_override_enbled(); } return false; } void World::set_canvas_transform_override(const Transform2D &p_transform) { - if (_override_world) { - _override_world->set_canvas_transform_override(p_transform); + if (_overriding_world) { + _overriding_world->set_canvas_transform_override(p_transform); } } Transform2D World::get_canvas_transform_override() const { - if (_override_world) { - return _override_world->get_canvas_transform_override(); + if (_overriding_world) { + return _overriding_world->get_canvas_transform_override(); } return Transform2D(); } void World::set_canvas_transform(const Transform2D &p_transform) { - if (_override_world) { - _override_world->set_canvas_transform(p_transform); + if (_overriding_world) { + _overriding_world->set_canvas_transform(p_transform); } } Transform2D World::get_canvas_transform() const { - if (_override_world) { - return _override_world->get_canvas_transform(); + if (_overriding_world) { + return _overriding_world->get_canvas_transform(); } return Transform2D(); } void World::set_global_canvas_transform(const Transform2D &p_transform) { - if (_override_world) { - _override_world->set_global_canvas_transform(p_transform); + if (_overriding_world) { + _overriding_world->set_global_canvas_transform(p_transform); } } Transform2D World::get_global_canvas_transform() const { - if (_override_world) { - return _override_world->get_global_canvas_transform(); + if (_overriding_world) { + return _overriding_world->get_global_canvas_transform(); } return Transform2D(); } Transform2D World::get_final_transform() const { - if (_override_world) { - return _override_world->get_final_transform(); + if (_overriding_world) { + return _overriding_world->get_final_transform(); } return Transform2D(); } Rect2 World::get_visible_rect() const { - if (_override_world) { - return _override_world->get_visible_rect(); + if (_overriding_world) { + return _overriding_world->get_visible_rect(); } return Rect2(); } RID World::get_viewport_rid() const { - if (_override_world) { - return _override_world->get_viewport_rid(); + if (_overriding_world) { + return _overriding_world->get_viewport_rid(); } return RID(); } Vector2 World::get_camera_coords(const Vector2 &p_viewport_coords) const { - if (_override_world) { - return _override_world->get_camera_coords(p_viewport_coords); + if (_overriding_world) { + return _overriding_world->get_camera_coords(p_viewport_coords); } Transform2D xf = get_final_transform(); @@ -355,8 +335,8 @@ Vector2 World::get_camera_coords(const Vector2 &p_viewport_coords) const { } Vector2 World::get_camera_rect_size() const { - if (_override_world) { - return _override_world->get_camera_rect_size(); + if (_overriding_world) { + return _overriding_world->get_camera_rect_size(); } return size; @@ -448,6 +428,7 @@ void World::_add_override_cameras_into(World *p_from) { World::World() { world_2d = Ref(memnew(World2D)); _override_world = NULL; + _overriding_world = NULL; _override_in_parent_viewport = false; camera = NULL; _own_active_camera = NULL; @@ -574,26 +555,6 @@ void World::_propagate_exit_world(Node *p_node) { } } -void World::_add_overridden_world(World *p_world) { - ERR_FAIL_COND(!p_world); - - _overriding_worlds.push_back(p_world); -} -void World::_remove_overridden_world(World *p_world) { - ERR_FAIL_COND(!p_world); - - for (int i = 0; i < _overriding_worlds.size(); ++i) { - World *w = _overriding_worlds[i]; - - if (w == p_world) { - _overriding_worlds.remove(i); - return; - } - } - - ERR_FAIL(); -} - void World::_own_world_3d_changed() { ERR_FAIL_COND(world_3d.is_null()); ERR_FAIL_COND(own_world_3d.is_null()); @@ -645,12 +606,6 @@ void World::_notification(int p_what) { case NOTIFICATION_EXIT_TREE: { if (!Engine::get_singleton()->is_editor_hint()) { set_override_world(NULL); - - while (_overriding_worlds.size() > 0) { - World *w = _overriding_worlds[0]; - - w->set_override_world(NULL); - } } } break; } diff --git a/scene/main/world.h b/scene/main/world.h index 3d96fc4a9..62d762e4a 100644 --- a/scene/main/world.h +++ b/scene/main/world.h @@ -28,6 +28,7 @@ public: bool get_override_in_parent_viewport(); void set_override_in_parent_viewport(const bool value); + //If a World has an override these are overridden Ref find_world_2d() const; Ref find_world_3d() const; Ref find_world_2d_no_override() const; @@ -38,6 +39,19 @@ public: void set_override_world(World *p_world); void set_override_world_bind(Node *p_world); + // If a world has an override the override defers these to the overridden + // As these are called by the child Nodes of the overriding world + + // Viewport root + // -- World w1 + // ---- PanelContainer pc + + // w1 is set as override for the root. + // pc's CanvasItem will use the canvas in w1, and will likely call gui_reset_canvas_sort_index() in w1 at least some point. + // What we want, is to defer this call to the root viewport, as that is the class that is able to deal with it. + // This is what makes overrides transparent to nodes like the Camera, as they can just use the world that they are a child of + // and overrides can be handled for them in the backgorund. + virtual void gui_reset_canvas_sort_index(); virtual int gui_get_canvas_sort_index(); @@ -87,9 +101,6 @@ public: virtual void _update_listener_2d(); protected: - void _add_overridden_world(World *p_world); - void _remove_overridden_world(World *p_world); - virtual void _own_world_3d_changed(); virtual void _on_set_use_own_world_3d(bool p_use_own_world_3d); virtual void _on_set_world_3d(const Ref &p_old_world); @@ -113,8 +124,8 @@ protected: bool _override_in_parent_viewport; World *_override_world; + World *_overriding_world; - Vector _overriding_worlds; Vector _override_cameras; Camera *camera;