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
TouchEventPoller.cs
2
4
23public sealed class TouchEventPoller : ITouchInput, IDisposable
24{
30 public static TouchEventPoller? Instance { get; private set; }
31
47 public static void Initialize(ITouchAdapter adapter, TouchEventConfiguration? config = null)
48 {
49 ArgumentNullException.ThrowIfNull(adapter);
50
51 config ??= new TouchEventConfiguration(
52 secondsBetweenEvents: Engine.Instance.Configuration.TimeBetweenTouchEvents);
53
54 Instance?.Dispose();
55 Instance = new TouchEventPoller(adapter, config);
56 }
57
61 public static void Reset()
62 {
63 Instance?.Dispose();
64 Instance = null;
65 }
66
67 private readonly Dictionary<int, TouchPoint> _activeTouches = new();
68 private TouchPoint[] _activeTouchesSnapshot = Array.Empty<TouchPoint>();
69
71 public IReadOnlyList<TouchPoint> ActiveTouches => _activeTouchesSnapshot;
72
78 public ITouchAdapter? Adapter { get; private set; }
79
84 public TouchEventConfiguration? Configuration { get; private set; }
85
87 public event EventHandler<TouchEventArgs>? TouchBegan;
88
90 public event EventHandler<TouchEventArgs>? TouchMoved;
91
93 public event EventHandler<TouchEventArgs>? TouchEnded;
94
109 public event Action<GestureEventArgs>? TouchEvent;
110
117
124
129
130 private bool _isDisposed;
131
133 {
134 Adapter = adapter;
135 Configuration = config;
136
140 TapRecognizer.CompetingSwipeRecognizer = SwipeRecognizer;
141
142 TapRecognizer.Tapped += (_, e) => TouchEvent?.Invoke(new GestureEventArgs(e));
143 SwipeRecognizer.Swiped += (_, e) => TouchEvent?.Invoke(new GestureEventArgs(e));
144 PinchRecognizer.PinchStarted += OnPinch;
145 PinchRecognizer.PinchUpdated += OnPinch;
146 PinchRecognizer.PinchEnded += OnPinch;
147 }
148
149 private void OnPinch(object? sender, PinchedEventArgs e)
150 => TouchEvent?.Invoke(new GestureEventArgs(e));
151
179 internal void PollForEvents(long tick)
180 {
181 if (_isDisposed || Adapter is null)
182 return;
183
184 var began = Adapter.ConsumeBeganTouches();
185 var ended = Adapter.ConsumeEndedTouches();
186
187 // A paused/stopped poller deliberately establishes a new logical contact boundary.
188 // Drain transient queues, forget prior contacts, and begin fresh on resume.
189 if (Configuration is null || Configuration.IsPaused)
190 {
191 ClearContactState();
192 return;
193 }
194
195 // Lifecycle transitions are lossless and are never throttled.
196 foreach (var beganPoint in began)
197 {
198 if (_activeTouches.ContainsKey(beganPoint.Id))
199 continue;
200
201 var normalized = new TouchPoint(
202 beganPoint.Id,
203 beganPoint.Position,
204 TouchPhase.Began);
205
206 _activeTouches[normalized.Id] = normalized;
207 TouchBegan?.Invoke(this, new TouchEventArgs(normalized, tick));
208 }
209
210 foreach (var endedPoint in ended)
211 {
212 // A beginning and ending may both have occurred between engine polls. The queued
213 // beginning above ensures consumers still receive a complete lifecycle.
214 if (_activeTouches.Remove(endedPoint.Id))
215 TouchEnded?.Invoke(this, new TouchEventArgs(endedPoint, tick));
216 }
217
218 var currentActive = Adapter.ActiveTouches;
219
220 // Discover active contacts for adapters that use the default empty beginning queue.
221 foreach (var point in currentActive)
222 {
223 if (!_activeTouches.ContainsKey(point.Id))
224 {
225 var normalized = new TouchPoint(point.Id, point.Position, TouchPhase.Began);
226 _activeTouches[normalized.Id] = normalized;
227 TouchBegan?.Invoke(this, new TouchEventArgs(normalized, tick));
228 }
229 }
230
231 bool movementEmitted = false;
232
233 if (Configuration.ReadyForNextEvent(tick))
234 {
235 foreach (var point in currentActive)
236 {
237 if (!_activeTouches.TryGetValue(point.Id, out var known) ||
238 known.Position == point.Position)
239 continue;
240
241 var movedPoint = new TouchPoint(point.Id, point.Position, TouchPhase.Moved);
242 _activeTouches[point.Id] = movedPoint;
243 TouchMoved?.Invoke(this, new TouchEventArgs(movedPoint, tick));
244 movementEmitted = true;
245 }
246 }
247
248 _activeTouchesSnapshot = _activeTouches.Values.ToArray();
249
250 if (movementEmitted)
251 Configuration._lastEventTick = tick;
252 }
253
270 public void StartMonitoringTouch(double timeBetweenEvents = -1, bool isPaused = false)
271 {
272 if (timeBetweenEvents < 0)
273 timeBetweenEvents = Engine.Instance.Configuration.TimeBetweenTouchEvents;
274
275 Configuration = new TouchEventConfiguration(timeBetweenEvents, isPaused);
276 }
277
283 {
284 Configuration = null;
285 ClearContactState();
286 }
287
288 private void ClearContactState()
289 {
290 _activeTouches.Clear();
291 _activeTouchesSnapshot = Array.Empty<TouchPoint>();
292 TapRecognizer.Reset();
293 SwipeRecognizer.Reset();
294 PinchRecognizer.Reset();
295 }
296
300 public void Dispose()
301 {
302 if (_isDisposed)
303 return;
304
305 _isDisposed = true;
306
307 TapRecognizer.Dispose();
308 SwipeRecognizer.Dispose();
309 PinchRecognizer.PinchStarted -= OnPinch;
310 PinchRecognizer.PinchUpdated -= OnPinch;
311 PinchRecognizer.PinchEnded -= OnPinch;
312 PinchRecognizer.Dispose();
313
314 (Adapter as IDisposable)?.Dispose();
315 Adapter = null;
316 Configuration = null;
317 _activeTouches.Clear();
318 _activeTouchesSnapshot = Array.Empty<TouchPoint>();
319
320 if (ReferenceEquals(Instance, this))
321 Instance = null;
322 }
323}
Represents the core game engine singleton responsible for managing the main update loop,...
Definition Engine.cs:36
static Engine Instance
Gets the singleton instance of the Engine.
Definition Engine.cs:49
EngineConfiguration Configuration
Gets the current engine configuration settings.
Definition Engine.cs:952
Provides data for the TouchEventPoller.TouchEvent, carrying the details of a recognized gesture....
Recognizes two-finger pinch and spread gestures by listening to touch events from an ITouchInput sour...
Provides data for the PinchGestureRecognizer.PinchUpdated event, describing the change in scale produ...
Recognizes swipe gestures by listening to touch events from an ITouchInput source....
Recognizes single-finger tap gestures by listening to touch events from an ITouchInput source....
Provides data for touch contact events raised by ITouchInput, including the touch point and engine ti...
Represents the configuration for touch event monitoring, including timing controls for movement-event...
Provides centralized polling and event management for touch input. This singleton class monitors touc...
EventHandler< TouchEventArgs >? TouchBegan
static void Reset()
Disposes and clears the current singleton instance, including its adapter.
TouchEventConfiguration? Configuration
Gets the current touch event configuration, which controls movement-event throttling and pause state....
Action< GestureEventArgs >? TouchEvent
Occurs when any recognized gesture — tap, swipe, or pinch — is detected. Inspect GestureEventArgs....
EventHandler< TouchEventArgs >? TouchMoved
void Dispose()
Releases recognizers and the current platform adapter.
EventHandler< TouchEventArgs >? TouchEnded
PinchGestureRecognizer PinchRecognizer
Gets the pinch gesture recognizer used by this poller.
void StartMonitoringTouch(double timeBetweenEvents=-1, bool isPaused=false)
Starts or reconfigures touch input monitoring with the specified settings. This method creates a new ...
static ? TouchEventPoller Instance
Gets the singleton instance of the TouchEventPoller class. This instance is created through the Initi...
ITouchAdapter? Adapter
Gets the touch adapter currently in use, which provides access to the underlying touch state includin...
TapGestureRecognizer TapRecognizer
Gets the tap gesture recognizer used by this poller. Adjust TapGestureRecognizer.MaxTapDurationSecond...
static void Initialize(ITouchAdapter adapter, TouchEventConfiguration? config=null)
Initializes the singleton instance of the TouchEventPoller with the specified touch adapter and optio...
void StopMonitoringTouch()
Stops monitoring touch input by clearing the configuration. After calling this method,...
IReadOnlyList< TouchPoint > ActiveTouches
Gets the list of touch contact points that are currently active (in contact with the surface)....
SwipeGestureRecognizer SwipeRecognizer
Gets the swipe gesture recognizer used by this poller. Adjust SwipeGestureRecognizer....
Defines a passive adapter interface for accessing touch device state. Implementations update their in...
IReadOnlyList< TouchPoint > ConsumeEndedTouches()
Drains and returns all touch contacts that ended (lifted or cancelled) since the last call....
IReadOnlyList< TouchPoint > ConsumeBeganTouches()
Drains and returns all touch contacts that began since the last call. Keeping beginnings in a queue p...
Represents a touch input source that tracks active contact points and raises lifecycle events when fi...
readonly record struct TouchPoint(int Id, Point Position, TouchPhase Phase)
Represents a single touch contact point, capturing its unique identifier, screen position,...
TouchPhase
Describes the current phase of a single touch contact point in its lifecycle.
Definition TouchPhase.cs:7