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-23 13:03:39 +00:00
|
|
|
|
if (_iconTextureRect == null)
|
2024-06-17 14:12:51 +00:00
|
|
|
|
{
|
2024-06-18 15:37:18 +00:00
|
|
|
|
return new Variant();
|
2024-06-17 14:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var textureRect = new TextureRect();
|
|
|
|
|
textureRect.ExpandMode = _iconTextureRect.ExpandMode;
|
|
|
|
|
textureRect.Size = _iconTextureRect.Size;
|
|
|
|
|
textureRect.Texture = _iconTextureRect.Texture;
|
|
|
|
|
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-25 15:01:52 +00:00
|
|
|
|
if (Item == null)
|
2024-06-17 14:12:51 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-09-25 15:01:52 +00:00
|
|
|
|
if (Item is PlaceholderItem)
|
2024-06-18 15:37:18 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-09-25 15:01:52 +00:00
|
|
|
|
return true;
|
2024-06-18 15:37:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void _DropData(Vector2 atPosition, Variant data)
|
|
|
|
|
{
|
|
|
|
|
var type = data.VariantType;
|
|
|
|
|
if (type == Variant.Type.Nil)
|
|
|
|
|
{
|
|
|
|
|
//The passed variable is null.
|
|
|
|
|
//传入的变量为null。
|
|
|
|
|
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
|
|
|
|
{
|
2024-06-21 14:55:31 +00:00
|
|
|
|
//Return null when trying to get the source item.
|
|
|
|
|
//尝试获取源物品时返回null。
|
2024-06-18 15:37:18 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-06-17 14:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-25 14:11:19 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Whether to place a backpack in the current slot</para>
|
|
|
|
|
/// <para>当前槽位是否允许放置背包</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool BackpackAllowed { get; set; }
|
|
|
|
|
|
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
|
|
|
|
}
|