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