BoolPref.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. namespace Animancer.Editor
  5. {
  6. /// <summary>[Editor-Only]
  7. /// A simple wrapper around <see cref="EditorPrefs"/> to get and set a bool.
  8. /// <para></para>
  9. /// If you're interested in a more comprehensive pref wrapper that supports more types, you should check out
  10. /// <see href="https://kybernetik.com.au/inspector-gadgets/docs/other/auto-prefs">Inspector Gadgets - Auto Prefs</see>.
  11. /// </summary>
  12. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/BoolPref
  13. ///
  14. public class BoolPref
  15. {
  16. /************************************************************************************************************************/
  17. /// <summary>The prefix which is automatically added before the <see cref="Key"/>.</summary>
  18. public const string KeyPrefix = nameof(Animancer) + "/";
  19. /// <summary>The identifier with which this pref will be saved.</summary>
  20. public readonly string Key;
  21. /// <summary>The label to use when adding a function to toggle this pref to a menu.</summary>
  22. public readonly string MenuItem;
  23. /// <summary>The starting value to use for this pref if none was previously saved.</summary>
  24. public readonly bool DefaultValue;
  25. /************************************************************************************************************************/
  26. private bool _HasValue;
  27. private bool _Value;
  28. /// <summary>The current value of this pref.</summary>
  29. public bool Value
  30. {
  31. get
  32. {
  33. if (!_HasValue)
  34. {
  35. _HasValue = true;
  36. _Value = EditorPrefs.GetBool(Key, DefaultValue);
  37. }
  38. return _Value;
  39. }
  40. set
  41. {
  42. if (_Value == value &&
  43. _HasValue)
  44. return;
  45. _Value = value;
  46. _HasValue = true;
  47. EditorPrefs.SetBool(Key, value);
  48. }
  49. }
  50. /// <summary>Returns the current value of the `pref`.</summary>
  51. public static implicit operator bool(BoolPref pref)
  52. => pref.Value;
  53. /************************************************************************************************************************/
  54. /// <summary>Creates a new <see cref="BoolPref"/>.</summary>
  55. public BoolPref(string menuItem, bool defaultValue = default)
  56. : this(null, menuItem, defaultValue) { }
  57. /// <summary>Creates a new <see cref="BoolPref"/>.</summary>
  58. public BoolPref(string keyPrefix, string menuItem, bool defaultValue = default)
  59. {
  60. MenuItem = menuItem + " ?";
  61. Key = KeyPrefix + keyPrefix + menuItem;
  62. DefaultValue = defaultValue;
  63. }
  64. /************************************************************************************************************************/
  65. /// <summary>Adds a menu function to toggle the <see cref="Value"/> of this pref.</summary>
  66. public void AddToggleFunction(GenericMenu menu)
  67. {
  68. menu.AddItem(new(MenuItem), Value, () =>
  69. {
  70. Value = !Value;
  71. });
  72. }
  73. /************************************************************************************************************************/
  74. /// <summary>Returns a string containing the <see cref="Key"/> and <see cref="Value"/>.</summary>
  75. public override string ToString()
  76. => $"{nameof(BoolPref)} (" +
  77. $"{nameof(Key)} = '{Key}'" +
  78. $", {nameof(Value)} = {Value})";
  79. /************************************************************************************************************************/
  80. }
  81. }
  82. #endif