Firstborn/Assets/RPG Creation Kit/Scripts/PauseMenu/PauseMenuManager.cs

139 lines
3.8 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPGCreationKit;
using RPGCreationKit.Player;
using UnityEngine.InputSystem;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace RPGCreationKit
{
public class PauseMenuManager : MonoBehaviour
{
public bool menuOpened = false;
[SerializeField] private GameObject defaultButton;
[SerializeField] private GameObject pauseUI;
[SerializeField] private LoadPanel loadPanel;
[SerializeField] private SavePanel savePanel;
[SerializeField] private SettingsMenuManager settingsPanel;
public Button saveButton;
public GameObject menuButtons;
public GameObject previouslySelected;
public void INPUT_PauseGame(InputAction.CallbackContext value)
{
if (value.started)
ToggleMenu();
}
public void ToggleMenu()
{
if (TutorialAlertMessage.instance.messageOpened)
return;
menuOpened = !menuOpened;
// Open
if(menuOpened && GameStatus.instance.PlayerPlaying())
{
RckInput.input.SwitchCurrentActionMap("PauseMenu");
RckPlayer.instance.FreezeAndDisableControl();
RckPlayer.instance.mouseLook.UnlockCursor();
Time.timeScale = 0;
pauseUI.SetActive(true);
saveButton.enabled = GameStatus.instance.canSave;
OnShowingUp();
if (RckInput.isUsingGamepad && !previouslySelected)
EventSystem.current.SetSelectedGameObject(defaultButton);
}
// Close
else
{
CloseAndResume();
}
}
public void OnShowingUp()
{
menuButtons.SetActive(true);
if (RckInput.isUsingGamepad)
{
if (previouslySelected != null)
EventSystem.current.SetSelectedGameObject(previouslySelected);
else
EventSystem.current.SetSelectedGameObject(defaultButton);
}
}
public void LoadButton()
{
menuButtons.SetActive(false);
previouslySelected = EventSystem.current.currentSelectedGameObject;
loadPanel.gameObject.SetActive(true);
if (RckInput.isUsingGamepad)
loadPanel.SelectFirstElementInList();
}
public void SettingsButton()
{
menuButtons.SetActive(false);
previouslySelected = EventSystem.current.currentSelectedGameObject;
settingsPanel.OpenSettings();
if (RckInput.isUsingGamepad)
settingsPanel.SelectFirstElementInList();
}
public void SaveButton()
{
menuButtons.SetActive(false);
previouslySelected = EventSystem.current.currentSelectedGameObject;
savePanel.gameObject.SetActive(true);
if (RckInput.isUsingGamepad)
savePanel.SelectFirstElementInList();
}
public void ResumeButton()
{
CloseAndResume();
}
public void CloseAndResume()
{
RckInput.input.SwitchCurrentActionMap("Player");
previouslySelected = null;
RckPlayer.instance.mouseLook.LockCursor();
Time.timeScale = 1;
pauseUI.SetActive(false);
RckPlayer.instance.UnfreezeAndEnableControls();
if(RckPlayer.instance.isInCutsceneMode)
RckPlayer.instance.EnterInCutsceneMode();
savePanel.ClosePanel();
loadPanel.ClosePanel();
}
public void QuitToMainMenu()
{
SceneManager.LoadScene("_MainMenu_");
}
}
}