-
Notifications
You must be signed in to change notification settings - Fork 39
/
ScrollableListBase.cs
267 lines (228 loc) · 8.54 KB
/
ScrollableListBase.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Rage;
using Rage.Native;
using RAGENativeUI.Elements;
using RAGENativeUI.Internals;
namespace RAGENativeUI
{
public interface IScrollableListItem
{
public bool Selected { get; set; }
public bool Skipped { get; set; }
}
public abstract class ScrollableListBase<T> where T : class, IScrollableListItem
{
protected abstract List<T> Items { get; set; }
private int maxItemsOnScreen = 15;
private int currentItem;
protected int MinItem { get; set; }
protected int MaxItem { get; set; }
protected int HoveredItem { get; set; } = -1;
/// <summary>
/// Gets or set the maximum number of visible items.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">If the specified value is less than 1.</exception>
public virtual int MaxItemsOnScreen
{
get => maxItemsOnScreen;
set
{
if (value < 1)
{
throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(MaxItemsOnScreen)} must be at least 1");
}
if (maxItemsOnScreen != value)
{
maxItemsOnScreen = value;
UpdateVisibleItemsIndices();
}
}
}
/// <summary>
/// Refreshes the current item index and min/max visible items, in case they are out of bounds.
/// </summary>
protected virtual void RefreshCurrentSelection()
{
CurrentSelection = GetIndexOfNextSelectableItem((CurrentSelection == -1 ? 0 : CurrentSelection) - 1);
}
/// <summary>
/// Gets or sets the currently selected item index.
/// A value of <c>-1</c> indicates that no selection exists, for example, when no items have been added to the menu.
/// </summary>
public virtual int CurrentSelection
{
get => currentItem;
set
{
if (value != -1 && !IsValidItemIndex(value))
{
throw new ArgumentOutOfRangeException(nameof(value), $"Value must be -1 or in the range [0, {nameof(Items)}.{nameof(Items.Count)})");
}
if (Items.Count == 0)
{
currentItem = -1;
MinItem = -1;
MaxItem = -1;
}
else
{
bool isNewItem = currentItem != value || (IsValidItemIndex(value) && !Items[value].Selected);
if (isNewItem)
{
if (IsValidItemIndex(currentItem))
{
Items[currentItem].Selected = false;
}
currentItem = value;
if (IsValidItemIndex(currentItem))
{
Items[currentItem].Selected = true;
}
}
onSelectionChange(value, isNewItem);
UpdateVisibleItemsIndices();
}
}
}
public T CurrentItem
{
get
{
int index = CurrentSelection;
if (IsValidItemIndex(index))
{
return Items[index];
}
return null;
}
}
protected virtual void onSelectionChange(int newItemIndex, bool isNewItem) { }
/// <summary>
/// Gets whether a selection exists. If the selection exists, <see cref="CurrentSelection"/> returns the index of the selected item; otherwise, <c>-1</c>.
/// </summary>
public bool HasSelection => IsValidItemIndex(CurrentSelection);
/// <summary>
/// Gets whether <paramref name="index"/> is in the range [0, Items.Count).
/// </summary>
protected bool IsValidItemIndex(int index) => index >= 0 && index < Items.Count;
/// <summary>
/// Gets the index of the first visible item.
/// </summary>
public int FirstItemOnScreen => MinItem;
/// <summary>
/// Gets the index of the last visible item.
/// </summary>
public int LastItemOnScreen => MaxItem;
public IEnumerable<(int iterIndex, int itemIndex, T item, bool isItemSelected)> IterateVisibleItems()
{
for (int c = MinItem; c <= MaxItem; c++)
{
yield return (c - MinItem, c, Items[c], c == CurrentSelection);
}
}
/// <summary>
/// Reset the current selected item to 0. Use this after you add or remove items from <see cref="Items"/> directly
/// instead of through <see cref="AddItem(UIMenuItem)"/>, <see cref="AddItem(UIMenuItem, int)"/> or <see cref="RemoveItemAt(int)"/>.
/// </summary>
public virtual void RefreshIndex()
{
if (Items.Count == 0)
{
CurrentSelection = -1;
return;
}
var selection = -1;
for (int i = 0; i < Items.Count; i++)
{
if (selection == -1 && !Items[i].Skipped)
{
selection = i;
}
Items[i].Selected = false;
}
CurrentSelection = selection;
}
/// <summary>
/// Remove all items from the menu.
/// </summary>
public void Clear()
{
Items.Clear();
CurrentSelection = -1;
}
public void MoveToPreviousItem()
{
CurrentSelection = GetIndexOfPreviousSelectableItem(CurrentSelection);
}
public void MoveToNextItem()
{
CurrentSelection = GetIndexOfNextSelectableItem(CurrentSelection);
}
protected int GetIndexOfPreviousSelectableItem(int startIndex)
=> GetIndexOfNextSelectableItem(startIndex, directionStep: -1);
protected int GetIndexOfNextSelectableItem(int startIndex)
=> GetIndexOfNextSelectableItem(startIndex, directionStep: +1);
protected int GetIndexOfNextSelectableItem(int startIndex, int directionStep)
{
int newSelection = startIndex;
int count = 0; // keep count to prevent an infinite loop when all items are skipped
do
{
newSelection = Common.Wrap(newSelection + directionStep, 0, Items.Count);
count++;
} while (count < Items.Count && Items[newSelection].Skipped);
return Items[newSelection].Skipped ? -1 : newSelection;
}
protected void UpdateVisibleItemsIndices()
{
int maxItems = Math.Min(MaxItemsOnScreen, Items.Count);
if (MaxItemsOnScreen >= Items.Count)
{
MinItem = 0;
MaxItem = Items.Count - 1;
return;
}
if (
(currentItem == -1 || MinItem == -1 || MaxItem == -1) // if no selection or no previous selection
|| (MaxItem < MinItem) // if invalid range
|| (MaxItem - MinItem < maxItems - 1) // if not enough items
)
{
MinItem = 0;
MaxItem = maxItems - 1;
}
else if (currentItem < MinItem) // moved selection up, out of current visible item
{
MinItem = currentItem;
MaxItem = currentItem + maxItems - 1;
}
else if (currentItem > MaxItem) // moved selection down, out of current visible item
{
MinItem = currentItem - maxItems + 1;
MaxItem = currentItem;
}
else if (MaxItem - MinItem + 1 != MaxItemsOnScreen) // MaxItemsOnScreen changed
{
if (MaxItem == currentItem)
{
MinItem = MaxItem - maxItems + 1;
}
else
{
MaxItem = MinItem + maxItems - 1;
if (MaxItem >= Items.Count)
{
int diff = MaxItem - Items.Count + 1;
MaxItem -= diff;
MinItem -= diff;
}
}
}
}
}
}