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;
|
2021-04-18 21:38:09 +10:00
|
|
|
|
using UnityEngine.UI;
|
2021-04-26 19:56:21 +10:00
|
|
|
|
using UnityExplorer.UI.ObjectPool;
|
2021-04-16 21:07:32 +10:00
|
|
|
|
using UnityExplorer.UI.Widgets;
|
|
|
|
|
|
|
|
|
|
namespace UnityExplorer.UI.Widgets
|
|
|
|
|
{
|
2021-04-26 19:56:21 +10:00
|
|
|
|
public class ButtonListSource<T> : IPoolDataSource<ButtonCell>
|
2021-04-16 21:07:32 +10:00
|
|
|
|
{
|
2021-04-26 19:56:21 +10:00
|
|
|
|
internal ScrollPool<ButtonCell> ScrollPool;
|
2021-04-16 21:07:32 +10:00
|
|
|
|
|
|
|
|
|
public int ItemCount => currentEntries.Count;
|
2021-04-25 21:20:50 +10:00
|
|
|
|
public readonly List<T> currentEntries = new List<T>();
|
2021-04-16 21:07:32 +10:00
|
|
|
|
|
2021-04-18 21:38:09 +10:00
|
|
|
|
public Func<List<T>> GetEntries;
|
2021-04-26 19:56:21 +10:00
|
|
|
|
public Action<ButtonCell, int> SetICell;
|
2021-04-18 21:38:09 +10:00
|
|
|
|
public Func<T, 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;
|
|
|
|
|
set => currentFilter = value?.ToLower() ?? "";
|
|
|
|
|
}
|
|
|
|
|
private string currentFilter;
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
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)
|
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()
|
|
|
|
|
{
|
|
|
|
|
var allEntries = GetEntries.Invoke();
|
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-04-18 21:38:09 +10:00
|
|
|
|
if (!ShouldDisplay.Invoke(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-04-26 19:56:21 +10:00
|
|
|
|
public void OnCellBorrowed(ButtonCell 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-04-26 19:56:21 +10:00
|
|
|
|
public void OnCellReturned(ButtonCell cell)
|
|
|
|
|
{
|
|
|
|
|
cell.OnClick -= OnCellClicked;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetCell(ButtonCell 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;
|
|
|
|
|
SetICell.Invoke(cell, index);
|
2021-04-16 21:07:32 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-21 23:07:15 +10:00
|
|
|
|
|
2021-04-29 21:01:08 +10:00
|
|
|
|
//public void DisableCell(ButtonCell cell, int index) => cell.Disable();
|
2021-04-16 21:07:32 +10:00
|
|
|
|
}
|
|
|
|
|
}
|