blob: 4ea82f7756b913e4559815aabb59b75fca0511c0 (
plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
use crate::display::Display;
use gilrs::{Button, GamepadId, Gilrs};
use minifb::Key;
pub struct Gamepad {
gilrs: Gilrs,
gamepad_id: Option<GamepadId>,
}
pub trait Input {
fn update_events(&mut self);
fn get_action_gamepad_reg(&self) -> u8;
fn get_direction_gamepad_reg(&self) -> u8;
}
impl Gamepad {
pub fn new() -> Self {
let mut gilrs = Gilrs::new().unwrap();
let gamepad_id = if let Some((gamepad_id, _gamepad)) = gilrs.gamepads().next() {
println!("Found Gamepad id: {:?}", gamepad_id);
Some(gamepad_id)
} else {
println!("No gamepad found");
None
};
Self { gilrs, gamepad_id }
}
pub fn check_special_actions(&self, is_debug: &mut bool) {
if let Some(gamepad_id) = self.gamepad_id {
if let Some(gamepad) = self.gilrs.connected_gamepad(gamepad_id) {
*is_debug = gamepad.is_pressed(Button::West);
}
}
}
}
impl Input for Gamepad {
fn update_events(&mut self) {
while let Some(_) = self.gilrs.next_event() {}
}
fn get_action_gamepad_reg(&self) -> u8 {
let mut res = 0xf;
if let Some(gamepad_id) = self.gamepad_id {
if let Some(gamepad) = self.gilrs.connected_gamepad(gamepad_id) {
if gamepad.is_pressed(Button::East) {
res &= 0b1110;
}
if gamepad.is_pressed(Button::South) {
res &= 0b1101;
}
if gamepad.is_pressed(Button::Select) {
res &= 0b1011;
}
if gamepad.is_pressed(Button::Start) {
res &= 0b0111;
}
}
}
res
}
fn get_direction_gamepad_reg(&self) -> u8 {
let mut res = 0xf;
if let Some(gamepad_id) = self.gamepad_id {
if let Some(gamepad) = self.gilrs.connected_gamepad(gamepad_id) {
if gamepad.is_pressed(Button::DPadRight) {
res &= 0b1110;
}
if gamepad.is_pressed(Button::DPadLeft) {
res &= 0b1101;
}
if gamepad.is_pressed(Button::DPadUp) {
res &= 0b1011;
}
if gamepad.is_pressed(Button::DPadDown) {
res &= 0b0111;
}
}
}
res
}
}
impl Input for Display {
fn update_events(&mut self) {}
fn get_action_gamepad_reg(&self) -> u8 {
let mut res = 0xf;
if self.window.is_key_down(Key::A) {
res &= 0b1110;
}
if self.window.is_key_down(Key::B) {
res &= 0b1101;
}
if self.window.is_key_down(Key::Backspace) {
res &= 0b1011;
}
if self.window.is_key_down(Key::Enter) {
res &= 0b0111;
}
res
}
fn get_direction_gamepad_reg(&self) -> u8 {
let mut res = 0xf;
if self.window.is_key_down(Key::Right) {
res &= 0b1110;
}
if self.window.is_key_down(Key::Left) {
res &= 0b1101;
}
if self.window.is_key_down(Key::Up) {
res &= 0b1011;
}
if self.window.is_key_down(Key::Down) {
res &= 0b0111;
}
res
}
}
|