| | |
| | | using Assets.Scripts.Enums; |
| | | using System; |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | |
| | | public GameObject inventoryPanel; |
| | | public GameObject itemContent; |
| | | public GameObject itemUIPrefab; |
| | | public GameObject itemNameDescription; |
| | | public GameObject itemDescription; |
| | | |
| | | public List<GameObject> itemUIs = new List<GameObject>(); |
| | | |
| | |
| | | inventoryPanel.SetActive(!inventoryPanel.activeSelf); |
| | | if (inventoryPanel.activeSelf) |
| | | { |
| | | GenerateItemContentList(); |
| | | UpdateItemContentList(); |
| | | } |
| | | else |
| | | { |
| | | ClearItemContentList(); |
| | | //ClearItemContentList(); |
| | | } |
| | | } |
| | | } |
| | |
| | | // GenerateItemContentList(); |
| | | //} |
| | | |
| | | private void GenerateItemContentList() |
| | | //TODO: Delete this |
| | | //private void GenerateItemContentList() |
| | | //{ |
| | | // inventory.items?.ForEach(slot => |
| | | // { |
| | | // GameObject itemUI = Instantiate(itemUIPrefab, itemContent.transform); |
| | | // itemUI.transform.Find("ItemName").GetComponent<TextMeshProUGUI>().text = slot.item.itemName; |
| | | // itemUI.transform.Find("ItemQuantity").GetComponent<TextMeshProUGUI>().text = slot.quantity.ToString() + "x"; |
| | | // itemUI.transform.Find("ItemIcon").GetComponent<Image>().sprite = slot.item.itemIcon; |
| | | // itemUIs.Add(itemUI); |
| | | // }); |
| | | //} |
| | | |
| | | public void OnItemClick(InventorySlot inventorySlot) |
| | | { |
| | | inventory.items?.ForEach(slot => |
| | | //// If itemNameDescription doesn't exist or doesn't have required component, create it |
| | | //if (itemNameDescription == null || itemNameDescription.GetComponent<ItemUIScript>() == 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<ItemUIScript>(); |
| | | // 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<TextMeshProUGUI>(); |
| | | // //var descriptionText = itemNameDescription.transform.Find("Description")?.GetComponent<TextMeshProUGUI>(); |
| | | // itemNameDescription.GetComponent<ItemUIScript>().InventorySlot = item.InventorySlot; |
| | | // //if (nameText != null) nameText.text = item.InventorySlot.item.itemName; |
| | | // //if (descriptionText != null) descriptionText.text = item.InventorySlot.item.itemDescription; |
| | | //} |
| | | if (itemNameDescription.GetComponent<ItemUIScript>()?.InventorySlot?.item?.itemName == inventorySlot.item.itemName) |
| | | { |
| | | GameObject itemUI = Instantiate(itemUIPrefab, itemContent.transform); |
| | | itemUI.transform.Find("ItemName").GetComponent<TextMeshProUGUI>().text = slot.item.itemName; |
| | | itemUI.transform.Find("ItemQuantity").GetComponent<TextMeshProUGUI>().text = slot.quantity.ToString() + "x"; |
| | | itemUI.transform.Find("ItemIcon").GetComponent<Image>().sprite = slot.item.itemIcon; |
| | | itemUIs.Add(itemUI); |
| | | }); |
| | | itemDescription.SetActive(!itemNameDescription.activeSelf); |
| | | itemNameDescription.SetActive(!itemNameDescription.activeSelf); |
| | | } |
| | | else { |
| | | itemDescription.SetActive(true); |
| | | itemNameDescription.SetActive(true); |
| | | } |
| | | itemNameDescription.GetComponent<ItemUIScript>().InventorySlot = inventorySlot; |
| | | itemDescription.GetComponent<ItemUIScript>().InventorySlot = inventorySlot; |
| | | } |
| | | |
| | | private void UpdateItemContentList() |
| | |
| | | inventory.items.ForEach(slot => |
| | | { |
| | | GameObject itemUI; |
| | | |
| | | bool isCreated = false; |
| | | // Reuse existing UI element if available |
| | | if (currentIndex < itemUIs.Count) |
| | | { |
| | | itemUI = itemUIs[currentIndex]; |
| | | //itemUI.GetComponent<ItemUIScript>().InventorySlot = slot; |
| | | itemUI.SetActive(true); |
| | | } |
| | | else |
| | | { |
| | | // Create new UI element if needed |
| | | itemUI = Instantiate(itemUIPrefab, itemContent.transform); |
| | | //itemUI.GetComponent<ItemUIScript>().InventorySlot = slot; |
| | | itemUIs.Add(itemUI); |
| | | isCreated = true; |
| | | } |
| | | ItemUIScript itemUIScript = itemUI.GetComponent<ItemUIScript>(); |
| | | itemUIScript.InventorySlot = slot; |
| | | |
| | | // Add click event listener only if the itemUI was just created |
| | | if (isCreated) |
| | | itemUI.GetComponent<Button>().onClick.AddListener(() => OnItemClick(itemUIScript.InventorySlot)); |
| | | else |
| | | { |
| | | // Update the itemUIScript of the itemNameDescription panel if the item is already selected |
| | | if (itemNameDescription.GetComponent<ItemUIScript>()?.InventorySlot?.item?.itemName == slot.item.itemName) |
| | | itemNameDescription.GetComponent<ItemUIScript>().InventorySlot = slot; |
| | | } |
| | | |
| | | // Update the UI elements |
| | | itemUI.transform.Find("ItemName").GetComponent<TextMeshProUGUI>().text = slot.item.itemName; |
| | | itemUI.transform.Find("ItemQuantity").GetComponent<TextMeshProUGUI>().text = slot.quantity.ToString() + "x"; |
| | | itemUI.transform.Find("ItemIcon").GetComponent<Image>().sprite = slot.item.itemIcon; |
| | | //itemUI.transform.Find("ItemName").GetComponent<TextMeshProUGUI>().text = slot.item.itemName; |
| | | //itemUI.transform.Find("ItemQuantity").GetComponent<TextMeshProUGUI>().text = slot.quantity.ToString() + "x"; |
| | | //itemUI.transform.Find("ItemIcon").GetComponent<Image>().sprite = slot.item.itemIcon; |
| | | |
| | | currentIndex++; |
| | | }); |