shine_input/
state.rs

1use crate::ButtonId;
2use crate::{ModifierFilterMask, ModifierId, ModifierMask};
3use std::collections::HashMap;
4
5/// State of a button
6#[derive(Clone, Default, Debug)]
7struct ButtonState {
8    /// Reset button automatically in prepare pass (ex: used for mouse move to reset axis when mouse is not moved)
9    autoreset: bool,
10
11    /// Required modifier mask
12    modifier_mask: ModifierFilterMask,
13
14    /// Current value in the [-1,1] range
15    value: f32,
16}
17
18/// Store the current input state
19pub struct State {
20    time: u128,                              // The last update time
21    cursore_position: (f32, f32),            // Cursore poisition on the normalize [0,1]^2 screen
22    modifier_mask: ModifierMask,             // Current modifiers
23    autoreset_modifiers: ModifierMask,       // Autoreset modifiers
24    buttons: HashMap<ButtonId, ButtonState>, // State of the buttons
25}
26
27impl State {
28    pub fn new() -> State {
29        State {
30            time: 0,
31            cursore_position: (0., 0.),
32            modifier_mask: ModifierMask::default(),
33            autoreset_modifiers: ModifierMask::default(),
34            buttons: HashMap::new(),
35        }
36    }
37
38    pub fn clear(&mut self) {
39        self.buttons.clear();
40        self.modifier_mask.clear();
41        self.autoreset_modifiers.clear();
42        self.time = 0;
43        self.cursore_position = (0., 0.);
44    }
45
46    /// Copy the previous state
47    pub fn prepare(&mut self, prev: &State, time: u128) {
48        self.clear();
49
50        self.time = time;
51
52        self.modifier_mask = ModifierMask::from_masked_clear(&prev.modifier_mask, &prev.autoreset_modifiers);
53
54        self.buttons.clear();
55        for (k, j) in prev.buttons.iter() {
56            if j.autoreset {
57                continue;
58            }
59
60            self.buttons.insert(*k, j.clone());
61        }
62    }
63
64    pub fn get_time(&self) -> u128 {
65        self.time
66    }
67
68    pub fn set_modifier(&mut self, modifier_id: ModifierId, pressed: bool, autoreset: bool) {
69        self.modifier_mask.set(modifier_id, pressed);
70        self.autoreset_modifiers.set(modifier_id, autoreset);
71    }
72
73    pub fn is_modifier(&self, modifier_id: ModifierId) -> bool {
74        self.modifier_mask.get(modifier_id)
75    }
76
77    pub fn remove_button(&mut self, button_id: ButtonId) {
78        let _ = self.buttons.remove(&button_id);
79    }
80
81    pub fn set_button(&mut self, button_id: ButtonId, modifier_mask: ModifierFilterMask, value: f32, autoreset: bool) {
82        // clamp input to [-1,1]
83        let value = if value > 1. {
84            1.
85        } else if value < -1. {
86            -1.
87        } else {
88            value
89        };
90
91        if value == 0. {
92            self.remove_button(button_id);
93        } else {
94            let entry = self.buttons.entry(button_id);
95            let mut state = entry.or_insert_with(ButtonState::default);
96            state.value = value;
97            state.modifier_mask = modifier_mask;
98            state.autoreset = autoreset;
99        }
100    }
101
102    pub fn get_button(&self, button_id: ButtonId) -> f32 {
103        match self.buttons.get(&button_id) {
104            None => 0.,
105            Some(ref s) => {
106                log::trace!("s: {:?}", s);
107                if s.modifier_mask.check(&self.modifier_mask) {
108                    s.value
109                } else {
110                    0.
111                }
112            }
113        }
114    }
115
116    pub fn is_button(&self, button_id: ButtonId) -> bool {
117        self.get_button(button_id) != 0.
118    }
119}
120
121impl Default for State {
122    fn default() -> Self {
123        State::new()
124    }
125}