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)]
23#[non_exhaustive]
24pub enum MouseButton {
25 /// Left button.
26 Left,
27 /// Right button.
28 Right,
29 /// Middle button.
30 Middle,
31 /// Back button.
32 Back,
33 /// Forward button.
34 Forward,
35 /// Any other button.
36 Other(u16),
37}
38
39/// Describes a difference in the mouse scroll wheel state.
40#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
41#[non_exhaustive]
42pub enum MouseScrollDelta {
43 /// Amount in lines or rows to scroll in the horizontal
44 /// and vertical directions.
45 ///
46 /// Positive values indicate rightwards movement in the X axis and downwards movement in the Y axis.
47 LineDelta(f32, f32),
48 /// Amount in pixels to scroll in the horizontal and
49 /// vertical direction.
50 ///
51 /// Scroll events are expressed as a pixel delta if
52 /// supported by the device (eg. a touchpad) and
53 /// platform.
54 PixelDelta(f32, f32),
55}
56impl MouseScrollDelta {
57 /// Gets the sign status of x and y.
58 ///
59 /// Positive values indicate rightwards movement in the X axis and downwards movement in the Y axis.
60 pub fn is_sign_positive(self) -> euclid::BoolVector2D {
61 match self {
62 MouseScrollDelta::LineDelta(x, y) | MouseScrollDelta::PixelDelta(x, y) => euclid::BoolVector2D {
63 x: x.is_sign_positive(),
64 y: y.is_sign_positive(),
65 },
66 }
67 }
68
69 /// Gets the sign status of x and y.
70 ///
71 /// Negative values indicate leftwards movement in the X axis and upwards movement in the Y axis.
72 pub fn is_sign_negative(self) -> euclid::BoolVector2D {
73 self.is_sign_positive().not()
74 }
75
76 /// Gets the pixel delta, line delta is converted using the `line_size`.
77 pub fn delta(self, line_size: euclid::Size2D<f32, Px>) -> euclid::Vector2D<f32, Px> {
78 match self {
79 MouseScrollDelta::LineDelta(x, y) => euclid::vec2(line_size.width * x, line_size.height * y),
80 MouseScrollDelta::PixelDelta(x, y) => euclid::vec2(x, y),
81 }
82 }
83}