tirbofish/dropbear
main / crates / redback-runtime / src / input.rs · 2474 bytes
crates/redback-runtime/src/input.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::PlayMode;
use dropbear_engine::input::{Controller, Keyboard, Mouse};
use gilrs::{Button, GamepadId};
use winit::dpi::PhysicalPosition;
use winit::event::MouseButton;
use winit::event_loop::ActiveEventLoop;
use winit::keyboard::KeyCode;
impl Keyboard for PlayMode {
fn key_down(&mut self, key: KeyCode, _event_loop: &ActiveEventLoop) {
self.input_state.pressed_keys.insert(key);
}
fn key_up(&mut self, key: KeyCode, _event_loop: &ActiveEventLoop) {
self.input_state.pressed_keys.remove(&key);
}
}
impl Mouse for PlayMode {
fn mouse_move(&mut self, position: PhysicalPosition<f64>, delta: Option<(f64, f64)>) {
let delta = if delta.is_none() {
if let Some(last_pos) = self.input_state.last_mouse_pos {
Some((position.x - last_pos.0, position.y - last_pos.1))
} else {
None
}
} else {
delta
};
self.input_state.mouse_delta = delta;
self.input_state.mouse_pos = (position.x, position.y);
self.input_state.last_mouse_pos = Some(<(f64, f64)>::from(position));
}
fn mouse_down(&mut self, button: MouseButton) {
self.input_state.mouse_button.insert(button);
}
fn mouse_up(&mut self, button: MouseButton) {
self.input_state.mouse_button.remove(&button);
}
}
impl Controller for PlayMode {
fn button_down(&mut self, button: Button, id: GamepadId) {
self.input_state
.pressed_buttons
.entry(id)
.or_default()
.insert(button);
}
fn button_up(&mut self, button: Button, id: GamepadId) {
if let Some(buttons) = self.input_state.pressed_buttons.get_mut(&id) {
buttons.remove(&button);
}
}
fn left_stick_changed(&mut self, x: f32, y: f32, id: GamepadId) {
self.input_state.left_stick_position.insert(id, (x, y));
}
fn right_stick_changed(&mut self, x: f32, y: f32, id: GamepadId) {
self.input_state.right_stick_position.insert(id, (x, y));
}
fn on_connect(&mut self, id: GamepadId) {
self.input_state.connected_gamepads.insert(id);
}
fn on_disconnect(&mut self, id: GamepadId) {
self.input_state.connected_gamepads.remove(&id);
self.input_state.pressed_buttons.remove(&id);
self.input_state.left_stick_position.remove(&id);
self.input_state.right_stick_position.remove(&id);
}
}