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
WidgetDragBehavior.cs
1using System.Drawing;
2using System.Numerics;
5
6namespace Gondwana.Widgets;
7
11internal sealed class WidgetDragBehavior
12{
13 private readonly WidgetBase _owner;
14
15 private bool _isPointerDown;
16 private bool _suppressNextPointerClick;
17 private float _dragThresholdPx = 3f;
18 private PointF _dragStartScreenPositionPx;
19 private Vector2 _dragStartWidgetPositionPx;
20 private WidgetPointerButtonEnum _dragButton = WidgetPointerButtonEnum.None;
21 private int _dragPointerId;
22 private View? _dragView;
23 private Vector2 _dragStartPointerPositionPx;
24
25 internal WidgetDragBehavior(WidgetBase owner)
26 {
27 _owner = owner ?? throw new ArgumentNullException(nameof(owner));
28 }
29
30 internal bool IsDragEnabled { get; set; } = true;
31
32 internal bool IsDragging { get; private set; }
33
34 internal float DragThresholdPx
35 {
36 get => _dragThresholdPx;
37 set
38 {
39 if (value < 0f)
40 {
41 throw new ArgumentOutOfRangeException(
42 nameof(value),
43 value,
44 "The drag threshold cannot be negative.");
45 }
46
47 _dragThresholdPx = value;
48 }
49 }
50
51 internal bool CanStartDrag(
52 WidgetPointerEventArgs args)
53 {
54 return IsDragEnabled &&
55 args.IsPrimaryButton;
56 }
57
58 internal void ProcessPointerDown(
59 WidgetPointerEventArgs args,
60 Func<WidgetPointerEventArgs, bool> canStartDrag)
61 {
62 if (_isPointerDown || !canStartDrag(args))
63 {
64 return;
65 }
66
67 _isPointerDown = true;
68 _suppressNextPointerClick = false;
69 IsDragging = false;
70
71 _dragStartScreenPositionPx = args.ScreenPositionPx;
72 _dragStartWidgetPositionPx = _owner.GetPosition();
73 _dragButton = args.Button;
74 _dragPointerId = args.PointerId;
75 _dragView = args.View;
76 _dragStartPointerPositionPx = GetPointerPositionInWidgetSpace(args.ScreenPositionPx, args.View);
77 }
78
79 internal void ProcessPointerMove(
80 WidgetPointerEventArgs args,
81 Func<Vector2, View, Vector2> convertDelta,
82 Func<Vector2, Vector2> constrainPosition,
83 Action<WidgetDragEventArgs> dragStarted,
84 Action<WidgetDragEventArgs> dragged)
85 {
86 if (!_isPointerDown || args.PointerId != _dragPointerId)
87 return;
88
89 Vector2 totalScreenDeltaPx = GetTotalScreenDelta(args.ScreenPositionPx);
90
91 if (!IsDragging)
92 {
93 float thresholdSquared = DragThresholdPx * DragThresholdPx;
94
95 if (totalScreenDeltaPx.LengthSquared() < thresholdSquared)
96 return;
97
98 IsDragging = true;
99 _suppressNextPointerClick = true;
100
101 dragStarted(CreateDragEventArgs(args));
102
103 // The callback may cancel or hide the widget, which resets this state.
104 if (!IsDragging)
105 return;
106 }
107
108 ApplyDragPosition(
109 totalScreenDeltaPx,
110 _dragView ?? args.View,
111 convertDelta,
112 constrainPosition);
113
114 dragged(CreateDragEventArgs(args));
115 }
116
117 internal void ProcessPointerUp(
118 WidgetPointerEventArgs args,
119 Func<Vector2, View, Vector2> convertDelta,
120 Func<Vector2, Vector2> constrainPosition,
121 Action<WidgetDragEventArgs> dragEnded)
122 {
123 if (!_isPointerDown || !IsMatchingRelease(args))
124 return;
125
126 WidgetDragEventArgs? dragArgs = null;
127
128 if (IsDragging)
129 {
130 Vector2 totalScreenDeltaPx = GetTotalScreenDelta(args.ScreenPositionPx);
131
132 ApplyDragPosition(
133 totalScreenDeltaPx,
134 _dragView ?? args.View,
135 convertDelta,
136 constrainPosition);
137
138 dragArgs = CreateDragEventArgs(args);
139 }
140
141 bool completedDrag = IsDragging;
142
143 ResetPointerState();
144
145 if (completedDrag && dragArgs is not null)
146 {
147 _suppressNextPointerClick = true;
148 dragEnded(dragArgs);
149 }
150 }
151
152 internal bool ShouldDispatchPointerClick(WidgetPointerEventArgs args)
153 {
154 if (!_suppressNextPointerClick)
155 return true;
156
157 _suppressNextPointerClick = false;
158 args.Handled = true;
159
160 return false;
161 }
162
163 internal Vector2 ConvertScreenDeltaToPositionDelta(
164 Vector2 totalScreenDeltaPx,
165 View view)
166 {
167 ArgumentNullException.ThrowIfNull(view);
168
169 if (_owner.Mode == DirectDrawingMode.View)
170 return totalScreenDeltaPx;
171
172 var currentScreenPositionPx = new PointF(
173 _dragStartScreenPositionPx.X + totalScreenDeltaPx.X,
174 _dragStartScreenPositionPx.Y + totalScreenDeltaPx.Y);
175
176 Vector2 currentPointerPositionPx = GetPointerPositionInWidgetSpace(currentScreenPositionPx, view);
177
178 return currentPointerPositionPx - _dragStartPointerPositionPx;
179 }
180
181 internal void ResetPointerState(bool clearClickSuppression = false)
182 {
183 _isPointerDown = false;
184 IsDragging = false;
185 _dragStartScreenPositionPx = PointF.Empty;
186 _dragStartWidgetPositionPx = Vector2.Zero;
187 _dragButton = WidgetPointerButtonEnum.None;
188 _dragPointerId = 0;
189 _dragView = null;
190 _dragStartPointerPositionPx = Vector2.Zero;
191
192 if (clearClickSuppression)
193 _suppressNextPointerClick = false;
194 }
195
196 private Vector2 GetPointerPositionInWidgetSpace(
197 PointF screenPositionPx,
198 View view)
199 {
200 if (_owner.Mode == DirectDrawingMode.View)
201 return new Vector2(screenPositionPx.X, screenPositionPx.Y);
202
203 var sceneLayer = _owner.SceneLayer
204 ?? throw new InvalidOperationException(
205 "A scene-layer draggable widget must contain children attached to a SceneLayer.");
206
207 PointF worldPositionPx = view.ScreenPxToWorldPx(sceneLayer, screenPositionPx);
208
209 return new Vector2(worldPositionPx.X, worldPositionPx.Y);
210 }
211
212 private Vector2 GetTotalScreenDelta(PointF currentScreenPositionPx)
213 {
214 return new Vector2(
215 currentScreenPositionPx.X - _dragStartScreenPositionPx.X,
216 currentScreenPositionPx.Y - _dragStartScreenPositionPx.Y);
217 }
218
219 private void ApplyDragPosition(
220 Vector2 totalScreenDeltaPx,
221 View view,
222 Func<Vector2, View, Vector2> convertDelta,
223 Func<Vector2, Vector2> constrainPosition)
224 {
225 Vector2 positionDeltaPx = convertDelta(totalScreenDeltaPx, view);
226 Vector2 proposedPositionPx = _dragStartWidgetPositionPx + positionDeltaPx;
227
228 _owner.SetPosition(constrainPosition(proposedPositionPx));
229 }
230
231 private bool IsMatchingRelease(WidgetPointerEventArgs args)
232 {
233 return args.PointerId == _dragPointerId &&
234 (args.Button == WidgetPointerButtonEnum.None ||
235 args.Button == _dragButton);
236 }
237
238 private WidgetDragEventArgs CreateDragEventArgs(WidgetPointerEventArgs pointerArgs)
239 {
240 return new WidgetDragEventArgs(
241 _owner,
242 _dragStartScreenPositionPx,
243 pointerArgs.ScreenPositionPx,
244 _dragButton,
245 pointerArgs.Tick,
246 pointerArgs.PointerId);
247 }
248}
PointF ScreenPxToWorldPx(SceneLayer layer, PointF screenPx)
Converts a screen-space pixel position into world-space coordinates for the specified scene layer,...
Definition View.cs:239
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.
WidgetPointerButtonEnum
Represents a pointer button used by a widget interaction.