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
BlazorKeyboardAdapter.cs
1using System.Runtime.CompilerServices;
4using Microsoft.AspNetCore.Components.Web;
5using Microsoft.Extensions.Logging;
6
8
26public sealed class BlazorKeyboardAdapter : IKeyboardAdapter, IDisposable
27{
28 private static readonly int KeyArraySize = Enum.GetValues<BlazorKey>().Cast<int>().Max() + 1;
29
30 private readonly BlazorRenderSurfaceComponentBase _component;
31 private readonly int[] _down;
32 private int _modsBits;
33 private bool _isDisposed;
34
37 (KeyboardModifierState)Volatile.Read(ref _modsBits);
38
49
57 {
58 _component = component ?? throw new ArgumentNullException(nameof(component));
59 _down = new int[KeyArraySize];
60
61 _component.KeyDown += OnKeyDown;
62 _component.KeyUp += OnKeyUp;
63
64 Engine.Logger.LogInformation("BlazorKeyboardAdapter initialized.");
65 }
66
68 [MethodImpl(MethodImplOptions.AggressiveInlining)]
69 public bool IsDown(int keyCode)
70 {
71 if ((uint)keyCode >= (uint)_down.Length)
72 return false;
73
74 return Volatile.Read(ref _down[keyCode]) != 0;
75 }
76
83 public static int? GetKeyCodeFromString(string keyName)
84 {
85 if (Enum.TryParse<BlazorKey>(keyName, true, out var key) && key != BlazorKey.None)
86 return (int)key;
87
88 Engine.Logger.LogWarning("Invalid Blazor key name: {KeyName}", keyName);
89 return null;
90 }
91
92 private void OnKeyDown(KeyboardEventArgs e)
93 {
94 if (_isDisposed)
95 return;
96
97 SetDown(e.Code, true);
98 UpdateMods(e);
99 }
100
101 private void OnKeyUp(KeyboardEventArgs e)
102 {
103 if (_isDisposed)
104 return;
105
106 SetDown(e.Code, false);
107 UpdateMods(e);
108 }
109
110 [MethodImpl(MethodImplOptions.AggressiveInlining)]
111 private void SetDown(string code, bool down)
112 {
113 if (!Enum.TryParse<BlazorKey>(code, false, out var key) || key == BlazorKey.None)
114 return;
115
116 var index = (int)key;
117
118 if ((uint)index >= (uint)_down.Length)
119 return;
120
121 Volatile.Write(ref _down[index], down ? 1 : 0);
122 }
123
124 [MethodImpl(MethodImplOptions.AggressiveInlining)]
125 private void UpdateMods(KeyboardEventArgs e)
126 {
127 int mods = 0;
128
129 if (e.ShiftKey)
130 mods |= (int)KeyboardModifierState.Shift;
131
132 if (e.CtrlKey)
133 mods |= (int)KeyboardModifierState.Ctrl;
134
135 if (e.AltKey)
136 mods |= (int)KeyboardModifierState.Alt;
137
138 Volatile.Write(ref _modsBits, mods);
139 }
140
142 public void Dispose()
143 {
144 if (_isDisposed)
145 return;
146
147 _isDisposed = true;
148
149 _component.KeyDown -= OnKeyDown;
150 _component.KeyUp -= OnKeyUp;
151
152 Engine.Logger.LogInformation("BlazorKeyboardAdapter disposed.");
153 }
154}
void Dispose()
Releases all resources and removes event handlers registered by this adapter.
KeyboardModifierState CurrentKeyboardModifiers
Gets the current state of modifier keys, such as Shift, Ctrl, and Alt. Must be safe to read from the ...
BlazorKeyboardAdapter(BlazorRenderSurfaceComponentBase component)
Initializes a new instance of BlazorKeyboardAdapter for any Gondwana Blazor render surface.
BlazorKeyboardAdapter(BlazorBitmapRenderSurfaceComponent component)
Initializes a new instance of BlazorKeyboardAdapter and attaches it to the keyboard events on the spe...
static ? int GetKeyCodeFromString(string keyName)
Converts a key name (a BlazorKey member name or a browser KeyboardEvent.code string) to its integer k...
bool IsDown(int keyCode)
Returns true if the specified platform-agnostic key code is currently down. Key codes should be stabl...
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
Represents an adapter for keyboard input, providing access to the currently pressed keys and active m...
BlazorKey
Maps browser KeyboardEvent.code values to stable integer key codes used by BlazorKeyboardAdapter and ...
Definition BlazorKey.cs:15
KeyboardModifierState
Represents the state of keyboard modifier keys (Shift, Ctrl, Alt) that can be pressed simultaneously ...