arypbatista / godot-swipe-detector

Detect gestures and swipes in your game
MIT License
125 stars 15 forks source link
gesture-detection swipe

Swipe Detector for Godot Engine

Swipe Detector is a swipe detection script that monitor screen input (mouse/touch) triggering different signals informing swipe progress or swipe finalization. There are a lot of configurations available to indicate detector when to consider a movement to a swipe or to indicate the precision of the detected swipe curve. On swipe end you will obtain a SwipeGesture object that holds swipe information such as duration, speed, distance (from initial point), points, Curve2D.

It also implements some basic pattern detections, but it is still experimental.

Trail

Usage

Add the script to a node in your scene and connect to the signals. There are many options available to customize Swipe Detector's behavior.

When swipe is detected, you will receive a SwipeGesture object with all the gesture information. This includes points conforming the gesture and duration of the swipe. Read more

You can set patterns for automatic detection (experimental), see Working With Patterns.

You can get the history of all gestures through history() method on swipe detector.

Basic Example

The following is an example of a callback connected to the swiped signal. This callback instances a cloud for each point. The cloud is a custom scene, you can replace it with your own.

func swiped(gesture):
    for point in gesture.get_points():
        var cloud = Cloud.instance()
        cloud.set_pos(point)
        add_child(cloud)

See the examples folder for more examples.

Working with Patterns

There are two ways to work with patterns, you can do it from editor by adding pattern nodes to the SwipeDetector node or you can interact directly with SwipeDetector API.

See all gesture detection examples here.

Using Pattern Nodes

Pattern nodes is not a particular node type (maybe in a future) but a node grouping other nodes which represent pattern points.

For example:

Square Pattern Tree Square Pattern Render

When nesting this tree under your SwipeDetector node it will be included as a trigger pattern with the same name as the pattern node.

See the Gesture Detection Example With Patterns for pattern detection example.

Using SwipeDetector API to Add Patterns

The following example builds a square pattern and sets it as trigger pattern.

onready var swipeDetector = get_node('SwipeDetector')

func ready():
    var pattern_points = [v(0,0), v(0, 100), v(0, 200), v(100, 200), v(200, 200), v(200, 100), v(200, 0), v(100, 0)]
    var pattern_gesture = swipeDetector.points_to_pattern(pattern_points)
    swipeDetector.add_pattern_detection('SquarePattern', pattern_gesture)
    swipeDetector.connect('pattern_detected', self, 'on_pattern_detection')

# Alias for Vector2
func v(x, y):
    return Vector2(x, y)

func on_pattern_detection(pattern_name, gesture):
    if pattern_name == 'SquarePattern':
        print('Square Pattern was detected!')

You may see Gesture Detection Example where SwipeDetector API is used to set a recorded gesture as trigger pattern.

Multiple Swipe Spaces using Area2D

You can specify the area where detection occurs by attaching Area2D children to SwipeDetector. A simple use case for this would be to implement a pong game where each player swipes over the screen to control the player's paddle.

Area2D Children

If Area2D children are attached to SwipeDetector, swipes will be only detected in these specific areas and each gesture will contain information regarding the area where it was originated.

func _on_SwipeDetector_swipe_started( partial_gesture ):
    print('Swipe started on area ', partial_gesture.get_area().get_name())

If not Area2D children are attached, gestures' get_area() method will return null.

You may see Swipe Areas Example where swipe detections are performed in two different areas and a trail is rendered in different color for each of these.

Options (Exported Variables)

There are some options available:

Signals

Public API Methods & constants

Methods intended for public usage are:

Direction constants, available on addons/swipe-detector/directions.gd:

Class References

SwipeGesture

The SwipeGesture class instances store all the information gathered from gestures.

API

Methods intended for public usage are:

Future Work

Want to know what is ahead? Visit the enhancements list!