WinterJamSnowman/Assets/Scripts/LemonGenericLib/UI/Tabs/TabGroup.cs

123 lines
3.1 KiB
C#
Raw Permalink Normal View History

2023-01-24 13:51:46 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
//TODO: make this system supprts tabs without using tab buttons
// or redo the entire thing
// yeah we need to redo the whole thing
// for now make your own page system
namespace Lemon.GenericLib.UI
{
public class TabGroup : MonoBehaviour
{
[SerializeField] private List<TabSet> tabSets;
[SerializeField] private int defaultTabIndex;
private TabButtonScript selectedButton;
[Serializable]
struct TabSet
{
public TabButtonScript tabButton;
public GameObject tabObject;
public TabSet(TabButtonScript tabButton, GameObject tabObject)
{
this.tabButton = tabButton;
this.tabObject = tabObject;
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
}
private void Start()
{
if (tabSets == null || defaultTabIndex < 0 || defaultTabIndex > tabSets.Count)
{
Debug.LogError("default tab cannot be asisgned, pleace chect the default tab index and make sure the tab list is initialized");
}
else
{
OnTabSelected(tabSets[defaultTabIndex].tabButton);
}
}
public void Subscribe(TabButtonScript button, GameObject tabObject)
{
if (tabSets == null)
{
tabSets = new List<TabSet>();
}
tabSets.Add(new TabSet(button, tabObject));
}
public void OnTabEnter(TabButtonScript button)
{
ResetTabs();
if (selectedButton == null || button != selectedButton)
{
button.OnEnter();
}
}
public void OnTabExit(TabButtonScript button)
{
ResetTabs();
}
public void OnTabSelected(TabButtonScript button)
{
if (selectedButton != null)
{
selectedButton.OnDeselected();
}
selectedButton = button;
button.OnSelected();
UpdateSelectedTab();
ResetTabs();
}
public void ResetTabs()
{
foreach (var item in tabSets)
{
if (selectedButton != null && item.tabButton == selectedButton) { continue; }
item.tabButton.OnExit();
}
}
//can overwite this
public void UpdateSelectedTab()
{
foreach (var tab in tabSets)
{
if (tab.tabButton == selectedButton)
{
tab.tabObject.SetActive(true);
tab.tabObject.transform.SetSiblingIndex(tabSets.Count - 1);
}
else
{
tab.tabObject.SetActive(false);
tab.tabObject.transform.SetSiblingIndex(0);
}
}
}
}
}