SupaLidlGame/UI/Inventory/InventorySlot.cs

96 lines
2.2 KiB
C#
Raw Permalink Normal View History

2023-12-31 05:59:33 -08:00
using Godot;
using GodotUtilities;
using GodotUtilities.SourceGenerators;
2024-05-29 14:55:23 -07:00
namespace SupaLidlGame.UI.Inventory;
2023-12-31 05:59:33 -08:00
2024-06-01 14:21:34 -07:00
public partial class InventorySlot : Button
2023-12-31 05:59:33 -08:00
{
private bool _isSelected = false;
2023-12-31 05:59:33 -08:00
public bool IsSelected
{
get => _isSelected;
set
{
_isSelected = value;
if (_selectedFrame is not null)
{
2024-06-01 14:21:34 -07:00
//_selectedFrame.Visible = _isSelected;
//_frame.Visible = !_isSelected;
}
}
}
2024-06-01 14:21:34 -07:00
[Export]
public bool UseFocusAsSelected { get; set; } = true;
private TextureRect _textureRect;
private NinePatchRect _frame;
2023-12-31 05:59:33 -08:00
private NinePatchRect _selectedFrame;
private static Texture2D _placeholderTexture;
2023-12-31 05:59:33 -08:00
private Items.ItemMetadata _item;
public Items.ItemMetadata Item
{
get => _item;
set
{
_item = value;
if (_item is null)
{
2024-06-01 14:21:34 -07:00
//_textureRect.Texture = null;
Icon = null;
2023-12-31 05:59:33 -08:00
}
else
{
2024-06-01 14:21:34 -07:00
//_textureRect.Texture = _item.Icon;
Icon = _item.Icon;
2023-12-31 05:59:33 -08:00
}
}
}
static InventorySlot()
{
_placeholderTexture = ResourceLoader.Load<Texture2D>(
"res://Assets/Sprites/UI/hotbar-inactive.png");
}
public override void _Ready()
2023-12-31 05:59:33 -08:00
{
_textureRect = GetNode<TextureRect>("TextureRect");
_frame = GetNode<NinePatchRect>("Frame");
_selectedFrame = GetNode<NinePatchRect>("SelectedFrame");
2024-06-01 14:21:34 -07:00
if (Item is null)
{
// do this to reset the icon
Item = null;
}
if (UseFocusAsSelected)
{
void focusEntered()
{
IsSelected = true;
}
void focusExited()
{
IsSelected = false;
}
Connect(SignalName.FocusEntered, Callable.From(focusEntered));
Connect(SignalName.FocusExited, Callable.From(focusExited));
Connect(SignalName.MouseEntered, Callable.From(focusEntered));
Connect(SignalName.MouseExited, Callable.From(focusExited));
}
2023-12-31 05:59:33 -08:00
}
}