diff options
Diffstat (limited to 'engine/src')
| -rw-r--r-- | engine/src/windowing.rs | 56 | ||||
| -rw-r--r-- | engine/src/windowing/mouse.rs | 17 |
2 files changed, 72 insertions, 1 deletions
diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs index eae7616..be8dc39 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, MouseScrollDelta, StartCause, WindowEvent}; 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 as f32; + mouse.scroll_delta.vert_lines = mouse_scroll_delta.1 as f32; + 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,50 @@ 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..abe2ab7 100644 --- a/engine/src/windowing/mouse.rs +++ b/engine/src/windowing/mouse.rs @@ -17,6 +17,23 @@ 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: f32, + pub hor_lines: f32, +} + +impl ScrollDelta +{ + pub fn is_zero(&self) -> bool + { + self.vert_lines == 0.0 && self.hor_lines == 0.0 + } } /// Mouse buttons. |
