Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Organize 2D audio, camera, and physics in Viewport #79183

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
298 changes: 149 additions & 149 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,32 +1068,6 @@ void Viewport::canvas_parent_mark_dirty(Node *p_node) {
}
}

void Viewport::_update_audio_listener_2d() {
if (AudioServer::get_singleton()) {
AudioServer::get_singleton()->notify_listener_changed();
}
}

void Viewport::set_as_audio_listener_2d(bool p_enable) {
ERR_MAIN_THREAD_GUARD;
if (p_enable == is_audio_listener_2d_enabled) {
return;
}

is_audio_listener_2d_enabled = p_enable;
_update_audio_listener_2d();
}

bool Viewport::is_audio_listener_2d() const {
ERR_READ_THREAD_GUARD_V(false);
return is_audio_listener_2d_enabled;
}

AudioListener2D *Viewport::get_audio_listener_2d() const {
ERR_READ_THREAD_GUARD_V(nullptr);
return audio_listener_2d;
}

void Viewport::enable_canvas_transform_override(bool p_enable) {
ERR_MAIN_THREAD_GUARD;
if (override_canvas_transform == p_enable) {
Expand Down Expand Up @@ -1162,25 +1136,6 @@ Transform2D Viewport::get_global_canvas_transform() const {
return global_canvas_transform;
}

void Viewport::_camera_2d_set(Camera2D *p_camera_2d) {
camera_2d = p_camera_2d;
}

void Viewport::_audio_listener_2d_set(AudioListener2D *p_listener) {
if (audio_listener_2d == p_listener) {
return;
} else if (audio_listener_2d) {
audio_listener_2d->clear_current();
}
audio_listener_2d = p_listener;
}

void Viewport::_audio_listener_2d_remove(AudioListener2D *p_listener) {
if (audio_listener_2d == p_listener) {
audio_listener_2d = nullptr;
}
}

void Viewport::_canvas_layer_add(CanvasLayer *p_canvas_layer) {
canvas_layers.insert(p_canvas_layer);
}
Expand Down Expand Up @@ -1272,40 +1227,11 @@ Ref<World2D> Viewport::get_world_2d() const {
return world_2d;
}

Camera2D *Viewport::get_camera_2d() const {
ERR_READ_THREAD_GUARD_V(nullptr);
return camera_2d;
}

Transform2D Viewport::get_final_transform() const {
ERR_READ_THREAD_GUARD_V(Transform2D());
return stretch_transform * global_canvas_transform;
}

void Viewport::assign_next_enabled_camera_2d(const StringName &p_camera_group) {
ERR_MAIN_THREAD_GUARD;
List<Node *> camera_list;
get_tree()->get_nodes_in_group(p_camera_group, &camera_list);

Camera2D *new_camera = nullptr;
for (Node *E : camera_list) {
Camera2D *cam = Object::cast_to<Camera2D>(E);
if (!cam) {
continue; // Non-camera node (e.g. ParallaxBackground).
}

if (cam->is_enabled()) {
new_camera = cam;
break;
}
}

_camera_2d_set(new_camera);
if (!camera_2d) {
set_canvas_transform(Transform2D());
}
}

void Viewport::_update_canvas_items(Node *p_node) {
if (p_node != this) {
Window *w = Object::cast_to<Window>(p_node);
Expand Down Expand Up @@ -2644,76 +2570,6 @@ void Viewport::_drop_physics_mouseover(bool p_paused_only) {
#endif // _3D_DISABLED
}

void Viewport::_cleanup_mouseover_colliders(bool p_clean_all_frames, bool p_paused_only, uint64_t p_frame_reference) {
List<ObjectID> to_erase;
List<ObjectID> to_mouse_exit;

for (const KeyValue<ObjectID, uint64_t> &E : physics_2d_mouseover) {
if (!p_clean_all_frames && E.value == p_frame_reference) {
continue;
}

Object *o = ObjectDB::get_instance(E.key);
if (o) {
CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
if (co && co->is_inside_tree()) {
if (p_clean_all_frames && p_paused_only && co->can_process()) {
continue;
}
to_mouse_exit.push_back(E.key);
}
}
to_erase.push_back(E.key);
}

while (to_erase.size()) {
physics_2d_mouseover.erase(to_erase.front()->get());
to_erase.pop_front();
}

// Per-shape.
List<Pair<ObjectID, int>> shapes_to_erase;
List<Pair<ObjectID, int>> shapes_to_mouse_exit;

for (KeyValue<Pair<ObjectID, int>, uint64_t> &E : physics_2d_shape_mouseover) {
if (!p_clean_all_frames && E.value == p_frame_reference) {
continue;
}

Object *o = ObjectDB::get_instance(E.key.first);
if (o) {
CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
if (co && co->is_inside_tree()) {
if (p_clean_all_frames && p_paused_only && co->can_process()) {
continue;
}
shapes_to_mouse_exit.push_back(E.key);
}
}
shapes_to_erase.push_back(E.key);
}

while (shapes_to_erase.size()) {
physics_2d_shape_mouseover.erase(shapes_to_erase.front()->get());
shapes_to_erase.pop_front();
}

while (to_mouse_exit.size()) {
Object *o = ObjectDB::get_instance(to_mouse_exit.front()->get());
CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
co->_mouse_exit();
to_mouse_exit.pop_front();
}

while (shapes_to_mouse_exit.size()) {
Pair<ObjectID, int> e = shapes_to_mouse_exit.front()->get();
Object *o = ObjectDB::get_instance(e.first);
CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
co->_mouse_shape_exit(e.second);
shapes_to_mouse_exit.pop_front();
}
}

void Viewport::_gui_grab_click_focus(Control *p_control) {
gui.mouse_click_grabber = p_control;
callable_mp(this, &Viewport::_post_gui_grab_click_focus).call_deferred();
Expand Down Expand Up @@ -4065,6 +3921,150 @@ bool Viewport::get_canvas_cull_mask_bit(uint32_t p_layer) const {
return (canvas_cull_mask & (1 << p_layer));
}

void Viewport::_update_audio_listener_2d() {
if (AudioServer::get_singleton()) {
AudioServer::get_singleton()->notify_listener_changed();
}
}

void Viewport::_audio_listener_2d_set(AudioListener2D *p_audio_listener) {
if (audio_listener_2d == p_audio_listener) {
return;
} else if (audio_listener_2d) {
audio_listener_2d->clear_current();
}
audio_listener_2d = p_audio_listener;
}

void Viewport::_audio_listener_2d_remove(AudioListener2D *p_audio_listener) {
if (audio_listener_2d == p_audio_listener) {
audio_listener_2d = nullptr;
}
}

void Viewport::_camera_2d_set(Camera2D *p_camera_2d) {
camera_2d = p_camera_2d;
}

void Viewport::_cleanup_mouseover_colliders(bool p_clean_all_frames, bool p_paused_only, uint64_t p_frame_reference) {
List<ObjectID> to_erase;
List<ObjectID> to_mouse_exit;

for (const KeyValue<ObjectID, uint64_t> &E : physics_2d_mouseover) {
if (!p_clean_all_frames && E.value == p_frame_reference) {
continue;
}

Object *o = ObjectDB::get_instance(E.key);
if (o) {
CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
if (co && co->is_inside_tree()) {
if (p_clean_all_frames && p_paused_only && co->can_process()) {
continue;
}
to_mouse_exit.push_back(E.key);
}
}
to_erase.push_back(E.key);
}

while (to_erase.size()) {
physics_2d_mouseover.erase(to_erase.front()->get());
to_erase.pop_front();
}

// Per-shape.
List<Pair<ObjectID, int>> shapes_to_erase;
List<Pair<ObjectID, int>> shapes_to_mouse_exit;

for (KeyValue<Pair<ObjectID, int>, uint64_t> &E : physics_2d_shape_mouseover) {
if (!p_clean_all_frames && E.value == p_frame_reference) {
continue;
}

Object *o = ObjectDB::get_instance(E.key.first);
if (o) {
CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
if (co && co->is_inside_tree()) {
if (p_clean_all_frames && p_paused_only && co->can_process()) {
continue;
}
shapes_to_mouse_exit.push_back(E.key);
}
}
shapes_to_erase.push_back(E.key);
}

while (shapes_to_erase.size()) {
physics_2d_shape_mouseover.erase(shapes_to_erase.front()->get());
shapes_to_erase.pop_front();
}

while (to_mouse_exit.size()) {
Object *o = ObjectDB::get_instance(to_mouse_exit.front()->get());
CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
co->_mouse_exit();
to_mouse_exit.pop_front();
}

while (shapes_to_mouse_exit.size()) {
Pair<ObjectID, int> e = shapes_to_mouse_exit.front()->get();
Object *o = ObjectDB::get_instance(e.first);
CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
co->_mouse_shape_exit(e.second);
shapes_to_mouse_exit.pop_front();
}
}

AudioListener2D *Viewport::get_audio_listener_2d() const {
ERR_READ_THREAD_GUARD_V(nullptr);
return audio_listener_2d;
}

void Viewport::set_as_audio_listener_2d(bool p_enable) {
ERR_MAIN_THREAD_GUARD;
if (p_enable == is_audio_listener_2d_enabled) {
return;
}

is_audio_listener_2d_enabled = p_enable;
_update_audio_listener_2d();
}

bool Viewport::is_audio_listener_2d() const {
ERR_READ_THREAD_GUARD_V(false);
return is_audio_listener_2d_enabled;
}

Camera2D *Viewport::get_camera_2d() const {
ERR_READ_THREAD_GUARD_V(nullptr);
return camera_2d;
}

void Viewport::assign_next_enabled_camera_2d(const StringName &p_camera_group) {
ERR_MAIN_THREAD_GUARD;
List<Node *> camera_list;
get_tree()->get_nodes_in_group(p_camera_group, &camera_list);

Camera2D *new_camera = nullptr;
for (Node *E : camera_list) {
Camera2D *cam = Object::cast_to<Camera2D>(E);
if (!cam) {
continue; // Non-camera node (e.g. ParallaxBackground).
}

if (cam->is_enabled()) {
new_camera = cam;
break;
}
}

_camera_2d_set(new_camera);
if (!camera_2d) {
set_canvas_transform(Transform2D());
}
}

#ifndef _3D_DISABLED
AudioListener3D *Viewport::get_audio_listener_3d() const {
ERR_READ_THREAD_GUARD_V(nullptr);
Expand Down Expand Up @@ -4640,10 +4640,6 @@ void Viewport::_bind_methods() {
ClassDB::bind_method(D_METHOD("push_unhandled_input", "event", "in_local_coords"), &Viewport::push_unhandled_input, DEFVAL(false));
#endif // DISABLE_DEPRECATED

ClassDB::bind_method(D_METHOD("get_camera_2d"), &Viewport::get_camera_2d);
ClassDB::bind_method(D_METHOD("set_as_audio_listener_2d", "enable"), &Viewport::set_as_audio_listener_2d);
ClassDB::bind_method(D_METHOD("is_audio_listener_2d"), &Viewport::is_audio_listener_2d);

ClassDB::bind_method(D_METHOD("get_mouse_position"), &Viewport::get_mouse_position);
ClassDB::bind_method(D_METHOD("warp_mouse", "position"), &Viewport::warp_mouse);
ClassDB::bind_method(D_METHOD("update_mouse_cursor_state"), &Viewport::update_mouse_cursor_state);
Expand Down Expand Up @@ -4712,6 +4708,10 @@ void Viewport::_bind_methods() {

ClassDB::bind_method(D_METHOD("_process_picking"), &Viewport::_process_picking);

ClassDB::bind_method(D_METHOD("set_as_audio_listener_2d", "enable"), &Viewport::set_as_audio_listener_2d);
ClassDB::bind_method(D_METHOD("is_audio_listener_2d"), &Viewport::is_audio_listener_2d);
ClassDB::bind_method(D_METHOD("get_camera_2d"), &Viewport::get_camera_2d);

#ifndef _3D_DISABLED
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);
Expand Down Expand Up @@ -4786,7 +4786,7 @@ void Viewport::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_listener_enable_2d"), "set_as_audio_listener_2d", "is_audio_listener_2d");
#ifndef _3D_DISABLED
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_listener_enable_3d"), "set_as_audio_listener_3d", "is_audio_listener_3d");
#endif
#endif // _3D_DISABLED
ADD_GROUP("Physics", "physics_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "physics_object_picking"), "set_physics_object_picking", "get_physics_object_picking");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "physics_object_picking_sort"), "set_physics_object_picking_sort", "get_physics_object_picking_sort");
Expand Down
Loading
Loading