summaryrefslogtreecommitdiff
path: root/scripts/Utils/MessageLogData.cs
blob: 46d2c3e9b0749b0cc40c46fd8adde2e094ee1640 (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
using Godot;
using TheLegendOfGustav.GUI;

namespace TheLegendOfGustav.Utils;

public partial class MessageLogData : Node
{
	/// <summary>
	/// Acionado sempre que uma mensagem for adicionada para o log.
	/// </summary>
	/// <param name="text">Mensagem.</param>
	[Signal]
	public delegate void messageSentEventHandler(Message message);

	public static MessageLogData Instance { get; private set; }

	public Godot.Collections.Array<Message> Messages { get; private set; } = [];

	private Message LastMessage
	{
		get
		{
			if (Messages.Count <= 0)
			{
				return null;
			}
			return Messages[^1];
		}
	}

	public override void _Ready()
	{
		base._Ready();
		Instance = this;
	}

	public void ClearMessages()
	{
		for (int i = Messages.Count - 1; i >= 0; i--)
		{
			Message message = Messages[i];
			Messages.RemoveAt(i);
			message.QueueFree();
		}
	}

	public void AddMessage(string text)
	{
		if (LastMessage != null && LastMessage.PlainText == text)
		{
			LastMessage.Count++;
			return;
		}

		Message message = new(text);
		Messages.Add(message);
		EmitSignal(SignalName.messageSent, message);
	}
}