2024-06-18 15:37:18 +00:00
using System ;
2024-06-11 18:22:04 +00:00
using ColdMint.scripts.item ;
2024-06-13 05:53:10 +00:00
using ColdMint.scripts.item.itemStacks ;
2024-06-18 15:37:18 +00:00
using ColdMint.scripts.map.events ;
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>
public partial class ItemSlotNode : MarginContainer
{
2024-06-11 18:22:04 +00:00
private IItemStack ? _itemStack ;
2024-05-08 10:22:04 +00:00
private TextureRect ? _backgroundTextureRect ;
private TextureRect ? _iconTextureRect ;
private Label ? _quantityLabel ;
private Control ? _control ;
2024-05-06 10:59:39 +00:00
private bool _isSelect ;
2024-05-08 10:22:04 +00:00
private Texture2D ? _backgroundTexture ;
private Texture2D ? _backgroundTextureWhenSelect ;
2024-06-18 15:37:18 +00:00
public Action < ItemStackChangeEvent > ? ItemStackChangeEvent ;
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-06-18 15:37:18 +00:00
if ( _iconTextureRect = = null | | _itemStack = = null )
2024-06-17 14:12:51 +00:00
{
2024-06-18 15:37:18 +00:00
//Drag is not allowed if there is no icon or no pile of items.
//如果没有图标或者没有物品堆,那么不允许拖动。
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-06-18 15:37:18 +00:00
//If the preplaced slot does not have an icon, the preplaced slot is not allowed.
//如果预放置的槽位没有图标,那么不允许放置。
2024-06-17 14:12:51 +00:00
if ( _iconTextureRect = = null )
{
return false ;
}
2024-06-18 15:37:18 +00:00
var type = data . VariantType ;
if ( type = = Variant . Type . Nil )
{
//The preplaced data is null.
//预放置的数据为null。
return false ;
}
var itemSlotNode = data . As < ItemSlotNode > ( ) ;
var itemStack = itemSlotNode . GetItemStack ( ) ;
if ( itemStack = = null )
{
//Return null when trying to get the source item heap.
//尝试获取源物品堆时返回null。
return false ;
}
//TODO: This is where we infer whether the two piles can merge.在这里推断两个物品堆是否可以融合。
return _itemStack = = null ;
}
public override void _DropData ( Vector2 atPosition , Variant data )
{
if ( _iconTextureRect = = null )
{
return ;
}
var type = data . VariantType ;
if ( type = = Variant . Type . Nil )
{
//The passed variable is null.
//传入的变量为null。
return ;
}
var itemSlotNode = data . As < ItemSlotNode > ( ) ;
var itemStack = itemSlotNode . ReplaceItemStack ( null ) ;
if ( itemStack = = null )
{
//Return null when trying to get the source item heap.
//尝试获取源物品堆时返回null。
return ;
}
ReplaceItemStack ( itemStack ) ;
2024-06-17 14:12:51 +00:00
}
2024-05-06 10:59:39 +00:00
public bool IsSelect
{
get = > _isSelect ;
set
{
2024-05-08 10:22:04 +00:00
if ( _backgroundTextureRect ! = null )
2024-05-06 10:59:39 +00:00
{
2024-05-08 10:22:04 +00:00
_backgroundTextureRect . Texture = value ? _backgroundTextureWhenSelect : _backgroundTexture ;
2024-05-06 10:59:39 +00:00
}
_isSelect = value ;
}
}
2024-05-08 10:22:04 +00:00
public TextureRect ? BackgroundTextureRect = > _backgroundTextureRect ;
2024-05-06 10:59:39 +00:00
2024-06-12 17:51:51 +00:00
public bool IsEmpty ( ) = > _itemStack = = null ;
2024-05-06 10:59:39 +00:00
/// <summary>
2024-06-11 18:22:04 +00:00
/// <para>Get the item stack in the item slot</para>
/// <para>获取物品槽内的物品堆</para>
2024-05-06 10:59:39 +00:00
/// </summary>
/// <returns></returns>
2024-06-12 09:57:38 +00:00
public IItemStack ? GetItemStack ( ) = > _itemStack ;
/// <summary>
/// <para>If present, remove an item in this slot and return it.</para>
/// <para>如果存在,移除该槽位中的一个物品并将其返回</para>
/// </summary>
/// <seealso cref="PickItems"/>
2024-06-12 19:07:55 +00:00
public IItem ? PickItem ( )
2024-06-12 09:57:38 +00:00
{
if ( _itemStack is null ) return null ;
var result = _itemStack . PickItem ( ) ;
2024-06-18 15:37:18 +00:00
if ( _itemStack . Empty )
{
SetItemStack ( null ) ;
}
2024-06-12 19:04:12 +00:00
UpdateAllDisplay ( ) ;
2024-06-12 09:57:38 +00:00
return result ;
}
/// <summary>
/// <para>Remove the specified number of items and return them as a new item stack</para>
/// <para>取出当前物品槽中指定数量的物品,并作为新的物品堆返回</para>
/// </summary>
/// <param name="value">
/// <para>Quantity to be taken out, inputs below zero represent all items</para>
/// <para>要取出的数量, 小于0的输入代表全部物品</para>
/// </param>
/// <seealso cref="PickItem"/>
public IItemStack ? PickItems ( int value )
{
if ( _itemStack is null ) return null ;
var result = _itemStack . PickItems ( value ) ;
2024-06-18 15:37:18 +00:00
if ( _itemStack . Empty )
{
SetItemStack ( null ) ;
}
2024-06-12 19:04:12 +00:00
UpdateAllDisplay ( ) ;
2024-06-12 09:57:38 +00:00
return result ;
}
2024-05-06 10:59:39 +00:00
/// <summary>
/// <para>Removes the specified number of items from the item slot</para>
/// <para>在物品槽内移除指定数量的物品</para>
/// </summary>
2024-06-12 17:18:55 +00:00
/// <param name="number">
/// <para>Quantity to be removed, inputs below zero represent all items</para>
/// <para>要删除的数量, 小于0的输入代表全部物品</para>
/// </param>
2024-06-11 18:22:04 +00:00
/// <returns>
/// <para>The remaining number, if the number of items in the current item stack is less than the specified number. Otherwise,0</para>
/// <para>若物品槽内物品少于指定的数量, 返回相差的数量。否则返回0</para>
/// </returns>
2024-06-12 09:57:38 +00:00
/// <remarks>
2024-06-12 17:18:55 +00:00
/// <para>Will remove the removed items from the game, if that is not the intent, consider using the <see cref="PickItems"/></para>
/// <para>会将移除的物品从游戏中删除,如果目的并非如此,请考虑使用<see cref="PickItems"/></para>
2024-06-12 09:57:38 +00:00
/// </remarks>
2024-06-11 18:22:04 +00:00
public int RemoveItem ( int number )
2024-05-06 10:59:39 +00:00
{
2024-06-11 18:22:04 +00:00
if ( _itemStack = = null )
2024-05-06 10:59:39 +00:00
{
2024-06-11 18:22:04 +00:00
return number ;
2024-05-06 10:59:39 +00:00
}
2024-06-11 18:22:04 +00:00
var result = _itemStack . RemoveItem ( number ) ;
//If the specified number of items is removed, the number of items is less than or equal to 0. Then we empty the inventory.
//如果移除指定数量的物品后, 物品数量小于或等于0。那么我们清空物品栏。
2024-06-18 15:37:18 +00:00
if ( _itemStack . Empty )
{
SetItemStack ( null ) ;
}
2024-06-12 19:04:12 +00:00
UpdateAllDisplay ( ) ;
2024-06-11 18:22:04 +00:00
return result ;
2024-05-06 10:59:39 +00:00
}
2024-06-12 09:57:38 +00:00
/// <summary>
/// <para>Remove item stack from slot and return it, equivalent to ReplaceItemStack(null)</para>
/// <para>从当前槽位中移出并返回物品堆, 等价于ReplaceItemStack(null)</para>
/// <seealso cref="ReplaceItemStack"/>
/// </summary>
public IItemStack ? RemoveItemStack ( ) = > ReplaceItemStack ( null ) ;
2024-05-06 10:59:39 +00:00
/// <summary>
2024-06-11 18:22:04 +00:00
/// <para>Empty the item slot</para>
2024-06-12 09:57:38 +00:00
/// <para>清空当前物品槽</para>
2024-05-06 10:59:39 +00:00
/// </summary>
/// <remarks>
2024-06-12 09:57:38 +00:00
///<para>This method will remove all items stored in the item slots from the game, if this is not what you want to do, consider using the <see cref="RemoveItemStack"/> method.</para>
///<para>此方法会从游戏中移除储存于物品槽中的所有物品,若这不是您希望的操作,请考虑使用<see cref="RemoveItemStack"/>方法。</para>
2024-05-06 10:59:39 +00:00
/// </remarks>
2024-06-11 18:22:04 +00:00
public void ClearSlot ( )
2024-05-06 10:59:39 +00:00
{
2024-06-12 09:57:38 +00:00
_itemStack ? . ClearStack ( ) ;
2024-06-18 15:37:18 +00:00
SetItemStack ( null ) ;
2024-06-12 09:57:38 +00:00
UpdateAllDisplay ( ) ;
2024-05-06 10:59:39 +00:00
}
2024-06-12 09:57:38 +00:00
/// <summary>
/// <para>
/// Set item stack for this slot, this will completely replace current item stack.
/// If you want the item stack to be added to current stack, use the <see cref="AddItemStack"/>.
/// </para>
/// <para>为物品槽设置物品堆,将完全替换掉当前物品堆。如果想要物品堆叠加至该物品堆,请使用<see cref="AddItemStack"/></para>
/// </summary>
/// <returns>
/// <para>The item stack that was previously in this slot</para>
/// <para>该槽位中原本的物品堆</para>
/// </returns>
public IItemStack ? ReplaceItemStack ( IItemStack ? newItemStack )
2024-05-06 10:59:39 +00:00
{
2024-06-12 09:57:38 +00:00
var result = _itemStack ;
2024-06-18 15:37:18 +00:00
SetItemStack ( newItemStack ) ;
2024-06-12 09:57:38 +00:00
UpdateAllDisplay ( ) ;
return result ;
}
2024-06-14 04:22:44 +00:00
/// <summary>
/// <para>Can the specified item be placed in the item slot?</para>
/// <para>指定的物品是否可设置在物品槽内?</para>
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool CanAddItem ( IItem item )
{
if ( _itemStack = = null ) return true ;
return _itemStack . CanAddItem ( item ) ;
}
2024-06-12 09:57:38 +00:00
/// <summary>
/// <para>Try to add an item to this slot, if it can't be added to this slot, return false</para>
/// <para>尝试向当前槽位中加入物品, 如果该物品不能被放入该槽位, 返回false</para>
/// </summary>
2024-06-12 19:07:55 +00:00
public bool AddItem ( IItem item )
2024-06-12 09:57:38 +00:00
{
bool result ;
if ( _itemStack is null )
2024-05-06 10:59:39 +00:00
{
2024-06-18 15:37:18 +00:00
SetItemStack ( IItemStack . FromItem ( item ) ) ;
2024-06-12 09:57:38 +00:00
result = true ;
2024-05-06 10:59:39 +00:00
}
2024-06-12 09:57:38 +00:00
else
2024-05-06 10:59:39 +00:00
{
2024-06-12 09:57:38 +00:00
result = _itemStack . AddItem ( item ) ;
2024-05-06 10:59:39 +00:00
}
2024-06-12 09:57:38 +00:00
if ( result )
2024-05-06 10:59:39 +00:00
{
2024-06-12 09:57:38 +00:00
UpdateAllDisplay ( ) ;
2024-05-06 10:59:39 +00:00
}
2024-06-12 09:57:38 +00:00
return result ;
2024-05-06 10:59:39 +00:00
}
2024-06-14 04:22:44 +00:00
/// <summary>
/// <para>Determines the number of items that can be received from the specified pile</para>
/// <para>判断能从指定物品堆中接收的物品数量</para>
/// </summary>
/// <param name="itemStack">
/// <para>Item stack to add to the current slot</para>
/// <para>向该物品槽中放入物品的物品堆</para>
/// </param>
/// <returns></returns>
public int CanAddItemStack ( IItemStack itemStack )
{
if ( _itemStack is null ) return itemStack . Quantity ;
return _itemStack . CanTakeFrom ( itemStack ) ;
}
2024-05-06 10:59:39 +00:00
/// <summary>
2024-06-12 09:57:38 +00:00
/// <para>Try to combine an item stack into this slot</para>
/// <para>尝试将一个物品堆合并至该槽位中</para>
2024-05-06 10:59:39 +00:00
/// </summary>
2024-06-12 09:57:38 +00:00
/// <returns>
2024-06-12 17:18:55 +00:00
/// <para>If the source item stack is empty after the operation is completed</para>
/// <para>操作完成后,源物品堆是否被取空</para>
2024-06-12 09:57:38 +00:00
/// </returns>
2024-06-12 17:18:55 +00:00
public bool AddItemStack ( IItemStack itemStack )
2024-05-06 10:59:39 +00:00
{
2024-06-12 17:18:55 +00:00
bool result ;
2024-06-12 09:57:38 +00:00
if ( _itemStack is null )
2024-05-06 10:59:39 +00:00
{
2024-06-18 15:37:18 +00:00
SetItemStack ( itemStack ) ;
2024-06-12 17:18:55 +00:00
result = false ;
2024-05-06 10:59:39 +00:00
}
else
{
2024-06-12 09:57:38 +00:00
result = _itemStack . TakeFrom ( itemStack ) ;
2024-05-06 10:59:39 +00:00
}
2024-06-12 09:57:38 +00:00
UpdateAllDisplay ( ) ;
return result ;
}
/// <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-06-12 09:57:38 +00:00
if ( _itemStack = = 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-06-18 15:37:18 +00:00
TooltipText = string . Format ( debugText , _itemStack . GetItem ( ) ? . Id ,
2024-06-17 14:12:51 +00:00
TranslationServerUtils . Translate ( _itemStack . Name ) ,
_itemStack . Quantity , _itemStack . MaxQuantity , _itemStack . GetType ( ) . Name ,
TranslationServerUtils . Translate ( _itemStack . Description ) ) ;
2024-05-24 14:58:52 +00:00
}
2024-05-06 10:59:39 +00:00
}
else
{
2024-06-18 15:37:18 +00:00
TooltipText = TranslationServerUtils . Translate ( _itemStack . Name ) + "\n" +
TranslationServerUtils . Translate ( _itemStack . 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-06-12 09:57:38 +00:00
switch ( _itemStack ? . 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时, 我们显示数量
_quantityLabel . Text = _itemStack ? . Quantity . ToString ( ) ;
_quantityLabel . Show ( ) ;
2024-05-06 10:59:39 +00:00
break ;
}
}
2024-06-18 15:37:18 +00:00
/// <summary>
/// <para>SetItemStack</para>
/// <para>设置物品堆</para>
/// </summary>
/// <remarks>
///<para>This method broadcasts changes to the stack to the outside world</para>
///<para>此方法会对外广播物品堆的变更事件</para>
/// </remarks>
/// <param name="itemStack"></param>
private void SetItemStack ( IItemStack ? itemStack )
{
_itemStack = itemStack ;
var stackChangeEvent = new ItemStackChangeEvent
{
ItemStack = itemStack
} ;
ItemStackChangeEvent ? . Invoke ( stackChangeEvent ) ;
}
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 )
{
_iconTextureRect . Texture = _itemStack ? . Icon ;
}
}
2024-06-18 15:37:18 +00:00
2024-05-06 10:59:39 +00:00
}