ItemContainer.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SoftKitty.InventoryEngine
  6. {
  7. public class ItemContainer : ContainerBase
  8. {
  9. #region Variables
  10. public bool DynamicSizeByItemCount = false;
  11. public ListItem TabPrefab;
  12. public Text TotalNumText;
  13. public InputField SearchInput;
  14. public GameObject ClearFilterButton;
  15. public GameObject FavActiveIcon;
  16. private ListItem[] TabItems;
  17. private int selectedTab;
  18. private bool filterFav = false;
  19. private bool wasDragging = false;
  20. #endregion
  21. #region MonoBehaviour
  22. private void Start()
  23. {
  24. FavActiveIcon.GetComponent<Image>().color = InventorySkin.instance.FavoriteColor;
  25. }
  26. public override void Update()
  27. {
  28. if (!inited) return;
  29. base.Update();
  30. CheckDragging();
  31. }
  32. #endregion
  33. public override void Initialize(InventoryHolder _inventoryHolder, InventoryHolder _equipHolder, string _name = "Inventory")//Initialize this interface
  34. {
  35. base.Initialize(_inventoryHolder, _equipHolder, _name);
  36. UpdateTotalNumber();
  37. List<ListItem> TabList = new List<ListItem>();
  38. selectedTab = 0;
  39. for (int i = 0; i < ItemManager.instance.itemTypes.Count + 1; i++)
  40. {
  41. if (i == 0 || ItemManager.instance.itemTypes[i - 1].visible)
  42. {
  43. GameObject _newItem = Instantiate(TabPrefab.gameObject, TabPrefab.transform.parent);
  44. _newItem.transform.localScale = Vector3.one;
  45. _newItem.SetActive(true);
  46. _newItem.GetComponent<ListItem>().mTexts[0].text = i == 0 ? "All" : (DynamicSizeByItemCount? ItemManager.instance.itemTypes[i - 1].name.Substring(0,1).ToUpper(): ItemManager.instance.itemTypes[i - 1].name);
  47. _newItem.GetComponent<ListItem>().mObjects[0].SetActive(i == selectedTab);
  48. _newItem.GetComponent<ListItem>().mID = TabList.Count;
  49. TabList.Add(_newItem.GetComponent<ListItem>());
  50. }
  51. }
  52. if (DynamicSizeByItemCount)
  53. {
  54. int _w = Mathf.Clamp(Mathf.CeilToInt(Mathf.Sqrt(Holder.InventorySize))-5,0,4);
  55. int _h = Mathf.Clamp(Mathf.CeilToInt(Holder.InventorySize * 1F / (_w+5))-3,0,5);
  56. GetComponent<RectTransform>().sizeDelta = new Vector2(390F+67F* _w,268F+67F* _h);
  57. }
  58. float _width = (GetComponent<RectTransform>().sizeDelta.x - 42F - 90F) / (TabList.Count - 1);
  59. for (int i = 0; i < TabList.Count; i++)
  60. {
  61. TabList[i].GetComponent<RectTransform>().sizeDelta = new Vector2(i == 0 ? 90F : _width, 44F);
  62. }
  63. TabItems = TabList.ToArray();
  64. inited = true;
  65. }
  66. public override void OnItemClick(int _index, int _button)//Callback for when player click an item
  67. {
  68. if (Items[_index].GetStackHolder() != null && !Items[_index].isEmpty())
  69. {
  70. foreach (var setting in ItemManager.instance.clickSettings)
  71. {
  72. if (_button == (int)setting.mouseButton && isKeyMatch(setting.key))
  73. {
  74. if (setting.function == ClickFunctions.Use)
  75. {
  76. Items[_index].GetStackHolder().UseItem(Items[_index].GetItemId(), 1, _index);
  77. Items[_index].Click();
  78. }
  79. else if (setting.function == ClickFunctions.Split)
  80. {
  81. Items[_index].Split();
  82. }
  83. else if (setting.function == ClickFunctions.Drop)
  84. {
  85. Items[_index].Drop();
  86. }
  87. else if (setting.function == ClickFunctions.MarkFavorite)
  88. {
  89. Items[_index].MarkFav();
  90. }
  91. }
  92. }
  93. }
  94. }
  95. private bool isKeyMatch(AlterKeys _key) {
  96. switch (_key)
  97. {
  98. case AlterKeys.None:
  99. return true;
  100. case AlterKeys.LeftCtrl:
  101. return InputProxy.GetKey(KeyCode.LeftControl);
  102. case AlterKeys.LeftAlt:
  103. return InputProxy.GetKey(KeyCode.LeftAlt);
  104. case AlterKeys.LeftShift:
  105. return InputProxy.GetKey(KeyCode.LeftShift);
  106. }
  107. return false;
  108. }
  109. private void CheckDragging()//Check if there is any item being dragged
  110. {
  111. if (ItemDragManager.isDragging)
  112. {
  113. wasDragging = true;
  114. }
  115. else
  116. {
  117. if (wasDragging) FilterItems();
  118. wasDragging = false;
  119. }
  120. }
  121. public void SwitchTab(int _id)//Switch catergory and filter items with this category
  122. {
  123. selectedTab = _id;
  124. for (int i = 0; i < TabItems.Length; i++)
  125. {
  126. TabItems[i].mObjects[0].SetActive(i == selectedTab);
  127. }
  128. FilterItems();
  129. SoundManager.Play2D("Tab");
  130. }
  131. public void FilterItems()//Filter items by different conditions
  132. {
  133. for (int i = 0; i < Items.Length; i++)
  134. {
  135. Items[i].SetVisible(Items[i].isNameMatch(SearchInput.text) && Items[i].isTypeMatch(selectedTab) && (!filterFav || Items[i].Fav.activeSelf));
  136. }
  137. ClearFilterButton.SetActive(SearchInput.text != "");
  138. }
  139. public void ClearFilter()//Clear all filters.
  140. {
  141. SearchInput.text = "";
  142. ClearFilterButton.SetActive(false);
  143. filterFav = false;
  144. for (int i = 0; i < Items.Length; i++)
  145. {
  146. Items[i].SetVisible(true);
  147. }
  148. }
  149. public void UpdateTotalNumber()//Update the total number of items inside this container.
  150. {
  151. int _num = 0;
  152. for (int i = 0; i < Holder.Stacks.Count; i++)
  153. {
  154. if (!Holder.Stacks[i].isEmpty()) _num++;
  155. }
  156. TotalNumText.text = " " + _num + "/" + Holder.InventorySize;
  157. }
  158. public void Sort()//Sort all items by their quality and category
  159. {
  160. SoundManager.Play2D("Sort");
  161. Holder.Stacks.Sort(SortByTypeAndQuality);
  162. foreach (InventoryItem obj in Items)
  163. {
  164. Destroy(obj.gameObject);
  165. }
  166. ShowList();
  167. FilterItems();
  168. Holder.ItemChanged(new Dictionary<Item, int>());
  169. }
  170. private static int SortByTypeAndQuality(InventoryStack a, InventoryStack b)//Sort compare method
  171. {
  172. int _scoreA = a.isEmpty() ? 900000 : 0;
  173. if (!a.isEmpty())
  174. {
  175. if (a.Item.favorite) _scoreA -= 900000;
  176. _scoreA += a.Item.type * 30000 + a.Item.quality * 3000 + a.Item.uid;
  177. }
  178. int _scoreB = b.isEmpty() ? 900000 : 0;
  179. if (!b.isEmpty())
  180. {
  181. if (b.Item.favorite) _scoreB -= 900000;
  182. _scoreB += b.Item.type * 30000 + b.Item.quality * 3000 + b.Item.uid;
  183. }
  184. return _scoreA.CompareTo(_scoreB);
  185. }
  186. public void ShowFavItems()//Toggle to filter the favorite items.
  187. {
  188. SoundManager.Play2D("Tab");
  189. filterFav = !filterFav;
  190. FavActiveIcon.SetActive(filterFav);
  191. FilterItems();
  192. }
  193. }
  194. }