1#[derive(PartialEq, Eq, Clone, Copy, Hash)]
2enum Action {
3 Debug,
4 Left, Right,
5 Click,
6 MouseR, MouseL, MouseU, MouseD,
7 ScrollU, ScrollD,
8 Undo
9}
10use winit_input_map::*;
11use Action::*;
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 (Action::Undo, [ControlLeft, KeyZ], [ControlRight, KeyZ])
28 ) };
29 input.mouse_scale = 1.0;
30 input.scroll_scale = 1.0;
31
32 let gilrs = Gilrs::new().unwrap();
33 let event_loop = EventLoop::new().unwrap();
34 event_loop.run_app(&mut App { window: None, input, gilrs }).unwrap();
35}
36struct App { window: Option<Window>, input: InputMap<Action>, gilrs: Gilrs }
37impl ApplicationHandler for App {
38 fn resumed(&mut self, event_loop: &ActiveEventLoop) {
39 let window_settings = Window::default_attributes();
40 let window = event_loop.create_window(window_settings);
41 self.window = Some(window.unwrap());
42 }
43 fn window_event(
44 &mut self, event_loop: &ActiveEventLoop,
45 _: WindowId, event: WindowEvent
46 ) {
47 self.input.update_with_window_event(&event);
48 if let WindowEvent::CloseRequested = &event { event_loop.exit() }
49 }
50 fn device_event(
51 &mut self, _: &ActiveEventLoop, id: DeviceId, 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 let input = &mut self.input;
59 let scroll = input.axis(ScrollU, ScrollD);
60
61 if input.pressed(Debug) {
62 println!("pressed {:?}", input.get_binds().into_iter().find(|&(a, _)| a == Debug).map(|(_, v)| v))
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) { println!("released") }
76
77 if input.pressed(Undo) { println!("Undone") }
78
79 if scroll != 0.0 {
80 println!("scrolling {}", scroll);
81 }
82 if let Some(other) = input.recently_pressed {
83 println!("{other:?}");
84 }
85 std::thread::sleep(std::time::Duration::from_millis(100));
86 input.init();
88 }
89}
90