Skip to content

Commit 87ce6dd

Browse files
committed
pages can now be closed with follow up opens
1 parent a571361 commit 87ce6dd

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

Menu/Page.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ namespace Menu {
77

88
public class Page : MonoBehaviour
99
{
10+
public static readonly string FLAG_ON = "On";
11+
public static readonly string FLAG_OFF = "Off";
12+
public static readonly string FLAG_NONE = "None";
13+
1014
public PageType type;
1115
public bool useAnimation;
12-
public bool isAnimating {get;private set;}
16+
public string targetState {get;private set;}
1317

1418
/*
1519
* Animaton Requirements...
@@ -18,7 +22,7 @@ public class Page : MonoBehaviour
1822
* 1. Resting
1923
* 2. Turning On
2024
* 3. Turning Off
21-
* - The animator must have a control boolean called 'on'. Otherwise a flag will be thrown.
25+
* - The animator must have a control boolean called 'on'. Otherwise the animator will not work.
2226
*/
2327
private Animator m_Animator;
2428

@@ -38,25 +42,24 @@ public void Animate(bool _on) {
3842

3943
StopCoroutine("AwaitAnimation");
4044
StartCoroutine("AwaitAnimation", _on);
41-
42-
Log("Page ["+type+"] finished transitioning to "+(_on ? "<color=#0f0>on</color>." : "<color=#f00>off</color>."));
4345
}
4446
}
4547
#endregion
4648

4749
#region Private Functions
4850
private IEnumerator AwaitAnimation(bool _on) {
49-
string _targetState = _on ? "On" : "Off";
50-
isAnimating = true;
51+
targetState = _on ? FLAG_ON : FLAG_OFF;
5152

52-
while (!m_Animator.GetCurrentAnimatorStateInfo(0).IsName(_targetState)) {
53+
while (!m_Animator.GetCurrentAnimatorStateInfo(0).IsName(targetState)) {
5354
yield return null;
5455
}
5556
while (m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1) {
5657
yield return null;
5758
}
5859

59-
isAnimating = false;
60+
targetState = FLAG_NONE;
61+
62+
Log("Page ["+type+"] finished transitioning to "+(_on ? "<color=#0f0>on</color>." : "<color=#f00>off</color>."));
6063

6164
if (!_on) {
6265
gameObject.SetActive(false);

Menu/PageController.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,29 @@ private void Awake() {
2626
m_OnList = new List<Page>();
2727
m_OffList = new List<Page>();
2828
RegisterAllPages();
29-
TurnPageOn(entryPage);
29+
30+
if (entryPage != PageType.None) {
31+
TurnPageOn(entryPage);
32+
}
3033
}
3134
}
3235

36+
#if UNITY_EDITOR
3337
private void Update() {
3438
if (Input.GetKeyUp(KeyCode.F)) {
3539
TurnPageOn(PageType.Loading);
3640
}
3741
if (Input.GetKeyUp(KeyCode.G)) {
3842
TurnPageOff(PageType.Loading);
3943
}
44+
if (Input.GetKeyUp(KeyCode.H)) {
45+
TurnPageOff(PageType.Loading, PageType.Menu);
46+
}
47+
if (Input.GetKeyUp(KeyCode.J)) {
48+
TurnPageOff(PageType.Loading, PageType.Menu, true);
49+
}
4050
}
51+
#endif
4152
#endregion
4253

4354
#region Public Functions
@@ -52,18 +63,36 @@ public void TurnPageOn(PageType _type) {
5263
_page.Animate(true);
5364
}
5465

55-
public void TurnPageOff(PageType _type) {
56-
if (!PageExists(_type)) {
57-
LogWarning("You are trying to turn a page off ["+_type+"] that has not been registered.");
66+
public void TurnPageOff(PageType _off, PageType _on=PageType.None, bool _waitForExit=false) {
67+
if (!PageExists(_off)) {
68+
LogWarning("You are trying to turn a page off ["+_on+"] that has not been registered.");
5869
return;
5970
}
6071

61-
Page _page = GetPage(_type);
62-
_page.Animate(false);
72+
Page _offPage = GetPage(_off);
73+
if (_offPage.gameObject.activeSelf) {
74+
_offPage.Animate(false);
75+
}
76+
77+
if (_waitForExit) {
78+
Page _onPage = GetPage(_on);
79+
StopCoroutine("WaitForPageExit");
80+
StartCoroutine(WaitForPageExit(_onPage, _offPage));
81+
} else {
82+
TurnPageOn(_on);
83+
}
6384
}
6485
#endregion
6586

6687
#region Private Functions
88+
private IEnumerator WaitForPageExit(Page _on, Page _off) {
89+
while (_off.targetState != Page.FLAG_NONE) {
90+
yield return null;
91+
}
92+
93+
TurnPageOn(_on.type);
94+
}
95+
6796
private void RegisterAllPages() {
6897
foreach(Page _page in pages) {
6998
RegisterPage(_page);

Menu/PageType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ namespace Menu {
44

55
public enum PageType {
66
None,
7-
Loading
7+
Loading,
8+
Menu
89
}
910
}
1011
}

0 commit comments

Comments
 (0)