replace old scrollpool with new dynamic one, more progress

This commit is contained in:
Sinai 2021-04-19 20:08:07 +10:00
parent 8b5e385c28
commit 7a253fa0a0
15 changed files with 1262 additions and 1281 deletions

View File

@ -1,313 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.Core.Config;
using UnityExplorer.Core.Runtime;
using UnityExplorer.UI.Models;
using UnityExplorer.UI.Utility;
using UnityExplorer.UI.Widgets;
namespace UnityExplorer.UI.Panels
{
public class GameObjectInspector : UIPanel
{
public override string Name => "GameObject Inspector";
//public SimpleListSource<Component> ComponentList;
public override void Update()
{
}
public List<Component> GetEntries()
{
var comp = Camera.main;
return new List<Component>
{
comp, comp, comp, comp, comp
};
}
public void OnCellClicked(SimpleCell<Component> cell)
{
ExplorerCore.Log("Cell clicked!");
}
//public void SetCell(SimpleCell<Component> cell, int index)
//{
// var comp = ComponentList.currentEntries[index];
// if (!comp)
// cell.buttonText.text = "<color=red>[Destroyed]</color>";
// else
// cell.buttonText.text = ToStringUtility.GetDefaultLabel(comp, ReflectionProvider.Instance.GetActualType(comp), true, false);
//}
public bool ShouldDisplay(Component comp, string filter)
{
return comp.name.ToLower().Contains(filter);
}
public override void LoadSaveData()
{
ApplySaveData(ConfigManager.GameObjectInspectorData.Value);
}
public override void SaveToConfigManager()
{
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.Refresh();
}
public override void SetDefaultPosAndAnchors()
{
mainPanelRect.localPosition = Vector2.zero;
mainPanelRect.anchorMin = new Vector2(1, 0);
mainPanelRect.anchorMax = new Vector2(1, 1);
mainPanelRect.sizeDelta = new Vector2(-300f, mainPanelRect.sizeDelta.y);
mainPanelRect.anchoredPosition = new Vector2(-200, 0);
mainPanelRect.offsetMin = new Vector2(mainPanelRect.offsetMin.x, 100); // bottom
mainPanelRect.offsetMax = new Vector2(mainPanelRect.offsetMax.x, -50); // top
mainPanelRect.pivot = new Vector2(0.5f, 0.5f);
}
private DynamicScrollPool scrollPool;
public override void ConstructPanelContent()
{
// temp debug
scrollPool = UIFactory.CreateDynamicScrollPool(content, "Test", out GameObject scrollObj,
out GameObject scrollContent, new Color(0.15f, 0.15f, 0.15f));
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
UIFactory.SetLayoutElement(scrollContent, flexibleHeight: 9999);
var test = new DynamicListTest(scrollPool);
test.Init();
var prototype = DynamicCellTest.CreatePrototypeCell(scrollContent);
scrollPool.PrototypeCell = prototype.GetComponent<RectTransform>();
//// Component list
//var scrollPool = (ScrollPool)UIFactory.CreateScrollPool<ScrollPool>(content, "ComponentList", out GameObject scrollObj,
// out GameObject scrollContent, new Color(0.15f, 0.15f, 0.15f));
//UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
//UIFactory.SetLayoutElement(scrollContent, flexibleHeight: 9999);
//ComponentList = new SimpleListSource<Component>(scrollPool, GetEntries, CreateCell, SetCell, ShouldDisplay);
//ComponentList.Init();
//// Prototype tree cell
//var prototype = SimpleCell<Component>.CreatePrototypeCell(scrollContent);
//scrollPool.PrototypeCell = prototype.GetComponent<RectTransform>();
previousRectHeight = mainPanelRect.rect.height;
}
}
public class DynamicListTest : IDynamicDataSource
{
public struct Data
{
public float height; public Color color;
public Data(float f, Color c) { height = f; color = c; }
};
public int ItemCount => imaginaryData.Count;
public float DefaultCellHeight => 25f;
// private List<DynamicCellTest> cellCache = new List<DynamicCellTest>();
private List<Data> imaginaryData;
internal DynamicScrollPool Scroller;
public DynamicListTest(DynamicScrollPool scroller) { Scroller = scroller; }
public void Init()
{
imaginaryData = new List<Data>();
for (int i = 0; i < 100; i++)
{
imaginaryData.Add(new Data
{
height = (UnityEngine.Random.Range(25f, 800f)
+ UnityEngine.Random.Range(25f, 50f)
+ 50f)
* 0.25f,
//height = 25f,
color = new Color
{
r = UnityEngine.Random.Range(0.1f, 0.8f),
g = UnityEngine.Random.Range(0.1f, 0.8f),
b = UnityEngine.Random.Range(0.1f, 0.8f),
a = 1
}
});
}
RuntimeProvider.Instance.StartCoroutine(InitCoroutine());
}
private IEnumerator InitCoroutine()
{
yield return null;
//RefreshData();
Scroller.DataSource = this;
Scroller.Initialize(this);
}
public ICell CreateCell(RectTransform cellTransform)
{
var cell = new DynamicCellTest(cellTransform.gameObject,
cellTransform.GetComponentInChildren<Image>(),
cellTransform.Find("Label").GetComponent<Text>());
return cell;
}
//public float GetHeightForCell(int index)
//{
// if (index < 0 || index >= imaginaryData.Count)
// return 0f;
// return imaginaryData[index].height;
//}
//public float GetTotalHeight()
//{
// float ret = 0f;
// foreach (var f in imaginaryData)
// ret += f.height;
// return ret;
//}
public void SetCell(ICell icell, int index)
{
if (index < 0 || index >= imaginaryData.Count)
{
icell.Disable();
return;
}
var cell = icell as DynamicCellTest;
var data = imaginaryData[index];
cell.image.color = data.color;
cell.text.text = $"{index}: {data.height}";
cell.Height = data.height;
}
}
public class DynamicCellTest : IDynamicCell
{
public DynamicScrollPool.CachedCell Cached;
public DynamicCellTest(GameObject uiRoot, Image image, Text text)
{
this.uiRoot = uiRoot;
this.image = image;
this.text = text;
var button = uiRoot.GetComponentInChildren<Button>();
var layout = uiRoot.GetComponent<LayoutElement>();
button.onClick.AddListener(() =>
{
if (!expanded)
{
layout.minHeight = this.Height;
expanded = true;
if (Cached != null) Cached.Height = Height;
}
else
{
this.Height = 25;// default height
layout.minHeight = Height;
expanded = false;
if (Cached != null) Cached.Height = Height;
}
});
}
private bool expanded;
public bool Enabled => m_enabled;
private bool m_enabled;
public float Height;
public GameObject uiRoot;
public Image image;
public Text text;
public LayoutGroup layoutGroup;
public Button button;
public void Disable()
{
m_enabled = false;
uiRoot.SetActive(false);
//image.color = Color.red;
}
public void Enable()
{
m_enabled = true;
uiRoot.SetActive(true);
}
public float GetHeight()
{
return Height;
}
public static GameObject CreatePrototypeCell(GameObject parent)
{
var prototype = UIFactory.CreateHorizontalGroup(parent, "PrototypeCell", true, true, true, true, 2, default,
new Color(0.15f, 0.15f, 0.15f), TextAnchor.MiddleCenter);
//var cell = prototype.AddComponent<TransformCell>();
var rect = prototype.GetComponent<RectTransform>();
rect.anchorMin = new Vector2(0, 1);
rect.anchorMax = new Vector2(0, 1);
rect.pivot = new Vector2(0.5f, 1);
rect.sizeDelta = new Vector2(25, 25);
UIFactory.SetLayoutElement(prototype, minWidth: 100, flexibleWidth: 9999, minHeight: 25, flexibleHeight: 0);
var text = UIFactory.CreateLabel(prototype, "Label", "notset", TextAnchor.MiddleCenter);
UIFactory.SetLayoutElement(text.gameObject, minHeight: 25, minWidth: 200, flexibleWidth: 0);
var button = UIFactory.CreateButton(prototype, "button", "Toggle");
UIFactory.SetLayoutElement(button.gameObject, minWidth: 50, flexibleWidth: 0, minHeight: 25);
prototype.SetActive(false);
return prototype;
}
}
}

View File

