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
WinFormsTouchState.cs
2
4
11internal sealed class WinFormsTouchState : ITouchAdapter
12{
13 private readonly object _sync = new();
14 private readonly Dictionary<int, TouchPoint> _nativeActive = [];
15 private readonly Dictionary<int, TouchPoint> _reportedActive = [];
16 private readonly Queue<TouchPoint> _pending = [];
17 private TouchPoint[] _batchBeginnings = [];
18 private TouchPoint[] _batchEndings = [];
19 private TouchPoint[] _snapshot = [];
20
26 public IReadOnlyList<TouchPoint> ActiveTouches
27 {
28 get
29 {
30 lock (_sync)
31 return _snapshot;
32 }
33 }
34
35 internal void Begin(int id, System.Drawing.Point position)
36 {
37 lock (_sync)
38 {
39 if (_nativeActive.ContainsKey(id))
40 return;
41
42 var point = new TouchPoint(id, position, TouchPhase.Began);
43 _nativeActive.Add(id, point);
44 _pending.Enqueue(point);
45 }
46 }
47
48 internal void Move(int id, System.Drawing.Point position)
49 {
50 lock (_sync)
51 {
52 if (!_nativeActive.ContainsKey(id))
53 return;
54
55 var point = new TouchPoint(id, position, TouchPhase.Moved);
56 _nativeActive[id] = point;
57 _pending.Enqueue(point);
58 }
59 }
60
61 internal void End(int id, System.Drawing.Point position, bool cancelled = false)
62 {
63 lock (_sync)
64 {
65 if (!_nativeActive.Remove(id))
66 return;
67
68 _pending.Enqueue(
69 new TouchPoint(
70 id,
71 position,
72 cancelled ? TouchPhase.Cancelled : TouchPhase.Ended));
73 }
74 }
75
76 internal void CancelAll()
77 {
78 lock (_sync)
79 {
80 foreach (TouchPoint point in _nativeActive.Values)
81 _pending.Enqueue(point with { Phase = TouchPhase.Cancelled });
82
83 _nativeActive.Clear();
84 }
85 }
86
92 public IReadOnlyList<TouchPoint> ConsumeBeganTouches()
93 {
94 lock (_sync)
95 {
96 var beginnings = new List<TouchPoint>();
97 var endings = new List<TouchPoint>();
98 bool reachedEnding = false;
99
100 // The original poller emits all beginnings before all endings.
101 // Stop before the next beginning after any ending, so separate
102 // taps never look like simultaneous fingers. This also prevents
103 // a reused native ID being deduplicated within a single tick.
104 while (_pending.Count > 0)
105 {
106 TouchPoint next = _pending.Peek();
107
108 if (reachedEnding && next.Phase == TouchPhase.Began)
109 break;
110
111 _pending.Dequeue();
112
113 switch (next.Phase)
114 {
115 case TouchPhase.Began:
116 beginnings.Add(next);
117 _reportedActive[next.Id] = next;
118 break;
119 case TouchPhase.Ended:
120 case TouchPhase.Cancelled:
121 endings.Add(next);
122 _reportedActive.Remove(next.Id);
123 reachedEnding = true;
124 break;
125 case TouchPhase.Moved:
126 if (_reportedActive.ContainsKey(next.Id))
127 _reportedActive[next.Id] = next;
128
129 break;
130 }
131 }
132
133 _batchBeginnings = beginnings.ToArray();
134 _batchEndings = endings.ToArray();
135 _snapshot = _reportedActive.Values.ToArray();
136 return _batchBeginnings;
137 }
138 }
139
141 public IReadOnlyList<TouchPoint> ConsumeEndedTouches()
142 {
143 lock (_sync)
144 {
145 TouchPoint[] result = _batchEndings;
146 _batchEndings = [];
147 _batchBeginnings = [];
148 return result;
149 }
150 }
151
152 // Existing internal names retained for direct contact-state unit tests.
153 internal IReadOnlyList<TouchPoint> ConsumeBeginnings() => ConsumeBeganTouches();
154 internal IReadOnlyList<TouchPoint> ConsumeEndings() => ConsumeEndedTouches();
155
160 internal static bool IsPromotedTouchMouseMessage(long extraInfo)
161 {
162 uint marker = unchecked((uint)extraInfo);
163
164 return (marker & 0xFFFFFF00u) == 0xFF515700u &&
165 (marker & 0x80u) != 0;
166 }
167}
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...
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