Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
- public class UIManager : MonoBehaviour
- {
- private enum PromptType { Storage, Backpack, PrecisionDrop, None }
- private PromptType currentPromptType = PromptType.None;
- public static UIManager Instance { get; private set; }
- public GameObject inventoryPanel;
- public GameObject inventoryPrompt;
- public Text promptText;
- public Button yesButton;
- public Button noButton;
- public Button okButton;
- public Button closeButton;
- public Button inventoryDoneButton;
- public Transform inventorySlotParent;
- public Transform storageHolder;
- public GameObject moneyDisplay;
- public GameObject doneButton;
- public GameObject itemDetailsPanel;
- public TextMeshProUGUI itemTitleText;
- public TextMeshProUGUI itemDescriptionText;
- public TextMeshProUGUI itemUsesText;
- public TextMeshProUGUI inventoryTitleText;
- [SerializeField] private GameObject itemIconPrefab;
- public GameObject blanketPrefab;
- private InventorySystem inventorySystem;
- private StorageSystem storageSystem;
- private InteractionSystem interactionSystem;
- private PlayerMovement playerMovement;
- private MouseMovement mouseMovement;
- private Item currentStorageItem;
- private Item draggedItem;
- private Transform originalSlot;
- public bool isInventoryOpen = false;
- public GameObject characterCustomizationBtn;
- public GameObject backpackBtn;
- public TextMeshProUGUI backpackBtnText;
- private bool isMouseMenuMode = false;
- private void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- }
- else
- {
- Destroy(gameObject);
- }
- }
- private void Start()
- {
- inventorySystem = InventorySystem.Instance;
- storageSystem = StorageSystem.Instance;
- interactionSystem = FindObjectOfType<InteractionSystem>();
- playerMovement = FindObjectOfType<PlayerMovement>();
- mouseMovement = FindObjectOfType<MouseMovement>();
- if (closeButton != null) closeButton.onClick.AddListener(CloseInventory);
- if (okButton != null)
- {
- okButton.onClick.AddListener(ClosePrompt);
- okButton.gameObject.SetActive(false);
- }
- if (yesButton != null) yesButton.onClick.AddListener(HandleYesButton);
- if (noButton != null) noButton.onClick.AddListener(HandleNoButton);
- if (inventoryDoneButton != null) inventoryDoneButton.onClick.AddListener(CloseInventory);
- //backpack
- if (backpackBtn != null)
- backpackBtn.GetComponent<Button>().onClick.AddListener(() => {
- InteractionSystem.Instance.UnequipBackpack();
- UpdateBackpackButtons(false);
- });
- }
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.M))
- {
- isMouseMenuMode = !isMouseMenuMode;
- if (isMouseMenuMode)
- {
- SetCursorState(true);
- FreezePlayerMovement(true);
- }
- else
- {
- // If any UI is open, keep mouse/menu frozen
- if (inventoryPanel.activeSelf || inventoryPrompt.activeSelf)
- {
- SetCursorState(true);
- FreezePlayerMovement(true);
- }
- else
- {
- SetCursorState(false);
- FreezePlayerMovement(false);
- }
- }
- }
- }
- private void HandleYesButton()
- {
- switch (currentPromptType)
- {
- case PromptType.Storage:
- OpenInventory(currentStorageItem);
- break;
- case PromptType.Backpack:
- interactionSystem.WearBackpack();
- ClosePrompt();
- break;
- }
- }
- private void HandleNoButton()
- {
- switch (currentPromptType)
- {
- case PromptType.Storage:
- ClosePrompt();
- break;
- case PromptType.Backpack:
- UpdateBackpackPrompt();
- break;
- }
- }
- public void ShowInventoryPrompt(Item storageItem)
- {
- currentPromptType = PromptType.Storage;
- currentStorageItem = storageItem;
- inventoryPrompt.SetActive(true);
- promptText.text = $"Open {storageItem.itemName}?";
- yesButton.gameObject.SetActive(true);
- noButton.gameObject.SetActive(true);
- okButton.gameObject.SetActive(false);
- TogglePlayerControls(false);
- }
- public void ShowPrecisionDropMessage(bool isEnabled)
- {
- currentPromptType = PromptType.PrecisionDrop;
- inventoryPrompt.SetActive(true);
- promptText.text = isEnabled ? "Precision drop enabled" : "Precision drop disabled";
- yesButton.gameObject.SetActive(false);
- noButton.gameObject.SetActive(false);
- okButton.gameObject.SetActive(true);
- SetCursorState(true);
- FreezePlayerMovement(true);
- }
- public void ShowRotationMessage(bool enabled)
- {
- inventoryPrompt.SetActive(true);
- promptText.text = enabled
- ? "Rotation mode enabled. Use Q/E to rotate horizontally, Z/C to rotate vertically."
- : "Rotation mode disabled.";
- yesButton.gameObject.SetActive(false);
- noButton.gameObject.SetActive(false);
- okButton.gameObject.SetActive(true);
- SetCursorState(enabled);
- FreezePlayerMovement(enabled);
- }
- public void ShowBackpackPrompt(Item backpackItem)
- {
- currentPromptType = PromptType.Backpack;
- currentStorageItem = backpackItem;
- inventoryPrompt.SetActive(true);
- promptText.text = "Would you like to wear the backpack?";
- yesButton.gameObject.SetActive(true);
- noButton.gameObject.SetActive(true);
- okButton.gameObject.SetActive(false);
- SetCursorState(true);
- FreezePlayerMovement(true);
- }
- public void HandleBackpackPrompt(bool accepted)
- {
- if (accepted)
- {
- interactionSystem.WearBackpack();
- }
- else
- {
- UpdateBackpackPrompt();
- }
- }
- public void UpdateBackpackPrompt()
- {
- promptText.text = "Okay, if you change your mind press E to equip the backpack";
- yesButton.gameObject.SetActive(false);
- noButton.gameObject.SetActive(false);
- okButton.gameObject.SetActive(true);
- SetCursorState(true);
- FreezePlayerMovement(true);
- }
- public void UpdateBackpackButtons(bool backpackEquipped)
- {
- if (characterCustomizationBtn != null)
- characterCustomizationBtn.SetActive(!backpackEquipped);
- if (backpackBtn != null)
- {
- backpackBtn.SetActive(backpackEquipped);
- if (backpackBtnText != null)
- backpackBtnText.text = "Drop Backpack";
- }
- }
- public void ShowError(string message)
- {
- promptText.text = message;
- yesButton.gameObject.SetActive(false);
- noButton.gameObject.SetActive(false);
- okButton.gameObject.SetActive(true);
- }
- public void UpdatePromptText(string message)
- {
- if (promptText != null)
- promptText.text = message;
- }
- public void ShowOkayButton()
- {
- if (okButton != null)
- {
- okButton.gameObject.SetActive(true);
- yesButton.gameObject.SetActive(false);
- noButton.gameObject.SetActive(false);
- }
- }
- public void OpenInventory(Item storageItem)
- {
- inventoryPrompt.SetActive(false);
- inventoryPanel.SetActive(true);
- currentStorageItem = storageItem;
- isInventoryOpen = true;
- TransferHeldItemsToStorage();
- PopulateInventoryUI(storageItem);
- TogglePlayerControls(false);
- if (inventoryTitleText != null)
- {
- inventoryTitleText.text = storageItem.itemName;
- }
- }
- private void TransferHeldItemsToStorage()
- {
- if (inventorySystem.leftHandItem != null && inventorySystem.leftHandItem != currentStorageItem)
- {
- bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.leftHandItem);
- if (added)
- {
- inventorySystem.UnequipItem(true);
- playerMovement.UpdateCarryingAnimations();
- }
- }
- if (inventorySystem.rightHandItem != null && inventorySystem.rightHandItem != currentStorageItem)
- {
- bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.rightHandItem);
- if (added)
- {
- inventorySystem.UnequipItem(false);
- playerMovement.UpdateCarryingAnimations();
- }
- }
- // Only add a blanket if not already present
- if (currentStorageItem.isBackpack && blanketPrefab != null)
- {
- var storageItems = storageSystem.GetStorageInventory(currentStorageItem);
- Item existingBlanket = storageItems.Find(item => item.isBlanket);
- if (existingBlanket == null)
- {
- Item blanketItem = Instantiate(blanketPrefab).GetComponent<Item>();
- storageSystem.AddToStorage(currentStorageItem, blanketItem);
- }
- }
- }
- public void PopulateInventoryUI(Item storageItem)
- {
- UIHelperFunctions.Instance.PopulateInventoryUI(storageItem, inventorySlotParent, storageSystem, this);
- if (!storageItem.isBackpack) return;
- foreach (var storedItem in storageSystem.GetStorageInventory(storageItem))
- {
- if (storedItem.isBackpack && blanketPrefab != null)
- {
- Item blanketItem = Instantiate(blanketPrefab).GetComponent<Item>();
- storageSystem.AddToStorage(storedItem, blanketItem);
- }
- }
- }
- public void UpdateInventorySlot(GameObject slotObject, Item item)
- {
- UIHelperFunctions.Instance.UpdateInventorySlot(slotObject, item, itemIconPrefab, this);
- }
- public void ShowItemDetails(Item item)
- {
- UIHelperFunctions.Instance.ShowItemDetails(item, itemDetailsPanel, itemTitleText, itemDescriptionText, itemUsesText);
- }
- public void HideItemDetails()
- {
- UIHelperFunctions.Instance.HideItemDetails(itemDetailsPanel);
- }
- public void TryEquipItem(Item item)
- {
- UIHelperFunctions.Instance.TryEquipItem(item, inventorySystem, storageSystem, currentStorageItem, interactionSystem, playerMovement, this);
- }
- public void BeginDrag(Transform iconTransform)
- {
- UIHelperFunctions.Instance.BeginDrag(iconTransform, ref draggedItem, ref originalSlot, inventoryPanel);
- }
- public void EndDrag(Transform iconTransform)
- {
- UIHelperFunctions.Instance.EndDrag(iconTransform, draggedItem, originalSlot, inventorySlotParent, currentStorageItem, storageSystem, this);
- draggedItem = null;
- }
- public void CloseInventory()
- {
- inventoryPanel.SetActive(false);
- if (currentStorageItem != null && currentStorageItem.isBackpack)
- {
- var storageItems = storageSystem.GetStorageInventory(currentStorageItem);
- Item blanket = storageItems.Find(item => item.isBlanket);
- if (blanket != null)
- {
- storageSystem.RemoveItemFromStorage(currentStorageItem, blanket);
- Destroy(blanket.gameObject);
- }
- }
- isInventoryOpen = false;
- TogglePlayerControls(true);
- }
- public void ClosePrompt()
- {
- inventoryPrompt.SetActive(false);
- // If we're still in precision drop mode, restore gameplay controls (lock mouse, enable movement)
- if (interactionSystem != null && interactionSystem.IsPrecisionDropEnabled())
- {
- SetCursorState(false);
- FreezePlayerMovement(false);
- }
- else
- {
- SetCursorState(false);
- FreezePlayerMovement(false);
- }
- }
- private void TogglePlayerControls(bool enable)
- {
- if (playerMovement != null) playerMovement.enabled = enable;
- if (mouseMovement != null) mouseMovement.enabled = enable;
- SetCursorState(!enable);
- }
- private void SetCursorState(bool visible)
- {
- Cursor.visible = visible;
- Cursor.lockState = visible ? CursorLockMode.None : CursorLockMode.Locked;
- }
- private void FreezePlayerMovement(bool freeze)
- {
- if (playerMovement != null) playerMovement.enabled = !freeze;
- if (mouseMovement != null) mouseMovement.enabled = !freeze;
- }
- }
- public class ItemReference : MonoBehaviour
- {
- public Item item;
- }
Advertisement
Add Comment
Please, Sign In to add comment