From a65aff09da7bbd243d52c4205a9883ae5389e188 Mon Sep 17 00:00:00 2001 From: fjruizpo Date: Sat, 7 Jan 2017 08:45:05 +0100 Subject: [PATCH] Add a simple event dispatcher with static accessors. --- .../Scripts/EventHandlers/EventDispatcher.cs | 174 ++++++++++++++++++ .../EventHandlers/EventDispatcher.cs.meta | 12 ++ 2 files changed, 186 insertions(+) create mode 100644 Assets/Fungus/Scripts/EventHandlers/EventDispatcher.cs create mode 100644 Assets/Fungus/Scripts/EventHandlers/EventDispatcher.cs.meta diff --git a/Assets/Fungus/Scripts/EventHandlers/EventDispatcher.cs b/Assets/Fungus/Scripts/EventHandlers/EventDispatcher.cs new file mode 100644 index 00000000..4f999570 --- /dev/null +++ b/Assets/Fungus/Scripts/EventHandlers/EventDispatcher.cs @@ -0,0 +1,174 @@ +using System.Collections.Generic; +using System; + +namespace Fungus +{ + public class EventDispatcher + { + public delegate void TypedDelegate(T e) where T : class; + + #region Statics + static EventDispatcher _instance; + public static EventDispatcher Instance + { + get + { + if(_instance == null) + { + _instance = new EventDispatcher(); + } + return _instance; + } + } + + public static void AddLog(Action log) + { + Instance.addLog(log); + } + + public static void RemoveLog(Action log) + { + Instance.removeLog(log); + } + + public static void AddListener(TypedDelegate listener) where T : class + { + Instance.addListener(listener); + } + + public static void RemoveListener(TypedDelegate listener) where T : class + { + Instance.removeListener(listener); + } + + public static void Raise(T evt) where T : class + { + Instance.raise(evt); + } + + public static void Raise() where T : class, new() + { + Instance.raise(new T()); + } + + public static void UnregisterAll() + { + Instance.unregisterAll(); + } + #endregion + + #region Private Members + readonly Dictionary> _delegates; + event Action _onLog; + #endregion + + #region Private Functions + private EventDispatcher() + { + _delegates = new Dictionary>(); + } + /// + /// Gets the delegate list copy. + /// + /// + /// As listener can modify the list while iterating it, it is better to iterate a copy of the delegates list instead of a reference. + /// + /// A copy of the delegates list if found. Null if the dictionary does not contain a delegate list for this event. + /// Event instance. + /// Type of the received event. + List getDelegateListCopy(T evt) + { + var type = typeof(T); + return _delegates.ContainsKey(type) ? new List(_delegates[type]) : null; + } + + void log(string message) + { + if(_onLog != null) + { + _onLog(message); + } + } + #endregion + + #region Public Functions + public void addLog(Action log) + { + _onLog += log; + } + + public void removeLog(Action log) + { + _onLog -= log; + } + + public void addListener(TypedDelegate listener) where T : class + { + var type = typeof(T); + if(!_delegates.ContainsKey(type)) + { + _delegates.Add(type, new List()); + } + + var list = _delegates[type]; + if(!list.Contains(listener)) + { + list.Add(listener); + } + } + + public void removeListener(TypedDelegate listener) where T : class + { + var type = typeof(T); + if(_delegates.ContainsKey(type)) + { + _delegates[type].Remove(listener); + return; + } + } + + public void raise(T evt) where T : class + { + if(evt == null) + { + log("Raised a null event"); + return; + } + + var list = getDelegateListCopy(evt); + if(list == null || list.Count < 1) + { + log("Raised an event with no listeners"); + return; + } + + for(int i = 0; i < list.Count; ++i) + { + var callback = list[i] as TypedDelegate; + + if(callback != null) + { + try + { + callback(evt); + } + catch(Exception gotcha) + { + log(gotcha.Message); + } + } + } + } + + public void raise() where T : class, new() + { + raise(new T()); + } + + public void unregisterAll() + { + _delegates.Clear(); + } + #endregion + } +} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/EventHandlers/EventDispatcher.cs.meta b/Assets/Fungus/Scripts/EventHandlers/EventDispatcher.cs.meta new file mode 100644 index 00000000..031a08d7 --- /dev/null +++ b/Assets/Fungus/Scripts/EventHandlers/EventDispatcher.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: d6bacc8888852c94bab5c3b36cdf1574 +timeCreated: 1483696929 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: