ExpandedState.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. namespace UnityEditor.Rendering
  3. {
  4. /// <summary>Used in editor drawer part to store the state of expendable areas.</summary>
  5. /// <typeparam name="TState">An enum to use to describe the state.</typeparam>
  6. /// <typeparam name="TTarget">A type given to automatically compute the key.</typeparam>
  7. public struct ExpandedState<TState, TTarget>
  8. where TState : struct, IConvertible
  9. {
  10. EditorPrefBoolFlags<TState> m_State;
  11. /// <summary>
  12. /// Constructor will create the key to store in the EditorPref the state given generic type passed.
  13. /// The key will be formated as such prefix:TTarget:TState:UI_State.
  14. /// </summary>
  15. /// <param name="defaultValue">If key did not exist, it will be created with this value for initialization.</param>
  16. /// <param name="prefix">[Optional] Prefix scope of the key (Default is CoreRP)</param>
  17. public ExpandedState(TState defaultValue, string prefix = "CoreRP")
  18. {
  19. string key = $"{prefix}:{typeof(TTarget).Name}:{typeof(TState).Name}:UI_State";
  20. m_State = new EditorPrefBoolFlags<TState>(key);
  21. //register key if not already there
  22. if (!EditorPrefs.HasKey(key))
  23. {
  24. EditorPrefs.SetInt(key, (int)(object)defaultValue);
  25. }
  26. }
  27. /// <summary>Get or set the state given the mask.</summary>
  28. /// <param name="mask">The filtering mask</param>
  29. /// <returns>True: All flagged area are expended</returns>
  30. public bool this[TState mask]
  31. {
  32. get { return m_State.HasFlag(mask); }
  33. set { m_State.SetFlag(mask, value); }
  34. }
  35. /// <summary>Accessor to the expended state of this specific mask.</summary>
  36. /// <param name="mask">The filtering mask</param>
  37. /// <returns>True: All flagged area are expended</returns>
  38. public bool GetExpandedAreas(TState mask)
  39. {
  40. return m_State.HasFlag(mask);
  41. }
  42. /// <summary>Setter to the expended state.</summary>
  43. /// <param name="mask">The filtering mask</param>
  44. /// <param name="value">The expended state to set</param>
  45. public void SetExpandedAreas(TState mask, bool value)
  46. {
  47. m_State.SetFlag(mask, value);
  48. }
  49. /// <summary> Utility to set all states to true </summary>
  50. public void ExpandAll()
  51. {
  52. m_State.rawValue = ~(-1);
  53. }
  54. /// <summary> Utility to set all states to false </summary>
  55. public void CollapseAll()
  56. {
  57. m_State.rawValue = 0;
  58. }
  59. }
  60. }