Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/command/InputProvider.java
1456 views
1
package org.newdawn.slick.command;
2
3
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.Iterator;
6
import java.util.List;
7
import java.util.Map;
8
9
import org.newdawn.slick.Input;
10
import org.newdawn.slick.util.InputAdapter;
11
12
/**
13
* The central provider that maps real device input into abstract commands
14
* defined by the developer. Registering a control against an command with this
15
* class will cause the provider to produce an event for the command when the
16
* input is pressed and released.
17
*
18
* @author joverton
19
*/
20
public class InputProvider {
21
/** The commands that have been defined */
22
private HashMap commands;
23
24
/** The list of listeners that may be listening */
25
private ArrayList listeners = new ArrayList();
26
27
/** The input context we're responding to */
28
private Input input;
29
30
/** The command input states */
31
private HashMap commandState = new HashMap();
32
33
/** True if this provider is actively sending events */
34
private boolean active = true;
35
36
/**
37
* Create a new input proider which will provide abstract input descriptions
38
* based on the input from the supplied context.
39
*
40
* @param input
41
* The input from which this provider will receive events
42
*/
43
public InputProvider(Input input) {
44
this.input = input;
45
46
input.addListener(new InputListenerImpl());
47
commands = new HashMap();
48
}
49
50
/**
51
* Get the list of commands that have been registered with the provider,
52
* i.e. the commands that can be issued to the listeners
53
*
54
* @return The list of commands (@see Command) that can be issued from this
55
* provider
56
*/
57
public List getUniqueCommands() {
58
List uniqueCommands = new ArrayList();
59
60
for (Iterator it = commands.values().iterator(); it.hasNext();) {
61
Command command = (Command) it.next();
62
63
if (!uniqueCommands.contains(command)) {
64
uniqueCommands.add(command);
65
}
66
}
67
68
return uniqueCommands;
69
}
70
71
/**
72
* Get a list of the registered controls (@see Control) that can cause a
73
* particular command to be invoked
74
*
75
* @param command
76
* The command to be invoked
77
* @return The list of controls that can cause the command (@see Control)
78
*/
79
public List getControlsFor(Command command) {
80
List controlsForCommand = new ArrayList();
81
82
for (Iterator it = commands.entrySet().iterator(); it.hasNext();) {
83
Map.Entry entry = (Map.Entry) it.next();
84
Control key = (Control) entry.getKey();
85
Command value = (Command) entry.getValue();
86
87
if (value == command) {
88
controlsForCommand.add(key);
89
}
90
}
91
return controlsForCommand;
92
}
93
94
/**
95
* Indicate whether this provider should be sending events
96
*
97
* @param active
98
* True if this provider should be sending events
99
*/
100
public void setActive(boolean active) {
101
this.active = active;
102
}
103
104
/**
105
* Check if this provider should be sending events
106
*
107
* @return True if this provider should be sending events
108
*/
109
public boolean isActive() {
110
return active;
111
}
112
113
/**
114
* Add a listener to the provider. This listener will be notified of
115
* commands detected from the input.
116
*
117
* @param listener
118
* The listener to be added
119
*/
120
public void addListener(InputProviderListener listener) {
121
listeners.add(listener);
122
}
123
124
/**
125
* Remove a listener from this provider. The listener will no longer be
126
* provided with notification of commands performe.
127
*
128
* @param listener
129
* The listener to be removed
130
*/
131
public void removeListener(InputProviderListener listener) {
132
listeners.remove(listener);
133
}
134
135
/**
136
* Bind an command to a control.
137
*
138
* @param command
139
* The command to bind to
140
* @param control
141
* The control that is pressed/released to represent the command
142
*/
143
public void bindCommand(Control control, Command command) {
144
commands.put(control, command);
145
146
if (commandState.get(command) == null) {
147
commandState.put(command, new CommandState());
148
}
149
}
150
151
/**
152
* Clear all the controls that have been configured for a given command
153
*
154
* @param command The command whose controls should be unbound
155
*/
156
public void clearCommand(Command command) {
157
List controls = getControlsFor(command);
158
159
for (int i=0;i<controls.size();i++) {
160
unbindCommand((Control) controls.get(i));
161
}
162
}
163
164
/**
165
* Unbinds the command associated with this control
166
*
167
* @param control
168
* The control to remove
169
*/
170
public void unbindCommand(Control control) {
171
Command command = (Command) commands.remove(control);
172
if (command != null) {
173
if (!commands.keySet().contains(command)) {
174
commandState.remove(command);
175
}
176
}
177
}
178
179
/**
180
* Get the recorded state for a given command
181
*
182
* @param command
183
* The command to get the state for
184
* @return The given command state
185
*/
186
private CommandState getState(Command command) {
187
return (CommandState) commandState.get(command);
188
}
189
190
/**
191
* Check if the last control event we recieved related to the given command
192
* indicated that a control was down
193
*
194
* @param command
195
* The command to check
196
* @return True if the last event indicated a button down
197
*/
198
public boolean isCommandControlDown(Command command) {
199
return getState(command).isDown();
200
}
201
202
/**
203
* Check if one of the controls related to the command specified has been
204
* pressed since we last called this method
205
*
206
* @param command
207
* The command to check
208
* @return True if one of the controls has been pressed
209
*/
210
public boolean isCommandControlPressed(Command command) {
211
return getState(command).isPressed();
212
}
213
214
/**
215
* Fire notification to any interested listeners that a control has been
216
* pressed indication an particular command
217
*
218
* @param command
219
* The command that has been pressed
220
*/
221
protected void firePressed(Command command) {
222
getState(command).down = true;
223
getState(command).pressed = true;
224
225
if (!isActive()) {
226
return;
227
}
228
229
for (int i = 0; i < listeners.size(); i++) {
230
((InputProviderListener) listeners.get(i)).controlPressed(command);
231
}
232
}
233
234
/**
235
* Fire notification to any interested listeners that a control has been
236
* released indication an particular command should be stopped
237
*
238
* @param command
239
* The command that has been pressed
240
*/
241
protected void fireReleased(Command command) {
242
getState(command).down = false;
243
244
if (!isActive()) {
245
return;
246
}
247
248
for (int i = 0; i < listeners.size(); i++) {
249
((InputProviderListener) listeners.get(i)).controlReleased(command);
250
}
251
}
252
253
/**
254
* A token representing the state of all the controls causing an command to
255
* be invoked
256
*
257
* @author kevin
258
*/
259
private class CommandState {
260
/** True if one of the controls for this command is down */
261
private boolean down;
262
263
/** True if one of the controls for this command is pressed */
264
private boolean pressed;
265
266
/**
267
* Check if a control for the command has been pressed since last call.
268
*
269
* @return True if the command has been pressed
270
*/
271
public boolean isPressed() {
272
if (pressed) {
273
pressed = false;
274
return true;
275
}
276
277
return false;
278
}
279
280
/**
281
* Check if the last event we had indicated the control was pressed
282
*
283
* @return True if the control was pressed
284
*/
285
public boolean isDown() {
286
return down;
287
}
288
}
289
290
/**
291
* A simple listener to respond to input and look up any required commands
292
*
293
* @author kevin
294
*/
295
private class InputListenerImpl extends InputAdapter {
296
/**
297
* @see org.newdawn.slick.util.InputAdapter#isAcceptingInput()
298
*/
299
public boolean isAcceptingInput() {
300
return true;
301
}
302
303
/**
304
* @see org.newdawn.slick.util.InputAdapter#keyPressed(int, char)
305
*/
306
public void keyPressed(int key, char c) {
307
Command command = (Command) commands.get(new KeyControl(key));
308
if (command != null) {
309
firePressed(command);
310
}
311
}
312
313
/**
314
* @see org.newdawn.slick.util.InputAdapter#keyReleased(int, char)
315
*/
316
public void keyReleased(int key, char c) {
317
Command command = (Command) commands.get(new KeyControl(key));
318
if (command != null) {
319
fireReleased(command);
320
}
321
}
322
323
/**
324
* @see org.newdawn.slick.util.InputAdapter#mousePressed(int, int, int)
325
*/
326
public void mousePressed(int button, int x, int y) {
327
Command command = (Command) commands.get(new MouseButtonControl(
328
button));
329
if (command != null) {
330
firePressed(command);
331
}
332
}
333
334
/**
335
* @see org.newdawn.slick.util.InputAdapter#mouseReleased(int, int, int)
336
*/
337
public void mouseReleased(int button, int x, int y) {
338
Command command = (Command) commands.get(new MouseButtonControl(
339
button));
340
if (command != null) {
341
fireReleased(command);
342
}
343
}
344
345
/**
346
* @see org.newdawn.slick.util.InputAdapter#controllerLeftPressed(int)
347
*/
348
public void controllerLeftPressed(int controller) {
349
Command command = (Command) commands
350
.get(new ControllerDirectionControl(controller,
351
ControllerDirectionControl.LEFT));
352
if (command != null) {
353
firePressed(command);
354
}
355
}
356
357
/**
358
* @see org.newdawn.slick.util.InputAdapter#controllerLeftReleased(int)
359
*/
360
public void controllerLeftReleased(int controller) {
361
Command command = (Command) commands
362
.get(new ControllerDirectionControl(controller,
363
ControllerDirectionControl.LEFT));
364
if (command != null) {
365
fireReleased(command);
366
}
367
}
368
369
/**
370
* @see org.newdawn.slick.util.InputAdapter#controllerRightPressed(int)
371
*/
372
public void controllerRightPressed(int controller) {
373
Command command = (Command) commands
374
.get(new ControllerDirectionControl(controller,
375
ControllerDirectionControl.RIGHT));
376
if (command != null) {
377
firePressed(command);
378
}
379
}
380
381
/**
382
* @see org.newdawn.slick.util.InputAdapter#controllerRightReleased(int)
383
*/
384
public void controllerRightReleased(int controller) {
385
Command command = (Command) commands
386
.get(new ControllerDirectionControl(controller,
387
ControllerDirectionControl.RIGHT));
388
if (command != null) {
389
fireReleased(command);
390
}
391
}
392
393
/**
394
* @see org.newdawn.slick.util.InputAdapter#controllerUpPressed(int)
395
*/
396
public void controllerUpPressed(int controller) {
397
Command command = (Command) commands
398
.get(new ControllerDirectionControl(controller,
399
ControllerDirectionControl.UP));
400
if (command != null)
401
firePressed(command);
402
}
403
404
/**
405
* @see org.newdawn.slick.util.InputAdapter#controllerUpReleased(int)
406
*/
407
public void controllerUpReleased(int controller) {
408
Command command = (Command) commands
409
.get(new ControllerDirectionControl(controller,
410
ControllerDirectionControl.UP));
411
if (command != null) {
412
fireReleased(command);
413
}
414
}
415
416
/**
417
* @see org.newdawn.slick.util.InputAdapter#controllerDownPressed(int)
418
*/
419
public void controllerDownPressed(int controller) {
420
Command command = (Command) commands
421
.get(new ControllerDirectionControl(controller,
422
ControllerDirectionControl.DOWN));
423
if (command != null) {
424
firePressed(command);
425
}
426
}
427
428
/**
429
* @see org.newdawn.slick.util.InputAdapter#controllerDownReleased(int)
430
*/
431
public void controllerDownReleased(int controller) {
432
Command command = (Command) commands
433
.get(new ControllerDirectionControl(controller,
434
ControllerDirectionControl.DOWN));
435
if (command != null) {
436
fireReleased(command);
437
}
438
}
439
440
/**
441
* @see org.newdawn.slick.util.InputAdapter#controllerButtonPressed(int,
442
* int)
443
*/
444
public void controllerButtonPressed(int controller, int button) {
445
Command command = (Command) commands
446
.get(new ControllerButtonControl(controller, button));
447
if (command != null) {
448
firePressed(command);
449
}
450
}
451
452
/**
453
* @see org.newdawn.slick.util.InputAdapter#controllerButtonReleased(int,
454
* int)
455
*/
456
public void controllerButtonReleased(int controller, int button) {
457
Command command = (Command) commands
458
.get(new ControllerButtonControl(controller, button));
459
if (command != null) {
460
fireReleased(command);
461
}
462
}
463
};
464
}
465
466