@ -0,0 +1,205 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.Core.Config;
using UnityExplorer.Core.Runtime;
using UnityExplorer.UI.Models;
using UnityExplorer.UI.Utility;
using UnityExplorer.UI.Widgets;
namespace UnityExplorer.UI.Panels
{
public class InspectorTest : UIPanel
{
public override string Name => "Inspector";
//public SimpleListSource<Component> ComponentList;
public override void Update()
{
}
public override void LoadSaveData()
{
ApplySaveData(ConfigManager.GameObjectInspectorData.Value);
}
public override void SaveToConfigManager()
{
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.Refresh();
}
public override void SetDefaultPosAndAnchors()
{
mainPanelRect.localPosition = Vector2.zero;
mainPanelRect.anchorMin = new Vector2(1, 0);
mainPanelRect.anchorMax = new Vector2(1, 1);
mainPanelRect.sizeDelta = new Vector2(-300f, mainPanelRect.sizeDelta.y);
mainPanelRect.anchoredPosition = new Vector2(-200, 0);
mainPanelRect.offsetMin = new Vector2(mainPanelRect.offsetMin.x, 100); // bottom
mainPanelRect.offsetMax = new Vector2(mainPanelRect.offsetMax.x, -50); // top
mainPanelRect.pivot = new Vector2(0.5f, 0.5f);
}
private ScrollPool scrollPool;
public override void ConstructPanelContent()
{
//UIRoot.GetComponent<Mask>().enabled = false;
// temp debug
scrollPool = UIFactory.CreateScrollPool(content, "Test", out GameObject scrollObj,
out GameObject scrollContent, new Color(0.15f, 0.15f, 0.15f));
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
UIFactory.SetLayoutElement(scrollContent, flexibleHeight: 9999);
var test = new DynamicListTest(scrollPool, this);
test.Init();
var prototype = DynamicCellTest.CreatePrototypeCell(scrollContent);
scrollPool.PrototypeCell = prototype.GetComponent<RectTransform>();
dummyContentHolder = new GameObject("DummyHolder");
dummyContentHolder.SetActive(false);
GameObject.DontDestroyOnLoad(dummyContentHolder);
for (int i = 0; i < 10; i++)
{
dummyContents.Add(CreateDummyContent());
}
previousRectHeight = mainPanelRect.rect.height;
}
internal GameObject dummyContentHolder;
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));
UIFactory.SetLayoutElement(obj, minHeight: 25, flexibleHeight: 9999);
var label = UIFactory.CreateLabel(obj, "label", "Dummy " + dummyContents.Count, TextAnchor.MiddleCenter);
UIFactory.SetLayoutElement(label.gameObject, minHeight: 25, flexibleHeight: 0);
var inputObj = UIFactory.CreateInputField(obj, "input", "...", out InputField inputField);
inputField.lineType = InputField.LineType.MultiLineNewline;
UIFactory.SetLayoutElement(inputObj, minHeight: 25, flexibleHeight: 9999);
//inputObj.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
return obj;
}
}
public class DynamicListTest : IPoolDataSource
{
internal ScrollPool Scroller;
internal InspectorTest Inspector;
public DynamicListTest(ScrollPool scroller, InspectorTest inspector)
{
Scroller = scroller;
Inspector = inspector;
}
public int ItemCount => Inspector.dummyContents.Count;
public void Init()
{
Scroller.DataSource = this;
Scroller.Initialize(this);
}
public ICell CreateCell(RectTransform cellTransform) => new DynamicCellTest(cellTransform.gameObject);
public void SetCell(ICell icell, int index)
{
if (index < 0 || index >= ItemCount)
{
icell.Disable();
return;
}
var root = (icell as DynamicCellTest).uiRoot;
if (root.transform.Find("Content") is Transform existing)
{
ExplorerCore.Log("removing existing content");
existing.transform.SetParent(Inspector.dummyContentHolder.transform, false);
}
var content = Inspector.dummyContents[index];
content.transform.SetParent(root.transform, false);
}
}
public class DynamicCellTest : ICell
{
public DynamicCellTest(GameObject uiRoot)
{
this.uiRoot = uiRoot;
}
public bool Enabled => m_enabled;
private bool m_enabled;
public GameObject uiRoot;
public InputField input;
public void Disable()
{
m_enabled = false;
uiRoot.SetActive(false);
}
public void Enable()
{
m_enabled = true;
uiRoot.SetActive(true);
}
public static GameObject CreatePrototypeCell(GameObject parent)
{
var prototype = UIFactory.CreateVerticalGroup(parent, "PrototypeCell", true, true, true, true, 2, default,
new Color(0.15f, 0.15f, 0.15f), TextAnchor.MiddleCenter);
var rect = prototype.GetComponent<RectTransform>();
rect.anchorMin = new Vector2(0, 1);
rect.anchorMax = new Vector2(0, 1);
rect.pivot = new Vector2(0.5f, 1);
rect.sizeDelta = new Vector2(25, 25);
UIFactory.SetLayoutElement(prototype, minWidth: 100, flexibleWidth: 9999, minHeight: 25, flexibleHeight: 9999);
prototype.SetActive(false);
return prototype;
}
}
}

View File

@ -56,9 +56,9 @@ namespace UnityExplorer.UI.Panels
public void ExpensiveUpdate()
{
Tree.Scroller.ExternallySetting = true;
//Tree.Scroller.ExternallySetting = true;
SceneHandler.Update();
Tree.RefreshData(true);
//Tree.RefreshData(true);
// Tree.Scroller.ExternallySetting = false;
}
@ -144,9 +144,9 @@ namespace UnityExplorer.UI.Panels
{
// height increased, hard refresh required.
highestRectHeight = obj.rect.height;
Tree.Scroller.ReloadData();
//Tree.Scroller.ReloadData();
}
Tree.Scroller.Refresh();
Tree.Scroller.RefreshCells(true);
}
public override void SaveToConfigManager()
@ -224,21 +224,21 @@ namespace UnityExplorer.UI.Panels
// Transform Tree
var infiniteScroll = UIFactory.CreateScrollPool<ScrollPool>(content, "TransformTree", out GameObject scrollObj,
var infiniteScroll = 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);
// Prototype tree cell
var prototype = TransformCell.CreatePrototypeCell(scrollContent);
infiniteScroll.PrototypeCell = prototype.GetComponent<RectTransform>();
Tree = new TransformTree(infiniteScroll)
{
GetRootEntriesMethod = GetRootEntries
};
Tree.Init();
// Prototype tree cell
var prototype = TransformCell.CreatePrototypeCell(scrollContent);
infiniteScroll.PrototypeCell = prototype.GetComponent<RectTransform>();
// some references
highestRectHeight = mainPanelRect.rect.height;

View File

