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
GamepadEventPoller.cs
2
8public sealed class GamepadEventPoller
9{
10 private readonly Dictionary<string, Dictionary<string, GamepadButtonEventConfiguration>> _configsByGamepadId = new();
11
17 public static GamepadEventPoller? Instance { get; private set; } = new();
18
19 private GamepadEventPoller()
20 { }
21
22 private GamepadEventPoller(IEnumerable<IGamepadAdapter>? adapters)
23 {
24 _configsByGamepadId.Clear();
25
26 RefreshAdapters(adapters);
27 }
28
37 public static void Initialize(IEnumerable<IGamepadAdapter>? adapters)
38 {
39 Instance = new GamepadEventPoller(adapters);
40 }
41
51 internal void RefreshAdapters(IEnumerable<IGamepadAdapter>? adapters)
52 {
53 var snapshot = adapters?.ToArray();
54 Adapters = snapshot;
55
56 if (snapshot is null)
57 return;
58
59 foreach (var adapter in snapshot)
60 {
61 if (!_configsByGamepadId.ContainsKey(adapter.GamepadId))
62 _configsByGamepadId[adapter.GamepadId] = new Dictionary<string, GamepadButtonEventConfiguration>();
63 }
64 }
65
70 public IEnumerable<IGamepadAdapter>? Adapters { get; private set; }
71
78 public bool PauseAllInput { get; set; }
79
85 public event Action<GamepadButtonDownEventArgs>? ButtonDown;
86
106 internal void PollForEvents(long tick)
107 {
108 if (PauseAllInput || ButtonDown is null || Adapters is null) return;
109
110 foreach (var adapter in Adapters)
111 {
112 if (!_configsByGamepadId.TryGetValue(adapter.GamepadId, out var configs))
113 continue;
114
115 foreach (var kvp in configs)
116 {
117 var button = kvp.Key;
118 var config = kvp.Value;
119
120 if (config.IsPaused || !adapter.PressedButtons.Contains(button)) continue;
121
122 if (config.ReadyForNextEvent(tick))
123 {
124 config._lastEventTick = tick;
125 configs[button] = config;
126
127 ButtonDown?.Invoke(new GamepadButtonDownEventArgs(config, adapter));
128 }
129 }
130 }
131 }
132
156 public void StartMonitoringButton(string gamepadId, string button, double timeBetweenEvents = -1, bool isPaused = false)
157 {
158 if (timeBetweenEvents < 0)
159 timeBetweenEvents = Engine.Instance.Configuration.TimeBetweenGamepadEvents;
160
161 if (!_configsByGamepadId.TryGetValue(gamepadId, out var configMap))
162 {
163 configMap = new Dictionary<string, GamepadButtonEventConfiguration>();
164 _configsByGamepadId[gamepadId] = configMap;
165 }
166
167 configMap[button] = new GamepadButtonEventConfiguration(button, timeBetweenEvents, isPaused);
168 }
169
181 public void StopMonitoringButton(string gamepadId, string button)
182 {
183 if (_configsByGamepadId.TryGetValue(gamepadId, out var configMap))
184 {
185 configMap.Remove(button);
186 }
187 }
188
197 public void StopMonitoringAllButtons(string gamepadId)
198 {
199 _configsByGamepadId.Remove(gamepadId);
200 }
201
208 public IReadOnlyDictionary<string, IReadOnlyDictionary<string, GamepadButtonEventConfiguration>> AllButtonConfigsByGamepadId
209 => _configsByGamepadId.ToDictionary(
210 entry => entry.Key,
211 entry => (IReadOnlyDictionary<string, GamepadButtonEventConfiguration>)entry.Value);
212}
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:928
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...