diff --git a/scripts/inventory/ItemContainerDisplayTemplate.cs b/scripts/inventory/ItemContainerDisplayTemplate.cs index 45ecd11..40ac4e5 100644 --- a/scripts/inventory/ItemContainerDisplayTemplate.cs +++ b/scripts/inventory/ItemContainerDisplayTemplate.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Threading.Tasks; using ColdMint.scripts.map.events; namespace ColdMint.scripts.inventory; @@ -9,7 +10,7 @@ public abstract class ItemContainerDisplayTemplate : IItemContainerDisplay protected readonly List ItemDisplayList = []; private IItemContainer? _itemContainer; - public void BindItemContainer(IItemContainer itemContainer) + public async void BindItemContainer(IItemContainer itemContainer) { if (_itemContainer == itemContainer) { @@ -52,6 +53,7 @@ public abstract class ItemContainerDisplayTemplate : IItemContainerDisplay } } + await Task.Yield(); UpdateData(itemContainer, adjustedEndIndex); } @@ -62,8 +64,7 @@ public abstract class ItemContainerDisplayTemplate : IItemContainerDisplay return; } - var usedCapacity = _itemContainer.GetUsedCapacity(); - UpdateDataForSingleLocation(_itemContainer, itemDataChangeEvent.NewIndex, usedCapacity); + UpdateDataForSingleLocation(_itemContainer, itemDataChangeEvent.NewIndex); } private void OnSelectedItemChangeEvent(SelectedItemChangeEvent selectedItemChangeEvent) @@ -73,9 +74,8 @@ public abstract class ItemContainerDisplayTemplate : IItemContainerDisplay return; } - var usedCapacity = _itemContainer.GetUsedCapacity(); - UpdateDataForSingleLocation(_itemContainer, selectedItemChangeEvent.OldIndex, usedCapacity); - UpdateDataForSingleLocation(_itemContainer, selectedItemChangeEvent.NewIndex, usedCapacity); + UpdateDataForSingleLocation(_itemContainer, selectedItemChangeEvent.OldIndex); + UpdateDataForSingleLocation(_itemContainer, selectedItemChangeEvent.NewIndex); } /// @@ -100,26 +100,9 @@ public abstract class ItemContainerDisplayTemplate : IItemContainerDisplay /// private void UpdateData(IItemContainer itemContainer, int endIndex, int startIndex = 0) { - var usedCapacity = itemContainer.GetUsedCapacity(); for (var i = startIndex; i < endIndex; i++) { - var itemDisplay = ItemDisplayList[i]; - if (i < usedCapacity) - { - itemDisplay.Update(itemContainer.GetItem(i)); - } - else - { - var placeHolderItem = itemContainer.GetPlaceHolderItem(); - if (placeHolderItem != null) - { - placeHolderItem.IsSelect = i == itemContainer.GetSelectIndex(); - } - - itemDisplay.Update(placeHolderItem); - } - - itemDisplay.ShowSelf(); + UpdateDataForSingleLocation(itemContainer,i); } } @@ -129,24 +112,20 @@ public abstract class ItemContainerDisplayTemplate : IItemContainerDisplay /// /// /// - /// - private void UpdateDataForSingleLocation(IItemContainer itemContainer, int index, int usedCapacity) + private void UpdateDataForSingleLocation(IItemContainer itemContainer, int index) { var itemDisplay = ItemDisplayList[index]; - if (index < usedCapacity) + var item = itemContainer.GetItem(index); + if (item == null) { - itemDisplay.Update(itemContainer.GetItem(index)); + item = itemContainer.GetPlaceHolderItem(); } - else + if (item != null) { - var placeHolderItem = itemContainer.GetPlaceHolderItem(); - if (placeHolderItem != null) - { - placeHolderItem.IsSelect = index == itemContainer.GetSelectIndex(); - } - - itemDisplay.Update(placeHolderItem); + item.IsSelect = index == itemContainer.GetSelectIndex(); } + itemDisplay.Update(item); + itemDisplay.ShowSelf(); } /// diff --git a/scripts/inventory/UniversalItemContainer.cs b/scripts/inventory/UniversalItemContainer.cs index 5e342cd..0dfbde2 100644 --- a/scripts/inventory/UniversalItemContainer.cs +++ b/scripts/inventory/UniversalItemContainer.cs @@ -170,38 +170,56 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer public IItem? GetSelectItem() { - if (_itemList.Count == 0) + var count = _itemList.Count; + if (count == 0) { return null; } - return _selectIndex < _itemList.Count ? _itemList[_selectIndex] : null; + return _selectIndex < count ? _itemList[_selectIndex] : null; } public IItem? GetItem(int index) { - var safeIndex = GetSafeIndex(index); - if (safeIndex == UnknownIndex) - { - return null; - } - - return _itemList[safeIndex]; + return GetValidIndex(index) == UnknownIndex ? null : _itemList[index]; } /// - /// Gets a secure subscript index - /// 获取安全的下标索引 + /// Get valid index + /// 获取有效的索引 /// /// /// - /// -1 is returned on failure, and the index that does not result in an out-of-bounds subscript is returned on success - /// 失败返回-1,成功返回不会导致下标越界的索引 + ///Return -1 if the given index exceeds the valid range of the list, otherwise return the given index itself. + ///如果给定的索引超过了列表的有效范围,那么返回-1,否则返回给定的索引本身。 /// - private int GetSafeIndex(int index) + private int GetValidIndex(int index) { var count = _itemList.Count; if (count == 0) + { + return UnknownIndex; + } + if (index >= count || index < 0) + { + return UnknownIndex; + } + return index; + } + + /// + /// Gets a normalized subscript index + /// 获取规范化的下标索引 + /// + /// + /// + /// The difference between this method and is that if the given index is out of range, the result will be returned after rounding. + /// 失败返回-1,成功返回不会导致下标越界的索引,此方法和区别是,如果给定的索引超出了范围,那么会将结果取余后返回。 + /// + private int GetNormalizeIndex(int index) + { + var count = _itemList.Count; + if (count == 0 || index < 0) { //Prevents the dividend from being 0 //防止被除数为0 @@ -224,20 +242,20 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer return 0; } - var safeIndex = GetSafeIndex(itemIndex); - if (safeIndex == UnknownIndex) + var index = GetValidIndex(itemIndex); + if (index == UnknownIndex) { return 0; } - var item = _itemList[safeIndex]; + var item = _itemList[index]; var originalQuantity = item.Quantity; if (number < 0) { //If the number entered is less than 0, all items are removed. //输入的数量小于0,则移除全部物品。 item.Quantity = 0; - _itemList.RemoveAt(safeIndex); + _itemList.RemoveAt(index); return originalQuantity; } @@ -245,7 +263,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer item.Quantity -= removed; if (item.Quantity < 1) { - _itemList.RemoveAt(safeIndex); + _itemList.RemoveAt(index); } return removed; @@ -350,7 +368,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer } else { - var safeIndex = GetSafeIndex(index); + var safeIndex = GetNormalizeIndex(index); if (safeIndex == UnknownIndex) { return;