mirror of
https://github.com/sinai-dev/UnityExplorer.git
synced 2025-01-07 18:13:35 +08:00
More progress on the dynamic scroll pool, almost done
This commit is contained in:
parent
a619df8e01
commit
ff7c822d69
@ -90,10 +90,12 @@ namespace UnityExplorer.UI.Panels
|
|||||||
dummyContentHolder.SetActive(false);
|
dummyContentHolder.SetActive(false);
|
||||||
|
|
||||||
GameObject.DontDestroyOnLoad(dummyContentHolder);
|
GameObject.DontDestroyOnLoad(dummyContentHolder);
|
||||||
for (int i = 0; i < 100; i++)
|
ExplorerCore.Log("Creating dummy objects");
|
||||||
|
for (int i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
dummyContents.Add(CreateDummyContent());
|
dummyContents.Add(CreateDummyContent());
|
||||||
}
|
}
|
||||||
|
ExplorerCore.Log("Done");
|
||||||
|
|
||||||
previousRectHeight = mainPanelRect.rect.height;
|
previousRectHeight = mainPanelRect.rect.height;
|
||||||
}
|
}
|
||||||
@ -104,17 +106,20 @@ namespace UnityExplorer.UI.Panels
|
|||||||
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(dummyContentHolder, "Content", true, true, true, true, 2, new Vector4(2, 2, 2, 2));
|
||||||
//UIFactory.SetLayoutElement(obj, minHeight: 25, flexibleHeight: 9999);
|
|
||||||
obj.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
obj.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||||
|
|
||||||
var label = UIFactory.CreateLabel(obj, "label", "Dummy " + dummyContents.Count, TextAnchor.MiddleCenter);
|
var horiGroup = UIFactory.CreateHorizontalGroup(obj, "topGroup", true, true, true, true);
|
||||||
UIFactory.SetLayoutElement(label.gameObject, minHeight: 25, flexibleHeight: 0);
|
UIFactory.SetLayoutElement(horiGroup, minHeight: 25, flexibleHeight: 0);
|
||||||
|
|
||||||
//var input = UIFactory.CreateSrollInputField(obj, "input2", "...", out InputFieldScroller inputScroller);
|
var mainLabel = UIFactory.CreateLabel(horiGroup, "label", "Dummy " + dummyContents.Count, TextAnchor.MiddleCenter);
|
||||||
//UIFactory.SetLayoutElement(input, minHeight: 50, flexibleHeight: 9999);
|
UIFactory.SetLayoutElement(mainLabel.gameObject, minHeight: 25, flexibleHeight: 0);
|
||||||
//input.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
|
||||||
|
|
||||||
var inputObj = UIFactory.CreateInputField(obj, "input", "...", out var inputField);
|
var expandButton = UIFactory.CreateButton(horiGroup, "Expand", "V");
|
||||||
|
UIFactory.SetLayoutElement(expandButton.gameObject, minWidth: 25, flexibleWidth: 0);
|
||||||
|
|
||||||
|
var subContent = UIFactory.CreateVerticalGroup(obj, "SubContent", true, true, true, true);
|
||||||
|
|
||||||
|
var inputObj = UIFactory.CreateInputField(subContent, "input", "...", out var inputField);
|
||||||
UIFactory.SetLayoutElement(inputObj, minHeight: 25, flexibleHeight: 9999);
|
UIFactory.SetLayoutElement(inputObj, minHeight: 25, flexibleHeight: 9999);
|
||||||
inputObj.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
inputObj.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||||
inputField.lineType = InputField.LineType.MultiLineNewline;
|
inputField.lineType = InputField.LineType.MultiLineNewline;
|
||||||
@ -124,6 +129,25 @@ namespace UnityExplorer.UI.Panels
|
|||||||
for (int i = 0; i < numLines; i++)
|
for (int i = 0; i < numLines; i++)
|
||||||
inputField.text += "\r\n";
|
inputField.text += "\r\n";
|
||||||
|
|
||||||
|
subContent.SetActive(false);
|
||||||
|
|
||||||
|
var btnLabel = expandButton.GetComponentInChildren<Text>();
|
||||||
|
expandButton.onClick.AddListener(OnExpand);
|
||||||
|
void OnExpand()
|
||||||
|
{
|
||||||
|
bool active = !subContent.activeSelf;
|
||||||
|
if (active)
|
||||||
|
{
|
||||||
|
subContent.SetActive(true);
|
||||||
|
btnLabel.text = "^";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
subContent.SetActive(false);
|
||||||
|
btnLabel.text = "V";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -788,11 +788,7 @@ namespace UnityExplorer.UI
|
|||||||
// finalize and create ScrollPool
|
// finalize and create ScrollPool
|
||||||
|
|
||||||
uiRoot = mainObj;
|
uiRoot = mainObj;
|
||||||
|
var scrollPool = new ScrollPool(scrollRect);
|
||||||
var scrollPool = new ScrollPool(scrollRect)
|
|
||||||
{
|
|
||||||
AutoResizeHandleRect = autoResizeSliderHandle
|
|
||||||
};
|
|
||||||
|
|
||||||
//viewportObj.GetComponent<Mask>().enabled = false;
|
//viewportObj.GetComponent<Mask>().enabled = false;
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
rect.anchorMin = new Vector2(0, 1);
|
rect.anchorMin = new Vector2(0, 1);
|
||||||
rect.anchorMax = new Vector2(0, 1);
|
rect.anchorMax = new Vector2(0, 1);
|
||||||
rect.pivot = new Vector2(0.5f, 1);
|
rect.pivot = new Vector2(0.5f, 1);
|
||||||
rect.sizeDelta = new Vector2(25, 25);
|
rect.sizeDelta = new Vector2(100, 30);
|
||||||
//UIFactory.SetLayoutElement(prototype, minWidth: 100, flexibleWidth: 9999, minHeight: 25, flexibleHeight: 9999);
|
//UIFactory.SetLayoutElement(prototype, minWidth: 100, flexibleWidth: 9999, minHeight: 25, flexibleHeight: 9999);
|
||||||
|
|
||||||
prototype.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
prototype.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||||
|
@ -10,30 +10,54 @@ using UnityExplorer.UI.Models;
|
|||||||
namespace UnityExplorer.UI.Widgets
|
namespace UnityExplorer.UI.Widgets
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A ScrollRect for a list of content with cells that vary in height, using VerticalLayoutGroup and LayoutElement.
|
/// An object-pooled ScrollRect, attempts to support content of any size and provide a scrollbar for it.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ScrollPool : UIBehaviourModel
|
public class ScrollPool : UIBehaviourModel
|
||||||
{
|
{
|
||||||
// a fancy list to track our total data height
|
// Some helper classes to make managing the complex parts of this a bit easier.
|
||||||
public class HeightCache
|
|
||||||
|
public class CachedHeight
|
||||||
{
|
{
|
||||||
private readonly List<float> heightCache = new List<float>();
|
public int dataIndex;
|
||||||
|
public float height, startPosition;
|
||||||
|
|
||||||
|
public static implicit operator float(CachedHeight ch) => ch.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DataHeightManager
|
||||||
|
{
|
||||||
|
private readonly List<CachedHeight> heightCache = new List<CachedHeight>();
|
||||||
|
|
||||||
|
public int Count => heightCache.Count;
|
||||||
|
|
||||||
public float TotalHeight => totalHeight;
|
public float TotalHeight => totalHeight;
|
||||||
private float totalHeight;
|
private float totalHeight;
|
||||||
|
|
||||||
private static readonly float defaultCellHeight = 25f;
|
public float DefaultHeight => 25f;
|
||||||
|
|
||||||
public float this[int index]
|
// for efficient lookup of "which index is at this range"
|
||||||
|
// list index: DefaultHeight * index from top of data
|
||||||
|
// list value: the data index at this position
|
||||||
|
private readonly List<int> rangeToDataIndexCache = new List<int>();
|
||||||
|
|
||||||
|
public CachedHeight this[int index]
|
||||||
{
|
{
|
||||||
get => heightCache[index];
|
get => heightCache[index];
|
||||||
set => OnSetIndex(index, value);
|
set => SetIndex(index, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private float currentEndPosition;
|
||||||
|
|
||||||
public void Add(float value)
|
public void Add(float value)
|
||||||
{
|
{
|
||||||
heightCache.Add(0f);
|
heightCache.Add(new CachedHeight()
|
||||||
OnSetIndex(heightCache.Count - 1, value);
|
{
|
||||||
|
height = 0f,
|
||||||
|
startPosition = currentEndPosition
|
||||||
|
});
|
||||||
|
|
||||||
|
currentEndPosition += value;
|
||||||
|
SetIndex(heightCache.Count - 1, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear()
|
public void Clear()
|
||||||
@ -42,46 +66,100 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
totalHeight = 0f;
|
totalHeight = 0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnSetIndex(int index, float value)
|
public void SetIndex(int dataIndex, float value)
|
||||||
{
|
{
|
||||||
if (index >= heightCache.Count)
|
if (dataIndex >= heightCache.Count)
|
||||||
{
|
{
|
||||||
while (index > heightCache.Count)
|
while (dataIndex > heightCache.Count)
|
||||||
Add(defaultCellHeight);
|
Add(DefaultHeight);
|
||||||
Add(value);
|
Add(value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var curr = heightCache[index];
|
var curr = heightCache[dataIndex];
|
||||||
if (curr.Equals(value))
|
if (curr.Equals(value))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var diff = value - curr;
|
var diff = value - curr;
|
||||||
totalHeight += diff;
|
totalHeight += diff;
|
||||||
heightCache[index] = value;
|
|
||||||
|
var cache = heightCache[dataIndex];
|
||||||
|
cache.height = value;
|
||||||
|
|
||||||
|
if (dataIndex > 0)
|
||||||
|
{
|
||||||
|
var prev = heightCache[dataIndex - 1];
|
||||||
|
cache.startPosition = prev.startPosition + prev.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (heightCache.Count > dataIndex + 1)
|
||||||
|
heightCache[dataIndex + 1].startPosition += diff;
|
||||||
|
|
||||||
|
// Update the range cache
|
||||||
|
|
||||||
|
// If we are setting an index outside of our cached range we need to naively fill the gap
|
||||||
|
int rangeIndex = (int)Math.Floor((decimal)cache.startPosition / (decimal)DefaultHeight);
|
||||||
|
if (rangeToDataIndexCache.Count <= rangeIndex)
|
||||||
|
{
|
||||||
|
if (!rangeToDataIndexCache.Any())
|
||||||
|
rangeToDataIndexCache.Add(dataIndex);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int lastCurrIndex = rangeToDataIndexCache[rangeToDataIndexCache.Count - 1];
|
||||||
|
while (rangeToDataIndexCache.Count <= rangeIndex)
|
||||||
|
{
|
||||||
|
rangeToDataIndexCache.Add(lastCurrIndex);
|
||||||
|
if (lastCurrIndex < dataIndex - 1)
|
||||||
|
lastCurrIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the starting 'index' of the cell
|
||||||
|
rangeToDataIndexCache[rangeIndex] = dataIndex;
|
||||||
|
|
||||||
|
// if the cell spreads over multiple range indices, then set those too.
|
||||||
|
int spread = (int)Math.Floor((decimal)value / (decimal)25f);
|
||||||
|
if (spread > 1)
|
||||||
|
{
|
||||||
|
for (int i = rangeIndex + 1; i < rangeIndex + spread - 1; i++)
|
||||||
|
{
|
||||||
|
if (i > rangeToDataIndexCache.Count)
|
||||||
|
rangeToDataIndexCache.Add(dataIndex);
|
||||||
|
else
|
||||||
|
rangeToDataIndexCache[i] = dataIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetDataIndexAtStartPosition(float desiredHeight)
|
||||||
|
=> GetDataIndexAtStartPosition(desiredHeight, out _);
|
||||||
|
|
||||||
|
public int GetDataIndexAtStartPosition(float desiredHeight, out CachedHeight cache)
|
||||||
|
{
|
||||||
|
cache = null;
|
||||||
|
|
||||||
|
//desiredHeight = Math.Max(0, desiredHeight);
|
||||||
|
//desiredHeight = Math.Min(TotalHeight, desiredHeight);
|
||||||
|
|
||||||
|
int rangeIndex = (int)Math.Floor((decimal)desiredHeight / (decimal)DefaultHeight);
|
||||||
|
|
||||||
|
if (rangeToDataIndexCache.Count <= rangeIndex)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int dataIndex = rangeToDataIndexCache[rangeIndex];
|
||||||
|
cache = heightCache[dataIndex];
|
||||||
|
|
||||||
|
return dataIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// internal class used to track and manage cell views
|
// internal class used to track and manage cell views
|
||||||
public class CachedCell
|
public class CachedCell
|
||||||
{
|
{
|
||||||
public ScrollPool Pool { get; } // reference to this scrollpool
|
public ScrollPool Pool { get; }
|
||||||
public RectTransform Rect { get; } // the Rect (actual UI object)
|
public RectTransform Rect { get; }
|
||||||
public ICell Cell { get; } // the ICell (to interface with DataSource)
|
public ICell Cell { get; }
|
||||||
|
|
||||||
// used to automatically manage the Pool's TotalCellHeight
|
|
||||||
public float Height
|
|
||||||
{
|
|
||||||
get => m_height;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (value.Equals(m_height))
|
|
||||||
return;
|
|
||||||
var diff = value - m_height;
|
|
||||||
Pool.TotalCellHeight += diff;
|
|
||||||
m_height = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private float m_height;
|
|
||||||
|
|
||||||
public CachedCell(ScrollPool pool, RectTransform rect, ICell cell)
|
public CachedCell(ScrollPool pool, RectTransform rect, ICell cell)
|
||||||
{
|
{
|
||||||
@ -96,12 +174,10 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
this.scrollRect = scrollRect;
|
this.scrollRect = scrollRect;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool AutoResizeHandleRect { get; set; }
|
|
||||||
public float ExtraPoolCoverageRatio = 1.3f;
|
public float ExtraPoolCoverageRatio = 1.3f;
|
||||||
|
|
||||||
public IPoolDataSource DataSource;
|
public IPoolDataSource DataSource;
|
||||||
public RectTransform PrototypeCell;
|
public RectTransform PrototypeCell;
|
||||||
private float DefaultCellHeight => PrototypeCell?.rect.height ?? 25f;
|
|
||||||
|
|
||||||
// UI
|
// UI
|
||||||
|
|
||||||
@ -119,7 +195,7 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
/// <summary>Extra clearance height relative to Viewport height, based on <see cref="ExtraPoolCoverageRatio"/>.</summary>
|
/// <summary>Extra clearance height relative to Viewport height, based on <see cref="ExtraPoolCoverageRatio"/>.</summary>
|
||||||
private Vector2 RecycleViewBounds;
|
private Vector2 RecycleViewBounds;
|
||||||
|
|
||||||
private readonly HeightCache DataHeightCache = new HeightCache();
|
private DataHeightManager HeightCache;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The first and last pooled indices relative to the DataSource's list
|
/// The first and last pooled indices relative to the DataSource's list
|
||||||
@ -186,6 +262,7 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
|
|
||||||
public void Initialize(IPoolDataSource dataSource)
|
public void Initialize(IPoolDataSource dataSource)
|
||||||
{
|
{
|
||||||
|
HeightCache = new DataHeightManager();
|
||||||
DataSource = dataSource;
|
DataSource = dataSource;
|
||||||
|
|
||||||
this.contentLayout = scrollRect.content.GetComponent<VerticalLayoutGroup>();
|
this.contentLayout = scrollRect.content.GetComponent<VerticalLayoutGroup>();
|
||||||
@ -210,29 +287,16 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
|
|
||||||
SetRecycleViewBounds();
|
SetRecycleViewBounds();
|
||||||
|
|
||||||
BuildInitialHeightCache();
|
ExplorerCore.Log("Creating cell pool");
|
||||||
|
float start = Time.realtimeSinceStartup;
|
||||||
CreateCellPool();
|
CreateCellPool();
|
||||||
|
|
||||||
//SetContentHeight();
|
SetSliderPositionAndSize();
|
||||||
|
ExplorerCore.Log("Done");
|
||||||
UpdateSliderPositionAndSize();
|
|
||||||
|
|
||||||
scrollRect.onValueChanged.AddListener(OnValueChangedListener);
|
scrollRect.onValueChanged.AddListener(OnValueChangedListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void BuildInitialHeightCache()
|
|
||||||
{
|
|
||||||
DataHeightCache.Clear();
|
|
||||||
float defaultHeight = DefaultCellHeight;
|
|
||||||
for (int i = 0; i < DataSource.ItemCount; i++)
|
|
||||||
{
|
|
||||||
if (i < CellPool.Count)
|
|
||||||
DataHeightCache.Add(CellPool[i].Height);
|
|
||||||
else
|
|
||||||
DataHeightCache.Add(defaultHeight);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetRecycleViewBounds()
|
private void SetRecycleViewBounds()
|
||||||
{
|
{
|
||||||
var extra = (Viewport.rect.height * ExtraPoolCoverageRatio) - Viewport.rect.height;
|
var extra = (Viewport.rect.height * ExtraPoolCoverageRatio) - Viewport.rect.height;
|
||||||
@ -266,12 +330,14 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RefreshCells(bool andReloadFromDataSource = false)
|
public void RefreshCells(bool andReloadFromDataSource = false, bool setSlider = true)
|
||||||
{
|
{
|
||||||
if (!CellPool.Any()) return;
|
if (!CellPool.Any()) return;
|
||||||
|
|
||||||
SetRecycleViewBounds();
|
SetRecycleViewBounds();
|
||||||
|
|
||||||
|
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
|
||||||
|
|
||||||
bool jumpToBottom = false;
|
bool jumpToBottom = false;
|
||||||
if (andReloadFromDataSource)
|
if (andReloadFromDataSource)
|
||||||
{
|
{
|
||||||
@ -281,8 +347,11 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
bottomDataIndex = Math.Max(count - 1, CellPool.Count - 1);
|
bottomDataIndex = Math.Max(count - 1, CellPool.Count - 1);
|
||||||
jumpToBottom = true;
|
jumpToBottom = true;
|
||||||
}
|
}
|
||||||
|
else if (HeightCache.Count < count)
|
||||||
|
HeightCache.SetIndex(count - 1, PrototypeCell?.rect.height ?? 25f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update date height cache, and set cells if 'andReload'
|
||||||
var enumerator = GetPoolEnumerator();
|
var enumerator = GetPoolEnumerator();
|
||||||
while (enumerator.MoveNext())
|
while (enumerator.MoveNext())
|
||||||
{
|
{
|
||||||
@ -292,21 +361,21 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
if (andReloadFromDataSource)
|
if (andReloadFromDataSource)
|
||||||
SetCell(cell, curr.dataIndex);
|
SetCell(cell, curr.dataIndex);
|
||||||
else
|
else
|
||||||
{
|
HeightCache.SetIndex(curr.dataIndex, cell.Rect.rect.height);
|
||||||
cell.Height = cell.Rect.rect.height;
|
|
||||||
DataHeightCache[curr.dataIndex] = cell.Height;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
|
||||||
SetRecycleViewBounds();
|
SetRecycleViewBounds();
|
||||||
|
|
||||||
|
// force check recycles
|
||||||
if (andReloadFromDataSource)
|
if (andReloadFromDataSource)
|
||||||
{
|
{
|
||||||
RecycleBottomToTop();
|
RecycleBottomToTop();
|
||||||
RecycleTopToBottom();
|
RecycleTopToBottom();
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateSliderPositionAndSize();
|
if (setSlider)
|
||||||
|
SetSliderPositionAndSize();
|
||||||
|
|
||||||
if (jumpToBottom)
|
if (jumpToBottom)
|
||||||
{
|
{
|
||||||
@ -320,10 +389,9 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
cachedCell.Cell.Enable();
|
cachedCell.Cell.Enable();
|
||||||
DataSource.SetCell(cachedCell.Cell, dataIndex);
|
DataSource.SetCell(cachedCell.Cell, dataIndex);
|
||||||
|
|
||||||
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
|
LayoutRebuilder.ForceRebuildLayoutImmediate(cachedCell.Rect);
|
||||||
|
|
||||||
cachedCell.Height = cachedCell.Cell.Enabled ? cachedCell.Rect.rect.height : 0f;
|
HeightCache.SetIndex(dataIndex, cachedCell.Cell.Enabled ? cachedCell.Rect.rect.height : 0f);
|
||||||
DataHeightCache[dataIndex] = cachedCell.Height;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cell pool
|
// Cell pool
|
||||||
@ -376,6 +444,8 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
SetCell(cell, i);
|
SetCell(cell, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
|
||||||
|
|
||||||
//Deactivate prototype cell if it is not a prefab(i.e it's present in scene)
|
//Deactivate prototype cell if it is not a prefab(i.e it's present in scene)
|
||||||
if (PrototypeCell.gameObject.scene.IsValid())
|
if (PrototypeCell.gameObject.scene.IsValid())
|
||||||
PrototypeCell.gameObject.SetActive(false);
|
PrototypeCell.gameObject.SetActive(false);
|
||||||
@ -413,7 +483,7 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
|
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
|
||||||
_prevAnchoredPos = scrollRect.content.anchoredPosition;
|
_prevAnchoredPos = scrollRect.content.anchoredPosition;
|
||||||
|
|
||||||
UpdateSliderPositionAndSize();
|
SetSliderPositionAndSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool ShouldRecycleTop => GetCellExtent(CellPool[topPoolCellIndex]) >= RecycleViewBounds.x
|
private bool ShouldRecycleTop => GetCellExtent(CellPool[topPoolCellIndex]) >= RecycleViewBounds.x
|
||||||
@ -498,64 +568,154 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
|
|
||||||
// Slider
|
// Slider
|
||||||
|
|
||||||
private void UpdateSliderPositionAndSize()
|
private void SetSliderPositionAndSize()
|
||||||
{
|
{
|
||||||
int total = DataSource.ItemCount;
|
var dataHeight = HeightCache.TotalHeight;
|
||||||
total = Math.Max(total, 1);
|
|
||||||
|
|
||||||
// NAIVE TEMP DEBUG - all cells will NOT be the same height!
|
// calculate handle size based on viewport / total data height
|
||||||
|
var viewportHeight = Viewport.rect.height;
|
||||||
|
var handleHeight = viewportHeight * Math.Min(1, viewportHeight / dataHeight);
|
||||||
|
handleHeight = Math.Max(15f, handleHeight);
|
||||||
|
|
||||||
var spread = CellPool.Count(it => it.Cell.Enabled);
|
// resize the handle container area for the size of the handle (bigger handle = smaller container)
|
||||||
|
var container = slider.m_HandleContainerRect;
|
||||||
|
container.offsetMax = new Vector2(container.offsetMax.x, -(handleHeight * 0.5f));
|
||||||
|
container.offsetMin = new Vector2(container.offsetMin.x, handleHeight * 0.5f);
|
||||||
|
|
||||||
// TODO temp debug
|
// set handle size
|
||||||
bool forceValue = true;
|
slider.handleRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, handleHeight);
|
||||||
if (forceValue)
|
|
||||||
|
// if slider is 100% height then make it not interactable
|
||||||
|
slider.interactable = !Mathf.Approximately(handleHeight, viewportHeight);
|
||||||
|
|
||||||
|
GetDisplayedCellLimits(out CellInfo? topVisibleCell, out _);
|
||||||
|
if (topVisibleCell != null)
|
||||||
{
|
{
|
||||||
if (spread >= total)
|
var topCell = CellPool[topVisibleCell.Value.cellIndex];
|
||||||
slider.value = 0f;
|
|
||||||
else
|
// get the starting height of the top displayed cell
|
||||||
slider.value = (float)((decimal)TopDataIndex / Math.Max(1, total - CellPool.Count));
|
float startHeight = contentLayout.padding.top;
|
||||||
|
int dataIndex = topVisibleCell.Value.dataIndex;
|
||||||
|
for (int i = 0; i < dataIndex; i++)
|
||||||
|
startHeight += HeightCache[i];
|
||||||
|
startHeight += topCell.Rect.MinY() - Viewport.MinY(); // add the amount above the viewport min it is
|
||||||
|
|
||||||
|
// set the value of the slider
|
||||||
|
WritingLocked = true;
|
||||||
|
slider.value = (float)((decimal)startHeight / (decimal)(HeightCache.TotalHeight - Viewport.rect.height));
|
||||||
}
|
}
|
||||||
|
else
|
||||||
if (AutoResizeHandleRect)
|
|
||||||
{
|
{
|
||||||
var viewportHeight = scrollRect.viewport.rect.height;
|
slider.value = 0f;
|
||||||
|
|
||||||
var handleRatio = (decimal)spread / total;
|
|
||||||
var handleHeight = viewportHeight * (float)Math.Min(1, handleRatio);
|
|
||||||
|
|
||||||
handleHeight = Math.Max(handleHeight, 15f);
|
|
||||||
|
|
||||||
// need to resize the handle container area for the size of the handle (bigger handle = smaller container)
|
|
||||||
var container = slider.m_HandleContainerRect;
|
|
||||||
container.offsetMax = new Vector2(container.offsetMax.x, -(handleHeight * 0.5f));
|
|
||||||
container.offsetMin = new Vector2(container.offsetMin.x, handleHeight * 0.5f);
|
|
||||||
|
|
||||||
var handle = slider.handleRect;
|
|
||||||
|
|
||||||
handle.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, handleHeight);
|
|
||||||
|
|
||||||
// if slider is 100% height then make it not interactable.
|
|
||||||
slider.interactable = !Mathf.Approximately(handleHeight, viewportHeight);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void GetDisplayedCellLimits(out CellInfo? top, out CellInfo? bottom)
|
||||||
|
{
|
||||||
|
// get the index of the top displayed cell
|
||||||
|
top = null;
|
||||||
|
bottom = null;
|
||||||
|
var enumerator = GetPoolEnumerator();
|
||||||
|
while (enumerator.MoveNext())
|
||||||
|
{
|
||||||
|
var curr = enumerator.Current;
|
||||||
|
var cell = CellPool[curr.cellIndex];
|
||||||
|
// if cell bottom is below viewport top
|
||||||
|
if (cell.Rect.MaxY() < Viewport.MinY())
|
||||||
|
{
|
||||||
|
// and if this is the top-most displayed cell
|
||||||
|
if (top == null || CellPool[top.Value.cellIndex].Rect.position.y < cell.Rect.position.y)
|
||||||
|
top = curr;
|
||||||
|
}
|
||||||
|
// if cell top is above viewport bottom
|
||||||
|
if (cell.Rect.MinY() > Viewport.MaxY())
|
||||||
|
{
|
||||||
|
// and if this is the bottom-most displayed cell
|
||||||
|
if (bottom == null || CellPool[bottom.Value.cellIndex].Rect.position.y > cell.Rect.position.y)
|
||||||
|
bottom = curr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mostly works, just little bit jumpy and jittery, needs some refining.
|
||||||
|
|
||||||
private void OnSliderValueChanged(float val)
|
private void OnSliderValueChanged(float val)
|
||||||
{
|
{
|
||||||
if (this.WritingLocked)
|
if (this.WritingLocked)
|
||||||
return;
|
return;
|
||||||
this.WritingLocked = true;
|
this.WritingLocked = true;
|
||||||
|
|
||||||
// TODO this cant work until we have a cache of all data heights.
|
var desiredPosition = val * HeightCache.TotalHeight;
|
||||||
// will need to maintain that as we go and assume default height for indeterminate cells.
|
|
||||||
}
|
|
||||||
|
|
||||||
private void JumpToIndex(int dataIndex)
|
// add the top and bottom extra area for recycle bounds
|
||||||
{
|
var recycleExtra = Viewport.rect.height * ExtraPoolCoverageRatio - Viewport.rect.height;
|
||||||
// TODO this cant work until we have a cache of all data heights.
|
var realMin = Math.Max(0f, desiredPosition - recycleExtra);
|
||||||
// will need to maintain that as we go and assume default height for indeterminate cells.
|
realMin = Math.Min(realMin, HeightCache.TotalHeight - Viewport.rect.height);
|
||||||
}
|
|
||||||
|
|
||||||
|
var realBottomIndex = HeightCache.GetDataIndexAtStartPosition(realMin);
|
||||||
|
if (realBottomIndex == -1)
|
||||||
|
realBottomIndex = DataSource.ItemCount - 1;
|
||||||
|
// calculate which data index should be at bottom of pool
|
||||||
|
bottomDataIndex = realBottomIndex + CellPool.Count - 1;
|
||||||
|
bottomDataIndex = Math.Min(bottomDataIndex, DataSource.ItemCount - 1);
|
||||||
|
|
||||||
|
ExplorerCore.Log("set bottom data index to " + bottomDataIndex);
|
||||||
|
RefreshCells(true, false);
|
||||||
|
|
||||||
|
var realDesiredIndex = HeightCache.GetDataIndexAtStartPosition(desiredPosition);
|
||||||
|
|
||||||
|
GetDisplayedCellLimits(out CellInfo? top, out CellInfo? bottom);
|
||||||
|
|
||||||
|
// TODO this is not quite right, I think this is causing the jittery jumpiness
|
||||||
|
|
||||||
|
// calculate how much we need to move up. use height cache for indices above top displayed, move that much.
|
||||||
|
float move = 0f;
|
||||||
|
if (realDesiredIndex < top.Value.dataIndex)
|
||||||
|
{
|
||||||
|
ExplorerCore.Log("desired cell is above viewport");
|
||||||
|
var enumerator = GetPoolEnumerator();
|
||||||
|
while (enumerator.MoveNext())
|
||||||
|
{
|
||||||
|
var curr = enumerator.Current;
|
||||||
|
if (curr.dataIndex == realDesiredIndex)
|
||||||
|
{
|
||||||
|
var cell = CellPool[curr.cellIndex];
|
||||||
|
move = Viewport.MinY() - cell.Rect.MinY();
|
||||||
|
ExplorerCore.Log("desired index is " + move + " above the viewport min");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (realDesiredIndex > bottom.Value.dataIndex)
|
||||||
|
{
|
||||||
|
ExplorerCore.Log("desired cell is below viewport");
|
||||||
|
var enumerator = GetPoolEnumerator();
|
||||||
|
while (enumerator.MoveNext())
|
||||||
|
{
|
||||||
|
var curr = enumerator.Current;
|
||||||
|
if (curr.dataIndex == realDesiredIndex)
|
||||||
|
{
|
||||||
|
var cell = CellPool[curr.cellIndex];
|
||||||
|
move = Viewport.MaxY() - cell.Rect.MaxY();
|
||||||
|
ExplorerCore.Log("desired index is " + move + " below the viewport min");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO move should account for desired actual position, otherwise we just snap to cells.
|
||||||
|
|
||||||
|
if (move != 0.0f)
|
||||||
|
{
|
||||||
|
ExplorerCore.Log("Content should move " + move);
|
||||||
|
Content.anchoredPosition += Vector2.up * move;
|
||||||
|
scrollRect.m_PrevPosition += Vector2.up * move;
|
||||||
|
}
|
||||||
|
|
||||||
|
SetSliderPositionAndSize();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Use <see cref="UIFactory.CreateScrollPool"/></summary>
|
/// <summary>Use <see cref="UIFactory.CreateScrollPool"/></summary>
|
||||||
public override void ConstructUI(GameObject parent) => throw new NotImplementedException();
|
public override void ConstructUI(GameObject parent) => throw new NotImplementedException();
|
||||||
|
@ -275,7 +275,6 @@
|
|||||||
<Compile Include="UI\Widgets\ScrollPool\DynamicCell.cs" />
|
<Compile Include="UI\Widgets\ScrollPool\DynamicCell.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" />
|
||||||
<Compile Include="UI\Widgets\ScrollPool\ScrollPool_bak.cs" />
|
|
||||||
<Compile Include="UI\Widgets\ScrollPool\ScrollPool.cs" />
|
<Compile Include="UI\Widgets\ScrollPool\ScrollPool.cs" />
|
||||||
<Compile Include="UI\Widgets\ScrollPool\UIExtensions.cs" />
|
<Compile Include="UI\Widgets\ScrollPool\UIExtensions.cs" />
|
||||||
<Compile Include="UI\Widgets\InputFieldScroller.cs" />
|
<Compile Include="UI\Widgets\InputFieldScroller.cs" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user