示例#1
0
  /**
   * Check to see if the touch has moved off of the element.
   *
   * <p>NOTE that in iOS the elasticScroll may make the touch/move cancel more difficult.
   *
   * @param event
   */
  private void onTouchMove(Event event) {

    if (!this.touchMoved) {
      Touch move = null;

      for (int i = 0; i < event.getChangedTouches().length(); i++) {
        if (event.getChangedTouches().get(i).getIdentifier() == this.touchId) {
          move = event.getChangedTouches().get(i);
        }
      }

      // Check to see if we moved off of the original element

      // Use Page coordinates since we compare with widget's absolute
      // coordinates
      if (move != null) {
        int yCord = move.getPageY();
        int xCord = move.getPageX();

        // is y above element
        boolean yTop = this.getAbsoluteTop() > yCord;
        boolean yBottom = (this.getAbsoluteTop() + this.getOffsetHeight()) < yCord; // y below

        // is x to the left of element
        boolean xLeft = this.getAbsoluteLeft() > xCord;
        boolean xRight = (this.getAbsoluteLeft() + this.getOffsetWidth()) < xCord; // x to the right

        if (yTop || yBottom || xLeft || xRight) {
          this.touchMoved = true;
          onHoldPressOffStyle(); // Go back to normal style
        }
      }
    }
  }
  public MockTouchMoveEvent(int id1, int x1, int y1, int id2, int x2, int y2) {
    this(id1, x1, y1);

    final Touch touch1 = mock(Touch.class);
    when(touches.get(1)).thenReturn(touch1);
    when(touch1.getIdentifier()).thenReturn(id2);
    when(touch1.getPageX()).thenReturn(x2);
    when(touch1.getPageY()).thenReturn(y2);
  }
 public MockTouchMoveEvent(int id, int x, int y) {
   touches = mock(JsArray.class);
   touch = mock(Touch.class);
   when(touches.length()).thenReturn(1);
   when(touches.get(0)).thenReturn(touch);
   when(touch.getIdentifier()).thenReturn(id);
   when(touch.getPageX()).thenReturn(x);
   when(touch.getPageY()).thenReturn(y);
 }
  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};
  }
示例#5
0
  private void calcValue(Touch event) {

    double offsetx = event.getRelativeX(getElement()), offsety = event.getRelativeY(getElement());

    double dv;

    if (isVertical()) {
      dv = valueOfKnobPosition(offsety);
    } else {
      dv = valueOfKnobPosition(offsetx);
    }

    setValue(dv);
  }
示例#6
0
  private void onTouchStart(Event event) {

    onHoldPressDownStyle(); // Show style change

    // Stop the event from bubbling up
    event.stopPropagation();

    // Only handle if we have exactly one touch
    if (event.getTargetTouches().length() == 1) {
      Touch start = event.getTargetTouches().get(0);
      this.touchId = start.getIdentifier();
      this.touchMoved = false;
    }
  }
示例#7
0
文件: GwtInput.java 项目: QiXi/vtm
  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();
  }