Skip to content

Commit 1e20672

Browse files
authored
FIX: collapsed and expanded treeview state is preserved when saving (#2096)
1 parent 8004589 commit 1e20672

File tree

2 files changed

+4
-85
lines changed

2 files changed

+4
-85
lines changed

Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorState.cs

-84
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
22
using System;
33
using System.Collections.Generic;
4-
using System.Collections.ObjectModel;
54
using System.Linq;
65
using UnityEditor;
76

@@ -92,7 +91,6 @@ public InputActionsEditorState(
9291
int selectedActionIndex = 0,
9392
int selectedBindingIndex = 0,
9493
SelectionType selectionType = SelectionType.Action,
95-
Dictionary<(string, string), HashSet<int>> expandedBindingIndices = null,
9694
InputControlScheme selectedControlScheme = default,
9795
int selectedControlSchemeIndex = -1,
9896
int selectedDeviceRequirementIndex = -1,
@@ -112,9 +110,6 @@ public InputActionsEditorState(
112110
m_selectedControlSchemeIndex = selectedControlSchemeIndex;
113111
m_selectedDeviceRequirementIndex = selectedDeviceRequirementIndex;
114112

115-
m_ExpandedCompositeBindings = expandedBindingIndices == null ?
116-
new Dictionary<(string, string), HashSet<int>>() :
117-
new Dictionary<(string, string), HashSet<int>>(expandedBindingIndices);
118113
m_CutElements = cutElements;
119114
}
120115

@@ -201,12 +196,6 @@ public InputActionsEditorState(InputActionsEditorState other, SerializedObject a
201196
m_ControlScheme = new InputControlScheme();
202197
}
203198

204-
// Editor may leave these as null after domain reloads, so recreate them in that case.
205-
// If they exist, we attempt to just preserve the same expanded items based on name for now for simplicity.
206-
m_ExpandedCompositeBindings = other.m_ExpandedCompositeBindings == null ?
207-
new Dictionary<(string, string), HashSet<int>>() :
208-
new Dictionary<(string, string), HashSet<int>>(other.m_ExpandedCompositeBindings);
209-
210199
m_CutElements = other.cutElements;
211200
}
212201

@@ -218,7 +207,6 @@ public InputActionsEditorState With(
218207
InputControlScheme? selectedControlScheme = null,
219208
int? selectedControlSchemeIndex = null,
220209
int? selectedDeviceRequirementIndex = null,
221-
Dictionary<(string, string), HashSet<int>> expandedBindingIndices = null,
222210
List<CutElement> cutElements = null)
223211
{
224212
return new InputActionsEditorState(
@@ -228,7 +216,6 @@ public InputActionsEditorState With(
228216
selectedActionIndex ?? this.selectedActionIndex,
229217
selectedBindingIndex ?? this.selectedBindingIndex,
230218
selectionType ?? this.selectionType,
231-
expandedBindingIndices ?? m_ExpandedCompositeBindings,
232219

233220
// Control schemes
234221
selectedControlScheme ?? this.selectedControlScheme,
@@ -248,7 +235,6 @@ public InputActionsEditorState ClearCutElements()
248235
selectedActionIndex,
249236
selectedBindingIndex,
250237
selectionType,
251-
m_ExpandedCompositeBindings,
252238
selectedControlScheme,
253239
selectedControlSchemeIndex,
254240
selectedDeviceRequirementIndex,
@@ -262,39 +248,6 @@ public SerializedProperty GetActionMapByName(string actionMapName)
262248
.FirstOrDefault(p => p.FindPropertyRelative(nameof(InputActionMap.m_Name)).stringValue == actionMapName);
263249
}
264250

265-
public InputActionsEditorState ExpandCompositeBinding(SerializedInputBinding binding)
266-
{
267-
var key = GetSelectedActionMapAndActionKey();
268-
269-
var expandedCompositeBindings = new Dictionary<(string, string), HashSet<int>>(m_ExpandedCompositeBindings);
270-
if (!expandedCompositeBindings.TryGetValue(key, out var expandedStates))
271-
{
272-
expandedStates = new HashSet<int>();
273-
expandedCompositeBindings.Add(key, expandedStates);
274-
}
275-
276-
expandedStates.Add(binding.indexOfBinding);
277-
278-
return With(expandedBindingIndices: expandedCompositeBindings);
279-
}
280-
281-
public InputActionsEditorState CollapseCompositeBinding(SerializedInputBinding binding)
282-
{
283-
var key = GetSelectedActionMapAndActionKey();
284-
285-
if (m_ExpandedCompositeBindings.ContainsKey(key) == false)
286-
throw new InvalidOperationException("Trying to collapse a composite binding tree that was never expanded.");
287-
288-
// do the dance of C# immutability
289-
var oldExpandedCompositeBindings = m_ExpandedCompositeBindings;
290-
var expandedCompositeBindings = oldExpandedCompositeBindings.Keys.Where(dictKey => dictKey != key)
291-
.ToDictionary(dictKey => dictKey, dictKey => oldExpandedCompositeBindings[dictKey]);
292-
var newHashset = new HashSet<int>(m_ExpandedCompositeBindings[key].Where(index => index != binding.indexOfBinding));
293-
expandedCompositeBindings.Add(key, newHashset);
294-
295-
return With(expandedBindingIndices: expandedCompositeBindings);
296-
}
297-
298251
public InputActionsEditorState SelectAction(string actionName)
299252
{
300253
var actionMap = GetSelectedActionMap();
@@ -419,48 +372,11 @@ public readonly List<CutElement> GetCutElements()
419372
return m_CutElements;
420373
}
421374

422-
public ReadOnlyCollection<int> GetOrCreateExpandedState()
423-
{
424-
return new ReadOnlyCollection<int>(GetOrCreateExpandedStateInternal().ToList());
425-
}
426-
427-
private HashSet<int> GetOrCreateExpandedStateInternal()
428-
{
429-
var key = GetSelectedActionMapAndActionKey();
430-
431-
if (m_ExpandedCompositeBindings.TryGetValue(key, out var expandedStates))
432-
return expandedStates;
433-
434-
expandedStates = new HashSet<int>();
435-
m_ExpandedCompositeBindings.Add(key, expandedStates);
436-
return expandedStates;
437-
}
438-
439-
internal (string, string) GetSelectedActionMapAndActionKey()
440-
{
441-
var selectedActionMap = GetSelectedActionMap();
442-
443-
var selectedAction = selectedActionMap
444-
.FindPropertyRelative(nameof(InputActionMap.m_Actions))
445-
.GetArrayElementAtIndex(selectedActionIndex);
446-
447-
var key = (
448-
selectedActionMap.FindPropertyRelative(nameof(InputActionMap.m_Name)).stringValue,
449-
selectedAction.FindPropertyRelative(nameof(InputAction.m_Name)).stringValue
450-
);
451-
return key;
452-
}
453-
454375
private SerializedProperty GetSelectedActionMap()
455376
{
456377
return Selectors.GetActionMapAtIndex(serializedObject, selectedActionMapIndex)?.wrappedProperty;
457378
}
458379

459-
/// <summary>
460-
/// Expanded states for the actions tree view. These are stored per InputActionMap
461-
/// </summary>
462-
private readonly Dictionary<(string, string), HashSet<int>> m_ExpandedCompositeBindings;
463-
464380
private readonly InputControlScheme m_ControlScheme;
465381
}
466382

Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/ActionsTreeView.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,12 @@ public static List<TreeViewItemData<ActionOrBindingData>> GetActionsAsTreeViewDa
659659

660660
private static int GetIdForGuid(Guid guid, Dictionary<Guid, int> idDictionary)
661661
{
662+
// This method is used to ensure that the same Guid always gets the same id
663+
// We use getHashCode instead of a counter, as we cannot guarantee that the same Guid will always be added in the same order
664+
// There is a tiny chance of a collision, but it is it does happen it will only affect the expanded state of the tree view
662665
if (!idDictionary.TryGetValue(guid, out var id))
663666
{
664-
id = idDictionary.Values.Count > 0 ? idDictionary.Values.Max() + 1 : 0;
667+
id = guid.GetHashCode();
665668
idDictionary.Add(guid, id);
666669
}
667670
return id;

0 commit comments

Comments
 (0)