Skip to content

Commit

Permalink
InputListener touchDragged and touchUp now return boolean.
Browse files Browse the repository at this point in the history
Sorry for the breakage. In most cases you return true from touchDown and always want touchDragged and touchUp to be handled, but sometimes you don't, such as ClickListener.
closes #1481
  • Loading branch information
NathanSweet committed Mar 18, 2014
1 parent b97b34a commit ca2a863
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 31 deletions.
10 changes: 6 additions & 4 deletions gdx/src/com/badlogic/gdx/scenes/scene2d/InputListener.java
Expand Up @@ -82,15 +82,17 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
}

/** Called when a mouse button or a finger touch goes up anywhere, but only if touchDown previously returned true for the mouse
* button or touch. The touchUp event is always {@link Event#handle() handled}.
* button or touch. When true is returned, the event is {@link Event#handle() handled}.
* @see InputEvent */
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
public boolean touchUp (InputEvent event, float x, float y, int pointer, int button) {
return false;
}

/** Called when a mouse button or a finger touch is moved anywhere, but only if touchDown previously returned true for the mouse
* button or touch. The touchDragged event is always {@link Event#handle() handled}.
* button or touch. When true is returned, the event is {@link Event#handle() handled}.
* @see InputEvent */
public void touchDragged (InputEvent event, float x, float y, int pointer) {
public boolean touchDragged (InputEvent event, float x, float y, int pointer) {
return false;
}

/** Called any time the mouse is moved when a button is not down. This event only occurs on the desktop. When true is returned,
Expand Down
10 changes: 6 additions & 4 deletions gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ScrollPane.java
Expand Up @@ -143,13 +143,14 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
return false;
}

public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
if (pointer != draggingPointer) return;
public boolean touchUp (InputEvent event, float x, float y, int pointer, int button) {
if (pointer != draggingPointer) return false;
cancel();
return true;
}

public void touchDragged (InputEvent event, float x, float y, int pointer) {
if (pointer != draggingPointer) return;
public boolean touchDragged (InputEvent event, float x, float y, int pointer) {
if (pointer != draggingPointer) return false;
if (touchScrollH) {
float delta = x - lastPoint.x;
float scrollH = handlePosition + delta;
Expand All @@ -169,6 +170,7 @@ public void touchDragged (InputEvent event, float x, float y, int pointer) {
if (total != 0) setScrollPercentY(1 - ((scrollV - vScrollBounds.y) / total));
lastPoint.set(x, y);
}
return true;
}

public boolean mouseMoved (InputEvent event, float x, float y) {
Expand Down
3 changes: 2 additions & 1 deletion gdx/src/com/badlogic/gdx/scenes/scene2d/ui/SelectBox.java
Expand Up @@ -302,13 +302,14 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
return false;
}

public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
public boolean touchUp (InputEvent event, float x, float y, int pointer, int button) {
if (hit(x, y, true) == list) {
ChangeEvent changeEvent = Pools.obtain(ChangeEvent.class);
SelectBox.this.fire(changeEvent);
Pools.free(changeEvent);
hideList();
}
return true;
}
});
}
Expand Down
8 changes: 5 additions & 3 deletions gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Slider.java
Expand Up @@ -66,19 +66,21 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
return true;
}

public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
if (pointer != draggingPointer) return;
public boolean touchUp (InputEvent event, float x, float y, int pointer, int button) {
if (pointer != draggingPointer) return false;
draggingPointer = -1;
if (!calculatePositionAndValue(x, y)) {
// Fire an event on touchUp even if the value didn't change, so listeners can see when a drag ends via isDragging.
ChangeEvent changeEvent = Pools.obtain(ChangeEvent.class);
fire(changeEvent);
Pools.free(changeEvent);
}
return true;
}

public void touchDragged (InputEvent event, float x, float y, int pointer) {
public boolean touchDragged (InputEvent event, float x, float y, int pointer) {
calculatePositionAndValue(x, y);
return true;
}
});
}
Expand Down
8 changes: 5 additions & 3 deletions gdx/src/com/badlogic/gdx/scenes/scene2d/ui/SplitPane.java
Expand Up @@ -93,12 +93,13 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
return false;
}

public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
public boolean touchUp (InputEvent event, float x, float y, int pointer, int button) {
if (pointer == draggingPointer) draggingPointer = -1;
return true;
}

public void touchDragged (InputEvent event, float x, float y, int pointer) {
if (pointer != draggingPointer) return;
public boolean touchDragged (InputEvent event, float x, float y, int pointer) {
if (pointer != draggingPointer) return false;

Drawable handle = style.handle;
if (!vertical) {
Expand All @@ -125,6 +126,7 @@ public void touchDragged (InputEvent event, float x, float y, int pointer) {
lastPoint.set(x, y);
}
invalidate();
return true;
}
});
}
Expand Down
6 changes: 4 additions & 2 deletions gdx/src/com/badlogic/gdx/scenes/scene2d/ui/TextField.java
Expand Up @@ -716,14 +716,16 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
return true;
}

public void touchDragged (InputEvent event, float x, float y, int pointer) {
public boolean touchDragged (InputEvent event, float x, float y, int pointer) {
super.touchDragged(event, x, y, pointer);
setCursorPosition(x, y);
return true;
}

public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
public boolean touchUp (InputEvent event, float x, float y, int pointer, int button) {
if (selectionStart == cursor) hasSelection = false;
super.touchUp(event, x, y, pointer, button);
return true;
}

protected void setCursorPosition (float x, float y) {
Expand Down
6 changes: 4 additions & 2 deletions gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Touchpad.java
Expand Up @@ -75,14 +75,16 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
}

@Override
public void touchDragged (InputEvent event, float x, float y, int pointer) {
public boolean touchDragged (InputEvent event, float x, float y, int pointer) {
calculatePositionAndValue(x, y, false);
return true;
}

@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
public boolean touchUp (InputEvent event, float x, float y, int pointer, int button) {
touched = false;
calculatePositionAndValue(x, y, true);
return true;
}
});
}
Expand Down
8 changes: 5 additions & 3 deletions gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Window.java
Expand Up @@ -109,12 +109,13 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
return edge != 0 || isModal;
}

public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
public boolean touchUp (InputEvent event, float x, float y, int pointer, int button) {
dragging = false;
return true;
}

public void touchDragged (InputEvent event, float x, float y, int pointer) {
if (!dragging) return;
public boolean touchDragged (InputEvent event, float x, float y, int pointer) {
if (!dragging) return false;
float width = getWidth(), height = getHeight();
float windowX = getX(), windowY = getY();

Expand Down Expand Up @@ -158,6 +159,7 @@ public void touchDragged (InputEvent event, float x, float y, int pointer) {
lastX = x;
lastY = y;
setBounds(Math.round(windowX), Math.round(windowY), Math.round(width), Math.round(height));
return true;
}

public boolean mouseMoved (InputEvent event, float x, float y) {
Expand Down
13 changes: 10 additions & 3 deletions gdx/src/com/badlogic/gdx/scenes/scene2d/utils/ClickListener.java
Expand Up @@ -19,6 +19,7 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Event;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.utils.TimeUtils;
Expand Down Expand Up @@ -61,17 +62,19 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
return true;
}

public void touchDragged (InputEvent event, float x, float y, int pointer) {
if (pointer != pressedPointer || cancelled) return;
public boolean touchDragged (InputEvent event, float x, float y, int pointer) {
if (pointer != pressedPointer || cancelled) return false;
pressed = isOver(event.getListenerActor(), x, y);
if (pressed && pointer == 0 && button != -1 && !Gdx.input.isButtonPressed(button)) pressed = false;
if (!pressed) {
// Once outside the tap square, don't use the tap square anymore.
invalidateTapSquare();
}
return false;
}

public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
public boolean touchUp (InputEvent event, float x, float y, int pointer, int button) {
boolean handled = false;
if (pointer == pressedPointer) {
if (!cancelled) {
boolean touchUpOver = isOver(event.getListenerActor(), x, y);
Expand All @@ -83,13 +86,15 @@ public void touchUp (InputEvent event, float x, float y, int pointer, int button
tapCount++;
lastTapTime = time;
clicked(event, x, y);
handled = !cancelled;
}
}
pressed = false;
pressedPointer = -1;
pressedButton = -1;
cancelled = false;
}
return handled;
}

public void enter (InputEvent event, float x, float y, int pointer, Actor fromActor) {
Expand All @@ -108,6 +113,8 @@ public void cancel () {
pressed = false;
}

/** Called when a click occurs. If called via {@link InputListener#touchUp(InputEvent, float, float, int, int) touchUp}, the
* touchUp event will be {@link Event#handle() handled}. To avoid this, call {@link #cancel()} from this method. */
public void clicked (InputEvent event, float x, float y) {
}

Expand Down
Expand Up @@ -40,8 +40,8 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
return true;
}

