From 9cd99b898bb027b1a187c15919610cac111bb248 Mon Sep 17 00:00:00 2001 From: yukkop Date: Sat, 6 Apr 2024 02:06:45 +0200 Subject: [PATCH] fix: get_just_pressed --- crate/bevy_controls/src/plugin.rs | 2 +- crate/bevy_controls/src/resource.rs | 36 ++++++++++++++--------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/crate/bevy_controls/src/plugin.rs b/crate/bevy_controls/src/plugin.rs index 6b0ca6d..d98f891 100644 --- a/crate/bevy_controls/src/plugin.rs +++ b/crate/bevy_controls/src/plugin.rs @@ -153,7 +153,7 @@ impl, Gs: GameState> ControlsPlugin } } - log::info!("{:#?}", collected_inputs); + //log::info!("{:#?}", collected_inputs); //log::info!("{:#?}", action_binding_pairs[0]); match &binding.input { diff --git a/crate/bevy_controls/src/resource.rs b/crate/bevy_controls/src/resource.rs index bb3a415..8cd1c7d 100644 --- a/crate/bevy_controls/src/resource.rs +++ b/crate/bevy_controls/src/resource.rs @@ -2,10 +2,7 @@ use std::error; use bevy_derive::{Deref, DerefMut}; use bevy_ecs::system::Resource; -use bevy_input::{ - keyboard::KeyCode, - mouse::MouseButton, -}; +use bevy_input::{keyboard::KeyCode, mouse::MouseButton}; #[cfg(feature = "inspector-egui")] use bevy_inspector_egui::prelude::*; #[cfg(all(feature = "reflect", feature = "serialize"))] @@ -122,14 +119,10 @@ impl PlayerActions { /// Returns `true` if current `InputValue` has become `InputValue::Boolean(true)` in this frame /// If input has not same type as `InputValue` on previous frame return `Error()` pub fn get_just_pressed(&self, action: A) -> Result> { - // SAFETY: action is always valid - // because we iterate over all actions in `default` method - if let InputValue::Boolean(current) = *self.current.get(&action).unwrap() { - if let InputValue::Boolean(previous) = *self.previous.get(&action).unwrap() { - return Ok(current && !previous); - } - return Err("Previous input is not boolean".into()); - } + return Ok( + self.current.get(&action).unwrap().to_boolean() + && !self.previous.get(&action).unwrap().to_boolean(), + ); Err("This input is not boolean".into()) } @@ -300,13 +293,12 @@ common_traits_conditions! { } impl ButtonCombination { - pub fn len(&self) -> usize { - use crate::resource::ButtonCombination::*; - match self { - Single(_) => 1 - // TODO: Chord - } + pub fn len(&self) -> usize { + use crate::resource::ButtonCombination::*; + match self { + Single(_) => 1, // TODO: Chord } + } } common_traits_conditions! { @@ -428,7 +420,13 @@ impl InputValue { pub fn to_float(&self) -> f32 { match self { - InputValue::Boolean(value) => if *value { 1. } else { 0. }, + InputValue::Boolean(value) => { + if *value { + 1. + } else { + 0. + } + } InputValue::Float(value) => *value, InputValue::Empty => 0., }