File size: 2,738 Bytes
18a519f | 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 | using System;
// Some fields assigned through only through serialization.
#pragma warning disable CS0649
namespace UnityEngine.InputSystem.Samples
{
/// <summary>
/// Base class for <see cref="InputActionVisualizer"/> and <see cref="InputControlVisualizer"/>.
/// Not meant to be extended outside of input system.
/// </summary>
public abstract class InputVisualizer : MonoBehaviour
{
protected void OnEnable()
{
ResolveParent();
}
protected void OnDisable()
{
m_Parent = null;
m_Visualizer = null;
}
protected void OnGUI()
{
if (Event.current.type != EventType.Repaint)
return;
// If we have a parent, offset our rect by the parent.
var rect = m_Rect;
if (m_Parent != null)
rect.position += m_Parent.m_Rect.position;
if (m_Visualizer != null)
m_Visualizer.OnDraw(rect);
else
VisualizationHelpers.DrawRectangle(rect, new Color(1, 1, 1, 0.1f));
// Draw label, if we have one.
if (!string.IsNullOrEmpty(m_Label))
{
if (m_LabelContent == null)
m_LabelContent = new GUIContent(m_Label);
if (s_LabelStyle == null)
{
s_LabelStyle = new GUIStyle();
s_LabelStyle.normal.textColor = Color.yellow;
}
////FIXME: why does CalcSize not calculate the rect width correctly?
var labelSize = s_LabelStyle.CalcSize(m_LabelContent);
var labelRect = new Rect(rect.x + 4, rect.y, labelSize.x + 4, rect.height);
s_LabelStyle.Draw(labelRect, m_LabelContent, false, false, false, false);
}
}
protected void OnValidate()
{
ResolveParent();
m_LabelContent = null;
}
protected void ResolveParent()
{
var parentTransform = transform.parent;
if (parentTransform != null)
m_Parent = parentTransform.GetComponent<InputControlVisualizer>();
}
[SerializeField] internal string m_Label;
[SerializeField] internal int m_HistorySamples = 500;
[SerializeField] internal float m_TimeWindow = 3;
[SerializeField] internal Rect m_Rect = new Rect(10, 10, 300, 30);
[NonSerialized] internal GUIContent m_LabelContent;
[NonSerialized] internal VisualizationHelpers.Visualizer m_Visualizer;
[NonSerialized] internal InputVisualizer m_Parent;
private static GUIStyle s_LabelStyle;
}
}
|