gwt: get multitouch events
This commit is contained in:
@@ -26,10 +26,12 @@ import com.badlogic.gdx.backends.gwt.widgets.TextInputDialogBox;
|
|||||||
import com.badlogic.gdx.backends.gwt.widgets.TextInputDialogBox.TextInputDialogListener;
|
import com.badlogic.gdx.backends.gwt.widgets.TextInputDialogBox.TextInputDialogListener;
|
||||||
import com.badlogic.gdx.utils.TimeUtils;
|
import com.badlogic.gdx.utils.TimeUtils;
|
||||||
import com.google.gwt.core.client.JavaScriptObject;
|
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.CanvasElement;
|
||||||
import com.google.gwt.dom.client.Document;
|
import com.google.gwt.dom.client.Document;
|
||||||
import com.google.gwt.dom.client.Element;
|
import com.google.gwt.dom.client.Element;
|
||||||
import com.google.gwt.dom.client.NativeEvent;
|
import com.google.gwt.dom.client.NativeEvent;
|
||||||
|
import com.google.gwt.dom.client.Touch;
|
||||||
import com.google.gwt.event.dom.client.KeyCodes;
|
import com.google.gwt.event.dom.client.KeyCodes;
|
||||||
|
|
||||||
public class GwtInput implements Input {
|
public class GwtInput implements Input {
|
||||||
@@ -73,7 +75,8 @@ public class GwtInput implements Input {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getX(int pointer) {
|
public int getX(int pointer) {
|
||||||
if (pointer != 0) return 0;
|
if (pointer != 0)
|
||||||
|
return 0;
|
||||||
return mouseX;
|
return mouseX;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +87,8 @@ public class GwtInput implements Input {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getDeltaX(int pointer) {
|
public int getDeltaX(int pointer) {
|
||||||
if (pointer != 0) return 0;
|
if (pointer != 0)
|
||||||
|
return 0;
|
||||||
return deltaX;
|
return deltaX;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +99,8 @@ public class GwtInput implements Input {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getY(int pointer) {
|
public int getY(int pointer) {
|
||||||
if (pointer != 0) return 0;
|
if (pointer != 0)
|
||||||
|
return 0;
|
||||||
return mouseY;
|
return mouseY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +111,8 @@ public class GwtInput implements Input {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getDeltaY(int pointer) {
|
public int getDeltaY(int pointer) {
|
||||||
if (pointer != 0) return 0;
|
if (pointer != 0)
|
||||||
|
return 0;
|
||||||
return deltaY;
|
return deltaY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,7 +128,8 @@ public class GwtInput implements Input {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isTouched(int pointer) {
|
public boolean isTouched(int pointer) {
|
||||||
if (pointer != 0) return false;
|
if (pointer != 0)
|
||||||
|
return false;
|
||||||
return touched;
|
return touched;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +140,8 @@ public class GwtInput implements Input {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isKeyPressed(int key) {
|
public boolean isKeyPressed(int key) {
|
||||||
if (key == Keys.ANY_KEY) return pressedKeys.size() > 0;
|
if (key == Keys.ANY_KEY)
|
||||||
|
return pressedKeys.size() > 0;
|
||||||
return pressedKeys.contains(key);
|
return pressedKeys.contains(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,12 +247,18 @@ public class GwtInput implements Input {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isPeripheralAvailable(Peripheral peripheral) {
|
public boolean isPeripheralAvailable(Peripheral peripheral) {
|
||||||
if (peripheral == Peripheral.Accelerometer) return false;
|
if (peripheral == Peripheral.Accelerometer)
|
||||||
if (peripheral == Peripheral.Compass) return false;
|
return false;
|
||||||
if (peripheral == Peripheral.HardwareKeyboard) return true;
|
if (peripheral == Peripheral.Compass)
|
||||||
if (peripheral == Peripheral.MultitouchScreen) return false;
|
return false;
|
||||||
if (peripheral == Peripheral.OnscreenKeyboard) return false;
|
if (peripheral == Peripheral.HardwareKeyboard)
|
||||||
if (peripheral == Peripheral.Vibrator) return false;
|
return true;
|
||||||
|
if (peripheral == Peripheral.MultitouchScreen)
|
||||||
|
return false;
|
||||||
|
if (peripheral == Peripheral.OnscreenKeyboard)
|
||||||
|
return false;
|
||||||
|
if (peripheral == Peripheral.Vibrator)
|
||||||
|
return false;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,8 +272,11 @@ public class GwtInput implements Input {
|
|||||||
return Orientation.Landscape;
|
return Orientation.Landscape;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** from https://github.com/toji/game-shim/blob/master/game-shim.js
|
/**
|
||||||
* @return is Cursor catched */
|
* from https://github.com/toji/game-shim/blob/master/game-shim.js
|
||||||
|
*
|
||||||
|
* @return is Cursor catched
|
||||||
|
*/
|
||||||
private native boolean isCursorCatchedJSNI() /*-{
|
private native boolean isCursorCatchedJSNI() /*-{
|
||||||
if (!navigator.pointer) {
|
if (!navigator.pointer) {
|
||||||
navigator.pointer = navigator.webkitPointer || navigator.mozPointer;
|
navigator.pointer = navigator.webkitPointer || navigator.mozPointer;
|
||||||
@@ -279,8 +296,11 @@ public class GwtInput implements Input {
|
|||||||
return false;
|
return false;
|
||||||
}-*/;
|
}-*/;
|
||||||
|
|
||||||
/** from https://github.com/toji/game-shim/blob/master/game-shim.js
|
/**
|
||||||
* @param element Canvas */
|
* from https://github.com/toji/game-shim/blob/master/game-shim.js
|
||||||
|
*
|
||||||
|
* @param element Canvas
|
||||||
|
*/
|
||||||
private native void setCursorCatchedJSNI(CanvasElement element) /*-{
|
private native void setCursorCatchedJSNI(CanvasElement element) /*-{
|
||||||
// Navigator pointer is not the right interface according to spec.
|
// Navigator pointer is not the right interface according to spec.
|
||||||
// Here for backwards compatibility only
|
// Here for backwards compatibility only
|
||||||
@@ -290,9 +310,8 @@ public class GwtInput implements Input {
|
|||||||
// element.requestPointerLock
|
// element.requestPointerLock
|
||||||
if (!element.requestPointerLock) {
|
if (!element.requestPointerLock) {
|
||||||
element.requestPointerLock = (function() {
|
element.requestPointerLock = (function() {
|
||||||
return element.webkitRequestPointerLock ||
|
return element.webkitRequestPointerLock
|
||||||
element.mozRequestPointerLock ||
|
|| element.mozRequestPointerLock || function() {
|
||||||
function(){
|
|
||||||
if (navigator.pointer) {
|
if (navigator.pointer) {
|
||||||
navigator.pointer.lock(element);
|
navigator.pointer.lock(element);
|
||||||
}
|
}
|
||||||
@@ -306,9 +325,8 @@ public class GwtInput implements Input {
|
|||||||
private native void exitCursorCatchedJSNI() /*-{
|
private native void exitCursorCatchedJSNI() /*-{
|
||||||
if (!$doc.exitPointerLock) {
|
if (!$doc.exitPointerLock) {
|
||||||
$doc.exitPointerLock = (function() {
|
$doc.exitPointerLock = (function() {
|
||||||
return $doc.webkitExitPointerLock ||
|
return $doc.webkitExitPointerLock || $doc.mozExitPointerLock
|
||||||
$doc.mozExitPointerLock ||
|
|| function() {
|
||||||
function(){
|
|
||||||
if (navigator.pointer) {
|
if (navigator.pointer) {
|
||||||
var elem = this;
|
var elem = this;
|
||||||
navigator.pointer.unlock();
|
navigator.pointer.unlock();
|
||||||
@@ -318,22 +336,31 @@ public class GwtInput implements Input {
|
|||||||
}
|
}
|
||||||
}-*/;
|
}-*/;
|
||||||
|
|
||||||
/** from https://github.com/toji/game-shim/blob/master/game-shim.js
|
/**
|
||||||
|
* from https://github.com/toji/game-shim/blob/master/game-shim.js
|
||||||
|
*
|
||||||
* @param event JavaScript Mouse Event
|
* @param event JavaScript Mouse Event
|
||||||
* @return movement in x direction */
|
* @return movement in x direction
|
||||||
|
*/
|
||||||
private native float getMovementXJSNI(NativeEvent event) /*-{
|
private native float getMovementXJSNI(NativeEvent event) /*-{
|
||||||
return event.movementX || event.webkitMovementX || 0;
|
return event.movementX || event.webkitMovementX || 0;
|
||||||
}-*/;
|
}-*/;
|
||||||
|
|
||||||
/** from https://github.com/toji/game-shim/blob/master/game-shim.js
|
/**
|
||||||
|
* from https://github.com/toji/game-shim/blob/master/game-shim.js
|
||||||
|
*
|
||||||
* @param event JavaScript Mouse Event
|
* @param event JavaScript Mouse Event
|
||||||
* @return movement in y direction */
|
* @return movement in y direction
|
||||||
|
*/
|
||||||
private native float getMovementYJSNI(NativeEvent event) /*-{
|
private native float getMovementYJSNI(NativeEvent event) /*-{
|
||||||
return event.movementY || event.webkitMovementY || 0;
|
return event.movementY || event.webkitMovementY || 0;
|
||||||
}-*/;
|
}-*/;
|
||||||
|
|
||||||
/** works only for Chrome > Version 18 with enabled Mouse Lock enable in about:flags or start Chrome with the
|
/**
|
||||||
* --enable-pointer-lock flag */
|
* works only for Chrome > Version 18 with enabled Mouse Lock enable in
|
||||||
|
* about:flags or start Chrome with the
|
||||||
|
* --enable-pointer-lock flag
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void setCursorCatched(boolean catched) {
|
public void setCursorCatched(boolean catched) {
|
||||||
if (catched)
|
if (catched)
|
||||||
@@ -353,7 +380,8 @@ public class GwtInput implements Input {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// kindly borrowed from our dear playn friends...
|
// kindly borrowed from our dear playn friends...
|
||||||
static native void addEventListener (JavaScriptObject target, String name, GwtInput handler, boolean capture) /*-{
|
static native void addEventListener(JavaScriptObject target, String name, GwtInput handler,
|
||||||
|
boolean capture) /*-{
|
||||||
target
|
target
|
||||||
.addEventListener(
|
.addEventListener(
|
||||||
name,
|
name,
|
||||||
@@ -404,12 +432,14 @@ public class GwtInput implements Input {
|
|||||||
|
|
||||||
/** Kindly borrowed from PlayN. **/
|
/** Kindly borrowed from PlayN. **/
|
||||||
protected static float getRelativeX(NativeEvent e, Element target) {
|
protected static float getRelativeX(NativeEvent e, Element target) {
|
||||||
return e.getClientX() - target.getAbsoluteLeft() + target.getScrollLeft() + target.getOwnerDocument().getScrollLeft();
|
return e.getClientX() - target.getAbsoluteLeft() + target.getScrollLeft()
|
||||||
|
+ target.getOwnerDocument().getScrollLeft();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Kindly borrowed from PlayN. **/
|
/** Kindly borrowed from PlayN. **/
|
||||||
protected static float getRelativeY(NativeEvent e, Element target) {
|
protected static float getRelativeY(NativeEvent e, Element target) {
|
||||||
return e.getClientY() - target.getAbsoluteTop() + target.getScrollTop() + target.getOwnerDocument().getScrollTop();
|
return e.getClientY() - target.getAbsoluteTop() + target.getScrollTop()
|
||||||
|
+ target.getOwnerDocument().getScrollTop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void hookEvents() {
|
private void hookEvents() {
|
||||||
@@ -423,22 +453,71 @@ public class GwtInput implements Input {
|
|||||||
addEventListener(Document.get(), "keydown", this, false);
|
addEventListener(Document.get(), "keydown", this, false);
|
||||||
addEventListener(Document.get(), "keyup", this, false);
|
addEventListener(Document.get(), "keyup", this, false);
|
||||||
addEventListener(Document.get(), "keypress", this, false);
|
addEventListener(Document.get(), "keypress", this, false);
|
||||||
|
addEventListener(canvas, "touchstart", this, true);
|
||||||
|
addEventListener(Document.get(), "touchstart", this, true);
|
||||||
|
addEventListener(canvas, "touchend", this, true);
|
||||||
|
addEventListener(Document.get(), "touchend", this, true);
|
||||||
|
addEventListener(canvas, "touchmove", this, true);
|
||||||
|
addEventListener(Document.get(), "touchmove", this, true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getButton(int button) {
|
private int getButton(int button) {
|
||||||
if (button == NativeEvent.BUTTON_LEFT) return Buttons.LEFT;
|
if (button == NativeEvent.BUTTON_LEFT)
|
||||||
if (button == NativeEvent.BUTTON_RIGHT) return Buttons.RIGHT;
|
return Buttons.LEFT;
|
||||||
if (button == NativeEvent.BUTTON_MIDDLE) return Buttons.MIDDLE;
|
if (button == NativeEvent.BUTTON_RIGHT)
|
||||||
|
return Buttons.RIGHT;
|
||||||
|
if (button == NativeEvent.BUTTON_MIDDLE)
|
||||||
|
return Buttons.MIDDLE;
|
||||||
return Buttons.LEFT;
|
return Buttons.LEFT;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleEvent(NativeEvent e) {
|
private void handleEvent(NativeEvent e) {
|
||||||
|
|
||||||
if (e.getType().equals("mousedown")) {
|
double r = GwtGraphics.getDevicePixelRatioJSNI();
|
||||||
|
|
||||||
|
if (e.getType().equals("touchstart")) {
|
||||||
|
this.justTouched = true;
|
||||||
|
this.deltaX = 0;
|
||||||
|
this.deltaY = 0;
|
||||||
|
this.currentEventTimeStamp = TimeUtils.nanoTime();
|
||||||
|
|
||||||
|
if (processor != null) {
|
||||||
|
e.preventDefault();
|
||||||
|
JsArray<Touch> ts = e.getChangedTouches();
|
||||||
|
for (int i = 0, n = ts.length(); i < n; i++) {
|
||||||
|
Touch t = ts.get(i);
|
||||||
|
processor.touchDown((int)(r * t.getClientX()), (int) (r * t.getClientY()), t.getIdentifier(),
|
||||||
|
Buttons.LEFT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (e.getType().equals("touchend")) {
|
||||||
|
this.currentEventTimeStamp = TimeUtils.nanoTime();
|
||||||
|
if (processor != null) {
|
||||||
|
e.preventDefault();
|
||||||
|
JsArray<Touch> ts = e.getChangedTouches();
|
||||||
|
for (int i = 0, n = ts.length(); i < n; i++) {
|
||||||
|
Touch t = ts.get(i);
|
||||||
|
processor.touchUp((int)(r * t.getClientX()), (int) (r * t.getClientY()), t.getIdentifier(),
|
||||||
|
Buttons.LEFT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (e.getType().equals("touchmove")) {
|
||||||
|
this.currentEventTimeStamp = TimeUtils.nanoTime();
|
||||||
|
if (processor != null) {
|
||||||
|
e.preventDefault();
|
||||||
|
JsArray<Touch> ts = e.getChangedTouches();
|
||||||
|
for (int i = 0, n = ts.length(); i < n; i++) {
|
||||||
|
Touch t = ts.get(i);
|
||||||
|
processor.touchDragged((int)(r * t.getClientX()), (int) (r * t.getClientY()), t.getIdentifier());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (e.getType().equals("mousedown")) {
|
||||||
if (!e.getEventTarget().equals(canvas) || touched) {
|
if (!e.getEventTarget().equals(canvas) || touched) {
|
||||||
float mouseX = (int) getRelativeX(e, canvas);
|
float mouseX = (int) getRelativeX(e, canvas);
|
||||||
float mouseY = (int) getRelativeY(e, canvas);
|
float mouseY = (int) getRelativeY(e, canvas);
|
||||||
if (mouseX < 0 || mouseX > Gdx.graphics.getWidth() || mouseY < 0 || mouseY > Gdx.graphics.getHeight()) {
|
if (mouseX < 0 || mouseX > Gdx.graphics.getWidth() || mouseY < 0
|
||||||
|
|| mouseY > Gdx.graphics.getHeight()) {
|
||||||
hasFocus = false;
|
hasFocus = false;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -457,10 +536,9 @@ public class GwtInput implements Input {
|
|||||||
this.mouseY = (int) getRelativeY(e, canvas);
|
this.mouseY = (int) getRelativeY(e, canvas);
|
||||||
}
|
}
|
||||||
this.currentEventTimeStamp = TimeUtils.nanoTime();
|
this.currentEventTimeStamp = TimeUtils.nanoTime();
|
||||||
if (processor != null) processor.touchDown(mouseX, mouseY, 0, getButton(e.getButton()));
|
if (processor != null)
|
||||||
}
|
processor.touchDown(mouseX, mouseY, 0, getButton(e.getButton()));
|
||||||
|
} else if (e.getType().equals("mousemove")) {
|
||||||
if (e.getType().equals("mousemove")) {
|
|
||||||
if (isCursorCatched()) {
|
if (isCursorCatched()) {
|
||||||
this.deltaX = (int) getMovementXJSNI(e);
|
this.deltaX = (int) getMovementXJSNI(e);
|
||||||
this.deltaY = (int) getMovementYJSNI(e);
|
this.deltaY = (int) getMovementYJSNI(e);
|
||||||
@@ -479,10 +557,9 @@ public class GwtInput implements Input {
|
|||||||
else
|
else
|
||||||
processor.mouseMoved(mouseX, mouseY);
|
processor.mouseMoved(mouseX, mouseY);
|
||||||
}
|
}
|
||||||
}
|
} else if (e.getType().equals("mouseup")) {
|
||||||
|
if (!touched)
|
||||||
if (e.getType().equals("mouseup")) {
|
return;
|
||||||
if (!touched) return;
|
|
||||||
this.pressedButtons.remove(getButton(e.getButton()));
|
this.pressedButtons.remove(getButton(e.getButton()));
|
||||||
this.touched = pressedButtons.size() > 0;
|
this.touched = pressedButtons.size() > 0;
|
||||||
if (isCursorCatched()) {
|
if (isCursorCatched()) {
|
||||||
@@ -498,10 +575,9 @@ public class GwtInput implements Input {
|
|||||||
}
|
}
|
||||||
this.currentEventTimeStamp = TimeUtils.nanoTime();
|
this.currentEventTimeStamp = TimeUtils.nanoTime();
|
||||||
this.touched = false;
|
this.touched = false;
|
||||||
if (processor != null) processor.touchUp(mouseX, mouseY, 0, getButton(e.getButton()));
|
if (processor != null)
|
||||||
}
|
processor.touchUp(mouseX, mouseY, 0, getButton(e.getButton()));
|
||||||
|
} else if (e.getType().equals("keydown") && hasFocus) {
|
||||||
if (e.getType().equals("keydown") && hasFocus) {
|
|
||||||
System.out.println("keydown");
|
System.out.println("keydown");
|
||||||
int code = keyForCode(e.getKeyCode());
|
int code = keyForCode(e.getKeyCode());
|
||||||
if (code == 67) {
|
if (code == 67) {
|
||||||
@@ -512,27 +588,25 @@ public class GwtInput implements Input {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.pressedKeys.add(code);
|
this.pressedKeys.add(code);
|
||||||
if (processor != null) processor.keyDown(code);
|
if (processor != null)
|
||||||
|
processor.keyDown(code);
|
||||||
}
|
}
|
||||||
}
|
} else if (e.getType().equals("keypress") && hasFocus) {
|
||||||
|
|
||||||
if (e.getType().equals("keypress") && hasFocus) {
|
|
||||||
System.out.println("keypress");
|
System.out.println("keypress");
|
||||||
char c = (char) e.getCharCode();
|
char c = (char) e.getCharCode();
|
||||||
if (processor != null) processor.keyTyped(c);
|
if (processor != null)
|
||||||
}
|
processor.keyTyped(c);
|
||||||
|
} else if (e.getType().equals("keyup") && hasFocus) {
|
||||||
if (e.getType().equals("keyup") && hasFocus) {
|
|
||||||
System.out.println("keyup");
|
System.out.println("keyup");
|
||||||
int code = keyForCode(e.getKeyCode());
|
int code = keyForCode(e.getKeyCode());
|
||||||
this.pressedKeys.remove(code);
|
this.pressedKeys.remove(code);
|
||||||
if (processor != null) processor.keyUp(code);
|
if (processor != null)
|
||||||
}
|
processor.keyUp(code);
|
||||||
|
} else if (e.getType().equals("mousewheel") || e.getType().equals("DOMMouseScroll")) {
|
||||||
if (e.getType().equals("mousewheel") ||e.getType().equals("DOMMouseScroll")){
|
|
||||||
float dir = getMouseWheelVelocity(e);
|
float dir = getMouseWheelVelocity(e);
|
||||||
|
|
||||||
if (dir != 0 && processor != null) processor.scrolled(dir > 0 ? 1 : -1);
|
if (dir != 0 && processor != null)
|
||||||
|
processor.scrolled(dir > 0 ? 1 : -1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user