-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventManager.cs
More file actions
84 lines (67 loc) · 1.63 KB
/
EventManager.cs
File metadata and controls
84 lines (67 loc) · 1.63 KB
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
using System.Collections;
using System.Collections.Generic;
public class EventManager {
public struct EventEntry
{
public delegate void Notify(string message = "");
public event Notify OnNotify;
public void NotifyAll(string message = "")
{
if (this.OnNotify != null)
{
this.OnNotify(message);
}
}
}
protected static Dictionary<string, EventEntry> entries = new Dictionary<string, EventEntry>();
/**
* Create Event Entry
*/
protected static void CreateEntry(string name)
{
if (!entries.ContainsKey(name))
{
entries.Add(name, new EventEntry());
}
}
/**
* Subscribe to an Event
*/
public static void Subscribe(string name, EventEntry.Notify callback)
{
CreateEntry(name);
entries[name].OnNotify += callback;
}
/**
* Unsubscribe from an Event
*/
public static void Unsubscribe(string name, EventEntry.Notify callback)
{
if (!entries.ContainsKey(name)) return;
entries[name].OnNotify -= callback;
}
/**
* Notify an Event
*/
public static void Notify(string name, string message = "")
{
if (!entries.ContainsKey(name)) return;
entries[name].NotifyAll(message);
}
/**
* Remove an Event
*/
public static void Remove(string name)
{
Notify("event.remove", name);
entries.Remove(name);
}
/**
* Clear Events
*/
public static void Clear()
{
Notify("event.clear");
entries.Clear();
}
}