summaryrefslogtreecommitdiff
path: root/scripts/GUI/SpellMenuEntry.cs
blob: de71f2aa1b28668d2ac1ac86acb151f0a3ad8751 (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
using Godot;
using System;
using TheLegendOfGustav.Magic;

namespace TheLegendOfGustav.GUI;

public partial class SpellMenuEntry : HBoxContainer
{
	private TextureRect icon;
	private Label shortcutLabel;
	private Label nameLabel;
	private Button castBtn;
	private SpellResource spell;

	[Signal]
	public delegate void CastEventHandler(SpellResource Item);

	public override void _Ready()
	{
		base._Ready();
		icon = GetNode<TextureRect>("Icon");
		shortcutLabel = GetNode<Label>("Shortcut");
		nameLabel = GetNode<Label>("SpellName");
		castBtn = GetNode<Button>("CastButton");

		castBtn.Pressed += () => EmitSignal(SignalName.Cast, spell);
	}

	public void Initialize(SpellResource spell, char? shortcut)
	{
		this.spell = spell;
		nameLabel.Text = spell.SpellName;
		if (shortcut != null)
		{
			shortcutLabel.Text = $"{shortcut}";
			
			int index = (int)shortcut - 'a';

			InputEventKey activateEvent = new()
			{
				Keycode = Key.A + index
			};

			Shortcut shortie = new()
			{
				Events = [activateEvent]
			};

			castBtn.Shortcut = shortie;
		}
		else
		{
			shortcutLabel.Text = "";
		}
		icon.Texture = spell.Icon;
	}
}