using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Cirno.Scripts.Utils; using Godot; namespace Cirno.Scripts.UI; public partial class InventoryMenu : TabContainer { private List _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"; [Export] public Control ParentContainer { get; private set; } public override void _Ready() { CallDeferred(MethodName.DeferredInitialize); 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; } public override void _Process(double delta) { if (Input.IsActionJustPressed(InventoryActionName) || Input.IsActionJustPressed(PauseActionName) || Input.IsActionJustPressed(CancelActionName)) { if (IsVisible()) { GameStateManager.Instance.ChangeState(GameState.Playing); //CallDeferred(MethodName.HideInventory); } // 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; // TODO: Move this on the game manager/controller side if (GameStateManager.Instance is not null) { GameStateManager.Instance.GameStateChange += state => { switch (state) { case GameState.Inventory: //CallDeferred(MethodName.ShowInventory); ShowInventory(); break; default: HideInventory(); //_ = UnpauseAsync(); // _ = new Task(async () => // { // await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame); // HideInventory(); // }); //CallDeferred(MethodName.HideInventory); //HideInventory(); break; } }; } } private async Task UnpauseAsync() { await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame); HideInventory(); } private void HideInventory() { if (!IsVisible()) return; HideCommand(); foreach (var menu in _itemMenus) { menu.Empty(); } } private void ShowInventory() { if (IsVisible()) return; ShowCommand(); foreach (var menu in _itemMenus) { menu.Fill(); } _itemMenus.FirstOrDefault()?.GrabFocus(); } }