ExpandedState.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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>Constructor will create the key to store in the EditorPref the state given generic type passed.</summary>
  12. /// <param name="defaultValue">If key did not exist, it will be created with this value for initialization.</param>
  13. /// <param name="prefix">[Optional] Prefix scope of the key (Default is CoreRP)</param>
  14. public ExpandedState(TState defaultValue, string prefix = "CoreRP")
  15. {
  16. String Key = string.Format("{0}:{1}:UI_State", prefix, typeof(TTarget).Name);
  17. m_State = new EditorPrefBoolFlags<TState>(Key);
  18. //register key if not already there
  19. if (!EditorPrefs.HasKey(Key))
  20. {
  21. EditorPrefs.SetInt(Key, (int)(object)defaultValue);
  22. }
  23. }
  24. /// <summary>Get or set the state given the mask.</summary>
  25. /// <param name="mask">The filtering mask</param>
  26. /// <returns>True: All flagged area are expended</returns>
  27. public bool this[TState mask]
  28. {
  29. get { return m_State.HasFlag(mask); }
  30. set { m_State.SetFlag(mask, value); }
  31. }
  32. /// <summary>Accessor to the expended state of this specific mask.</summary>
  33. /// <param name="mask">The filtering mask</param>
  34. /// <returns>True: All flagged area are expended</returns>
  35. public bool GetExpandedAreas(TState mask)
  36. {
  37. return m_State.HasFlag(mask);
  38. }
  39. /// <summary>Setter to the expended state.</summary>
  40. /// <param name="mask">The filtering mask</param>
  41. /// <param name="value">The expended state to set</param>
  42. public void SetExpandedAreas(TState mask, bool value)
  43. {
  44. m_State.SetFlag(mask, value);
  45. }
  46. /// <summary> Utility to set all states to true </summary>
  47. public void ExpandAll()
  48. {
  49. m_State.rawValue = ~(-1);
  50. }
  51. /// <summary> Utility to set all states to false </summary>
  52. public void CollapseAll()
  53. {
  54. m_State.rawValue = 0;
  55. }
  56. }
  57. }