APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.input

public Class GestureDetector

extends InputAdapter
Class Inheritance
Known Direct Subclasses
com.badlogic.gdx.graphics.g3d.utils.CameraInputController
Imports
com.badlogic.gdx.Gdx, .InputAdapter, .InputProcessor, com.badlogic.gdx.math.Vector2, com.badlogic.gdx.utils.TimeUtils, .Timer, .Timer.Task

InputProcessor implementation that detects gestures (tap, long press, fling, pan, zoom, pinch) and hands them to a GestureListener.
Author
mzechner

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
GestureDetector.GestureAdapter

Derrive from this if you only want to implement a subset of GestureListener.

public static interface
GestureDetector.GestureListener

Register an instance of this class with a GestureDetector to receive gestures such as taps, long presses, flings, panning or pinch zooming.

pack-priv static class

Field Summary

Modifier and TypeField and Description
private final Vector2
private final Vector2
private boolean
private int
private int
private long
private float
private float
pack-priv final GestureDetector.GestureListener
pack-priv boolean
private float
private final Timer.Task
private long
private boolean
private boolean
pack-priv Vector2
private final Vector2
private int
private long
private float
private float
private float
private float
private long
private final GestureDetector.VelocityTracker

Constructor Summary

AccessConstructor and Description
public
GestureDetector(GestureDetector.GestureListener listener)

Creates a new GestureDetector with default values: halfTapSquareSize=20, tapCountInterval=0.4f, longPressDuration=1.1f, maxFlingDelay=Integer.MAX_VALUE.

public
GestureDetector(float
half width in pixels of the square around an initial touch event, see GestureListener#tap(float, float, int, int).
halfTapSquareSize
,
float
time in seconds that must pass for two touch down/up sequences to be detected as consecutive taps.
tapCountInterval
,
float
time in seconds that must pass for the detector to fire a GestureListener#longPress(float, float) event.
longPressDuration
,
float
no fling event is fired when the time in seconds the finger was dragged is larger than this, see GestureListener#fling(float, float, int)
maxFlingDelay
,
GestureDetector.GestureListener listener)

public
GestureDetector(float
half width in pixels of the rectangle around an initial touch event, see GestureListener#tap(float, float, int, int).
halfTapRectangleWidth
,
float
half height in pixels of the rectangle around an initial touch event, see GestureListener#tap(float, float, int, int).
halfTapRectangleHeight
,
float
time in seconds that must pass for two touch down/up sequences to be detected as consecutive taps.
tapCountInterval
,
float
time in seconds that must pass for the detector to fire a GestureListener#longPress(float, float) event.
longPressDuration
,
float
no fling event is fired when the time in seconds the finger was dragged is larger than this, see GestureListener#fling(float, float, int)
maxFlingDelay
,
GestureDetector.GestureListener listener)

Method Summary

Modifier and TypeMethod and Description
public void
cancel()

No further gesture events will be triggered for the current touch, if any.

public void
invalidateTapSquare()

The tap square will no longer be used for the current touch.

public boolean
isLongPressed()

Returns: whether the user touched the screen long enough to trigger a long press event.

public boolean
isLongPressed(float duration)

Returns: whether the user touched the screen for as much or more than the given duration.

public boolean
private boolean
isWithinTapRectangle(float x, float y, float centerX, float centerY)

public void
public void
setLongPressSeconds(float longPressSeconds)

public void
setMaxFlingDelay(long maxFlingDelay)

public void
setTapCountInterval(float
time in seconds that must pass for two touch down/up sequences to be detected as consecutive taps.
tapCountInterval
)

public void
setTapRectangleSize(float halfTapRectangleWidth, float halfTapRectangleHeight)

public void
setTapSquareSize(float halfTapSquareSize)

public boolean
touchCancelled(int screenX, int screenY, int pointer, int button)

Overrides com.badlogic.gdx.InputAdapter.touchCancelled.

Implements com.badlogic.gdx.InputProcessor.touchCancelled.

