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.

MultiValueContentProvider.cs 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace UnityEditor.TestTools.TestRunner.GUI.Controls
  5. {
  6. internal class MultiValueContentProvider<T> : ISelectionDropDownContentProvider where T : IEquatable<T>
  7. {
  8. private T[] m_Values;
  9. private bool[] m_Selected;
  10. private Action<T[]> m_SelectionChangeCallback;
  11. public MultiValueContentProvider(T[] values, T[] selectedValues, Action<T[]> selectionChangeCallback)
  12. {
  13. m_Values = values ?? throw new ArgumentNullException(nameof(values));
  14. if (selectedValues == null)
  15. {
  16. m_Selected = new bool[values.Length];
  17. }
  18. else
  19. {
  20. m_Selected = values.Select(value => selectedValues.Contains(value)).ToArray();
  21. }
  22. m_SelectionChangeCallback = selectionChangeCallback;
  23. }
  24. public int Count
  25. {
  26. get { return m_Values.Length; }
  27. }
  28. public bool IsMultiSelection
  29. {
  30. get { return true; }
  31. }
  32. public int[] SeparatorIndices
  33. {
  34. get { return new int[0]; }
  35. }
  36. public string GetName(int index)
  37. {
  38. if (!ValidateIndexBounds(index))
  39. {
  40. return string.Empty;
  41. }
  42. return m_Values[index].ToString();
  43. }
  44. public void SelectItem(int index)
  45. {
  46. if (!ValidateIndexBounds(index))
  47. {
  48. return;
  49. }
  50. m_Selected[index] = !m_Selected[index];
  51. m_SelectionChangeCallback.Invoke(m_Values.Where((v, i) => m_Selected[i]).ToArray());
  52. }
  53. public bool IsSelected(int index)
  54. {
  55. if (!ValidateIndexBounds(index))
  56. {
  57. return false;
  58. }
  59. return m_Selected[index];
  60. }
  61. private bool ValidateIndexBounds(int index)
  62. {
  63. if (index < 0 || index >= Count)
  64. {
  65. Debug.LogError($"Requesting item index {index} from a collection of size {Count}");
  66. return false;
  67. }
  68. return true;
  69. }
  70. }
  71. }