ItemContainerUi.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Events;
  6. using TMPro;
  7. using Sirenix.OdinInspector;
  8. using KairoEngine.Core;
  9. using KairoEngine.UI;
  10. namespace KairoEngine.Inventory
  11. {
  12. public class ItemContainerUi : MonoBehaviour
  13. {
  14. public string containerTitle;
  15. public GameObject itemContainerPanel;
  16. public GameObject itemSlotPrefab;
  17. public bool generateSlots = true;
  18. public bool showPrice = false;
  19. public bool showValueText = false;
  20. public int itemSlotCount = 16;
  21. public Transform slotsContainer;
  22. public TextMeshProUGUI windowTitleText;
  23. public List<ItemSlotUi> itemSlots = new List<ItemSlotUi>();
  24. public ItemContainer targetItemContainer;
  25. void OnEnable()
  26. {
  27. ItemContainerEvents.StartListeningOnOpen(containerTitle, this.OnOpenWindow);
  28. ItemContainerEvents.StartListeningOnClose(containerTitle, this.OnCloseWindow);
  29. ItemContainerEvents.OnUpdate += this.OnUpdateItemContainer;
  30. }
  31. void OnDisable()
  32. {
  33. ItemContainerEvents.StopListeningOnOpen(containerTitle, this.OnOpenWindow);
  34. ItemContainerEvents.StopListeningOnClose(containerTitle, this.OnCloseWindow);
  35. ItemContainerEvents.OnUpdate -= this.OnUpdateItemContainer;
  36. }
  37. void Start()
  38. {
  39. InstantiateItemSlots();
  40. }
  41. private void InstantiateItemSlots()
  42. {
  43. if(generateSlots == false) return;
  44. for (int i = 0; i < itemSlotCount; i++)
  45. {
  46. GameObject itemSlotObj = GameObject.Instantiate(itemSlotPrefab, slotsContainer);
  47. ItemSlotUi itemSlotUi = itemSlotObj.GetComponentInChildren<ItemSlotUi>();
  48. if(itemSlotUi == null) Debug.LogError("ItemSlotPrefab GameObject is missing a ItemSlotUi component");
  49. else
  50. {
  51. itemSlots.Add(itemSlotUi);
  52. }
  53. }
  54. }
  55. private void ResetItemSlots()
  56. {
  57. for (int i = 0; i < itemSlots.Count; i++)
  58. {
  59. string value = "";
  60. if(showValueText) value = (i + 1).ToString();
  61. itemSlots[i].Setup(null, targetItemContainer, this, true, showPrice, value);
  62. }
  63. }
  64. public void SetWindowTitle(string title)
  65. {
  66. if(windowTitleText == null)
  67. {
  68. return;
  69. }
  70. windowTitleText.text = title;
  71. }
  72. private void UpdateItemSlots()
  73. {
  74. List<ItemRef> itemList = targetItemContainer.ItemList;
  75. if(itemList == null)
  76. {
  77. Debug.LogError("Missing itemList in ItemContainerUI");
  78. }
  79. for (int i = 0; i < itemList.Count; i++)
  80. {
  81. ItemSlotUi slot = null;
  82. for (int a = 0; a < itemSlots.Count; a++)
  83. {
  84. if (itemSlots[a].itemRef == null)
  85. {
  86. slot = itemSlots[a];
  87. break;
  88. }
  89. }
  90. string value = "";
  91. if(showValueText) value = (i + 1).ToString();
  92. if (slot != null) slot.Setup(itemList[i], targetItemContainer, this, true, showPrice, value);
  93. //else Debug.LogError("No available ItemSlotUi was found for item " + itemList[i].item.title);
  94. }
  95. }
  96. private void OnOpenWindow(ItemContainer itemContainer)
  97. {
  98. if(itemContainer == null)
  99. {
  100. Debug.LogError("EventOpenItemContainer contains no reference to target itemContainer");
  101. return;
  102. }
  103. targetItemContainer = itemContainer;
  104. if(!itemContainerPanel.activeSelf)
  105. {
  106. ResetItemSlots();
  107. UpdateItemSlots();
  108. SetWindowTitle(itemContainer.title);
  109. itemContainerPanel.SetActive(true);
  110. UiAnimator[] animators = itemContainerPanel.GetComponentsInChildren<UiAnimator>(true);
  111. for (int i = 0; i < animators.Length; i++)
  112. {
  113. animators[i].gameObject.SetActive(true);
  114. }
  115. }
  116. }
  117. private void OnCloseWindow()
  118. {
  119. UiAnimator panelAnimator = itemContainerPanel.GetComponent<UiAnimator>();
  120. UiAnimator[] animators = itemContainerPanel.GetComponentsInChildren<UiAnimator>();
  121. if(animators.Length > 0 || panelAnimator != null)
  122. {
  123. panelAnimator.Disable();
  124. foreach (var animator in animators)
  125. {
  126. animator.Disable();
  127. }
  128. }
  129. else
  130. {
  131. itemContainerPanel.SetActive(false);
  132. }
  133. }
  134. private void OnUpdateItemContainer(ItemContainer itemContainer)
  135. {
  136. if(itemContainer != targetItemContainer) return;
  137. if(itemContainer == targetItemContainer)
  138. {
  139. ResetItemSlots();
  140. UpdateItemSlots();
  141. }
  142. }
  143. public void CloseWindow()
  144. {
  145. ICommand closeInventoryCommand = new CloseItemContainerCommand(containerTitle);
  146. CommandInvoker.AddCommand(closeInventoryCommand);
  147. }
  148. }
  149. }