summaryrefslogtreecommitdiff
path: root/scripts/GUI/InventoryMenu.cs
blob: 7e264fa82021d375752e21c0e88272b7631a634f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using Godot;
using TheLegendOfGustav.Entities.Items;
using TheLegendOfGustav.Entities.Actors;

namespace TheLegendOfGustav.GUI;

public partial class InventoryMenu : CanvasLayer
{
	private static readonly PackedScene itemMenuEntryScene = GD.Load<PackedScene>("res://scenes/GUI/item_menu_entry.tscn");
	
	private VBoxContainer itemsNode;
	
	[Signal]
	public delegate void ItemSelectedEventHandler(Item item);
	[Signal]
	public delegate void ItemDropEventHandler(Item item);

	public override void _Ready()
	{
		base._Ready();

		itemsNode = GetNode<VBoxContainer>("CenterContainer/PanelContainer/VBoxContainer/Items");
		Hide();
	}

	public void OnActivate(Item item)
	{
		EmitSignal(SignalName.ItemSelected, item);
	}

	public void OnDrop(Item item)
	{
		EmitSignal(SignalName.ItemDrop, item);
	}

	public void Initialize(Inventory inventory)
	{
		for (int i = 0; i < inventory.Items.Count; i++)
		{
			RegisterItem(i, inventory.Items[i]);
		}

		Show();
	}
	
	private void RegisterItem(int index, Item item)
	{
		char? shortcut = null;

		// Só terá atalho para as letras do alfabeto.
		if (index < 26)
		{
			shortcut = (char)('a' + index);
		}

		ItemMenuEntry itemEntry = itemMenuEntryScene.Instantiate<ItemMenuEntry>();

		itemsNode.AddChild(itemEntry);
		itemEntry.Initialize(item, shortcut);
		itemEntry.Activate += OnActivate;
		itemEntry.Drop += OnDrop;
	}
}