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
BlazorTouchAdapter.cs
1using System.Collections.Concurrent;
2using System.Drawing;
5using Microsoft.Extensions.Logging;
6using BrowserTouchEventArgs = Microsoft.AspNetCore.Components.Web.TouchEventArgs;
7using BrowserTouchPoint = Microsoft.AspNetCore.Components.Web.TouchPoint;
8using GondwanaTouchPoint = Gondwana.Input.Touch.TouchPoint;
9
11
28public sealed class BlazorTouchAdapter : ITouchAdapter, IDisposable
29{
30 private readonly BlazorRenderSurfaceComponentBase _component;
31 private readonly Dictionary<long, GondwanaTouchPoint> _activeTouches = new();
32 private GondwanaTouchPoint[] _activeTouchesSnapshot = Array.Empty<GondwanaTouchPoint>();
33 private readonly ConcurrentQueue<GondwanaTouchPoint> _pendingBegins = new();
34 private readonly ConcurrentQueue<GondwanaTouchPoint> _pendingEnds = new();
35 private bool _isDisposed;
36
38 public IReadOnlyList<GondwanaTouchPoint> ActiveTouches => _activeTouchesSnapshot;
39
50
58 {
59 _component = component ?? throw new ArgumentNullException(nameof(component));
60
61 _component.TouchStart += OnTouchStart;
62 _component.TouchMove += OnTouchMove;
63 _component.TouchEnd += OnTouchEnd;
64 _component.TouchCancel += OnTouchCancel;
65
66 Engine.Logger.LogInformation("BlazorTouchAdapter initialized.");
67 }
68
69 private void OnTouchStart(BrowserTouchEventArgs e)
70 {
71 if (_isDisposed)
72 return;
73
74 foreach (var t in e.ChangedTouches)
75 {
76 var point = new GondwanaTouchPoint((int)t.Identifier, GetPosition(t), TouchPhase.Began);
77 _activeTouches[t.Identifier] = point;
78 _pendingBegins.Enqueue(point);
79 }
80
81 RebuildSnapshot();
82 }
83
84 private void OnTouchMove(BrowserTouchEventArgs e)
85 {
86 if (_isDisposed)
87 return;
88
89 foreach (var t in e.ChangedTouches)
90 {
91 if (!_activeTouches.ContainsKey(t.Identifier))
92 continue;
93
94 var point = new GondwanaTouchPoint((int)t.Identifier, GetPosition(t), TouchPhase.Moved);
95 _activeTouches[t.Identifier] = point;
96 }
97
98 RebuildSnapshot();
99 }
100
101 private void OnTouchEnd(BrowserTouchEventArgs e)
102 {
103 if (_isDisposed)
104 return;
105
106 foreach (var t in e.ChangedTouches)
107 {
108 if (!_activeTouches.ContainsKey(t.Identifier))
109 continue;
110
111 var point = new GondwanaTouchPoint((int)t.Identifier, GetPosition(t), TouchPhase.Ended);
112 _activeTouches.Remove(t.Identifier);
113 _pendingEnds.Enqueue(point);
114 }
115
116 RebuildSnapshot();
117 }
118
119 private void OnTouchCancel(BrowserTouchEventArgs e)
120 {
121 if (_isDisposed)
122 return;
123
124 foreach (var t in e.ChangedTouches)
125 {
126 if (!_activeTouches.TryGetValue(t.Identifier, out var existing))
127 continue;
128
129 var point = new GondwanaTouchPoint(existing.Id, existing.Position, TouchPhase.Cancelled);
130 _activeTouches.Remove(t.Identifier);
131 _pendingEnds.Enqueue(point);
132 }
133
134 RebuildSnapshot();
135 }
136
138 public IReadOnlyList<GondwanaTouchPoint> ConsumeBeganTouches()
139 {
140 if (_pendingBegins.IsEmpty)
141 return Array.Empty<GondwanaTouchPoint>();
142
143 var snapshot = new List<GondwanaTouchPoint>(_pendingBegins.Count);
144
145 while (_pendingBegins.TryDequeue(out var point))
146 snapshot.Add(point);
147
148 return snapshot;
149 }
150
152 public IReadOnlyList<GondwanaTouchPoint> ConsumeEndedTouches()
153 {
154 if (_pendingEnds.IsEmpty)
155 return Array.Empty<GondwanaTouchPoint>();
156
157 var snapshot = new List<GondwanaTouchPoint>(_pendingEnds.Count);
158
159 while (_pendingEnds.TryDequeue(out var point))
160 snapshot.Add(point);
161
162 return snapshot;
163 }
164
165 private void RebuildSnapshot()
166 {
167 _activeTouchesSnapshot = _activeTouches.Values.ToArray();
168 }
169
170 private Point GetPosition(BrowserTouchPoint t) =>
171 _component.ToScreenPx(t.ClientX, t.ClientY);
172
174 public void Dispose()
175 {
176 if (_isDisposed)
177 return;
178
179 _isDisposed = true;
180
181 _component.TouchStart -= OnTouchStart;
182 _component.TouchMove -= OnTouchMove;
183 _component.TouchEnd -= OnTouchEnd;
184 _component.TouchCancel -= OnTouchCancel;
185
186 Engine.Logger.LogInformation("BlazorTouchAdapter disposed.");
187 }
188}
BlazorTouchAdapter(BlazorRenderSurfaceComponentBase component)
Initializes a new instance of BlazorTouchAdapter for any Gondwana Blazor render surface.
IReadOnlyList< GondwanaTouchPoint > ActiveTouches
Gets the list of touch contact points that are currently active (in contact with the surface)....
BlazorTouchAdapter(BlazorBitmapRenderSurfaceComponent component)
Initializes a new instance of BlazorTouchAdapter and attaches it to the touch events on the specified...
IReadOnlyList< GondwanaTouchPoint > ConsumeBeganTouches()
Drains and returns all touch contacts that began since the last call. Keeping beginnings in a queue p...
void Dispose()
Releases all resources and removes event handlers registered by this adapter.
IReadOnlyList< GondwanaTouchPoint > ConsumeEndedTouches()
Drains and returns all touch contacts that ended (lifted or cancelled) since the last call....
A Blazor component that presents Gondwana game frames on an HTML <canvas> element and forwards keyboa...
Provides the shared DOM input surface used by Gondwana's Blazor bitmap and GPU render components.
Represents the core game engine singleton responsible for managing the main update loop,...
Definition Engine.cs:36
static ILogger< Engine > Logger
Gets the logger instance used by the engine for diagnostic and informational messages.
Definition Engine.cs:59
Provides data for touch contact events raised by ITouchInput, including the touch point and engine ti...
Defines a passive adapter interface for accessing touch device state. Implementations update their in...
TouchPhase
Describes the current phase of a single touch contact point in its lifecycle.
Definition TouchPhase.cs:7