Migrate bevy picking (#15690)
# Objective Migrate `bevy_picking` to the required components API ## Solution - Made `PointerId` require `PointerLocation`, `PointerPress`, and `PointerInteraction` - Removed `PointerBundle` - Removed all engine uses of `PointerBundle` - Added convenience constructor `PointerLocation::new(location: Location)` ## Testing - ran unit tests - ran `sprite_picking` example, everything seemed fine. ## Migration Guide This API hasn't shipped yet, so I didn't bother with a deprecation. However, for any crates tracking main the changes are as follows: Previous api: ```rs commands.insert(PointerBundle::new(PointerId::Mouse)); commands.insert(PointerBundle::new(PointerId::Mouse).with_location(location)); ``` New api: ```rs commands.insert(PointerId::Mouse); commands.insert((PointerId::Mouse, PointerLocation::new(location))); ```
This commit is contained in:
		
							parent
							
								
									d1bd46d45e
								
							
						
					
					
						commit
						d1927736de
					
				| @ -25,9 +25,9 @@ use bevy_render::camera::RenderTarget; | ||||
| use bevy_utils::{tracing::debug, HashMap, HashSet}; | ||||
| use bevy_window::{PrimaryWindow, WindowEvent, WindowRef}; | ||||
| 
 | ||||
| use crate::{ | ||||
|     pointer::{Location, PointerAction, PointerButton, PointerId, PointerInput, PressDirection}, | ||||
|     PointerBundle, | ||||
| use crate::pointer::{ | ||||
|     Location, PointerAction, PointerButton, PointerId, PointerInput, PointerLocation, | ||||
|     PressDirection, | ||||
| }; | ||||
| 
 | ||||
| use crate::PickSet; | ||||
| @ -99,7 +99,7 @@ impl Plugin for PointerInputPlugin { | ||||
| 
 | ||||
| /// Spawns the default mouse pointer.
 | ||||
| pub fn spawn_mouse_pointer(mut commands: Commands) { | ||||
|     commands.spawn((PointerBundle::new(PointerId::Mouse),)); | ||||
|     commands.spawn(PointerId::Mouse); | ||||
| } | ||||
| 
 | ||||
| /// Sends mouse pointer events to be processed by the core plugin
 | ||||
| @ -192,7 +192,7 @@ pub fn touch_pick_events( | ||||
|             match touch.phase { | ||||
|                 TouchPhase::Started => { | ||||
|                     debug!("Spawning pointer {:?}", pointer); | ||||
|                     commands.spawn(PointerBundle::new(pointer).with_location(location.clone())); | ||||
|                     commands.spawn((pointer, PointerLocation::new(location.clone()))); | ||||
| 
 | ||||
|                     pointer_events.send(PointerInput::new( | ||||
|                         pointer, | ||||
|  | ||||
| @ -236,42 +236,6 @@ impl Default for Pickable { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /// Components needed to build a pointer. Multiple pointers can be active at once, with each pointer
 | ||||
| /// being an entity.
 | ||||
| ///
 | ||||
| /// `Mouse` and `Touch` pointers are automatically spawned as needed. Use this bundle if you are
 | ||||
| /// spawning a custom `PointerId::Custom` pointer, either for testing, as a software controlled
 | ||||
| /// pointer, or if you are replacing the default touch and mouse inputs.
 | ||||
| #[derive(Bundle)] | ||||
| pub struct PointerBundle { | ||||
|     /// The pointer's unique [`PointerId`](pointer::PointerId).
 | ||||
|     pub id: pointer::PointerId, | ||||
|     /// Tracks the pointer's location.
 | ||||
|     pub location: pointer::PointerLocation, | ||||
|     /// Tracks the pointer's button press state.
 | ||||
|     pub click: pointer::PointerPress, | ||||
|     /// The interaction state of any hovered entities.
 | ||||
|     pub interaction: pointer::PointerInteraction, | ||||
| } | ||||
| 
 | ||||
| impl PointerBundle { | ||||
|     /// Create a new pointer with the provided [`PointerId`](pointer::PointerId).
 | ||||
|     pub fn new(id: pointer::PointerId) -> Self { | ||||
|         PointerBundle { | ||||
|             id, | ||||
|             location: pointer::PointerLocation::default(), | ||||
|             click: pointer::PointerPress::default(), | ||||
|             interaction: pointer::PointerInteraction::default(), | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /// Sets the location of the pointer bundle
 | ||||
|     pub fn with_location(mut self, location: pointer::Location) -> Self { | ||||
|         self.location.location = Some(location); | ||||
|         self | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /// Groups the stages of the picking process under shared labels.
 | ||||
| #[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)] | ||||
| pub enum PickSet { | ||||
|  | ||||
| @ -26,6 +26,7 @@ use crate::backend::HitData; | ||||
| /// This component is needed because pointers can be spawned and despawned, but they need to have a
 | ||||
| /// stable ID that persists regardless of the Entity they are associated with.
 | ||||
| #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash, Component, Reflect)] | ||||
| #[require(PointerLocation, PointerPress, PointerInteraction)] | ||||
| #[reflect(Component, Default, Debug, Hash, PartialEq)] | ||||
| pub enum PointerId { | ||||
|     /// The mouse pointer.
 | ||||
| @ -164,6 +165,13 @@ pub struct PointerLocation { | ||||
| } | ||||
| 
 | ||||
| impl PointerLocation { | ||||
|     ///Returns a [`PointerLocation`] associated with the given location
 | ||||
|     pub fn new(location: Location) -> Self { | ||||
|         Self { | ||||
|             location: Some(location), | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /// Returns `Some(&`[`Location`]`)` if the pointer is active, or `None` if the pointer is
 | ||||
|     /// inactive.
 | ||||
|     pub fn location(&self) -> Option<&Location> { | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Emerson Coskey
						Emerson Coskey