FadeGroupDrawer.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace Animancer.Editor
  7. {
  8. /// <summary>[Editor-Only] A custom GUI for <see cref="FadeGroup"/>.</summary>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/FadeGroupDrawer
  10. [CustomGUI(typeof(FadeGroup))]
  11. public class FadeGroupDrawer : CustomGUI<FadeGroup>
  12. {
  13. /************************************************************************************************************************/
  14. private bool _IsExpanded;
  15. private AnimationCurve _DisplayCurve;
  16. /************************************************************************************************************************/
  17. /// <inheritdoc/>
  18. public override void DoGUI()
  19. {
  20. _IsExpanded = EditorGUILayout.Foldout(_IsExpanded, "", true);
  21. var area = GUILayoutUtility.GetLastRect();
  22. InitializeDisplayCurve(ref _DisplayCurve);
  23. _DisplayCurve = EditorGUI.CurveField(area, TargetName, _DisplayCurve);
  24. if (_IsExpanded)
  25. DoDetailsGUI();
  26. }
  27. /************************************************************************************************************************/
  28. /// <summary>The display name of the target.</summary>
  29. protected virtual string TargetName
  30. {
  31. get
  32. {
  33. var name = Value.GetType().GetNameCS(false);
  34. if (!Value.IsValid)
  35. name += " (Cancelled)";
  36. return name;
  37. }
  38. }
  39. /************************************************************************************************************************/
  40. private static readonly Keyframe[] DisplayCurveKeyframes = new Keyframe[16];
  41. /// <summary>Initializes the `curve` to represent the target's fade values over normalized time.</summary>
  42. protected virtual void InitializeDisplayCurve(ref AnimationCurve curve)
  43. {
  44. curve ??= new();
  45. try
  46. {
  47. var increment = 1f / (DisplayCurveKeyframes.Length - 1);
  48. for (int i = 0; i < DisplayCurveKeyframes.Length; i++)
  49. {
  50. var progress = increment * i;
  51. var weight = Value.Easing != null
  52. ? Value.Easing(progress)
  53. : progress;
  54. DisplayCurveKeyframes[i] = new(progress, weight);
  55. }
  56. }
  57. catch (Exception exception)
  58. {
  59. Debug.LogException(exception);
  60. Array.Clear(DisplayCurveKeyframes, 0, DisplayCurveKeyframes.Length);
  61. }
  62. curve.keys = DisplayCurveKeyframes;
  63. }
  64. /************************************************************************************************************************/
  65. /// <summary>Draws the GUI for the target's fields.</summary>
  66. protected virtual void DoDetailsGUI()
  67. {
  68. EditorGUI.indentLevel++;
  69. EditorGUI.BeginChangeCheck();
  70. Value.NormalizedTime = EditorGUILayout.Slider("Normalized Time", Value.NormalizedTime, 0, 1);
  71. if (EditorGUI.EndChangeCheck())
  72. {
  73. Value.NormalizedTime = Mathf.Clamp(Value.NormalizedTime, 0, 0.99f);
  74. Value.ApplyWeights();
  75. }
  76. EditorGUI.BeginChangeCheck();
  77. var fadeDuration = EditorGUILayout.FloatField("Fade Duration", Value.FadeDuration);
  78. if (EditorGUI.EndChangeCheck())
  79. Value.FadeDuration = fadeDuration;
  80. EditorGUILayout.LabelField(
  81. Value.TargetWeight > 0 ? "Fade In" : "Fade Out",
  82. "To " + Value.TargetWeight);
  83. EditorGUI.indentLevel++;
  84. DoNodeWeightGUI(Value.FadeIn);
  85. EditorGUI.indentLevel--;
  86. var fadeOutCount = Value.FadeOut.Count;
  87. if (fadeOutCount > 0)
  88. {
  89. EditorGUILayout.LabelField("Fade Out", fadeOutCount.ToStringCached());
  90. EditorGUI.indentLevel++;
  91. for (int i = 0; i < fadeOutCount; i++)
  92. DoNodeWeightGUI(Value.FadeOut[i]);
  93. EditorGUI.indentLevel--;
  94. }
  95. EditorGUI.indentLevel--;
  96. }
  97. /************************************************************************************************************************/
  98. /// <summary>Draws the GUI for the given `nodeWeight`.</summary>
  99. private void DoNodeWeightGUI(NodeWeight nodeWeight)
  100. {
  101. EditorGUILayout.LabelField(nodeWeight.Node?.GetPath());
  102. }
  103. /************************************************************************************************************************/
  104. }
  105. }
  106. #endif