diff options
Diffstat (limited to 'scene/main')
-rw-r--r-- | scene/main/viewport.cpp | 84 | ||||
-rw-r--r-- | scene/main/viewport.h | 6 |
2 files changed, 59 insertions, 31 deletions
diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index c6fe1117d1..38d2a35efb 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -724,7 +724,6 @@ void Viewport::_process_picking() { bool send_event = true; if (is_mouse) { Map<ObjectID, uint64_t>::Element *F = physics_2d_mouseover.find(res[i].collider_id); - if (!F) { physics_2d_mouseover.insert(res[i].collider_id, frame); co->_mouse_enter(); @@ -735,6 +734,13 @@ void Viewport::_process_picking() { send_event = false; } } + Map<Pair<ObjectID, int>, uint64_t, PairSort<ObjectID, int>>::Element *SF = physics_2d_shape_mouseover.find(Pair(res[i].collider_id, res[i].shape)); + if (!SF) { + physics_2d_shape_mouseover.insert(Pair(res[i].collider_id, res[i].shape), frame); + co->_mouse_shape_enter(res[i].shape); + } else { + SF->get() = frame; + } } if (send_event) { @@ -746,25 +752,7 @@ void Viewport::_process_picking() { } if (is_mouse) { - List<Map<ObjectID, uint64_t>::Element *> to_erase; - - for (Map<ObjectID, uint64_t>::Element *E = physics_2d_mouseover.front(); E; E = E->next()) { - 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(); - } - } - to_erase.push_back(E); - } - } - - while (to_erase.size()) { - physics_2d_mouseover.erase(to_erase.front()->get()); - to_erase.pop_front(); - } + _cleanup_mouseover_colliders(false, false, frame); } } @@ -2607,20 +2595,41 @@ void Viewport::_drop_mouse_focus() { void Viewport::_drop_physics_mouseover(bool p_paused_only) { physics_has_last_mousepos = false; + _cleanup_mouseover_colliders(true, p_paused_only); + +#ifndef _3D_DISABLED + if (physics_object_over.is_valid()) { + CollisionObject3D *co = Object::cast_to<CollisionObject3D>(ObjectDB::get_instance(physics_object_over)); + if (co) { + if (!(p_paused_only && co->can_process())) { + co->_mouse_exit(); + physics_object_over = ObjectID(); + physics_object_capture = ObjectID(); + } + } + } +#endif +} + +void Viewport::_cleanup_mouseover_colliders(bool p_clean_all_frames, bool p_paused_only, uint64_t p_frame_reference) { List<Map<ObjectID, uint64_t>::Element *> to_erase; for (Map<ObjectID, uint64_t>::Element *E = physics_2d_mouseover.front(); E; E = E->next()) { + if (!p_clean_all_frames && E->get() == p_frame_reference) { + continue; + } + Object *o = ObjectDB::get_instance(E->key()); if (o) { CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o); if (co) { - if (p_paused_only && co->can_process()) { + if (p_clean_all_frames && p_paused_only && co->can_process()) { continue; } co->_mouse_exit(); - to_erase.push_back(E); } } + to_erase.push_back(E); } while (to_erase.size()) { @@ -2628,18 +2637,31 @@ void Viewport::_drop_physics_mouseover(bool p_paused_only) { to_erase.pop_front(); } -#ifndef _3D_DISABLED - if (physics_object_over.is_valid()) { - CollisionObject3D *co = Object::cast_to<CollisionObject3D>(ObjectDB::get_instance(physics_object_over)); - if (co) { - if (!(p_paused_only && co->can_process())) { - co->_mouse_exit(); - physics_object_over = ObjectID(); - physics_object_capture = ObjectID(); + // Per-shape + List<Map<Pair<ObjectID, int>, uint64_t, PairSort<ObjectID, int>>::Element *> shapes_to_erase; + + for (Map<Pair<ObjectID, int>, uint64_t, PairSort<ObjectID, int>>::Element *E = physics_2d_shape_mouseover.front(); E; E = E->next()) { + if (!p_clean_all_frames && E->get() == p_frame_reference) { + continue; + } + + Object *o = ObjectDB::get_instance(E->key().first); + if (o) { + CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o); + if (co) { + if (p_clean_all_frames && p_paused_only && co->can_process()) { + continue; + } + co->_mouse_shape_exit(E->key().second); } } + shapes_to_erase.push_back(E); + } + + while (shapes_to_erase.size()) { + physics_2d_shape_mouseover.erase(shapes_to_erase.front()->get()); + shapes_to_erase.pop_front(); } -#endif } Control *Viewport::_gui_get_focus_owner() { diff --git a/scene/main/viewport.h b/scene/main/viewport.h index 0f11e6fb19..7d6a2a10d0 100644 --- a/scene/main/viewport.h +++ b/scene/main/viewport.h @@ -32,6 +32,7 @@ #define VIEWPORT_H #include "core/math/transform_2d.h" +#include "core/templates/pair.h" #include "scene/main/node.h" #include "scene/resources/texture.h" #include "scene/resources/world_2d.h" @@ -271,7 +272,12 @@ private: bool handle_input_locally = true; bool local_input_handled = false; + // Collider to frame Map<ObjectID, uint64_t> physics_2d_mouseover; + // Collider & shape to frame + Map<Pair<ObjectID, int>, uint64_t, PairSort<ObjectID, int>> physics_2d_shape_mouseover; + // Cleans up colliders corresponding to old frames or all of them. + void _cleanup_mouseover_colliders(bool p_clean_all_frames, bool p_paused_only, uint64_t p_frame_reference = 0); Ref<World2D> world_2d; Ref<World3D> world_3d; |