From ee1703b69b7977a8cd6d37dd097f425c8c905882 Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Thu, 03 Apr 2025 22:24:35 +0000
Subject: [PATCH] #42 added jobs for tile generation

---
 Assets/Scripts/Inventory/InventoryDisplay.cs |  181 ++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 160 insertions(+), 21 deletions(-)

diff --git a/Assets/Scripts/Inventory/InventoryDisplay.cs b/Assets/Scripts/Inventory/InventoryDisplay.cs
index 7ca2098..0b2690b 100644
--- a/Assets/Scripts/Inventory/InventoryDisplay.cs
+++ b/Assets/Scripts/Inventory/InventoryDisplay.cs
@@ -1,3 +1,5 @@
+using Assets.Scripts.Enums;
+using Assets.Scripts.Helpers;
 using System;
 using System.Collections;
 using System.Collections.Generic;
@@ -13,6 +15,10 @@
     public GameObject inventoryPanel;
     public GameObject itemContent;
     public GameObject itemUIPrefab;
+    public GameObject itemNameDescription;
+    public GameObject itemDescription;
+    public GameObject itemDeleteButton;
+    public GameObject inventoryMaxAmount;
 
     public List<GameObject> itemUIs = new List<GameObject>();
 
@@ -54,15 +60,20 @@
         //}
         if (context.started)
         {
-            inventoryPanel.SetActive(!inventoryPanel.activeSelf);
-            if (inventoryPanel.activeSelf)
-            {
-                GenerateItemContentList();
-            }
-            else
-            {
-                ClearItemContentList();
-            }
+            ToggleInventoryPanel();
+        }
+    }
+
+    public void ToggleInventoryPanel()
+    {
+        inventoryPanel.SetActive(!inventoryPanel.activeSelf);
+        if (inventoryPanel.activeSelf)
+        {
+            UpdateItemContentList();
+        }
+        else
+        {
+            //ClearItemContentList();
         }
     }
 
@@ -96,20 +107,131 @@
     //    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.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<ItemUIScript>().InventorySlot = inventorySlot;
+        itemDescription.GetComponent<ItemUIScript>().InventorySlot = inventorySlot;
+        ResetScrollbarForDescription();
+    }
+    public void OnItemDeleteClick()
+    {
+        InventorySlot inventorySlot = itemNameDescription.GetComponent<ItemUIScript>()?.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<ScrollRect>();
+        //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<RectTransform>();
+        //        if (rectTransform != null)
+        //        {
+        //            rectTransform.anchoredPosition = Vector2.zero;
+        //        }
+        //    }
+        //}
+        // Reset the scroll position
+        var scrollrect = itemDescription.transform.parent.parent.GetComponent<ScrollRect>();
+        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<RectTransform>();
+        //    if (rectTransform != null)
+        //    {
+        //        rectTransform.anchoredPosition = Vector2.zero;
+        //    }
+        //}
     }
 
     private void UpdateItemContentList()
     {
+        UIHelper.UpdateItemMaxCountText(inventoryMaxAmount.GetComponent<TextMeshProUGUI>(), inventory.items.Count, inventory.maxInventorySize);
+
         // First, deactivate all existing itemUIs
         itemUIs.ForEach(itemUI => itemUI.SetActive(false));
 
@@ -119,24 +241,41 @@
         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++;
         });

--
Gitblit v1.9.3