Skip to content

Commit

Permalink
Use f32 for actuation
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Oct 15, 2024
1 parent e7b275a commit 2bf7db3
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 130 deletions.
18 changes: 18 additions & 0 deletions src/action_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ impl ActionValue {
}
}

/// Returns `true` if the value in sufficiently large.
pub fn is_actuated(self, actuation: f32) -> bool {
let value = match self {
ActionValue::Bool(value) => {
if value {
1.0
} else {
0.0
}
}
ActionValue::Axis1D(value) => value * value,
ActionValue::Axis2D(value) => value.length_squared(),
ActionValue::Axis3D(value) => value.length_squared(),
};

value >= actuation * actuation
}

/// Returns the value as a boolean.
///
/// If the value is not [`ActionValue::Bool`],
Expand Down
4 changes: 3 additions & 1 deletion src/input_context/input_condition.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub mod blocked_by;
pub mod chord;
pub mod down;
pub mod held_timer;
pub mod hold;
pub mod hold_and_release;
pub mod pressed;
pub mod primitives;
pub mod pulse;
pub mod released;
pub mod tap;
Expand All @@ -16,6 +16,8 @@ use bevy::prelude::*;
use super::input_action::{ActionState, ActionsData};
use crate::action_value::ActionValue;

pub const DEFAULT_ACTUATION: f32 = 0.5;

/// Defines how input activates.
///
/// Most conditions analyze the input itself, checking for minimum actuation values
Expand Down
16 changes: 11 additions & 5 deletions src/input_context/input_condition/down.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
use bevy::prelude::*;

use super::{primitives::Actuation, InputCondition};
use super::{InputCondition, DEFAULT_ACTUATION};
use crate::{
action_value::ActionValue,
input_context::input_action::{ActionState, ActionsData},
};

/// Returns [`ActionState::Fired`] when the input exceeds the actuation threshold.
#[derive(Default, Debug)]
#[derive(Debug)]
pub struct Down {
/// Trigger threshold.
pub actuation: Actuation,
pub actuation: f32,
}

