1#[derive(PartialEq, Eq, Clone, Copy, Hash)]
2enum Actions {
3 Debug,
4 Left,
5 Right,
6 Click,
7 MouseR, MouseL, MouseU, MouseD,
8 ScrollU, ScrollD
9}
10use winit_input_map::*;
11use Actions::*;
12use gilrs::Gilrs;
13use winit::{event::*, application::*, window::*, event_loop::*};
14fn main() {
15 let mut input = { use base_input_codes::*; input_map!(
16 (Debug, Space, South),
17 (Left, ArrowLeft, KeyA, LeftStickLeft ),
18 (Right, ArrowRight, KeyD, LeftStickRight),
19 (Click, MouseButton::Left),
20 (MouseR, MouseMoveRight, RightStickRight),
21 (MouseL, MouseMoveLeft, RightStickLeft ),
22 (MouseU, MouseMoveUp, RightStickUp ),
23 (MouseD, MouseMoveDown, RightStickDown ),
24 (ScrollU, Equal, MouseScrollUp ),
25 (ScrollD, Minus, MouseScrollDown)
26 ) };
27 input.mouse_scale = 1.0;
28 input.scroll_scale = 1.0;
29
30 let gilrs = Gilrs::new().unwrap();
31 let event_loop = EventLoop::new().unwrap();
32 event_loop.run_app(&mut App { window: None, input, gilrs }).unwrap();
33}
34struct App { window: Option<Window>, input: InputMap<Actions>, gilrs: Gilrs }
35impl 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,
43 _: WindowId, event: WindowEvent
44 ) {
45 self.input.update_with_window_event(&event);
46 if let WindowEvent::CloseRequested = &event { event_loop.exit() }
47 }
48 fn device_event(
49 &mut self, _: &ActiveEventLoop, id: DeviceId, event: DeviceEvent
50 ) {
51 self.input.update_with_device_event(id, &event);
52 }
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 input.init();
87 }
88}
89