A comprehensive solution for handling touch and gesture inputs in Godot Engine using C#. The InputEventGestureHandler class provides high-level gesture detection and emits events for various touch gestures such as taps, swipes, pinches, twists, and more.
- Gesture Detection: Recognizes various touch gestures, including single and multi-finger taps, swipes, pinches, twists, drags, and long presses.
- Event Emission: Emits C# events for each recognized gesture, allowing for type-safe and straightforward integration into your game logic.
- Customizable Thresholds: Configurable constants for gesture detection thresholds and limits.
- Mouse Input Emulation: Supports gesture detection using mouse input for platforms without touch capabilities.
- Raw Gesture Data: Provides raw gesture data for advanced usage or custom gesture recognition.
- Clone or Download the
InputEventGestureHandlerfolder and include it in your Godot project. - Autoload the InputEventGestureHandler:
- In Godot, go to Project > Project Settings > GLobals.
- Click on folder icon right next to the path and select
InputEventGestureHandler.cs. - Set the Node Name to
InputEventGestureHandlerand click + Add.
- Reference the InputEventGestureHandler in your scripts where you need gesture input:
var gestureHandler = InputEventGestureHandler.Instance;
- Subscribe to the desired events emitted by the
InputEventGestureHandler(see Available Events).
To respond to gesture events, subscribe to the events emitted by the InputEventGestureHandler.
Example: Subscribing to Single Touch and Drag Events
public override void _Ready() { var gestureHandler = InputEventGestureHandler.Instance; gestureHandler.SingleTouch += OnTouch; gestureHandler.SingleDrag += OnDrag; ... }The InputEventGestureHandler emits the following C# events:
event Action<string, InputEvent> AnyGestureevent Action<InputEventSingleScreenTap> SingleTapevent Action<InputEventSingleScreenSwipe> SingleSwipeevent Action<InputEventSingleScreenTouch> SingleTouchevent Action<InputEventSingleScreenDrag> SingleDragevent Action<InputEventSingleScreenLongPress> SingleLongPressevent Action<InputEventMultiScreenTap> MultiTapevent Action<InputEventMultiScreenSwipe> MultiSwipeevent Action<InputEventMultiScreenTouch> MultiTouchevent Action<InputEventMultiScreenDrag> MultiDragevent Action<InputEventMultiScreenLongPress> MultiLongPressevent Action<InputEventScreenPinch> Pinchevent Action<InputEventScreenTwist> Twistevent Action<RawGesture> RawGestureevent Action<InputEventScreenCancel> Cancel
The InputEventGestureHandler includes configurable constants to adjust gesture detection sensitivity and thresholds:
private const float TAP_TIME_LIMIT = 0.2f; private const float TAP_DISTANCE_LIMIT = 25.0f; private const float SWIPE_TIME_LIMIT = 0.5f; private const float SWIPE_DISTANCE_THRESHOLD = 200.0f; // ... Other configuration constantsAdjust these values as needed for your game's input requirements.
Example Implementation:
public partial class PlayerController : Node { ... public override void _Ready() { var gestureHandler = InputEventGestureHandler.Instance; gestureHandler.SingleTouch += OnTouch; gestureHandler.SingleDrag += OnDrag; } private void OnTouch(InputEventSingleScreenTouch touchEvent) { ... } private void OnDrag(InputEventSingleScreenDrag dragEvent) { ... } }- Event Not Emitted: Ensure that you are subscribing to the correct event and that the event name matches exactly.
- Event Name Mismatch: Event names are case-sensitive. Use the
+=operator to subscribe to events:gestureHandler.SingleTap += OnSingleTap;
- No Gesture Recognition: Verify that the
InputEventGestureHandleris properly added as an autoload singleton and that_Inputis being called. - Null Reference Exception: Ensure that
InputEventGestureHandler.Instanceis notnulland that the handler is properly initialized.
Contributions are welcome! Please follow these steps:
- Fork the Repository
- Create a Feature Branch (
git checkout -b feature/your-feature) - Commit Your Changes (
git commit -am 'Add new feature') - Push to the Branch (
git push origin feature/your-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
By following these guidelines and adjusting your InputEventGestureHandler class accordingly, you'll ensure that your gesture handling system works smoothly with C# events, providing a robust and type-safe interface for gesture recognition in your Godot projects.
If you have any questions or need further assistance, feel free to ask!