You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

386 lines
15 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using SiegeSong;
using UnityEngine.EventSystems;
namespace SiegeSong
{
public class MenuManager : MonoBehaviour
{
public SaveFileLoader SaveFileLoader;
public GameObject SavedGameListRowPrefab;
public GameObject MenuContainer;
public GameObject MenuHelpers;
public GameObject PauseMenuOptions;
public GameObject StartMenuOptions;
public GameObject SettingsSubMenuOptions;
public GameObject FileMenu;
public GameObject FileListDisplay;
public Text FileMetaDataDisplay;
public Image FilePreviewImageDisplay;
public MetaData[] SavedGames;
public bool MenuActive = true;
public Sprite DefaultImagePreview;
public HintManager HintManager;
public int SelectedIndex;
public int SubMenuSelectedIndex;
public bool SaveLoadSubmenuOpen;
public bool SettingsSubmenuOpen;
public bool SaveLoadSubmenuInSavingMode;
public bool MenuOpening;
private Color activeColor = Color.white;
private Color inactiveColor = Color.grey;
private int activeFontSize;
private int inactiveFontSize;
private bool saveLoadHasDelayed;
private bool startMenu = true;
private bool previous;
void Start() { }
void LateUpdate()
{
if (!MenuActive && MenuOpening)
{
HintManager.SetHint(HintArea.MidLeft, "");
HintManager.SetHint(HintArea.MidRight, "");
HintManager.SetHint(HintArea.Center, "");
HintManager.SetHint(HintArea.FarLeft, "Confirm Selection", InputButton.ActivateAccept);
HintManager.SetHint(HintArea.FarRight, "Exit", InputButton.BackReject);
StartCoroutine(SaveFileLoader.TakeTemporaryScreenCapture());
MenuActive = true;
}
}
void Update()
{
var menuOptions = startMenu ? StartMenuOptions : PauseMenuOptions;
MenuHelpers.SetActive(MenuActive);
FileMenu.SetActive(SaveLoadSubmenuOpen);
if (MenuActive)
{
SaveFileLoader.RuntimeManager.InputManager.CurrentState = InputState.OutGameMenu;
Time.timeScale = 0.0f;
SaveFileLoader.RuntimeManager.DePopulateReferences();
for (var i = 0; i < menuOptions.transform.childCount; i++)
{
var childObject = menuOptions.transform.GetChild(i);
var childMenuOption = childObject.GetComponent<MenuOption>();
childMenuOption.Index = i;
childMenuOption.Text.color = SelectedIndex == i ? activeColor : inactiveColor;
childMenuOption.Background.color = SelectedIndex == i ? activeColor : inactiveColor;
childMenuOption.Text.fontSize = SelectedIndex == i ? activeFontSize : inactiveFontSize;
}
}
else
{
StartMenuOptions.SetActive(false);
PauseMenuOptions.SetActive(false);
}
}
public void SelectPreviousIndex()
{
previous = true;
SelectNextIndex();
}
public void SelectNextIndex()
{
var menuOptions = startMenu ? StartMenuOptions : PauseMenuOptions;
if (previous)
{
previous = false;
if (SaveLoadSubmenuOpen)
{
if (SavedGames.Length + (SaveLoadSubmenuInSavingMode ? 1 : 0) > 0)
{
if (SubMenuSelectedIndex == (SaveLoadSubmenuOpen ? SavedGames.Length - 1 + (SaveLoadSubmenuInSavingMode ? 1 : 0) : SettingsSubMenuOptions.transform.childCount - 1))
SubMenuSelectedIndex = 0;
else
SubMenuSelectedIndex++;
UpdateSaveLoadMetaDataTextDisplay();
UpdateSaveLoadSubmenu();
}
}
else
{
if (SelectedIndex == menuOptions.transform.childCount - 1)
SelectedIndex = 0;
else
SelectedIndex++;
}
}
else
{
if (SaveLoadSubmenuOpen)
{
if (SavedGames.Length + (SaveLoadSubmenuInSavingMode ? 1 : 0) > 0)
{
if (SubMenuSelectedIndex == 0)
SubMenuSelectedIndex = SaveLoadSubmenuOpen ? SavedGames.Length - 1 + (SaveLoadSubmenuInSavingMode ? 1 : 0) : SettingsSubMenuOptions.transform.childCount - 1;
else
SubMenuSelectedIndex--;
UpdateSaveLoadMetaDataTextDisplay();
UpdateSaveLoadSubmenu();
}
}
else
{
if (SelectedIndex == 0)
SelectedIndex = menuOptions.transform.childCount - 1;
else
SelectedIndex--;
}
}
}
public void ActivateSelectedOption()
{
if (!SaveLoadSubmenuOpen && !SettingsSubmenuOpen)
{
if (startMenu)
{
switch (SelectedIndex)
{
case 0:
StartNewGame();
break;
case 1:
OpenLoadingSubmenu();
break;
case 3:
OpenSettingsSubmenu();
break;
case 4:
ExitGame();
break;
}
}
else
{
switch (SelectedIndex)
{
case 0:
ResumeGame();
break;
case 1:
OpenSavingSubmenu();
break;
case 2:
OpenLoadingSubmenu();
break;
case 3:
OpenSettingsSubmenu();
break;
case 4:
ExitGame();
break;
}
}
}
else if (SaveLoadSubmenuOpen)
{
if (SaveLoadSubmenuInSavingMode)
SaveGame();
else
LoadGame();
HintManager.SetHint(HintArea.FarLeft, "");
HintManager.SetHint(HintArea.MidLeft, "");
HintManager.SetHint(HintArea.Center, "");
HintManager.SetHint(HintArea.MidRight, "");
HintManager.SetHint(HintArea.FarRight, "");
}
}
public void ResumeGame()
{
SaveFileLoader.RuntimeManager.PopulateReferences();
startMenu = false;
MenuActive = false;
SaveLoadSubmenuOpen = false;
SaveLoadSubmenuInSavingMode = false;
SelectedIndex = 0;
SubMenuSelectedIndex = 0;
Time.timeScale = 1.0f;
}
public void LoadGame()
{
SaveFileLoader.ActiveSaveFileLocation = $"{Application.persistentDataPath.Replace("/", "\\")}\\{SaveFileLoader.DirectoryName}\\{SavedGames[SubMenuSelectedIndex].PlayerFullName.Replace(" ", "_")}_{SavedGames[SubMenuSelectedIndex].FileNameSuffix}";
SaveFileLoader.LoadGameFile();
ResumeGame();
SaveFileLoader.RuntimeManager.PlayerActorInstance.GetComponentInChildren<NotificationManager>().Notify("Game Loaded!");
}
public void SaveGame()
{
if (SubMenuSelectedIndex == 0)
SaveFileLoader.SaveGameFile();
else
SaveFileLoader.SaveGameFile($"{Application.persistentDataPath.Replace("/", "\\")}\\{SaveFileLoader.DirectoryName}\\{SavedGames[SubMenuSelectedIndex - 1].PlayerFullName.Replace(" ", "_")}_{SavedGames[SubMenuSelectedIndex - 1].FileNameSuffix}");
ResumeGame();
SaveFileLoader.RuntimeManager.PlayerActorInstance.GetComponentInChildren<NotificationManager>().Notify("Game Saved!");
InitializeSaveLoadSubmenu();
}
public void UpdateSaveLoadMetaDataTextDisplay()
{
var selectedSavedGame = (!(SaveLoadSubmenuInSavingMode && SubMenuSelectedIndex == 0) && SubMenuSelectedIndex - (SaveLoadSubmenuInSavingMode ? 1 : 0) < SavedGames.Length && SubMenuSelectedIndex - (SaveLoadSubmenuInSavingMode ? 1 : 0) > -1) ? SavedGames[SubMenuSelectedIndex - (SaveLoadSubmenuInSavingMode ? 1 : 0)] : new MetaData();
var filePath = $"{Application.persistentDataPath.Replace("/", "\\")}\\{SaveFileLoader.DirectoryName}\\{selectedSavedGame.PlayerFullName.Replace(" ", "_")}_{selectedSavedGame.FileNameSuffix}";
if (!SaveLoadSubmenuInSavingMode)
SaveFileLoader.ActiveSaveFileLocation = filePath;
FileMetaDataDisplay.text = $"{selectedSavedGame.PlayerFullName}\n{selectedSavedGame.PlayerLevel}\n{selectedSavedGame.PlayerActiveQuestFriendlyName}\n{selectedSavedGame.InGameDate}\n{selectedSavedGame.LastPlayedOn}";
if (System.IO.File.Exists($"{filePath}.png"))
{
var imagePreviewData = System.IO.File.ReadAllBytes($"{filePath}.png");
var imagePreviewTexture = new Texture2D(selectedSavedGame.ScreenWidth, selectedSavedGame.ScreenHeight);
imagePreviewTexture.filterMode = FilterMode.Trilinear;
imagePreviewTexture.LoadImage(imagePreviewData);
var imagePreviewSprite = Sprite.Create(imagePreviewTexture, new Rect(0, 0, selectedSavedGame.ScreenWidth, selectedSavedGame.ScreenHeight), new Vector2(0.5f, 0.0f), 1.0f);
FilePreviewImageDisplay.sprite = imagePreviewSprite;
}
else
{
FilePreviewImageDisplay.sprite = DefaultImagePreview;
}
}
public void StartNewGame()
{
startMenu = false;
MenuActive = false;
SaveLoadSubmenuOpen = false;
SettingsSubmenuOpen = false;
Time.timeScale = 1.0f;
SaveFileLoader.RunNewGameSetup();
}
public void InitializeSaveLoadSubmenu()
{
SavedGames = SaveFileLoader.GetSaveFilesMetaData();
UpdateSaveLoadSubmenu();
}
public void UpdateSaveLoadSubmenu()
{
for (var i = 0; i < FileListDisplay.transform.childCount; i++)
if (FileListDisplay.transform.GetChild(i).gameObject.activeSelf)
Destroy(FileListDisplay.transform.GetChild(i).gameObject);
else
FileListDisplay.transform.GetChild(i).gameObject.SetActive(false);
for (var i = 0; i < SavedGames.Length + (SaveLoadSubmenuInSavingMode ? 1 : 0); i++)
{
var rowSizeBuffer = 4;
var newRow = Instantiate(SavedGameListRowPrefab);
var newRowMenuOption = newRow.GetComponent<MenuOption>();
var newRowText = newRowMenuOption.Text;
var newRowBackground = newRowMenuOption.Background;
var newRowRect = newRow.GetComponent<RectTransform>();
newRowMenuOption.InSubmenu = true;
newRowMenuOption.Index = i;
newRow.SetActive(true);
if (SaveLoadSubmenuInSavingMode && i == 0)
{
if (SubMenuSelectedIndex == 0)
{
newRowText.text = $"~ New Saved Game ~";
newRowText.color = activeColor;
newRowBackground.color = activeColor;
}
else
{
newRowText.text = $"New Saved Game";
newRowText.color = inactiveColor;
newRowBackground.color = inactiveColor;
}
}
else
{
var savedGame = SavedGames[i - (SaveLoadSubmenuInSavingMode ? 1 : 0)];
if (i + (SaveLoadSubmenuInSavingMode ? 1 : 0) == SubMenuSelectedIndex + (SaveLoadSubmenuInSavingMode ? 1 : 0))
{
newRowText.text = $"~ {savedGame.Title.Replace("_", " ")} ~";
newRowText.color = activeColor;
newRowBackground.color = activeColor;
}
else
{
newRowText.text = $"{savedGame.Title.Replace("_", " ")}";
newRowText.color = inactiveColor;
newRowBackground.color = inactiveColor;
}
}
newRowRect.transform.parent = FileListDisplay.transform;
newRowRect.transform.localScale = Vector3.one;
newRowRect.position = new Vector2(0, (newRowText.fontSize + rowSizeBuffer) * -(i + 1 + (SaveLoadSubmenuInSavingMode ? 1 : 0)));
newRowRect.offsetMin = new Vector2(00, (newRowText.fontSize + rowSizeBuffer) * -(i + 1 + (SaveLoadSubmenuInSavingMode ? 1 : 0)));
newRowRect.offsetMax = new Vector2(0, (newRowText.fontSize + rowSizeBuffer) * -(i + (SaveLoadSubmenuInSavingMode ? 1 : 0)));
newRowRect.sizeDelta = new Vector2(250, (newRowText.fontSize + rowSizeBuffer));
}
UpdateSaveLoadMetaDataTextDisplay();
}
public void OpenLoadingSubmenu()
{
SubMenuSelectedIndex = 0;
SaveLoadSubmenuOpen = true;
SaveLoadSubmenuInSavingMode = false;
InitializeSaveLoadSubmenu();
}
public void OpenSavingSubmenu()
{
SubMenuSelectedIndex = 0;
SaveLoadSubmenuOpen = true;
SaveLoadSubmenuInSavingMode = true;
InitializeSaveLoadSubmenu();
}
public void OpenSettingsSubmenu()
{
SubMenuSelectedIndex = 0;
SettingsSubmenuOpen = true;
}
public void ExitGame()
{
if (startMenu)
startMenu = false;
else
Application.Quit();
}
}
}