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
WinFormsMouseAdapter.cs
5
7
11public sealed class WinFormsMouseAdapter : IMouseAdapter, IDisposable
12{
13 private readonly Control _control;
14 private readonly HashSet<MouseButton> _pressed = new();
15 private readonly object _pressedLock = new();
16 private Point _currentPosition;
17 private KeyboardModifierState _modifiers;
18 private int _scrollDelta;
19
23 public Point CurrentPosition => _control switch
24 {
25 WinFormGpuRenderSurfaceControl gpu => gpu.Adapter?.AdapterPxToScreenPx(_currentPosition) ?? _currentPosition,
26 WinFormBitmapRenderSurfaceControl bitmap => bitmap.Adapter.AdapterPxToScreenPx(_currentPosition),
27 _ => _currentPosition
28 };
29
36 public HashSet<MouseButton> PressedButtons
37 {
38 get
39 {
40 var osButtons = Control.MouseButtons;
41
42 lock (_pressedLock)
43 {
44 if (!osButtons.HasFlag(MouseButtons.Left))
45 _pressed.Remove(MouseButton.Left);
46
47 if (!osButtons.HasFlag(MouseButtons.Right))
48 _pressed.Remove(MouseButton.Right);
49
50 if (!osButtons.HasFlag(MouseButtons.Middle))
51 _pressed.Remove(MouseButton.Middle);
52
53 return new HashSet<MouseButton>(_pressed);
54 }
55 }
56 }
57
62
66 public int ScrollDelta => Interlocked.Exchange(ref _scrollDelta, 0); // one-time read & reset
67
73 public WinFormsMouseAdapter(Control control)
74 {
75 _control = control ?? throw new ArgumentNullException(nameof(control));
76
77 _control.MouseDown += OnMouseDown;
78 _control.MouseUp += OnMouseUp;
79 _control.MouseMove += OnMouseMove;
80 _control.MouseWheel += OnMouseWheel;
81 }
82
83 private void OnMouseDown(object? sender, System.Windows.Forms.MouseEventArgs e)
84 {
85 if (WinFormsTouchAdapter.IsNativeTouchMouseMessage(_control))
86 return;
87
88 lock (_pressedLock)
89 _pressed.Add(MapButton(e.Button));
90
91 UpdatePosition(e);
92 }
93
94 private void OnMouseUp(object? sender, System.Windows.Forms.MouseEventArgs e)
95 {
96 if (WinFormsTouchAdapter.IsNativeTouchMouseMessage(_control))
97 return;
98
99 lock (_pressedLock)
100 _pressed.Remove(MapButton(e.Button));
101
102 UpdatePosition(e);
103 }
104
105 private void OnMouseMove(object? sender, System.Windows.Forms.MouseEventArgs e)
106 {
107 if (WinFormsTouchAdapter.IsNativeTouchMouseMessage(_control))
108 return;
109
110 UpdatePosition(e);
111 }
112
113 private void UpdatePosition(System.Windows.Forms.MouseEventArgs e)
114 {
115 _currentPosition = e.Location;
116
117 _modifiers = KeyboardModifierState.None;
118
119 if (Control.ModifierKeys.HasFlag(Keys.Shift))
120 _modifiers |= KeyboardModifierState.Shift;
121
122 if (Control.ModifierKeys.HasFlag(Keys.Control))
123 _modifiers |= KeyboardModifierState.Ctrl;
124
125 if (Control.ModifierKeys.HasFlag(Keys.Alt))
126 _modifiers |= KeyboardModifierState.Alt;
127 }
128
129 private static MouseButton MapButton(MouseButtons btn) => btn switch
130 {
131 MouseButtons.Left => MouseButton.Left,
132 MouseButtons.Right => MouseButton.Right,
133 MouseButtons.Middle => MouseButton.Middle,
135 };
136
137 private void OnMouseWheel(object? sender, System.Windows.Forms.MouseEventArgs e)
138 {
139 Interlocked.Add(ref _scrollDelta, e.Delta);
140 }
141
143 public void Dispose()
144 {
145 _control.MouseDown -= OnMouseDown;
146 _control.MouseUp -= OnMouseUp;
147 _control.MouseMove -= OnMouseMove;
148 _control.MouseWheel -= OnMouseWheel;
149 }
150}
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.
Captures native Windows touchscreen contacts (WM_TOUCH) from a WinForms render control and its child ...
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...