summaryrefslogtreecommitdiff
path: root/engine/src/windowing/mouse.rs
blob: 1afe5949fb5f0555e34842233f5dca63280d8db3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::collections::HashMap;

use ecs::Sole;

use crate::vector::Vec2;

#[derive(Debug, Default, Clone, Sole)]
#[non_exhaustive]
pub struct Motion
{
    pub position_delta: Vec2<f64>,
}

/// Mouse buttons.
#[derive(Debug, Default, Sole)]
pub struct Buttons
{
    map: HashMap<Button, ButtonData>,
}

impl Buttons
{
    pub fn get(&self, button: Button) -> ButtonState
    {
        let Some(button_data) = self.map.get(&button) else {
            return ButtonState::Released;
        };

        button_data.current_state
    }

    pub fn get_previous(&self, button: Button) -> ButtonState
    {
        let Some(button_data) = self.map.get(&button) else {
            return ButtonState::Released;
        };

        button_data.previous_state
    }

    pub fn set(&mut self, button: Button, button_state: ButtonState)
    {
        let button_data = self.map.entry(button).or_default();

        button_data.current_state = button_state;
    }

    pub(crate) fn make_states_previous(&mut self)
    {
        for button_data in self.map.values_mut() {
            button_data.previous_state = button_data.current_state;
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Button
{
    Left,
    Right,
    Middle,
    Back,
    Forward,
    Other(u16),
}

impl From<winit::event::MouseButton> for Button
{
    fn from(mouse_button: winit::event::MouseButton) -> Self
    {
        match mouse_button {
            winit::event::MouseButton::Left => Self::Left,
            winit::event::MouseButton::Right => Self::Right,
            winit::event::MouseButton::Middle => Self::Middle,
            winit::event::MouseButton::Back => Self::Back,
            winit::event::MouseButton::Forward => Self::Forward,
            winit::event::MouseButton::Other(other_mouse_button) => {
                Self::Other(other_mouse_button)
            }
        }
    }
}

/// Mouse button state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ButtonState
{
    Pressed,
    Released,
}

impl ButtonState
{
    #[must_use]
    #[inline]
    pub fn is_pressed(&self) -> bool
    {
        matches!(self, Self::Pressed)
    }

    #[must_use]
    #[inline]
    pub fn is_released(&self) -> bool
    {
        matches!(self, Self::Released)
    }
}

impl From<winit::event::ElementState> for ButtonState
{
    fn from(element_state: winit::event::ElementState) -> Self
    {
        match element_state {
            winit::event::ElementState::Pressed => Self::Pressed,
            winit::event::ElementState::Released => Self::Released,
        }
    }
}

#[derive(Debug)]
struct ButtonData
{
    current_state: ButtonState,
    previous_state: ButtonState,
}

impl Default for ButtonData
{
    fn default() -> Self
    {
        Self {
            current_state: ButtonState::Released,
            previous_state: ButtonState::Released,
        }
    }
}