Struct winit_input_map::Input
source · pub struct Input<const BINDS: usize> {
pub binds: [Vec<InputCode>; BINDS],
pub pressing: [bool; BINDS],
pub pressed: [bool; BINDS],
pub released: [bool; BINDS],
pub mouse_move: (f32, f32),
pub mouse_pos: (f32, f32),
}
Expand description
input system. define actions and their key binds and then see if their pressing, pressed or released. get mouse position and how much its moved. you can use anythin that implements the Into<usize>
trait as an action, but it’s recommended to use an action enum.
enum Actions{
Debug,
Left,
Right,
Click
}
impl Into<usize> for Actions{
fn into(self) -> usize {
self as usize
}
}
use input::{Input, InputCode};
use Actions::*;
let mut input = Input::new([
(vec![InputCode::keycode(KeyCode::Space)], Debug),
(vec![InputCode::keycode(KeyCode::ArrowLeft), InputCode::keycode(KeyCode::KeyA)], Left),
(vec![InputCode::keycode(KeyCode::ArrowRight), InputCode::keycode(KeyCode::KeyD)], Right),
(vec![InputCode::Mouse(MouseButton::Left)], Click)
]);
use winit::{event::*, keyboard::KeyCode};
let event_loop = winit::event_loop::EventLoop::new().unwrap();
event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);
let _window = winit::window::Window::new(&event_loop).unwrap();
event_loop.run(|event, target|{
input.update(&event);
match &event {
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { target.exit() },
Event::AboutToWait => {
if input.pressed(Debug) { println!("pressed {:?}", input.binds(Debug)) }
if input.pressing(Right) || input.pressing(Left) { println!("axis: {}", input.axis(Right, Left)) }
if input.mouse_move != (0.0, 0.0) { println!("mouse moved: {:?} and is now at {:?}", input.mouse_move, input.mouse_pos) }
if input.released(Click) { println!("released {:?}", input.binds(Click)) }
std::thread::sleep(std::time::Duration::from_millis(100));
//since init is required to be called once before update, we put it at the end before it loops.
input.init();
}
_ => ()
}
}).unwrap();
Fields§
§binds: [Vec<InputCode>; BINDS]
§pressing: [bool; BINDS]
§pressed: [bool; BINDS]
§released: [bool; BINDS]
§mouse_move: (f32, f32)
§mouse_pos: (f32, f32)
Implementations§
source§impl<const BINDS: usize> Input<BINDS>
impl<const BINDS: usize> Input<BINDS>
sourcepub fn new(binds: [(Vec<InputCode>, impl Into<usize>); BINDS]) -> Self
pub fn new(binds: [(Vec<InputCode>, impl Into<usize>); BINDS]) -> Self
create new input system. recommended to use an action enum which implements the Into<usize>
trait for the second value.
enum Action{
Forward,
Back,
Left,
Right
}
impl Into<usize> for Action{
fn into(self) -> usize{
self as usize
}
}
use Action::*;
use input::{Input, InputCode};
use winit::keyboard::KeyCode;
//doesnt have to be the same ordered as the enum.
let mut input = Input::new([
(vec![InputCode::keycode(KeyCode::KeyW)], Forward),
(vec![InputCode::keycode(KeyCode::KeyA)], Left),
(vec![InputCode::keycode(KeyCode::KeyS)], Back),
(vec![InputCode::keycode(KeyCode::KeyD)], Right)
]);
sourcepub fn update(&mut self, event: &Event<()>)
pub fn update(&mut self, event: &Event<()>)
updates the input using a winit event. requires input.init()
to be used before being updated.
use winit::event::*;
use input::Input;
let mut event_loop = winit::event_loop::EventLoopBuilder::new().build().unwrap();
event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);
let mut input = Input::new([]);
event_loop.run(|event, target|{
input.update(&event);
match &event{
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => target.exit(),
Event::AboutToWait => input.init(),
_ => ()
}
});
sourcepub fn init(&mut self)
pub fn init(&mut self)
initialise input. required to be called for mouse_move
, pressed()
and released()
to work.
required to put input.init()
before input.update()
use winit::event::*;
use input::Input;
let mut event_loop = winit::event_loop::EventLoopBuilder::new().build().unwrap();
event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);
let mut input = Input::new([]);
event_loop.run(|event, target|{
input.update(&event);
match &event{
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => target.exit(),
Event::AboutToWait => input.init(),
_ => ()
}
});
sourcepub fn binds(&mut self, action: impl Into<usize>) -> &mut Vec<InputCode>
pub fn binds(&mut self, action: impl Into<usize>) -> &mut Vec<InputCode>
get binds of action. same as self.binds[action.into()]
sourcepub fn pressing(&self, action: impl Into<usize>) -> bool
pub fn pressing(&self, action: impl Into<usize>) -> bool
checks if action is being pressed currently. same as self.pressing[action.into()]
sourcepub fn pressed(&self, action: impl Into<usize>) -> bool
pub fn pressed(&self, action: impl Into<usize>) -> bool
checks if action was just pressed. same as self.pressed[action.into()]
sourcepub fn released(&self, action: impl Into<usize>) -> bool
pub fn released(&self, action: impl Into<usize>) -> bool
checks if action was just released. same as self.released[action.into()]
sourcepub fn axis(&self, pos: impl Into<usize>, neg: impl Into<usize>) -> f32
pub fn axis(&self, pos: impl Into<usize>, neg: impl Into<usize>) -> f32
returns 1.0 if pos is pressed, -1.0 if neg is pressed or 0.0 if either pos and neg or nothing is pressed. usefull for movement controls. same as input::axis(input.pressing(pos), input.pressing(neg))
let move_dir = (input.axis(Right, Left), input.axis(Up, Down));
Auto Trait Implementations§
impl<const BINDS: usize> Freeze for Input<BINDS>
impl<const BINDS: usize> RefUnwindSafe for Input<BINDS>
impl<const BINDS: usize> Send for Input<BINDS>
impl<const BINDS: usize> Sync for Input<BINDS>
impl<const BINDS: usize> Unpin for Input<BINDS>
impl<const BINDS: usize> UnwindSafe for Input<BINDS>
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> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.