UnityExplorer/src/UI/Widgets/ButtonList/ButtonListSource.cs

91 lines
2.6 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.UI.ObjectPool;
using UnityExplorer.UI.Widgets;
namespace UnityExplorer.UI.Widgets
{
public class ButtonListSource<T> : IPoolDataSource<ButtonCell>
{
internal ScrollPool<ButtonCell> ScrollPool;
public int ItemCount => currentEntries.Count;
public readonly List<T> currentEntries = new List<T>();
public Func<List<T>> GetEntries;
public Action<ButtonCell, int> SetICell;
public Func<T, string, bool> ShouldDisplay;
2021-04-23 21:50:58 +10:00
public Action<int> OnCellClicked;
public string CurrentFilter
{
get => currentFilter;
set => currentFilter = value?.ToLower() ?? "";
}
private string currentFilter;
public ButtonListSource(ScrollPool<ButtonCell> scrollPool, Func<List<T>> getEntriesMethod,
Action<ButtonCell, int> setICellMethod, Func<T, string, bool> shouldDisplayMethod,
2021-04-23 21:50:58 +10:00
Action<int> onCellClickedMethod)
{
ScrollPool = scrollPool;
GetEntries = getEntriesMethod;
SetICell = setICellMethod;
ShouldDisplay = shouldDisplayMethod;
OnCellClicked = onCellClickedMethod;
}
public void RefreshData()
{
var allEntries = GetEntries.Invoke();
currentEntries.Clear();
foreach (var entry in allEntries)
{
if (!string.IsNullOrEmpty(currentFilter))
{
if (!ShouldDisplay.Invoke(entry, currentFilter))
continue;
currentEntries.Add(entry);
}
else
currentEntries.Add(entry);
}
}
public void OnCellBorrowed(ButtonCell cell)
{
cell.OnClick += OnCellClicked;
}
public void OnCellReturned(ButtonCell cell)
{
cell.OnClick -= OnCellClicked;
}
public void SetCell(ButtonCell cell, int index)
{
2021-04-23 21:50:58 +10:00
if (currentEntries == null)
RefreshData();
if (index < 0 || index >= currentEntries.Count)
cell.Disable();
else
{
cell.Enable();
cell.CurrentDataIndex = index;
SetICell.Invoke(cell, index);
}
}
//public void DisableCell(ButtonCell cell, int index) => cell.Disable();
}
}