-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathRebindActionUIEditor.cs
177 lines (151 loc) · 7.77 KB
/
RebindActionUIEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#if UNITY_EDITOR
using System.Linq;
using UnityEditor;
////TODO: support multi-object editing
namespace UnityEngine.InputSystem.Samples.RebindUI
{
/// <summary>
/// A custom inspector for <see cref="RebindActionUI"/> which provides a more convenient way for
/// picking the binding which to rebind.
/// </summary>
[CustomEditor(typeof(RebindActionUI))]
public class RebindActionUIEditor : UnityEditor.Editor
{
protected void OnEnable()
{
m_ActionProperty = serializedObject.FindProperty("m_Action");
m_BindingIdProperty = serializedObject.FindProperty("m_BindingId");
m_ActionLabelProperty = serializedObject.FindProperty("m_ActionLabel");
m_BindingTextProperty = serializedObject.FindProperty("m_BindingText");
m_RebindOverlayProperty = serializedObject.FindProperty("m_RebindOverlay");
m_RebindTextProperty = serializedObject.FindProperty("m_RebindText");
m_UpdateBindingUIEventProperty = serializedObject.FindProperty("m_UpdateBindingUIEvent");
m_RebindStartEventProperty = serializedObject.FindProperty("m_RebindStartEvent");
m_RebindStopEventProperty = serializedObject.FindProperty("m_RebindStopEvent");
m_DisplayStringOptionsProperty = serializedObject.FindProperty("m_DisplayStringOptions");
RefreshBindingOptions();
}
public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
// Binding section.
EditorGUILayout.LabelField(m_BindingLabel, Styles.boldLabel);
using (new EditorGUI.IndentLevelScope())
{
EditorGUILayout.PropertyField(m_ActionProperty);
var newSelectedBinding = EditorGUILayout.Popup(m_BindingLabel, m_SelectedBindingOption, m_BindingOptions);
if (newSelectedBinding != m_SelectedBindingOption)
{
var bindingId = m_BindingOptionValues[newSelectedBinding];
m_BindingIdProperty.stringValue = bindingId;
m_SelectedBindingOption = newSelectedBinding;
}
var optionsOld = (InputBinding.DisplayStringOptions)m_DisplayStringOptionsProperty.intValue;
var optionsNew = (InputBinding.DisplayStringOptions)EditorGUILayout.EnumFlagsField(m_DisplayOptionsLabel, optionsOld);
if (optionsOld != optionsNew)
m_DisplayStringOptionsProperty.intValue = (int)optionsNew;
}
// UI section.
EditorGUILayout.Space();
EditorGUILayout.LabelField(m_UILabel, Styles.boldLabel);
using (new EditorGUI.IndentLevelScope())
{
EditorGUILayout.PropertyField(m_ActionLabelProperty);
EditorGUILayout.PropertyField(m_BindingTextProperty);
EditorGUILayout.PropertyField(m_RebindOverlayProperty);
EditorGUILayout.PropertyField(m_RebindTextProperty);
}
// Events section.
EditorGUILayout.Space();
EditorGUILayout.LabelField(m_EventsLabel, Styles.boldLabel);
using (new EditorGUI.IndentLevelScope())
{
EditorGUILayout.PropertyField(m_RebindStartEventProperty);
EditorGUILayout.PropertyField(m_RebindStopEventProperty);
EditorGUILayout.PropertyField(m_UpdateBindingUIEventProperty);
}
if (EditorGUI.EndChangeCheck())
{
serializedObject.ApplyModifiedProperties();
RefreshBindingOptions();
}
}
protected void RefreshBindingOptions()
{
var actionReference = (InputActionReference)m_ActionProperty.objectReferenceValue;
var action = actionReference?.action;
if (action == null)
{
m_BindingOptions = new GUIContent[0];
m_BindingOptionValues = new string[0];
m_SelectedBindingOption = -1;
return;
}
var bindings = action.bindings;
var bindingCount = bindings.Count;
m_BindingOptions = new GUIContent[bindingCount];
m_BindingOptionValues = new string[bindingCount];
m_SelectedBindingOption = -1;
var currentBindingId = m_BindingIdProperty.stringValue;
for (var i = 0; i < bindingCount; ++i)
{
var binding = bindings[i];
var bindingId = binding.id.ToString();
var haveBindingGroups = !string.IsNullOrEmpty(binding.groups);
// If we don't have a binding groups (control schemes), show the device that if there are, for example,
// there are two bindings with the display string "A", the user can see that one is for the keyboard
// and the other for the gamepad.
var displayOptions =
InputBinding.DisplayStringOptions.DontUseShortDisplayNames | InputBinding.DisplayStringOptions.IgnoreBindingOverrides;
if (!haveBindingGroups)
displayOptions |= InputBinding.DisplayStringOptions.DontOmitDevice;
// Create display string.
var displayString = action.GetBindingDisplayString(i, displayOptions);
// If binding is part of a composite, include the part name.
if (binding.isPartOfComposite)
displayString = $"{ObjectNames.NicifyVariableName(binding.name)}: {displayString}";
// Some composites use '/' as a separator. When used in popup, this will lead to to submenus. Prevent
// by instead using a backlash.
displayString = displayString.Replace('/', '\\');
// If the binding is part of control schemes, mention them.
if (haveBindingGroups)
{
var asset = action.actionMap?.asset;
if (asset != null)
{
var controlSchemes = string.Join(", ",
binding.groups.Split(InputBinding.Separator)
.Select(x => asset.controlSchemes.FirstOrDefault(c => c.bindingGroup == x).name));
displayString = $"{displayString} ({controlSchemes})";
}
}
m_BindingOptions[i] = new GUIContent(displayString);
m_BindingOptionValues[i] = bindingId;
if (currentBindingId == bindingId)
m_SelectedBindingOption = i;
}
}
private SerializedProperty m_ActionProperty;
private SerializedProperty m_BindingIdProperty;
private SerializedProperty m_ActionLabelProperty;
private SerializedProperty m_BindingTextProperty;
private SerializedProperty m_RebindOverlayProperty;
private SerializedProperty m_RebindTextProperty;
private SerializedProperty m_RebindStartEventProperty;
private SerializedProperty m_RebindStopEventProperty;
private SerializedProperty m_UpdateBindingUIEventProperty;
private SerializedProperty m_DisplayStringOptionsProperty;
private GUIContent m_BindingLabel = new GUIContent("Binding");
private GUIContent m_DisplayOptionsLabel = new GUIContent("Display Options");
private GUIContent m_UILabel = new GUIContent("UI");
private GUIContent m_EventsLabel = new GUIContent("Events");
private GUIContent[] m_BindingOptions;
private string[] m_BindingOptionValues;
private int m_SelectedBindingOption;
private static class Styles
{
public static GUIStyle boldLabel = new GUIStyle("MiniBoldLabel");
}
}
}
#endif