Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/Input.java
1456 views
1
package org.newdawn.slick;
2
3
import java.io.IOException;
4
import java.io.OutputStream;
5
import java.util.ArrayList;
6
import java.util.Arrays;
7
import java.util.HashSet;
8
import java.util.Iterator;
9
10
import org.lwjgl.LWJGLException;
11
import org.lwjgl.input.Controller;
12
import org.lwjgl.input.Controllers;
13
import org.lwjgl.input.Keyboard;
14
import org.lwjgl.input.Mouse;
15
import org.lwjgl.opengl.Display;
16
import org.newdawn.slick.util.Log;
17
18
/**
19
* A wrapped for all keyboard, mouse and controller input
20
*
21
* @author kevin
22
*/
23
public class Input {
24
/** The controller index to pass to check all controllers */
25
public static final int ANY_CONTROLLER = -1;
26
27
/** The maximum number of buttons on controllers */
28
private static final int MAX_BUTTONS = 100;
29
30
/** */
31
public static final int KEY_ESCAPE = 0x01;
32
/** */
33
public static final int KEY_1 = 0x02;
34
/** */
35
public static final int KEY_2 = 0x03;
36
/** */
37
public static final int KEY_3 = 0x04;
38
/** */
39
public static final int KEY_4 = 0x05;
40
/** */
41
public static final int KEY_5 = 0x06;
42
/** */
43
public static final int KEY_6 = 0x07;
44
/** */
45
public static final int KEY_7 = 0x08;
46
/** */
47
public static final int KEY_8 = 0x09;
48
/** */
49
public static final int KEY_9 = 0x0A;
50
/** */
51
public static final int KEY_0 = 0x0B;
52
/** */
53
public static final int KEY_MINUS = 0x0C; /* - on main keyboard */
54
/** */
55
public static final int KEY_EQUALS = 0x0D;
56
/** */
57
public static final int KEY_BACK = 0x0E; /* backspace */
58
/** */
59
public static final int KEY_TAB = 0x0F;
60
/** */
61
public static final int KEY_Q = 0x10;
62
/** */
63
public static final int KEY_W = 0x11;
64
/** */
65
public static final int KEY_E = 0x12;
66
/** */
67
public static final int KEY_R = 0x13;
68
/** */
69
public static final int KEY_T = 0x14;
70
/** */
71
public static final int KEY_Y = 0x15;
72
/** */
73
public static final int KEY_U = 0x16;
74
/** */
75
public static final int KEY_I = 0x17;
76
/** */
77
public static final int KEY_O = 0x18;
78
/** */
79
public static final int KEY_P = 0x19;
80
/** */
81
public static final int KEY_LBRACKET = 0x1A;
82
/** */
83
public static final int KEY_RBRACKET = 0x1B;
84
/** */
85
public static final int KEY_RETURN = 0x1C; /* Enter on main keyboard */
86
/** */
87
public static final int KEY_ENTER = 0x1C; /* Enter on main keyboard */
88
/** */
89
public static final int KEY_LCONTROL = 0x1D;
90
/** */
91
public static final int KEY_A = 0x1E;
92
/** */
93
public static final int KEY_S = 0x1F;
94
/** */
95
public static final int KEY_D = 0x20;
96
/** */
97
public static final int KEY_F = 0x21;
98
/** */
99
public static final int KEY_G = 0x22;
100
/** */
101
public static final int KEY_H = 0x23;
102
/** */
103
public static final int KEY_J = 0x24;
104
/** */
105
public static final int KEY_K = 0x25;
106
/** */
107
public static final int KEY_L = 0x26;
108
/** */
109
public static final int KEY_SEMICOLON = 0x27;
110
/** */
111
public static final int KEY_APOSTROPHE = 0x28;
112
/** */
113
public static final int KEY_GRAVE = 0x29; /* accent grave */
114
/** */
115
public static final int KEY_LSHIFT = 0x2A;
116
/** */
117
public static final int KEY_BACKSLASH = 0x2B;
118
/** */
119
public static final int KEY_Z = 0x2C;
120
/** */
121
public static final int KEY_X = 0x2D;
122
/** */
123
public static final int KEY_C = 0x2E;
124
/** */
125
public static final int KEY_V = 0x2F;
126
/** */
127
public static final int KEY_B = 0x30;
128
/** */
129
public static final int KEY_N = 0x31;
130
/** */
131
public static final int KEY_M = 0x32;
132
/** */
133
public static final int KEY_COMMA = 0x33;
134
/** */
135
public static final int KEY_PERIOD = 0x34; /* . on main keyboard */
136
/** */
137
public static final int KEY_SLASH = 0x35; /* / on main keyboard */
138
/** */
139
public static final int KEY_RSHIFT = 0x36;
140
/** */
141
public static final int KEY_MULTIPLY = 0x37; /* * on numeric keypad */
142
/** */
143
public static final int KEY_LMENU = 0x38; /* left Alt */
144
/** */
145
public static final int KEY_SPACE = 0x39;
146
/** */
147
public static final int KEY_CAPITAL = 0x3A;
148
/** */
149
public static final int KEY_F1 = 0x3B;
150
/** */
151
public static final int KEY_F2 = 0x3C;
152
/** */
153
public static final int KEY_F3 = 0x3D;
154
/** */
155
public static final int KEY_F4 = 0x3E;
156
/** */
157
public static final int KEY_F5 = 0x3F;
158
/** */
159
public static final int KEY_F6 = 0x40;
160
/** */
161
public static final int KEY_F7 = 0x41;
162
/** */
163
public static final int KEY_F8 = 0x42;
164
/** */
165
public static final int KEY_F9 = 0x43;
166
/** */
167
public static final int KEY_F10 = 0x44;
168
/** */
169
public static final int KEY_NUMLOCK = 0x45;
170
/** */
171
public static final int KEY_SCROLL = 0x46; /* Scroll Lock */
172
/** */
173
public static final int KEY_NUMPAD7 = 0x47;
174
/** */
175
public static final int KEY_NUMPAD8 = 0x48;
176
/** */
177
public static final int KEY_NUMPAD9 = 0x49;
178
/** */
179
public static final int KEY_SUBTRACT = 0x4A; /* - on numeric keypad */
180
/** */
181
public static final int KEY_NUMPAD4 = 0x4B;
182
/** */
183
public static final int KEY_NUMPAD5 = 0x4C;
184
/** */
185
public static final int KEY_NUMPAD6 = 0x4D;
186
/** */
187
public static final int KEY_ADD = 0x4E; /* + on numeric keypad */
188
/** */
189
public static final int KEY_NUMPAD1 = 0x4F;
190
/** */
191
public static final int KEY_NUMPAD2 = 0x50;
192
/** */
193
public static final int KEY_NUMPAD3 = 0x51;
194
/** */
195
public static final int KEY_NUMPAD0 = 0x52;
196
/** */
197
public static final int KEY_DECIMAL = 0x53; /* . on numeric keypad */
198
/** */
199
public static final int KEY_F11 = 0x57;
200
/** */
201
public static final int KEY_F12 = 0x58;
202
/** */
203
public static final int KEY_F13 = 0x64; /* (NEC PC98) */
204
/** */
205
public static final int KEY_F14 = 0x65; /* (NEC PC98) */
206
/** */
207
public static final int KEY_F15 = 0x66; /* (NEC PC98) */
208
/** */
209
public static final int KEY_KANA = 0x70; /* (Japanese keyboard) */
210
/** */
211
public static final int KEY_CONVERT = 0x79; /* (Japanese keyboard) */
212
/** */
213
public static final int KEY_NOCONVERT = 0x7B; /* (Japanese keyboard) */
214
/** */
215
public static final int KEY_YEN = 0x7D; /* (Japanese keyboard) */
216
/** */
217
public static final int KEY_NUMPADEQUALS = 0x8D; /* = on numeric keypad (NEC PC98) */
218
/** */
219
public static final int KEY_CIRCUMFLEX = 0x90; /* (Japanese keyboard) */
220
/** */
221
public static final int KEY_AT = 0x91; /* (NEC PC98) */
222
/** */
223
public static final int KEY_COLON = 0x92; /* (NEC PC98) */
224
/** */
225
public static final int KEY_UNDERLINE = 0x93; /* (NEC PC98) */
226
/** */
227
public static final int KEY_KANJI = 0x94; /* (Japanese keyboard) */
228
/** */
229
public static final int KEY_STOP = 0x95; /* (NEC PC98) */
230
/** */
231
public static final int KEY_AX = 0x96; /* (Japan AX) */
232
/** */
233
public static final int KEY_UNLABELED = 0x97; /* (J3100) */
234
/** */
235
public static final int KEY_NUMPADENTER = 0x9C; /* Enter on numeric keypad */
236
/** */
237
public static final int KEY_RCONTROL = 0x9D;
238
/** */
239
public static final int KEY_NUMPADCOMMA = 0xB3; /* , on numeric keypad (NEC PC98) */
240
/** */
241
public static final int KEY_DIVIDE = 0xB5; /* / on numeric keypad */
242
/** */
243
public static final int KEY_SYSRQ = 0xB7;
244
/** */
245
public static final int KEY_RMENU = 0xB8; /* right Alt */
246
/** */
247
public static final int KEY_PAUSE = 0xC5; /* Pause */
248
/** */
249
public static final int KEY_HOME = 0xC7; /* Home on arrow keypad */
250
/** */
251
public static final int KEY_UP = 0xC8; /* UpArrow on arrow keypad */
252
/** */
253
public static final int KEY_PRIOR = 0xC9; /* PgUp on arrow keypad */
254
/** */
255
public static final int KEY_LEFT = 0xCB; /* LeftArrow on arrow keypad */
256
/** */
257
public static final int KEY_RIGHT = 0xCD; /* RightArrow on arrow keypad */
258
/** */
259
public static final int KEY_END = 0xCF; /* End on arrow keypad */
260
/** */
261
public static final int KEY_DOWN = 0xD0; /* DownArrow on arrow keypad */
262
/** */
263
public static final int KEY_NEXT = 0xD1; /* PgDn on arrow keypad */
264
/** */
265
public static final int KEY_INSERT = 0xD2; /* Insert on arrow keypad */
266
/** */
267
public static final int KEY_DELETE = 0xD3; /* Delete on arrow keypad */
268
/** */
269
public static final int KEY_LWIN = 0xDB; /* Left Windows key */
270
/** */
271
public static final int KEY_RWIN = 0xDC; /* Right Windows key */
272
/** */
273
public static final int KEY_APPS = 0xDD; /* AppMenu key */
274
/** */
275
public static final int KEY_POWER = 0xDE;
276
/** */
277
public static final int KEY_SLEEP = 0xDF;
278
279
/** A helper for left ALT */
280
public static final int KEY_LALT = KEY_LMENU;
281
/** A helper for right ALT */
282
public static final int KEY_RALT = KEY_RMENU;
283
284
/** Control index */
285
private static final int LEFT = 0;
286
/** Control index */
287
private static final int RIGHT = 1;
288
/** Control index */
289
private static final int UP = 2;
290
/** Control index */
291
private static final int DOWN = 3;
292
/** Control index */
293
private static final int BUTTON1 = 4;
294
/** Control index */
295
private static final int BUTTON2 = 5;
296
/** Control index */
297
private static final int BUTTON3 = 6;
298
/** Control index */
299
private static final int BUTTON4 = 7;
300
/** Control index */
301
private static final int BUTTON5 = 8;
302
/** Control index */
303
private static final int BUTTON6 = 9;
304
/** Control index */
305
private static final int BUTTON7 = 10;
306
/** Control index */
307
private static final int BUTTON8 = 11;
308
/** Control index */
309
private static final int BUTTON9 = 12;
310
/** Control index */
311
private static final int BUTTON10 = 13;
312
313
/** The left mouse button indicator */
314
public static final int MOUSE_LEFT_BUTTON = 0;
315
/** The right mouse button indicator */
316
public static final int MOUSE_RIGHT_BUTTON = 1;
317
/** The middle mouse button indicator */
318
public static final int MOUSE_MIDDLE_BUTTON = 2;
319
320
/** True if the controllers system has been initialised */
321
private static boolean controllersInited = false;
322
/** The list of controllers */
323
private static ArrayList controllers = new ArrayList();
324
325
/** The last recorded mouse x position */
326
private int lastMouseX;
327
/** The last recorded mouse y position */
328
private int lastMouseY;
329
/** THe state of the mouse buttons */
330
protected boolean[] mousePressed = new boolean[10];
331
/** THe state of the controller buttons */
332
private boolean[][] controllerPressed = new boolean[100][MAX_BUTTONS];
333
334
/** The character values representing the pressed keys */
335
protected char[] keys = new char[1024];
336
/** True if the key has been pressed since last queries */
337
protected boolean[] pressed = new boolean[1024];
338
/** The time since the next key repeat to be fired for the key */
339
protected long[] nextRepeat = new long[1024];
340
341
/** The control states from the controllers */
342
private boolean[][] controls = new boolean[10][MAX_BUTTONS+10];
343
/** True if the event has been consumed */
344
protected boolean consumed = false;
345
/** A list of listeners to be notified of input events */
346
protected HashSet allListeners = new HashSet();
347
/** The listeners to notify of key events */
348
protected ArrayList keyListeners = new ArrayList();
349
/** The listener to add */
350
protected ArrayList keyListenersToAdd = new ArrayList();
351
/** The listeners to notify of mouse events */
352
protected ArrayList mouseListeners = new ArrayList();
353
/** The listener to add */
354
protected ArrayList mouseListenersToAdd = new ArrayList();
355
/** The listener to nofiy of controller events */
356
protected ArrayList controllerListeners = new ArrayList();
357
/** The current value of the wheel */
358
private int wheel;
359
/** The height of the display */
360
private int height;
361
362
/** True if the display is active */
363
private boolean displayActive = true;
364
365
/** True if key repeat is enabled */
366
private boolean keyRepeat;
367
/** The initial delay for key repeat starts */
368
private int keyRepeatInitial;
369
/** The interval of key repeat */
370
private int keyRepeatInterval;
371
372
/** True if the input is currently paused */
373
private boolean paused;
374
/** The scale to apply to screen coordinates */
375
private float scaleX = 1;
376
/** The scale to apply to screen coordinates */
377
private float scaleY = 1;
378
/** The offset to apply to screen coordinates */
379
private float xoffset = 0;
380
/** The offset to apply to screen coordinates */
381
private float yoffset = 0;
382
383
/** The delay before determining a single or double click */
384
private int doubleClickDelay = 250;
385
/** The timer running out for a single click */
386
private long doubleClickTimeout = 0;
387
388
/** The clicked x position */
389
private int clickX;
390
/** The clicked y position */
391
private int clickY;
392
/** The clicked button */
393
private int clickButton;
394
395
/** The x position location the mouse was pressed */
396
private int pressedX = -1;
397
398
/** The x position location the mouse was pressed */
399
private int pressedY = -1;
400
401
/** The pixel distance the mouse can move to accept a mouse click */
402
private int mouseClickTolerance = 5;
403
404
/**
405
* Disables support for controllers. This means the jinput JAR and native libs
406
* are not required.
407
*/
408
public static void disableControllers() {
409
controllersInited = true;
410
}
411
412
/**
413
* Create a new input with the height of the screen
414
*
415
* @param height The height of the screen
416
*/
417
public Input(int height) {
418
init(height);
419
}
420
421
/**
422
* Set the double click interval, the time between the first
423
* and second clicks that should be interpreted as a
424
* double click.
425
*
426
* @param delay The delay between clicks
427
*/
428
public void setDoubleClickInterval(int delay) {
429
doubleClickDelay = delay;
430
}
431
432
/**
433
* Set the pixel distance the mouse can move to accept a mouse click.
434
* Default is 5.
435
*
436
* @param mouseClickTolerance The number of pixels.
437
*/
438
public void setMouseClickTolerance (int mouseClickTolerance) {
439
this.mouseClickTolerance = mouseClickTolerance;
440
}
441
442
/**
443
* Set the scaling to apply to screen coordinates
444
*
445
* @param scaleX The scaling to apply to the horizontal axis
446
* @param scaleY The scaling to apply to the vertical axis
447
*/
448
public void setScale(float scaleX, float scaleY) {
449
this.scaleX = scaleX;
450
this.scaleY = scaleY;
451
}
452
453
/**
454
* Set the offset to apply to the screen coodinates
455
*
456
* @param xoffset The offset on the x-axis
457
* @param yoffset The offset on the y-axis
458
*/
459
public void setOffset(float xoffset, float yoffset) {
460
this.xoffset = xoffset;
461
this.yoffset = yoffset;
462
}
463
464
/**
465
* Reset the transformation being applied to the input to the default
466
*/
467
public void resetInputTransform() {
468
setOffset(0, 0);
469
setScale(1, 1);
470
}
471
472
/**
473
* Add a listener to be notified of input events
474
*
475
* @param listener The listener to be notified
476
*/
477
public void addListener(InputListener listener) {
478
addKeyListener(listener);
479
addMouseListener(listener);
480
addControllerListener(listener);
481
}
482
483
/**
484
* Add a key listener to be notified of key input events
485
*
486
* @param listener The listener to be notified
487
*/
488
public void addKeyListener(KeyListener listener) {
489
keyListenersToAdd.add(listener);
490
}
491
492
/**
493
* Add a key listener to be notified of key input events
494
*
495
* @param listener The listener to be notified
496
*/
497
private void addKeyListenerImpl(KeyListener listener) {
498
if (keyListeners.contains(listener)) {
499
return;
500
}
501
keyListeners.add(listener);
502
allListeners.add(listener);
503
}
504
505
/**
506
* Add a mouse listener to be notified of mouse input events
507
*
508
* @param listener The listener to be notified
509
*/
510
public void addMouseListener(MouseListener listener) {
511
mouseListenersToAdd.add(listener);
512
}
513
514
/**
515
* Add a mouse listener to be notified of mouse input events
516
*
517
* @param listener The listener to be notified
518
*/
519
private void addMouseListenerImpl(MouseListener listener) {
520
if (mouseListeners.contains(listener)) {
521
return;
522
}
523
mouseListeners.add(listener);
524
allListeners.add(listener);
525
}
526
527
/**
528
* Add a controller listener to be notified of controller input events
529
*
530
* @param listener The listener to be notified
531
*/
532
public void addControllerListener(ControllerListener listener) {
533
if (controllerListeners.contains(listener)) {
534
return;
535
}
536
controllerListeners.add(listener);
537
allListeners.add(listener);
538
}
539
540
/**
541
* Remove all the listeners from this input
542
*/
543
public void removeAllListeners() {
544
removeAllKeyListeners();
545
removeAllMouseListeners();
546
removeAllControllerListeners();
547
}
548
549
/**
550
* Remove all the key listeners from this input
551
*/
552
public void removeAllKeyListeners() {
553
allListeners.removeAll(keyListeners);
554
keyListeners.clear();
555
}
556
557
/**
558
* Remove all the mouse listeners from this input
559
*/
560
public void removeAllMouseListeners() {
561
allListeners.removeAll(mouseListeners);
562
mouseListeners.clear();
563
}
564
565
/**
566
* Remove all the controller listeners from this input
567
*/
568
public void removeAllControllerListeners() {
569
allListeners.removeAll(controllerListeners);
570
controllerListeners.clear();
571
}
572
573
/**
574
* Add a listener to be notified of input events. This listener
575
* will get events before others that are currently registered
576
*
577
* @param listener The listener to be notified
578
*/
579
public void addPrimaryListener(InputListener listener) {
580
removeListener(listener);
581
582
keyListeners.add(0, listener);
583
mouseListeners.add(0, listener);
584
controllerListeners.add(0, listener);
585
586
allListeners.add(listener);
587
}
588
589
/**
590
* Remove a listener that will no longer be notified
591
*
592
* @param listener The listen to be removed
593
*/
594
public void removeListener(InputListener listener) {
595
removeKeyListener(listener);
596
removeMouseListener(listener);
597
removeControllerListener(listener);
598
}
599
600
/**
601
* Remove a key listener that will no longer be notified
602
*
603
* @param listener The listen to be removed
604
*/
605
public void removeKeyListener(KeyListener listener) {
606
keyListeners.remove(listener);
607
608
if (!mouseListeners.contains(listener) && !controllerListeners.contains(listener)) {
609
allListeners.remove(listener);
610
}
611
}
612
613
/**
614
* Remove a controller listener that will no longer be notified
615
*
616
* @param listener The listen to be removed
617
*/
618
public void removeControllerListener(ControllerListener listener) {
619
controllerListeners.remove(listener);
620
621
if (!mouseListeners.contains(listener) && !keyListeners.contains(listener)) {
622
allListeners.remove(listener);
623
}
624
}
625
626
/**
627
* Remove a mouse listener that will no longer be notified
628
*
629
* @param listener The listen to be removed
630
*/
631
public void removeMouseListener(MouseListener listener) {
632
mouseListeners.remove(listener);
633
634
if (!controllerListeners.contains(listener) && !keyListeners.contains(listener)) {
635
allListeners.remove(listener);
636
}
637
}
638
639
/**
640
* Initialise the input system
641
*
642
* @param height The height of the window
643
*/
644
void init(int height) {
645
this.height = height;
646
lastMouseX = getMouseX();
647
lastMouseY = getMouseY();
648
}
649
650
/**
651
* Get the character representation of the key identified by the specified code
652
*
653
* @param code The key code of the key to retrieve the name of
654
* @return The name or character representation of the key requested
655
*/
656
public static String getKeyName(int code) {
657
return Keyboard.getKeyName(code);
658
}
659
660
/**
661
* Check if a particular key has been pressed since this method
662
* was last called for the specified key
663
*
664
* @param code The key code of the key to check
665
* @return True if the key has been pressed
666
*/
667
public boolean isKeyPressed(int code) {
668
if (pressed[code]) {
669
pressed[code] = false;
670
return true;
671
}
672
673
return false;
674
}
675
676
/**
677
* Check if a mouse button has been pressed since last call
678
*
679
* @param button The button to check
680
* @return True if the button has been pressed since last call
681
*/
682
public boolean isMousePressed(int button) {
683
if (mousePressed[button]) {
684
mousePressed[button] = false;
685
return true;
686
}
687
688
return false;
689
}
690
691
/**
692
* Check if a controller button has been pressed since last
693
* time
694
*
695
* @param button The button to check for (note that this includes directional controls first)
696
* @return True if the button has been pressed since last time
697
*/
698
public boolean isControlPressed(int button) {
699
return isControlPressed(button, 0);
700
}
701
702
/**
703
* Check if a controller button has been pressed since last
704
* time
705
*
706
* @param controller The index of the controller to check
707
* @param button The button to check for (note that this includes directional controls first)
708
* @return True if the button has been pressed since last time
709
*/
710
public boolean isControlPressed(int button, int controller) {
711
if (controllerPressed[controller][button]) {
712
controllerPressed[controller][button] = false;
713
return true;
714
}
715
716
return false;
717
}
718
719
/**
720
* Clear the state for isControlPressed method. This will reset all
721
* controls to not pressed
722
*/
723
public void clearControlPressedRecord() {
724
for (int i=0;i<controllers.size();i++) {
725
Arrays.fill(controllerPressed[i], false);
726
}
727
}
728
729
/**
730
* Clear the state for the <code>isKeyPressed</code> method. This will
731
* resort in all keys returning that they haven't been pressed, until
732
* they are pressed again
733
*/
734
public void clearKeyPressedRecord() {
735
Arrays.fill(pressed, false);
736
}
737
738
/**
739
* Clear the state for the <code>isMousePressed</code> method. This will
740
* resort in all mouse buttons returning that they haven't been pressed, until
741
* they are pressed again
742
*/
743
public void clearMousePressedRecord() {
744
Arrays.fill(mousePressed, false);
745
}
746
747
/**
748
* Check if a particular key is down
749
*
750
* @param code The key code of the key to check
751
* @return True if the key is down
752
*/
753
public boolean isKeyDown(int code) {
754
return Keyboard.isKeyDown(code);
755
}
756
757
/**
758
* Get the absolute x position of the mouse cursor within the container
759
*
760
* @return The absolute x position of the mouse cursor
761
*/
762
public int getAbsoluteMouseX() {
763
return Mouse.getX();
764
}
765
766
/**
767
* Get the absolute y position of the mouse cursor within the container
768
*
769
* @return The absolute y position of the mouse cursor
770
*/
771
public int getAbsoluteMouseY() {
772
return height - Mouse.getY();
773
}
774
775
/**
776
* Get the x position of the mouse cursor
777
*
778
* @return The x position of the mouse cursor
779
*/
780
public int getMouseX() {
781
return (int) ((Mouse.getX() * scaleX)+xoffset);
782
}
783
784
/**
785
* Get the y position of the mouse cursor
786
*
787
* @return The y position of the mouse cursor
788
*/
789
public int getMouseY() {
790
return (int) (((height-Mouse.getY()) * scaleY)+yoffset);
791
}
792
793
/**
794
* Check if a given mouse button is down
795
*
796
* @param button The index of the button to check (starting at 0)
797
* @return True if the mouse button is down
798
*/
799
public boolean isMouseButtonDown(int button) {
800
return Mouse.isButtonDown(button);
801
}
802
803
/**
804
* Check if any mouse button is down
805
*
806
* @return True if any mouse button is down
807
*/
808
private boolean anyMouseDown() {
809
for (int i=0;i<3;i++) {
810
if (Mouse.isButtonDown(i)) {
811
return true;
812
}
813
}
814
815
return false;
816
}
817
818
/**
819
* Get a count of the number of controlles available
820
*
821
* @return The number of controllers available
822
*/
823
public int getControllerCount() {
824
try {
825
initControllers();
826
} catch (SlickException e) {
827
throw new RuntimeException("Failed to initialise controllers");
828
}
829
830
return controllers.size();
831
}
832
833
/**
834
* Get the number of axis that are avaiable on a given controller
835
*
836
* @param controller The index of the controller to check
837
* @return The number of axis available on the controller
838
*/
839
public int getAxisCount(int controller) {
840
return ((Controller) controllers.get(controller)).getAxisCount();
841
}
842
843
/**
844
* Get the value of the axis with the given index
845
*
846
* @param controller The index of the controller to check
847
* @param axis The index of the axis to read
848
* @return The axis value at time of reading
849
*/
850
public float getAxisValue(int controller, int axis) {
851
return ((Controller) controllers.get(controller)).getAxisValue(axis);
852
}
853
854
/**
855
* Get the name of the axis with the given index
856
*
857
* @param controller The index of the controller to check
858
* @param axis The index of the axis to read
859
* @return The name of the specified axis
860
*/
861
public String getAxisName(int controller, int axis) {
862
return ((Controller) controllers.get(controller)).getAxisName(axis);
863
}
864
865
/**
866
* Check if the controller has the left direction pressed
867
*
868
* @param controller The index of the controller to check
869
* @return True if the controller is pressed to the left
870
*/
871
public boolean isControllerLeft(int controller) {
872
if (controller >= getControllerCount()) {
873
return false;
874
}
875
876
if (controller == ANY_CONTROLLER) {
877
for (int i=0;i<controllers.size();i++) {
878
if (isControllerLeft(i)) {
879
return true;
880
}
881
}
882
883
return false;
884
}
885
886
return ((Controller) controllers.get(controller)).getXAxisValue() < -0.5f
887
|| ((Controller) controllers.get(controller)).getPovX() < -0.5f;
888
}
889
890
/**
891
* Check if the controller has the right direction pressed
892
*
893
* @param controller The index of the controller to check
894
* @return True if the controller is pressed to the right
895
*/
896
public boolean isControllerRight(int controller) {
897
if (controller >= getControllerCount()) {
898
return false;
899
}
900
901
if (controller == ANY_CONTROLLER) {
902
for (int i=0;i<controllers.size();i++) {
903
if (isControllerRight(i)) {
904
return true;
905
}
906
}
907
908
return false;
909
}
910
911
return ((Controller) controllers.get(controller)).getXAxisValue() > 0.5f
912
|| ((Controller) controllers.get(controller)).getPovX() > 0.5f;
913
}
914
915
/**
916
* Check if the controller has the up direction pressed
917
*
918
* @param controller The index of the controller to check
919
* @return True if the controller is pressed to the up
920
*/
921
public boolean isControllerUp(int controller) {
922
if (controller >= getControllerCount()) {
923
return false;
924
}
925
926
if (controller == ANY_CONTROLLER) {
927
for (int i=0;i<controllers.size();i++) {
928
if (isControllerUp(i)) {
929
return true;
930
}
931
}
932
933
return false;
934
}
935
return ((Controller) controllers.get(controller)).getYAxisValue() < -0.5f
936
|| ((Controller) controllers.get(controller)).getPovY() < -0.5f;
937
}
938
939
/**
940
* Check if the controller has the down direction pressed
941
*
942
* @param controller The index of the controller to check
943
* @return True if the controller is pressed to the down
944
*/
945
public boolean isControllerDown(int controller) {
946
if (controller >= getControllerCount()) {
947
return false;
948
}
949
950
if (controller == ANY_CONTROLLER) {
951
for (int i=0;i<controllers.size();i++) {
952
if (isControllerDown(i)) {
953
return true;
954
}
955
}
956
957
return false;
958
}
959
960
return ((Controller) controllers.get(controller)).getYAxisValue() > 0.5f
961
|| ((Controller) controllers.get(controller)).getPovY() > 0.5f;
962
963
}
964
965
/**
966
* Check if controller button is pressed
967
*
968
* @param controller The index of the controller to check
969
* @param index The index of the button to check
970
* @return True if the button is pressed
971
*/
972
public boolean isButtonPressed(int index, int controller) {
973
if (controller >= getControllerCount()) {
974
return false;
975
}
976
977
if (controller == ANY_CONTROLLER) {
978
for (int i=0;i<controllers.size();i++) {
979
if (isButtonPressed(index, i)) {
980
return true;
981
}
982
}
983
984
return false;
985
}
986
987
return ((Controller) controllers.get(controller)).isButtonPressed(index);
988
}
989
990
/**
991
* Check if button 1 is pressed
992
*
993
* @param controller The index of the controller to check
994
* @return True if the button is pressed
995
*/
996
public boolean isButton1Pressed(int controller) {
997
return isButtonPressed(0, controller);
998
}
999
1000
/**
1001
* Check if button 2 is pressed
1002
*
1003
* @param controller The index of the controller to check
1004
* @return True if the button is pressed
1005
*/
1006
public boolean isButton2Pressed(int controller) {
1007
return isButtonPressed(1, controller);
1008
}
1009
1010
/**
1011
* Check if button 3 is pressed
1012
*
1013
* @param controller The index of the controller to check
1014
* @return True if the button is pressed
1015
*/
1016
public boolean isButton3Pressed(int controller) {
1017
return isButtonPressed(2, controller);
1018
}
1019
1020
/**
1021
* Initialise the controllers system
1022
*
1023
* @throws SlickException Indicates a failure to use the hardware
1024
*/
1025
public void initControllers() throws SlickException {
1026
if (controllersInited) {
1027
return;
1028
}
1029
1030
controllersInited = true;
1031
try {
1032
Controllers.create();
1033
int count = Controllers.getControllerCount();
1034
1035
for (int i = 0; i < count; i++) {
1036
Controller controller = Controllers.getController(i);
1037
1038
if ((controller.getButtonCount() >= 3) && (controller.getButtonCount() < MAX_BUTTONS)) {
1039
controllers.add(controller);
1040
}
1041
}
1042
1043
Log.info("Found "+controllers.size()+" controllers");
1044
for (int i=0;i<controllers.size();i++) {
1045
Log.info(i+" : "+((Controller) controllers.get(i)).getName());
1046
}
1047
} catch (LWJGLException e) {
1048
if (e.getCause() instanceof ClassNotFoundException) {
1049
throw new SlickException("Unable to create controller - no jinput found - add jinput.jar to your classpath");
1050
}
1051
throw new SlickException("Unable to create controllers");
1052
} catch (NoClassDefFoundError e) {
1053
// forget it, no jinput availble
1054
}
1055
}
1056
1057
/**
1058
* Notification from an event handle that an event has been consumed
1059
*/
1060
public void consumeEvent() {
1061
consumed = true;
1062
}
1063
1064
/**
1065
* A null stream to clear out those horrid errors
1066
*
1067
* @author kevin
1068
*/
1069
private class NullOutputStream extends OutputStream {
1070
/**
1071
* @see java.io.OutputStream#write(int)
1072
*/
1073
public void write(int b) throws IOException {
1074
// null implemetnation
1075
}
1076
1077
}
1078
1079
/**
1080
* Hook to allow us to translate any key character into special key
1081
* codes for easier use.
1082
*
1083
* @param key The original key code
1084
* @param c The character that was fired
1085
* @return The key code to fire
1086
*/
1087
private int resolveEventKey(int key, char c) {
1088
// BUG with LWJGL - equals comes back with keycode = 0
1089
// See: http://slick.javaunlimited.net/viewtopic.php?t=617
1090
if ((c == 61) || (key == 0)) {
1091
return KEY_EQUALS;
1092
}
1093
1094
return key;
1095
}
1096
1097
/**
1098
* Notification that the mouse has been pressed and hence we
1099
* should consider what we're doing with double clicking
1100
*
1101
* @param button The button pressed/released
1102
* @param x The location of the mouse
1103
* @param y The location of the mouse
1104
*/
1105
public void considerDoubleClick(int button, int x, int y) {
1106
if (doubleClickTimeout == 0) {
1107
clickX = x;
1108
clickY = y;
1109
clickButton = button;
1110
doubleClickTimeout = System.currentTimeMillis() + doubleClickDelay;
1111
fireMouseClicked(button, x, y, 1);
1112
} else {
1113
if (clickButton == button) {
1114
if ((System.currentTimeMillis() < doubleClickTimeout)) {
1115
fireMouseClicked(button, x, y, 2);
1116
doubleClickTimeout = 0;
1117
}
1118
}
1119
}
1120
}
1121
1122
/**
1123
* Poll the state of the input
1124
*
1125
* @param width The width of the game view
1126
* @param height The height of the game view
1127
*/
1128
public void poll(int width, int height) {
1129
if (paused) {
1130
while (Keyboard.next()) {}
1131
while (Mouse.next()) {}
1132
return;
1133
}
1134
1135
// add any listeners requested since last time
1136
for (int i=0;i<keyListenersToAdd.size();i++) {
1137
addKeyListenerImpl((KeyListener) keyListenersToAdd.get(i));
1138
}
1139
keyListenersToAdd.clear();
1140
for (int i=0;i<mouseListenersToAdd.size();i++) {
1141
addMouseListenerImpl((MouseListener) mouseListenersToAdd.get(i));
1142
}
1143
mouseListenersToAdd.clear();
1144
1145
if (doubleClickTimeout != 0) {
1146
if (System.currentTimeMillis() > doubleClickTimeout) {
1147
doubleClickTimeout = 0;
1148
}
1149
}
1150
1151
this.height = height;
1152
1153
Iterator allStarts = allListeners.iterator();
1154
while (allStarts.hasNext()) {
1155
ControlledInputReciever listener = (ControlledInputReciever) allStarts.next();
1156
listener.inputStarted();
1157
}
1158
1159
while (Keyboard.next()) {
1160
if (Keyboard.getEventKeyState()) {
1161
int eventKey = resolveEventKey(Keyboard.getEventKey(), Keyboard.getEventCharacter());
1162
1163
keys[eventKey] = Keyboard.getEventCharacter();
1164
pressed[eventKey] = true;
1165
nextRepeat[eventKey] = System.currentTimeMillis() + keyRepeatInitial;
1166
1167
consumed = false;
1168
for (int i=0;i<keyListeners.size();i++) {
1169
KeyListener listener = (KeyListener) keyListeners.get(i);
1170
1171
if (listener.isAcceptingInput()) {
1172
listener.keyPressed(eventKey, Keyboard.getEventCharacter());
1173
if (consumed) {
1174
break;
1175
}
1176
}
1177
}
1178
} else {
1179
int eventKey = resolveEventKey(Keyboard.getEventKey(), Keyboard.getEventCharacter());
1180
nextRepeat[eventKey] = 0;
1181
1182
consumed = false;
1183
for (int i=0;i<keyListeners.size();i++) {
1184
KeyListener listener = (KeyListener) keyListeners.get(i);
1185
if (listener.isAcceptingInput()) {
1186
listener.keyReleased(eventKey, keys[eventKey]);
1187
if (consumed) {
1188
break;
1189
}
1190
}
1191
}
1192
}
1193
}
1194
1195
while (Mouse.next()) {
1196
if (Mouse.getEventButton() >= 0) {
1197
if (Mouse.getEventButtonState()) {
1198
consumed = false;
1199
mousePressed[Mouse.getEventButton()] = true;
1200
1201
pressedX = (int) (xoffset + (Mouse.getEventX() * scaleX));
1202
pressedY = (int) (yoffset + ((height-Mouse.getEventY()) * scaleY));
1203
1204
for (int i=0;i<mouseListeners.size();i++) {
1205
MouseListener listener = (MouseListener) mouseListeners.get(i);
1206
if (listener.isAcceptingInput()) {
1207
listener.mousePressed(Mouse.getEventButton(), pressedX, pressedY);
1208
if (consumed) {
1209
break;
1210
}
1211
}
1212
}
1213
} else {
1214
consumed = false;
1215
mousePressed[Mouse.getEventButton()] = false;
1216
1217
int releasedX = (int) (xoffset + (Mouse.getEventX() * scaleX));
1218
int releasedY = (int) (yoffset + ((height-Mouse.getEventY()) * scaleY));
1219
if ((pressedX != -1) &&
1220
(pressedY != -1) &&
1221
(Math.abs(pressedX - releasedX) < mouseClickTolerance) &&
1222
(Math.abs(pressedY - releasedY) < mouseClickTolerance)) {
1223
considerDoubleClick(Mouse.getEventButton(), releasedX, releasedY);
1224
pressedX = pressedY = -1;
1225
}
1226
1227
for (int i=0;i<mouseListeners.size();i++) {
1228
MouseListener listener = (MouseListener) mouseListeners.get(i);
1229
if (listener.isAcceptingInput()) {
1230
listener.mouseReleased(Mouse.getEventButton(), releasedX, releasedY);
1231
if (consumed) {
1232
break;
1233
}
1234
}
1235
}
1236
}
1237
} else {
1238
if (Mouse.isGrabbed()) {
1239
if ((Mouse.getEventDX() != 0) || (Mouse.getEventDY() != 0)) {
1240
consumed = false;
1241
for (int i=0;i<mouseListeners.size();i++) {
1242
MouseListener listener = (MouseListener) mouseListeners.get(i);
1243
if (listener.isAcceptingInput()) {
1244
if (anyMouseDown()) {
1245
listener.mouseDragged(0, 0, Mouse.getEventDX(), -Mouse.getEventDY());
1246
} else {
1247
listener.mouseMoved(0, 0, Mouse.getEventDX(), -Mouse.getEventDY());
1248
}
1249
1250
if (consumed) {
1251
break;
1252
}
1253
}
1254
}
1255
}
1256
}
1257
1258
int dwheel = Mouse.getEventDWheel();
1259
wheel += dwheel;
1260
if (dwheel != 0) {
1261
consumed = false;
1262
for (int i=0;i<mouseListeners.size();i++) {
1263
MouseListener listener = (MouseListener) mouseListeners.get(i);
1264
if (listener.isAcceptingInput()) {
1265
listener.mouseWheelMoved(dwheel);
1266
if (consumed) {
1267
break;
1268
}
1269
}
1270
}
1271
}
1272
}
1273
}
1274
1275
if (!displayActive) {
1276
lastMouseX = getMouseX();
1277
lastMouseY = getMouseY();
1278
} else {
1279
if ((lastMouseX != getMouseX()) || (lastMouseY != getMouseY())) {
1280
consumed = false;
1281
for (int i=0;i<mouseListeners.size();i++) {
1282
MouseListener listener = (MouseListener) mouseListeners.get(i);
1283
if (listener.isAcceptingInput()) {
1284
if (anyMouseDown()) {
1285
listener.mouseDragged(lastMouseX , lastMouseY, getMouseX(), getMouseY());
1286
} else {
1287
listener.mouseMoved(lastMouseX , lastMouseY, getMouseX(), getMouseY());
1288
}
1289
if (consumed) {
1290
break;
1291
}
1292
}
1293
}
1294
lastMouseX = getMouseX();
1295
lastMouseY = getMouseY();
1296
}
1297
}
1298
1299
if (controllersInited) {
1300
for (int i=0;i<getControllerCount();i++) {
1301
int count = ((Controller) controllers.get(i)).getButtonCount()+3;
1302
count = Math.min(count, 24);
1303
for (int c=0;c<=count;c++) {
1304
if (controls[i][c] && !isControlDwn(c, i)) {
1305
controls[i][c] = false;
1306
fireControlRelease(c, i);
1307
} else if (!controls[i][c] && isControlDwn(c, i)) {
1308
controllerPressed[i][c] = true;
1309
controls[i][c] = true;
1310
fireControlPress(c, i);
1311
}
1312
}
1313
}
1314
}
1315
1316
if (keyRepeat) {
1317
for (int i=0;i<1024;i++) {
1318
if (pressed[i] && (nextRepeat[i] != 0)) {
1319
if (System.currentTimeMillis() > nextRepeat[i]) {
1320
nextRepeat[i] = System.currentTimeMillis() + keyRepeatInterval;
1321
consumed = false;
1322
for (int j=0;j<keyListeners.size();j++) {
1323
KeyListener listener = (KeyListener) keyListeners.get(j);
1324
1325
if (listener.isAcceptingInput()) {
1326
listener.keyPressed(i, keys[i]);
1327
if (consumed) {
1328
break;
1329
}
1330
}
1331
}
1332
}
1333
}
1334
}
1335
}
1336
1337
1338
Iterator all = allListeners.iterator();
1339
while (all.hasNext()) {
1340
ControlledInputReciever listener = (ControlledInputReciever) all.next();
1341
listener.inputEnded();
1342
}
1343
1344
if (Display.isCreated()) {
1345
displayActive = Display.isActive();
1346
}
1347
}
1348
1349
/**
1350
* Enable key repeat for this input context. This will cause keyPressed to get called repeatedly
1351
* at a set interval while the key is pressed
1352
*
1353
* @param initial The interval before key repreating starts after a key press
1354
* @param interval The interval between key repeats in ms
1355
* @deprecated
1356
*/
1357
public void enableKeyRepeat(int initial, int interval) {
1358
Keyboard.enableRepeatEvents(true);
1359
}
1360
1361
/**
1362
* Enable key repeat for this input context. Uses the system settings for repeat
1363
* interval configuration.
1364
*/
1365
public void enableKeyRepeat() {
1366
Keyboard.enableRepeatEvents(true);
1367
}
1368
1369
/**
1370
* Disable key repeat for this input context
1371
*/
1372
public void disableKeyRepeat() {
1373
Keyboard.enableRepeatEvents(false);
1374
}
1375
1376
/**
1377
* Check if key repeat is enabled
1378
*
1379
* @return True if key repeat is enabled
1380
*/
1381
public boolean isKeyRepeatEnabled() {
1382
return Keyboard.areRepeatEventsEnabled();
1383
}
1384
1385
/**
1386
* Fire an event indicating that a control has been pressed
1387
*
1388
* @param index The index of the control pressed
1389
* @param controllerIndex The index of the controller on which the control was pressed
1390
*/
1391
private void fireControlPress(int index, int controllerIndex) {
1392
consumed = false;
1393
for (int i=0;i<controllerListeners.size();i++) {
1394
ControllerListener listener = (ControllerListener) controllerListeners.get(i);
1395
if (listener.isAcceptingInput()) {
1396
switch (index) {
1397
case LEFT:
1398
listener.controllerLeftPressed(controllerIndex);
1399
break;
1400
case RIGHT:
1401
listener.controllerRightPressed(controllerIndex);
1402
break;
1403
case UP:
1404
listener.controllerUpPressed(controllerIndex);
1405
break;
1406
case DOWN:
1407
listener.controllerDownPressed(controllerIndex);
1408
break;
1409
default:
1410
// assume button pressed
1411
listener.controllerButtonPressed(controllerIndex, (index - BUTTON1) + 1);
1412
break;
1413
}
1414
if (consumed) {
1415
break;
1416
}
1417
}
1418
}
1419
}
1420
1421
/**
1422
* Fire an event indicating that a control has been released
1423
*
1424
* @param index The index of the control released
1425
* @param controllerIndex The index of the controller on which the control was released
1426
*/
1427
private void fireControlRelease(int index, int controllerIndex) {
1428
consumed = false;
1429
for (int i=0;i<controllerListeners.size();i++) {
1430
ControllerListener listener = (ControllerListener) controllerListeners.get(i);
1431
if (listener.isAcceptingInput()) {
1432
switch (index) {
1433
case LEFT:
1434
listener.controllerLeftReleased(controllerIndex);
1435
break;
1436
case RIGHT:
1437
listener.controllerRightReleased(controllerIndex);
1438
break;
1439
case UP:
1440
listener.controllerUpReleased(controllerIndex);
1441
break;
1442
case DOWN:
1443
listener.controllerDownReleased(controllerIndex);
1444
break;
1445
default:
1446
// assume button release
1447
listener.controllerButtonReleased(controllerIndex, (index - BUTTON1) + 1);
1448
break;
1449
}
1450
if (consumed) {
1451
break;
1452
}
1453
}
1454
}
1455
}
1456
1457
/**
1458
* Check if a particular control is currently pressed
1459
*
1460
* @param index The index of the control
1461
* @param controllerIndex The index of the control to which the control belongs
1462
* @return True if the control is pressed
1463
*/
1464
private boolean isControlDwn(int index, int controllerIndex) {
1465
switch (index) {
1466
case LEFT:
1467
return isControllerLeft(controllerIndex);
1468
case RIGHT:
1469
return isControllerRight(controllerIndex);
1470
case UP:
1471
return isControllerUp(controllerIndex);
1472
case DOWN:
1473
return isControllerDown(controllerIndex);
1474
}
1475
1476
if (index >= BUTTON1) {
1477
return isButtonPressed((index-BUTTON1), controllerIndex);
1478
}
1479
1480
throw new RuntimeException("Unknown control index");
1481
}
1482
1483
1484
/**
1485
* Pauses the polling and sending of input events.
1486
*/
1487
public void pause() {
1488
paused = true;
1489
1490
// Reset all polling arrays
1491
clearKeyPressedRecord();
1492
clearMousePressedRecord();
1493
clearControlPressedRecord();
1494
}
1495
1496
/**
1497
* Resumes the polling and sending of input events.
1498
*/
1499
public void resume() {
1500
paused = false;
1501
}
1502
1503
/**
1504
* Notify listeners that the mouse button has been clicked
1505
*
1506
* @param button The button that has been clicked
1507
* @param x The location at which the button was clicked
1508
* @param y The location at which the button was clicked
1509
* @param clickCount The number of times the button was clicked (single or double click)
1510
*/
1511
private void fireMouseClicked(int button, int x, int y, int clickCount) {
1512
consumed = false;
1513
for (int i=0;i<mouseListeners.size();i++) {
1514
MouseListener listener = (MouseListener) mouseListeners.get(i);
1515
if (listener.isAcceptingInput()) {
1516
listener.mouseClicked(button, x, y, clickCount);
1517
if (consumed) {
1518
break;
1519
}
1520
}
1521
}
1522
}
1523
}
1524
1525