Improving the ability to drag items, there are still some issues.

改进拖动物品的功能,仍然存在一些问题。
This commit is contained in:
Cold-Mint 2024-09-27 23:24:22 +08:00
parent 617164a4bc
commit 4f2208bd60
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
8 changed files with 128 additions and 22 deletions

View File

@ -171,6 +171,12 @@ public static class Config
/// </remarks>
public const string DefaultVersionName = "Default";
/// <summary>
/// <para>EmptyVariant</para>
/// <para>空变量</para>
/// </summary>
public static readonly Variant EmptyVariant = new();
/// <summary>
/// <para>IsDebug</para>
@ -231,7 +237,13 @@ public static class Config
/// <para>remove</para>
/// <para>移除</para>
/// </summary>
Remove
Remove,
/// <summary>
/// <para>Replace</para>
/// <para>被替换</para>
/// </summary>
Replace
}
public enum OsEnum

View File

@ -789,7 +789,7 @@ public partial class CharacterTemplate : CharacterBody2D
{
return;
}
item.ItemContainer = null;
NodeUtils.CallDeferredAddChild(NodeUtils.FindContainerNode(node2D, GetNode("/root")), node2D);
switch (item)
{

View File

@ -46,6 +46,12 @@ public interface IItem
/// </summary>
bool IsSelect { get; set; }
/// <summary>
/// <para>The container in which the item is located</para>
/// <para>物品所在的物品容器</para>
/// </summary>
IItemContainer? ItemContainer { get; set; }
/// <summary>
/// <para>Calculate how many items can be merged with other items</para>
/// <para>计算当前物品可与其他物品合并多少个</para>

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using ColdMint.scripts.map.events;
namespace ColdMint.scripts.inventory;
@ -76,6 +75,21 @@ public interface IItemContainer
/// <returns></returns>
IItem? GetItem(int index);
/// <summary>
/// <para>ReplaceItem</para>
/// <para>替换物品</para>
/// </summary>
/// <remarks>
///<para>Even if the item corresponding to the index is null, it can be successfully replaced.</para>
///<para>即使索引对应的物品为null也可以成功的替换。</para>
/// </remarks>
/// <param name="index"></param>
/// <param name="item"></param>
/// <returns></returns>
bool ReplaceItem(int index, IItem item);
bool ReplaceItem(IItem oldItem, IItem newItem);
/// <summary>
/// <para>Gets the item in the specified location, equivalent to <see cref="GetItem"/></para>
/// <para>获取指定位置的物品,等同于<see cref="GetItem"/></para>
@ -114,6 +128,14 @@ public interface IItemContainer
/// </returns>
int RemoveItem(int itemIndex, int number);
/// <summary>
/// <para>Remove item</para>
/// <para>移除物品</para>
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
int RemoveItem(IItem item, int number);
/// <summary>
/// <para>Gets the used capacity of the item container</para>
/// <para>获取物品容器已使用的容量</para>

View File

@ -32,9 +32,17 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay
public override Variant _GetDragData(Vector2 atPosition)
{
switch (Item)
{
case null:
return Config.EmptyVariant;
case PlaceholderItem:
return Config.EmptyVariant;
}
if (_iconTextureRect == null)
{
return new Variant();
return Config.EmptyVariant;
}
var textureRect = new TextureRect();
@ -47,15 +55,25 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay
public override bool _CanDropData(Vector2 atPosition, Variant data)
{
if (Item == null)
var type = data.VariantType;
if (type == Variant.Type.Nil)
{
return false;
}
if (Item is PlaceholderItem)
switch (Item)
{
return false;
}
case null:
case PlaceholderItem:
return true;
default:
var itemSlotNode = data.As<ItemSlotNode>();
var sourceItem = itemSlotNode.Item;
if (sourceItem == null)
{
return false;
}
return Item.MergeableItemCount(sourceItem, sourceItem.Quantity) > 0;
}
}
public override void _DropData(Vector2 atPosition, Variant data)
@ -63,25 +81,20 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay
var type = data.VariantType;
if (type == Variant.Type.Nil)
{
//The passed variable is null.
//传入的变量为null。
return;
}
var itemSlotNode = data.As<ItemSlotNode>();
var sourceItem = itemSlotNode.Item;
if (sourceItem == null)
{
//Return null when trying to get the source item.
//尝试获取源物品时返回null。
return;
}
if (Item is null || Item is PlaceholderItem)
{
sourceItem.ItemContainer?.RemoveItem(sourceItem, -1);
Item?.ItemContainer?.ReplaceItem(Item, sourceItem);
}
}
/// <summary>
/// <para>Whether to place a backpack in the current slot</para>
/// <para>当前槽位是否允许放置背包</para>
/// </summary>
public bool BackpackAllowed { get; set; }
private void UpdateBackground(bool isSelect)

