6
6
7
7
namespace UnityExplorer . UI . Widgets
8
8
{
9
- public class DataViewInfo
9
+ public struct DataViewInfo
10
10
{
11
- public int dataIndex ;
12
- public float height , startPosition ;
13
- public int normalizedSpread ;
11
+ // static
12
+ public static DataViewInfo None => s_default ;
13
+ private static DataViewInfo s_default = default ;
14
14
15
15
public static implicit operator float ( DataViewInfo it ) => it . height ;
16
+
17
+ // instance
18
+ public int dataIndex , normalizedSpread ;
19
+ public float height , startPosition ;
20
+
21
+ public override bool Equals ( object obj )
22
+ {
23
+ var other = ( DataViewInfo ) obj ;
24
+
25
+ return this . dataIndex == other . dataIndex
26
+ && this . height == other . height
27
+ && this . startPosition == other . startPosition
28
+ && this . normalizedSpread == other . normalizedSpread ;
29
+ }
30
+
31
+ public override int GetHashCode ( ) => base . GetHashCode ( ) ;
16
32
}
17
33
18
34
public class DataHeightCache < T > where T : ICell
@@ -53,14 +69,8 @@ public DataViewInfo this[int index]
53
69
/// <summary>Get the first range (division of DefaultHeight) which the position appears in.</summary>
54
70
private int GetRangeFloorOfPosition ( float position ) => ( int ) Math . Floor ( ( decimal ) position / ( decimal ) DefaultHeight ) ;
55
71
56
- /// <summary>Get the data index at the specified position of the total height cache.</summary>
57
- public int GetFirstDataIndexAtPosition ( float desiredHeight ) => GetFirstDataIndexAtPosition ( desiredHeight , out _ ) ;
58
-
59
- /// <summary>Get the data index and DataViewInfo at the specified position of the total height cache.</summary>
60
- public int GetFirstDataIndexAtPosition ( float desiredHeight , out DataViewInfo cache )
72
+ public int GetFirstDataIndexAtPosition ( float desiredHeight )
61
73
{
62
- cache = default ;
63
-
64
74
if ( ! heightCache . Any ( ) )
65
75
return 0 ;
66
76
@@ -72,12 +82,11 @@ public int GetFirstDataIndexAtPosition(float desiredHeight, out DataViewInfo cac
72
82
if ( rangeIndex >= rangeCache . Count )
73
83
{
74
84
int idx = ScrollPool . DataSource . ItemCount - 1 ;
75
- cache = heightCache [ idx ] ;
76
85
return idx ;
77
86
}
78
87
79
88
int dataIndex = rangeCache [ rangeIndex ] ;
80
- cache = heightCache [ dataIndex ] ;
89
+ var cache = heightCache [ dataIndex ] ;
81
90
82
91
// if the DataViewInfo is outdated, need to rebuild
83
92
int expectedMin = GetRangeCeilingOfPosition ( cache . startPosition ) ;
@@ -88,7 +97,7 @@ public int GetFirstDataIndexAtPosition(float desiredHeight, out DataViewInfo cac
88
97
89
98
rangeIndex = GetRangeFloorOfPosition ( desiredHeight ) ;
90
99
dataIndex = rangeCache [ rangeIndex ] ;
91
- cache = heightCache [ dataIndex ] ;
100
+ // cache = heightCache[dataIndex];
92
101
}
93
102
94
103
return dataIndex ;
@@ -141,8 +150,7 @@ public void RemoveLast()
141
150
if ( ! heightCache . Any ( ) )
142
151
return ;
143
152
144
- var val = heightCache [ heightCache . Count - 1 ] ;
145
- totalHeight -= val ;
153
+ totalHeight -= heightCache [ heightCache . Count - 1 ] ;
146
154
heightCache . RemoveAt ( heightCache . Count - 1 ) ;
147
155
148
156
int idx = heightCache . Count ;
@@ -214,6 +222,9 @@ public void SetIndex(int dataIndex, float height)
214
222
215
223
SetSpread ( dataIndex , rangeIndex , spreadDiff ) ;
216
224
}
225
+
226
+ // set the struct back to the array (TODO necessary?)
227
+ heightCache [ dataIndex ] = cache ;
217
228
}
218
229
219
230
private void SetSpread ( int dataIndex , int rangeIndex , int spreadDiff )
@@ -244,12 +255,12 @@ private void RecalculateStartPositions(int toIndex)
244
255
return ;
245
256
246
257
DataViewInfo cache ;
247
- DataViewInfo prev = null ;
258
+ DataViewInfo prev = DataViewInfo . None ;
248
259
for ( int i = 0 ; i <= toIndex && i < heightCache . Count ; i ++ )
249
260
{
250
261
cache = heightCache [ i ] ;
251
262
252
- if ( prev != null )
263
+ if ( prev != DataViewInfo . None )
253
264
cache . startPosition = prev . startPosition + prev . height ;
254
265
else
255
266
cache . startPosition = 0 ;
@@ -262,19 +273,5 @@ private void RecalculateStartPositions(int toIndex)
262
273
prev = cache ;
263
274
}
264
275
}
265
-
266
- //private void HardRebuildRanges()
267
- //{
268
- // var tempList = new List<float>();
269
- // for (int i = 0; i < heightCache.Count; i++)
270
- // tempList.Add(heightCache[i]);
271
- //
272
- // heightCache.Clear();
273
- // rangeCache.Clear();
274
- // totalHeight = 0;
275
- //
276
- // for (int i = 0; i < tempList.Count; i++)
277
- // SetIndex(i, tempList[i]);
278
- //}
279
276
}
280
277
}
0 commit comments