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
AvaloniaMouseAdapter.cs
1using System.Drawing;
2using Avalonia.Controls;
3using Avalonia.Input;
6using GondwanaMouseButton = Gondwana.Input.Mouse.MouseButton;
7
9
14public sealed class AvaloniaMouseAdapter : IMouseAdapter, IDisposable
15{
16 private readonly Control _control;
17 private readonly HashSet<GondwanaMouseButton> _pressed = new();
18 private PointF _currentPosition;
19 private KeyboardModifierState _modifiers;
20 private int _scrollDelta;
21
25 public Point CurrentPosition => PointerCoordinates.ToScreenPx(_control, _currentPosition);
26
30 public HashSet<GondwanaMouseButton> PressedButtons => _pressed;
31
36
40 public int ScrollDelta => Interlocked.Exchange(ref _scrollDelta, 0);
41
47 public AvaloniaMouseAdapter(Control control)
48 {
49 _control = control ?? throw new ArgumentNullException(nameof(control));
50
51 _control.PointerPressed += OnPointerPressed;
52 _control.PointerReleased += OnPointerReleased;
53 _control.PointerMoved += OnPointerMoved;
54 _control.PointerWheelChanged += OnPointerWheelChanged;
55 }
56
57 private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
58 {
59 var point = e.GetCurrentPoint(sender as Control);
60 _pressed.Add(MapButton(point.Properties));
61 UpdatePosition(e, sender as Control);
62 }
63
64 private void OnPointerReleased(object? sender, PointerReleasedEventArgs e)
65 {
66 _pressed.Remove(MapReleasedButton(e.InitialPressMouseButton));
67 UpdatePosition(e, sender as Control);
68 }
69
70 private void OnPointerMoved(object? sender, PointerEventArgs e)
71 {
72 UpdatePosition(e, sender as Control);
73 }
74
75 private void UpdatePosition(PointerEventArgs e, Control? relativeTo)
76 {
77 var pos = e.GetPosition(relativeTo);
78 _currentPosition = new PointF((float)pos.X, (float)pos.Y);
79
80 _modifiers = KeyboardModifierState.None;
81 var km = e.KeyModifiers;
82 if ((km & KeyModifiers.Shift) != 0) _modifiers |= KeyboardModifierState.Shift;
83 if ((km & KeyModifiers.Control) != 0) _modifiers |= KeyboardModifierState.Ctrl;
84 if ((km & KeyModifiers.Alt) != 0) _modifiers |= KeyboardModifierState.Alt;
85 }
86
87 private static GondwanaMouseButton MapButton(PointerPointProperties props)
88 {
89 if (props.IsLeftButtonPressed) return GondwanaMouseButton.Left;
90 if (props.IsRightButtonPressed) return GondwanaMouseButton.Right;
91 if (props.IsMiddleButtonPressed) return GondwanaMouseButton.Middle;
92 return GondwanaMouseButton.None;
93 }
94
95 private static GondwanaMouseButton MapReleasedButton(global::Avalonia.Input.MouseButton initialPressButton) => initialPressButton switch
96 {
97 global::Avalonia.Input.MouseButton.Left => GondwanaMouseButton.Left,
98 global::Avalonia.Input.MouseButton.Right => GondwanaMouseButton.Right,
99 global::Avalonia.Input.MouseButton.Middle => GondwanaMouseButton.Middle,
100 _ => GondwanaMouseButton.None
101 };
102
103 private void OnPointerWheelChanged(object? sender, PointerWheelEventArgs e)
104 {
105 // Avalonia Delta.Y is in lines; positive = scroll up (matches WinForms convention).
106 // Multiply by 120 to approximate Windows WHEEL_DELTA units for compatibility.
107 var delta = (int)(e.Delta.Y * 120);
108 Interlocked.Add(ref _scrollDelta, delta);
109 }
110
112 public void Dispose()
113 {
114 _control.PointerPressed -= OnPointerPressed;
115 _control.PointerReleased -= OnPointerReleased;
116 _control.PointerMoved -= OnPointerMoved;
117 _control.PointerWheelChanged -= OnPointerWheelChanged;
118 }
119}
Point CurrentPosition
Gets the current position of the pointer cursor in client (control-local) coordinates.
void Dispose()
Unsubscribes from the control's pointer events.
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 ...