using System; using System.Collections.Generic; using Sirenix.OdinInspector; using TMPro; using UnityEngine; // ReSharper disable once CheckNamespace namespace BlueWaterProject { public class ItemInventoryUi : MonoBehaviour { private enum InventoryType { NONE = 0, OCEAN_INVENTORY_IN_OCEAN, OCEAN_INVENTORY_IN_COMBAT, COMBAT_INVENTORY_IN_COMBAT, } [SerializeField] private InventoryType inventoryType; [SerializeField, Required] private TMP_Dropdown sortingDropdown; [SerializeField, Required] private TMP_Text currentWeight; [SerializeField, Required] private GameObject itemSlotUiPrefab; [SerializeField, Required] private Transform instantiateLocation; [field: SerializeField] public List ItemSlotUiList { get; set; } = new(); private PlayerInventory inventory; private List selectedList = new(); private void OnEnable() { InitAndUpdateInventory(); inventory.OnChangeItemSlot += ChangedData; } private void Start() { InitAndUpdateInventory(); } private void OnDisable() { inventory.OnChangeItemSlot -= ChangedData; } public void InitAndUpdateInventory() { inventory ??= GetPlayerInventory(); InitInventory(); InventorySynchronization(); InitWeight(); } private PlayerInventory GetPlayerInventory() { switch (inventoryType) { case InventoryType.NONE: return null; case InventoryType.OCEAN_INVENTORY_IN_OCEAN: case InventoryType.OCEAN_INVENTORY_IN_COMBAT: return DataManager.Inst.OceanInventory; case InventoryType.COMBAT_INVENTORY_IN_COMBAT: return DataManager.Inst.CombatInventory; default: throw new ArgumentOutOfRangeException(); } } 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) { UiManager.Inst.CurrentDiscardPopupUi.DiscardMessage(selectedList[0]); } else if (selectedList.Count > 1) { UiManager.Inst.CurrentDiscardPopupUi.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); } } public void SetActiveInventoryUi(bool value) => gameObject.SetActive(value); } }