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
BlazorMouseAdapter.cs
1using System.Drawing;
5using BrowserMouseEventArgs = Microsoft.AspNetCore.Components.Web.MouseEventArgs;
6using BrowserWheelEventArgs = Microsoft.AspNetCore.Components.Web.WheelEventArgs;
7using GondwanaMouseButton = Gondwana.Input.Mouse.MouseButton;
8
10
15public sealed class BlazorMouseAdapter : IMouseAdapter, IDisposable
16{
17 private readonly BlazorRenderSurfaceComponentBase _component;
18 private readonly HashSet<GondwanaMouseButton> _pressed = new();
19 private readonly object _pressedLock = new();
20 private PointF _currentPosition;
21 private KeyboardModifierState _modifiers;
22 private int _scrollDelta;
23
25 public Point CurrentPosition => _component.ToScreenPx(_currentPosition.X, _currentPosition.Y);
26
28 public HashSet<GondwanaMouseButton> PressedButtons
29 {
30 get
31 {
32 lock (_pressedLock)
33 return new HashSet<GondwanaMouseButton>(_pressed);
34 }
35 }
36
38
40 public int ScrollDelta => Interlocked.Exchange(ref _scrollDelta, 0);
41
52
60 {
61 ArgumentNullException.ThrowIfNull(component);
62 _component = component;
63
64 _component.MouseDown += OnMouseDown;
65 _component.MouseUp += OnMouseUp;
66 _component.MouseMove += OnMouseMove;
67 _component.Wheel += OnWheel;
68 }
69
70 private void OnMouseDown(BrowserMouseEventArgs e)
71 {
72 lock (_pressedLock)
73 _pressed.Add(MapButton(e.Button));
74 UpdatePosition(e);
75 }
76
77 private void OnMouseUp(BrowserMouseEventArgs e)
78 {
79 lock (_pressedLock)
80 _pressed.Remove(MapButton(e.Button));
81 UpdatePosition(e);
82 }
83
84 private void OnMouseMove(BrowserMouseEventArgs e) => UpdatePosition(e);
85
86 private void OnWheel(BrowserWheelEventArgs e)
87 {
88 // Browser DeltaY: positive = scroll down. Negate to match the convention used by other
89 // Gondwana adapters (positive = scroll up / scroll away from user).
90 Interlocked.Add(ref _scrollDelta, (int)(-e.DeltaY));
91 }
92
93 private void UpdatePosition(BrowserMouseEventArgs e)
94 {
95 _currentPosition = new PointF((float)e.OffsetX, (float)e.OffsetY);
96
97 _modifiers = KeyboardModifierState.None;
98 if (e.ShiftKey) _modifiers |= KeyboardModifierState.Shift;
99 if (e.CtrlKey) _modifiers |= KeyboardModifierState.Ctrl;
100 if (e.AltKey) _modifiers |= KeyboardModifierState.Alt;
101 }
102
103 private static GondwanaMouseButton MapButton(long button) => button switch
104 {
105 0 => GondwanaMouseButton.Left,
106 1 => GondwanaMouseButton.Middle,
107 2 => GondwanaMouseButton.Right,
108 _ => GondwanaMouseButton.None
109 };
110
112 public void Dispose()
113 {
114 _component.MouseDown -= OnMouseDown;
115 _component.MouseUp -= OnMouseUp;
116 _component.MouseMove -= OnMouseMove;
117 _component.Wheel -= OnWheel;
118 }
119}
KeyboardModifierState CurrentKeyboardModifiers
Gets the current state of keyboard modifier keys (Shift, Ctrl, Alt) at the time of mouse input....
void Dispose()
Unsubscribes from the render component's mouse events.
BlazorMouseAdapter(BlazorBitmapRenderSurfaceComponent component)
Initializes a new instance of BlazorMouseAdapter and attaches it to the mouse events on the specified...
int ScrollDelta
Gets the accumulated scroll wheel delta since the last poll, measured in implementation-defined units...
BlazorMouseAdapter(BlazorRenderSurfaceComponentBase component)
Initializes a new instance of BlazorMouseAdapter for any Gondwana Blazor render surface.
HashSet< GondwanaMouseButton > PressedButtons
Gets the set of mouse buttons that are currently pressed. This collection includes identifiers for al...
Point CurrentPosition
Gets the current position of the mouse cursor in screen or client coordinates, depending on the imple...
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.
Provides comprehensive data for mouse events, including cursor position (current and previous),...
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 ...