iizzaya
5 年前
当前提交
4f3274d0
共有 14 个文件被更改,包括 695 次插入 和 34 次删除
-
24Runtime/editor/editor_window.cs
-
10Runtime/gestures/binding.cs
-
25Runtime/gestures/converter.cs
-
123Runtime/gestures/events.cs
-
98Runtime/gestures/mouse_tracking.cs
-
98Runtime/rendering/proxy_box.cs
-
4Runtime/ui/pointer.cs
-
40Runtime/widgets/basic.cs
-
189Runtime/editor/editor_mouse_tracking.cs
-
11Runtime/editor/editor_mouse_tracking.cs.meta
-
8Runtime/editor/widgets.meta
-
88Runtime/editor/widgets/unity_object_detector.cs
-
11Runtime/editor/widgets/unity_object_detector.cs.meta
|
|||
using System.Collections.Generic; |
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.scheduler; |
|||
using UnityEditor; |
|||
|
|||
namespace Unity.UIWidgets.gestures { |
|||
public partial class MouseTracker { |
|||
bool _enableDragFromEditorRelease = false; |
|||
|
|||
void _handleDragFromEditorEvent(PointerEvent evt, int deviceId) { |
|||
if (evt is PointerDragFromEditorReleaseEvent) { |
|||
this._enableDragFromEditorRelease = false; |
|||
this._scheduleDragFromEditorReleaseCheck(); |
|||
this._lastMouseEvent.Remove(deviceId); |
|||
} |
|||
else if (evt is PointerDragFromEditorEnterEvent || |
|||
evt is PointerDragFromEditorHoverEvent || |
|||
evt is PointerDragFromEditorExitEvent) { |
|||
if (!this._lastMouseEvent.ContainsKey(deviceId) || |
|||
this._lastMouseEvent[deviceId].position != evt.position) { |
|||
// Only schedule a frame if we have our first event, or if the
|
|||
// location of the mouse has changed, and only if there are tracked annotations.
|
|||
this._scheduleDragFromEditorMousePositionCheck(); |
|||
} |
|||
|
|||
this._lastMouseEvent[deviceId] = evt; |
|||
} |
|||
} |
|||
|
|||
void _scheduleDragFromEditorReleaseCheck() { |
|||
DragAndDrop.AcceptDrag(); |
|||
|
|||
var lastMouseEvent = new List<(PointerEvent, int)>(); |
|||
foreach (int deviceId in this._lastMouseEvent.Keys) { |
|||
lastMouseEvent.Add((this._lastMouseEvent[deviceId], deviceId)); |
|||
} |
|||
|
|||
SchedulerBinding.instance.addPostFrameCallback(_ => { |
|||
foreach (var lastEvent in lastMouseEvent) { |
|||
MouseTrackerAnnotation hit = this.annotationFinder(lastEvent.Item1.position); |
|||
|
|||
if (hit == null) { |
|||
foreach (_TrackedAnnotation trackedAnnotation in this._trackedAnnotations.Values) { |
|||
if (trackedAnnotation.activeDevices.Contains(lastEvent.Item2)) { |
|||
trackedAnnotation.activeDevices.Remove(lastEvent.Item2); |
|||
} |
|||
} |
|||
|
|||
return; |
|||
} |
|||
|
|||
_TrackedAnnotation hitAnnotation = this._findAnnotation(hit); |
|||
|
|||
// release
|
|||
if (hitAnnotation.activeDevices.Contains(lastEvent.Item2)) { |
|||
if (hitAnnotation.annotation?.onDragFromEditorRelease != null) { |
|||
hitAnnotation.annotation.onDragFromEditorRelease( |
|||
PointerDragFromEditorReleaseEvent |
|||
.fromDragFromEditorEvent( |
|||
lastEvent.Item1, DragAndDrop.objectReferences)); |
|||
} |
|||
|
|||
hitAnnotation.activeDevices.Remove(lastEvent.Item2); |
|||
} |
|||
} |
|||
}); |
|||
SchedulerBinding.instance.scheduleFrame(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Due to the [DragAndDrop] property, DragAndDrop.visualMode must be set to Copy
|
|||
/// after which editor window can trigger DragPerform event.
|
|||
/// And because visualMode will be set to None when every frame finished in IMGUI,
|
|||
/// here we start a scheduler to update VisualMode in every post frame.
|
|||
/// When [_enableDragFromEditorRelease] set to false, it will stop, vice versa.
|
|||
/// </summary>
|
|||
void _enableDragFromEditorReleaseVisualModeLoop() { |
|||
if (this._enableDragFromEditorRelease) { |
|||
DragAndDrop.visualMode = DragAndDropVisualMode.Copy; |
|||
SchedulerBinding.instance.addPostFrameCallback(_ => { |
|||
this._enableDragFromEditorReleaseVisualModeLoop(); |
|||
}); |
|||
SchedulerBinding.instance.scheduleFrame(); |
|||
} |
|||
} |
|||
|
|||
void _scheduleDragFromEditorMousePositionCheck() { |
|||
SchedulerBinding.instance.addPostFrameCallback(_ => { this.collectDragFromEditorMousePositions(); }); |
|||
SchedulerBinding.instance.scheduleFrame(); |
|||
} |
|||
|
|||
public void collectDragFromEditorMousePositions() { |
|||
void exitAnnotation(_TrackedAnnotation trackedAnnotation, int deviceId) { |
|||
if (trackedAnnotation.activeDevices.Contains(deviceId)) { |
|||
this._enableDragFromEditorRelease = false; |
|||
if (trackedAnnotation.annotation?.onDragFromEditorExit != null) { |
|||
trackedAnnotation.annotation.onDragFromEditorExit( |
|||
PointerDragFromEditorExitEvent.fromDragFromEditorEvent( |
|||
this._lastMouseEvent[deviceId])); |
|||
} |
|||
|
|||
trackedAnnotation.activeDevices.Remove(deviceId); |
|||
} |
|||
} |
|||
|
|||
void exitAllDevices(_TrackedAnnotation trackedAnnotation) { |
|||
if (trackedAnnotation.activeDevices.isNotEmpty()) { |
|||
HashSet<int> deviceIds = new HashSet<int>(trackedAnnotation.activeDevices); |
|||
foreach (int deviceId in deviceIds) { |
|||
exitAnnotation(trackedAnnotation, deviceId); |
|||
} |
|||
} |
|||
} |
|||
|
|||
if (!this.mouseIsConnected) { |
|||
foreach (var annotation in this._trackedAnnotations.Values) { |
|||
exitAllDevices(annotation); |
|||
} |
|||
|
|||
return; |
|||
} |
|||
|
|||
foreach (int deviceId in this._lastMouseEvent.Keys) { |
|||
PointerEvent lastEvent = this._lastMouseEvent[deviceId]; |
|||
MouseTrackerAnnotation hit = this.annotationFinder(lastEvent.position); |
|||
|
|||
if (hit == null) { |
|||
foreach (_TrackedAnnotation trackedAnnotation in this._trackedAnnotations.Values) { |
|||
exitAnnotation(trackedAnnotation, deviceId); |
|||
} |
|||
|
|||
return; |
|||
} |
|||
|
|||
_TrackedAnnotation hitAnnotation = this._findAnnotation(hit); |
|||
|
|||
// While acrossing two areas, set the flag to true to prevent setting the Pointer Copy VisualMode to None
|
|||
bool enterFlag = false; |
|||
|
|||
// enter
|
|||
if (!hitAnnotation.activeDevices.Contains(deviceId)) { |
|||
hitAnnotation.activeDevices.Add(deviceId); |
|||
enterFlag = true; |
|||
// Both onRelease or onEnter event will enable Copy VisualMode
|
|||
if (hitAnnotation.annotation?.onDragFromEditorRelease != null || |
|||
hitAnnotation.annotation?.onDragFromEditorEnter != null) { |
|||
if (!this._enableDragFromEditorRelease) { |
|||
this._enableDragFromEditorRelease = true; |
|||
this._enableDragFromEditorReleaseVisualModeLoop(); |
|||
} |
|||
|
|||
if (hitAnnotation.annotation?.onDragFromEditorEnter != null) { |
|||
hitAnnotation.annotation.onDragFromEditorEnter( |
|||
PointerDragFromEditorEnterEvent |
|||
.fromDragFromEditorEvent(lastEvent)); |
|||
} |
|||
} |
|||
} |
|||
|
|||
// hover
|
|||
if (hitAnnotation.annotation?.onDragFromEditorHover != null) { |
|||
hitAnnotation.annotation.onDragFromEditorHover( |
|||
PointerDragFromEditorHoverEvent.fromDragFromEditorEvent(lastEvent)); |
|||
} |
|||
|
|||
// leave
|
|||
foreach (_TrackedAnnotation trackedAnnotation in this._trackedAnnotations.Values) { |
|||
if (hitAnnotation == trackedAnnotation) { |
|||
continue; |
|||
} |
|||
|
|||
if (trackedAnnotation.activeDevices.Contains(deviceId)) { |
|||
if (!enterFlag) { |
|||
this._enableDragFromEditorRelease = false; |
|||
} |
|||
|
|||
if (trackedAnnotation.annotation?.onDragFromEditorExit != null) { |
|||
trackedAnnotation.annotation.onDragFromEditorExit( |
|||
PointerDragFromEditorExitEvent |
|||
.fromDragFromEditorEvent(lastEvent)); |
|||
} |
|||
|
|||
trackedAnnotation.activeDevices.Remove(deviceId); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 08df36a525b2c486aaf1c342786f08db |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: d5417058cad6148859c8ffde0f0f054b |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.gestures; |
|||
using Unity.UIWidgets.rendering; |
|||
using Unity.UIWidgets.widgets; |
|||
using UnityEngine; |
|||
using Object = UnityEngine.Object; |
|||
|
|||
namespace Unity.UIWidgets.editor { |
|||
public delegate void DragFromEditorEnterCallback(); |
|||
|
|||
public delegate void DragFromEditorHoverCallback(); |
|||
|
|||
public delegate void DragFromEditorExitCallback(); |
|||
|
|||
public delegate void DragFromEditorReleaseCallback(DragFromEditorDetails details); |
|||
|
|||
public class DragFromEditorDetails { |
|||
public DragFromEditorDetails(Object[] objectReferences) { |
|||
this.objectReferences = objectReferences; |
|||
} |
|||
|
|||
public readonly Object[] objectReferences; |
|||
} |
|||
|
|||
public class UnityObjectDetector : StatefulWidget { |
|||
public UnityObjectDetector( |
|||
Key key = null, |
|||
Widget child = null, |
|||
DragFromEditorEnterCallback onEnter = null, |
|||
DragFromEditorHoverCallback onHover = null, |
|||
DragFromEditorExitCallback onExit = null, |
|||
DragFromEditorReleaseCallback onRelease = null, |
|||
HitTestBehavior? behavior = null |
|||
) : base(key: key) { |
|||
this.child = child; |
|||
this.onDragFromEditorEnter = onEnter; |
|||
this.onDragFromEditorHover = onHover; |
|||
this.onDragFromEditorExit = onExit; |
|||
this.onDragFromEditorRelease = onRelease; |
|||
this.behavior = behavior; |
|||
} |
|||
|
|||
public readonly Widget child; |
|||
|
|||
public readonly DragFromEditorEnterCallback onDragFromEditorEnter; |
|||
public readonly DragFromEditorHoverCallback onDragFromEditorHover; |
|||
public readonly DragFromEditorExitCallback onDragFromEditorExit; |
|||
public readonly DragFromEditorReleaseCallback onDragFromEditorRelease; |
|||
|
|||
public readonly HitTestBehavior? behavior; |
|||
|
|||
public override State createState() { |
|||
return new UnityObjectDetectorState(); |
|||
} |
|||
} |
|||
|
|||
public class UnityObjectDetectorState : State<UnityObjectDetector> { |
|||
HitTestBehavior _defaultBehavior { |
|||
get { return this.widget.child == null ? HitTestBehavior.translucent : HitTestBehavior.deferToChild; } |
|||
} |
|||
|
|||
public override Widget build(BuildContext context) { |
|||
Widget result = new Listener( |
|||
child: this.widget.child, |
|||
onPointerDragFromEditorEnter: this.widget.onDragFromEditorEnter == null |
|||
? ((PointerDragFromEditorEnterEventListener) null) |
|||
: (evt) => { this.widget.onDragFromEditorEnter.Invoke(); }, |
|||
onPointerDragFromEditorHover: this.widget.onDragFromEditorHover == null |
|||
? ((PointerDragFromEditorHoverEventListener) null) |
|||
: (evt) => { this.widget.onDragFromEditorHover.Invoke(); }, |
|||
onPointerDragFromEditorExit: this.widget.onDragFromEditorExit == null |
|||
? ((PointerDragFromEditorExitEventListener) null) |
|||
: (evt) => { this.widget.onDragFromEditorExit.Invoke(); }, |
|||
onPointerDragFromEditorRelease: this.widget.onDragFromEditorRelease == null |
|||
? ((PointerDragFromEditorReleaseEventListener) null) |
|||
: (evt) => { |
|||
this.widget.onDragFromEditorRelease.Invoke(new DragFromEditorDetails(evt.objectReferences)); |
|||
}, |
|||
behavior: this.widget.behavior ?? this._defaultBehavior |
|||
); |
|||
return result; |
|||
} |
|||
} |
|||
|
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 899c58f21c1724297b48da615ccb7088 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue