Skip to content

Commit 58bb0eb

Browse files
committed
Add winit phase to MouseWheel event
1 parent 454fa56 commit 58bb0eb

File tree

8 files changed

+42
-17
lines changed

8 files changed

+42
-17
lines changed

crates/bevy_input/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.17.0-dev", features = [
6969
bevy_platform = { path = "../bevy_platform", version = "0.17.0-dev", default-features = false }
7070

7171
# other
72+
winit = { version = "0.30", default-features = false, features = ["rwh_06"] }
7273
serde = { version = "1", features = [
7374
"alloc",
7475
"derive",

crates/bevy_input/src/mouse.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The mouse input functionality.
22
3-
use crate::{ButtonInput, ButtonState};
3+
use crate::{touch::TouchPhase, ButtonInput, ButtonState};
44
use bevy_ecs::{
55
change_detection::DetectChangesMut,
66
entity::Entity,
@@ -160,6 +160,8 @@ pub struct MouseWheel {
160160
pub y: f32,
161161
/// Window that received the input.
162162
pub window: Entity,
163+
/// Phase of the wheel event.
164+
pub phase: TouchPhase,
163165
}
164166

165167
/// Updates the [`ButtonInput<MouseButton>`] resource with the latest [`MouseButtonInput`] events.

crates/bevy_input/src/touch.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,14 @@ pub enum ForceTouch {
127127
reflect(Serialize, Deserialize)
128128
)]
129129
pub enum TouchPhase {
130-
/// A finger started to touch the touchscreen.
130+
/// A finger started to touch the touchscreen or a wheel scroll started.
131131
Started,
132-
/// A finger moved over the touchscreen.
132+
/// A finger moved over the touchscreen or touchpad scroll is in progress.
133+
///
134+
/// This can be used to detect whether a scroll action is taking place on a
135+
/// mouse wheel or touchpad, because mouse wheel scrolls do not emit this event.
133136
Moved,
134-
/// A finger stopped touching the touchscreen.
137+
/// A finger stopped touching the touchscreen or a wheel scroll finished.
135138
Ended,
136139
/// The system canceled the tracking of the finger.
137140
///
@@ -140,6 +143,17 @@ pub enum TouchPhase {
140143
Canceled,
141144
}
142145

146+
impl From<winit::event::TouchPhase> for TouchPhase {
147+
fn from(phase: winit::event::TouchPhase) -> TouchPhase {
148+
match phase {
149+
winit::event::TouchPhase::Started => TouchPhase::Started,
150+
winit::event::TouchPhase::Moved => TouchPhase::Moved,
151+
winit::event::TouchPhase::Ended => TouchPhase::Ended,
152+
winit::event::TouchPhase::Cancelled => TouchPhase::Canceled,
153+
}
154+
}
155+
}
156+
143157
/// A touch input.
144158
///
145159
/// ## Usage

crates/bevy_picking/src/events.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
use core::{fmt::Debug, time::Duration};
4141

4242
use bevy_ecs::{prelude::*, query::QueryData, system::SystemParam, traversal::Traversal};
43-
use bevy_input::mouse::MouseScrollUnit;
43+
use bevy_input::{mouse::MouseScrollUnit, touch::TouchPhase};
4444
use bevy_math::Vec2;
4545
use bevy_platform::collections::HashMap;
4646
use bevy_platform::time::Instant;
@@ -334,6 +334,8 @@ pub struct Scroll {
334334
pub y: f32,
335335
/// Information about the picking intersection.
336336
pub hit: HitData,
337+
/// `TouchPhase` of the scroll.
338+
pub phase: TouchPhase,
337339
}
338340

339341
/// An entry in the cache that drives the `pointer_events` system, storing additional data
@@ -776,7 +778,7 @@ pub fn pointer_events(
776778
event_writers.move_events.write(move_event);
777779
}
778780
}
779-
PointerAction::Scroll { x, y, unit } => {
781+
PointerAction::Scroll { x, y, unit, phase } => {
780782
for (hovered_entity, hit) in hover_map
781783
.get(&pointer_id)
782784
.iter()
@@ -791,6 +793,7 @@ pub fn pointer_events(
791793
x,
792794
y,
793795
hit: hit.clone(),
796+
phase,
794797
},
795798
);
796799
commands.trigger_targets(scroll_event.clone(), hovered_entity);

crates/bevy_picking/src/input.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,13 @@ pub fn mouse_pick_events(
174174
pointer_events.write(PointerInput::new(PointerId::Mouse, location, action));
175175
}
176176
WindowEvent::MouseWheel(event) => {
177-
let MouseWheel { unit, x, y, window } = *event;
177+
let MouseWheel {
178+
unit,
179+
x,
180+
y,
181+
window,
182+
phase,
183+
} = *event;
178184

179185
let location = Location {
180186
target: match RenderTarget::Window(WindowRef::Entity(window))
@@ -186,7 +192,7 @@ pub fn mouse_pick_events(
186192
position: *cursor_last,
187193
};
188194

189-
let action = PointerAction::Scroll { x, y, unit };
195+
let action = PointerAction::Scroll { x, y, unit, phase };
190196

191197
pointer_events.write(PointerInput::new(PointerId::Mouse, location, action));
192198
}

crates/bevy_picking/src/pointer.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//! driven by lower-level input devices and consumed by higher-level interaction systems.
1010
1111
use bevy_ecs::prelude::*;
12-
use bevy_input::mouse::MouseScrollUnit;
12+
use bevy_input::{mouse::MouseScrollUnit, touch::TouchPhase};
1313
use bevy_math::Vec2;
1414
use bevy_platform::collections::HashMap;
1515
use bevy_reflect::prelude::*;
@@ -263,6 +263,8 @@ pub enum PointerAction {
263263
x: f32,
264264
/// The vertical scroll value.
265265
y: f32,
266+
/// The phase of the scroll.
267+
phase: TouchPhase,
266268
},
267269
/// Cancel the pointer. Often used for touch events.
268270
Cancel,

crates/bevy_winit/src/converters.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bevy_ecs::entity::Entity;
22
use bevy_input::{
33
keyboard::{KeyCode, KeyboardInput, NativeKeyCode},
44
mouse::MouseButton,
5-
touch::{ForceTouch, TouchInput, TouchPhase},
5+
touch::{ForceTouch, TouchInput},
66
ButtonState,
77
};
88
use bevy_math::{CompassOctant, Vec2};
@@ -51,12 +51,7 @@ pub fn convert_touch_input(
5151
window_entity: Entity,
5252
) -> TouchInput {
5353
TouchInput {
54-
phase: match touch_input.phase {
55-
winit::event::TouchPhase::Started => TouchPhase::Started,
56-
winit::event::TouchPhase::Moved => TouchPhase::Moved,
57-
winit::event::TouchPhase::Ended => TouchPhase::Ended,
58-
winit::event::TouchPhase::Cancelled => TouchPhase::Canceled,
59-
},
54+
phase: touch_input.phase.into(),
6055
position: Vec2::new(location.x as f32, location.y as f32),
6156
window: window_entity,
6257
force: touch_input.force.map(|f| match f {

crates/bevy_winit/src/state.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,14 @@ impl<T: BufferedEvent> ApplicationHandler<T> for WinitAppRunnerState<T> {
320320
y: delta.y,
321321
}));
322322
}
323-
WindowEvent::MouseWheel { delta, .. } => match delta {
323+
WindowEvent::MouseWheel { delta, phase, .. } => match delta {
324324
event::MouseScrollDelta::LineDelta(x, y) => {
325325
self.bevy_window_events.send(MouseWheel {
326326
unit: MouseScrollUnit::Line,
327327
x,
328328
y,
329329
window,
330+
phase: phase.into(),
330331
});
331332
}
332333
event::MouseScrollDelta::PixelDelta(p) => {
@@ -335,6 +336,7 @@ impl<T: BufferedEvent> ApplicationHandler<T> for WinitAppRunnerState<T> {
335336
x: p.x as f32,
336337
y: p.y as f32,
337338
window,
339+
phase: phase.into(),
338340
});
339341
}
340342
},

0 commit comments

Comments
 (0)