Calculate pointer location correctly

This commit is contained in:
Sergey Smirnykh 2020-03-23 17:02:29 +07:00 committed by Victor Berger
parent 3bc1c728a8
commit e798259be9
1 changed files with 7 additions and 8 deletions

View File

@ -381,8 +381,7 @@ impl KeyboardKeyEvent for WinitKeyboardInputEvent {
pub struct WinitMouseMovedEvent { pub struct WinitMouseMovedEvent {
size: Rc<RefCell<WindowSize>>, size: Rc<RefCell<WindowSize>>,
time: u32, time: u32,
x: f64, logical_position: LogicalPosition<f64>,
y: f64,
} }
impl BackendEvent for WinitMouseMovedEvent { impl BackendEvent for WinitMouseMovedEvent {
@ -394,24 +393,24 @@ impl BackendEvent for WinitMouseMovedEvent {
impl PointerMotionAbsoluteEvent for WinitMouseMovedEvent { // TODO: maybe use {Logical, Physical}Position from winit? impl PointerMotionAbsoluteEvent for WinitMouseMovedEvent { // TODO: maybe use {Logical, Physical}Position from winit?
fn x(&self) -> f64 { fn x(&self) -> f64 {
let wsize = self.size.borrow(); let wsize = self.size.borrow();
self.x * wsize.scale_factor self.logical_position.x * wsize.scale_factor
} }
fn y(&self) -> f64 { fn y(&self) -> f64 {
let wsize = self.size.borrow(); let wsize = self.size.borrow();
self.y * wsize.scale_factor self.logical_position.y * wsize.scale_factor
} }
fn x_transformed(&self, width: u32) -> u32 { fn x_transformed(&self, width: u32) -> u32 {
let wsize = self.size.borrow(); let wsize = self.size.borrow();
let w_width = wsize.physical_size.to_logical::<f64>(wsize.scale_factor).width; let w_width = wsize.physical_size.to_logical::<f64>(wsize.scale_factor).width;
cmp::max((self.x * width as f64 / w_width) as i32, 0) as u32 cmp::max((self.logical_position.x * width as f64 / w_width) as i32, 0) as u32
} }
fn y_transformed(&self, height: u32) -> u32 { fn y_transformed(&self, height: u32) -> u32 {
let wsize = self.size.borrow(); let wsize = self.size.borrow();
let w_height = wsize.physical_size.to_logical::<f64>(wsize.scale_factor).height; let w_height = wsize.physical_size.to_logical::<f64>(wsize.scale_factor).height;
cmp::max((self.y * height as f64 / w_height) as i32, 0) as u32 cmp::max((self.logical_position.y * height as f64 / w_height) as i32, 0) as u32
} }
} }
@ -771,13 +770,13 @@ impl InputBackend for WinitInputBackend {
} }
(WindowEvent::CursorMoved { position, .. }, Some(handler), _) => { (WindowEvent::CursorMoved { position, .. }, Some(handler), _) => {
trace!(logger, "Calling on_pointer_move_absolute with {:?}", position); trace!(logger, "Calling on_pointer_move_absolute with {:?}", position);
let lpos = position.to_logical(window_size.borrow().scale_factor);
handler.on_pointer_move_absolute( handler.on_pointer_move_absolute(
seat, seat,
WinitMouseMovedEvent { WinitMouseMovedEvent {
size: window_size.clone(), size: window_size.clone(),
time, time,
x: position.x, logical_position: lpos,
y: position.y,
}, },
) )
} }