aevyrie / bevy_mod_picking

Picking and pointer events for Bevy.
https://crates.io/crates/bevy_mod_picking
Apache License 2.0
764 stars 170 forks source link

[Feature] Prevent events from bubbling up #309

Closed tychedelia closed 6 months ago

tychedelia commented 6 months ago

Sometimes, it's useful to be able to effect child entities independently of a parent, while still benefiting from things like transform propagation of their parent.

For example, both parent and child might have On::<Pointer<Drag>> components. With event bubbling, this means that moving a child will additionally cause it to be moved relative to it's parent's transform when the parent's drag even handler fires.

There are two solutions I've tried implementing, both of which require manually implementing EntityEvent:

  1. Add a generic parameter to Pointer which controls the return value of can_bubble, eg On::<Pointer<Drag, NoBubble>>.

  2. Add some kind of method like stop_propagation to Pointer which the user can call inside run.

Personally, I kind of favor 1, since it plays more nicely with On components that don't provide mutable access to the underlying event.

tychedelia commented 6 months ago

I was able to solve this by making my "parent" a sibling of my children and having a non-pickable root parent. Moving the false sibling "parent" updates the root parent which modifies the child global transform. 👍

aevyrie commented 6 months ago

This feature already exists: https://docs.rs/bevy_eventlistener/latest/bevy_eventlistener/callbacks/struct.ListenerInput.html#method.stop_propagation