-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEventManager.cs
111 lines (100 loc) · 3.54 KB
/
EventManager.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Collections.Generic;
using System.Linq;
using System;
namespace RedotUtils;
/// <summary>
/// This class was created to attempt to simplify the process of creating C# events for gamedev.
///
/// ########### Example #1 ###########
///
/// Events.Generic.AddListener(EventGeneric.OnKeyboardInput, (args) =>
/// {
/// GD.Print(args[0]);
/// GD.Print(args[1]);
/// GD.Print(args[2]);
/// }, "someId");
///
/// Events.Generic.RemoveListeners(EventGeneric.OnKeyboardInput, "someId");
///
/// // Listener is never called because it was removed
/// Events.Generic.Notify(EventGeneric.OnKeyboardInput, 1, 2, 3);
///
/// ########### Example #2 ###########
/// Events.Player.AddListener<PlayerSpawnArgs>(EventPlayer.OnPlayerSpawn, (args) =>
/// {
/// GD.Print(args.Name);
/// GD.Print(args.Location);
/// GD.Print(args.Player);
/// });
///
/// Events.Player.Notify(EventPlayer.OnPlayerSpawn, new PlayerSpawnArgs(name, location, player));
/// </summary>
/// <typeparam name="TEvent">The event type enum to be used. For example 'EventPlayer' enum.</typeparam>
public class EventManager<TEvent>
{
private readonly Dictionary<TEvent, List<object>> eventListeners = [];
/// <summary>
/// The event type to be listened to (Action uses object[] params by default)
/// </summary>
public void AddListener(TEvent eventType, Action<object[]> action, string id = "")
{
AddListener<object[]>(eventType, action, id);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1854:Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method", Justification = "Don't have time to test this right now")]
public void AddListener<T>(TEvent eventType, Action<T> action, string id = "")
{
if (!eventListeners.ContainsKey(eventType))
{
eventListeners.Add(eventType, []);
}
eventListeners[eventType].Add(new Listener(action, id));
}
/// <summary>
/// Remove all listeners of type 'eventType' with 'id'
/// For example. If there is a listener of type OnPlayerSpawn with id 1 and another
/// with id 1 (same id). Then this function will remove both these listeners.
/// </summary>
public void RemoveListeners(TEvent eventType, string id = "")
{
if (!eventListeners.ContainsKey(eventType))
{
throw new InvalidOperationException($"Tried to remove listener of event type '{eventType}' from an event type that has not even been defined yet");
}
foreach (KeyValuePair<TEvent, List<object>> pair in eventListeners)
{
for (int i = pair.Value.Count - 1; i >= 0; i--)
{
if (pair.Key.Equals(eventType) && ((Listener)pair.Value[i]).Id == id)
{
pair.Value.RemoveAt(i);
}
}
}
}
/// <summary>
/// Remove ALL listeners from ALL event types
/// </summary>
public void RemoveAllListeners()
{
eventListeners.Clear();
}
/// <summary>
/// Notify all listeners
/// </summary>
public void Notify(TEvent eventType, params object[] args)
{
if (!eventListeners.TryGetValue(eventType, out List<object> value))
{
return;
}
foreach (dynamic listener in value.ToList()) // if ToList() is not here then issue #137 will occur
{
listener.Action(args);
}
}
}
public class Listener(dynamic action, string id)
{
public dynamic Action { get; set; } = action;
public string Id { get; set; } = id;
}