-
Notifications
You must be signed in to change notification settings - Fork 0
Gesture detection
Touch screens lend themselves well to gesture based input. A gesture could be a pinch with two fingers to indicate the desire to zoom, a tap or double tap, a long press and so on.
Libgdx provides a GestureDetector (source) that lets you detect the following gestures:
- tap: A user touches the screen and lifts the finger again. The finger must not move outside a specified square area around the initial touch position for a tap to be registered. Multiple consecutive taps will be detected if the user performs taps within a specified time interval.
- pan: A user drags a finger across the screen. The detector will report the current touch coordinates as well as the delta between the current and previous touch positions. Useful to implement camera panning in 2D.
- fling: A user dragged the finger across the screen, then lifted it. Useful to implement swipe gestures.
- zoom: A user places two fingers on the screen and moves them together/apart. The detector will report both the initial and current distance between fingers in pixels. Useful to implement camera zooming.
- pinch: Similar to zoom. The detector will report the initial and current finger positions instead of the distance. Useful to implement camera zooming and more sophisticated gestures such as rotation.
A GestureDetector is an Event Handling in disguise. To listen for gestures, one has to implement the GestureListener interface and pass it to the constructor of the GestureDetector. The detector is then set as an InputProcessor, either on an InputMultiplexer or as the main InputProcessor:
public class MyGestureListener implements GestureListener {
@Override
public boolean touchDown (int x, int y, int pointer) {
return false;
}
@Override
public boolean tap (int x, int y, int count) {
return false;
}
@Override
public boolean longPress (int x, int y) {
return false;
}
@Override
public boolean fling (float velocityX, float velocityY) {
return false;
}
@Override
public boolean pan (int x, int y, int deltaX, int deltaY) {
return false;
}
@Override
public boolean zoom (float originalDistance, float currentDistance) {
return false;
}
@Override
public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer) {
return false;
}
}Gdx.input.setInputProcessor(new GestureDetector(new MyGestureListener()));The GestureListener can signal whether it consumed the event or wants it to be passed on to the next InputProcessor by returning either true or false respectively from its methods.
As with the events reported to a normal InputProcessor, the respective methods will be called right before the call to ApplicationListener.render() on the rendering thread.
The GestureDetector also has a second constructor that allows it to specify various parameters for gesture detection. Please refer to the Javadocs for more information.
-
Developer's Guide
- Introduction
- Goals & Features
- Community & Support
- Contributing
- Games Built with Libgdx
- Prerequisites
- Project Setup, Running & Debugging
- Third Party Services
- Working from Source
- Using libgdx with other JVM languages
- The Application Framework
- A Simple Game
- File Handling
- Preferences
- Input Handling
- Memory Management
- Audio
-
Graphics
- Configuration & Querying Graphics ??
- Fullscreen & VSync
- Continuous & Non-Continuous Rendering
- Clearing the Screen
- OpenGL ES Support * Configuration & Querying OpenGL ?? * Direct Access ?? * Utility Classes * Rendering Shapes * Textures & TextureRegions * Meshes * Shaders * Frame Buffer Objects
- 2D Graphics * SpriteBatch, TextureRegions, and Sprite * Clipping, with the use of ScissorStack * Orthographic camera * Mapping Touch Coordinates ?? * NinePatches * Bitmap Fonts * Distance field fonts * Using TextureAtlases * Pixmaps * Packing Atlases Offline * Packing Atlases at Runtime * 2D Particle Effects * Tile Maps * scene2d * scene2d.ui * Skin
- 3D Graphics ?? * Quick Start * 3D animations and skinning * Importing Blender models in LibGDX * Perspective Camera ?? * Picking ??
- Managing Your Assets
- Utilities
-
Math Utilities
- Interpolation
- Vectors, Matrices, Quaternions
- Circles, Planes, Rays, etc.
- Bounding Volumes ??
- Intersection & Overlap Testing ??
- Physics
- Tools
- Extensions
- gdx-audio ??
- gdx-freetype
- Deploying your Application
- Building Libgdx ??
- Articles
- Deprecated (May be outdated)
- The Application Framework
- The Application Life-cycle ??
- Modules Overview
- File Module
- Graphics Module
- Asset Manager
- Tutorials
- Video Tutorials
- Beginner * Hello World * My First Triangle * Mesh, Color & Texture * Projection, Viewport, & Camera * Creating and texturing 3d models in Blender for Libgdx
- Intermediate * Actions
- Misc * AdMob in Libgdx * Integrating Libgdx and the Device camera * String Builder ??
- The Application Framework