-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy pathInputActionVisualizer.cs
325 lines (277 loc) · 12.2 KB
/
InputActionVisualizer.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using System;
using System.Collections.Generic;
////TODO: support ProcessEventsManually
////TODO: add way to pick by player index
// Some fields assigned through only through serialization.
#pragma warning disable CS0649
namespace UnityEngine.InputSystem.Samples
{
/// <summary>
/// A component for debugging purposes that adds an on-screen display which shows
/// activity on an input action over time (<see cref="InputActionVisualizer.Visualization.Interaction"/>)
/// or an action's current value (<see cref="InputActionVisualizer.Visualization.Value"/>).
/// </summary>
/// <seealso cref="InputControlVisualizer"/>
[AddComponentMenu("Input/Debug/Input Action Visualizer")]
[ExecuteInEditMode]
public class InputActionVisualizer : InputVisualizer
{
/// <summary>
/// The action that is being visualized. May be null.
/// </summary>
public InputAction action => m_Action;
protected void FixedUpdate()
{
if (m_Visualization != Visualization.Value || m_Action == null || m_Visualizer == null)
return;
if (InputSystem.settings.updateMode != InputSettings.UpdateMode.ProcessEventsInFixedUpdate)
return;
RecordValue(Time.fixedTime);
}
protected void Update()
{
if (m_Visualization != Visualization.Value || m_Action == null || m_Visualizer == null)
return;
if (InputSystem.settings.updateMode != InputSettings.UpdateMode.ProcessEventsInDynamicUpdate)
return;
RecordValue(Time.time);
}
protected new void OnEnable()
{
if (m_Visualization == Visualization.None)
return;
base.OnEnable();
ResolveAction();
SetupVisualizer();
if (s_EnabledInstances == null)
s_EnabledInstances = new List<InputActionVisualizer>();
if (s_EnabledInstances.Count == 0)
InputSystem.onActionChange += OnActionChange;
s_EnabledInstances.Add(this);
}
protected new void OnDisable()
{
base.OnDisable();
if (s_EnabledInstances != null)
{
s_EnabledInstances.Remove(this);
if (s_EnabledInstances.Count == 0)
InputSystem.onActionChange -= OnActionChange;
}
if (m_Visualization == Visualization.Interaction && m_Action != null)
{
m_Action.started -= OnActionTriggered;
m_Action.performed -= OnActionTriggered;
m_Action.canceled -= OnActionTriggered;
}
}
protected new void OnGUI()
{
if (m_Visualization == Visualization.None)
return;
if (Event.current.type != EventType.Repaint)
return;
base.OnGUI();
if (m_ShowControlName && m_ActiveControlName != null)
VisualizationHelpers.DrawText(m_ActiveControlName, new Vector2(m_Rect.x, m_Rect.yMax),
VisualizationHelpers.ValueTextStyle);
}
private void RecordValue(double time)
{
Debug.Assert(m_Action != null);
Debug.Assert(m_Visualizer != null);
var value = m_Action.ReadValueAsObject();
m_Visualizer.AddSample(value, time);
if (m_ShowControlName)
RecordControlName();
}
private void RecordControlName()
{
var control = m_Action.activeControl;
if (control == m_ActiveControl)
return;
m_ActiveControl = control;
m_ActiveControlName = control != null ? new GUIContent(control.path) : null;
}
private void ResolveAction()
{
// If we have a reference to an action, try that first.
if (m_ActionReference != null)
m_Action = m_ActionReference.action;
// If we didn't get an action from that but we have an action name,
// just search through the currently enabled actions for one that
// matches by name.
if (m_Action == null && !string.IsNullOrEmpty(m_ActionName))
{
var slashIndex = m_ActionName.IndexOf('/');
var mapName = slashIndex != -1 ? m_ActionName.Substring(0, slashIndex) : null;
var actionName = slashIndex != -1 ? m_ActionName.Substring(slashIndex + 1) : m_ActionName;
var enabledActions = InputSystem.ListEnabledActions();
foreach (var action in enabledActions)
{
if (string.Compare(actionName, action.name, StringComparison.InvariantCultureIgnoreCase) != 0)
continue;
if (mapName != null && action.actionMap != null && string.Compare(mapName, action.actionMap.name,
StringComparison.InvariantCultureIgnoreCase) != 0)
continue;
m_Action = action;
break;
}
}
// If we still don't have an action, there's nothing much for us to do.
// The action may show up at a later point.
if (m_Action == null)
return;
if (m_Visualization == Visualization.Interaction)
{
m_Action.performed += OnActionTriggered;
m_Action.started += OnActionTriggered;
m_Action.canceled += OnActionTriggered;
}
}
private void SetupVisualizer()
{
m_Visualizer = null;
if (m_Action == null)
return;
switch (m_Visualization)
{
case Visualization.Value:
switch (m_Action.type)
{
case InputActionType.Button:
m_Visualizer = new VisualizationHelpers.ScalarVisualizer<float>
{
limitMax = 1
};
break;
case InputActionType.Value:
case InputActionType.PassThrough:
if (!string.IsNullOrEmpty(m_Action.expectedControlType))
{
var layout = InputSystem.LoadLayout(m_Action.expectedControlType);
if (layout != null)
{
var valueType = layout.GetValueType();
if (valueType == typeof(float))
m_Visualizer = new VisualizationHelpers.ScalarVisualizer<float>
{
limitMax = 1
};
else if (valueType == typeof(int))
m_Visualizer = new VisualizationHelpers.ScalarVisualizer<int>
{
limitMax = 1
};
else if (valueType == typeof(Vector2))
m_Visualizer = new VisualizationHelpers.Vector2Visualizer();
}
}
break;
}
break;
case Visualization.Interaction:
// We don't really know which interactions are sitting on the action and its bindings
// and while we could do and perform work to find out, it's simpler to just wait until
// we get input and then whatever interactions we encounter as we go along. Also keeps
// the visualization a little less cluttered.
m_Visualizer = new VisualizationHelpers.TimelineVisualizer();
break;
}
}
private void OnActionDisabled()
{
}
private void OnActionTriggered(InputAction.CallbackContext context)
{
Debug.Assert(m_Visualization == Visualization.Interaction);
var timelineName = "Default";
var interaction = context.interaction;
if (interaction != null)
{
timelineName = interaction.GetType().Name;
if (timelineName.EndsWith("Interaction"))
timelineName = timelineName.Substring(0, timelineName.Length - "Interaction".Length);
}
var visualizer = (VisualizationHelpers.TimelineVisualizer)m_Visualizer;
var timelineIndex = visualizer.GetTimeline(timelineName);
if (timelineIndex == -1)
{
Color color;
timelineIndex = visualizer.timelineCount;
if (timelineIndex < s_InteractionColors.Length)
color = s_InteractionColors[timelineIndex];
else
color = new Color(Random.value, Random.value, Random.value, 1);
visualizer.AddTimeline(timelineName, color);
if (timelineIndex > 0)
visualizer.showLegend = true;
}
var time = (float)context.time;
switch (context.phase)
{
case InputActionPhase.Canceled:
visualizer.AddSample(timelineIndex, 0f, time);
break;
case InputActionPhase.Performed:
visualizer.AddSample(timelineIndex, 1f, time);
visualizer.AddSample(timelineIndex, 0f, time);
break;
case InputActionPhase.Started:
visualizer.AddSample(timelineIndex, 0.5f, time);
break;
}
if (m_ShowControlName)
RecordControlName();
}
private static void OnActionChange(object actionOrMap, InputActionChange change)
{
switch (change)
{
case InputActionChange.ActionEnabled:
case InputActionChange.ActionMapEnabled:
for (var i = 0; i < s_EnabledInstances.Count; ++i)
if (s_EnabledInstances[i].m_Action == null)
{
s_EnabledInstances[i].ResolveAction();
if (s_EnabledInstances[i].m_Action != null)
s_EnabledInstances[i].SetupVisualizer();
}
break;
case InputActionChange.ActionDisabled:
for (var i = 0; i < s_EnabledInstances.Count; ++i)
if (actionOrMap == s_EnabledInstances[i].m_Action)
s_EnabledInstances[i].OnActionDisabled();
break;
case InputActionChange.ActionMapDisabled:
for (var i = 0; i < s_EnabledInstances.Count; ++i)
if (s_EnabledInstances[i].m_Action?.actionMap == actionOrMap)
s_EnabledInstances[i].OnActionDisabled();
break;
}
}
[SerializeField] private Visualization m_Visualization;
[SerializeField] private InputActionReference m_ActionReference;
[SerializeField] private string m_ActionName;
[SerializeField] private bool m_ShowControlName;
[NonSerialized] private InputAction m_Action;
[NonSerialized] private InputControl m_ActiveControl;
[NonSerialized] private GUIContent m_ActiveControlName;
private static List<InputActionVisualizer> s_EnabledInstances;
private static readonly Color[] s_InteractionColors =
{
new Color(1, 0, 0, 1),
new Color(0, 0, 1, 1),
new Color(1, 1, 0, 1),
new Color(1, 0, 1, 1),
new Color(0, 1, 1, 1),
new Color(0, 1, 0, 1),
};
public enum Visualization
{
None,
Value,
Interaction,
}
}
}