winit_input_map/
lib.rs

1//! Define actions and their input binds and then see if its `pressing`, `pressed` or
2//! `released`. This library handles variable pressure through the `action_val` function aswellas
3//! multiple variable pressure inputs for things like 1d axis (through `axis`) and 2d vectors 
4//! (through `dir` and `dir_max_len_1`). This makes it easy to have things like 3d camera controls
5//! applied for both mouse movement and the gamepads right stick. The input map also can get the
6//! `mouse_pos`, what was `recently_pressed` (used for rebinding things) and the `text_typed`
7//! (useful for typing) to make sure it is fully featured. For better user control there are the
8//! `mouse_scale` and `scroll_scale` variables to make sensitivity align with eveything else
9//! 0-1 range and a `press_sensitivity` to control when a action counts as being pressed. Finaly,
10//! theres an input_map! macro to reduce boilerplate and increase readability.
11//! ```
12//! use winit::{
13//!     window::*, application::*, keyboard::*,
14//!     event_loop::*, event::*
15//! };
16//! use gilrs::Gilrs;
17//! use winit_input_map::*;
18//!
19//! #[derive(Hash, PartialEq, Eq, Clone, Copy)]
20//! enum Actions{ Foo }
21//! use Actions::*;
22//!
23//! let input = input_map!(
24//!     (Foo, KeyCode::Space, GamepadInput::South)
25//! );
26//! let ev = EventLoop::new().unwrap();
27//! let gilrs = Gilrs::new().unwrap();
28//! ev.run_app(&mut App { window: None, input, gilrs }).unwrap();
29//!
30//! struct App {
31//!     window: Option<Window>,
32//!     input: InputMap<Actions>,
33//!     gilrs: Gilrs
34//! }
35//! impl ApplicationHandler for App {
36//!     fn resumed(&mut self, event_loop: &ActiveEventLoop) {
37//!         let window_settings = Window::default_attributes();
38//!         let window = event_loop.create_window(window_settings);
39//!         self.window = Some(window.unwrap());
40//!     }
41//!     fn window_event(
42//!         &mut self, event_loop: &ActiveEventLoop, _: WindowId,
43//!         event: WindowEvent
44//!     ) {
45//!         self.input.update_with_window_event(&event);
46//!         if let WindowEvent::CloseRequested = &event
47//!         { event_loop.exit() }
48//!     }
49//!     fn device_event(
50//!         &mut self, _: &ActiveEventLoop, id: DeviceId,
51//!         event: DeviceEvent
52//!     ) {
53//!         self.input.update_with_device_event(id, &event);
54//!     }
55//!     fn about_to_wait(&mut self, _: &ActiveEventLoop) {
56//!         self.input.update_with_gilrs(&mut self.gilrs);
57//!         
58//!         // your code here!
59//!         if self.input.pressed(Foo) { println!("bar") }
60//!
61//!         self.input.init();
62//!     }
63//! }
64//! ```
65mod input;
66mod input_code;
67pub use crate::input::*;
68pub use crate::input_code::*;
69/// Creates new input map with inputed input codes bound to the acompaning action.
70/// Anything that impliments `into<InputCode>` can be bound to an action
71/// ```
72/// use Action::*;
73/// use winit_input_map::*;
74/// use winit::{keyboard::KeyCode, event::MouseButton};
75/// #[derive(Hash, PartialEq, Eq, Clone, Copy)]
76/// enum Action {
77///     Jump,
78///     Left,
79///     Right,
80///     Interact
81/// }
82/// let mut input = input_map!(
83///     (Jump,     KeyCode::Space                    ),
84///     (Left,     KeyCode::KeyA, KeyCode::ArrowLeft ),
85///     (Right,    KeyCode::KeyD, KeyCode::ArrowRight),
86///     (Interact, MouseButton::Left                 )
87/// );
88/// ```
89#[macro_export]
90macro_rules! input_map {
91    () => { InputMap::<()>::empty() };
92    ( $( ( $x:expr, $( $k:expr ),* ) ),* ) => {
93        InputMap::new(&[ $(
94            ($x, vec![ $( $k.into(), )* ]),
95        )*])
96    };
97}