Make DataViewInfo a struct instead of class

This commit is contained in:
Sinai 2021-05-25 15:46:30 +10:00
parent 4f0553d293
commit a54888ae3a

View File

@ -6,13 +6,29 @@ using UnityEngine;
namespace UnityExplorer.UI.Widgets namespace UnityExplorer.UI.Widgets
{ {
public class DataViewInfo public struct DataViewInfo
{ {
public int dataIndex; // static
public float height, startPosition; public static DataViewInfo None => s_default;
public int normalizedSpread; private static DataViewInfo s_default = default;
public static implicit operator float(DataViewInfo it) => it.height; public static implicit operator float(DataViewInfo it) => it.height;
// instance
public int dataIndex, normalizedSpread;
public float height, startPosition;
public override bool Equals(object obj)
{
var other = (DataViewInfo)obj;
return this.dataIndex == other.dataIndex
&& this.height == other.height
&& this.startPosition == other.startPosition
&& this.normalizedSpread == other.normalizedSpread;
}
public override int GetHashCode() => base.GetHashCode();
} }
public class DataHeightCache<T> where T : ICell public class DataHeightCache<T> where T : ICell
@ -53,14 +69,8 @@ namespace UnityExplorer.UI.Widgets
/// <summary>Get the first range (division of DefaultHeight) which the position appears in.</summary> /// <summary>Get the first range (division of DefaultHeight) which the position appears in.</summary>
private int GetRangeFloorOfPosition(float position) => (int)Math.Floor((decimal)position / (decimal)DefaultHeight); private int GetRangeFloorOfPosition(float position) => (int)Math.Floor((decimal)position / (decimal)DefaultHeight);
/// <summary>Get the data index at the specified position of the total height cache.</summary> public int GetFirstDataIndexAtPosition(float desiredHeight)
public int GetFirstDataIndexAtPosition(float desiredHeight) => GetFirstDataIndexAtPosition(desiredHeight, out _);
/// <summary>Get the data index and DataViewInfo at the specified position of the total height cache.</summary>
public int GetFirstDataIndexAtPosition(float desiredHeight, out DataViewInfo cache)
{ {
cache = default;
if (!heightCache.Any()) if (!heightCache.Any())
return 0; return 0;
@ -72,12 +82,11 @@ namespace UnityExplorer.UI.Widgets
if (rangeIndex >= rangeCache.Count) if (rangeIndex >= rangeCache.Count)
{ {
int idx = ScrollPool.DataSource.ItemCount - 1; int idx = ScrollPool.DataSource.ItemCount - 1;
cache = heightCache[idx];
return idx; return idx;
} }
int dataIndex = rangeCache[rangeIndex]; int dataIndex = rangeCache[rangeIndex];
cache = heightCache[dataIndex]; var cache = heightCache[dataIndex];
// if the DataViewInfo is outdated, need to rebuild // if the DataViewInfo is outdated, need to rebuild
int expectedMin = GetRangeCeilingOfPosition(cache.startPosition); int expectedMin = GetRangeCeilingOfPosition(cache.startPosition);
@ -88,7 +97,7 @@ namespace UnityExplorer.UI.Widgets
rangeIndex = GetRangeFloorOfPosition(desiredHeight); rangeIndex = GetRangeFloorOfPosition(desiredHeight);
dataIndex = rangeCache[rangeIndex]; dataIndex = rangeCache[rangeIndex];
cache = heightCache[dataIndex]; //cache = heightCache[dataIndex];
} }
return dataIndex; return dataIndex;
@ -141,8 +150,7 @@ namespace UnityExplorer.UI.Widgets
if (!heightCache.Any()) if (!heightCache.Any())
return; return;
var val = heightCache[heightCache.Count - 1]; totalHeight -= heightCache[heightCache.Count - 1];
totalHeight -= val;
heightCache.RemoveAt(heightCache.Count - 1); heightCache.RemoveAt(heightCache.Count - 1);
int idx = heightCache.Count; int idx = heightCache.Count;
@ -214,6 +222,9 @@ namespace UnityExplorer.UI.Widgets
SetSpread(dataIndex, rangeIndex, spreadDiff); SetSpread(dataIndex, rangeIndex, spreadDiff);
} }
// set the struct back to the array (TODO necessary?)
heightCache[dataIndex] = cache;
} }
private void SetSpread(int dataIndex, int rangeIndex, int spreadDiff) private void SetSpread(int dataIndex, int rangeIndex, int spreadDiff)
@ -244,12 +255,12 @@ namespace UnityExplorer.UI.Widgets
return; return;
DataViewInfo cache; DataViewInfo cache;
DataViewInfo prev = null; DataViewInfo prev = DataViewInfo.None;
for (int i = 0; i <= toIndex && i < heightCache.Count; i++) for (int i = 0; i <= toIndex && i < heightCache.Count; i++)
{ {
cache = heightCache[i]; cache = heightCache[i];
if (prev != null) if (prev != DataViewInfo.None)
cache.startPosition = prev.startPosition + prev.height; cache.startPosition = prev.startPosition + prev.height;
else else
cache.startPosition = 0; cache.startPosition = 0;
@ -262,19 +273,5 @@ namespace UnityExplorer.UI.Widgets
prev = cache; prev = cache;
} }
} }
//private void HardRebuildRanges()
//{
// var tempList = new List<float>();
// for (int i = 0; i < heightCache.Count; i++)
// tempList.Add(heightCache[i]);
//
// heightCache.Clear();
// rangeCache.Clear();
// totalHeight = 0;
//
// for (int i = 0; i < tempList.Count; i++)
// SetIndex(i, tempList[i]);
//}
} }
} }