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
AvaloniaKeyboardAdapter.cs
1using System.Runtime.CompilerServices;
2using Avalonia;
3using Avalonia.Controls;
4using Avalonia.Input;
5using Avalonia.Interactivity;
7using Microsoft.Extensions.Logging;
8
10
17public sealed class AvaloniaKeyboardAdapter : IKeyboardAdapter, IDisposable
18{
24 public static int? GetKeyCodeFromString(string keyName)
25 {
26 if (Enum.TryParse<Key>(keyName, true, out var key))
27 return (int)key;
28
29 Engine.Logger.LogWarning("Invalid Avalonia key name: {KeyName}", keyName);
30 return null;
31 }
32
33 private readonly Control _lifetimeOwner;
34 private TopLevel? _topLevel;
35
36 // Key state table indexed by (int)Key
37 private readonly int[] _down = new int[512];
38
39 // Modifier bits published lock-free
40 private int _modsBits;
41
42 private bool _isDisposed;
43
48 (KeyboardModifierState)Volatile.Read(ref _modsBits);
49
58 public AvaloniaKeyboardAdapter(Control lifetimeOwner)
59 {
60 _lifetimeOwner = lifetimeOwner ?? throw new ArgumentNullException(nameof(lifetimeOwner));
61
62 _lifetimeOwner.AttachedToVisualTree += OnAttachedToVisualTree;
63 _lifetimeOwner.DetachedFromVisualTree += OnDetachedFromVisualTree;
64
65 // If the control is already part of the visual tree, connect immediately
66 if (TopLevel.GetTopLevel(_lifetimeOwner) != null)
67 ConnectToTopLevel();
68
69 Engine.Logger.LogInformation("AvaloniaKeyboardAdapter initialized.");
70 }
71
72 private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
73 => ConnectToTopLevel();
74
75 private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
76 => DisconnectFromTopLevel();
77
78 private void ConnectToTopLevel()
79 {
80 if (_isDisposed)
81 return;
82
83 var tl = TopLevel.GetTopLevel(_lifetimeOwner);
84
85 if (tl == null || ReferenceEquals(tl, _topLevel))
86 return;
87
88 _topLevel = tl;
89 tl.AddHandler(InputElement.KeyDownEvent, OnKeyDown, RoutingStrategies.Tunnel);
90 tl.AddHandler(InputElement.KeyUpEvent, OnKeyUp, RoutingStrategies.Tunnel);
91 }
92
93 private void DisconnectFromTopLevel()
94 {
95 if (_topLevel == null)
96 return;
97
98 _topLevel.RemoveHandler(InputElement.KeyDownEvent, OnKeyDown);
99 _topLevel.RemoveHandler(InputElement.KeyUpEvent, OnKeyUp);
100 _topLevel = null;
101 }
102
103 private void OnKeyDown(object? sender, KeyEventArgs e)
104 {
105 if (_isDisposed)
106 return;
107
108 SetDown((int)e.Key, true);
109 UpdateMods(e.KeyModifiers);
110 }
111
112 private void OnKeyUp(object? sender, KeyEventArgs e)
113 {
114 if (_isDisposed)
115 return;
116
117 SetDown((int)e.Key, false);
118 UpdateMods(e.KeyModifiers);
119 }
120
127 [MethodImpl(MethodImplOptions.AggressiveInlining)]
128 public bool IsDown(int keyCode)
129 {
130 if ((uint)keyCode >= (uint)_down.Length)
131 return false;
132
133 return Volatile.Read(ref _down[keyCode]) != 0;
134 }
135
136 [MethodImpl(MethodImplOptions.AggressiveInlining)]
137 private void SetDown(int keyCode, bool down)
138 {
139 if ((uint)keyCode >= (uint)_down.Length)
140 return;
141
142 Volatile.Write(ref _down[keyCode], down ? 1 : 0);
143 }
144
145 [MethodImpl(MethodImplOptions.AggressiveInlining)]
146 private void UpdateMods(KeyModifiers km)
147 {
148 int mods = 0;
149
150 if ((km & KeyModifiers.Shift) != 0)
151 mods |= (int)KeyboardModifierState.Shift;
152
153 if ((km & KeyModifiers.Control) != 0)
154 mods |= (int)KeyboardModifierState.Ctrl;
155
156 if ((km & KeyModifiers.Alt) != 0)
157 mods |= (int)KeyboardModifierState.Alt;
158
159 Volatile.Write(ref _modsBits, mods);
160 }
161
165 public void Dispose()
166 {
167 if (_isDisposed)
168 return;
169
170 _isDisposed = true;
171
172 DisconnectFromTopLevel();
173 _lifetimeOwner.AttachedToVisualTree -= OnAttachedToVisualTree;
174 _lifetimeOwner.DetachedFromVisualTree -= OnDetachedFromVisualTree;
175
176 Engine.Logger.LogInformation("AvaloniaKeyboardAdapter disposed.");
177 }
178}
void Dispose()
Releases all resources and removes all event handlers registered by this adapter.
AvaloniaKeyboardAdapter(Control lifetimeOwner)
Initializes a new instance of the AvaloniaKeyboardAdapter class.
KeyboardModifierState CurrentKeyboardModifiers
Gets the current state of keyboard modifiers (Shift, Ctrl, Alt).
bool IsDown(int keyCode)
Returns true if the key represented by keyCode (an Key value cast to int) is currently pressed.
static ? int GetKeyCodeFromString(string keyName)
Converts an Avalonia Key name (case-insensitive) to its integer key code.
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...
KeyboardModifierState
Represents the state of keyboard modifier keys (Shift, Ctrl, Alt) that can be pressed simultaneously ...