using System; using System.Collections.Generic; using UnityEngine; namespace DDD { public class TabGroupUi : MonoBehaviour where T : Enum { [SerializeField] private List> _tabButtons; private Dictionary> _tabLookup; private Action _onTabSelected; public T CurrentTab { get; private set; } public List TabOrder => _tabButtons.ConvertAll(b => b.TabType); public void Initialize(Action onTabSelected) { _onTabSelected = onTabSelected; _tabLookup = new Dictionary>(); foreach (var tab in _tabButtons) { tab.Initialize(OnTabClicked); _tabLookup[tab.TabType] = tab; } if (_tabButtons.Count > 0) { SelectTab(_tabButtons[0].TabType); } } private void OnTabClicked(T type) { SelectTab(type); } public void SelectTab(T type) { CurrentTab = type; foreach (var tab in _tabButtons) { tab.SetSelected(tab.TabType.Equals(type)); } _onTabSelected?.Invoke(type); } public void Move(int direction) { var tabTypes = TabOrder; int index = tabTypes.IndexOf(CurrentTab); int count = tabTypes.Count; if (count == 0) return; int newIndex = (index + direction + count) % count; SelectTab(tabTypes[newIndex]); } } }