Skip to content

Commit

Permalink
Event position is defined in PhysicalPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
asny committed Feb 26, 2024
1 parent 17b1f13 commit a239ca4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 84 deletions.
4 changes: 2 additions & 2 deletions examples/mandelbrot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions examples/picking/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions examples/shapes2d/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
74 changes: 9 additions & 65 deletions src/renderer/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogicalPoint> 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<LogicalPoint> 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<LogicalPoint> 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 {
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -116,10 +60,10 @@ pub enum Event {
MouseMotion {
/// Type of button if a button is pressed.
button: Option<MouseButton>,
/// 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.
Expand All @@ -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.
Expand Down
43 changes: 33 additions & 10 deletions src/window/winit_window/frame_input_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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,
});
Expand All @@ -222,15 +222,15 @@ impl FrameInputGenerator {
self.mouse_pressed = Some(b);
crate::Event::MousePress {
button: b,
position,
position: position.into(),
modifiers: self.modifiers,
handled: false,
}
} else {
self.mouse_pressed = None;
crate::Event::MouseRelease {
button: b,
position,
position: position.into(),
modifiers: self.modifiers,
handled: false,
}
Expand All @@ -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,
});
Expand Down Expand Up @@ -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,
});
Expand All @@ -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,
});
Expand All @@ -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: (
Expand All @@ -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),
Expand All @@ -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: (
Expand Down Expand Up @@ -438,3 +438,26 @@ fn translate_virtual_key_code(key: winit::event::VirtualKeyCode) -> Option<crate
}
})
}

///
/// 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)]
struct LogicalPoint {
/// The horizontal pixel distance from the left edge.
x: f32,
/// The vertical pixel distance from the top edge.
y: f32,
device_pixel_ratio: f32,
height: f32,
}

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,
}
}
}

0 comments on commit a239ca4

Please sign in to comment.