Added 'temp height cache' to ScrollPool for when filtering, fix some issues

This commit is contained in:
Sinai 2021-04-22 17:53:29 +10:00
parent fc26452f64
commit fdfadcefc1
10 changed files with 251 additions and 125 deletions

View File

@ -7,6 +7,7 @@ using UnityExplorer.Core.Config;
using UnityExplorer.Core.Input;
using UnityExplorer.Core.Runtime;
using UnityExplorer.UI;
using UnityExplorer.UI.Panels;
namespace UnityExplorer
{

View File

@ -34,28 +34,6 @@ namespace UnityExplorer.UI.Panels
ConfigManager.GameObjectInspectorData.Value = this.ToSaveData();
}
public override void OnFinishResize(RectTransform panel)
{
base.OnFinishResize(panel);
RuntimeProvider.Instance.StartCoroutine(DelayedRefresh(panel));
}
private float previousRectHeight;
private IEnumerator DelayedRefresh(RectTransform obj)
{
yield return null;
if (obj.rect.height != previousRectHeight)
{
// height changed, hard refresh required.
previousRectHeight = obj.rect.height;
//scrollPool.ReloadData();
}
scrollPool.RefreshCells(true);
}
public override void SetDefaultPosAndAnchors()
{
mainPanelRect.localPosition = Vector2.zero;
@ -68,6 +46,8 @@ namespace UnityExplorer.UI.Panels
mainPanelRect.anchoredPosition = new Vector2(-150, 0);
}
internal static DynamicListTest listInstance;
private ScrollPool scrollPool;
public override void ConstructPanelContent()
@ -83,32 +63,33 @@ namespace UnityExplorer.UI.Panels
//scrollPool.Viewport.GetComponent<Mask>().enabled = false;
//scrollPool.Content.gameObject.AddComponent<Image>().color = new Color(1f, 0f, 1f, 0.3f);
var test = new DynamicListTest(scrollPool, this);
test.Init();
listInstance = new DynamicListTest(scrollPool, this);
listInstance.Init();
//var prototype = DynamicCell.CreatePrototypeCell(scrollContent);
//scrollPool.PrototypeCell = prototype.GetComponent<RectTransform>();
dummyContentHolder = new GameObject("DummyHolder");
dummyContentHolder.SetActive(false);
contentHolder = new GameObject("DummyHolder");
contentHolder.SetActive(false);
contentHolder.transform.SetParent(this.content.transform, false);
GameObject.DontDestroyOnLoad(dummyContentHolder);
GameObject.DontDestroyOnLoad(contentHolder);
ExplorerCore.Log("Creating dummy objects");
for (int i = 0; i < 100; i++)
for (int i = 0; i < 10; i++)
{
dummyContents.Add(CreateDummyContent());
}
ExplorerCore.Log("Done");
previousRectHeight = mainPanelRect.rect.height;
//previousRectHeight = mainPanelRect.rect.height;
}
internal GameObject dummyContentHolder;
internal GameObject contentHolder;
internal readonly List<GameObject> dummyContents = new List<GameObject>();
private GameObject CreateDummyContent()
{
var obj = UIFactory.CreateVerticalGroup(dummyContentHolder, "Content", true, true, true, true, 2, new Vector4(2, 2, 2, 2));
var obj = UIFactory.CreateVerticalGroup(contentHolder, "Content", true, true, true, true, 2, new Vector4(2, 2, 2, 2));
obj.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
var horiGroup = UIFactory.CreateHorizontalGroup(obj, "topGroup", true, true, true, true);
@ -157,29 +138,76 @@ namespace UnityExplorer.UI.Panels
public class DynamicListTest : IPoolDataSource
{
internal ScrollPool Scroller;
internal ScrollPool ScrollPool;
internal InspectorTest Inspector;
public DynamicListTest(ScrollPool scroller, InspectorTest inspector)
{
Scroller = scroller;
ScrollPool = scroller;
Inspector = inspector;
}
public int ItemCount => Inspector.dummyContents.Count;
public int ItemCount => filtering ? filteredIndices.Count : Inspector.dummyContents.Count;
private bool filtering;
private readonly List<int> filteredIndices = new List<int>();
public int GetRealIndexOfTempIndex(int index)
{
if (index < 0 || index >= filteredIndices.Count)
return -1;
return filteredIndices[index];
}
public void ToggleFilter()
{
if (filtering)
{
DisableFilter();
ScrollPool.DisableTempCache();
}
else
{
EnableRandomFilter();
ScrollPool.EnableTempCache();
}
ExplorerCore.Log("Filter toggled, new count: " + ItemCount);
ScrollPool.Rebuild();
}
public void EnableRandomFilter()
{
filteredIndices.Clear();
filtering = true;
int counter = UnityEngine.Random.Range(0, Inspector.dummyContents.Count);
while (filteredIndices.Count < counter)
{
var i = UnityEngine.Random.Range(0, Inspector.dummyContents.Count);
if (!filteredIndices.Contains(i))
filteredIndices.Add(i);
}
filteredIndices.Sort();
}
public void DisableFilter()
{
filtering = false;
}
public void OnDisableCell(CellViewHolder cell, int dataIndex)
{
if (cell.UIRoot.transform.Find("Content") is Transform existing)
existing.transform.SetParent(Inspector.dummyContentHolder.transform, false);
existing.transform.SetParent(Inspector.contentHolder.transform, false);
}
public void Init()
{
var prototype = CellViewHolder.CreatePrototypeCell(Scroller.UIRoot);
var prototype = CellViewHolder.CreatePrototypeCell(ScrollPool.UIRoot);
Scroller.DataSource = this;
Scroller.Initialize(this, prototype);
ScrollPool.DataSource = this;
ScrollPool.Initialize(this, prototype);
}
public ICell CreateCell(RectTransform cellTransform) => new CellViewHolder(cellTransform.gameObject);
@ -202,6 +230,9 @@ namespace UnityExplorer.UI.Panels
return;
}
if (filtering)
index = GetRealIndexOfTempIndex(index);
var content = Inspector.dummyContents[index];
if (content.transform.parent.ReferenceEqual(root.transform))
@ -215,7 +246,7 @@ namespace UnityExplorer.UI.Panels
private void DisableContent(GameObject cellRoot)
{
if (cellRoot.transform.Find("Content") is Transform existing)
existing.transform.SetParent(Inspector.dummyContentHolder.transform, false);
existing.transform.SetParent(Inspector.contentHolder.transform, false);
}
}
}

