zng_view_api/
mouse.rs

1//! Mouse types.
2
3use serde::{Deserialize, Serialize};
4
5use zng_unit::Px;
6
7/// Identifier for a specific button on some device.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct ButtonId(pub u32);
11
12/// State a [`MouseButton`] has entered.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
14pub enum ButtonState {
15    /// The button was pressed.
16    Pressed,
17    /// The button was released.
18    Released,
19}
20
21/// Describes a button of a mouse controller.
22#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
23pub enum MouseButton {
24    /// Left button.
25    Left,
26    /// Right button.
27    Right,
28    /// Middle button.
29    Middle,
30    /// Back button.
31    Back,
32    /// Forward button.
33    Forward,
34    /// Any other button.
35    Other(u16),
36}
37
38/// Describes a difference in the mouse scroll wheel state.
39#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
40pub enum MouseScrollDelta {
41    /// Amount in lines or rows to scroll in the horizontal
42    /// and vertical directions.
43    ///
44    /// Positive values indicate rightwards movement in the X axis and downwards movement in the Y axis.
45    LineDelta(f32, f32),
46    /// Amount in pixels to scroll in the horizontal and
47    /// vertical direction.
48    ///
49    /// Scroll events are expressed as a pixel delta if
50    /// supported by the device (eg. a touchpad) and
51    /// platform.
52    PixelDelta(f32, f32),
53}
54impl MouseScrollDelta {
55    /// Gets the sign status of x and y.
56    ///
57    /// Positive values indicate rightwards movement in the X axis and downwards movement in the Y axis.
58    pub fn is_sign_positive(self) -> euclid::BoolVector2D {
59        match self {
60            MouseScrollDelta::LineDelta(x, y) | MouseScrollDelta::PixelDelta(x, y) => euclid::BoolVector2D {
61                x: x.is_sign_positive(),
62                y: y.is_sign_positive(),
63            },
64        }
65    }
66
67    /// Gets the sign status of x and y.
68    ///
69    /// Negative values indicate leftwards movement in the X axis and upwards movement in the Y axis.
70    pub fn is_sign_negative(self) -> euclid::BoolVector2D {
71        self.is_sign_positive().not()
72    }
73
74    /// Gets the pixel delta, line delta is converted using the `line_size`.
75    pub fn delta(self, line_size: euclid::Size2D<f32, Px>) -> euclid::Vector2D<f32, Px> {
76        match self {
77            MouseScrollDelta::LineDelta(x, y) => euclid::vec2(line_size.width * x, line_size.height * y),
78            MouseScrollDelta::PixelDelta(x, y) => euclid::vec2(x, y),
79        }
80    }
81}