Advertisement
evelynshilosky

UIManager - Part 3 & 4

Feb 7th, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.89 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. using System.Collections.Generic;
  5. using TMPro;
  6.  
  7. public class UIManager : MonoBehaviour
  8. {
  9.     public static UIManager Instance { get; private set; }
  10.  
  11.     public GameObject inventoryPanel;
  12.     public GameObject inventoryPrompt;
  13.     public Text promptText;
  14.     public Button yesButton;
  15.     public Button noButton;
  16.     public Button okButton;
  17.     public Button closeButton;
  18.     public Button inventoryDoneButton;
  19.     public Transform inventorySlotParent;
  20.     public Transform storageHolder;
  21.     public GameObject moneyDisplay;
  22.     public GameObject doneButton;
  23.     public GameObject itemDetailsPanel;
  24.     public TextMeshProUGUI itemTitleText;
  25.     public TextMeshProUGUI itemDescriptionText;
  26.     public TextMeshProUGUI itemUsesText;
  27.  
  28.     [SerializeField] private GameObject itemIconPrefab;
  29.  
  30.     private InventorySystem inventorySystem;
  31.     private StorageSystem storageSystem;
  32.     private InteractionSystem interactionSystem;
  33.     private PlayerMovement playerMovement;
  34.     private Item currentStorageItem;
  35.     private Item draggedItem;
  36.     private Transform originalSlot;
  37.  
  38.     public bool isInventoryOpen = false;
  39.  
  40.     private void Awake()
  41.     {
  42.         if (Instance == null)
  43.         {
  44.             Instance = this;
  45.         }
  46.         else
  47.         {
  48.             Destroy(gameObject);
  49.         }
  50.     }
  51.  
  52.     private void Start()
  53.     {
  54.         inventorySystem = InventorySystem.Instance;
  55.         storageSystem = StorageSystem.Instance;
  56.         interactionSystem = FindObjectOfType<InteractionSystem>();
  57.         playerMovement = FindObjectOfType<PlayerMovement>();
  58.  
  59.         if (closeButton != null) closeButton.onClick.AddListener(CloseInventory);
  60.         if (okButton != null)
  61.         {
  62.             okButton.onClick.AddListener(ResetPrompt);
  63.             okButton.gameObject.SetActive(false);
  64.         }
  65.         if (yesButton != null) yesButton.onClick.AddListener(() => OpenInventory(currentStorageItem));
  66.         if (noButton != null) noButton.onClick.AddListener(ClosePrompt);
  67.         if (inventoryDoneButton != null) inventoryDoneButton.onClick.AddListener(CloseInventory);
  68.     }
  69.  
  70.     public void ShowInventoryPrompt(Item storageItem)
  71.     {
  72.         if (storageItem == null) return;
  73.  
  74.         currentStorageItem = storageItem;
  75.         inventoryPrompt.SetActive(true);
  76.         promptText.text = $"Open {storageItem.itemName}?";
  77.         TogglePlayerControls(false);
  78.     }
  79.  
  80.     public void OpenInventory(Item storageItem)
  81.     {
  82.         inventoryPrompt.SetActive(false);
  83.         inventoryPanel.SetActive(true);
  84.         currentStorageItem = storageItem;
  85.         isInventoryOpen = true;
  86.  
  87.         TransferHeldItemsToStorage();
  88.         PopulateInventoryUI(storageItem);
  89.         TogglePlayerControls(false);
  90.     }
  91.  
  92.     private void TransferHeldItemsToStorage()
  93.     {
  94.         if (inventorySystem.leftHandItem != null)
  95.         {
  96.             bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.leftHandItem);
  97.             if (added)
  98.             {
  99.                 inventorySystem.UnequipItem(true);
  100.                 playerMovement.UpdateCarryingAnimations();
  101.             }
  102.         }
  103.         if (inventorySystem.rightHandItem != null)
  104.         {
  105.             bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.rightHandItem);
  106.             if (added)
  107.             {
  108.                 inventorySystem.UnequipItem(false);
  109.                 playerMovement.UpdateCarryingAnimations();
  110.             }
  111.         }
  112.     }
  113.  
  114.     private void PopulateInventoryUI(Item storageItem)
  115.     {
  116.         ClearInventoryUI();
  117.         List<Item> storageInventory = storageSystem.GetStorageInventory(storageItem);
  118.  
  119.         for (int i = 0; i < inventorySlotParent.childCount - 3; i++)
  120.         {
  121.             Transform slot = inventorySlotParent.GetChild(i);
  122.             if (i < storageInventory.Count)
  123.             {
  124.                 UpdateInventorySlot(slot.gameObject, storageInventory[i]);
  125.             }
  126.             else
  127.             {
  128.                 ClearInventorySlot(slot.gameObject);
  129.             }
  130.         }
  131.     }
  132.  
  133.     private void ClearInventoryUI()
  134.     {
  135.         for (int i = 0; i < inventorySlotParent.childCount - 3; i++)
  136.         {
  137.             Transform slot = inventorySlotParent.GetChild(i);
  138.             ClearInventorySlot(slot.gameObject);
  139.         }
  140.     }
  141.  
  142.     private void UpdateInventorySlot(GameObject slotObject, Item item)
  143.     {
  144.         ClearInventorySlot(slotObject);
  145.  
  146.         GameObject iconObject = Instantiate(itemIconPrefab, slotObject.transform);
  147.         Image iconImage = iconObject.GetComponent<Image>();
  148.         if (iconImage != null)
  149.         {
  150.             iconImage.sprite = item.icon;
  151.             iconImage.enabled = true;
  152.         }
  153.  
  154.         iconObject.AddComponent<ItemReference>().item = item;
  155.  
  156.         EventTrigger trigger = iconObject.GetComponent<EventTrigger>();
  157.         if (trigger == null) trigger = iconObject.AddComponent<EventTrigger>();
  158.  
  159.         trigger.triggers.Clear();
  160.  
  161.         EventTrigger.Entry rightClickEntry = new EventTrigger.Entry();
  162.         rightClickEntry.eventID = EventTriggerType.PointerClick;
  163.         rightClickEntry.callback.AddListener((data) => {
  164.             if (((PointerEventData)data).button == PointerEventData.InputButton.Right)
  165.                 TryEquipItem(item);
  166.         });
  167.  
  168.         EventTrigger.Entry dragEntry = new EventTrigger.Entry();
  169.         dragEntry.eventID = EventTriggerType.BeginDrag;
  170.         dragEntry.callback.AddListener((data) => BeginDrag(iconObject.transform));
  171.  
  172.         EventTrigger.Entry dragEndEntry = new EventTrigger.Entry();
  173.         dragEndEntry.eventID = EventTriggerType.EndDrag;
  174.         dragEndEntry.callback.AddListener((data) => EndDrag(iconObject.transform));
  175.  
  176.         EventTrigger.Entry pointerEnterEntry = new EventTrigger.Entry();
  177.         pointerEnterEntry.eventID = EventTriggerType.PointerEnter;
  178.         pointerEnterEntry.callback.AddListener((data) => ShowItemDetails(item));
  179.  
  180.         EventTrigger.Entry pointerExitEntry = new EventTrigger.Entry();
  181.         pointerExitEntry.eventID = EventTriggerType.PointerExit;
  182.         pointerExitEntry.callback.AddListener((data) => HideItemDetails());
  183.  
  184.         trigger.triggers.Add(rightClickEntry);
  185.         trigger.triggers.Add(dragEntry);
  186.         trigger.triggers.Add(dragEndEntry);
  187.         trigger.triggers.Add(pointerEnterEntry);
  188.         trigger.triggers.Add(pointerExitEntry);
  189.     }
  190.  
  191.     private void ClearInventorySlot(GameObject slotObject)
  192.     {
  193.         foreach (Transform child in slotObject.transform)
  194.         {
  195.             Destroy(child.gameObject);
  196.         }
  197.     }
  198.  
  199.     private void ShowItemDetails(Item item)
  200.     {
  201.         if (item != null && itemDetailsPanel != null)
  202.         {
  203.             itemDetailsPanel.SetActive(true);
  204.             if (itemTitleText != null) itemTitleText.text = item.itemName;
  205.             if (itemDescriptionText != null) itemDescriptionText.text = item.description;
  206.             if (itemUsesText != null) itemUsesText.text = item.itemUses;
  207.         }
  208.     }
  209.  
  210.     private void HideItemDetails()
  211.     {
  212.         if (itemDetailsPanel != null)
  213.         {
  214.             itemDetailsPanel.SetActive(false);
  215.         }
  216.     }
  217.  
  218.     private void TryEquipItem(Item item)
  219.     {
  220.         if (inventorySystem.leftHandItem != null && inventorySystem.rightHandItem != null)
  221.         {
  222.             ShowError("Hands are full!");
  223.             return;
  224.         }
  225.  
  226.         storageSystem.RemoveItemFromStorage(currentStorageItem, item);
  227.         bool isLeftHand = inventorySystem.leftHandItem == null;
  228.         inventorySystem.EquipItem(item, isLeftHand, item.isTwoHanded);
  229.         interactionSystem.UpdateItemPosition(item.gameObject, isLeftHand);
  230.         playerMovement.UpdateCarryingAnimations();
  231.         PopulateInventoryUI(currentStorageItem);
  232.         HideItemDetails();
  233.     }
  234.  
  235.     private void BeginDrag(Transform iconTransform)
  236.     {
  237.         ItemReference itemRef = iconTransform.GetComponent<ItemReference>();
  238.         if (itemRef != null && itemRef.item != null)
  239.         {
  240.             draggedItem = itemRef.item;
  241.             originalSlot = iconTransform.parent;
  242.             iconTransform.SetParent(inventoryPanel.transform);
  243.         }
  244.     }
  245.  
  246.     private void EndDrag(Transform iconTransform)
  247.     {
  248.         if (draggedItem == null) return;
  249.  
  250.         Transform closestSlot = FindClosestSlot();
  251.  
  252.         if (closestSlot != null)
  253.         {
  254.             SwapItems(closestSlot, iconTransform);
  255.         }
  256.         else
  257.         {
  258.             ReturnItemToOriginalSlot(iconTransform);
  259.         }
  260.  
  261.         draggedItem = null;
  262.     }
  263.  
  264.     private Transform FindClosestSlot()
  265.     {
  266.         Transform closestSlot = null;
  267.         float minDistance = Mathf.Infinity;
  268.  
  269.         foreach (Transform slot in inventorySlotParent)
  270.         {
  271.             float dist = Vector3.Distance(slot.position, Input.mousePosition);
  272.             if (dist < minDistance)
  273.             {
  274.                 minDistance = dist;
  275.                 closestSlot = slot;
  276.             }
  277.         }
  278.  
  279.         return closestSlot;
  280.     }
  281.  
  282.     private void SwapItems(Transform newSlot, Transform draggedIcon)
  283.     {
  284.         if (newSlot.childCount > 0)
  285.         {
  286.             Transform existingIcon = newSlot.GetChild(0);
  287.             existingIcon.SetParent(originalSlot);
  288.             existingIcon.localPosition = Vector3.zero;
  289.         }
  290.  
  291.         draggedIcon.SetParent(newSlot);
  292.         draggedIcon.localPosition = Vector3.zero;
  293.     }
  294.  
  295.     private void ReturnItemToOriginalSlot(Transform draggedIcon)
  296.     {
  297.         draggedIcon.SetParent(originalSlot);
  298.         draggedIcon.localPosition = Vector3.zero;
  299.     }
  300.  
  301.     public void ShowError(string message)
  302.     {
  303.         promptText.text = message;
  304.         yesButton.gameObject.SetActive(false);
  305.         noButton.gameObject.SetActive(false);
  306.         okButton.gameObject.SetActive(true);
  307.     }
  308.  
  309.     private void ResetPrompt()
  310.     {
  311.         promptText.text = $"Open {currentStorageItem.itemName}?";
  312.         yesButton.gameObject.SetActive(true);
  313.         noButton.gameObject.SetActive(true);
  314.         okButton.gameObject.SetActive(false);
  315.         inventoryPrompt.SetActive(false);
  316.         TogglePlayerControls(true);
  317.     }
  318.  
  319.     private void ClosePrompt()
  320.     {
  321.         inventoryPrompt.SetActive(false);
  322.         TogglePlayerControls(true);
  323.     }
  324.  
  325.     public void CloseInventory()
  326.     {
  327.         inventoryPanel.SetActive(false);
  328.         isInventoryOpen = false;
  329.         TogglePlayerControls(true);
  330.     }
  331.  
  332.     private void TogglePlayerControls(bool enable)
  333.     {
  334.         if (playerMovement != null)
  335.         {
  336.             playerMovement.enabled = enable;
  337.             playerMovement.GetComponent<MouseMovement>().enabled = enable;
  338.         }
  339.  
  340.         Cursor.lockState = enable ? CursorLockMode.Locked : CursorLockMode.None;
  341.         Cursor.visible = !enable;
  342.     }
  343. }
  344.  
  345. public class ItemReference : MonoBehaviour
  346. {
  347.     public Item item;
  348. }
  349.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement