2021-04-16 21:07:32 +10:00
|
|
|
|
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-04-16 21:07:32 +10:00
|
|
|
|
{
|
2021-05-16 21:45:19 +10:00
|
|
|
|
internal ScrollPool<TCell> ScrollPool;
|
2021-04-16 21:07:32 +10:00
|
|
|
|
|
|
|
|
|
public int ItemCount => currentEntries.Count;
|
2021-05-16 21:45:19 +10:00
|
|
|
|
public readonly List<TData> currentEntries = new List<TData>();
|
2021-04-16 21:07:32 +10:00
|
|
|
|
|
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;
|
2021-04-16 21:07:32 +10:00
|
|
|
|
|
|
|
|
|
public string CurrentFilter
|
|
|
|
|
{
|
|
|
|
|
get => currentFilter;
|
2021-04-30 23:43:27 +10:00
|
|
|
|
set => currentFilter = value ?? "";
|
2021-04-16 21:07:32 +10:00
|
|
|
|
}
|
|
|
|
|
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)
|
2021-04-16 21:07:32 +10:00
|
|
|
|
{
|
2021-04-26 19:56:21 +10:00
|
|
|
|
ScrollPool = scrollPool;
|
2021-04-16 21:07:32 +10:00
|
|
|
|
|
|
|
|
|
GetEntries = getEntriesMethod;
|
|
|
|
|
SetICell = setICellMethod;
|
2021-04-18 21:38:09 +10:00
|
|
|
|
ShouldDisplay = shouldDisplayMethod;
|
|
|
|
|
OnCellClicked = onCellClickedMethod;
|
2021-04-16 21:07:32 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RefreshData()
|
|
|
|
|
{
|
2021-05-16 21:45:19 +10:00
|
|
|
|
var allEntries = GetEntries();
|
2021-04-25 21:20:50 +10:00
|
|
|
|
currentEntries.Clear();
|
2021-04-16 21:07:32 +10:00
|
|
|
|
|
|
|
|
|
foreach (var entry in allEntries)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(currentFilter))
|
|
|
|
|
{
|
2021-05-16 21:45:19 +10:00
|
|
|
|
if (!ShouldDisplay(entry, currentFilter))
|
2021-04-16 21:07:32 +10:00
|
|
|
|
continue;
|
|
|
|
|
|
2021-04-25 21:20:50 +10:00
|
|
|
|
currentEntries.Add(entry);
|
2021-04-16 21:07:32 +10:00
|
|
|
|
}
|
|
|
|
|
else
|
2021-04-25 21:20:50 +10:00
|
|
|
|
currentEntries.Add(entry);
|
2021-04-16 21:07:32 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-16 21:45:19 +10:00
|
|
|
|
public virtual void OnCellBorrowed(TCell cell)
|
2021-04-16 21:07:32 +10:00
|
|
|
|
{
|
2021-04-18 21:38:09 +10:00
|
|
|
|
cell.OnClick += OnCellClicked;
|
2021-04-16 21:07:32 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-16 21:45:19 +10:00
|
|
|
|
public virtual void SetCell(TCell cell, int index)
|
2021-04-16 21:07:32 +10:00
|
|
|
|
{
|
2021-04-23 21:50:58 +10:00
|
|
|
|
if (currentEntries == null)
|
|
|
|
|
RefreshData();
|
|
|
|
|
|
2021-04-16 21:07:32 +10:00
|
|
|
|
if (index < 0 || index >= currentEntries.Count)
|
|
|
|
|
cell.Disable();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cell.Enable();
|
2021-04-26 19:56:21 +10:00
|
|
|
|
cell.CurrentDataIndex = index;
|
2021-05-16 21:45:19 +10:00
|
|
|
|
SetICell(cell, index);
|
2021-04-16 21:07:32 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|