ListContentProvider.cs
9.13 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
using System;
using UnityEngine;
namespace AirFishLab.ScrollingList.ContentManagement
{
public class ListContentProvider
{
#region Constant Value
/// <summary>
/// The content id that represents there has no content to display
/// </summary>
public const int NO_CONTENT_ID = int.MinValue;
#endregion
#region Private Members
/// <summary>
/// The setting of the list
/// </summary>
private readonly ListSetting _listSetting;
/// <summary>
/// The components holding the list contents
/// </summary>
private readonly IListBank _listBank;
/// <summary>
/// The number of the list boxes
/// </summary>
private readonly int _numOfBoxes;
/// <summary>
/// The factor for getting the next/last content ID
/// </summary>
private readonly int _idFactor;
/// <summary>
/// The function for calculating the final content ID
/// </summary>
private readonly Func<int, int> _idCalculationFunc;
#endregion
/// <summary>
/// Constructor
/// </summary>
/// <param name="listSetting">The setting of the list</param>
/// <param name="listBank">The list content bank</param>
/// <param name="numOfBoxes">The number of boxes</param>
public ListContentProvider(
ListSetting listSetting, IListBank listBank, int numOfBoxes)
{
_listSetting = listSetting;
_listBank = listBank;
_numOfBoxes = numOfBoxes;
_idFactor = _listSetting.ReverseContentOrder ? -1 : 1;
if (_listSetting.ListType == CircularScrollingList.ListType.Circular)
_idCalculationFunc = GetLoopedContentID;
else
_idCalculationFunc = GetNonLoopedContentID;
}
#region Content ID Calculation
/// <summary>
/// Get the initial content ID for the specified box
/// </summary>
/// <param name="listBoxID">The id of the box</param>
/// <returns>The content ID</returns>
public int GetInitialContentID(int listBoxID)
{
var contentCount = GetContentCount();
if (contentCount == 0)
return NO_CONTENT_ID;
var contentID = 0;
var initFocusedContentID = _listSetting.InitFocusingContentID;
switch (_listSetting.FocusingPosition) {
case CircularScrollingList.FocusingPosition.Top:
case CircularScrollingList.FocusingPosition.Bottom:
if (_listSetting.ListType
== CircularScrollingList.ListType.Circular)
// No need to do content adjusting in the circular mode
contentID = 0;
else if (contentCount <= _numOfBoxes)
initFocusedContentID = 0;
else {
var numOfLackingContents =
contentCount - initFocusedContentID - _numOfBoxes;
if (numOfLackingContents < 0)
initFocusedContentID += numOfLackingContents;
}
// The reverse content order will be true,
// if focusing position is 'bottom'.
// Otherwise, it will be false.
contentID =
_listSetting.ReverseContentOrder
? _numOfBoxes - 1 - listBoxID + initFocusedContentID
: listBoxID + initFocusedContentID;
break;
case CircularScrollingList.FocusingPosition.Center:
contentID =
_listSetting.ReverseContentOrder
? _numOfBoxes / 2 - listBoxID
: listBoxID - _numOfBoxes / 2;
contentID += initFocusedContentID;
break;
}
return _idCalculationFunc(contentID);
}
/// <summary>
/// Get the content id after the list content is refreshed
/// </summary>
/// <param name="origContentID">The original content ID</param>
/// <returns>The converted content ID</returns>
public int GetRefreshedContentID(int origContentID) =>
_listBank.GetContentCount() == 0
? NO_CONTENT_ID
: _idCalculationFunc(origContentID);
/// <summary>
/// Get the content ID according to the content ID of the next box
/// </summary>
/// <param name="nextBoxContentID">The content ID of the next box</param>
/// <returns>The content ID</returns>
public int GetContentIDByNextBox(int nextBoxContentID) =>
_idCalculationFunc(nextBoxContentID - _idFactor);
/// <summary>
/// Get the content ID according to the content ID of the last box
/// </summary>
/// <param name="lastBoxContentID">The content ID of the last box</param>
/// <returns>The content ID</returns>
public int GetContentIDByLastBox(int lastBoxContentID) =>
_idCalculationFunc(lastBoxContentID + _idFactor);
/// <summary>
/// Check the state of the specified id
/// </summary>
/// <param name="contentID">The content id</param>
/// <returns>The state of the id</returns>
public ContentIDState GetIDState(int contentID)
{
return GetIDState(contentID, _listBank.GetContentCount());
}
/// <summary>
/// Check the state of the specified id
/// </summary>
/// <param name="contentID">The content id</param>
/// <param name="contentCount">The number of the contents</param>
/// <returns>The state of the id</returns>
public static ContentIDState GetIDState(int contentID, int contentCount)
{
if (contentID == NO_CONTENT_ID)
return ContentIDState.NoContent;
var state = contentID < 0
? ContentIDState.Underflow
: contentID >= contentCount
? ContentIDState.Overflow
: ContentIDState.Valid;
if (state != ContentIDState.Valid)
return state;
if (contentID == 0)
state |= ContentIDState.First;
if (contentID == contentCount - 1)
state |= ContentIDState.Last;
return state;
}
/// <summary>
/// Is the content id valid for getting the list content?
/// </summary>
/// <param name="contentID">The content id</param>
/// <returns>The content id is valid or not</returns>
public bool IsIDValid(int contentID) =>
contentID >= 0 && contentID < _listBank.GetContentCount();
/// <summary>
/// Get the shortest length for starting from one id to another id
/// </summary>
/// <param name="fromContentID">The starting content id</param>
/// <param name="toContentID">The target content id</param>
/// <returns>The shortest length</returns>
public int GetShortestIDDiff(int fromContentID, int toContentID)
{
if (!IsIDValid(fromContentID))
throw new IndexOutOfRangeException(nameof(fromContentID));
if (!IsIDValid(toContentID))
throw new IndexOutOfRangeException(nameof(toContentID));
var length = toContentID - fromContentID;
if (_listSetting.ListType == CircularScrollingList.ListType.Linear)
return length;
var numOfContents = _listBank.GetContentCount();
var halfNumOfContents = numOfContents / 2;
if (Mathf.Abs(length) > halfNumOfContents)
length -= (int) Mathf.Sign(length) * numOfContents;
return length;
}
/// <summary>
/// Loop the input ID within the range of the indexes of the contents
/// </summary>
private int GetLoopedContentID(int contentID) =>
(int)Mathf.Repeat(contentID, _listBank.GetContentCount());
/// <summary>
/// Just return the input ID
/// </summary>
private int GetNonLoopedContentID(int contentID) => contentID;
#endregion
#region Content Handling
/// <summary>
/// Get the number of the contents
/// </summary>
public int GetContentCount() => _listBank.GetContentCount();
/// <summary>
/// Try to get the content of the list
/// </summary>
/// <param name="contentID">The id of the content</param>
/// <param name="content">
/// The content. If the content is not available, it will be null.
/// </param>
/// <returns>It the content available?</returns>
public bool TryGetContent(int contentID, out IListContent content)
{
var isIDValid = IsIDValid(contentID);
content = isIDValid ? _listBank.GetListContent(contentID) : null;
return isIDValid;
}
#endregion
}
}