Skip to content

Commit

Permalink
Add support for custom deadzones.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgao committed Aug 7, 2019
1 parent 83e0f0f commit 32a5308
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
14 changes: 13 additions & 1 deletion dhc/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};

use crate::logger;

#[derive(Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Config {
#[serde(default = "default_console")]
pub console: bool,
Expand All @@ -20,6 +20,14 @@ pub struct Config {

#[serde(default = "default_xinput_enabled")]
pub xinput_enabled: bool,

pub deadzone: Option<DeadzoneConfig>,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct DeadzoneConfig {
pub enabled: bool,
pub threshold: f32,
}

fn default_console() -> bool {
Expand All @@ -45,6 +53,10 @@ impl Default for Config {
log_level: default_log_level(),
device_count: default_device_count(),
xinput_enabled: default_xinput_enabled(),
deadzone: Some(DeadzoneConfig {
enabled: false,
threshold: 0.5,
}),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions dhc/src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ impl RawInputDeviceState {
}
}

crate::mangle_inputs(&mut inputs);
self.buffer.write(inputs);
}
}
Expand Down
25 changes: 25 additions & 0 deletions dhc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,28 @@ impl Context {
state.update();
}
}

pub(crate) fn mangle_inputs(inputs: &mut DeviceInputs) {
if let Some(deadzone_config) = &CONFIG.deadzone {
if deadzone_config.enabled {
for ref mut axis in &mut [&mut inputs.axis_left_stick_x, &mut inputs.axis_left_stick_y] {
let value = axis.get();
// For convenience, interpolate from [0, 1] to [-1, 1], perform the
// deadzone operations, and then interpolate back.
let (sign, magnitude) = if value > 0.5 {
(1.0, (value - 0.5) * 2.0)
} else {
(-1.0, (0.5 - value) * 2.0)
};

let magnitude = if magnitude > deadzone_config.threshold {
1.0
} else {
0.0
};

axis.set_value(0.5 + 0.5 * sign * magnitude);
}
}
}
}

0 comments on commit 32a5308

Please sign in to comment.