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.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.Element;
|
||||
import com.google.gwt.dom.client.NativeEvent;
|
||||
import com.google.gwt.dom.client.Touch;
|
||||
import com.google.gwt.event.dom.client.KeyCodes;
|
||||
|
||||
public class GwtInput implements Input {
|
||||
@@ -73,7 +75,8 @@ public class GwtInput implements Input {
|
||||
|
||||
@Override
|
||||
public int getX(int pointer) {
|
||||
if (pointer != 0) return 0;
|
||||
if (pointer != 0)
|
||||
return 0;
|
||||
return mouseX;
|
||||
}
|
||||
|
||||
@@ -84,7 +87,8 @@ public class GwtInput implements Input {
|
||||
|
||||
@Override
|
||||
public int getDeltaX(int pointer) {
|
||||
if (pointer != 0) return 0;
|
||||
if (pointer != 0)
|
||||
return 0;
|
||||
return deltaX;
|
||||
}
|
||||
|
||||
@@ -95,7 +99,8 @@ public class GwtInput implements Input {
|
||||
|
||||
@Override
|
||||
public int getY(int pointer) {
|
||||
if (pointer != 0) return 0;
|
||||
if (pointer != 0)
|
||||
return 0;
|
||||
return mouseY;
|
||||
}
|
||||
|
||||
@@ -106,7 +111,8 @@ public class GwtInput implements Input {
|
||||
|
||||
@Override
|
||||
public int getDeltaY(int pointer) {
|
||||
if (pointer != 0) return 0;
|
||||
if (pointer != 0)
|
||||
return 0;
|
||||
return deltaY;
|
||||
}
|
||||
|
||||
@@ -122,7 +128,8 @@ public class GwtInput implements Input {
|
||||
|
||||
@Override
|
||||
public boolean isTouched(int pointer) {
|
||||
if (pointer != 0) return false;
|
||||
if (pointer != 0)
|
||||
return false;
|
||||
return touched;
|
||||
}
|
||||
|
||||
@@ -133,7 +140,8 @@ public class GwtInput implements Input {
|
||||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -239,12 +247,18 @@ public class GwtInput implements Input {
|
||||
|
||||
@Override
|
||||
public boolean isPeripheralAvailable(Peripheral peripheral) {
|
||||
if (peripheral == Peripheral.Accelerometer) return false;
|
||||
if (peripheral == Peripheral.Compass) return false;
|
||||
if (peripheral == Peripheral.HardwareKeyboard) return true;
|
||||
if (peripheral == Peripheral.MultitouchScreen) return false;
|
||||
if (peripheral == Peripheral.OnscreenKeyboard) return false;
|
||||
if (peripheral == Peripheral.Vibrator) return false;
|
||||
if (peripheral == Peripheral.Accelerometer)
|
||||
return false;
|
||||
if (peripheral == Peripheral.Compass)
|
||||
return false;
|
||||
if (peripheral == Peripheral.HardwareKeyboard)
|
||||
return true;
|
||||
if (peripheral == Peripheral.MultitouchScreen)
|
||||
return false;
|
||||
if (peripheral == Peripheral.OnscreenKeyboard)
|
||||
return false;
|
||||
if (peripheral == Peripheral.Vibrator)
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -258,8 +272,11 @@ public class GwtInput implements Input {
|
||||
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() /*-{
|
||||
if (!navigator.pointer) {
|
||||
navigator.pointer = navigator.webkitPointer || navigator.mozPointer;
|
||||
@@ -279,8 +296,11 @@ public class GwtInput implements Input {
|
||||
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) /*-{
|
||||
// Navigator pointer is not the right interface according to spec.
|
||||
// Here for backwards compatibility only
|
||||
@@ -290,9 +310,8 @@ public class GwtInput implements Input {
|
||||
// element.requestPointerLock
|
||||
if (!element.requestPointerLock) {
|
||||
element.requestPointerLock = (function() {
|
||||
return element.webkitRequestPointerLock ||
|
||||
element.mozRequestPointerLock ||
|
||||
function(){
|
||||
return element.webkitRequestPointerLock
|
||||
|| element.mozRequestPointerLock || function() {
|
||||
if (navigator.pointer) {
|
||||
navigator.pointer.lock(element);
|
||||
}
|
||||
@@ -306,9 +325,8 @@ public class GwtInput implements Input {
|
||||
private native void exitCursorCatchedJSNI() /*-{
|
||||
if (!$doc.exitPointerLock) {
|
||||
$doc.exitPointerLock = (function() {
|
||||
return $doc.webkitExitPointerLock ||
|
||||
$doc.mozExitPointerLock ||
|
||||
function(){
|
||||
return $doc.webkitExitPointerLock || $doc.mozExitPointerLock
|
||||
|| function() {
|
||||
if (navigator.pointer) {
|
||||
var elem = this;
|
||||
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
|
||||
* @return movement in x direction */
|
||||
* @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
|
||||
/**
|
||||
* from https://github.com/toji/game-shim/blob/master/game-shim.js
|
||||
*
|
||||
* @param event JavaScript Mouse Event
|
||||
* @return movement in y direction */
|
||||
* @return movement in y direction
|
||||
*/
|
||||
private native float getMovementYJSNI(NativeEvent event) /*-{
|
||||
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
|
||||
public void setCursorCatched(boolean catched) {
|
||||
if (catched)
|
||||
@@ -353,7 +380,8 @@ public class GwtInput implements Input {
|
||||
}
|
||||
|
||||
// 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
|
||||
.addEventListener(
|
||||
name,
|
||||
@@ -404,12 +432,14 @@ public class GwtInput implements Input {
|
||||
|
||||
/** Kindly borrowed from PlayN. **/
|
||||
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. **/
|
||||
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() {
|
||||
@@ -423,22 +453,71 @@ public class GwtInput implements Input {
|
||||
addEventListener(Document.get(), "keydown", this, false);
|
||||
addEventListener(Document.get(), "keyup", 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) {
|
||||
if (button == NativeEvent.BUTTON_LEFT) return Buttons.LEFT;
|
||||
if (button == NativeEvent.BUTTON_RIGHT) return Buttons.RIGHT;
|
||||
if (button == NativeEvent.BUTTON_MIDDLE) return Buttons.MIDDLE;
|
||||
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")) {
|
||||
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) {
|
||||
float mouseX = (int) getRelativeX(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;
|
||||
}
|
||||
return;
|
||||
@@ -457,10 +536,9 @@ public class GwtInput implements Input {
|
||||
this.mouseY = (int) getRelativeY(e, canvas);
|
||||
}
|
||||
this.currentEventTimeStamp = TimeUtils.nanoTime();
|
||||
if (processor != null) processor.touchDown(mouseX, mouseY, 0, getButton(e.getButton()));
|
||||
}
|
||||
|
||||
if (e.getType().equals("mousemove")) {
|
||||
if (processor != null)
|
||||
processor.touchDown(mouseX, mouseY, 0, getButton(e.getButton()));
|
||||
} else if (e.getType().equals("mousemove")) {
|
||||
if (isCursorCatched()) {
|
||||
this.deltaX = (int) getMovementXJSNI(e);
|
||||
this.deltaY = (int) getMovementYJSNI(e);
|
||||
@@ -479,10 +557,9 @@ public class GwtInput implements Input {
|
||||
else
|
||||
processor.mouseMoved(mouseX, mouseY);
|
||||
}
|
||||
}
|
||||
|
||||
if (e.getType().equals("mouseup")) {
|
||||
if (!touched) return;
|
||||
} else if (e.getType().equals("mouseup")) {
|
||||
if (!touched)
|
||||
return;
|
||||
this.pressedButtons.remove(getButton(e.getButton()));
|
||||
this.touched = pressedButtons.size() > 0;
|
||||
if (isCursorCatched()) {
|
||||
@@ -498,10 +575,9 @@ public class GwtInput implements Input {
|
||||
}
|
||||
this.currentEventTimeStamp = TimeUtils.nanoTime();
|
||||
this.touched = false;
|
||||
if (processor != null) processor.touchUp(mouseX, mouseY, 0, getButton(e.getButton()));
|
||||
}
|
||||
|
||||
if (e.getType().equals("keydown") && hasFocus) {
|
||||
if (processor != null)
|
||||
processor.touchUp(mouseX, mouseY, 0, getButton(e.getButton()));
|
||||
} else if (e.getType().equals("keydown") && hasFocus) {
|
||||
System.out.println("keydown");
|
||||
int code = keyForCode(e.getKeyCode());
|
||||
if (code == 67) {
|
||||
@@ -512,27 +588,25 @@ public class GwtInput implements Input {
|
||||
}
|
||||
} else {
|
||||
this.pressedKeys.add(code);
|
||||
if (processor != null) processor.keyDown(code);
|
||||
if (processor != null)
|
||||
processor.keyDown(code);
|
||||
}
|
||||
}
|
||||
|
||||
if (e.getType().equals("keypress") && hasFocus) {
|
||||
} else if (e.getType().equals("keypress") && hasFocus) {
|
||||
System.out.println("keypress");
|
||||
char c = (char) e.getCharCode();
|
||||
if (processor != null) processor.keyTyped(c);
|
||||
}
|
||||
|
||||
if (e.getType().equals("keyup") && hasFocus) {
|
||||
if (processor != null)
|
||||
processor.keyTyped(c);
|
||||
} else if (e.getType().equals("keyup") && hasFocus) {
|
||||
System.out.println("keyup");
|
||||
int code = keyForCode(e.getKeyCode());
|
||||
this.pressedKeys.remove(code);
|
||||
if (processor != null) processor.keyUp(code);
|
||||
}
|
||||
|
||||
if (e.getType().equals("mousewheel") ||e.getType().equals("DOMMouseScroll")){
|
||||
if (processor != null)
|
||||
processor.keyUp(code);
|
||||
} else if (e.getType().equals("mousewheel") || e.getType().equals("DOMMouseScroll")) {
|
||||
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