浏览代码

Merge pull request #275 from IIzzaya/hook1.5.4

[1.5.4] Upgrade gestures.
/main
GitHub 5 年前
当前提交
0bf76cfb
共有 9 个文件被更改,包括 364 次插入48 次删除
  1. 10
      Runtime/gestures/binding.cs
  2. 2
      Runtime/gestures/constants.cs
  3. 14
      Runtime/gestures/converter.cs
  4. 55
      Runtime/gestures/events.cs
  5. 240
      Runtime/gestures/multitap.cs
  6. 6
      Runtime/gestures/scale.cs
  7. 23
      Runtime/gestures/tap.cs
  8. 51
      Runtime/gestures/pointer_signal_resolver.cs
  9. 11
      Runtime/gestures/pointer_signal_resolver.cs.meta

10
Runtime/gestures/binding.cs


public readonly GestureArenaManager gestureArena;
public readonly PointerSignalResolver pointerSignalResolver = new PointerSignalResolver();
public readonly Dictionary<int, HitTestResult> _hitTests = new Dictionary<int, HitTestResult>();
public readonly HashSet<HitTestTarget> lastMoveTargets = new HashSet<HitTestTarget>();

}
HitTestResult hitTestResult = null;
if (evt is PointerDownEvent) {
if (evt is PointerDownEvent || evt is PointerSignalResolver) {
if (evt is PointerDownEvent) {
this._hitTests[evt.pointer] = hitTestResult;
}
this._hitTests[evt.pointer] = hitTestResult;
D.assert(() => {

}
else if (evt is PointerUpEvent) {
this.gestureArena.sweep(evt.pointer);
}
else if (evt is PointerSignalEvent) {
this.pointerSignalResolver.resolve((PointerSignalEvent) evt);
}
}
}

2
Runtime/gestures/constants.cs


public static readonly TimeSpan kDoubleTapTimeout = new TimeSpan(0, 0, 0, 0, 300);
public static readonly TimeSpan kDoubleTapMinTime = new TimeSpan(0, 0, 0, 0, 40);
public static readonly TimeSpan kLongPressTimeout = new TimeSpan(0, 0, 0, 0, 500);
public const float kMinFlingVelocity = 50.0f;

14
Runtime/gestures/converter.cs


public Offset lastPosition;
public Offset deltaTo(Offset to) {
return to - this.lastPosition;
}
public int _synthesiseDownButtons(int buttons, PointerDeviceKind kind) {
switch (kind) {
case PointerDeviceKind.touch:
return buttons;
default:
return buttons;
}
}
public static class PointerEventConverter {
static readonly Dictionary<int, _PointerState> _pointers = new Dictionary<int, _PointerState>();

55
Runtime/gestures/events.cs


kind: kind,
device: device,
position: position
) {
}
) { }
}
public class PointerRemovedEvent : PointerEvent {

timeStamp: timeStamp,
kind: kind,
device: device
) {
}
) { }
}
public class PointerScrollEvent : PointerEvent {

device: device,
position: position,
down: true,
delta: delta) {
}
delta: delta) { }
}
public class PointerDragFromEditorEnterEvent : PointerEvent {

kind: kind,
device: device,
position: position
) {
}
) { }
public static PointerDragFromEditorEnterEvent fromDragFromEditorEvent(PointerEvent evt) {
return new PointerDragFromEditorEnterEvent(

kind: kind,
device: device,
position: position
) {
}
) { }
public static PointerDragFromEditorExitEvent fromDragFromEditorEvent(PointerEvent evt) {
return new PointerDragFromEditorExitEvent(

kind: kind,
device: device,
position: position
) {
}
) { }
public static PointerDragFromEditorHoverEvent fromDragFromEditorEvent(PointerEvent evt) {
return new PointerDragFromEditorHoverEvent(

kind: kind,
device: device,
position: position,
down: false) {
}
down: false) { }
public static PointerHoverEvent fromHoverEvent(PointerEvent hover) {
return new PointerHoverEvent(

kind: kind,
device: device,
position: position,
down: false) {
}
down: false) { }
public static PointerEnterEvent fromHoverEvent(PointerEvent hover) {
return new PointerEnterEvent(

kind: kind,
device: device,
position: position,
down: false) {
}
down: false) { }
public static PointerExitEvent fromHoverEvent(PointerEvent hover) {
return new PointerExitEvent(

kind: kind,
device: device,
position: position,
down: true) {
}
down: true) { }
}
public class PointerMoveEvent : PointerEvent {

position: position,
delta: delta,
down: true,
synthesized: synthesized) {
}
synthesized: synthesized) { }
}
public class PointerUpEvent : PointerEvent {

kind: kind,
device: device,
position: position,
down: false) {
}
down: false) { }
}
public class PointerCancelEvent : PointerEvent {

kind: kind,
device: device,
position: position,
down: false) {
}
down: false) { }
}
public class PointerSignalEvent : PointerEvent {
public PointerSignalEvent(
TimeSpan timeStamp,
int pointer = 0,
PointerDeviceKind kind = PointerDeviceKind.mouse,
int device = 0,
Offset position = null
) : base(
timeStamp,
pointer: pointer,
kind: kind,
device: device,
position: position
) { }
}
}

