diff --git a/src/UI/Inspectors/CacheObject/CacheObjectBase.cs b/src/UI/Inspectors/CacheObject/CacheObjectBase.cs index 65d3247..33427d1 100644 --- a/src/UI/Inspectors/CacheObject/CacheObjectBase.cs +++ b/src/UI/Inspectors/CacheObject/CacheObjectBase.cs @@ -72,7 +72,7 @@ namespace UnityExplorer.UI.Inspectors.CacheObject // On parent destroying this - public virtual void OnDestroyed() + public virtual void ReleasePooledObjects() { // TODO release IValue / Evaluate back to pool, etc ReleaseIValue(); @@ -237,6 +237,7 @@ namespace UnityExplorer.UI.Inspectors.CacheObject if (this.IValue == null) { IValue = (InteractiveValue)Pool.Borrow(typeof(InteractiveValue)); + CurrentIValueType = IValue.GetType(); IValue.SetOwner(this); IValue.UIRoot.transform.SetParent(CellView.SubContentHolder.transform, false); CellView.SubContentHolder.SetActive(true); diff --git a/src/UI/Inspectors/CacheObject/Views/CacheMemberCell.cs b/src/UI/Inspectors/CacheObject/Views/CacheMemberCell.cs index 79d5d7f..7f31040 100644 --- a/src/UI/Inspectors/CacheObject/Views/CacheMemberCell.cs +++ b/src/UI/Inspectors/CacheObject/Views/CacheMemberCell.cs @@ -10,7 +10,7 @@ namespace UnityExplorer.UI.Inspectors.CacheObject.Views { public class CacheMemberCell : CacheObjectCell { - public ReflectionInspector CurrentOwner { get; set; } + public ReflectionInspector Owner { get; set; } public CacheMember MemberOccupant => Occupant as CacheMember; @@ -24,15 +24,6 @@ namespace UnityExplorer.UI.Inspectors.CacheObject.Views // TODO } - public override void OnReturnToPool() - { - base.OnReturnToPool(); - - // TODO ? - - CurrentOwner = null; - } - protected override void ConstructEvaluateHolder(GameObject parent) { // Evaluate vert group diff --git a/src/UI/Inspectors/CacheObject/Views/CacheObjectCell.cs b/src/UI/Inspectors/CacheObject/Views/CacheObjectCell.cs index 9eb93b8..bf5e4fd 100644 --- a/src/UI/Inspectors/CacheObject/Views/CacheObjectCell.cs +++ b/src/UI/Inspectors/CacheObject/Views/CacheObjectCell.cs @@ -59,18 +59,6 @@ namespace UnityExplorer.UI.Inspectors.CacheObject.Views public GameObject SubContentHolder; - public virtual void OnReturnToPool() - { - if (Occupant != null) - { - // TODO ? - - SubContentHolder.SetActive(false); - - Occupant = null; - } - } - protected virtual void ApplyClicked() { Occupant.OnCellApplyClicked(); diff --git a/src/UI/Inspectors/GameObjectInspector.cs b/src/UI/Inspectors/GameObjectInspector.cs index 33e657a..67179f4 100644 --- a/src/UI/Inspectors/GameObjectInspector.cs +++ b/src/UI/Inspectors/GameObjectInspector.cs @@ -49,7 +49,7 @@ namespace UnityExplorer.UI.Inspectors TransformTree.Rebuild(); - ComponentList.ScrollPool.Rebuild(); + ComponentList.ScrollPool.Refresh(true, true); UpdateComponents(); } @@ -57,12 +57,12 @@ namespace UnityExplorer.UI.Inspectors { base.OnReturnToPool(); - // release component and transform lists - this.TransformTree.ScrollPool.ReturnCells(); - this.TransformTree.ScrollPool.SetUninitialized(); - - this.ComponentList.ScrollPool.ReturnCells(); - this.ComponentList.ScrollPool.SetUninitialized(); + //// release component and transform lists + //this.TransformTree.ScrollPool.ReleaseCells(); + //this.TransformTree.ScrollPool.SetUninitialized(); + // + //this.ComponentList.ScrollPool.ReleaseCells(); + //this.ComponentList.ScrollPool.SetUninitialized(); } private float timeOfLastUpdate; @@ -150,7 +150,7 @@ namespace UnityExplorer.UI.Inspectors _componentEntries.Add(comp); ComponentList.RefreshData(); - ComponentList.ScrollPool.RefreshCells(true); + ComponentList.ScrollPool.Refresh(true); } protected override void OnCloseClicked() diff --git a/src/UI/Inspectors/ReflectionInspector.cs b/src/UI/Inspectors/ReflectionInspector.cs index 27c037b..d1ff5ec 100644 --- a/src/UI/Inspectors/ReflectionInspector.cs +++ b/src/UI/Inspectors/ReflectionInspector.cs @@ -46,7 +46,8 @@ namespace UnityExplorer.UI.Inspectors SetTitleLayouts(); SetTarget(target); - MemberScrollPool.Initialize(this); + // MemberScrollPool.SetDataSource(this); + MemberScrollPool.Refresh(true, true); RuntimeProvider.Instance.StartCoroutine(InitCoroutine()); } @@ -60,11 +61,7 @@ namespace UnityExplorer.UI.Inspectors public override void OnReturnToPool() { foreach (var member in members) - member.OnDestroyed(); - - // release all cachememberviews - MemberScrollPool.ReturnCells(); - MemberScrollPool.SetUninitialized(); + member.ReleasePooledObjects(); members.Clear(); filteredMembers.Clear(); @@ -173,7 +170,7 @@ namespace UnityExplorer.UI.Inspectors } if (shouldRefresh) - MemberScrollPool.RefreshCells(false); + MemberScrollPool.Refresh(false); } // Member cells @@ -182,12 +179,7 @@ namespace UnityExplorer.UI.Inspectors public void OnCellBorrowed(CacheMemberCell cell) { - cell.CurrentOwner = this; - } - - public void OnCellReturned(CacheMemberCell cell) - { - cell.OnReturnToPool(); + cell.Owner = this; } public void SetCell(CacheMemberCell cell, int index) @@ -315,6 +307,7 @@ namespace UnityExplorer.UI.Inspectors MemberScrollPool = UIFactory.CreateScrollPool(uiRoot, "MemberList", out GameObject scrollObj, out GameObject _, new Color(0.09f, 0.09f, 0.09f)); UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999); + MemberScrollPool.Initialize(this); //InspectorPanel.Instance.UIRoot.GetComponent().enabled = false; //MemberScrollPool.Viewport.GetComponent().enabled = false; diff --git a/src/UI/Panels/ObjectExplorer/ObjectSearch.cs b/src/UI/ObjectExplorer/ObjectSearch.cs similarity index 98% rename from src/UI/Panels/ObjectExplorer/ObjectSearch.cs rename to src/UI/ObjectExplorer/ObjectSearch.cs index 6bd454b..8d0006f 100644 --- a/src/UI/Panels/ObjectExplorer/ObjectSearch.cs +++ b/src/UI/ObjectExplorer/ObjectSearch.cs @@ -83,7 +83,7 @@ namespace UnityExplorer.UI.Panels } dataHandler.RefreshData(); - resultsScrollPool.RefreshCells(true); + resultsScrollPool.Refresh(true); resultsLabel.text = $"{currentResults.Count} results"; } @@ -212,7 +212,7 @@ namespace UnityExplorer.UI.Panels //if (!Pool.PrototypeObject) // Pool.PrototypeObject = ButtonCell.CreatePrototypeCell(Pool.InactiveHolder).gameObject; - resultsScrollPool.Initialize(dataHandler);//, ButtonCell.CreatePrototypeCell(uiRoot)); + resultsScrollPool.Initialize(dataHandler); UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999); } } diff --git a/src/UI/Panels/ObjectExplorer/SceneExplorer.cs b/src/UI/ObjectExplorer/SceneExplorer.cs similarity index 100% rename from src/UI/Panels/ObjectExplorer/SceneExplorer.cs rename to src/UI/ObjectExplorer/SceneExplorer.cs diff --git a/src/UI/Widgets/AutoComplete/AutoCompleter.cs b/src/UI/Widgets/AutoComplete/AutoCompleter.cs index 5d1af15..3badf59 100644 --- a/src/UI/Widgets/AutoComplete/AutoCompleter.cs +++ b/src/UI/Widgets/AutoComplete/AutoCompleter.cs @@ -116,7 +116,7 @@ namespace UnityExplorer.UI.Widgets.AutoComplete UIRoot.SetActive(true); UIRoot.transform.SetAsLastSibling(); dataHandler.RefreshData(); - scrollPool.RefreshAndJumpToTop(); + scrollPool.Refresh(true, true); } } diff --git a/src/UI/Widgets/ButtonList/ButtonListSource.cs b/src/UI/Widgets/ButtonList/ButtonListSource.cs index 63b5ad7..2b07ea8 100644 --- a/src/UI/Widgets/ButtonList/ButtonListSource.cs +++ b/src/UI/Widgets/ButtonList/ButtonListSource.cs @@ -65,7 +65,7 @@ namespace UnityExplorer.UI.Widgets cell.OnClick += OnCellClicked; } - public void OnCellReturned(ButtonCell cell) + public void ReleaseCell(ButtonCell cell) { cell.OnClick -= OnCellClicked; } diff --git a/src/UI/Widgets/ScrollPool/CellViewHolder.cs b/src/UI/Widgets/ScrollPool/CellViewHolder.cs deleted file mode 100644 index 9fd13fb..0000000 --- a/src/UI/Widgets/ScrollPool/CellViewHolder.cs +++ /dev/null @@ -1,83 +0,0 @@ -//using System; -//using System.Collections.Generic; -//using System.Linq; -//using System.Text; -//using UnityEngine; -//using UnityEngine.UI; - -//namespace UnityExplorer.UI.Widgets -//{ -// public class CellViewHolder : ICell -// { -// public CellViewHolder(GameObject uiRoot) -// { -// this.UIRoot = uiRoot; -// this.Rect = uiRoot.GetComponent(); -// m_enabled = uiRoot.activeSelf; -// } - -// public bool Enabled => m_enabled; -// private bool m_enabled; - -// public GameObject UIRoot { get; } -// public RectTransform Rect { get; } - -// private GameObject m_content; - -// public GameObject SetContent(GameObject newContent) -// { -// var ret = m_content; - -// if (ret && newContent && ret.ReferenceEqual(newContent)) -// return null; - -// newContent.transform.SetParent(this.UIRoot.transform, false); -// (this as ICell).Enable(); - -// m_content = newContent; -// return ret; -// } - -// public GameObject DisableContent() -// { -// var ret = m_content; -// (this as ICell).Disable(); -// return ret; -// } - -// void ICell.Enable() -// { -// m_enabled = true; -// UIRoot.SetActive(true); -// } - -// void ICell.Disable() -// { -// m_enabled = false; -// UIRoot.SetActive(false); -// } - -// public static RectTransform CreatePrototypeCell(GameObject parent) -// { -// // using an image on the cell view holder is fine, we only need to make about 20-50 of these per pool. -// var prototype = UIFactory.CreateVerticalGroup(parent, "PrototypeCell", true, true, true, true, 0, new Vector4(0, 0, 0, 0), -// new Color(0.11f, 0.11f, 0.11f), TextAnchor.MiddleCenter); - -// var rect = prototype.GetComponent(); -// rect.anchorMin = new Vector2(0, 1); -// rect.anchorMax = new Vector2(0, 1); -// rect.pivot = new Vector2(0.5f, 1); -// rect.sizeDelta = new Vector2(100, 30); - -// prototype.AddComponent().verticalFit = ContentSizeFitter.FitMode.PreferredSize; - -// var sepObj = UIFactory.CreateUIObject("separator", prototype); -// sepObj.AddComponent().color = Color.black; -// UIFactory.SetLayoutElement(sepObj, minHeight: 1, preferredHeight: 1, flexibleHeight: 0); - -// prototype.SetActive(false); - -// return rect; -// } -// } -//} diff --git a/src/UI/Widgets/ScrollPool/IPoolDataSource.cs b/src/UI/Widgets/ScrollPool/IPoolDataSource.cs index 47eb7a2..f456a77 100644 --- a/src/UI/Widgets/ScrollPool/IPoolDataSource.cs +++ b/src/UI/Widgets/ScrollPool/IPoolDataSource.cs @@ -11,7 +11,7 @@ namespace UnityExplorer.UI.Widgets int ItemCount { get; } void OnCellBorrowed(T cell); - void OnCellReturned(T cell); + //void ReleaseCell(T cell); void SetCell(T cell, int index); //void DisableCell(T cell, int index); diff --git a/src/UI/Widgets/ScrollPool/ScrollPool.cs b/src/UI/Widgets/ScrollPool/ScrollPool.cs index 5abf911..f51f79a 100644 --- a/src/UI/Widgets/ScrollPool/ScrollPool.cs +++ b/src/UI/Widgets/ScrollPool/ScrollPool.cs @@ -35,9 +35,7 @@ namespace UnityExplorer.UI.Widgets public float PrototypeHeight => _protoHeight ?? (float)(_protoHeight = Pool.Instance.DefaultHeight); private float? _protoHeight; - //private float PrototypeHeight => DefaultHeight.rect.height; - - public int ExtraPoolCells => 10; + public int ExtraPoolCells => 6; public float RecycleThreshold => PrototypeHeight * ExtraPoolCells; public float HalfThreshold => RecycleThreshold * 0.5f; @@ -70,6 +68,7 @@ namespace UnityExplorer.UI.Widgets /// private int bottomDataIndex; private int TopDataIndex => Math.Max(0, bottomDataIndex - CellPool.Count + 1); + private int CurrentDataCount => bottomDataIndex + 1; private float TotalDataHeight => HeightCache.TotalHeight + contentLayout.padding.top + contentLayout.padding.bottom; @@ -78,8 +77,6 @@ namespace UnityExplorer.UI.Widgets /// private int topPoolIndex, bottomPoolIndex; - private int CurrentDataCount => bottomDataIndex + 1; - private Vector2 prevAnchoredPos; private float prevViewportHeight; @@ -102,14 +99,9 @@ namespace UnityExplorer.UI.Widgets private float prevContentHeight = 1.0f; - public void SetUninitialized() - { - m_initialized = false; - } - public override void Update() { - if (!m_initialized || !ScrollRect || DataSource == null) + if (!ScrollRect || DataSource == null) return; if (writingLocked && timeofLastWriteLock < Time.time) @@ -129,65 +121,38 @@ namespace UnityExplorer.UI.Widgets // Public methods - public void Rebuild() + public void Refresh(bool setCellData, bool jumpToTop = false) { - HeightCache = new DataHeightCache(this); + if (jumpToTop) + { + bottomDataIndex = CellPool.Count - 1; + Content.anchoredPosition = Vector2.zero; + } - SetRecycleViewBounds(false); - SetScrollBounds(); - - CheckExtendCellPool(); - writingLocked = false; - Content.anchoredPosition = Vector2.zero; - UpdateSliderHandle(true); - - m_initialized = true; - } - - public void RefreshAndJumpToTop() - { - bottomDataIndex = CellPool.Count - 1; - RefreshCells(true); - Content.anchoredPosition = Vector2.zero; - UpdateSliderHandle(true); - } - - public void RecreateHeightCache() - { - HeightCache = new DataHeightCache(this); - CheckDataSourceCountChange(out _); - } - - public void RefreshCells(bool reloadData) - { - RefreshCells(reloadData, true); + RefreshCells(setCellData, true); } // Initialize - private bool m_doneFirstInit; - private bool m_initialized; + //private bool Initialized; + /// Should be called only once, when the scroll pool is created. public void Initialize(IPoolDataSource dataSource) { + this.DataSource = dataSource; + HeightCache = new DataHeightCache(this); + // Ensure the pool for the cell type is initialized. Pool.GetPool(); - HeightCache = new DataHeightCache(this); - DataSource = dataSource; + this.contentLayout = ScrollRect.content.GetComponent(); + this.slider = ScrollRect.GetComponentInChildren(); + slider.onValueChanged.AddListener(OnSliderValueChanged); - if (!m_doneFirstInit) - { - m_doneFirstInit = true; - this.contentLayout = ScrollRect.content.GetComponent(); - this.slider = ScrollRect.GetComponentInChildren(); - slider.onValueChanged.AddListener(OnSliderValueChanged); + ScrollRect.vertical = true; + ScrollRect.horizontal = false; - ScrollRect.vertical = true; - ScrollRect.horizontal = false; - } - - ScrollRect.onValueChanged.RemoveListener(OnValueChangedListener); + //ScrollRect.onValueChanged.RemoveListener(OnValueChangedListener); RuntimeProvider.Instance.StartCoroutine(InitCoroutine()); } @@ -196,6 +161,8 @@ namespace UnityExplorer.UI.Widgets ScrollRect.content.anchoredPosition = Vector2.zero; yield return null; + LayoutRebuilder.ForceRebuildLayoutImmediate(Content); + // set intial bounds prevAnchoredPos = Content.anchoredPosition; SetRecycleViewBounds(false); @@ -203,14 +170,18 @@ namespace UnityExplorer.UI.Widgets // create initial cell pool and set cells CreateCellPool(); + var enumerator = GetPoolEnumerator(); + while (enumerator.MoveNext()) + SetCell(CellPool[enumerator.Current.cellIndex], enumerator.Current.dataIndex); + + LayoutRebuilder.ForceRebuildLayoutImmediate(Content); + // update slider SetScrollBounds(); UpdateSliderHandle(); // add onValueChanged listener after setup ScrollRect.onValueChanged.AddListener(OnValueChangedListener); - - m_initialized = true; } private void SetScrollBounds() @@ -218,28 +189,43 @@ namespace UnityExplorer.UI.Widgets NormalizedScrollBounds = new Vector2(Viewport.rect.height * 0.5f, TotalDataHeight - (Viewport.rect.height * 0.5f)); } + private void SetRecycleViewBounds(bool extendPoolIfGrown) + { + RecycleViewBounds = new Vector2(Viewport.MinY() + HalfThreshold, Viewport.MaxY() - HalfThreshold); + + if (extendPoolIfGrown && prevViewportHeight < Viewport.rect.height && prevViewportHeight != 0.0f) + CheckExtendCellPool(); + + prevViewportHeight = Viewport.rect.height; + } + // Cell pool - public void ReturnCells() - { - if (CellPool.Any()) - { - foreach (var cell in CellPool) - { - DataSource.OnCellReturned(cell); - Pool.Return(cell); - } - CellPool.Clear(); - } + private CellInfo _cellInfo = new CellInfo(); - bottomDataIndex = -1; - topPoolIndex = 0; - bottomPoolIndex = 0; + public IEnumerator GetPoolEnumerator() + { + int cellIdx = topPoolIndex; + int dataIndex = TopDataIndex; + int iterated = 0; + while (iterated < CellPool.Count) + { + _cellInfo.cellIndex = cellIdx; + _cellInfo.dataIndex = dataIndex; + yield return _cellInfo; + + cellIdx++; + if (cellIdx >= CellPool.Count) + cellIdx = 0; + + dataIndex++; + iterated++; + } } private void CreateCellPool() { - ReturnCells(); + //ReleaseCells(); CheckDataSourceCountChange(out _); @@ -256,8 +242,8 @@ namespace UnityExplorer.UI.Widgets bottomPoolIndex++; var cell = Pool.Borrow(); - DataSource.OnCellBorrowed(cell); CellPool.Add(cell); + DataSource.OnCellBorrowed(cell); cell.Rect.SetParent(ScrollRect.content, false); currentPoolCoverage += PrototypeHeight; @@ -266,26 +252,11 @@ namespace UnityExplorer.UI.Widgets bottomDataIndex = CellPool.Count - 1; LayoutRebuilder.ForceRebuildLayoutImmediate(Content); - - // after creating pool, set displayed cells. - var enumerator = GetPoolEnumerator(); - while (enumerator.MoveNext()) - SetCell(CellPool[enumerator.Current.cellIndex], enumerator.Current.dataIndex); - } - - private void SetRecycleViewBounds(bool extendPoolIfGrown) - { - RecycleViewBounds = new Vector2(Viewport.MinY() + HalfThreshold, Viewport.MaxY() - HalfThreshold); - - if (extendPoolIfGrown && prevViewportHeight < Viewport.rect.height && prevViewportHeight != 0.0f) - CheckExtendCellPool(); - - prevViewportHeight = Viewport.rect.height; } private bool CheckExtendCellPool() { - CheckDataSourceCountChange(out _); + CheckDataSourceCountChange(); var requiredCoverage = Math.Abs(RecycleViewBounds.y - RecycleViewBounds.x); var currentCoverage = CellPool.Count * PrototypeHeight; @@ -317,7 +288,7 @@ namespace UnityExplorer.UI.Widgets } } - RefreshCells(true); + RefreshCells(true, true); //ExplorerCore.Log("Anchor: " + Content.localPosition.y + ", prev: " + prevAnchor); //ExplorerCore.Log("Height: " + Content.rect.height + ", prev:" + prevHeight); @@ -343,27 +314,7 @@ namespace UnityExplorer.UI.Widgets // Refresh methods - private CellInfo _cellInfo = new CellInfo(); - - public IEnumerator GetPoolEnumerator() - { - int cellIdx = topPoolIndex; - int dataIndex = TopDataIndex; - int iterated = 0; - while (iterated < CellPool.Count) - { - _cellInfo.cellIndex = cellIdx; - _cellInfo.dataIndex = dataIndex; - yield return _cellInfo; - - cellIdx++; - if (cellIdx >= CellPool.Count) - cellIdx = 0; - - dataIndex++; - iterated++; - } - } + private bool CheckDataSourceCountChange() => CheckDataSourceCountChange(out _); private bool CheckDataSourceCountChange(out bool shouldJumpToBottom) { @@ -428,7 +379,9 @@ namespace UnityExplorer.UI.Widgets Content.anchoredPosition += Vector2.up * diff; } - LayoutRebuilder.ForceRebuildLayoutImmediate(Content); + if (andReloadFromDataSource) + LayoutRebuilder.ForceRebuildLayoutImmediate(Content); + SetScrollBounds(); ScrollRect.UpdatePrevData(); } @@ -456,7 +409,7 @@ namespace UnityExplorer.UI.Widgets private void OnValueChangedListener(Vector2 val) { - if (WritingLocked || !m_initialized) + if (WritingLocked || DataSource == null) return; if (InputManager.MouseScrollDelta != Vector2.zero) @@ -529,8 +482,6 @@ namespace UnityExplorer.UI.Widgets topPoolIndex = (topPoolIndex + 1) % CellPool.Count; } - //LayoutRebuilder.ForceRebuildLayoutImmediate(Content); - return -recycledheight; } @@ -572,8 +523,6 @@ namespace UnityExplorer.UI.Widgets bottomPoolIndex = (bottomPoolIndex - 1 + CellPool.Count) % CellPool.Count; } - //LayoutRebuilder.ForceRebuildLayoutImmediate(Content); - return recycledheight; } @@ -624,7 +573,7 @@ namespace UnityExplorer.UI.Widgets private void OnSliderValueChanged(float val) { - if (this.WritingLocked || !m_initialized) + if (this.WritingLocked || DataSource == null) return; this.WritingLocked = true; diff --git a/src/UI/Widgets/TransformTree/TransformTree.cs b/src/UI/Widgets/TransformTree/TransformTree.cs index 14e3fd1..9162d14 100644 --- a/src/UI/Widgets/TransformTree/TransformTree.cs +++ b/src/UI/Widgets/TransformTree/TransformTree.cs @@ -72,7 +72,7 @@ namespace UnityExplorer.UI.Widgets RefreshData(true, true); } - public void RefreshData(bool andReload = false, bool hardReload = false) + public void RefreshData(bool andReload = false, bool jumpToTop = false) { displayedObjects.Clear(); @@ -86,10 +86,10 @@ namespace UnityExplorer.UI.Widgets if (andReload) { - if (!hardReload) - ScrollPool.RefreshCells(true); + if (!jumpToTop) + ScrollPool.Refresh(true); else - ScrollPool.Rebuild(); + ScrollPool.Refresh(true, true); } } @@ -172,7 +172,7 @@ namespace UnityExplorer.UI.Widgets cell.OnExpandToggled += ToggleExpandCell; } - public void OnCellReturned(TransformCell cell) + public void ReleaseCell(TransformCell cell) { cell.OnExpandToggled -= ToggleExpandCell; } diff --git a/src/UnityExplorer.csproj b/src/UnityExplorer.csproj index d46ba31..573c880 100644 --- a/src/UnityExplorer.csproj +++ b/src/UnityExplorer.csproj @@ -321,10 +321,9 @@ - - + + -