-
Notifications
You must be signed in to change notification settings - Fork 1
/
ButtonsGroupController.cs
166 lines (133 loc) · 3.92 KB
/
ButtonsGroupController.cs
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
164
165
166
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public enum ButtonStatus
{
Idle,
Hover,
Active
}
public class ButtonsGroupController : MonoBehaviour
{
[Header("Single Or Multiple Active")]
public bool bIsExclusive = true;
public bool bEnforceOneActive = false;
public bool bFirstButtonActive = false;
[Header("Buttons Colors")]
public Color mIdleColor;
public Color mActiveColor;
public Color mHoverColor;
private List<GameObject> _mButtons = new List<GameObject>();
private List<ButtonStatus> _mStatuses = new List<ButtonStatus>();
private int _mNumberButtons;
//private BattleController _mBattleController;
void Start()
{
RegisterElements();
}
public void RegisterElements()
{
_mNumberButtons = transform.childCount;
for (int i = 0; i < _mNumberButtons; i++)
{
GameObject currChild = transform.GetChild(i).gameObject;
currChild.GetComponent<ClickableObject>().mIdObject = i;
_mButtons.Add(currChild);
_mStatuses.Add(ButtonStatus.Idle);
}
_mStatuses[0] = (bFirstButtonActive) ? ButtonStatus.Active : ButtonStatus.Idle;
if (bEnforceOneActive)
{
_mStatuses[0] = ButtonStatus.Active;
}
SetColorButtons();
}
private void SetColorButtons()
{
for (int i = 0; i < _mNumberButtons; i++)
{
Color buttonColor = mIdleColor;
if (_mStatuses[i] == ButtonStatus.Active)
{
buttonColor = mActiveColor;
} else if (_mStatuses[i] == ButtonStatus.Hover)
{
buttonColor = mHoverColor;
}
_mButtons[i].GetComponent<Image>().color = buttonColor;
}
// Put the call to the state manager in this call to send latest state?
//_mBattleController.NotifyChangeStateMainControls(GetListActiveButtons());
}
public void NotifySelection(int idButton)
{
bool isDirty = false;
if (_mStatuses[idButton] == ButtonStatus.Active)
{
if (bEnforceOneActive)
{
if (GetNumberActive() > 1)
{
isDirty = true;
_mStatuses[idButton] = ButtonStatus.Idle;
}
} else
{
isDirty = true;
_mStatuses[idButton] = ButtonStatus.Idle;
}
} else
{
if (bIsExclusive)
{
for (int i = 0; i < _mNumberButtons; i++)
{
_mStatuses[i] = ButtonStatus.Idle;
}
}
_mStatuses[idButton] = ButtonStatus.Active;
isDirty = true;
}
if (isDirty)
{
SetColorButtons();
}
}
public void NotifyHovering(int idButton)
{
if (_mStatuses[idButton] != ButtonStatus.Hover &&_mStatuses[idButton] != ButtonStatus.Active)
{
_mStatuses[idButton] = ButtonStatus.Hover;
SetColorButtons();
}
}
public void NotifyStoppedHovering(int idButton)
{
if (_mStatuses[idButton] != ButtonStatus.Active)
{
_mStatuses[idButton] = ButtonStatus.Idle;
SetColorButtons();
}
}
public List<bool> GetListActiveButtons()
{
List<bool> activeButtons = new List<bool>(_mButtons.Count);
for (int i = 0; i < _mNumberButtons; i++)
{
activeButtons.Add(_mStatuses[i] == ButtonStatus.Active);
}
return activeButtons;
}
public int GetNumberActive()
{
int nbActive = 0;
foreach (ButtonStatus status in _mStatuses)
{
if (status == ButtonStatus.Active)
{
nbActive++;
}
}
return nbActive;
}
}