using Assets.Scripts.Enums; using Assets.Scripts.Helpers; using System; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.UI; public class InventoryDisplay : MonoBehaviour { private GameObject _playerGO; private Inventory inventory; public GameObject inventoryPanel; public GameObject itemContent; public GameObject itemUIPrefab; public GameObject itemNameDescription; public GameObject itemDescription; public GameObject itemDeleteButton; public GameObject inventoryMaxAmount; public List itemUIs = new List(); public GameObject PlayerGO { get { if (_playerGO == null) { _playerGO = GameObject.Find("Player"); } return _playerGO; } private set { _playerGO = value; } } private void Awake() { PlayerGO = GameObject.Find("Player"); inventory = _playerGO.GetComponent(); //originalRect = healthImage.rectTransform.rect; } private void OnEnable() { GameStateEvents.inventoryChanged += UpdateItemContentList; } private void OnDisable() { GameStateEvents.inventoryChanged -= UpdateItemContentList; } public void OnInventoryButtonPressed(InputAction.CallbackContext context) { //TODO: Delete this //if (context.started) //{ // ResumeOrPauseGame(GameManager.GameIsPaused); //} if (context.started) { ToggleInventoryPanel(); } } public void ToggleInventoryPanel() { inventoryPanel.SetActive(!inventoryPanel.activeSelf); if (inventoryPanel.activeSelf) { UpdateItemContentList(); } else { //ClearItemContentList(); } } //TODO: Delete this //private void ResumeOrPauseGame(bool resume) //{ // if (resume) // { // ResumeWithMenu(); // } // else // { // PauseWithMenu(); // } //} //TODO: Delete this //private void ResumeWithMenu() //{ // GameManager.ResumeGame(); // inventoryPanel.SetActive(false); // ClearItemContentList(); //} //TODO: Delete this //private void PauseWithMenu() //{ // GameManager.PauseGame(); // inventoryPanel.SetActive(true); // GenerateItemContentList(); //} //TODO: Delete this //private void GenerateItemContentList() //{ // inventory.items?.ForEach(slot => // { // GameObject itemUI = Instantiate(itemUIPrefab, itemContent.transform); // itemUI.transform.Find("ItemName").GetComponent().text = slot.item.itemName; // itemUI.transform.Find("ItemQuantity").GetComponent().text = slot.quantity.ToString() + "x"; // itemUI.transform.Find("ItemIcon").GetComponent().sprite = slot.item.itemIcon; // itemUIs.Add(itemUI); // }); //} public void OnItemClick(InventorySlot inventorySlot) { //// If itemNameDescription doesn't exist or doesn't have required component, create it //if (itemNameDescription == null || itemNameDescription.GetComponent() == null) //{ // Debug.LogError("ItemNameDescription is not properly set up with ItemUIScript component"); // return; //} //// Toggle the description panel //itemNameDescription.SetActive(!itemNameDescription.activeSelf); //if (itemNameDescription.activeSelf) //{ // var itemUIScript = itemNameDescription.GetComponent(); // if (itemUIScript != null) // { // itemUIScript.InventorySlot = item.InventorySlot; // } // else // { // Debug.LogError("ItemUIScript component not found on itemNameDescription"); // } //} //// Toggle the description panel //itemNameDescription.SetActive(!itemNameDescription.activeSelf); //if (itemNameDescription.activeSelf) //{ // // Update description panel content // var nameText = itemNameDescription.transform.Find("ItemName")?.GetComponent(); // //var descriptionText = itemNameDescription.transform.Find("Description")?.GetComponent(); // itemNameDescription.GetComponent().InventorySlot = item.InventorySlot; // //if (nameText != null) nameText.text = item.InventorySlot.item.itemName; // //if (descriptionText != null) descriptionText.text = item.InventorySlot.item.itemDescription; //} if (itemNameDescription.GetComponent()?.InventorySlot?.item?.itemName == inventorySlot.item.itemName) { itemDescription.transform.parent.parent.parent.gameObject.SetActive(!itemNameDescription.activeSelf); itemDeleteButton.SetActive(!itemNameDescription.activeSelf); itemNameDescription.SetActive(!itemNameDescription.activeSelf); } else { itemDescription.transform.parent.parent.parent.gameObject.SetActive(true); itemDeleteButton.SetActive(true); itemNameDescription.SetActive(true); } itemNameDescription.GetComponent().InventorySlot = inventorySlot; itemDescription.GetComponent().InventorySlot = inventorySlot; ResetScrollbarForDescription(); } public void OnItemDeleteClick() { InventorySlot inventorySlot = itemNameDescription.GetComponent()?.InventorySlot; if (inventorySlot?.item?.itemName != null) { inventory.RemoveItem(inventorySlot.item, inventorySlot.quantity); itemNameDescription.SetActive(false); itemDeleteButton.SetActive(false); ResetScrollbarForDescription(); itemDescription.transform.parent.parent.parent.gameObject.SetActive(false); } } private void ResetScrollbarForDescription() { //var scrollRect = itemDescription.transform.parent.parent.GetComponent(); //if (scrollRect != null) //{ // // Reset the scroll position // scrollRect.normalizedPosition = Vector2.one; // (1,1) = top, (0,0) = bottom // // Force the scroll view to refresh its layout // LayoutRebuilder.ForceRebuildLayoutImmediate(scrollRect.content); // // Reset the content's position // var content = itemDescription.transform.Find("ItemTextDescription"); // if (content != null) // { // RectTransform rectTransform = content.GetComponent(); // if (rectTransform != null) // { // rectTransform.anchoredPosition = Vector2.zero; // } // } //} // Reset the scroll position var scrollrect = itemDescription.transform.parent.parent.GetComponent(); if (scrollrect != null) { //Debug.Log("Resetting scroll position"); scrollrect.verticalNormalizedPosition = 1; // 0 = top, 1 = bottom } // Reset the content's position // transform.Find("ItemTextDescription") //var content = itemDescription.transform.Find("ItemTextDescription"); //if (content != null) //{ // RectTransform rectTransform = content.GetComponent(); // if (rectTransform != null) // { // rectTransform.anchoredPosition = Vector2.zero; // } //} } private void UpdateItemContentList() { UIHelper.UpdateItemMaxCountText(inventoryMaxAmount.GetComponent(), inventory.items.Count, inventory.maxInventorySize); // First, deactivate all existing itemUIs itemUIs.ForEach(itemUI => itemUI.SetActive(false)); // Keep track of which UI elements we've used int currentIndex = 0; inventory.items.ForEach(slot => { GameObject itemUI; bool isCreated = false; // Reuse existing UI element if available if (currentIndex < itemUIs.Count) { itemUI = itemUIs[currentIndex]; //itemUI.GetComponent().InventorySlot = slot; itemUI.SetActive(true); } else { // Create new UI element if needed itemUI = Instantiate(itemUIPrefab, itemContent.transform); //itemUI.GetComponent().InventorySlot = slot; itemUIs.Add(itemUI); isCreated = true; } ItemUIScript itemUIScript = itemUI.GetComponent(); itemUIScript.InventorySlot = slot; // Add click event listener only if the itemUI was just created if (isCreated) { itemUI.GetComponent