SkillUi.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SoftKitty.InventoryEngine
  6. {
  7. public class SkillUi : HiddenContainer
  8. {
  9. #region Variables
  10. public string[] TagList;
  11. public ListItem TabPrefab;
  12. public InputField SearchInput;
  13. public GameObject ClearFilterButton;
  14. public GameObject FavActiveIcon;
  15. private ListItem[] TabItems;
  16. private int selectedTab;
  17. private bool filterFav = false;
  18. private bool wasDragging = false;
  19. #endregion
  20. # region MonoBehaviour
  21. void Start()
  22. {
  23. FavActiveIcon.GetComponent<Image>().color = InventorySkin.instance.FavoriteColor;
  24. }
  25. public override void Update()
  26. {
  27. if (!inited) return;
  28. base.Update();
  29. CheckDragging();
  30. FilterItems();
  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. List<ListItem> TabList = new List<ListItem>();
  37. selectedTab = 0;
  38. for (int i = 0; i < TagList.Length; i++)
  39. {
  40. GameObject _newItem = Instantiate(TabPrefab.gameObject, TabPrefab.transform.parent);
  41. _newItem.transform.localScale = Vector3.one;
  42. _newItem.SetActive(true);
  43. _newItem.GetComponent<ListItem>().mTexts[0].text = TagList[i];
  44. _newItem.GetComponent<ListItem>().mObjects[0].SetActive(i == selectedTab);
  45. _newItem.GetComponent<ListItem>().mID =i;
  46. TabList.Add(_newItem.GetComponent<ListItem>());
  47. }
  48. float _width = (GetComponent<RectTransform>().sizeDelta.x - 42F - 90F) / (TabList.Count - 1);
  49. for (int i = 0; i < TabList.Count; i++)
  50. {
  51. TabList[i].GetComponent<RectTransform>().sizeDelta = new Vector2(i == 0 ? 90F : _width, 44F);
  52. }
  53. TabItems = TabList.ToArray();
  54. inited = true;
  55. }
  56. private void CheckDragging()//Check if there is any item being dragged
  57. {
  58. if (ItemDragManager.isDragging)
  59. {
  60. wasDragging = true;
  61. }
  62. else
  63. {
  64. if (wasDragging) FilterItems();
  65. wasDragging = false;
  66. }
  67. }
  68. public void SwitchTab(int _id)//Switch bwteen different tags
  69. {
  70. selectedTab = _id;
  71. for (int i = 0; i < TabItems.Length; i++)
  72. {
  73. TabItems[i].mObjects[0].SetActive(i == selectedTab);
  74. }
  75. SoundManager.Play2D("Tab");
  76. }
  77. public void FilterItems()//Filter items by the tag
  78. {
  79. for (int i = 0; i < Items.Count; i++)
  80. {
  81. Items[i].SetVisible(Items[i].isNameMatch(SearchInput.text) && Items[i].isTagMatchText(TagList[selectedTab]) && (!filterFav || Items[i].Fav.activeSelf));
  82. }
  83. ClearFilterButton.SetActive(SearchInput.text != "");
  84. }
  85. public void ClearFilter()//Cleart all filters
  86. {
  87. SearchInput.text = "";
  88. ClearFilterButton.SetActive(false);
  89. filterFav = false;
  90. for (int i = 0; i < Items.Count; i++)
  91. {
  92. Items[i].SetVisible(true);
  93. }
  94. }
  95. public void ShowFavItems()//Toggle to only show the favorite items
  96. {
  97. filterFav = !filterFav;
  98. FavActiveIcon.SetActive(filterFav);
  99. SoundManager.Play2D("Tab");
  100. }
  101. }
  102. }