Skip to main content

anathema_widgets/components/events/
mouse.rs

1use anathema_geometry::Pos;
2
3#[derive(Debug, Copy, Clone, PartialEq)]
4pub struct MouseEvent {
5    pub x: u16,
6    pub y: u16,
7    pub state: MouseState,
8}
9
10impl MouseEvent {
11    /// Translate the x and y position into a `Position`
12    pub fn pos(&self) -> Pos {
13        (self.x, self.y).into()
14    }
15
16    /// Returns true if the left mouse button is pressed
17    pub fn left_down(&self) -> bool {
18        matches!(self.state, MouseState::Down(MouseButton::Left))
19    }
20
21    /// Returns true if the right mouse button is pressed
22    pub fn right_down(&self) -> bool {
23        matches!(self.state, MouseState::Down(MouseButton::Right))
24    }
25
26    /// Returns true if the middle mouse button is pressed
27    pub fn middle_down(&self) -> bool {
28        matches!(self.state, MouseState::Down(MouseButton::Middle))
29    }
30
31    /// Returns true if the left mouse button is down
32    pub fn is_left_down(&self) -> bool {
33        matches!(
34            self.state,
35            MouseState::Down(MouseButton::Left) | MouseState::Drag(MouseButton::Left)
36        )
37    }
38
39    /// Returns true if the right mouse button is down
40    pub fn is_right_down(&self) -> bool {
41        matches!(
42            self.state,
43            MouseState::Down(MouseButton::Right) | MouseState::Drag(MouseButton::Right)
44        )
45    }
46
47    /// Returns true if the middle mouse button is down
48    pub fn is_middle_down(&self) -> bool {
49        matches!(
50            self.state,
51            MouseState::Down(MouseButton::Middle) | MouseState::Drag(MouseButton::Middle)
52        )
53    }
54
55    /// Returns true if the left mouse button is released
56    pub fn left_up(&self) -> bool {
57        matches!(self.state, MouseState::Up(MouseButton::Left))
58    }
59
60    /// Returns true if the right mouse button is released
61    pub fn right_up(&self) -> bool {
62        matches!(self.state, MouseState::Up(MouseButton::Right))
63    }
64
65    /// Returns true if the middle mouse button is released
66    pub fn middle_up(&self) -> bool {
67        matches!(self.state, MouseState::Up(MouseButton::Middle))
68    }
69}
70
71#[derive(Debug, Copy, Clone, PartialEq)]
72pub enum MouseState {
73    Down(MouseButton),
74    Up(MouseButton),
75    Drag(MouseButton),
76    Move,
77    ScrollUp,
78    ScrollDown,
79    ScrollLeft,
80    ScrollRight,
81}
82
83#[derive(Debug, Copy, Clone, PartialEq)]
84pub enum MouseButton {
85    Left,
86    Middle,
87    Right,
88}