Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExpandedState.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. namespace UnityEditor.Rendering
  3. {
  4. /// <summary>Used in editor drawer part to store the state of expandable areas.</summary>
  5. /// <typeparam name="TState">An enum to use to describe the state.</typeparam>
  6. public abstract class ExpandedStateBase<TState>
  7. where TState : struct, IConvertible
  8. {
  9. /// <summary>Accessor to the expended state of this specific mask.</summary>
  10. /// <param name="mask">The filtering mask</param>
  11. /// <returns>True: All flagged area are expended</returns>
  12. public abstract bool GetExpandedAreas(TState mask);
  13. /// <summary>Setter to the expended state.</summary>
  14. /// <param name="mask">The filtering mask</param>
  15. /// <param name="value">The expended state to set</param>
  16. public abstract void SetExpandedAreas(TState mask, bool value);
  17. /// <summary> Utility to set all states to true </summary>
  18. public abstract void ExpandAll();
  19. /// <summary> Utility to set all states to false </summary>
  20. public abstract void CollapseAll();
  21. /// <summary>Get or set the state given the mask.</summary>
  22. /// <param name="mask">The filtering mask</param>
  23. /// <value>True: All flagged area are expended</value>
  24. public bool this[TState mask]
  25. {
  26. get => GetExpandedAreas(mask);
  27. set => SetExpandedAreas(mask, value);
  28. }
  29. }
  30. /// <summary>Used in editor drawer part to store the state of expandable areas using EditorPrefBoolFlags.</summary>
  31. /// <typeparam name="TState">An enum to use to describe the state.</typeparam>
  32. /// <typeparam name="TTarget">A type given to automatically compute the key.</typeparam>
  33. public class ExpandedState<TState, TTarget> : ExpandedStateBase<TState>
  34. where TState : struct, IConvertible
  35. {
  36. /// <summary>
  37. /// The variable which stores the state of expandable areas.
  38. /// </summary>
  39. protected internal EditorPrefBoolFlags<TState> m_State;
  40. /// <summary>
  41. /// Constructor will create the key to store in the EditorPref the state given generic type passed.
  42. /// The key will be formated as such prefix:TTarget:TState:UI_State.
  43. /// </summary>
  44. /// <param name="defaultValue">If key did not exist, it will be created with this value for initialization.</param>
  45. /// <param name="prefix">[Optional] Prefix scope of the key (Default is CoreRP)</param>
  46. /// <param name="stateId">[Optional] Postfix used to differentiate between different keys (Default is UI_State)</param>
  47. public ExpandedState(TState defaultValue, string prefix = "CoreRP", string stateId = "UI_State")
  48. {
  49. string key = $"{prefix}:{typeof(TTarget).Name}:{typeof(TState).Name}:{stateId}";
  50. m_State = new EditorPrefBoolFlags<TState>(key);
  51. //register key if not already there
  52. if (!EditorPrefs.HasKey(key))
  53. {
  54. EditorPrefs.SetInt(key, (int)(object)defaultValue);
  55. }
  56. }
  57. /// <inheritdoc/>
  58. public override bool GetExpandedAreas(TState mask)
  59. {
  60. return m_State.HasFlag(mask);
  61. }
  62. /// <inheritdoc/>
  63. public override void SetExpandedAreas(TState mask, bool value)
  64. {
  65. m_State.SetFlag(mask, value);
  66. }
  67. /// <inheritdoc/>
  68. public override void ExpandAll()
  69. {
  70. m_State.rawValue = uint.MaxValue;
  71. }
  72. /// <inheritdoc/>
  73. public override void CollapseAll()
  74. {
  75. m_State.rawValue = 0u;
  76. }
  77. }
  78. /// <summary>Used in editor drawer part to store the state of expandable areas using EditorPrefBoolFlags for a list of elements.</summary>
  79. /// <typeparam name="TTarget">A type given to automatically compute the key.</typeparam>
  80. public class ExpandedStateList<TTarget> : ExpandedState<int, TTarget>
  81. {
  82. /// <summary>
  83. /// Constructor will create the key to store in the EditorPref the state given generic type passed.
  84. /// The key will be formated as such prefix:TTarget:TState:UI_State.
  85. /// </summary>
  86. /// <param name="prefix">[Optional] Prefix scope of the key (Default is CoreRP)</param>
  87. public ExpandedStateList(string prefix = "CoreRP")
  88. : base(default(int), prefix, "UI_State_List") { }
  89. /// <summary>
  90. /// Swap flag between src index and dst index.
  91. /// </summary>
  92. /// <param name="srcIndex">src index to swap.</param>
  93. /// <param name="dstIndex">dst index to swap.</param>
  94. public void SwapFlags(int srcIndex, int dstIndex)
  95. {
  96. int srcFlag = 1 << srcIndex;
  97. int dstFlag = 1 << dstIndex;
  98. bool srcVal = GetExpandedAreas(srcFlag);
  99. SetExpandedAreas(srcFlag, GetExpandedAreas(dstFlag));
  100. SetExpandedAreas(dstFlag, srcVal);
  101. }
  102. /// <summary> Removes a flag at a given index which causes the following flags' index to decrease by one.</summary>
  103. /// <param name="index">The index of the flag to be removed.</param>
  104. public void RemoveFlagAtIndex(int index)
  105. {
  106. m_State.rawValue = RightShiftOnceFromIndexToMSB(index, m_State.rawValue);
  107. }
  108. /// <summary> Utility to logical right shift every bit from the index flag to MSB resulting in the index flag being shifted out.<\summary>
  109. /// <param name="index">Right shift every bit greater or equal to the index.</param>
  110. /// <param name="value">Value to make the operations on.</param>
  111. /// <returns>The right shift value after the given index.</returns>
  112. internal static uint RightShiftOnceFromIndexToMSB(int index, uint value)
  113. { // Example of each operation:
  114. // 1011 1001 - Value
  115. uint indexBit = 1u << index; // 0000 1000 - Index bit
  116. uint remainArea = indexBit - 1u; // 0000 0111
  117. uint remainBits = remainArea & value; // 0000 0001
  118. uint movedBits = (~remainArea - indexBit & value) >> 1; // 1111 1000
  119. // 1111 0000
  120. // 1011 0000
  121. // 0101 1000
  122. return movedBits | remainBits; // 0101 1001 - Result
  123. }
  124. }
  125. }