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 {
|
||||||
@@ -46,111 +48,117 @@ public class GwtInput implements Input {
|
|||||||
final CanvasElement canvas;
|
final CanvasElement canvas;
|
||||||
boolean hasFocus = true;
|
boolean hasFocus = true;
|
||||||
|
|
||||||
public GwtInput (CanvasElement canvas) {
|
public GwtInput(CanvasElement canvas) {
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
hookEvents();
|
hookEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getAccelerometerX () {
|
public float getAccelerometerX() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getAccelerometerY () {
|
public float getAccelerometerY() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getAccelerometerZ () {
|
public float getAccelerometerZ() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getX () {
|
public int getX() {
|
||||||
return mouseX;
|
return mouseX;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getDeltaX () {
|
public int getDeltaX() {
|
||||||
return deltaX;
|
return deltaX;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getY () {
|
public int getY() {
|
||||||
return mouseY;
|
return mouseY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getDeltaY () {
|
public int getDeltaY() {
|
||||||
return deltaY;
|
return deltaY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isTouched () {
|
public boolean isTouched() {
|
||||||
return touched;
|
return touched;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean justTouched () {
|
public boolean justTouched() {
|
||||||
return justTouched;
|
return justTouched;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isButtonPressed (int button) {
|
public boolean isButtonPressed(int button) {
|
||||||
return button == Buttons.LEFT && touched;
|
return button == Buttons.LEFT && touched;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getTextInput (TextInputListener listener, String title, String text) {
|
public void getTextInput(TextInputListener listener, String title, String text) {
|
||||||
TextInputDialogBox dialog = new TextInputDialogBox(title, text, null);
|
TextInputDialogBox dialog = new TextInputDialogBox(title, text, null);
|
||||||
final TextInputListener capturedListener = listener;
|
final TextInputListener capturedListener = listener;
|
||||||
dialog.setListener(new TextInputDialogListener() {
|
dialog.setListener(new TextInputDialogListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onPositive (String text) {
|
public void onPositive(String text) {
|
||||||
if (capturedListener != null) {
|
if (capturedListener != null) {
|
||||||
capturedListener.input(text);
|
capturedListener.input(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNegative () {
|
public void onNegative() {
|
||||||
if (capturedListener != null) {
|
if (capturedListener != null) {
|
||||||
capturedListener.canceled();
|
capturedListener.canceled();
|
||||||
}
|
}
|
||||||
@@ -159,19 +167,19 @@ public class GwtInput implements Input {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getPlaceholderTextInput (TextInputListener listener, String title, String placeholder) {
|
public void getPlaceholderTextInput(TextInputListener listener, String title, String placeholder) {
|
||||||
TextInputDialogBox dialog = new TextInputDialogBox(title, null, placeholder);
|
TextInputDialogBox dialog = new TextInputDialogBox(title, null, placeholder);
|
||||||
final TextInputListener capturedListener = listener;
|
final TextInputListener capturedListener = listener;
|
||||||
dialog.setListener(new TextInputDialogListener() {
|
dialog.setListener(new TextInputDialogListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onPositive (String text) {
|
public void onPositive(String text) {
|
||||||
if (capturedListener != null) {
|
if (capturedListener != null) {
|
||||||
capturedListener.input(text);
|
capturedListener.input(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNegative () {
|
public void onNegative() {
|
||||||
if (capturedListener != null) {
|
if (capturedListener != null) {
|
||||||
capturedListener.canceled();
|
capturedListener.canceled();
|
||||||
}
|
}
|
||||||
@@ -180,98 +188,107 @@ public class GwtInput implements Input {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setOnscreenKeyboardVisible (boolean visible) {
|
public void setOnscreenKeyboardVisible(boolean visible) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void vibrate (int milliseconds) {
|
public void vibrate(int milliseconds) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void vibrate (long[] pattern, int repeat) {
|
public void vibrate(long[] pattern, int repeat) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cancelVibrate () {
|
public void cancelVibrate() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getAzimuth () {
|
public float getAzimuth() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getPitch () {
|
public float getPitch() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getRoll () {
|
public float getRoll() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getRotationMatrix (float[] matrix) {
|
public void getRotationMatrix(float[] matrix) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getCurrentEventTime () {
|
public long getCurrentEventTime() {
|
||||||
return currentEventTimeStamp;
|
return currentEventTimeStamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setCatchBackKey (boolean catchBack) {
|
public void setCatchBackKey(boolean catchBack) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setCatchMenuKey (boolean catchMenu) {
|
public void setCatchMenuKey(boolean catchMenu) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setInputProcessor (InputProcessor processor) {
|
public void setInputProcessor(InputProcessor processor) {
|
||||||
this.processor = processor;
|
this.processor = processor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputProcessor getInputProcessor () {
|
public InputProcessor getInputProcessor() {
|
||||||
return processor;
|
return processor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getRotation () {
|
public int getRotation() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Orientation getNativeOrientation () {
|
public Orientation getNativeOrientation() {
|
||||||
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
|
||||||
private native boolean isCursorCatchedJSNI () /*-{
|
*
|
||||||
if(!navigator.pointer) {
|
* @return is Cursor catched
|
||||||
|
*/
|
||||||
|
private native boolean isCursorCatchedJSNI() /*-{
|
||||||
|
if (!navigator.pointer) {
|
||||||
navigator.pointer = navigator.webkitPointer || navigator.mozPointer;
|
navigator.pointer = navigator.webkitPointer || navigator.mozPointer;
|
||||||
}
|
}
|
||||||
if(navigator.pointer) {
|
if (navigator.pointer) {
|
||||||
if(typeof(navigator.pointer.isLocked) === "boolean") {
|
if (typeof (navigator.pointer.isLocked) === "boolean") {
|
||||||
// Chrome initially launched with this interface
|
// Chrome initially launched with this interface
|
||||||
return navigator.pointer.isLocked;
|
return navigator.pointer.isLocked;
|
||||||
} else if(typeof(navigator.pointer.isLocked) === "function") {
|
} else if (typeof (navigator.pointer.isLocked) === "function") {
|
||||||
// Some older builds might provide isLocked as a function
|
// Some older builds might provide isLocked as a function
|
||||||
return navigator.pointer.isLocked();
|
return navigator.pointer.isLocked();
|
||||||
} else if(typeof(navigator.pointer.islocked) === "function") {
|
} else if (typeof (navigator.pointer.islocked) === "function") {
|
||||||
// For compatibility with early Firefox build
|
// For compatibility with early Firefox build
|
||||||
return navigator.pointer.islocked();
|
return navigator.pointer.islocked();
|
||||||
}
|
}
|
||||||
@@ -279,21 +296,23 @@ 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
|
||||||
private native void setCursorCatchedJSNI (CanvasElement element) /*-{
|
*
|
||||||
|
* @param element Canvas
|
||||||
|
*/
|
||||||
|
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
|
||||||
if(!navigator.pointer) {
|
if (!navigator.pointer) {
|
||||||
navigator.pointer = navigator.webkitPointer || navigator.mozPointer;
|
navigator.pointer = navigator.webkitPointer || navigator.mozPointer;
|
||||||
}
|
}
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -303,13 +322,12 @@ 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 */
|
||||||
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,24 +336,33 @@ 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)
|
||||||
setCursorCatchedJSNI(canvas);
|
setCursorCatchedJSNI(canvas);
|
||||||
else
|
else
|
||||||
@@ -343,17 +370,18 @@ public class GwtInput implements Input {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isCursorCatched () {
|
public boolean isCursorCatched() {
|
||||||
return isCursorCatchedJSNI();
|
return isCursorCatchedJSNI();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setCursorPosition (int x, int y) {
|
public void setCursorPosition(int x, int y) {
|
||||||
// FIXME??
|
// FIXME??
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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,
|
||||||
@@ -362,7 +390,7 @@ public class GwtInput implements Input {
|
|||||||
}, capture);
|
}, capture);
|
||||||
}-*/;
|
}-*/;
|
||||||
|
|
||||||
private static native float getMouseWheelVelocity (NativeEvent evt) /*-{
|
private static native float getMouseWheelVelocity(NativeEvent evt) /*-{
|
||||||
var delta = 0.0;
|
var delta = 0.0;
|
||||||
var agentInfo = @com.badlogic.gdx.backends.gwt.GwtApplication::agentInfo()();
|
var agentInfo = @com.badlogic.gdx.backends.gwt.GwtApplication::agentInfo()();
|
||||||
|
|
||||||
@@ -394,7 +422,7 @@ public class GwtInput implements Input {
|
|||||||
}-*/;
|
}-*/;
|
||||||
|
|
||||||
/** Kindly borrowed from PlayN. **/
|
/** Kindly borrowed from PlayN. **/
|
||||||
protected static native String getMouseWheelEvent () /*-{
|
protected static native String getMouseWheelEvent() /*-{
|
||||||
if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
|
if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
|
||||||
return "DOMMouseScroll";
|
return "DOMMouseScroll";
|
||||||
} else {
|
} else {
|
||||||
@@ -403,16 +431,18 @@ 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() {
|
||||||
addEventListener(canvas, "mousedown", this, true);
|
addEventListener(canvas, "mousedown", this, true);
|
||||||
addEventListener(Document.get(), "mousedown", this, true);
|
addEventListener(Document.get(), "mousedown", this, true);
|
||||||
addEventListener(canvas, "mouseup", this, true);
|
addEventListener(canvas, "mouseup", this, true);
|
||||||
@@ -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;
|
||||||
@@ -453,24 +532,23 @@ public class GwtInput implements Input {
|
|||||||
this.mouseX += getMovementXJSNI(e);
|
this.mouseX += getMovementXJSNI(e);
|
||||||
this.mouseY += getMovementYJSNI(e);
|
this.mouseY += getMovementYJSNI(e);
|
||||||
} else {
|
} else {
|
||||||
this.mouseX = (int)getRelativeX(e, canvas);
|
this.mouseX = (int) getRelativeX(e, canvas);
|
||||||
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);
|
||||||
this.mouseX += getMovementXJSNI(e);
|
this.mouseX += getMovementXJSNI(e);
|
||||||
this.mouseY += getMovementYJSNI(e);
|
this.mouseY += getMovementYJSNI(e);
|
||||||
} else {
|
} else {
|
||||||
this.deltaX = (int)getRelativeX(e, canvas) - mouseX;
|
this.deltaX = (int) getRelativeX(e, canvas) - mouseX;
|
||||||
this.deltaY = (int)getRelativeY(e, canvas) - mouseY;
|
this.deltaY = (int) getRelativeY(e, canvas) - mouseY;
|
||||||
this.mouseX = (int)getRelativeX(e, canvas);
|
this.mouseX = (int) getRelativeX(e, canvas);
|
||||||
this.mouseY = (int)getRelativeY(e, canvas);
|
this.mouseY = (int) getRelativeY(e, canvas);
|
||||||
}
|
}
|
||||||
this.currentEventTimeStamp = TimeUtils.nanoTime();
|
this.currentEventTimeStamp = TimeUtils.nanoTime();
|
||||||
if (processor != null) {
|
if (processor != null) {
|
||||||
@@ -479,29 +557,27 @@ 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()) {
|
||||||
this.deltaX = (int)getMovementXJSNI(e);
|
this.deltaX = (int) getMovementXJSNI(e);
|
||||||
this.deltaY = (int)getMovementYJSNI(e);
|
this.deltaY = (int) getMovementYJSNI(e);
|
||||||
this.mouseX += getMovementXJSNI(e);
|
this.mouseX += getMovementXJSNI(e);
|
||||||
this.mouseY += getMovementYJSNI(e);
|
this.mouseY += getMovementYJSNI(e);
|
||||||
} else {
|
} else {
|
||||||
this.deltaX = (int)getRelativeX(e, canvas) - mouseX;
|
this.deltaX = (int) getRelativeX(e, canvas) - mouseX;
|
||||||
this.deltaY = (int)getRelativeY(e, canvas) - mouseY;
|
this.deltaY = (int) getRelativeY(e, canvas) - mouseY;
|
||||||
this.mouseX = (int)getRelativeX(e, canvas);
|
this.mouseX = (int) getRelativeX(e, canvas);
|
||||||
this.mouseY = (int)getRelativeY(e, canvas);
|
this.mouseY = (int) getRelativeY(e, canvas);
|
||||||
}
|
}
|
||||||
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,35 +588,33 @@ 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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if(hasFocus) e.preventDefault();
|
// if(hasFocus) e.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** borrowed from PlayN, thanks guys **/
|
/** borrowed from PlayN, thanks guys **/
|
||||||
private static int keyForCode (int keyCode) {
|
private static int keyForCode(int keyCode) {
|
||||||
switch (keyCode) {
|
switch (keyCode) {
|
||||||
case KeyCodes.KEY_ALT:
|
case KeyCodes.KEY_ALT:
|
||||||
return Keys.ALT_LEFT;
|
return Keys.ALT_LEFT;
|
||||||
|
|||||||
Reference in New Issue
Block a user