Skip to content

Commit

Permalink
derive Copy for HitData and adjust consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
databasedav committed May 24, 2024
1 parent d846416 commit bfb30d5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_picking_core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl PointerHits {
}

/// Holds data from a successful pointer hit test. See [`HitData::depth`] for important details.
#[derive(Clone, Debug, PartialEq, Reflect)]
#[derive(Clone, Copy, Debug, PartialEq, Reflect)]
pub struct HitData {
/// The camera entity used to detect this hit. Useful when you need to find the ray that was
/// casted for this hit when using a raycasting backend.
Expand Down
52 changes: 26 additions & 26 deletions crates/bevy_picking_core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,29 @@ impl<E: Debug + Clone + Reflect> Pointer<E> {
}

/// Fires when a pointer is no longer available.
#[derive(Event, Clone, PartialEq, Debug, Reflect)]
#[derive(Event, Clone, Copy, PartialEq, Debug, Reflect)]
pub struct PointerCancel {
/// ID of the pointer that was cancelled.
#[reflect(ignore)]
pub pointer_id: PointerId,
}

/// Fires when a the pointer crosses into the bounds of the `target` entity.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct Over {
/// Information about the picking intersection.
pub hit: HitData,
}

/// Fires when a the pointer crosses out of the bounds of the `target` entity.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct Out {
/// Information about the latest prior picking intersection.
pub hit: HitData,
}

/// Fires when a pointer button is pressed over the `target` entity.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct Down {
/// Pointer button pressed to trigger this event.
pub button: PointerButton,
Expand All @@ -94,7 +94,7 @@ pub struct Down {
}

