summaryrefslogtreecommitdiff
path: root/src/input.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/input.rs')
-rw-r--r--src/input.rs36
1 files changed, 33 insertions, 3 deletions
diff --git a/src/input.rs b/src/input.rs
index 3eea33c..c12d622 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -1,11 +1,16 @@
-use std::{collections::VecDeque, fmt::Debug, time::Duration};
+#[cfg(feature = "gilrs")]
+use std::time::Duration;
+use std::{collections::VecDeque, fmt::Debug};
use crossterm::event::KeyCode;
+#[cfg(feature = "gilrs")]
use gilrs::{
Axis, Button, GamepadId, Gilrs, PowerInfo,
ff::{BaseEffect, Replay, Ticks},
};
-use rustc_hash::{FxHashMap, FxHashSet};
+#[cfg(feature = "gilrs")]
+use rustc_hash::FxHashMap;
+use rustc_hash::FxHashSet;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Direction {
@@ -22,6 +27,7 @@ pub struct Position {
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
+#[cfg(feature = "gilrs")]
struct ControllerButton {
gamepad_id: usize,
button: Button,
@@ -36,11 +42,16 @@ pub struct InputManager {
is_focused: bool,
is_focused_this_frame: bool,
is_unfocused_this_frame: bool,
+ #[cfg(feature = "gilrs")]
gil_cx: Option<Gilrs>,
+ #[cfg(feature = "gilrs")]
gamepad_map: FxHashMap<usize, GamepadId>,
+ #[cfg(feature = "gilrs")]
gamepad_map_rev: FxHashMap<GamepadId, usize>,
connected_gamepads: VecDeque<usize>,
+ #[cfg(feature = "gilrs")]
pressed_buttons: FxHashSet<ControllerButton>,
+ #[cfg(feature = "gilrs")]
released_buttons: FxHashSet<ControllerButton>,
}
@@ -54,11 +65,16 @@ impl InputManager {
is_focused: true,
is_focused_this_frame: false,
is_unfocused_this_frame: false,
+ #[cfg(feature = "gilrs")]
gil_cx: Gilrs::new().ok(),
+ #[cfg(feature = "gilrs")]
gamepad_map: FxHashMap::default(),
+ #[cfg(feature = "gilrs")]
gamepad_map_rev: FxHashMap::default(),
connected_gamepads: VecDeque::new(),
+ #[cfg(feature = "gilrs")]
pressed_buttons: FxHashSet::default(),
+ #[cfg(feature = "gilrs")]
released_buttons: FxHashSet::default(),
}
}
@@ -80,13 +96,16 @@ impl InputManager {
}
pub fn reset_next_frame(&mut self) {
+ #[cfg(feature = "puffin")]
puffin::profile_function!();
self.pressed_keys.clear();
self.mouse_clicked = false;
self.scroll_direction = None;
self.is_focused_this_frame = false;
self.is_unfocused_this_frame = false;
+ #[cfg(feature = "gilrs")]
self.pressed_buttons.clear();
+ #[cfg(feature = "gilrs")]
self.released_buttons.clear();
self.connected_gamepads.pop_front();
}
@@ -107,6 +126,10 @@ impl InputManager {
self.scroll_direction
}
+ #[cfg(not(feature = "gilrs"))]
+ pub fn read_gil_events(&mut self) {}
+
+ #[cfg(feature = "gilrs")]
pub fn read_gil_events(&mut self) {
let Some(gil) = self.gil_cx.as_mut() else {
return;
@@ -143,16 +166,19 @@ impl InputManager {
self.connected_gamepads.front().copied()
}
+ #[cfg(feature = "gilrs")]
pub fn is_button_pressed_this_frame(&self, gamepad_id: usize, button: Button) -> bool {
self.pressed_buttons
.contains(&ControllerButton { gamepad_id, button })
}
+ #[cfg(feature = "gilrs")]
pub fn is_button_released_this_frame(&self, gamepad_id: usize, button: Button) -> bool {
self.released_buttons
.contains(&ControllerButton { gamepad_id, button })
}
+ #[cfg(feature = "gilrs")]
pub fn is_button_down(&self, gamepad_id: usize, button: Button) -> bool {
self.gamepad_map.get(&gamepad_id).is_some_and(|id| {
self.gil_cx
@@ -161,12 +187,14 @@ impl InputManager {
})
}
+ #[cfg(feature = "gilrs")]
pub fn axis_value(&self, gamepad_id: usize, axis: Axis) -> Option<f32> {
self.gamepad_map
.get(&gamepad_id)
.and_then(|id| self.gil_cx.as_ref().map(|gil| gil.gamepad(*id).value(axis)))
}
+ #[cfg(feature = "gilrs")]
pub fn is_gamepad_connected(&self, gamepad_id: usize) -> bool {
self.gamepad_map.get(&gamepad_id).is_some_and(|id| {
self.gil_cx
@@ -175,6 +203,7 @@ impl InputManager {
})
}
+ #[cfg(feature = "gilrs")]
pub fn gamepad_power_info(&self, gamepad_id: usize) -> Option<PowerInfo> {
self.gamepad_map.get(&gamepad_id).and_then(|id| {
self.gil_cx
@@ -183,6 +212,7 @@ impl InputManager {
})
}
+ #[cfg(feature = "gilrs")]
pub fn vibrate(&mut self, gamepad_id: usize, intensity: f32, duration: Duration) {
let Some(gamepad_id) = self.gamepad_map.get(&gamepad_id) else {
return;
@@ -202,7 +232,7 @@ impl InputManager {
play_for: Ticks::from_ms(duration.as_millis() as u32),
with_delay: Ticks::from_ms(0),
},
- envelope: todo!(),
+ envelope: gilrs::ff::Envelope::default(),
})
.repeat(gilrs::ff::Repeat::For(Ticks::from_ms(
duration.as_millis() as u32