Search in sources :

Example 16 with TouchEvent

use of com.jme3.input.event.TouchEvent in project jmonkeyengine by jMonkeyEngine.

the class AndroidGestureProcessor method onSingleTapConfirmed.

/* Events from onDoubleTapListener */
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
    // Up of single tap when no double tap followed.
    //        logger.log(Level.INFO, "onSingleTapConfirmed pointerId: {0}, action: {1}, x: {2}, y: {3}",
    //                new Object[]{touchInput.getPointerId(event), touchInput.getAction(event), event.getX(), event.getY()});
    float jmeX = touchInput.getJmeX(event.getX());
    float jmeY = touchInput.invertY(touchInput.getJmeY(event.getY()));
    TouchEvent touchEvent = touchInput.getFreeTouchEvent();
    touchEvent.set(TouchEvent.Type.TAP, jmeX, jmeY, 0, 0);
    touchEvent.setPointerId(touchInput.getPointerId(event));
    touchEvent.setTime(event.getEventTime());
    touchEvent.setPressure(event.getPressure());
    touchInput.addEvent(touchEvent);
    return true;
}
Also used : TouchEvent(com.jme3.input.event.TouchEvent)

Example 17 with TouchEvent

use of com.jme3.input.event.TouchEvent in project jmonkeyengine by jMonkeyEngine.

the class AndroidGestureProcessor method onScaleEnd.

@Override
public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) {
    //        logger.log(Level.INFO, "onScaleEnd");
    scaleStartX = gestureDownX;
    scaleStartY = gestureDownY;
    TouchEvent touchEvent = touchInput.getFreeTouchEvent();
    touchEvent.set(TouchEvent.Type.SCALE_END, scaleStartX, scaleStartY, 0f, 0f);
    touchEvent.setPointerId(0);
    touchEvent.setTime(scaleGestureDetector.getEventTime());
    touchEvent.setScaleSpan(scaleGestureDetector.getCurrentSpan());
    touchEvent.setDeltaScaleSpan(scaleGestureDetector.getCurrentSpan() - scaleGestureDetector.getPreviousSpan());
    touchEvent.setScaleFactor(scaleGestureDetector.getScaleFactor());
    touchEvent.setScaleSpanInProgress(touchInput.getScaleDetector().isInProgress());
    touchInput.addEvent(touchEvent);
}
Also used : TouchEvent(com.jme3.input.event.TouchEvent)

Example 18 with TouchEvent

use of com.jme3.input.event.TouchEvent in project jmonkeyengine by jMonkeyEngine.

the class AndroidTouchInput method onTouch.