impl Down {
#[must_use]
pub fn new(actuation: Actuation) -> Self {
pub fn new(actuation: f32) -> Self {
Self { actuation }
}
}

impl Default for Down {
fn default() -> Self {
Self::new(DEFAULT_ACTUATION)
}
}

impl InputCondition for Down {
fn evaluate(
&mut self,
Expand All @@ -28,7 +34,7 @@ impl InputCondition for Down {
_delta: f32,
value: ActionValue,
) -> ActionState {
if self.actuation.is_actuated(value) {
if value.is_actuated(self.actuation) {
ActionState::Fired
} else {
ActionState::None
Expand Down
38 changes: 38 additions & 0 deletions src/input_context/input_condition/held_timer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use bevy::prelude::*;

/// Helper for building triggers that have firing conditions governed by elapsed time.
#[derive(Default, Debug)]
pub struct HeldTimer {
/// If set to `true`, [`Time::relative_speed`] will be applied to the held duration.
///
/// By default is set to `false`.
pub relative_to_speed: bool,

duration: f32,
}

impl HeldTimer {
pub fn relative_to_speed(relative_to_speed: bool) -> Self {
Self {
relative_to_speed,
duration: 0.0,
}
}

pub fn update(&mut self, world: &World, mut delta: f32) {
if self.relative_to_speed {
let time = world.resource::<Time<Virtual>>();
delta *= time.relative_speed()
}

self.duration += delta;
}

pub fn reset(&mut self) {
self.duration = 0.0;
}

pub fn duration(&self) -> f32 {
self.duration
}
}
15 changes: 6 additions & 9 deletions src/input_context/input_condition/hold.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use bevy::prelude::*;

use super::{
primitives::{Actuation, HeldTimer},
InputCondition,
};
use super::{held_timer::HeldTimer, InputCondition, DEFAULT_ACTUATION};
use crate::{
action_value::ActionValue,
input_context::input_action::{ActionState, ActionsData},
Expand All @@ -23,7 +20,7 @@ pub struct Hold {
pub one_shot: bool,

/// Trigger threshold.
pub actuation: Actuation,
pub actuation: f32,

held_timer: HeldTimer,

Expand All @@ -36,7 +33,7 @@ impl Hold {
Self {
hold_time,
one_shot: false,
actuation: Default::default(),
actuation: DEFAULT_ACTUATION,
held_timer: Default::default(),
fired: false,
}
Expand All @@ -49,8 +46,8 @@ impl Hold {
}

#[must_use]
pub fn with_actuation(mut self, actuation: impl Into<Actuation>) -> Self {
self.actuation = actuation.into();
pub fn with_actuation(mut self, actuation: f32) -> Self {
self.actuation = actuation;
self
}

Expand All @@ -69,7 +66,7 @@ impl InputCondition for Hold {
delta: f32,
value: ActionValue,
) -> ActionState {
let actuated = self.actuation.is_actuated(value);
let actuated = value.is_actuated(self.actuation);
if actuated {
self.held_timer.update(world, delta);
} else {
Expand Down
15 changes: 6 additions & 9 deletions src/input_context/input_condition/hold_and_release.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use bevy::prelude::*;

use super::{
primitives::{Actuation, HeldTimer},
InputCondition,
};
use super::{held_timer::HeldTimer, InputCondition, DEFAULT_ACTUATION};
use crate::{
action_value::ActionValue,
input_context::input_action::{ActionState, ActionsData},
Expand All @@ -19,7 +16,7 @@ pub struct HoldAndRelease {
pub hold_time: f32,

/// Trigger threshold.
pub actuation: Actuation,
pub actuation: f32,

held_timer: HeldTimer,
}
Expand All @@ -29,14 +26,14 @@ impl HoldAndRelease {
pub fn new(hold_time: f32) -> Self {
Self {
hold_time,
actuation: Default::default(),
actuation: DEFAULT_ACTUATION,
held_timer: Default::default(),
}
}

#[must_use]
pub fn with_actuation(mut self, actuation: impl Into<Actuation>) -> Self {
self.actuation = actuation.into();
pub fn with_actuation(mut self, actuation: f32) -> Self {
self.actuation = actuation;
self
}

Expand All @@ -61,7 +58,7 @@ impl InputCondition for HoldAndRelease {
self.held_timer.update(world, delta);
let held_duration = self.held_timer.duration();

if self.actuation.is_actuated(value) {
if value.is_actuated(self.actuation) {
ActionState::Ongoing
} else {
self.held_timer.reset();
Expand Down
16 changes: 11 additions & 5 deletions src/input_context/input_condition/pressed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::prelude::*;

use super::{primitives::Actuation, InputCondition};
use super::{InputCondition, DEFAULT_ACTUATION};
use crate::{
action_value::ActionValue,
input_context::input_action::{ActionState, ActionsData},
Expand All @@ -9,23 +9,29 @@ use crate::{
/// Like [`super::down::Down`] but returns [`ActionState::Fired`] only once until the next actuation.
///
/// Holding the input will not cause further triggers.
#[derive(Default, Debug)]
#[derive(Debug)]
pub struct Pressed {
/// Trigger threshold.
pub actuation: Actuation,
pub actuation: f32,
actuated: bool,
}

impl Pressed {
#[must_use]
pub fn new(actuation: Actuation) -> Self {
pub fn new(actuation: f32) -> Self {
Self {
actuation,
actuated: false,
}
}
}

impl Default for Pressed {
fn default() -> Self {
Self::new(DEFAULT_ACTUATION)
}
}

impl InputCondition for Pressed {
fn evaluate(
&mut self,
Expand All @@ -35,7 +41,7 @@ impl InputCondition for Pressed {
value: ActionValue,
) -> ActionState {
let previosly_actuated = self.actuated;
self.actuated = self.actuation.is_actuated(value);
self.actuated = value.is_actuated(self.actuation);

if self.actuated && !previosly_actuated {
ActionState::Fired
Expand Down
76 changes: 0 additions & 76 deletions src/input_context/input_condition/primitives.rs

This file was deleted.

Loading

0 comments on commit 2bf7db3

Please sign in to comment.