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
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) return false;
72 return Volatile.Read(ref _down[keyCode]) != 0;
73 }
74
81 public static int? GetKeyCodeFromString(string keyName)
82 {
83 if (Enum.TryParse<BlazorKey>(keyName, true, out var key) && key != BlazorKey.None)
84 return (int)key;
85
86 Engine.Logger.LogWarning("Invalid Blazor key name: {KeyName}", keyName);
87 return null;
88 }
89
90 private void OnKeyDown(KeyboardEventArgs e)
91 {
92 if (_isDisposed) return;
93 SetDown(e.Code, true);
94 UpdateMods(e);
95 }
96
97 private void OnKeyUp(KeyboardEventArgs e)
98 {
99 if (_isDisposed) return;
100 SetDown(e.Code, false);
101 UpdateMods(e);
102 }
103
104 [MethodImpl(MethodImplOptions.AggressiveInlining)]
105 private void SetDown(string code, bool down)
106 {
107 if (!Enum.TryParse<BlazorKey>(code, false, out var key) || key == BlazorKey.None) return;
108 var index = (int)key;
109 if ((uint)index >= (uint)_down.Length) return;
110 Volatile.Write(ref _down[index], down ? 1 : 0);
111 }
112
113 [MethodImpl(MethodImplOptions.AggressiveInlining)]
114 private void UpdateMods(KeyboardEventArgs e)
115 {
116 int mods = 0;
117 if (e.ShiftKey) mods |= (int)KeyboardModifierState.Shift;
118 if (e.CtrlKey) mods |= (int)KeyboardModifierState.Ctrl;
119 if (e.AltKey) mods |= (int)KeyboardModifierState.Alt;
120 Volatile.Write(ref _modsBits, mods);
121 }
122
124 public void Dispose()
125 {
126 if (_isDisposed) return;
127 _isDisposed = true;
128
129 _component.KeyDown -= OnKeyDown;
130 _component.KeyUp -= OnKeyUp;
131
132 Engine.Logger.LogInformation("BlazorKeyboardAdapter disposed.");
133 }
134}
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 ...