240
Runtime/gestures/multitap.cs


using System;
using System.Collections.Generic;
using Unity.UIWidgets.async;
using Unity.UIWidgets.foundation;

public delegate void GestureDoubleTapCallback(DoubleTapDetails details);
public delegate void GestureMultiTapDownCallback(int pointer, TapDownDetails details);
public delegate void GestureMultiTapUpCallback(int pointer, TapUpDetails details);
public delegate void GestureMultiTapCallback(int pointer);
public delegate void GestureMultiTapCancelCallback(int pointer);
public class _CountdownZoned {
_CountdownZoned(TimeSpan duration) {
D.assert(duration != null);
this._timer = Window.instance.run(duration, this._onTimeout);
}
public bool _timeout = false;
public Timer _timer;
public bool timeout {
get { return this._timeout; }
}
void _onTimeout() {
this._timeout = true;
}
}
public class DoubleTapDetails {
public DoubleTapDetails(Offset firstGlobalPosition = null) {
this.firstGlobalPosition = firstGlobalPosition ?? Offset.zero;

class _TapTracker {
internal _TapTracker(
PointerDownEvent evt = null,
GestureArenaEntry entry = null) {
PointerDownEvent evt,
TimeSpan doubleTapMinTime,
GestureArenaEntry entry = null
) {
this.pointer = evt.pointer;
this._initialPosition = evt.position;
this.entry = entry;

public readonly GestureArenaEntry entry;
internal readonly Offset _initialPosition;
internal readonly _CountdownZoned _doubleTapMinTimeCountdown;
bool _isTrackingPointer = false;

}
}
public void stopTrackingPointer(PointerRoute route) {
public virtual void stopTrackingPointer(PointerRoute route) {
if (this._isTrackingPointer) {
this._isTrackingPointer = false;
GestureBinding.instance.pointerRouter.removeRoute(this.pointer, route);

Offset offset = evt.position - this._initialPosition;
return offset.distance <= tolerance;
}
public bool hasElapsedMinTime() {
return this._doubleTapMinTimeCountdown.timeout;
}
: base(debugOwner: debugOwner, kind: kind) {
}
: base(debugOwner: debugOwner, kind: kind) { }
public GestureDoubleTapCallback onDoubleTap;

this._stopDoubleTapTimer();
_TapTracker tracker = new _TapTracker(
evt: evt,
entry: GestureBinding.instance.gestureArena.add(evt.pointer, this)
entry: GestureBinding.instance.gestureArena.add(evt.pointer, this),
doubleTapMinTime: Constants.kDoubleTapMinTime
);
this._trackers[evt.pointer] = tracker;
tracker.startTrackingPointer(this._handleEvent);

}
}
public override void acceptGesture(int pointer) {
}
public override void acceptGesture(int pointer) { }
public override void rejectGesture(int pointer) {
_TapTracker tracker;

foreach (var tracker in this._trackers.Values) {
this._reject(tracker);
}
D.assert(this._trackers.isEmpty());
}

public override string debugDescription {
get { return "double tap"; }
}
}
class _TapGesture : _TapTracker {
public _TapGesture(
MultiTapGestureRecognizer gestureRecognizer,
PointerEvent evt,
TimeSpan longTapDelay
) : base(
evt: (PointerDownEvent) evt,
entry: GestureBinding.instance.gestureArena.add(evt.pointer, gestureRecognizer),
doubleTapMinTime: Constants.kDoubleTapMinTime
) {
this.gestureRecognizer = gestureRecognizer;
this._lastPosition = evt.position;
this.startTrackingPointer(this.handleEvent);
if (longTapDelay > TimeSpan.Zero) {
this._timer = Window.instance.run(longTapDelay, () => {
this._timer = null;
this.gestureRecognizer._dispatchLongTap(evt.pointer, this._lastPosition);
});
}
}
public readonly MultiTapGestureRecognizer gestureRecognizer;
bool _wonArena = false;
Timer _timer;
Offset _lastPosition;
Offset _finalPosition;
void handleEvent(PointerEvent evt) {
D.assert(evt.pointer == this.pointer);
if (evt is PointerMoveEvent) {
if (!this.isWithinTolerance(evt, Constants.kTouchSlop)) {
this.cancel();
}
else {
this._lastPosition = evt.position;
}
}
else if (evt is PointerCancelEvent) {
this.cancel();
}
else if (evt is PointerUpEvent) {
this.stopTrackingPointer(this.handleEvent);
this._finalPosition = evt.position;
this._check();
}
}
public override void stopTrackingPointer(PointerRoute route) {
this._timer?.cancel();
this._timer = null;
base.stopTrackingPointer(route);
}
public void accept() {
this._wonArena = true;
this._check();
}
public void reject() {
this.stopTrackingPointer(this.handleEvent);
this.gestureRecognizer._dispatchCancel(this.pointer);
}
public void cancel() {
if (this._wonArena) {
this.reject();
}
else {
this.entry.resolve(GestureDisposition.rejected);
}
}
void _check() {
if (this._wonArena && this._finalPosition != null) {
this.gestureRecognizer._dispatchTap(this.pointer, this._finalPosition);
}
}
}
public class MultiTapGestureRecognizer : GestureRecognizer {
public MultiTapGestureRecognizer(
TimeSpan? longTapDelay = null,
object debugOwner = null,
PointerDeviceKind? kind = null
) : base(debugOwner: debugOwner, kind: kind) {
this.longTapDelay = longTapDelay ?? TimeSpan.Zero;
}
GestureMultiTapDownCallback onTapDown;
GestureMultiTapUpCallback onTapUp;
GestureMultiTapCallback onTap;
GestureMultiTapCancelCallback onTapCancel;
TimeSpan longTapDelay;
GestureMultiTapDownCallback onLongTapDown;
readonly Dictionary<int, _TapGesture> _gestureMap = new Dictionary<int, _TapGesture>();
public override void addAllowedPointer(PointerDownEvent evt) {
D.assert(!this._gestureMap.ContainsKey(evt.pointer));
this._gestureMap[evt.pointer] = new _TapGesture(
gestureRecognizer: this,
evt: evt,
longTapDelay: this.longTapDelay
);
if (this.onTapDown != null) {
this.invokeCallback<object>("onTapDown", () => {
this.onTapDown(evt.pointer, new TapDownDetails(globalPosition: evt.position));
return null;
});
}
}
public override void acceptGesture(int pointer) {
D.assert(this._gestureMap.ContainsKey(pointer));
this._gestureMap[pointer].accept();
}
public override void rejectGesture(int pointer) {
D.assert(this._gestureMap.ContainsKey(pointer));
this._gestureMap[pointer].reject();
D.assert(!this._gestureMap.ContainsKey(pointer));
}
public void _dispatchCancel(int pointer) {
D.assert(this._gestureMap.ContainsKey(pointer));
this._gestureMap.Remove(pointer);
if (this.onTapCancel != null) {
this.invokeCallback<object>("onTapCancel", () => {
this.onTapCancel(pointer);
return null;
});
}
}
public void _dispatchTap(int pointer, Offset globalPosition) {
D.assert(this._gestureMap.ContainsKey(pointer));
this._gestureMap.Remove(pointer);
if (this.onTapUp != null) {
this.invokeCallback<object>("onTapUp",
() => {
this.onTapUp(pointer, new TapUpDetails(globalPosition: globalPosition));
return null;
});
}
if (this.onTap != null) {
this.invokeCallback<object>("onTap", () => {
this.onTap(pointer);
return null;
});
}
}
public void _dispatchLongTap(int pointer, Offset lastPosition) {
D.assert(this._gestureMap.ContainsKey(pointer));
if (this.onLongTapDown != null) {
this.invokeCallback<object>("onLongTapDown",
() => {
this.onLongTapDown(pointer, new TapDownDetails(globalPosition: lastPosition));
return null;
});
}
}
public override void dispose() {
List<_TapGesture> localGestures = new List<_TapGesture>();
foreach (var item in this._gestureMap) {
localGestures.Add(item.Value);
}
foreach (_TapGesture gesture in localGestures) {
gesture.cancel();
}
D.assert(this._gestureMap.isEmpty);
base.dispose();
}
public override string debugDescription {
get { return "multitap"; }
}
}
}

