123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- 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<ItemSlotUi> itemSlots = new List<ItemSlotUi>();
- 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<ItemSlotUi>();
- 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<ItemRef> 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<UiAnimator>(true);
- for (int i = 0; i < animators.Length; i++)
- {
- animators[i].gameObject.SetActive(true);
- }
- }
- }
- private void OnCloseWindow()
- {
- UiAnimator panelAnimator = itemContainerPanel.GetComponent<UiAnimator>();
- UiAnimator[] animators = itemContainerPanel.GetComponentsInChildren<UiAnimator>();
- 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);
- }
- }
- }
|