From 86748fbc681ea070bc37e90a569c192555b402cd Mon Sep 17 00:00:00 2001 From: Norbert Nemec Date: Thu, 11 Mar 2021 22:08:29 +0100 Subject: [PATCH] Fix communication of fractional RI_MOUSE_WHEEL events (Windows) (#1877) --- CHANGELOG.md | 1 + examples/mouse_wheel.rs | 48 +++++++++++++++++++++++++ src/platform_impl/windows/event_loop.rs | 5 +-- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 examples/mouse_wheel.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index cdd48cce..d155e714 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Unreleased +- On Windows, fix fractional deltas for mouse wheel device events. - On macOS, fix segmentation fault after dropping the main window. - On Android, `InputEvent::KeyEvent` is partially implemented providing the key scancode. - Added `is_maximized` method to `Window`. diff --git a/examples/mouse_wheel.rs b/examples/mouse_wheel.rs new file mode 100644 index 00000000..e61b64af --- /dev/null +++ b/examples/mouse_wheel.rs @@ -0,0 +1,48 @@ +use simple_logger::SimpleLogger; +use winit::{ + event::{DeviceEvent, Event, WindowEvent}, + event_loop::{ControlFlow, EventLoop}, + window::WindowBuilder, +}; + +fn main() { + SimpleLogger::new().init().unwrap(); + let event_loop = EventLoop::new(); + + let window = WindowBuilder::new() + .with_title("Mouse Wheel events") + .build(&event_loop) + .unwrap(); + + event_loop.run(move |event, _, control_flow| { + *control_flow = ControlFlow::Wait; + + match event { + Event::WindowEvent { event, .. } => match event { + WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, + _ => (), + }, + Event::DeviceEvent { event, .. } => match event { + DeviceEvent::MouseWheel { delta } => match delta { + winit::event::MouseScrollDelta::LineDelta(x, y) => { + println!("mouse wheel Line Delta: ({},{})", x, y); + let pixels_per_line = 120.0; + let mut pos = window.outer_position().unwrap(); + pos.x -= (x * pixels_per_line) as i32; + pos.y -= (y * pixels_per_line) as i32; + window.set_outer_position(pos) + } + winit::event::MouseScrollDelta::PixelDelta(p) => { + println!("mouse wheel Pixel Delta: ({},{})", p.x, p.y); + let mut pos = window.outer_position().unwrap(); + pos.x -= p.x as i32; + pos.y -= p.y as i32; + window.set_outer_position(pos) + } + }, + _ => (), + }, + _ => (), + } + }); +} diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 5b49cbe8..795d840d 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -2101,11 +2101,12 @@ unsafe extern "system" fn thread_event_target_callback( } if util::has_flag(mouse.usButtonFlags, winuser::RI_MOUSE_WHEEL) { - let delta = mouse.usButtonData as SHORT / winuser::WHEEL_DELTA; + let delta = + mouse.usButtonData as SHORT as f32 / winuser::WHEEL_DELTA as f32; subclass_input.send_event(Event::DeviceEvent { device_id, event: MouseWheel { - delta: LineDelta(0.0, delta as f32), + delta: LineDelta(0.0, delta), }, }); }