cirnogodot/Scripts/UI/InventoryMenu.cs

148 lines
3.6 KiB
C#
Raw Permalink Normal View History

2025-03-13 13:29:13 +01:00
using System.Collections.Generic;
using System.Linq;
2025-12-30 15:27:37 +01:00
using System.Threading.Tasks;
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";
2025-12-30 15:27:37 +01:00
[Export]
public Control ParentContainer { get; private set; }
2025-03-13 13:29:13 +01:00
public override void _Ready()
{
CallDeferred(MethodName.DeferredInitialize);
2025-12-30 15:27:37 +01:00
HideCommand();
}
private void ShowCommand()
{
ParentContainer?.Show();
this.Show();
}
private void HideCommand()
{
if (ParentContainer is not null)
{
ParentContainer.Hide();
this.Show();
}
else
{
this.Hide();
}
}
private new bool IsVisible()
{
return ParentContainer?.Visible ?? Visible;
2025-03-13 13:29:13 +01:00
}
public override void _Process(double delta)
{
2025-12-30 15:27:37 +01:00
if (Input.IsActionJustPressed(InventoryActionName) ||
Input.IsActionJustPressed(PauseActionName) ||
Input.IsActionJustPressed(CancelActionName))
2025-03-13 13:29:13 +01:00
{
2025-12-30 15:27:37 +01:00
if (IsVisible())
2025-03-13 13:29:13 +01:00
{
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:
2025-12-30 15:27:37 +01:00
//CallDeferred(MethodName.ShowInventory);
ShowInventory();
2025-06-12 18:03:55 +02:00
break;
default:
2025-12-30 15:27:37 +01:00
HideInventory();
//_ = UnpauseAsync();
// _ = new Task(async () =>
// {
// await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
// HideInventory();
// });
//CallDeferred(MethodName.HideInventory);
2025-06-12 18:03:55 +02:00
//HideInventory();
break;
}
};
}
2025-12-30 15:27:37 +01:00
}
private async Task UnpauseAsync()
{
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
HideInventory();
2025-03-13 13:29:13 +01:00
}
private void HideInventory()
{
2025-12-30 15:27:37 +01:00
if (!IsVisible()) return;
HideCommand();
2025-03-13 13:29:13 +01:00
foreach (var menu in _itemMenus)
{
menu.Empty();
}
}
private void ShowInventory()
{
2025-12-30 15:27:37 +01:00
if (IsVisible()) return;
ShowCommand();
2025-03-13 13:29:13 +01:00
foreach (var menu in _itemMenus)
{
menu.Fill();
}
_itemMenus.FirstOrDefault()?.GrabFocus();
}
}