1use std::mem;
2
3#[derive(Clone, Default)]
5pub struct Input {
6 current_input: [bool; 6],
8 last_input: [bool; 6],
9}
10
11impl Input {
12 pub const UP: usize = 0;
13 pub const DOWN: usize = 1;
14 pub const LEFT: usize = 2;
15 pub const RIGHT: usize = 3;
16 pub const ENTER: usize = 4;
17 pub const BACKSPACE: usize = 5;
18
19 pub fn update(&mut self, new_state: [bool; 6]) -> bool {
21 let last_input = mem::replace(&mut self.current_input, new_state);
22 if new_state != last_input {
23 self.last_input = last_input;
24 true
25 } else {
26 false
27 }
28 }
29
30 pub fn is_up(&self) -> bool {
31 self.current_input[Input::UP]
32 }
33
34 pub fn pressed_up(&self) -> bool {
35 self.current_input[Input::UP] && !self.last_input[Input::UP]
36 }
37
38 pub fn released_up(&self) -> bool {
39 !self.current_input[Input::UP] && self.last_input[Input::UP]
40 }
41
42 pub fn is_down(&self) -> bool {
43 self.current_input[Input::DOWN]
44 }
45
46 pub fn pressed_down(&self) -> bool {
47 self.current_input[Input::DOWN] && !self.last_input[Input::DOWN]
48 }
49
50 pub fn released_down(&self) -> bool {
51 !self.current_input[Input::DOWN] && self.last_input[Input::DOWN]
52 }
53
54 pub fn is_left(&self) -> bool {
55 self.current_input[Input::LEFT]
56 }
57
58 pub fn pressed_left(&self) -> bool {
59 self.current_input[Input::LEFT] && !self.last_input[Input::LEFT]
60 }
61
62 pub fn released_left(&self) -> bool {
63 !self.current_input[Input::LEFT] && self.last_input[Input::LEFT]
64 }
65
66 pub fn is_right(&self) -> bool {
67 self.current_input[Input::RIGHT]
68 }
69
70 pub fn pressed_right(&self) -> bool {
71 self.current_input[Input::RIGHT] && !self.last_input[Input::RIGHT]
72 }
73
74 pub fn released_right(&self) -> bool {
75 !self.current_input[Input::RIGHT] && self.last_input[Input::RIGHT]
76 }
77
78 pub fn is_enter(&self) -> bool {
79 self.current_input[Input::ENTER]
80 }
81
82 pub fn pressed_enter(&self) -> bool {
83 self.current_input[Input::ENTER] && !self.last_input[Input::ENTER]
84 }
85
86 pub fn released_enter(&self) -> bool {
87 !self.current_input[Input::ENTER] && self.last_input[Input::ENTER]
88 }
89
90 pub fn is_backspace(&self) -> bool {
91 self.current_input[Input::BACKSPACE]
92 }
93
94 pub fn pressed_backspace(&self) -> bool {
95 self.current_input[Input::BACKSPACE] && !self.last_input[Input::BACKSPACE]
96 }
97
98 pub fn released_backspace(&self) -> bool {
99 !self.current_input[Input::BACKSPACE] && self.last_input[Input::BACKSPACE]
100 }
101}