mod config;
mod serde_enums;
#[cfg(test)]
mod tests;
use crate::Error;
pub use config::{ActionBinding, AxisConfig, BindingConfig};
use rayengine_core::input::{Action, Axis, Input};
use raylib::prelude::{GamepadAxis, GamepadButton, KeyboardKey, MouseButton, RaylibHandle};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(from = "serde_enums::ButtonData", into = "serde_enums::ButtonData")]
pub enum Button {
Key(KeyboardKey),
Mouse(MouseButton),
Gamepad {
device: i32,
button: GamepadButton,
},
}
impl From<KeyboardKey> for Button {
fn from(key: KeyboardKey) -> Self {
Self::Key(key)
}
}
impl Button {
fn validate(self) -> Result<(), Error> {
match self {
Self::Key(KeyboardKey::KEY_NULL) => Err(invalid("KEY_NULL is not a binding")),
Self::Gamepad { device, button } => {
validate_device(device)?;
if button == GamepadButton::GAMEPAD_BUTTON_UNKNOWN {
return Err(invalid("GAMEPAD_BUTTON_UNKNOWN is not a binding"));
}
Ok(())
}
_ => Ok(()),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "value", deny_unknown_fields)]
pub enum AxisSource {
Buttons {
negative: Button,
positive: Button,
},
Gamepad {
device: i32,
#[serde(with = "serde_enums::axis")]
axis: GamepadAxis,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AxisBinding {
pub source: AxisSource,
pub dead_zone: f32,
pub inverted: bool,
pub sensitivity: f32,
}
impl AxisBinding {
pub fn new(source: AxisSource) -> Self {
Self {
source,
dead_zone: 0.15,
inverted: false,
sensitivity: 1.0,
}
}
pub fn validate(&self) -> Result<(), Error> {
match self.source {
AxisSource::Buttons { negative, positive } => {
negative.validate()?;
positive.validate()?;
}
AxisSource::Gamepad { device, .. } => validate_device(device)?,
}
if !self.dead_zone.is_finite() || !(0.0..1.0).contains(&self.dead_zone) {
return Err(invalid("dead_zone must be finite and in [0, 1)"));
}
if !self.sensitivity.is_finite() || !(0.0..=100.0).contains(&self.sensitivity) {
return Err(invalid("sensitivity must be finite and in [0, 100]"));
}
Ok(())
}
pub fn process(&self, raw: Option<f32>) -> f32 {
let Some(mut value) = raw.filter(|v| v.is_finite()) else {
return 0.0;
};
value = value.clamp(-1.0, 1.0);
if matches!(
self.source,
AxisSource::Gamepad {
axis: GamepadAxis::GAMEPAD_AXIS_LEFT_TRIGGER
| GamepadAxis::GAMEPAD_AXIS_RIGHT_TRIGGER,
..
}
) {
value = (value + 1.0) * 0.5;
}
let magnitude = ((value.abs() - self.dead_zone) / (1.0 - self.dead_zone)).max(0.0);
let value = (value.signum() * magnitude * self.sensitivity).clamp(-1.0, 1.0);
if self.inverted { -value } else { value }
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Bindings {
actions: Vec<(Action, Vec<Button>)>,
axes: Vec<(Axis, Vec<AxisBinding>)>,
}
impl Bindings {
pub fn new() -> Self {
Self::default()
}
pub fn bind(mut self, action: Action, button: impl Into<Button>) -> Self {
self.push_button(action, button.into());
self
}
pub fn bind_axis(mut self, axis: Axis, binding: AxisBinding) -> Result<Self, Error> {
self.add_axis(axis, binding)?;
Ok(self)
}
fn push_button(&mut self, action: Action, button: Button) {
if let Some((_, buttons)) = self.actions.iter_mut().find(|(a, _)| *a == action) {
buttons.push(button);
} else {
self.actions.push((action, vec![button]));
}
}
pub fn add(&mut self, action: Action, button: impl Into<Button>) -> Result<(), Error> {
let button = button.into();
button.validate()?;
self.push_button(action, button);
Ok(())
}
pub fn rebind(&mut self, action: Action, buttons: Vec<Button>) -> Result<(), Error> {
for button in &buttons {
button.validate()?;
}
self.remove(action);
if !buttons.is_empty() {
self.actions.push((action, buttons));
}
Ok(())
}
pub fn remove_button(&mut self, action: Action, button: Button) -> bool {
let Some((_, buttons)) = self.actions.iter_mut().find(|(a, _)| *a == action) else {
return false;
};
let before = buttons.len();
buttons.retain(|b| *b != button);
let removed = before != buttons.len();
if buttons.is_empty() {
self.remove(action);
}
removed
}
pub fn remove(&mut self, action: Action) -> bool {
let before = self.actions.len();
self.actions.retain(|(a, _)| *a != action);
before != self.actions.len()
}
pub fn add_axis(&mut self, axis: Axis, binding: AxisBinding) -> Result<(), Error> {
binding.validate()?;
if let Some((_, sources)) = self.axes.iter_mut().find(|(a, _)| *a == axis) {
sources.push(binding);
} else {
self.axes.push((axis, vec![binding]));
}
Ok(())
}
pub fn rebind_axis(&mut self, axis: Axis, sources: Vec<AxisBinding>) -> Result<(), Error> {
for source in &sources {
source.validate()?;
}
self.remove_axis(axis);
if !sources.is_empty() {
self.axes.push((axis, sources));
}
Ok(())
}
pub fn remove_axis(&mut self, axis: Axis) -> bool {
let before = self.axes.len();
self.axes.retain(|(a, _)| *a != axis);
before != self.axes.len()
}
pub fn buttons(&self, action: Action) -> &[Button] {
self.actions
.iter()
.find(|(a, _)| *a == action)
.map_or(&[], |(_, b)| b)
}
pub fn axis_bindings(&self, axis: Axis) -> &[AxisBinding] {
self.axes
.iter()
.find(|(a, _)| *a == axis)
.map_or(&[], |(_, b)| b)
}
pub fn validate(&self) -> Result<(), Error> {
for (_, buttons) in &self.actions {
for button in buttons {
button.validate()?;
}
}
for (_, bindings) in &self.axes {
for binding in bindings {
binding.validate()?;
}
}
Ok(())
}
pub(crate) fn capacities(&self) -> (usize, usize) {
(
self.actions
.iter()
.map(|(a, _)| usize::from(a.0) + 1)
.max()
.unwrap_or(0),
self.axes
.iter()
.map(|(a, _)| usize::from(a.0) + 1)
.max()
.unwrap_or(0),
)
}
pub(crate) fn reconcile(&self, previous: &mut Self, input: &mut Input) -> Result<(), Error> {
if self == previous {
return Ok(());
}
self.validate()?;
for (action, buttons) in &previous.actions {
if self.buttons(*action) != buttons {
input.set(*action, false);
}
}
for (axis, sources) in &previous.axes {
if self.axis_bindings(*axis) != sources {
input.set_axis(*axis, 0.0);
}
}
previous.clone_from(self);
Ok(())
}
pub(crate) fn sample(
&self,
raylib: &RaylibHandle,
input: &mut Input,
sampling: &mut SamplingState,
) {
self.sample_from(&NativeInput(raylib), input, sampling);
}
fn sample_from(
&self,
backend: &impl PhysicalInput,
input: &mut Input,
sampling: &mut SamplingState,
) {
if !backend.focused() {
sampling.trigger_armed.fill(false);
input.release_all();
return;
}
for device in 0..4 {
if !backend.available(device) {
sampling.trigger_armed[device as usize * 2..device as usize * 2 + 2].fill(false);
}
}
for (action, buttons) in &self.actions {
let mut down = false;
let mut pressed = false;
for &button in buttons {
let state = backend.button(button);
down |= state.0;
pressed |= state.1;
}
if pressed && !input.down(*action) {
input.set(*action, true);
}
input.set(*action, down);
}
for (axis, sources) in &self.axes {
let mut value: f32 = 0.0;
for source in sources {
let raw = match source.source {
AxisSource::Buttons { negative, positive } => Some(
f32::from(backend.button(positive).0)
- f32::from(backend.button(negative).0),
),
AxisSource::Gamepad { device, axis } => {
sampling.filter(device, axis, backend.axis(device, axis))
}
};
let candidate = source.process(raw);
if candidate.abs() > value.abs() {
value = candidate;
}
}
input.set_axis(*axis, value);
}
}
}
#[derive(Debug, Default)]
pub(crate) struct SamplingState {
trigger_armed: [bool; 8],
}
impl SamplingState {
fn filter(&mut self, device: i32, axis: GamepadAxis, raw: Option<f32>) -> Option<f32> {
let trigger = match axis {
GamepadAxis::GAMEPAD_AXIS_LEFT_TRIGGER => 0,
GamepadAxis::GAMEPAD_AXIS_RIGHT_TRIGGER => 1,
_ => return raw,
};
if !(0..4).contains(&device) {
return None;
}
let armed = &mut self.trigger_armed[device as usize * 2 + trigger];
let Some(raw) = raw.filter(|value| value.is_finite()) else {
*armed = false;
return None;
};
if raw <= -0.95 {
*armed = true;
}
armed.then_some(raw)
}
}
trait PhysicalInput {
fn focused(&self) -> bool;
fn available(&self, device: i32) -> bool;
fn button(&self, button: Button) -> (bool, bool);
fn axis(&self, device: i32, axis: GamepadAxis) -> Option<f32>;
}
struct NativeInput<'a>(&'a RaylibHandle);
impl PhysicalInput for NativeInput<'_> {
fn focused(&self) -> bool {
self.0.is_window_focused()
}
fn available(&self, device: i32) -> bool {
(0..4).contains(&device) && self.0.is_gamepad_available(device)
}
fn button(&self, button: Button) -> (bool, bool) {
match button {
Button::Key(key) => (self.0.is_key_down(key), self.0.is_key_pressed(key)),
Button::Mouse(button) => (
self.0.is_mouse_button_down(button),
self.0.is_mouse_button_pressed(button),
),
Button::Gamepad { device, button } if self.available(device) => (
self.0.is_gamepad_button_down(device, button),
self.0.is_gamepad_button_pressed(device, button),
),
Button::Gamepad { .. } => (false, false),
}
}
fn axis(&self, device: i32, axis: GamepadAxis) -> Option<f32> {
(self.available(device) && self.0.get_gamepad_axis_count(device) > axis as i32)
.then(|| self.0.get_gamepad_axis_movement(device, axis))
}
}
fn invalid(message: impl Into<String>) -> Error {
Error::Config(message.into())
}
fn validate_device(device: i32) -> Result<(), Error> {
if !(0..4).contains(&device) {
return Err(invalid("gamepad device must be a slot in 0..=3"));
}
Ok(())
}