public boolean onTouch(MotionEvent event) {
    if (!isInitialized()) {
        return false;
    }
    boolean bWasHandled = false;
    TouchEvent touch = null;
    //    System.out.println("native : " + event.getAction());
    int action = getAction(event);
    int pointerIndex = getPointerIndex(event);
    int pointerId = getPointerId(event);
    Vector2f lastPos = lastPositions.get(pointerId);
    float jmeX;
    float jmeY;
    numPointers = event.getPointerCount();
    //final int pointerCount = event.getPointerCount();
    switch(getAction(event)) {
        case MotionEvent.ACTION_POINTER_DOWN:
        case MotionEvent.ACTION_DOWN:
            jmeX = getJmeX(event.getX(pointerIndex));
            jmeY = invertY(getJmeY(event.getY(pointerIndex)));
            touch = getFreeTouchEvent();
            touch.set(TouchEvent.Type.DOWN, jmeX, jmeY, 0, 0);
            touch.setPointerId(pointerId);
            touch.setTime(event.getEventTime());
            touch.setPressure(event.getPressure(pointerIndex));
            lastPos = new Vector2f(jmeX, jmeY);
            lastPositions.put(pointerId, lastPos);
            addEvent(touch);
            addEvent(generateMouseEvent(touch));
            bWasHandled = true;
            break;
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            jmeX = getJmeX(event.getX(pointerIndex));
            jmeY = invertY(getJmeY(event.getY(pointerIndex)));
            touch = getFreeTouchEvent();
            touch.set(TouchEvent.Type.UP, jmeX, jmeY, 0, 0);
            touch.setPointerId(pointerId);
            touch.setTime(event.getEventTime());
            touch.setPressure(event.getPressure(pointerIndex));
            lastPositions.remove(pointerId);
            addEvent(touch);
            addEvent(generateMouseEvent(touch));
            bWasHandled = true;
            break;
        case MotionEvent.ACTION_MOVE:
            // Convert all pointers into events
            for (int p = 0; p < event.getPointerCount(); p++) {
                jmeX = getJmeX(event.getX(p));
                jmeY = invertY(getJmeY(event.getY(p)));
                lastPos = lastPositions.get(event.getPointerId(p));
                if (lastPos == null) {
                    lastPos = new Vector2f(jmeX, jmeY);
                    lastPositions.put(event.getPointerId(p), lastPos);
                }
                float dX = jmeX - lastPos.x;
                float dY = jmeY - lastPos.y;
                if (dX != 0 || dY != 0) {
                    touch = getFreeTouchEvent();
                    touch.set(TouchEvent.Type.MOVE, jmeX, jmeY, dX, dY);
                    touch.setPointerId(event.getPointerId(p));
                    touch.setTime(event.getEventTime());
                    touch.setPressure(event.getPressure(p));
                    lastPos.set(jmeX, jmeY);
                    addEvent(touch);
                    addEvent(generateMouseEvent(touch));
                    bWasHandled = true;
                }
            }
            break;
        case MotionEvent.ACTION_OUTSIDE:
            break;
    }
    // Try to detect gestures
    if (gestureDetector != null) {
        gestureDetector.onTouchEvent(event);
    }
    if (scaleDetector != null) {
        scaleDetector.onTouchEvent(event);
    }
    return bWasHandled;
}
Also used : Vector2f(com.jme3.math.Vector2f) TouchEvent(com.jme3.input.event.TouchEvent)

Example 19 with TouchEvent

use of com.jme3.input.event.TouchEvent in project jmonkeyengine by jMonkeyEngine.

the class AndroidTouchInput method onKey.

public boolean onKey(KeyEvent event) {
    if (!isInitialized()) {
        return false;
    }
    TouchEvent evt;
    // TODO: get touch event from pool
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        evt = new TouchEvent();
        evt.set(TouchEvent.Type.KEY_DOWN);
        evt.setKeyCode(event.getKeyCode());
        evt.setCharacters(event.getCharacters());
        evt.setTime(event.getEventTime());
        // Send the event
        addEvent(evt);
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        evt = new TouchEvent();
        evt.set(TouchEvent.Type.KEY_UP);
        evt.setKeyCode(event.getKeyCode());
        evt.setCharacters(event.getCharacters());
        evt.setTime(event.getEventTime());
        // Send the event
        addEvent(evt);
    }
    if (isSimulateKeyboard()) {
        KeyInputEvent kie;
        char unicodeChar = (char) event.getUnicodeChar();
        int jmeKeyCode = AndroidKeyMapping.getJmeKey(event.getKeyCode());
        boolean pressed = event.getAction() == KeyEvent.ACTION_DOWN;
        boolean repeating = pressed && event.getRepeatCount() > 0;
        kie = new KeyInputEvent(jmeKeyCode, unicodeChar, pressed, repeating);
        kie.setTime(event.getEventTime());
        addEvent(kie);
    //            logger.log(Level.FINE, "onKey keyCode: {0}, jmeKeyCode: {1}, pressed: {2}, repeating: {3}",
    //                    new Object[]{event.getKeyCode(), jmeKeyCode, pressed, repeating});
    //            logger.log(Level.FINE, "creating KeyInputEvent: {0}", kie);
    }
    //   by the user in MainActivity
    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) || (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) || (event.getKeyCode() == KeyEvent.KEYCODE_MENU)) {
        return false;
    } else {
        return true;
    }
}
Also used : KeyInputEvent(com.jme3.input.event.KeyInputEvent) TouchEvent(com.jme3.input.event.TouchEvent)

