Traveller/scripts/loader/uiLoader/PacksackUi.cs
Cold-Mint adeda2f786
Support display backpack page.
支持展示背包页面。
2024-06-16 20:18:44 +08:00

112 lines
2.7 KiB
C#

using ColdMint.scripts.debug;
using ColdMint.scripts.inventory;
using ColdMint.scripts.utils;
using Godot;
namespace ColdMint.scripts.loader.uiLoader;
/// <summary>
/// <para>Backpack UI</para>
/// <para>背包UI</para>
/// </summary>
public partial class PacksackUi : UiLoaderTemplate
{
private IItemContainer? _itemContainer;
private PackedScene? _packedScene;
private GridContainer? _gridContainer;
private Label? _titleLabel;
private string? _title;
private Button? _exitButton;
/// <summary>
/// <para>title</para>
/// <para>标题</para>
/// </summary>
public string? Title
{
get => _title;
set
{
_title = value;
SetTile(value);
}
}
/// <summary>
/// <para>Packsack</para>
/// <para>背包</para>
/// </summary>
public IItemContainer? ItemContainer
{
get => _itemContainer;
set
{
_itemContainer = value;
PlaceItemSlot(value);
}
}
/// <summary>
/// <para>Place item slots according to item information</para>
/// <para>根据物品信息放置物品槽</para>
/// </summary>
/// <param name="itemContainer"></param>
private void PlaceItemSlot(IItemContainer? itemContainer)
{
if (_gridContainer == null || itemContainer == null)
{
return;
}
NodeUtils.DeleteAllChild(_gridContainer);
foreach (var itemSlotNode in itemContainer)
{
itemSlotNode.Reparent(_gridContainer);
}
}
/// <summary>
/// <para>SetTile</para>
/// <para>设置标题</para>
/// </summary>
/// <param name="title"></param>
private void SetTile(string? title)
{
if (_titleLabel == null)
{
return;
}
_titleLabel.Text = title;
}
public override void InitializeData()
{
_packedScene = GD.Load<PackedScene>("res://prefab/ui/ItemSlot.tscn");
}
public override void InitializeUi()
{
_gridContainer = GetNode<GridContainer>("GridContainer");
_titleLabel = GetNode<Label>("TitleLabel");
_exitButton = GetNode<Button>("ExitButton");
_gridContainer.Columns = Config.HotBarSize;
//If the item container was set before this node was placed in the node tree, load it here.
//若物品容器在此节点放置到节点树之前被设置了,那么在这里加载。
PlaceItemSlot(_itemContainer);
SetTile(_title);
}
public override void LoadUiActions()
{
if (_exitButton != null)
{
_exitButton.Pressed += Hide;
}
}
}