From f0bab95c4dc13c1989979c611a4663eb5d590a0d Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Fri, 12 Jun 2015 15:32:11 +0100 Subject: Express scroll deltas as either line or pixel deltas Depending on the platform and device, scroll deltas may either be represented as pixel deltas specifying the amount in pixels to scroll or they may be expressed in 'lines' or 'chunks' for low resolution devices (eg. a traditional mouse wheel). Pixel deltas are currently available on OS X. X11 currently supports only integer line deltas, though pixel deltas are available via XInput2. Windows supports fractional line deltas. --- examples/cursor.rs | 2 +- src/api/cocoa/mod.rs | 6 +++++- src/api/win32/callback.rs | 5 +++-- src/api/x11/window.rs | 7 +++++-- src/events.rs | 21 +++++++++++++++++++-- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/examples/cursor.rs b/examples/cursor.rs index 0dced66..d3beb84 100644 --- a/examples/cursor.rs +++ b/examples/cursor.rs @@ -17,7 +17,7 @@ fn main() { println!("This example requires glutin to be compiled with the `wind #[cfg(feature = "window")] fn main() { - let mut window = glutin::Window::new().unwrap(); + let window = glutin::Window::new().unwrap(); window.set_title("A fantastic window!"); unsafe { window.make_current() }; diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index 511148b..43628ea 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -275,7 +275,11 @@ impl<'a> Iterator for PollEventsIterator<'a> { self.window.delegate.state.pending_events.lock().unwrap().extend(events.into_iter()); event }, - NSScrollWheel => { Some(MouseWheel(event.scrollingDeltaX() as f64, event.scrollingDeltaY() as f64)) }, + NSScrollWheel => { + use events::MouseScrollDelta::PixelDelta; + let delta = PixelDelta(event.scrollingDeltaX() as f32, event.scrollingDeltaY() as f32); + Some(MouseWheel(delta)) + }, _ => { None }, }; diff --git a/src/api/win32/callback.rs b/src/api/win32/callback.rs index 86d5f1c..6ac56f3 100644 --- a/src/api/win32/callback.rs +++ b/src/api/win32/callback.rs @@ -112,12 +112,13 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT, winapi::WM_MOUSEWHEEL => { use events::Event::MouseWheel; + use events::MouseScrollDelta::LineDelta; let value = (wparam >> 16) as i16; let value = value as i32; - let value = value as f64 / winapi::WHEEL_DELTA as f64; + let value = value as f32 / winapi::WHEEL_DELTA as f32; - send_event(window, MouseWheel(0.0, value)); + send_event(window, MouseWheel(LineDelta(0.0, value))); 0 }, diff --git a/src/api/x11/window.rs b/src/api/x11/window.rs index 8b74e77..55ee007 100644 --- a/src/api/x11/window.rs +++ b/src/api/x11/window.rs @@ -219,6 +219,7 @@ impl<'a> Iterator for PollEventsIterator<'a> { use events::Event::{MouseInput, MouseWheel}; use events::ElementState::{Pressed, Released}; use events::MouseButton::{Left, Right, Middle}; + use events::MouseScrollDelta::{LineDelta}; let event: &ffi::XButtonEvent = unsafe { mem::transmute(&xev) }; @@ -229,11 +230,13 @@ impl<'a> Iterator for PollEventsIterator<'a> { ffi::Button2 => Some(Middle), ffi::Button3 => Some(Right), ffi::Button4 => { - self.window.pending_events.lock().unwrap().push_back(MouseWheel(0.0, 1.0)); + let delta = LineDelta(0.0, 1.0); + self.window.pending_events.lock().unwrap().push_back(MouseWheel(delta)); None } ffi::Button5 => { - self.window.pending_events.lock().unwrap().push_back(MouseWheel(0.0, -1.0)); + let delta = LineDelta(0.0, -1.0); + self.window.pending_events.lock().unwrap().push_back(MouseWheel(delta)); None } _ => None diff --git a/src/events.rs b/src/events.rs index 7962706..1103452 100644 --- a/src/events.rs +++ b/src/events.rs @@ -25,11 +25,11 @@ pub enum Event { /// The parameter are the (x,y) coords in pixels relative to the top-left corner of the window. MouseMoved((i32, i32)), - /// Returns the horizontal and vertical mouse scrolling. + /// A mouse wheel or touchpad scroll occurred. Depending on whether the /// /// A positive value indicates that the wheel was rotated forward, away from the user; /// a negative value indicates that the wheel was rotated backward, toward the user. - MouseWheel(f64, f64), + MouseWheel(MouseScrollDelta), /// An event from the mouse has been received. MouseInput(ElementState, MouseButton), @@ -57,6 +57,23 @@ pub enum MouseButton { Other(u8), } +#[derive(Debug, Clone, Copy)] +pub enum MouseScrollDelta { + /// Amount in lines or rows to scroll in the horizontal + /// and vertical directions. + /// + /// Positive values indicate movement forward + /// (away from the user) or righwards. + LineDelta(f32, f32), + /// Amount in pixels to scroll in the horizontal and + /// vertical direction. + /// + /// Scroll events are expressed as a PixelDelta if + /// supported by the device (eg. a touchpad) and + /// platform. + PixelDelta(f32, f32) +} + #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] pub enum VirtualKeyCode { /// The '1' key over the letters. -- cgit v1.2.3 From 5e626b7fdf1942af9c3f1faade1eba6ecd47e0ca Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Sat, 13 Jun 2015 23:22:51 +0100 Subject: Correct typo in MouseScrollDelta docs --- src/events.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/events.rs b/src/events.rs index 1103452..07be8c9 100644 --- a/src/events.rs +++ b/src/events.rs @@ -63,7 +63,7 @@ pub enum MouseScrollDelta { /// and vertical directions. /// /// Positive values indicate movement forward - /// (away from the user) or righwards. + /// (away from the user) or rightwards. LineDelta(f32, f32), /// Amount in pixels to scroll in the horizontal and /// vertical direction. -- cgit v1.2.3 From 5b08220df5cbdf16ee2879a5889cdaaa1f89984e Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Sun, 14 Jun 2015 18:09:02 +0100 Subject: Report scroll deltas in lines for non-touch devices on OS X Scroll deltas on OS X may be reported either as pixel deltas to scroll by if supported by the device or line/row deltas otherwise. --- src/api/cocoa/mod.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index 43628ea..877aba7 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -276,8 +276,12 @@ impl<'a> Iterator for PollEventsIterator<'a> { event }, NSScrollWheel => { - use events::MouseScrollDelta::PixelDelta; - let delta = PixelDelta(event.scrollingDeltaX() as f32, event.scrollingDeltaY() as f32); + use events::MouseScrollDelta::{LineDelta, PixelDelta}; + let delta = if event.hasPreciseScrollingDeltas() == YES { + PixelDelta(event.scrollingDeltaX() as f32, event.scrollingDeltaY() as f32) + } else { + LineDelta(event.scrollingDeltaX() as f32, event.scrollingDeltaY() as f32) + }; Some(MouseWheel(delta)) }, _ => { None }, -- cgit v1.2.3 From b2c2f300dce0b874efee4c328629ff9c2316c056 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Mon, 15 Jun 2015 23:54:43 +0100 Subject: Correct MouseWheel doc comment. Remove incomplete sentence and obsolete comment about the meaning of the parameter. --- src/events.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/events.rs b/src/events.rs index 07be8c9..c175810 100644 --- a/src/events.rs +++ b/src/events.rs @@ -25,10 +25,7 @@ pub enum Event { /// The parameter are the (x,y) coords in pixels relative to the top-left corner of the window. MouseMoved((i32, i32)), - /// A mouse wheel or touchpad scroll occurred. Depending on whether the - /// - /// A positive value indicates that the wheel was rotated forward, away from the user; - /// a negative value indicates that the wheel was rotated backward, toward the user. + /// A mouse wheel movement or touchpad scroll occurred. MouseWheel(MouseScrollDelta), /// An event from the mouse has been received. -- cgit v1.2.3