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.Input;
using UnityExplorer.Core.Runtime; using UnityExplorer.Core.Runtime;
using UnityExplorer.UI; using UnityExplorer.UI;
using UnityExplorer.UI.Panels;
namespace UnityExplorer namespace UnityExplorer
{ {

View File

@ -34,28 +34,6 @@ namespace UnityExplorer.UI.Panels
ConfigManager.GameObjectInspectorData.Value = this.ToSaveData(); 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() public override void SetDefaultPosAndAnchors()
{ {
mainPanelRect.localPosition = Vector2.zero; mainPanelRect.localPosition = Vector2.zero;
@ -68,6 +46,8 @@ namespace UnityExplorer.UI.Panels
mainPanelRect.anchoredPosition = new Vector2(-150, 0); mainPanelRect.anchoredPosition = new Vector2(-150, 0);
} }
internal static DynamicListTest listInstance;
private ScrollPool scrollPool; private ScrollPool scrollPool;
public override void ConstructPanelContent() public override void ConstructPanelContent()
@ -83,32 +63,33 @@ namespace UnityExplorer.UI.Panels
//scrollPool.Viewport.GetComponent<Mask>().enabled = false; //scrollPool.Viewport.GetComponent<Mask>().enabled = false;
//scrollPool.Content.gameObject.AddComponent<Image>().color = new Color(1f, 0f, 1f, 0.3f); //scrollPool.Content.gameObject.AddComponent<Image>().color = new Color(1f, 0f, 1f, 0.3f);
var test = new DynamicListTest(scrollPool, this); listInstance = new DynamicListTest(scrollPool, this);
test.Init(); listInstance.Init();
//var prototype = DynamicCell.CreatePrototypeCell(scrollContent); //var prototype = DynamicCell.CreatePrototypeCell(scrollContent);
//scrollPool.PrototypeCell = prototype.GetComponent<RectTransform>(); //scrollPool.PrototypeCell = prototype.GetComponent<RectTransform>();
dummyContentHolder = new GameObject("DummyHolder"); contentHolder = new GameObject("DummyHolder");
dummyContentHolder.SetActive(false); contentHolder.SetActive(false);
contentHolder.transform.SetParent(this.content.transform, false);
GameObject.DontDestroyOnLoad(dummyContentHolder); GameObject.DontDestroyOnLoad(contentHolder);
ExplorerCore.Log("Creating dummy objects"); ExplorerCore.Log("Creating dummy objects");
for (int i = 0; i < 100; i++) for (int i = 0; i < 10; i++)
{ {
dummyContents.Add(CreateDummyContent()); dummyContents.Add(CreateDummyContent());
} }
ExplorerCore.Log("Done"); 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>(); internal readonly List<GameObject> dummyContents = new List<GameObject>();
private GameObject CreateDummyContent() 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; obj.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
var horiGroup = UIFactory.CreateHorizontalGroup(obj, "topGroup", true, true, true, true); var horiGroup = UIFactory.CreateHorizontalGroup(obj, "topGroup", true, true, true, true);
@ -157,29 +138,76 @@ namespace UnityExplorer.UI.Panels
public class DynamicListTest : IPoolDataSource public class DynamicListTest : IPoolDataSource
{ {
internal ScrollPool Scroller; internal ScrollPool ScrollPool;
internal InspectorTest Inspector; internal InspectorTest Inspector;
public DynamicListTest(ScrollPool scroller, InspectorTest inspector) public DynamicListTest(ScrollPool scroller, InspectorTest inspector)
{ {
Scroller = scroller; ScrollPool = scroller;
Inspector = inspector; 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) public void OnDisableCell(CellViewHolder cell, int dataIndex)
{ {
if (cell.UIRoot.transform.Find("Content") is Transform existing) 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() public void Init()
{ {
var prototype = CellViewHolder.CreatePrototypeCell(Scroller.UIRoot); var prototype = CellViewHolder.CreatePrototypeCell(ScrollPool.UIRoot);
Scroller.DataSource = this; ScrollPool.DataSource = this;
Scroller.Initialize(this, prototype); ScrollPool.Initialize(this, prototype);
} }
public ICell CreateCell(RectTransform cellTransform) => new CellViewHolder(cellTransform.gameObject); public ICell CreateCell(RectTransform cellTransform) => new CellViewHolder(cellTransform.gameObject);
@ -202,6 +230,9 @@ namespace UnityExplorer.UI.Panels
return; return;
} }
if (filtering)
index = GetRealIndexOfTempIndex(index);
var content = Inspector.dummyContents[index]; var content = Inspector.dummyContents[index];
if (content.transform.parent.ReferenceEqual(root.transform)) if (content.transform.parent.ReferenceEqual(root.transform))
@ -215,7 +246,7 @@ namespace UnityExplorer.UI.Panels
private void DisableContent(GameObject cellRoot) private void DisableContent(GameObject cellRoot)
{ {
if (cellRoot.transform.Find("Content") is Transform existing) 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) private void OnFilterInput(string input)
{ {
Tree.CurrentFilter = input; Tree.CurrentFilter = input;
Tree.RefreshData(true); Tree.RefreshData(true, true);
} }
//private float highestRectHeight; //private float highestRectHeight;
@ -220,14 +220,17 @@ namespace UnityExplorer.UI.Panels
// Transform Tree // 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)); out GameObject scrollContent, new Color(0.15f, 0.15f, 0.15f));
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999); UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
UIFactory.SetLayoutElement(scrollContent, flexibleHeight: 9999); UIFactory.SetLayoutElement(scrollContent, flexibleHeight: 9999);
Tree = new TransformTree(infiniteScroll) { GetRootEntriesMethod = GetRootEntries }; Tree = new TransformTree(scrollPool) { GetRootEntriesMethod = GetRootEntries };
Tree.Init(); Tree.Init();
//scrollPool.Viewport.GetComponent<Mask>().enabled = false;
//UIRoot.GetComponent<Mask>().enabled = false;
// Scene Loader // Scene Loader
ConstructSceneLoader(); ConstructSceneLoader();

View File

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

View File

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

View File

@ -11,18 +11,27 @@ namespace UnityExplorer.UI.Widgets
public float height, startPosition; public float height, startPosition;
public int normalizedSpread; 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 ScrollPool ScrollPool { get; }
private DataHeightCache sisterCache { get; }
public DataHeightManager(ScrollPool scrollPool) public DataHeightCache(ScrollPool scrollPool)
{ {
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>(); private readonly List<DataViewInfo> heightCache = new List<DataViewInfo>();
public int Count => heightCache.Count; public int Count => heightCache.Count;
@ -62,6 +71,17 @@ namespace UnityExplorer.UI.Widgets
totalHeight += value; 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() public void RemoveLast()
{ {
if (!heightCache.Any()) if (!heightCache.Any())
@ -70,13 +90,6 @@ namespace UnityExplorer.UI.Widgets
var val = heightCache[heightCache.Count - 1]; var val = heightCache[heightCache.Count - 1];
totalHeight -= val; totalHeight -= val;
heightCache.RemoveAt(heightCache.Count - 1); heightCache.RemoveAt(heightCache.Count - 1);
}
public void Clear()
{
heightCache.Clear();
totalHeight = 0f;
} }
private void AppendDataSpread(int dataIdx, int spread) 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 (!ignoreDataCount)
{ {
if (dataIndex >= ScrollPool.DataSource.ItemCount) if (dataIndex >= ScrollPool.DataSource.ItemCount)
{
while (heightCache.Count > dataIndex)
RemoveLast();
return; return;
}
}
if (dataIndex >= heightCache.Count) if (dataIndex >= heightCache.Count)
{ {
@ -183,10 +203,20 @@ namespace UnityExplorer.UI.Widgets
rangeToDataIndexCache.RemoveAt(rangeStart); 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) public int GetDataIndexAtPosition(float desiredHeight)
=> GetDataIndexAtPosition(desiredHeight, out _); {
return GetDataIndexAtPosition(desiredHeight, out _);
}
public int GetDataIndexAtPosition(float desiredHeight, out DataViewInfo cache) 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 SetCell(ICell cell, int index);
void DisableCell(ICell cell, int index); void DisableCell(ICell cell, int index);
int GetRealIndexOfTempIndex(int tempIndex);
ICell CreateCell(RectTransform cellTransform); 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 /// The first and last pooled indices relative to the DataSource's list
/// </summary> /// </summary>
private int bottomDataIndex; 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 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; 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 int CurrentDataCount => bottomDataIndex + 1;
private Vector2 _prevAnchoredPos; private Vector2 prevAnchoredPos;
private float _prevViewportHeight; // TODO track viewport height and add if height increased private float prevViewportHeight;
#region Internal set tracking and update #region Internal set tracking and update
@ -84,20 +85,54 @@ namespace UnityExplorer.UI.Widgets
private bool writingLocked; private bool writingLocked;
private float timeofLastWriteLock; private float timeofLastWriteLock;
private float prevContentHeight;
public override void Update() public override void Update()
{ {
if (writingLocked && timeofLastWriteLock < Time.time) if (writingLocked && timeofLastWriteLock < Time.time)
writingLocked = false; 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 #endregion
// Initialize // Public methods
public void Rebuild() 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) public void Initialize(IPoolDataSource dataSource, RectTransform prototypeCell)
{ {
if (!prototypeCell) if (!prototypeCell)
@ -106,7 +141,7 @@ namespace UnityExplorer.UI.Widgets
this.PrototypeCell = prototypeCell; this.PrototypeCell = prototypeCell;
PrototypeCell.transform.SetParent(Viewport, false); PrototypeCell.transform.SetParent(Viewport, false);
HeightCache = new DataHeightManager(this); HeightCache = new DataHeightCache(this);
DataSource = dataSource; DataSource = dataSource;
this.contentLayout = ScrollRect.content.GetComponent<VerticalLayoutGroup>(); this.contentLayout = ScrollRect.content.GetComponent<VerticalLayoutGroup>();
@ -126,7 +161,7 @@ namespace UnityExplorer.UI.Widgets
yield return null; yield return null;
// set intial bounds // set intial bounds
_prevAnchoredPos = Content.anchoredPosition; prevAnchoredPos = Content.anchoredPosition;
SetRecycleViewBounds(false); SetRecycleViewBounds(false);
// create initial cell pool and set cells // create initial cell pool and set cells
@ -197,24 +232,32 @@ namespace UnityExplorer.UI.Widgets
RecycleViewBounds = new Vector2(Viewport.MinY() + HalfThreshold, Viewport.MaxY() - HalfThreshold); RecycleViewBounds = new Vector2(Viewport.MinY() + HalfThreshold, Viewport.MaxY() - HalfThreshold);
if (checkHeightGrow && _prevViewportHeight < Viewport.rect.height && _prevViewportHeight != 0.0f) if (checkHeightGrow && prevViewportHeight < Viewport.rect.height && prevViewportHeight != 0.0f)
{ ret = RecreateCellPool(false, false, null);
ret = ExtendCellPool();
}
_prevViewportHeight = Viewport.rect.height; prevViewportHeight = Viewport.rect.height;
return ret; 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; bool ret = false;
CheckDataSourceCountChange(out _);
var requiredCoverage = Math.Abs(RecycleViewBounds.y - RecycleViewBounds.x); var requiredCoverage = Math.Abs(RecycleViewBounds.y - RecycleViewBounds.x);
var currentCoverage = CellPool.Count * PrototypeHeight; var currentCoverage = CellPool.Count * PrototypeHeight;
int cellsRequired = (int)Math.Ceiling((decimal)(requiredCoverage - currentCoverage) / (decimal)PrototypeHeight); int cellsRequired = (int)Math.Ceiling((decimal)(requiredCoverage - currentCoverage) / (decimal)PrototypeHeight);
if (cellsRequired > 0) if (cellsRequired > 0 || forceRecreate)
{ {
ret = true; ret = true;
WritingLocked = true; WritingLocked = true;
@ -233,7 +276,7 @@ namespace UnityExplorer.UI.Widgets
bottomDataIndex = maxDataIndex; bottomDataIndex = maxDataIndex;
// CreateCellPool will destroy existing cells and recreate list. // CreateCellPool will destroy existing cells and recreate list.
CreateCellPool(false); CreateCellPool(resetDataIndex);
LayoutRebuilder.ForceRebuildLayoutImmediate(Content); LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
@ -241,6 +284,7 @@ namespace UnityExplorer.UI.Widgets
ScrollRect.UpdatePrevData(); ScrollRect.UpdatePrevData();
SetScrollBounds(); SetScrollBounds();
UpdateSliderHandle(true);
} }
return ret; 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; if (!CellPool.Any()) return;
SetRecycleViewBounds(true); SetRecycleViewBounds(true);
// jump to bottom if the data count went below our bottom data index CheckDataSourceCountChange(out bool jumpToBottom);
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();
}
}
// update date height cache, and set cells if 'andReload' // update date height cache, and set cells if 'andReload'
var enumerator = GetPoolEnumerator(); var enumerator = GetPoolEnumerator();
@ -339,8 +391,6 @@ namespace UnityExplorer.UI.Widgets
DataSource.SetCell(cachedCell, dataIndex); DataSource.SetCell(cachedCell, dataIndex);
LayoutRebuilder.ForceRebuildLayoutImmediate(cachedCell.Rect); LayoutRebuilder.ForceRebuildLayoutImmediate(cachedCell.Rect);
if (dataIndex < DataSource.ItemCount)
HeightCache.SetIndex(dataIndex, cachedCell.Rect.rect.height); HeightCache.SetIndex(dataIndex, cachedCell.Rect.rect.height);
} }
@ -355,9 +405,9 @@ namespace UnityExplorer.UI.Widgets
ScrollRect.StopMovement(); ScrollRect.StopMovement();
if (!SetRecycleViewBounds(true)) if (!SetRecycleViewBounds(true))
RefreshCells(); RefreshCells(false);
float yChange = (ScrollRect.content.anchoredPosition - _prevAnchoredPos).y; float yChange = (ScrollRect.content.anchoredPosition - prevAnchoredPos).y;
float adjust = 0f; float adjust = 0f;
if (yChange > 0) // Scrolling down if (yChange > 0) // Scrolling down
@ -377,7 +427,7 @@ namespace UnityExplorer.UI.Widgets
ScrollRect.m_PrevPosition += vector; ScrollRect.m_PrevPosition += vector;
LayoutRebuilder.ForceRebuildLayoutImmediate(Content); LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
_prevAnchoredPos = ScrollRect.content.anchoredPosition; prevAnchoredPos = ScrollRect.content.anchoredPosition;
SetScrollBounds(); SetScrollBounds();
@ -468,6 +518,8 @@ namespace UnityExplorer.UI.Widgets
private void UpdateSliderHandle(bool forcePositionValue = true) private void UpdateSliderHandle(bool forcePositionValue = true)
{ {
CheckDataSourceCountChange(out _);
var dataHeight = TotalDataHeight; var dataHeight = TotalDataHeight;
// calculate handle size based on viewport / total data height // calculate handle size based on viewport / total data height
@ -491,7 +543,10 @@ namespace UnityExplorer.UI.Widgets
float val = 0f; float val = 0f;
if (TotalDataHeight > 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; var scrollPos = topPos + Content.anchoredPosition.y;
val = (float)((decimal)scrollPos / (decimal)(TotalDataHeight - Viewport.rect.height)); val = (float)((decimal)scrollPos / (decimal)(TotalDataHeight - Viewport.rect.height));
@ -518,35 +573,30 @@ namespace UnityExplorer.UI.Widgets
var desiredPosition = val * scrollHeight + NormalizedScrollBounds.x; var desiredPosition = val * scrollHeight + NormalizedScrollBounds.x;
// add offset above it for viewport height // add offset above it for viewport height
var halfheight = Viewport.rect.height * 0.5f; var halfView = Viewport.rect.height * 0.5f;
var desiredMinY = desiredPosition - halfheight; var desiredMinY = desiredPosition - halfView;
// get the data index at the top of the viewport // get the data index at the top of the viewport
int topViewportIndex = HeightCache.GetDataIndexAtPosition(desiredMinY); int topViewportIndex = HeightCache.GetDataIndexAtPosition(desiredMinY);
topViewportIndex = Math.Max(0, topViewportIndex); topViewportIndex = Math.Max(0, topViewportIndex);
topViewportIndex = Math.Min(DataSource.ItemCount - 1, topViewportIndex);
// get the real top pooled data index to display our content // get the real top pooled data index to display our content
int poolStartIndex = Math.Max(0, topViewportIndex - (int)(ExtraPoolCells * 0.5f)); int poolStartIndex = Math.Max(0, topViewportIndex - (int)(ExtraPoolCells * 0.5f));
poolStartIndex = Math.Min(DataSource.ItemCount - CellPool.Count, poolStartIndex); poolStartIndex = Math.Min(Math.Max(0, 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);
}
bottomDataIndex = poolStartIndex + CellPool.Count - 1; bottomDataIndex = poolStartIndex + CellPool.Count - 1;
RefreshCells(true, false); 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); UpdateSliderHandle(true);
} }

View File

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

View File

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