/
DefaultGwtInput.java
1125 lines (1027 loc) · 31.4 KB
/
DefaultGwtInput.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.backends.gwt;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.backends.gwt.widgets.TextInputDialogBox;
import com.badlogic.gdx.backends.gwt.widgets.TextInputDialogBox.TextInputDialogListener;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.IntSet;
import com.badlogic.gdx.utils.IntSet.IntSetIterator;
import com.badlogic.gdx.utils.TimeUtils;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.CanvasElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.Touch;
import com.google.gwt.event.dom.client.KeyCodes;
public class DefaultGwtInput implements GwtInput {
static final int MAX_TOUCHES = 20;
boolean justTouched = false;
private IntMap<Integer> touchMap = new IntMap<Integer>(20);
private boolean[] touched = new boolean[MAX_TOUCHES];
private int[] touchX = new int[MAX_TOUCHES];
private int[] touchY = new int[MAX_TOUCHES];
private int[] deltaX = new int[MAX_TOUCHES];
private int[] deltaY = new int[MAX_TOUCHES];
IntSet pressedButtons = new IntSet();
int pressedKeyCount = 0;
IntSet pressedKeySet = new IntSet();
boolean[] pressedKeys = new boolean[Keys.MAX_KEYCODE + 1];
boolean keyJustPressed = false;
boolean[] justPressedKeys = new boolean[Keys.MAX_KEYCODE + 1];
boolean[] justPressedButtons = new boolean[5];
InputProcessor processor;
long currentEventTimeStamp;
final CanvasElement canvas;
final GwtApplicationConfiguration config;
boolean hasFocus = true;
GwtAccelerometer accelerometer;
GwtGyroscope gyroscope;
public DefaultGwtInput (CanvasElement canvas, GwtApplicationConfiguration config) {
this.canvas = canvas;
this.config = config;
if (config.useAccelerometer && GwtFeaturePolicy.allowsFeature(GwtAccelerometer.PERMISSION)) {
if (GwtApplication.agentInfo().isFirefox()) {
setupAccelerometer();
} else {
GwtPermissions.queryPermission(GwtAccelerometer.PERMISSION, new GwtPermissions.GwtPermissionResult() {
@Override
public void granted() {
setupAccelerometer();
}
@Override
public void denied() {
}
@Override
public void prompt() {
setupAccelerometer();
}
});
}
}
if (config.useGyroscope) {
if (GwtApplication.agentInfo().isFirefox()) {
setupGyroscope();
} else {
GwtPermissions.queryPermission(GwtGyroscope.PERMISSION, new GwtPermissions.GwtPermissionResult() {
@Override
public void granted() {
setupGyroscope();
}
@Override
public void denied() {
}
@Override
public void prompt() {
setupGyroscope();
}
});
}
}
hookEvents();
}
@Override
public void reset () {
if (justTouched) {
justTouched = false;
for (int i = 0; i < justPressedButtons.length; i++) {
justPressedButtons[i] = false;
}
}
if (keyJustPressed) {
keyJustPressed = false;
for (int i = 0; i < justPressedKeys.length; i++) {
justPressedKeys[i] = false;
}
}
}
void setupAccelerometer () {
if (GwtAccelerometer.isSupported() && GwtFeaturePolicy.allowsFeature(GwtAccelerometer.PERMISSION)) {
if (accelerometer == null) accelerometer = GwtAccelerometer.getInstance();
if (!accelerometer.activated()) accelerometer.start();
}
}
void setupGyroscope () {
if (GwtGyroscope.isSupported() && GwtFeaturePolicy.allowsFeature(GwtGyroscope.PERMISSION)) {
if (gyroscope == null) gyroscope = GwtGyroscope.getInstance();
if (!gyroscope.activated()) gyroscope.start();
}
}
@Override
public float getAccelerometerX () {
return this.accelerometer != null ? (float) this.accelerometer.x() : 0;
}
@Override
public float getAccelerometerY () {
return this.accelerometer != null ? (float) this.accelerometer.y() : 0;
}
@Override
public float getAccelerometerZ () {
return this.accelerometer != null ? (float) this.accelerometer.z() : 0;
}
private boolean isAccelerometerPresent() {
return getAccelerometerX() != 0 || getAccelerometerY() != 0 || getAccelerometerZ() != 0;
}
@Override
public float getGyroscopeX () {
return this.gyroscope != null ? (float) this.gyroscope.x() : 0;
}
@Override
public float getGyroscopeY () {
return this.gyroscope != null ? (float) this.gyroscope.y() : 0;
}
@Override
public float getGyroscopeZ () {
return this.gyroscope != null ? (float) this.gyroscope.z() : 0;
}
private boolean isGyroscopePresent() {
return getGyroscopeX() != 0 || getGyroscopeY() != 0 || getGyroscopeZ() != 0;
}
@Override
public int getMaxPointers () {
return MAX_TOUCHES;
}
@Override
public int getX () {
return touchX[0];
}
@Override
public int getX (int pointer) {
return touchX[pointer];
}
@Override
public int getDeltaX () {
return deltaX[0];
}
@Override
public int getDeltaX (int pointer) {
return deltaX[pointer];
}
@Override
public int getY () {
return touchY[0];
}
@Override
public int getY (int pointer) {
return touchY[pointer];
}
@Override
public int getDeltaY () {
return deltaY[0];
}
@Override
public int getDeltaY (int pointer) {
return deltaY[pointer];
}
@Override
public boolean isTouched () {
for (int pointer = 0; pointer < MAX_TOUCHES; pointer++) {
if (touched[pointer]) {
return true;
}
}
return false;
}
@Override
public boolean justTouched () {
return justTouched;
}
@Override
public boolean isTouched (int pointer) {
return touched[pointer];
}
@Override
public boolean isButtonPressed (int button) {
return pressedButtons.contains(button) && touched[0];
}
@Override
public boolean isButtonJustPressed(int button) {
if(button < 0 || button >= justPressedButtons.length) return false;
return justPressedButtons[button];
}
@Override
public float getPressure () {
return getPressure(0);
}
@Override
public float getPressure (int pointer) {
return isTouched(pointer) ? 1 : 0;
}
@Override
public boolean isKeyPressed (int key) {
if (key == Keys.ANY_KEY) {
return pressedKeyCount > 0;
}
if (key < 0 || key > 255) {
return false;
}
return pressedKeys[key];
}
@Override
public boolean isKeyJustPressed (int key) {
if (key == Keys.ANY_KEY) {
return keyJustPressed;
}
if (key < 0 || key > 255) {
return false;
}
return justPressedKeys[key];
}
public void getTextInput (TextInputListener listener, String title, String text, String hint) {
TextInputDialogBox dialog = new TextInputDialogBox(title, text, hint);
final TextInputListener capturedListener = listener;
dialog.setListener(new TextInputDialogListener() {
@Override
public void onPositive (String text) {
if (capturedListener != null) {
capturedListener.input(text);
}
}
@Override
public void onNegative () {
if (capturedListener != null) {
capturedListener.canceled();
}
}
});
}
@Override
public void setOnscreenKeyboardVisible (boolean visible) {
}
@Override
public void vibrate (int milliseconds) {
}
@Override
public void vibrate (long[] pattern, int repeat) {
}
@Override
public void cancelVibrate () {
}
@Override
public float getAzimuth () {
return 0;
}
@Override
public float getPitch () {
return 0;
}
@Override
public float getRoll () {
return 0;
}
@Override
public void getRotationMatrix (float[] matrix) {
}
@Override
public long getCurrentEventTime () {
return currentEventTimeStamp;
}
@Override
public void setCatchBackKey (boolean catchBack) {
}
@Override
public boolean isCatchBackKey () {
return false;
}
@Override
public void setCatchMenuKey (boolean catchMenu) {
}
@Override
public boolean isCatchMenuKey () {
return false;
}
@Override
public void setCatchKey (int keycode, boolean catchKey) {
}
@Override
public boolean isCatchKey (int keycode) {
return false;
}
@Override
public void setInputProcessor (InputProcessor processor) {
this.processor = processor;
}
@Override
public InputProcessor getInputProcessor () {
return processor;
}
@Override
public boolean isPeripheralAvailable (Peripheral peripheral) {
if (peripheral == Peripheral.Gyroscope) return GwtGyroscope.isSupported() && isGyroscopePresent()
&& GwtFeaturePolicy.allowsFeature(GwtGyroscope.PERMISSION);
if (peripheral == Peripheral.Accelerometer) return GwtAccelerometer.isSupported() && isAccelerometerPresent()
&& GwtFeaturePolicy.allowsFeature(GwtAccelerometer.PERMISSION);
if (peripheral == Peripheral.Compass) return false;
if (peripheral == Peripheral.HardwareKeyboard) return !GwtApplication.isMobileDevice();
if (peripheral == Peripheral.MultitouchScreen) return isTouchScreen();
if (peripheral == Peripheral.OnscreenKeyboard) return GwtApplication.isMobileDevice();
if (peripheral == Peripheral.Vibrator) return false;
return false;
}
@Override
public int getRotation () {
return 0;
}
@Override
public Orientation getNativeOrientation () {
return Orientation.Landscape;
}
/** from https://github.com/toji/game-shim/blob/master/game-shim.js
* @return is Cursor catched */
private native boolean isCursorCatchedJSNI (CanvasElement canvas) /*-{
if (!navigator.pointer) {
navigator.pointer = navigator.pointer || navigator.webkitPointer || navigator.mozPointer;
}
if (navigator.pointer) {
if (typeof (navigator.pointer.isLocked) === "boolean") {
// Chrome initially launched with this interface
return navigator.pointer.isLocked;
} else if (typeof (navigator.pointer.isLocked) === "function") {
// Some older builds might provide isLocked as a function
return navigator.pointer.isLocked();
} else if (typeof (navigator.pointer.islocked) === "function") {
// For compatibility with early Firefox build
return navigator.pointer.islocked();
}
}
if ($doc.pointerLockElement === canvas || $doc.mozPointerLockElement === canvas) {
return true;
}
return false;
}-*/;
/** from https://github.com/toji/game-shim/blob/master/game-shim.js
* @param element Canvas */
private native void setCursorCatchedJSNI (CanvasElement element) /*-{
// Navigator pointer is not the right interface according to spec.
// Here for backwards compatibility only
if (!navigator.pointer) {
navigator.pointer = navigator.pointer || navigator.webkitPointer || navigator.mozPointer;
}
// element.requestPointerLock
if (!element.requestPointerLock) {
element.requestPointerLock = (function() {
return element.webkitRequestPointerLock
|| element.mozRequestPointerLock || function() {
if (navigator.pointer) {
navigator.pointer.lock(element);
}
};
})();
}
element.requestPointerLock();
}-*/;
/** from https://github.com/toji/game-shim/blob/master/game-shim.js */
private native void exitCursorCatchedJSNI () /*-{
if (!$doc.exitPointerLock) {
$doc.exitPointerLock = (function() {
return $doc.webkitExitPointerLock || $doc.mozExitPointerLock
|| function() {
if (navigator.pointer) {
var elem = this;
navigator.pointer.unlock();
}
};
})();
}
}-*/;
/** from https://github.com/toji/game-shim/blob/master/game-shim.js
* @param event JavaScript Mouse Event
* @return movement in x direction */
private native float getMovementXJSNI (NativeEvent event) /*-{
return event.movementX || event.webkitMovementX || 0;
}-*/;
/** from https://github.com/toji/game-shim/blob/master/game-shim.js
* @param event JavaScript Mouse Event
* @return movement in y direction */
private native float getMovementYJSNI (NativeEvent event) /*-{
return event.movementY || event.webkitMovementY || 0;
}-*/;
private static native boolean isTouchScreen () /*-{
return (('ontouchstart' in window) || (navigator.msMaxTouchPoints > 0));
}-*/;
/** works only for Chrome > Version 18 with enabled Mouse Lock enable in about:flags or start Chrome with the
* --enable-pointer-lock flag */
@Override
public void setCursorCatched (boolean catched) {
if (catched)
setCursorCatchedJSNI(canvas);
else
exitCursorCatchedJSNI();
}
@Override
public boolean isCursorCatched () {
return isCursorCatchedJSNI(canvas);
}
@Override
public void setCursorPosition (int x, int y) {
// FIXME??
}
// kindly borrowed from our dear playn friends...
static native void addEventListener (JavaScriptObject target, String name, DefaultGwtInput handler, boolean capture) /*-{
target
.addEventListener(
name,
function(e) {
handler.@com.badlogic.gdx.backends.gwt.DefaultGwtInput::handleEvent(Lcom/google/gwt/dom/client/NativeEvent;)(e);
}, capture);
}-*/;
private static native float getMouseWheelVelocity (NativeEvent evt) /*-{
var delta = 0.0;
var agentInfo = @com.badlogic.gdx.backends.gwt.GwtApplication::agentInfo()();
if (agentInfo.isFirefox) {
if (agentInfo.isMacOS) {
delta = 1.0 * evt.detail;
} else {
delta = 1.0 * evt.detail / 3;
}
} else if (agentInfo.isOpera) {
if (agentInfo.isLinux) {
delta = -1.0 * evt.wheelDelta / 80;
} else {
// on mac
delta = -1.0 * evt.wheelDelta / 40;
}
} else if (agentInfo.isChrome || agentInfo.isSafari || agentInfo.isIE) {
delta = -1.0 * evt.wheelDelta / 120;
// handle touchpad for chrome
if (Math.abs(delta) < 1) {
if (agentInfo.isWindows) {
delta = -1.0 * evt.wheelDelta;
} else if (agentInfo.isMacOS) {
delta = -1.0 * evt.wheelDelta / 3;
}
}
}
return delta;
}-*/;
/** Kindly borrowed from PlayN. **/
protected static native String getMouseWheelEvent () /*-{
if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
return "DOMMouseScroll";
} else {
return "mousewheel";
}
}-*/;
/** Kindly borrowed from PlayN. **/
protected int getRelativeX (NativeEvent e, CanvasElement target) {
float xScaleRatio = target.getWidth() * 1f / target.getClientWidth(); // Correct for canvas CSS scaling
return Math.round(xScaleRatio
* (e.getClientX() - target.getAbsoluteLeft() + target.getScrollLeft() + target.getOwnerDocument().getScrollLeft()));
}
/** Kindly borrowed from PlayN. **/
protected int getRelativeY (NativeEvent e, CanvasElement target) {
float yScaleRatio = target.getHeight() * 1f / target.getClientHeight(); // Correct for canvas CSS scaling
return Math.round(yScaleRatio
* (e.getClientY() - target.getAbsoluteTop() + target.getScrollTop() + target.getOwnerDocument().getScrollTop()));
}
protected int getRelativeX (Touch touch, CanvasElement target) {
float xScaleRatio = target.getWidth() * 1f / target.getClientWidth(); // Correct for canvas CSS scaling
return Math.round(xScaleRatio * touch.getRelativeX(target));
}
protected int getRelativeY (Touch touch, CanvasElement target) {
float yScaleRatio = target.getHeight() * 1f / target.getClientHeight(); // Correct for canvas CSS scaling
return Math.round(yScaleRatio * touch.getRelativeY(target));
}
private static native JavaScriptObject getWindow () /*-{
return $wnd;
}-*/;
private void hookEvents () {
addEventListener(canvas, "mousedown", this, true);
addEventListener(Document.get(), "mousedown", this, true);
addEventListener(canvas, "mouseup", this, true);
addEventListener(Document.get(), "mouseup", this, true);
addEventListener(canvas, "mousemove", this, true);
addEventListener(Document.get(), "mousemove", this, true);
addEventListener(canvas, getMouseWheelEvent(), this, true);
addEventListener(Document.get(), "keydown", this, false);
addEventListener(Document.get(), "keyup", this, false);
addEventListener(Document.get(), "keypress", this, false);
addEventListener(getWindow(), "blur", this, false);
addEventListener(canvas, "touchstart", this, true);
addEventListener(canvas, "touchmove", this, true);
addEventListener(canvas, "touchcancel", this, true);
addEventListener(canvas, "touchend", this, true);
}
private int getButton (int button) {
if (button == NativeEvent.BUTTON_LEFT) return Buttons.LEFT;
if (button == NativeEvent.BUTTON_RIGHT) return Buttons.RIGHT;
if (button == NativeEvent.BUTTON_MIDDLE) return Buttons.MIDDLE;
return Buttons.LEFT;
}
private void handleEvent (NativeEvent e) {
if (e.getType().equals("mousedown")) {
if (!e.getEventTarget().equals(canvas) || pressedButtons.contains(getButton(e.getButton()))) {
float mouseX = getRelativeX(e, canvas);
float mouseY = getRelativeY(e, canvas);
if (mouseX < 0 || mouseX > Gdx.graphics.getWidth() || mouseY < 0 || mouseY > Gdx.graphics.getHeight()) {
hasFocus = false;
}
return;
}
hasFocus = true;
this.justTouched = true;
this.touched[0] = true;
final int button = getButton(e.getButton());
this.pressedButtons.add(button);
justPressedButtons[button] = true;
this.deltaX[0] = 0;
this.deltaY[0] = 0;
if (isCursorCatched()) {
this.touchX[0] += getMovementXJSNI(e);
this.touchY[0] += getMovementYJSNI(e);
} else {
this.touchX[0] = getRelativeX(e, canvas);
this.touchY[0] = getRelativeY(e, canvas);
}
this.currentEventTimeStamp = TimeUtils.nanoTime();
if (processor != null) processor.touchDown(touchX[0], touchY[0], 0, getButton(e.getButton()));
}
if (e.getType().equals("mousemove")) {
if (isCursorCatched()) {
this.deltaX[0] = (int)getMovementXJSNI(e);
this.deltaY[0] = (int)getMovementYJSNI(e);
this.touchX[0] += getMovementXJSNI(e);
this.touchY[0] += getMovementYJSNI(e);
} else {
this.deltaX[0] = getRelativeX(e, canvas) - touchX[0];
this.deltaY[0] = getRelativeY(e, canvas) - touchY[0];
this.touchX[0] = getRelativeX(e, canvas);
this.touchY[0] = getRelativeY(e, canvas);
}
this.currentEventTimeStamp = TimeUtils.nanoTime();
if (processor != null) {
if (touched[0])
processor.touchDragged(touchX[0], touchY[0], 0);
else
processor.mouseMoved(touchX[0], touchY[0]);
}
}
if (e.getType().equals("mouseup")) {
if (!pressedButtons.contains(getButton(e.getButton()))) return;
this.pressedButtons.remove(getButton(e.getButton()));
this.touched[0] = pressedButtons.size > 0;
if (isCursorCatched()) {
this.deltaX[0] = (int)getMovementXJSNI(e);
this.deltaY[0] = (int)getMovementYJSNI(e);
this.touchX[0] += getMovementXJSNI(e);
this.touchY[0] += getMovementYJSNI(e);
} else {
this.deltaX[0] = getRelativeX(e, canvas) - touchX[0];
this.deltaY[0] = getRelativeY(e, canvas) - touchY[0];
this.touchX[0] = getRelativeX(e, canvas);
this.touchY[0] = getRelativeY(e, canvas);
}
this.currentEventTimeStamp = TimeUtils.nanoTime();
this.touched[0] = false;
if (processor != null) processor.touchUp(touchX[0], touchY[0], 0, getButton(e.getButton()));
}
if (e.getType().equals(getMouseWheelEvent())) {
if (processor != null) {
processor.scrolled(0, (int)getMouseWheelVelocity(e));
}
this.currentEventTimeStamp = TimeUtils.nanoTime();
e.preventDefault();
}
if (hasFocus && !e.getType().equals("blur")) {
if (e.getType().equals("keydown")) {
// Gdx.app.log("DefaultGwtInput", "keydown");
int code = keyForCode(e.getKeyCode());
if (code == 67) {
e.preventDefault();
if (processor != null) {
processor.keyDown(code);
processor.keyTyped('\b');
}
} else {
if (!pressedKeys[code]) {
pressedKeySet.add(code);
pressedKeyCount++;
pressedKeys[code] = true;
keyJustPressed = true;
justPressedKeys[code] = true;
if (processor != null) {
processor.keyDown(code);
}
}
}
}
if (e.getType().equals("keypress")) {
// Gdx.app.log("DefaultGwtInput", "keypress");
char c = (char)e.getCharCode();
if (processor != null) processor.keyTyped(c);
}
if (e.getType().equals("keyup")) {
// Gdx.app.log("DefaultGwtInput", "keyup");
int code = keyForCode(e.getKeyCode());
if (pressedKeys[code]) {
pressedKeySet.remove(code);
pressedKeyCount--;
pressedKeys[code] = false;
}
if (processor != null) {
processor.keyUp(code);
}
}
}
else if (pressedKeyCount > 0) {
// Gdx.app.log("DefaultGwtInput", "unfocused");
IntSetIterator iterator = pressedKeySet.iterator();
while (iterator.hasNext) {
int code = iterator.next();
if (pressedKeys[code]) {
pressedKeySet.remove(code);
pressedKeyCount--;
pressedKeys[code] = false;
}
if (processor != null) {
processor.keyUp(code);
}
}
}
if (e.getType().equals("touchstart")) {
this.justTouched = true;
JsArray<Touch> touches = e.getChangedTouches();
for (int i = 0, j = touches.length(); i < j; i++) {
Touch touch = touches.get(i);
int real = touch.getIdentifier();
int touchId;
touchMap.put(real, touchId = getAvailablePointer());
touched[touchId] = true;
touchX[touchId] = getRelativeX(touch, canvas);
touchY[touchId] = getRelativeY(touch, canvas);
deltaX[touchId] = 0;
deltaY[touchId] = 0;
if (processor != null) {
processor.touchDown(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT);
}
}
this.currentEventTimeStamp = TimeUtils.nanoTime();
e.preventDefault();
}
if (e.getType().equals("touchmove")) {
JsArray<Touch> touches = e.getChangedTouches();
for (int i = 0, j = touches.length(); i < j; i++) {
Touch touch = touches.get(i);
int real = touch.getIdentifier();
int touchId = touchMap.get(real);
deltaX[touchId] = getRelativeX(touch, canvas) - touchX[touchId];
deltaY[touchId] = getRelativeY(touch, canvas) - touchY[touchId];
touchX[touchId] = getRelativeX(touch, canvas);
touchY[touchId] = getRelativeY(touch, canvas);
if (processor != null) {
processor.touchDragged(touchX[touchId], touchY[touchId], touchId);
}
}
this.currentEventTimeStamp = TimeUtils.nanoTime();
e.preventDefault();
}
if (e.getType().equals("touchcancel")) {
JsArray<Touch> touches = e.getChangedTouches();
for (int i = 0, j = touches.length(); i < j; i++) {
Touch touch = touches.get(i);
int real = touch.getIdentifier();
int touchId = touchMap.get(real);
touchMap.remove(real);
touched[touchId] = false;
deltaX[touchId] = getRelativeX(touch, canvas) - touchX[touchId];
deltaY[touchId] = getRelativeY(touch, canvas) - touchY[touchId];
touchX[touchId] = getRelativeX(touch, canvas);
touchY[touchId] = getRelativeY(touch, canvas);
if (processor != null) {
processor.touchUp(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT);
}
}
this.currentEventTimeStamp = TimeUtils.nanoTime();
e.preventDefault();
}
if (e.getType().equals("touchend")) {
JsArray<Touch> touches = e.getChangedTouches();
for (int i = 0, j = touches.length(); i < j; i++) {
Touch touch = touches.get(i);
int real = touch.getIdentifier();
int touchId = touchMap.get(real);
touchMap.remove(real);
touched[touchId] = false;
deltaX[touchId] = getRelativeX(touch, canvas) - touchX[touchId];
deltaY[touchId] = getRelativeY(touch, canvas) - touchY[touchId];
touchX[touchId] = getRelativeX(touch, canvas);
touchY[touchId] = getRelativeY(touch, canvas);
if (processor != null) {
processor.touchUp(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT);
}
}
this.currentEventTimeStamp = TimeUtils.nanoTime();
e.preventDefault();
}
// if(hasFocus) e.preventDefault();
}
private int getAvailablePointer () {
for (int i = 0; i < MAX_TOUCHES; i++) {
if (!touchMap.containsValue(i, false)) return i;
}
return -1;
}
/** borrowed from PlayN, thanks guys **/
private static int keyForCode (int keyCode) {
switch (keyCode) {
case KeyCodes.KEY_ALT:
return Keys.ALT_LEFT;
case KeyCodes.KEY_BACKSPACE:
return Keys.BACKSPACE;
case KeyCodes.KEY_CTRL:
return Keys.CONTROL_LEFT;
case KeyCodes.KEY_DELETE:
return Keys.DEL;
case KeyCodes.KEY_DOWN:
return Keys.DOWN;
case KeyCodes.KEY_END:
return Keys.END;
case KeyCodes.KEY_ENTER:
return Keys.ENTER;
case KeyCodes.KEY_ESCAPE:
return Keys.ESCAPE;
case KeyCodes.KEY_HOME:
return Keys.HOME;
case KeyCodes.KEY_LEFT:
return Keys.LEFT;
case KeyCodes.KEY_PAGEDOWN:
return Keys.PAGE_DOWN;
case KeyCodes.KEY_PAGEUP:
return Keys.PAGE_UP;
case KeyCodes.KEY_RIGHT:
return Keys.RIGHT;
case KeyCodes.KEY_SHIFT:
return Keys.SHIFT_LEFT;
case KeyCodes.KEY_TAB:
return Keys.TAB;
case KeyCodes.KEY_UP:
return Keys.UP;
case KEY_PAUSE:
return Keys.UNKNOWN; // FIXME
case KEY_CAPS_LOCK:
return Keys.UNKNOWN; // FIXME
case KEY_SPACE:
return Keys.SPACE;
case KEY_INSERT:
return Keys.INSERT;
case KEY_0:
return Keys.NUM_0;
case KEY_1:
return Keys.NUM_1;
case KEY_2:
return Keys.NUM_2;
case KEY_3:
return Keys.NUM_3;
case KEY_4:
return Keys.NUM_4;
case KEY_5:
return Keys.NUM_5;
case KEY_6:
return Keys.NUM_6;
case KEY_7:
return Keys.NUM_7;
case KEY_8:
return Keys.NUM_8;
case KEY_9:
return Keys.NUM_9;
case KEY_A:
return Keys.A;
case KEY_B:
return Keys.B;
case KEY_C:
return Keys.C;
case KEY_D:
return Keys.D;
case KEY_E:
return Keys.E;
case KEY_F:
return Keys.F;
case KEY_G:
return Keys.G;
case KEY_H:
return Keys.H;
case KEY_I:
return Keys.I;
case KEY_J:
return Keys.J;
case KEY_K:
return Keys.K;
case KEY_L:
return Keys.L;
case KEY_M:
return Keys.M;
case KEY_N:
return Keys.N;
case KEY_O:
return Keys.O;
case KEY_P:
return Keys.P;
case KEY_Q:
return Keys.Q;
case KEY_R:
return Keys.R;
case KEY_S:
return Keys.S;
case KEY_T:
return Keys.T;
case KEY_U:
return Keys.U;
case KEY_V:
return Keys.V;
case KEY_W:
return Keys.W;
case KEY_X:
return Keys.X;
case KEY_Y:
return Keys.Y;
case KEY_Z:
return Keys.Z;
case KEY_LEFT_WINDOW_KEY:
return Keys.UNKNOWN; // FIXME
case KEY_RIGHT_WINDOW_KEY:
return Keys.UNKNOWN; // FIXME
// case KEY_SELECT_KEY: return Keys.SELECT_KEY;
case KEY_NUMPAD0:
return Keys.NUMPAD_0;
case KEY_NUMPAD1:
return Keys.NUMPAD_1;
case KEY_NUMPAD2:
return Keys.NUMPAD_2;
case KEY_NUMPAD3:
return Keys.NUMPAD_3;
case KEY_NUMPAD4:
return Keys.NUMPAD_4;
case KEY_NUMPAD5:
return Keys.NUMPAD_5;
case KEY_NUMPAD6:
return Keys.NUMPAD_6;
case KEY_NUMPAD7:
return Keys.NUMPAD_7;
case KEY_NUMPAD8:
return Keys.NUMPAD_8;
case KEY_NUMPAD9:
return Keys.NUMPAD_9;
case KEY_MULTIPLY:
return Keys.UNKNOWN; // FIXME
case KEY_ADD:
return Keys.PLUS;
case KEY_SUBTRACT:
return Keys.MINUS;
case KEY_DECIMAL_POINT_KEY:
return Keys.PERIOD;
case KEY_DIVIDE:
return Keys.UNKNOWN; // FIXME
case KEY_F1:
return Keys.F1;
case KEY_F2:
return Keys.F2;
case KEY_F3:
return Keys.F3;
case KEY_F4:
return Keys.F4;
case KEY_F5:
return Keys.F5;
case KEY_F6:
return Keys.F6;
case KEY_F7:
return Keys.F7;
case KEY_F8:
return Keys.F8;