Files
UnityExplorer_Fix/src/UI/Widgets/ButtonList/ButtonListHandler.cs

81 lines
2.3 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace UnityExplorer.UI.Widgets
{
2021-05-16 21:45:19 +10:00
public class ButtonListHandler<TData, TCell> : ICellPoolDataSource<TCell> where TCell : ButtonCell
{
2021-05-16 21:45:19 +10:00
internal ScrollPool<TCell> ScrollPool;
public int ItemCount => currentEntries.Count;
2021-05-16 21:45:19 +10:00
public readonly List<TData> currentEntries = new List<TData>();
2021-05-16 21:45:19 +10:00
public Func<List<TData>> GetEntries;
public Action<TCell, int> SetICell;
public Func<TData, string, bool> ShouldDisplay;
2021-04-23 21:50:58 +10:00
public Action<int> OnCellClicked;
public string CurrentFilter
{
get => currentFilter;
set => currentFilter = value ?? "";
}
private string currentFilter;
2021-06-05 19:36:09 +10:00
public ButtonListHandler(ScrollPool<TCell> scrollPool, Func<List<TData>> getEntriesMethod,
2021-05-16 21:45:19 +10:00
Action<TCell, int> setICellMethod, Func<TData, 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()
{
2021-05-16 21:45:19 +10:00
var allEntries = GetEntries();
currentEntries.Clear();
foreach (var entry in allEntries)
{
if (!string.IsNullOrEmpty(currentFilter))
{
2021-05-16 21:45:19 +10:00
if (!ShouldDisplay(entry, currentFilter))
continue;
currentEntries.Add(entry);
}
else
currentEntries.Add(entry);
}
}
2021-05-16 21:45:19 +10:00
public virtual void OnCellBorrowed(TCell cell)
{
cell.OnClick += OnCellClicked;
}
2021-05-16 21:45:19 +10:00
public virtual void SetCell(TCell 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;
2021-05-16 21:45:19 +10:00
SetICell(cell, index);
}
}
}
}