mirror of
https://github.com/sinai-dev/UnityExplorer.git
synced 2025-01-07 18:13:35 +08:00
Working on a dynamic-height scroll pool, almost done
This commit is contained in:
parent
bcc89455a7
commit
8b5e385c28
@ -17,7 +17,7 @@ namespace UnityExplorer.UI.Panels
|
|||||||
{
|
{
|
||||||
public override string Name => "GameObject Inspector";
|
public override string Name => "GameObject Inspector";
|
||||||
|
|
||||||
public SimpleListSource<Component> ComponentList;
|
//public SimpleListSource<Component> ComponentList;
|
||||||
|
|
||||||
public override void Update()
|
public override void Update()
|
||||||
{
|
{
|
||||||
@ -33,28 +33,19 @@ namespace UnityExplorer.UI.Panels
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleCell<Component> CreateCell(RectTransform rect)
|
|
||||||
{
|
|
||||||
var button = rect.GetComponentInChildren<Button>();
|
|
||||||
var text = button.GetComponentInChildren<Text>();
|
|
||||||
var cell = new SimpleCell<Component>(ComponentList, rect.gameObject, button, text);
|
|
||||||
cell.OnClick += OnCellClicked;
|
|
||||||
return cell;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnCellClicked(SimpleCell<Component> cell)
|
public void OnCellClicked(SimpleCell<Component> cell)
|
||||||
{
|
{
|
||||||
ExplorerCore.Log("Cell clicked!");
|
ExplorerCore.Log("Cell clicked!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetCell(SimpleCell<Component> cell, int index)
|
//public void SetCell(SimpleCell<Component> cell, int index)
|
||||||
{
|
//{
|
||||||
var comp = ComponentList.currentEntries[index];
|
// var comp = ComponentList.currentEntries[index];
|
||||||
if (!comp)
|
// if (!comp)
|
||||||
cell.buttonText.text = "<color=red>[Destroyed]</color>";
|
// cell.buttonText.text = "<color=red>[Destroyed]</color>";
|
||||||
else
|
// else
|
||||||
cell.buttonText.text = ToStringUtility.GetDefaultLabel(comp, ReflectionProvider.Instance.GetActualType(comp), true, false);
|
// cell.buttonText.text = ToStringUtility.GetDefaultLabel(comp, ReflectionProvider.Instance.GetActualType(comp), true, false);
|
||||||
}
|
//}
|
||||||
|
|
||||||
public bool ShouldDisplay(Component comp, string filter)
|
public bool ShouldDisplay(Component comp, string filter)
|
||||||
{
|
{
|
||||||
@ -87,10 +78,10 @@ namespace UnityExplorer.UI.Panels
|
|||||||
{
|
{
|
||||||
// height changed, hard refresh required.
|
// height changed, hard refresh required.
|
||||||
previousRectHeight = obj.rect.height;
|
previousRectHeight = obj.rect.height;
|
||||||
ComponentList.Scroller.ReloadData();
|
//scrollPool.ReloadData();
|
||||||
}
|
}
|
||||||
|
|
||||||
ComponentList.Scroller.Refresh();
|
//scrollPool.Refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void SetDefaultPosAndAnchors()
|
public override void SetDefaultPosAndAnchors()
|
||||||
@ -105,24 +96,218 @@ namespace UnityExplorer.UI.Panels
|
|||||||
mainPanelRect.pivot = new Vector2(0.5f, 0.5f);
|
mainPanelRect.pivot = new Vector2(0.5f, 0.5f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private DynamicScrollPool scrollPool;
|
||||||
|
|
||||||
public override void ConstructPanelContent()
|
public override void ConstructPanelContent()
|
||||||
{
|
{
|
||||||
// Transform Tree
|
// temp debug
|
||||||
|
scrollPool = UIFactory.CreateDynamicScrollPool(content, "Test", out GameObject scrollObj,
|
||||||
var infiniteScroll = UIFactory.CreateInfiniteScroll(content, "ComponentList", out GameObject scrollObj,
|
|
||||||
out GameObject scrollContent, new Color(0.15f, 0.15f, 0.15f));
|
out GameObject scrollContent, new Color(0.15f, 0.15f, 0.15f));
|
||||||
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
|
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
|
||||||
UIFactory.SetLayoutElement(scrollContent, flexibleHeight: 9999);
|
UIFactory.SetLayoutElement(scrollContent, flexibleHeight: 9999);
|
||||||
|
|
||||||
ComponentList = new SimpleListSource<Component>(infiniteScroll, GetEntries, CreateCell, SetCell, ShouldDisplay);
|
var test = new DynamicListTest(scrollPool);
|
||||||
ComponentList.Init();
|
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>();
|
||||||
|
|
||||||
// Prototype tree cell
|
|
||||||
var prototype = SimpleCell<Component>.CreatePrototypeCell(scrollContent);
|
|
||||||
infiniteScroll.PrototypeCell = prototype.GetComponent<RectTransform>();
|
|
||||||
|
|
||||||
// some references
|
|
||||||
previousRectHeight = mainPanelRect.rect.height;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,7 +128,7 @@ namespace UnityExplorer.UI.Panels
|
|||||||
Tree.RefreshData(true);
|
Tree.RefreshData(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private float previousRectHeight;
|
private float highestRectHeight;
|
||||||
|
|
||||||
public override void OnFinishResize(RectTransform panel)
|
public override void OnFinishResize(RectTransform panel)
|
||||||
{
|
{
|
||||||
@ -140,10 +140,10 @@ namespace UnityExplorer.UI.Panels
|
|||||||
{
|
{
|
||||||
yield return null;
|
yield return null;
|
||||||
|
|
||||||
if (obj.rect.height != previousRectHeight)
|
if (obj.rect.height > highestRectHeight)
|
||||||
{
|
{
|
||||||
// height changed, hard refresh required.
|
// height increased, hard refresh required.
|
||||||
previousRectHeight = obj.rect.height;
|
highestRectHeight = obj.rect.height;
|
||||||
Tree.Scroller.ReloadData();
|
Tree.Scroller.ReloadData();
|
||||||
}
|
}
|
||||||
Tree.Scroller.Refresh();
|
Tree.Scroller.Refresh();
|
||||||
@ -224,7 +224,7 @@ namespace UnityExplorer.UI.Panels
|
|||||||
|
|
||||||
// Transform Tree
|
// Transform Tree
|
||||||
|
|
||||||
var infiniteScroll = UIFactory.CreateInfiniteScroll(content, "TransformTree", out GameObject scrollObj,
|
var infiniteScroll = UIFactory.CreateScrollPool<ScrollPool>(content, "TransformTree", out GameObject scrollObj,
|
||||||
out GameObject scrollContent, new Color(0.15f, 0.15f, 0.15f));
|
out GameObject scrollContent, new Color(0.15f, 0.15f, 0.15f));
|
||||||
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
|
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
|
||||||
UIFactory.SetLayoutElement(scrollContent, flexibleHeight: 9999);
|
UIFactory.SetLayoutElement(scrollContent, flexibleHeight: 9999);
|
||||||
@ -240,7 +240,7 @@ namespace UnityExplorer.UI.Panels
|
|||||||
infiniteScroll.PrototypeCell = prototype.GetComponent<RectTransform>();
|
infiniteScroll.PrototypeCell = prototype.GetComponent<RectTransform>();
|
||||||
|
|
||||||
// some references
|
// some references
|
||||||
previousRectHeight = mainPanelRect.rect.height;
|
highestRectHeight = mainPanelRect.rect.height;
|
||||||
|
|
||||||
// Scene Loader
|
// Scene Loader
|
||||||
try
|
try
|
||||||
|
@ -4,6 +4,7 @@ using UnityEngine.Events;
|
|||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
using UnityExplorer.Core.Config;
|
using UnityExplorer.Core.Config;
|
||||||
using UnityExplorer.Core.Runtime;
|
using UnityExplorer.Core.Runtime;
|
||||||
|
using UnityExplorer.UI.Models;
|
||||||
using UnityExplorer.UI.Utility;
|
using UnityExplorer.UI.Utility;
|
||||||
using UnityExplorer.UI.Widgets;
|
using UnityExplorer.UI.Widgets;
|
||||||
|
|
||||||
@ -692,8 +693,22 @@ namespace UnityExplorer.UI
|
|||||||
return dropdownObj;
|
return dropdownObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ScrollPool CreateInfiniteScroll(GameObject parent, string name, out GameObject uiRoot,
|
public static DynamicScrollPool CreateDynamicScrollPool(GameObject parent, string name, out GameObject uiRoot,
|
||||||
out GameObject content, Color? bgColor = null, bool autoResizeSliderHandle = true)
|
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
|
||||||
{
|
{
|
||||||
var mainObj = CreateUIObject(name, parent, new Vector2(1, 1));
|
var mainObj = CreateUIObject(name, parent, new Vector2(1, 1));
|
||||||
mainObj.AddComponent<Image>().color = bgColor ?? new Color(0.12f, 0.12f, 0.12f);
|
mainObj.AddComponent<Image>().color = bgColor ?? new Color(0.12f, 0.12f, 0.12f);
|
||||||
@ -752,10 +767,10 @@ namespace UnityExplorer.UI
|
|||||||
|
|
||||||
uiRoot = mainObj;
|
uiRoot = mainObj;
|
||||||
|
|
||||||
var infiniteScroll = new ScrollPool(scrollRect);
|
var scrollPool = (T)Activator.CreateInstance(typeof(T), new object[] { scrollRect });
|
||||||
infiniteScroll.AutoResizeHandleRect = autoResizeSliderHandle;
|
scrollPool.AutoResizeHandleRect = autoResizeSliderHandle;
|
||||||
|
|
||||||
return infiniteScroll;
|
return scrollPool;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
487
src/UI/Widgets/ScrollPool/Dynamic/DynamicScrollPool.cs
Normal file
487
src/UI/Widgets/ScrollPool/Dynamic/DynamicScrollPool.cs
Normal file
@ -0,0 +1,487 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
17
src/UI/Widgets/ScrollPool/Dynamic/IDynamicCell.cs
Normal file
17
src/UI/Widgets/ScrollPool/Dynamic/IDynamicCell.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
13
src/UI/Widgets/ScrollPool/Dynamic/IDynamicDataSource.cs
Normal file
13
src/UI/Widgets/ScrollPool/Dynamic/IDynamicDataSource.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
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; }
|
||||||
|
}
|
||||||
|
}
|
12
src/UI/Widgets/ScrollPool/IScrollPool.cs
Normal file
12
src/UI/Widgets/ScrollPool/IScrollPool.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace UnityExplorer.UI.Widgets
|
||||||
|
{
|
||||||
|
public interface IScrollPool
|
||||||
|
{
|
||||||
|
bool AutoResizeHandleRect { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -9,7 +9,10 @@ using UnityExplorer.UI.Models;
|
|||||||
|
|
||||||
namespace UnityExplorer.UI.Widgets
|
namespace UnityExplorer.UI.Widgets
|
||||||
{
|
{
|
||||||
public class ScrollPool : UIBehaviourModel
|
/// <summary>
|
||||||
|
/// A <see cref="ScrollRect"/> handler which pools displayed cells to improve performance.
|
||||||
|
/// </summary>
|
||||||
|
public class ScrollPool : UIBehaviourModel, IScrollPool
|
||||||
{
|
{
|
||||||
public ScrollPool(ScrollRect scrollRect)
|
public ScrollPool(ScrollRect scrollRect)
|
||||||
{
|
{
|
||||||
@ -23,11 +26,13 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
|
|
||||||
public override GameObject UIRoot => scrollRect.gameObject;
|
public override GameObject UIRoot => scrollRect.gameObject;
|
||||||
|
|
||||||
/// <summary>Use <see cref="UIFactory.CreateInfiniteScroll"/></summary>
|
/// <summary>Use <see cref="UIFactory.CreateScrollPool"/></summary>
|
||||||
public override void ConstructUI(GameObject parent) => throw new NotImplementedException();
|
public override void ConstructUI(GameObject parent) => throw new NotImplementedException();
|
||||||
|
|
||||||
internal ScrollRect scrollRect;
|
internal ScrollRect scrollRect;
|
||||||
|
|
||||||
|
public bool AutoResizeHandleRect { get; set; }
|
||||||
|
|
||||||
internal RectTransform PrototypeCell;
|
internal RectTransform PrototypeCell;
|
||||||
internal Slider _slider;
|
internal Slider _slider;
|
||||||
|
|
||||||
@ -49,9 +54,6 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
|
|
||||||
internal int currentItemCount; //item count corresponding to the datasource.
|
internal int currentItemCount; //item count corresponding to the datasource.
|
||||||
internal int topMostCellIndex, bottomMostCellIndex; //Topmost and bottommost cell in the heirarchy
|
internal int topMostCellIndex, bottomMostCellIndex; //Topmost and bottommost cell in the heirarchy
|
||||||
internal int _topMostCellColoumn, _bottomMostCellColoumn; // used for recyling in Grid layout. top-most and bottom-most coloumn
|
|
||||||
|
|
||||||
public bool AutoResizeHandleRect;
|
|
||||||
|
|
||||||
public bool ExternallySetting
|
public bool ExternallySetting
|
||||||
{
|
{
|
||||||
@ -61,7 +63,7 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
if (externallySetting == value)
|
if (externallySetting == value)
|
||||||
return;
|
return;
|
||||||
timeOfLastExternalSet = Time.time;
|
timeOfLastExternalSet = Time.time;
|
||||||
externallySetting = true;
|
externallySetting = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private bool externallySetting;
|
private bool externallySetting;
|
||||||
@ -102,12 +104,12 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
scrollRect.m_ContentStartPosition += ProcessValueChange(dir);
|
scrollRect.m_ContentStartPosition += ProcessValueChange(dir);
|
||||||
_prevAnchoredPos = scrollRect.content.anchoredPosition;
|
_prevAnchoredPos = scrollRect.content.anchoredPosition;
|
||||||
|
|
||||||
SetSliderFromScrollValue();
|
UpdateSlider();
|
||||||
|
|
||||||
// ExternallySetting = false;
|
// ExternallySetting = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SetSliderFromScrollValue(bool forceValue = true)
|
internal void UpdateSlider(bool forceValue = true)
|
||||||
{
|
{
|
||||||
int total = DataSource.ItemCount;
|
int total = DataSource.ItemCount;
|
||||||
total = Math.Max(total, 1);
|
total = Math.Max(total, 1);
|
||||||
@ -132,10 +134,9 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
var handleRatio = (decimal)spread / total;
|
var handleRatio = (decimal)spread / total;
|
||||||
var handleHeight = viewportHeight * (float)Math.Min(1, handleRatio);
|
var handleHeight = viewportHeight * (float)Math.Min(1, handleRatio);
|
||||||
|
|
||||||
// minimum handle size
|
|
||||||
handleHeight = Math.Max(handleHeight, 15f);
|
handleHeight = Math.Max(handleHeight, 15f);
|
||||||
|
|
||||||
// need to resize the handle container area for the size of the handle (bigger handle = smaller area)
|
// 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.offsetMax = new Vector2(container.offsetMax.x, -(handleHeight * 0.5f));
|
||||||
container.offsetMin = new Vector2(container.offsetMin.x, handleHeight * 0.5f);
|
container.offsetMin = new Vector2(container.offsetMin.x, handleHeight * 0.5f);
|
||||||
@ -185,7 +186,6 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initialize with the provided DataSource
|
/// Initialize with the provided DataSource
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dataSource"></param>
|
|
||||||
public void Initialize(IPoolDataSource dataSource)
|
public void Initialize(IPoolDataSource dataSource)
|
||||||
{
|
{
|
||||||
DataSource = dataSource;
|
DataSource = dataSource;
|
||||||
@ -242,14 +242,12 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
|
|
||||||
RefreshContentSize();
|
RefreshContentSize();
|
||||||
|
|
||||||
//internallySetting = true;
|
UpdateSlider(false);
|
||||||
SetSliderFromScrollValue(false);
|
|
||||||
//internallySetting = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PopulateCells()
|
public void PopulateCells()
|
||||||
{
|
{
|
||||||
var width = scrollRect.viewport.GetComponent<RectTransform>().rect.width;
|
var width = scrollRect.viewport.rect.width;
|
||||||
scrollRect.content.sizeDelta = new Vector2(width, scrollRect.content.sizeDelta.y);
|
scrollRect.content.sizeDelta = new Vector2(width, scrollRect.content.sizeDelta.y);
|
||||||
|
|
||||||
int cellIndex = topMostCellIndex;
|
int cellIndex = topMostCellIndex;
|
||||||
@ -259,8 +257,6 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
{
|
{
|
||||||
var cell = _cachedCells[cellIndex];
|
var cell = _cachedCells[cellIndex];
|
||||||
cellIndex++;
|
cellIndex++;
|
||||||
if (cellIndex < 0)
|
|
||||||
continue;
|
|
||||||
if (cellIndex >= _cachedCells.Count)
|
if (cellIndex >= _cachedCells.Count)
|
||||||
cellIndex = 0;
|
cellIndex = 0;
|
||||||
DataSource.SetCell(cell, itemIndex);
|
DataSource.SetCell(cell, itemIndex);
|
||||||
@ -284,7 +280,7 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
yield return null;
|
yield return null;
|
||||||
SetRecyclingBounds();
|
SetRecyclingBounds();
|
||||||
|
|
||||||
//Cell Poool
|
//Cell Pool
|
||||||
CreateCellPool();
|
CreateCellPool();
|
||||||
currentItemCount = _cellPool.Count;
|
currentItemCount = _cellPool.Count;
|
||||||
topMostCellIndex = 0;
|
topMostCellIndex = 0;
|
||||||
@ -334,9 +330,6 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
PrototypeCell.gameObject.SetActive(true);
|
PrototypeCell.gameObject.SetActive(true);
|
||||||
SetTopAnchor(PrototypeCell);
|
SetTopAnchor(PrototypeCell);
|
||||||
|
|
||||||
//Reset
|
|
||||||
_topMostCellColoumn = _bottomMostCellColoumn = 0;
|
|
||||||
|
|
||||||
//Temps
|
//Temps
|
||||||
float currentPoolCoverage = 0;
|
float currentPoolCoverage = 0;
|
||||||
int poolSize = 0;
|
int poolSize = 0;
|
||||||
@ -366,7 +359,7 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
//Setting data for Cell
|
//Setting data for Cell
|
||||||
var cell = DataSource.CreateCell(item);
|
var cell = DataSource.CreateCell(item);
|
||||||
_cachedCells.Add(cell);
|
_cachedCells.Add(cell);
|
||||||
DataSource.SetCell(_cachedCells[_cachedCells.Count - 1], poolSize);
|
DataSource.SetCell(cell, poolSize);
|
||||||
|
|
||||||
//Update the Pool size
|
//Update the Pool size
|
||||||
poolSize++;
|
poolSize++;
|
||||||
|
@ -4,6 +4,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.UI;
|
||||||
using UnityExplorer.UI.Widgets;
|
using UnityExplorer.UI.Widgets;
|
||||||
|
|
||||||
namespace UnityExplorer.UI.Widgets
|
namespace UnityExplorer.UI.Widgets
|
||||||
@ -12,15 +13,13 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
{
|
{
|
||||||
internal ScrollPool Scroller;
|
internal ScrollPool Scroller;
|
||||||
|
|
||||||
public Func<List<T>> GetEntries;
|
public int ItemCount => currentEntries.Count;
|
||||||
public List<T> currentEntries;
|
public List<T> currentEntries;
|
||||||
|
|
||||||
public int ItemCount => currentEntries.Count;
|
public Func<List<T>> GetEntries;
|
||||||
|
|
||||||
public Func<RectTransform, SimpleCell<T>> CreateICell;
|
|
||||||
public Action<SimpleCell<T>, int> SetICell;
|
public Action<SimpleCell<T>, int> SetICell;
|
||||||
|
public Func<T, string, bool> ShouldDisplay;
|
||||||
public Func<T, string, bool> ShouldFilter;
|
public Action<SimpleCell<T>> OnCellClicked;
|
||||||
|
|
||||||
public string CurrentFilter
|
public string CurrentFilter
|
||||||
{
|
{
|
||||||
@ -30,15 +29,15 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
private string currentFilter;
|
private string currentFilter;
|
||||||
|
|
||||||
public SimpleListSource(ScrollPool infiniteScroller, Func<List<T>> getEntriesMethod,
|
public SimpleListSource(ScrollPool infiniteScroller, Func<List<T>> getEntriesMethod,
|
||||||
Func<RectTransform, SimpleCell<T>> createCellMethod, Action<SimpleCell<T>, int> setICellMethod,
|
Action<SimpleCell<T>, int> setICellMethod, Func<T, string, bool> shouldDisplayMethod,
|
||||||
Func<T, string, bool> shouldFilterMethod)
|
Action<SimpleCell<T>> onCellClickedMethod)
|
||||||
{
|
{
|
||||||
Scroller = infiniteScroller;
|
Scroller = infiniteScroller;
|
||||||
|
|
||||||
GetEntries = getEntriesMethod;
|
GetEntries = getEntriesMethod;
|
||||||
CreateICell = createCellMethod;
|
|
||||||
SetICell = setICellMethod;
|
SetICell = setICellMethod;
|
||||||
ShouldFilter = shouldFilterMethod;
|
ShouldDisplay = shouldDisplayMethod;
|
||||||
|
OnCellClicked = onCellClickedMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Init()
|
public void Init()
|
||||||
@ -64,7 +63,7 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(currentFilter))
|
if (!string.IsNullOrEmpty(currentFilter))
|
||||||
{
|
{
|
||||||
if (!ShouldFilter.Invoke(entry, currentFilter))
|
if (!ShouldDisplay.Invoke(entry, currentFilter))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
list.Add(entry);
|
list.Add(entry);
|
||||||
@ -76,9 +75,13 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
currentEntries = list;
|
currentEntries = list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICell CreateCell(RectTransform cellTransform)
|
public ICell CreateCell(RectTransform rect)
|
||||||
{
|
{
|
||||||
return CreateICell.Invoke(cellTransform);
|
var button = rect.GetComponentInChildren<Button>();
|
||||||
|
var text = button.GetComponentInChildren<Text>();
|
||||||
|
var cell = new SimpleCell<T>(this, rect.gameObject, button, text);
|
||||||
|
cell.OnClick += OnCellClicked;
|
||||||
|
return cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetCell(ICell cell, int index)
|
public void SetCell(ICell cell, int index)
|
||||||
|
@ -272,10 +272,14 @@
|
|||||||
<Compile Include="UI\Utility\PanelDragger.cs" />
|
<Compile Include="UI\Utility\PanelDragger.cs" />
|
||||||
<Compile Include="UI\Utility\SignatureHighlighter.cs" />
|
<Compile Include="UI\Utility\SignatureHighlighter.cs" />
|
||||||
<Compile Include="UI\Utility\ToStringUtility.cs" />
|
<Compile Include="UI\Utility\ToStringUtility.cs" />
|
||||||
<Compile Include="UI\Widgets\InfiniteScroll\ICell.cs" />
|
<Compile Include="UI\Widgets\ScrollPool\Dynamic\IDynamicCell.cs" />
|
||||||
<Compile Include="UI\Widgets\InfiniteScroll\IListDataSource.cs" />
|
<Compile Include="UI\Widgets\ScrollPool\ICell.cs" />
|
||||||
<Compile Include="UI\Widgets\InfiniteScroll\InfiniteScrollRect.cs" />
|
<Compile Include="UI\Widgets\ScrollPool\Dynamic\IDynamicDataSource.cs" />
|
||||||
<Compile Include="UI\Widgets\InfiniteScroll\UIExtensions.cs" />
|
<Compile Include="UI\Widgets\ScrollPool\IPoolDataSource.cs" />
|
||||||
|
<Compile Include="UI\Widgets\ScrollPool\IScrollPool.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\InputFieldScroller.cs" />
|
||||||
<Compile Include="UI\Widgets\SimpleList\SimpleCell.cs" />
|
<Compile Include="UI\Widgets\SimpleList\SimpleCell.cs" />
|
||||||
<Compile Include="UI\Widgets\SimpleList\SimpleListSource.cs" />
|
<Compile Include="UI\Widgets\SimpleList\SimpleListSource.cs" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user