euv_core/game/input/
impl.rs1use super::*;
2
3impl InputState {
5 pub fn begin_frame(&mut self) {
11 self.get_mut_keys_just_pressed().clear();
12 self.get_mut_keys_just_released().clear();
13 self.set_pointer_delta(Point::new(0.0, 0.0));
14 self.set_scroll_delta(Point::new(0.0, 0.0));
15 }
16
17 pub fn ingest(&mut self, event: &WindowEvent) {
24 match event {
25 WindowEvent::KeyDown(_, key) => {
26 let code: KeyCode = key.get_code();
27 if self.get_mut_keys_down().insert(code) {
28 let _: bool = self.get_mut_keys_just_pressed().insert(code);
29 }
30 }
31 WindowEvent::KeyUp(_, key) => {
32 let code: KeyCode = key.get_code();
33 if self.get_mut_keys_down().remove(&code) {
34 let _: bool = self.get_mut_keys_just_released().insert(code);
35 }
36 }
37 WindowEvent::PointerMove(_, ptr) => {
38 let prev: Point = self.get_pointer_position();
39 let next: Point = ptr.get_position();
40 let delta: Point = self.get_pointer_delta();
41 self.set_pointer_delta(Point::new(
42 delta.get_x() + (next.get_x() - prev.get_x()),
43 delta.get_y() + (next.get_y() - prev.get_y()),
44 ));
45 self.set_pointer_position(next);
46 }
47 WindowEvent::PointerDown(_, ptr) => {
48 self.set_pointer_position(ptr.get_position());
49 let _: bool = self.get_mut_buttons_down().insert(ptr.get_button());
50 }
51 WindowEvent::PointerUp(_, ptr) => {
52 self.set_pointer_position(ptr.get_position());
53 let _: bool = self.get_mut_buttons_down().remove(&ptr.get_button());
54 }
55 WindowEvent::Scroll(_, delta) => {
56 let cur: Point = self.get_scroll_delta();
57 self.set_scroll_delta(Point::new(
58 cur.get_x() + delta.get_x(),
59 cur.get_y() + delta.get_y(),
60 ));
61 }
62 WindowEvent::Focused(_, false) => {
63 self.get_mut_keys_down().clear();
64 self.get_mut_buttons_down().clear();
65 }
66 _ => {}
67 }
68 }
69
70 #[must_use]
81 pub fn is_key_down(&self, key: KeyCode) -> bool {
82 self.get_keys_down().contains(&key)
83 }
84
85 #[must_use]
96 pub fn is_key_just_pressed(&self, key: KeyCode) -> bool {
97 self.get_keys_just_pressed().contains(&key)
98 }
99
100 #[must_use]
111 pub fn is_button_down(&self, button: PointerButton) -> bool {
112 self.get_buttons_down().contains(&button)
113 }
114
115 #[must_use]
127 pub fn axis(&self, negative: KeyCode, positive: KeyCode) -> f32 {
128 let mut value: f32 = 0.0;
129 if self.is_key_down(negative) {
130 value -= 1.0;
131 }
132 if self.is_key_down(positive) {
133 value += 1.0;
134 }
135 value
136 }
137
138 pub fn set_virtual_stick(&mut self, dx: f32, dy: f32) {
146 self.set_virtual_axis(Point::new(dx.clamp(-1.0, 1.0), dy.clamp(-1.0, 1.0)));
147 }
148
149 pub fn clear_virtual_stick(&mut self) {
155 self.set_virtual_axis(Point::new(0.0, 0.0));
156 }
157}
158
159impl Default for ActionMap {
161 fn default() -> Self {
167 let mut map: Self = Self {
168 key_bindings: HashMap::new(),
169 button_bindings: HashMap::new(),
170 };
171 map.bind_key(GameAction::MoveLeft, KeyCode::ArrowLeft);
172 map.bind_key(GameAction::MoveLeft, KeyCode::Character('a'));
173 map.bind_key(GameAction::MoveLeft, KeyCode::Character('A'));
174 map.bind_key(GameAction::MoveRight, KeyCode::ArrowRight);
175 map.bind_key(GameAction::MoveRight, KeyCode::Character('d'));
176 map.bind_key(GameAction::MoveRight, KeyCode::Character('D'));
177 map.bind_key(GameAction::MoveUp, KeyCode::ArrowUp);
178 map.bind_key(GameAction::MoveUp, KeyCode::Character('w'));
179 map.bind_key(GameAction::MoveUp, KeyCode::Character('W'));
180 map.bind_key(GameAction::MoveDown, KeyCode::ArrowDown);
181 map.bind_key(GameAction::MoveDown, KeyCode::Character('s'));
182 map.bind_key(GameAction::MoveDown, KeyCode::Character('S'));
183 map.bind_key(GameAction::Fire, KeyCode::Space);
184 map.bind_key(GameAction::Fire, KeyCode::Character('j'));
185 map.bind_button(GameAction::Fire, PointerButton::Primary);
186 map.bind_key(GameAction::Dash, KeyCode::Character('e'));
187 map.bind_key(GameAction::Dash, KeyCode::Character('E'));
188 map.bind_key(GameAction::Jump, KeyCode::Space);
189 map.bind_key(GameAction::Pause, KeyCode::Escape);
190 map.bind_key(GameAction::Pause, KeyCode::Character('p'));
191 map.bind_key(GameAction::ActionAlt, KeyCode::Character('r'));
192 map.bind_key(GameAction::ActionAlt, KeyCode::Character('R'));
193 map
194 }
195}
196
197impl ActionMap {
199 pub fn bind_key(&mut self, action: GameAction, key: KeyCode) {
207 self.get_mut_key_bindings()
208 .entry(action)
209 .or_default()
210 .push(key);
211 }
212
213 pub fn bind_button(&mut self, action: GameAction, button: PointerButton) {
221 self.get_mut_button_bindings()
222 .entry(action)
223 .or_default()
224 .push(button);
225 }
226
227 #[must_use]
239 pub fn is_active(&self, action: GameAction, input: &InputState) -> bool {
240 if action == GameAction::Fire && input.get_virtual_fire() {
241 return true;
242 }
243 if action == GameAction::Dash && input.get_virtual_dash() {
244 return true;
245 }
246 let key_hit: bool = self
247 .get_key_bindings()
248 .get(&action)
249 .map(|keys: &Vec<KeyCode>| keys.iter().copied().any(|k: KeyCode| input.is_key_down(k)))
250 .unwrap_or(false);
251 if key_hit {
252 return true;
253 }
254 self.get_button_bindings()
255 .get(&action)
256 .map(|btns: &Vec<PointerButton>| {
257 btns.iter()
258 .copied()
259 .any(|b: PointerButton| input.is_button_down(b))
260 })
261 .unwrap_or(false)
262 }
263
264 #[must_use]
276 pub fn is_just_pressed(&self, action: GameAction, input: &InputState) -> bool {
277 self.get_key_bindings()
278 .get(&action)
279 .map(|keys: &Vec<KeyCode>| {
280 keys.iter()
281 .copied()
282 .any(|k: KeyCode| input.is_key_just_pressed(k))
283 })
284 .unwrap_or(false)
285 }
286
287 #[must_use]
298 pub fn movement_vector(&self, input: &InputState) -> Point {
299 let mut dx: f32 = input.get_virtual_axis().get_x();
300 let mut dy: f32 = input.get_virtual_axis().get_y();
301 if self.is_active(GameAction::MoveLeft, input) {
302 dx -= 1.0;
303 }
304 if self.is_active(GameAction::MoveRight, input) {
305 dx += 1.0;
306 }
307 if self.is_active(GameAction::MoveUp, input) {
308 dy -= 1.0;
309 }
310 if self.is_active(GameAction::MoveDown, input) {
311 dy += 1.0;
312 }
313 let len: f32 = (dx * dx + dy * dy).sqrt();
314 if len > 1.0 {
315 Point::new(dx / len, dy / len)
316 } else {
317 Point::new(dx, dy)
318 }
319 }
320}