diff --git a/examples/mandelbrot/src/main.rs b/examples/mandelbrot/src/main.rs index bf4449ef..7e5aefb3 100644 --- a/examples/mandelbrot/src/main.rs +++ b/examples/mandelbrot/src/main.rs @@ -77,9 +77,9 @@ pub fn main() { redraw |= camera.set_viewport(frame_input.viewport); for event in frame_input.events.iter() { - match event { + match *event { Event::MouseMotion { delta, button, .. } => { - if *button == Some(MouseButton::Left) { + if button == Some(MouseButton::Left) { let speed = 0.003 * camera.position().z.abs(); let right = camera.right_direction(); let up = right.cross(camera.view_direction()); diff --git a/examples/picking/src/main.rs b/examples/picking/src/main.rs index 7f62cd23..afeb4f29 100644 --- a/examples/picking/src/main.rs +++ b/examples/picking/src/main.rs @@ -61,9 +61,9 @@ pub async fn run() { for event in frame_input.events.iter() { if let Event::MousePress { button, position, .. - } = event + } = *event { - if *button == MouseButton::Left { + if button == MouseButton::Left { if let Some(pick) = pick(&context, &camera, position, &monkey) { pick_mesh.set_transformation(Mat4::from_translation(pick)); change = true; diff --git a/examples/shapes2d/src/main.rs b/examples/shapes2d/src/main.rs index 522a2824..c30c2981 100644 --- a/examples/shapes2d/src/main.rs +++ b/examples/shapes2d/src/main.rs @@ -55,19 +55,19 @@ pub fn main() { position, modifiers, .. - } = event + } = *event { - if *button == MouseButton::Left && !modifiers.ctrl { + if button == MouseButton::Left && !modifiers.ctrl { rectangle.set_center(position); } - if *button == MouseButton::Right && !modifiers.ctrl { + if button == MouseButton::Right && !modifiers.ctrl { circle.set_center(position); } - if *button == MouseButton::Left && modifiers.ctrl { + if button == MouseButton::Left && modifiers.ctrl { let ep = line.end_point1(); line.set_endpoints(position, ep); } - if *button == MouseButton::Right && modifiers.ctrl { + if button == MouseButton::Right && modifiers.ctrl { let ep = line.end_point0(); line.set_endpoints(ep, position); } diff --git a/src/renderer/control.rs b/src/renderer/control.rs index d955fab4..71b93127 100644 --- a/src/renderer/control.rs +++ b/src/renderer/control.rs @@ -20,62 +20,6 @@ pub use fly_control::*; pub use three_d_asset::PixelPoint as PhysicalPoint; -/// -/// A pixel coordinate in logical pixels, where `x` is on the horizontal axis with zero being at the left edge -/// and `y` is on the vertical axis with zero being at top edge. -/// -#[derive(Debug, Copy, Clone, PartialEq)] -pub struct LogicalPoint { - /// The horizontal pixel distance from the left edge. - pub x: f32, - /// The vertical pixel distance from the top edge. - pub y: f32, - pub(crate) device_pixel_ratio: f32, - pub(crate) height: f32, -} - -impl From for (f32, f32) { - fn from(value: LogicalPoint) -> Self { - Self::from(&value) - } -} - -impl From<&LogicalPoint> for (f32, f32) { - fn from(value: &LogicalPoint) -> Self { - (value.x, value.y) - } -} - -impl From for crate::Vec2 { - fn from(value: LogicalPoint) -> Self { - Self::from(&value) - } -} - -impl From<&LogicalPoint> for crate::Vec2 { - fn from(value: &LogicalPoint) -> Self { - Self { - x: value.x, - y: value.y, - } - } -} - -impl From for PhysicalPoint { - fn from(value: LogicalPoint) -> Self { - Self::from(&value) - } -} - -impl From<&LogicalPoint> for PhysicalPoint { - fn from(value: &LogicalPoint) -> Self { - Self { - x: value.x * value.device_pixel_ratio, - y: value.height - value.y * value.device_pixel_ratio, - } - } -} - /// Type of mouse button. #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub enum MouseButton { @@ -94,8 +38,8 @@ pub enum Event { MousePress { /// Type of button button: MouseButton, - /// The screen position in logical pixels. - position: LogicalPoint, + /// The screen position in physical pixels. + position: PhysicalPoint, /// The state of modifiers. modifiers: Modifiers, /// Whether or not this event already have been handled. @@ -105,8 +49,8 @@ pub enum Event { MouseRelease { /// Type of button button: MouseButton, - /// The screen position in logical pixels. - position: LogicalPoint, + /// The screen position in physical pixels. + position: PhysicalPoint, /// The state of modifiers. modifiers: Modifiers, /// Whether or not this event already have been handled. @@ -116,10 +60,10 @@ pub enum Event { MouseMotion { /// Type of button if a button is pressed. button: Option, - /// The relative movement of the mouse/finger since last [Event::MouseMotion] event. + /// The relative movement of the mouse/finger since last [Event::MouseMotion] event in logical pixels. delta: (f32, f32), - /// The screen position in logical pixels. - position: LogicalPoint, + /// The screen position in physical pixels. + position: PhysicalPoint, /// The state of modifiers. modifiers: Modifiers, /// Whether or not this event already have been handled. @@ -129,8 +73,8 @@ pub enum Event { MouseWheel { /// The relative scrolling since the last [Event::MouseWheel] event. delta: (f32, f32), - /// The screen position in logical pixels. - position: LogicalPoint, + /// The screen position in physical pixels. + position: PhysicalPoint, /// The state of modifiers. modifiers: Modifiers, /// Whether or not this event already have been handled. diff --git a/src/window/winit_window/frame_input_generator.rs b/src/window/winit_window/frame_input_generator.rs index 5dc425e2..2fc8eb86 100644 --- a/src/window/winit_window/frame_input_generator.rs +++ b/src/window/winit_window/frame_input_generator.rs @@ -191,7 +191,7 @@ impl FrameInputGenerator { let line_height = 24.0; // TODO self.events.push(crate::Event::MouseWheel { delta: (*x * line_height, *y * line_height), - position, + position: position.into(), modifiers: self.modifiers, handled: false, }); @@ -200,7 +200,7 @@ impl FrameInputGenerator { let d = delta.to_logical(self.device_pixel_ratio); self.events.push(crate::Event::MouseWheel { delta: (d.x, d.y), - position, + position: position.into(), modifiers: self.modifiers, handled: false, }); @@ -222,7 +222,7 @@ impl FrameInputGenerator { self.mouse_pressed = Some(b); crate::Event::MousePress { button: b, - position, + position: position.into(), modifiers: self.modifiers, handled: false, } @@ -230,7 +230,7 @@ impl FrameInputGenerator { self.mouse_pressed = None; crate::Event::MouseRelease { button: b, - position, + position: position.into(), modifiers: self.modifiers, handled: false, } @@ -254,7 +254,7 @@ impl FrameInputGenerator { self.events.push(crate::Event::MouseMotion { button: self.mouse_pressed, delta, - position, + position: position.into(), modifiers: self.modifiers, handled: false, }); @@ -285,7 +285,7 @@ impl FrameInputGenerator { if self.finger_id.is_none() { self.events.push(crate::Event::MousePress { button: MouseButton::Left, - position, + position: position.into(), modifiers: self.modifiers, handled: false, }); @@ -300,7 +300,7 @@ impl FrameInputGenerator { if self.finger_id.map(|id| id == touch.id).unwrap_or(false) { self.events.push(crate::Event::MouseRelease { button: MouseButton::Left, - position, + position: position.into(), modifiers: self.modifiers, handled: false, }); @@ -320,7 +320,7 @@ impl FrameInputGenerator { let last_pos = self.cursor_pos.unwrap(); if let Some(p) = self.secondary_cursor_pos { self.events.push(crate::Event::MouseWheel { - position, + position: position.into(), modifiers: self.modifiers, handled: false, delta: ( @@ -331,7 +331,7 @@ impl FrameInputGenerator { } else { self.events.push(crate::Event::MouseMotion { button: Some(MouseButton::Left), - position, + position: position.into(), modifiers: self.modifiers, handled: false, delta: (position.x - last_pos.x, position.y - last_pos.y), @@ -346,7 +346,7 @@ impl FrameInputGenerator { let last_pos = self.secondary_cursor_pos.unwrap(); if let Some(p) = self.cursor_pos { self.events.push(crate::Event::MouseWheel { - position: p, + position: p.into(), modifiers: self.modifiers, handled: false, delta: ( @@ -438,3 +438,26 @@ fn translate_virtual_key_code(key: winit::event::VirtualKeyCode) -> Option for PhysicalPoint { + fn from(value: LogicalPoint) -> Self { + Self { + x: value.x * value.device_pixel_ratio, + y: value.height - value.y * value.device_pixel_ratio, + } + } +}