From 326a44240730db6772cc59bea287b07b875111c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Freire?= Date: Mon, 14 Aug 2023 13:11:10 +0200 Subject: [PATCH 01/39] DROP Enable UITK Asset Editor --- .../Editor/UITKAssetEditor/InputActionsEditorWindow.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs index e76064f195..a418a820a8 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs @@ -17,7 +17,7 @@ internal static class EnableUITKEditor static EnableUITKEditor() { // set this feature flag to true to enable the UITK editor - InputSystem.settings.SetInternalFeatureFlag(InputFeatureNames.kUseUIToolkitEditor, false); + InputSystem.settings.SetInternalFeatureFlag(InputFeatureNames.kUseUIToolkitEditor, true); } } From c8f3ea557cf8ef6a3144a7ac6abafe4d48ad642e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Freire?= Date: Mon, 14 Aug 2023 13:12:00 +0200 Subject: [PATCH 02/39] WIP Add Drag And Drop UI functionality --- .../UITKAssetEditor/Views/ActionMapsView.cs | 2 + .../UITKAssetEditor/Views/ActionsTreeView.cs | 1 + .../UITKAssetEditor/Views/DragManipulator.cs | 62 ++++++++++++++++ .../Views/DragManipulator.cs.meta | 3 + .../UITKAssetEditor/Views/DropManipulator.cs | 74 +++++++++++++++++++ .../Views/DropManipulator.cs.meta | 3 + .../Views/InputActionsTreeViewItem.cs | 57 +++++++++++++- 7 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DragManipulator.cs create mode 100644 Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DragManipulator.cs.meta create mode 100644 Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DropManipulator.cs create mode 100644 Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DropManipulator.cs.meta diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ActionMapsView.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ActionMapsView.cs index 33f0408a06..967eb23713 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ActionMapsView.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ActionMapsView.cs @@ -50,6 +50,8 @@ public ActionMapsView(VisualElement root, StateContainer stateContainer) }; m_ListView.RegisterCallback(OnKeyDownEvent); + m_ListView.AddManipulator(new DropManipulator()); + CreateSelector(s => new ViewStateCollection(Selectors.GetActionMapNames(s)), (actionMapNames, state) => new ViewState(Selectors.GetSelectedActionMap(state), actionMapNames)); diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ActionsTreeView.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ActionsTreeView.cs index f636b20cb9..8d84429ece 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ActionsTreeView.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ActionsTreeView.cs @@ -115,6 +115,7 @@ public ActionsTreeView(VisualElement root, StateContainer stateContainer) }; m_ActionsTreeView.RegisterCallback(OnKeyDownEvent); + m_ActionsTreeView.AddManipulator(new DragManipulator()); CreateSelector(Selectors.GetActionsForSelectedActionMap, Selectors.GetActionMapCount, (_, count, state) => diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DragManipulator.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DragManipulator.cs new file mode 100644 index 0000000000..a53c298d84 --- /dev/null +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DragManipulator.cs @@ -0,0 +1,62 @@ +using System; +using UnityEditor; +using UnityEngine; +using UnityEngine.UIElements; + +namespace UnityEngine.InputSystem.Editor +{ + public class DragManipulator : Manipulator + { + String initialLabelText; + + public static bool dragging; + private bool m_IsPointerDown; + protected override void RegisterCallbacksOnTarget() + { + Debug.Log("Registering callbacks"); + target.RegisterCallback(OnPointerMove); + target.RegisterCallback(OnPointerDown); + target.RegisterCallback(OnPointerUp); + } + + void OnPointerMove(PointerMoveEvent evt) + { + if (!dragging && m_IsPointerDown) + { + m_IsPointerDown = false; + Debug.Log("Starting drag!"); + DragAndDrop.PrepareStartDrag(); + DragAndDrop.SetGenericData("string", "dropped"); + DragAndDrop.StartDrag(string.Empty); + dragging = true; + } + } + + void OnPointerUp(PointerUpEvent evt) + { + Debug.Log("Pointer up"); + m_IsPointerDown = false; + if (dragging) + { + dragging = false; + DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; + } + } + + void OnPointerDown(PointerDownEvent evt) + { + if (evt.isPrimary && evt.button == 0) + { + Debug.Log("Pointer down"); + m_IsPointerDown = true; + } + } + + protected override void UnregisterCallbacksFromTarget() + { + target.UnregisterCallback(OnPointerMove); + target.UnregisterCallback(OnPointerDown); + target.UnregisterCallback(OnPointerUp); + } + } +} diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DragManipulator.cs.meta b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DragManipulator.cs.meta new file mode 100644 index 0000000000..6e5bd071c4 --- /dev/null +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DragManipulator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 60dc7f6666ce47e3acc6d121313fd2f9 +timeCreated: 1691760360 \ No newline at end of file diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DropManipulator.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DropManipulator.cs new file mode 100644 index 0000000000..108daba6d4 --- /dev/null +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DropManipulator.cs @@ -0,0 +1,74 @@ +using System; +using UnityEditor; +using UnityEngine; +using UnityEngine.UIElements; + +namespace UnityEngine.InputSystem.Editor +{ + public class DropManipulator : Manipulator + { + protected override void RegisterCallbacksOnTarget() + { + Debug.Log("Registering callbacks for DropManipulator"); + target.RegisterCallback(OnDragEnterEvent); + target.RegisterCallback(OnDragLeaveEvent); + target.RegisterCallback(OnDragUpdatedEvent); + target.RegisterCallback(OnDragPerformEvent); + target.RegisterCallback(OnDragExitedEvent); + } + + protected override void UnregisterCallbacksFromTarget() + { + target.UnregisterCallback(OnDragEnterEvent); + target.UnregisterCallback(OnDragLeaveEvent); + target.UnregisterCallback(OnDragUpdatedEvent); + target.UnregisterCallback(OnDragPerformEvent); + target.UnregisterCallback(OnDragExitedEvent); + } + + void OnDragExitedEvent(DragExitedEvent evt) + { + Debug.Log("Drag exited"); + // ((Label)evt.target).style.backgroundColor = Color.clear; + object draggedLabel = DragAndDrop.GetGenericData("string"); + if (DragManipulator.dragging) + { + DragManipulator.dragging = false; + } + } + + void OnDragPerformEvent(DragPerformEvent evt) + { + DragAndDrop.AcceptDrag(); + object data = DragAndDrop.GetGenericData("string"); + Debug.Log("Drag performed with data: " + data + "to target: " + evt.currentTarget); + DragManipulator.dragging = false; + } + + void OnDragUpdatedEvent(DragUpdatedEvent evt) + { + DragAndDrop.visualMode = DragAndDropVisualMode.Copy; + // Unclear why we need this, but saw it in another example. + evt.StopPropagation(); + } + + void OnDragLeaveEvent(DragLeaveEvent evt) + { + Debug.Log("Drag leave"); + // ((Label)evt.target).style.backgroundColor = Color.clear; + } + + void OnDragEnterEvent(DragEnterEvent evt) + { + Debug.Log("Drag enter event " + evt.currentTarget); + + // This makes sure that drop is only allowed on the correct type of element that started the drag. + // The drag element will set a string in the DragAndDrop generic data, which we can use to check if the + // drop is allowed. + if (DragAndDrop.GetGenericData("string") != null) + { + // ((Label)evt.target).style.backgroundColor = Color.gray; + } + } + } +} diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DropManipulator.cs.meta b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DropManipulator.cs.meta new file mode 100644 index 0000000000..3ba3f9dca1 --- /dev/null +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/DropManipulator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b52a4b3b22334b02a4d22d942a479be6 +timeCreated: 1691762301 \ No newline at end of file diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/InputActionsTreeViewItem.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/InputActionsTreeViewItem.cs index e26a6bade7..1d6f2a90cb 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/InputActionsTreeViewItem.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/InputActionsTreeViewItem.cs @@ -24,6 +24,8 @@ internal class InputActionsTreeViewItem : VisualElement private bool m_IsEditing; + DropManipulator m_Manipulator; + public InputActionsTreeViewItem() { var template = AssetDatabase.LoadAssetAtPath( @@ -38,10 +40,63 @@ public InputActionsTreeViewItem() renameTextfield.selectAllOnFocus = true; renameTextfield.selectAllOnMouseUp = false; - RegisterCallback(OnMouseDownEventForRename); + + // RegisterCallback(OnMouseDownEventForRename); + // RegisterCallback(OnDragEnterEvent); + // RegisterCallback(OnDragLeaveEvent); + // RegisterCallback(OnDragUpdatedEvent); + // RegisterCallback(OnDragPerformEvent); + // RegisterCallback(OnDragExitedEvent); + renameTextfield.RegisterCallback(e => OnEditTextFinished()); } + void OnDragExitedEvent(DragExitedEvent evt) + { + Debug.Log("Drag exited"); + // ((Label)evt.target).style.backgroundColor = Color.clear; + object draggedLabel = DragAndDrop.GetGenericData("string"); + if (DragManipulator.dragging) + { + DragManipulator.dragging = false; + } + } + + void OnDragPerformEvent(DragPerformEvent evt) + { + DragAndDrop.AcceptDrag(); + object data = DragAndDrop.GetGenericData("string"); + Debug.Log("Drag performed with data: " + data + "to target: " + evt.currentTarget); + DragManipulator.dragging = false; + } + + void OnDragUpdatedEvent(DragUpdatedEvent evt) + { + Debug.Log("Drag UPDATE event " + (evt.target as VisualElement)); + DragAndDrop.visualMode = DragAndDropVisualMode.Move; + // Unclear why we need this, but saw it in another example. + // evt.StopPropagation(); + } + + void OnDragLeaveEvent(DragLeaveEvent evt) + { + Debug.Log("Drag leave"); + // ((Label)evt.target).style.backgroundColor = Color.clear; + } + + void OnDragEnterEvent(DragEnterEvent evt) + { + Debug.Log("Drag enter event " + (evt.target as VisualElement)); + + // This makes sure that drop is only allowed on the correct type of element that started the drag. + // The drag element will set a string in the DragAndDrop generic data, which we can use to check if the + // drop is allowed. + if (DragAndDrop.GetGenericData("string") != null) + { + // ((Label)evt.target).style.backgroundColor = Color.gray; + } + } + public Label label => this.Q