ToggledSpeedSlider.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. using UnityEngine;
  5. using static Animancer.Editor.AnimancerGUI;
  6. namespace Animancer.Editor
  7. {
  8. /// <summary>[Editor-Only] Utility for a toggle which can show and hide a speed slider.</summary>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/ToggledSpeedSlider
  10. public class ToggledSpeedSlider
  11. {
  12. /************************************************************************************************************************/
  13. /// <summary>The content displayed on the toggle.</summary>
  14. /// <remarks>The <see cref="GUIContent.text"/> is set by the <see cref="Speed"/>.</remarks>
  15. public readonly GUIContent GUIContent = new();
  16. /// <summary>Is the toggle currently on?</summary>
  17. public readonly BoolPref IsOn;
  18. /************************************************************************************************************************/
  19. private float _Speed = float.NaN;
  20. /// <summary>The current speed value.</summary>
  21. public float Speed
  22. {
  23. get => _Speed;
  24. set
  25. {
  26. if (_Speed == value)
  27. return;
  28. _Speed = value;
  29. GUIContent.text = _Speed.ToString("0.0x");
  30. OnSetSpeed(_Speed);
  31. }
  32. }
  33. /// <summary>Called when the <see cref="Speed"/> is changed.</summary>
  34. protected virtual void OnSetSpeed(float speed) { }
  35. /************************************************************************************************************************/
  36. /// <summary>Creates a new <see cref="ToggledSpeedSlider"/>.</summary>
  37. public ToggledSpeedSlider(string prefKey)
  38. {
  39. IsOn = new(prefKey);
  40. }
  41. /************************************************************************************************************************/
  42. /// <summary>Draws a toggle to show or hide the speed slider.</summary>
  43. public virtual bool DoToggleGUI(Rect area, GUIStyle style)
  44. {
  45. HandleResetClick(area);
  46. style ??= GUI.skin.toggle;
  47. IsOn.Value = GUI.Toggle(area, IsOn, GUIContent, style);
  48. return IsOn;
  49. }
  50. /************************************************************************************************************************/
  51. private static float _SpeedLabelWidth;
  52. /// <summary>Draws a slider to control the <see cref="Speed"/>.</summary>
  53. public float DoSpeedSlider(ref Rect area, GUIStyle backgroundStyle = null)
  54. {
  55. if (!IsOn)
  56. return Speed;
  57. var sliderArea = StealLineFromTop(ref area);
  58. sliderArea = sliderArea.Expand(-StandardSpacing, 0);
  59. if (backgroundStyle != null)
  60. GUI.Label(sliderArea, GUIContent.none, backgroundStyle);
  61. HandleResetClick(sliderArea);
  62. var label = "Speed";
  63. if (_SpeedLabelWidth == 0)
  64. _SpeedLabelWidth = CalculateLabelWidth(label);
  65. var labelWidth = EditorGUIUtility.labelWidth;
  66. EditorGUIUtility.labelWidth = _SpeedLabelWidth;
  67. EditorGUI.BeginChangeCheck();
  68. var speed = EditorGUI.Slider(sliderArea, label, Speed, 0.1f, 2);
  69. if (EditorGUI.EndChangeCheck())
  70. Speed = speed;
  71. EditorGUIUtility.labelWidth = labelWidth;
  72. return Speed;
  73. }
  74. /************************************************************************************************************************/
  75. /// <summary>Handles Right or Middle Clicking in the `area` to reset the <see cref="Speed"/>.</summary>
  76. public void HandleResetClick(Rect area)
  77. {
  78. if (!TryUseClickEvent(area, 1) && !TryUseClickEvent(area, 2))
  79. return;
  80. if (Speed != 1)
  81. Speed = 1;
  82. else
  83. Speed = 0.5f;
  84. }
  85. /************************************************************************************************************************/
  86. }
  87. }
  88. #endif