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 ButtonListSource : IPoolDataSource { internal ScrollPool Scroller; public int ItemCount => currentEntries.Count; public List currentEntries; public Func> GetEntries; public Action, int> SetICell; public Func ShouldDisplay; public Action> OnCellClicked; public string CurrentFilter { get => currentFilter; set => currentFilter = value?.ToLower() ?? ""; } private string currentFilter; public ButtonListSource(ScrollPool infiniteScroller, Func> getEntriesMethod, Action, int> setICellMethod, Func shouldDisplayMethod, Action> onCellClickedMethod) { Scroller = infiniteScroller; GetEntries = getEntriesMethod; SetICell = setICellMethod; ShouldDisplay = shouldDisplayMethod; OnCellClicked = onCellClickedMethod; } public void Init() { RuntimeProvider.Instance.StartCoroutine(InitCoroutine()); } private IEnumerator InitCoroutine() { yield return null; var proto = ButtonCell.CreatePrototypeCell(Scroller.UIRoot); RefreshData(); Scroller.DataSource = this; Scroller.Initialize(this, proto); } public void RefreshData() { var allEntries = GetEntries.Invoke(); var list = new List(); 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