243 lines
7.4 KiB
C#
243 lines
7.4 KiB
C#
using OTGIntegrated.Core;
|
|
using OTGIntegrated.Crafting;
|
|
using OTGIntegrated.Dialogue;
|
|
using OTGIntegrated.Inventory;
|
|
using OTGIntegrated.Player;
|
|
using OTGIntegrated.Quests;
|
|
using OTGIntegrated.Save;
|
|
using OTGIntegrated.Talents;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace OTGIntegrated.UI
|
|
{
|
|
public enum UIWindowType
|
|
{
|
|
None,
|
|
Inventory,
|
|
Crafting,
|
|
QuestLog,
|
|
TalentTree,
|
|
Dialogue,
|
|
Pause
|
|
}
|
|
|
|
public sealed class UIManager : MonoBehaviour
|
|
{
|
|
public GameObject inventoryPanel;
|
|
public GameObject craftingPanel;
|
|
public GameObject questLogPanel;
|
|
public GameObject talentTreePanel;
|
|
public GameObject dialoguePanel;
|
|
public GameObject pausePanel;
|
|
|
|
public InventoryUI inventoryUI;
|
|
public CraftingUI craftingUI;
|
|
public QuestLogUI questLogUI;
|
|
public TalentTreeUI talentTreeUI;
|
|
public HUDController hudController;
|
|
|
|
public Button resumeButton;
|
|
public Button saveButton;
|
|
public Button loadButton;
|
|
public Button resetSaveButton;
|
|
|
|
private PlayerController playerController;
|
|
private UIWindowType currentWindow = UIWindowType.None;
|
|
|
|
public UIWindowType CurrentWindow => currentWindow;
|
|
public bool IsBlockingGameplay => currentWindow != UIWindowType.None;
|
|
|
|
public void Initialize(PlayerController controller)
|
|
{
|
|
playerController = controller;
|
|
GameManager gameManager = GameManager.Instance;
|
|
|
|
if (gameManager != null)
|
|
{
|
|
inventoryUI?.Initialize(gameManager.inventory);
|
|
craftingUI?.Initialize(gameManager.craftingSystem, gameManager.inventory);
|
|
questLogUI?.Initialize(gameManager.questManager);
|
|
talentTreeUI?.Initialize(gameManager.talentManager, gameManager.playerStats);
|
|
hudController?.Initialize(gameManager.playerStats, gameManager.levelSystem, gameManager.questManager, gameManager.abilityManager, gameManager.weaponManager);
|
|
}
|
|
|
|
if (resumeButton != null) resumeButton.onClick.AddListener(CloseCurrentWindow);
|
|
if (saveButton != null) saveButton.onClick.AddListener(() => FindObjectOfType<SaveSystem>()?.Save());
|
|
if (loadButton != null) loadButton.onClick.AddListener(() => FindObjectOfType<SaveSystem>()?.Load());
|
|
if (resetSaveButton != null) resetSaveButton.onClick.AddListener(() => FindObjectOfType<SaveSystem>()?.ResetSave());
|
|
|
|
HideAllPanels();
|
|
ApplyGameplayFocus();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.F5))
|
|
{
|
|
FindObjectOfType<SaveSystem>()?.Save();
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.F9))
|
|
{
|
|
FindObjectOfType<SaveSystem>()?.Load();
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.Backspace))
|
|
{
|
|
FindObjectOfType<SaveSystem>()?.ResetSave();
|
|
}
|
|
|
|
if (currentWindow == UIWindowType.Dialogue)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.I)) ToggleInventory();
|
|
if (Input.GetKeyDown(KeyCode.C)) ToggleCrafting();
|
|
if (Input.GetKeyDown(KeyCode.J)) ToggleQuestLog();
|
|
if (Input.GetKeyDown(KeyCode.K)) ToggleTalentTree();
|
|
|
|
if (Input.GetKeyDown(KeyCode.Escape))
|
|
{
|
|
if (currentWindow == UIWindowType.None)
|
|
{
|
|
OpenPause();
|
|
}
|
|
else
|
|
{
|
|
CloseCurrentWindow();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ToggleInventory() => ToggleWindow(UIWindowType.Inventory);
|
|
public void ToggleCrafting() => ToggleWindow(UIWindowType.Crafting);
|
|
public void ToggleQuestLog() => ToggleWindow(UIWindowType.QuestLog);
|
|
public void ToggleTalentTree() => ToggleWindow(UIWindowType.TalentTree);
|
|
public void OpenCrafting() => OpenWindow(UIWindowType.Crafting);
|
|
public void OpenPause() => OpenWindow(UIWindowType.Pause);
|
|
|
|
public void OpenDialogue()
|
|
{
|
|
CloseCurrentWindow(false);
|
|
currentWindow = UIWindowType.Dialogue;
|
|
SetPanel(dialoguePanel, true);
|
|
ApplyWindowFocus();
|
|
}
|
|
|
|
public void CloseDialogue()
|
|
{
|
|
if (currentWindow == UIWindowType.Dialogue)
|
|
{
|
|
SetPanel(dialoguePanel, false);
|
|
currentWindow = UIWindowType.None;
|
|
ApplyGameplayFocus();
|
|
}
|
|
}
|
|
|
|
public void CloseCurrentWindow()
|
|
{
|
|
CloseCurrentWindow(true);
|
|
}
|
|
|
|
public void OpenWindow(UIWindowType windowType)
|
|
{
|
|
if (windowType == UIWindowType.None)
|
|
{
|
|
CloseCurrentWindow();
|
|
return;
|
|
}
|
|
|
|
CloseCurrentWindow(false);
|
|
currentWindow = windowType;
|
|
SetPanel(GetPanel(windowType), true);
|
|
ApplyWindowFocus();
|
|
}
|
|
|
|
private void ToggleWindow(UIWindowType windowType)
|
|
{
|
|
if (currentWindow == windowType)
|
|
{
|
|
CloseCurrentWindow();
|
|
}
|
|
else
|
|
{
|
|
OpenWindow(windowType);
|
|
}
|
|
}
|
|
|
|
private void CloseCurrentWindow(bool restoreGameplay)
|
|
{
|
|
SetPanel(GetPanel(currentWindow), false);
|
|
currentWindow = UIWindowType.None;
|
|
if (restoreGameplay)
|
|
{
|
|
ApplyGameplayFocus();
|
|
}
|
|
}
|
|
|
|
private GameObject GetPanel(UIWindowType windowType)
|
|
{
|
|
switch (windowType)
|
|
{
|
|
case UIWindowType.Inventory:
|
|
return inventoryPanel;
|
|
case UIWindowType.Crafting:
|
|
return craftingPanel;
|
|
case UIWindowType.QuestLog:
|
|
return questLogPanel;
|
|
case UIWindowType.TalentTree:
|
|
return talentTreePanel;
|
|
case UIWindowType.Dialogue:
|
|
return dialoguePanel;
|
|
case UIWindowType.Pause:
|
|
return pausePanel;
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private void HideAllPanels()
|
|
{
|
|
SetPanel(inventoryPanel, false);
|
|
SetPanel(craftingPanel, false);
|
|
SetPanel(questLogPanel, false);
|
|
SetPanel(talentTreePanel, false);
|
|
SetPanel(dialoguePanel, false);
|
|
SetPanel(pausePanel, false);
|
|
currentWindow = UIWindowType.None;
|
|
}
|
|
|
|
private void SetPanel(GameObject panel, bool active)
|
|
{
|
|
if (panel != null)
|
|
{
|
|
panel.SetActive(active);
|
|
}
|
|
}
|
|
|
|
private void ApplyWindowFocus()
|
|
{
|
|
if (playerController != null)
|
|
{
|
|
playerController.SetInputEnabled(false);
|
|
}
|
|
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
}
|
|
|
|
private void ApplyGameplayFocus()
|
|
{
|
|
if (playerController != null)
|
|
{
|
|
playerController.SetInputEnabled(true);
|
|
}
|
|
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
}
|
|
}
|
|
}
|