summaryrefslogtreecommitdiff
path: root/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs
diff options
context:
space:
mode:
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs')
-rw-r--r--WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs89
1 files changed, 89 insertions, 0 deletions
diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs b/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs
new file mode 100644
index 0000000..0002f14
--- /dev/null
+++ b/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs
@@ -0,0 +1,89 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace WK
+{
+
+ public class ScopedNotification
+ {
+ public delegate void NotificatonHandler(params object[] args);
+
+ private Dictionary<string, List<NotificatonHandler>> m_EventListeners = new Dictionary<string, List<NotificatonHandler>>();
+
+ public void AddObserver(string eventName, NotificatonHandler handler)
+ {
+ if (handler == null)
+ {
+ return;
+ }
+
+ if (string.IsNullOrEmpty(eventName))
+ {
+ return;
+ }
+
+ List<NotificatonHandler> handlers;
+ if (!m_EventListeners.ContainsKey(eventName))
+ {
+ m_EventListeners.Add(eventName, new List<NotificatonHandler>());
+ }
+
+ handlers = m_EventListeners[eventName];
+ handlers.Add(handler);
+ }
+
+ public void RemoveObserver(string eventName, NotificatonHandler handler)
+ {
+ if(handler == null) { return; }
+
+ if(string.IsNullOrEmpty(eventName)) { return; }
+
+ if (!m_EventListeners.ContainsKey(eventName))
+ return;
+
+ List<NotificatonHandler> handlers = m_EventListeners[eventName];
+ if(handlers.Contains(handler))
+ handlers.Remove(handler);
+ }
+
+ public void RemoveEvent(string eventName)
+ {
+ if (string.IsNullOrEmpty(eventName)) { return; }
+ if(m_EventListeners.ContainsKey(eventName))
+ {
+ m_EventListeners.Remove(eventName);
+ }
+ }
+
+ public void Clean()
+ {
+ m_EventListeners.Clear();
+ }
+
+ public void PostNotification(string eventName, params object[] args)
+ {
+ if (string.IsNullOrEmpty(eventName)) { return; }
+
+ if (!m_EventListeners.ContainsKey(eventName))
+ return;
+
+ List<NotificatonHandler> handlers = m_EventListeners[eventName];
+ for(int i = 0; i < handlers.Count; i++)
+ {
+ var handler = handlers[i];
+ if(handler != null)
+ {
+ handler(args);
+ }
+ }
+ }
+
+ public void PostNotification(string eventName)
+ {
+ PostNotification(eventName, null);
+ }
+
+ }
+
+} \ No newline at end of file