/
GameInput.java
220 lines (186 loc) · 5.44 KB
/
GameInput.java
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* This file is part of the Bonsai Game Library.
*
* The Bonsai Game Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Bonsai Game Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Bonsai Game Library. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.bonsai.dev;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import java.util.List;
public class GameInput extends GameComponent implements MouseListener,
MouseMotionListener, KeyListener, FocusListener {
public GameInput(final Game game) {
super(game);
}
// Mouse
private final List<Integer> mousePressed = new LinkedList<Integer>();
private final List<Integer> mouseDown = new LinkedList<Integer>();
private int mouseX = 0;
private int mouseY = 0;
public void mouseClicked(final MouseEvent e) {
}
public final void mousePressed(final MouseEvent e) {
int button = e.getButton();
if (!mouseDown.contains(button)) {
mouseDown.add(button);
mousePressed.add(button);
}
}
public final void mouseReleased(final MouseEvent e) {
int button = e.getButton();
mouseDown.remove(Integer.valueOf(button));
if (mousePressed.contains(button)) {
mousePressed.remove(Integer.valueOf(button));
}
}
public void mouseEntered(final MouseEvent e) {
}
public final void mouseExited(final MouseEvent e) {
mouseDown.clear();
mousePressed.clear();
}
public final void mouseDragged(final MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
public final void mouseMoved(final MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
public final synchronized void clearMouse() {
mousePressed.clear();
}
// Keyboard
private final List<Integer> keysPressed = new LinkedList<Integer>();
private final List<Integer> keysDown = new LinkedList<Integer>();
private final List<Integer> keysRemove = new LinkedList<Integer>();
private final List<Integer> lastKeys = new LinkedList<Integer>();
public void keyTyped(final KeyEvent e) {
if (game.console != null && game.consoleOpen) {
game.console.onKey(e.getKeyChar());
}
}
public final void keyPressed(final KeyEvent e) {
int key = e.getKeyCode();
// Fix AutoKeyRepeat under X11
if (keysRemove.contains(key)) {
keysRemove.remove(Integer.valueOf(key));
}
if (!keysDown.contains(key)) {
keysDown.add(key);
keysPressed.add(key);
lastKeys.add(key);
if (lastKeys.size() > 16) {
lastKeys.remove(0);
}
}
e.consume();
}
public final void keyReleased(final KeyEvent e) {
int key = e.getKeyCode();
// Fix bugged PRINTSCREEN key event, only fires on keyReleased
if (key == java.awt.event.KeyEvent.VK_PRINTSCREEN) {
keysPressed.add(key);
}
keysRemove.add(key);
e.consume();
}
public final synchronized void clearKeys() {
for (Integer key : keysRemove) {
keysDown.remove(Integer.valueOf(key));
if (keysPressed.contains(key)) {
keysPressed.remove(Integer.valueOf(key));
}
}
keysRemove.clear();
keysPressed.clear();
}
// Focus
public final void focusGained(final FocusEvent e) {
game.setFocused(true);
if (game.isPausedOnFocus()) {
game.pause(false);
}
game.focusElement.requestFocus();
}
public final void focusLost(final FocusEvent e) {
game.setFocused(false);
if (game.isPausedOnFocus()) {
game.pause(true);
}
keysDown.clear();
keysPressed.clear();
mouseDown.clear();
mousePressed.clear();
}
// Getters
public final int mouseX() {
return mouseX / game.scale();
}
public final int mouseY() {
return mouseY / game.scale();
}
public final boolean mouseDown(final int button) {
return mouseDown.contains(button);
}
public final boolean mousePressed(final int button) {
return mousePressed.contains(button);
}
public final boolean keyDown(final int key) {
return keyDown(key, false);
}
public final boolean keyDown(final int key, final boolean console) {
return (!game.consoleOpen || console) && keysDown.contains(key);
}
public final boolean keyPressed(final int key) {
return keyPressed(key, false);
}
public final boolean keyPressed(final int key, final boolean console) {
return (!game.consoleOpen || console) && keysPressed.contains(key);
}
public final boolean keySequence(final List<Integer> keys) {
return keySequence(keys, false);
}
public final boolean keySequence(final List<Integer> keys,
final boolean console) {
if ((!game.consoleOpen || console) && keysPressed.size() > 0) {
int start = -1;
for (int i = 0; i < lastKeys.size(); i++) {
if (lastKeys.get(i) == keys.get(0)) {
start = i;
break;
}
}
if (start != -1) {
for (int i = 0; i < keys.size(); i++) {
if (i + start >= lastKeys.size()) {
return false;
}
if (lastKeys.get(i + start) != keys.get(i)) {
return false;
}
}
return true;
}
}
return false;
}
}