using System; using System.Collections.Generic; using UnityEngine.EventSystems; namespace AlicizaX.UI { public abstract class UGListBase where TAdapter : Adapter where TData : ISimpleViewData { protected readonly RecyclerView _recyclerView; protected readonly TAdapter _adapter; private List _datas; protected UGListBase(RecyclerView recyclerView, TAdapter adapter) { _recyclerView = recyclerView; _adapter = adapter; if (_recyclerView != null) { _recyclerView.SetAdapter(_adapter); } } public RecyclerView RecyclerView => _recyclerView; public TAdapter Adapter => _adapter; public List Data { get => _datas; set { _datas = value; _adapter.SetList(_datas); } } public int DataCount => _datas?.Count ?? 0; public int FocusIndex => _recyclerView != null ? _recyclerView.CurrentIndex : -1; public int ChoiceIndex { get => _adapter != null ? _adapter.ChoiceIndex : -1; set { if (_adapter != null) { _adapter.ChoiceIndex = value; } } } public bool HasChoice => ChoiceIndex >= 0; public float ScrollPosition => _recyclerView != null ? _recyclerView.GetScrollPosition() : 0f; public event Action OnFocusIndexChanged { add { if (_recyclerView != null) { _recyclerView.OnFocusIndexChanged += value; } } remove { if (_recyclerView != null) { _recyclerView.OnFocusIndexChanged -= value; } } } public event Action OnChoiceIndexChanged { add { if (_adapter != null) { _adapter.OnChoiceIndexChanged += value; } } remove { if (_adapter != null) { _adapter.OnChoiceIndexChanged -= value; } } } public event Action ScrollValueChanged { add { if (_recyclerView != null) { _recyclerView.OnScrollValueChanged += value; } } remove { if (_recyclerView != null) { _recyclerView.OnScrollValueChanged -= value; } } } public event Action ScrollStopped { add { if (_recyclerView != null) { _recyclerView.OnScrollStopped += value; } } remove { if (_recyclerView != null) { _recyclerView.OnScrollStopped -= value; } } } public event Action ScrollDraggingChanged { add { if (_recyclerView != null) { _recyclerView.OnScrollDraggingChanged += value; } } remove { if (_recyclerView != null) { _recyclerView.OnScrollDraggingChanged -= value; } } } public void RegisterItemRender(string viewName = "") where TItemRender : ItemRenderBase { RegisterItemRender(typeof(TItemRender), viewName); } public void RegisterItemRender(Type itemRenderType, string viewName = "") { _adapter.RegisterItemRender(itemRenderType, viewName); } public bool UnregisterItemRender() where TItemRender : ItemRenderBase { return UnregisterItemRender(typeof(TItemRender)); } public bool UnregisterItemRender(Type itemRenderType) { return UnregisterItemRender(nameof(itemRenderType)); } private bool UnregisterItemRender(string viewName = "") { return _adapter.UnregisterItemRender(viewName); } public void ClearItemRenderRegistrations() { _adapter.ClearItemRenderRegistrations(); } public void ClearChoice() { ChoiceIndex = -1; } public bool TryFocus(int index, bool smooth = false, ScrollAlignment alignment = ScrollAlignment.Center) { return _recyclerView != null && _recyclerView.TryFocusIndex(index, smooth, alignment); } public bool TryFocusChoice(bool smooth = false, ScrollAlignment alignment = ScrollAlignment.Center) { int index = ChoiceIndex; return index >= 0 && TryFocus(index, smooth, alignment); } public bool TryFocusEntry(MoveDirection entryDirection) { return _recyclerView != null && _recyclerView.TryFocusEntry(entryDirection); } public void ScrollToIndex(int index, bool smooth = false) { _recyclerView?.ScrollTo(index, smooth); } public void ScrollTo( int index, ScrollAlignment alignment = ScrollAlignment.Start, float offset = 0f, bool smooth = false, float duration = 0.3f) { _recyclerView?.ScrollToWithAlignment(index, alignment, offset, smooth, duration); } public void ScrollToChoice( ScrollAlignment alignment = ScrollAlignment.Center, float offset = 0f, bool smooth = false, float duration = 0.3f) { int index = ChoiceIndex; if (index >= 0) { ScrollTo(index, alignment, offset, smooth, duration); } } public void ScrollToFocus( ScrollAlignment alignment = ScrollAlignment.Center, float offset = 0f, bool smooth = false, float duration = 0.3f) { int index = FocusIndex; if (index >= 0) { ScrollTo(index, alignment, offset, smooth, duration); } } public void ScrollToStart(int index, float offset = 0f, bool smooth = false, float duration = 0.3f) { ScrollTo(index, ScrollAlignment.Start, offset, smooth, duration); } public void ScrollToCenter(int index, float offset = 0f, bool smooth = false, float duration = 0.3f) { ScrollTo(index, ScrollAlignment.Center, offset, smooth, duration); } public void ScrollToEnd(int index, float offset = 0f, bool smooth = false, float duration = 0.3f) { ScrollTo(index, ScrollAlignment.End, offset, smooth, duration); } } public class UGList : UGListBase> where TData : ISimpleViewData { public UGList(RecyclerView recyclerView) : base(recyclerView, new Adapter(recyclerView)) { } } public class UGGroupList : UGListBase> where TData : class, IGroupViewData, new() { public UGGroupList(RecyclerView recyclerView, string groupViewName) : base(recyclerView, new GroupAdapter(recyclerView, groupViewName)) { } public bool IsGroupIndex(int index) { return _adapter.IsGroupIndex(index); } public bool TryGetDisplayData(int index, out TData data) { return _adapter.TryGetDisplayData(index, out data); } public bool SetExpanded(int index, bool expanded) { return _adapter.SetExpanded(index, expanded); } public void Expand(int index) { _adapter.Expand(index); } public void Collapse(int index) { _adapter.Collapse(index); } public void Activate(int index) { _adapter.Activate(index); } } public class UGLoopList : UGListBase> where TData : ISimpleViewData, new() { public UGLoopList(RecyclerView recyclerView) : base(recyclerView, new LoopAdapter(recyclerView)) { } } public class UGMixedList : UGListBase> where TData : IMixedViewData { public UGMixedList(RecyclerView recyclerView) : base(recyclerView, new MixedAdapter(recyclerView)) { } } public static class UGListCreateHelper { public static UGList Create(RecyclerView recyclerView) where TData : ISimpleViewData => new UGList(recyclerView); public static UGGroupList CreateGroup(RecyclerView recyclerView, string groupViewName) where TData : class, IGroupViewData, new() => new UGGroupList(recyclerView, groupViewName); public static UGLoopList CreateLoop(RecyclerView recyclerView) where TData : ISimpleViewData, new() => new UGLoopList(recyclerView); public static UGMixedList CreateMixed(RecyclerView recyclerView) where TData : IMixedViewData => new UGMixedList(recyclerView); } }