Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 861c4a8

Browse files
committed
gesture 1.0 -> 1.2
1 parent 9ae129a commit 861c4a8

File tree

5 files changed

+98
-25
lines changed

5 files changed

+98
-25
lines changed

Runtime/foundation/debug.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
using Unity.UIWidgets.editor;
55
using Unity.UIWidgets.painting;
66
using Unity.UIWidgets.ui;
7+
using Canvas = Unity.UIWidgets.ui.Canvas;
8+
using Color = Unity.UIWidgets.ui.Color;
79
using Debug = UnityEngine.Debug;
10+
using Rect = Unity.UIWidgets.ui.Rect;
811

912
namespace Unity.UIWidgets.foundation {
1013
public static class D {
11-
1214
public static void logError(string message, Exception ex = null) {
1315
Debug.LogException(new AssertionError(message, ex));
1416
}
@@ -22,7 +24,7 @@ public static bool debugEnabled {
2224
#endif
2325
}
2426
}
25-
27+
2628
[Conditional("UIWidgets_DEBUG")]
2729
public static void assert(Func<bool> result, string message = null) {
2830
if (!result()) {
@@ -73,7 +75,9 @@ public static void assert(bool result, string message = null) {
7375

7476
public static bool debugCheckIntrinsicSizes = false;
7577

76-
public static HSVColor debugCurrentRepaintColor =
78+
public static bool debugPrintMouseHoverEvents = false;
79+
80+
public static HSVColor debugCurrentRepaintColor =
7781
HSVColor.fromAHSV(0.4f, 60.0f, 1.0f, 1.0f);
7882

7983
public static void _debugDrawDoubleRect(Canvas canvas, Rect outerRect, Rect innerRect, Color color) {
@@ -103,34 +107,34 @@ public static void debugPaintPadding(Canvas canvas, Rect outerRect, Rect innerRe
103107
return true;
104108
});
105109
}
106-
110+
107111
public static void setDebugPaint(bool? debugPaintSizeEnabled = null,
108112
bool? debugPaintBaselinesEnabled = null,
109-
bool? debugPaintPointersEnabled= null,
113+
bool? debugPaintPointersEnabled = null,
110114
bool? debugPaintLayerBordersEnabled = null,
111-
bool? debugRepaintRainbowEnabled = null ) {
112-
115+
bool? debugRepaintRainbowEnabled = null) {
113116
bool needRepaint = false;
114117
if (debugPaintSizeEnabled != null && debugPaintSizeEnabled != D.debugPaintSizeEnabled) {
115118
D.debugPaintSizeEnabled = debugPaintSizeEnabled.Value;
116119
needRepaint = true;
117120
}
118-
121+
119122
if (debugPaintBaselinesEnabled != null && debugPaintBaselinesEnabled != D.debugPaintBaselinesEnabled) {
120123
D.debugPaintBaselinesEnabled = debugPaintBaselinesEnabled.Value;
121124
needRepaint = true;
122125
}
123-
126+
124127
if (debugPaintPointersEnabled != null && debugPaintPointersEnabled != D.debugPaintPointersEnabled) {
125128
D.debugPaintPointersEnabled = debugPaintPointersEnabled.Value;
126129
needRepaint = true;
127130
}
128-
129-
if (debugPaintLayerBordersEnabled != null && debugPaintLayerBordersEnabled != D.debugPaintLayerBordersEnabled) {
131+
132+
if (debugPaintLayerBordersEnabled != null &&
133+
debugPaintLayerBordersEnabled != D.debugPaintLayerBordersEnabled) {
130134
D.debugPaintLayerBordersEnabled = debugPaintLayerBordersEnabled.Value;
131135
needRepaint = true;
132136
}
133-
137+
134138
if (debugRepaintRainbowEnabled != null && debugRepaintRainbowEnabled != D.debugRepaintRainbowEnabled) {
135139
D.debugRepaintRainbowEnabled = debugRepaintRainbowEnabled.Value;
136140
needRepaint = true;
@@ -147,7 +151,7 @@ public static void setDebugPaint(bool? debugPaintSizeEnabled = null,
147151
[Serializable]
148152
public class AssertionError : Exception {
149153
readonly Exception innerException;
150-
154+
151155
public AssertionError(string message) : base(message) {
152156
}
153157

@@ -160,7 +164,7 @@ public override string StackTrace {
160164
if (this.innerException != null) {
161165
return this.innerException.StackTrace;
162166
}
163-
167+
164168
var stackTrace = base.StackTrace;
165169
var lines = stackTrace.Split('\n');
166170
var strippedLines = lines.Skip(1);

Runtime/gestures/binding.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void _handlePointerEvent(PointerEvent evt) {
6464
this._handlePointerHoverEvent(evt);
6565
}
6666

67-
HitTestResult result;
67+
HitTestResult result = null;
6868
if (evt is PointerDownEvent) {
6969
D.assert(!this._hitTests.ContainsKey(evt.pointer));
7070
result = new HitTestResult();
@@ -86,11 +86,20 @@ void _handlePointerEvent(PointerEvent evt) {
8686
else if (evt.down) {
8787
result = this._hitTests.getOrDefault(evt.pointer);
8888
}
89-
else {
90-
return;
91-
}
9289

93-
if (result != null) {
90+
D.assert(() => {
91+
if (D.debugPrintMouseHoverEvents && evt is PointerHoverEvent) {
92+
Debug.LogFormat("{0}", evt);
93+
}
94+
95+
return true;
96+
});
97+
98+
if (result != null ||
99+
evt is PointerHoverEvent ||
100+
evt is PointerAddedEvent ||
101+
evt is PointerRemovedEvent
102+
) {
94103
this.dispatchEvent(evt, result);
95104
}
96105
}
@@ -154,6 +163,27 @@ public virtual void hitTest(HitTestResult result, Offset position) {
154163
}
155164

156165
public void dispatchEvent(PointerEvent evt, HitTestResult result) {
166+
if (result == null) {
167+
D.assert(evt is PointerHoverEvent || evt is PointerAddedEvent || evt is PointerRemovedEvent);
168+
try {
169+
this.pointerRouter.route(evt);
170+
}
171+
catch (Exception ex) {
172+
UIWidgetsError.reportError(new UIWidgetsErrorDetails(
173+
exception: ex,
174+
library: "gesture library",
175+
context: "while dispatching a non-hit-tested pointer event",
176+
informationCollector: information => {
177+
information.AppendLine("Event: ");
178+
information.AppendFormat(" {0}", evt);
179+
}
180+
)
181+
);
182+
}
183+
184+
return;
185+
}
186+
157187
foreach (HitTestEntry entry in result.path) {
158188
try {
159189
entry.target.handleEvent(evt, entry);

Runtime/gestures/converter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public override string ToString() {
5656
public static class PointerEventConverter {
5757
static readonly Dictionary<int, _PointerState> _pointers = new Dictionary<int, _PointerState>();
5858

59+
static void clearPointers() {
60+
_pointers.Clear();
61+
}
62+
5963
static _PointerState _ensureStateForPointer(PointerData datum, Offset position) {
6064
return _pointers.putIfAbsent(
6165
datum.device,
@@ -152,7 +156,7 @@ public static IEnumerable<PointerEvent> expand(IEnumerable<PointerData> data, fl
152156
if (state == null || !state.down) {
153157
break;
154158
}
155-
159+
156160
D.assert(state.down);
157161
if (position != state.lastPosition) {
158162
Offset offset = position - state.lastPosition;

Runtime/gestures/monodrag.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public abstract class DragGestureRecognizer : OneSequenceGestureRecognizer {
1919
public DragGestureRecognizer(
2020
object debugOwner = null,
2121
PointerDeviceKind? kind = null,
22-
DragStartBehavior dragStartBehavior = DragStartBehavior.start)
22+
DragStartBehavior dragStartBehavior = DragStartBehavior.down)
2323
: base(debugOwner: debugOwner, kind: kind) {
2424
this.dragStartBehavior = dragStartBehavior;
2525
}
@@ -264,6 +264,11 @@ public override void dispose() {
264264
this._velocityTrackers.Clear();
265265
base.dispose();
266266
}
267+
268+
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
269+
base.debugFillProperties(properties);
270+
properties.add(new EnumProperty<DragStartBehavior>("start behavior", this.dragStartBehavior));
271+
}
267272
}
268273

269274
public class VerticalDragGestureRecognizer : DragGestureRecognizer {

Runtime/gestures/recognizer.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,20 +194,35 @@ protected void stopTrackingIfPointerNoLongerDown(PointerEvent evt) {
194194
public enum GestureRecognizerState {
195195
ready,
196196
possible,
197+
accepted,
197198
defunct,
198199
}
199200

200201
public abstract class PrimaryPointerGestureRecognizer : OneSequenceGestureRecognizer {
201202
protected PrimaryPointerGestureRecognizer(
202203
TimeSpan? deadline = null,
203204
object debugOwner = null,
204-
PointerDeviceKind? kind = null
205+
PointerDeviceKind? kind = null,
206+
float? preAcceptSlotTolerance = Constants.kTouchSlop,
207+
float? postAcceptSlotTolerance = Constants.kTouchSlop
205208
) : base(debugOwner: debugOwner, kind: kind) {
209+
D.assert(preAcceptSlotTolerance == null || preAcceptSlotTolerance >= 0,
210+
"The preAcceptSlopTolerance must be positive or null");
211+
212+
D.assert(postAcceptSlotTolerance == null || postAcceptSlotTolerance >= 0,
213+
"The postAcceptSlopTolerance must be positive or null");
214+
206215
this.deadline = deadline;
216+
this.preAcceptSlotTolerance = preAcceptSlotTolerance;
217+
this.postAcceptSlotTolerance = postAcceptSlotTolerance;
207218
}
208219

209220
public readonly TimeSpan? deadline;
210221

222+
public readonly float? preAcceptSlotTolerance;
223+
224+
public readonly float? postAcceptSlotTolerance;
225+
211226
public GestureRecognizerState state = GestureRecognizerState.ready;
212227

213228
public int primaryPointer;
@@ -230,8 +245,16 @@ public override void addAllowedPointer(PointerDownEvent evt) {
230245

231246
protected override void handleEvent(PointerEvent evt) {
232247
D.assert(this.state != GestureRecognizerState.ready);
233-
if (this.state == GestureRecognizerState.possible && evt.pointer == this.primaryPointer) {
234-
if (evt is PointerMoveEvent && this._getDistance(evt) > Constants.kTouchSlop) {
248+
249+
if (evt.pointer == this.primaryPointer) {
250+
bool isPreAcceptSlopPastTolerance = this.state == GestureRecognizerState.possible &&
251+
this.preAcceptSlotTolerance != null &&
252+
this._getDistance(evt) > this.preAcceptSlotTolerance;
253+
bool isPostAcceptSlopPastTolerance = this.state == GestureRecognizerState.accepted &&
254+
this.postAcceptSlotTolerance != null &&
255+
this._getDistance(evt) > this.postAcceptSlotTolerance;
256+
257+
if (evt is PointerMoveEvent && (isPreAcceptSlopPastTolerance || isPostAcceptSlopPastTolerance)) {
235258
this.resolve(GestureDisposition.rejected);
236259
this.stopTrackingPointer(this.primaryPointer);
237260
}
@@ -249,8 +272,15 @@ protected virtual void didExceedDeadline() {
249272
D.assert(this.deadline == null);
250273
}
251274

252-
public override void rejectGesture(int pointer) {
275+
public override void acceptGesture(int pointer) {
253276
if (pointer == this.primaryPointer && this.state == GestureRecognizerState.possible) {
277+
this.state = GestureRecognizerState.accepted;
278+
}
279+
}
280+
281+
public override void rejectGesture(int pointer) {
282+
if (pointer == this.primaryPointer && (this.state == GestureRecognizerState.possible ||
283+
this.state == GestureRecognizerState.accepted)) {
254284
this._stopTimer();
255285
this.state = GestureRecognizerState.defunct;
256286
}

0 commit comments

Comments
 (0)