Struct QueryInputHandler

Source
pub struct QueryInputHandler<I, C> { /* private fields */ }
Expand description

Input handler for an query-based game engine.

Use this when your game engine provides inputs via a function you can call to query the state of a given key.

At the top of your game loop, you MUST call QueryInputHandler::update. The general logic should look like this:

// This is predefined by your game engine
#[derive(Clone, Copy, Hash, Eq, PartialEq)]
enum Key {
    Up, Down, Left, Right, Escape,
    // etc ...
}

// You define this!
#[derive(Clone, Copy, Hash, Eq, PartialEq)]
enum Control {
    Up,
    Down,
    Left,
    Right,
    Pause,
}

let mut input_handler = QueryInputHandler::new_with_controls(vec![
    (Key::Up, Control::Up),
    (Key::Down, Control::Down),
    (Key::Left, Control::Left),
    (Key::Right, Control::Right),
    (Key::Escape, Control::Pause),
]);

// Even if your game engine requires the use of a `Context` to query keys
// you can still use `QueryInputHandler`.
let ctx = get_context();

loop {     
    input_handler.update(|input| ctx.is_key_pressed(input));

    // Now do game logic ...
    if input_handler.down(Control::Left) {
        player.x += 1.0;
    } else if input_handler.down(Control::Right) {
        player.x -= 1.0;
    } else if input_handler.clicked(Control::Up) {
        player.jump();
    }

}

I is the type of your inputs, and C is the type of your controls.

Implementations§

Source§

impl<I: Hash + Eq + Clone, C: Hash + Eq + Clone> QueryInputHandler<I, C>

Source

pub fn new() -> Self

Create a new PollingInputHandler with no control mapping.

Source

pub fn new_with_controls(map: impl IntoIterator<Item = (I, C)>) -> Self

Create a new PollingInputHandler with the given mapping of inputs to controls.

If two entries in the iterator have the same input, the first one will be clobbered and the second one will remain.

Source

pub fn clear_inputs(&mut self)

Manually unpress all inputs.

Note you should not have to call this at the beginning of your loop. (In fact, if you do, your inputs will never be pressed.)

Source

pub fn update(&mut self, is_pressed: impl FnMut(I) -> bool)

Update the input handler. Give it a function that returns true if the given input is pressed this frame, and false if the given input is not pressed.

Depending on your grame engine’s architecture, you might need some kind of Context struct to query the state from. In this case, pass it a closure that borrows the Context.

You MUST CALL THIS FIRST THING in your game loop. Otherwise things won’t get updated correctly.

Source

pub fn press_time(&self, ctrl: C) -> u32

Return the number of frames the given control has been pressed for.

Source

pub fn down(&self, ctrl: C) -> bool

Return if this control is held down (ie, the corresponding input has been pressed for 1 or more frames).

Source

pub fn up(&self, ctrl: C) -> bool

Return if this control is up.

Source

pub fn clicked(&self, ctrl: C) -> bool

Return if this control was clicked down this frame (ie, the corresponding input was just pressed this frame).

Trait Implementations§

Source§

impl<I: Clone, C: Clone> Clone for QueryInputHandler<I, C>

Source§

fn clone(&self) -> QueryInputHandler<I, C>

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<I: Debug, C: Debug> Debug for QueryInputHandler<I, C>

Source§

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

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

impl<I, C> Default for QueryInputHandler<I, C>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<I, C> Freeze for QueryInputHandler<I, C>

§

impl<I, C> RefUnwindSafe for QueryInputHandler<I, C>

§

impl<I, C> Send for QueryInputHandler<I, C>
where I: Send, C: Send,

§

impl<I, C> Sync for QueryInputHandler<I, C>
where I: Sync, C: Sync,

§

impl<I, C> Unpin for QueryInputHandler<I, C>
where I: Unpin, C: Unpin,

§

impl<I, C> UnwindSafe for QueryInputHandler<I, C>
where I: UnwindSafe, C: UnwindSafe,

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<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<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> 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.