Skip to main content

InputManager

Struct InputManager 

Source
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

Source

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 binding
Source

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.

Source

pub fn clear_action(&mut self, action: &str) -> bool

Removes all bindings for an action.

Returns true if the action existed and was removed.

Source

pub fn clear_all_actions(&mut self)

Removes all action mappings.

Source

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.

Source

pub fn has_action(&self, action: &str) -> bool

Returns true if the action has any bindings.

Source

pub fn action_names(&self) -> impl Iterator<Item = &str>

Returns an iterator over all action names.

Source

pub fn action_count(&self) -> usize

Returns the number of registered actions.

Source

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.

Source

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.

Source

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.

Source

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

Source

pub fn buffer_duration(&self) -> Duration

Returns the current buffer duration.

Source

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.

Source

pub fn buffer_size(&self) -> usize

Returns the number of inputs currently in the buffer.

Source

pub fn clear_buffer(&mut self)

Clears the input buffer.

Useful when resetting combos or canceling sequences.

Source

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();
}
Source

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.

Source

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.

Source

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

Source

pub fn press_gamepad_button(&mut self, gamepad_id: usize, button: u32)

Sets a gamepad button as pressed.

§Panics

Panics if gamepad_id >= 4.

Source

pub fn release_gamepad_button(&mut self, gamepad_id: usize, button: u32)

Sets a gamepad button as released.

§Panics

Panics if gamepad_id >= 4.

Source

pub fn gamepad_button_pressed(&self, gamepad_id: usize, button: u32) -> bool

Returns true if the gamepad button is currently pressed.

Returns false if gamepad_id is invalid.

Source

pub fn gamepad_button_just_pressed( &self, gamepad_id: usize, button: u32, ) -> bool

Returns true if the gamepad button was just pressed this frame.

Source

pub fn gamepad_button_just_released( &self, gamepad_id: usize, button: u32, ) -> bool

Returns true if the gamepad button was just released this frame.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn set_gamepad_connected(&mut self, gamepad_id: usize, connected: bool)

Sets the connection status of a gamepad.

Source

pub fn is_gamepad_connected(&self, gamepad_id: usize) -> bool

Returns true if the gamepad is currently connected.

Source

pub fn connected_gamepad_count(&self) -> usize

Returns the number of connected gamepads.

Source

pub fn connected_gamepads(&self) -> impl Iterator<Item = usize> + '_

Returns an iterator over all connected gamepad IDs.

Source

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.

Source

pub fn gamepad_vibration(&self, gamepad_id: usize) -> f32

Returns the current vibration intensity for a gamepad (0.0-1.0).

Source

pub fn stop_gamepad_vibration(&mut self, gamepad_id: usize)

Stops vibration for a gamepad (sets intensity to 0.0).

Source

pub fn stop_all_vibration(&mut self)

Stops vibration for all gamepads.

Source

pub fn analog_deadzone(&self) -> f32

Returns the current analog deadzone threshold (0.0-1.0).

Default is 0.1 (10%).

Source

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

Source

pub fn new() -> Self

Creates a new InputManager with no inputs pressed.

Default buffer duration is 200ms, suitable for most combo detection.

Source

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
Source

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.

Source

pub fn press_key(&mut self, key: Key)

Sets a key as pressed.

Source

pub fn release_key(&mut self, key: Key)

Sets a key as released.

Source

pub fn key_pressed(&self, key: Key) -> bool

Returns true if the key is currently pressed.

Source

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.

Source

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.

Source

pub fn keys_pressed(&self) -> impl Iterator<Item = &Key>

Returns an iterator over all currently pressed keys.

Source

pub fn press_mouse_button(&mut self, button: MouseButton)

Sets a mouse button as pressed.

Source

pub fn release_mouse_button(&mut self, button: MouseButton)

Sets a mouse button as released.

Source

pub fn mouse_button_pressed(&self, button: MouseButton) -> bool

Returns true if the mouse button is currently pressed.

Source

pub fn mouse_button_just_pressed(&self, button: MouseButton) -> bool

Returns true if the mouse button was just pressed this frame.

Source

pub fn mouse_button_just_released(&self, button: MouseButton) -> bool

Returns true if the mouse button was just released this frame.

Source

pub fn mouse_buttons_pressed(&self) -> impl Iterator<Item = &MouseButton>

Returns an iterator over all currently pressed mouse buttons.

Source

pub fn set_mouse_position(&mut self, position: Vec2)

Updates the mouse position.

Source

pub fn mouse_position(&self) -> Vec2

Returns the current mouse position in screen coordinates.

Source

pub fn mouse_delta(&self) -> Vec2

Returns the mouse movement delta since last frame.

Source

pub fn add_scroll_delta(&mut self, delta: Vec2)

Updates the mouse scroll delta.

Source

pub fn scroll_delta(&self) -> Vec2

Returns the mouse scroll delta for this frame.

Source

pub fn clear(&mut self)

Clears all input state (useful for focus loss or pausing).

Trait Implementations§

Source§

impl Clone for InputManager

Source§

fn clone(&self) -> InputManager

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for InputManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for InputManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

impl<T> QueryState for T
where T: Send + Sync + Clone + 'static,

Source§

impl<T> Resource for T
where T: Send + Sync + 'static,