evelynshilosky

UIManager - Part 6.3.4

May 2nd, 2025
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.98 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using TMPro;
  4.  
  5. public class UIManager : MonoBehaviour
  6. {
  7.     private enum PromptType { Storage, Backpack, PrecisionDrop, None }
  8.     private PromptType currentPromptType = PromptType.None;
  9.  
  10.     public static UIManager Instance { get; private set; }
  11.  
  12.     public GameObject inventoryPanel;
  13.     public GameObject inventoryPrompt;
  14.     public Text promptText;
  15.     public Button yesButton;
  16.     public Button noButton;
  17.     public Button okButton;
  18.     public Button closeButton;
  19.     public Button inventoryDoneButton;
  20.     public Transform inventorySlotParent;
  21.     public Transform storageHolder;
  22.     public GameObject moneyDisplay;
  23.     public GameObject doneButton;
  24.     public GameObject itemDetailsPanel;
  25.     public TextMeshProUGUI itemTitleText;
  26.     public TextMeshProUGUI itemDescriptionText;
  27.     public TextMeshProUGUI itemUsesText;
  28.     public TextMeshProUGUI inventoryTitleText;
  29.  
  30.     [SerializeField] private GameObject itemIconPrefab;
  31.     public GameObject blanketPrefab;
  32.  
  33.     private InventorySystem inventorySystem;
  34.     private StorageSystem storageSystem;
  35.     private InteractionSystem interactionSystem;
  36.     private PlayerMovement playerMovement;
  37.     private MouseMovement mouseMovement;
  38.     private Item currentStorageItem;
  39.     private Item draggedItem;
  40.     private Transform originalSlot;
  41.  
  42.     public bool isInventoryOpen = false;
  43.  
  44.     public GameObject characterCustomizationBtn;
  45.     public GameObject backpackBtn;
  46.     public TextMeshProUGUI backpackBtnText;
  47.  
  48.     private bool isMouseMenuMode = false;
  49.  
  50.     private void Awake()
  51.     {
  52.         if (Instance == null)
  53.         {
  54.             Instance = this;
  55.         }
  56.         else
  57.         {
  58.             Destroy(gameObject);
  59.         }
  60.     }
  61.  
  62.     private void Start()
  63.     {
  64.         inventorySystem = InventorySystem.Instance;
  65.         storageSystem = StorageSystem.Instance;
  66.         interactionSystem = FindObjectOfType<InteractionSystem>();
  67.         playerMovement = FindObjectOfType<PlayerMovement>();
  68.         mouseMovement = FindObjectOfType<MouseMovement>();
  69.  
  70.         if (closeButton != null) closeButton.onClick.AddListener(CloseInventory);
  71.         if (okButton != null)
  72.         {
  73.             okButton.onClick.AddListener(ClosePrompt);
  74.             okButton.gameObject.SetActive(false);
  75.         }
  76.         if (yesButton != null) yesButton.onClick.AddListener(HandleYesButton);
  77.         if (noButton != null) noButton.onClick.AddListener(HandleNoButton);
  78.         if (inventoryDoneButton != null) inventoryDoneButton.onClick.AddListener(CloseInventory);
  79.  
  80.         //backpack
  81.         if (backpackBtn != null)
  82.             backpackBtn.GetComponent<Button>().onClick.AddListener(() => {
  83.                 InteractionSystem.Instance.UnequipBackpack();
  84.                 UpdateBackpackButtons(false);
  85.             });
  86.  
  87.     }
  88.  
  89.     private void Update()
  90.     {
  91.         if (Input.GetKeyDown(KeyCode.M))
  92.         {
  93.             isMouseMenuMode = !isMouseMenuMode;
  94.             if (isMouseMenuMode)
  95.             {
  96.                 SetCursorState(true);
  97.                 FreezePlayerMovement(true);
  98.             }
  99.             else
  100.             {
  101.                 // If any UI is open, keep mouse/menu frozen
  102.                 if (inventoryPanel.activeSelf || inventoryPrompt.activeSelf)
  103.                 {
  104.                     SetCursorState(true);
  105.                     FreezePlayerMovement(true);
  106.                 }
  107.                 else
  108.                 {
  109.                     SetCursorState(false);
  110.                     FreezePlayerMovement(false);
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.     private void HandleYesButton()
  117.     {
  118.         switch (currentPromptType)
  119.         {
  120.             case PromptType.Storage:
  121.                 OpenInventory(currentStorageItem);
  122.                 break;
  123.             case PromptType.Backpack:
  124.                 interactionSystem.WearBackpack();
  125.                 ClosePrompt();
  126.                 break;
  127.         }
  128.     }
  129.  
  130.     private void HandleNoButton()
  131.     {
  132.         switch (currentPromptType)
  133.         {
  134.             case PromptType.Storage:
  135.                 ClosePrompt();
  136.                 break;
  137.             case PromptType.Backpack:
  138.                 UpdateBackpackPrompt();
  139.                 break;
  140.         }
  141.     }
  142.  
  143.     public void ShowInventoryPrompt(Item storageItem)
  144.     {
  145.         currentPromptType = PromptType.Storage;
  146.         currentStorageItem = storageItem;
  147.         inventoryPrompt.SetActive(true);
  148.         promptText.text = $"Open {storageItem.itemName}?";
  149.         yesButton.gameObject.SetActive(true);
  150.         noButton.gameObject.SetActive(true);
  151.         okButton.gameObject.SetActive(false);
  152.         TogglePlayerControls(false);
  153.     }
  154.  
  155.     public void ShowPrecisionDropMessage(bool isEnabled)
  156.     {
  157.         currentPromptType = PromptType.PrecisionDrop;
  158.         inventoryPrompt.SetActive(true);
  159.  
  160.         promptText.text = isEnabled ? "Precision drop enabled" : "Precision drop disabled";
  161.  
  162.         yesButton.gameObject.SetActive(false);
  163.         noButton.gameObject.SetActive(false);
  164.         okButton.gameObject.SetActive(true);
  165.  
  166.         SetCursorState(true);
  167.         FreezePlayerMovement(true);
  168.     }
  169.  
  170.     public void ShowRotationMessage(bool enabled)
  171.     {
  172.         inventoryPrompt.SetActive(true);
  173.  
  174.         promptText.text = enabled
  175.             ? "Rotation mode enabled. Use Q/E to rotate horizontally, Z/C to rotate vertically."
  176.             : "Rotation mode disabled.";
  177.  
  178.         yesButton.gameObject.SetActive(false);
  179.         noButton.gameObject.SetActive(false);
  180.         okButton.gameObject.SetActive(true);
  181.  
  182.         SetCursorState(enabled);
  183.         FreezePlayerMovement(enabled);
  184.     }
  185.  
  186.     public void ShowBackpackPrompt(Item backpackItem)
  187.     {
  188.         currentPromptType = PromptType.Backpack;
  189.         currentStorageItem = backpackItem;
  190.         inventoryPrompt.SetActive(true);
  191.         promptText.text = "Would you like to wear the backpack?";
  192.         yesButton.gameObject.SetActive(true);
  193.         noButton.gameObject.SetActive(true);
  194.         okButton.gameObject.SetActive(false);
  195.         SetCursorState(true);
  196.         FreezePlayerMovement(true);
  197.     }
  198.  
  199.     public void HandleBackpackPrompt(bool accepted)
  200.     {
  201.         if (accepted)
  202.         {
  203.             interactionSystem.WearBackpack();
  204.         }
  205.         else
  206.         {
  207.             UpdateBackpackPrompt();
  208.         }
  209.     }
  210.  
  211.     public void UpdateBackpackPrompt()
  212.     {
  213.         promptText.text = "Okay, if you change your mind press E to equip the backpack";
  214.         yesButton.gameObject.SetActive(false);
  215.         noButton.gameObject.SetActive(false);
  216.         okButton.gameObject.SetActive(true);
  217.         SetCursorState(true);
  218.         FreezePlayerMovement(true);
  219.     }
  220.  
  221.     public void UpdateBackpackButtons(bool backpackEquipped)
  222.     {
  223.         if (characterCustomizationBtn != null)
  224.             characterCustomizationBtn.SetActive(!backpackEquipped);
  225.  
  226.         if (backpackBtn != null)
  227.         {
  228.             backpackBtn.SetActive(backpackEquipped);
  229.             if (backpackBtnText != null)
  230.                 backpackBtnText.text = "Drop Backpack";
  231.         }
  232.     }
  233.  
  234.     public void ShowError(string message)
  235.     {
  236.         promptText.text = message;
  237.         yesButton.gameObject.SetActive(false);
  238.         noButton.gameObject.SetActive(false);
  239.         okButton.gameObject.SetActive(true);
  240.     }
  241.  
  242.     public void UpdatePromptText(string message)
  243.     {
  244.         if (promptText != null)
  245.             promptText.text = message;
  246.     }
  247.  
  248.     public void ShowOkayButton()
  249.     {
  250.         if (okButton != null)
  251.         {
  252.             okButton.gameObject.SetActive(true);
  253.             yesButton.gameObject.SetActive(false);
  254.             noButton.gameObject.SetActive(false);
  255.         }
  256.     }
  257.  
  258.     public void OpenInventory(Item storageItem)
  259.     {
  260.         inventoryPrompt.SetActive(false);
  261.         inventoryPanel.SetActive(true);
  262.  
  263.         currentStorageItem = storageItem;
  264.         isInventoryOpen = true;
  265.  
  266.         TransferHeldItemsToStorage();
  267.         PopulateInventoryUI(storageItem);
  268.         TogglePlayerControls(false);
  269.  
  270.         if (inventoryTitleText != null)
  271.         {
  272.             inventoryTitleText.text = storageItem.itemName;
  273.         }
  274.     }
  275.  
  276.     private void TransferHeldItemsToStorage()
  277.     {
  278.         if (inventorySystem.leftHandItem != null && inventorySystem.leftHandItem != currentStorageItem)
  279.         {
  280.             bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.leftHandItem);
  281.             if (added)
  282.             {
  283.                 inventorySystem.UnequipItem(true);
  284.                 playerMovement.UpdateCarryingAnimations();
  285.             }
  286.         }
  287.  
  288.         if (inventorySystem.rightHandItem != null && inventorySystem.rightHandItem != currentStorageItem)
  289.         {
  290.             bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.rightHandItem);
  291.             if (added)
  292.             {
  293.                 inventorySystem.UnequipItem(false);
  294.                 playerMovement.UpdateCarryingAnimations();
  295.             }
  296.         }
  297.  
  298.         // Only add a blanket if not already present
  299.         if (currentStorageItem.isBackpack && blanketPrefab != null)
  300.         {
  301.             var storageItems = storageSystem.GetStorageInventory(currentStorageItem);
  302.             Item existingBlanket = storageItems.Find(item => item.isBlanket);
  303.             if (existingBlanket == null)
  304.             {
  305.                 Item blanketItem = Instantiate(blanketPrefab).GetComponent<Item>();
  306.                 storageSystem.AddToStorage(currentStorageItem, blanketItem);
  307.             }
  308.         }
  309.     }
  310.  
  311.     public void PopulateInventoryUI(Item storageItem)
  312.     {
  313.         UIHelperFunctions.Instance.PopulateInventoryUI(storageItem, inventorySlotParent, storageSystem, this);
  314.  
  315.         if (!storageItem.isBackpack) return;
  316.  
  317.         foreach (var storedItem in storageSystem.GetStorageInventory(storageItem))
  318.         {
  319.             if (storedItem.isBackpack && blanketPrefab != null)
  320.             {
  321.                 Item blanketItem = Instantiate(blanketPrefab).GetComponent<Item>();
  322.                 storageSystem.AddToStorage(storedItem, blanketItem);
  323.             }
  324.         }
  325.     }
  326.  
  327.     public void UpdateInventorySlot(GameObject slotObject, Item item)
  328.     {
  329.         UIHelperFunctions.Instance.UpdateInventorySlot(slotObject, item, itemIconPrefab, this);
  330.     }
  331.  
  332.     public void ShowItemDetails(Item item)
  333.     {
  334.         UIHelperFunctions.Instance.ShowItemDetails(item, itemDetailsPanel, itemTitleText, itemDescriptionText, itemUsesText);
  335.     }
  336.  
  337.     public void HideItemDetails()
  338.     {
  339.         UIHelperFunctions.Instance.HideItemDetails(itemDetailsPanel);
  340.     }
  341.  
  342.     public void TryEquipItem(Item item)
  343.     {
  344.         UIHelperFunctions.Instance.TryEquipItem(item, inventorySystem, storageSystem, currentStorageItem, interactionSystem, playerMovement, this);
  345.     }
  346.  
  347.     public void BeginDrag(Transform iconTransform)
  348.     {
  349.         UIHelperFunctions.Instance.BeginDrag(iconTransform, ref draggedItem, ref originalSlot, inventoryPanel);
  350.     }
  351.  
  352.     public void EndDrag(Transform iconTransform)
  353.     {
  354.         UIHelperFunctions.Instance.EndDrag(iconTransform, draggedItem, originalSlot, inventorySlotParent, currentStorageItem, storageSystem, this);
  355.         draggedItem = null;
  356.     }
  357.  
  358.     public void CloseInventory()
  359.     {
  360.         inventoryPanel.SetActive(false);
  361.  
  362.         if (currentStorageItem != null && currentStorageItem.isBackpack)
  363.         {
  364.             var storageItems = storageSystem.GetStorageInventory(currentStorageItem);
  365.             Item blanket = storageItems.Find(item => item.isBlanket);
  366.             if (blanket != null)
  367.             {
  368.                 storageSystem.RemoveItemFromStorage(currentStorageItem, blanket);
  369.                 Destroy(blanket.gameObject);
  370.             }
  371.         }
  372.  
  373.         isInventoryOpen = false;
  374.         TogglePlayerControls(true);
  375.     }
  376.  
  377.     public void ClosePrompt()
  378.     {
  379.         inventoryPrompt.SetActive(false);
  380.  
  381.         // If we're still in precision drop mode, restore gameplay controls (lock mouse, enable movement)
  382.         if (interactionSystem != null && interactionSystem.IsPrecisionDropEnabled())
  383.         {
  384.             SetCursorState(false);
  385.             FreezePlayerMovement(false);  
  386.         }
  387.         else
  388.         {
  389.             SetCursorState(false);
  390.             FreezePlayerMovement(false);
  391.         }
  392.     }
  393.  
  394.     private void TogglePlayerControls(bool enable)
  395.     {
  396.         if (playerMovement != null) playerMovement.enabled = enable;
  397.         if (mouseMovement != null) mouseMovement.enabled = enable;
  398.         SetCursorState(!enable);
  399.     }
  400.  
  401.     private void SetCursorState(bool visible)
  402.     {
  403.         Cursor.visible = visible;
  404.         Cursor.lockState = visible ? CursorLockMode.None : CursorLockMode.Locked;
  405.     }
  406.  
  407.     private void FreezePlayerMovement(bool freeze)
  408.     {
  409.         if (playerMovement != null) playerMovement.enabled = !freeze;
  410.         if (mouseMovement != null) mouseMovement.enabled = !freeze;
  411.     }
  412. }
  413.  
  414. public class ItemReference : MonoBehaviour
  415. {
  416.     public Item item;
  417. }
  418.  
Advertisement
Add Comment
Please, Sign In to add comment