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
WinFormsMouseAdapter.cs
4
6
10public sealed class WinFormsMouseAdapter : IMouseAdapter, IDisposable
11{
12 private readonly Control _control;
13 private readonly HashSet<MouseButton> _pressed = new();
14 private readonly object _pressedLock = new();
15 private Point _currentPosition;
16 private KeyboardModifierState _modifiers;
17 private int _scrollDelta;
18
22 public Point CurrentPosition => _control switch
23 {
24 WinFormGpuRenderSurfaceControl gpu => gpu.Adapter?.AdapterPxToScreenPx(_currentPosition) ?? _currentPosition,
25 WinFormBitmapRenderSurfaceControl bitmap => bitmap.Adapter.AdapterPxToScreenPx(_currentPosition),
26 _ => _currentPosition
27 };
28
35 public HashSet<MouseButton> PressedButtons
36 {
37 get
38 {
39 var osButtons = Control.MouseButtons;
40 lock (_pressedLock)
41 {
42 if (!osButtons.HasFlag(MouseButtons.Left)) _pressed.Remove(MouseButton.Left);
43 if (!osButtons.HasFlag(MouseButtons.Right)) _pressed.Remove(MouseButton.Right);
44 if (!osButtons.HasFlag(MouseButtons.Middle)) _pressed.Remove(MouseButton.Middle);
45 return new HashSet<MouseButton>(_pressed);
46 }
47 }
48 }
49
54
58 public int ScrollDelta => Interlocked.Exchange(ref _scrollDelta, 0); // one-time read & reset
59
65 public WinFormsMouseAdapter(Control control)
66 {
67 _control = control ?? throw new ArgumentNullException(nameof(control));
68
69 _control.MouseDown += OnMouseDown;
70 _control.MouseUp += OnMouseUp;
71 _control.MouseMove += OnMouseMove;
72 _control.MouseWheel += OnMouseWheel;
73 }
74
75 private void OnMouseDown(object? sender, System.Windows.Forms.MouseEventArgs e)
76 {
77 lock (_pressedLock)
78 _pressed.Add(MapButton(e.Button));
79 UpdatePosition(e);
80 }
81
82 private void OnMouseUp(object? sender, System.Windows.Forms.MouseEventArgs e)
83 {
84 lock (_pressedLock)
85 _pressed.Remove(MapButton(e.Button));
86 UpdatePosition(e);
87 }
88
89 private void OnMouseMove(object? sender, System.Windows.Forms.MouseEventArgs e)
90 {
91 UpdatePosition(e);
92 }
93
94 private void UpdatePosition(System.Windows.Forms.MouseEventArgs e)
95 {
96 _currentPosition = e.Location;
97
98 _modifiers = KeyboardModifierState.None;
99 if (Control.ModifierKeys.HasFlag(Keys.Shift)) _modifiers |= KeyboardModifierState.Shift;
100 if (Control.ModifierKeys.HasFlag(Keys.Control)) _modifiers |= KeyboardModifierState.Ctrl;
101 if (Control.ModifierKeys.HasFlag(Keys.Alt)) _modifiers |= KeyboardModifierState.Alt;
102 }
103
104 private static MouseButton MapButton(MouseButtons btn) => btn switch
105 {
106 MouseButtons.Left => MouseButton.Left,
107 MouseButtons.Right => MouseButton.Right,
108 MouseButtons.Middle => MouseButton.Middle,
110 };
111
112 private void OnMouseWheel(object? sender, System.Windows.Forms.MouseEventArgs e)
113 {
114 Interlocked.Add(ref _scrollDelta, e.Delta);
115 }
116
118 public void Dispose()
119 {
120 _control.MouseDown -= OnMouseDown;
121 _control.MouseUp -= OnMouseUp;
122 _control.MouseMove -= OnMouseMove;
123 _control.MouseWheel -= OnMouseWheel;
124 }
125}
Point AdapterPxToScreenPx(PointF adapterPx)
Converts adapter input to logical ScreenPx, retaining outside coordinates for capture/leave.
int ScrollDelta
Gets the accumulated scroll wheel delta since the last read, then resets it to zero.
void Dispose()
Unsubscribes from the control's mouse events.
WinFormsMouseAdapter(Control control)
Initializes a new instance of the WinFormsMouseAdapter class attached to the specified control.
HashSet< MouseButton > PressedButtons
Gets the set of currently pressed mouse buttons, reconciled against the actual OS button state to pre...
KeyboardModifierState CurrentKeyboardModifiers
Gets the current state of keyboard modifiers (Shift, Ctrl, Alt).
Point CurrentPosition
Gets the current position of the mouse cursor.
Provides a Windows Forms control that hosts bitmap-based Gondwana rendering.
WinFormBitmapRenderSurfaceAdapter Adapter
Gets the render surface adapter used by this control.
Provides a Windows Forms control that hosts GPU-accelerated Gondwana rendering.
WinFormGpuRenderSurfaceAdapter Adapter
Gets the GPU render surface adapter used by this 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 ...
MouseButton
Represents mouse button identifiers that can be used individually or combined to represent multiple s...
@ None
No mouse button is pressed. This represents the default mouse state when the cursor is not actively c...