View File

@ -125,7 +125,7 @@ namespace UnityExplorer.UI.Panels
private void OnFilterInput(string input)
{
Tree.CurrentFilter = input;
Tree.RefreshData(true);
Tree.RefreshData(true, true);
}
//private float highestRectHeight;
@ -220,14 +220,17 @@ namespace UnityExplorer.UI.Panels
// Transform Tree
var infiniteScroll = UIFactory.CreateScrollPool(content, "TransformTree", out GameObject scrollObj,
var scrollPool = UIFactory.CreateScrollPool(content, "TransformTree", out GameObject scrollObj,
out GameObject scrollContent, new Color(0.15f, 0.15f, 0.15f));
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
UIFactory.SetLayoutElement(scrollContent, flexibleHeight: 9999);
Tree = new TransformTree(infiniteScroll) { GetRootEntriesMethod = GetRootEntries };
Tree = new TransformTree(scrollPool) { GetRootEntriesMethod = GetRootEntries };
Tree.Init();
//scrollPool.Viewport.GetComponent<Mask>().enabled = false;
//UIRoot.GetComponent<Mask>().enabled = false;
// Scene Loader
ConstructSceneLoader();

View File

@ -22,7 +22,7 @@ namespace UnityExplorer.UI
// panels
public static SceneExplorer SceneExplorer { get; private set; }
public static InspectorTest GOInspector { get; private set; }
public static InspectorTest Inspector { get; private set; }
// bundle assets
internal static Font ConsoleFont { get; private set; }
@ -54,8 +54,8 @@ namespace UnityExplorer.UI
SceneExplorer = new SceneExplorer();
SceneExplorer.ConstructUI(CanvasRoot);
GOInspector = new InspectorTest();
GOInspector.ConstructUI(CanvasRoot);
Inspector = new InspectorTest();
Inspector.ConstructUI(CanvasRoot);
//MainMenu.Create();
//InspectUnderMouse.ConstructUI();

View File

@ -11,6 +11,8 @@ namespace UnityExplorer.UI.Widgets
{
public class ButtonListSource<T> : IPoolDataSource
{
public int GetRealIndexOfTempIndex(int index) => throw new NotImplementedException("TODO");
internal ScrollPool Scroller;
public int ItemCount => currentEntries.Count;

View File

@ -11,18 +11,27 @@ namespace UnityExplorer.UI.Widgets
public float height, startPosition;
public int normalizedSpread;
public static implicit operator float(DataViewInfo ch) => ch.height;
public static implicit operator float(DataViewInfo it) => it.height;
}
public class DataHeightManager
public class DataHeightCache
{
private ScrollPool ScrollPool { get; }
private DataHeightCache sisterCache { get; }
public DataHeightManager(ScrollPool scrollPool)
public DataHeightCache(ScrollPool scrollPool)
{
ScrollPool = scrollPool;
}
public DataHeightCache(ScrollPool scrollPool, DataHeightCache sisterCache) : this(scrollPool)
{
this.sisterCache = sisterCache;
ExplorerCore.Log("Creating backup height cache, this count: " + scrollPool.DataSource.ItemCount);
AddRange(sisterCache.Take(scrollPool.DataSource.ItemCount));
}
private readonly List<DataViewInfo> heightCache = new List<DataViewInfo>();
public int Count => heightCache.Count;
@ -62,6 +71,17 @@ namespace UnityExplorer.UI.Widgets
totalHeight += value;
}
public void AddRange(IEnumerable<DataViewInfo> collection)
{
foreach (var entry in collection)
Add(entry);
}
public IEnumerable<DataViewInfo> Take(int count)
{
return heightCache.Take(count);
}
public void RemoveLast()
{
if (!heightCache.Any())
@ -70,13 +90,6 @@ namespace UnityExplorer.UI.Widgets
var val = heightCache[heightCache.Count - 1];
totalHeight -= val;
heightCache.RemoveAt(heightCache.Count - 1);
}
public void Clear()
{
heightCache.Clear();
totalHeight = 0f;
}
private void AppendDataSpread(int dataIdx, int spread)
@ -88,10 +101,17 @@ namespace UnityExplorer.UI.Widgets
}
}
public void SetIndex(int dataIndex, float value)
public void SetIndex(int dataIndex, float value, bool ignoreDataCount = false)
{
if (dataIndex >= ScrollPool.DataSource.ItemCount)
return;
if (!ignoreDataCount)
{
if (dataIndex >= ScrollPool.DataSource.ItemCount)
{
while (heightCache.Count > dataIndex)
RemoveLast();
return;
}
}
if (dataIndex >= heightCache.Count)
{
@ -183,10 +203,20 @@ namespace UnityExplorer.UI.Widgets
rangeToDataIndexCache.RemoveAt(rangeStart);
}
}
// if sister cache is set, then update it too.
if (sisterCache != null)
{
var realIdx = ScrollPool.DataSource.GetRealIndexOfTempIndex(dataIndex);
if (realIdx >= 0)
sisterCache.SetIndex(realIdx, value, true);
}
}
public int GetDataIndexAtPosition(float desiredHeight)
=> GetDataIndexAtPosition(desiredHeight, out _);
{
return GetDataIndexAtPosition(desiredHeight, out _);
}
public int GetDataIndexAtPosition(float desiredHeight, out DataViewInfo cache)
{

View File

@ -13,6 +13,8 @@ namespace UnityExplorer.UI.Widgets
void SetCell(ICell cell, int index);
void DisableCell(ICell cell, int index);
int GetRealIndexOfTempIndex(int tempIndex);
ICell CreateCell(RectTransform cellTransform);
}
}

