using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using TMPro; using Sirenix.OdinInspector; using KairoEngine.Core; using KairoEngine.UI; namespace KairoEngine.Inventory { public class ItemContainerUi : MonoBehaviour { public string containerTitle; public GameObject itemContainerPanel; public GameObject itemSlotPrefab; public bool generateSlots = true; public bool showPrice = false; public bool showValueText = false; public int itemSlotCount = 16; public Transform slotsContainer; public TextMeshProUGUI windowTitleText; public List itemSlots = new List(); public ItemContainer targetItemContainer; void OnEnable() { ItemContainerEvents.StartListeningOnOpen(containerTitle, this.OnOpenWindow); ItemContainerEvents.StartListeningOnClose(containerTitle, this.OnCloseWindow); ItemContainerEvents.OnUpdate += this.OnUpdateItemContainer; } void OnDisable() { ItemContainerEvents.StopListeningOnOpen(containerTitle, this.OnOpenWindow); ItemContainerEvents.StopListeningOnClose(containerTitle, this.OnCloseWindow); ItemContainerEvents.OnUpdate -= this.OnUpdateItemContainer; } void Start() { InstantiateItemSlots(); } private void InstantiateItemSlots() { if(generateSlots == false) return; for (int i = 0; i < itemSlotCount; i++) { GameObject itemSlotObj = GameObject.Instantiate(itemSlotPrefab, slotsContainer); ItemSlotUi itemSlotUi = itemSlotObj.GetComponentInChildren(); if(itemSlotUi == null) Debug.LogError("ItemSlotPrefab GameObject is missing a ItemSlotUi component"); else { itemSlots.Add(itemSlotUi); } } } private void ResetItemSlots() { for (int i = 0; i < itemSlots.Count; i++) { string value = ""; if(showValueText) value = (i + 1).ToString(); itemSlots[i].Setup(null, targetItemContainer, this, true, showPrice, value); } } public void SetWindowTitle(string title) { if(windowTitleText == null) { return; } windowTitleText.text = title; } private void UpdateItemSlots() { List itemList = targetItemContainer.ItemList; if(itemList == null) { Debug.LogError("Missing itemList in ItemContainerUI"); } for (int i = 0; i < itemList.Count; i++) { ItemSlotUi slot = null; for (int a = 0; a < itemSlots.Count; a++) { if (itemSlots[a].itemRef == null) { slot = itemSlots[a]; break; } } string value = ""; if(showValueText) value = (i + 1).ToString(); if (slot != null) slot.Setup(itemList[i], targetItemContainer, this, true, showPrice, value); //else Debug.LogError("No available ItemSlotUi was found for item " + itemList[i].item.title); } } private void OnOpenWindow(ItemContainer itemContainer) { if(itemContainer == null) { Debug.LogError("EventOpenItemContainer contains no reference to target itemContainer"); return; } targetItemContainer = itemContainer; if(!itemContainerPanel.activeSelf) { ResetItemSlots(); UpdateItemSlots(); SetWindowTitle(itemContainer.title); itemContainerPanel.SetActive(true); UiAnimator[] animators = itemContainerPanel.GetComponentsInChildren(true); for (int i = 0; i < animators.Length; i++) { animators[i].gameObject.SetActive(true); } } } private void OnCloseWindow() { UiAnimator panelAnimator = itemContainerPanel.GetComponent(); UiAnimator[] animators = itemContainerPanel.GetComponentsInChildren(); if(animators.Length > 0 || panelAnimator != null) { panelAnimator.Disable(); foreach (var animator in animators) { animator.Disable(); } } else { itemContainerPanel.SetActive(false); } } private void OnUpdateItemContainer(ItemContainer itemContainer) { if(itemContainer != targetItemContainer) return; if(itemContainer == targetItemContainer) { ResetItemSlots(); UpdateItemSlots(); } } public void CloseWindow() { ICommand closeInventoryCommand = new CloseItemContainerCommand(containerTitle); CommandInvoker.AddCommand(closeInventoryCommand); } } }