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
AvaloniaMouseAdapter.cs
1using System.Drawing;
2using Avalonia;
3using Avalonia.Controls;
4using Avalonia.Input;
5using Avalonia.Interactivity;
8using GondwanaMouseButton = Gondwana.Input.Mouse.MouseButton;
9
11
16public sealed class AvaloniaMouseAdapter : IMouseAdapter, IDisposable
17{
18 private readonly Control _control;
19 private TopLevel? _topLevel;
20 private readonly HashSet<GondwanaMouseButton> _pressed = new();
21 private PointF _currentPosition;
22 private KeyboardModifierState _modifiers;
23 private int _scrollDelta;
24
28 public System.Drawing.Point CurrentPosition => PointerCoordinates.ToScreenPx(_control, _currentPosition);
29
33 public HashSet<GondwanaMouseButton> PressedButtons => _pressed;
34
39
43 public int ScrollDelta => Interlocked.Exchange(ref _scrollDelta, 0);
44
50 public AvaloniaMouseAdapter(Control control)
51 {
52 _control = control ?? throw new ArgumentNullException(nameof(control));
53
54 // OpenGlControlBase can render in a native child surface (notably WGL).
55 // Route input at the Avalonia TopLevel in the tunnel phase, as keyboard
56 // input already does, so the GL surface cannot swallow pointer events.
57 _control.AttachedToVisualTree += OnAttachedToVisualTree;
58 _control.DetachedFromVisualTree += OnDetachedFromVisualTree;
59
60 if (TopLevel.GetTopLevel(_control) is not null)
61 ConnectToTopLevel();
62 }
63
64 private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
65 => ConnectToTopLevel();
66
67 private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
68 => DisconnectFromTopLevel();
69
70 private void ConnectToTopLevel()
71 {
72 var topLevel = TopLevel.GetTopLevel(_control);
73
74 if (topLevel is null || ReferenceEquals(topLevel, _topLevel))
75 return;
76
77 DisconnectFromTopLevel();
78 _topLevel = topLevel;
79 topLevel.AddHandler(InputElement.PointerPressedEvent, OnPointerPressed, RoutingStrategies.Tunnel);
80 topLevel.AddHandler(InputElement.PointerReleasedEvent, OnPointerReleased, RoutingStrategies.Tunnel);
81 topLevel.AddHandler(InputElement.PointerMovedEvent, OnPointerMoved, RoutingStrategies.Tunnel);
82 topLevel.AddHandler(InputElement.PointerWheelChangedEvent, OnPointerWheelChanged, RoutingStrategies.Tunnel);
83 }
84
85 private void DisconnectFromTopLevel()
86 {
87 if (_topLevel is null)
88 return;
89
90 _topLevel.RemoveHandler(InputElement.PointerPressedEvent, OnPointerPressed);
91 _topLevel.RemoveHandler(InputElement.PointerReleasedEvent, OnPointerReleased);
92 _topLevel.RemoveHandler(InputElement.PointerMovedEvent, OnPointerMoved);
93 _topLevel.RemoveHandler(InputElement.PointerWheelChangedEvent, OnPointerWheelChanged);
94 _topLevel = null;
95 }
96
97 private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
98 {
99 var point = e.GetCurrentPoint(_control);
100 _pressed.Add(MapButton(point.Properties));
101 UpdatePosition(e);
102 }
103
104 private void OnPointerReleased(object? sender, PointerReleasedEventArgs e)
105 {
106 _pressed.Remove(MapReleasedButton(e.InitialPressMouseButton));
107 UpdatePosition(e);
108 }
109
110 private void OnPointerMoved(object? sender, PointerEventArgs e)
111 {
112 UpdatePosition(e);
113 }
114
115 private void UpdatePosition(PointerEventArgs e)
116 {
117 // Pointer events are raised on TopLevel, but positions must remain
118 // local to the render control for DPI/presentation normalization.
119 var pos = e.GetPosition(_control);
120 _currentPosition = new PointF((float)pos.X, (float)pos.Y);
121
122 _modifiers = KeyboardModifierState.None;
123 var km = e.KeyModifiers;
124
125 if ((km & KeyModifiers.Shift) != 0)
126 _modifiers |= KeyboardModifierState.Shift;
127
128 if ((km & KeyModifiers.Control) != 0)
129 _modifiers |= KeyboardModifierState.Ctrl;
130
131 if ((km & KeyModifiers.Alt) != 0)
132 _modifiers |= KeyboardModifierState.Alt;
133 }
134
135 private static GondwanaMouseButton MapButton(PointerPointProperties props)
136 {
137 if (props.IsLeftButtonPressed)
138 return GondwanaMouseButton.Left;
139
140 if (props.IsRightButtonPressed)
141 return GondwanaMouseButton.Right;
142
143 if (props.IsMiddleButtonPressed)
144 return GondwanaMouseButton.Middle;
145
146 return GondwanaMouseButton.None;
147 }
148
149 private static GondwanaMouseButton MapReleasedButton(global::Avalonia.Input.MouseButton initialPressButton) =>
150 initialPressButton switch
151 {
152 global::Avalonia.Input.MouseButton.Left => GondwanaMouseButton.Left,
153 global::Avalonia.Input.MouseButton.Right => GondwanaMouseButton.Right,
154 global::Avalonia.Input.MouseButton.Middle => GondwanaMouseButton.Middle,
155 _ => GondwanaMouseButton.None
156 };
157
158 private void OnPointerWheelChanged(object? sender, PointerWheelEventArgs e)
159 {
160 // Avalonia Delta.Y is in lines; positive = scroll up (matches WinForms convention).
161 // Multiply by 120 to approximate Windows WHEEL_DELTA units for compatibility.
162 var delta = (int)(e.Delta.Y * 120);
163 Interlocked.Add(ref _scrollDelta, delta);
164 }
165
167 public void Dispose()
168 {
169 DisconnectFromTopLevel();
170 _control.AttachedToVisualTree -= OnAttachedToVisualTree;
171 _control.DetachedFromVisualTree -= OnDetachedFromVisualTree;
172 }
173}
void Dispose()
Unsubscribes from the control's pointer events.
System.Drawing.Point CurrentPosition
Gets the current position of the pointer cursor in client (control-local) coordinates.
KeyboardModifierState CurrentKeyboardModifiers
Gets the current state of keyboard modifiers (Shift, Ctrl, Alt).
HashSet< GondwanaMouseButton > PressedButtons
Gets the set of currently pressed mouse buttons.
int ScrollDelta
Gets the accumulated scroll wheel delta since the last read, then resets it to zero.
AvaloniaMouseAdapter(Control control)
Initializes a new instance of the AvaloniaMouseAdapter class attached to the specified control.
Defines an adapter interface for accessing mouse device state, including cursor position,...
KeyboardModifierState
Represents the state of keyboard modifier keys (Shift, Ctrl, Alt) that can be pressed simultaneously ...