2024-09-22 15:13:59 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
2024-09-25 13:56:54 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2024-10-13 09:53:37 +00:00
|
|
|
|
using ColdMint.scripts.debug;
|
2024-09-22 15:13:59 +00:00
|
|
|
|
using ColdMint.scripts.map.events;
|
|
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.inventory;
|
|
|
|
|
|
|
|
|
|
public abstract class ItemContainerDisplayTemplate : IItemContainerDisplay
|
|
|
|
|
{
|
|
|
|
|
protected readonly List<IItemDisplay> ItemDisplayList = [];
|
|
|
|
|
private IItemContainer? _itemContainer;
|
|
|
|
|
|
2024-10-04 08:13:34 +00:00
|
|
|
|
public async void BindItemContainer(IItemContainer? itemContainer)
|
2024-09-22 15:13:59 +00:00
|
|
|
|
{
|
|
|
|
|
if (_itemContainer == itemContainer)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_itemContainer != null)
|
|
|
|
|
{
|
|
|
|
|
_itemContainer.SelectedItemChangeEvent -= OnSelectedItemChangeEvent;
|
2024-09-23 13:03:39 +00:00
|
|
|
|
_itemContainer.ItemDataChangeEvent -= OnItemDataChangeEvent;
|
2024-09-22 15:13:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_itemContainer = itemContainer;
|
2024-10-04 08:13:34 +00:00
|
|
|
|
if (itemContainer == null)
|
|
|
|
|
{
|
|
|
|
|
//Set empty items container to hide all ui.
|
|
|
|
|
//设置空物品容器,隐藏全部ui。
|
2024-10-13 09:53:37 +00:00
|
|
|
|
for (var i = 0; i < ItemDisplayList.Count; i++)
|
2024-10-04 08:13:34 +00:00
|
|
|
|
{
|
2024-10-13 09:53:37 +00:00
|
|
|
|
var itemDisplay = ItemDisplayList[i];
|
|
|
|
|
LogCat.LogWithFormat("hide_display_item", LogCat.LogLabel.ItemContainerDisplay, i);
|
2024-10-04 08:13:34 +00:00
|
|
|
|
itemDisplay.Update(null);
|
|
|
|
|
itemDisplay.HideSelf();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
itemContainer.SelectedItemChangeEvent += OnSelectedItemChangeEvent;
|
|
|
|
|
itemContainer.ItemDataChangeEvent += OnItemDataChangeEvent;
|
2024-09-22 15:13:59 +00:00
|
|
|
|
var totalCapacity = itemContainer.GetTotalCapacity();
|
|
|
|
|
var currentCapacity = ItemDisplayList.Count;
|
|
|
|
|
var capacityDifference = totalCapacity - currentCapacity;
|
|
|
|
|
if (capacityDifference > 0)
|
|
|
|
|
{
|
|
|
|
|
//There are those that need to be added, and we create them.
|
|
|
|
|
//有需要添加的,我们创建他们。
|
|
|
|
|
for (var i = 0; i < capacityDifference; i++)
|
|
|
|
|
{
|
2024-10-13 09:53:37 +00:00
|
|
|
|
LogCat.LogWithFormat("add_display_item", LogCat.LogLabel.ItemContainerDisplay, i);
|
2024-09-22 15:13:59 +00:00
|
|
|
|
AddItemDisplay();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (capacityDifference < 0)
|
|
|
|
|
{
|
|
|
|
|
//There are things that need to be hidden
|
|
|
|
|
//有需要被隐藏的
|
2024-10-13 09:53:37 +00:00
|
|
|
|
for (var i = currentCapacity - 1; i >= totalCapacity; i--)
|
2024-09-22 15:13:59 +00:00
|
|
|
|
{
|
2024-10-13 09:53:37 +00:00
|
|
|
|
LogCat.LogWithFormat("hide_display_item", LogCat.LogLabel.ItemContainerDisplay, i);
|
2024-09-22 15:13:59 +00:00
|
|
|
|
var itemDisplay = ItemDisplayList[i];
|
|
|
|
|
itemDisplay.Update(null);
|
|
|
|
|
itemDisplay.HideSelf();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 13:56:54 +00:00
|
|
|
|
await Task.Yield();
|
2024-10-13 09:53:37 +00:00
|
|
|
|
UpdateData(itemContainer, totalCapacity);
|
2024-09-22 15:13:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-23 13:03:39 +00:00
|
|
|
|
private void OnItemDataChangeEvent(ItemDataChangeEvent itemDataChangeEvent)
|
|
|
|
|
{
|
|
|
|
|
if (_itemContainer == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 13:56:54 +00:00
|
|
|
|
UpdateDataForSingleLocation(_itemContainer, itemDataChangeEvent.NewIndex);
|
2024-09-23 13:03:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 15:13:59 +00:00
|
|
|
|
private void OnSelectedItemChangeEvent(SelectedItemChangeEvent selectedItemChangeEvent)
|
|
|
|
|
{
|
|
|
|
|
if (_itemContainer == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 13:56:54 +00:00
|
|
|
|
UpdateDataForSingleLocation(_itemContainer, selectedItemChangeEvent.OldIndex);
|
|
|
|
|
UpdateDataForSingleLocation(_itemContainer, selectedItemChangeEvent.NewIndex);
|
2024-09-22 15:13:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Update data</para>
|
|
|
|
|
/// <para>更新数据</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
///<para>Used to batch update the Item data in itemContainer to the display.</para>
|
|
|
|
|
///<para>用于将itemContainer内的Item数据批量更新到显示器内。</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="itemContainer">
|
|
|
|
|
///<para>Item container data</para>
|
|
|
|
|
///<para>物品容器数据</para>
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="endIndex">
|
|
|
|
|
///<para>endIndex</para>
|
|
|
|
|
///<para>结束位置</para>
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="startIndex">
|
|
|
|
|
///<para>startIndex</para>
|
|
|
|
|
///<para>起始位置</para>
|
|
|
|
|
/// </param>
|
|
|
|
|
private void UpdateData(IItemContainer itemContainer, int endIndex, int startIndex = 0)
|
|
|
|
|
{
|
2024-10-13 09:53:37 +00:00
|
|
|
|
LogCat.LogWithFormat("batch_update_data", LogCat.LogLabel.ItemContainerDisplay, startIndex, endIndex);
|
2024-09-22 15:13:59 +00:00
|
|
|
|
for (var i = startIndex; i < endIndex; i++)
|
|
|
|
|
{
|
2024-10-04 08:13:34 +00:00
|
|
|
|
UpdateDataForSingleLocation(itemContainer, i);
|
2024-09-22 15:13:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Update data for a single location</para>
|
|
|
|
|
/// <para>更新单个位置的数据</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="itemContainer"></param>
|
|
|
|
|
/// <param name="index"></param>
|
2024-09-25 13:56:54 +00:00
|
|
|
|
private void UpdateDataForSingleLocation(IItemContainer itemContainer, int index)
|
2024-09-22 15:13:59 +00:00
|
|
|
|
{
|
2024-10-13 09:53:37 +00:00
|
|
|
|
LogCat.LogWithFormat("update_display_item", LogCat.LogLabel.ItemContainerDisplay, index);
|
2024-09-22 15:13:59 +00:00
|
|
|
|
var itemDisplay = ItemDisplayList[index];
|
2024-09-25 13:56:54 +00:00
|
|
|
|
var item = itemContainer.GetItem(index);
|
|
|
|
|
if (item == null)
|
2024-09-23 15:17:57 +00:00
|
|
|
|
{
|
2024-09-28 14:59:25 +00:00
|
|
|
|
item = itemContainer.GetPlaceHolderItem(index);
|
2024-09-23 15:17:57 +00:00
|
|
|
|
}
|
2024-09-28 14:59:25 +00:00
|
|
|
|
if (itemContainer.SupportSelect)
|
2024-09-23 15:17:57 +00:00
|
|
|
|
{
|
2024-09-25 13:56:54 +00:00
|
|
|
|
item.IsSelect = index == itemContainer.GetSelectIndex();
|
2024-09-23 15:17:57 +00:00
|
|
|
|
}
|
2024-09-28 14:59:25 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
item.IsSelect = false;
|
|
|
|
|
}
|
2024-09-25 13:56:54 +00:00
|
|
|
|
itemDisplay.Update(item);
|
|
|
|
|
itemDisplay.ShowSelf();
|
2024-09-22 15:13:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Add item display</para>
|
|
|
|
|
/// <para>添加物品显示器</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected abstract void AddItemDisplay();
|
|
|
|
|
|
|
|
|
|
public IEnumerator<IItemDisplay> GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
return ItemDisplayList.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
return GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
}
|