InputVisualizer.cs 2.7 KB

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