input_actions/binding/
binding.rs

1use crate::{
2	binding, device, event, source,
3	source::{Axis, Button, Key, MouseButton},
4};
5
6/// Enumeration containing all the possible input sources across all kinds of devices.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum Source {
9	Mouse(binding::Mouse),
10	Keyboard(Key),
11	Gamepad(device::GamepadKind, binding::Gamepad),
12}
13
14/// All possible inputs from a mouse device.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub enum Mouse {
17	Button(MouseButton),
18	Move,
19	Scroll,
20}
21
22/// All possible inputs from a gamepad device.
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
24pub enum Gamepad {
25	Button(Button),
26	Axis(Axis),
27}
28
29impl Source {
30	/// Returns the kind of source this the source is (button or axis).
31	pub fn kind(&self) -> source::Kind {
32		match *self {
33			Self::Mouse(_) => source::Kind::Button,
34			Self::Keyboard(_) => source::Kind::Button,
35			Self::Gamepad(_, binding::Gamepad::Button(_)) => source::Kind::Button,
36			Self::Gamepad(_, binding::Gamepad::Axis(_)) => source::Kind::Axis,
37		}
38	}
39
40	/// Returns the kind of device that the source is (mouse, keyboard, gamepad).
41	pub fn device_kind(&self) -> device::Kind {
42		match *self {
43			Self::Mouse(_) => device::Kind::Mouse,
44			Self::Keyboard(_) => device::Kind::Keyboard,
45			Self::Gamepad(gamepad, _) => device::Kind::Gamepad(gamepad),
46		}
47	}
48
49	/// Converts the Source into a Binding with default properties/modifiers.
50	pub fn bound(self) -> Binding {
51		self.with_modifier(1.0)
52	}
53
54	/// Converts the Source into a Binding with a provided value modifier.
55	pub fn with_modifier(self, modifier: f32) -> Binding {
56		Binding {
57			source: self,
58			modifier,
59		}
60	}
61}
62
63/// A binding to some source with additional properties like a value modifier.
64/// Useful for binding buttons to axis actions for example.
65#[derive(Debug, Clone, Copy)]
66pub struct Binding {
67	pub source: Source,
68	/// Multipier to the value of the source when its event is received.
69	/// Can be used to invert button values from (0..1) range to (0..-1)
70	/// before they are applied as the input to an axis action.
71	pub modifier: f32,
72}
73
74impl Binding {
75	pub(crate) fn apply(&self, event: event::Event) -> event::Event {
76		let mut event = event;
77		match &mut event.state {
78			event::State::ButtonState(_btn_state) => {}
79			event::State::MouseMove(_x, _y) => {}
80			event::State::MouseScroll(_x, _y) => {}
81			event::State::ValueChanged(value) => *value *= self.modifier,
82		}
83		event
84	}
85}