cirnogodot/Scripts/UI/InventoryMenu.cs

105 lines
2.6 KiB
C#
Raw Permalink Normal View History

2025-03-13 13:29:13 +01:00
using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts.Utils;
2025-03-13 13:29:13 +01:00
using Godot;
namespace Cirno.Scripts.UI;
public partial class InventoryMenu : TabContainer
{
private List<ItemsMenu> _itemMenus = [];
[Export]
public StringName InventoryActionName { get; private set; } = "inventory";
[Export]
public StringName PauseActionName { get; private set; } = "pause";
[Export]
public StringName CancelActionName { get; private set; } = "ui_cancel";
public override void _Ready()
{
CallDeferred(MethodName.DeferredInitialize);
this.Hide();
}
public override void _Process(double delta)
{
2025-03-13 14:20:30 +01:00
if (Input.IsActionJustPressed(InventoryActionName) || Input.IsActionJustPressed(PauseActionName) || Input.IsActionJustPressed(CancelActionName))
2025-03-13 13:29:13 +01:00
{
if (Visible)
{
GameStateManager.Instance.ChangeState(GameState.Playing);
2025-03-13 14:20:30 +01:00
//CallDeferred(MethodName.HideInventory);
2025-03-13 13:29:13 +01:00
}
// else
// {
// ShowInventory();
// }
}
}
private void DeferredInitialize()
{
var children = this.GetChildren();
foreach (var child in children)
{
if (child is ItemsMenu menu)
{
_itemMenus.Add(menu);
menu.Init(this);
}
}
//Clear();
//ItemActivated += OnItemSelected;
2025-06-12 18:03:55 +02:00
// TODO: Move this on the game manager/controller side
if (GameStateManager.Instance is not null)
2025-03-13 13:29:13 +01:00
{
GameStateManager.Instance.GameStateChange += state =>
2025-03-13 13:29:13 +01:00
{
2025-06-12 18:03:55 +02:00
switch (state)
{
case GameState.Inventory:
CallDeferred(MethodName.ShowInventory);
break;
default:
CallDeferred(MethodName.HideInventory);
//HideInventory();
break;
}
};
}
2025-03-13 13:29:13 +01:00
}
private void HideInventory()
{
if (!Visible) return;
GD.Print("Hiding inventory");
this.Hide();
foreach (var menu in _itemMenus)
{
menu.Empty();
}
//_itemsDic.Clear();
//GameManager.Instance.ChangeState(GameState.Playing);
}
private void ShowInventory()
{
if (Visible) return;
this.Show();
foreach (var menu in _itemMenus)
{
menu.Fill();
}
_itemMenus.FirstOrDefault()?.GrabFocus();
}
}