@ -693,22 +693,8 @@ namespace UnityExplorer.UI
return dropdownObj;
}
public static DynamicScrollPool CreateDynamicScrollPool(GameObject parent, string name, out GameObject uiRoot,
out GameObject content, Color? bgColor, bool autoResizeSliderHandle = true)
{
var pool = CreateScrollPool<DynamicScrollPool>(parent, name, out uiRoot, out content, bgColor, autoResizeSliderHandle);
SetLayoutGroup<VerticalLayoutGroup>(content, true, false, true, true, 2, 2, 2, 2, 2,
TextAnchor.UpperCenter);
var rect = content.GetComponent<RectTransform>();
rect.pivot = new Vector2(0.5f, 1f);
return pool;
}
public static T CreateScrollPool<T>(GameObject parent, string name, out GameObject uiRoot,
out GameObject content, Color? bgColor = null, bool autoResizeSliderHandle = true) where T : IScrollPool
public static ScrollPool CreateScrollPool(GameObject parent, string name, out GameObject uiRoot,
out GameObject content, Color? bgColor = null, bool autoResizeSliderHandle = true)
{
var mainObj = CreateUIObject(name, parent, new Vector2(1, 1));
mainObj.AddComponent<Image>().color = bgColor ?? new Color(0.12f, 0.12f, 0.12f);
@ -753,6 +739,8 @@ namespace UnityExplorer.UI
slider.direction = Slider.Direction.TopToBottom;
SetLayoutElement(sliderObj, minWidth: 25, flexibleWidth: 0, flexibleHeight: 9999);
RuntimeProvider.Instance.SetColorBlock(slider, disabled: new Color(0.1f, 0.1f, 0.1f));
if (autoResizeSliderHandle)
{
slider.handleRect.offsetMin = new Vector2(slider.handleRect.offsetMin.x, 0);
@ -767,8 +755,18 @@ namespace UnityExplorer.UI
uiRoot = mainObj;
var scrollPool = (T)Activator.CreateInstance(typeof(T), new object[] { scrollRect });
scrollPool.AutoResizeHandleRect = autoResizeSliderHandle;
var scrollPool = new ScrollPool(scrollRect)
{
AutoResizeHandleRect = autoResizeSliderHandle
};
SetLayoutGroup<VerticalLayoutGroup>(content, true, false, true, false, 0, 2, 2, 2, 2,
TextAnchor.UpperCenter);
//viewportObj.GetComponent<Mask>().enabled = false;
var rect = content.GetComponent<RectTransform>();
rect.pivot = new Vector2(0.5f, 1f);
return scrollPool;
}

View File

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

View File

@ -7,20 +7,20 @@ using UnityEngine.UI;
namespace UnityExplorer.UI.Widgets
{
public class SimpleCell<T> : ICell
public class ButtonCell<T> : ICell
{
public bool Enabled => m_enabled;
private bool m_enabled;
public Action<SimpleCell<T>> OnClick;
public Action<ButtonCell<T>> OnClick;
public SimpleListSource<T> list;
public ButtonListCell<T> list;
public GameObject uiRoot;
public Text buttonText;
public Button button;
public SimpleCell(SimpleListSource<T> list, GameObject uiRoot, Button button, Text text)
public ButtonCell(ButtonListCell<T> list, GameObject uiRoot, Button button, Text text)
{
this.list = list;
this.uiRoot = uiRoot;

View File

@ -9,7 +9,7 @@ using UnityExplorer.UI.Widgets;
namespace UnityExplorer.UI.Widgets
{
public class SimpleListSource<T> : IPoolDataSource
public class ButtonListCell<T> : IPoolDataSource
{
internal ScrollPool Scroller;
@ -17,9 +17,9 @@ namespace UnityExplorer.UI.Widgets
public List<T> currentEntries;
public Func<List<T>> GetEntries;
public Action<SimpleCell<T>, int> SetICell;
public Action<ButtonCell<T>, int> SetICell;
public Func<T, string, bool> ShouldDisplay;
public Action<SimpleCell<T>> OnCellClicked;
public Action<ButtonCell<T>> OnCellClicked;
public string CurrentFilter
{
@ -28,9 +28,9 @@ namespace UnityExplorer.UI.Widgets
}
private string currentFilter;
public SimpleListSource(ScrollPool infiniteScroller, Func<List<T>> getEntriesMethod,
Action<SimpleCell<T>, int> setICellMethod, Func<T, string, bool> shouldDisplayMethod,
Action<SimpleCell<T>> onCellClickedMethod)
public ButtonListCell(ScrollPool infiniteScroller, Func<List<T>> getEntriesMethod,
Action<ButtonCell<T>, int> setICellMethod, Func<T, string, bool> shouldDisplayMethod,
Action<ButtonCell<T>> onCellClickedMethod)
{
Scroller = infiniteScroller;
@ -79,7 +79,7 @@ namespace UnityExplorer.UI.Widgets
{
var button = rect.GetComponentInChildren<Button>();
var text = button.GetComponentInChildren<Text>();
var cell = new SimpleCell<T>(this, rect.gameObject, button, text);
var cell = new ButtonCell<T>(this, rect.gameObject, button, text);
cell.OnClick += OnCellClicked;
return cell;
}
@ -91,7 +91,7 @@ namespace UnityExplorer.UI.Widgets
else
{
cell.Enable();
SetICell.Invoke((SimpleCell<T>)cell, index);
SetICell.Invoke((ButtonCell<T>)cell, index);
}
}
}

View File

@ -1,487 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.UI.Models;
namespace UnityExplorer.UI.Widgets
{
/// <summary>
/// A <see cref="ScrollPool"/> for content with cells that vary in height, using VerticalLayoutGroup and LayoutElement.
/// </summary>
public class DynamicScrollPool : UIBehaviourModel, IScrollPool
{
// internal class used to track and manage cell views
public class CachedCell
{
public DynamicScrollPool Pool { get; } // reference to this scrollpool
public RectTransform Rect { get; } // the Rect (actual UI object)
public IDynamicCell 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(DynamicScrollPool pool, RectTransform rect, IDynamicCell cell)
{
this.Pool = pool;
this.Rect = rect;
this.Cell = cell;
// TODO temp debug
(cell as Panels.DynamicCellTest).Cached = this;
}
}
public DynamicScrollPool(ScrollRect scrollRect)
{
this.scrollRect = scrollRect;
}
public bool AutoResizeHandleRect { get; set; }
public float ExtraPoolCoverageRatio = 1.2f;
public IDynamicDataSource DataSource;
public RectTransform PrototypeCell;
// UI
public override GameObject UIRoot => scrollRect.gameObject;
public RectTransform Viewport => scrollRect.viewport;
public RectTransform Content => scrollRect.content;
internal Slider slider;
internal ScrollRect scrollRect;
internal VerticalLayoutGroup contentLayout;
// Cache / tracking
// 1.2x height of Viewport height.
private Vector2 RecycleViewBounds;
private readonly List<CachedCell> CellPool = new List<CachedCell>();
// private readonly List<Vector2> DataHeightCache = new List<Vector2>();
private readonly List<float> DataHeightCache = new List<float>();
public float AdjustedTotalCellHeight => TotalCellHeight + (contentLayout.spacing * (CellPool.Count - 1));
internal float TotalCellHeight
{
get => m_totalCellHeight;
set
{
if (TotalCellHeight.Equals(value))
return;
m_totalCellHeight = value;
SetDisplayedContentHeight();
}
}
private float m_totalCellHeight;
/// <summary>
/// The first and last displayed indexes relative to the DataSource's list
/// </summary>
private int topDataIndex, bottomDataIndex;
public bool IsDisplayed(int index) => index >= topDataIndex && index <= bottomDataIndex;
/// <summary>
/// For keeping track of where cellPool[0] and cellPool[last] actually are in the transform heirarchy
/// </summary>
private int topMostCellIndex, bottomMostCellIndex;
private int CurrentDataCount => bottomDataIndex + 1;
private Vector2 _prevAnchoredPos;
private Vector2 _prevViewportSize; // TODO track viewport height and rebuild on change? or leave that to datasource?
#region internal set tracking and update
//private bool _recycling;
public bool ExternallySetting
{
get => externallySetting;
internal set
{
if (externallySetting == value)
return;
timeOfLastExternalSet = Time.time;
externallySetting = value;
}
}
private bool externallySetting;
private float timeOfLastExternalSet;
public override void Update()
{
if (externallySetting && timeOfLastExternalSet < Time.time)
externallySetting = false;
}
#endregion
// Initialize
public void Initialize(IDynamicDataSource dataSource)
{
this.slider = scrollRect.GetComponentInChildren<Slider>();
slider.onValueChanged.AddListener(OnSliderValueChanged);
this.contentLayout = scrollRect.content.GetComponent<VerticalLayoutGroup>();
DataSource = dataSource;
scrollRect.vertical = true;
scrollRect.horizontal = false;
scrollRect.onValueChanged.RemoveListener(OnValueChangedListener);
RuntimeProvider.Instance.StartCoroutine(InitCoroutine());
}
private IEnumerator InitCoroutine()
{
scrollRect.content.anchoredPosition = Vector2.zero;
yield return null;
_prevAnchoredPos = scrollRect.content.anchoredPosition;
_prevViewportSize = new Vector2(scrollRect.viewport.rect.width, scrollRect.viewport.rect.height);
SetRecycleViewBounds();
//RefreshDataHeightCache();
CreateCellPool();
BuildInitialHeightCache();
SetDisplayedContentHeight();
UpdateSliderPositionAndSize();
scrollRect.onValueChanged.AddListener(OnValueChangedListener);
}
private void BuildInitialHeightCache()
{
float defaultHeight = DataSource.DefaultCellHeight;
for (int i = 0; i < DataSource.ItemCount; i++)
{
if (i < CellPool.Count)
DataHeightCache.Add(CellPool[i].Height);
else
DataHeightCache.Add(defaultHeight);
}
}
private float GetStartPositionOfData(int index)
{
float start = 0f;
for (int i = 0; i < index; i++)
start += DataHeightCache[i] + contentLayout.spacing;
return start;
}
// Refresh methods
// TODO need a quick refresh method / populate cells (?)
public void SetRecycleViewBounds()
{
var extra = (Viewport.rect.height * ExtraPoolCoverageRatio) - Viewport.rect.height;
RecycleViewBounds = new Vector2(Viewport.MinY() + extra, Viewport.MaxY() - extra);
}
public void SetDisplayedContentHeight()
{
var viewRect = scrollRect.viewport;
scrollRect.content.sizeDelta = new Vector2(
scrollRect.content.sizeDelta.x,
AdjustedTotalCellHeight - viewRect.rect.height);
}
public void UpdateDisplayedHeightCache()
{
if (!CellPool.Any()) return;
int cellIdx = topMostCellIndex;
int dataIndex = topDataIndex;
int iterated = 0;
while (iterated < CellPool.Count)
{
var cell = CellPool[cellIdx];
cellIdx++;
if (cellIdx >= CellPool.Count)
cellIdx = 0;
cell.Height = cell.Rect.rect.height;
DataHeightCache[dataIndex] = cell.Height;
dataIndex++;
iterated++;
}
}
// Cell pool
private void CreateCellPool()
{
//Reseting Pool
if (CellPool.Any())
{
foreach (var cell in CellPool)
GameObject.Destroy(cell.Rect.gameObject);
CellPool.Clear();
}
//Set the prototype cell active and set cell anchor as top
PrototypeCell.gameObject.SetActive(true);
//Temps
float currentPoolCoverage = 0f;
float requiredCoverage = scrollRect.viewport.rect.height * ExtraPoolCoverageRatio;
topMostCellIndex = 0;
topDataIndex = 0;
bottomMostCellIndex = -1;
// create cells until the Pool area is covered.
// use minimum default height so that maximum pool count is reached.
while (currentPoolCoverage < requiredCoverage)
{
bottomMostCellIndex++;
//Instantiate and add to Pool
RectTransform rect = GameObject.Instantiate(PrototypeCell.gameObject).GetComponent<RectTransform>();
rect.name = $"Cell_{CellPool.Count + 1}";
var cell = (IDynamicCell)DataSource.CreateCell(rect);
CellPool.Add(new CachedCell(this, rect, cell));
rect.SetParent(scrollRect.content, false);
currentPoolCoverage += rect.rect.height + this.contentLayout.spacing;
}
bottomDataIndex = bottomMostCellIndex;
// after creating pool, set displayed cells.
//posY = 0f;
for (int i = 0; i < CellPool.Count; i++)
{
var cell = CellPool[i];
SetCell(cell, i);
}
//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);
}
private void SetCell(CachedCell cell, int dataIndex)
{
cell.Cell.Enable();
DataSource.SetCell(cell.Cell, dataIndex);
cell.Height = cell.Cell.Enabled ? cell.Rect.rect.height : 0f;
}
// Value change processor
internal void OnValueChangedListener(Vector2 val)
{
if (ExternallySetting)
return;
// ExternallySetting = true;
SetRecycleViewBounds();
UpdateDisplayedHeightCache();
// ExternallySetting = false;
Vector2 dir = scrollRect.content.anchoredPosition - _prevAnchoredPos;
var adjust = ProcessValueChange(dir.y);
scrollRect.m_ContentStartPosition += adjust;
scrollRect.m_PrevPosition += adjust;
_prevAnchoredPos = scrollRect.content.anchoredPosition;
UpdateSliderPositionAndSize();
}
internal Vector2 ProcessValueChange(float yChange)
{
if (ExternallySetting)
return Vector2.zero;
SetRecycleViewBounds();
float adjust = 0f;
var topCell = CellPool[topMostCellIndex].Rect;
var bottomCell = CellPool[bottomMostCellIndex].Rect;
if (yChange > 0) // Scrolling down
{
if (topCell.position.y >= RecycleViewBounds.x)
adjust = RecycleTopToBottom();
}
else if (yChange < 0) // Scrolling up
{
if (bottomCell.MaxY() < RecycleViewBounds.y)
adjust = RecycleBottomToTop();
}
return new Vector2(0, adjust);
}
/// <summary>
/// Recycles cells from top to bottom in the List heirarchy
/// </summary>
private float RecycleTopToBottom()
{
ExternallySetting = true;
float recycledheight = 0f;
//float posY;
while (CellPool[topMostCellIndex].Rect.position.y > RecycleViewBounds.x && CurrentDataCount < DataSource.ItemCount)
{
var cell = CellPool[topMostCellIndex];
//Move top cell to bottom
cell.Rect.SetAsLastSibling();
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
// update content position
Content.anchoredPosition -= Vector2.up * cell.Height;
recycledheight += cell.Height + contentLayout.spacing;
//set Cell
DataSource.SetCell(cell.Cell, CurrentDataCount);
cell.Height = cell.Rect.rect.height;
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
//set new indices
topDataIndex++;
bottomDataIndex++;
bottomMostCellIndex = topMostCellIndex;
topMostCellIndex = (topMostCellIndex + 1) % CellPool.Count;
}
return -recycledheight;
}
/// <summary>
/// Recycles cells from bottom to top in the List heirarchy
/// </summary>
private float RecycleBottomToTop()
{
ExternallySetting = true;
float recycledheight = 0f;
//float posY;
while (CellPool[bottomMostCellIndex].Rect.MaxY() < RecycleViewBounds.y && CurrentDataCount > CellPool.Count)
{
var cell = CellPool[bottomMostCellIndex];
//Move bottom cell to top
cell.Rect.SetAsFirstSibling();
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
// update content position
Content.anchoredPosition += Vector2.up * cell.Height;
recycledheight += cell.Height + contentLayout.spacing;
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
//set new index
topDataIndex--;
bottomDataIndex--;
//set Cell
DataSource.SetCell(cell.Cell, topDataIndex);
cell.Height = cell.Rect.rect.height;
//set new indices
topMostCellIndex = bottomMostCellIndex;
bottomMostCellIndex = (bottomMostCellIndex - 1 + CellPool.Count) % CellPool.Count;
}
return recycledheight;
}
// Slider
private void UpdateSliderPositionAndSize()
{
int total = DataSource.ItemCount;
total = Math.Max(total, 1);
// 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)
{
if (this.ExternallySetting)
return;
this.ExternallySetting = true;
// 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.
}
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.
}
/// <summary>Use <see cref="UIFactory.CreateScrollPool"/></summary>
public override void ConstructUI(GameObject parent) => throw new NotImplementedException();
}
}

View File

@ -1,17 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
namespace UnityExplorer.UI.Widgets
{
public interface IDynamicCell : ICell
{
//GameObject ExpandableContent { get; }
//void OnExpand(bool expanded);
//event Action<bool> OnExpanded;
}
}

View File

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnityExplorer.UI.Widgets
{
public interface IDynamicDataSource : IPoolDataSource
{
//float GetHeightForCell(int index);
float DefaultCellHeight { get; }
}
}

View File

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnityExplorer.UI.Widgets
{
public interface IScrollPool
{
bool AutoResizeHandleRect { get; set; }
}
}

View File

@ -10,50 +10,152 @@ using UnityExplorer.UI.Models;
namespace UnityExplorer.UI.Widgets
{
/// <summary>
/// A <see cref="ScrollRect"/> handler which pools displayed cells to improve performance.
/// A ScrollRect for a list of content with cells that vary in height, using VerticalLayoutGroup and LayoutElement.
/// </summary>
public class ScrollPool : UIBehaviourModel, IScrollPool
public class ScrollPool : UIBehaviourModel
{
// 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)
heightCache.Add(defaultCellHeight);
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;
}
}
public ScrollPool(ScrollRect scrollRect)
{
this.scrollRect = scrollRect;
Init();
}
public IPoolDataSource DataSource;
public bool AutoResizeHandleRect { get; set; }
public float ExtraPoolCoverageRatio = 1.3f;
public int PoolCount => _cachedCells.Count;
public IPoolDataSource DataSource;
public RectTransform PrototypeCell;
private float DefaultCellHeight => PrototypeCell?.rect.height ?? 25f;
// UI
public override GameObject UIRoot => scrollRect.gameObject;
/// <summary>Use <see cref="UIFactory.CreateScrollPool"/></summary>
public override void ConstructUI(GameObject parent) => throw new NotImplementedException();
public RectTransform Viewport => scrollRect.viewport;
public RectTransform Content => scrollRect.content;
internal Slider slider;
internal ScrollRect scrollRect;
internal VerticalLayoutGroup contentLayout;
public bool AutoResizeHandleRect { get; set; }
// Cache / tracking
internal RectTransform PrototypeCell;
internal Slider _slider;
/// <summary>Extra clearance height relative to Viewport height, based on <see cref="ExtraPoolCoverageRatio"/>.</summary>
private Vector2 RecycleViewBounds;
// Cell pool
private float _cellWidth, _cellHeight;
private List<RectTransform> _cellPool;
private List<ICell> _cachedCells;
private Bounds _recyclableViewBounds;
private readonly HeightCache DataHeightCache = new HeightCache();
/// <summary>
/// Extra pooled cells above AND below the viewport (so actual extra pool is double this value).
/// The first and last pooled indices relative to the DataSource's list
/// </summary>
public int ExtraCellPoolSize = 2;
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;
SetContentHeight();
}
}
private float m_totalCellHeight;
/// <summary>
/// The first and last indices of our CellPool in the transform heirarchy
/// </summary>
private int topPoolCellIndex, bottomPoolIndex;
private int CurrentDataCount => bottomDataIndex + 1;
private readonly Vector3[] _corners = new Vector3[4];
private bool _recycling;
private Vector2 _prevAnchoredPos;
internal Vector2 _lastScroll;
private Vector2 _prevViewportSize; // TODO track viewport height and rebuild on change
internal int currentItemCount; //item count corresponding to the datasource.
internal int topMostCellIndex, bottomMostCellIndex; //Topmost and bottommost cell in the heirarchy
#region internal set tracking and update
//private bool _recycling;
public bool ExternallySetting
{
@ -69,64 +171,411 @@ namespace UnityExplorer.UI.Widgets
private bool externallySetting;
private float timeOfLastExternalSet;
private Vector2 zeroVector = Vector2.zero;
public override void Init()
{
_slider = scrollRect.GetComponentInChildren<Slider>();
_slider.onValueChanged.AddListener((float val) =>
{
if (this.ExternallySetting)
return;
this.ExternallySetting = true;
// Jump to val * count (ie, 0.0 would jump to top, 1.0 would jump to bottom)
var index = Math.Floor(val * DataSource.ItemCount);
JumpToIndex((int)index);
});
}
public override void Update()
{
if (externallySetting && timeOfLastExternalSet < Time.time)
externallySetting = false;
}
#endregion
// Initialize
internal void OnValueChangedListener(Vector2 _)
public void Rebuild()
{
Initialize(DataSource);
}
public void Initialize(IPoolDataSource dataSource)
{
DataSource = dataSource;
this.contentLayout = scrollRect.content.GetComponent<VerticalLayoutGroup>();
this.slider = scrollRect.GetComponentInChildren<Slider>();
slider.onValueChanged.AddListener(OnSliderValueChanged);
scrollRect.vertical = true;
scrollRect.horizontal = false;
scrollRect.onValueChanged.RemoveListener(OnValueChangedListener);
RuntimeProvider.Instance.StartCoroutine(InitCoroutine());
}
private IEnumerator InitCoroutine()
{
scrollRect.content.anchoredPosition = Vector2.zero;
yield return null;
_prevAnchoredPos = scrollRect.content.anchoredPosition;
_prevViewportSize = new Vector2(scrollRect.viewport.rect.width, scrollRect.viewport.rect.height);
SetRecycleViewBounds();
BuildInitialHeightCache();
CreateCellPool();
SetContentHeight();
UpdateSliderPositionAndSize();
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()
{
var extra = (Viewport.rect.height * ExtraPoolCoverageRatio) - Viewport.rect.height;
extra *= 0.5f;
RecycleViewBounds = new Vector2(Viewport.MinY() + extra, Viewport.MaxY() - extra);
}
private void SetContentHeight()
{
var viewRect = scrollRect.viewport;
scrollRect.content.sizeDelta = new Vector2(
scrollRect.content.sizeDelta.x,
AdjustedTotalCellHeight - viewRect.rect.height);
}
// Refresh methods
private struct CellInfo { public int cellIndex, dataIndex; }
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
};
cellIdx++;
if (cellIdx >= CellPool.Count)
cellIdx = 0;
dataIndex++;
iterated++;
}
}
// TODO this is not quite right, it can move the content, it shouldnt move it
public void RefreshCells(bool andReloadFromDataSource = false)
{
if (!CellPool.Any()) return;
SetRecycleViewBounds();
bool jumpToBottom = false;
if (andReloadFromDataSource)
{
int count = DataSource.ItemCount;
if (bottomDataIndex > count)
{
bottomDataIndex = Math.Max(count - 1, CellPool.Count - 1);
jumpToBottom = true;
}
}
var enumerator = GetPoolEnumerator();
while (enumerator.MoveNext())
{
var curr = enumerator.Current;
var cell = CellPool[curr.cellIndex];
if (andReloadFromDataSource)
SetCell(cell, curr.dataIndex);
else
{
cell.Height = cell.Rect.rect.height;
DataHeightCache[curr.dataIndex] = cell.Height;
}
}
SetRecycleViewBounds();
SetContentHeight();
if (andReloadFromDataSource)
{
RecycleBottomToTop();
RecycleTopToBottom();
}
SetContentHeight();
UpdateSliderPositionAndSize();
if (jumpToBottom)
{
var diff = Viewport.MaxY() - CellPool[bottomPoolIndex].Rect.MaxY();
Content.anchoredPosition += Vector2.up * diff;
}
}
private void SetCell(CachedCell cachedCell, int dataIndex)
{
cachedCell.Cell.Enable();
DataSource.SetCell(cachedCell.Cell, dataIndex);
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
// ExplorerCore.Log("Set cell, real height is " + cachedCell.Rect.rect.height + ", pref height is " + cachedCell.Rect.GetComponent<LayoutElement>().preferredHeight);
cachedCell.Height = cachedCell.Cell.Enabled ? cachedCell.Rect.rect.height : 0f;
DataHeightCache[dataIndex] = cachedCell.Height;
//ExplorerCore.Log("set to cache as " + cachedCell.Height);
}
//private void UpdateDisplayedHeightCache()
//{
// if (!CellPool.Any()) return;
// var enumerator = GetPoolEnumerator();
// while (enumerator.MoveNext())
// {
// var curr = enumerator.Current;
// var cell = CellPool[curr.cellIndex];
// cell.Height = cell.Rect.rect.height;
// DataHeightCache[curr.dataIndex] = cell.Height;
// }
// //int cellIdx = topPoolCellIndex;
// //int dataIndex = topDataIndex;
// //int iterated = 0;
// //while (iterated < CellPool.Count)
// //{
// // var cell = CellPool[cellIdx];
// // cellIdx++;
// // if (cellIdx >= CellPool.Count)
// // cellIdx = 0;
// //
// // cell.Height = cell.Rect.rect.height;
// // DataHeightCache[dataIndex] = cell.Height;
// //
// // dataIndex++;
// // iterated++;
// //}
//}
// Cell pool
private void CreateCellPool()
{
//Reseting Pool
if (CellPool.Any())
{
foreach (var cell in CellPool)
GameObject.Destroy(cell.Rect.gameObject);
CellPool.Clear();
}
if (!PrototypeCell)
{
ExplorerCore.Log("no prototype cell, cannot initialize");
return;
}
//Set the prototype cell active and set cell anchor as top
PrototypeCell.gameObject.SetActive(true);
float currentPoolCoverage = 0f;
float requiredCoverage = scrollRect.viewport.rect.height * ExtraPoolCoverageRatio;
topPoolCellIndex = 0;
//topDataIndex = 0;
bottomPoolIndex = -1;
// create cells until the Pool area is covered.
// use minimum default height so that maximum pool count is reached.
while (currentPoolCoverage < requiredCoverage)
{
bottomPoolIndex++;
//Instantiate and add to Pool
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);
currentPoolCoverage += rect.rect.height + this.contentLayout.spacing;
}
bottomDataIndex = bottomPoolIndex;
// after creating pool, set displayed cells.
//posY = 0f;
for (int i = 0; i < CellPool.Count; i++)
{
var cell = CellPool[i];
SetCell(cell, i);
}
//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);
}
// Value change processor
private void OnValueChangedListener(Vector2 val)
{
if (ExternallySetting)
return;
ExternallySetting = true;
SetRecycleViewBounds();
RefreshCells();
Vector2 dir = scrollRect.content.anchoredPosition - _prevAnchoredPos;
scrollRect.m_ContentStartPosition += ProcessValueChange(dir);
float yChange = (scrollRect.content.anchoredPosition - _prevAnchoredPos).y;
float adjust = 0f;
if (yChange > 0) // Scrolling down
{
if (ShouldRecycleTop)
adjust = RecycleTopToBottom();
}
else if (yChange < 0) // Scrolling up
{
if (ShouldRecycleBottom)
adjust = RecycleBottomToTop();
}
var vector = new Vector2(0, adjust);
scrollRect.m_ContentStartPosition += vector;
scrollRect.m_PrevPosition += vector;
LayoutRebuilder.ForceRebuildLayoutImmediate(Content);
_prevAnchoredPos = scrollRect.content.anchoredPosition;
UpdateSlider();
// ExternallySetting = false;
UpdateSliderPositionAndSize();
}
internal void UpdateSlider(bool forceValue = true)
private bool ShouldRecycleTop => GetCellExtent(CellPool[topPoolCellIndex]) >= RecycleViewBounds.x
//&& CellPool[topMostCellIndex].Rect.position.y >= Viewport.MinY()
&& CellPool[bottomPoolIndex].Rect.position.y > RecycleViewBounds.y;
private bool ShouldRecycleBottom => GetCellExtent(CellPool[bottomPoolIndex]) < RecycleViewBounds.y
//&& CellPool[bottomMostCellIndex].Rect.position.y < Viewport.MaxY()
&& CellPool[topPoolCellIndex].Rect.position.y < RecycleViewBounds.x;
private float GetCellExtent(CachedCell cell) => cell.Rect.MaxY() - contentLayout.spacing;
private float RecycleTopToBottom()
{
ExternallySetting = true;
float recycledheight = 0f;
while (ShouldRecycleTop && CurrentDataCount < DataSource.ItemCount)
//while (GetCellExtent(CellPool[topMostCellIndex]) > Viewport.MinY() && CurrentDataCount < DataSource.ItemCount)
{
var cell = CellPool[topPoolCellIndex];
//Move top cell to bottom
cell.Rect.SetAsLastSibling();
var prevHeight = cell.Rect.rect.height;
// update content position
Content.anchoredPosition -= Vector2.up * prevHeight;
recycledheight += prevHeight + contentLayout.spacing;
//set Cell
SetCell(cell, CurrentDataCount);
//set new indices
//topDataIndex++;
bottomDataIndex++;
bottomPoolIndex = topPoolCellIndex;
topPoolCellIndex = (topPoolCellIndex + 1) % CellPool.Count;
}
return -recycledheight;
}
private float RecycleBottomToTop()
{
ExternallySetting = true;
float recycledheight = 0f;
// works, except when moving+resizing a cell at the top, that seems to cause issues, need to fix that.
while (ShouldRecycleBottom && CurrentDataCount > CellPool.Count)
{
var cell = CellPool[bottomPoolIndex];
//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
//topDataIndex--;
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)
{
SetContentHeight();
Content.anchoredPosition += Vector2.up * diff;
recycledheight += diff;
}
//set new indices
topPoolCellIndex = bottomPoolIndex;
bottomPoolIndex = (bottomPoolIndex - 1 + CellPool.Count) % CellPool.Count;
}
return recycledheight;
}
// Slider
private void UpdateSliderPositionAndSize()
{
int total = DataSource.ItemCount;
total = Math.Max(total, 1);
var spread = _cellPool.Count - (ExtraCellPoolSize * 2);
// 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)
{
var range = GetDisplayedRange();
if (spread >= total)
_slider.value = 0f;
slider.value = 0f;
else
// top-most displayed index divided by (totalCount - displayedRange)
_slider.value = (float)((decimal)range.x / Math.Max(1, (total - _cellPool.Count)));
slider.value = (float)((decimal)TopDataIndex / Math.Max(1, total - CellPool.Count));
}
// resize the handle rect to reflect the size of the displayed content vs. the total content height.
if (AutoResizeHandleRect)
{
var viewportHeight = scrollRect.viewport.rect.height;
@ -137,358 +586,37 @@ namespace UnityExplorer.UI.Widgets
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;
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;
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);
slider.interactable = !Mathf.Approximately(handleHeight, viewportHeight);
}
}
/// <summary>
/// Try to jump to the specified index. Pretty accurate, not perfect. Currently assumes all elements are the same height.
/// </summary>
public void JumpToIndex(int index)
private void OnSliderValueChanged(float val)
{
var realCount = DataSource.ItemCount;
// clamp to real index limit
index = Math.Min(index, realCount - 1);
// add the buffer count to desired index and set our currentItemCount to that.
currentItemCount = index + _cachedCells.Count;
currentItemCount = Math.Max(Math.Min(currentItemCount, realCount - 1), _cachedCells.Count);
Refresh();
// if we're jumping to the very bottom we need to show the extra pooled cells which are normally hidden.
var y = 0f;
if (index >= realCount - (ExtraCellPoolSize * 4))
y = _cellHeight * (index - realCount + (4 * ExtraCellPoolSize)) + ExtraCellPoolSize; // add +1 to show the last entry.
scrollRect.content.anchoredPosition = new Vector2(scrollRect.content.anchoredPosition.x, y);
}
/// <summary>
/// Get the start and end indexes (relative to DataSource) of the cell pool
/// </summary>
public Vector2 GetDisplayedRange()
{
int max = currentItemCount;
int min = max - _cachedCells.Count;
return new Vector2(min, max);
}
/// <summary>
/// Initialize with the provided DataSource
/// </summary>
public void Initialize(IPoolDataSource dataSource)
{
DataSource = dataSource;
scrollRect.vertical = true;
scrollRect.horizontal = false;
_prevAnchoredPos = scrollRect.content.anchoredPosition;
scrollRect.onValueChanged.RemoveListener(OnValueChangedListener);
RuntimeProvider.Instance.StartCoroutine(InitCoroutine(() =>
{
scrollRect.onValueChanged.AddListener(OnValueChangedListener);
}));
}
public void ReloadData()
{
ReloadData(DataSource);
}
public void ReloadData(IPoolDataSource dataSource)
{
if (scrollRect.onValueChanged == null)
if (this.ExternallySetting)
return;
this.ExternallySetting = true;
scrollRect.StopMovement();
scrollRect.onValueChanged.RemoveListener(OnValueChangedListener);
DataSource = dataSource;
RuntimeProvider.Instance.StartCoroutine(InitCoroutine(() =>
scrollRect.onValueChanged.AddListener(OnValueChangedListener)
));
_prevAnchoredPos = scrollRect.content.anchoredPosition;
// 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.
}
public void Refresh()
private void JumpToIndex(int dataIndex)
{
if (DataSource == null || _cellPool == null)
return;
int count = DataSource.ItemCount;
if (currentItemCount > count)
currentItemCount = Math.Max(count, _cellPool.Count);
SetRecyclingBounds();
RecycleBottomToTop();
RecycleTopToBottom();
PopulateCells();
RefreshContentSize();
UpdateSlider(false);
// 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.
}
public void PopulateCells()
{
var width = scrollRect.viewport.rect.width;
scrollRect.content.sizeDelta = new Vector2(width, scrollRect.content.sizeDelta.y);
int cellIndex = topMostCellIndex;
var itemIndex = currentItemCount - _cachedCells.Count;
int iterated = 0;
while (iterated < _cachedCells.Count)
{
var cell = _cachedCells[cellIndex];
cellIndex++;
if (cellIndex >= _cachedCells.Count)
cellIndex = 0;
DataSource.SetCell(cell, itemIndex);
itemIndex++;
var rect = _cellPool[cellIndex];
rect.sizeDelta = new Vector2(width, rect.sizeDelta.y);
iterated++;
}
}
#region RECYCLING INIT
private IEnumerator InitCoroutine(Action onInitialized)
{
yield return null;
SetTopAnchor(scrollRect.content);
scrollRect.content.anchoredPosition = Vector3.zero;
yield return null;
SetRecyclingBounds();
//Cell Pool
CreateCellPool();
currentItemCount = _cellPool.Count;
topMostCellIndex = 0;
bottomMostCellIndex = _cellPool.Count - 1;
//Set content height according to no of rows
RefreshContentSize();
SetTopAnchor(scrollRect.content);
onInitialized?.Invoke();
}
private void RefreshContentSize()
{
int noOfRows = 0;
foreach (var cell in _cachedCells)
if (cell.Enabled) noOfRows++;
float contentYSize = noOfRows * _cellHeight;
scrollRect.content.sizeDelta = new Vector2(scrollRect.content.sizeDelta.x, contentYSize);
}
private void SetRecyclingBounds()
{
scrollRect.viewport.GetCorners(_corners);
float threshHold = _cellHeight * ExtraCellPoolSize; //RecyclingThreshold * (_corners[2].y - _corners[0].y);
_recyclableViewBounds.min = new Vector3(_corners[0].x, _corners[0].y - threshHold);
_recyclableViewBounds.max = new Vector3(_corners[2].x, _corners[2].y + threshHold);
}
private void CreateCellPool()
{
//Reseting Pool
if (_cellPool != null)
{
_cellPool.ForEach((RectTransform item) => GameObject.Destroy(item.gameObject));
_cellPool.Clear();
_cachedCells.Clear();
}
else
{
_cachedCells = new List<ICell>();
_cellPool = new List<RectTransform>();
}
//Set the prototype cell active and set cell anchor as top
PrototypeCell.gameObject.SetActive(true);
SetTopAnchor(PrototypeCell);
//Temps
float currentPoolCoverage = 0;
int poolSize = 0;
float posY = 0;
//set new cell size according to its aspect ratio
_cellWidth = scrollRect.content.rect.width;
_cellHeight = PrototypeCell.rect.height;
//Get the required pool coverage and mininum size for the Cell pool
float requiredCoverage = scrollRect.viewport.rect.height + (_cellHeight * (ExtraCellPoolSize * 2));
//create cells untill the Pool area is covered
while (currentPoolCoverage < requiredCoverage)
{
//Instantiate and add to Pool
RectTransform item = GameObject.Instantiate(PrototypeCell.gameObject).GetComponent<RectTransform>();
item.name = $"Cell_{_cachedCells.Count + 1}";
item.sizeDelta = new Vector2(_cellWidth, _cellHeight);
_cellPool.Add(item);
item.SetParent(scrollRect.content, false);
item.anchoredPosition = new Vector2(0, posY);
posY = item.anchoredPosition.y - item.rect.height;
currentPoolCoverage += item.rect.height;
//Setting data for Cell
var cell = DataSource.CreateCell(item);
_cachedCells.Add(cell);
DataSource.SetCell(cell, poolSize);
//Update the Pool size
poolSize++;
}
//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);
}
#endregion
#region RECYCLING
public Vector2 ProcessValueChange(Vector2 direction)
{
if (_recycling || _cellPool == null || _cellPool.Count == 0)
return zeroVector;
//Updating Recyclable view bounds since it can change with resolution changes.
SetRecyclingBounds();
_lastScroll = direction;
if (direction.y > 0 && _cellPool[bottomMostCellIndex].MaxY() > _recyclableViewBounds.min.y)
{
return RecycleTopToBottom();
}
else if (direction.y < 0 && _cellPool[topMostCellIndex].MinY() < _recyclableViewBounds.max.y)
{
return RecycleBottomToTop();
}
return zeroVector;
}
/// <summary>
/// Recycles cells from top to bottom in the List heirarchy
/// </summary>
private Vector2 RecycleTopToBottom()
{
_recycling = true;
int n = 0;
float posY;
//to determine if content size needs to be updated
//Recycle until cell at Top is avaiable and current item count smaller than datasource
while (_cellPool[topMostCellIndex].MinY() > _recyclableViewBounds.max.y && currentItemCount < DataSource.ItemCount)
{
//Move top cell to bottom
posY = _cellPool[bottomMostCellIndex].anchoredPosition.y - _cellPool[bottomMostCellIndex].sizeDelta.y;
_cellPool[topMostCellIndex].anchoredPosition = new Vector2(_cellPool[topMostCellIndex].anchoredPosition.x, posY);
//Cell for row at
DataSource.SetCell(_cachedCells[topMostCellIndex], currentItemCount);
//set new indices
bottomMostCellIndex = topMostCellIndex;
topMostCellIndex = (topMostCellIndex + 1) % _cellPool.Count;
currentItemCount++;
n++;
}
//Content anchor position adjustment.
_cellPool.ForEach((RectTransform cell) => cell.anchoredPosition += n * Vector2.up * _cellPool[topMostCellIndex].sizeDelta.y);
scrollRect.content.anchoredPosition -= n * Vector2.up * _cellPool[topMostCellIndex].sizeDelta.y;
_recycling = false;
return -new Vector2(0, n * _cellPool[topMostCellIndex].sizeDelta.y);
}
/// <summary>
/// Recycles cells from bottom to top in the List heirarchy
/// </summary>
private Vector2 RecycleBottomToTop()
{
_recycling = true;
int n = 0;
float posY = 0;
//to determine if content size needs to be updated
//Recycle until cell at bottom is avaiable and current item count is greater than cellpool size
while (_cellPool[bottomMostCellIndex].MaxY() < _recyclableViewBounds.min.y && currentItemCount > _cellPool.Count)
{
//Move bottom cell to top
posY = _cellPool[topMostCellIndex].anchoredPosition.y + _cellPool[topMostCellIndex].sizeDelta.y;
_cellPool[bottomMostCellIndex].anchoredPosition = new Vector2(_cellPool[bottomMostCellIndex].anchoredPosition.x, posY);
n++;
currentItemCount--;
//Cell for row at
DataSource.SetCell(_cachedCells[bottomMostCellIndex], currentItemCount - _cellPool.Count);
//set new indices
topMostCellIndex = bottomMostCellIndex;
bottomMostCellIndex = (bottomMostCellIndex - 1 + _cellPool.Count) % _cellPool.Count;
}
_cellPool.ForEach((RectTransform cell) => cell.anchoredPosition -= n * Vector2.up * _cellPool[topMostCellIndex].sizeDelta.y);
scrollRect.content.anchoredPosition += n * Vector2.up * _cellPool[topMostCellIndex].sizeDelta.y;
_recycling = false;
return new Vector2(0, n * _cellPool[topMostCellIndex].sizeDelta.y);
}
#endregion
#region HELPERS
/// <summary>
/// Anchoring cell and content rect transforms to top preset. Makes repositioning easy.
/// </summary>
/// <param name="rectTransform"></param>
private void SetTopAnchor(RectTransform rectTransform)
{
//Saving to reapply after anchoring. Width and height changes if anchoring is change.
float width = rectTransform.rect.width;
float height = rectTransform.rect.height;
//Setting top anchor
rectTransform.anchorMin = new Vector2(0.5f, 1);
rectTransform.anchorMax = new Vector2(0.5f, 1);
rectTransform.pivot = new Vector2(0.5f, 1);
//Reapply size
rectTransform.sizeDelta = new Vector2(width, height);
}
#endregion
/// <summary>Use <see cref="UIFactory.CreateScrollPool"/></summary>
public override void ConstructUI(GameObject parent) => throw new NotImplementedException();
}
}