/// Fires when a pointer button is released over the `target` entity.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct Up {
/// Pointer button lifted to trigger this event.
pub button: PointerButton,
Expand All @@ -104,7 +104,7 @@ pub struct Up {

/// Fires when a pointer sends a pointer down event followed by a pointer up event, with the same
/// `target` entity for both events.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct Click {
/// Pointer button pressed and lifted to trigger this event.
pub button: PointerButton,
Expand All @@ -113,7 +113,7 @@ pub struct Click {
}

/// Fires while a pointer is moving over the `target` entity.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct Move {
/// Information about the picking intersection.
pub hit: HitData,
Expand All @@ -122,7 +122,7 @@ pub struct Move {
}

/// Fires when the `target` entity receives a pointer down event followed by a pointer move event.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct DragStart {
/// Pointer button pressed and moved to trigger this event.
pub button: PointerButton,
Expand All @@ -131,7 +131,7 @@ pub struct DragStart {
}

/// Fires while the `target` entity is being dragged.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct Drag {
/// Pointer button pressed and moved to trigger this event.
pub button: PointerButton,
Expand All @@ -142,7 +142,7 @@ pub struct Drag {
}

/// Fires when a pointer is dragging the `target` entity and a pointer up event is received.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct DragEnd {
/// Pointer button pressed, moved, and lifted to trigger this event.
pub button: PointerButton,
Expand All @@ -151,7 +151,7 @@ pub struct DragEnd {
}

/// Fires when a pointer dragging the `dragged` entity enters the `target` entity.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct DragEnter {
/// Pointer button pressed to enter drag.
pub button: PointerButton,
Expand All @@ -162,7 +162,7 @@ pub struct DragEnter {
}

/// Fires while the `dragged` entity is being dragged over the `target` entity.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct DragOver {
/// Pointer button pressed while dragging over.
pub button: PointerButton,
Expand All @@ -173,7 +173,7 @@ pub struct DragOver {
}

/// Fires when a pointer dragging the `dragged` entity leaves the `target` entity.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct DragLeave {
/// Pointer button pressed while leaving drag.
pub button: PointerButton,
Expand All @@ -184,7 +184,7 @@ pub struct DragLeave {
}

/// Fires when a pointer drops the `dropped` entity onto the `target` entity.
#[derive(Clone, PartialEq, Debug, Reflect)]
#[derive(Clone, Copy, PartialEq, Debug, Reflect)]
pub struct Drop {
/// Pointer button lifted to drop.
pub button: PointerButton,
Expand Down Expand Up @@ -226,7 +226,7 @@ pub fn pointer_events(
for (hovered_entity, hit) in hover_map
.get(&pointer_id)
.iter()
.flat_map(|h| h.iter().map(|(entity, data)| (*entity, data.to_owned())))
.flat_map(|h| h.iter().map(|(entity, data)| (*entity, *data)))
{
pointer_move.send(Pointer::new(
pointer_id,
Expand All @@ -245,7 +245,7 @@ pub fn pointer_events(
for (hovered_entity, hit) in previous_hover_map
.get(&press_event.pointer_id)
.iter()
.flat_map(|h| h.iter().map(|(entity, data)| (*entity, data.clone())))
.flat_map(|h| h.iter().map(|(entity, data)| (*entity, *data)))
{
if let PressDirection::Up = press_event.direction {
let Some(location) = pointer_location(press_event.pointer_id) else {
Expand All @@ -266,7 +266,7 @@ pub fn pointer_events(
for (hovered_entity, hit) in hover_map
.get(&press_event.pointer_id)
.iter()
.flat_map(|h| h.iter().map(|(entity, data)| (*entity, data.clone())))
.flat_map(|h| h.iter().map(|(entity, data)| (*entity, *data)))
{
if let PressDirection::Down = press_event.direction {
let Some(location) = pointer_location(press_event.pointer_id) else {
Expand All @@ -289,7 +289,7 @@ pub fn pointer_events(
// If the entity is hovered...
for (pointer_id, hovered_entity, hit) in hover_map
.iter()
.flat_map(|(id, hashmap)| hashmap.iter().map(|data| (*id, *data.0, data.1.clone())))
.flat_map(|(id, hashmap)| hashmap.iter().map(|data| (*id, *data.0, *data.1)))
{
// ...but was not hovered last frame...
if !previous_hover_map
Expand All @@ -313,7 +313,7 @@ pub fn pointer_events(
// If the entity was hovered by a specific pointer last frame...
for (pointer_id, hovered_entity, hit) in previous_hover_map
.iter()
.flat_map(|(id, hashmap)| hashmap.iter().map(|data| (*id, *data.0, data.1.clone())))
.flat_map(|(id, hashmap)| hashmap.iter().map(|data| (*id, *data.0, *data.1)))
{
// ...but is now not being hovered by that same pointer...
if !hover_map
Expand Down Expand Up @@ -403,7 +403,7 @@ pub fn send_click_and_drag_events(
down.target,
DragStart {
button,
hit: down.hit.clone(),
hit: down.hit,
},
));
}
Expand Down Expand Up @@ -520,11 +520,11 @@ pub fn send_drag_over_events(
)
{
let drag_entry = drag_over_map.entry((pointer_id, button)).or_default();
drag_entry.insert(target, hit.clone());
drag_entry.insert(target, hit);
let event = DragEnter {
button,
dragged: *drag_target,
hit: hit.clone(),
hit,
};
pointer_drag_enter.send(Pointer::new(
pointer_id,
Expand Down Expand Up @@ -560,7 +560,7 @@ pub fn send_drag_over_events(
DragOver {
button,
dragged: *drag_target,
hit: hit.clone(),
hit,
},
));
}
Expand Down Expand Up @@ -589,7 +589,7 @@ pub fn send_drag_over_events(
DragLeave {
button,
dragged: target,
hit: hit.clone(),
hit,
},
));
pointer_drop.send(Pointer::new(
Expand All @@ -599,7 +599,7 @@ pub fn send_drag_over_events(
Drop {
button,
dropped: target,
hit: hit.clone(),
hit,
},
));
}
Expand Down Expand Up @@ -631,7 +631,7 @@ pub fn send_drag_over_events(
DragLeave {
button,
dragged: *drag_target,
hit: hit.clone(),
hit,
},
));
}
Expand Down
11 changes: 7 additions & 4 deletions crates/bevy_picking_core/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn build_over_map(
for (entity, pick_data) in entities_under_pointer.picks.iter() {
let layer = entities_under_pointer.order;
let hits = layer_map.entry(FloatOrd(layer)).or_insert_with(Vec::new);
hits.push((*entity, pick_data.clone()));
hits.push((*entity, *pick_data));
}
}

Expand Down Expand Up @@ -151,13 +151,13 @@ fn build_hover_map(
for (entity, pick_data) in layer_map.values().rev().flatten() {
if let Ok(pickable) = pickable.get(*entity) {
if pickable.is_hoverable {
pointer_entity_set.insert(*entity, pick_data.clone());
pointer_entity_set.insert(*entity, *pick_data);
}
if pickable.should_block_lower {
break;
}
} else {
pointer_entity_set.insert(*entity, pick_data.clone()); // Emit events by default
pointer_entity_set.insert(*entity, *pick_data); // Emit events by default
break; // Entities block by default so we break out of the loop
}
}
Expand Down Expand Up @@ -216,7 +216,10 @@ pub fn update_interactions(
for (pointer, pointer_press, mut pointer_interaction) in &mut pointers {
if let Some(pointers_hovered_entities) = hover_map.get(pointer) {
// Insert a sorted list of hit entities into the pointer's interaction component.
let mut sorted_entities: Vec<_> = pointers_hovered_entities.clone().drain().collect();
let mut sorted_entities: Vec<_> = pointers_hovered_entities
.into_iter()
.map(|(entity, hitdata)| (*entity, *hitdata))
.collect();
sorted_entities.sort_by_key(|(_entity, hit)| FloatOrd(hit.depth));
pointer_interaction.sorted_entities = sorted_entities;

Expand Down

0 comments on commit bfb30d5

Please sign in to comment.