Trait CustomDualAxisProcessor

Source
pub trait CustomDualAxisProcessor:
    Send
    + Sync
    + Debug
    + DynClone
    + DynEq
    + DynHash
    + Reflect
    + Serialize {
    // Required method
    fn process(&self, input_value: Vec2) -> Vec2;
}
Expand description

A trait for creating custom processor that handles dual-axis input values, accepting a Vec2 input and producing a Vec2 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 its absolute value,
/// and discards results that meet the specified condition on the X-axis.
// 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 DoubleAbsoluteValueThenRejectX(pub f32);

// Add this attribute for ensuring proper serialization and deserialization.
#[serde_typetag]
impl CustomDualAxisProcessor for DoubleAbsoluteValueThenRejectX {
    fn process(&self, input_value: Vec2) -> Vec2 {
        // Implement the logic just like you would in a normal function.

        // You can use other processors within this function.
        let value = DualAxisSensitivity::all(2.0).scale(input_value);

        let value = value.abs();
        let new_x = if value.x == self.0 {
            0.0
        } else {
            value.x
        };
        Vec2::new(new_x, value.y)
    }
}

// Unfortunately, manual implementation is required due to the float field.
impl Eq for DoubleAbsoluteValueThenRejectX {}
impl Hash for DoubleAbsoluteValueThenRejectX {
    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_dual_axis_processor::<DoubleAbsoluteValueThenRejectX>();

// Now you can use it!
let processor = DoubleAbsoluteValueThenRejectX(4.0);

// Rejected X!
assert_eq!(processor.process(Vec2::splat(2.0)), Vec2::new(0.0, 4.0));
assert_eq!(processor.process(Vec2::splat(-2.0)), Vec2::new(0.0, 4.0));

// Others are just doubled absolute value.
assert_eq!(processor.process(Vec2::splat(6.0)), Vec2::splat(12.0));
assert_eq!(processor.process(Vec2::splat(4.0)), Vec2::splat(8.0));
assert_eq!(processor.process(Vec2::splat(0.0)), Vec2::splat(0.0));
assert_eq!(processor.process(Vec2::splat(-4.0)), Vec2::splat(8.0));
assert_eq!(processor.process(Vec2::splat(-6.0)), Vec2::splat(12.0));

// The ways to create a DualAxisProcessor.
let dual_axis_processor = DualAxisProcessor::Custom(Box::new(processor));
assert_eq!(dual_axis_processor, DualAxisProcessor::from(processor));

Required Methods§

Source

fn process(&self, input_value: Vec2) -> Vec2

Computes the result by processing the input_value.

Trait Implementations§

Source§

impl<'de> Deserialize<'de> for Box<dyn CustomDualAxisProcessor>

Source§

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 CustomDualAxisProcessor>

Source§

fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self>

Constructs a concrete instance of Self from a reflected value.
Source§

fn take_from_reflect( reflect: Box<dyn PartialReflect>, ) -> Result<Self, Box<dyn PartialReflect>>

Attempts to downcast the given value to Self using, constructing the value using from_reflect if that fails. Read more
Source§

impl GetTypeRegistration for Box<dyn CustomDualAxisProcessor>

Source§

fn get_type_registration() -> TypeRegistration

Returns the default TypeRegistration for this type.
Source§

fn register_type_dependencies(_registry: &mut TypeRegistry)

Registers other types needed by this type. Read more
Source§

impl<'hash> Hash for dyn CustomDualAxisProcessor + 'hash

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
Source§

impl<'hash> Hash for dyn CustomDualAxisProcessor + Send + 'hash

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
Source§

impl<'hash> Hash for dyn CustomDualAxisProcessor + Send + Sync + 'hash

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
Source§

impl<'hash> Hash for dyn CustomDualAxisProcessor + Sync + 'hash

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
Source§

impl<'eq> PartialEq<&Box<dyn CustomDualAxisProcessor + 'eq>> for Box<dyn CustomDualAxisProcessor + 'eq>

Source§

fn eq(&self, other: &&Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'eq> PartialEq<&Box<dyn CustomDualAxisProcessor + Send + 'eq>> for Box<dyn CustomDualAxisProcessor + Send + 'eq>

Source§

fn eq(&self, other: &&Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'eq> PartialEq<&Box<dyn CustomDualAxisProcessor + Sync + Send + 'eq>> for Box<dyn CustomDualAxisProcessor + Send + Sync + 'eq>

Source§

fn eq(&self, other: &&Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'eq> PartialEq<&Box<dyn CustomDualAxisProcessor + Sync + 'eq>> for Box<dyn CustomDualAxisProcessor + Sync + 'eq>

Source§

fn eq(&self, other: &&Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'eq> PartialEq for dyn CustomDualAxisProcessor + 'eq

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'eq> PartialEq for dyn CustomDualAxisProcessor + Send + 'eq

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'eq> PartialEq for dyn CustomDualAxisProcessor + Send + Sync + 'eq

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'eq> PartialEq for dyn CustomDualAxisProcessor + Sync + 'eq

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialReflect for Box<dyn CustomDualAxisProcessor>

Source§

fn get_represented_type_info(&self) -> Option<&'static TypeInfo>

Returns the TypeInfo of the type represented by this value. Read more
Source§

fn reflect_kind(&self) -> ReflectKind

Returns a zero-sized enumeration of “kinds” of type. Read more
Source§

fn reflect_ref(&self) -> ReflectRef<'_>

Returns an immutable enumeration of “kinds” of type. Read more
Source§

fn reflect_mut(&mut self) -> ReflectMut<'_>

Returns a mutable enumeration of “kinds” of type. Read more
Source§

fn reflect_owned(self: Box<Self>) -> ReflectOwned

Returns an owned enumeration of “kinds” of type. Read more
Source§

fn clone_value(&self) -> Box<dyn PartialReflect>

👎Deprecated since 0.16.0: to clone reflected values, prefer using reflect_clone. To convert reflected values to dynamic ones, use to_dynamic.
Clones Self into its dynamic representation. Read more
Source§

fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError>

Tries to apply a reflected value to this value. Read more
Source§

fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>

Casts this type to a boxed, reflected value. Read more
Source§

fn as_partial_reflect(&self) -> &dyn PartialReflect

Casts this type to a reflected value. Read more
Source§

fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect

Casts this type to a mutable, reflected value. Read more
Source§

fn try_into_reflect( self: Box<Self>, ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>

Attempts to cast this type to a boxed, fully-reflected value.
Source§

fn try_as_reflect(&self) -> Option<&dyn Reflect>

Attempts to cast this type to a fully-reflected value.
Source§

fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>

Attempts to cast this type to a mutable, fully-reflected value.
Source§

fn apply(&mut self, value: &(dyn PartialReflect + 'static))

Applies a reflected value to this value. Read more
Source§

fn to_dynamic(&self) -> Box<dyn PartialReflect>

Converts this reflected value into its dynamic representation based on its kind. Read more
Source§

fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError>

Attempts to clone Self using reflection. Read more
Source§

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 PartialReflect + 'static), ) -> Option<bool>

Returns a “partial equality” comparison result. Read more
Source§

fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Debug formatter for the value. Read more
Source§

fn is_dynamic(&self) -> bool

Indicates whether or not this type is a dynamic type. Read more
Source§

impl Reflect for Box<dyn CustomDualAxisProcessor>

Source§

fn into_any(self: Box<Self>) -> Box<dyn Any>

Returns the value as a Box<dyn Any>. Read more
Source§

fn as_any(&self) -> &dyn Any

Returns the value as a &dyn Any. Read more
Source§

fn as_any_mut(&mut self) -> &mut dyn Any

Returns the value as a &mut dyn Any. Read more
Source§

fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>

Casts this type to a boxed, fully-reflected value.
Source§

fn as_reflect(&self) -> &dyn Reflect

Casts this type to a fully-reflected value.
Source§

fn as_reflect_mut(&mut self) -> &mut dyn Reflect

Casts this type to a mutable, fully-reflected value.
Source§

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§

impl Serialize for dyn CustomDualAxisProcessor + '_

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TypePath for Box<dyn CustomDualAxisProcessor>

Source§

fn type_path() -> &'static str

Returns the fully qualified path of the underlying type. Read more
Source§

fn short_type_path() -> &'static str

Returns a short, pretty-print enabled path to the type. Read more
Source§

fn type_ident() -> Option<&'static str>

Returns the name of the type, or None if it is anonymous. Read more
Source§

fn crate_name() -> Option<&'static str>

Returns the name of the crate the type is in, or None if it is anonymous. Read more
Source§

fn module_path() -> Option<&'static str>

Returns the path to the module the type is in, or None if it is anonymous. Read more
Source§

impl Typed for Box<dyn CustomDualAxisProcessor>

Source§

fn type_info() -> &'static TypeInfo

Returns the compile-time info for the underlying type.
Source§

impl<'eq> Eq for dyn CustomDualAxisProcessor + 'eq

Source§

impl<'eq> Eq for dyn CustomDualAxisProcessor + Send + 'eq

Source§

impl<'eq> Eq for dyn CustomDualAxisProcessor + Send + Sync + 'eq

Source§

impl<'eq> Eq for dyn CustomDualAxisProcessor + Sync + 'eq

Implementors§