using System; using System.Collections.Generic; using Sirenix.OdinInspector; using TMPro; using UnityEngine; // ReSharper disable once CheckNamespace namespace BlueWaterProject { public abstract class ItemInventoryUi : PopupUi { [SerializeField, Required] protected TMP_Dropdown sortingDropdown; [SerializeField, Required] protected TMP_Text currentWeight; [SerializeField, Required] protected GameObject itemSlotUiPrefab; [SerializeField, Required] protected Transform instantiateLocation; [SerializeField, Required] protected DiscardPopupUi discardPopupUi; [field: SerializeField] public List ItemSlotUiList { get; set; } = new(); protected PlayerInventory inventory; protected List selectedList = new(); private void OnEnable() { discardPopupUi.Close(); InitAndUpdateInventory(); inventory.OnChangeItemSlot += ChangedData; } private void Start() { InitAndUpdateInventory(); } private void OnDisable() { inventory.OnChangeItemSlot -= ChangedData; } public override void Close() { if (discardPopupUi.gameObject.activeSelf) { discardPopupUi.Close(); } base.Close(); } protected virtual void InitAndUpdateInventory() { InitInventory(); InventorySynchronization(); InitWeight(); } private void InitInventory() { ItemSlotUiList.Clear(); foreach (Transform element in instantiateLocation) { Destroy(element.gameObject); } } private void InventorySynchronization() { foreach (var element in inventory.ItemSlotList) { var newItemContent = Instantiate(itemSlotUiPrefab, instantiateLocation).GetComponent(); newItemContent.InitData(element); ItemSlotUiList.Add(newItemContent); } } private void InitWeight() { if (float.IsPositiveInfinity(inventory.WeightLimit)) { currentWeight.text = inventory.CurrentTotalWeight + " / ∞Kg"; } else { currentWeight.text = inventory.CurrentTotalWeight + " / " + inventory.WeightLimit + "Kg"; } currentWeight.color = inventory.IsOverWeight ? Color.red : Color.white; } public void ChangedData(ItemSlot changedItemSlot, bool added) { if (added) { AddItem(changedItemSlot); } else { RemoveItem(changedItemSlot); } InitWeight(); } private void AddItem(ItemSlot addItemSlot) { var existingItemSlotUi = ItemSlotUiList.Find(i => i.ItemSlot.Idx == addItemSlot.Idx); if (existingItemSlotUi != null) { existingItemSlotUi.UpdateData(addItemSlot); } else { var newItemSlot = Instantiate(itemSlotUiPrefab, instantiateLocation).GetComponent(); newItemSlot.InitData(addItemSlot); ItemSlotUiList.Add(newItemSlot); } } private void RemoveItem(ItemSlot removeItemSlot) { var existingItemSlotUi = ItemSlotUiList.Find(i => i.ItemSlot.Idx == removeItemSlot.Idx); if (existingItemSlotUi != null) { existingItemSlotUi.UpdateData(removeItemSlot); if (existingItemSlotUi.ItemSlot.Count <= 0) { ItemSlotUiList.Remove(existingItemSlotUi); Destroy(existingItemSlotUi.gameObject); } } } public void SelectAll() { foreach (var element in ItemSlotUiList) { element.ToggleOn(); } } public void DeselectAll() { foreach (var element in ItemSlotUiList) { element.ToggleOff(); } } public void DiscardButton() { selectedList.Clear(); foreach (var element in ItemSlotUiList) { if (!element.ToggleIsOn) continue; selectedList.Add(element); } if (selectedList.Count == 1) { discardPopupUi.DiscardMessage(selectedList[0]); } else if (selectedList.Count > 1) { discardPopupUi.DiscardAllMessage(selectedList); } } public void SortButton() { if (sortingDropdown.value == 0) return; inventory.SortItem((InventorySortingType)sortingDropdown.value); SortItemSlotUi((InventorySortingType)sortingDropdown.value); sortingDropdown.value = 0; } private void SortItemSlotUi(InventorySortingType sortingType) { switch (sortingType) { case InventorySortingType.NONE: return; case InventorySortingType.RECENT: ItemSlotUiList.Sort((x, y) => y.ItemSlot.AcquisitionTime.CompareTo(x.ItemSlot.AcquisitionTime)); break; case InventorySortingType.NAME: ItemSlotUiList.Sort((x, y) => string.Compare(ItemManager.Inst.ItemDictionary[x.ItemSlot.Idx].name, ItemManager.Inst.ItemDictionary[y.ItemSlot.Idx].name, StringComparison.Ordinal)); break; case InventorySortingType.CATEGORY: ItemSlotUiList.Sort((x, y) => ItemManager.Inst.ItemDictionary[x.ItemSlot.Idx].category.CompareTo(ItemManager.Inst.ItemDictionary[y.ItemSlot.Idx].category)); break; case InventorySortingType.QUANTITY: ItemSlotUiList.Sort((x, y) => y.ItemSlot.Count.CompareTo(x.ItemSlot.Count)); break; default: throw new ArgumentOutOfRangeException(); } for (var i = 0; i < ItemSlotUiList.Count; i++) { ItemSlotUiList[i].transform.SetSiblingIndex(i); } } } }