Called when the touch gesture is cancelled.

public boolean
touchDown(int x, int y, int pointer, int button)

Overrides com.badlogic.gdx.InputAdapter.touchDown.

Implements com.badlogic.gdx.InputProcessor.touchDown.

Called when the screen was touched or a mouse button was pressed.

public boolean
touchDown(float x, float y, int pointer, int button)

public boolean
touchDragged(int x, int y, int pointer)

Overrides com.badlogic.gdx.InputAdapter.touchDragged.

Implements com.badlogic.gdx.InputProcessor.touchDragged.

Called when a finger or the mouse was dragged.

public boolean
touchDragged(float x, float y, int pointer)

public boolean
touchUp(int x, int y, int pointer, int button)

Overrides com.badlogic.gdx.InputAdapter.touchUp.

Implements com.badlogic.gdx.InputProcessor.touchUp.

Called when a finger was lifted or a mouse button was released.

public boolean
touchUp(float x, float y, int pointer, int button)

Inherited from com.badlogic.gdx.InputAdapter:
keyDownkeyTypedkeyUpmouseMovedscrolled

Field Detail

initialPointer1back to summary
private final Vector2 initialPointer1
initialPointer2back to summary
private final Vector2 initialPointer2
inTapRectangleback to summary
private boolean inTapRectangle
lastTapButtonback to summary
private int lastTapButton
lastTapPointerback to summary
private int lastTapPointer
lastTapTimeback to summary
private long lastTapTime
lastTapXback to summary
private float lastTapX
lastTapYback to summary
private float lastTapY
listenerback to summary
pack-priv final GestureDetector.GestureListener listener
longPressFiredback to summary
pack-priv boolean longPressFired
longPressSecondsback to summary
private float longPressSeconds
longPressTaskback to summary
private final Timer.Task longPressTask
maxFlingDelayback to summary
private long maxFlingDelay
panningback to summary
private boolean panning
pinchingback to summary
private boolean pinching
pointer1back to summary
pack-priv Vector2 pointer1
pointer2back to summary
private final Vector2 pointer2
tapCountback to summary
private int tapCount
tapCountIntervalback to summary
private long tapCountInterval
tapRectangleCenterXback to summary
private float tapRectangleCenterX
tapRectangleCenterYback to summary
private float tapRectangleCenterY
tapRectangleHeightback to summary
private float tapRectangleHeight
tapRectangleWidthback to summary
private float tapRectangleWidth
touchDownTimeback to summary
private long touchDownTime
trackerback to summary
private final GestureDetector.VelocityTracker tracker

Constructor Detail

GestureDetectorback to summary
public GestureDetector(GestureDetector.GestureListener listener)

Creates a new GestureDetector with default values: halfTapSquareSize=20, tapCountInterval=0.4f, longPressDuration=1.1f, maxFlingDelay=Integer.MAX_VALUE.

GestureDetectorback to summary
public GestureDetector(float halfTapSquareSize, float tapCountInterval, float longPressDuration, float maxFlingDelay, GestureDetector.GestureListener listener)
Parameters
halfTapSquareSize:float

half width in pixels of the square around an initial touch event, see GestureListener#tap(float, float, int, int).

tapCountInterval:float

time in seconds that must pass for two touch down/up sequences to be detected as consecutive taps.

longPressDuration:float

time in seconds that must pass for the detector to fire a GestureListener#longPress(float, float) event.

maxFlingDelay:float

no fling event is fired when the time in seconds the finger was dragged is larger than this, see GestureListener#fling(float, float, int)

GestureDetectorback to summary
public GestureDetector(float halfTapRectangleWidth, float halfTapRectangleHeight, float tapCountInterval, float longPressDuration, float maxFlingDelay, GestureDetector.GestureListener listener)
Parameters
halfTapRectangleWidth:float

half width in pixels of the rectangle around an initial touch event, see GestureListener#tap(float, float, int, int).

halfTapRectangleHeight:float

