1use crate::inputs::KeyState::{Held, JustPressed, JustReleased, Released};
2
3#[derive(Debug, Default)]
4pub struct GamePad {
5 pub up: bool,
6 pub down: bool,
7 pub left: bool,
8 pub right: bool,
9 pub b: bool,
10 pub a: bool,
11 pub c: bool,
12 pub start: bool,
13
14 pub port_select: bool,
15}
16
17#[derive(Copy, Clone, Debug)]
18#[derive(Eq, Hash, PartialEq)]
19pub enum ControllerButton {
20 Up,
21 Down,
22 Left,
23 Right,
24 B,
25 A,
26 Start,
27 C,
28}
29
30#[derive(Copy, Clone, Debug)]
31#[derive(Eq, Hash, PartialEq)]
32pub enum InputCommand {
33 Controller1(ControllerButton),
34 Controller2(ControllerButton),
35 PlayPause,
36 SoftReset,
37 HardReset,
38}
39
40#[derive(Copy, Clone, Debug)]
41#[derive(Eq, Hash, PartialEq)]
42pub enum KeyState {
43 JustPressed,
44 Held,
45 JustReleased,
46 Released
47}
48
49
50impl KeyState {
51 pub fn is_pressed(&self) -> bool {
52 match self {
53 JustPressed => { true }
54 Held => { true }
55 JustReleased => { false }
56 Released => { false }
57 }
58 }
59
60 pub fn new(pressed: bool) -> Self {
61 if pressed {
62 return JustPressed
63 }
64 Released
65 }
66
67 pub fn update_state(&self, pressed: bool) -> Self {
68 if pressed {
69 return match self {
70 JustPressed => { JustPressed }
71 Held => { Held }
72 JustReleased => { JustPressed }
73 Released => { JustPressed }
74 }
75 }
76 match self {
77 JustPressed => { JustReleased }
78 Held => { JustReleased }
79 JustReleased => { Released }
80 Released => { Released }
81 }
82 }
83
84 pub fn update(&self) -> Self {
85 match self {
86 JustPressed => { Held }
87 Held => { Held }
88 JustReleased => { Released }
89 Released => { Released }
90 }
91 }
92}