/// <summary> /// Function to post event to listeners /// </summary> /// <param name="eventType">Event to invoke</param> /// <param name="Sender">Object invoking event</param> /// <param name="Param">Optional argument</param> /// <param name="methodName">Optional -> name of method posting notification (for debugging purposes)</param> public void PostNotification(ToolEventType eventType, Component Sender, object Param = null, string methodName = "Unknown") { //Notify all listeners of an event //List of listeners for this event only List <OnEvent> ListenList = null; //If no event entry exists, then exit because there are no listeners to notify if (!dictOfListeners.TryGetValue(eventType, out ListenList)) { Debug.LogWarningFormat("ToolEvents: Invalid event (No listeners present in dictOfListeners) for \"{0}\"", eventType); return; } //Entry exists. Now notify appropriate listeners for (int i = 0; i < ListenList.Count; i++) { if (ListenList[i] != null) { if (Param != null) { Debug.LogFormat("[Evm]: PostNotification -> type: {0}, param: {1}, {2}, sender: {3}{4}", eventType, Param.ToString(), Param.GetType(), methodName, "\n"); } else { Debug.LogFormat("[Evm]: PostNotification -> type: {0}, NO param, sender: {1}{2}", eventType, methodName, "\n"); } //If object is not null, then send message via delegate ListenList[i](eventType, Sender, Param); } } }
/// <summary> /// handles events /// </summary> /// <param name="eventType"></param> /// <param name="Sender"></param> /// <param name="Param"></param> public void OnEvent(ToolEventType eventType, Component Sender, object Param = null) { //Detect event type switch (eventType) { case ToolEventType.QuitTools: QuitTools(); break; default: Debug.LogError(string.Format("Invalid eventType {0}{1}", eventType, "\n")); break; } }
/// <summary> /// Function to add specified listener-object to array of listeners /// </summary> /// <param name="eventType">Event to Listen for</param> /// <param name="Listener">Object to listen for event</param> /// <param name="methodName">Optional -> name of method posting notification (for debugging purposes)</param> public void AddListener(ToolEventType eventType, OnEvent Listener, string methodName = "Unknown") { //List of listeners for this event List <OnEvent> ListenList = null; //New item to be added. Check for existing event type key. If one exists, add to list if (dictOfListeners.TryGetValue(eventType, out ListenList)) { //List exists, so add new item (if not already present -> prevents duplicates in the case of a followOn level) if (ListenList.Exists(x => x == Listener) == false) { ListenList.Add(Listener); } } else { //Otherwise create new list as dictionary key ListenList = new List <OnEvent>(); ListenList.Add(Listener); //Add to internal listeners list dictOfListeners.Add(eventType, ListenList); } /*Debug.LogFormat("[Tol] -> Listener Added -> type: {0}, sender: {1}{2}", eventType, methodName, "\n");*/ }
private int returnData = -1; //optional int data parameter that you can supply (SetButton) which is passed back as a parameter when button clicked /// <summary> /// set Event Type and optional data to pass as a parameter once button clicked /// </summary> /// <param name="type"></param> public void SetButton(ToolEventType type, int returnData = -1) { eventType = type; this.returnData = returnData; }
//Remove event type entry from dictionary, including all listeners public void RemoveEvent(ToolEventType eventType) { //Remove entry from dictionary dictOfListeners.Remove(eventType); }