half height in pixels of the rectangle around an initial touch event, see GestureListener#tap(float, float, int, int).

tapCountInterval:float

time in seconds that must pass for two touch down/up sequences to be detected as consecutive taps.

longPressDuration:float

time in seconds that must pass for the detector to fire a GestureListener#longPress(float, float) event.

maxFlingDelay:float

no fling event is fired when the time in seconds the finger was dragged is larger than this, see GestureListener#fling(float, float, int)

Method Detail

cancelback to summary
public void cancel()

No further gesture events will be triggered for the current touch, if any.

invalidateTapSquareback to summary
public void invalidateTapSquare()

The tap square will no longer be used for the current touch.

isLongPressedback to summary
public boolean isLongPressed()
Returns:boolean

whether the user touched the screen long enough to trigger a long press event.

isLongPressedback to summary
public boolean isLongPressed(float duration)
Returns:boolean

whether the user touched the screen for as much or more than the given duration.

isPanningback to summary
public boolean isPanning()
isWithinTapRectangleback to summary
private boolean isWithinTapRectangle(float x, float y, float centerX, float centerY)
resetback to summary
public void reset()
setLongPressSecondsback to summary
public void setLongPressSeconds(float longPressSeconds)
setMaxFlingDelayback to summary
public void setMaxFlingDelay(long maxFlingDelay)
setTapCountIntervalback to summary
public void setTapCountInterval(float tapCountInterval)
Parameters
tapCountInterval:float

time in seconds that must pass for two touch down/up sequences to be detected as consecutive taps.

setTapRectangleSizeback to summary
public void setTapRectangleSize(float halfTapRectangleWidth, float halfTapRectangleHeight)
setTapSquareSizeback to summary
public void setTapSquareSize(float halfTapSquareSize)
touchCancelledback to summary
public boolean touchCancelled(int screenX, int screenY, int pointer, int button)

Overrides com.badlogic.gdx.InputAdapter.touchCancelled.

Implements com.badlogic.gdx.InputProcessor.touchCancelled.

Doc from com.badlogic.gdx.InputProcessor.touchCancelled.

Called when the touch gesture is cancelled. Reason may be from OS interruption to touch becoming a large surface such as the user cheek). Relevant on Android and iOS only. The button parameter will be Buttons#LEFT on iOS.

Annotations
@Override
touchDownback to summary
public boolean touchDown(int x, int y, int pointer, int button)

Overrides com.badlogic.gdx.InputAdapter.touchDown.

Implements com.badlogic.gdx.InputProcessor.touchDown.

Doc from com.badlogic.gdx.InputProcessor.touchDown.

Called when the screen was touched or a mouse button was pressed. The button parameter will be Buttons#LEFT on iOS.

Annotations
@Override
touchDownback to summary
public boolean touchDown(float x, float y, int pointer, int button)
touchDraggedback to summary
public boolean touchDragged(int x, int y, int pointer)

Overrides com.badlogic.gdx.InputAdapter.touchDragged.

Implements com.badlogic.gdx.InputProcessor.touchDragged.

Doc from com.badlogic.gdx.InputProcessor.touchDragged.

Called when a finger or the mouse was dragged.

Annotations
@Override
touchDraggedback to summary
public boolean touchDragged(float x, float y, int pointer)
touchUpback to summary
public boolean touchUp(int x, int y, int pointer, int button)

Overrides com.badlogic.gdx.InputAdapter.touchUp.

Implements com.badlogic.gdx.InputProcessor.touchUp.

Doc from com.badlogic.gdx.InputProcessor.touchUp.

Called when a finger was lifted or a mouse button was released. The button parameter will be Buttons#LEFT on iOS.

Annotations
@Override
touchUpback to summary
public boolean touchUp(float x, float y, int pointer, int button)
com.badlogic.gdx.input back to summary

public Class GestureDetector.GestureAdapter

