protected boolean touchEvent(TouchEvent message) { boolean isConsumed = false; boolean isOutOfBounds = false; int x = message.getX(1); int y = message.getY(1); // Check to ensure point is within this field if (x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) { isOutOfBounds = true; } switch (message.getEvent()) { case TouchEvent.CLICK: case TouchEvent.MOVE: if (isOutOfBounds) return true; // consume _selected = true; // Pressed effect // update state int stateWidth = getExtent().width / _numStates; int numerator = x / stateWidth; int denominator = x % stateWidth; if (denominator > stateWidth / 2) { numerator++; } _currentState = numerator; invalidate(); isConsumed = true; break; case TouchEvent.UNCLICK: if (isOutOfBounds) { _selected = false; // Reset presssed effect return true; } // A field change notification is only sent on UNCLICK to allow for recovery // should the user cancel, i.e. click and move off the button _selected = false; // Reset pressed effect // Update state stateWidth = getExtent().width / _numStates; numerator = x / stateWidth; denominator = x % stateWidth; if (denominator > stateWidth / 2) { numerator++; } _currentState = numerator; invalidate(); fieldChangeNotify(0); isConsumed = true; break; } return isConsumed; }
/** 多点 */ public synchronized void multiTouch(List<TouchEvent> events) { if (preTouch) { touchUp(mTouchX, mTouchY); } else { performMultiPointerUp(); } Log.d(TAG, "touchUp: " + mTouchX + "," + mTouchY); if (events.isEmpty()) { return; } List<PointerCoords[]> coordsList = new ArrayList<PointerCoords[]>(); for (TouchEvent event : events) { switch (event.getAction()) { case TouchEvent.ACTION_DOWN: PointerCoords[] pointers = new PointerCoords[1]; pointers[0] = createPoint(event.getX(), event.getY()); coordsList.add(pointers); break; case TouchEvent.ACTION_UP: break; case TouchEvent.ACTION_SWIPE: break; } } PointerCoords[][] pointers = coordsList.toArray(new PointerCoords[1][2]); Log.d(TAG, "Size: " + coordsList.size()); if (coordsList.size() > 1) { performMultiPointers(pointers); performMultiPointerDown(); preTouch = false; } else if (coordsList.size() == 1) { if (coordsList.get(0) != null && coordsList.get(0).length > 0) { mTouchX = (int) pointers[0][0].x; mTouchY = (int) pointers[0][0].y; touchDown(mTouchX, mTouchY, 0); preTouch = true; Log.d(TAG, "touchDown: " + mTouchX + "," + mTouchY); } } else { Log.d(TAG, "tap: "); } }
private int[] getPositionInTouch(TouchEvent<? extends EventHandler> event) { if (event instanceof TouchEndEvent) return new int[] {previousX, previousY}; JsArray<Touch> touches = event.getTouches(); if (touches == null || touches.length() == 0) return null; // Get the first touch Touch touch = touches.get(0); int x = touch.getRelativeX(canvas.getElement()); int y = touch.getRelativeY(canvas.getElement()); return new int[] {x, y}; }
protected boolean touchEvent(TouchEvent te) { System.out.println("touchEvent type " + Integer.toString(te.getEvent())); if (te.getEvent() == TouchEvent.CANCEL) return true; int x = te.getX(1); int y = te.getY(1); if (x < 0 || x > this.getWidth() || y < 0 || y > this.getHeight()) { Logger.log( "touchedEvent type " + Integer.toString(te.getEvent()) + " x=" + Integer.toString(x) + " y=" + Integer.toString(x) + " outside " + Integer.toString(getWidth()) + "x" + Integer.toString(getHeight())); return super.touchEvent(te); } switch (te.getEvent()) { case TouchEvent.DOWN: touchDownEvent(x, y); break; case TouchEvent.UP: touchUpEvent(x, y); break; case TouchEvent.MOVE: touchMovedEvent(x, y); break; default: return super.touchEvent(te); } return true; }
/** * Dispatches touch events to touch listeners * * @param evt The touch event to be dispatched to all onTouch listeners */ public void onTouchEventQueued(TouchEvent evt) { ArrayList<Mapping> maps = bindings.get(TouchTrigger.touchHash(evt.getKeyCode())); if (maps == null) { return; } int size = maps.size(); for (int i = size - 1; i >= 0; i--) { Mapping mapping = maps.get(i); ArrayList<InputListener> listeners = mapping.listeners; int listenerSize = listeners.size(); for (int j = listenerSize - 1; j >= 0; j--) { InputListener listener = listeners.get(j); if (listener instanceof TouchListener) { ((TouchListener) listener).onTouch(mapping.name, evt, frameTPF); } } } }
protected void postTap(int x, int y) { synchronized (this) { TouchEvent event = usedTouchEvents.obtain(); event.timeStamp = System.nanoTime(); event.pointer = 0; event.x = x; event.y = y; event.type = TouchEvent.TOUCH_DOWN; touchEvents.add(event); event = usedTouchEvents.obtain(); event.timeStamp = System.nanoTime(); event.pointer = 0; event.x = x; event.y = y; event.type = TouchEvent.TOUCH_UP; touchEvents.add(event); } Gdx.app.getGraphics().requestRendering(); }
@Override public boolean onTouch(View v, MotionEvent event) { synchronized (this) { int action = event.getAction() & MotionEvent.ACTION_MASK; int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; int pointerCount = event.getPointerCount(); TouchEvent touchEvent; for (int i = 0; i < MAX_TOUCHPOINTS; i++) { if (i >= pointerCount) { isTouched[i] = false; id[i] = -1; continue; } int pointerId = event.getPointerId(i); if (event.getAction() != MotionEvent.ACTION_MOVE && i != pointerIndex) { continue; } switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: touchEvent = touchEventPool.newObject(); touchEvent.setType(TouchEvent.TOUCH_DOWN); touchEvent.setPointer(pointerId); touchEvent.setX(touchX[i] = (int) (event.getX(i) * scaleX)); touchEvent.setY(touchY[i] = (int) (event.getY(i) * scaleY)); isTouched[i] = true; id[i] = pointerId; touchEventsBuffer.add(touchEvent); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_CANCEL: touchEvent = touchEventPool.newObject(); touchEvent.setType(TouchEvent.TOUCH_UP); touchEvent.setPointer(pointerId); touchEvent.setX(touchX[i] = (int) (event.getX(i) * scaleX)); touchEvent.setY(touchY[i] = (int) (event.getY(i) * scaleY)); isTouched[i] = false; id[i] = -1; touchEventsBuffer.add(touchEvent); break; case MotionEvent.ACTION_MOVE: touchEvent = touchEventPool.newObject(); touchEvent.setType(TouchEvent.TOUCH_DRAGGED); touchEvent.setPointer(pointerId); touchEvent.setX(touchX[i] = (int) (event.getX(i) * scaleX)); touchEvent.setY(touchY[i] = (int) (event.getY(i) * scaleY)); isTouched[i] = true; id[i] = pointerId; touchEventsBuffer.add(touchEvent); break; } } return true; } }