Files
HauntedBloodlines/Assets/Scripts/Inventory/InventoryManager/InventoryCollectablesMenuManager.cs
2025-05-29 22:31:40 +03:00

432 lines
16 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;
public class InventoryCollectablesMenuManager : MonoBehaviour
{
[Header("General")]
public GameObject diariesMenu;
public GameObject diariesMenuButtonPrompts;
public RectTransform diaryEntryTextScrollView;
public ScrollRect diaryEntryScrollRect;
public TextMeshProUGUI entryText;
public GameObject previewEntryPageMenu;
public GameObject readingEntryPageMenu;
public RawImage entryImage;
[Header("Medea Diary Related")]
public GameObject medeaDiaryEntriesMenu;
public List<Button> medeaDiaryDiaryEntryButton;
public List<string> loadedDiaryEntryNames;
public bool updateCursorVisibility;
public bool shouldUpdateMedeaDiaryMenuInput = true;
public bool shouldUpdatePreviewEntryPageInput;
public bool shouldUpdateControllerInput;
[Header("Redable Item")]
public GameObject readingRedableItemPageMenu;
public RectTransform redableItemTextScrollView;
public TextMeshProUGUI redableItemEntryText;
public ScrollRect redableItemScrollRect;
public bool shouldUpdateRedableItemMenuInput = true;
private static InventoryCollectablesMenuManager _instance;
public static InventoryCollectablesMenuManager GetInstance() { return _instance; }
private void Awake()
{
if (!_instance)
{
_instance = this;
}
}
public enum CollectableMenuState
{
None,
CollectablesMenu,
MedeaDiaryMenu,
RedableItemMenu
}
public CollectableMenuState collectableMenuState = CollectableMenuState.None;
public enum EntryMenuState
{
None,
PreviewEntryPage,
ReadingEntryPage,
}
public EntryMenuState entryMenuState = EntryMenuState.None;
public enum RedableItemMenuState
{
None,
ReadingRedableItemsPage
}
public RedableItemMenuState redableItemMenuState = RedableItemMenuState.None;
// Update is called once per frame
void Update()
{
//UpdateCollectablesMenu();
switch (collectableMenuState)
{
case CollectableMenuState.None:
break;
case CollectableMenuState.MedeaDiaryMenu:
if (updateCursorVisibility)
{
InputControlManager.Getinstance().CursorVisibilityUpdater();
}
if (shouldUpdateMedeaDiaryMenuInput)
{
if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Mouse1) || Input.GetKeyDown(KeyCode.JoystickButton1))
{
updateCursorVisibility = false;
medeaDiaryEntriesMenu.SetActive(false);
InventoryManager.GetInstance().GoToInventoryMenuState();
StartCoroutine(InventoryInputIsEnabledAfterSeconds());
//diaryEntryTextScrollView.gameObject.SetActive(false);
diariesMenu.SetActive(false);
InventoryManager.GetInstance().inventoryCollectablesCategoryGO.SetActive(true);
InventoryManager.GetInstance().inventoryButtonPromptsPanel.SetActive(true);
InputControlManager.Getinstance().CursorVisibility(false);
UIManager.GetInstance().customPassUIRenderOnTop_UIAndModels.gameObject.SetActive(true);
UIManager.GetInstance().CustomPassUIRenderOnTop_UIOnly.gameObject.SetActive(false);
RingInventory.GetInstance().UpdateItemDescription();
GoToCollectableMenuNoneState();
}
}
UpdateControllerInput(diaryEntryScrollRect);
break;
case CollectableMenuState.RedableItemMenu:
if (updateCursorVisibility)
{
InputControlManager.Getinstance().CursorVisibilityUpdater();
}
if (shouldUpdateRedableItemMenuInput)
{
if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Mouse1) || Input.GetKeyDown(KeyCode.JoystickButton1))
{
updateCursorVisibility = false;
InventoryManager.GetInstance().GoToInventoryMenuState();
StartCoroutine(InventoryInputIsEnabledAfterSeconds());
readingRedableItemPageMenu.SetActive(false);
InventoryManager.GetInstance().inventoryCollectablesCategoryGO.SetActive(true);
InventoryManager.GetInstance().inventoryButtonPromptsPanel.SetActive(true);
InputControlManager.Getinstance().CursorVisibility(false);
UIManager.GetInstance().customPassUIRenderOnTop_UIAndModels.gameObject.SetActive(true);
UIManager.GetInstance().CustomPassUIRenderOnTop_UIOnly.gameObject.SetActive(false);
RingInventory.GetInstance().UpdateItemDescription();
GoToCollectableMenuNoneState();
}
}
UpdateControllerInput(redableItemScrollRect);
break;
default:
break;
}
switch (entryMenuState)
{
case EntryMenuState.None:
break;
case EntryMenuState.PreviewEntryPage:
#region Read Input
if (Input.GetKeyDown(KeyCode.JoystickButton0/*A Button*/) && shouldUpdatePreviewEntryPageInput
|| Input.GetKeyDown(KeyCode.E) && shouldUpdatePreviewEntryPageInput)
{
GoToReadingEntryPage();
}
#endregion
if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Mouse1) || Input.GetKeyDown(KeyCode.JoystickButton1 /*B Button*/))
{
//Disables the menu and goes to Entry Menu None state.
GoToEntryMenuNoneState();
}
break;
case EntryMenuState.ReadingEntryPage:
if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Mouse1) || Input.GetKeyDown(KeyCode.JoystickButton1 /*B Button*/))
{
GoToPreviewEntryPage();
}
break;
default:
break;
}
switch (redableItemMenuState)
{
case RedableItemMenuState.None:
break;
case RedableItemMenuState.ReadingRedableItemsPage:
if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Mouse1) || Input.GetKeyDown(KeyCode.JoystickButton1 /*B Button*/))
{
GoToRedableItemMenuNoneState();
}
break;
default:
break;
}
}
public void UpdateCollectablesMenu()
{
switch (collectableMenuState)
{
case CollectableMenuState.None:
break;
case CollectableMenuState.CollectablesMenu:
break;
case CollectableMenuState.MedeaDiaryMenu:
InventoryManager.GetInstance().InventoryInputIsDisabled();
InventoryManager.GetInstance().inventoryCollectablesCategoryGO.SetActive(false);
InventoryManager.GetInstance().inventoryButtonPromptsPanel.SetActive(false);
UIManager.GetInstance().customPassUIRenderOnTop_UIAndModels.gameObject.SetActive(false);
UIManager.GetInstance().CustomPassUIRenderOnTop_UIOnly.gameObject.SetActive(true);
diariesMenu.SetActive(true);
medeaDiaryEntriesMenu.SetActive(true);
UpdateMedeaDiaryButtonsInteractability();
RingInventory.GetInstance().CleanItemNameAndDescription();
//shouldUpdateControllerInput = true;
//StartCoroutine(ControllerInputForScroll(diaryEntryScrollRect));
//UIManager.GetInstance().eventSystem.SetSelectedGameObject(medeaDiaryDiaryEntryButton[0].gameObject);
EventSystemController.GetInstance().firstSelectedButton = medeaDiaryDiaryEntryButton[0].gameObject;
EventSystem.current.SetSelectedGameObject(EventSystemController.GetInstance().firstSelectedButton);
updateCursorVisibility = true;
break;
case CollectableMenuState.RedableItemMenu:
InventoryManager.GetInstance().InventoryInputIsDisabled();
InventoryManager.GetInstance().inventoryCollectablesCategoryGO.SetActive(false);
InventoryManager.GetInstance().inventoryButtonPromptsPanel.SetActive(false);
UIManager.GetInstance().customPassUIRenderOnTop_UIAndModels.gameObject.SetActive(false);
UIManager.GetInstance().CustomPassUIRenderOnTop_UIOnly.gameObject.SetActive(true);
EnableReadingRedableItemPageMenu();
RingInventory.GetInstance().CleanItemNameAndDescription();
shouldUpdateControllerInput = true;
//StartCoroutine(ControllerInputForScroll(redableItemScrollRect));
updateCursorVisibility = true;
break;
default:
break;
}
}
#region Update Controller Input
IEnumerator ControllerInputForScroll(ScrollRect diaryEntryScrollRect)
{
while (shouldUpdateControllerInput)
{
float DpadVertical = Input.GetAxis("DpadVertical");
float newPosition = diaryEntryScrollRect.verticalNormalizedPosition + DpadVertical * 1 * Time.deltaTime;
newPosition = Mathf.Clamp01(newPosition); // Ensure position stays within valid range (0 to 1)
diaryEntryScrollRect.verticalNormalizedPosition = newPosition;
//if (DpadVertical > 0)
//{
// print("Scrolling up");
// diaryEntryScrollRect.verticalNormalizedPosition -= 1 * Time.deltaTime;
//}
//if (DpadVertical < 0)
//{
// print("Scrolling down");
// diaryEntryScrollRect.verticalNormalizedPosition += 1 * Time.deltaTime;
//}
yield return null;
}
}
public void UpdateControllerInput(ScrollRect scrollRect)
{
if (shouldUpdateControllerInput)
{
float DpadVertical = Input.GetAxis("Joystick Y");
float newPosition = scrollRect.verticalNormalizedPosition + DpadVertical * 1 * Time.unscaledDeltaTime;
newPosition = Mathf.Clamp01(newPosition); // Ensure position stays within valid range (0 to 1)
scrollRect.verticalNormalizedPosition = newPosition;
}
}
#endregion
#region INPUT COROUTINES RELATED
IEnumerator InventoryInputIsEnabledAfterSeconds()
{
yield return new WaitForSecondsRealtime(0.1f);
InventoryManager.GetInstance().InventoryInputIsEnabled();
}
IEnumerator MedeaDiaryMenuInputIsEnabledAfterSeconds()
{
yield return new WaitForSecondsRealtime(0.1f);
shouldUpdateMedeaDiaryMenuInput = true;
}
IEnumerator EnablePreviewEntryPageInputAfterSeconds()
{
yield return new WaitForSecondsRealtime(0.1f);
shouldUpdatePreviewEntryPageInput = true;
}
#endregion
#region MENUS ACTIVATION DEACTIVATION
public void EnablePreviewEntryImageRelatedMenu()
{
previewEntryPageMenu.SetActive(true);
}
public void DisablePreviewEntryImageRelatedMenu()
{
previewEntryPageMenu.SetActive(false);
}
public void EnableReadingEntryImageRelatedMenu()
{
readingEntryPageMenu.SetActive(true);
}
public void DisableReadingEntryImageRelatedMenu()
{
readingEntryPageMenu.SetActive(false);
}
public void DisableReadingRedableItemPageMenu()
{
readingRedableItemPageMenu.SetActive(false);
}
public void EnableReadingRedableItemPageMenu()
{
readingRedableItemPageMenu.SetActive(true);
}
#endregion
#region Collectable Menu States Related
public void UpdateCollectableMenuStateByName(string StateName)
{
collectableMenuState = (CollectableMenuState)Enum.Parse(typeof(CollectableMenuState), StateName);
}
public bool IsOnCollectableMenuState(CollectableMenuState checkState)
{
return collectableMenuState == checkState;
}
public void GoToMedeaDiaryMenu()
{
collectableMenuState = CollectableMenuState.MedeaDiaryMenu;
}
public void GoToCollectableMenuNoneState()
{
collectableMenuState = CollectableMenuState.None;
}
public void GoToRedableItemMenu()
{
EnableReadingRedableItemPageMenu();
collectableMenuState = CollectableMenuState.RedableItemMenu;
}
#endregion
#region EntryMenuState
public void UpdateEntryMenuStateByName(string StateName)
{
entryMenuState = (EntryMenuState)Enum.Parse(typeof(EntryMenuState), StateName);
}
public bool IsOnEntryMenuStateState(EntryMenuState checkState)
{
return entryMenuState == checkState;
}
public void GoToPreviewEntryPage()
{
shouldUpdateControllerInput = false;
StartCoroutine(EnablePreviewEntryPageInputAfterSeconds());
shouldUpdateMedeaDiaryMenuInput = false;
diariesMenuButtonPrompts.SetActive(false);
DisableReadingEntryImageRelatedMenu();
EnablePreviewEntryImageRelatedMenu();
entryMenuState = EntryMenuState.PreviewEntryPage;
}
public void GoToReadingEntryPage()
{
shouldUpdateControllerInput = true;
shouldUpdatePreviewEntryPageInput = false;
DisablePreviewEntryImageRelatedMenu();
EnableReadingEntryImageRelatedMenu();
entryMenuState = EntryMenuState.ReadingEntryPage;
}
public void GoToEntryMenuNoneState()
{
shouldUpdateControllerInput = false;
shouldUpdateControllerInput = false;
shouldUpdatePreviewEntryPageInput = false;
DisablePreviewEntryImageRelatedMenu();
DisableReadingEntryImageRelatedMenu();
StartCoroutine(MedeaDiaryMenuInputIsEnabledAfterSeconds());
diariesMenuButtonPrompts.SetActive(true);
entryMenuState = EntryMenuState.None;
}
#endregion
#region RedableItemMenuState
public void GoToRedableItemMenuNoneState()
{
shouldUpdateControllerInput = false;
shouldUpdateRedableItemMenuInput = false;
DisableReadingRedableItemPageMenu();
StartCoroutine(InventoryInputIsEnabledAfterSeconds());
redableItemMenuState = RedableItemMenuState.None;
}
#endregion
#region Select Diary Menu Buttons Interactability Update
public void UpdateMedeaDiaryButtonsInteractability()
{
int selectedSaveSlotID = SaveSlotManager.GetInstance().selectedSaveSlotID;
GameSaveData loadedData = SaveSlotManager.LoadPlayerData(selectedSaveSlotID);
if (loadedData != null)
{
/*List<string> */loadedDiaryEntryNames = loadedData.collectablesData.medeaDiaryEntryName;
LoadDiaryButtonsInteractability(medeaDiaryDiaryEntryButton, loadedDiaryEntryNames);
}
}
#endregion
public void LoadDiaryButtonsInteractability(List<Button> diaryEntryButton, List<string> loadedDiaryEntryName)
{
foreach (var loadedDiaryEntry in loadedDiaryEntryName)
{
foreach (var button in diaryEntryButton)
{
if (button.GetComponent<CollectableEntryMenuItem>().entryName == loadedDiaryEntry)
{
button.interactable = true;
button.GetComponent<CollectableEntryMenuItem>().UpdateEntry();
//entryImage.texture = button.GetComponent<CollectableEntryMenuItem>().diaryEntryImage;
print("Is interactable and the entry has updated");
}
}
}
}
}