bevy_enhanced_input/input_condition/
press.rs

1use bevy::prelude::*;
2
3use super::{DEFAULT_ACTUATION, InputCondition};
4use crate::{
5    action_map::{ActionMap, ActionState},
6    action_value::ActionValue,
7};
8
9/// Returns [`ActionState::Fired`] when the input exceeds the actuation threshold.
10#[derive(Clone, Copy, Debug)]
11pub struct Press {
12    /// Trigger threshold.
13    pub actuation: f32,
14}
15
16impl Press {
17    #[must_use]
18    pub fn new(actuation: f32) -> Self {
19        Self { actuation }
20    }
21}
22
23impl Default for Press {
24    fn default() -> Self {
25        Self::new(DEFAULT_ACTUATION)
26    }
27}
28
29impl InputCondition for Press {
30    fn evaluate(
31        &mut self,
32        _action_map: &ActionMap,
33        _time: &Time<Virtual>,
34        value: ActionValue,
35    ) -> ActionState {
36        if value.is_actuated(self.actuation) {
37            ActionState::Fired
38        } else {
39            ActionState::None
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47    use crate::action_map::ActionMap;
48
49    #[test]
50    fn down() {
51        let mut condition = Press::new(1.0);
52        let action_map = ActionMap::default();
53        let time = Time::default();
54
55        assert_eq!(
56            condition.evaluate(&action_map, &time, 0.0.into()),
57            ActionState::None
58        );
59        assert_eq!(
60            condition.evaluate(&action_map, &time, 1.0.into()),
61            ActionState::Fired,
62        );
63    }
64}