diff --git a/scripts/inventory/HotBar.cs b/scripts/inventory/HotBar.cs index 0def510..79bf7ea 100644 --- a/scripts/inventory/HotBar.cs +++ b/scripts/inventory/HotBar.cs @@ -15,7 +15,11 @@ public partial class HotBar : HBoxContainer public override void _Ready() { base._Ready(); - _itemContainer = new UniversalItemContainer(Config.HotBarSize); + var universalItemContainer = new UniversalItemContainer(Config.HotBarSize) + { + EnablePlaceholder = true + }; + _itemContainer = universalItemContainer; _itemContainer.SupportSelect = true; _itemContainerDisplay = new ItemSlotContainerDisplay(this); _itemContainerDisplay.BindItemContainer(_itemContainer); diff --git a/scripts/inventory/IItemContainer.cs b/scripts/inventory/IItemContainer.cs index ab1d22f..7478e08 100644 --- a/scripts/inventory/IItemContainer.cs +++ b/scripts/inventory/IItemContainer.cs @@ -47,6 +47,12 @@ public interface IItemContainer : IEnumerable /// public bool SupportSelect { get; set; } + /// + /// Gets a placeholder object + /// 获取占位符对象 + /// + /// + IItem? GetPlaceHolderItem(); /// /// Gets the selected location diff --git a/scripts/inventory/ItemContainerDisplayTemplate.cs b/scripts/inventory/ItemContainerDisplayTemplate.cs index 73e7cac..45ecd11 100644 --- a/scripts/inventory/ItemContainerDisplayTemplate.cs +++ b/scripts/inventory/ItemContainerDisplayTemplate.cs @@ -1,6 +1,5 @@ using System.Collections; using System.Collections.Generic; -using System.Threading.Tasks; using ColdMint.scripts.map.events; namespace ColdMint.scripts.inventory; @@ -105,7 +104,21 @@ public abstract class ItemContainerDisplayTemplate : IItemContainerDisplay for (var i = startIndex; i < endIndex; i++) { var itemDisplay = ItemDisplayList[i]; - itemDisplay.Update(i < usedCapacity ? itemContainer.GetItem(i) : null); + 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(); } } @@ -120,7 +133,20 @@ public abstract class ItemContainerDisplayTemplate : IItemContainerDisplay private void UpdateDataForSingleLocation(IItemContainer itemContainer, int index, int usedCapacity) { var itemDisplay = ItemDisplayList[index]; - itemDisplay.Update(index < usedCapacity ? itemContainer.GetItem(index) : null); + if (index < usedCapacity) + { + itemDisplay.Update(itemContainer.GetItem(index)); + } + else + { + var placeHolderItem = itemContainer.GetPlaceHolderItem(); + if (placeHolderItem != null) + { + placeHolderItem.IsSelect = index == itemContainer.GetSelectIndex(); + } + + itemDisplay.Update(placeHolderItem); + } } /// diff --git a/scripts/inventory/Packsack.cs b/scripts/inventory/Packsack.cs index dd6e47d..c0729f5 100644 --- a/scripts/inventory/Packsack.cs +++ b/scripts/inventory/Packsack.cs @@ -1,4 +1,4 @@ -using ColdMint.scripts.pickable; +using ColdMint.scripts.pickable; using ColdMint.scripts.utils; using Godot; using PacksackUi = ColdMint.scripts.loader.uiLoader.PacksackUi; diff --git a/scripts/inventory/PlaceholderItem.cs b/scripts/inventory/PlaceholderItem.cs index 01ec472..1c747cc 100644 --- a/scripts/inventory/PlaceholderItem.cs +++ b/scripts/inventory/PlaceholderItem.cs @@ -3,8 +3,8 @@ using Godot; namespace ColdMint.scripts.inventory; /// -/// A blank item used to occupy a space in the display. The IsSelect property is set to true so that the item display always appears selected when drawn. -/// 物品容器显示器内用于占位的空白物品。IsSelect属性设置为true,使得物品显示器绘制时总是显示为选中状态。 +/// PlaceholderItem +/// 占位物品 /// public class PlaceholderItem : IItem { @@ -14,7 +14,7 @@ public class PlaceholderItem : IItem public string? Description { get; } = null; public int Quantity { get; set; } = 1; public int MaxQuantity { get; } = 1; - public bool IsSelect { get; set; } = true; + public bool IsSelect { get; set; } public int MergeableItemCount(IItem other, int unallocatedQuantity) { diff --git a/scripts/inventory/UniversalItemContainer.cs b/scripts/inventory/UniversalItemContainer.cs index 254794e..5e342cd 100644 --- a/scripts/inventory/UniversalItemContainer.cs +++ b/scripts/inventory/UniversalItemContainer.cs @@ -43,7 +43,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer { //If the capacity is not full, directly return to add items //如果未占满容量,直接返回可添加物品 - if (GetUsedCapacity() < GetTotalCapacity()) + if (GetUsedCapacity() < totalCapacity) { return true; } @@ -76,12 +76,16 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer return unallocatedQuantity < 1; } + private void UpdateSelectStatus(int index, IItem item) + { + item.IsSelect = index == _selectIndex; + } public int AddItem(IItem item) { if (item.MaxQuantity == 1) { - if (GetUsedCapacity() >= GetTotalCapacity()) + if (GetUsedCapacity() >= totalCapacity) { //Items cannot be stacked and cannot be added if the capacity is full. //物品不能叠加,且容量已满,则无法添加。 @@ -89,6 +93,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer } _itemList.Add(item); + UpdateSelectStatus(_itemList.Count - 1, item); ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent { NewItem = item, @@ -130,7 +135,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer //New items have some left over. //新物品有一些剩余。 - if (GetUsedCapacity() >= GetTotalCapacity()) + if (GetUsedCapacity() >= totalCapacity) { //The capacity is full. The remaining capacity cannot be stored. //容量已满,无法存放剩余。 @@ -140,6 +145,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer //Add the rest to the container. //添加剩余到容器内。 _itemList.Add(item); + UpdateSelectStatus(_itemList.Count - 1, item); ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent { NewItem = item, @@ -150,6 +156,12 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer } public bool SupportSelect { get; set; } + public bool EnablePlaceholder { get; set; } + + public IItem? GetPlaceHolderItem() + { + return EnablePlaceholder ? new PlaceholderItem() : null; + } public int GetSelectIndex() { @@ -158,7 +170,12 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer public IItem? GetSelectItem() { - return _itemList.Count == 0 ? null : _itemList[_selectIndex]; + if (_itemList.Count == 0) + { + return null; + } + + return _selectIndex < _itemList.Count ? _itemList[_selectIndex] : null; } public IItem? GetItem(int index) @@ -247,7 +264,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer public void SelectNextItem() { - var count = _itemList.Count; + var count = EnablePlaceholder ? totalCapacity : _itemList.Count; if (count == 0) { return; @@ -257,7 +274,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer var newSelectIndex = _selectIndex + 1; if (newSelectIndex >= count) { - newSelectIndex = count - 1; + newSelectIndex = 0; } PrivateSelectItem(oldSelectIndex, newSelectIndex); @@ -265,7 +282,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer public void SelectPreviousItem() { - var count = _itemList.Count; + var count = EnablePlaceholder ? totalCapacity : _itemList.Count; if (count == 0) { return; @@ -294,10 +311,23 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer return; } - var oldItem = _itemList[oldIndex]; - oldItem.IsSelect = false; - var newItem = _itemList[newIndex]; - newItem.IsSelect = true; + //There is no need to broadcast placeholders when an event is invoked. + //在调用事件时,无需广播占位符。 + var oldItem = GetItem(oldIndex); + if (oldItem != null) + { + oldItem.IsSelect = false; + } + + //There is no need to broadcast placeholders when an event is invoked. + //在调用事件时,无需广播占位符。 + var newItem = GetItem(newIndex); + if (newItem != null) + { + newItem.IsSelect = true; + } + + _selectIndex = newIndex; SelectedItemChangeEvent?.Invoke(new SelectedItemChangeEvent { NewIndex = newIndex, @@ -305,18 +335,28 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer NewItem = newItem, OldItem = oldItem }); - _selectIndex = newIndex; } public void SelectItem(int index) { - var safeIndex = GetSafeIndex(index); - if (safeIndex == UnknownIndex) + if (EnablePlaceholder) { - return; + if (totalCapacity == 0) + { + return; + } + PrivateSelectItem(_selectIndex, index % totalCapacity); } + else + { + var safeIndex = GetSafeIndex(index); + if (safeIndex == UnknownIndex) + { + return; + } - PrivateSelectItem(_selectIndex, safeIndex); + PrivateSelectItem(_selectIndex, safeIndex); + } } } \ No newline at end of file