pub struct InputMap<F: Hash + Eq + Clone + Copy> {
pub binds: HashMap<InputCode, (f32, Vec<F>)>,
pub mouse_pos: (f32, f32),
pub recently_pressed: Option<InputCode>,
pub text_typed: Option<String>,
pub mouse_scale: f32,
pub scroll_scale: f32,
pub press_sensitivity: f32,
/* private fields */
}
Expand description
A struct that handles all your input needs once you’ve hooked it up to winit and gilrs.
use gilrs::Gilrs;
use winit::{event::*, application::*, window::*, event_loop::*};
use winit_input_map::*;
struct App {
window: Option<Window>,
input: InputMap<()>,
gilrs: Gilrs
}
impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
self.window = Some(event_loop.create_window(Window::default_attributes()).unwrap());
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, _: WindowId, event: WindowEvent) {
self.input.update_with_window_event(&event);
if let WindowEvent::CloseRequested = &event { event_loop.exit() }
}
fn device_event(&mut self, _: &ActiveEventLoop, id: DeviceId, event: DeviceEvent) {
self.input.update_with_device_event(id, &event);
}
fn about_to_wait(&mut self, _: &ActiveEventLoop) {
self.input.update_with_gilrs(&mut self.gilrs);
// put your code here
self.input.init();
}
}
Fields§
§binds: HashMap<InputCode, (f32, Vec<F>)>
Stores what each input code is bound to and its previous press value
mouse_pos: (f32, f32)
The mouse position
recently_pressed: Option<InputCode>
The last input event, even if it isn’t in the binds. Useful for handling rebinding
text_typed: Option<String>
The text typed this loop
mouse_scale: f32
Since most values are from 0-1 reducing the mouse sensitivity will result in better consistancy
scroll_scale: f32
Since most values are from 0-1 reducing the scroll sensitivity will result in better consistancy
press_sensitivity: f32
The minimum value something has to be at to count as being pressed. Values over 1 will result in regular buttons being unusable
Implementations§
Source§impl<F: Hash + Eq + Copy> InputMap<F>
impl<F: Hash + Eq + Copy> InputMap<F>
Sourcepub fn new(binds: &[(F, Vec<InputCode>)]) -> Self
pub fn new(binds: &[(F, Vec<InputCode>)]) -> Self
Create new input system. It’s recommended to use the input_map!
macro to reduce boilerplate
and increase readability.
use Action::*;
use winit_input_map::*;
use winit::keyboard::KeyCode;
#[derive(Hash, PartialEq, Eq, Clone, Copy)]
enum Action {
Forward,
Back,
Pos,
Neg
}
//doesnt have to be the same ordered as the enum.
let input = InputMap::new(&[
(Forward, vec![KeyCode::KeyW.into()]),
(Pos, vec![KeyCode::KeyA.into()]),
(Back, vec![KeyCode::KeyS.into()]),
(Neg, vec![KeyCode::KeyD.into()])
]);
Sourcepub fn empty() -> InputMap<()>
pub fn empty() -> InputMap<()>
Use if you dont want to have any actions and binds. Will still have access to everything else.
Sourcepub fn mut_bind(&mut self, input_code: InputCode) -> &mut Vec<F>
pub fn mut_bind(&mut self, input_code: InputCode) -> &mut Vec<F>
Gets a mutable vector of what actions input_code is bound to
Sourcepub fn update_with_winit(&mut self, event: &Event<()>)
👎Deprecated: use update_with_window_event
and update_with_device_event
pub fn update_with_winit(&mut self, event: &Event<()>)
update_with_window_event
and update_with_device_event
Updates the input map using a winit event. Make sure to call input.init()
when your done with
the input this loop.
use winit::{event::*, window::Window, event_loop::EventLoop};
use winit_input_map::InputMap;
let mut event_loop = EventLoop::new().unwrap();
event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);
let _window = Window::new(&event_loop).unwrap();
let mut input = input_map!();
event_loop.run(|event, target|{
input.update_with_winit(&event);
match &event{
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => target.exit(),
Event::AboutToWait => input.init(),
_ => ()
}
});
Sourcepub fn update_with_device_event(&mut self, id: DeviceId, event: &DeviceEvent)
pub fn update_with_device_event(&mut self, id: DeviceId, event: &DeviceEvent)
Examples found in repository?
More examples
Sourcepub fn update_with_window_event(&mut self, event: &WindowEvent)
pub fn update_with_window_event(&mut self, event: &WindowEvent)
Examples found in repository?
More examples
Sourcepub fn update_with_gilrs(&mut self, gilrs: &mut Gilrs)
pub fn update_with_gilrs(&mut self, gilrs: &mut Gilrs)
Examples found in repository?
More examples
28 fn about_to_wait(&mut self, _: &ActiveEventLoop) {
29 self.input.update_with_gilrs(&mut self.gilrs);
30
31 if self.input.pressed(Action::Return) {
32 println!("{}", self.text);
33 self.text = String::new();
34 } else if let Some(new) = &self.input.text_typed {
35 self.text.push_str(new);
36 }
37
38 self.input.init();
39 }
53 fn about_to_wait(&mut self, _: &ActiveEventLoop) {
54 self.input.update_with_gilrs(&mut self.gilrs);
55
56 let input = &mut self.input;
57 let scroll = input.axis(ScrollU, ScrollD);
58
59 if input.pressed(Debug) {
60 println!("pressed {:?}", input.binds.iter().filter_map(|(a, (_, s))| {
61 if s.contains(&Debug) { Some(*a) } else { None }
62 }).collect::<Vec<InputCode>>())
63 }
64 if input.pressing(Right) || input.pressing(Left) {
65 println!("axis: {}", input.axis(Right, Left))
66 }
67
68 let mouse_move = input.dir(MouseL, MouseR, MouseU, MouseD);
69 if mouse_move != (0.0, 0.0) {
70 println!(
71 "mouse moved: {:?} and is now at {:?}",
72 mouse_move, input.mouse_pos
73 )
74 }
75 if input.released(Click) {
76 println!("released")
77 }
78 if scroll != 0.0 {
79 println!("scrolling {}", scroll);
80 }
81 if let Some(other) = input.recently_pressed {
82 println!("{other:?}");
83 }
84 std::thread::sleep(std::time::Duration::from_millis(100));
85 //reset input. use after your done with the input
86 input.init();
87 }
Sourcepub fn init(&mut self)
pub fn init(&mut self)
Makes the input map ready to recieve new events.
Examples found in repository?
More examples
28 fn about_to_wait(&mut self, _: &ActiveEventLoop) {
29 self.input.update_with_gilrs(&mut self.gilrs);
30
31 if self.input.pressed(Action::Return) {
32 println!("{}", self.text);
33 self.text = String::new();
34 } else if let Some(new) = &self.input.text_typed {
35 self.text.push_str(new);
36 }
37
38 self.input.init();
39 }
53 fn about_to_wait(&mut self, _: &ActiveEventLoop) {
54 self.input.update_with_gilrs(&mut self.gilrs);
55
56 let input = &mut self.input;
57 let scroll = input.axis(ScrollU, ScrollD);
58
59 if input.pressed(Debug) {
60 println!("pressed {:?}", input.binds.iter().filter_map(|(a, (_, s))| {
61 if s.contains(&Debug) { Some(*a) } else { None }
62 }).collect::<Vec<InputCode>>())
63 }
64 if input.pressing(Right) || input.pressing(Left) {
65 println!("axis: {}", input.axis(Right, Left))
66 }
67
68 let mouse_move = input.dir(MouseL, MouseR, MouseU, MouseD);
69 if mouse_move != (0.0, 0.0) {
70 println!(
71 "mouse moved: {:?} and is now at {:?}",
72 mouse_move, input.mouse_pos
73 )
74 }
75 if input.released(Click) {
76 println!("released")
77 }
78 if scroll != 0.0 {
79 println!("scrolling {}", scroll);
80 }
81 if let Some(other) = input.recently_pressed {
82 println!("{other:?}");
83 }
84 std::thread::sleep(std::time::Duration::from_millis(100));
85 //reset input. use after your done with the input
86 input.init();
87 }
Sourcepub fn pressing(&self, action: F) -> bool
pub fn pressing(&self, action: F) -> bool
Checks if action is being pressed currently. same as input.action_val(action) >= input.press_sensitivity
Examples found in repository?
53 fn about_to_wait(&mut self, _: &ActiveEventLoop) {
54 self.input.update_with_gilrs(&mut self.gilrs);
55
56 let input = &mut self.input;
57 let scroll = input.axis(ScrollU, ScrollD);
58
59 if input.pressed(Debug) {
60 println!("pressed {:?}", input.binds.iter().filter_map(|(a, (_, s))| {
61 if s.contains(&Debug) { Some(*a) } else { None }
62 }).collect::<Vec<InputCode>>())
63 }
64 if input.pressing(Right) || input.pressing(Left) {
65 println!("axis: {}", input.axis(Right, Left))
66 }
67
68 let mouse_move = input.dir(MouseL, MouseR, MouseU, MouseD);
69 if mouse_move != (0.0, 0.0) {
70 println!(
71 "mouse moved: {:?} and is now at {:?}",
72 mouse_move, input.mouse_pos
73 )
74 }
75 if input.released(Click) {
76 println!("released")
77 }
78 if scroll != 0.0 {
79 println!("scrolling {}", scroll);
80 }
81 if let Some(other) = input.recently_pressed {
82 println!("{other:?}");
83 }
84 std::thread::sleep(std::time::Duration::from_millis(100));
85 //reset input. use after your done with the input
86 input.init();
87 }
Sourcepub fn action_val(&self, action: F) -> f32
pub fn action_val(&self, action: F) -> f32
Checks how wheremuch action is being pressed. May be higher than 1 in the case of scroll wheels and mouse movement.
Sourcepub fn pressed(&self, action: F) -> bool
pub fn pressed(&self, action: F) -> bool
checks if action was just pressed
Examples found in repository?
28 fn about_to_wait(&mut self, _: &ActiveEventLoop) {
29 self.input.update_with_gilrs(&mut self.gilrs);
30
31 if self.input.pressed(Action::Return) {
32 println!("{}", self.text);
33 self.text = String::new();
34 } else if let Some(new) = &self.input.text_typed {
35 self.text.push_str(new);
36 }
37
38 self.input.init();
39 }
More examples
53 fn about_to_wait(&mut self, _: &ActiveEventLoop) {
54 self.input.update_with_gilrs(&mut self.gilrs);
55
56 let input = &mut self.input;
57 let scroll = input.axis(ScrollU, ScrollD);
58
59 if input.pressed(Debug) {
60 println!("pressed {:?}", input.binds.iter().filter_map(|(a, (_, s))| {
61 if s.contains(&Debug) { Some(*a) } else { None }
62 }).collect::<Vec<InputCode>>())
63 }
64 if input.pressing(Right) || input.pressing(Left) {
65 println!("axis: {}", input.axis(Right, Left))
66 }
67
68 let mouse_move = input.dir(MouseL, MouseR, MouseU, MouseD);
69 if mouse_move != (0.0, 0.0) {
70 println!(
71 "mouse moved: {:?} and is now at {:?}",
72 mouse_move, input.mouse_pos
73 )
74 }
75 if input.released(Click) {
76 println!("released")
77 }
78 if scroll != 0.0 {
79 println!("scrolling {}", scroll);
80 }
81 if let Some(other) = input.recently_pressed {
82 println!("{other:?}");
83 }
84 std::thread::sleep(std::time::Duration::from_millis(100));
85 //reset input. use after your done with the input
86 input.init();
87 }
Sourcepub fn released(&self, action: F) -> bool
pub fn released(&self, action: F) -> bool
checks if action was just released
Examples found in repository?
53 fn about_to_wait(&mut self, _: &ActiveEventLoop) {
54 self.input.update_with_gilrs(&mut self.gilrs);
55
56 let input = &mut self.input;
57 let scroll = input.axis(ScrollU, ScrollD);
58
59 if input.pressed(Debug) {
60 println!("pressed {:?}", input.binds.iter().filter_map(|(a, (_, s))| {
61 if s.contains(&Debug) { Some(*a) } else { None }
62 }).collect::<Vec<InputCode>>())
63 }
64 if input.pressing(Right) || input.pressing(Left) {
65 println!("axis: {}", input.axis(Right, Left))
66 }
67
68 let mouse_move = input.dir(MouseL, MouseR, MouseU, MouseD);
69 if mouse_move != (0.0, 0.0) {
70 println!(
71 "mouse moved: {:?} and is now at {:?}",
72 mouse_move, input.mouse_pos
73 )
74 }
75 if input.released(Click) {
76 println!("released")
77 }
78 if scroll != 0.0 {
79 println!("scrolling {}", scroll);
80 }
81 if let Some(other) = input.recently_pressed {
82 println!("{other:?}");
83 }
84 std::thread::sleep(std::time::Duration::from_millis(100));
85 //reset input. use after your done with the input
86 input.init();
87 }
Sourcepub fn axis(&self, pos: F, neg: F) -> f32
pub fn axis(&self, pos: F, neg: F) -> f32
Returns f32 based on how much pos and neg are pressed. may return values higher than 1.0 in
the case of mouse movement and scrolling. usefull for movement controls. for 2d values see
[dir]
and [dir_max_len_1]
let move_dir = input.axis(Neg, Pos);
same as input.action_val(pos) - input.action_val(neg)
Examples found in repository?
53 fn about_to_wait(&mut self, _: &ActiveEventLoop) {
54 self.input.update_with_gilrs(&mut self.gilrs);
55
56 let input = &mut self.input;
57 let scroll = input.axis(ScrollU, ScrollD);
58
59 if input.pressed(Debug) {
60 println!("pressed {:?}", input.binds.iter().filter_map(|(a, (_, s))| {
61 if s.contains(&Debug) { Some(*a) } else { None }
62 }).collect::<Vec<InputCode>>())
63 }
64 if input.pressing(Right) || input.pressing(Left) {
65 println!("axis: {}", input.axis(Right, Left))
66 }
67
68 let mouse_move = input.dir(MouseL, MouseR, MouseU, MouseD);
69 if mouse_move != (0.0, 0.0) {
70 println!(
71 "mouse moved: {:?} and is now at {:?}",
72 mouse_move, input.mouse_pos
73 )
74 }
75 if input.released(Click) {
76 println!("released")
77 }
78 if scroll != 0.0 {
79 println!("scrolling {}", scroll);
80 }
81 if let Some(other) = input.recently_pressed {
82 println!("{other:?}");
83 }
84 std::thread::sleep(std::time::Duration::from_millis(100));
85 //reset input. use after your done with the input
86 input.init();
87 }
Sourcepub fn dir(&self, pos_x: F, neg_x: F, pos_y: F, neg_y: F) -> (f32, f32)
pub fn dir(&self, pos_x: F, neg_x: F, pos_y: F, neg_y: F) -> (f32, f32)
Returns a vector based off of x and y axis. For movement controls see dir_max_len_1
Examples found in repository?
53 fn about_to_wait(&mut self, _: &ActiveEventLoop) {
54 self.input.update_with_gilrs(&mut self.gilrs);
55
56 let input = &mut self.input;
57 let scroll = input.axis(ScrollU, ScrollD);
58
59 if input.pressed(Debug) {
60 println!("pressed {:?}", input.binds.iter().filter_map(|(a, (_, s))| {
61 if s.contains(&Debug) { Some(*a) } else { None }
62 }).collect::<Vec<InputCode>>())
63 }
64 if input.pressing(Right) || input.pressing(Left) {
65 println!("axis: {}", input.axis(Right, Left))
66 }
67
68 let mouse_move = input.dir(MouseL, MouseR, MouseU, MouseD);
69 if mouse_move != (0.0, 0.0) {
70 println!(
71 "mouse moved: {:?} and is now at {:?}",
72 mouse_move, input.mouse_pos
73 )
74 }
75 if input.released(Click) {
76 println!("released")
77 }
78 if scroll != 0.0 {
79 println!("scrolling {}", scroll);
80 }
81 if let Some(other) = input.recently_pressed {
82 println!("{other:?}");
83 }
84 std::thread::sleep(std::time::Duration::from_millis(100));
85 //reset input. use after your done with the input
86 input.init();
87 }
Sourcepub fn dir_max_len_1(
&self,
pos_x: F,
neg_x: F,
pos_y: F,
neg_y: F,
) -> (f32, f32)
pub fn dir_max_len_1( &self, pos_x: F, neg_x: F, pos_y: F, neg_y: F, ) -> (f32, f32)
Returns a vector based off of x and y axis with a maximum length of 1 (the same as a normalised
vector). If this undesirable see dir
Trait Implementations§
Auto Trait Implementations§
impl<F> Freeze for InputMap<F>
impl<F> RefUnwindSafe for InputMap<F>where
F: RefUnwindSafe,
impl<F> Send for InputMap<F>where
F: Send,
impl<F> Sync for InputMap<F>where
F: Sync,
impl<F> Unpin for InputMap<F>where
F: Unpin,
impl<F> UnwindSafe for InputMap<F>where
F: 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> 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.