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
use std::collections::HashMap; use std::fmt::Debug; use std::hash::Hash; use bevy_ecs::prelude::ResMut; #[derive(Debug)] pub struct Input<T: 'static + Debug + Sized + Eq + Hash + Send + Sync> { pub(crate) states: HashMap<T, u32>, } impl<T: 'static + Debug + Sized + Eq + Hash + Send + Sync> Clone for Input<T> where T: Clone, { fn clone(&self) -> Self { Self { states: self.states.clone(), } } } impl<T: 'static + Debug + Sized + Eq + Hash + Send + Sync> Default for Input<T> { fn default() -> Self { Self { states: Default::default(), } } } impl<T: 'static + Debug + Sized + Eq + Hash + Send + Sync> Input<T> { pub fn new() -> Self { Self { states: HashMap::new(), } } pub fn insert(&mut self, key: T, state: bool) { if state { self.states.insert(key, 0); } else { self.states.remove(&key); } } pub fn take(&mut self, key: T) -> bool { self.states.remove(&key).is_some() } pub fn update(mut keys: ResMut<Input<T>>) { keys.states.iter_mut().for_each(|(_, k)| *k += 1); } pub fn just_pressed(&self, key: T) -> bool { if let Some(state) = self.states.get(&key) { *state == 0 } else { false } } pub fn pressed(&self, key: T) -> bool { self.states.get(&key).is_some() } }