浏览代码
Merge pull request #268 from Unity-Technologies/zxw/support_editor_dragdrop
Merge pull request #268 from Unity-Technologies/zxw/support_editor_dragdrop
Add support to Editor Drag&Drop/main
GitHub
3 年前
当前提交
96bfc5f5
共有 33 个文件被更改,包括 942 次插入 和 3 次删除
-
8com.unity.uiwidgets/Editor/UIWidgetsEditorPanel.cs
-
27com.unity.uiwidgets/Runtime/engine/UIWidgetsPanelWrapper.cs
-
20com.unity.uiwidgets/Runtime/gestures/converter.cs
-
20com.unity.uiwidgets/Runtime/gestures/events.cs
-
2com.unity.uiwidgets/Runtime/ui/pointer.cs
-
2com.unity.uiwidgets/Runtime/widgets/binding.cs
-
2engine/src/lib/ui/window/pointer_data.h
-
8engine/src/lib/ui/window/pointer_data_packet_converter.cc
-
4engine/src/shell/platform/embedder/embedder.cc
-
2engine/src/shell/platform/embedder/embedder.h
-
4engine/src/shell/platform/unity/darwin/macos/uiwidgets_panel.h
-
42engine/src/shell/platform/unity/darwin/macos/uiwidgets_panel.mm
-
43engine/src/shell/platform/unity/windows/uiwidgets_panel.cc
-
5engine/src/shell/platform/unity/windows/uiwidgets_panel.h
-
63Samples/UIWidgetsSamples_2019_4/Assets/Editor/EditorWindowSample/UnityObjectDragging.cs
-
3Samples/UIWidgetsSamples_2019_4/Assets/Editor/EditorWindowSample/UnityObjectDragging.cs.meta
-
8com.unity.uiwidgets/Editor/gesture.meta
-
3com.unity.uiwidgets/Editor/widgets.meta
-
64com.unity.uiwidgets/Editor/gesture/binding.cs
-
3com.unity.uiwidgets/Editor/gesture/binding.cs.meta
-
3com.unity.uiwidgets/Editor/gesture/editor_mouse_tracking.cs.meta
-
260com.unity.uiwidgets/Editor/gesture/editor_mouse_tracking.cs
-
222com.unity.uiwidgets/Editor/widgets/unity_editor_listener.cs
-
3com.unity.uiwidgets/Editor/widgets/unity_editor_listener.cs.meta
-
121com.unity.uiwidgets/Editor/widgets/unity_object_detector.cs
-
3com.unity.uiwidgets/Editor/widgets/unity_object_detector.cs.meta
|
|||
using System.Collections.Generic; |
|||
using uiwidgets; |
|||
using Unity.UIWidgets.Editor; |
|||
using Unity.UIWidgets.gestures; |
|||
using Unity.UIWidgets.widgets; |
|||
using UnityEditor; |
|||
using UnityEngine; |
|||
|
|||
namespace UIWidgetsEditorWindowSample |
|||
{ |
|||
public class UnityObjectDraggingWindow : UIWidgetsEditorPanel |
|||
{ |
|||
[MenuItem("UIWidgets/EditorSample/UnityObjectDragging")] |
|||
public static void CountDemo() |
|||
{ |
|||
CreateWindow<UnityObjectDraggingWindow>(); |
|||
} |
|||
|
|||
protected override void onEnable() |
|||
{ |
|||
AddFont("Material Icons", new List<string> {"MaterialIcons-Regular.ttf"}, new List<int> {0}); |
|||
AddFont("CupertinoIcons", new List<string> {"CupertinoIcons.ttf"}, new List<int> {0}); |
|||
AddFont("GalleryIcons", new List<string> {"gallery/GalleryIcons.ttf"}, new List<int> {0}); |
|||
} |
|||
|
|||
protected override void main() |
|||
{ |
|||
editor_ui_.runEditorApp(new MyApp()); |
|||
} |
|||
|
|||
public class MyApp : StatelessWidget |
|||
{ |
|||
public override Widget build(BuildContext context) |
|||
{ |
|||
return |
|||
new Container(color: Colors.green, child: |
|||
new Center(child: |
|||
new UnityObjectDetector( |
|||
child: new Container(color: Colors.blue, width: 200f, height: 200f, |
|||
child: new Center(child: new Text("Drag UnityObject or file to me"))), |
|||
onEnter: (details) => |
|||
{ |
|||
Debug.Log("enter"); |
|||
}, |
|||
onHover: (details => |
|||
{ |
|||
Debug.Log("position = " + details.position); |
|||
}), |
|||
onExit: () => |
|||
{ |
|||
Debug.Log("on exit"); |
|||
}, |
|||
onRelease: (details) => |
|||
{ |
|||
Debug.Log("on release"); |
|||
} |
|||
) |
|||
) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 9916a1957385427fa807b2ce6a676522 |
|||
timeCreated: 1637912586 |
|
|||
fileFormatVersion: 2 |
|||
guid: acbe72faebec34ea3832cc33152ff6ab |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 3bdd889f92fa4e7a9fc1fa29df736e55 |
|||
timeCreated: 1637914329 |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Unity.UIWidgets.gestures; |
|||
using Unity.UIWidgets.ui; |
|||
|
|||
namespace Unity.UIWidgets.widgets { |
|||
public class UiWidgetsEditorBinding : UiWidgetsBinding { |
|||
public new static UiWidgetsEditorBinding instance { |
|||
get { return (UiWidgetsEditorBinding) UiWidgetsBinding.instance; } |
|||
set { UiWidgetsBinding.instance = value; } |
|||
} |
|||
|
|||
public static UiWidgetsEditorBinding ensureInitializedForEditor() { |
|||
if (UiWidgetsEditorBinding.instance == null) { |
|||
return new UiWidgetsEditorBinding(); |
|||
} |
|||
|
|||
return UiWidgetsEditorBinding.instance; |
|||
} |
|||
|
|||
public EditorMouseTracker editorMouseTracker { |
|||
get { return _editorMouseTracker; } |
|||
} |
|||
|
|||
EditorMouseTracker _editorMouseTracker; |
|||
|
|||
private void initEditorMouseTracker(EditorMouseTracker tracker = null) { |
|||
_editorMouseTracker?.dispose(); |
|||
_editorMouseTracker = tracker ?? new EditorMouseTracker(pointerRouter, hitTestMouseTrackers); |
|||
} |
|||
|
|||
public EditorMouseTrackerAnnotation hitTestMouseTrackers(Offset position) { |
|||
List<EditorMouseTrackerAnnotation> annotations = |
|||
new List<EditorMouseTrackerAnnotation>(renderView.layer.findAllAnnotations<EditorMouseTrackerAnnotation>( |
|||
position * renderView.configuration.devicePixelRatio |
|||
).annotations); |
|||
|
|||
if (annotations is null || annotations.Count == 0) { |
|||
return null; |
|||
} |
|||
|
|||
return annotations[0]; |
|||
} |
|||
|
|||
protected override void initInstances() { |
|||
base.initInstances(); |
|||
|
|||
addPersistentFrameCallback(_handlePersistentFrameCallbackForEditor); |
|||
initEditorMouseTracker(); |
|||
} |
|||
|
|||
private void _handlePersistentFrameCallbackForEditor(TimeSpan timeStamp) { |
|||
_editorMouseTracker.schedulePostFrameCheck(); |
|||
} |
|||
} |
|||
|
|||
public static class editor_ui_ { |
|||
public static void runEditorApp(Widget app) { |
|||
var instance = UiWidgetsEditorBinding.ensureInitializedForEditor(); |
|||
instance.scheduleAttachRootWidget(app); |
|||
instance.scheduleWarmUpFrame(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 4a770ce594324a7eb95b9457697ace16 |
|||
timeCreated: 1637749525 |
|
|||
fileFormatVersion: 2 |
|||
guid: d4a8f193e3d549a8bf5093b001ea327b |
|||
timeCreated: 1637750451 |
|
|||
using System.Collections.Generic; |
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.scheduler; |
|||
using Unity.UIWidgets.ui; |
|||
using UnityEditor; |
|||
|
|||
namespace Unity.UIWidgets.gestures { |
|||
public delegate void PointerDragFromEditorEnterEventListener(PointerDragFromEditorEnterEvent evt); |
|||
|
|||
public delegate void PointerDragFromEditorHoverEventListener(PointerDragFromEditorHoverEvent evt); |
|||
|
|||
public delegate void PointerDragFromEditorExitEventListener(PointerDragFromEditorExitEvent evt); |
|||
|
|||
public delegate void PointerDragFromEditorReleaseEventListener(PointerDragFromEditorReleaseEvent evt); |
|||
|
|||
public delegate EditorMouseTrackerAnnotation EditorMouseDetectorAnnotationFinder(Offset offset); |
|||
|
|||
public class EditorMouseTrackerAnnotation { |
|||
public EditorMouseTrackerAnnotation( |
|||
PointerDragFromEditorEnterEventListener onDragFromEditorEnter = null, |
|||
PointerDragFromEditorHoverEventListener onDragFromEditorHover = null, |
|||
PointerDragFromEditorExitEventListener onDragFromEditorExit = null, |
|||
PointerDragFromEditorReleaseEventListener onDragFromEditorRelease = null |
|||
) { |
|||
this.onDragFromEditorEnter = onDragFromEditorEnter; |
|||
this.onDragFromEditorHover = onDragFromEditorHover; |
|||
this.onDragFromEditorExit = onDragFromEditorExit; |
|||
this.onDragFromEditorRelease = onDragFromEditorRelease; |
|||
} |
|||
|
|||
public readonly PointerDragFromEditorEnterEventListener onDragFromEditorEnter; |
|||
public readonly PointerDragFromEditorHoverEventListener onDragFromEditorHover; |
|||
public readonly PointerDragFromEditorExitEventListener onDragFromEditorExit; |
|||
public readonly PointerDragFromEditorReleaseEventListener onDragFromEditorRelease; |
|||
} |
|||
|
|||
public class _EditorTrackedAnnotation { |
|||
public _EditorTrackedAnnotation(EditorMouseTrackerAnnotation annotation) { |
|||
this.annotation = annotation; |
|||
} |
|||
|
|||
public readonly EditorMouseTrackerAnnotation annotation; |
|||
|
|||
public readonly HashSet<int> activeDevices = new HashSet<int>(); |
|||
} |
|||
|
|||
public class EditorMouseTracker : ChangeNotifier { |
|||
public readonly Dictionary<int, PointerEvent> _lastMouseEvent = new Dictionary<int, PointerEvent>(); |
|||
public bool mouseIsConnected => _lastMouseEvent.isNotEmpty(); |
|||
|
|||
public readonly EditorMouseDetectorAnnotationFinder annotationFinder; |
|||
|
|||
public readonly Dictionary<EditorMouseTrackerAnnotation, _EditorTrackedAnnotation> _trackedAnnotations = |
|||
new Dictionary<EditorMouseTrackerAnnotation, _EditorTrackedAnnotation>(); |
|||
|
|||
public void attachDragFromEditorAnnotation(EditorMouseTrackerAnnotation annotation) { |
|||
_trackedAnnotations[annotation] = new _EditorTrackedAnnotation(annotation); |
|||
_scheduleDragFromEditorMousePositionCheck(); |
|||
} |
|||
|
|||
public void detachDragFromEditorAnnotation(EditorMouseTrackerAnnotation annotation) { |
|||
var trackedAnnotation = _findAnnotation(annotation); |
|||
foreach (var deviceId in trackedAnnotation.activeDevices) { |
|||
annotation.onDragFromEditorExit(PointerDragFromEditorExitEvent.fromDragFromEditorEvent( |
|||
_lastMouseEvent[deviceId] |
|||
)); |
|||
} |
|||
|
|||
_trackedAnnotations.Remove(annotation); |
|||
} |
|||
|
|||
_EditorTrackedAnnotation _findAnnotation(EditorMouseTrackerAnnotation annotation) { |
|||
if (!_trackedAnnotations.TryGetValue(annotation, out var trackedAnnotation)) { |
|||
D.assert(false, () => $"Unable to find annotation {annotation} in tracked annotations."); |
|||
} |
|||
|
|||
return trackedAnnotation; |
|||
} |
|||
|
|||
public EditorMouseTracker( |
|||
PointerRouter router, |
|||
EditorMouseDetectorAnnotationFinder annotationFinder |
|||
) { |
|||
D.assert(router != null); |
|||
D.assert(annotationFinder != null); |
|||
router.addGlobalRoute(route: _handleEvent); |
|||
this.annotationFinder = annotationFinder; |
|||
} |
|||
|
|||
void _handleEvent(PointerEvent evt) { |
|||
int deviceId = 0; |
|||
|
|||
if (_trackedAnnotations.isEmpty()) { |
|||
_lastMouseEvent.Remove(deviceId); |
|||
return; |
|||
} |
|||
|
|||
if (evt is PointerDragFromEditorReleaseEvent) { |
|||
_scheduleDragFromEditorReleaseCheck(); |
|||
_lastMouseEvent.Remove(deviceId); |
|||
} |
|||
else if (evt is PointerDragFromEditorEnterEvent || |
|||
evt is PointerDragFromEditorHoverEvent || |
|||
evt is PointerDragFromEditorExitEvent) { |
|||
if (!_lastMouseEvent.ContainsKey(deviceId) || |
|||
_lastMouseEvent[deviceId].position != evt.position) { |
|||
_scheduleDragFromEditorMousePositionCheck(); |
|||
} |
|||
|
|||
_lastMouseEvent[deviceId] = evt; |
|||
} |
|||
} |
|||
|
|||
public void schedulePostFrameCheck() { |
|||
|
|||
} |
|||
|
|||
void _scheduleDragFromEditorReleaseCheck() { |
|||
DragAndDrop.AcceptDrag(); |
|||
|
|||
var lastMouseEvent = new List<PointerEvent>(); |
|||
foreach (int deviceId in _lastMouseEvent.Keys) { |
|||
var _deviceId = deviceId; |
|||
var deviceEvent = _lastMouseEvent[_deviceId]; |
|||
|
|||
//only process PointerEditorDragEvents
|
|||
if (!(deviceEvent is PointerDragFromEditorEnterEvent || |
|||
deviceEvent is PointerDragFromEditorHoverEvent || |
|||
deviceEvent is PointerDragFromEditorExitEvent)) { |
|||
continue; |
|||
} |
|||
|
|||
lastMouseEvent.Add(_lastMouseEvent[_deviceId]); |
|||
SchedulerBinding.instance.addPostFrameCallback(_ => { |
|||
foreach (var lastEvent in lastMouseEvent) { |
|||
EditorMouseTrackerAnnotation hit = annotationFinder(lastEvent.position); |
|||
|
|||
if (hit == null) { |
|||
foreach (_EditorTrackedAnnotation trackedAnnotation in _trackedAnnotations.Values) { |
|||
if (trackedAnnotation.activeDevices.Contains(_deviceId)) { |
|||
trackedAnnotation.activeDevices.Remove(_deviceId); |
|||
} |
|||
} |
|||
|
|||
return; |
|||
} |
|||
|
|||
_EditorTrackedAnnotation hitAnnotation = _findAnnotation(hit); |
|||
|
|||
// release
|
|||
if (hitAnnotation.activeDevices.Contains(_deviceId)) { |
|||
if (hitAnnotation.annotation?.onDragFromEditorRelease != null) { |
|||
hitAnnotation.annotation.onDragFromEditorRelease( |
|||
PointerDragFromEditorReleaseEvent |
|||
.fromDragFromEditorEvent( |
|||
lastEvent, DragAndDrop.objectReferences, DragAndDrop.paths)); |
|||
} |
|||
|
|||
hitAnnotation.activeDevices.Remove(_deviceId); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
SchedulerBinding.instance.scheduleFrame(); |
|||
} |
|||
|
|||
void _scheduleDragFromEditorMousePositionCheck() { |
|||
SchedulerBinding.instance.addPostFrameCallback(_ => { collectDragFromEditorMousePositions(); }); |
|||
SchedulerBinding.instance.scheduleFrame(); |
|||
} |
|||
|
|||
public void collectDragFromEditorMousePositions() { |
|||
void exitAnnotation(_EditorTrackedAnnotation trackedAnnotation, int deviceId) { |
|||
if (trackedAnnotation.activeDevices.Contains(deviceId)) { |
|||
if (trackedAnnotation.annotation?.onDragFromEditorExit != null) { |
|||
trackedAnnotation.annotation.onDragFromEditorExit( |
|||
PointerDragFromEditorExitEvent.fromDragFromEditorEvent( |
|||
_lastMouseEvent[deviceId])); |
|||
} |
|||
|
|||
trackedAnnotation.activeDevices.Remove(deviceId); |
|||
} |
|||
} |
|||
|
|||
void exitAllDevices(_EditorTrackedAnnotation trackedAnnotation) { |
|||
if (trackedAnnotation.activeDevices.isNotEmpty()) { |
|||
HashSet<int> deviceIds = new HashSet<int>(trackedAnnotation.activeDevices); |
|||
foreach (int deviceId in deviceIds) { |
|||
exitAnnotation(trackedAnnotation, deviceId); |
|||
} |
|||
} |
|||
} |
|||
|
|||
if (!mouseIsConnected) { |
|||
foreach (var annotation in _trackedAnnotations.Values) { |
|||
exitAllDevices(annotation); |
|||
} |
|||
|
|||
return; |
|||
} |
|||
|
|||
foreach (int deviceId in _lastMouseEvent.Keys) { |
|||
PointerEvent lastEvent = _lastMouseEvent[deviceId]; |
|||
EditorMouseTrackerAnnotation hit = annotationFinder(lastEvent.position); |
|||
|
|||
if (hit == null) { |
|||
foreach (_EditorTrackedAnnotation trackedAnnotation in _trackedAnnotations.Values) { |
|||
exitAnnotation(trackedAnnotation, deviceId); |
|||
} |
|||
|
|||
return; |
|||
} |
|||
|
|||
_EditorTrackedAnnotation hitAnnotation = _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 (hitAnnotation.annotation?.onDragFromEditorEnter != null) { |
|||
hitAnnotation.annotation.onDragFromEditorEnter( |
|||
PointerDragFromEditorEnterEvent |
|||
.fromDragFromEditorEvent(lastEvent, DragAndDrop.objectReferences, DragAndDrop.paths)); |
|||
} |
|||
} |
|||
} |
|||
|
|||
// hover
|
|||
if (hitAnnotation.annotation?.onDragFromEditorHover != null) { |
|||
hitAnnotation.annotation.onDragFromEditorHover( |
|||
PointerDragFromEditorHoverEvent.fromDragFromEditorEvent(lastEvent)); |
|||
} |
|||
|
|||
// leave
|
|||
foreach (_EditorTrackedAnnotation trackedAnnotation in _trackedAnnotations.Values) { |
|||
if (hitAnnotation == trackedAnnotation) { |
|||
continue; |
|||
} |
|||
|
|||
if (trackedAnnotation.activeDevices.Contains(deviceId)) { |
|||
if (trackedAnnotation.annotation?.onDragFromEditorExit != null) { |
|||
trackedAnnotation.annotation.onDragFromEditorExit( |
|||
PointerDragFromEditorExitEvent |
|||
.fromDragFromEditorEvent(lastEvent)); |
|||
} |
|||
|
|||
trackedAnnotation.activeDevices.Remove(deviceId); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
using System.Collections.Generic; |
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.rendering; |
|||
using Unity.UIWidgets.ui; |
|||
using Unity.UIWidgets.widgets; |
|||
|
|||
namespace Unity.UIWidgets.gestures |
|||
{ |
|||
public class UnityEditorListener : SingleChildRenderObjectWidget |
|||
{ |
|||
public UnityEditorListener( |
|||
Key key = null, |
|||
PointerDragFromEditorEnterEventListener onPointerDragFromEditorEnter = null, |
|||
PointerDragFromEditorHoverEventListener onPointerDragFromEditorHover = null, |
|||
PointerDragFromEditorExitEventListener onPointerDragFromEditorExit = null, |
|||
PointerDragFromEditorReleaseEventListener onPointerDragFromEditorRelease = null, |
|||
HitTestBehavior behavior = HitTestBehavior.deferToChild, |
|||
Widget child = null) : base(key: key, child: child) |
|||
{ |
|||
this.onPointerDragFromEditorEnter = onPointerDragFromEditorEnter; |
|||
this.onPointerDragFromEditorHover = onPointerDragFromEditorHover; |
|||
this.onPointerDragFromEditorExit = onPointerDragFromEditorExit; |
|||
this.onPointerDragFromEditorRelease = onPointerDragFromEditorRelease; |
|||
this.behavior = behavior; |
|||
} |
|||
|
|||
public readonly PointerDragFromEditorEnterEventListener onPointerDragFromEditorEnter; |
|||
public readonly PointerDragFromEditorHoverEventListener onPointerDragFromEditorHover; |
|||
public readonly PointerDragFromEditorExitEventListener onPointerDragFromEditorExit; |
|||
public readonly PointerDragFromEditorReleaseEventListener onPointerDragFromEditorRelease; |
|||
public readonly HitTestBehavior behavior; |
|||
|
|||
public override RenderObject createRenderObject(BuildContext context) { |
|||
return new UnityEditorRenderPointerListener( |
|||
onPointerDragFromEditorEnter: onPointerDragFromEditorEnter, |
|||
onPointerDragFromEditorHover: onPointerDragFromEditorHover, |
|||
onPointerDragFromEditorExit: onPointerDragFromEditorExit, |
|||
onPointerDragFromEditorRelease: onPointerDragFromEditorRelease, |
|||
behavior: behavior |
|||
); |
|||
} |
|||
|
|||
public override void updateRenderObject(BuildContext context, RenderObject renderObjectRaw) { |
|||
var renderObject = (UnityEditorRenderPointerListener) renderObjectRaw; |
|||
renderObject.behavior = behavior; |
|||
renderObject.onPointerDragFromEditorEnter = onPointerDragFromEditorEnter; |
|||
renderObject.onPointerDragFromEditorHover = onPointerDragFromEditorHover; |
|||
renderObject.onPointerDragFromEditorExit = onPointerDragFromEditorExit; |
|||
renderObject.onPointerDragFromEditorRelease = onPointerDragFromEditorRelease; |
|||
} |
|||
|
|||
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) { |
|||
base.debugFillProperties(properties); |
|||
List<string> listeners = new List<string>(); |
|||
if (onPointerDragFromEditorEnter != null) { |
|||
listeners.Add("dragFromEditorEnter"); |
|||
} |
|||
|
|||
if (onPointerDragFromEditorHover != null) { |
|||
listeners.Add("dragFromEditorHover"); |
|||
} |
|||
|
|||
if (onPointerDragFromEditorExit != null) { |
|||
listeners.Add("dragFromEditorExit"); |
|||
} |
|||
|
|||
if (onPointerDragFromEditorRelease != null) { |
|||
listeners.Add("dragFromEditorRelease"); |
|||
} |
|||
properties.add(new EnumerableProperty<string>("listeners", listeners, ifEmpty: "<none>")); |
|||
properties.add(new EnumProperty<HitTestBehavior>("behavior", behavior)); |
|||
} |
|||
} |
|||
|
|||
|
|||
public class UnityEditorRenderPointerListener : RenderProxyBoxWithHitTestBehavior { |
|||
public UnityEditorRenderPointerListener( |
|||
PointerDragFromEditorEnterEventListener onPointerDragFromEditorEnter = null, |
|||
PointerDragFromEditorHoverEventListener onPointerDragFromEditorHover = null, |
|||
PointerDragFromEditorExitEventListener onPointerDragFromEditorExit = null, |
|||
PointerDragFromEditorReleaseEventListener onPointerDragFromEditorRelease = null, |
|||
HitTestBehavior behavior = HitTestBehavior.deferToChild, |
|||
RenderBox child = null |
|||
) : base(behavior: behavior, child: child) { |
|||
_onPointerDragFromEditorEnter = onPointerDragFromEditorEnter; |
|||
_onPointerDragFromEditorHover = onPointerDragFromEditorHover; |
|||
_onPointerDragFromEditorExit = onPointerDragFromEditorExit; |
|||
_onPointerDragFromEditorRelease = onPointerDragFromEditorRelease; |
|||
|
|||
if (_onPointerDragFromEditorEnter != null || |
|||
_onPointerDragFromEditorHover != null || |
|||
_onPointerDragFromEditorExit != null || |
|||
_onPointerDragFromEditorRelease != null |
|||
) { |
|||
_hoverAnnotation = new EditorMouseTrackerAnnotation( |
|||
onDragFromEditorEnter: _onPointerDragFromEditorEnter, |
|||
onDragFromEditorHover: _onPointerDragFromEditorHover, |
|||
onDragFromEditorExit: _onPointerDragFromEditorExit, |
|||
onDragFromEditorRelease: _onPointerDragFromEditorRelease |
|||
); |
|||
} |
|||
} |
|||
|
|||
PointerDragFromEditorEnterEventListener _onPointerDragFromEditorEnter; |
|||
|
|||
public PointerDragFromEditorEnterEventListener onPointerDragFromEditorEnter { |
|||
get { return _onPointerDragFromEditorEnter; } |
|||
set { |
|||
if (_onPointerDragFromEditorEnter != value) { |
|||
_onPointerDragFromEditorEnter = value; |
|||
_updateAnnotations(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
PointerDragFromEditorExitEventListener _onPointerDragFromEditorExit; |
|||
|
|||
public PointerDragFromEditorExitEventListener onPointerDragFromEditorExit { |
|||
get { return _onPointerDragFromEditorExit; } |
|||
set { |
|||
if (_onPointerDragFromEditorExit != value) { |
|||
_onPointerDragFromEditorExit = value; |
|||
_updateAnnotations(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
PointerDragFromEditorHoverEventListener _onPointerDragFromEditorHover; |
|||
|
|||
public PointerDragFromEditorHoverEventListener onPointerDragFromEditorHover { |
|||
get { return _onPointerDragFromEditorHover; } |
|||
set { |
|||
if (_onPointerDragFromEditorHover != value) { |
|||
_onPointerDragFromEditorHover = value; |
|||
_updateAnnotations(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
PointerDragFromEditorReleaseEventListener _onPointerDragFromEditorRelease; |
|||
|
|||
public PointerDragFromEditorReleaseEventListener onPointerDragFromEditorRelease { |
|||
get { return _onPointerDragFromEditorRelease; } |
|||
set { |
|||
if (_onPointerDragFromEditorRelease != value) { |
|||
_onPointerDragFromEditorRelease = value; |
|||
_updateAnnotations(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
EditorMouseTrackerAnnotation _hoverAnnotation; |
|||
|
|||
public EditorMouseTrackerAnnotation hoverAnnotation { |
|||
get { return _hoverAnnotation; } |
|||
} |
|||
|
|||
void _updateAnnotations() { |
|||
D.assert(_onPointerDragFromEditorEnter != _hoverAnnotation.onDragFromEditorEnter |
|||
|| _onPointerDragFromEditorHover != _hoverAnnotation.onDragFromEditorHover |
|||
|| _onPointerDragFromEditorExit != _hoverAnnotation.onDragFromEditorExit |
|||
|| _onPointerDragFromEditorRelease != _hoverAnnotation.onDragFromEditorRelease |
|||
, () => "Shouldn't call _updateAnnotations if nothing has changed."); |
|||
|
|||
if (_hoverAnnotation != null && attached) { |
|||
UiWidgetsEditorBinding.instance.editorMouseTracker.detachDragFromEditorAnnotation(_hoverAnnotation); |
|||
} |
|||
|
|||
if (_onPointerDragFromEditorEnter != null |
|||
|| _onPointerDragFromEditorHover != null |
|||
|| _onPointerDragFromEditorExit != null |
|||
|| _onPointerDragFromEditorRelease != null |
|||
) { |
|||
_hoverAnnotation = new EditorMouseTrackerAnnotation( |
|||
onDragFromEditorEnter: _onPointerDragFromEditorEnter |
|||
, onDragFromEditorHover: _onPointerDragFromEditorHover |
|||
, onDragFromEditorExit: _onPointerDragFromEditorExit |
|||
, onDragFromEditorRelease: _onPointerDragFromEditorRelease |
|||
); |
|||
|
|||
if (attached) { |
|||
UiWidgetsEditorBinding.instance.editorMouseTracker.attachDragFromEditorAnnotation(_hoverAnnotation); |
|||
} |
|||
} |
|||
else { |
|||
_hoverAnnotation = null; |
|||
} |
|||
|
|||
markNeedsPaint(); |
|||
} |
|||
|
|||
public override void attach(object owner) { |
|||
base.attach(owner); |
|||
if (_hoverAnnotation != null) { |
|||
UiWidgetsEditorBinding.instance.editorMouseTracker.attachDragFromEditorAnnotation(_hoverAnnotation); |
|||
} |
|||
} |
|||
|
|||
public override void detach() { |
|||
base.detach(); |
|||
if (_hoverAnnotation != null) { |
|||
UiWidgetsEditorBinding.instance.editorMouseTracker.detachDragFromEditorAnnotation(_hoverAnnotation); |
|||
} |
|||
} |
|||
|
|||
public override void paint(PaintingContext context, Offset offset) { |
|||
if (_hoverAnnotation != null) { |
|||
AnnotatedRegionLayer<EditorMouseTrackerAnnotation> layer = new AnnotatedRegionLayer<EditorMouseTrackerAnnotation>( |
|||
_hoverAnnotation, size: size, offset: offset); |
|||
|
|||
context.pushLayer(layer, base.paint, offset); |
|||
} |
|||
else { |
|||
base.paint(context, offset); |
|||
} |
|||
} |
|||
|
|||
protected override void performResize() { |
|||
size = constraints.biggest; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: ce9c6d444c634f238d062461e1b513b7 |
|||
timeCreated: 1637724477 |
|
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.rendering; |
|||
using Unity.UIWidgets.ui; |
|||
using Unity.UIWidgets.widgets; |
|||
using UnityEditor; |
|||
using UnityEngine; |
|||
|
|||
namespace Unity.UIWidgets.gestures |
|||
{ |
|||
public delegate void DragInEditorEnterCallback(DragFromEditorDetails details); |
|||
|
|||
public delegate void DragInEditorHoverCallback(DragFromHoverEditorDetails details); |
|||
|
|||
public delegate void DragInEditorExitCallback(); |
|||
|
|||
public delegate void DragInEditorReleaseCallback(DragFromEditorDetails details); |
|||
|
|||
public class DragFromEditorDetails { |
|||
public DragFromEditorDetails(Object[] objectReferences, string[] paths, Offset position) { |
|||
this.objectReferences = objectReferences; |
|||
this.paths = paths; |
|||
this.position = position; |
|||
} |
|||
|
|||
public readonly Object[] objectReferences; |
|||
public readonly string[] paths; |
|||
public readonly Offset position; |
|||
} |
|||
public class DragFromHoverEditorDetails { |
|||
public DragFromHoverEditorDetails( Offset position) { |
|||
|
|||
this.position = position; |
|||
} |
|||
public readonly Offset position; |
|||
} |
|||
|
|||
public class UnityObjectDetector : StatefulWidget { |
|||
public UnityObjectDetector( |
|||
Key key = null, |
|||
Widget child = null, |
|||
DragInEditorEnterCallback onEnter = null, |
|||
DragInEditorHoverCallback onHover = null, |
|||
DragInEditorExitCallback onExit = null, |
|||
DragInEditorReleaseCallback onRelease = null, |
|||
HitTestBehavior? behavior = null |
|||
) : base(key: key) { |
|||
this.child = child; |
|||
onDragInEditorEnter = onEnter; |
|||
onDragInEditorHover = onHover; |
|||
onDragInEditorExit = onExit; |
|||
onDragInEditorRelease = onRelease; |
|||
this.behavior = behavior; |
|||
} |
|||
|
|||
public readonly Widget child; |
|||
|
|||
public readonly DragInEditorEnterCallback onDragInEditorEnter; |
|||
public readonly DragInEditorHoverCallback onDragInEditorHover; |
|||
public readonly DragInEditorExitCallback onDragInEditorExit; |
|||
public readonly DragInEditorReleaseCallback onDragInEditorRelease; |
|||
|
|||
public readonly HitTestBehavior? behavior; |
|||
|
|||
public override State createState() { |
|||
return new UnityObjectDetectorState(); |
|||
} |
|||
} |
|||
|
|||
public class UnityObjectDetectorState : State<UnityObjectDetector> { |
|||
HitTestBehavior _defaultBehavior { |
|||
get { return widget.child == null ? HitTestBehavior.translucent : HitTestBehavior.deferToChild; } |
|||
} |
|||
|
|||
private bool isActivated = false; |
|||
|
|||
private Object[] objectReferences; |
|||
|
|||
private string[] paths; |
|||
|
|||
private Offset position; |
|||
|
|||
private bool isUnityObjectDragging() |
|||
{ |
|||
var dragObjects = DragAndDrop.objectReferences; |
|||
var dragPaths = DragAndDrop.paths; |
|||
|
|||
if (dragObjects != null && dragObjects.Length == 1 || |
|||
dragPaths != null && dragPaths.Length == 1) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public override Widget build(BuildContext context) |
|||
{ |
|||
Widget result = new UnityEditorListener( |
|||
child: widget.child, |
|||
onPointerDragFromEditorEnter: widget.onDragInEditorEnter == null |
|||
? ((PointerDragFromEditorEnterEventListener) null) |
|||
: evt => { widget.onDragInEditorEnter.Invoke(new DragFromEditorDetails(evt.objectReferences, evt.paths, evt.position)); }, |
|||
onPointerDragFromEditorHover: widget.onDragInEditorHover == null |
|||
? ((PointerDragFromEditorHoverEventListener) null) |
|||
: evt => { |
|||
widget.onDragInEditorHover.Invoke(new DragFromHoverEditorDetails(evt.position)); |
|||
}, |
|||
onPointerDragFromEditorExit: widget.onDragInEditorExit == null |
|||
? ((PointerDragFromEditorExitEventListener) null) |
|||
: evt => { widget.onDragInEditorExit.Invoke(); }, |
|||
onPointerDragFromEditorRelease: widget.onDragInEditorRelease == null |
|||
? ((PointerDragFromEditorReleaseEventListener) null) |
|||
: evt => { |
|||
widget.onDragInEditorRelease.Invoke(new DragFromEditorDetails(evt.objectReferences, evt.paths, evt.position)); |
|||
}, |
|||
behavior: widget.behavior ?? _defaultBehavior |
|||
); |
|||
return result; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 1e9774220f644e23992deb96ca656908 |
|||
timeCreated: 1637723510 |
部分文件因为文件数量过多而无法显示
撰写
预览
正在加载...
取消
保存
Reference in new issue