DebugUIDrawer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. namespace UnityEditor.Rendering
  5. {
  6. /// <summary>
  7. /// Attribute specifying wich type of Debug Item should this drawer be used with.
  8. /// </summary>
  9. public class DebugUIDrawerAttribute : Attribute
  10. {
  11. internal readonly Type type;
  12. /// <summary>
  13. /// Constructor for DebugUIDraw Attribute
  14. /// </summary>
  15. /// <param name="type">Type of Debug Item this draw should be used with.</param>
  16. public DebugUIDrawerAttribute(Type type)
  17. {
  18. this.type = type;
  19. }
  20. }
  21. /// <summary>
  22. /// Debug Item Drawer
  23. /// </summary>
  24. public class DebugUIDrawer
  25. {
  26. /// <summary>
  27. /// Cast into the proper type.
  28. /// </summary>
  29. /// <typeparam name="T">Type of the drawer</typeparam>
  30. /// <param name="o">Object to be cast</param>
  31. /// <returns>Returns o cast to type T</returns>
  32. protected T Cast<T>(object o)
  33. where T : class
  34. {
  35. var casted = o as T;
  36. string typeName = o == null ? "null" : o.GetType().ToString();
  37. if (casted == null)
  38. throw new InvalidOperationException("Can't cast " + typeName + " to " + typeof(T));
  39. return casted;
  40. }
  41. /// <summary>
  42. /// Implement this to execute processing before UI rendering.
  43. /// </summary>
  44. /// <param name="widget">Widget that is going to be rendered.</param>
  45. /// <param name="state">Debug State associated with the Debug Item.</param>
  46. public virtual void Begin(DebugUI.Widget widget, DebugState state)
  47. {}
  48. /// <summary>
  49. /// Implement this to execute UI rendering.
  50. /// </summary>
  51. /// <param name="widget">Widget that is going to be rendered.</param>
  52. /// <param name="state">Debug State associated with the Debug Item.</param>
  53. /// <returns>Returns the state of the widget.</returns>
  54. public virtual bool OnGUI(DebugUI.Widget widget, DebugState state)
  55. {
  56. return true;
  57. }
  58. /// <summary>
  59. /// Implement this to execute processing after UI rendering.
  60. /// </summary>
  61. /// <param name="widget">Widget that is going to be rendered.</param>
  62. /// <param name="state">Debug State associated with the Debug Item.</param>
  63. public virtual void End(DebugUI.Widget widget, DebugState state)
  64. {}
  65. /// <summary>
  66. /// Applies a value to the widget and the Debug State of the Debug Item.
  67. /// </summary>
  68. /// <param name="widget">Debug Item widget.</param>
  69. /// <param name="state">Debug State associated with the Debug Item</param>
  70. /// <param name="value">Input value.</param>
  71. protected void Apply(DebugUI.IValueField widget, DebugState state, object value)
  72. {
  73. Undo.RegisterCompleteObjectUndo(state, "Debug Property Change");
  74. state.SetValue(value, widget);
  75. widget.SetValue(value);
  76. EditorUtility.SetDirty(state);
  77. DebugState.m_CurrentDirtyState = state;
  78. UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
  79. }
  80. /// <summary>
  81. /// Prepares the rendering Rect of the Drawer/
  82. /// </summary>
  83. /// <param name="height">Height of the rect.</param>
  84. /// <returns>Appropriate Rect for drawing.</returns>
  85. protected Rect PrepareControlRect(float height = -1)
  86. {
  87. if (height < 0)
  88. height = EditorGUIUtility.singleLineHeight;
  89. var rect = GUILayoutUtility.GetRect(1f, 1f, height, height);
  90. rect.width -= 2f;
  91. rect.xMin += 2f;
  92. EditorGUIUtility.labelWidth = rect.width / 2f;
  93. return rect;
  94. }
  95. }
  96. }