Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
SwipeGestureRecognizer.cs
1using System.Drawing;
3
5
20public sealed class SwipeGestureRecognizer : IDisposable
21{
22 private readonly ITouchInput _touchInput;
23 private readonly Dictionary<int, SwipeState> _activeSwipes = new();
24 private readonly HashSet<int> _activeContacts = new();
25 private bool _isMultiTouchSequence;
26 private bool _isDisposed;
27
33 public double MinimumSwipeSpeedPixelsPerSecond { get; set; } = 200;
34
40 public double MinimumSwipeDistancePixels { get; set; } = 30;
41
45 public event EventHandler<SwipedEventArgs>? Swiped;
46
58 {
59 _touchInput = touchInput ?? throw new ArgumentNullException(nameof(touchInput));
60 _touchInput.TouchBegan += OnTouchBegan;
61 _touchInput.TouchEnded += OnTouchEnded;
62 }
63
64 private void OnTouchBegan(object? sender, TouchEventArgs e)
65 {
66 _activeContacts.Add(e.Touch.Id);
67
68 if (_activeContacts.Count > 1)
69 {
70 _isMultiTouchSequence = true;
71 _activeSwipes.Clear();
72 return;
73 }
74
75 if (!_isMultiTouchSequence)
76 _activeSwipes[e.Touch.Id] = new SwipeState(e.Touch.Position, e.Tick);
77 }
78
79 private void OnTouchEnded(object? sender, TouchEventArgs e)
80 {
81 _activeContacts.Remove(e.Touch.Id);
82
83 if (_isMultiTouchSequence)
84 {
85 _activeSwipes.Remove(e.Touch.Id);
86
87 if (_activeContacts.Count == 0)
88 _isMultiTouchSequence = false;
89
90 return;
91 }
92
93 if (!_activeSwipes.TryGetValue(e.Touch.Id, out var state))
94 return;
95
96 _activeSwipes.Remove(e.Touch.Id);
97
98 // A system-cancelled contact must never produce a swipe.
99 if (e.Touch.Phase == TouchPhase.Cancelled)
100 return;
101
102 var endPos = e.Touch.Position;
103 var dx = endPos.X - state.StartPosition.X;
104 var dy = endPos.Y - state.StartPosition.Y;
105
106 var distance = Math.Sqrt(dx * dx + dy * dy);
107 var elapsed = HighResTimer.GetDuration(state.StartTick, e.Tick);
108
109 if (elapsed <= 0)
110 return;
111
112 var speed = distance / elapsed;
113
114 if (!WouldRecognize(distance, elapsed))
115 return;
116
117 var direction = Math.Abs(dx) >= Math.Abs(dy)
118 ? (dx >= 0 ? SwipeDirection.Right : SwipeDirection.Left)
119 : (dy >= 0 ? SwipeDirection.Down : SwipeDirection.Up);
120
121 Swiped?.Invoke(
122 this,
123 new SwipedEventArgs(
124 direction,
125 state.StartPosition,
126 endPos,
127 speed));
128 }
129
134 public void Dispose()
135 {
136 if (_isDisposed)
137 return;
138
139 _isDisposed = true;
140
141 _touchInput.TouchBegan -= OnTouchBegan;
142 _touchInput.TouchEnded -= OnTouchEnded;
143 Reset();
144 }
145
146 internal void Reset()
147 {
148 _activeSwipes.Clear();
149 _activeContacts.Clear();
150 _isMultiTouchSequence = false;
151 }
152
153 internal bool WouldRecognize(double distance, double elapsedSeconds)
154 => elapsedSeconds > 0 &&
155 distance >= MinimumSwipeDistancePixels &&
156 distance / elapsedSeconds >= MinimumSwipeSpeedPixelsPerSecond;
157
158 private readonly record struct SwipeState(Point StartPosition, long StartTick);
159}
SwipeGestureRecognizer(ITouchInput touchInput)
Initializes a new instance of the SwipeGestureRecognizer class, subscribing to touch events from the ...
double MinimumSwipeDistancePixels
Gets or sets the minimum straight-line travel distance required for a swipe. This prevents a short,...
EventHandler< SwipedEventArgs >? Swiped
Occurs when a qualifying swipe gesture is detected.
void Dispose()
Releases all resources held by this recognizer and unsubscribes from the touch input source....
double MinimumSwipeSpeedPixelsPerSecond
Gets or sets the minimum average speed, in pixels per second, that a touch must reach when it ends in...
Provides data for touch contact events raised by ITouchInput, including the touch point and engine ti...
TouchPoint Touch
Gets the touch contact point associated with this event.
long Tick
Gets the engine tick at which this touch event was emitted.
Represents a touch input source that tracks active contact points and raises lifecycle events when fi...
SwipeDirection
Specifies the direction of a swipe gesture.
@ Down
The swipe moved primarily downward (positive Y axis in screen coordinates).
@ Up
The swipe moved primarily upward (negative Y axis in screen coordinates).
TouchPhase
Describes the current phase of a single touch contact point in its lifecycle.
Definition TouchPhase.cs:7