summaryrefslogtreecommitdiff
path: root/engine/src
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src')
-rw-r--r--engine/src/input.rs34
1 files changed, 23 insertions, 11 deletions
diff --git a/engine/src/input.rs b/engine/src/input.rs
index c8ba16a..e292b76 100644
--- a/engine/src/input.rs
+++ b/engine/src/input.rs
@@ -15,7 +15,7 @@ mod reexports
pub use reexports::*;
-#[derive(Debug, Clone, Default, Sole)]
+#[derive(Debug, Clone, Sole)]
pub struct Keys
{
map: HashMap<Key, KeyState>,
@@ -26,33 +26,45 @@ impl Keys
#[must_use]
pub fn new() -> Self
{
- Self::default()
+ Self {
+ map: Key::KEYS
+ .iter()
+ .map(|key| (*key, KeyState::Released))
+ .collect(),
+ }
}
#[must_use]
pub fn get_key_state(&self, key: Key) -> KeyState
{
- self.map.get(&key).copied().unwrap_or(KeyState::Released)
+ self.map.get(&key).copied().expect("Key not in key map")
}
- pub fn set_key_state(&mut self, key: Key, key_state: KeyState)
+ pub fn set_key_state(&mut self, key: Key, new_key_state: KeyState)
{
- if matches!(key_state, KeyState::Released) {
- self.map.remove(&key);
+ if matches!(new_key_state, KeyState::Repeat) {
return;
}
- if matches!(key_state, KeyState::Repeat) {
- return;
- }
+ let key_state = self.map.get_mut(&key).expect("Key not in key map");
- self.map.insert(key, key_state);
+ *key_state = new_key_state;
}
#[must_use]
pub fn is_anything_pressed(&self) -> bool
{
- !self.map.is_empty()
+ self.map
+ .values()
+ .any(|key_state| matches!(key_state, KeyState::Pressed))
+ }
+}
+
+impl Default for Keys
+{
+ fn default() -> Self
+ {
+ Self::new()
}
}