View File

@ -49,11 +49,12 @@ namespace UnityExplorer.UI.Widgets
/// The first and last pooled indices relative to the DataSource's list
/// </summary>
private int bottomDataIndex;
private int TopDataIndex => bottomDataIndex - CellPool.Count + 1;
private int TopDataIndex => Math.Max(0, bottomDataIndex - CellPool.Count + 1);
private readonly List<ICell> CellPool = new List<ICell>();
private DataHeightManager HeightCache;
internal DataHeightCache HeightCache;
internal DataHeightCache tempHeightCache;
private float TotalDataHeight => HeightCache.TotalHeight + contentLayout.padding.top + contentLayout.padding.bottom;
@ -64,8 +65,8 @@ namespace UnityExplorer.UI.Widgets
private int CurrentDataCount => bottomDataIndex + 1;
private Vector2 _prevAnchoredPos;
private float _prevViewportHeight; // TODO track viewport height and add if height increased
private Vector2 prevAnchoredPos;
private float prevViewportHeight;
#region Internal set tracking and update
@ -84,20 +85,54 @@ namespace UnityExplorer.UI.Widgets
private bool writingLocked;
private float timeofLastWriteLock;
private float prevContentHeight;
public override void Update()
{
if (writingLocked && timeofLastWriteLock < Time.time)
writingLocked = false;
if (prevContentHeight == 0.0f && Content?.rect.height != 0.0f)
prevContentHeight = Content.rect.height;
else if (Content.rect.height != prevContentHeight)
{
prevContentHeight = Content.rect.height;
OnValueChangedListener(Vector2.zero);
}
}
#endregion
// Initialize
// Public methods
public void Rebuild()
{
Initialize(DataSource, PrototypeCell);
RecreateCellPool(true, true, null);
}
public void EnableTempCache()
{
if (tempHeightCache == null)
tempHeightCache = HeightCache;
HeightCache = new DataHeightCache(this, tempHeightCache);
}
public void DisableTempCache()
{
if (tempHeightCache == null)
return;
HeightCache = tempHeightCache;
tempHeightCache = null;
}
public void RefreshCells(bool reloadData)
{
RefreshCells(reloadData, true);
}
// Initialize
public void Initialize(IPoolDataSource dataSource, RectTransform prototypeCell)
{
if (!prototypeCell)
@ -106,7 +141,7 @@ namespace UnityExplorer.UI.Widgets
this.PrototypeCell = prototypeCell;
PrototypeCell.transform.SetParent(Viewport, false);
HeightCache = new DataHeightManager(this);
HeightCache = new DataHeightCache(this);
DataSource = dataSource;
this.contentLayout = ScrollRect.content.GetComponent<VerticalLayoutGroup>();
@ -126,7 +161,7 @@ namespace UnityExplorer.UI.Widgets
yield return null;
// set intial bounds
_prevAnchoredPos = Content.anchoredPosition;
prevAnchoredPos = Content.anchoredPosition;
SetRecycleViewBounds(false);
// create initial cell pool and set cells
@ -197,24 +232,32 @@ namespace UnityExplorer.UI.Widgets
RecycleViewBounds = new Vector2(Viewport.MinY() + HalfThreshold, Viewport.MaxY() - HalfThreshold);
if (checkHeightGrow && _prevViewportHeight < Viewport.rect.height && _prevViewportHeight != 0.0f)
{
ret = ExtendCellPool();
}
if (checkHeightGrow && prevViewportHeight < Viewport.rect.height && prevViewportHeight != 0.0f)
ret = RecreateCellPool(false, false, null);
_prevViewportHeight = Viewport.rect.height;
prevViewportHeight = Viewport.rect.height;
return ret;
}
private bool ExtendCellPool()
private bool RecreateCellPool(bool forceRecreate, bool resetDataIndex, bool? setTempCacheEnabledTo)
{
if (setTempCacheEnabledTo != null)
{
if (setTempCacheEnabledTo == true)
EnableTempCache();
else if (setTempCacheEnabledTo == false)
DisableTempCache();
}
bool ret = false;
CheckDataSourceCountChange(out _);
var requiredCoverage = Math.Abs(RecycleViewBounds.y - RecycleViewBounds.x);
var currentCoverage = CellPool.Count * PrototypeHeight;
int cellsRequired = (int)Math.Ceiling((decimal)(requiredCoverage - currentCoverage) / (decimal)PrototypeHeight);
if (cellsRequired > 0)
if (cellsRequired > 0 || forceRecreate)
{
ret = true;
WritingLocked = true;
@ -233,7 +276,7 @@ namespace UnityExplorer.UI.Widgets
bottomDataIndex = maxDataIndex;
// CreateCellPool will destroy existing cells and recreate list.
CreateCellPool(false);
CreateCellPool(resetDataIndex);
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
@ -241,6 +284,7 @@ namespace UnityExplorer.UI.Widgets
ScrollRect.UpdatePrevData();
SetScrollBounds();
UpdateSliderHandle(true);
}
return ret;
@ -272,32 +316,40 @@ namespace UnityExplorer.UI.Widgets
}
}
public void RefreshCells(bool andReloadFromDataSource = false, bool setSlider = true)
private bool CheckDataSourceCountChange(out bool shouldJumpToBottom)
{
bool ret = false;
shouldJumpToBottom = false;
int count = DataSource.ItemCount;
if (bottomDataIndex > count && bottomDataIndex >= CellPool.Count)
{
bottomDataIndex = Math.Max(count - 1, CellPool.Count - 1);
shouldJumpToBottom = true;
}
if (HeightCache.Count < count)
{
HeightCache.SetIndex(count - 1, PrototypeHeight);
return true;
}
else if (HeightCache.Count > count)
{
while (HeightCache.Count > count)
HeightCache.RemoveLast();
return false;
}
return ret;
}
private void RefreshCells(bool andReloadFromDataSource, bool setSlider)
{
if (!CellPool.Any()) return;
SetRecycleViewBounds(true);
// jump to bottom if the data count went below our bottom data index
bool jumpToBottom = false;
if (andReloadFromDataSource)
{
int count = DataSource.ItemCount;
if (bottomDataIndex > count)
{
bottomDataIndex = Math.Max(count - 1, CellPool.Count - 1);
jumpToBottom = true;
}
if (HeightCache.Count < count)
HeightCache.SetIndex(count - 1, PrototypeHeight);
else if (HeightCache.Count > count)
{
while (HeightCache.Count > count)
HeightCache.RemoveLast();
}
}
CheckDataSourceCountChange(out bool jumpToBottom);
// update date height cache, and set cells if 'andReload'
var enumerator = GetPoolEnumerator();
@ -339,9 +391,7 @@ namespace UnityExplorer.UI.Widgets
DataSource.SetCell(cachedCell, dataIndex);
LayoutRebuilder.ForceRebuildLayoutImmediate(cachedCell.Rect);
if (dataIndex < DataSource.ItemCount)
HeightCache.SetIndex(dataIndex, cachedCell.Rect.rect.height);
HeightCache.SetIndex(dataIndex, cachedCell.Rect.rect.height);
}
// Value change processor
@ -355,9 +405,9 @@ namespace UnityExplorer.UI.Widgets
ScrollRect.StopMovement();
if (!SetRecycleViewBounds(true))
RefreshCells();
RefreshCells(false);
float yChange = (ScrollRect.content.anchoredPosition - _prevAnchoredPos).y;
float yChange = (ScrollRect.content.anchoredPosition - prevAnchoredPos).y;
float adjust = 0f;
if (yChange > 0) // Scrolling down
@ -377,7 +427,7 @@ namespace UnityExplorer.UI.Widgets
ScrollRect.m_PrevPosition += vector;
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
_prevAnchoredPos = ScrollRect.content.anchoredPosition;
prevAnchoredPos = ScrollRect.content.anchoredPosition;
SetScrollBounds();
@ -468,6 +518,8 @@ namespace UnityExplorer.UI.Widgets
private void UpdateSliderHandle(bool forcePositionValue = true)
{
CheckDataSourceCountChange(out _);
var dataHeight = TotalDataHeight;
// calculate handle size based on viewport / total data height
@ -491,7 +543,10 @@ namespace UnityExplorer.UI.Widgets
float val = 0f;
if (TotalDataHeight > 0f)
{
var topPos = HeightCache[TopDataIndex].startPosition;
float topPos = 0f;
if (HeightCache.Count > 0)
topPos = HeightCache[TopDataIndex].startPosition;
var scrollPos = topPos + Content.anchoredPosition.y;
val = (float)((decimal)scrollPos / (decimal)(TotalDataHeight - Viewport.rect.height));
@ -518,35 +573,30 @@ namespace UnityExplorer.UI.Widgets
var desiredPosition = val * scrollHeight + NormalizedScrollBounds.x;
// add offset above it for viewport height
var halfheight = Viewport.rect.height * 0.5f;
var desiredMinY = desiredPosition - halfheight;
var halfView = Viewport.rect.height * 0.5f;
var desiredMinY = desiredPosition - halfView;
// get the data index at the top of the viewport
int topViewportIndex = HeightCache.GetDataIndexAtPosition(desiredMinY);
topViewportIndex = Math.Max(0, topViewportIndex);
topViewportIndex = Math.Min(DataSource.ItemCount - 1, topViewportIndex);
// get the real top pooled data index to display our content
int poolStartIndex = Math.Max(0, topViewportIndex - (int)(ExtraPoolCells * 0.5f));
poolStartIndex = Math.Min(DataSource.ItemCount - CellPool.Count, poolStartIndex);
// for content at the very top, just use the desired position as the anchor pos.
if (desiredMinY < RecycleThreshold * 0.5f)
{
Content.anchoredPosition = new Vector2(0, desiredMinY);
}
else // else calculate anchor pos
{
var topStartPos = HeightCache[poolStartIndex].startPosition;
// how far the actual top cell is from our desired center
var diff = desiredMinY - topStartPos;
Content.anchoredPosition = new Vector2(0, diff);
}
poolStartIndex = Math.Min(Math.Max(0, DataSource.ItemCount - CellPool.Count), poolStartIndex);
bottomDataIndex = poolStartIndex + CellPool.Count - 1;
RefreshCells(true, false);
var topStartPos = HeightCache[poolStartIndex].startPosition;
float desiredAnchor;
if (desiredMinY < HalfThreshold)
desiredAnchor = desiredMinY;
else
desiredAnchor = desiredMinY - topStartPos;
Content.anchoredPosition = new Vector2(0, desiredAnchor);
UpdateSliderHandle(true);
}

View File

@ -11,6 +11,8 @@ namespace UnityExplorer.UI.Widgets
{
public class TransformTree : IPoolDataSource
{
public int GetRealIndexOfTempIndex(int index) => throw new NotImplementedException("TODO");
public Func<IEnumerable<GameObject>> GetRootEntriesMethod;
public bool Filtering => !string.IsNullOrEmpty(currentFilter);
@ -93,7 +95,7 @@ namespace UnityExplorer.UI.Widgets
: expandedInstanceIDs.Contains(instanceID);
}
public void RefreshData(bool andReload = false)
public void RefreshData(bool andReload = false, bool hardReload = false)
{
displayedObjects.Clear();
@ -106,7 +108,12 @@ namespace UnityExplorer.UI.Widgets
}
if (andReload)
Scroller.RefreshCells(true);
{
if (!hardReload)
Scroller.RefreshCells(true);
else
Scroller.Rebuild();
}
}
private void Traverse(Transform transform, CachedTransform parent = null)

View File

@ -272,7 +272,7 @@
<Compile Include="UI\Utility\PanelDragger.cs" />
<Compile Include="UI\Utility\SignatureHighlighter.cs" />
<Compile Include="UI\Utility\ToStringUtility.cs" />
<Compile Include="UI\Widgets\ScrollPool\DataHeightManager.cs" />
<Compile Include="UI\Widgets\ScrollPool\DataHeightCache.cs" />
<Compile Include="UI\Widgets\ScrollPool\CellViewHolder.cs" />
<Compile Include="UI\Widgets\ScrollPool\ICell.cs" />
<Compile Include="UI\Widgets\ScrollPool\IPoolDataSource.cs" />