Skip to content

Commit

Permalink
Use GDTIM v2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico-Ciuffardi committed Dec 19, 2022
1 parent 19836a4 commit 2c4e546
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 44 deletions.
14 changes: 0 additions & 14 deletions GDTIM-v2_1_0/CustomInputEvents/InputEventSingleScreenLongPress.gd

This file was deleted.

32 changes: 32 additions & 0 deletions GDTIM-v2_1_1/CustomInputEvents/InputEventSingleScreenLongPress.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class_name InputEventSingleScreenLongPress
extends InputEventAction

var position : Vector2
var raw_gesture : RawGesture

func _init(_raw_gesture : RawGesture = null) -> void:
raw_gesture = _raw_gesture
if raw_gesture:
if !raw_gesture.presses.has(0):
print("RAW GESTURE:\n" + raw_gesture.as_text())
var rollback_res = raw_gesture.rollback_relative(1.0)
var discarded_events = rollback_res[1]
var history = "\nHISTORY:\n"
for e in discarded_events:
if e is RawGesture.Drag:
history += "D | "
else:
history += "T | "
history += e.as_text()
history +="\n"
print(history)
var error_msg_short = "Hello! we are trying to fix this bug.\nPlease help us by following the steps at the end of the output.\nThanks!"
var error_msg="Hello! we are trying to fix this bug.\nTo help us please copy the output and comment it (attached as a file) in the following issue: https://github.com/Federico-Ciuffardi/GodotTouchInputManager/issues/20\nAlso, if you can, include in that comment what version of Godot you are using, what platform you are running on, and what you were doing when the error occurred.\nThanks!"
print(error_msg)

assert(false,error_msg_short)
position = raw_gesture.presses[0].position


func as_text() -> String:
return "position=" + str(position)
18 changes: 9 additions & 9 deletions GDTIM-v2_1_0/InputManager.gd → GDTIM-v2_1_1/InputManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,16 @@ func _unhandled_input(event : InputEvent) -> void:
_handle_action(event)

func _handle_mouse_motion(event : InputEventMouseMotion) -> void:
if _mouse_event == Gesture.SINGLE_DRAG:
if raw_gesture.size() == 1 and _mouse_event == Gesture.SINGLE_DRAG:
_emit("drag", _native_drag_event(0, event.position, event.relative, event.speed))
elif _mouse_event == Gesture.MULTI_DRAG:
var offset = Vector2(5,5)
var e0 = _native_drag_event(0, event.position-offset, event.relative, event.speed)
raw_gesture._update_screen_drag(e0)
_emit("multi_drag", InputEventMultiScreenDrag.new(raw_gesture,e0))
var e1 = _native_drag_event(1, event.position+offset, event.relative, event.speed)
raw_gesture._update_screen_drag(e1)
_emit("multi_drag", InputEventMultiScreenDrag.new(raw_gesture,e1))
elif raw_gesture.size() == 2 and _mouse_event == Gesture.MULTI_DRAG:
var offset = Vector2(5,5)
var e0 = _native_drag_event(0, event.position-offset, event.relative, event.speed)
raw_gesture._update_screen_drag(e0)
var e1 = _native_drag_event(1, event.position+offset, event.relative, event.speed)
raw_gesture._update_screen_drag(e1)
_emit("multi_drag", InputEventMultiScreenDrag.new(raw_gesture,e0))
_emit("multi_drag", InputEventMultiScreenDrag.new(raw_gesture,e1))
elif _mouse_event == Gesture.TWIST:
var rel1 = event.position - _mouse_event_press_position
var rel2 = rel1 + event.relative
Expand Down
21 changes: 21 additions & 0 deletions GDTIM-v2_1_1/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Federico Ciuffardi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
118 changes: 118 additions & 0 deletions GDTIM-v2_1_1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<img src="https://i.imgur.com/HxwBAK2.png" align="right" />

