summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Decompile/EnumSelector.cs
blob: 8ad79be2031a800f62fce1b96e39e90594b80e6e (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;

public class EnumSelector : MonoBehaviour
{
	public ThronefallUIElement target;

	public List<string> options = new List<string>();

	private int index;

	public bool delayedApply;

	public GameObject applyButton;

	public ThronefallUIElement.NavigationDirection increase;

	public ThronefallUIElement.NavigationDirection decrease = ThronefallUIElement.NavigationDirection.Left;

	public TextMeshProUGUI display;

	public GameObject buttons;

	public UnityEvent onChange = new UnityEvent();

	public UnityEvent onIncrease = new UnityEvent();

	public UnityEvent onDecrease = new UnityEvent();

	private bool unpropagatedChanges;

	private int bufferIndex;

	public int Index => index;

	private void Start()
	{
		target.onApply.AddListener(PropagateDelayedChange);
		target.onSelectionStateChange.AddListener(OnLinkedTFUINavigate);
	}

	private void UpdateDisplay()
	{
		if (unpropagatedChanges)
		{
			display.text = options[index] + "<style=Settings Warning> *</style>";
			if ((bool)applyButton)
			{
				applyButton.SetActive(value: true);
			}
		}
		else
		{
			display.text = options[index];
			if ((bool)applyButton)
			{
				applyButton.SetActive(value: false);
			}
		}
	}

	public void Navigate(ThronefallUIElement.NavigationDirection direction)
	{
		if (direction == increase)
		{
			onIncrease.Invoke();
			index++;
		}
		else if (direction == decrease)
		{
			index--;
			onDecrease.Invoke();
		}
		if (index > options.Count - 1)
		{
			index = 0;
		}
		if (index < 0)
		{
			index = options.Count - 1;
		}
		if (delayedApply && index != bufferIndex)
		{
			unpropagatedChanges = true;
		}
		else
		{
			unpropagatedChanges = false;
		}
		UpdateDisplay();
		if (!delayedApply)
		{
			ApplyChanges();
		}
	}

	public void Increase()
	{
		Navigate(increase);
	}

	public void Decrease()
	{
		Navigate(decrease);
	}

	public void OnTFUIStateChange()
	{
		if (target.CurrentState != 0)
		{
			buttons.SetActive(value: true);
		}
		else
		{
			buttons.SetActive(value: false);
		}
	}

	public void SetIndex(int i)
	{
		index = i;
		if (index > options.Count - 1)
		{
			index = 0;
		}
		if (index < 0)
		{
			index = options.Count - 1;
		}
		bufferIndex = index;
		UpdateDisplay();
	}

	public void OnLinkedTFUINavigate()
	{
		CancelChanges();
	}

	public void PropagateDelayedChange()
	{
		if (delayedApply)
		{
			ApplyChanges();
		}
	}

	private void ApplyChanges()
	{
		bufferIndex = index;
		onChange.Invoke();
		unpropagatedChanges = false;
		UpdateDisplay();
	}

	private void CancelChanges()
	{
		index = bufferIndex;
		unpropagatedChanges = false;
		UpdateDisplay();
	}
}