11internal sealed class WidgetDragBehavior
13 private readonly WidgetBase _owner;
15 private bool _isPointerDown;
16 private bool _suppressNextPointerClick;
17 private float _dragThresholdPx = 3f;
18 private PointF _dragStartScreenPositionPx;
19 private Vector2 _dragStartWidgetPositionPx;
21 private int _dragPointerId;
22 private View? _dragView;
23 private Vector2 _dragStartPointerPositionPx;
25 internal WidgetDragBehavior(WidgetBase owner)
27 _owner = owner ??
throw new ArgumentNullException(nameof(owner));
30 internal bool IsDragEnabled {
get;
set; } =
true;
32 internal bool IsDragging {
get;
private set; }
34 internal float DragThresholdPx
36 get => _dragThresholdPx;
41 throw new ArgumentOutOfRangeException(
44 "The drag threshold cannot be negative.");
47 _dragThresholdPx = value;
51 internal bool CanStartDrag(
52 WidgetPointerEventArgs args)
54 return IsDragEnabled &&
58 internal void ProcessPointerDown(
59 WidgetPointerEventArgs args,
60 Func<WidgetPointerEventArgs, bool> canStartDrag)
62 if (_isPointerDown || !canStartDrag(args))
67 _isPointerDown =
true;
68 _suppressNextPointerClick =
false;
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);
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)
86 if (!_isPointerDown || args.PointerId != _dragPointerId)
89 Vector2 totalScreenDeltaPx = GetTotalScreenDelta(args.ScreenPositionPx);
93 float thresholdSquared = DragThresholdPx * DragThresholdPx;
95 if (totalScreenDeltaPx.LengthSquared() < thresholdSquared)
99 _suppressNextPointerClick =
true;
101 dragStarted(CreateDragEventArgs(args));
110 _dragView ?? args.View,
114 dragged(CreateDragEventArgs(args));
117 internal void ProcessPointerUp(
118 WidgetPointerEventArgs args,
119 Func<Vector2, View, Vector2> convertDelta,
120 Func<Vector2, Vector2> constrainPosition,
121 Action<WidgetDragEventArgs> dragEnded)
123 if (!_isPointerDown || !IsMatchingRelease(args))
126 WidgetDragEventArgs? dragArgs =
null;
130 Vector2 totalScreenDeltaPx = GetTotalScreenDelta(args.ScreenPositionPx);
134 _dragView ?? args.View,
138 dragArgs = CreateDragEventArgs(args);
141 bool completedDrag = IsDragging;
145 if (completedDrag && dragArgs is not
null)
147 _suppressNextPointerClick =
true;
152 internal bool ShouldDispatchPointerClick(WidgetPointerEventArgs args)
154 if (!_suppressNextPointerClick)
157 _suppressNextPointerClick =
false;
163 internal Vector2 ConvertScreenDeltaToPositionDelta(
164 Vector2 totalScreenDeltaPx,
167 ArgumentNullException.ThrowIfNull(view);
170 return totalScreenDeltaPx;
172 var currentScreenPositionPx =
new PointF(
173 _dragStartScreenPositionPx.X + totalScreenDeltaPx.X,
174 _dragStartScreenPositionPx.Y + totalScreenDeltaPx.Y);
176 Vector2 currentPointerPositionPx = GetPointerPositionInWidgetSpace(currentScreenPositionPx, view);
178 return currentPointerPositionPx - _dragStartPointerPositionPx;
181 internal void ResetPointerState(
bool clearClickSuppression =
false)
183 _isPointerDown =
false;
185 _dragStartScreenPositionPx = PointF.Empty;
186 _dragStartWidgetPositionPx = Vector2.Zero;
190 _dragStartPointerPositionPx = Vector2.Zero;
192 if (clearClickSuppression)
193 _suppressNextPointerClick =
false;
196 private Vector2 GetPointerPositionInWidgetSpace(
197 PointF screenPositionPx,
201 return new Vector2(screenPositionPx.X, screenPositionPx.Y);
203 var sceneLayer = _owner.SceneLayer
204 ??
throw new InvalidOperationException(
205 "A scene-layer draggable widget must contain children attached to a SceneLayer.");
209 return new Vector2(worldPositionPx.X, worldPositionPx.Y);
212 private Vector2 GetTotalScreenDelta(PointF currentScreenPositionPx)
215 currentScreenPositionPx.X - _dragStartScreenPositionPx.X,
216 currentScreenPositionPx.Y - _dragStartScreenPositionPx.Y);
219 private void ApplyDragPosition(
220 Vector2 totalScreenDeltaPx,
222 Func<Vector2, View, Vector2> convertDelta,
223 Func<Vector2, Vector2> constrainPosition)
225 Vector2 positionDeltaPx = convertDelta(totalScreenDeltaPx, view);
226 Vector2 proposedPositionPx = _dragStartWidgetPositionPx + positionDeltaPx;
228 _owner.SetPosition(constrainPosition(proposedPositionPx));
231 private bool IsMatchingRelease(WidgetPointerEventArgs args)
233 return args.PointerId == _dragPointerId &&
234 (args.Button == WidgetPointerButtonEnum.None ||
235 args.Button == _dragButton);
238 private WidgetDragEventArgs CreateDragEventArgs(WidgetPointerEventArgs pointerArgs)
240 return new WidgetDragEventArgs(
242 _dragStartScreenPositionPx,
243 pointerArgs.ScreenPositionPx,
246 pointerArgs.PointerId);
PointF ScreenPxToWorldPx(SceneLayer layer, PointF screenPx)
Converts a screen-space pixel position into world-space coordinates for the specified scene layer,...
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.