-
Notifications
You must be signed in to change notification settings - Fork 1
/
ButtonBrancher.cs
353 lines (311 loc) · 14.2 KB
/
ButtonBrancher.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using UnityEngine;
using System.Collections;
using UnityEngine.UI; //because we will be requiring UI data types
using System.Collections.Generic; //because we will be using dynamic lists
public class ButtonBrancher : MonoBehaviour {
public class ButtonScaler
{
enum ScaleMode { MatchWidthHeight, IndependentWidthHeight }
ScaleMode mode;
Vector2 referenceButtonSize;
[HideInInspector]
public Vector2 referenceScreenSize;
public Vector2 newButtonSize;
public void Initialize(Vector2 refButtonSize, Vector2 refScreenSize, int scaleMode)
{
mode = (ScaleMode)scaleMode;
referenceButtonSize = refButtonSize;
referenceScreenSize = refScreenSize;
SetNewButtonSize();
}
void SetNewButtonSize()
{
if (mode == ScaleMode.IndependentWidthHeight)
{
newButtonSize.x = (referenceButtonSize.x * Screen.width) / referenceScreenSize.x;
newButtonSize.y = (referenceButtonSize.y * Screen.height) / referenceScreenSize.y;
}
else if (mode == ScaleMode.MatchWidthHeight)
{
newButtonSize.x = (referenceButtonSize.x * Screen.width) / referenceScreenSize.x;
newButtonSize.y = newButtonSize.x;
}
}
}
[System.Serializable]//we want our class and its members to be seen from the inspector
public class RevealSettings
{
public enum RevealOption { Linear, Circular };
public RevealOption option;
public float translateSmooth = 5f; //how fast the buttons move to their positions
public float fadeSmooth = 0.01f; //how fast the buttons fade in (if they fade in)
public bool revealOnStart = false;
[HideInInspector] //we do not need to see these variables in the inspector
public bool opening = false;
[HideInInspector]
public bool spawned = false;
}
[System.Serializable]
public class LinearSpawner
{
public enum RevealStyle { SlideToPosition, FadeInAtPosition };
public RevealStyle revealStyle;
public Vector2 direction = new Vector2(0, 1); //slide down
public float baseButtonSpacing = 5f; //how much space between each button
public int buttonNumOffset = 0; //how many button spaces offset? Sometimes necessary when using multiple button branchers
[HideInInspector]
public float buttonSpacing = 5f;
public void FitSpacingToScreenSize(Vector2 refScreenSize)
{
float refScreenFloat = (refScreenSize.x + refScreenSize.y) / 2;
float screenFloat = (Screen.width + Screen.height) / 2;
buttonSpacing = (baseButtonSpacing * screenFloat) / refScreenFloat;
}
}
[System.Serializable]
public class CircularSpawner
{
public enum RevealStyle { SlideToPosition, FadeInAtPosition };
public RevealStyle revealStyle;
public Angle angle;
public float baseDistFromBrancher = 20;
[HideInInspector]
public float distFromBrancher = 0;
[System.Serializable]
public struct Angle { public float minAngle; public float maxAngle; }
public void FitDistanceToScreenSize(Vector2 refScreenSize)
{
float refScreenFloat = (refScreenSize.x + refScreenSize.y) / 2;
float screenFloat = (Screen.width + Screen.height) / 2;
distFromBrancher = (baseDistFromBrancher * screenFloat) / refScreenFloat;
}
}
public GameObject[] buttonRefs; //PREFABS
[HideInInspector]
public List<GameObject> buttons;
public enum ScaleMode { MatchWidthHeight, IndependentWidthHeight };
public ScaleMode mode;
public Vector2 referenceButtonSize;
public Vector2 referenceScreenSize;
ButtonScaler buttonScaler = new ButtonScaler();
public RevealSettings revealSettings = new RevealSettings();
public LinearSpawner linSpawner = new LinearSpawner();
public CircularSpawner circSpawner = new CircularSpawner();
float lastScreenWidth = 0;
float lastScreenHeight = 0;
void Start()
{
buttons = new List<GameObject>();
buttonScaler = new ButtonScaler();
lastScreenWidth = Screen.width;
lastScreenHeight = Screen.height;
buttonScaler.Initialize(referenceButtonSize, referenceScreenSize, (int)mode);
circSpawner.FitDistanceToScreenSize(buttonScaler.referenceScreenSize);
linSpawner.FitSpacingToScreenSize(buttonScaler.referenceScreenSize);
if (revealSettings.revealOnStart)
{
SpawnButtons();
}
}
void Update()
{
if (Screen.width != lastScreenWidth || Screen.height != lastScreenHeight)
{
lastScreenWidth = Screen.width;
lastScreenHeight = Screen.height;
buttonScaler.Initialize(referenceButtonSize, referenceScreenSize, (int)mode);
circSpawner.FitDistanceToScreenSize(buttonScaler.referenceScreenSize);
linSpawner.FitSpacingToScreenSize(buttonScaler.referenceScreenSize);
SpawnButtons();
}
if (revealSettings.opening)
{
if (!revealSettings.spawned)
SpawnButtons();
switch (revealSettings.option)
{
case RevealSettings.RevealOption.Linear:
switch (linSpawner.revealStyle)
{
case LinearSpawner.RevealStyle.SlideToPosition: RevealLinearlyNormal(); break;
case LinearSpawner.RevealStyle.FadeInAtPosition: RevealLinearlyFade(); break;
}
break;
case RevealSettings.RevealOption.Circular:
switch (circSpawner.revealStyle)
{
case CircularSpawner.RevealStyle.SlideToPosition: RevealCircularNormal(); break;
case CircularSpawner.RevealStyle.FadeInAtPosition: RevealCircularFade(); break;
}
break;
}
}
}
public void SpawnButtons() //if revealOnStart == false, this method will be called by the button click event
{
revealSettings.opening = true;
//clear button list, in case there are some already in it
for (int i = buttons.Count - 1; i >= 0; i--)
Destroy(buttons[i]);
buttons.Clear();
//clear buttons on any other button brancher that has the same parent as this brancher
ClearCommonButtonBranchers();
for (int i = 0; i < buttonRefs.Length; i++ )
{
GameObject b = Instantiate(buttonRefs[i] as GameObject);
b.transform.SetParent(transform); //make button child of button brancher
b.transform.position = transform.position; //zeroing the position places the button on the button brancher
//check if button will fade or not
if (linSpawner.revealStyle == LinearSpawner.RevealStyle.FadeInAtPosition || circSpawner.revealStyle == CircularSpawner.RevealStyle.FadeInAtPosition)
{
//change color alpha of button and its text to 0;
Color c = b.GetComponent<Image>().color;
c.a = 0;
b.GetComponent<Image>().color = c;
if (b.GetComponentInChildren<Text>()) //button may not have a text component
{
c = b.GetComponentInChildren<Text>().color;
c.a = 0;
b.GetComponentInChildren<Text>().color = c;
}
}
buttons.Add(b);
}
revealSettings.spawned = true;
}
void RevealLinearlyNormal()
{
for (int i = 0; i < buttons.Count; i++)
{
//give the button a position to move toward
Vector3 targetPos;
RectTransform buttonRect = buttons[i].GetComponent<RectTransform>();
//set size
buttonRect.sizeDelta = new Vector2(buttonScaler.newButtonSize.x, buttonScaler.newButtonSize.y);
//set pos
targetPos.x = linSpawner.direction.x * ( (i + linSpawner.buttonNumOffset) * (buttonRect.sizeDelta.x + linSpawner.buttonSpacing) ) + transform.position.x;
targetPos.y = linSpawner.direction.y * ( (i + linSpawner.buttonNumOffset) * (buttonRect.sizeDelta.y + linSpawner.buttonSpacing) ) + transform.position.y;
targetPos.z = 0;
buttonRect.position = Vector3.Lerp(buttonRect.position, targetPos, revealSettings.translateSmooth * Time.deltaTime);
}
}
void RevealLinearlyFade()
{
for (int i = 0; i < buttons.Count; i++)
{
//give the button a position to move toward
Vector3 targetPos;
ButtonFader previousButtonFader;
if (i > 0)
previousButtonFader = buttons[i - 1].GetComponent<ButtonFader>();
else
previousButtonFader = null;
ButtonFader buttonFader = buttons[i].GetComponent<ButtonFader>();
RectTransform buttonRect = buttons[i].GetComponent<RectTransform>();
//set size
buttonRect.sizeDelta = new Vector2(buttonScaler.newButtonSize.x, buttonScaler.newButtonSize.y);
//set pos
targetPos.x = linSpawner.direction.x * ((i + linSpawner.buttonNumOffset) * (buttonRect.sizeDelta.x + linSpawner.buttonSpacing)) + transform.position.x;
targetPos.y = linSpawner.direction.y * ((i + linSpawner.buttonNumOffset) * (buttonRect.sizeDelta.y + linSpawner.buttonSpacing)) + transform.position.y;
targetPos.z = 0;
if (previousButtonFader) //first button wont have a previous button
{
if (previousButtonFader.faded)
{
buttons[i].transform.position = targetPos;
if (buttonFader)
buttonFader.Fade(revealSettings.fadeSmooth);
else
Debug.LogError("You want to fade your buttons, but they need a ButtonFader script to be attached first.");
}
}
else
{
buttons[i].transform.position = targetPos;
if (buttonFader)
buttonFader.Fade(revealSettings.fadeSmooth); //for the first button in the array
else
Debug.LogError("You want to fade your buttons, but they need a ButtonFader script to be attached first.");
}
}
}
void RevealCircularNormal()
{
for (int i = 0; i < buttons.Count; i++)
{
//find angle
float angleDist = circSpawner.angle.maxAngle - circSpawner.angle.minAngle;
float targetAngle = circSpawner.angle.minAngle + (angleDist / buttons.Count) * i;
//find pos
Vector3 targetPos = transform.position + Vector3.right * circSpawner.distFromBrancher;
targetPos = RotatePointAroundPivot(targetPos, transform.position, targetAngle);
RectTransform buttonRect = buttons[i].GetComponent<RectTransform>();
//resize button
buttonRect.sizeDelta = new Vector2(buttonScaler.newButtonSize.x, buttonScaler.newButtonSize.y);
buttonRect.position = Vector3.Lerp(buttonRect.position, targetPos, revealSettings.translateSmooth * Time.deltaTime);
}
}
void RevealCircularFade()
{
for (int i = 0; i < buttons.Count; i++)
{
ButtonFader previousButtonFader;
if (i > 0)
previousButtonFader = buttons[i - 1].GetComponent<ButtonFader>();
else
previousButtonFader = null;
ButtonFader buttonFader = buttons[i].GetComponent<ButtonFader>();
//find angle
float angleDist = circSpawner.angle.maxAngle - circSpawner.angle.minAngle;
float targetAngle = circSpawner.angle.minAngle + (angleDist / buttons.Count) * i;
//find pos
Vector3 targetPos = transform.position + Vector3.right * circSpawner.distFromBrancher;
targetPos = RotatePointAroundPivot(targetPos, transform.position, targetAngle);
RectTransform buttonRect = buttons[i].GetComponent<RectTransform>();
//resize button
buttonRect.sizeDelta = new Vector2(buttonScaler.newButtonSize.x, buttonScaler.newButtonSize.y);
if (previousButtonFader) //first button wont have a previous button
{
if (previousButtonFader.faded)
{
buttonRect.position = targetPos;
if (buttonFader)
buttonFader.Fade(revealSettings.fadeSmooth);
else
Debug.LogError("You want to fade your buttons, but they need a ButtonFader script to be attached first.");
}
}
else
{
buttonRect.position = targetPos;
if (buttonFader)
buttonFader.Fade(revealSettings.fadeSmooth); //for the first button in the array
else
Debug.LogError("You want to fade your buttons, but they need a ButtonFader script to be attached first.");
}
}
}
Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, float angle)
{
Vector3 targetPoint = point - pivot;
targetPoint = Quaternion.Euler(0, 0, angle) * targetPoint;
targetPoint += pivot;
return targetPoint;
}
void ClearCommonButtonBranchers()
{
GameObject[] branchers = GameObject.FindGameObjectsWithTag("ButtonBrancher");
foreach(GameObject brancher in branchers)
{
//check if the parent of this brancher is the same as the brancher we are currently looking at
if (brancher.transform.parent == transform.parent)
{
//remove the brancher's buttons to keep things tidy
ButtonBrancher bb = brancher.GetComponent<ButtonBrancher>();
for (int i = bb.buttons.Count - 1; i >= 0; i--)
Destroy(bb.buttons[i]);
bb.buttons.Clear();
}
}
}
}