Skip to content

Commit

Permalink
FIX: Select appropriate action/action map when right clicking (ISX-17…
Browse files Browse the repository at this point in the history
…43). (#1806)

* Intercept right click to select appropriate items before context menus appear.

* Undo callback changes.

* Formatting fix.

* Update changelog, remove unneeded brackets.

* Store the Action generated for context menus so it can be correctly removed later.

* Add null check for element hunting.

* Formatting fix.
  • Loading branch information
graham-huws authored Feb 15, 2024
1 parent 6db0cc5 commit 2fecde0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ however, it has to be formatted properly to pass verification tests.
- Fixed Documentation~/filter.yml GlobalNamespace rule removing all API documentation.
- Fixed `Destroy may not be called from edit mode` error [ISXB-695](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-695)
- Fixed possible exceptions thrown when deleting and adding Action Maps.
- Fixed selection not changing when right-clicking an Action Map or Action in the Project Settings Input Action Editor.
- Fixed potential race condition on access to GCHandle in DefferedResolutionOfBindings and halved number of calls to GCHandle resolution [ISXB-726](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-726)
- Fixed issue where composite part dropdown manipulates binding path and leaves composite part field unchanged.
- Fixed lingering highlight effect on Save Asset button after clicking.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public ActionMapsView(VisualElement root, StateContainer stateContainer)

m_ListView.RegisterCallback<ExecuteCommandEvent>(OnExecuteCommand);
m_ListView.RegisterCallback<ValidateCommandEvent>(OnValidateCommand);
m_ListView.RegisterCallback<PointerDownEvent>(OnPointerDown, TrickleDown.TrickleDown);
var treeView = root.Q<TreeView>("actions-tree-view");
m_ListView.AddManipulator(new DropManipulator(OnDroppedHandler, treeView));
m_ListView.itemIndexChanged += OnReorder;
Expand Down Expand Up @@ -194,6 +195,16 @@ private void OnValidateCommand(ValidateCommandEvent evt)
}
}

private void OnPointerDown(PointerDownEvent evt)
{
// Allow right clicks to select an item before we bring up the matching context menu.
if (evt.button == (int)MouseButton.RightMouse && evt.clickCount == 1)
{
var actionMap = (evt.target as VisualElement).GetFirstAncestorOfType<InputActionMapsTreeViewItem>();
m_ListView.SetSelection(actionMap.parent.IndexOf(actionMap));
}
}

private readonly CollectionViewSelectionChangeFilter m_ListViewSelectionChangeFilter;
private bool m_EnterRenamingMode;
private readonly ListView m_ListView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public ActionsTreeView(VisualElement root, StateContainer stateContainer)

if (item.isAction)
{
addBindingButton.clicked += ContextMenu.GetContextMenuForActionAddItem(treeViewItem, item.controlLayout);
Action action = ContextMenu.GetContextMenuForActionAddItem(treeViewItem, item.controlLayout);
addBindingButton.clicked += action;
addBindingButton.userData = action; // Store to use in unbindItem
addBindingButton.clickable.activators.Add(new ManipulatorActivationFilter(){button = MouseButton.RightMouse});
addBindingButton.style.display = DisplayStyle.Flex;
treeViewItem.EditTextFinishedCallback = newName =>
Expand Down Expand Up @@ -111,6 +113,12 @@ public ActionsTreeView(VisualElement root, StateContainer stateContainer)
if (item.isAction || item.isComposite)
treeViewItem.Reset();

if (item.isAction)
{
var button = element.Q<Button>("add-new-binding-button");
button.clicked -= button.userData as Action;
}

treeViewItem.OnDeleteItem -= treeViewItem.DeleteCallback;
treeViewItem.OnDuplicateItem -= treeViewItem.DuplicateCallback;
treeViewItem.EditTextFinished -= treeViewItem.EditTextFinishedCallback;
Expand All @@ -135,6 +143,7 @@ public ActionsTreeView(VisualElement root, StateContainer stateContainer)

m_ActionsTreeView.RegisterCallback<ExecuteCommandEvent>(OnExecuteCommand);
m_ActionsTreeView.RegisterCallback<ValidateCommandEvent>(OnValidateCommand);
m_ActionsTreeView.RegisterCallback<PointerDownEvent>(OnPointerDown, TrickleDown.TrickleDown);
m_ActionsTreeView.RegisterCallback<DragPerformEvent>(OnDraggedItem);

CreateSelector(Selectors.GetActionsForSelectedActionMap, Selectors.GetActionMapCount,
Expand Down Expand Up @@ -423,6 +432,23 @@ private void OnValidateCommand(ValidateCommandEvent evt)
}
}

private void OnPointerDown(PointerDownEvent evt)
{
// Allow right clicks to select an item before we bring up the matching context menu.
if (evt.button == (int)MouseButton.RightMouse && evt.clickCount == 1)
{
// Look upwards to the immediate child of the scroll view, so we know what Index to use
var element = evt.target as VisualElement;
while (element != null && element.name != "unity-tree-view__item")
element = element.parent;

if (element == null)
return;

m_ActionsTreeView.SetSelection(element.parent.IndexOf(element));
}
}

private string GetPreviousActionNameFromViewTree(in ActionOrBindingData data)
{
Debug.Assert(data.isAction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine.InputSystem.Editor;
using UnityEngine.UIElements;

namespace UnityEngine.InputSystem.Editor
Expand Down

0 comments on commit 2fecde0

Please sign in to comment.