2024-06-12 17:18:55 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2024-10-04 02:21:09 +00:00
|
|
|
|
using System.Reflection;
|
2024-06-19 16:02:32 +00:00
|
|
|
|
using ColdMint.scripts.map.events;
|
2024-06-13 02:43:54 +00:00
|
|
|
|
|
2024-06-12 13:33:29 +00:00
|
|
|
|
namespace ColdMint.scripts.inventory;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>UniversalItemContainer</para>
|
|
|
|
|
/// <para>通用的物品容器</para>
|
|
|
|
|
/// </summary>
|
2024-09-22 08:51:42 +00:00
|
|
|
|
public class UniversalItemContainer(int totalCapacity) : IItemContainer
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-09-27 12:53:04 +00:00
|
|
|
|
private readonly Dictionary<int, IItem> _itemDictionary = [];
|
2024-06-12 13:33:29 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>UnknownIndex</para>
|
|
|
|
|
/// <para>未知位置</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const int UnknownIndex = -1;
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
//_selectIndex defaults to 0.
|
2024-06-12 13:33:29 +00:00
|
|
|
|
//_selectIndex默认为0.
|
|
|
|
|
private int _selectIndex;
|
|
|
|
|
|
2024-10-04 02:21:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>The type of item that can be added to the item container</para>
|
|
|
|
|
/// <para>物品容器允许添加的物品类型</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly HashSet<int> _allowedItemTypes = new();
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
public Action<SelectedItemChangeEvent>? SelectedItemChangeEvent { get; set; }
|
2024-09-23 13:03:39 +00:00
|
|
|
|
public Action<ItemDataChangeEvent>? ItemDataChangeEvent { get; set; }
|
2024-06-19 16:02:32 +00:00
|
|
|
|
|
2024-10-04 02:21:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Allow Item Types Except Placeholder</para>
|
|
|
|
|
/// <para>允许添加除占位符以外的所有物品类型</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void AllowItemTypesExceptPlaceholder()
|
|
|
|
|
{
|
|
|
|
|
var itemTypeType = typeof(Config.ItemType);
|
|
|
|
|
//Get all fields
|
|
|
|
|
//获取所有字段
|
|
|
|
|
var fields = itemTypeType.GetFields(BindingFlags.Public | BindingFlags.Static);
|
|
|
|
|
//Traversal field
|
|
|
|
|
//遍历字段
|
|
|
|
|
foreach (var field in fields)
|
|
|
|
|
{
|
|
|
|
|
//Gets the value of the field
|
|
|
|
|
//获取字段的值
|
|
|
|
|
var value = field.GetValue(null);
|
|
|
|
|
if (value == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
var intValue = (int)value;
|
|
|
|
|
if (intValue == Config.ItemType.Placeholder)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
_allowedItemTypes.Add(intValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AllowAddingItemByType(int itemType)
|
|
|
|
|
{
|
|
|
|
|
_allowedItemTypes.Add(itemType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DisallowAddingItemByType(int itemType)
|
|
|
|
|
{
|
|
|
|
|
_allowedItemTypes.Remove(itemType);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 19:07:55 +00:00
|
|
|
|
public bool CanAddItem(IItem item)
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-10-04 02:21:09 +00:00
|
|
|
|
if (!_allowedItemTypes.Contains(item.ItemType))
|
2024-09-29 14:12:54 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-09-22 08:51:42 +00:00
|
|
|
|
//If the capacity is not full, directly return to add items
|
|
|
|
|
//如果未占满容量,直接返回可添加物品
|
2024-09-23 15:17:57 +00:00
|
|
|
|
if (GetUsedCapacity() < totalCapacity)
|
2024-09-22 08:51:42 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-06-12 13:33:29 +00:00
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
if (item.MaxQuantity == 1)
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-09-22 08:51:42 +00:00
|
|
|
|
//New items do not support overlay, capacity is full, return cannot add.
|
|
|
|
|
//新物品不支持叠加,容量已满,返回不能添加。
|
2024-06-12 13:33:29 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
//If the capacity is full, we calculate whether we can spread the new items evenly among the existing items.
|
|
|
|
|
//如果容量占满了,我们计算是否能将新物品均摊在已有的物品内。
|
|
|
|
|
var unallocatedQuantity = item.Quantity;
|
2024-09-27 12:53:04 +00:00
|
|
|
|
foreach (var unitItem in _itemDictionary.Values)
|
2024-09-22 08:51:42 +00:00
|
|
|
|
{
|
|
|
|
|
var number = unitItem.MergeableItemCount(item, unallocatedQuantity);
|
|
|
|
|
if (number == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unallocatedQuantity -= number;
|
|
|
|
|
if (unallocatedQuantity < 1)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-12 13:33:29 +00:00
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
return unallocatedQuantity < 1;
|
2024-06-12 13:33:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-23 15:17:57 +00:00
|
|
|
|
private void UpdateSelectStatus(int index, IItem item)
|
|
|
|
|
{
|
|
|
|
|
item.IsSelect = index == _selectIndex;
|
|
|
|
|
}
|
2024-09-23 13:03:39 +00:00
|
|
|
|
|
2024-09-27 12:53:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Update the next available index location</para>
|
|
|
|
|
/// <para>更新下个可用的索引位置</para>
|
|
|
|
|
/// </summary>
|
2024-10-08 01:03:10 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
///<para>For example, the variable [1,2,3,5,6] represents 4, or the variable [1,2,3,4,5,6,7] represents 8.</para>
|
|
|
|
|
///<para>例如[1,2,3,5,6]这个变量表示4,再或者[1,2,3,4,5,6,7]这个变量表示8。</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <returns>
|
|
|
|
|
///<para>The next available index is returned after obtaining failure<see cref="UnknownIndex"/></para>
|
|
|
|
|
///<para>下次可用的索引,获取失败返回<see cref="UnknownIndex"/></para>
|
|
|
|
|
/// </returns>
|
|
|
|
|
private int GetNextAvailableIndex()
|
2024-09-27 12:53:04 +00:00
|
|
|
|
{
|
|
|
|
|
if (totalCapacity <= 0)
|
|
|
|
|
{
|
2024-10-08 01:03:10 +00:00
|
|
|
|
return UnknownIndex;
|
2024-09-27 12:53:04 +00:00
|
|
|
|
}
|
|
|
|
|
for (var i = 0; i < totalCapacity; i++)
|
|
|
|
|
{
|
|
|
|
|
var contains = _itemDictionary.ContainsKey(i);
|
|
|
|
|
if (!contains)
|
|
|
|
|
{
|
2024-10-08 01:03:10 +00:00
|
|
|
|
return i;
|
2024-09-27 12:53:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-08 01:03:10 +00:00
|
|
|
|
return UnknownIndex;
|
2024-09-27 12:53:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
public int AddItem(IItem item)
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-10-08 01:03:10 +00:00
|
|
|
|
var nextAvailableIndex = GetNextAvailableIndex();
|
2024-09-22 08:51:42 +00:00
|
|
|
|
if (item.MaxQuantity == 1)
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-10-08 01:03:10 +00:00
|
|
|
|
if (nextAvailableIndex == UnknownIndex)
|
2024-09-22 08:51:42 +00:00
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 12:53:04 +00:00
|
|
|
|
_itemDictionary[nextAvailableIndex] = item;
|
2024-09-28 14:59:25 +00:00
|
|
|
|
item.Index = nextAvailableIndex;
|
2024-09-27 15:24:22 +00:00
|
|
|
|
item.ItemContainer = this;
|
2024-09-30 00:56:00 +00:00
|
|
|
|
if (nextAvailableIndex != _selectIndex)
|
|
|
|
|
{
|
|
|
|
|
item.HideSelf();
|
|
|
|
|
}
|
2024-09-27 12:53:04 +00:00
|
|
|
|
UpdateSelectStatus(nextAvailableIndex, item);
|
2024-09-23 13:03:39 +00:00
|
|
|
|
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
|
|
|
|
|
{
|
|
|
|
|
NewItem = item,
|
2024-09-27 12:53:04 +00:00
|
|
|
|
NewIndex = nextAvailableIndex,
|
2024-09-23 13:03:39 +00:00
|
|
|
|
Type = Config.ItemDataChangeEventType.QuantityAdded
|
|
|
|
|
});
|
2024-09-22 08:51:42 +00:00
|
|
|
|
return item.Quantity;
|
2024-06-12 13:33:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
//There can be more than one item, try to share equally.
|
|
|
|
|
//物品可有多个,尝试均摊。
|
|
|
|
|
var originalQuantity = item.Quantity;
|
2024-09-29 15:30:42 +00:00
|
|
|
|
var temporarilyQuantity = item.Quantity;
|
2024-09-23 13:03:39 +00:00
|
|
|
|
var index = 0;
|
2024-09-27 12:53:04 +00:00
|
|
|
|
foreach (var unitItem in _itemDictionary.Values)
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-09-29 15:30:42 +00:00
|
|
|
|
var number = unitItem.MergeableItemCount(item, temporarilyQuantity);
|
2024-09-22 08:51:42 +00:00
|
|
|
|
if (number == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 15:30:42 +00:00
|
|
|
|
temporarilyQuantity -= number;
|
2024-09-22 08:51:42 +00:00
|
|
|
|
unitItem.Quantity += number;
|
2024-09-23 13:03:39 +00:00
|
|
|
|
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
|
|
|
|
|
{
|
|
|
|
|
NewItem = unitItem,
|
|
|
|
|
NewIndex = index,
|
|
|
|
|
Type = Config.ItemDataChangeEventType.QuantityAdded
|
|
|
|
|
});
|
2024-09-22 08:51:42 +00:00
|
|
|
|
if (item.Quantity < 1)
|
|
|
|
|
{
|
|
|
|
|
//New items are fully shared.
|
|
|
|
|
//新物品完全被均摊。
|
2024-09-30 00:56:00 +00:00
|
|
|
|
item.HideSelf();
|
2024-09-22 08:51:42 +00:00
|
|
|
|
return originalQuantity;
|
|
|
|
|
}
|
2024-06-12 13:33:29 +00:00
|
|
|
|
|
2024-09-23 13:03:39 +00:00
|
|
|
|
index++;
|
2024-09-22 08:51:42 +00:00
|
|
|
|
}
|
2024-06-12 13:33:29 +00:00
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
//New items have some left over.
|
|
|
|
|
//新物品有一些剩余。
|
2024-09-23 15:17:57 +00:00
|
|
|
|
if (GetUsedCapacity() >= totalCapacity)
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-09-22 08:51:42 +00:00
|
|
|
|
//The capacity is full. The remaining capacity cannot be stored.
|
|
|
|
|
//容量已满,无法存放剩余。
|
2024-09-29 15:30:42 +00:00
|
|
|
|
return originalQuantity - temporarilyQuantity;
|
2024-06-12 13:33:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
//Add the rest to the container.
|
|
|
|
|
//添加剩余到容器内。
|
2024-10-08 01:03:10 +00:00
|
|
|
|
if (nextAvailableIndex == UnknownIndex)
|
2024-09-27 12:53:04 +00:00
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-10-08 01:03:10 +00:00
|
|
|
|
_itemDictionary[nextAvailableIndex] = item;
|
|
|
|
|
item.Index = nextAvailableIndex;
|
2024-09-27 15:24:22 +00:00
|
|
|
|
item.ItemContainer = this;
|
2024-10-08 01:03:10 +00:00
|
|
|
|
if (nextAvailableIndex != _selectIndex)
|
2024-09-30 00:56:00 +00:00
|
|
|
|
{
|
|
|
|
|
item.HideSelf();
|
|
|
|
|
}
|
2024-10-08 01:03:10 +00:00
|
|
|
|
UpdateSelectStatus(nextAvailableIndex, item);
|
2024-09-23 13:03:39 +00:00
|
|
|
|
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
|
|
|
|
|
{
|
|
|
|
|
NewItem = item,
|
2024-10-08 01:03:10 +00:00
|
|
|
|
NewIndex = nextAvailableIndex,
|
2024-09-23 13:03:39 +00:00
|
|
|
|
Type = Config.ItemDataChangeEventType.Add
|
|
|
|
|
});
|
2024-09-22 08:51:42 +00:00
|
|
|
|
return originalQuantity;
|
2024-06-12 13:33:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
public bool SupportSelect { get; set; }
|
2024-09-28 14:59:25 +00:00
|
|
|
|
|
|
|
|
|
public IItem GetPlaceHolderItem(int index)
|
2024-09-23 15:17:57 +00:00
|
|
|
|
{
|
2024-09-28 14:59:25 +00:00
|
|
|
|
var placeholderItem = new PlaceholderItem
|
|
|
|
|
{
|
|
|
|
|
Index = index,
|
|
|
|
|
ItemContainer = this
|
|
|
|
|
};
|
|
|
|
|
return placeholderItem;
|
2024-09-23 15:17:57 +00:00
|
|
|
|
}
|
2024-09-22 08:51:42 +00:00
|
|
|
|
|
|
|
|
|
public int GetSelectIndex()
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-09-22 08:51:42 +00:00
|
|
|
|
return _selectIndex;
|
|
|
|
|
}
|
2024-06-12 13:33:29 +00:00
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
public IItem? GetSelectItem()
|
|
|
|
|
{
|
2024-09-27 12:53:04 +00:00
|
|
|
|
return _itemDictionary.TryGetValue(_selectIndex, out var item) ? item : null;
|
2024-06-12 13:33:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
public IItem? GetItem(int index)
|
2024-06-12 17:18:55 +00:00
|
|
|
|
{
|
2024-09-27 12:53:04 +00:00
|
|
|
|
return _itemDictionary.TryGetValue(index, out var item) ? item : null;
|
2024-09-25 13:56:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 15:24:22 +00:00
|
|
|
|
public bool ReplaceItem(int index, IItem item)
|
|
|
|
|
{
|
|
|
|
|
var oldItem = GetItem(index);
|
|
|
|
|
_itemDictionary[index] = item;
|
2024-09-28 14:59:25 +00:00
|
|
|
|
item.Index = index;
|
|
|
|
|
item.ItemContainer = this;
|
2024-09-27 15:24:22 +00:00
|
|
|
|
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
|
|
|
|
|
{
|
|
|
|
|
NewItem = item,
|
|
|
|
|
NewIndex = index,
|
|
|
|
|
OldIndex = index,
|
|
|
|
|
OldItem = oldItem,
|
|
|
|
|
Type = Config.ItemDataChangeEventType.Replace
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 14:12:54 +00:00
|
|
|
|
public bool CanReplaceItem(int index, IItem item)
|
|
|
|
|
{
|
2024-10-04 02:21:09 +00:00
|
|
|
|
if (!_allowedItemTypes.Contains(item.ItemType))
|
2024-09-29 14:12:54 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-28 14:59:25 +00:00
|
|
|
|
|
|
|
|
|
public bool ClearItem(int index)
|
2024-09-27 15:24:22 +00:00
|
|
|
|
{
|
2024-10-01 12:53:56 +00:00
|
|
|
|
if (!_itemDictionary.TryGetValue(index, out var item))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-09-28 14:59:25 +00:00
|
|
|
|
var result = _itemDictionary.Remove(index);
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
|
|
|
|
|
{
|
|
|
|
|
NewItem = null,
|
|
|
|
|
NewIndex = index,
|
|
|
|
|
OldIndex = index,
|
|
|
|
|
OldItem = null,
|
|
|
|
|
Type = Config.ItemDataChangeEventType.Clear
|
|
|
|
|
});
|
2024-10-01 12:53:56 +00:00
|
|
|
|
if (SupportSelect && index == _selectIndex)
|
|
|
|
|
{
|
|
|
|
|
item.HideSelf();
|
|
|
|
|
SelectedItemChangeEvent?.Invoke(new SelectedItemChangeEvent()
|
|
|
|
|
{
|
|
|
|
|
NewIndex = index,
|
|
|
|
|
OldIndex = index,
|
|
|
|
|
NewItem = null,
|
|
|
|
|
OldItem = null
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-09-28 14:59:25 +00:00
|
|
|
|
}
|
|
|
|
|
return result;
|
2024-09-27 15:24:22 +00:00
|
|
|
|
}
|
2024-09-29 15:30:42 +00:00
|
|
|
|
|
2024-09-27 15:24:22 +00:00
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
public int RemoveSelectItem(int number)
|
2024-06-21 11:16:40 +00:00
|
|
|
|
{
|
2024-09-22 08:51:42 +00:00
|
|
|
|
return RemoveItem(_selectIndex, number);
|
2024-06-21 11:16:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
public int RemoveItem(int itemIndex, int number)
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-09-22 08:51:42 +00:00
|
|
|
|
if (number == 0)
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-09-22 08:51:42 +00:00
|
|
|
|
return 0;
|
2024-06-12 13:33:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 12:53:04 +00:00
|
|
|
|
if (!_itemDictionary.TryGetValue(itemIndex, out var item))
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-09-22 08:51:42 +00:00
|
|
|
|
return 0;
|
2024-06-12 13:33:29 +00:00
|
|
|
|
}
|
2024-09-27 15:24:22 +00:00
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
var originalQuantity = item.Quantity;
|
|
|
|
|
if (number < 0)
|
2024-06-21 14:55:31 +00:00
|
|
|
|
{
|
2024-09-22 08:51:42 +00:00
|
|
|
|
//If the number entered is less than 0, all items are removed.
|
|
|
|
|
//输入的数量小于0,则移除全部物品。
|
|
|
|
|
item.Quantity = 0;
|
2024-09-27 12:53:04 +00:00
|
|
|
|
_itemDictionary.Remove(itemIndex);
|
2024-09-27 13:16:00 +00:00
|
|
|
|
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
|
|
|
|
|
{
|
|
|
|
|
NewItem = item,
|
|
|
|
|
NewIndex = itemIndex,
|
|
|
|
|
Type = Config.ItemDataChangeEventType.Remove
|
|
|
|
|
});
|
2024-09-22 08:51:42 +00:00
|
|
|
|
return originalQuantity;
|
2024-06-21 14:55:31 +00:00
|
|
|
|
}
|
2024-09-22 08:51:42 +00:00
|
|
|
|
|
|
|
|
|
var removed = Math.Min(number, item.Quantity);
|
|
|
|
|
item.Quantity -= removed;
|
|
|
|
|
if (item.Quantity < 1)
|
2024-06-21 14:55:31 +00:00
|
|
|
|
{
|
2024-09-27 12:53:04 +00:00
|
|
|
|
_itemDictionary.Remove(itemIndex);
|
2024-09-27 13:16:00 +00:00
|
|
|
|
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
|
|
|
|
|
{
|
|
|
|
|
NewItem = item,
|
|
|
|
|
NewIndex = itemIndex,
|
|
|
|
|
Type = Config.ItemDataChangeEventType.Remove
|
|
|
|
|
});
|
2024-06-21 14:55:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
return removed;
|
2024-06-12 13:33:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
public int GetUsedCapacity()
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-09-27 12:53:04 +00:00
|
|
|
|
return _itemDictionary.Count;
|
2024-09-22 08:51:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetTotalCapacity()
|
|
|
|
|
{
|
|
|
|
|
return totalCapacity;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 13:33:29 +00:00
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
public void SelectNextItem()
|
|
|
|
|
{
|
2024-09-27 12:53:04 +00:00
|
|
|
|
var count = totalCapacity;
|
2024-06-12 13:33:29 +00:00
|
|
|
|
if (count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var oldSelectIndex = _selectIndex;
|
|
|
|
|
var newSelectIndex = _selectIndex + 1;
|
|
|
|
|
if (newSelectIndex >= count)
|
|
|
|
|
{
|
2024-09-23 15:17:57 +00:00
|
|
|
|
newSelectIndex = 0;
|
2024-06-12 13:33:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
PrivateSelectItem(oldSelectIndex, newSelectIndex);
|
2024-06-12 13:33:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
public void SelectPreviousItem()
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-09-27 12:53:04 +00:00
|
|
|
|
var count = totalCapacity;
|
2024-06-12 13:33:29 +00:00
|
|
|
|
if (count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var oldSelectIndex = _selectIndex;
|
|
|
|
|
var newSelectIndex = _selectIndex - 1;
|
|
|
|
|
if (newSelectIndex < 0)
|
|
|
|
|
{
|
|
|
|
|
newSelectIndex = count - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
PrivateSelectItem(oldSelectIndex, newSelectIndex);
|
2024-06-12 13:33:29 +00:00
|
|
|
|
}
|
2024-06-21 14:55:31 +00:00
|
|
|
|
|
2024-06-12 13:33:29 +00:00
|
|
|
|
/// <summary>
|
2024-09-22 08:51:42 +00:00
|
|
|
|
/// <para>Private methods for selecting items</para>
|
|
|
|
|
/// <para>选择物品的私有方法</para>
|
2024-06-12 13:33:29 +00:00
|
|
|
|
/// </summary>
|
2024-09-22 08:51:42 +00:00
|
|
|
|
/// <param name="oldIndex"></param>
|
|
|
|
|
/// <param name="newIndex"></param>
|
|
|
|
|
private void PrivateSelectItem(int oldIndex, int newIndex)
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
2024-09-22 08:51:42 +00:00
|
|
|
|
if (!SupportSelect || oldIndex == newIndex)
|
2024-06-12 13:33:29 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-23 15:17:57 +00:00
|
|
|
|
//There is no need to broadcast placeholders when an event is invoked.
|
|
|
|
|
//在调用事件时,无需广播占位符。
|
|
|
|
|
var oldItem = GetItem(oldIndex);
|
|
|
|
|
if (oldItem != null)
|
|
|
|
|
{
|
2024-09-30 00:56:00 +00:00
|
|
|
|
oldItem.HideSelf();
|
2024-09-23 15:17:57 +00:00
|
|
|
|
oldItem.IsSelect = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//There is no need to broadcast placeholders when an event is invoked.
|
|
|
|
|
//在调用事件时,无需广播占位符。
|
|
|
|
|
var newItem = GetItem(newIndex);
|
|
|
|
|
if (newItem != null)
|
|
|
|
|
{
|
2024-09-30 00:56:00 +00:00
|
|
|
|
newItem.ShowSelf();
|
2024-09-23 15:17:57 +00:00
|
|
|
|
newItem.IsSelect = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_selectIndex = newIndex;
|
2024-09-22 08:51:42 +00:00
|
|
|
|
SelectedItemChangeEvent?.Invoke(new SelectedItemChangeEvent
|
2024-06-19 16:02:32 +00:00
|
|
|
|
{
|
2024-09-22 15:13:59 +00:00
|
|
|
|
NewIndex = newIndex,
|
|
|
|
|
OldIndex = oldIndex,
|
2024-09-22 08:51:42 +00:00
|
|
|
|
NewItem = newItem,
|
|
|
|
|
OldItem = oldItem
|
2024-06-19 16:02:32 +00:00
|
|
|
|
});
|
2024-06-18 15:37:18 +00:00
|
|
|
|
}
|
2024-06-21 14:55:31 +00:00
|
|
|
|
|
2024-06-13 02:43:54 +00:00
|
|
|
|
|
2024-09-22 08:51:42 +00:00
|
|
|
|
public void SelectItem(int index)
|
2024-06-13 02:43:54 +00:00
|
|
|
|
{
|
2024-09-27 12:53:04 +00:00
|
|
|
|
if (totalCapacity == 0 || index < 0)
|
2024-09-23 15:17:57 +00:00
|
|
|
|
{
|
2024-09-27 12:53:04 +00:00
|
|
|
|
return;
|
2024-09-23 15:17:57 +00:00
|
|
|
|
}
|
2024-09-27 12:53:04 +00:00
|
|
|
|
PrivateSelectItem(_selectIndex, index % totalCapacity);
|
2024-06-13 02:43:54 +00:00
|
|
|
|
}
|
2024-06-12 13:33:29 +00:00
|
|
|
|
}
|