View File

@ -0,0 +1,494 @@
//using System;
//using System.Collections;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using UnityEngine;
//using UnityEngine.UI;
//using UnityExplorer.UI.Models;
//namespace UnityExplorer.UI.Widgets
//{
// /// <summary>
// /// A <see cref="ScrollRect"/> handler which pools displayed cells to improve performance.
// /// </summary>
// public class ScrollPool : UIBehaviourModel, IScrollPool
// {
// public ScrollPool(ScrollRect scrollRect)
// {
// this.scrollRect = scrollRect;
// Init();
// }
// public IPoolDataSource DataSource;
// public int PoolCount => _cachedCells.Count;
// public override GameObject UIRoot => scrollRect.gameObject;
// /// <summary>Use <see cref="UIFactory.CreateScrollPool"/></summary>
// public override void ConstructUI(GameObject parent) => throw new NotImplementedException();
// internal ScrollRect scrollRect;
// public bool AutoResizeHandleRect { get; set; }
// internal RectTransform PrototypeCell;
// internal Slider _slider;
// // Cell pool
// private float _cellWidth, _cellHeight;
// private List<RectTransform> _cellPool;
// private List<ICell> _cachedCells;
// private Bounds _recyclableViewBounds;
// /// <summary>
// /// Extra pooled cells above AND below the viewport (so actual extra pool is double this value).
// /// </summary>
// public int ExtraCellPoolSize = 2;
// private readonly Vector3[] _corners = new Vector3[4];
// private bool _recycling;
// private Vector2 _prevAnchoredPos;
// internal Vector2 _lastScroll;
// internal int currentItemCount; //item count corresponding to the datasource.
// internal int topMostCellIndex, bottomMostCellIndex; //Topmost and bottommost cell in the heirarchy
// public bool ExternallySetting
// {
// get => externallySetting;
// internal set
// {
// if (externallySetting == value)
// return;
// timeOfLastExternalSet = Time.time;
// externallySetting = value;
// }
// }
// private bool externallySetting;
// private float timeOfLastExternalSet;
// private Vector2 zeroVector = Vector2.zero;
// public override void Init()
// {
// _slider = scrollRect.GetComponentInChildren<Slider>();
// _slider.onValueChanged.AddListener((float val) =>
// {
// if (this.ExternallySetting)
// return;
// this.ExternallySetting = true;
// // Jump to val * count (ie, 0.0 would jump to top, 1.0 would jump to bottom)
// var index = Math.Floor(val * DataSource.ItemCount);
// JumpToIndex((int)index);
// });
// }
// public override void Update()
// {
// if (externallySetting && timeOfLastExternalSet < Time.time)
// externallySetting = false;
// }
// internal void OnValueChangedListener(Vector2 _)
// {
// if (ExternallySetting)
// return;
// ExternallySetting = true;
// Vector2 dir = scrollRect.content.anchoredPosition - _prevAnchoredPos;
// scrollRect.m_ContentStartPosition += ProcessValueChange(dir);
// _prevAnchoredPos = scrollRect.content.anchoredPosition;
// UpdateSlider();
// // ExternallySetting = false;
// }
// internal void UpdateSlider(bool forceValue = true)
// {
// int total = DataSource.ItemCount;
// total = Math.Max(total, 1);
// var spread = _cellPool.Count - (ExtraCellPoolSize * 2);
// if (forceValue)
// {
// var range = GetDisplayedRange();
// if (spread >= total)
// _slider.value = 0f;
// else
// // top-most displayed index divided by (totalCount - displayedRange)
// _slider.value = (float)((decimal)range.x / Math.Max(1, (total - _cellPool.Count)));
// }
// // resize the handle rect to reflect the size of the displayed content vs. the total content height.
// 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);
// }
// }
// /// <summary>
// /// Try to jump to the specified index. Pretty accurate, not perfect. Currently assumes all elements are the same height.
// /// </summary>
// public void JumpToIndex(int index)
// {
// var realCount = DataSource.ItemCount;
// // clamp to real index limit
// index = Math.Min(index, realCount - 1);
// // add the buffer count to desired index and set our currentItemCount to that.
// currentItemCount = index + _cachedCells.Count;
// currentItemCount = Math.Max(Math.Min(currentItemCount, realCount - 1), _cachedCells.Count);
// Refresh();
// // if we're jumping to the very bottom we need to show the extra pooled cells which are normally hidden.
// var y = 0f;
// if (index >= realCount - (ExtraCellPoolSize * 4))
// y = _cellHeight * (index - realCount + (4 * ExtraCellPoolSize)) + ExtraCellPoolSize; // add +1 to show the last entry.
// scrollRect.content.anchoredPosition = new Vector2(scrollRect.content.anchoredPosition.x, y);
// }
// /// <summary>
// /// Get the start and end indexes (relative to DataSource) of the cell pool
// /// </summary>
// public Vector2 GetDisplayedRange()
// {
// int max = currentItemCount;
// int min = max - _cachedCells.Count;
// return new Vector2(min, max);
// }
// /// <summary>
// /// Initialize with the provided DataSource
// /// </summary>
// public void Initialize(IPoolDataSource dataSource)
// {
// DataSource = dataSource;
// scrollRect.vertical = true;
// scrollRect.horizontal = false;
// _prevAnchoredPos = scrollRect.content.anchoredPosition;
// scrollRect.onValueChanged.RemoveListener(OnValueChangedListener);
// RuntimeProvider.Instance.StartCoroutine(InitCoroutine(() =>
// {
// scrollRect.onValueChanged.AddListener(OnValueChangedListener);
// }));
// }
// public void ReloadData()
// {
// ReloadData(DataSource);
// }
// public void ReloadData(IPoolDataSource dataSource)
// {
// if (scrollRect.onValueChanged == null)
// return;
// scrollRect.StopMovement();
// scrollRect.onValueChanged.RemoveListener(OnValueChangedListener);
// DataSource = dataSource;
// RuntimeProvider.Instance.StartCoroutine(InitCoroutine(() =>
// scrollRect.onValueChanged.AddListener(OnValueChangedListener)
// ));
// _prevAnchoredPos = scrollRect.content.anchoredPosition;
// }
// public void Refresh()
// {
// if (DataSource == null || _cellPool == null)
// return;
// int count = DataSource.ItemCount;
// if (currentItemCount > count)
// currentItemCount = Math.Max(count, _cellPool.Count);
// SetRecyclingBounds();
// RecycleBottomToTop();
// RecycleTopToBottom();
// PopulateCells();
// RefreshContentSize();
// UpdateSlider(false);
// }
// public void PopulateCells()
// {
// var width = scrollRect.viewport.rect.width;
// scrollRect.content.sizeDelta = new Vector2(width, scrollRect.content.sizeDelta.y);
// int cellIndex = topMostCellIndex;
// var itemIndex = currentItemCount - _cachedCells.Count;
// int iterated = 0;
// while (iterated < _cachedCells.Count)
// {
// var cell = _cachedCells[cellIndex];
// cellIndex++;
// if (cellIndex >= _cachedCells.Count)
// cellIndex = 0;
// DataSource.SetCell(cell, itemIndex);
// itemIndex++;
// var rect = _cellPool[cellIndex];
// rect.sizeDelta = new Vector2(width, rect.sizeDelta.y);
// iterated++;
// }
// }
// #region RECYCLING INIT
// private IEnumerator InitCoroutine(Action onInitialized)
// {
// yield return null;
// SetTopAnchor(scrollRect.content);
// scrollRect.content.anchoredPosition = Vector3.zero;
// yield return null;
// SetRecyclingBounds();
// //Cell Pool
// CreateCellPool();
// currentItemCount = _cellPool.Count;
// topMostCellIndex = 0;
// bottomMostCellIndex = _cellPool.Count - 1;
// //Set content height according to no of rows
// RefreshContentSize();
// SetTopAnchor(scrollRect.content);
// onInitialized?.Invoke();
// }
// private void RefreshContentSize()
// {
// int noOfRows = 0;
// foreach (var cell in _cachedCells)
// if (cell.Enabled) noOfRows++;
// float contentYSize = noOfRows * _cellHeight;
// scrollRect.content.sizeDelta = new Vector2(scrollRect.content.sizeDelta.x, contentYSize);
// }
// private void SetRecyclingBounds()
// {
// scrollRect.viewport.GetCorners(_corners);
// float threshHold = _cellHeight * ExtraCellPoolSize; //RecyclingThreshold * (_corners[2].y - _corners[0].y);
// _recyclableViewBounds.min = new Vector3(_corners[0].x, _corners[0].y - threshHold);
// _recyclableViewBounds.max = new Vector3(_corners[2].x, _corners[2].y + threshHold);
// }
// private void CreateCellPool()
// {
// //Reseting Pool
// if (_cellPool != null)
// {
// _cellPool.ForEach((RectTransform item) => GameObject.Destroy(item.gameObject));
// _cellPool.Clear();
// _cachedCells.Clear();
// }
// else
// {
// _cachedCells = new List<ICell>();
// _cellPool = new List<RectTransform>();
// }
// //Set the prototype cell active and set cell anchor as top
// PrototypeCell.gameObject.SetActive(true);
// SetTopAnchor(PrototypeCell);
// //Temps
// float currentPoolCoverage = 0;
// int poolSize = 0;
// float posY = 0;
// //set new cell size according to its aspect ratio
// _cellWidth = scrollRect.content.rect.width;
// _cellHeight = PrototypeCell.rect.height;
// //Get the required pool coverage and mininum size for the Cell pool
// float requiredCoverage = scrollRect.viewport.rect.height + (_cellHeight * (ExtraCellPoolSize * 2));
// //create cells untill the Pool area is covered
// while (currentPoolCoverage < requiredCoverage)
// {
// //Instantiate and add to Pool
// RectTransform item = GameObject.Instantiate(PrototypeCell.gameObject).GetComponent<RectTransform>();
// item.name = $"Cell_{_cachedCells.Count + 1}";
// item.sizeDelta = new Vector2(_cellWidth, _cellHeight);
// _cellPool.Add(item);
// item.SetParent(scrollRect.content, false);
// item.anchoredPosition = new Vector2(0, posY);
// posY = item.anchoredPosition.y - item.rect.height;
// currentPoolCoverage += item.rect.height;
// //Setting data for Cell
// var cell = DataSource.CreateCell(item);
// _cachedCells.Add(cell);
// DataSource.SetCell(cell, poolSize);
// //Update the Pool size
// poolSize++;
// }
// //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);
// }
// #endregion
// #region RECYCLING
// public Vector2 ProcessValueChange(Vector2 direction)
// {
// if (_recycling || _cellPool == null || _cellPool.Count == 0)
// return zeroVector;
// //Updating Recyclable view bounds since it can change with resolution changes.
// SetRecyclingBounds();
// _lastScroll = direction;
// if (direction.y > 0 && _cellPool[bottomMostCellIndex].MaxY() > _recyclableViewBounds.min.y)
// {
// return RecycleTopToBottom();
// }
// else if (direction.y < 0 && _cellPool[topMostCellIndex].MinY() < _recyclableViewBounds.max.y)
// {
// return RecycleBottomToTop();
// }
// return zeroVector;
// }
// /// <summary>
// /// Recycles cells from top to bottom in the List heirarchy
// /// </summary>
// private Vector2 RecycleTopToBottom()
// {
// _recycling = true;
// int n = 0;
// float posY;
// //to determine if content size needs to be updated
// //Recycle until cell at Top is avaiable and current item count smaller than datasource
// while (_cellPool[topMostCellIndex].MinY() > _recyclableViewBounds.max.y && currentItemCount < DataSource.ItemCount)
// {
// //Move top cell to bottom
// posY = _cellPool[bottomMostCellIndex].anchoredPosition.y - _cellPool[bottomMostCellIndex].sizeDelta.y;
// _cellPool[topMostCellIndex].anchoredPosition = new Vector2(_cellPool[topMostCellIndex].anchoredPosition.x, posY);
// //Cell for row at
// DataSource.SetCell(_cachedCells[topMostCellIndex], currentItemCount);
// //set new indices
// bottomMostCellIndex = topMostCellIndex;
// topMostCellIndex = (topMostCellIndex + 1) % _cellPool.Count;
// currentItemCount++;
// n++;
// }
// //Content anchor position adjustment.
// _cellPool.ForEach((RectTransform cell) => cell.anchoredPosition += n * Vector2.up * _cellPool[topMostCellIndex].sizeDelta.y);
// scrollRect.content.anchoredPosition -= n * Vector2.up * _cellPool[topMostCellIndex].sizeDelta.y;
// _recycling = false;
// return -new Vector2(0, n * _cellPool[topMostCellIndex].sizeDelta.y);
// }
// /// <summary>
// /// Recycles cells from bottom to top in the List heirarchy
// /// </summary>
// private Vector2 RecycleBottomToTop()
// {
// _recycling = true;
// int n = 0;
// float posY = 0;
// //to determine if content size needs to be updated
// //Recycle until cell at bottom is avaiable and current item count is greater than cellpool size
// while (_cellPool[bottomMostCellIndex].MaxY() < _recyclableViewBounds.min.y && currentItemCount > _cellPool.Count)
// {
// //Move bottom cell to top
// posY = _cellPool[topMostCellIndex].anchoredPosition.y + _cellPool[topMostCellIndex].sizeDelta.y;
// _cellPool[bottomMostCellIndex].anchoredPosition = new Vector2(_cellPool[bottomMostCellIndex].anchoredPosition.x, posY);
// n++;
// currentItemCount--;
// //Cell for row at
// DataSource.SetCell(_cachedCells[bottomMostCellIndex], currentItemCount - _cellPool.Count);
// //set new indices
// topMostCellIndex = bottomMostCellIndex;
// bottomMostCellIndex = (bottomMostCellIndex - 1 + _cellPool.Count) % _cellPool.Count;
// }
// _cellPool.ForEach((RectTransform cell) => cell.anchoredPosition -= n * Vector2.up * _cellPool[topMostCellIndex].sizeDelta.y);
// scrollRect.content.anchoredPosition += n * Vector2.up * _cellPool[topMostCellIndex].sizeDelta.y;
// _recycling = false;
// return new Vector2(0, n * _cellPool[topMostCellIndex].sizeDelta.y);
// }
// #endregion
// #region HELPERS
// /// <summary>
// /// Anchoring cell and content rect transforms to top preset. Makes repositioning easy.
// /// </summary>
// /// <param name="rectTransform"></param>
// private void SetTopAnchor(RectTransform rectTransform)
// {
// //Saving to reapply after anchoring. Width and height changes if anchoring is change.
// float width = rectTransform.rect.width;
// float height = rectTransform.rect.height;
// //Setting top anchor
// rectTransform.anchorMin = new Vector2(0.5f, 1);
// rectTransform.anchorMax = new Vector2(0.5f, 1);
// rectTransform.pivot = new Vector2(0.5f, 1);
// //Reapply size
// rectTransform.sizeDelta = new Vector2(width, height);
// }
// #endregion
// }
//}

