chenxin
2020-12-02 e6a2684b79e1b66844e37f99a18d17468455ee9e
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
using Core.Game;
using TowerDefense.Game;
using TowerDefense.UI.HUD;
using UnityEngine;
using UnityEngine.UI;
using GameUIState = TowerDefense.UI.HUD.GameUI.State;
 
namespace TowerDefense.UI
{
    /// <summary>
    /// In-game pause menu
    /// </summary>
    public class PauseMenu : MonoBehaviour
    {
        /// <summary>
        /// Enum to represent state of pause menu
        /// </summary>
        protected enum State
        {
            Open,
            LevelSelectPressed,
            RestartPressed,
            Closed
        }
 
        /// <summary>
        /// The CanvasGroup that holds the pause menu UI
        /// </summary>
        public Canvas pauseMenuCanvas;
 
        public Text titleText;
        
        public Text descriptionText;
 
        /// <summary>
        /// The buttons present in the pause menu
        /// </summary>
        public Button levelSelectConfirmButton;
 
        public Button restartConfirmButton;
        
        public Button levelSelectButton;
        
        public Button restartButton;
 
        public Image topPanel;
 
        /// <summary>
        /// Color to change the top panel to highlight confirmation button
        /// </summary>
        public Color topPanelDisabledColor = new Color(1, 1, 1, 1);
 
        /// <summary>
        /// State of pause menu
        /// </summary>
        protected State m_State;
 
        /// <summary>
        /// If the pause menu was opened/closed this frame
        /// </summary>
        bool m_MenuChangedThisFrame;
 
        /// <summary>
        /// Open the pause menu
        /// </summary>
        public void OpenPauseMenu()
        {
            SetPauseMenuCanvas(true);
 
            LevelItem level = GameManager.instance.GetLevelForCurrentScene();
            if (level == null)
            {
                return;
            }
            if (titleText != null)
            {
                titleText.text = level.name;
            }
            if (descriptionText != null)
            {
                descriptionText.text = level.description;
            }
 
            m_State = State.Open;
        }
 
        /// <summary>
        /// Fired when GameUI's State changes
        /// </summary>
        /// <param name="oldState">The State that GameUI is leaving</param>
        /// <param name="newState">The State that GameUI is entering</param>
        protected void OnGameUIStateChanged(GameUIState oldState, GameUIState newState)
        {
            m_MenuChangedThisFrame = true;
            if (newState == GameUIState.Paused)
            {
                OpenPauseMenu();
            }
            else
            {
                ClosePauseMenu();
            }
        }
 
        /// <summary>
        /// Level select button pressed, display/hide confirmation button
        /// </summary>
        public void LevelSelectPressed()
        {
            bool open = m_State == State.Open;
            restartButton.interactable = !open;
            topPanel.color = open ? topPanelDisabledColor : Color.white;
            levelSelectConfirmButton.gameObject.SetActive(open);
            m_State = open ? State.LevelSelectPressed : State.Open;
        }
 
        /// <summary>
        /// Restart button pressed, display/hide confirmation button
        /// </summary>
        public void RestartPressed()
        {
            bool open = m_State == State.Open;
            levelSelectButton.interactable = !open;
            topPanel.color = open ? topPanelDisabledColor : Color.white;
            restartConfirmButton.gameObject.SetActive(open);
            m_State = open ? State.RestartPressed : State.Open;
        }
 
        /// <summary>
        /// Close the pause menu
        /// </summary>
        public void ClosePauseMenu()
        {
            SetPauseMenuCanvas(false);
 
            levelSelectConfirmButton.gameObject.SetActive(false);
            restartConfirmButton.gameObject.SetActive(false);
            levelSelectButton.interactable = true;
            restartButton.interactable = true;
            topPanel.color = Color.white;
 
            m_State = State.Closed;
        }
 
        /// <summary>
        /// Hide the pause menu on awake
        /// </summary>
        protected void Awake()
        {
            SetPauseMenuCanvas(false);
            m_State = State.Closed;
        }
 
        /// <summary>
        /// Subscribe to GameUI's stateChanged event
        /// </summary>
        protected void Start()
        {
            if (GameUI.instanceExists)
            {
                GameUI.instance.stateChanged += OnGameUIStateChanged;
            }
        }
 
        /// <summary>
        /// Unpause the game if the game is paused and the Escape key is pressed
        /// </summary>
        protected virtual void Update()
        {
            if (m_MenuChangedThisFrame)
            {
                m_MenuChangedThisFrame = false;
                return;
            }
 
            if (UnityEngine.Input.GetKeyDown(KeyCode.Escape) && GameUI.instance.state == GameUIState.Paused)
            {
                Unpause();
            }
        }
 
        /// <summary>
        /// Show/Hide the pause menu canvas group
        /// </summary>
        protected void SetPauseMenuCanvas(bool enable)
        {
            pauseMenuCanvas.enabled = enable;
        }
 
        public void Pause()
        {
            if (GameUI.instanceExists)
            {
                GameUI.instance.Pause();
            }
        }
 
        public void Unpause()
        {
            if (GameUI.instanceExists)
            {
                GameUI.instance.Unpause();
            }
        }
    }
}