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>
impl<I: Hash + Eq + Clone, C: Hash + Eq + Clone> QueryInputHandler<I, C>
Sourcepub fn new_with_controls(map: impl IntoIterator<Item = (I, C)>) -> Self
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.
Sourcepub fn clear_inputs(&mut self)
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.)
Sourcepub fn update(&mut self, is_pressed: impl FnMut(I) -> bool)
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.
Sourcepub fn press_time(&self, ctrl: C) -> u32
pub fn press_time(&self, ctrl: C) -> u32
Return the number of frames the given control has been pressed for.
Trait Implementations§
Source§impl<I: Clone, C: Clone> Clone for QueryInputHandler<I, C>
impl<I: Clone, C: Clone> Clone for QueryInputHandler<I, C>
Source§fn clone(&self) -> QueryInputHandler<I, C>
fn clone(&self) -> QueryInputHandler<I, C>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl<I, C> Freeze for QueryInputHandler<I, C>
impl<I, C> RefUnwindSafe for QueryInputHandler<I, C>where
I: RefUnwindSafe,
C: RefUnwindSafe,
impl<I, C> Send for QueryInputHandler<I, C>
impl<I, C> Sync for QueryInputHandler<I, C>
impl<I, C> Unpin for QueryInputHandler<I, C>
impl<I, C> UnwindSafe for QueryInputHandler<I, C>where
I: UnwindSafe,
C: UnwindSafe,
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<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 more