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
GamepadEventPoller.cs
2
8public sealed class GamepadEventPoller
9{
10 private readonly Dictionary<string, Dictionary<string, GamepadButtonEventConfiguration>>
11 _configsByGamepadId = new();
12
18 public static GamepadEventPoller? Instance { get; private set; } = new();
19
20 private GamepadEventPoller()
21 {
22 }
23
24 private GamepadEventPoller(IEnumerable<IGamepadAdapter>? adapters)
25 {
26 _configsByGamepadId.Clear();
27
28 RefreshAdapters(adapters);
29 }
30
39 public static void Initialize(IEnumerable<IGamepadAdapter>? adapters)
40 {
41 Instance = new GamepadEventPoller(adapters);
42 }
43
53 internal void RefreshAdapters(IEnumerable<IGamepadAdapter>? adapters)
54 {
55 var snapshot = adapters?.ToArray();
56 Adapters = snapshot;
57
58 if (snapshot is null)
59 return;
60
61 foreach (var adapter in snapshot)
62 {
63 if (!_configsByGamepadId.ContainsKey(adapter.GamepadId))
64 _configsByGamepadId[adapter.GamepadId] = new Dictionary<string, GamepadButtonEventConfiguration>();
65 }
66 }
67
72 public IEnumerable<IGamepadAdapter>? Adapters { get; private set; }
73
80 public bool PauseAllInput { get; set; }
81
87 public event Action<GamepadButtonDownEventArgs>? ButtonDown;
88
108 internal void PollForEvents(long tick)
109 {
110 if (PauseAllInput || ButtonDown is null || Adapters is null)
111 return;
112
113 foreach (var adapter in Adapters)
114 {
115 if (!_configsByGamepadId.TryGetValue(adapter.GamepadId, out var configs))
116 continue;
117
118 foreach (var kvp in configs)
119 {
120 var button = kvp.Key;
121 var config = kvp.Value;
122
123 if (config.IsPaused || !adapter.PressedButtons.Contains(button))
124 continue;
125
126 if (config.ReadyForNextEvent(tick))
127 {
128 config._lastEventTick = tick;
129 configs[button] = config;
130
131 ButtonDown?.Invoke(new GamepadButtonDownEventArgs(config, adapter));
132 }
133 }
134 }
135 }
136
161 string gamepadId,
162 string button,
163 double timeBetweenEvents = -1,
164 bool isPaused = false)
165 {
166 if (timeBetweenEvents < 0)
167 timeBetweenEvents = Engine.Instance.Configuration.TimeBetweenGamepadEvents;
168
169 if (!_configsByGamepadId.TryGetValue(gamepadId, out var configMap))
170 {
171 configMap = new Dictionary<string, GamepadButtonEventConfiguration>();
172 _configsByGamepadId[gamepadId] = configMap;
173 }
174
175 configMap[button] = new GamepadButtonEventConfiguration(button, timeBetweenEvents, isPaused);
176 }
177
189 public void StopMonitoringButton(string gamepadId, string button)
190 {
191 if (_configsByGamepadId.TryGetValue(gamepadId, out var configMap))
192 {
193 configMap.Remove(button);
194 }
195 }
196
205 public void StopMonitoringAllButtons(string gamepadId)
206 {
207 _configsByGamepadId.Remove(gamepadId);
208 }
209
216 public IReadOnlyDictionary<string, IReadOnlyDictionary<string, GamepadButtonEventConfiguration>>
218 => _configsByGamepadId.ToDictionary(
219 entry => entry.Key,
220 entry => (IReadOnlyDictionary<string, GamepadButtonEventConfiguration>)entry.Value);
221}
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 gamepad button down events, containing information about the button configuration a...
Represents the configuration for gamepad button events, including button identification and timing co...
Provides centralized polling and event management for gamepad button inputs across multiple gamepad d...
IEnumerable< IGamepadAdapter >? Adapters
Gets the collection of gamepad adapters currently being monitored by this poller. Each adapter repres...
Action< GamepadButtonDownEventArgs >? ButtonDown
Occurs when a monitored gamepad button is pressed and the button's configuration allows the event to ...
void StartMonitoringButton(string gamepadId, string button, double timeBetweenEvents=-1, bool isPaused=false)
Registers a button on a specific gamepad for event monitoring with optional throttling and pause conf...
bool PauseAllInput
Gets or sets a value indicating whether all input processing is globally paused. When set to true,...
static ? GamepadEventPoller Instance
Gets the singleton instance of the GamepadEventPoller class. This instance is created automatically o...
static void Initialize(IEnumerable< IGamepadAdapter >? adapters)
Initializes the singleton instance of the GamepadEventPoller with the specified gamepad adapters....
void StopMonitoringAllButtons(string gamepadId)
Unregisters all buttons on a specific gamepad from event monitoring. This effectively removes all but...
void StopMonitoringButton(string gamepadId, string button)
Unregisters a specific button on a gamepad from event monitoring. After calling this method,...
IReadOnlyDictionary< string, IReadOnlyDictionary< string, GamepadButtonEventConfiguration > > AllButtonConfigsByGamepadId
Gets a read-only view of all button configurations organized by gamepad ID. The outer dictionary maps...