2020-10-21 17:27:00 +00:00
|
|
|
use crate::{Axis, Input};
|
|
|
|
use bevy_app::{EventReader, Events};
|
|
|
|
use bevy_ecs::{Local, Res, ResMut};
|
|
|
|
use bevy_utils::HashMap;
|
|
|
|
|
2020-09-18 21:43:47 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2020-10-01 17:54:20 +00:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2020-09-18 21:43:47 +00:00
|
|
|
pub struct Gamepad(pub usize);
|
|
|
|
|
2020-10-21 17:27:00 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2020-10-01 17:54:20 +00:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2020-09-18 21:43:47 +00:00
|
|
|
pub enum GamepadEventType {
|
|
|
|
Connected,
|
|
|
|
Disconnected,
|
2020-10-21 17:27:00 +00:00
|
|
|
ButtonChanged(GamepadButtonType, f32),
|
|
|
|
AxisChanged(GamepadAxisType, f32),
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 17:27:00 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2020-10-01 17:54:20 +00:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2020-09-18 21:43:47 +00:00
|
|
|
pub struct GamepadEvent(pub Gamepad, pub GamepadEventType);
|
|
|
|
|
2020-10-21 22:56:07 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
|
|
|
pub struct GamepadEventRaw(pub Gamepad, pub GamepadEventType);
|
|
|
|
|
2020-09-18 21:43:47 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2020-10-01 17:54:20 +00:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2020-09-18 21:43:47 +00:00
|
|
|
pub enum GamepadButtonType {
|
|
|
|
South,
|
|
|
|
East,
|
|
|
|
North,
|
|
|
|
West,
|
|
|
|
C,
|
|
|
|
Z,
|
|
|
|
LeftTrigger,
|
|
|
|
LeftTrigger2,
|
|
|
|
RightTrigger,
|
|
|
|
RightTrigger2,
|
|
|
|
Select,
|
|
|
|
Start,
|
|
|
|
Mode,
|
|
|
|
LeftThumb,
|
|
|
|
RightThumb,
|
|
|
|
DPadUp,
|
|
|
|
DPadDown,
|
|
|
|
DPadLeft,
|
|
|
|
DPadRight,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2020-10-01 17:54:20 +00:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2020-09-18 21:43:47 +00:00
|
|
|
pub struct GamepadButton(pub Gamepad, pub GamepadButtonType);
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2020-10-01 17:54:20 +00:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2020-09-18 21:43:47 +00:00
|
|
|
pub enum GamepadAxisType {
|
|
|
|
LeftStickX,
|
|
|
|
LeftStickY,
|
|
|
|
LeftZ,
|
|
|
|
RightStickX,
|
|
|
|
RightStickY,
|
|
|
|
RightZ,
|
|
|
|
DPadX,
|
|
|
|
DPadY,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2020-10-01 17:54:20 +00:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2020-09-18 21:43:47 +00:00
|
|
|
pub struct GamepadAxis(pub Gamepad, pub GamepadAxisType);
|
2020-10-21 17:27:00 +00:00
|
|
|
|
|
|
|
#[derive(Default, Debug)]
|
2020-10-21 22:56:07 +00:00
|
|
|
pub struct GamepadSettings {
|
|
|
|
pub default_button_settings: ButtonSettings,
|
|
|
|
pub default_axis_settings: AxisSettings,
|
|
|
|
pub default_button_axis_settings: ButtonAxisSettings,
|
|
|
|
pub button_settings: HashMap<GamepadButton, ButtonSettings>,
|
|
|
|
pub axis_settings: HashMap<GamepadAxis, AxisSettings>,
|
|
|
|
pub button_axis_settings: HashMap<GamepadButton, ButtonAxisSettings>,
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 22:56:07 +00:00
|
|
|
impl GamepadSettings {
|
|
|
|
pub fn get_button_settings(&self, button: GamepadButton) -> &ButtonSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
self.button_settings
|
|
|
|
.get(&button)
|
2020-10-21 22:56:07 +00:00
|
|
|
.unwrap_or(&self.default_button_settings)
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 22:56:07 +00:00
|
|
|
pub fn get_axis_settings(&self, axis: GamepadAxis) -> &AxisSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
self.axis_settings
|
|
|
|
.get(&axis)
|
2020-10-21 22:56:07 +00:00
|
|
|
.unwrap_or(&self.default_axis_settings)
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 22:56:07 +00:00
|
|
|
pub fn get_button_axis_settings(&self, button: GamepadButton) -> &ButtonAxisSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
self.button_axis_settings
|
|
|
|
.get(&button)
|
2020-10-21 22:56:07 +00:00
|
|
|
.unwrap_or(&self.default_button_axis_settings)
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2020-10-21 22:56:07 +00:00
|
|
|
pub struct ButtonSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
pub press: f32,
|
|
|
|
pub release: f32,
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:56:07 +00:00
|
|
|
impl Default for ButtonSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
fn default() -> Self {
|
2020-10-21 22:56:07 +00:00
|
|
|
ButtonSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
press: 0.75,
|
|
|
|
release: 0.65,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:56:07 +00:00
|
|
|
impl ButtonSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
fn is_pressed(&self, value: f32) -> bool {
|
|
|
|
value >= self.press
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_released(&self, value: f32) -> bool {
|
|
|
|
value <= self.release
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2020-10-21 22:56:07 +00:00
|
|
|
pub struct AxisSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
pub positive_high: f32,
|
|
|
|
pub positive_low: f32,
|
|
|
|
pub negative_high: f32,
|
|
|
|
pub negative_low: f32,
|
|
|
|
pub threshold: f32,
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:56:07 +00:00
|
|
|
impl Default for AxisSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
fn default() -> Self {
|
2020-10-21 22:56:07 +00:00
|
|
|
AxisSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
positive_high: 0.95,
|
|
|
|
positive_low: 0.05,
|
|
|
|
negative_high: -0.95,
|
|
|
|
negative_low: -0.05,
|
|
|
|
threshold: 0.01,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:56:07 +00:00
|
|
|
impl AxisSettings {
|
2020-10-29 20:55:35 +00:00
|
|
|
fn filter(&self, new_value: f32, old_value: Option<f32>) -> Option<f32> {
|
2020-10-21 17:27:00 +00:00
|
|
|
if let Some(old_value) = old_value {
|
|
|
|
if (new_value - old_value).abs() <= self.threshold {
|
2020-10-29 20:55:35 +00:00
|
|
|
return None;
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if new_value <= self.positive_low && new_value >= self.negative_low {
|
2020-10-29 20:55:35 +00:00
|
|
|
return Some(0.0);
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
if new_value >= self.positive_high {
|
2020-10-29 20:55:35 +00:00
|
|
|
return Some(1.0);
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
if new_value <= self.negative_high {
|
2020-10-29 20:55:35 +00:00
|
|
|
return Some(-1.0);
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
2020-10-29 20:55:35 +00:00
|
|
|
Some(new_value)
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2020-10-21 22:56:07 +00:00
|
|
|
pub struct ButtonAxisSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
pub high: f32,
|
|
|
|
pub low: f32,
|
|
|
|
pub threshold: f32,
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:56:07 +00:00
|
|
|
impl Default for ButtonAxisSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
fn default() -> Self {
|
2020-10-21 22:56:07 +00:00
|
|
|
ButtonAxisSettings {
|
2020-10-21 17:27:00 +00:00
|
|
|
high: 0.95,
|
|
|
|
low: 0.05,
|
|
|
|
threshold: 0.01,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:56:07 +00:00
|
|
|
impl ButtonAxisSettings {
|
2020-10-29 20:55:35 +00:00
|
|
|
fn filter(&self, new_value: f32, old_value: Option<f32>) -> Option<f32> {
|
2020-10-21 17:27:00 +00:00
|
|
|
if let Some(old_value) = old_value {
|
|
|
|
if (new_value - old_value).abs() <= self.threshold {
|
2020-10-29 20:55:35 +00:00
|
|
|
return None;
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if new_value <= self.low {
|
2020-10-29 20:55:35 +00:00
|
|
|
return Some(0.0);
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
if new_value >= self.high {
|
2020-10-29 20:55:35 +00:00
|
|
|
return Some(1.0);
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
2020-10-29 20:55:35 +00:00
|
|
|
Some(new_value)
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gamepad_event_system(
|
2020-10-21 22:56:07 +00:00
|
|
|
mut event_reader: Local<EventReader<GamepadEventRaw>>,
|
2020-10-21 17:27:00 +00:00
|
|
|
mut button_input: ResMut<Input<GamepadButton>>,
|
|
|
|
mut axis: ResMut<Axis<GamepadAxis>>,
|
|
|
|
mut button_axis: ResMut<Axis<GamepadButton>>,
|
2020-10-21 22:56:07 +00:00
|
|
|
raw_events: Res<Events<GamepadEventRaw>>,
|
|
|
|
mut events: ResMut<Events<GamepadEvent>>,
|
|
|
|
settings: Res<GamepadSettings>,
|
2020-10-21 17:27:00 +00:00
|
|
|
) {
|
|
|
|
button_input.update();
|
2020-10-21 22:56:07 +00:00
|
|
|
for event in event_reader.iter(&raw_events) {
|
|
|
|
let (gamepad, event) = (event.0, &event.1);
|
2020-10-21 17:27:00 +00:00
|
|
|
match event {
|
|
|
|
GamepadEventType::Connected => {
|
2020-10-21 22:56:07 +00:00
|
|
|
events.send(GamepadEvent(gamepad, event.clone()));
|
2020-10-21 17:27:00 +00:00
|
|
|
for button_type in ALL_BUTTON_TYPES.iter() {
|
2020-10-21 22:56:07 +00:00
|
|
|
let gamepad_button = GamepadButton(gamepad, *button_type);
|
2020-10-21 17:27:00 +00:00
|
|
|
button_input.reset(gamepad_button);
|
|
|
|
button_axis.set(gamepad_button, 0.0);
|
|
|
|
}
|
|
|
|
for axis_type in ALL_AXIS_TYPES.iter() {
|
2020-10-21 22:56:07 +00:00
|
|
|
axis.set(GamepadAxis(gamepad, *axis_type), 0.0);
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
GamepadEventType::Disconnected => {
|
2020-10-21 22:56:07 +00:00
|
|
|
events.send(GamepadEvent(gamepad, event.clone()));
|
2020-10-21 17:27:00 +00:00
|
|
|
for button_type in ALL_BUTTON_TYPES.iter() {
|
2020-10-21 22:56:07 +00:00
|
|
|
let gamepad_button = GamepadButton(gamepad, *button_type);
|
2020-10-21 17:27:00 +00:00
|
|
|
button_input.reset(gamepad_button);
|
|
|
|
button_axis.remove(gamepad_button);
|
|
|
|
}
|
|
|
|
for axis_type in ALL_AXIS_TYPES.iter() {
|
2020-10-21 22:56:07 +00:00
|
|
|
axis.remove(GamepadAxis(gamepad, *axis_type));
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
GamepadEventType::AxisChanged(axis_type, value) => {
|
2020-10-21 22:56:07 +00:00
|
|
|
let gamepad_axis = GamepadAxis(gamepad, *axis_type);
|
2020-10-29 20:55:35 +00:00
|
|
|
if let Some(filtered_value) = settings
|
2020-10-21 22:56:07 +00:00
|
|
|
.get_axis_settings(gamepad_axis)
|
2020-10-29 20:55:35 +00:00
|
|
|
.filter(*value, axis.get(gamepad_axis))
|
|
|
|
{
|
|
|
|
axis.set(gamepad_axis, filtered_value);
|
|
|
|
events.send(GamepadEvent(
|
|
|
|
gamepad,
|
|
|
|
GamepadEventType::AxisChanged(*axis_type, filtered_value),
|
|
|
|
))
|
2020-10-21 22:56:07 +00:00
|
|
|
}
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
GamepadEventType::ButtonChanged(button_type, value) => {
|
2020-10-21 22:56:07 +00:00
|
|
|
let gamepad_button = GamepadButton(gamepad, *button_type);
|
2020-10-29 20:55:35 +00:00
|
|
|
if let Some(filtered_value) = settings
|
2020-10-21 22:56:07 +00:00
|
|
|
.get_button_axis_settings(gamepad_button)
|
2020-10-29 20:55:35 +00:00
|
|
|
.filter(*value, button_axis.get(gamepad_button))
|
|
|
|
{
|
|
|
|
button_axis.set(gamepad_button, filtered_value);
|
|
|
|
events.send(GamepadEvent(
|
|
|
|
gamepad,
|
|
|
|
GamepadEventType::ButtonChanged(*button_type, filtered_value),
|
|
|
|
))
|
|
|
|
}
|
2020-10-21 17:27:00 +00:00
|
|
|
|
2020-10-21 22:56:07 +00:00
|
|
|
let button_property = settings.get_button_settings(gamepad_button);
|
2020-10-21 17:27:00 +00:00
|
|
|
if button_input.pressed(gamepad_button) {
|
|
|
|
if button_property.is_released(*value) {
|
|
|
|
button_input.release(gamepad_button);
|
|
|
|
}
|
|
|
|
} else if button_property.is_pressed(*value) {
|
|
|
|
button_input.press(gamepad_button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ALL_BUTTON_TYPES: [GamepadButtonType; 19] = [
|
|
|
|
GamepadButtonType::South,
|
|
|
|
GamepadButtonType::East,
|
|
|
|
GamepadButtonType::North,
|
|
|
|
GamepadButtonType::West,
|
|
|
|
GamepadButtonType::C,
|
|
|
|
GamepadButtonType::Z,
|
|
|
|
GamepadButtonType::LeftTrigger,
|
|
|
|
GamepadButtonType::LeftTrigger2,
|
|
|
|
GamepadButtonType::RightTrigger,
|
|
|
|
GamepadButtonType::RightTrigger2,
|
|
|
|
GamepadButtonType::Select,
|
|
|
|
GamepadButtonType::Start,
|
|
|
|
GamepadButtonType::Mode,
|
|
|
|
GamepadButtonType::LeftThumb,
|
|
|
|
GamepadButtonType::RightThumb,
|
|
|
|
GamepadButtonType::DPadUp,
|
|
|
|
GamepadButtonType::DPadDown,
|
|
|
|
GamepadButtonType::DPadLeft,
|
|
|
|
GamepadButtonType::DPadRight,
|
|
|
|
];
|
|
|
|
|
|
|
|
const ALL_AXIS_TYPES: [GamepadAxisType; 8] = [
|
|
|
|
GamepadAxisType::LeftStickX,
|
|
|
|
GamepadAxisType::LeftStickY,
|
|
|
|
GamepadAxisType::LeftZ,
|
|
|
|
GamepadAxisType::RightStickX,
|
|
|
|
GamepadAxisType::RightStickY,
|
|
|
|
GamepadAxisType::RightZ,
|
|
|
|
GamepadAxisType::DPadX,
|
|
|
|
GamepadAxisType::DPadY,
|
|
|
|
];
|