TransitionPreviewWindow.Inspector.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6. namespace Animancer.Editor
  7. {
  8. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/TransitionPreviewWindow
  9. partial class TransitionPreviewWindow
  10. {
  11. /// <summary>[Internal] Custom Inspector for the <see cref="TransitionPreviewWindow"/>.</summary>
  12. /// <remarks>
  13. /// Documentation: <see href="https://kybernetik.com.au/animancer/docs/manual/transitions#previews">Previews</see>
  14. /// </remarks>
  15. [CustomEditor(typeof(TransitionPreviewWindow))]
  16. internal class Inspector : UnityEditor.Editor
  17. {
  18. /************************************************************************************************************************/
  19. private static readonly string[]
  20. TabNames = { "Preview", "Settings" };
  21. private const int
  22. PreviewTab = 0,
  23. SettingsTab = 1;
  24. /************************************************************************************************************************/
  25. [SerializeField]
  26. private int _CurrentTab;
  27. private readonly AnimancerPlayableDrawer
  28. PlayableDrawer = new AnimancerPlayableDrawer();
  29. public TransitionPreviewWindow Target { get; private set; }
  30. /************************************************************************************************************************/
  31. public override bool UseDefaultMargins() => false;
  32. /************************************************************************************************************************/
  33. public override void OnInspectorGUI()
  34. {
  35. GUILayout.Space(AnimancerGUI.StandardSpacing * 2);
  36. Target = (TransitionPreviewWindow)target;
  37. if (Event.current.type == EventType.MouseDown)
  38. Target.ShowTab();
  39. _CurrentTab = GUILayout.Toolbar(_CurrentTab, TabNames);
  40. _CurrentTab = Mathf.Clamp(_CurrentTab, 0, TabNames.Length - 1);
  41. switch (_CurrentTab)
  42. {
  43. case PreviewTab: DoPreviewInspectorGUI(); break;
  44. case SettingsTab: Settings.DoInspectorGUI(); break;
  45. default: GUILayout.Label("Tab index is out of bounds"); break;
  46. }
  47. }
  48. /************************************************************************************************************************/
  49. private void DoPreviewInspectorGUI()
  50. {
  51. if (!Target._TransitionProperty.IsValid())
  52. {
  53. EditorGUILayout.HelpBox("No target property", MessageType.Info, true);
  54. Target.DestroyTransitionProperty();
  55. return;
  56. }
  57. DoTransitionPropertyGUI();
  58. DoTransitionGUI();
  59. Target._Animations.DoGUI();
  60. var animancer = Target._Scene.Animancer;
  61. if (animancer != null)
  62. {
  63. PlayableDrawer.DoGUI(animancer.Component);
  64. if (animancer.IsGraphPlaying)
  65. GUI.changed = true;
  66. }
  67. }
  68. /************************************************************************************************************************/
  69. /// <summary>The tooltip used for the Close button.</summary>
  70. public const string CloseTooltip = "Close the Transition Preview Window";
  71. /// <summary>Draws the target object and path of the <see cref="TransitionProperty"/>.</summary>
  72. private void DoTransitionPropertyGUI()
  73. {
  74. var property = Target._TransitionProperty;
  75. property.Update();
  76. using (new EditorGUI.DisabledScope(true))
  77. {
  78. EditorGUI.showMixedValue = property.TargetObjects.Length > 1;
  79. EditorGUILayout.ObjectField(property.TargetObject, typeof(Object), true);
  80. EditorGUI.showMixedValue = false;
  81. GUILayout.BeginHorizontal();
  82. {
  83. GUILayout.Label(property.Property.GetFriendlyPath());
  84. GUI.enabled = true;
  85. using (ObjectPool.Disposable.AcquireContent(out var label, "Close", CloseTooltip))
  86. {
  87. if (GUILayout.Button(label, EditorStyles.miniButton, AnimancerGUI.DontExpandWidth))
  88. {
  89. Target.Close();
  90. GUIUtility.ExitGUI();
  91. }
  92. }
  93. }
  94. GUILayout.EndHorizontal();
  95. }
  96. }
  97. /************************************************************************************************************************/
  98. private void DoTransitionGUI()
  99. {
  100. var property = Target._TransitionProperty;
  101. var isExpanded = property.Property.isExpanded;
  102. property.Property.isExpanded = true;
  103. var height = EditorGUI.GetPropertyHeight(property, true);
  104. const float Indent = 12;
  105. var padding = GUI.skin.box.padding;
  106. var area = GUILayoutUtility.GetRect(0, height + padding.horizontal - padding.bottom);
  107. area.x += Indent + padding.left;
  108. area.width -= Indent + padding.horizontal;
  109. EditorGUI.BeginChangeCheck();
  110. EditorGUI.PropertyField(area, property, true);
  111. property.Property.isExpanded = isExpanded;
  112. if (EditorGUI.EndChangeCheck())
  113. property.ApplyModifiedProperties();
  114. }
  115. /************************************************************************************************************************/
  116. }
  117. }
  118. }
  119. #endif