extends Object
implements GestureListener
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.input.GestureDetector.GestureListener
Known Direct Subclasses
com.badlogic.gdx.graphics.g3d.utils.CameraInputController.CameraGestureListener

Derrive from this if you only want to implement a subset of GestureListener.
Author
mzechner

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
fling(float velocityX, float velocityY, int button)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.fling.

Called when the user dragged a finger over the screen and lifted it.

public boolean
public boolean
pan(float x, float y, float deltaX, float deltaY)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.pan.

Called when the user drags a finger over the screen.

public boolean
panStop(float x, float y, int pointer, int button)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.panStop.

Called when no longer panning.

public boolean
pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.pinch.

Called when a user performs a pinch zoom gesture.

public void
public boolean
tap(float x, float y, int count, int button)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.tap.

Called when a tap occured.

public boolean
touchDown(float x, float y, int pointer, int button)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.touchDown.

public boolean
zoom(float initialDistance, float distance)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.zoom.

Called when the user performs a pinch zoom gesture.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

GestureAdapterback to summary
public GestureAdapter()

Method Detail

flingback to summary
public boolean fling(float velocityX, float velocityY, int button)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.fling.

Doc from com.badlogic.gdx.input.GestureDetector.GestureListener.fling.

Called when the user dragged a finger over the screen and lifted it. Reports the last known velocity of the finger in pixels per second.

Annotations
@Override
longPressback to summary
public boolean longPress(float x, float y)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.longPress.

Annotations
@Override
panback to summary
public boolean pan(float x, float y, float deltaX, float deltaY)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.pan.

Doc from com.badlogic.gdx.input.GestureDetector.GestureListener.pan.

Called when the user drags a finger over the screen.

Annotations
@Override
panStopback to summary
public boolean panStop(float x, float y, int pointer, int button)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.panStop.

Doc from com.badlogic.gdx.input.GestureDetector.GestureListener.panStop.

Called when no longer panning.

Annotations
@Override
pinchback to summary
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.pinch.

Doc from com.badlogic.gdx.input.GestureDetector.GestureListener.pinch.

Called when a user performs a pinch zoom gesture. Reports the initial positions of the two involved fingers and their current positions.

Annotations
@Override
pinchStopback to summary
public void pinchStop()

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.pinchStop.

Doc from com.badlogic.gdx.input.GestureDetector.GestureListener.pinchStop.

Called when no longer pinching.

Annotations
@Override
tapback to summary
public boolean tap(float x, float y, int count, int button)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.tap.

Doc from com.badlogic.gdx.input.GestureDetector.GestureListener.tap.

Called when a tap occured. A tap happens if a touch went down on the screen and was lifted again without moving outside of the tap square. The tap square is a rectangular area around the initial touch position as specified on construction time of the GestureDetector.

Annotations
@Override
touchDownback to summary
public boolean touchDown(float x, float y, int pointer, int button)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.touchDown.

Annotations
@Override
zoomback to summary
public boolean zoom(float initialDistance, float distance)

Implements com.badlogic.gdx.input.GestureDetector.GestureListener.zoom.

Doc from com.badlogic.gdx.input.GestureDetector.GestureListener.zoom.

Called when the user performs a pinch zoom gesture. The original distance is the distance in pixels when the gesture started.

Annotations
@Override
com.badlogic.gdx.input back to summary

public Interface GestureDetector.GestureListener

Known Direct Implementers
com.badlogic.gdx.input.GestureDetector.GestureAdapter

Register an instance of this class with a GestureDetector to receive gestures such as taps, long presses, flings, panning or pinch zooming. Each method returns a boolean indicating if the event should be handed to the next listener (false to hand it to the next listener, true otherwise).
Author
mzechner

Method Summary

Modifier and TypeMethod and Description
public boolean
fling(float
velocity on x in seconds
velocityX
,
float
velocity on y in seconds
velocityY
,
int button)

Called when the user dragged a finger over the screen and lifted it.

public boolean
longPress(float x, float y)

