InputVisualizer.cs (2738B)
1 using System; 2 3 // Some fields assigned through only through serialization. 4 #pragma warning disable CS0649 5 6 namespace UnityEngine.InputSystem.Samples 7 { 8 /// <summary> 9 /// Base class for <see cref="InputActionVisualizer"/> and <see cref="InputControlVisualizer"/>. 10 /// Not meant to be extended outside of input system. 11 /// </summary> 12 public abstract class InputVisualizer : MonoBehaviour 13 { 14 protected void OnEnable() 15 { 16 ResolveParent(); 17 } 18 19 protected void OnDisable() 20 { 21 m_Parent = null; 22 m_Visualizer = null; 23 } 24 25 protected void OnGUI() 26 { 27 if (Event.current.type != EventType.Repaint) 28 return; 29 30 // If we have a parent, offset our rect by the parent. 31 var rect = m_Rect; 32 if (m_Parent != null) 33 rect.position += m_Parent.m_Rect.position; 34 35 if (m_Visualizer != null) 36 m_Visualizer.OnDraw(rect); 37 else 38 VisualizationHelpers.DrawRectangle(rect, new Color(1, 1, 1, 0.1f)); 39 40 // Draw label, if we have one. 41 if (!string.IsNullOrEmpty(m_Label)) 42 { 43 if (m_LabelContent == null) 44 m_LabelContent = new GUIContent(m_Label); 45 if (s_LabelStyle == null) 46 { 47 s_LabelStyle = new GUIStyle(); 48 s_LabelStyle.normal.textColor = Color.yellow; 49 } 50 51 ////FIXME: why does CalcSize not calculate the rect width correctly? 52 var labelSize = s_LabelStyle.CalcSize(m_LabelContent); 53 var labelRect = new Rect(rect.x + 4, rect.y, labelSize.x + 4, rect.height); 54 55 s_LabelStyle.Draw(labelRect, m_LabelContent, false, false, false, false); 56 } 57 } 58 59 protected void OnValidate() 60 { 61 ResolveParent(); 62 m_LabelContent = null; 63 } 64 65 protected void ResolveParent() 66 { 67 var parentTransform = transform.parent; 68 if (parentTransform != null) 69 m_Parent = parentTransform.GetComponent<InputControlVisualizer>(); 70 } 71 72 [SerializeField] internal string m_Label; 73 [SerializeField] internal int m_HistorySamples = 500; 74 [SerializeField] internal float m_TimeWindow = 3; 75 [SerializeField] internal Rect m_Rect = new Rect(10, 10, 300, 30); 76 77 [NonSerialized] internal GUIContent m_LabelContent; 78 [NonSerialized] internal VisualizationHelpers.Visualizer m_Visualizer; 79 [NonSerialized] internal InputVisualizer m_Parent; 80 81 private static GUIStyle s_LabelStyle; 82 } 83 }