Skip to content

Commit f2c0423

Browse files
authored
FIX: exception on renaming control scheme (ISX-1544) (#1722)
* fixed exception on renaming control scheme * added explanation comments * format fix * refactored and fixed canceling renaming of control schemes * clean up before merge
1 parent de5999a commit f2c0423

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Commands/ControlSchemeCommands.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static Command RemoveDeviceRequirement(int selectedDeviceIndex)
3232
};
3333
}
3434

35-
public static Command SaveControlScheme(bool updateExisting = false)
35+
public static Command SaveControlScheme(string newName = "", bool updateExisting = false)
3636
{
3737
return (in InputActionsEditorState state) =>
3838
{
@@ -53,7 +53,7 @@ public static Command SaveControlScheme(bool updateExisting = false)
5353
controlScheme = controlSchemesArray.GetArrayElementAtIndex(controlSchemesArray.arraySize - 1);
5454
}
5555

56-
controlScheme.FindPropertyRelative(nameof(InputControlScheme.m_Name)).stringValue = controlSchemeName;
56+
controlScheme.FindPropertyRelative(nameof(InputControlScheme.m_Name)).stringValue = string.IsNullOrEmpty(newName) ? controlSchemeName : newName;
5757

5858
var serializedDeviceRequirements = controlScheme.FindPropertyRelative(nameof(InputControlScheme.m_DeviceRequirements));
5959
serializedDeviceRequirements.ClearArray();
@@ -71,8 +71,7 @@ public static Command SaveControlScheme(bool updateExisting = false)
7171
}
7272

7373
state.serializedObject.ApplyModifiedProperties();
74-
75-
return state.With(selectedControlSchemeIndex: controlSchemesArray.arraySize - 1);
74+
return state.With(selectedControlScheme: new InputControlScheme(controlScheme));
7675
};
7776
}
7877

@@ -137,7 +136,7 @@ public static Command DeleteSelectedControlScheme()
137136
};
138137
}
139138

140-
private static string MakeUniqueControlSchemeName(InputActionsEditorState state, string name)
139+
internal static string MakeUniqueControlSchemeName(InputActionsEditorState state, string name)
141140
{
142141
var controlSchemes = state.serializedObject.FindProperty(nameof(InputActionAsset.m_ControlSchemes));
143142

@@ -165,15 +164,6 @@ public static Command ChangeDeviceRequirement(int deviceRequirementIndex, bool i
165164
};
166165
}
167166

168-
public static Command ChangeSelectedControlSchemeName(string controlSchemeName)
169-
{
170-
return (in InputActionsEditorState state) => state.With(
171-
selectedControlScheme: new InputControlScheme(
172-
controlSchemeName,
173-
state.selectedControlScheme.deviceRequirements,
174-
state.selectedControlScheme.m_BindingGroup));
175-
}
176-
177167
public static Command ReorderDeviceRequirements(int oldPosition, int newPosition)
178168
{
179169
return (in InputActionsEditorState state) =>

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_UI_TK_ASSET_EDITOR
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using UnityEditor;
@@ -9,6 +10,10 @@ namespace UnityEngine.InputSystem.Editor
910
{
1011
internal class ControlSchemesView : ViewBase<InputControlScheme>
1112
{
13+
//is used to save the new name of the control scheme when renaming
14+
private string m_NewName;
15+
public event Action<ViewBase<InputControlScheme>> OnClosing;
16+
1217
public ControlSchemesView(VisualElement root, StateContainer stateContainer, bool updateExisting = false)
1318
: base(stateContainer)
1419
{
@@ -25,7 +30,12 @@ public ControlSchemesView(VisualElement root, StateContainer stateContainer, boo
2530
controlSchemeVisualElement.Q<Button>(kSaveButton).clicked += SaveAndClose;
2631
controlSchemeVisualElement.Q<TextField>(kControlSchemeNameTextField).RegisterCallback<FocusOutEvent>(evt =>
2732
{
28-
Dispatch(ControlSchemeCommands.ChangeSelectedControlSchemeName(((TextField)evt.currentTarget).value));
33+
Dispatch((in InputActionsEditorState state) =>
34+
{
35+
m_NewName = ControlSchemeCommands.MakeUniqueControlSchemeName(state,
36+
((TextField)evt.currentTarget).value);
37+
return state.With(selectedControlScheme: state.selectedControlScheme);
38+
});
2939
});
3040

3141
m_ModalWindow = new VisualElement
@@ -102,10 +112,16 @@ public override void DestroyView()
102112

103113
private void SaveAndClose()
104114
{
105-
Dispatch(ControlSchemeCommands.SaveControlScheme(m_UpdateExisting));
115+
Dispatch(ControlSchemeCommands.SaveControlScheme(m_NewName, m_UpdateExisting));
106116
Close();
107117
}
108118

119+
private void Close()
120+
{
121+
m_NewName = "";
122+
OnClosing?.Invoke(this);
123+
}
124+
109125
private VisualElement MakeRequiredCell()
110126
{
111127
var ve = new VisualElement

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ internal interface IView
1818

1919
internal abstract class ViewBase<TViewState> : IView
2020
{
21-
public event Action<ViewBase<TViewState>> OnClosing;
22-
2321
protected ViewBase(StateContainer stateContainer)
2422
{
2523
this.stateContainer = stateContainer;
@@ -56,11 +54,6 @@ public TView CreateChildView<TView>(TView view) where TView : IView
5654
return view;
5755
}
5856

59-
public void Close()
60-
{
61-
OnClosing?.Invoke(this);
62-
}
63-
6457
public void DestroyChildView<TView>(TView view) where TView : IView
6558
{
6659
if (view == null)

0 commit comments

Comments
 (0)