evelynshilosky

UIHelperFunctions - Part 6.1

Mar 27th, 2025
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.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 UIHelperFunctions : MonoBehaviour
  8. {
  9.     public static UIHelperFunctions Instance { get; private set; }
  10.  
  11.     private void Awake()
  12.     {
  13.         if (Instance == null)
  14.         {
  15.             Instance = this;
  16.             DontDestroyOnLoad(gameObject);
  17.         }
  18.         else
  19.         {
  20.             Destroy(gameObject);
  21.         }
  22.     }
  23.  
  24.     public void TransferHeldItemsToStorage(InventorySystem inventorySystem, StorageSystem storageSystem, Item currentStorageItem, PlayerMovement playerMovement)
  25.     {
  26.         if (inventorySystem.leftHandItem != null)
  27.         {
  28.             bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.leftHandItem);
  29.             if (added)
  30.             {
  31.                 inventorySystem.UnequipItem(true);
  32.                 playerMovement.UpdateCarryingAnimations();
  33.             }
  34.         }
  35.         if (inventorySystem.rightHandItem != null)
  36.         {
  37.             bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.rightHandItem);
  38.             if (added)
  39.             {
  40.                 inventorySystem.UnequipItem(false);
  41.                 playerMovement.UpdateCarryingAnimations();
  42.             }
  43.         }
  44.     }
  45.  
  46.     public void PopulateInventoryUI(Item storageItem, Transform inventorySlotParent, StorageSystem storageSystem, UIManager uiManager)
  47.     {
  48.         ClearInventoryUI(inventorySlotParent);
  49.         List<Item> storageInventory = storageSystem.GetStorageInventory(storageItem);
  50.  
  51.         for (int i = 0; i < inventorySlotParent.childCount - 3; i++)
  52.         {
  53.             Transform slot = inventorySlotParent.GetChild(i);
  54.             ClearInventorySlot(slot.gameObject);
  55.         }
  56.  
  57.         foreach (Item item in storageInventory)
  58.         {
  59.             int slotIndex = storageSystem.GetItemSlotIndex(storageItem, item);
  60.             if (slotIndex == -1 || slotIndex >= inventorySlotParent.childCount - 3)
  61.             {
  62.                 slotIndex = FindFirstEmptySlot(inventorySlotParent);
  63.             }
  64.  
  65.             if (slotIndex != -1)
  66.             {
  67.                 Transform slot = inventorySlotParent.GetChild(slotIndex);
  68.                 uiManager.UpdateInventorySlot(slot.gameObject, item);
  69.                 storageSystem.UpdateItemPosition(storageItem, item, slotIndex);
  70.             }
  71.         }
  72.     }
  73.  
  74.     public void UpdateInventorySlot(GameObject slotObject, Item item, GameObject itemIconPrefab, UIManager uiManager)
  75.     {
  76.         ClearInventorySlot(slotObject);
  77.  
  78.         GameObject iconObject = Instantiate(itemIconPrefab, slotObject.transform);
  79.         Image iconImage = iconObject.GetComponent<Image>();
  80.         if (iconImage != null)
  81.         {
  82.             iconImage.sprite = item.icon;
  83.             iconImage.enabled = true;
  84.         }
  85.  
  86.         iconObject.AddComponent<ItemReference>().item = item;
  87.  
  88.         EventTrigger trigger = iconObject.GetComponent<EventTrigger>();
  89.         if (trigger == null) trigger = iconObject.AddComponent<EventTrigger>();
  90.  
  91.         trigger.triggers.Clear();
  92.  
  93.         EventTrigger.Entry rightClickEntry = new EventTrigger.Entry();
  94.         rightClickEntry.eventID = EventTriggerType.PointerClick;
  95.         rightClickEntry.callback.AddListener((data) => {
  96.             if (((PointerEventData)data).button == PointerEventData.InputButton.Right)
  97.                 uiManager.TryEquipItem(item);
  98.         });
  99.  
  100.         EventTrigger.Entry dragEntry = new EventTrigger.Entry();
  101.         dragEntry.eventID = EventTriggerType.BeginDrag;
  102.         dragEntry.callback.AddListener((data) => uiManager.BeginDrag(iconObject.transform));
  103.  
  104.         EventTrigger.Entry dragEndEntry = new EventTrigger.Entry();
  105.         dragEndEntry.eventID = EventTriggerType.EndDrag;
  106.         dragEndEntry.callback.AddListener((data) => uiManager.EndDrag(iconObject.transform));
  107.  
  108.         EventTrigger.Entry pointerEnterEntry = new EventTrigger.Entry();
  109.         pointerEnterEntry.eventID = EventTriggerType.PointerEnter;
  110.         pointerEnterEntry.callback.AddListener((data) => uiManager.ShowItemDetails(item));
  111.  
  112.         EventTrigger.Entry pointerExitEntry = new EventTrigger.Entry();
  113.         pointerExitEntry.eventID = EventTriggerType.PointerExit;
  114.         pointerExitEntry.callback.AddListener((data) => uiManager.HideItemDetails());
  115.  
  116.         trigger.triggers.Add(rightClickEntry);
  117.         trigger.triggers.Add(dragEntry);
  118.         trigger.triggers.Add(dragEndEntry);
  119.         trigger.triggers.Add(pointerEnterEntry);
  120.         trigger.triggers.Add(pointerExitEntry);
  121.     }
  122.  
  123.     public void ShowItemDetails(Item item, GameObject itemDetailsPanel, TextMeshProUGUI itemTitleText, TextMeshProUGUI itemDescriptionText, TextMeshProUGUI itemUsesText)
  124.     {
  125.         if (item != null && itemDetailsPanel != null)
  126.         {
  127.             itemDetailsPanel.SetActive(true);
  128.             if (itemTitleText != null) itemTitleText.text = item.itemName;
  129.             if (itemDescriptionText != null) itemDescriptionText.text = item.description;
  130.             if (itemUsesText != null) itemUsesText.text = item.itemUses;
  131.         }
  132.     }
  133.  
  134.     public void HideItemDetails(GameObject itemDetailsPanel)
  135.     {
  136.         if (itemDetailsPanel != null)
  137.         {
  138.             itemDetailsPanel.SetActive(false);
  139.         }
  140.     }
  141.  
  142.     public void TryEquipItem(Item item, InventorySystem inventorySystem, StorageSystem storageSystem, Item currentStorageItem, InteractionSystem interactionSystem, PlayerMovement playerMovement, UIManager uiManager)
  143.     {
  144.         if (inventorySystem.leftHandItem != null && inventorySystem.rightHandItem != null)
  145.         {
  146.             uiManager.ShowError("Hands are full!");
  147.             return;
  148.         }
  149.  
  150.         storageSystem.RemoveItemFromStorage(currentStorageItem, item);
  151.         bool isLeftHand = inventorySystem.leftHandItem == null;
  152.         inventorySystem.EquipItem(item, isLeftHand, item.isTwoHanded);
  153.         interactionSystem.UpdateItemPosition(item.gameObject, isLeftHand);
  154.         playerMovement.UpdateCarryingAnimations();
  155.         uiManager.PopulateInventoryUI(currentStorageItem);
  156.         uiManager.HideItemDetails();
  157.     }
  158.  
  159.     public void BeginDrag(Transform iconTransform, ref Item draggedItem, ref Transform originalSlot, GameObject inventoryPanel)
  160.     {
  161.         ItemReference itemRef = iconTransform.GetComponent<ItemReference>();
  162.         if (itemRef != null && itemRef.item != null)
  163.         {
  164.             draggedItem = itemRef.item;
  165.             originalSlot = iconTransform.parent;
  166.             iconTransform.SetParent(inventoryPanel.transform);
  167.         }
  168.     }
  169.  
  170.     public void EndDrag(Transform iconTransform, Item draggedItem, Transform originalSlot, Transform inventorySlotParent, Item currentStorageItem, StorageSystem storageSystem, UIManager uiManager)
  171.     {
  172.         if (draggedItem == null) return;
  173.  
  174.         Transform closestSlot = FindClosestSlot(inventorySlotParent, Input.mousePosition);
  175.  
  176.         if (closestSlot != null)
  177.         {
  178.             SwapItems(closestSlot, iconTransform, originalSlot, currentStorageItem, storageSystem);
  179.         }
  180.         else
  181.         {
  182.             ReturnItemToOriginalSlot(iconTransform, originalSlot);
  183.         }
  184.     }
  185.  
  186.     private void ClearInventoryUI(Transform inventorySlotParent)
  187.     {
  188.         for (int i = 0; i < inventorySlotParent.childCount - 3; i++)
  189.         {
  190.             Transform slot = inventorySlotParent.GetChild(i);
  191.             ClearInventorySlot(slot.gameObject);
  192.         }
  193.     }
  194.  
  195.     private void ClearInventorySlot(GameObject slotObject)
  196.     {
  197.         foreach (Transform child in slotObject.transform)
  198.         {
  199.             Destroy(child.gameObject);
  200.         }
  201.     }
  202.  
  203.     public int FindFirstEmptySlot(Transform inventorySlotParent)
  204.     {
  205.         for (int i = 0; i < inventorySlotParent.childCount - 3; i++)
  206.         {
  207.             if (inventorySlotParent.GetChild(i).childCount == 0)
  208.             {
  209.                 return i;
  210.             }
  211.         }
  212.         return -1;
  213.     }
  214.  
  215.     private Transform FindClosestSlot(Transform inventorySlotParent, Vector3 position)
  216.     {
  217.         Transform closestSlot = null;
  218.         float minDistance = Mathf.Infinity;
  219.  
  220.         foreach (Transform slot in inventorySlotParent)
  221.         {
  222.             float dist = Vector3.Distance(slot.position, position);
  223.             if (dist < minDistance)
  224.             {
  225.                 minDistance = dist;
  226.                 closestSlot = slot;
  227.             }
  228.         }
  229.  
  230.         return closestSlot;
  231.     }
  232.  
  233.     private void SwapItems(Transform newSlot, Transform draggedIcon, Transform originalSlot, Item currentStorageItem, StorageSystem storageSystem)
  234.     {
  235.         if (newSlot.childCount > 0)
  236.         {
  237.             Transform existingIcon = newSlot.GetChild(0);
  238.             existingIcon.SetParent(originalSlot);
  239.             existingIcon.localPosition = Vector3.zero;
  240.  
  241.             Item existingItem = existingIcon.GetComponent<ItemReference>().item;
  242.             storageSystem.UpdateItemPosition(currentStorageItem, existingItem, originalSlot.GetSiblingIndex());
  243.         }
  244.  
  245.         draggedIcon.SetParent(newSlot);
  246.         draggedIcon.localPosition = Vector3.zero;
  247.  
  248.         Item draggedItem = draggedIcon.GetComponent<ItemReference>().item;
  249.         storageSystem.UpdateItemPosition(currentStorageItem, draggedItem, newSlot.GetSiblingIndex());
  250.     }
  251.  
  252.     private void ReturnItemToOriginalSlot(Transform draggedIcon, Transform originalSlot)
  253.     {
  254.         draggedIcon.SetParent(originalSlot);
  255.         draggedIcon.localPosition = Vector3.zero;
  256.     }
  257.  
  258.     public void TogglePlayerControls(bool enable, PlayerMovement playerMovement)
  259.     {
  260.         if (playerMovement != null)
  261.         {
  262.             playerMovement.enabled = enable;
  263.             playerMovement.GetComponent<MouseMovement>().enabled = enable;
  264.         }
  265.  
  266.         Cursor.lockState = enable ? CursorLockMode.Locked : CursorLockMode.None;
  267.         Cursor.visible = !enable;
  268.     }
  269. }
  270.  
Advertisement
Add Comment
Please, Sign In to add comment