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
MouseEventPoller.cs
1using System.Drawing;
3
5
12public sealed class MouseEventPoller : IDisposable
13{
19 public static MouseEventPoller? Instance { get; private set; }
20
36 public static void Initialize(IMouseAdapter adapter, MouseEventConfiguration? mouseEventConfiguration = null)
37 {
38 ArgumentNullException.ThrowIfNull(adapter);
39
40 if (mouseEventConfiguration == null)
41 {
42 mouseEventConfiguration = new MouseEventConfiguration(
43 trackMouseMovement: true,
44 secondsBetweenEvents: Engine.Instance.Configuration.TimeBetweenMouseEvents);
45 }
46
47 Instance?.Dispose();
48 Instance = new MouseEventPoller(adapter, mouseEventConfiguration);
49 }
50
52 public static void Reset()
53 {
54 Instance?.Dispose();
55 Instance = null;
56 }
57
65 public event Action<MouseEventArgs>? MouseEvent;
66
67 private readonly Dictionary<MouseButton, MouseButtonState> _buttonStates = new();
68 private Point _lastPosition = new(0, 0);
69 private int _lastScrollDelta;
70 private bool _isDisposed;
71
77 public IMouseAdapter? Adapter { get; private set; }
78
83 public MouseEventConfiguration? Configuration { get; private set; }
84
91 public Point CurrentPosition => Adapter?.CurrentPosition ?? default;
92
99 Adapter?.CurrentKeyboardModifiers ?? KeyboardModifierState.None;
100
107 public IReadOnlyDictionary<MouseButton, MouseButtonState> ButtonStates => _buttonStates;
108
115 public int ScrollDelta => _lastScrollDelta;
116
117 private MouseEventPoller(IMouseAdapter adapter, MouseEventConfiguration? mouseEventConfiguration)
118 {
119 Adapter = adapter;
120 Configuration = mouseEventConfiguration;
121
122 foreach (MouseButton button in Enum.GetValues(typeof(MouseButton)))
123 {
124 if (button != MouseButton.None)
125 _buttonStates[button] = new MouseButtonState();
126 }
127 }
128
150 internal void PollForEvents(long tick)
151 {
152 if (Adapter is null)
153 return;
154
155 if ((Configuration?.IsPaused ?? true) || !(Configuration?.ReadyForNextEvent(tick) ?? false))
156 return;
157
158 var currentPos = Adapter.CurrentPosition;
159 var pressed = Adapter.PressedButtons;
160 var scrollDelta = Adapter.ScrollDelta;
161
162 bool anyButtonChange = false;
163 bool isAnyButtonDown = false;
164
165 // update per-button state every cycle
166 foreach (var kvp in _buttonStates.ToList())
167 {
168 var button = kvp.Key;
169 var state = kvp.Value;
170
171 bool isCurrentlyDown = pressed.Contains(button);
172 bool justPressed = isCurrentlyDown && !state.IsDown;
173 bool justReleased = !isCurrentlyDown && state.IsDown;
174
175 state.JustPressed = justPressed;
176 state.JustReleased = justReleased;
177 state.IsDown = isCurrentlyDown;
178
179 anyButtonChange |= justPressed || justReleased;
180 _buttonStates[button] = state;
181
182 if (isCurrentlyDown)
183 isAnyButtonDown = true;
184 }
185
186 // now decide whether to emit an event
187 bool moved = (Configuration?.TrackMouseMovement ?? false) && _lastPosition != currentPos;
188 // Adapters supply accumulated movement since the last read. Equal deltas on
189 // consecutive polls are separate wheel inputs, not a held button state.
190 bool scrolled = scrollDelta != 0;
191 _lastScrollDelta = scrollDelta;
192
193 if (anyButtonChange || moved || scrolled || isAnyButtonDown)
194 {
195 MouseEvent?.Invoke(
196 new MouseEventArgs(
200 _lastPosition,
201 currentPos,
202 scrollDelta,
203 tick));
204
205 _lastPosition = currentPos;
206 Configuration!._lastEventTick = tick;
207 }
208 }
209
232 bool trackMouseMovement = true,
233 double timeBetweenEvents = -1,
234 bool isPaused = false)
235 {
236 if (timeBetweenEvents < 0)
237 timeBetweenEvents = Engine.Instance.Configuration.TimeBetweenMouseEvents;
238
239 Configuration = new MouseEventConfiguration(trackMouseMovement, timeBetweenEvents, isPaused);
240 }
241
248 public void StopMonitoringMouse() => Configuration = null;
249
251 public void Dispose()
252 {
253 if (_isDisposed)
254 return;
255
256 _isDisposed = true;
257 (Adapter as IDisposable)?.Dispose();
258 Adapter = null;
259 Configuration = null;
260
261 if (ReferenceEquals(Instance, this))
262 Instance = null;
263 }
264}
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
Represents the configuration for mouse event monitoring, including movement tracking settings and tim...
static void Reset()
Disposes and clears the current singleton instance.
int ScrollDelta
Gets the most recent scroll wheel delta value captured during the last poll. Positive values indicate...
void StartMonitoringMouse(bool trackMouseMovement=true, double timeBetweenEvents=-1, bool isPaused=false)
Starts or reconfigures mouse input monitoring with the specified settings. This method creates a new ...
static void Initialize(IMouseAdapter adapter, MouseEventConfiguration? mouseEventConfiguration=null)
Initializes the singleton instance of the MouseEventPoller with the specified mouse adapter and optio...
KeyboardModifierState CurrentKeyboardModifiers
Gets the current state of keyboard modifier keys (Shift, Ctrl, Alt) at the time of the last poll....
MouseEventConfiguration? Configuration
Gets the current mouse event configuration, which controls movement tracking, throttling intervals,...
Action< MouseEventArgs >? MouseEvent
Occurs when mouse input is detected and the configuration allows the event to be raised based on thro...
IReadOnlyDictionary< MouseButton, MouseButtonState > ButtonStates
Gets a read-only dictionary containing the current state of all monitored mouse buttons,...
static ? MouseEventPoller Instance
Gets the singleton instance of the MouseEventPoller class. This instance is created through the Initi...
void Dispose()
Releases the current platform adapter when it supports disposal.
IMouseAdapter? Adapter
Gets the mouse adapter currently in use, which provides access to the underlying mouse hardware state...
Point CurrentPosition
Gets the current position of the mouse cursor in screen or client coordinates. If no adapter is confi...
void StopMonitoringMouse()
Stops monitoring mouse input by clearing the configuration. After calling this method,...
Defines an adapter interface for accessing mouse device state, including cursor position,...
int ScrollDelta
Gets the accumulated scroll wheel delta since the last poll, measured in implementation-defined units...
HashSet< MouseButton > PressedButtons
Gets the set of mouse buttons that are currently pressed. This collection includes identifiers for al...
Point CurrentPosition
Gets the current position of the mouse cursor in screen or client coordinates, depending on the imple...
KeyboardModifierState
Represents the state of keyboard modifier keys (Shift, Ctrl, Alt) that can be pressed simultaneously ...
MouseButton
Represents mouse button identifiers that can be used individually or combined to represent multiple s...
Represents the current and transitional state of a mouse button, tracking whether the button is curre...