Skip to main content

BindingTypes

Trait BindingTypes 

Source
pub trait BindingTypes:
    Debug
    + Send
    + Sync
    + 'static {
    type Axis: Clone + Debug + Hash + Eq + Send + Sync + 'static;
    type Action: Clone + Debug + Hash + Eq + Send + Sync + 'static;
}
Expand description

Define a set of types used for bindings configuration. Usually defaulted to StringBindings, which uses Strings.

By defining your own set of types (usually enums), you will be able to have compile-time guarantees while handling events, and you can also add additional context, for example player index in local multiplayer game.

Example configuration for local multiplayer driving game might look like this:

type PlayerId = u8;

#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
enum AxisBinding {
    Throttle(PlayerId),
    Steering(PlayerId),
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
enum ActionBinding {
    UsePowerup(PlayerId),
}

#[derive(Debug)]
struct DriverBindingTypes;
impl BindingTypes for DriverBindingTypes {
    type Axis = AxisBinding;
    type Action = ActionBinding;
}

type GameBindings = Bindings<DriverBindingTypes>;

And the bindings.ron:

(
  axes: {
    Throttle(0): Emulated(pos: Key(W), neg: Key(S)),
    Steering(0): Emulated(pos: Key(D), neg: Key(A)),
    Throttle(1): Emulated(pos: Key(Up), neg: Key(Down)),
    Steering(1): Emulated(pos: Key(Right), neg: Key(Left)),
  },
  actions: {
    UsePowerup(0): [[Key(E)]],
    UsePowerup(1): [[Key(P)]],
  },
)

Required Associated Types§

Source

type Axis: Clone + Debug + Hash + Eq + Send + Sync + 'static

Type used for defining axis keys. Usually an enum or string.

Source

type Action: Clone + Debug + Hash + Eq + Send + Sync + 'static

Type used for defining action keys. Usually an enum or string.

Implementors§