replace old scrollpool with new dynamic one, more progress

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

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
namespace UnityExplorer.UI.Widgets
{
public class ButtonCell<T> : ICell
{
public bool Enabled => m_enabled;
private bool m_enabled;
public Action<ButtonCell<T>> OnClick;
public ButtonListCell<T> list;
public GameObject uiRoot;
public Text buttonText;
public Button button;
public ButtonCell(ButtonListCell<T> list, GameObject uiRoot, Button button, Text text)
{
this.list = list;
this.uiRoot = uiRoot;
this.buttonText = text;
this.button = button;
button.onClick.AddListener(() => { OnClick?.Invoke(this); });
}
public void Disable()
{
m_enabled = false;
uiRoot.SetActive(false);
}
public void Enable()
{
m_enabled = true;
uiRoot.SetActive(true);
}
public static GameObject CreatePrototypeCell(GameObject parent)
{
var prototype = UIFactory.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 nameButton = UIFactory.CreateButton(prototype, "NameButton", "Name", null);
UIFactory.SetLayoutElement(nameButton.gameObject, flexibleWidth: 9999, minHeight: 25, flexibleHeight: 0);
var nameLabel = nameButton.GetComponentInChildren<Text>();
nameLabel.horizontalOverflow = HorizontalWrapMode.Overflow;
nameLabel.alignment = TextAnchor.MiddleLeft;
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(nameButton, normal, highlight, pressed, disabled);
prototype.SetActive(false);
return prototype;
}
}
}

View File

@ -0,0 +1,98 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.UI.Widgets;
namespace UnityExplorer.UI.Widgets
{
public class ButtonListCell<T> : IPoolDataSource
{
internal ScrollPool Scroller;
public int ItemCount => currentEntries.Count;
public List<T> currentEntries;
public Func<List<T>> GetEntries;
public Action<ButtonCell<T>, int> SetICell;
public Func<T, string, bool> ShouldDisplay;
public Action<ButtonCell<T>> OnCellClicked;
public string CurrentFilter
{
get => currentFilter;
set => currentFilter = value?.ToLower() ?? "";
}
private string currentFilter;
public ButtonListCell(ScrollPool infiniteScroller, Func<List<T>> getEntriesMethod,
Action<ButtonCell<T>, int> setICellMethod, Func<T, string, bool> shouldDisplayMethod,
Action<ButtonCell<T>> onCellClickedMethod)
{
Scroller = infiniteScroller;
GetEntries = getEntriesMethod;
SetICell = setICellMethod;
ShouldDisplay = shouldDisplayMethod;
OnCellClicked = onCellClickedMethod;
}
public void Init()
{
RuntimeProvider.Instance.StartCoroutine(InitCoroutine());
}
private IEnumerator InitCoroutine()
{
yield return null;
RefreshData();
Scroller.DataSource = this;
Scroller.Initialize(this);
}
public void RefreshData()
{
var allEntries = GetEntries.Invoke();
var list = new List<T>();
foreach (var entry in allEntries)
{
if (!string.IsNullOrEmpty(currentFilter))
{
if (!ShouldDisplay.Invoke(entry, currentFilter))
continue;
list.Add(entry);
}
else
list.Add(entry);
}
currentEntries = list;
}
public ICell CreateCell(RectTransform rect)
{
var button = rect.GetComponentInChildren<Button>();
var text = button.GetComponentInChildren<Text>();
var cell = new ButtonCell<T>(this, rect.gameObject, button, text);
cell.OnClick += OnCellClicked;
return cell;
}
public void SetCell(ICell cell, int index)
{
if (index < 0 || index >= currentEntries.Count)
cell.Disable();
else
{
cell.Enable();
SetICell.Invoke((ButtonCell<T>)cell, index);
}
}
}
}