2021-01-12 21:21:45 +00:00
|
|
|
use bevy_utils::HashMap;
|
2020-11-11 01:06:55 +00:00
|
|
|
use std::hash::Hash;
|
2020-09-18 21:43:47 +00:00
|
|
|
|
2022-04-18 21:26:54 +00:00
|
|
|
/// Stores the position data of the input devices of type `T`.
|
2021-12-11 23:31:46 +00:00
|
|
|
///
|
2022-04-18 21:26:54 +00:00
|
|
|
/// The values are stored as `f32`s, which range from [`Axis::MIN`] to [`Axis::MAX`], inclusive.
|
2020-10-08 18:43:01 +00:00
|
|
|
#[derive(Debug)]
|
2020-09-18 21:43:47 +00:00
|
|
|
pub struct Axis<T> {
|
2022-04-18 21:26:54 +00:00
|
|
|
/// The position data of the input devices.
|
2020-09-18 21:43:47 +00:00
|
|
|
axis_data: HashMap<T, f32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Default for Axis<T>
|
|
|
|
where
|
|
|
|
T: Copy + Eq + Hash,
|
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
Axis {
|
2021-01-12 21:21:45 +00:00
|
|
|
axis_data: HashMap::default(),
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Axis<T>
|
|
|
|
where
|
|
|
|
T: Copy + Eq + Hash,
|
|
|
|
{
|
2022-04-18 21:26:54 +00:00
|
|
|
/// The smallest possible axis value.
|
2021-12-11 23:31:46 +00:00
|
|
|
pub const MIN: f32 = -1.0;
|
2022-04-18 21:26:54 +00:00
|
|
|
|
|
|
|
/// The largest possible axis value.
|
2021-12-11 23:31:46 +00:00
|
|
|
pub const MAX: f32 = 1.0;
|
|
|
|
|
2022-04-18 21:26:54 +00:00
|
|
|
/// Sets the position data of the `input_device` to `position_data`.
|
2021-12-11 23:31:46 +00:00
|
|
|
///
|
2022-04-18 21:26:54 +00:00
|
|
|
/// The `position_data` is clamped to be between [`Axis::MIN`] and [`Axis::MAX`], inclusive.
|
2021-12-11 23:31:46 +00:00
|
|
|
///
|
2022-04-18 21:26:54 +00:00
|
|
|
/// If the `input_device`:
|
|
|
|
/// - was present before, the position data is updated, and the old value is returned.
|
|
|
|
/// - wasn't present before, [None] is returned.
|
2021-12-11 23:31:46 +00:00
|
|
|
pub fn set(&mut self, input_device: T, position_data: f32) -> Option<f32> {
|
|
|
|
let new_position_data = position_data.clamp(Self::MIN, Self::MAX);
|
|
|
|
self.axis_data.insert(input_device, new_position_data)
|
|
|
|
}
|
|
|
|
|
2022-04-18 21:26:54 +00:00
|
|
|
/// Returns a position data corresponding to the `input_device`.
|
2021-12-11 23:31:46 +00:00
|
|
|
pub fn get(&self, input_device: T) -> Option<f32> {
|
|
|
|
self.axis_data.get(&input_device).copied()
|
|
|
|
}
|
2022-04-18 21:26:54 +00:00
|
|
|
/// Removes the position data of the `input_device`, returning the position data if the input device was previously set.
|
2021-12-11 23:31:46 +00:00
|
|
|
pub fn remove(&mut self, input_device: T) -> Option<f32> {
|
|
|
|
self.axis_data.remove(&input_device)
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
2021-12-11 23:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::{
|
|
|
|
gamepad::{Gamepad, GamepadButton, GamepadButtonType},
|
|
|
|
Axis,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_axis_set() {
|
|
|
|
let cases = [
|
|
|
|
(-1.5, Some(-1.0)),
|
|
|
|
(-1.1, Some(-1.0)),
|
|
|
|
(-1.0, Some(-1.0)),
|
|
|
|
(-0.9, Some(-0.9)),
|
|
|
|
(-0.1, Some(-0.1)),
|
|
|
|
(0.0, Some(0.0)),
|
|
|
|
(0.1, Some(0.1)),
|
|
|
|
(0.9, Some(0.9)),
|
|
|
|
(1.0, Some(1.0)),
|
|
|
|
(1.1, Some(1.0)),
|
|
|
|
(1.6, Some(1.0)),
|
|
|
|
];
|
2020-09-18 21:43:47 +00:00
|
|
|
|
2021-12-11 23:31:46 +00:00
|
|
|
for (value, expected) in cases {
|
|
|
|
let gamepad_button = GamepadButton(Gamepad(1), GamepadButtonType::RightTrigger);
|
|
|
|
let mut axis = Axis::<GamepadButton>::default();
|
|
|
|
|
|
|
|
axis.set(gamepad_button, value);
|
|
|
|
|
|
|
|
let actual = axis.get(gamepad_button);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 23:31:46 +00:00
|
|
|
#[test]
|
|
|
|
fn test_axis_remove() {
|
|
|
|
let cases = [-1.0, -0.9, -0.1, 0.0, 0.1, 0.9, 1.0];
|
|
|
|
|
|
|
|
for value in cases {
|
|
|
|
let gamepad_button = GamepadButton(Gamepad(1), GamepadButtonType::RightTrigger);
|
|
|
|
let mut axis = Axis::<GamepadButton>::default();
|
|
|
|
|
|
|
|
axis.set(gamepad_button, value);
|
|
|
|
assert!(axis.get(gamepad_button).is_some());
|
|
|
|
|
|
|
|
axis.remove(gamepad_button);
|
|
|
|
let actual = axis.get(gamepad_button);
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
|
|
|
}
|