2024-05-24 14:58:52 +00:00
|
|
|
|
using ColdMint.scripts.utils;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
using Godot;
|
|
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.inventory;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>A slot in the inventory</para>
|
|
|
|
|
/// <para>物品栏内的一个插槽</para>
|
|
|
|
|
/// </summary>
|
2024-09-22 15:13:59 +00:00
|
|
|
|
public partial class ItemSlotNode : MarginContainer, IItemDisplay
|
2024-04-28 13:55:19 +00:00
|
|
|
|
{
|
2024-05-08 10:22:04 +00:00
|
|
|
|
private TextureRect? _backgroundTextureRect;
|
|
|
|
|
private TextureRect? _iconTextureRect;
|
|
|
|
|
private Label? _quantityLabel;
|
|
|
|
|
private Control? _control;
|
|
|
|
|
private Texture2D? _backgroundTexture;
|
|
|
|
|
private Texture2D? _backgroundTextureWhenSelect;
|
2024-09-25 15:01:52 +00:00
|
|
|
|
public IItem? Item { get; private set; }
|
|
|
|
|
|
2024-06-21 14:55:31 +00:00
|
|
|
|
|
2024-06-18 15:37:18 +00:00
|
|
|
|
public override void _Ready()
|
|
|
|
|
{
|
|
|
|
|
_backgroundTexture = GD.Load<Texture2D>("res://sprites/ui/ItemBarEmpty.png");
|
|
|
|
|
_backgroundTextureWhenSelect = GD.Load<Texture2D>("res://sprites/ui/ItemBarFocus.png");
|
|
|
|
|
_backgroundTextureRect =
|
|
|
|
|
GetNode<TextureRect>("BackgroundTexture");
|
|
|
|
|
_iconTextureRect = GetNode<TextureRect>("BackgroundTexture/IconTextureRect");
|
|
|
|
|
_quantityLabel = GetNode<Label>("Control/QuantityLabel");
|
|
|
|
|
_control = GetNode<Control>("Control");
|
|
|
|
|
_quantityLabel.Hide();
|
|
|
|
|
}
|
2024-05-06 10:59:39 +00:00
|
|
|
|
|
2024-06-17 14:12:51 +00:00
|
|
|
|
public override Variant _GetDragData(Vector2 atPosition)
|
|
|
|
|
{
|
2024-09-27 15:24:22 +00:00
|
|
|
|
switch (Item)
|
|
|
|
|
{
|
|
|
|
|
case null:
|
|
|
|
|
return Config.EmptyVariant;
|
|
|
|
|
case PlaceholderItem:
|
|
|
|
|
return Config.EmptyVariant;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-23 13:03:39 +00:00
|
|
|
|
if (_iconTextureRect == null)
|
2024-06-17 14:12:51 +00:00
|
|
|
|
{
|
2024-09-27 15:24:22 +00:00
|
|
|
|
return Config.EmptyVariant;
|
2024-06-17 14:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var textureRect = new TextureRect();
|
|
|
|
|
textureRect.ExpandMode = _iconTextureRect.ExpandMode;
|
|
|
|
|
textureRect.Size = _iconTextureRect.Size;
|
|
|
|
|
textureRect.Texture = _iconTextureRect.Texture;
|
2024-10-01 12:53:56 +00:00
|
|
|
|
textureRect.ZIndex = Config.ZIndexManager.FloatingIcon;
|
2024-06-17 14:12:51 +00:00
|
|
|
|
SetDragPreview(textureRect);
|
2024-06-18 15:37:18 +00:00
|
|
|
|
return Variant.CreateFrom(this);
|
2024-06-17 14:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool _CanDropData(Vector2 atPosition, Variant data)
|
|
|
|
|
{
|
2024-09-27 15:24:22 +00:00
|
|
|
|
var type = data.VariantType;
|
|
|
|
|
if (type == Variant.Type.Nil)
|
2024-06-17 14:12:51 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-09-29 14:12:54 +00:00
|
|
|
|
var itemSlotNode = data.As<ItemSlotNode>();
|
|
|
|
|
var sourceItem = itemSlotNode.Item;
|
|
|
|
|
if (sourceItem == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-09-27 15:24:22 +00:00
|
|
|
|
switch (Item)
|
2024-06-18 15:37:18 +00:00
|
|
|
|
{
|
2024-09-27 15:24:22 +00:00
|
|
|
|
case null:
|
|
|
|
|
return true;
|
2024-09-29 14:12:54 +00:00
|
|
|
|
case PlaceholderItem placeholderItem:
|
|
|
|
|
var placeholderItemContainer = placeholderItem.ItemContainer;
|
|
|
|
|
if (placeholderItemContainer == null)
|
2024-09-27 15:24:22 +00:00
|
|
|
|
{
|
2024-09-29 14:12:54 +00:00
|
|
|
|
return true;
|
2024-09-27 15:24:22 +00:00
|
|
|
|
}
|
2024-09-29 14:12:54 +00:00
|
|
|
|
return placeholderItemContainer.CanReplaceItem(placeholderItem.Index, sourceItem);
|
|
|
|
|
default:
|
2024-09-29 15:30:42 +00:00
|
|
|
|
var sourceItemSelfContainer = sourceItem.SelfItemContainer;
|
|
|
|
|
if (sourceItemSelfContainer != null)
|
|
|
|
|
{
|
|
|
|
|
//Place the container on the item.
|
|
|
|
|
//将容器放在物品上。
|
|
|
|
|
return sourceItemSelfContainer.CanAddItem(Item);
|
|
|
|
|
}
|
|
|
|
|
var itemSelfContainer = Item.SelfItemContainer;
|
|
|
|
|
if (itemSelfContainer != null)
|
|
|
|
|
{
|
|
|
|
|
//Drag the item onto the container.
|
|
|
|
|
//将物品拖到容器上。
|
|
|
|
|
return itemSelfContainer.CanAddItem(sourceItem);
|
|
|
|
|
}
|
2024-09-27 15:24:22 +00:00
|
|
|
|
return Item.MergeableItemCount(sourceItem, sourceItem.Quantity) > 0;
|
2024-06-18 15:37:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void _DropData(Vector2 atPosition, Variant data)
|
|
|
|
|
{
|
2024-09-28 14:59:25 +00:00
|
|
|
|
//The item is empty and the corresponding item container cannot be retrieved.
|
|
|
|
|
//物品为空,无法获取对应的物品容器。
|
|
|
|
|
if (Item is null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-06-18 15:37:18 +00:00
|
|
|
|
var type = data.VariantType;
|
|
|
|
|
if (type == Variant.Type.Nil)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var itemSlotNode = data.As<ItemSlotNode>();
|
2024-09-25 15:01:52 +00:00
|
|
|
|
var sourceItem = itemSlotNode.Item;
|
2024-07-03 15:31:31 +00:00
|
|
|
|
if (sourceItem == null)
|
2024-06-18 15:37:18 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-09-28 14:59:25 +00:00
|
|
|
|
|
2024-09-29 15:30:42 +00:00
|
|
|
|
if (Item.SelfItemContainer != null)
|
|
|
|
|
{
|
|
|
|
|
//Use items and place them on the container.
|
|
|
|
|
//用物品,在物品容器上放置。
|
|
|
|
|
var oldIndex = sourceItem.Index;
|
|
|
|
|
var oldItemContainer = sourceItem.ItemContainer;
|
|
|
|
|
var addNumber = Item.SelfItemContainer.AddItem(sourceItem);
|
|
|
|
|
if (addNumber >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (addNumber == sourceItem.Quantity)
|
|
|
|
|
{
|
|
|
|
|
oldItemContainer?.ClearItem(oldIndex);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
oldItemContainer?.RemoveItem(oldIndex, addNumber);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sourceItem.SelfItemContainer != null)
|
|
|
|
|
{
|
|
|
|
|
//Use containers and place on top of items.
|
|
|
|
|
//用容器物品,在物品上放置。
|
|
|
|
|
var oldIndex = Item.Index;
|
|
|
|
|
var oldItemContainer = Item.ItemContainer;
|
|
|
|
|
var addNumber = sourceItem.SelfItemContainer.AddItem(Item);
|
|
|
|
|
if (addNumber >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (addNumber == Item.Quantity)
|
|
|
|
|
{
|
|
|
|
|
oldItemContainer?.ClearItem(oldIndex);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
oldItemContainer?.RemoveItem(oldIndex, addNumber);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-28 14:59:25 +00:00
|
|
|
|
if (Item is PlaceholderItem placeholderItem)
|
2024-09-27 15:24:22 +00:00
|
|
|
|
{
|
2024-09-28 14:59:25 +00:00
|
|
|
|
var placeholderItemContainer = placeholderItem.ItemContainer;
|
|
|
|
|
var sourceItemContainer = sourceItem.ItemContainer;
|
|
|
|
|
var sourceItemIndex = sourceItem.Index;
|
2024-09-29 14:12:54 +00:00
|
|
|
|
var replaceResult = false;
|
2024-09-28 14:59:25 +00:00
|
|
|
|
if (placeholderItemContainer != null)
|
|
|
|
|
{
|
2024-09-29 14:12:54 +00:00
|
|
|
|
replaceResult = placeholderItemContainer.ReplaceItem(placeholderItem.Index, sourceItem);
|
2024-09-28 14:59:25 +00:00
|
|
|
|
}
|
2024-09-29 14:12:54 +00:00
|
|
|
|
if (replaceResult && sourceItemContainer != null)
|
2024-09-28 14:59:25 +00:00
|
|
|
|
{
|
|
|
|
|
sourceItemContainer.ClearItem(sourceItemIndex);
|
|
|
|
|
}
|
2024-09-27 15:24:22 +00:00
|
|
|
|
}
|
2024-06-17 14:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 10:59:39 +00:00
|
|
|
|
|
2024-06-26 15:18:50 +00:00
|
|
|
|
private void UpdateBackground(bool isSelect)
|
|
|
|
|
{
|
|
|
|
|
if (_backgroundTextureRect == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_backgroundTextureRect.Texture = isSelect ? _backgroundTextureWhenSelect : _backgroundTexture;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-08 10:22:04 +00:00
|
|
|
|
public TextureRect? BackgroundTextureRect => _backgroundTextureRect;
|
2024-05-06 10:59:39 +00:00
|
|
|
|
|
2024-06-12 09:57:38 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Update all displays of this slot</para>
|
|
|
|
|
/// <para>更新该槽位的一切显示信息</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void UpdateAllDisplay()
|
|
|
|
|
{
|
|
|
|
|
UpdateIconTexture();
|
|
|
|
|
UpdateQuantityLabel();
|
|
|
|
|
UpdateTooltipText();
|
2024-05-06 10:59:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Update item tips</para>
|
|
|
|
|
/// <para>更新物品的提示内容</para>
|
|
|
|
|
/// </summary>
|
2024-06-12 09:57:38 +00:00
|
|
|
|
private void UpdateTooltipText()
|
2024-05-06 10:59:39 +00:00
|
|
|
|
{
|
2024-09-27 13:16:00 +00:00
|
|
|
|
if (Item is PlaceholderItem or null)
|
2024-05-08 10:22:04 +00:00
|
|
|
|
{
|
2024-06-18 15:37:18 +00:00
|
|
|
|
TooltipText = null;
|
2024-05-08 10:22:04 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 10:59:39 +00:00
|
|
|
|
if (Config.IsDebug())
|
|
|
|
|
{
|
2024-05-24 14:58:52 +00:00
|
|
|
|
var debugText = TranslationServerUtils.Translate("item_prompt_debug");
|
|
|
|
|
if (debugText != null)
|
|
|
|
|
{
|
2024-09-25 15:01:52 +00:00
|
|
|
|
TooltipText = string.Format(debugText, Item.Id,
|
|
|
|
|
TranslationServerUtils.Translate(Item.Name),
|
|
|
|
|
Item.Quantity, Item.MaxQuantity, Item.GetType().Name,
|
|
|
|
|
TranslationServerUtils.Translate(Item.Description));
|
2024-05-24 14:58:52 +00:00
|
|
|
|
}
|
2024-05-06 10:59:39 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-09-25 15:01:52 +00:00
|
|
|
|
TooltipText = TranslationServerUtils.Translate(Item.Name) + "\n" +
|
|
|
|
|
TranslationServerUtils.Translate(Item.Description);
|
2024-05-06 10:59:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Update quantity label</para>
|
|
|
|
|
/// <para>更新数量标签</para>
|
|
|
|
|
/// </summary>
|
2024-06-12 09:57:38 +00:00
|
|
|
|
private void UpdateQuantityLabel()
|
2024-05-06 10:59:39 +00:00
|
|
|
|
{
|
2024-05-08 10:22:04 +00:00
|
|
|
|
if (_quantityLabel == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 13:16:00 +00:00
|
|
|
|
if (Item is PlaceholderItem or null)
|
|
|
|
|
{
|
|
|
|
|
_quantityLabel.Hide();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 15:01:52 +00:00
|
|
|
|
switch (Item?.Quantity)
|
2024-05-06 10:59:39 +00:00
|
|
|
|
{
|
2024-06-12 09:57:38 +00:00
|
|
|
|
case null or 1:
|
2024-06-08 15:59:24 +00:00
|
|
|
|
_quantityLabel.Hide();
|
2024-05-06 10:59:39 +00:00
|
|
|
|
return;
|
|
|
|
|
default:
|
2024-06-12 09:57:38 +00:00
|
|
|
|
//When the quantity is not null or 1, we display the quantity.
|
|
|
|
|
//当数量不为null或1时,我们显示数量
|
2024-09-25 15:01:52 +00:00
|
|
|
|
_quantityLabel.Text = Item?.Quantity.ToString();
|
2024-06-12 09:57:38 +00:00
|
|
|
|
_quantityLabel.Show();
|
2024-05-06 10:59:39 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 15:37:18 +00:00
|
|
|
|
|
2024-06-12 09:57:38 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Update texture of the icon rect</para>
|
|
|
|
|
/// <para>更新显示的物品图标</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void UpdateIconTexture()
|
|
|
|
|
{
|
|
|
|
|
if (_iconTextureRect != null)
|
|
|
|
|
{
|
2024-09-25 15:01:52 +00:00
|
|
|
|
_iconTextureRect.Texture = Item?.Icon;
|
2024-06-21 11:16:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 15:13:59 +00:00
|
|
|
|
public void Update(IItem? item)
|
|
|
|
|
{
|
2024-09-27 13:16:00 +00:00
|
|
|
|
Item = item;
|
|
|
|
|
UpdateAllDisplay();
|
2024-09-23 13:03:39 +00:00
|
|
|
|
UpdateBackground(item is { IsSelect: true });
|
2024-06-21 11:16:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 15:13:59 +00:00
|
|
|
|
public void ShowSelf()
|
2024-06-21 11:16:40 +00:00
|
|
|
|
{
|
2024-09-22 15:13:59 +00:00
|
|
|
|
Show();
|
|
|
|
|
}
|
2024-06-25 14:11:19 +00:00
|
|
|
|
|
2024-09-22 15:13:59 +00:00
|
|
|
|
public void HideSelf()
|
|
|
|
|
{
|
|
|
|
|
Hide();
|
2024-06-21 11:16:40 +00:00
|
|
|
|
}
|
2024-05-06 10:59:39 +00:00
|
|
|
|
}
|