public void touchDragged (InputEvent event, float x, float y, int pointer) {
if (pointer != pressedPointer) return;
public boolean touchDragged (InputEvent event, float x, float y, int pointer) {
if (pointer != pressedPointer) return false;
if (!dragging && (Math.abs(touchDownX - x) > tapSquareSize || Math.abs(touchDownY - y) > tapSquareSize)) {
dragging = true;
dragStart(event, x, y, pointer);
Expand All @@ -55,13 +55,15 @@ public void touchDragged (InputEvent event, float x, float y, int pointer) {
deltaX = x;
deltaY = y;
}
return true;
}

public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
public boolean touchUp (InputEvent event, float x, float y, int pointer, int button) {
if (pointer == pressedPointer) {
if (dragging) dragStop(event, x, y, pointer);
cancel();
}
return true;
}

public void dragStart (InputEvent event, float x, float y, int pointer) {
Expand Down
3 changes: 2 additions & 1 deletion tests/gdx-tests/src/com/badlogic/gdx/tests/Scene2dTest.java
Expand Up @@ -70,8 +70,9 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
return true;
}

public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
public boolean touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("up " + event.getTarget());
return true;
}
});

Expand Down
5 changes: 3 additions & 2 deletions tests/gdx-tests/src/com/badlogic/gdx/tests/YDownTest.java
Expand Up @@ -144,9 +144,9 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
return true;
}

public void touchDragged (InputEvent event, float x, float y, int pointer) {
public boolean touchDragged (InputEvent event, float x, float y, int pointer) {
// we only care for the first finger to make things easier
if (pointer != 0) return;
if (pointer != 0) return false;

// adjust the actor's position by (current mouse position - last mouse position)
// in the actor's coordinate system.
Expand All @@ -157,6 +157,7 @@ public void touchDragged (InputEvent event, float x, float y, int pointer) {
// are in the actor's local coordinate system automatically.
lastX = x - (x - lastX);
lastY = y - (y - lastY);
return true;
}
});
}
Expand Down

0 comments on commit ca2a863

Please sign in to comment.