-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathBindingUI.cs
More file actions
146 lines (119 loc) · 5.79 KB
/
BindingUI.cs
File metadata and controls
146 lines (119 loc) · 5.79 KB
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
#if UNITY_EDITOR
using System;
using System.Linq;
using UnityEditor;
namespace UnityEngine.InputSystem.Samples.RebindUI
{
/// <summary>
/// Common binding UI helper to allow editor composition.
/// </summary>
internal class BindingUI
{
private readonly SerializedProperty m_ActionProperty;
private readonly SerializedProperty m_BindingIdProperty;
private readonly SerializedProperty m_DisplayStringOptionsProperty;
public BindingUI(SerializedObject serializedObject)
: this(serializedObject.FindProperty("m_Action"), serializedObject.FindProperty("m_BindingId"),
serializedObject.FindProperty("m_DisplayStringOptions"))
{}
public BindingUI(SerializedProperty actionProperty, SerializedProperty bindingIdProperty,
SerializedProperty displayStringOptionsProperty = null)
{
m_ActionProperty = actionProperty;
m_BindingIdProperty = bindingIdProperty;
m_DisplayStringOptionsProperty = displayStringOptionsProperty;
Reset();
Refresh();
}
private void Reset()
{
bindingOptions = Array.Empty<GUIContent>();
bindingOptionValues = Array.Empty<string>();
selectedBindingIndex = -1;
}
public void Draw()
{
// Binding section.
EditorGUILayout.LabelField(m_BindingLabel);
using (new EditorGUI.IndentLevelScope())
{
EditorGUILayout.PropertyField(m_ActionProperty);
var newSelectedBinding = EditorGUILayout.Popup(m_BindingLabel, selectedBindingIndex, bindingOptions);
if (newSelectedBinding != selectedBindingIndex)
{
var id = bindingOptionValues[newSelectedBinding];
m_BindingIdProperty.stringValue = id;
selectedBindingIndex = newSelectedBinding;
}
if (m_DisplayStringOptionsProperty != null)
{
var optionsOld = (InputBinding.DisplayStringOptions)m_DisplayStringOptionsProperty.intValue;
var optionsNew = (InputBinding.DisplayStringOptions)EditorGUILayout.EnumFlagsField(m_DisplayOptionsLabel, optionsOld);
if (optionsOld != optionsNew)
m_DisplayStringOptionsProperty.intValue = (int)optionsNew;
}
}
}
public bool Refresh()
{
if (action == null)
{
Reset();
return false;
}
var bindings = action.bindings;
var bindingCount = bindings.Count;
bindingOptions = new GUIContent[bindingCount];
bindingOptionValues = new string[bindingCount];
selectedBindingIndex = -1;
var currentBindingId = m_BindingIdProperty.stringValue;
for (var i = 0; i < bindingCount; ++i)
{
var binding = bindings[i];
var id = 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})";
}
}
bindingOptions[i] = new GUIContent(displayString);
bindingOptionValues[i] = id;
if (currentBindingId == id)
selectedBindingIndex = i;
}
return true;
}
public string bindingId => m_BindingIdProperty.stringValue;
public int bindingIndex => action.FindBindingById(m_BindingIdProperty.stringValue);
public InputAction action => ((InputActionReference)m_ActionProperty.objectReferenceValue)?.action;
private GUIContent[] bindingOptions { get; set; }
private string[] bindingOptionValues { get; set; }
private int selectedBindingIndex { get; set; }
private readonly GUIContent m_BindingLabel = new GUIContent("Binding");
private readonly GUIContent m_DisplayOptionsLabel = new GUIContent("Display Options");
}
}
#endif // UNITY_EDITOR