View File

@ -15,6 +15,7 @@ public class PlaceholderItem : IItem
public int Quantity { get; set; } = 1;
public int MaxQuantity { get; } = 1;
public bool IsSelect { get; set; }
public IItemContainer? ItemContainer { get; set; }
public int MergeableItemCount(IItem other, int unallocatedQuantity)
{

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using ColdMint.scripts.debug;
using ColdMint.scripts.map.events;
namespace ColdMint.scripts.inventory;
@ -87,7 +86,6 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
_nextAvailableIndex = UnknownIndex;
if (totalCapacity <= 0)
{
LogCat.Log("Next available item"+_nextAvailableIndex);
return;
}
for (var i = 0; i < totalCapacity; i++)
@ -96,7 +94,6 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
if (!contains)
{
_nextAvailableIndex = i;
LogCat.Log("Next available item"+_nextAvailableIndex);
return;
}
}
@ -113,6 +110,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
var nextAvailableIndex = _nextAvailableIndex;
_itemDictionary[nextAvailableIndex] = item;
item.ItemContainer = this;
UpdateNextAvailableIndex();
UpdateSelectStatus(nextAvailableIndex, item);
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
@ -171,6 +169,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
}
var finalNextAvailableIndex = _nextAvailableIndex;
_itemDictionary[finalNextAvailableIndex] = item;
item.ItemContainer = this;
UpdateNextAvailableIndex();
UpdateSelectStatus(finalNextAvailableIndex, item);
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
@ -205,6 +204,27 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
return _itemDictionary.TryGetValue(index, out var item) ? item : null;
}
public bool ReplaceItem(int index, IItem item)
{
var oldItem = GetItem(index);
_itemDictionary[index] = item;
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
{
NewItem = item,
NewIndex = index,
OldIndex = index,
OldItem = oldItem,
Type = Config.ItemDataChangeEventType.Replace
});
return true;
}
public bool ReplaceItem(IItem oldItem, IItem newItem)
{
var index = GetIndexByItem(oldItem);
return index != UnknownIndex && ReplaceItem(index, newItem);
}
public int RemoveSelectItem(int number)
{
return RemoveItem(_selectIndex, number);
@ -256,6 +276,37 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
return removed;
}
public int RemoveItem(IItem item, int number)
{
var index = GetIndexByItem(item);
return index == UnknownIndex ? 0 : RemoveItem(index, number);
}
/// <summary>
/// <para>Find the corresponding index based on the item object</para>
/// <para>根据物品对象查找对应的索引</para>
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
private int GetIndexByItem(IItem item)
{
if (totalCapacity <= 0)
{
return UnknownIndex;
}
for (var i = 0; i < totalCapacity; i++)
{
var contains = _itemDictionary.ContainsKey(i);
if (!contains) continue;
if (item == _itemDictionary[i])
{
return i;
}
}
return UnknownIndex;
}
public int GetUsedCapacity()
{
return _itemDictionary.Count;

View File

@ -78,6 +78,7 @@ public partial class PickAbleTemplate : RigidBody2D, IItem
public int MaxQuantity { get; set; } = 1;
public bool IsSelect { get; set; }
public IItemContainer? ItemContainer { get; set; }
private Label? _tipLabel;