View File

@ -71,7 +71,6 @@ namespace UnityExplorer.UI.Widgets
yield return null;
RefreshData();
Scroller.DataSource = this;
Scroller.Initialize(this);
}
@ -103,7 +102,7 @@ namespace UnityExplorer.UI.Widgets
}
if (andReload)
Scroller.Refresh();
Scroller.RefreshCells(true);
}
private void Traverse(Transform transform, CachedTransform parent = null)

View File

@ -265,24 +265,21 @@
<Compile Include="UI\Models\UIBehaviourModel.cs" />
<Compile Include="UI\Models\UIModel.cs" />
<Compile Include="UI\Models\UIPanel.cs" />
<Compile Include="UI\Panels\GameObjectInspector.cs" />
<Compile Include="UI\Panels\InspectorTest.cs" />
<Compile Include="UI\Panels\SceneExplorer.cs" />
<Compile Include="UI\UIFactory.cs" />
<Compile Include="UI\UIManager.cs" />
<Compile Include="UI\Utility\PanelDragger.cs" />
<Compile Include="UI\Utility\SignatureHighlighter.cs" />
<Compile Include="UI\Utility\ToStringUtility.cs" />
<Compile Include="UI\Widgets\ScrollPool\Dynamic\IDynamicCell.cs" />
<Compile Include="UI\Widgets\ScrollPool\ICell.cs" />
<Compile Include="UI\Widgets\ScrollPool\Dynamic\IDynamicDataSource.cs" />
<Compile Include="UI\Widgets\ScrollPool\IPoolDataSource.cs" />
<Compile Include="UI\Widgets\ScrollPool\IScrollPool.cs" />
<Compile Include="UI\Widgets\ScrollPool\ScrollPoolBak.cs" />
<Compile Include="UI\Widgets\ScrollPool\ScrollPool.cs" />
<Compile Include="UI\Widgets\ScrollPool\Dynamic\DynamicScrollPool.cs" />
<Compile Include="UI\Widgets\ScrollPool\UIExtensions.cs" />
<Compile Include="UI\Widgets\InputFieldScroller.cs" />
<Compile Include="UI\Widgets\SimpleList\SimpleCell.cs" />
<Compile Include="UI\Widgets\SimpleList\SimpleListSource.cs" />
<Compile Include="UI\Widgets\ButtonList\ButtonCell.cs" />
<Compile Include="UI\Widgets\ButtonList\ButtonListCell.cs" />
<Compile Include="UI\Widgets\SliderScrollbar.cs" />
<Compile Include="UI\Widgets\TransformTree\CachedTransform.cs" />
<Compile Include="UI\Widgets\TransformTree\TransformCell.cs" />
@ -293,7 +290,9 @@
<None Include="ILRepack.targets" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="UI\Widgets\ScrollPool\Dynamic\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="packages\ILRepack.Lib.MSBuild.Task.2.0.18.1\build\ILRepack.Lib.MSBuild.Task.targets" Condition="Exists('packages\ILRepack.Lib.MSBuild.Task.2.0.18.1\build\ILRepack.Lib.MSBuild.Task.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">