summaryrefslogtreecommitdiff
path: root/Valheim_v0.141.2_r202102/Valheim/assembly_guiutils/TabHandler.cs
blob: ae40fc74b7133981aee0b4566aafa94c3ab98986 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class TabHandler : MonoBehaviour
{
	[Serializable]
	public class Tab
	{
		public Button m_button;

		public RectTransform m_page;

		public bool m_default;

		public UnityEvent m_onClick;
	}

	public bool m_gamepadInput;

	public List<Tab> m_tabs = new List<Tab>();

	private int m_selected;

	private void Awake()
	{
		int activeTab = 0;
		for (int i = 0; i < m_tabs.Count; i++)
		{
			Tab tab = m_tabs[i];
			tab.m_button.onClick.AddListener(delegate
			{
				OnClick(tab.m_button);
			});
			Transform transform = tab.m_button.gameObject.transform.Find("Selected");
			if ((bool)transform)
			{
				transform.GetComponentInChildren<Text>().text = tab.m_button.GetComponentInChildren<Text>().text;
			}
			if (tab.m_default)
			{
				activeTab = i;
			}
		}
		SetActiveTab(activeTab);
	}

	private void Update()
	{
		if (m_gamepadInput)
		{
			if (ZInput.GetButtonDown("JoyTabLeft"))
			{
				SetActiveTab(Mathf.Max(0, m_selected - 1));
			}
			if (ZInput.GetButtonDown("JoyTabRight"))
			{
				SetActiveTab(Mathf.Min(m_tabs.Count - 1, m_selected + 1));
			}
		}
	}

	private void OnClick(Button button)
	{
		SetActiveTab(button);
	}

	private void SetActiveTab(Button button)
	{
		for (int i = 0; i < m_tabs.Count; i++)
		{
			if (m_tabs[i].m_button == button)
			{
				SetActiveTab(i);
				break;
			}
		}
	}

	public void SetActiveTab(int index)
	{
		m_selected = index;
		for (int i = 0; i < m_tabs.Count; i++)
		{
			Tab tab = m_tabs[i];
			bool flag = i == index;
			tab.m_page.gameObject.SetActive(flag);
			tab.m_button.interactable = !flag;
			Transform transform = tab.m_button.gameObject.transform.Find("Selected");
			if ((bool)transform)
			{
				transform.gameObject.SetActive(flag);
			}
			if (flag)
			{
				tab.m_onClick.Invoke();
			}
		}
	}
}