TransitionPreviewWindow.Inspector.cs 6.2 KB

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