evelynshilosky

UIHelperFunctions - Part 6.3.4

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