pub trait CustomAxisProcessor:
Send
+ Sync
+ Debug
+ DynClone
+ DynEq
+ DynHash
+ Reflect
+ Serialize {
// Required method
fn process(&self, input_value: f32) -> f32;
}
Expand description
A trait for creating custom processor that handles single-axis input values,
accepting a f32
input and producing a f32
output.
§Examples
use std::hash::{Hash, Hasher};
use bevy::prelude::*;
use bevy::math::FloatOrd;
use serde::{Deserialize, Serialize};
use leafwing_input_manager::prelude::*;
/// Doubles the input, takes the absolute value,
/// and discards results that meet the specified condition.
// If your processor includes fields not implemented Eq and Hash,
// implementation is necessary as shown below.
// Otherwise, you can derive Eq and Hash directly.
#[derive(Debug, Clone, Copy, PartialEq, Reflect, Serialize, Deserialize)]
pub struct DoubleAbsoluteValueThenIgnored(pub f32);
// Add this attribute for ensuring proper serialization and deserialization.
#[serde_typetag]
impl CustomAxisProcessor for DoubleAbsoluteValueThenIgnored {
fn process(&self, input_value: f32) -> f32 {
// Implement the logic just like you would in a normal function.
// You can use other processors within this function.
let value = AxisProcessor::Sensitivity(2.0).process(input_value);
let value = value.abs();
if value == self.0 {
0.0
} else {
value
}
}
}
// Unfortunately, manual implementation is required due to the float field.
impl Eq for DoubleAbsoluteValueThenIgnored {}
impl Hash for DoubleAbsoluteValueThenIgnored {
fn hash<H: Hasher>(&self, state: &mut H) {
// Encapsulate the float field for hashing.
FloatOrd(self.0).hash(state);
}
}
// Remember to register your processor - it will ensure everything works smoothly!
let mut app = App::new();
app.register_axis_processor::<DoubleAbsoluteValueThenIgnored>();
// Now you can use it!
let processor = DoubleAbsoluteValueThenIgnored(4.0);
// Rejected!
assert_eq!(processor.process(2.0), 0.0);
assert_eq!(processor.process(-2.0), 0.0);
// Others are just doubled absolute value.
assert_eq!(processor.process(6.0), 12.0);
assert_eq!(processor.process(4.0), 8.0);
assert_eq!(processor.process(0.0), 0.0);
assert_eq!(processor.process(-4.0), 8.0);
assert_eq!(processor.process(-6.0), 12.0);
// The ways to create an AxisProcessor.
let axis_processor = AxisProcessor::Custom(Box::new(processor));
assert_eq!(axis_processor, AxisProcessor::from(processor));
Required Methods§
Trait Implementations§
source§impl<'de> Deserialize<'de> for Box<dyn CustomAxisProcessor>
impl<'de> Deserialize<'de> for Box<dyn CustomAxisProcessor>
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl FromReflect for Box<dyn CustomAxisProcessor>
impl FromReflect for Box<dyn CustomAxisProcessor>
source§fn from_reflect(reflect: &dyn Reflect) -> Option<Self>
fn from_reflect(reflect: &dyn Reflect) -> Option<Self>
Constructs a concrete instance of
Self
from a reflected value.source§fn take_from_reflect(
reflect: Box<dyn Reflect>,
) -> Result<Self, Box<dyn Reflect>>
fn take_from_reflect( reflect: Box<dyn Reflect>, ) -> Result<Self, Box<dyn Reflect>>
Attempts to downcast the given value to
Self
using,
constructing the value using from_reflect
if that fails. Read moresource§impl GetTypeRegistration for Box<dyn CustomAxisProcessor>
impl GetTypeRegistration for Box<dyn CustomAxisProcessor>
source§fn get_type_registration() -> TypeRegistration
fn get_type_registration() -> TypeRegistration
Returns the default
TypeRegistration
for this type.source§fn register_type_dependencies(registry: &mut TypeRegistry)
fn register_type_dependencies(registry: &mut TypeRegistry)
Registers other types needed by this type. Read more
source§impl<'hash> Hash for dyn CustomAxisProcessor + 'hash
impl<'hash> Hash for dyn CustomAxisProcessor + 'hash
source§impl<'hash> Hash for dyn CustomAxisProcessor + Send + 'hash
impl<'hash> Hash for dyn CustomAxisProcessor + Send + 'hash
source§impl<'hash> Hash for dyn CustomAxisProcessor + Sync + 'hash
impl<'hash> Hash for dyn CustomAxisProcessor + Sync + 'hash
source§impl<'eq> PartialEq<&Box<dyn CustomAxisProcessor + 'eq>> for Box<dyn CustomAxisProcessor + 'eq>
impl<'eq> PartialEq<&Box<dyn CustomAxisProcessor + 'eq>> for Box<dyn CustomAxisProcessor + 'eq>
source§impl<'eq> PartialEq<&Box<dyn CustomAxisProcessor + Send + 'eq>> for Box<dyn CustomAxisProcessor + Send + 'eq>
impl<'eq> PartialEq<&Box<dyn CustomAxisProcessor + Send + 'eq>> for Box<dyn CustomAxisProcessor + Send + 'eq>
source§impl<'eq> PartialEq<&Box<dyn CustomAxisProcessor + Sync + Send + 'eq>> for Box<dyn CustomAxisProcessor + Send + Sync + 'eq>
impl<'eq> PartialEq<&Box<dyn CustomAxisProcessor + Sync + Send + 'eq>> for Box<dyn CustomAxisProcessor + Send + Sync + 'eq>
source§impl<'eq> PartialEq<&Box<dyn CustomAxisProcessor + Sync + 'eq>> for Box<dyn CustomAxisProcessor + Sync + 'eq>
impl<'eq> PartialEq<&Box<dyn CustomAxisProcessor + Sync + 'eq>> for Box<dyn CustomAxisProcessor + Sync + 'eq>
source§impl<'eq> PartialEq for dyn CustomAxisProcessor + 'eq
impl<'eq> PartialEq for dyn CustomAxisProcessor + 'eq
source§impl<'eq> PartialEq for dyn CustomAxisProcessor + Send + 'eq
impl<'eq> PartialEq for dyn CustomAxisProcessor + Send + 'eq
source§impl<'eq> PartialEq for dyn CustomAxisProcessor + Sync + 'eq
impl<'eq> PartialEq for dyn CustomAxisProcessor + Sync + 'eq
source§impl Reflect for Box<dyn CustomAxisProcessor>
impl Reflect for Box<dyn CustomAxisProcessor>
source§fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
Returns the value as a
&mut dyn Any
.source§fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
Casts this type to a boxed reflected value.
source§fn as_reflect(&self) -> &dyn Reflect
fn as_reflect(&self) -> &dyn Reflect
Casts this type to a reflected value.
source§fn as_reflect_mut(&mut self) -> &mut dyn Reflect
fn as_reflect_mut(&mut self) -> &mut dyn Reflect
Casts this type to a mutable reflected value.
source§fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
Performs a type-checked assignment of a reflected value to this value. Read more
source§fn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
Returns a zero-sized enumeration of “kinds” of type. Read more
source§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
Returns an immutable enumeration of “kinds” of type. Read more
source§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
Returns a mutable enumeration of “kinds” of type. Read more
source§fn reflect_owned(self: Box<Self>) -> ReflectOwned
fn reflect_owned(self: Box<Self>) -> ReflectOwned
Returns an owned enumeration of “kinds” of type. Read more
source§fn clone_value(&self) -> Box<dyn Reflect>
fn clone_value(&self) -> Box<dyn Reflect>
Clones the value as a
Reflect
trait object. Read moresource§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
Returns a hash of the value (which includes the type). Read more
source§fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>
Returns a “partial equality” comparison result. Read more
source§fn serializable(&self) -> Option<Serializable<'_>>
fn serializable(&self) -> Option<Serializable<'_>>
Returns a serializable version of the value. Read more
source§fn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
Indicates whether or not this type is a dynamic type. Read more
source§impl<'a> Serialize for dyn CustomAxisProcessor + 'a
impl<'a> Serialize for dyn CustomAxisProcessor + 'a
source§impl TypePath for Box<dyn CustomAxisProcessor>
impl TypePath for Box<dyn CustomAxisProcessor>
source§fn type_path() -> &'static str
fn type_path() -> &'static str
Returns the fully qualified path of the underlying type. Read more
source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
Returns a short, pretty-print enabled path to the type. Read more