2020-08-29 00:08:51 +00:00
|
|
|
use bevy_utils::HashSet;
|
|
|
|
use std::hash::Hash;
|
2020-06-05 05:48:53 +00:00
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// A "press-able" input of type `T`
|
2020-10-01 17:58:21 +00:00
|
|
|
#[derive(Debug)]
|
2020-06-05 06:34:21 +00:00
|
|
|
pub struct Input<T> {
|
|
|
|
pressed: HashSet<T>,
|
|
|
|
just_pressed: HashSet<T>,
|
|
|
|
just_released: HashSet<T>,
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
impl<T> Default for Input<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
pressed: Default::default(),
|
|
|
|
just_pressed: Default::default(),
|
|
|
|
just_released: Default::default(),
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-05 06:34:21 +00:00
|
|
|
}
|
2020-06-05 05:48:53 +00:00
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
impl<T> Input<T>
|
|
|
|
where
|
|
|
|
T: Copy + Eq + Hash,
|
|
|
|
{
|
|
|
|
pub fn press(&mut self, input: T) {
|
|
|
|
if !self.pressed(input) {
|
|
|
|
self.just_pressed.insert(input);
|
|
|
|
}
|
2020-06-05 05:48:53 +00:00
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
self.pressed.insert(input);
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
pub fn pressed(&self, input: T) -> bool {
|
|
|
|
self.pressed.contains(&input)
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
pub fn release(&mut self, input: T) {
|
|
|
|
self.pressed.remove(&input);
|
|
|
|
self.just_released.insert(input);
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
pub fn just_pressed(&self, input: T) -> bool {
|
|
|
|
self.just_pressed.contains(&input)
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
pub fn just_released(&self, input: T) -> bool {
|
|
|
|
self.just_released.contains(&input)
|
|
|
|
}
|
2020-06-05 05:48:53 +00:00
|
|
|
|
2020-09-18 21:43:47 +00:00
|
|
|
pub fn reset(&mut self, input: T) {
|
|
|
|
self.pressed.remove(&input);
|
|
|
|
self.just_pressed.remove(&input);
|
|
|
|
self.just_released.remove(&input);
|
|
|
|
}
|
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
pub fn update(&mut self) {
|
|
|
|
self.just_pressed.clear();
|
|
|
|
self.just_released.clear();
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
2020-09-04 00:32:17 +00:00
|
|
|
|
|
|
|
pub fn get_pressed(&self) -> impl ExactSizeIterator<Item = &T> {
|
|
|
|
self.pressed.iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_just_pressed(&self) -> impl ExactSizeIterator<Item = &T> {
|
|
|
|
self.just_pressed.iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_just_released(&self) -> impl ExactSizeIterator<Item = &T> {
|
|
|
|
self.just_released.iter()
|
|
|
|
}
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
2020-12-01 06:58:49 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn input_test() {
|
|
|
|
use crate::Input;
|
|
|
|
|
|
|
|
/// Used for testing `Input` functionality
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
|
|
|
|
enum DummyInput {
|
|
|
|
Input1,
|
|
|
|
Input2,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut input = Input::default();
|
|
|
|
|
|
|
|
// Test pressing
|
|
|
|
input.press(DummyInput::Input1);
|
|
|
|
input.press(DummyInput::Input2);
|
|
|
|
|
|
|
|
// Check if they were "just pressed" (pressed on this update)
|
|
|
|
assert!(input.just_pressed(DummyInput::Input1));
|
|
|
|
assert!(input.just_pressed(DummyInput::Input2));
|
|
|
|
|
|
|
|
// Check if they are also marked as pressed
|
|
|
|
assert!(input.pressed(DummyInput::Input1));
|
|
|
|
assert!(input.pressed(DummyInput::Input2));
|
|
|
|
|
|
|
|
// Update the `Input` and check press state
|
|
|
|
input.update();
|
|
|
|
|
|
|
|
// Check if they're marked "just pressed"
|
|
|
|
assert!(!input.just_pressed(DummyInput::Input1));
|
|
|
|
assert!(!input.just_pressed(DummyInput::Input2));
|
|
|
|
|
|
|
|
// Check if they're marked as pressed
|
|
|
|
assert!(input.pressed(DummyInput::Input1));
|
|
|
|
assert!(input.pressed(DummyInput::Input2));
|
|
|
|
|
|
|
|
// Release the inputs and check state
|
|
|
|
|
|
|
|
input.release(DummyInput::Input1);
|
|
|
|
input.release(DummyInput::Input2);
|
|
|
|
|
|
|
|
// Check if they're marked as "just released" (released on this update)
|
|
|
|
assert!(input.just_released(DummyInput::Input1));
|
|
|
|
assert!(input.just_released(DummyInput::Input2));
|
|
|
|
|
|
|
|
// Check that they're not incorrectly marked as pressed
|
|
|
|
assert!(!input.pressed(DummyInput::Input1));
|
|
|
|
assert!(!input.pressed(DummyInput::Input2));
|
|
|
|
|
|
|
|
// Update the `Input` and check for removal from `just_released`
|
|
|
|
|
|
|
|
input.update();
|
|
|
|
|
|
|
|
// Check that they're not incorrectly marked as just released
|
|
|
|
assert!(!input.just_released(DummyInput::Input1));
|
|
|
|
assert!(!input.just_released(DummyInput::Input2));
|
|
|
|
|
|
|
|
// Set up an `Input` to test resetting.
|
|
|
|
let mut input = Input::default();
|
|
|
|
|
|
|
|
input.press(DummyInput::Input1);
|
|
|
|
input.release(DummyInput::Input2);
|
|
|
|
|
|
|
|
// Reset the `Input` and test it was reset correctly.
|
|
|
|
input.reset(DummyInput::Input1);
|
|
|
|
input.reset(DummyInput::Input2);
|
|
|
|
|
|
|
|
assert!(!input.just_pressed(DummyInput::Input1));
|
|
|
|
assert!(!input.pressed(DummyInput::Input1));
|
|
|
|
|
|
|
|
assert!(!input.just_released(DummyInput::Input2));
|
|
|
|
}
|
|
|
|
}
|