diff options
| author | HampusM <hampus@hampusmat.com> | 2026-07-12 16:56:31 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-07-12 16:56:31 +0200 |
| commit | a8e786d9bf0419eb4385bb09beb86938826864d0 (patch) | |
| tree | 3384e231aedd62376d0a8d12cc1d35e6b49a4c4c /engine | |
| parent | 453247306d66f008e12961323d4beea8c219deca (diff) | |
feat(engine): add scroll delta to Mouse
Diffstat (limited to 'engine')
| -rw-r--r-- | engine/src/windowing.rs | 59 | ||||
| -rw-r--r-- | engine/src/windowing/mouse.rs | 8 |
2 files changed, 66 insertions, 1 deletions
diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs index eae7616..a5a75c7 100644 --- a/engine/src/windowing.rs +++ b/engine/src/windowing.rs @@ -8,7 +8,7 @@ use crossbeam_queue::ArrayQueue; use raw_window_handle::{DisplayHandle, HandleError, HasDisplayHandle, WindowHandle}; use winit::application::ApplicationHandler; use winit::error::EventLoopError; -use winit::event::{DeviceEvent, DeviceId, StartCause, WindowEvent}; +use winit::event::{DeviceEvent, DeviceId, StartCause, WindowEvent, MouseScrollDelta}; use winit::event_loop::{ ActiveEventLoop, ControlFlow as EventLoopControlFlow, @@ -172,6 +172,14 @@ fn update_stuff( y: absolute_mouse_pos.1, }; + let mouse_scroll_delta = context + .shared_state + .mouse_scroll_delta + .swap((0.0, 0.0), Ordering::Relaxed); + + mouse.scroll_delta.hor_lines = mouse_scroll_delta.0; + mouse.scroll_delta.vert_lines = mouse_scroll_delta.1; + keyboard.set_text_keys(iter_array_queue(&context.shared_state.text_keys)); let Context { @@ -649,6 +657,7 @@ struct SharedState message_to_app_queue: ArrayQueue<MessageToApp>, relative_mouse_pos_delta: AtomicTwoF64, absolute_mouse_pos: AtomicTwoF64, + mouse_scroll_delta: AtomicTwoF64, text_keys: ArrayQueue<char>, is_dropped: AtomicBool, thread_panicked: AtomicBool, @@ -667,6 +676,7 @@ impl Default for SharedState message_to_app_queue: ArrayQueue::new(MESSAGE_TO_APP_QUEUE_SIZE), relative_mouse_pos_delta: AtomicTwoF64::new((0.0, 0.0)), absolute_mouse_pos: AtomicTwoF64::new((0.0, 0.0)), + mouse_scroll_delta: AtomicTwoF64::new((0.0, 0.0)), text_keys: ArrayQueue::new(TEXT_KEY_QUEUE_SIZE), is_dropped: AtomicBool::new(false), thread_panicked: AtomicBool::new(false), @@ -979,6 +989,53 @@ impl ApplicationHandler for App .store(delta, Ordering::Relaxed); } } + DeviceEvent::MouseWheel { delta } => { + let line_delta = match delta { + MouseScrollDelta::LineDelta(hor_lines, vert_lines) => { + (hor_lines as f64, vert_lines as f64) + } + MouseScrollDelta::PixelDelta(pos_delta) => { + let hor_lines = match pos_delta.x.partial_cmp(&0.0) { + Some(std::cmp::Ordering::Greater) => 1.0, + Some(std::cmp::Ordering::Less) => -1.0, + _ => 0.0, + }; + + let vert_lines = match pos_delta.y.partial_cmp(&0.0) { + Some(std::cmp::Ordering::Greater) => 1.0, + Some(std::cmp::Ordering::Less) => -1.0, + _ => 0.0, + }; + + (hor_lines, vert_lines) + } + }; + + let curr_mouse_scroll_delta = self + .shared_state + .mouse_scroll_delta + .load(Ordering::Relaxed); + + if self + .shared_state + .mouse_scroll_delta + .compare_exchange( + curr_mouse_scroll_delta, + ( + curr_mouse_scroll_delta.0 + line_delta.0, + curr_mouse_scroll_delta.1 + line_delta.1, + ), + Ordering::Relaxed, + Ordering::Relaxed, + ) + .is_err() + { + self.shared_state + .mouse_scroll_delta + .store(line_delta, Ordering::Relaxed); + } + + } _ => {} } } diff --git a/engine/src/windowing/mouse.rs b/engine/src/windowing/mouse.rs index 649036a..91719e2 100644 --- a/engine/src/windowing/mouse.rs +++ b/engine/src/windowing/mouse.rs @@ -17,6 +17,14 @@ pub struct Mouse /// Coordinates in pixels relative to the top-left corner of the window. May have /// been affected by cursor acceleration pub position: PhysicalPosition<f64>, + + pub scroll_delta: ScrollDelta, +} + +#[derive(Debug, Clone, Default)] +pub struct ScrollDelta { + pub vert_lines: f64, + pub hor_lines: f64, } /// Mouse buttons. |