# Godot Touch Input Manager
Godot Touch Input Manager (GDTIM) is an asset that improves touch input support (includes [new gestures](#supported-gestures)) in the Godot game engine. You just need to autoload a script and it will start analyzing the touch input. When a gesture is detected a Custom Input Event corresponding to the detected gesture will be created and [fed up](https://docs.godotengine.org/en/stable/classes/class_input.html#class-input-method-parse-input-event) to the Godot built in Input Event system so it triggers functions like [`_input(InputEvent event)`](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-input). There is also a signal for each gesture if you prefer using signals to the aforementioned.

This asset was ported to be added to Godot and is now a milestone for version 4.0 ([PR](https://github.com/godotengine/godot/pull/36953)).

## How to use
* Download the latest release from https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/releases
* Extract the downloaded *.zip* file somewhere in you project
* Locate the extracted `InputManager.gd`, and [Autoload](https://docs.godotengine.org/en/3.4/tutorials/scripting/singletons_autoload.html) it.
* Done! Now you can use the GodotTouchInputManager's [signals and its Custom Input Events](#supported-gestures).

## Examples
### [GodotTouchInputManager-Demo](https://github.com/Federico-Ciuffardi/GodotTouchInputManager-Demo)
![Demo](https://media.giphy.com/media/wnMStTBUdhQcnXLXpB/giphy.gif)
### [GestureControlledCamera2D](https://github.com/Federico-Ciuffardi/GestureControlledCamera2D)
![Demo](https://media.giphy.com/media/Xzdynnlx4XAqndgVe0/giphy.gif)

## Documentation

* [Supported gestures](#supported-gestures)
* [Gesture emulation](#gesture-emulation)
* [Configuration](#configuration)

### Supported gestures

| Gesture name | Signal | Custom input event / Signal arg | Description |
|----------------------------|-------------------|---------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------|
| Single finger touch | single_touch | [InputEventSingleScreenTouch](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/wiki/InputEventSingleScreenTouch) | Touch with a single finger |
| Single finger tap | single_tap | [InputEventSingleScreenTap](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/wiki/InputEventSingleScreenTap) | Fast press and release with a single finger |
| Single finger long press | single_long_press | [InputEventSingleScreenLongPress](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/wiki/InputEventSingleScreenLongPress) | Press and hold with a single finger |
| Single finger drag | single_drag | [InputEventSingleScreenDrag](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/wiki/InputEventSingleScreenDrag) | Drag with a single finger |
| Single finger swipe | single_swipe | [InputEventSingleScreenSwipe](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/wiki/InputEventSingleScreenSwipe) | Fast drag and release with a single finger |
| Multiple finger tap | multi_tap | [InputEventMultiScreenTap](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/wiki/InputEventMultiScreenTap) | Fast press and release with multiple fingers |
| Multiple finger long press | multi_long_press | [InputEventMultiScreenLongPress](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/wiki/InputEventMultiScreenLongPress) | Press and hold with multiple fingers |
| Multiple finger drag | multi_drag | [InputEventMultiScreenDrag](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/wiki/InputEventMultiScreenDrag) | Drag with multiple fingers (same direction) |
| Multiple finger swipe | multi_swipe | [InputEventMultiScreenTap](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/wiki/InputEventMultiScreenTap) | Fast drag and release with multiple fingers |
| Pinch | pinch | [InputEventScreenPinch](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/wiki/InputEventScreenPinch) | Drag with multiple fingers (inward/outward) |
| Twist | twist | [InputEventScreenTwist](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/wiki/InputEventScreenTwist) | Drag with multiple fingers (rotate) |
| Raw gesture | raw_gesture | [RawGesture](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/wiki/RawGesture) | Raw gesture state

When one of these gestures is detected a Custom Input Event corresponding to the detected gesture will be created and [fed up](https://docs.godotengine.org/en/stable/classes/class_input.html#class-input-method-parse-input-event) to the Godot built in Input Event system so it triggers functions like [`_input(InputEvent event)`](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-input).

### Gesture emulation

The gestures can be triggered by named [input actions](https://docs.godotengine.org/en/stable/tutorials/inputs/input_examples.html#inputmap) with specific names. If the input
action does not exists there is a default event that will trigger the gesture.

The following table shows the default event and the names of the input actions
that will trigger each of the gestures that can be emulated.

| Gesture name | Input action name | Default event |
|------------------------------------|-------------------------|---------------|
| Single touch | single_touch | **\*** |
| Multiple touch (2 fingers) | multi_touch | Middle click |
| Pinch (outward) | pinch_outward | Scroll up |
| Pinch (inward) | pinch_inward | Scroll down |
| Twist | twist | Right click |
| Single finger swipe (up) | single_swipe_up | w |
| Single finger swipe (up-right) | single_swipe_up_right | e |
| Single finger swipe (right) | single_swipe_right | d |
| Single finger swipe (down-right) | single_swipe_down_right | c |
| Single finger swipe (down) | single_swipe_down | x |
| Single finger swipe (down-left) | single_swipe_down_left | z |
| Single finger swipe (left) | single_swipe_left | a |
| Single finger swipe (left-up) | single_swipe_up_left | q |
| Multiple finger swipe (up) | multi_swipe_up | i |
| Multiple finger swipe (up-right) | multi_swipe_up_right | o |
| Multiple finger swipe (right) | multi_swipe_right | l |
| Multiple finger swipe (down-right) | multi_swipe_down_right | . |
| Multiple finger swipe (down) | multi_swipe_down | , |
| Multiple finger swipe (down-left) | multi_swipe_down_left | m |
| Multiple finger swipe (left) | multi_swipe_left | j |
| Multiple finger swipe (left-up) | multi_swipe_up_left | u |

**\*** There are two options to enable single finger gestures:
1. Go to **Project > Project Settings > General > Input Devices > Pointing**
and turn on *Emulate Touch From Mouse* to emulate a single finger touch with
the left click.
2. Go to **Project > Project Settings > General > Input Devices > Pointing**
and turn off both *Emulate Touch From Mouse* and *Emulate Mouse From Touch*.
Then set an input action called `single_touch`.

## Configuration

These are located in the first lines of [InputManager.gd](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/blob/master/InputManager.gd), to change them modify the
values on the script.

| Name | Default value | Description |
|--------------------------------|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| DEFAULT_BINDIGS | true | Enable or disable default events for [gesture emulation](#gesture-emulation) |
| DEBUG | false | Enable or disable debug information |
| DRAG_STARTUP_TIME | 0.2 | Seconds from the first native drag event to the first [single finger drag](#gestures-supported) custom event |
| FINGER_SIZE | 100.0 | The distance between the fingers must be less than `fingers*FINGER_SIZE` pixels for the [multiple finger tap](#supported-gestures) and [multiple finger swipe](#supported-gestures) gestures to be recognized. Setting it to `INF` removes this restriction. |
| MULTI_FINGER_RELEASE_THRESHOLD | 0.1 | All fingers must be released within `MULTI_FINGER_REALEASE_THRESHOLD` seconds before the gesture ends for the [multiple finger tap](#gestures-supported) and [multiple finger swipe](#gestures-supported) gestures to be recognized |
| TAP_TIME_LIMIT | 0.2 | The time between the first press and the last release must be less than `TAP_TIME_LIMIT` seconds for the [single finger tap](#supported-gestures) and [multiple finger tap](#supported-gestures) gestures to be recognized |
| TAP_DISTANCE_LIMIT | 25.0 | The centroid of the finger presses must differ less than `TAP_DISTANCE_LIMIT` pixels from the centroid of the finger releases for the [single finger tap](#supported-gestures) and [multiple finger tap](#supported-gestures) gestures to be recognized. |
| SWIPE_TIME_LIMIT | 0.5 | The time between the first press and the last release must be less than `SWIPE_TIME_LIMIT` seconds for the [single finger swipe](#supported-gestures) and [multiple finger swipe](#supported-gestures) gestures to be recognized. |
| SWIPE_DISTANCE_THRESHOLD | 200.0 | The centroid of the finger presses must differ by more than `SWIPE_DISTANCE_THRESHOLD` pixels from the centroid of the finger releases for the [single finger swipe](#supported-gestures) and [multiple finger swipe](#supported-gestures) gestures to be recognized. |
| LONG_PRESS_TIME_THRESHOLD | 0.75 | The fingers must press for `LONG_PRESS_TIME_THRESHOLD` seconds for [single-finger long press](#gestures-supported) and [multi-finger long press](#gestures-supported) gestures to be recognized. |
| LONG_PRESS_DISTANCE_LIMIT | 25.0 | The centroid of the finger presses must differ less than `LONG_PRESS_DISTANCE_LIMIT` pixels from the centroid of the fingers last positions for the [single finger long press](#supported-gestures) and [multiple finger long press](#supported-gestures) gestures to be recognized. |


## Versioning
Using [SemVer](http://semver.org/) for versioning. For the versions available, see the [releases](https://github.com/Federico-Ciuffardi/Godot-Touch-Input-Manager/releases).

## Authors
* Federico Ciuffardi

Feel free to append yourself here if you've made contributions.

## Note
Thank you for checking out this repository, you can send all your questions and comments to [email protected].

If you are willing to contribute in any way, please contact me.


20 changes: 14 additions & 6 deletions GDTIM-v2_1_0/RawGesture.gd → GDTIM-v2_1_1/RawGesture.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@ const Util : Object = preload("Util.gd")
class Event:
var time : float = -1 # (secs)
var index : int = -1
func as_text() -> String:
return "ind: " + str(index) + " | time: " + str(time)

class Touch:
extends Event
var position : Vector2 = Vector2.ZERO
var pressed : bool
func as_text() -> String:
return .as_text() + " | pos: " + str(position) + " | pressed: " + str(pressed)


class Drag:
extends Event
var position : Vector2 = Vector2.ZERO
var relative : Vector2 = Vector2.ZERO
var speed : Vector2 = Vector2.ZERO

func as_text() -> String:
return .as_text() + " | pos: " + str(position) + " | relative: " + str(relative)


#############
# Variables #
Expand Down Expand Up @@ -173,14 +181,14 @@ func latest_event_id(latest_time : float = -1) -> Array:

func as_text() -> String:
var txt = "presses: "
for index in presses:
txt += "("+str(index)+":"+str(presses[index].time)+") "
for e in presses.values():
txt += "\n" + e.as_text()
txt += "\ndrags: "
for index in drags:
txt += "("+str(index)+":"+str(drags[index].time)+") "
for e in drags.values():
txt += "\n" + e.as_text()
txt += "\nreleases: "
for index in releases:
txt += "("+str(index)+":"+str(releases[index].time)+") "
for e in releases.values():
txt += "\n" + e.as_text()
return txt

func _update_screen_drag(event : InputEventScreenDrag, time : float = -1) -> void:
Expand Down
File renamed without changes.
Loading

0 comments on commit 2c4e546

Please sign in to comment.