public boolean
pan(float x, float y, float
the difference in pixels to the last drag event on x.
deltaX
,
float
the difference in pixels to the last drag event on y.
deltaY
)

Called when the user drags a finger over the screen.

public boolean
panStop(float x, float y, int pointer, int button)

Called when no longer panning.

public boolean
pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2)

Called when a user performs a pinch zoom gesture.

public void
pinchStop()

Called when no longer pinching.

public boolean
tap(float x, float y, int
the number of taps.
count
,
int button)

Called when a tap occured.

public boolean
touchDown(float x, float y, int pointer, int button)

public boolean
zoom(float
distance between fingers when the gesture started.
initialDistance
,
float
current distance between fingers.
distance
)

Called when the user performs a pinch zoom gesture.

Method Detail

flingback to summary
public boolean fling(float velocityX, float velocityY, int button)

Called when the user dragged a finger over the screen and lifted it. Reports the last known velocity of the finger in pixels per second.

Parameters
velocityX:float

velocity on x in seconds

velocityY:float

velocity on y in seconds

longPressback to summary
public boolean longPress(float x, float y)
panback to summary
public boolean pan(float x, float y, float deltaX, float deltaY)

Called when the user drags a finger over the screen.

Parameters
deltaX:float

the difference in pixels to the last drag event on x.

deltaY:float

the difference in pixels to the last drag event on y.

panStopback to summary
public boolean panStop(float x, float y, int pointer, int button)

Called when no longer panning.

pinchback to summary
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2)

Called when a user performs a pinch zoom gesture. Reports the initial positions of the two involved fingers and their current positions.

pinchStopback to summary
public void pinchStop()

Called when no longer pinching.

tapback to summary
public boolean tap(float x, float y, int count, int button)

Called when a tap occured. A tap happens if a touch went down on the screen and was lifted again without moving outside of the tap square. The tap square is a rectangular area around the initial touch position as specified on construction time of the GestureDetector.

Parameters
count:int

the number of taps.

touchDownback to summary
public boolean touchDown(float x, float y, int pointer, int button)
See Also
InputProcessor#touchDown(int, int, int, int)
zoomback to summary
public boolean zoom(float initialDistance, float distance)

Called when the user performs a pinch zoom gesture. The original distance is the distance in pixels when the gesture started.

Parameters
initialDistance:float

distance between fingers when the gesture started.

distance:float

current distance between fingers.

com.badlogic.gdx.input back to summary

pack-priv Class GestureDetector.VelocityTracker

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv float
pack-priv float
pack-priv long
pack-priv float
pack-priv float
pack-priv long[]
pack-priv float[]
pack-priv float[]
pack-priv int
pack-priv int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private float
getAverage(float[] values, int numSamples)

private long
getAverage(long[] values, int numSamples)

private float
getSum(float[] values, int numSamples)

public float
public float
public void
start(float x, float y, long timeStamp)

public void
update(float x, float y, long currTime)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

deltaXback to summary
pack-priv float deltaX
deltaYback to summary
pack-priv float deltaY
lastTimeback to summary
pack-priv long lastTime
lastXback to summary
pack-priv float lastX
lastYback to summary
pack-priv float lastY
meanTimeback to summary
pack-priv long[] meanTime
meanXback to summary
pack-priv float[] meanX
meanYback to summary
pack-priv float[] meanY
numSamplesback to summary
pack-priv int numSamples
sampleSizeback to summary
pack-priv int sampleSize

Constructor Detail

VelocityTrackerback to summary
pack-priv VelocityTracker()

Method Detail

getAverageback to summary
private float getAverage(float[] values, int numSamples)
getAverageback to summary
private long getAverage(long[] values, int numSamples)
getSumback to summary
private float getSum(float[] values, int numSamples)
getVelocityXback to summary
public float getVelocityX()
getVelocityYback to summary
public float getVelocityY()
startback to summary
public void start(float x, float y, long timeStamp)
updateback to summary
public void update(float x, float y, long currTime)