1use crate::event::{Event, Key, Modifiers};
2
3use winit::event::{DeviceEvent, ElementState, KeyboardInput, MouseButton, MouseScrollDelta, WindowEvent};
4
5pub fn convert_event<T>(ev: winit::event::Event<T>) -> Option<Event> {
8 match ev {
9 winit::event::Event::WindowEvent { event, .. } => match event {
10 WindowEvent::Resized(size) => Some(Event::Resize(size.width as f32, size.height as f32)),
11 WindowEvent::CloseRequested => Some(Event::Exit),
12 WindowEvent::Focused(f) => Some(Event::Focus(f)),
13 WindowEvent::ReceivedCharacter(c) => Some(Event::Text(c)),
14 WindowEvent::KeyboardInput { input, .. } => match input {
15 KeyboardInput {
16 state: ElementState::Pressed,
17 virtual_keycode: Some(key),
18 ..
19 } => convert_key(key).map(Event::Press),
20 KeyboardInput {
21 state: ElementState::Released,
22 virtual_keycode: Some(key),
23 ..
24 } => convert_key(key).map(Event::Release),
25 _ => None,
26 },
27 WindowEvent::ModifiersChanged(modifiers) => Some(Event::Modifiers(convert_mods(modifiers))),
28 WindowEvent::MouseInput {
29 state: ElementState::Pressed,
30 button,
31 ..
32 } => match button {
33 MouseButton::Left => Some(Event::Press(Key::LeftMouseButton)),
34 MouseButton::Right => Some(Event::Press(Key::RightMouseButton)),
35 MouseButton::Middle => Some(Event::Press(Key::MiddleMouseButton)),
36 MouseButton::Other(_) => None,
37 },
38 WindowEvent::MouseInput {
39 state: ElementState::Released,
40 button,
41 ..
42 } => match button {
43 MouseButton::Left => Some(Event::Release(Key::LeftMouseButton)),
44 MouseButton::Right => Some(Event::Release(Key::RightMouseButton)),
45 MouseButton::Middle => Some(Event::Release(Key::MiddleMouseButton)),
46 MouseButton::Other(_) => None,
47 },
48 WindowEvent::CursorMoved { position, .. } => Some(Event::Cursor(position.x as f32, position.y as f32)),
49 WindowEvent::MouseWheel { delta, .. } => match delta {
50 MouseScrollDelta::LineDelta(dx, dy) => Some(Event::Scroll(dx * 20.0, dy * 20.0)),
51
52 MouseScrollDelta::PixelDelta(delta) => Some(Event::Scroll(delta.x as f32, delta.y as f32)),
53 },
54 _ => None,
55 },
56 winit::event::Event::DeviceEvent {
57 event: DeviceEvent::MouseMotion { delta: (x, y) },
58 ..
59 } => Some(Event::Motion(x as f32, y as f32)),
60 _ => None,
61 }
62}
63
64fn convert_mods(x: winit::event::ModifiersState) -> Modifiers {
65 Modifiers {
66 ctrl: x.ctrl(),
67 alt: x.alt(),
68 shift: x.shift(),
69 logo: x.logo(),
70 #[cfg(target_os = "macos")]
71 command: x.logo(),
72 #[cfg(not(target_os = "macos"))]
73 command: x.ctrl(),
74 }
75}
76
77fn convert_key(key: winit::event::VirtualKeyCode) -> Option<Key> {
78 use winit::event::VirtualKeyCode as Vk;
79
80 match key {
81 Vk::Key1 => Some(Key::Key1),
82 Vk::Key2 => Some(Key::Key2),
83 Vk::Key3 => Some(Key::Key3),
84 Vk::Key4 => Some(Key::Key4),
85 Vk::Key5 => Some(Key::Key5),
86 Vk::Key6 => Some(Key::Key6),
87 Vk::Key7 => Some(Key::Key7),
88 Vk::Key8 => Some(Key::Key8),
89 Vk::Key9 => Some(Key::Key9),
90 Vk::Key0 => Some(Key::Key0),
91 Vk::F1 => Some(Key::F1),
92 Vk::F2 => Some(Key::F2),
93 Vk::F3 => Some(Key::F3),
94 Vk::F4 => Some(Key::F4),
95 Vk::F5 => Some(Key::F5),
96 Vk::F6 => Some(Key::F6),
97 Vk::F7 => Some(Key::F7),
98 Vk::F8 => Some(Key::F8),
99 Vk::F9 => Some(Key::F9),
100 Vk::F10 => Some(Key::F10),
101 Vk::F11 => Some(Key::F11),
102 Vk::F12 => Some(Key::F12),
103 Vk::A => Some(Key::A),
104 Vk::B => Some(Key::B),
105 Vk::C => Some(Key::C),
106 Vk::D => Some(Key::D),
107 Vk::E => Some(Key::E),
108 Vk::F => Some(Key::F),
109 Vk::G => Some(Key::G),
110 Vk::H => Some(Key::H),
111 Vk::I => Some(Key::I),
112 Vk::J => Some(Key::J),
113 Vk::K => Some(Key::K),
114 Vk::L => Some(Key::L),
115 Vk::M => Some(Key::M),
116 Vk::N => Some(Key::N),
117 Vk::O => Some(Key::O),
118 Vk::P => Some(Key::P),
119 Vk::Q => Some(Key::Q),
120 Vk::R => Some(Key::R),
121 Vk::S => Some(Key::S),
122 Vk::T => Some(Key::T),
123 Vk::U => Some(Key::U),
124 Vk::V => Some(Key::V),
125 Vk::W => Some(Key::W),
126 Vk::X => Some(Key::X),
127 Vk::Y => Some(Key::Y),
128 Vk::Z => Some(Key::Z),
129 Vk::Tab => Some(Key::Tab),
130 Vk::LShift => Some(Key::Shift),
131 Vk::LControl => Some(Key::Ctrl),
132 Vk::LAlt => Some(Key::Alt),
133 Vk::Space => Some(Key::Space),
134 Vk::Return => Some(Key::Enter),
135 Vk::Back => Some(Key::Backspace),
136 Vk::Escape => Some(Key::Escape),
137 Vk::Left => Some(Key::Left),
138 Vk::Right => Some(Key::Right),
139 Vk::Up => Some(Key::Up),
140 Vk::Down => Some(Key::Down),
141 Vk::Home => Some(Key::Home),
142 Vk::End => Some(Key::End),
143 Vk::Minus => Some(Key::Minus),
144 Vk::Plus => Some(Key::Plus),
145 Vk::LBracket => Some(Key::BracketOpen),
146 Vk::RBracket => Some(Key::BracketClose),
147 Vk::Comma => Some(Key::Comma),
148 Vk::Period => Some(Key::Period),
149 Vk::Semicolon => Some(Key::Semicolon),
150 Vk::Apostrophe => Some(Key::Quote),
151 Vk::Grave => Some(Key::Tilde),
152 Vk::Backslash => Some(Key::Backslash),
153 Vk::Slash => Some(Key::Slash),
154 _ => None,
155 }
156}