ListSetting.cs
12 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
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;
namespace AirFishLab.ScrollingList
{
[Serializable]
public class ListSetting
{
#region List Mode
[SerializeField]
[Tooltip("The type of the list.")]
private CircularScrollingList.ListType _listType =
CircularScrollingList.ListType.Circular;
[SerializeField]
[Tooltip("The major moving direction of the list.")]
private CircularScrollingList.Direction _direction =
CircularScrollingList.Direction.Vertical;
[SerializeField]
[Tooltip("The controlling mode of the list.")]
private CircularScrollingList.ControlMode _controlMode =
CircularScrollingList.ControlMode.Everything;
[SerializeField]
[Tooltip("The focusing position of the list")]
private CircularScrollingList.FocusingPosition _focusingPosition =
CircularScrollingList.FocusingPosition.Center;
[SerializeField]
[Tooltip("To show the list contents in the reversed order. "
+ "Available when the 'FocusingPosition' is 'center'")]
[FormerlySerializedAs("_reverseOrder")]
private bool _reverseContentOrder;
[SerializeField]
[Tooltip("To align a box at the focusing position after sliding")]
[FormerlySerializedAs("_alignMiddle")]
[FormerlySerializedAs("_alignInCenter")]
private bool _alignAtFocusingPosition;
[SerializeField]
[Tooltip("To reverse the scrolling direction")]
[FormerlySerializedAs("_reverseDirection")]
private bool _reverseScrollingDirection;
[SerializeField]
[Tooltip("Specify the initial content ID at the focusing position")]
[FormerlySerializedAs(" _centeredContentID")]
private int _initFocusingContentID;
[SerializeField]
[Tooltip("Move the selected box to the focusing position")]
[FormerlySerializedAs("_centerSelectedBox")]
private bool _focusSelectedBox;
[SerializeField]
[Tooltip("Whether to initialize the list on Start or not. " +
"If set to false, manually call Initialize() to initialize the list.")]
private bool _initializeOnStart = true;
#endregion
#region List Appearance
[SerializeField]
[Tooltip("The factor that adjusting the distance between boxes. " +
"The larger, the closer.")]
private float _boxDensity = 1.0f;
[SerializeField]
[Tooltip("The curve specifying the passive position of the box. " +
"The x axis is the major position of the box, which is mapped to [-1, 1]. " +
"The y axis defines the factor of the passive position of the box. " +
"Point (0, 0) is the center of the list layout.")]
private AnimationCurve _boxPositionCurve =
AnimationCurve.Constant(-1.0f, 1.0f, 0.0f);
[SerializeField]
[Tooltip("The curve specifying the box scale. " +
"The x axis is the major position of the box, which is mapped to [-1, 1]. " +
"The y axis specifies the value of 'localScale' of the box at the " +
"corresponding position.")]
private AnimationCurve _boxScaleCurve =
AnimationCurve.Constant(-1.0f, 1.0f, 1.0f);
[SerializeField]
[Tooltip("The curve specifying the velocity factor of the box after releasing. " +
"The x axis is the the moving duration in seconds, which starts from 0. " +
"The y axis is the factor of releasing velocity.")]
private AnimationCurve _boxVelocityCurve =
new AnimationCurve(
new Keyframe(0.0f, 1.0f, 0.0f, -2.5f),
new Keyframe(1.0f, 0.0f, 0.0f, 0.0f));
[SerializeField]
[Tooltip("The curve specifying the movement factor of the box. " +
"The x axis is the moving duration in seconds, which starts from 0. " +
"The y axis is the lerping factor for reaching the target position.")]
private AnimationCurve _boxMovementCurve =
new AnimationCurve(
new Keyframe(0.0f, 0.0f, 0.0f, 8f),
new Keyframe(0.25f, 1.0f, 0.0f, 0.0f));
#endregion
#region Events
[SerializeField]
[Tooltip("The callback to be invoked when a box is selected. " +
"The registered callbacks will be added to the 'onClick' event of boxes, " +
"therefore, boxes should be 'Button's.")]
private ListBoxSelectedEvent _onBoxSelected;
[SerializeField]
[Tooltip("The callback to be invoked when the focusing box is changed. " +
"The first argument is previous focusing box, " +
"and the second one is current focusing box.")]
[FormerlySerializedAs("_onCenteredBoxChanged")]
private ListTwoBoxesEvent _onFocusingBoxChanged;
[SerializeField]
[Tooltip("The callback to be invoked when the movement is ended")]
private UnityEvent _onMovementEnd;
#endregion
#region Setting Getter
public CircularScrollingList.ListType ListType => _listType;
public CircularScrollingList.Direction Direction => _direction;
public CircularScrollingList.ControlMode ControlMode => _controlMode;
public bool AlignAtFocusingPosition => _alignAtFocusingPosition;
public bool ReverseScrollingDirection => _reverseScrollingDirection;
public CircularScrollingList.FocusingPosition FocusingPosition =>
_focusingPosition;
public bool ReverseContentOrder => _reverseContentOrder;
public int InitFocusingContentID => _initFocusingContentID;
public bool FocusSelectedBox => _focusSelectedBox;
public bool InitializeOnStart => _initializeOnStart;
public float BoxDensity => _boxDensity;
public AnimationCurve BoxPositionCurve => _boxPositionCurve;
public AnimationCurve BoxScaleCurve => _boxScaleCurve;
public AnimationCurve BoxVelocityCurve => _boxVelocityCurve;
public AnimationCurve BoxMovementCurve => _boxMovementCurve;
public ListBoxSelectedEvent OnBoxSelected => _onBoxSelected;
public ListTwoBoxesEvent OnFocusingBoxChanged => _onFocusingBoxChanged;
public UnityEvent OnMovementEnd => _onMovementEnd;
#endregion
#region Private Members
/// <summary>
/// The name of the list
/// </summary>
private string _name;
/// <summary>
/// Is the setting initialized?
/// </summary>
private bool _isInitialized;
#endregion
#region Property Setters
public void SetListType(CircularScrollingList.ListType listType)
{
if (CheckIsInitialized())
return;
_listType = listType;
}
public void SetDirection(CircularScrollingList.Direction direction)
{
if (CheckIsInitialized())
return;
_direction = direction;
}
public void SetControlMode(CircularScrollingList.ControlMode controlMode)
{
if (CheckIsInitialized())
return;
_controlMode = controlMode;
}
public void SetAlignAtFocusingPosition(bool toAlign)
{
if (CheckIsInitialized())
return;
_alignAtFocusingPosition = toAlign;
}
public void SetReverseScrollingDirection(bool toReverse)
{
if (CheckIsInitialized())
return;
_reverseScrollingDirection = toReverse;
}
public void SetFocusingPosition(
CircularScrollingList.FocusingPosition focusingPosition)
{
if (CheckIsInitialized())
return;
_focusingPosition = focusingPosition;
}
public void SetReverseContentOrder(bool toReverse)
{
if (CheckIsInitialized())
return;
_reverseContentOrder = toReverse;
}
public void SetInitFocusingContentID(int contentID)
{
if (CheckIsInitialized())
return;
_initFocusingContentID = contentID;
}
public void SetFocusSelectedBox(bool toFocus)
{
if (CheckIsInitialized())
return;
_focusSelectedBox = toFocus;
}
#endregion
#region Appearance Setters
public void SetBoxDensity(float boxDensity)
{
if (CheckIsInitialized())
return;
_boxDensity = boxDensity;
}
public void SetBoxPositionCurve(AnimationCurve curve)
{
if (CheckIsInitialized())
return;
_boxPositionCurve = curve;
}
public void SetBoxScaleCurve(AnimationCurve curve)
{
if (CheckIsInitialized())
return;
_boxScaleCurve = curve;
}
public void SetBoxVelocityCurve(AnimationCurve curve)
{
if (CheckIsInitialized())
return;
_boxVelocityCurve = curve;
}
public void SetBoxMovementCurve(AnimationCurve curve)
{
if (CheckIsInitialized())
return;
_boxMovementCurve = curve;
}
#endregion
#region Event Setters
public void AddOnBoxSelectedCallback(UnityAction<ListBox> callback) =>
_onBoxSelected.AddListener(callback);
public void RemoveOnBoxSelectedCallback(UnityAction<ListBox> callback) =>
_onBoxSelected.RemoveListener(callback);
public void AddOnFocusingBoxChangedCallback(
UnityAction<ListBox, ListBox> callback) =>
_onFocusingBoxChanged.AddListener(callback);
public void RemoveOnFocusingBoxChangedCallback(
UnityAction<ListBox, ListBox> callback) =>
_onFocusingBoxChanged.RemoveListener(callback);
public void AddOnMovementEndCallback(UnityAction callback) =>
_onMovementEnd.AddListener(callback);
public void RemoveOnMovementEndCallback(UnityAction callback) =>
_onMovementEnd.RemoveListener(callback);
#endregion
#region Public Functions
/// <summary>
/// Is the list initialized?
/// </summary>
private bool CheckIsInitialized()
{
if (_isInitialized)
Debug.LogWarning(
$"The list setting of the list '{_name}' is initialized. Skip");
return _isInitialized;
}
/// <summary>
/// Initialize the settings
/// </summary>
/// <param name="listBank">The list bank</param>
/// <param name="name">The name of the list</param>
public void Initialize(BaseListBank listBank, string name)
{
var contentCount = listBank.GetContentCount();
if (_initFocusingContentID < 0
|| (contentCount > 0 && _initFocusingContentID >= contentCount))
throw new IndexOutOfRangeException(
"The 'InitFocusingContentID' is negative "
+ "or greater than the number of contents in the list bank "
+ $"in the list '{name}'.");
if (Mathf.Approximately(_boxDensity, 0f))
throw new InvalidOperationException(
$"The 'BoxDensity' shouldn't be 0 in the list '{name}'");
switch (_focusingPosition) {
case CircularScrollingList.FocusingPosition.Top:
_reverseContentOrder = false;
break;
case CircularScrollingList.FocusingPosition.Bottom:
_reverseContentOrder = true;
break;
}
_name = name;
_isInitialized = true;
}
#endregion
}
}