-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ItemSelector.cs
197 lines (169 loc) 路 5.52 KB
/
ItemSelector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
锘縰sing System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine;
namespace AdrianMiasik.Components.Base
{
/// <summary>
/// A **generic** class that keeps track of our current (and previous) generic item selection in a collection.
/// (e.g. radio button, selecting one element out of a list).
/// </summary>
/// <typeparam name="T"></typeparam>
public class ItemSelector<T> : ThemeElement
{
[SerializeField] protected Collection<T> m_items = new Collection<T>();
private T currentItem;
private int currentIndex;
private T lastSelectedItem;
/// <summary>
/// Invoked when the selection changes
/// </summary>
/// <param name="previousSelection"></param>
/// <param name="currentSelection"></param>
public delegate void OnSelectionChange(T previousSelection, T currentSelection);
public OnSelectionChange onSelectionChange;
/// <summary>
/// Invoked when an item gets selected
/// </summary>
/// <param name="selectedItem"></param>
public delegate void OnSelected(T selectedItem);
public OnSelected onSelected;
/// <summary>
/// Invoked when an item gets deselected
/// </summary>
/// <param name="deselectedItem"></param>
public delegate void OnDeselected(T deselectedItem);
public OnDeselected onDeselected;
/// <summary>
/// Initializes with a specific set of objects
/// </summary>
/// <param name="allSelectionItems"></param>
protected virtual void Initialize(IEnumerable<T> allSelectionItems)
{
foreach (T item in allSelectionItems)
{
m_items.Add(item);
}
Initialize();
}
/// <summary>
/// Initializes with the serialized list
/// </summary>
private void Initialize()
{
currentIndex = 0;
currentItem = m_items[currentIndex];
}
public void Clear()
{
m_items.Clear();
currentItem = default;
}
public void AddItem(T item)
{
m_items.Add(item);
}
public void RemoveItem(T item)
{
m_items.Remove(item);
}
/// <summary>
/// Selects the previous item in the collection
/// </summary>
public void PreviousItem()
{
ChangeIndex(-1);
Select(currentIndex);
}
/// <summary>
/// Selects the next item in the collection
/// </summary>
public void NextItem()
{
ChangeIndex(1);
Select(currentIndex);
}
/// <summary>
/// Increment/decrement our current index while staying within the bounds of our list by wrapping.
/// </summary>
/// <param name="difference"></param>
private void ChangeIndex(int difference)
{
currentIndex += difference;
currentIndex = (currentIndex + m_items.Count) % m_items.Count;
}
/// <summary>
/// If your item is within our list, we will set the current item to it.
/// </summary>
/// <param name="itemInList"></param>
public bool Select(T itemInList)
{
if (!m_items.Contains(itemInList))
{
Debug.LogWarning("Item not set! Unable to find the provided item.");
return false;
}
ChangeSelection(itemInList);
onSelectionChange?.Invoke(lastSelectedItem, currentItem);
return true;
}
/// <summary>
/// If your index is valid, we will set the current item to the object at the provided index and return true.
/// Otherwise we will only return false. (No current item change)
/// </summary>
/// <param name="index"></param>
private bool Select(int index)
{
if (!IsIndexValid(index))
{
Debug.LogWarning("Item not set! Invalid index.");
return false;
}
ChangeSelection(m_items[index]);
return true;
}
/// <summary>
/// Returns true if the index is within the bounds of the elements list
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
private bool IsIndexValid(int index)
{
return index >= 0 && index < m_items.Count;
}
private void ChangeSelection(T itemInList)
{
// Deselect
lastSelectedItem = currentItem;
onDeselected?.Invoke(currentItem);
// Swap
currentItem = itemInList;
currentIndex = m_items.IndexOf(itemInList);
// Select
onSelected?.Invoke(currentItem);
}
/// <summary>
/// Returns the current visible item
/// </summary>
/// <returns></returns>
public T GetCurrentItem()
{
return currentItem;
}
public int GetCurrentIndex()
{
return currentIndex;
}
public int GetCount()
{
return m_items.Count;
}
public T GetLastSelectedItem()
{
return lastSelectedItem;
}
public Collection<T> GetItems()
{
return m_items;
}
}
}