1 package org.newdawn.slick.command;
2
3 import org.newdawn.slick.Input;
4 import org.newdawn.slick.util.AbstractInputAdapter;
5
6 import java.util.*;
7
8
9
10
11
12
13
14
15
16 public final class InputProvider {
17
18 private final Map<Control, Command> commands;
19
20
21 private final List<InputProviderListener> listeners = new ArrayList<>();
22
23
24 private final Input input;
25
26
27 private final Map<Command, CommandState> commandState = new HashMap<>();
28
29
30 private boolean active = true;
31
32
33
34
35
36
37
38
39 public InputProvider(Input input) {
40 this.input = input;
41 this.input.addListener(new InputListenerImpl());
42 commands = new HashMap<>();
43 }
44
45
46
47
48
49
50
51
52 public Set<Command> getUniqueCommands() {
53 final Set<Command> uniqueCommands = new HashSet<>();
54 commands.values().stream().filter(command -> !uniqueCommands.contains(command)).forEach(uniqueCommands::add);
55 return uniqueCommands;
56 }
57
58
59
60
61
62
63
64
65
66 List<Control> getControlsFor(Command command) {
67 List<Control> controlsForCommand = new ArrayList<>();
68
69 for (Map.Entry<Control, Command> controlCommandEntry : commands.entrySet()) {
70 Control key = controlCommandEntry.getKey();
71 Command value = controlCommandEntry.getValue();
72
73 if (value == command) {
74 controlsForCommand.add(key);
75 }
76 }
77 return controlsForCommand;
78 }
79
80
81
82
83
84
85
86 public void setActive(boolean active) {
87 this.active = active;
88 }
89
90
91
92
93
94
95 boolean isActive() {
96 return active;
97 }
98
99
100
101
102
103
104
105
106 public void addListener(InputProviderListener listener) {
107 listeners.add(listener);
108 }
109
110
111
112
113
114
115
116
117 public void removeListener(InputProviderListener listener) {
118 listeners.remove(listener);
119 }
120
121
122
123
124
125
126
127
128
129 public void bindCommand(Control control, Command command) {
130 commands.put(control, command);
131
132 if (commandState.get(command) == null) {
133 commandState.put(command, new CommandState());
134 }
135 }
136
137
138
139
140
141
142 public void clearCommand(Command command) {
143 List<Control> controls = getControlsFor(command);
144
145 controls.forEach(this::unbindCommand);
146 }
147
148
149
150
151
152
153
154 void unbindCommand(Control control) {
155 Command command = commands.remove(control);
156 if (command != null) {
157 if (!commandState.keySet().contains(command)) {
158 commandState.remove(command);
159 }
160 }
161 }
162
163
164
165
166
167
168
169
170 private CommandState getState(Command command) {
171 return commandState.get(command);
172 }
173
174
175
176
177
178
179
180
181
182 public boolean isCommandControlDown(Command command) {
183 return getState(command).isDown();
184 }
185
186
187
188
189
190
191
192
193
194 public boolean isCommandControlPressed(Command command) {
195 return getState(command).isPressed();
196 }
197
198
199
200
201
202
203
204
205 void firePressed(Command command) {
206 getState(command).down = true;
207 getState(command).pressed = true;
208
209 if (!isActive()) {
210 return;
211 }
212
213 for (InputProviderListener listener : listeners) {
214 listener.controlPressed(command);
215 }
216 }
217
218
219
220
221
222
223
224
225 void fireReleased(Command command) {
226 getState(command).down = false;
227
228 if (!isActive()) {
229 return;
230 }
231
232 for (InputProviderListener listener : listeners) {
233 listener.controlReleased(command);
234 }
235 }
236
237
238
239
240
241
242
243 private class CommandState {
244
245 private boolean down;
246
247
248 private boolean pressed;
249
250
251
252
253
254
255 public boolean isPressed() {
256 if (pressed) {
257 pressed = false;
258 return true;
259 }
260
261 return false;
262 }
263
264
265
266
267
268
269 public boolean isDown() {
270 return down;
271 }
272 }
273
274
275
276
277
278
279 private final class InputListenerImpl extends AbstractInputAdapter {
280
281
282
283 @Override
284 public boolean isAcceptingInput() {
285 return true;
286 }
287
288
289
290
291 @Override
292 public void keyPressed(int key, char c) {
293 Command command = commands.get(new KeyControl(key));
294 if (command != null) {
295 firePressed(command);
296 }
297 }
298
299
300
301
302 @Override
303 public void keyReleased(int key, char c) {
304 Command command = commands.get(new KeyControl(key));
305 if (command != null) {
306 fireReleased(command);
307 }
308 }
309
310
311
312
313 @Override
314 public void mousePressed(int button, int x, int y) {
315 Command command = commands.get(new MouseButtonControl(
316 button));
317 if (command != null) {
318 firePressed(command);
319 }
320 }
321
322
323
324
325 @Override
326 public void mouseReleased(int button, int x, int y) {
327 Command command = commands.get(new MouseButtonControl(
328 button));
329 if (command != null) {
330 fireReleased(command);
331 }
332 }
333
334
335
336
337 @Override
338 public void controllerLeftPressed(int controller) {
339 Command command = commands
340 .get(new ControllerDirectionControl(controller,
341 ControllerDirectionControl.LEFT));
342 if (command != null) {
343 firePressed(command);
344 }
345 }
346
347
348
349
350 @Override
351 public void controllerLeftReleased(int controller) {
352 Command command = commands
353 .get(new ControllerDirectionControl(controller,
354 ControllerDirectionControl.LEFT));
355 if (command != null) {
356 fireReleased(command);
357 }
358 }
359
360
361
362
363 @Override
364 public void controllerRightPressed(int controller) {
365 Command command = commands
366 .get(new ControllerDirectionControl(controller,
367 ControllerDirectionControl.RIGHT));
368 if (command != null) {
369 firePressed(command);
370 }
371 }
372
373
374
375
376 @Override
377 public void controllerRightReleased(int controller) {
378 Command command = commands
379 .get(new ControllerDirectionControl(controller,
380 ControllerDirectionControl.RIGHT));
381 if (command != null) {
382 fireReleased(command);
383 }
384 }
385
386
387
388
389 @Override
390 public void controllerUpPressed(int controller) {
391 Command command = commands
392 .get(new ControllerDirectionControl(controller,
393 ControllerDirectionControl.UP));
394 if (command != null)
395 firePressed(command);
396 }
397
398
399
400
401 @Override
402 public void controllerUpReleased(int controller) {
403 Command command = commands
404 .get(new ControllerDirectionControl(controller,
405 ControllerDirectionControl.UP));
406 if (command != null) {
407 fireReleased(command);
408 }
409 }
410
411
412
413
414 @Override
415 public void controllerDownPressed(int controller) {
416 Command command = commands
417 .get(new ControllerDirectionControl(controller,
418 ControllerDirectionControl.DOWN));
419 if (command != null) {
420 firePressed(command);
421 }
422 }
423
424
425
426
427 @Override
428 public void controllerDownReleased(int controller) {
429 Command command = commands
430 .get(new ControllerDirectionControl(controller,
431 ControllerDirectionControl.DOWN));
432 if (command != null) {
433 fireReleased(command);
434 }
435 }
436
437
438
439
440
441 @Override
442 public void controllerButtonPressed(int controller, int button) {
443 Command command = commands
444 .get(new ControllerButtonControl(controller, button));
445 if (command != null) {
446 firePressed(command);
447 }
448 }
449
450
451
452
453
454 @Override
455 public void controllerButtonReleased(int controller, int button) {
456 Command command = commands
457 .get(new ControllerButtonControl(controller, button));
458 if (command != null) {
459 fireReleased(command);
460 }
461 }
462 }
463 }