6
Runtime/gestures/scale.cs


public class ScaleGestureRecognizer : OneSequenceGestureRecognizer {
public ScaleGestureRecognizer(object debugOwner) : base(debugOwner: debugOwner) {
}
public ScaleGestureRecognizer(object debugOwner, PointerDeviceKind? kind = null) : base(debugOwner: debugOwner,
kind: kind) { }
public GestureScaleStartCallback onStart;

get { return "scale"; }
}
}
}
}

23
Runtime/gestures/tap.cs


public class TapGestureRecognizer : PrimaryPointerGestureRecognizer {
public TapGestureRecognizer(object debugOwner = null)
: base(deadline: Constants.kPressTimeout, debugOwner: debugOwner) {
}
: base(deadline: Constants.kPressTimeout, debugOwner: debugOwner) { }
public GestureTapDownCallback onTapDown;

protected override void handlePrimaryPointer(PointerEvent evt) {
if (evt is PointerUpEvent) {
this._finalPosition = evt.position;
this._checkUp();
if (this._wonArenaForPrimaryPointer) {
this.resolve(GestureDisposition.accepted);
this._checkUp();
}
if (this._sentTapDown && this.onTapCancel != null) {
this.invokeCallback<object>("onTapCancel", () => this.onTapCancel);
}
this._reset();
}
}

D.assert(this._sentTapDown);
if (this.onTapCancel != null) {
this.invokeCallback<object>("spontaneous onTapCancel", () => {
this.onTapCancel();

public override void rejectGesture(int pointer) {
base.rejectGesture(pointer);
if (pointer == this.primaryPointer) {
if (this.onTapCancel != null) {
if (this._sentTapDown && this.onTapCancel != null) {
this.invokeCallback<object>("forced onTapCancel", () => {
this.onTapCancel();
return null;

}
void _checkUp() {
if (this._wonArenaForPrimaryPointer && this._finalPosition != null) {
this.resolve(GestureDisposition.accepted);
if (!this._wonArenaForPrimaryPointer || this._finalPosition == null) {
return;
}
if (this._finalPosition != null) {
if (this.onTapUp != null) {
this.invokeCallback<object>("onTapUp", () => {
this.onTapUp(new TapUpDetails(globalPosition: this._finalPosition));

51
Runtime/gestures/pointer_signal_resolver.cs


using System;
using Unity.UIWidgets.foundation;
namespace Unity.UIWidgets.gestures {
public delegate void PointerSignalResolvedCallback(PointerSignalEvent evt);
public class PointerSignalResolver {
PointerSignalResolvedCallback _firstRegisteredCallback;
PointerSignalEvent _currentEvent;
public void register(PointerSignalEvent evt, PointerSignalResolvedCallback callback) {
D.assert(evt != null);
D.assert(callback != null);
D.assert(this._currentEvent == null || this._currentEvent == evt);
if (this._firstRegisteredCallback != null) {
return;
}
this._currentEvent = evt;
this._firstRegisteredCallback = callback;
}
public void resolve(PointerSignalEvent evt) {
if (this._firstRegisteredCallback == null) {
D.assert(this._currentEvent == null);
return;
}
D.assert(this._currentEvent == evt);
try {
this._firstRegisteredCallback(evt);
}
catch (Exception exception) {
UIWidgetsError.reportError(new UIWidgetsErrorDetails(
exception: exception,
library: "gesture library",
context: "while resolving a PointerSignalEvent",
informationCollector: information => {
information.AppendLine("Event: ");
information.AppendFormat(" {0}", evt);
}
)
);
}
this._firstRegisteredCallback = null;
this._currentEvent = null;
}
}
}

11
Runtime/gestures/pointer_signal_resolver.cs.meta


fileFormatVersion: 2
guid: 95a50dc2c902d46c5aea687bc4ecd1b8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存