2021-04-15 20:18:03 +10:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
2021-04-16 02:48:49 +10:00
|
|
|
|
using UnityExplorer.UI.Models;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 21:07:32 +10:00
|
|
|
|
namespace UnityExplorer.UI.Widgets
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-18 21:38:09 +10:00
|
|
|
|
/// <summary>
|
2021-04-19 20:08:07 +10:00
|
|
|
|
/// A ScrollRect for a list of content with cells that vary in height, using VerticalLayoutGroup and LayoutElement.
|
2021-04-18 21:38:09 +10:00
|
|
|
|
/// </summary>
|
2021-04-19 20:08:07 +10:00
|
|
|
|
public class ScrollPool : UIBehaviourModel
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
// a fancy list to track our total data height
|
|
|
|
|
public class HeightCache
|
|
|
|
|
{
|
|
|
|
|
private readonly List<float> heightCache = new List<float>();
|
|
|
|
|
|
|
|
|
|
public float TotalHeight => totalHeight;
|
|
|
|
|
private float totalHeight;
|
|
|
|
|
|
|
|
|
|
private static readonly float defaultCellHeight = 25f;
|
|
|
|
|
|
|
|
|
|
public float this[int index]
|
|
|
|
|
{
|
|
|
|
|
get => heightCache[index];
|
|
|
|
|
set => OnSetIndex(index, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Add(float value)
|
|
|
|
|
{
|
|
|
|
|
heightCache.Add(0f);
|
|
|
|
|
OnSetIndex(heightCache.Count - 1, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
heightCache.Clear();
|
|
|
|
|
totalHeight = 0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnSetIndex(int index, float value)
|
|
|
|
|
{
|
|
|
|
|
if (index >= heightCache.Count)
|
|
|
|
|
{
|
|
|
|
|
while (index > heightCache.Count)
|
2021-04-19 23:47:25 +10:00
|
|
|
|
Add(defaultCellHeight);
|
2021-04-19 20:08:07 +10:00
|
|
|
|
Add(value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var curr = heightCache[index];
|
|
|
|
|
if (curr.Equals(value))
|
|
|
|
|
return;
|
|
|
|
|
var diff = value - curr;
|
|
|
|
|
totalHeight += diff;
|
|
|
|
|
heightCache[index] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// internal class used to track and manage cell views
|
|
|
|
|
public class CachedCell
|
|
|
|
|
{
|
|
|
|
|
public ScrollPool Pool { get; } // reference to this scrollpool
|
|
|
|
|
public RectTransform Rect { get; } // the Rect (actual UI object)
|
|
|
|
|
public ICell Cell { get; } // the ICell (to interface with DataSource)
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
{
|
|
|
|
|
this.Pool = pool;
|
|
|
|
|
this.Rect = rect;
|
|
|
|
|
this.Cell = cell;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-17 04:11:45 +10:00
|
|
|
|
public ScrollPool(ScrollRect scrollRect)
|
2021-04-16 02:48:49 +10:00
|
|
|
|
{
|
|
|
|
|
this.scrollRect = scrollRect;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
public bool AutoResizeHandleRect { get; set; }
|
|
|
|
|
public float ExtraPoolCoverageRatio = 1.3f;
|
|
|
|
|
|
2021-04-17 04:11:45 +10:00
|
|
|
|
public IPoolDataSource DataSource;
|
2021-04-19 20:08:07 +10:00
|
|
|
|
public RectTransform PrototypeCell;
|
|
|
|
|
private float DefaultCellHeight => PrototypeCell?.rect.height ?? 25f;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
// UI
|
2021-04-16 02:48:49 +10:00
|
|
|
|
|
|
|
|
|
public override GameObject UIRoot => scrollRect.gameObject;
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
public RectTransform Viewport => scrollRect.viewport;
|
|
|
|
|
public RectTransform Content => scrollRect.content;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
internal Slider slider;
|
2021-04-16 02:48:49 +10:00
|
|
|
|
internal ScrollRect scrollRect;
|
2021-04-19 20:08:07 +10:00
|
|
|
|
internal VerticalLayoutGroup contentLayout;
|
2021-04-16 02:48:49 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
// Cache / tracking
|
2021-04-18 21:38:09 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
/// <summary>Extra clearance height relative to Viewport height, based on <see cref="ExtraPoolCoverageRatio"/>.</summary>
|
|
|
|
|
private Vector2 RecycleViewBounds;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private readonly HeightCache DataHeightCache = new HeightCache();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The first and last pooled indices relative to the DataSource's list
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int bottomDataIndex;
|
|
|
|
|
private int TopDataIndex => bottomDataIndex - CellPool.Count + 1;
|
|
|
|
|
|
|
|
|
|
private readonly List<CachedCell> CellPool = new List<CachedCell>();
|
|
|
|
|
|
|
|
|
|
public float AdjustedTotalCellHeight => TotalCellHeight + (contentLayout.spacing * (CellPool.Count - 1));
|
|
|
|
|
internal float TotalCellHeight
|
|
|
|
|
{
|
|
|
|
|
get => m_totalCellHeight;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (TotalCellHeight.Equals(value))
|
|
|
|
|
return;
|
|
|
|
|
m_totalCellHeight = value;
|
2021-04-19 23:47:25 +10:00
|
|
|
|
//SetContentHeight();
|
2021-04-19 20:08:07 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private float m_totalCellHeight;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 17:49:05 +10:00
|
|
|
|
/// <summary>
|
2021-04-19 20:08:07 +10:00
|
|
|
|
/// The first and last indices of our CellPool in the transform heirarchy
|
2021-04-16 17:49:05 +10:00
|
|
|
|
/// </summary>
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private int topPoolCellIndex, bottomPoolIndex;
|
|
|
|
|
|
|
|
|
|
private int CurrentDataCount => bottomDataIndex + 1;
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
private Vector2 _prevAnchoredPos;
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private Vector2 _prevViewportSize; // TODO track viewport height and rebuild on change
|
|
|
|
|
|
2021-04-19 23:47:25 +10:00
|
|
|
|
#region Internal set tracking and update
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 23:47:25 +10:00
|
|
|
|
// A sanity check so only one thing is setting the value per frame.
|
|
|
|
|
public bool WritingLocked
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 23:47:25 +10:00
|
|
|
|
get => writingLocked;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
internal set
|
|
|
|
|
{
|
2021-04-19 23:47:25 +10:00
|
|
|
|
if (writingLocked == value)
|
2021-04-15 20:18:03 +10:00
|
|
|
|
return;
|
2021-04-19 23:47:25 +10:00
|
|
|
|
timeofLastWriteLock = Time.time;
|
|
|
|
|
writingLocked = value;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-19 23:47:25 +10:00
|
|
|
|
private bool writingLocked;
|
|
|
|
|
private float timeofLastWriteLock;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 02:48:49 +10:00
|
|
|
|
public override void Update()
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 23:47:25 +10:00
|
|
|
|
if (writingLocked && timeofLastWriteLock < Time.time)
|
|
|
|
|
writingLocked = false;
|
2021-04-16 02:48:49 +10:00
|
|
|
|
}
|
2021-04-19 20:08:07 +10:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
// Initialize
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
public void Rebuild()
|
2021-04-16 02:48:49 +10:00
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
Initialize(DataSource);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
public void Initialize(IPoolDataSource dataSource)
|
2021-04-16 17:49:05 +10:00
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
DataSource = dataSource;
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
this.contentLayout = scrollRect.content.GetComponent<VerticalLayoutGroup>();
|
|
|
|
|
this.slider = scrollRect.GetComponentInChildren<Slider>();
|
|
|
|
|
slider.onValueChanged.AddListener(OnSliderValueChanged);
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
scrollRect.vertical = true;
|
|
|
|
|
scrollRect.horizontal = false;
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
scrollRect.onValueChanged.RemoveListener(OnValueChangedListener);
|
|
|
|
|
RuntimeProvider.Instance.StartCoroutine(InitCoroutine());
|
2021-04-16 17:49:05 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private IEnumerator InitCoroutine()
|
2021-04-16 17:49:05 +10:00
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
scrollRect.content.anchoredPosition = Vector2.zero;
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
yield return null;
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
_prevAnchoredPos = scrollRect.content.anchoredPosition;
|
|
|
|
|
_prevViewportSize = new Vector2(scrollRect.viewport.rect.width, scrollRect.viewport.rect.height);
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
SetRecycleViewBounds();
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
BuildInitialHeightCache();
|
|
|
|
|
CreateCellPool();
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 23:47:25 +10:00
|
|
|
|
//SetContentHeight();
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
UpdateSliderPositionAndSize();
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
scrollRect.onValueChanged.AddListener(OnValueChangedListener);
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private void BuildInitialHeightCache()
|
|
|
|
|
{
|
|
|
|
|
DataHeightCache.Clear();
|
|
|
|
|
float defaultHeight = DefaultCellHeight;
|
|
|
|
|
for (int i = 0; i < DataSource.ItemCount; i++)
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
if (i < CellPool.Count)
|
|
|
|
|
DataHeightCache.Add(CellPool[i].Height);
|
|
|
|
|
else
|
|
|
|
|
DataHeightCache.Add(defaultHeight);
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private void SetRecycleViewBounds()
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
var extra = (Viewport.rect.height * ExtraPoolCoverageRatio) - Viewport.rect.height;
|
|
|
|
|
extra *= 0.5f;
|
|
|
|
|
RecycleViewBounds = new Vector2(Viewport.MinY() + extra, Viewport.MaxY() - extra);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
// Refresh methods
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private struct CellInfo { public int cellIndex, dataIndex; }
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private IEnumerator<CellInfo> GetPoolEnumerator()
|
|
|
|
|
{
|
|
|
|
|
int cellIdx = topPoolCellIndex;
|
|
|
|
|
int dataIndex = TopDataIndex;
|
|
|
|
|
int iterated = 0;
|
|
|
|
|
while (iterated < CellPool.Count)
|
|
|
|
|
{
|
|
|
|
|
yield return new CellInfo()
|
|
|
|
|
{
|
|
|
|
|
cellIndex = cellIdx,
|
|
|
|
|
dataIndex = dataIndex
|
|
|
|
|
};
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
cellIdx++;
|
|
|
|
|
if (cellIdx >= CellPool.Count)
|
|
|
|
|
cellIdx = 0;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
dataIndex++;
|
|
|
|
|
iterated++;
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
public void RefreshCells(bool andReloadFromDataSource = false)
|
|
|
|
|
{
|
|
|
|
|
if (!CellPool.Any()) return;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
SetRecycleViewBounds();
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
bool jumpToBottom = false;
|
|
|
|
|
if (andReloadFromDataSource)
|
|
|
|
|
{
|
|
|
|
|
int count = DataSource.ItemCount;
|
|
|
|
|
if (bottomDataIndex > count)
|
|
|
|
|
{
|
|
|
|
|
bottomDataIndex = Math.Max(count - 1, CellPool.Count - 1);
|
|
|
|
|
jumpToBottom = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
var enumerator = GetPoolEnumerator();
|
|
|
|
|
while (enumerator.MoveNext())
|
|
|
|
|
{
|
|
|
|
|
var curr = enumerator.Current;
|
|
|
|
|
var cell = CellPool[curr.cellIndex];
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
if (andReloadFromDataSource)
|
|
|
|
|
SetCell(cell, curr.dataIndex);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cell.Height = cell.Rect.rect.height;
|
|
|
|
|
DataHeightCache[curr.dataIndex] = cell.Height;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
SetRecycleViewBounds();
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
if (andReloadFromDataSource)
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
RecycleBottomToTop();
|
|
|
|
|
RecycleTopToBottom();
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
UpdateSliderPositionAndSize();
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
if (jumpToBottom)
|
|
|
|
|
{
|
|
|
|
|
var diff = Viewport.MaxY() - CellPool[bottomPoolIndex].Rect.MaxY();
|
|
|
|
|
Content.anchoredPosition += Vector2.up * diff;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private void SetCell(CachedCell cachedCell, int dataIndex)
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
cachedCell.Cell.Enable();
|
|
|
|
|
DataSource.SetCell(cachedCell.Cell, dataIndex);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
cachedCell.Height = cachedCell.Cell.Enabled ? cachedCell.Rect.rect.height : 0f;
|
|
|
|
|
DataHeightCache[dataIndex] = cachedCell.Height;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
// Cell pool
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
|
|
|
|
private void CreateCellPool()
|
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
if (CellPool.Any())
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
foreach (var cell in CellPool)
|
|
|
|
|
GameObject.Destroy(cell.Rect.gameObject);
|
|
|
|
|
CellPool.Clear();
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
2021-04-19 20:08:07 +10:00
|
|
|
|
|
|
|
|
|
if (!PrototypeCell)
|
2021-04-19 23:47:25 +10:00
|
|
|
|
throw new Exception("No prototype cell set, cannot initialize");
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
|
|
|
|
//Set the prototype cell active and set cell anchor as top
|
|
|
|
|
PrototypeCell.gameObject.SetActive(true);
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
float currentPoolCoverage = 0f;
|
|
|
|
|
float requiredCoverage = scrollRect.viewport.rect.height * ExtraPoolCoverageRatio;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
topPoolCellIndex = 0;
|
|
|
|
|
bottomPoolIndex = -1;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
// create cells until the Pool area is covered.
|
|
|
|
|
// use minimum default height so that maximum pool count is reached.
|
2021-04-15 20:18:03 +10:00
|
|
|
|
while (currentPoolCoverage < requiredCoverage)
|
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
bottomPoolIndex++;
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
//Instantiate and add to Pool
|
2021-04-19 20:08:07 +10:00
|
|
|
|
RectTransform rect = GameObject.Instantiate(PrototypeCell.gameObject).GetComponent<RectTransform>();
|
|
|
|
|
rect.name = $"Cell_{CellPool.Count + 1}";
|
|
|
|
|
var cell = DataSource.CreateCell(rect);
|
|
|
|
|
CellPool.Add(new CachedCell(this, rect, cell));
|
|
|
|
|
rect.SetParent(scrollRect.content, false);
|
|
|
|
|
|
2021-04-19 23:47:25 +10:00
|
|
|
|
cell.Disable();
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
currentPoolCoverage += rect.rect.height + this.contentLayout.spacing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bottomDataIndex = bottomPoolIndex;
|
|
|
|
|
|
|
|
|
|
// after creating pool, set displayed cells.
|
|
|
|
|
for (int i = 0; i < CellPool.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var cell = CellPool[i];
|
|
|
|
|
SetCell(cell, i);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Deactivate prototype cell if it is not a prefab(i.e it's present in scene)
|
|
|
|
|
if (PrototypeCell.gameObject.scene.IsValid())
|
|
|
|
|
PrototypeCell.gameObject.SetActive(false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
// Value change processor
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private void OnValueChangedListener(Vector2 val)
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 23:47:25 +10:00
|
|
|
|
if (WritingLocked)
|
2021-04-19 20:08:07 +10:00
|
|
|
|
return;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
SetRecycleViewBounds();
|
|
|
|
|
RefreshCells();
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
float yChange = (scrollRect.content.anchoredPosition - _prevAnchoredPos).y;
|
|
|
|
|
float adjust = 0f;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
if (yChange > 0) // Scrolling down
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
if (ShouldRecycleTop)
|
|
|
|
|
adjust = RecycleTopToBottom();
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
2021-04-19 20:08:07 +10:00
|
|
|
|
else if (yChange < 0) // Scrolling up
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
if (ShouldRecycleBottom)
|
|
|
|
|
adjust = RecycleBottomToTop();
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
var vector = new Vector2(0, adjust);
|
|
|
|
|
scrollRect.m_ContentStartPosition += vector;
|
|
|
|
|
scrollRect.m_PrevPosition += vector;
|
|
|
|
|
|
|
|
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
|
|
|
|
|
_prevAnchoredPos = scrollRect.content.anchoredPosition;
|
|
|
|
|
|
|
|
|
|
UpdateSliderPositionAndSize();
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private bool ShouldRecycleTop => GetCellExtent(CellPool[topPoolCellIndex]) >= RecycleViewBounds.x
|
|
|
|
|
&& CellPool[bottomPoolIndex].Rect.position.y > RecycleViewBounds.y;
|
|
|
|
|
|
|
|
|
|
private bool ShouldRecycleBottom => GetCellExtent(CellPool[bottomPoolIndex]) < RecycleViewBounds.y
|
|
|
|
|
&& CellPool[topPoolCellIndex].Rect.position.y < RecycleViewBounds.x;
|
|
|
|
|
|
|
|
|
|
private float GetCellExtent(CachedCell cell) => cell.Rect.MaxY() - contentLayout.spacing;
|
|
|
|
|
|
|
|
|
|
private float RecycleTopToBottom()
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 23:47:25 +10:00
|
|
|
|
WritingLocked = true;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
float recycledheight = 0f;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
while (ShouldRecycleTop && CurrentDataCount < DataSource.ItemCount)
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 20:08:07 +10:00
|
|
|
|
var cell = CellPool[topPoolCellIndex];
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
//Move top cell to bottom
|
2021-04-19 20:08:07 +10:00
|
|
|
|
cell.Rect.SetAsLastSibling();
|
|
|
|
|
var prevHeight = cell.Rect.rect.height;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
// update content position
|
|
|
|
|
Content.anchoredPosition -= Vector2.up * prevHeight;
|
|
|
|
|
recycledheight += prevHeight + contentLayout.spacing;
|
|
|
|
|
|
|
|
|
|
//set Cell
|
|
|
|
|
SetCell(cell, CurrentDataCount);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
|
|
|
|
//set new indices
|
2021-04-19 20:08:07 +10:00
|
|
|
|
bottomDataIndex++;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
bottomPoolIndex = topPoolCellIndex;
|
|
|
|
|
topPoolCellIndex = (topPoolCellIndex + 1) % CellPool.Count;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
return -recycledheight;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private float RecycleBottomToTop()
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 23:47:25 +10:00
|
|
|
|
WritingLocked = true;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
float recycledheight = 0f;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
while (ShouldRecycleBottom && CurrentDataCount > CellPool.Count)
|
|
|
|
|
{
|
|
|
|
|
var cell = CellPool[bottomPoolIndex];
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
//Move bottom cell to top
|
|
|
|
|
cell.Rect.SetAsFirstSibling();
|
|
|
|
|
var prevHeight = cell.Rect.rect.height;
|
|
|
|
|
|
|
|
|
|
// update content position
|
|
|
|
|
Content.anchoredPosition += Vector2.up * prevHeight;
|
|
|
|
|
recycledheight += prevHeight + contentLayout.spacing;
|
|
|
|
|
|
|
|
|
|
//set new index
|
|
|
|
|
bottomDataIndex--;
|
|
|
|
|
|
|
|
|
|
//set Cell
|
|
|
|
|
SetCell(cell, TopDataIndex);
|
|
|
|
|
|
|
|
|
|
// move content again for new cell size
|
|
|
|
|
var newHeight = cell.Rect.rect.height;
|
|
|
|
|
var diff = newHeight - prevHeight;
|
|
|
|
|
if (diff != 0.0f)
|
|
|
|
|
{
|
|
|
|
|
Content.anchoredPosition += Vector2.up * diff;
|
|
|
|
|
recycledheight += diff;
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
|
|
|
|
//set new indices
|
2021-04-19 20:08:07 +10:00
|
|
|
|
topPoolCellIndex = bottomPoolIndex;
|
|
|
|
|
bottomPoolIndex = (bottomPoolIndex - 1 + CellPool.Count) % CellPool.Count;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
return recycledheight;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
// Slider
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private void UpdateSliderPositionAndSize()
|
|
|
|
|
{
|
|
|
|
|
int total = DataSource.ItemCount;
|
|
|
|
|
total = Math.Max(total, 1);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
// NAIVE TEMP DEBUG - all cells will NOT be the same height!
|
|
|
|
|
|
|
|
|
|
var spread = CellPool.Count(it => it.Cell.Enabled);
|
|
|
|
|
|
|
|
|
|
// TODO temp debug
|
|
|
|
|
bool forceValue = true;
|
|
|
|
|
if (forceValue)
|
|
|
|
|
{
|
|
|
|
|
if (spread >= total)
|
|
|
|
|
slider.value = 0f;
|
|
|
|
|
else
|
|
|
|
|
slider.value = (float)((decimal)TopDataIndex / Math.Max(1, total - CellPool.Count));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (AutoResizeHandleRect)
|
|
|
|
|
{
|
|
|
|
|
var viewportHeight = scrollRect.viewport.rect.height;
|
|
|
|
|
|
|
|
|
|
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 OnSliderValueChanged(float val)
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-19 23:47:25 +10:00
|
|
|
|
if (this.WritingLocked)
|
2021-04-19 20:08:07 +10:00
|
|
|
|
return;
|
2021-04-19 23:47:25 +10:00
|
|
|
|
this.WritingLocked = true;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
// TODO this cant work until we have a cache of all data heights.
|
|
|
|
|
// will need to maintain that as we go and assume default height for indeterminate cells.
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
private void JumpToIndex(int dataIndex)
|
|
|
|
|
{
|
|
|
|
|
// TODO this cant work until we have a cache of all data heights.
|
|
|
|
|
// will need to maintain that as we go and assume default height for indeterminate cells.
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:08:07 +10:00
|
|
|
|
|
|
|
|
|
/// <summary>Use <see cref="UIFactory.CreateScrollPool"/></summary>
|
|
|
|
|
public override void ConstructUI(GameObject parent) => throw new NotImplementedException();
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
}
|