IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
PatientEventSystem.cs
1 using UnityEngine;
2 using UnityEngine.Events;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System;
6 
7 public class ObjectEvent : UnityEvent<object> { } //empty class; just needs to exist
8 
9 
10 public class EventTupel
11 {
12  public ObjectEvent objEvent;
13  public object obj;
14 
15  public EventTupel(ObjectEvent objEvent, object obj)
16  {
17  this.objEvent = objEvent;
18  this.obj = obj;
19  }
20 }
24 public class PatientEventSystem
25 {
26  private Dictionary< Event, ObjectEvent> mEventDictionary;
27  private static List<EventTupel> savedEvents = new List<EventTupel>();
28  private static PatientEventSystem mInstance = null;
29 
31  public enum Event {
46 
54  MESH_LoadedSingle, // Called whenever a new mesh has been loaded
56  MESH_LoadedAll, // Called after all of the patient's meshes have been loaded
60 
77 
82  }
83 
85  public static PatientEventSystem instance
86  {
87  get {
88  if (mInstance == null) {
89  mInstance = new PatientEventSystem ();
90  }
91  return mInstance;
92  }
93  }
94 
96  public static void startListening(Event eventType, UnityAction<object> listener)
97  {
98  ObjectEvent thisEvent = null;
99  // Attempt to get the the UnityEvent from the dictionary. If this succeeds,
100  // thisEvent will be filled and the if will evaluate to true:
101  if (instance.mEventDictionary.TryGetValue(eventType, out thisEvent))
102  {
103  thisEvent.AddListener(listener);
104  }
105  else {
106  thisEvent = new ObjectEvent();
107  thisEvent.AddListener(listener);
108  instance.mEventDictionary.Add(eventType, thisEvent);
109  }
110  //Debug.Log("Added event listener for event: " + eventType);
111  }
114  public static void stopListening(Event eventType, UnityAction<object> listener)
115  {
116  if (mInstance == null)
117  return;
118 
119  ObjectEvent thisEvent = null;
120  // Attempt to get the the UnityEvent from the dictionary. If this succeeds,
121  // thisEvent will be filled and the if will evaluate to true:
122  if (instance.mEventDictionary.TryGetValue(eventType, out thisEvent))
123  {
124  thisEvent.RemoveListener(listener);
125  }
126  //Debug.Log("Removed event listener for event: " + eventType);
127  }
128  public static void triggerEvent(Event eventType, object obj = null )
129  {
130  ObjectEvent thisEvent = null;
131  // Attempt to get the the UnityEvent from the dictionary. If this succeeds,
132  // thisEvent will be filled and the if will evaluate to true:
133  if (instance.mEventDictionary.TryGetValue(eventType, out thisEvent))
134  {
135  //Debug.Log("Triggering Event: " + eventType);
136  //thisEvent.Invoke( obj );
137 
138  //save event and execute them in main thread, because events can be called form other thread (e.g. PATIENT_Loaded)
139  savedEvents.Add(new EventTupel(thisEvent, obj));
140 
141  }
142  }
143 
144  /*
145  * Executes all saved events since the last call
146  */
147  public static void executeSavedEvents()
148  {
149  if (savedEvents.Count == 0)
150  {
151  return;
152  }
153 
154  List<EventTupel> savedEventsCopy = new List<EventTupel>(savedEvents);
155  savedEvents.Clear();
156 
157  foreach(EventTupel e in savedEventsCopy)
158  {
159  e.objEvent.Invoke(e.obj);
160  }
161  }
162 
163  private PatientEventSystem()
164  {
165  if( mEventDictionary == null )
166  {
167  mEventDictionary = new Dictionary< Event, ObjectEvent >();
168  }
169  }
170 
171 }
172 
static void stopListening(Event eventType, UnityAction< object > listener)
static PatientEventSystem instance
static void startListening(Event eventType, UnityAction< object > listener)