Skip to content
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
6 changes: 5 additions & 1 deletion crates/bevy_input/src/mouse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The mouse input functionality.

use crate::{ButtonInput, ButtonState};
use crate::{touch::TouchPhase, ButtonInput, ButtonState};
#[cfg(feature = "bevy_reflect")]
use bevy_ecs::prelude::ReflectMessage;
use bevy_ecs::{
Expand Down Expand Up @@ -174,6 +174,10 @@ pub struct MouseWheel {
pub y: f32,
/// Window that received the input.
pub window: Entity,
/// Touch phase of the input.
///
/// When using a mouse, this will always be [`TouchPhase::Moved`].
pub phase: TouchPhase,
}

/// Updates the [`ButtonInput<MouseButton>`] resource with the latest [`MouseButtonInput`] events.
Expand Down
9 changes: 7 additions & 2 deletions crates/bevy_picking/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use bevy_ecs::{
system::SystemParam,
traversal::Traversal,
};
use bevy_input::mouse::MouseScrollUnit;
use bevy_input::{mouse::MouseScrollUnit, touch::TouchPhase};
use bevy_math::Vec2;
use bevy_platform::collections::HashMap;
use bevy_platform::time::Instant;
Expand Down Expand Up @@ -458,6 +458,10 @@ pub struct Scroll {
pub y: f32,
/// Information about the picking intersection.
pub hit: HitData,
/// Touch phase of the input.
///
/// When using a mouse, this will always be [`TouchPhase::Moved`].
pub phase: TouchPhase,
}

/// An entry in the cache that drives the `pointer_events` system, storing additional data
Expand Down Expand Up @@ -1141,7 +1145,7 @@ pub fn pointer_events(
message_writers.move_events.write(move_event);
}
}
PointerAction::Scroll { x, y, unit } => {
PointerAction::Scroll { x, y, unit, phase } => {
for (hovered_entity, hit) in hover_map
.get(&pointer_id)
.iter()
Expand All @@ -1156,6 +1160,7 @@ pub fn pointer_events(
x,
y,
hit: hit.clone(),
phase,
},
hovered_entity,
);
Expand Down
10 changes: 8 additions & 2 deletions crates/bevy_picking/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ pub fn mouse_pick_events(
pointer_inputs.write(PointerInput::new(PointerId::Mouse, location, action));
}
WindowEvent::MouseWheel(event) => {
let MouseWheel { unit, x, y, window } = *event;
let MouseWheel {
unit,
x,
y,
window,
phase,
} = *event;

let location = Location {
target: match RenderTarget::Window(WindowRef::Entity(window))
Expand All @@ -185,7 +191,7 @@ pub fn mouse_pick_events(
position: *cursor_last,
};

let action = PointerAction::Scroll { x, y, unit };
let action = PointerAction::Scroll { x, y, unit, phase };

pointer_inputs.write(PointerInput::new(PointerId::Mouse, location, action));
}
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_picking/src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use bevy_camera::NormalizedRenderTarget;
use bevy_camera::{Camera, RenderTarget};
use bevy_ecs::prelude::*;
use bevy_input::mouse::MouseScrollUnit;
use bevy_input::touch::TouchPhase;
use bevy_math::Vec2;
use bevy_platform::collections::HashMap;
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -264,6 +265,10 @@ pub enum PointerAction {
x: f32,
/// The vertical scroll value.
y: f32,
/// Touch phase of the input.
///
/// When using a mouse, this will always be [`TouchPhase::Moved`].
phase: TouchPhase,
},
/// Cancel the pointer. Often used for touch events.
Cancel,
Expand Down
17 changes: 11 additions & 6 deletions crates/bevy_winit/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,24 @@ pub fn convert_mouse_button(mouse_button: winit::event::MouseButton) -> MouseBut
}
}

/// Converts a [`winit::event::TouchPhase`] to a Bevy [`TouchPhase`].
pub fn convert_touch_phase(phase: winit::event::TouchPhase) -> TouchPhase {
match phase {
winit::event::TouchPhase::Started => TouchPhase::Started,
winit::event::TouchPhase::Moved => TouchPhase::Moved,
winit::event::TouchPhase::Ended => TouchPhase::Ended,
winit::event::TouchPhase::Cancelled => TouchPhase::Canceled,
}
}

/// Converts a [`winit::event::Touch`], [`winit::dpi::LogicalPosition<f64>`] and window [`Entity`] to a Bevy [`TouchInput`]
pub fn convert_touch_input(
touch_input: winit::event::Touch,
location: winit::dpi::LogicalPosition<f64>,
window_entity: Entity,
) -> TouchInput {
TouchInput {
phase: match touch_input.phase {
winit::event::TouchPhase::Started => TouchPhase::Started,
winit::event::TouchPhase::Moved => TouchPhase::Moved,
winit::event::TouchPhase::Ended => TouchPhase::Ended,
winit::event::TouchPhase::Cancelled => TouchPhase::Canceled,
},
phase: convert_touch_phase(touch_input.phase),
position: Vec2::new(location.x as f32, location.y as f32),
window: window_entity,
force: touch_input.force.map(|f| match f {
Expand Down
42 changes: 24 additions & 18 deletions crates/bevy_winit/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ use bevy_window::{CursorOptions, PrimaryWindow, RawHandleWrapper};

use crate::{
accessibility::ACCESS_KIT_ADAPTERS,
converters, create_windows,
converters::{self, convert_touch_phase},
create_windows,
system::{create_monitors, CachedWindow, WinitWindowPressedKeys},
AppSendEvent, CreateMonitorParams, CreateWindowParams, RawWinitWindowEvent, UpdateMode,
WinitSettings, WinitUserEvent, WINIT_WINDOWS,
Expand Down Expand Up @@ -336,24 +337,29 @@ impl ApplicationHandler<WinitUserEvent> for WinitAppRunnerState {
y: delta.y,
}));
}
WindowEvent::MouseWheel { delta, .. } => match delta {
event::MouseScrollDelta::LineDelta(x, y) => {
self.bevy_window_events.send(MouseWheel {
unit: MouseScrollUnit::Line,
x,
y,
window,
});
}
event::MouseScrollDelta::PixelDelta(p) => {
self.bevy_window_events.send(MouseWheel {
unit: MouseScrollUnit::Pixel,
x: p.x as f32,
y: p.y as f32,
window,
});
WindowEvent::MouseWheel { delta, phase, .. } => {
let phase = convert_touch_phase(phase);
match delta {
event::MouseScrollDelta::LineDelta(x, y) => {
self.bevy_window_events.send(MouseWheel {
unit: MouseScrollUnit::Line,
x,
y,
window,
phase,
});
}
event::MouseScrollDelta::PixelDelta(p) => {
self.bevy_window_events.send(MouseWheel {
unit: MouseScrollUnit::Pixel,
x: p.x as f32,
y: p.y as f32,
window,
phase,
});
}
}
},
}
WindowEvent::Touch(touch) => {
let location = touch
.location
Expand Down