* Using publicized mono assemblies
* Remaking UI from scratch. Done the Scene Explorer so far.
This commit is contained in:
Sinai
2021-04-15 20:18:03 +10:00
parent a6c24f91e4
commit 7eb4b1bc77
84 changed files with 1819 additions and 9082 deletions

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnityExplorer.UI.Widgets.InfiniteScroll
{
public interface ICell
{
bool Enabled { get; }
void Enable();
void Disable();
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnityExplorer.UI.Widgets.InfiniteScroll
{
public interface IListDataSource
{
int ItemCount { get; }
void SetCell(ICell cell, int index);
}
}

View File

@ -0,0 +1,497 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
namespace UnityExplorer.UI.Widgets.InfiniteScroll
{
public class InfiniteScrollRect : ScrollRect
{
public IListDataSource DataSource;
internal RectTransform PrototypeCell;
internal Slider _slider;
// Cell pool
private float _cellWidth, _cellHeight;
private List<RectTransform> _cellPool;
private List<ICell> _cachedCells;
private Bounds _recyclableViewBounds;
//Temps, Flags
private readonly Vector3[] _corners = new Vector3[4];
private bool _recycling;
private Vector2 _prevAnchoredPos;
//Trackers
internal int currentItemCount; //item count corresponding to the datasource.
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
private Vector2 zeroVector = Vector2.zero;
// Flag to keep track of when we are manually setting our slider/scrollrect value directly, to avoid callback loops.
private bool internallySetting = false;
// external sources use this flag, it will stay true until the start of the next frame to prevent our update overwriting it.
public bool ExternallySetting
{
get => externallySetting;
internal set
{
if (externallySetting == value)
return;
timeOfLastExternalSet = Time.time;
externallySetting = true;
}
}
private bool externallySetting;
private float timeOfLastExternalSet;
internal new void Start()
{
// Link up the Slider and ScrollRect onValueChanged to sync them.
_slider = this.GetComponentInChildren<Slider>();
onValueChanged.AddListener((Vector2 val) =>
{
try
{
if (internallySetting || ExternallySetting)
return;
internallySetting = true;
SetSliderFromScrollValue();
internallySetting = false;
}
catch (Exception ex)
{
ExplorerCore.Log(ex);
}
});
_slider.onValueChanged.AddListener((float val) =>
{
if (internallySetting || ExternallySetting)
return;
internallySetting = 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);
internallySetting = false;
});
}
internal void SetSliderFromScrollValue()
{
// calculate where slider handle should be based on displayed range.
var range = GetDisplayedRange();
int total = DataSource.ItemCount;
var spread = range.y - range.x;
//var orig = _slider.value;
if (spread >= total)
_slider.value = 0f;
else
// top-most displayed index divided by (totalCount - displayedRange)
_slider.value = (float)((decimal)range.x / (decimal)(total - spread));
}
internal void Update()
{
if (externallySetting && timeOfLastExternalSet < Time.time)
externallySetting = false;
}
/// <summary>
/// public API for Initializing when datasource is not set in controller's Awake. Make sure selfInitalize is set to false.
/// </summary>
public void Initialize(IListDataSource dataSource)
{
DataSource = dataSource;
vertical = true;
horizontal = false;
_prevAnchoredPos = base.content.anchoredPosition;
onValueChanged.RemoveListener(OnValueChangedListener);
RuntimeProvider.Instance.StartCoroutine(InitCoroutine(() =>
{
onValueChanged.AddListener(OnValueChangedListener);
}));
}
/// <summary>
/// Added as a listener to the OnValueChanged event of Scroll rect.
/// Recycling entry point for recyling systems.
/// </summary>
/// <param name="direction">scroll direction</param>
internal void OnValueChangedListener(Vector2 normalizedPos)
{
Vector2 dir = base.content.anchoredPosition - _prevAnchoredPos;
m_ContentStartPosition += ProcessValueChange(dir);
_prevAnchoredPos = base.content.anchoredPosition;
}
/// <summary>
///Reloads the data. Call this if a new datasource is assigned.
/// </summary>
public void ReloadData()
{
ReloadData(DataSource);
}
/// <summary>
/// Overloaded ReloadData with dataSource param
///Reloads the data. Call this if a new datasource is assigned.
/// </summary>
public void ReloadData(IListDataSource dataSource)
{
if (onValueChanged == null)
return;
StopMovement();
onValueChanged.RemoveListener(OnValueChangedListener);
DataSource = dataSource;
RuntimeProvider.Instance.StartCoroutine(InitCoroutine(() =>
onValueChanged.AddListener(OnValueChangedListener)
));
_prevAnchoredPos = base.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();
//// Close, but not quite accurate enough to be useful.
//internallySetting = true;
//SetSliderFromScrollValue();
//internallySetting = false;
}
public Vector2 GetDisplayedRange()
{
int max = currentItemCount;
int min = max - _cachedCells.Count;
return new Vector2(min, max);
}
public void JumpToIndex(int index)
{
var realCount = DataSource.ItemCount;
index = Math.Min(index, realCount - 1);
var indexBuffer = (int)(_cachedCells.Count * (1 - (index / (decimal)(realCount - 1))));
currentItemCount = index + indexBuffer;// Math.Max(index + _cachedCells.Count, currentItemCount);
currentItemCount = Math.Max(Math.Min(currentItemCount, realCount), _cachedCells.Count);
Refresh();
var y = 0f;
var displayRange = viewport.rect.height / _cellHeight;
var poolRange = content.rect.height / _cellHeight;
var poolExtra = poolRange - displayRange;
if (index >= realCount - poolExtra)
y = _cellHeight * (index - realCount + poolExtra);
content.anchoredPosition = new Vector2(content.anchoredPosition.x, y);
}
public void PopulateCells()
{
var width = viewport.GetComponent<RectTransform>().rect.width;
content.sizeDelta = new Vector2(width, 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 < 0)
continue;
if (cellIndex >= _cachedCells.Count)
cellIndex = 0;
DataSource.SetCell(cell, itemIndex);
itemIndex++;
var rect = (cell as Component).GetComponent<RectTransform>();
rect.sizeDelta = new Vector2(width, rect.sizeDelta.y);
iterated++;
}
}
#region RECYCLING INIT
/// <summary>
/// Corotuine for initiazation.
/// Using coroutine for init because few UI stuff requires a frame to update
/// </summary>
/// <param name="onInitialized">callback when init done</param>
/// <returns></returns>>
public IEnumerator InitCoroutine(Action onInitialized)
{
yield return null;
SetTopAnchor(content);
content.anchoredPosition = Vector3.zero;
yield return null;
SetRecyclingBounds();
//Cell Poool
CreateCellPool();
currentItemCount = _cellPool.Count;
topMostCellIndex = 0;
bottomMostCellIndex = _cellPool.Count - 1;
//Set content height according to no of rows
RefreshContentSize();
SetTopAnchor(content);
onInitialized?.Invoke();
}
private void RefreshContentSize()
{
int noOfRows = _cachedCells.Where(it => it.Enabled).Count();
float contentYSize = noOfRows * _cellHeight;
content.sizeDelta = new Vector2(content.sizeDelta.x, contentYSize);
}
/// <summary>
/// Sets the uppper and lower bounds for recycling cells.
/// </summary>
private void SetRecyclingBounds()
{
viewport.GetWorldCorners(_corners);
float threshHold = _cellHeight / 2; //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);
}
/// <summary>
/// Creates cell Pool for recycling, Caches ICells
/// </summary>
private void CreateCellPool()
{
//Reseting Pool
if (_cellPool != null)
{
_cellPool.ForEach((RectTransform item) => 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);
//Reset
_topMostCellColoumn = _bottomMostCellColoumn = 0;
//Temps
float currentPoolCoverage = 0;
int poolSize = 0;
float posY = 0;
//set new cell size according to its aspect ratio
_cellWidth = content.rect.width;
_cellHeight = PrototypeCell.rect.height;
//Get the required pool coverage and mininum size for the Cell pool
float requiredCoverage = viewport.rect.height + (_cellHeight * 2);
//create cells untill the Pool area is covered
while (currentPoolCoverage < requiredCoverage)
{
//Instantiate and add to Pool
RectTransform item = Instantiate(PrototypeCell.gameObject).GetComponent<RectTransform>();
item.name = $"Cell_{_cachedCells.Count + 1}";
item.sizeDelta = new Vector2(_cellWidth, _cellHeight);
_cellPool.Add(item);
item.SetParent(content, false);
item.anchoredPosition = new Vector2(0, posY);
posY = item.anchoredPosition.y - item.rect.height;
currentPoolCoverage += item.rect.height;
//Setting data for Cell
_cachedCells.Add(item.GetComponent<ICell>());
DataSource.SetCell(_cachedCells[_cachedCells.Count - 1], 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
internal Vector2 LastScroll;
/// <summary>
/// Recyling entry point
/// </summary>
/// <param name="direction">scroll direction </param>
/// <returns></returns>
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);
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);
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

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace UnityExplorer.UI.Widgets.InfiniteScroll
{
public static class UIExtension
{
public static Vector3[] GetCorners(this RectTransform rectTransform)
{
Vector3[] corners = new Vector3[4];
rectTransform.GetWorldCorners(corners);
return corners;
}
public static float MaxY(this RectTransform rectTransform)
{
return rectTransform.GetCorners()[1].y;
}
public static float MinY(this RectTransform rectTransform)
{
return rectTransform.GetCorners()[0].y;
}
public static float MaxX(this RectTransform rectTransform)
{
return rectTransform.GetCorners()[2].x;
}
public static float MinX(this RectTransform rectTransform)
{
return rectTransform.GetCorners()[0].x;
}
}
}

View File

@ -0,0 +1,132 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
namespace UnityExplorer.UI.Utility
{
// To fix an issue with Input Fields and allow them to go inside a ScrollRect nicely.
public class InputFieldScroller
{
public static readonly List<InputFieldScroller> Instances = new List<InputFieldScroller>();
public static void UpdateInstances()
{
if (!Instances.Any())
return;
for (int i = 0; i < Instances.Count; i++)
{
var input = Instances[i];
if (input.CheckDestroyed())
i--;
else
input.Update();
}
}
internal SliderScrollbar sliderScroller;
internal InputField inputField;
internal RectTransform inputRect;
internal LayoutElement layoutElement;
internal VerticalLayoutGroup parentLayoutGroup;
internal static CanvasScaler canvasScaler;
public InputFieldScroller(SliderScrollbar sliderScroller, InputField inputField)
{
Instances.Add(this);
this.sliderScroller = sliderScroller;
this.inputField = inputField;
sliderScroller.m_parentInputScroller = this;
inputField.onValueChanged.AddListener(OnTextChanged);
inputRect = inputField.GetComponent<RectTransform>();
layoutElement = inputField.gameObject.AddComponent<LayoutElement>();
parentLayoutGroup = inputField.transform.parent.GetComponent<VerticalLayoutGroup>();
layoutElement.minHeight = 25;
layoutElement.minWidth = 100;
if (!canvasScaler)
canvasScaler = UIManager.CanvasRoot.GetComponent<CanvasScaler>();
}
internal string m_lastText;
internal bool m_updateWanted;
// only done once, to fix height on creation.
internal bool heightInitAfterLayout;
public void Update()
{
if (!heightInitAfterLayout)
{
heightInitAfterLayout = true;
var height = sliderScroller.m_scrollRect.parent.parent.GetComponent<RectTransform>().rect.height;
layoutElement.preferredHeight = height;
}
if (m_updateWanted && inputField.gameObject.activeInHierarchy)
{
m_updateWanted = false;
RefreshUI();
}
}
internal bool CheckDestroyed()
{
if (sliderScroller == null || sliderScroller.CheckDestroyed())
{
Instances.Remove(this);
return true;
}
return false;
}
internal void OnTextChanged(string text)
{
m_lastText = text;
m_updateWanted = true;
}
internal void RefreshUI()
{
var curInputRect = inputField.textComponent.rectTransform.rect;
var scaleFactor = canvasScaler.scaleFactor;
// Current text settings
var texGenSettings = inputField.textComponent.GetGenerationSettings(curInputRect.size);
texGenSettings.generateOutOfBounds = false;
texGenSettings.scaleFactor = scaleFactor;
// Preferred text rect height
var textGen = inputField.textComponent.cachedTextGeneratorForLayout;
float preferredHeight = textGen.GetPreferredHeight(m_lastText, texGenSettings) + 10;
// Default text rect height (fit to scroll parent or expand to fit text)
float minHeight = Mathf.Max(preferredHeight, sliderScroller.m_scrollRect.rect.height - 25);
layoutElement.preferredHeight = minHeight;
if (inputField.caretPosition == inputField.text.Length
&& inputField.text.Length > 0
&& inputField.text[inputField.text.Length - 1] == '\n')
{
sliderScroller.m_slider.value = 0f;
}
}
}
}

View File

@ -0,0 +1,200 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityExplorer.Core.Config;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.UI.Utility;
namespace UnityExplorer.UI.Models
{
public enum Turn
{
Left,
Right
}
public class PageHandler : UIModel, IEnumerator
{
public PageHandler(SliderScrollbar scroll)
{
ItemsPerPage = ConfigManager.Default_Page_Limit?.Value ?? 20;
m_scrollbar = scroll;
}
public event Action OnPageChanged;
// UI members
private readonly SliderScrollbar m_scrollbar;
private GameObject m_pageUIHolder;
private Text m_currentPageLabel;
public override GameObject UIRoot => m_pageUIHolder;
// For now this is just set when the PageHandler is created, based on config.
// At some point I might make it possible to change this after creation again.
public int ItemsPerPage { get; }
// IEnumerator.Current
public object Current => m_currentIndex;
private int m_currentIndex = 0;
public int CurrentPage
{
get => m_currentPage;
set
{
if (value < PageCount)
m_currentPage = value;
}
}
private int m_currentPage;
// set and maintained by owner of list
private int m_listCount;
public int ListCount
{
get => m_listCount;
set
{
m_listCount = value;
if (PageCount <= 0 && m_pageUIHolder.activeSelf)
{
m_pageUIHolder.SetActive(false);
}
else if (PageCount > 0 && !m_pageUIHolder.activeSelf)
{
m_pageUIHolder.SetActive(true);
}
RefreshUI();
}
}
public int PageCount => (int)Math.Ceiling(ListCount / (decimal)ItemsPerPage) - 1;
// The index of the first element of the current page
public int StartIndex
{
get
{
int offset = m_currentPage * ItemsPerPage;
if (offset >= ListCount)
{
offset = 0;
m_currentPage = 0;
}
return offset;
}
}
public int EndIndex
{
get
{
int end = StartIndex + ItemsPerPage;
if (end >= ListCount)
end = ListCount - 1;
return end;
}
}
// IEnumerator.MoveNext()
public bool MoveNext()
{
m_currentIndex++;
return m_currentIndex < StartIndex + ItemsPerPage;
}
// IEnumerator.Reset()
public void Reset()
{
m_currentIndex = StartIndex - 1;
}
public IEnumerator<int> GetEnumerator()
{
Reset();
while (MoveNext())
{
yield return m_currentIndex;
}
}
public void TurnPage(Turn direction)
{
bool didTurn = false;
if (direction == Turn.Left)
{
if (m_currentPage > 0)
{
m_currentPage--;
didTurn = true;
}
}
else
{
if (m_currentPage < PageCount)
{
m_currentPage++;
didTurn = true;
}
}
if (didTurn)
{
if (m_scrollbar != null)
m_scrollbar.m_scrollbar.value = 1;
OnPageChanged?.Invoke();
RefreshUI();
}
}
public void Show() => m_pageUIHolder?.SetActive(true);
public void Hide() => m_pageUIHolder?.SetActive(false);
public void RefreshUI()
{
m_currentPageLabel.text = $"Page {CurrentPage + 1} / {CurrentPage + 1}";
// TODO
}
public override void ConstructUI(GameObject parent)
{
m_pageUIHolder = UIFactory.CreateHorizontalGroup(parent, "PageHandlerButtons", false, true, true, true);
Image image = m_pageUIHolder.GetComponent<Image>();
image.color = new Color(0.2f, 0.2f, 0.2f, 0.5f);
UIFactory.SetLayoutElement(m_pageUIHolder, minHeight: 25, minWidth: 100, flexibleWidth: 5000);
var leftBtnObj = UIFactory.CreateButton(m_pageUIHolder,
"BackBtn",
"◄",
() => { TurnPage(Turn.Left); },
new Color(0.15f, 0.15f, 0.15f));
UIFactory.SetLayoutElement(leftBtnObj.gameObject, flexibleWidth: 1500, minWidth: 25, minHeight: 25);
m_currentPageLabel = UIFactory.CreateLabel(m_pageUIHolder, "PageLabel", "Page 1 / TODO", TextAnchor.MiddleCenter);
UIFactory.SetLayoutElement(m_currentPageLabel.gameObject, minWidth: 100, flexibleWidth: 40);
Button rightBtn = UIFactory.CreateButton(m_pageUIHolder,
"RightBtn",
"►",
() => { TurnPage(Turn.Right); },
new Color(0.15f, 0.15f, 0.15f));
UIFactory.SetLayoutElement(rightBtn.gameObject, flexibleWidth: 1500, minWidth: 25, minHeight: 25);
ListCount = 0;
}
}
}

View File

@ -0,0 +1,184 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityExplorer;
using UnityExplorer.Core;
using UnityExplorer.UI;
namespace UnityExplorer.UI.Utility
{
// Basically just to fix an issue with Scrollbars, instead we use a Slider as the scrollbar.
public class SliderScrollbar
{
internal static readonly List<SliderScrollbar> Instances = new List<SliderScrollbar>();
public static void UpdateInstances()
{
if (!Instances.Any())
return;
for (int i = 0; i < Instances.Count; i++)
{
var slider = Instances[i];
if (slider.CheckDestroyed())
i--;
else
slider.Update();
}
}
public bool IsActive { get; private set; }
public event Action<float> OnValueChanged;
internal readonly Scrollbar m_scrollbar;
internal readonly Slider m_slider;
internal readonly RectTransform m_scrollRect;
internal InputFieldScroller m_parentInputScroller;
public SliderScrollbar(Scrollbar scrollbar, Slider slider)
{
Instances.Add(this);
this.m_scrollbar = scrollbar;
this.m_slider = slider;
this.m_scrollRect = scrollbar.transform.parent.GetComponent<RectTransform>();
this.m_scrollbar.onValueChanged.AddListener(this.OnScrollbarValueChanged);
this.m_slider.onValueChanged.AddListener(this.OnSliderValueChanged);
this.RefreshVisibility();
this.m_slider.Set(1f, false);
}
internal bool CheckDestroyed()
{
if (!m_slider || !m_scrollbar)
{
Instances.Remove(this);
return true;
}
return false;
}
internal void Update()
{
this.RefreshVisibility();
}
internal void RefreshVisibility()
{
if (!m_slider.gameObject.activeInHierarchy)
{
IsActive = false;
return;
}
bool shouldShow = !Mathf.Approximately(this.m_scrollbar.size, 1);
var obj = this.m_slider.handleRect.gameObject;
if (IsActive != shouldShow)
{
IsActive = shouldShow;
obj.SetActive(IsActive);
if (IsActive)
this.m_slider.Set(this.m_scrollbar.value, false);
else
m_slider.Set(1f, false);
}
}
public void OnScrollbarValueChanged(float _value)
{
if (this.m_slider.value != _value)
this.m_slider.Set(_value, false);
OnValueChanged?.Invoke(_value);
}
public void OnSliderValueChanged(float _value)
{
this.m_scrollbar.value = _value;
OnValueChanged?.Invoke(_value);
}
#region UI CONSTRUCTION
public static GameObject CreateSliderScrollbar(GameObject parent, out Slider slider)
{
GameObject sliderObj = UIFactory.CreateUIObject("SliderScrollbar", parent, UIFactory._smallElementSize);
GameObject bgObj = UIFactory.CreateUIObject("Background", sliderObj);
GameObject fillAreaObj = UIFactory.CreateUIObject("Fill Area", sliderObj);
GameObject fillObj = UIFactory.CreateUIObject("Fill", fillAreaObj);
GameObject handleSlideAreaObj = UIFactory.CreateUIObject("Handle Slide Area", sliderObj);
GameObject handleObj = UIFactory.CreateUIObject("Handle", handleSlideAreaObj);
Image bgImage = bgObj.AddComponent<Image>();
bgImage.type = Image.Type.Sliced;
bgImage.color = new Color(0.05f, 0.05f, 0.05f, 1.0f);
RectTransform bgRect = bgObj.GetComponent<RectTransform>();
bgRect.anchorMin = Vector2.zero;
bgRect.anchorMax = Vector2.one;
bgRect.sizeDelta = Vector2.zero;
bgRect.offsetMax = new Vector2(0f, 0f);
RectTransform fillAreaRect = fillAreaObj.GetComponent<RectTransform>();
fillAreaRect.anchorMin = new Vector2(0f, 0.20f);
fillAreaRect.anchorMax = new Vector2(1f, 0.8f);
fillAreaRect.anchoredPosition = new Vector2(0f, 0f);
fillAreaRect.sizeDelta = new Vector2(-20f, 0f);
Image fillImage = fillObj.AddComponent<Image>();
fillImage.type = Image.Type.Sliced;
fillImage.color = Color.clear;
fillObj.GetComponent<RectTransform>().sizeDelta = new Vector2(10f, 0f);
RectTransform handleSlideRect = handleSlideAreaObj.GetComponent<RectTransform>();
handleSlideRect.anchorMin = new Vector2(0f, 0f);
handleSlideRect.anchorMax = new Vector2(1f, 1f);
handleSlideRect.offsetMin = new Vector2(25f, 30f);
handleSlideRect.offsetMax = new Vector2(-15f, 0f);
handleSlideRect.sizeDelta = new Vector2(-20f, -30f);
Image handleImage = handleObj.AddComponent<Image>();
handleImage.color = new Color(0.5f, 0.5f, 0.5f, 1.0f);
var handleRect = handleObj.GetComponent<RectTransform>();
handleRect.sizeDelta = new Vector2(15f, 30f);
handleRect.offsetMin = new Vector2(-13f, -28f);
handleRect.offsetMax = new Vector2(2f, -2f);
var sliderBarLayout = sliderObj.AddComponent<LayoutElement>();
sliderBarLayout.minWidth = 25;
sliderBarLayout.flexibleWidth = 0;
sliderBarLayout.minHeight = 30;
sliderBarLayout.flexibleHeight = 5000;
slider = sliderObj.AddComponent<Slider>();
slider.fillRect = fillObj.GetComponent<RectTransform>();
slider.handleRect = handleObj.GetComponent<RectTransform>();
slider.targetGraphic = handleImage;
slider.direction = Slider.Direction.BottomToTop;
RuntimeProvider.Instance.SetColorBlock(
slider,
new Color(0.25f, 0.25f, 0.25f),
new Color(0.3f, 0.3f, 0.3f),
new Color(0.2f, 0.2f, 0.2f));
return sliderObj;
}
#endregion
}
}

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace UnityExplorer.UI.Widgets
{
public class CachedTransform
{
public Transform RefTransform { get; }
public CachedTransform Parent { get; internal set; }
public string Name { get; internal set; }
public int ChildCount { get; internal set; }
public int Depth { get; internal set; }
public bool Expanded { get; set; }
public CachedTransform(Transform transform, CachedTransform parent = null)
{
RefTransform = transform;
Expanded = false;
Parent = parent;
Update();
}
public void Update()
{
Name = RefTransform.name;
ChildCount = RefTransform.childCount;
Depth = Parent?.Depth + 1 ?? 0;
}
}
}

View File

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.UI.Widgets.InfiniteScroll;
namespace UnityExplorer.UI.Widgets
{
public class TransformCell : MonoBehaviour, ICell
{
public bool Enabled => m_enabled;
private bool m_enabled;
public TransformTree tree;
internal CachedTransform cachedTransform;
internal int _cellIndex;
public Text nameLabel;
public Button nameButton;
public Text expandLabel;
public Button expandButton;
public LayoutElement spacer;
internal void Start()
{
nameButton.onClick.AddListener(OnMainButtonClicked);
expandButton.onClick.AddListener(OnExpandClicked);
}
//This is called from the SetCell method in DataSource
public void ConfigureCell(CachedTransform cached, int cellIndex)
{
if (!Enabled)
Enable();
_cellIndex = cellIndex;
cachedTransform = cached;
spacer.minWidth = cached.Depth * 15;
nameLabel.text = cached.Name;
nameLabel.color = cached.RefTransform.gameObject.activeSelf ? Color.white : Color.grey;
if (cached.ChildCount > 0)
{
nameLabel.text = $"<color=grey>[{cached.ChildCount}]</color> {nameLabel.text}";
expandButton.interactable = true;
expandLabel.enabled = true;
expandLabel.text = cached.Expanded ? "▼" : "►";
expandLabel.color = cached.Expanded ? new Color(0.5f, 0.5f, 0.5f) : new Color(0.3f, 0.3f, 0.3f);
}
else
{
expandButton.interactable = false;
expandLabel.enabled = false;
}
}
public void Disable()
{
m_enabled = false;
this.gameObject.SetActive(false);
}
public void Enable()
{
m_enabled = true;
this.gameObject.SetActive(true);
}
public void OnExpandClicked()
{
tree.ToggleExpandCell(this);
}
public void OnMainButtonClicked()
{
Debug.Log($"TODO Inspect {cachedTransform.RefTransform.name}");
}
}
}

View File

@ -0,0 +1,181 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.Core;
using UnityExplorer.UI.Widgets.InfiniteScroll;
namespace UnityExplorer.UI.Widgets
{
public class TransformTree : MonoBehaviour, IListDataSource
{
public Func<IEnumerable<GameObject>> GetRootEntriesMethod;
public string CurrentFilter
{
get => currentFilter;
set => currentFilter = value?.ToLower() ?? "";
}
private string currentFilter;
internal InfiniteScrollRect infiniteScroll;
//internal readonly List<CachedTransform> objectTree = new List<CachedTransform>();
internal readonly Dictionary<IntPtr, CachedTransform> objectCache = new Dictionary<IntPtr, CachedTransform>();
internal Dictionary<IntPtr, CachedTransform> tempObjectCache;
public int ItemCount => objectCache.Count;
internal void Awake()
{
RuntimeProvider.Instance.StartCoroutine(InitCoroutine());
}
private IEnumerator InitCoroutine()
{
yield return null;
// stress test
//for (int i = 0; i < 10000; i++)
// new GameObject(i.ToString());
var root = new GameObject().transform;
for (int i = 0; i < 100; i++)
{
var obj = new GameObject(i.ToString());
obj.transform.parent = root;
for (int j = 0; j < 100; j++)
new GameObject(j.ToString()).transform.parent = obj.transform;
}
RefreshData();
infiniteScroll.DataSource = this;
infiniteScroll.Initialize(this);
}
public void RefreshData(bool andReload = false)
{
tempObjectCache = objectCache.ToDictionary(it => it.Key, it => it.Value);
objectCache.Clear();
var objects = GetRootEntriesMethod.Invoke();
foreach (var obj in objects)
Traverse(obj.transform);
if (andReload)
infiniteScroll.Refresh();
}
private void Traverse(Transform transform, CachedTransform parent = null)
{
CachedTransform cached;
if (tempObjectCache.ContainsKey(transform.m_CachedPtr))
{
cached = tempObjectCache[transform.m_CachedPtr];
cached.Update();
}
else
cached = new CachedTransform(transform, parent);
if (!string.IsNullOrEmpty(CurrentFilter))
{
if (!FilterHierarchy(transform))
return;
// auto-expand to show results: works, but then we need to collapse after the search ends.
//if (FilterHierarchy(transform))
// cached.Expanded = true;
//else
// return;
}
objectCache.Add(transform.m_CachedPtr, cached);
if (cached.Expanded && cached.ChildCount > 0)
{
for (int i = 0; i < transform.childCount; i++)
Traverse(transform.GetChild(i), cached);
}
}
private bool FilterHierarchy(Transform obj)
{
if (obj.name.ToLower().Contains(currentFilter))
return true;
if (obj.childCount <= 0)
return false;
for (int i = 0; i < obj.childCount; i++)
if (FilterHierarchy(obj.GetChild(i)))
return true;
return false;
}
public void SetCell(ICell iCell, int index)
{
var cell = iCell as TransformCell;
if (index < objectCache.Count)
cell.ConfigureCell(objectCache.ElementAt(index).Value, index);
else
cell.Disable();
}
public void ToggleExpandCell(TransformCell cell)
{
cell.cachedTransform.Expanded = !cell.cachedTransform.Expanded;
RefreshData(true);
}
public GameObject CreatePrototypeCell(GameObject parent, TransformTree tree)
{
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 spacer = UIFactory.CreateUIObject("Spacer", prototype, new Vector2(0,0));
UIFactory.SetLayoutElement(spacer, minWidth: 0, flexibleWidth: 0, minHeight: 0, flexibleHeight: 0);
var expandButton = UIFactory.CreateButton(prototype, "ExpandButton", "►", null);
UIFactory.SetLayoutElement(expandButton.gameObject, minWidth: 15, flexibleWidth: 0, minHeight: 25, flexibleHeight: 0);
var nameButton = UIFactory.CreateButton(prototype, "NameButton", "Name", null);
UIFactory.SetLayoutElement(nameButton.gameObject, flexibleWidth: 9999, minHeight: 25, flexibleHeight: 0);
nameButton.GetComponentInChildren<Text>().horizontalOverflow = HorizontalWrapMode.Overflow;
Color normal = new Color(0.15f, 0.15f, 0.15f);
Color highlight = new Color(0.25f, 0.25f, 0.25f);
Color pressed = new Color(0.05f, 0.05f, 0.05f);
Color disabled = new Color(1, 1, 1, 0);
RuntimeProvider.Instance.SetColorBlock(expandButton, normal, highlight, pressed, disabled);
RuntimeProvider.Instance.SetColorBlock(nameButton, normal, highlight, pressed, disabled);
cell.tree = tree;
cell.nameButton = nameButton;
cell.nameLabel = nameButton.GetComponentInChildren<Text>();
cell.nameLabel.alignment = TextAnchor.MiddleLeft;
cell.expandButton = expandButton;
cell.expandLabel = expandButton.GetComponentInChildren<Text>();
cell.spacer = spacer.GetComponent<LayoutElement>();
prototype.SetActive(false);
return prototype;
}
}
}