No Description
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.

FlagEnumContentProvider.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.TestTools.TestRunner.GUI.Controls
  4. {
  5. /// <summary>
  6. /// A flag enum content provider to be used with the <see cref="SelectionDropDown" /> control.
  7. /// </summary>
  8. /// <typeparam name="T">The flag enum type.</typeparam>
  9. internal class FlagEnumContentProvider<T> : ISelectionDropDownContentProvider where T : Enum
  10. {
  11. private readonly Action<T> m_ValueChangedCallback;
  12. private readonly T[] m_Values;
  13. internal Func<string, string> DisplayNameGenerator = ObjectNames.NicifyVariableName;
  14. private T m_CurrentValue;
  15. /// <summary>
  16. /// Creates a new instance of the <see cref="FlagEnumContentProvider{T}" /> class.
  17. /// </summary>
  18. /// <param name="initialValue">The initial selection value.</param>
  19. /// <param name="valueChangedCallback">The callback to be invoked on selection change.</param>
  20. /// <exception cref="ArgumentException">
  21. /// Thrown if the generic enum parameter type is not integer based
  22. /// or if the initial selection value is empty.
  23. /// </exception>
  24. /// <exception cref="ArgumentNullException">Thrown if the provided change callback is null.</exception>
  25. public FlagEnumContentProvider(T initialValue, Action<T> valueChangedCallback)
  26. {
  27. if (Enum.GetUnderlyingType(typeof(T)) != typeof(int))
  28. {
  29. throw new ArgumentException("Argument underlying type must be integer.");
  30. }
  31. if ((int)(object)initialValue == 0)
  32. {
  33. throw new ArgumentException("The initial value must not be an empty set.", nameof(initialValue));
  34. }
  35. if (valueChangedCallback == null)
  36. {
  37. throw new ArgumentNullException(nameof(valueChangedCallback), "The value change callback must not be null.");
  38. }
  39. m_CurrentValue = initialValue;
  40. m_Values = (T[])Enum.GetValues(typeof(T));
  41. m_ValueChangedCallback = valueChangedCallback;
  42. }
  43. public int Count => m_Values.Length;
  44. public bool IsMultiSelection => true;
  45. public string GetName(int index)
  46. {
  47. return ValidateIndexBounds(index) ? DisplayNameGenerator(m_Values[index].ToString()) : string.Empty;
  48. }
  49. public int[] SeparatorIndices => new int[0];
  50. public bool IsSelected(int index)
  51. {
  52. return ValidateIndexBounds(index) && IsSet(m_Values[index]);
  53. }
  54. public void SelectItem(int index)
  55. {
  56. if (!ValidateIndexBounds(index))
  57. {
  58. return;
  59. }
  60. if (ChangeValue(m_Values[index]))
  61. {
  62. m_ValueChangedCallback(m_CurrentValue);
  63. }
  64. }
  65. private bool ChangeValue(T flag)
  66. {
  67. var value = flag;
  68. var count = GetSetCount();
  69. if (IsSet(value))
  70. {
  71. if (count == 1)
  72. {
  73. return false;
  74. }
  75. m_CurrentValue = FlagEnumUtility.RemoveFlag(m_CurrentValue, flag);
  76. return true;
  77. }
  78. m_CurrentValue = FlagEnumUtility.SetFlag(m_CurrentValue, flag);
  79. return true;
  80. }
  81. private bool IsSet(T flag)
  82. {
  83. return FlagEnumUtility.HasFlag(m_CurrentValue, flag);
  84. }
  85. private int GetSetCount()
  86. {
  87. return BitUtility.GetCardinality((int)(object)m_CurrentValue);
  88. }
  89. private bool ValidateIndexBounds(int index)
  90. {
  91. if (index < 0 || index >= Count)
  92. {
  93. Debug.LogError($"Requesting item index {index} from a collection of size {Count}");
  94. return false;
  95. }
  96. return true;
  97. }
  98. }
  99. }