Example 20 with TouchEvent

use of com.jme3.input.event.TouchEvent in project jmonkeyengine by jMonkeyEngine.

the class AndroidTouchInput14 method onHover.

public boolean onHover(MotionEvent event) {
    boolean consumed = false;
    int action = getAction(event);
    int pointerId = getPointerId(event);
    int pointerIndex = getPointerIndex(event);
    Vector2f lastPos = lastHoverPositions.get(pointerId);
    float jmeX;
    float jmeY;
    numPointers = event.getPointerCount();
    //        logger.log(Level.INFO, "onHover pointerId: {0}, action: {1}, x: {2}, y: {3}, numPointers: {4}",
    //                new Object[]{pointerId, action, event.getX(), event.getY(), event.getPointerCount()});
    TouchEvent touchEvent;
    switch(action) {
        case MotionEvent.ACTION_HOVER_ENTER:
            jmeX = getJmeX(event.getX(pointerIndex));
            jmeY = invertY(getJmeY(event.getY(pointerIndex)));
            touchEvent = getFreeTouchEvent();
            touchEvent.set(TouchEvent.Type.HOVER_START, jmeX, jmeY, 0, 0);
            touchEvent.setPointerId(pointerId);
            touchEvent.setTime(event.getEventTime());
            touchEvent.setPressure(event.getPressure(pointerIndex));
            lastPos = new Vector2f(jmeX, jmeY);
            lastHoverPositions.put(pointerId, lastPos);
            addEvent(touchEvent);
            consumed = true;
            break;
        case MotionEvent.ACTION_HOVER_MOVE:
            // Convert all pointers into events
            for (int p = 0; p < event.getPointerCount(); p++) {
                jmeX = getJmeX(event.getX(p));
                jmeY = invertY(getJmeY(event.getY(p)));
                lastPos = lastHoverPositions.get(event.getPointerId(p));
                if (lastPos == null) {
                    lastPos = new Vector2f(jmeX, jmeY);
                    lastHoverPositions.put(event.getPointerId(p), lastPos);
                }
                float dX = jmeX - lastPos.x;
                float dY = jmeY - lastPos.y;
                if (dX != 0 || dY != 0) {
                    touchEvent = getFreeTouchEvent();
                    touchEvent.set(TouchEvent.Type.HOVER_MOVE, jmeX, jmeY, dX, dY);
                    touchEvent.setPointerId(event.getPointerId(p));
                    touchEvent.setTime(event.getEventTime());
                    touchEvent.setPressure(event.getPressure(p));
                    lastPos.set(jmeX, jmeY);
                    addEvent(touchEvent);
                }
            }
            consumed = true;
            break;
        case MotionEvent.ACTION_HOVER_EXIT:
            jmeX = getJmeX(event.getX(pointerIndex));
            jmeY = invertY(getJmeY(event.getY(pointerIndex)));
            touchEvent = getFreeTouchEvent();
            touchEvent.set(TouchEvent.Type.HOVER_END, jmeX, jmeY, 0, 0);
            touchEvent.setPointerId(pointerId);
            touchEvent.setTime(event.getEventTime());
            touchEvent.setPressure(event.getPressure(pointerIndex));
            lastHoverPositions.remove(pointerId);
            addEvent(touchEvent);
            consumed = true;
            break;
        default:
            consumed = false;
            break;
    }
    return consumed;
}
Also used : Vector2f(com.jme3.math.Vector2f) TouchEvent(com.jme3.input.event.TouchEvent)

Aggregations

TouchEvent (com.jme3.input.event.TouchEvent)20 Vector2f (com.jme3.math.Vector2f)4 InputEvent (com.jme3.input.event.InputEvent)2 KeyInputEvent (com.jme3.input.event.KeyInputEvent)2 MouseButtonEvent (com.jme3.input.event.MouseButtonEvent)2 MouseMotionEvent (com.jme3.input.event.MouseMotionEvent)2