7
7
using UnityEngine . UI ;
8
8
using UnityExplorer . Core . Input ;
9
9
using UnityExplorer . UI . Models ;
10
- using UnityExplorer . UI . ObjectPool ;
11
10
using UnityExplorer . UI . Panels ;
12
11
13
12
namespace UnityExplorer . UI . Widgets
@@ -20,7 +19,7 @@ public struct CellInfo
20
19
/// <summary>
21
20
/// An object-pooled ScrollRect, attempts to support content of any size and provide a scrollbar for it.
22
21
/// </summary>
23
- public class ScrollPool < T > : UIBehaviourModel where T : ICell
22
+ public class ScrollPool < T > : UIBehaviourModel , IEnumerable < CellInfo > where T : ICell
24
23
{
25
24
public ScrollPool ( ScrollRect scrollRect )
26
25
{
@@ -138,9 +137,23 @@ public void Refresh(bool setCellData, bool jumpToTop = false)
138
137
RefreshCells ( setCellData , true ) ;
139
138
}
140
139
141
- // Initialize
140
+ public void JumpToIndex ( int index )
141
+ {
142
+ RefreshCells ( true , true ) ;
143
+
144
+ float normalized = HeightCache [ index ] . startPosition / HeightCache . TotalHeight ;
145
+
146
+ // Slide to the normalized position
147
+ OnSliderValueChanged ( normalized ) ;
148
+ }
149
+
150
+ // IEnumerable
142
151
143
- //private bool Initialized;
152
+ public IEnumerator < CellInfo > GetEnumerator ( ) => EnumerateCellPool ( ) ;
153
+
154
+ IEnumerator IEnumerable . GetEnumerator ( ) => EnumerateCellPool ( ) ;
155
+
156
+ // Initialize
144
157
145
158
/// <summary>Should be called only once, when the scroll pool is created.</summary>
146
159
public void Initialize ( ICellPoolDataSource < T > dataSource , Action onHeightChangedListener = null )
@@ -178,9 +191,8 @@ private IEnumerator InitCoroutine(Action onHeightChangedListener)
178
191
// create initial cell pool and set cells
179
192
CreateCellPool ( ) ;
180
193
181
- var enumerator = GetPoolEnumerator ( ) ;
182
- while ( enumerator . MoveNext ( ) )
183
- SetCell ( CellPool [ enumerator . Current . cellIndex ] , enumerator . Current . dataIndex ) ;
194
+ foreach ( var cell in this )
195
+ SetCell ( CellPool [ cell . cellIndex ] , cell . dataIndex ) ;
184
196
185
197
LayoutRebuilder . ForceRebuildLayoutImmediate ( Content ) ;
186
198
prevContentHeight = Content . rect . height ;
@@ -217,18 +229,18 @@ private bool CheckRecycleViewBounds(bool extendPoolIfGrown)
217
229
218
230
// Cell pool
219
231
220
- private CellInfo _cellInfo = new CellInfo ( ) ;
232
+ private CellInfo _current ;
221
233
222
- public IEnumerator < CellInfo > GetPoolEnumerator ( )
234
+ private IEnumerator < CellInfo > EnumerateCellPool ( )
223
235
{
224
236
int cellIdx = topPoolIndex ;
225
237
int dataIndex = TopDataIndex ;
226
238
int iterated = 0 ;
227
239
while ( iterated < CellPool . Count )
228
240
{
229
- _cellInfo . cellIndex = cellIdx ;
230
- _cellInfo . dataIndex = dataIndex ;
231
- yield return _cellInfo ;
241
+ _current . cellIndex = cellIdx ;
242
+ _current . dataIndex = dataIndex ;
243
+ yield return _current ;
232
244
233
245
cellIdx ++ ;
234
246
if ( cellIdx >= CellPool . Count )
@@ -368,16 +380,13 @@ private void RefreshCells(bool andReloadFromDataSource, bool setSlider)
368
380
CheckDataSourceCountChange ( out bool jumpToBottom ) ;
369
381
370
382
// update date height cache, and set cells if 'andReload'
371
- var enumerator = GetPoolEnumerator ( ) ;
372
- while ( enumerator . MoveNext ( ) )
383
+ foreach ( var cellInfo in this )
373
384
{
374
- var curr = enumerator . Current ;
375
- var cell = CellPool [ curr . cellIndex ] ;
376
-
385
+ var cell = CellPool [ cellInfo . cellIndex ] ;
377
386
if ( andReloadFromDataSource )
378
- SetCell ( cell , curr . dataIndex ) ;
387
+ SetCell ( cell , cellInfo . dataIndex ) ;
379
388
else
380
- HeightCache . SetIndex ( curr . dataIndex , cell . Rect . rect . height ) ;
389
+ HeightCache . SetIndex ( cellInfo . dataIndex , cell . Rect . rect . height ) ;
381
390
}
382
391
383
392
// force check recycles
@@ -405,12 +414,8 @@ private void RefreshCells(bool andReloadFromDataSource, bool setSlider)
405
414
406
415
private void RefreshCellHeightsFast ( )
407
416
{
408
- var enumerator = GetPoolEnumerator ( ) ;
409
- while ( enumerator . MoveNext ( ) )
410
- {
411
- var curr = enumerator . Current ;
412
- HeightCache . SetIndex ( curr . dataIndex , CellPool [ curr . cellIndex ] . Rect . rect . height ) ;
413
- }
417
+ foreach ( var cellInfo in this )
418
+ HeightCache . SetIndex ( cellInfo . dataIndex , CellPool [ cellInfo . cellIndex ] . Rect . rect . height ) ;
414
419
}
415
420
416
421
private void SetCell ( T cachedCell , int dataIndex )
@@ -619,12 +624,10 @@ private void OnSliderValueChanged(float val)
619
624
else
620
625
{
621
626
bottomDataIndex = desiredBottomIndex ;
622
- var enumerator = GetPoolEnumerator ( ) ;
623
- while ( enumerator . MoveNext ( ) )
627
+ foreach ( var info in this )
624
628
{
625
- var curr = enumerator . Current ;
626
- var cell = CellPool [ curr . cellIndex ] ;
627
- SetCell ( cell , curr . dataIndex ) ;
629
+ var cell = CellPool [ info . cellIndex ] ;
630
+ SetCell ( cell , info . dataIndex ) ;
628
631
}
629
632
}
630
633
0 commit comments