pub struct InputManager { /* private fields */ }Expand description
Input management resource for ECS integration.
Tracks keyboard, mouse, and gamepad input state across frames, enabling queries for current state, just pressed, and just released. Also supports action mapping for semantic input handling.
Implementations§
Source§impl InputManager
impl InputManager
Sourcepub fn map_action(&mut self, action: impl Into<String>, binding: InputBinding)
pub fn map_action(&mut self, action: impl Into<String>, binding: InputBinding)
Maps an input binding to an action.
An action can have multiple bindings. If the action already exists, the binding is added to its list. Duplicate bindings are allowed.
§Example
use goud_engine::ecs::{InputManager, InputBinding};
use glfw::Key;
let mut input = InputManager::new();
input.map_action("Jump", InputBinding::Key(Key::Space));
input.map_action("Jump", InputBinding::Key(Key::W)); // Alternative bindingSourcepub fn unmap_action(&mut self, action: &str, binding: InputBinding) -> bool
pub fn unmap_action(&mut self, action: &str, binding: InputBinding) -> bool
Unmaps a specific input binding from an action.
Returns true if the binding was removed, false if it wasn’t found.
Sourcepub fn clear_action(&mut self, action: &str) -> bool
pub fn clear_action(&mut self, action: &str) -> bool
Removes all bindings for an action.
Returns true if the action existed and was removed.
Sourcepub fn clear_all_actions(&mut self)
pub fn clear_all_actions(&mut self)
Removes all action mappings.
Sourcepub fn get_action_bindings(&self, action: &str) -> &[InputBinding]
pub fn get_action_bindings(&self, action: &str) -> &[InputBinding]
Returns all bindings for an action.
Returns an empty slice if the action doesn’t exist.
Sourcepub fn has_action(&self, action: &str) -> bool
pub fn has_action(&self, action: &str) -> bool
Returns true if the action has any bindings.
Sourcepub fn action_names(&self) -> impl Iterator<Item = &str>
pub fn action_names(&self) -> impl Iterator<Item = &str>
Returns an iterator over all action names.
Sourcepub fn action_count(&self) -> usize
pub fn action_count(&self) -> usize
Returns the number of registered actions.
Sourcepub fn action_pressed(&self, action: &str) -> bool
pub fn action_pressed(&self, action: &str) -> bool
Returns true if ANY binding for the action is currently pressed.
Returns false if the action doesn’t exist or has no bindings.
Sourcepub fn action_just_pressed(&self, action: &str) -> bool
pub fn action_just_pressed(&self, action: &str) -> bool
Returns true if ANY binding for the action was just pressed this frame.
Returns false if the action doesn’t exist or has no bindings.
Sourcepub fn action_just_released(&self, action: &str) -> bool
pub fn action_just_released(&self, action: &str) -> bool
Returns true if ANY binding for the action was just released this frame.
Returns false if the action doesn’t exist or has no bindings.
Sourcepub fn action_strength(&self, action: &str) -> f32
pub fn action_strength(&self, action: &str) -> f32
Returns the strength of the action (0.0-1.0).
For digital inputs (keys, buttons), this returns 1.0 if pressed, 0.0 otherwise. This method exists for future analog input support (triggers, analog sticks).
Source§impl InputManager
impl InputManager
Sourcepub fn buffer_duration(&self) -> Duration
pub fn buffer_duration(&self) -> Duration
Returns the current buffer duration.
Sourcepub fn set_buffer_duration(&mut self, duration: Duration)
pub fn set_buffer_duration(&mut self, duration: Duration)
Sets the buffer duration for input sequences.
This determines how long inputs are remembered for combo detection.
Sourcepub fn buffer_size(&self) -> usize
pub fn buffer_size(&self) -> usize
Returns the number of inputs currently in the buffer.
Sourcepub fn clear_buffer(&mut self)
pub fn clear_buffer(&mut self)
Clears the input buffer.
Useful when resetting combos or canceling sequences.
Sourcepub fn sequence_detected(&self, sequence: &[InputBinding]) -> bool
pub fn sequence_detected(&self, sequence: &[InputBinding]) -> bool
Checks if a sequence of inputs was pressed within the buffer duration.
Returns true if all bindings in the sequence were pressed in order, with each subsequent input occurring within the buffer window.
§Example
use goud_engine::ecs::{InputManager, InputBinding};
use glfw::Key;
let mut input = InputManager::new();
// Detect "Down, Down, Forward, Punch" combo (fighting game)
let combo = vec![
InputBinding::Key(Key::Down),
InputBinding::Key(Key::Down),
InputBinding::Key(Key::Right),
InputBinding::Key(Key::Space),
];
if input.sequence_detected(&combo) {
player.perform_special_move();
}Sourcepub fn consume_sequence(&mut self, sequence: &[InputBinding]) -> bool
pub fn consume_sequence(&mut self, sequence: &[InputBinding]) -> bool
Checks if a sequence was pressed and clears the buffer if detected.
This is useful for consuming combos so they don’t trigger multiple times.
Returns true if the sequence was detected and consumed.
Sourcepub fn time_since_last_input(&self) -> Option<f32>
pub fn time_since_last_input(&self) -> Option<f32>
Returns the time since the last buffered input in seconds.
Returns None if the buffer is empty.
Sourcepub fn buffered_inputs(&self) -> impl Iterator<Item = (InputBinding, f32)> + '_
pub fn buffered_inputs(&self) -> impl Iterator<Item = (InputBinding, f32)> + '_
Returns all inputs in the buffer (oldest to newest).
Useful for debugging or visualizing input history.
Source§impl InputManager
impl InputManager
Returns true if the gamepad button is currently pressed.
Returns false if gamepad_id is invalid.
Returns true if the gamepad button was just pressed this frame.
Returns true if the gamepad button was just released this frame.
Sourcepub fn set_gamepad_axis(
&mut self,
gamepad_id: usize,
axis: GamepadAxis,
value: f32,
)
pub fn set_gamepad_axis( &mut self, gamepad_id: usize, axis: GamepadAxis, value: f32, )
Sets a gamepad analog axis value (-1.0 to 1.0).
Values within the deadzone threshold are clamped to 0.0.
Sourcepub fn gamepad_axis(&self, gamepad_id: usize, axis: GamepadAxis) -> f32
pub fn gamepad_axis(&self, gamepad_id: usize, axis: GamepadAxis) -> f32
Returns the current value of a gamepad analog axis (-1.0 to 1.0).
Returns 0.0 if the gamepad or axis doesn’t exist.
Sourcepub fn gamepad_left_stick(&self, gamepad_id: usize) -> Vec2
pub fn gamepad_left_stick(&self, gamepad_id: usize) -> Vec2
Returns the left stick as a Vec2 (x, y) where -1.0 is left/down and 1.0 is right/up.
Returns Vec2::zero() if the gamepad doesn’t exist.
Sourcepub fn gamepad_right_stick(&self, gamepad_id: usize) -> Vec2
pub fn gamepad_right_stick(&self, gamepad_id: usize) -> Vec2
Returns the right stick as a Vec2 (x, y) where -1.0 is left/down and 1.0 is right/up.
Returns Vec2::zero() if the gamepad doesn’t exist.
Sourcepub fn gamepad_left_trigger(&self, gamepad_id: usize) -> f32
pub fn gamepad_left_trigger(&self, gamepad_id: usize) -> f32
Returns the left trigger value (0.0 to 1.0).
Returns 0.0 if the gamepad doesn’t exist.
Sourcepub fn gamepad_right_trigger(&self, gamepad_id: usize) -> f32
pub fn gamepad_right_trigger(&self, gamepad_id: usize) -> f32
Returns the right trigger value (0.0 to 1.0).
Returns 0.0 if the gamepad doesn’t exist.
Sourcepub fn set_gamepad_connected(&mut self, gamepad_id: usize, connected: bool)
pub fn set_gamepad_connected(&mut self, gamepad_id: usize, connected: bool)
Sets the connection status of a gamepad.
Sourcepub fn is_gamepad_connected(&self, gamepad_id: usize) -> bool
pub fn is_gamepad_connected(&self, gamepad_id: usize) -> bool
Returns true if the gamepad is currently connected.
Sourcepub fn connected_gamepad_count(&self) -> usize
pub fn connected_gamepad_count(&self) -> usize
Returns the number of connected gamepads.
Sourcepub fn connected_gamepads(&self) -> impl Iterator<Item = usize> + '_
pub fn connected_gamepads(&self) -> impl Iterator<Item = usize> + '_
Returns an iterator over all connected gamepad IDs.
Sourcepub fn set_gamepad_vibration(&mut self, gamepad_id: usize, intensity: f32)
pub fn set_gamepad_vibration(&mut self, gamepad_id: usize, intensity: f32)
Sets the vibration intensity for a gamepad (0.0-1.0).
Note: Actual vibration must be implemented by the platform layer. This only tracks the requested vibration intensity.
Sourcepub fn gamepad_vibration(&self, gamepad_id: usize) -> f32
pub fn gamepad_vibration(&self, gamepad_id: usize) -> f32
Returns the current vibration intensity for a gamepad (0.0-1.0).
Sourcepub fn stop_gamepad_vibration(&mut self, gamepad_id: usize)
pub fn stop_gamepad_vibration(&mut self, gamepad_id: usize)
Stops vibration for a gamepad (sets intensity to 0.0).
Sourcepub fn stop_all_vibration(&mut self)
pub fn stop_all_vibration(&mut self)
Stops vibration for all gamepads.
Sourcepub fn analog_deadzone(&self) -> f32
pub fn analog_deadzone(&self) -> f32
Returns the current analog deadzone threshold (0.0-1.0).
Default is 0.1 (10%).
Sourcepub fn set_analog_deadzone(&mut self, deadzone: f32)
pub fn set_analog_deadzone(&mut self, deadzone: f32)
Sets the analog deadzone threshold (0.0-1.0).
Analog axis values within this threshold are clamped to 0.0. This prevents stick drift and accidental input.
Source§impl InputManager
impl InputManager
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new InputManager with no inputs pressed.
Default buffer duration is 200ms, suitable for most combo detection.
Sourcepub fn with_buffer_duration(buffer_duration: Duration) -> Self
pub fn with_buffer_duration(buffer_duration: Duration) -> Self
Creates a new InputManager with custom buffer duration.
The buffer duration determines how long inputs are remembered for sequence detection.
- Short durations (50-100ms): Strict, requires fast input
- Medium durations (200-300ms): Balanced, works for most games
- Long durations (500ms+): Lenient, easier for casual players
Sourcepub fn update(&mut self)
pub fn update(&mut self)
Updates the input state for the next frame.
This should be called at the start of each frame, before any input queries. It copies current state to previous state and resets deltas.
Sourcepub fn release_key(&mut self, key: Key)
pub fn release_key(&mut self, key: Key)
Sets a key as released.
Sourcepub fn key_pressed(&self, key: Key) -> bool
pub fn key_pressed(&self, key: Key) -> bool
Returns true if the key is currently pressed.
Sourcepub fn key_just_pressed(&self, key: Key) -> bool
pub fn key_just_pressed(&self, key: Key) -> bool
Returns true if the key was just pressed this frame.
True only on the first frame the key is pressed.
Sourcepub fn key_just_released(&self, key: Key) -> bool
pub fn key_just_released(&self, key: Key) -> bool
Returns true if the key was just released this frame.
True only on the first frame the key is released.
Sourcepub fn keys_pressed(&self) -> impl Iterator<Item = &Key>
pub fn keys_pressed(&self) -> impl Iterator<Item = &Key>
Returns an iterator over all currently pressed keys.
Sets a mouse button as pressed.
Sets a mouse button as released.
Returns true if the mouse button is currently pressed.
Returns true if the mouse button was just pressed this frame.
Returns true if the mouse button was just released this frame.
Returns an iterator over all currently pressed mouse buttons.
Sourcepub fn set_mouse_position(&mut self, position: Vec2)
pub fn set_mouse_position(&mut self, position: Vec2)
Updates the mouse position.
Sourcepub fn mouse_position(&self) -> Vec2
pub fn mouse_position(&self) -> Vec2
Returns the current mouse position in screen coordinates.
Sourcepub fn mouse_delta(&self) -> Vec2
pub fn mouse_delta(&self) -> Vec2
Returns the mouse movement delta since last frame.
Sourcepub fn add_scroll_delta(&mut self, delta: Vec2)
pub fn add_scroll_delta(&mut self, delta: Vec2)
Updates the mouse scroll delta.
Sourcepub fn scroll_delta(&self) -> Vec2
pub fn scroll_delta(&self) -> Vec2
Returns the mouse scroll delta for this frame.
Trait Implementations§
Source§impl Clone for InputManager
impl Clone for InputManager
Source§fn clone(&self) -> InputManager
fn clone(&self) -> InputManager
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for InputManager
impl Debug for InputManager
Auto Trait Implementations§
impl Freeze for InputManager
impl RefUnwindSafe for InputManager
impl Send for InputManager
impl Sync for InputManager
impl Unpin for InputManager
impl UnsafeUnpin for InputManager
impl UnwindSafe for InputManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().