EditorPrefBoolFlags.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. namespace UnityEditor.Rendering
  3. {
  4. /// <summary>Bool flag saved in EditorPref</summary>
  5. /// <typeparam name="T">Underlying enum type</typeparam>
  6. public struct EditorPrefBoolFlags<T> : IEquatable<T>, IEquatable<EditorPrefBoolFlags<T>>
  7. where T : struct, IConvertible
  8. {
  9. readonly string m_Key;
  10. /// <summary>The value as the underlying enum type used</summary>
  11. public T value
  12. { get => (T)(object)EditorPrefs.GetInt(m_Key); set => EditorPrefs.SetInt(m_Key, (int)(object)value); }
  13. /// <summary>The raw value</summary>
  14. public uint rawValue
  15. { get => (uint)EditorPrefs.GetInt(m_Key); set => EditorPrefs.SetInt(m_Key, (int)value); }
  16. /// <summary>Constructor</summary>
  17. /// <param name="key">Name of the Key in EditorPrefs to save the value</param>
  18. public EditorPrefBoolFlags(string key) => m_Key = key;
  19. /// <summary>Test if saved value is equal to the one given</summary>
  20. /// <param name="other">Given value</param>
  21. /// <returns>True if value are the same</returns>
  22. public bool Equals(T other) => (int)(object)value == (int)(object)other;
  23. /// <summary>Test if this EditorPrefBoolFlags is the same than the given one</summary>
  24. /// <param name="other">Given EditorPrefBoolFlags</param>
  25. /// <returns>True if they use the same value</returns>
  26. public bool Equals(EditorPrefBoolFlags<T> other) => m_Key == other.m_Key;
  27. /// <summary>Test if the given flags are set</summary>
  28. /// <param name="v">Given flags</param>
  29. /// <returns>True: all the given flags are set</returns>
  30. public bool HasFlag(T v) => ((uint)(int)(object)v & rawValue) == (uint)(int)(object)v;
  31. /// <summary>Set or unset the flags</summary>
  32. /// <param name="f">Flags to edit</param>
  33. /// <param name="v">Boolean value to set to the given flags</param>
  34. public void SetFlag(T f, bool v)
  35. {
  36. if (v) rawValue |= (uint)(int)(object)f;
  37. else rawValue &= ~(uint)(int)(object)f;
  38. }
  39. /// <summary>Explicit conversion operator to the underlying type</summary>
  40. /// <param name="v">The EditorPrefBoolFlags to convert</param>
  41. /// <returns>The converted value</returns>
  42. public static explicit operator T(EditorPrefBoolFlags<T> v) => v.value;
  43. /// <summary>Or operator between a EditorPrefBoolFlags and a value</summary>
  44. /// <param name="l">The EditorPrefBoolFlags</param>
  45. /// <param name="r">The value</param>
  46. /// <returns>A EditorPrefBoolFlags with OR operator performed</returns>
  47. public static EditorPrefBoolFlags<T> operator |(EditorPrefBoolFlags<T> l, T r)
  48. {
  49. l.rawValue |= (uint)(int)(object)r;
  50. return l;
  51. }
  52. /// <summary>And operator between a EditorPrefBoolFlags and a value</summary>
  53. /// <param name="l">The EditorPrefBoolFlags</param>
  54. /// <param name="r">The value</param>
  55. /// <returns>A EditorPrefBoolFlags with AND operator performed</returns>
  56. public static EditorPrefBoolFlags<T> operator &(EditorPrefBoolFlags<T> l, T r)
  57. {
  58. l.rawValue &= (uint)(int)(object)r;
  59. return l;
  60. }
  61. /// <summary>Xor operator between a EditorPrefBoolFlags and a value</summary>
  62. /// <param name="l">The EditorPrefBoolFlags</param>
  63. /// <param name="r">The value</param>
  64. /// <returns>A EditorPrefBoolFlags with XOR operator performed</returns>
  65. public static EditorPrefBoolFlags<T> operator ^(EditorPrefBoolFlags<T> l, T r)
  66. {
  67. l.rawValue ^= (uint)(int)(object)r;
  68. return l;
  69. }
  70. }
  71. }