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