-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuManager.cs
More file actions
99 lines (78 loc) · 2.33 KB
/
MenuManager.cs
File metadata and controls
99 lines (78 loc) · 2.33 KB
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
using System;
using System.Collections.Generic;
using UnityEngine;
namespace abMenuSystem
{
public class MenuManager : MonoBehaviour
{
public delegate void OnMenuEventCallback(string menuId, string menuEvent);
public event OnMenuEventCallback MenuEvent;
public bool OpenMenu(string id)
{
MenuPair newMenuPair = Array.Find(_menuList, menuPair => menuPair.id == id);
return OpenAndAddMenu(newMenuPair.menu);
}
public void ShowTopMenu()
{
Menu topMenu = _menuStack.Peek();
if (topMenu != null)
topMenu.Show();
}
public void HideTopMenu()
{
Menu topMenu = _menuStack.Peek();
if (topMenu != null)
topMenu.Hide();
}
public void CloseTopMenu()
{
HideTopMenu();
_menuStack.Pop();
}
public bool IsActive => HasMenus && _menuStack.Peek().Active;
public string TopMenuId => HasMenus ? GetMenuId(_menuStack.Peek()) : "";
[Serializable]
struct MenuPair
{
public string id;
public Menu menu;
}
void Awake()
{
foreach (MenuPair menuPair in _menuList)
{
menuPair.menu.MenuChangeEvent += OnMenuEvent;
menuPair.menu.Hide();
}
}
string GetMenuId(Menu menu)
{
MenuPair foundMenuPair = Array.Find(_menuList, menuPair => menuPair.menu == menu);
if (foundMenuPair.id != null)
return foundMenuPair.id;
return "";
}
bool OpenAndAddMenu(Menu newMenu)
{
if (newMenu == null)
return false;
if (HasMenus)
{
Menu curMenu = _menuStack.Peek();
if (curMenu == newMenu)
return false;
}
newMenu.Show();
_menuStack.Push(newMenu);
return true;
}
void OnMenuEvent(string evId, Menu menu)
{
MenuEvent?.Invoke(GetMenuId(menu), evId);
}
bool HasMenus => _menuStack.Count > 0;
[SerializeField]
MenuPair[] _menuList;
readonly Stack<Menu> _menuStack = new Stack<Menu>();
}
}