ListBoxController.cs
15.9 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
using System;
using System.Collections.Generic;
using AirFishLab.ScrollingList.ContentManagement;
using UnityEngine;
namespace AirFishLab.ScrollingList.ListStateProcessing.Linear
{
public class ListBoxController : IListBoxController
{
#region Public Properties
/// <summary>
/// The state of the list
/// </summary>
public ListFocusingState ListFocusingState { get; private set; } =
ListFocusingState.Middle;
/// <summary>
/// The distance how long is the box away from the focusing position
/// </summary>
public float FocusingDistanceOffset { get; private set; }
#endregion
#region Private Components
/// <summary>
/// The setting of the list
/// </summary>
private ListSetting _setting;
/// <summary>
/// The managed boxes
/// </summary>
private readonly List<IListBox> _boxes = new List<IListBox>();
/// <summary>
/// The container for storing the boxes to be updated
/// </summary>
private readonly List<IListBox> _boxesToBeUpdated = new List<IListBox>();
/// <summary>
/// The component for controlling the box transform
/// </summary>
private BoxTransformController _transformController;
/// <summary>
/// The component for finding the focusing box
/// </summary>
private FocusingBoxFinder _focusingBoxFinder;
/// <summary>
/// The component for getting the list contents
/// </summary>
private ListContentProvider _contentProvider;
/// <summary>
/// The function for updating the focusing box
/// </summary>
private Action _updateFocusingBoxFunc;
/// <summary>
/// The function for recalculating the all box content
/// </summary>
private Action<int> _recalculateAllBoxContentFunc;
#endregion
#region Private Fields
/// <summary>
/// The box which is closest to the center position
/// </summary>
private IListBox _focusingBox;
#endregion
#region Initialize
public void Initialize(ListSetupData setupData)
{
_setting = setupData.ListSetting;
_boxes.Clear();
_boxes.AddRange(setupData.ListBoxes);
_transformController = new BoxTransformController(setupData);
_contentProvider = setupData.ListContentProvider;
_focusingBoxFinder =
new FocusingBoxFinder(
_boxes, _setting,
_transformController.TopBaseline,
_transformController.MiddleBaseline,
_transformController.BottomBaseline);
switch (_setting.FocusingPosition) {
case CircularScrollingList.FocusingPosition.Top:
_updateFocusingBoxFunc = UpdateTopFocusingBox;
_recalculateAllBoxContentFunc = RecalculateAllBoxContentForBothEnds;
break;
case CircularScrollingList.FocusingPosition.Center:
_updateFocusingBoxFunc = UpdateCenterFocusingBox;
_recalculateAllBoxContentFunc = RecalculateAllBoxContentFofMiddle;
break;
case CircularScrollingList.FocusingPosition.Bottom:
_updateFocusingBoxFunc = UpdateBottomFocusingBox;
_recalculateAllBoxContentFunc = RecalculateAllBoxContentForBothEnds;
break;
}
InitializeBoxes(setupData);
}
/// <summary>
/// Initialize the boxes
/// </summary>
private void InitializeBoxes(ListSetupData setupData)
{
var numOfBoxes = _boxes.Count;
for (var boxID = 0; boxID < numOfBoxes; ++boxID) {
var box = _boxes[boxID];
var lastListBox =
_boxes[(int)Mathf.Repeat(boxID - 1, numOfBoxes)];
var nextListBox =
_boxes[(int)Mathf.Repeat(boxID + 1, numOfBoxes)];
box.Initialize(
setupData.ScrollingList,
boxID, lastListBox, nextListBox);
box.OnBoxSelected.AddListener(_setting.OnBoxSelected.Invoke);
_transformController.SetInitialLocalTransform(box, boxID);
var contentID =
_contentProvider.GetInitialContentID(boxID);
UpdateBoxContent(box, contentID);
}
_updateFocusingBoxFunc();
InitializeBoxLayerSorting();
}
/// <summary>
/// Initialize the sorting of the image of the boxes
/// </summary>
private void InitializeBoxLayerSorting()
{
var centeredBoxIndex = 0;
var numOfBoxes = _boxes.Count;
for (; centeredBoxIndex < numOfBoxes; ++centeredBoxIndex)
if (_boxes[centeredBoxIndex] == _focusingBox)
break;
for (var i = centeredBoxIndex - 1; i >= 0; --i)
_boxes[i].PushToBack();
for (var i = centeredBoxIndex + 1; i < numOfBoxes; ++i)
_boxes[i].PushToBack();
}
#endregion
#region Box Updating
public void UpdateBoxes(float movementValue)
{
if (Mathf.Approximately(movementValue, 0f))
return;
var allPositionStatuses = BoxPositionState.Nothing;
foreach (var box in _boxes) {
var positionStatus =
_transformController.UpdateLocalTransform(box, movementValue);
if (positionStatus == BoxPositionState.Nothing)
continue;
_boxesToBeUpdated.Add(box);
// The position statuses of all boxes updated in a single call
// are the same
allPositionStatuses = positionStatus;
}
UpdateBoxesInOrder(_boxesToBeUpdated, allPositionStatuses);
_boxesToBeUpdated.Clear();
_updateFocusingBoxFunc();
}
public void RefreshBoxes(int focusingContentID = -1)
{
var curFocusingContentID = _focusingBox.ContentID;
var numOfContents = _contentProvider.GetContentCount();
if (focusingContentID >= numOfContents)
throw new IndexOutOfRangeException(
$"{nameof(focusingContentID)} is larger than the number of contents");
if (focusingContentID < 0)
focusingContentID =
curFocusingContentID == ListContentProvider.NO_CONTENT_ID
? 0
: Mathf.Min(curFocusingContentID, numOfContents - 1);
_recalculateAllBoxContentFunc(focusingContentID);
}
public IListBox GetFocusingBox() => _focusingBox;
/// <summary>
/// Update the multiple boxes in their position order
/// </summary>
/// <param name="boxes">The boxes to be updated</param>
/// <param name="positionState">The position state of these boxes</param>
private void UpdateBoxesInOrder(
List<IListBox> boxes, BoxPositionState positionState)
{
if (positionState == BoxPositionState.Nothing)
return;
switch (positionState) {
case BoxPositionState.JumpToTop:
// The lower one is updated first
boxes.Sort((a, b) =>
a.GetPositionFactor().CompareTo(b.GetPositionFactor()));
foreach (var box in boxes) {
var contentID =
_contentProvider.GetContentIDByNextBox(
box.NextListBox.ContentID);
box.PushToBack();
UpdateBoxContent(box, contentID);
}
break;
case BoxPositionState.JumpToBottom:
// The higher one is updated first
boxes.Sort((a, b) =>
b.GetPositionFactor().CompareTo(a.GetPositionFactor()));
foreach (var box in boxes) {
var contentID =
_contentProvider.GetContentIDByLastBox(
box.LastListBox.ContentID);
box.PushToBack();
UpdateBoxContent(box, contentID);
}
break;
}
}
#endregion
#region List State
/// <summary>
/// Update the top focusing box
/// </summary>
private void UpdateTopFocusingBox()
{
var result =
_focusingBoxFinder.FindForBothEnds(_contentProvider.GetContentCount());
var topFocusing = result.TopFocusing;
var topBoxIDState =
_contentProvider.GetIDState(topFocusing.Box.ContentID);
var bottomFocusing = result.BottomFocusing;
var bottomBoxIDState =
_contentProvider.GetIDState(bottomFocusing.Box.ContentID);
var distanceOffset =
bottomBoxIDState.HasFlag(ContentIDState.Last)
&& !topBoxIDState.HasFlag(ContentIDState.First)
? bottomFocusing.DistanceOffset
: topFocusing.DistanceOffset;
UpdateFocusingBox(
topFocusing.Box, distanceOffset, result.ListFocusingState);
}
/// <summary>
/// Update the center focusing box
/// </summary>
private void UpdateCenterFocusingBox()
{
var result =
_focusingBoxFinder.FindForMiddle(_contentProvider.GetContentCount());
var (focusingBox, aligningDistance) = result.MiddleFocusing;
UpdateFocusingBox(
focusingBox, aligningDistance, result.ListFocusingState);
}
/// <summary>
/// Update the bottom focusing box
/// </summary>
private void UpdateBottomFocusingBox()
{
var result =
_focusingBoxFinder.FindForBothEnds(_contentProvider.GetContentCount());
var topFocusing = result.TopFocusing;
var topBoxIDState =
_contentProvider.GetIDState(topFocusing.Box.ContentID);
var bottomFocusing = result.BottomFocusing;
var bottomBoxIDState =
_contentProvider.GetIDState(bottomFocusing.Box.ContentID);
var distanceOffset =
topBoxIDState.HasFlag(ContentIDState.Last)
&& !bottomBoxIDState.HasFlag(ContentIDState.First)
? topFocusing.DistanceOffset
: bottomFocusing.DistanceOffset;
UpdateFocusingBox(
bottomFocusing.Box, distanceOffset, result.ListFocusingState);
}
/// <summary>
/// Update the focusing box
/// </summary>
private void UpdateFocusingBox(
IListBox focusingBox, float distanceOffset,
ListFocusingState listFocusingState)
{
ListFocusingState = listFocusingState;
FocusingDistanceOffset = distanceOffset;
if (focusingBox == _focusingBox)
return;
focusingBox.PopToFront();
_setting.OnFocusingBoxChanged.Invoke(
(ListBox)_focusingBox, (ListBox)focusingBox);
_focusingBox = focusingBox;
}
#endregion
#region Content Management
/// <summary>
/// Recalculate all the box contents for the middle focusing position
/// </summary>
/// <param name="newFocusingContentID">The new focusing content ID</param>
private void RecalculateAllBoxContentFofMiddle(int newFocusingContentID)
{
var numOfBoxes = _boxes.Count;
var focusingBoxID = _focusingBox.ListBoxID;
var reverseFactor = _setting.ReverseContentOrder ? -1 : 1;
foreach (var box in _boxes) {
var posFactor = box.GetPositionFactor();
var tempBoxID = box.ListBoxID;
if (tempBoxID > focusingBoxID && posFactor > 0)
tempBoxID -= numOfBoxes;
else if (tempBoxID < focusingBoxID && posFactor < 0)
tempBoxID += numOfBoxes;
var contentID =
newFocusingContentID + (tempBoxID - focusingBoxID) * reverseFactor;
var newContentID = _contentProvider.GetRefreshedContentID(contentID);
UpdateBoxContent(box, newContentID);
}
_updateFocusingBoxFunc();
}
/// <summary>
/// Recalculate all the box contents for both ends
/// </summary>
/// <param name="newFocusingContentID">The new focusing content ID</param>
private void RecalculateAllBoxContentForBothEnds(int newFocusingContentID)
{
// TODO Combine the algorithm with the GetInitialContentID
// in the ContentProvider
var tempBoxList = new List<IListBox>(_boxes);
if (!_setting.ReverseContentOrder)
tempBoxList.Sort((a, b) =>
b.GetPositionFactor().CompareTo(a.GetPositionFactor()));
else
tempBoxList.Sort((a, b) =>
a.GetPositionFactor().CompareTo(b.GetPositionFactor()));
var numOfBoxes = tempBoxList.Count;
var numOfContents = _contentProvider.GetContentCount();
if (_setting.ListType == CircularScrollingList.ListType.Circular)
// No need to do content content id adjusting
newFocusingContentID = newFocusingContentID;
else if (numOfContents <= numOfBoxes)
newFocusingContentID = 0;
else {
var numOfLackingContents =
numOfContents - newFocusingContentID - numOfBoxes;
if (numOfLackingContents < 0)
newFocusingContentID += numOfLackingContents;
}
foreach (var box in tempBoxList) {
var newContentID =
_contentProvider.GetRefreshedContentID(newFocusingContentID);
UpdateBoxContent(box, newContentID);
++newFocusingContentID;
}
_updateFocusingBoxFunc();
}
/// <summary>
/// Update the box content according to the position status
/// </summary>
/// <param name="box">The target box</param>
/// <param name="contentID">The target content ID</param>
private void UpdateBoxContent(IListBox box, int contentID)
{
var idState = _contentProvider.GetIDState(contentID);
ToggleBoxActivation(box, idState);
box.SetContentID(contentID);
if (_contentProvider.TryGetContent(contentID, out var content))
box.SetContent(content);
}
/// <summary>
/// Check if it needs to activate/inactivate the box
/// </summary>
/// <param name="box">The target box</param>
/// <param name="idState">The content id state of the box</param>
private void ToggleBoxActivation(IListBox box, ContentIDState idState)
{
// If there has no content in the content provider,
// just inactivate the box.
if (idState == ContentIDState.NoContent) {
box.IsActivated = false;
return;
}
var isPreviouslyActivated = box.IsActivated;
var isIdValid = idState.HasFlag(ContentIDState.Valid);
if (!isIdValid && isPreviouslyActivated)
box.IsActivated = false;
else if (isIdValid && !isPreviouslyActivated)
box.IsActivated = true;
}
#endregion
}
}