summaryrefslogtreecommitdiff
path: root/scripts/GUI/Details.cs
blob: 3ead3a90ca89c8ede74e5a03b4fd89480befb195 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Godot;
using TheLegendOfGustav.Entities;
using TheLegendOfGustav.Map;
using TheLegendOfGustav.Utils;

namespace TheLegendOfGustav.GUI;

public partial class Details : CanvasLayer
{
	private static readonly LabelSettings lblSettings = GD.Load<LabelSettings>("res://assets/definitions/message_label_settings.tres");

	[Export]
	private Map.Map map;
	private VBoxContainer EntityNames { get; set; }
	private Godot.Collections.Array<Entity> Entities { get; set; } = [];

	private Godot.Collections.Array<Label> ActorsLabel { get; set; } = [];

	private SignalBus.EnterInspectionModeEventHandler enterLambda;
	private SignalBus.ExitInspectionModeEventHandler exitLambda;

	public override void _Ready()
	{
		base._Ready();
		EntityNames = GetNode<VBoxContainer>("HBoxContainer/PanelContainer/ScrollContainer/Entities");

		enterLambda = () => Visible = true;
		exitLambda = () => Visible = false;
		SignalBus.Instance.InspectorMoved += OnInspectorWalk;
		SignalBus.Instance.EnterInspectionMode += enterLambda;
		SignalBus.Instance.ExitInspectionMode += exitLambda;
	}

	public void OnInspectorWalk(Vector2I pos)
	{
		MapData mapData = map.MapData;
		Entities = mapData.GetEntitiesAtPosition(pos);
		UpdateLabels();
	}

	public override void _Notification(int what)
	{
		if (what == NotificationPredelete)
		{
			SignalBus.Instance.InspectorMoved -= OnInspectorWalk;
			if (enterLambda != null)
			{
				SignalBus.Instance.EnterInspectionMode -= enterLambda;
			}
			if (exitLambda != null)
			{
				SignalBus.Instance.ExitInspectionMode -= exitLambda;
			}
		}
		base._Notification(what);
	}

	private void UpdateLabels()
	{
		foreach (Label label in ActorsLabel)
		{
			label.QueueFree();
		}

		ActorsLabel.Clear();

		foreach (Entity entity in Entities)
		{
			Label label = new()
			{
				Text = entity.DisplayName,
				LabelSettings = lblSettings
			};

			ActorsLabel.Add(label);
			EntityNames.AddChild(label);
		}
	}
}