SupaLidlGame/UI/Inventory/ShopMenu.cs

101 lines
2.3 KiB
C#
Raw Normal View History

2024-05-30 22:07:37 -07:00
using Godot;
2024-06-01 14:21:34 -07:00
using System.Collections.Generic;
2024-05-30 22:07:37 -07:00
namespace SupaLidlGame.UI.Inventory;
public partial class ShopMenu : Control, IModal
{
private Items.IItemCollection<Items.ShopEntry> _source;
public Items.IItemCollection<Items.ShopEntry> Source
{
get => _source;
set
{
_source = value;
_inventoryGrid.Source = value;
}
}
[Export]
private InventoryGrid _inventoryGrid;
2024-06-01 14:21:34 -07:00
private InventorySlot _selected;
2024-06-04 11:35:47 -07:00
public void ShowModal()
{
Show();
var animPlayer = GetNode<AnimationPlayer>("%AnimationPlayer");
animPlayer.Play("open");
}
2024-05-30 22:07:37 -07:00
public void HideModal()
{
Hide();
_source = null;
}
2024-06-04 11:35:47 -07:00
public async void Close()
2024-05-30 22:07:37 -07:00
{
2024-06-04 11:35:47 -07:00
var animPlayer = GetNode<AnimationPlayer>("%AnimationPlayer");
animPlayer.Play("close");
await ToSignal(animPlayer, AnimationPlayer.SignalName.AnimationFinished);
HideModal();
}
2024-06-01 14:21:34 -07:00
2024-06-04 11:35:47 -07:00
public override void _Ready()
{
2024-06-01 14:21:34 -07:00
_inventoryGrid.SlotFocused += (InventorySlot slot) =>
{
if (slot.Item is not null)
{
SetTooltipItem(slot);
}
};
_inventoryGrid.SlotUnfocused += (InventorySlot slot) =>
{
SetTooltipItem(_selected);
};
_inventoryGrid.SlotSelected += (InventorySlot slot) =>
{
_selected = slot;
SetTooltipItem(slot);
GetNode<Button>("%BuyButton").GrabFocus();
};
2024-06-04 11:35:47 -07:00
GetNode<Button>("%BuyButton").GuiInput += (InputEvent @event) =>
2024-06-01 14:21:34 -07:00
{
2024-06-04 11:35:47 -07:00
if (@event.IsActionPressed("ui_cancel"))
2024-06-01 14:21:34 -07:00
{
2024-06-04 11:35:47 -07:00
GetViewport().SetInputAsHandled();
2024-06-01 14:21:34 -07:00
_selected?.GrabFocus();
}
};
}
private void SetTooltipItem(InventorySlot slot)
{
GetNode<ItemTooltip>("%ItemTooltip").Item = slot?.Item;
if (slot == _selected)
{
GetNode<Button>("%BuyButton").Disabled = false;
}
else
{
GetNode<Button>("%BuyButton").Disabled = true;
}
2024-05-30 22:07:37 -07:00
}
2024-06-04 11:35:47 -07:00
public override void _UnhandledInput(InputEvent @event)
{
if (@event.IsActionPressed("ui_cancel"))
{
GetViewport().SetInputAsHandled();
Close();
}
}
2024-05-30 22:07:37 -07:00
}