-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.pde
105 lines (84 loc) · 1.99 KB
/
Input.pde
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
static class Input {
static class KeyState {
boolean beingHeld, pressed, released;
KeyState() {
beingHeld = false;
pressed = false;
released = false;
}
}
static PApplet parent;
static ArrayList<Object> keys = new ArrayList();
static HashMap<Object, KeyState> keyState = new HashMap();
static void configure(PApplet parent) {
Input.parent = parent;
keys.add('z');
keys.add('x');
keys.add('q');
keys.add('w');
keys.add(' ');
keys.add(UP);
keys.add(DOWN);
keys.add(LEFT);
keys.add(RIGHT);
keys.add(ENTER);
keys.add(RETURN);
for (Object k : keys) {
keyState.put(k, new KeyState());
}
}
static boolean isKey(Object key) {
if (key instanceof Integer) {
return isKey((int) key);
}
return isKey((char) key);
}
static boolean isKey(char key) {
return parent.key != CODED && parent.key == key;
}
static boolean isKey(int key) {
return parent.key == CODED && parent.keyCode == key;
}
static boolean holdKey(Object key) {
return keyState.get(key).beingHeld;
}
static boolean pressKey(Object key) {
return keyState.get(key).pressed;
}
static boolean releaseKey(Object key) {
return keyState.get(key).released;
}
static void resetKeys() {
for (Object k : keys) {
KeyState state = keyState.get(k);
state.pressed = false;
state.released = false;
}
}
static void keyPressed() {
for (Object k : keys) {
if (isKey(k)) {
KeyState state = keyState.get(k);
if (!state.beingHeld) {
state.pressed = true;
}
state.beingHeld = true;
}
}
}
static void keyReleased() {
for (Object k : keys) {
if (isKey(k)) {
KeyState state = keyState.get(k);
state.released = true;
state.beingHeld = false;
}
}
}
}
void keyPressed() {
Input.keyPressed();
}
void keyReleased() {
Input.keyReleased();
}