218 lines
5.0 KiB
C#
218 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
namespace UIWidgets
|
|
{
|
|
[AddComponentMenu("UI/Tabs", 290)]
|
|
public class Tabs : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
public Transform Container;
|
|
|
|
[SerializeField]
|
|
public Button DefaultTabButton;
|
|
|
|
[SerializeField]
|
|
public Button ActiveTabButton;
|
|
|
|
[SerializeField]
|
|
private Tab[] tabObjects = new Tab[0];
|
|
|
|
public int selectedIndex;
|
|
|
|
[Tooltip("If true does not deactivate hidden tabs.")]
|
|
[SerializeField]
|
|
public bool KeepTabsActive;
|
|
|
|
[SerializeField]
|
|
public TabSelectEvent OnTabSelect = new TabSelectEvent();
|
|
|
|
public List<Button> defaultButtons = new List<Button>();
|
|
|
|
public List<Button> activeButtons = new List<Button>();
|
|
|
|
private List<UnityAction> callbacks = new List<UnityAction>();
|
|
|
|
public Tab[] TabObjects
|
|
{
|
|
get
|
|
{
|
|
return tabObjects;
|
|
}
|
|
set
|
|
{
|
|
tabObjects = value;
|
|
UpdateButtons();
|
|
}
|
|
}
|
|
|
|
public Tab SelectedTab { get; protected set; }
|
|
|
|
public void Initialize()
|
|
{
|
|
if (Container == null)
|
|
{
|
|
throw new NullReferenceException("Container is null. Set object of type GameObject to Container.");
|
|
}
|
|
if (DefaultTabButton == null)
|
|
{
|
|
throw new NullReferenceException("DefaultTabButton is null. Set object of type GameObject to DefaultTabButton.");
|
|
}
|
|
if (ActiveTabButton == null)
|
|
{
|
|
throw new NullReferenceException("ActiveTabButton is null. Set object of type GameObject to ActiveTabButton.");
|
|
}
|
|
DefaultTabButton.gameObject.SetActive(false);
|
|
ActiveTabButton.gameObject.SetActive(false);
|
|
UpdateButtons();
|
|
}
|
|
|
|
private void UpdateButtons()
|
|
{
|
|
if (tabObjects.Length == 0)
|
|
{
|
|
throw new ArgumentException("TabObjects array is empty. Fill it.");
|
|
}
|
|
RemoveCallbacks();
|
|
CreateButtons();
|
|
AddCallbacks();
|
|
SelectTab(tabObjects[0].Name);
|
|
}
|
|
|
|
private void AddCallback(Tab tab, int index)
|
|
{
|
|
string tabName = tab.Name;
|
|
UnityAction item = delegate
|
|
{
|
|
SelectTab(tabName);
|
|
};
|
|
callbacks.Add(item);
|
|
defaultButtons[index].onClick.AddListener(callbacks[index]);
|
|
}
|
|
|
|
private void AddCallbacks()
|
|
{
|
|
tabObjects.ForEach(AddCallback);
|
|
}
|
|
|
|
private void RemoveCallback(Tab tab, int index)
|
|
{
|
|
if (tab != null && index < callbacks.Count)
|
|
{
|
|
defaultButtons[index].onClick.RemoveListener(callbacks[index]);
|
|
}
|
|
}
|
|
|
|
private void RemoveCallbacks()
|
|
{
|
|
if (callbacks.Count > 0)
|
|
{
|
|
tabObjects.ForEach(RemoveCallback);
|
|
callbacks.Clear();
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
RemoveCallbacks();
|
|
}
|
|
|
|
public void SelectTab(string tabName, bool onDemand = false)
|
|
{
|
|
int num = Array.FindIndex(tabObjects, (Tab x) => x.Name == tabName);
|
|
if (num == -1)
|
|
{
|
|
throw new ArgumentException(string.Format("Tab with name \"{0}\" not found.", tabName));
|
|
}
|
|
if (onDemand)
|
|
{
|
|
if (num == selectedIndex)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
EquipmentGUI equipmentGUI = UnityEngine.Object.FindObjectOfType<EquipmentGUI>();
|
|
if ((bool)equipmentGUI)
|
|
{
|
|
equipmentGUI.DeselectedEquipment();
|
|
equipmentGUI.RefreshAll();
|
|
}
|
|
}
|
|
if (KeepTabsActive)
|
|
{
|
|
tabObjects[num].TabObject.transform.SetAsLastSibling();
|
|
}
|
|
else
|
|
{
|
|
tabObjects.ForEach(DeactivateTab);
|
|
tabObjects[num].TabObject.SetActive(true);
|
|
}
|
|
defaultButtons.ForEach(ActivateButton);
|
|
defaultButtons[num].gameObject.SetActive(false);
|
|
activeButtons.ForEach(DeactivateButton);
|
|
activeButtons[num].gameObject.SetActive(true);
|
|
SelectedTab = tabObjects[num];
|
|
OnTabSelect.Invoke(num);
|
|
selectedIndex = num;
|
|
}
|
|
|
|
private void DeactivateTab(Tab tab)
|
|
{
|
|
tab.TabObject.SetActive(false);
|
|
}
|
|
|
|
private void ActivateButton(Button button)
|
|
{
|
|
button.gameObject.SetActive(true);
|
|
}
|
|
|
|
private void DeactivateButton(Button button)
|
|
{
|
|
button.gameObject.SetActive(false);
|
|
}
|
|
|
|
private void CreateButtons()
|
|
{
|
|
if (tabObjects.Length > defaultButtons.Count)
|
|
{
|
|
for (int i = defaultButtons.Count; i < tabObjects.Length; i++)
|
|
{
|
|
Button button = UnityEngine.Object.Instantiate(DefaultTabButton);
|
|
button.transform.SetParent(Container, false);
|
|
defaultButtons.Add(button);
|
|
Button button2 = UnityEngine.Object.Instantiate(ActiveTabButton);
|
|
button2.transform.SetParent(Container, false);
|
|
activeButtons.Add(button2);
|
|
}
|
|
}
|
|
if (tabObjects.Length < defaultButtons.Count)
|
|
{
|
|
for (int num = defaultButtons.Count; num > tabObjects.Length; num--)
|
|
{
|
|
UnityEngine.Object.Destroy(defaultButtons[num]);
|
|
UnityEngine.Object.Destroy(activeButtons[num]);
|
|
defaultButtons.RemoveAt(num);
|
|
activeButtons.RemoveAt(num);
|
|
}
|
|
}
|
|
defaultButtons.ForEach(SetButtonName);
|
|
activeButtons.ForEach(SetButtonName);
|
|
}
|
|
|
|
private void SetButtonName(Button button, int index)
|
|
{
|
|
button.gameObject.SetActive(true);
|
|
Text componentInChildren = button.GetComponentInChildren<Text>();
|
|
if ((bool)componentInChildren)
|
|
{
|
|
}
|
|
button.GetComponent<MyTabButton>().iconImage.sprite = tabObjects[index].Icon;
|
|
}
|
|
}
|
|
}
|