Gondwana Game Engine Development (master) - 2.6.1
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) return;
72 foreach (var t in e.ChangedTouches)
73 {
74 var point = new GondwanaTouchPoint((int)t.Identifier, GetPosition(t), TouchPhase.Began);
75 _activeTouches[t.Identifier] = point;
76 _pendingBegins.Enqueue(point);
77 }
78 RebuildSnapshot();
79 }
80
81 private void OnTouchMove(BrowserTouchEventArgs e)
82 {
83 if (_isDisposed) return;
84 foreach (var t in e.ChangedTouches)
85 {
86 if (!_activeTouches.ContainsKey(t.Identifier)) continue;
87 var point = new GondwanaTouchPoint((int)t.Identifier, GetPosition(t), TouchPhase.Moved);
88 _activeTouches[t.Identifier] = point;
89 }
90 RebuildSnapshot();
91 }
92
93 private void OnTouchEnd(BrowserTouchEventArgs e)
94 {
95 if (_isDisposed) return;
96 foreach (var t in e.ChangedTouches)
97 {
98 if (!_activeTouches.ContainsKey(t.Identifier)) continue;
99 var point = new GondwanaTouchPoint((int)t.Identifier, GetPosition(t), TouchPhase.Ended);
100 _activeTouches.Remove(t.Identifier);
101 _pendingEnds.Enqueue(point);
102 }
103 RebuildSnapshot();
104 }
105
106 private void OnTouchCancel(BrowserTouchEventArgs e)
107 {
108 if (_isDisposed) return;
109 foreach (var t in e.ChangedTouches)
110 {
111 if (!_activeTouches.TryGetValue(t.Identifier, out var existing)) continue;
112 var point = new GondwanaTouchPoint(existing.Id, existing.Position, TouchPhase.Cancelled);
113 _activeTouches.Remove(t.Identifier);
114 _pendingEnds.Enqueue(point);
115 }
116 RebuildSnapshot();
117 }
118
120 public IReadOnlyList<GondwanaTouchPoint> ConsumeBeganTouches()
121 {
122 if (_pendingBegins.IsEmpty)
123 return Array.Empty<GondwanaTouchPoint>();
124
125 var snapshot = new List<GondwanaTouchPoint>(_pendingBegins.Count);
126 while (_pendingBegins.TryDequeue(out var point))
127 snapshot.Add(point);
128 return snapshot;
129 }
130
132 public IReadOnlyList<GondwanaTouchPoint> ConsumeEndedTouches()
133 {
134 if (_pendingEnds.IsEmpty)
135 return Array.Empty<GondwanaTouchPoint>();
136
137 var snapshot = new List<GondwanaTouchPoint>(_pendingEnds.Count);
138 while (_pendingEnds.TryDequeue(out var point))
139 snapshot.Add(point);
140 return snapshot;
141 }
142
143 private void RebuildSnapshot()
144 {
145 _activeTouchesSnapshot = _activeTouches.Values.ToArray();
146 }
147
148 private Point GetPosition(BrowserTouchPoint t) =>
149 _component.ToScreenPx(t.ClientX, t.ClientY);
150
152 public void Dispose()
153 {
154 if (_isDisposed) return;
155 _isDisposed = true;
156
157 _component.TouchStart -= OnTouchStart;
158 _component.TouchMove -= OnTouchMove;
159 _component.TouchEnd -= OnTouchEnd;
160 _component.TouchCancel -= OnTouchCancel;
161
162 Engine.Logger.LogInformation("BlazorTouchAdapter disposed.");
163 }
164}
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