diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ContextMenu.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ContextMenu.cs index ef70992280..51b3a5d331 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ContextMenu.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ContextMenu.cs @@ -25,6 +25,21 @@ internal static class ContextMenu private static readonly string add_Binding_String = "Add Binding"; #region ActionMaps + // Determine whether current clipboard contents can can pasted into the ActionMaps view + // + // can always paste an ActionMap + // need an existing map to be able to paste an Action + // + private static bool CanPasteIntoActionMaps(ActionMapsView mapView) + { + bool haveMap = mapView.GetMapCount() > 0; + var copiedType = CopyPasteHelper.GetCopiedClipboardType(); + bool copyIsMap = copiedType == typeof(InputActionMap); + bool copyIsAction = copiedType == typeof(InputAction); + bool hasPastableData = (copyIsMap || (copyIsAction && haveMap)); + return hasPastableData; + } + public static void GetContextMenuForActionMapItem(ActionMapsView mapView, InputActionMapsTreeViewItem treeViewItem, int index) { treeViewItem.OnContextualMenuPopulateEvent = (menuEvent => @@ -39,9 +54,15 @@ public static void GetContextMenuForActionMapItem(ActionMapsView mapView, InputA menuEvent.menu.AppendAction(copy_String, _ => mapView.CopyItems()); menuEvent.menu.AppendAction(cut_String, _ => mapView.CutItems()); - var copiedAction = CopyPasteHelper.GetCopiedClipboardType() == typeof(InputAction); - if (CopyPasteHelper.HasPastableClipboardData(typeof(InputActionMap))) - menuEvent.menu.AppendAction(paste_String, _ => mapView.PasteItems(copiedAction)); + if (CanPasteIntoActionMaps(mapView)) + { + bool copyIsAction = CopyPasteHelper.GetCopiedClipboardType() == typeof(InputAction); + if (CopyPasteHelper.HasPastableClipboardData(typeof(InputActionMap))) + menuEvent.menu.AppendAction(paste_String, _ => mapView.PasteItems(copyIsAction)); + } + + menuEvent.menu.AppendSeparator(); + menuEvent.menu.AppendAction(add_Action_Map_String, _ => mapView.AddActionMap()); }); } @@ -51,11 +72,12 @@ public static void GetContextMenuForActionMapsEmptySpace(ActionMapsView mapView, { _ = new ContextualMenuManipulator(menuEvent => { - var copiedAction = CopyPasteHelper.GetCopiedClipboardType() == typeof(InputAction); - if (CopyPasteHelper.HasPastableClipboardData(typeof(InputActionMap))) - menuEvent.menu.AppendAction(paste_String, _ => mapView.PasteItems(copiedAction)); - - menuEvent.menu.AppendSeparator(); + if (CanPasteIntoActionMaps(mapView)) + { + bool copyIsAction = CopyPasteHelper.GetCopiedClipboardType() == typeof(InputAction); + menuEvent.menu.AppendAction(paste_String, _ => mapView.PasteItems(copyIsAction)); + menuEvent.menu.AppendSeparator(); + } menuEvent.menu.AppendAction(add_Action_Map_String, _ => mapView.AddActionMap()); }) { target = element }; } @@ -63,15 +85,47 @@ public static void GetContextMenuForActionMapsEmptySpace(ActionMapsView mapView, #endregion #region Actions + // Determine whether current clipboard contents can pasted into the Actions TreeView + // + // item selected => can paste either Action or Binding (depends on selected item context) + // empty view => can only paste Action + // no selection => can only paste Action + // + private static bool CanPasteIntoActions(TreeView treeView) + { + bool hasPastableData = false; + bool selected = treeView.selectedIndex != -1; + if (selected) + { + var item = treeView.GetItemDataForIndex(treeView.selectedIndex); + var itemType = item.isAction ? typeof(InputAction) : typeof(InputBinding); + hasPastableData = CopyPasteHelper.HasPastableClipboardData(itemType); + } + else + { + // Cannot paste Binding when no Action is selected or into an empty view + bool copyIsBinding = CopyPasteHelper.GetCopiedClipboardType() == typeof(InputBinding); + hasPastableData = !copyIsBinding && CopyPasteHelper.HasPastableClipboardData(typeof(InputAction)); + } + return hasPastableData; + } + // Add the "Paste" option to all elements in the Action area. public static void GetContextMenuForActionListView(ActionsTreeView actionsTreeView, TreeView treeView, VisualElement target) { _ = new ContextualMenuManipulator(menuEvent => { - var item = treeView.GetItemDataForIndex(treeView.selectedIndex); - var hasPastableData = CopyPasteHelper.HasPastableClipboardData(item.isAction ? typeof(InputAction) : typeof(InputBinding)); - if (hasPastableData) - menuEvent.menu.AppendAction(paste_String, _ => actionsTreeView.PasteItems()); + bool haveMap = actionsTreeView.GetMapCount() > 0; + if (haveMap) + { + bool hasPastableData = CanPasteIntoActions(treeView); + if (hasPastableData) + { + menuEvent.menu.AppendAction(paste_String, _ => actionsTreeView.PasteItems()); + } + menuEvent.menu.AppendSeparator(); + menuEvent.menu.AppendAction(add_Action_String, _ => actionsTreeView.AddAction()); + } }) { target = target }; } @@ -81,13 +135,15 @@ public static void GetContextMenuForActionsEmptySpace(ActionsTreeView actionsTre { _ = new ContextualMenuManipulator(menuEvent => { - if (actionsTreeView.GetMapCount() > 0 && (!onlyShowIfTreeIsEmpty || treeView.GetTreeCount() == 0)) + bool haveMap = actionsTreeView.GetMapCount() > 0; + if (haveMap && (!onlyShowIfTreeIsEmpty || treeView.GetTreeCount() == 0)) { - var item = treeView.GetItemDataForIndex(treeView.selectedIndex); - if (CopyPasteHelper.HasPastableClipboardData(item.isAction ? typeof(InputAction) : typeof(InputBinding))) + bool hasPastableData = CanPasteIntoActions(treeView); + if (hasPastableData) + { menuEvent.menu.AppendAction(paste_String, _ => actionsTreeView.PasteItems()); - - menuEvent.menu.AppendSeparator(); + menuEvent.menu.AppendSeparator(); + } menuEvent.menu.AppendAction(add_Action_String, _ => actionsTreeView.AddAction()); } }) { target = target }; diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/CopyPasteHelper.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/CopyPasteHelper.cs index 3d2fad8100..9dfea2c1d6 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/CopyPasteHelper.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/CopyPasteHelper.cs @@ -266,8 +266,17 @@ private static void PasteBlocks(string transmission, int indexToInsert, Serializ { var actionName = Selectors.GetSelectedBinding(s_State)?.wrappedProperty.FindPropertyRelative("m_Action") .stringValue; + if (s_State.selectionType == SelectionType.Action) - actionName = PropertyName(Selectors.GetSelectedAction(s_State)?.wrappedProperty); + { + SerializedProperty property = Selectors.GetSelectedAction(s_State)?.wrappedProperty; + if (property == null) + return; + actionName = PropertyName(property); + } + if (actionName == null) + return; + PasteBindingOrComposite(arrayToInsertInto, block, indexToInsert, actionName); } }