/** * Return the {@link Mouse} button given a {@link NativeEvent} * * @param evt Native event * @return {@link Mouse} button corresponding to the event */ protected static int getMouseButton(NativeEvent evt) { switch (evt.getButton()) { case (NativeEvent.BUTTON_LEFT): return Mouse.BUTTON_LEFT; case (NativeEvent.BUTTON_MIDDLE): return Mouse.BUTTON_MIDDLE; case (NativeEvent.BUTTON_RIGHT): return Mouse.BUTTON_RIGHT; default: return evt.getButton(); } }
private void handleEvent(NativeEvent e) { if (e.getType().equals("contextmenu")) { e.preventDefault(); e.stopPropagation(); return; } if (e.getType().equals("mousedown")) { if (!e.getEventTarget().equals(canvas) || touched[0]) { 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()) { hasFocus = false; } return; } hasFocus = true; this.justTouched = true; this.touched[0] = true; this.pressedButtons.add(getButton(e.getButton())); this.deltaX[0] = 0; this.deltaY[0] = 0; if (isCursorCatched()) { this.touchX[0] += getMovementXJSNI(e); this.touchY[0] += getMovementYJSNI(e); } else { this.touchX[0] = (int) getRelativeX(e, canvas); this.touchY[0] = (int) getRelativeY(e, canvas); } this.currentEventTimeStamp = TimeUtils.nanoTime(); if (processor != null) if (processor.touchDown(touchX[0], touchY[0], 0, getButton(e.getButton()))) { e.preventDefault(); e.stopPropagation(); } } 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] = (int) getRelativeX(e, canvas) - touchX[0]; this.deltaY[0] = (int) getRelativeY(e, canvas) - touchY[0]; this.touchX[0] = (int) getRelativeX(e, canvas); this.touchY[0] = (int) 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 (!touched[0]) 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] = (int) getRelativeX(e, canvas) - touchX[0]; this.deltaY[0] = (int) getRelativeY(e, canvas) - touchY[0]; this.touchX[0] = (int) getRelativeX(e, canvas); this.touchY[0] = (int) 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("keydown") && hasFocus) { // System.out.println("keydown"); int code = keyForCode(e.getKeyCode()); if (code == 67) { e.preventDefault(); if (processor != null) { processor.keyDown(code); processor.keyTyped('\b'); } } else { this.pressedKeys.add(code); if (processor != null) processor.keyDown(code); } } 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) { // System.out.println("keyup"); int code = keyForCode(e.getKeyCode()); this.pressedKeys.remove(code); 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 touchId = touch.getIdentifier(); touched[touchId] = true; double r = GwtGraphics.getDevicePixelRatioJSNI(); int x = (int) (touch.getRelativeX(canvas) * r); int y = (int) (touch.getRelativeY(canvas) * r); touchX[touchId] = x; touchY[touchId] = y; 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 touchId = touch.getIdentifier(); double r = GwtGraphics.getDevicePixelRatioJSNI(); int x = (int) (touch.getRelativeX(canvas) * r); int y = (int) (touch.getRelativeY(canvas) * r); deltaX[touchId] = x - touchX[touchId]; deltaY[touchId] = y - touchY[touchId]; touchX[touchId] = x; touchY[touchId] = y; 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 touchId = touch.getIdentifier(); touched[touchId] = false; double r = GwtGraphics.getDevicePixelRatioJSNI(); int x = (int) (touch.getRelativeX(canvas) * r); int y = (int) (touch.getRelativeY(canvas) * r); deltaX[touchId] = x - touchX[touchId]; deltaY[touchId] = y - touchY[touchId]; touchX[touchId] = x; touchY[touchId] = y; 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 touchId = touch.getIdentifier(); touched[touchId] = false; double r = GwtGraphics.getDevicePixelRatioJSNI(); int x = (int) (touch.getRelativeX(canvas) * r); int y = (int) (touch.getRelativeY(canvas) * r); deltaX[touchId] = x - touchX[touchId]; deltaY[touchId] = y - touchY[touchId]; touchX[touchId] = x; touchY[touchId] = y; if (processor != null) { processor.touchUp(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT); } } this.currentEventTimeStamp = TimeUtils.nanoTime(); e.preventDefault(); } 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(hasFocus) e.preventDefault(); }