Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using System.Collections.Generic;
- using TMPro;
- public class UIHelperFunctions : MonoBehaviour
- {
- public static UIHelperFunctions Instance { get; private set; }
- private void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- DontDestroyOnLoad(gameObject);
- }
- else
- {
- Destroy(gameObject);
- }
- }
- public void TransferHeldItemsToStorage(InventorySystem inventorySystem, StorageSystem storageSystem, Item currentStorageItem, PlayerMovement playerMovement)
- {
- if (inventorySystem.leftHandItem != null)
- {
- bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.leftHandItem);
- if (added)
- {
- inventorySystem.UnequipItem(true);
- playerMovement.UpdateCarryingAnimations();
- }
- }
- if (inventorySystem.rightHandItem != null)
- {
- bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.rightHandItem);
- if (added)
- {
- inventorySystem.UnequipItem(false);
- playerMovement.UpdateCarryingAnimations();
- }
- }
- }
- public void PopulateInventoryUI(Item storageItem, Transform inventorySlotParent, StorageSystem storageSystem, UIManager uiManager)
- {
- ClearInventoryUI(inventorySlotParent);
- List<Item> storageInventory = storageSystem.GetStorageInventory(storageItem);
- for (int i = 0; i < storageInventory.Count; i++)
- {
- Transform slot = inventorySlotParent.GetChild(i);
- uiManager.UpdateInventorySlot(slot.gameObject, storageInventory[i]);
- storageSystem.UpdateItemPosition(storageItem, storageInventory[i], i);
- }
- }
- public void UpdateInventorySlot(GameObject slotObject, Item item, GameObject itemIconPrefab, UIManager uiManager)
- {
- ClearInventorySlot(slotObject);
- GameObject iconObject = Instantiate(itemIconPrefab, slotObject.transform);
- Image iconImage = iconObject.GetComponent<Image>();
- if (iconImage != null)
- {
- iconImage.sprite = item.icon;
- iconImage.enabled = true;
- }
- iconObject.AddComponent<ItemReference>().item = item;
- EventTrigger trigger = iconObject.GetComponent<EventTrigger>();
- if (trigger == null) trigger = iconObject.AddComponent<EventTrigger>();
- trigger.triggers.Clear();
- EventTrigger.Entry rightClickEntry = new EventTrigger.Entry();
- rightClickEntry.eventID = EventTriggerType.PointerClick;
- rightClickEntry.callback.AddListener((data) => {
- if (((PointerEventData)data).button == PointerEventData.InputButton.Right)
- uiManager.TryEquipItem(item);
- });
- EventTrigger.Entry dragEntry = new EventTrigger.Entry();
- dragEntry.eventID = EventTriggerType.BeginDrag;
- dragEntry.callback.AddListener((data) => uiManager.BeginDrag(iconObject.transform));
- EventTrigger.Entry dragEndEntry = new EventTrigger.Entry();
- dragEndEntry.eventID = EventTriggerType.EndDrag;
- dragEndEntry.callback.AddListener((data) => uiManager.EndDrag(iconObject.transform));
- EventTrigger.Entry pointerEnterEntry = new EventTrigger.Entry();
- pointerEnterEntry.eventID = EventTriggerType.PointerEnter;
- pointerEnterEntry.callback.AddListener((data) => uiManager.ShowItemDetails(item));
- EventTrigger.Entry pointerExitEntry = new EventTrigger.Entry();
- pointerExitEntry.eventID = EventTriggerType.PointerExit;
- pointerExitEntry.callback.AddListener((data) => uiManager.HideItemDetails());
- trigger.triggers.Add(rightClickEntry);
- trigger.triggers.Add(dragEntry);
- trigger.triggers.Add(dragEndEntry);
- trigger.triggers.Add(pointerEnterEntry);
- trigger.triggers.Add(pointerExitEntry);
- }
- public void ShowItemDetails(Item item, GameObject itemDetailsPanel, TextMeshProUGUI itemTitleText, TextMeshProUGUI itemDescriptionText, TextMeshProUGUI itemUsesText)
- {
- if (item != null && itemDetailsPanel != null)
- {
- itemDetailsPanel.SetActive(true);
- if (itemTitleText != null) itemTitleText.text = item.itemName;
- if (itemDescriptionText != null) itemDescriptionText.text = item.description;
- if (itemUsesText != null) itemUsesText.text = item.itemUses;
- }
- }
- public void HideItemDetails(GameObject itemDetailsPanel)
- {
- if (itemDetailsPanel != null)
- {
- itemDetailsPanel.SetActive(false);
- }
- }
- public void TryEquipItem(Item item, InventorySystem inventorySystem, StorageSystem storageSystem, Item currentStorageItem, InteractionSystem interactionSystem, PlayerMovement playerMovement, UIManager uiManager)
- {
- if (inventorySystem.leftHandItem != null && inventorySystem.rightHandItem != null)
- {
- uiManager.ShowError("Hands are full!");
- return;
- }
- storageSystem.RemoveItemFromStorage(currentStorageItem, item);
- bool isLeftHand = inventorySystem.leftHandItem == null;
- inventorySystem.EquipItem(item, isLeftHand, item.isTwoHanded);
- interactionSystem.UpdateItemPosition(item.gameObject, isLeftHand);
- playerMovement.UpdateCarryingAnimations();
- uiManager.PopulateInventoryUI(currentStorageItem);
- uiManager.HideItemDetails();
- }
- public void BeginDrag(Transform iconTransform, ref Item draggedItem, ref Transform originalSlot, GameObject inventoryPanel)
- {
- ItemReference itemRef = iconTransform.GetComponent<ItemReference>();
- if (itemRef != null && itemRef.item != null)
- {
- draggedItem = itemRef.item;
- originalSlot = iconTransform.parent;
- iconTransform.SetParent(inventoryPanel.transform);
- }
- }
- public void EndDrag(Transform iconTransform, Item draggedItem, Transform originalSlot, Transform inventorySlotParent, Item currentStorageItem, StorageSystem storageSystem, UIManager uiManager)
- {
- if (draggedItem == null) return;
- Transform closestSlot = FindClosestSlot(inventorySlotParent, Input.mousePosition);
- if (closestSlot != null)
- {
- SwapItems(closestSlot, iconTransform, originalSlot, currentStorageItem, storageSystem);
- }
- else
- {
- ReturnItemToOriginalSlot(iconTransform, originalSlot);
- }
- }
- private void ClearInventoryUI(Transform inventorySlotParent)
- {
- for (int i = 0; i < inventorySlotParent.childCount - 3; i++)
- {
- Transform slot = inventorySlotParent.GetChild(i);
- ClearInventorySlot(slot.gameObject);
- }
- }
- private void ClearInventorySlot(GameObject slotObject)
- {
- foreach (Transform child in slotObject.transform)
- {
- Destroy(child.gameObject);
- }
- }
- public int FindFirstEmptySlot(Transform inventorySlotParent)
- {
- for (int i = 0; i < inventorySlotParent.childCount - 3; i++)
- {
- if (inventorySlotParent.GetChild(i).childCount == 0)
- {
- return i;
- }
- }
- return -1;
- }
- private Transform FindClosestSlot(Transform inventorySlotParent, Vector3 position)
- {
- Transform closestSlot = null;
- float minDistance = Mathf.Infinity;
- foreach (Transform slot in inventorySlotParent)
- {
- float dist = Vector3.Distance(slot.position, position);
- if (dist < minDistance)
- {
- minDistance = dist;
- closestSlot = slot;
- }
- }
- return closestSlot;
- }
- private void SwapItems(Transform newSlot, Transform draggedIcon, Transform originalSlot, Item currentStorageItem, StorageSystem storageSystem)
- {
- if (newSlot.childCount > 0)
- {
- Transform existingIcon = newSlot.GetChild(0);
- existingIcon.SetParent(originalSlot);
- existingIcon.localPosition = Vector3.zero;
- Item existingItem = existingIcon.GetComponent<ItemReference>().item;
- storageSystem.UpdateItemPosition(currentStorageItem, existingItem, originalSlot.GetSiblingIndex());
- }
- draggedIcon.SetParent(newSlot);
- draggedIcon.localPosition = Vector3.zero;
- Item draggedItem = draggedIcon.GetComponent<ItemReference>().item;
- storageSystem.UpdateItemPosition(currentStorageItem, draggedItem, newSlot.GetSiblingIndex());
- }
- private void ReturnItemToOriginalSlot(Transform draggedIcon, Transform originalSlot)
- {
- draggedIcon.SetParent(originalSlot);
- draggedIcon.localPosition = Vector3.zero;
- }
- public void TogglePlayerControls(bool enable, PlayerMovement playerMovement)
- {
- if (playerMovement != null)
- {
- playerMovement.enabled = enable;
- playerMovement.GetComponent<MouseMovement>().enabled = enable;
- }
- Cursor.lockState = enable ? CursorLockMode.Locked : CursorLockMode.None;
- Cursor.visible = !enable;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment