-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventObject.cs
238 lines (207 loc) · 5.37 KB
/
EventObject.cs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/************************************************************************************************************
Class Name: EventObject / ListenerObject
Namespace: Com.EpixCode.Util.Events.EpixEvents
Type: Util
Definition:
An [EventObject] represent the relation of an [EventName] and a [Dispatcher]. It keeps the
list of all [ListenerObject] registered to him to trigger their Callback when an event is
raised.
************************************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ListenerObject
{
private System.WeakReference _listener;
public Action<EventObject> Callback { get; set; }
public int Priority { get; set; }
public object ObjectReference
{
get
{
return _listener.Target;
}
}
public bool IsAlive
{
get
{
return (bool)(this._listener != null && this._listener.IsAlive && !this._listener.Target.Equals(null));
}
}
public ListenerObject(object listener, Action<EventObject> callback, int priority = 1)
{
this._listener = new System.WeakReference(listener);
this.Callback = callback;
this.Priority = priority;
}
}
public class EventObject
{
//Reference Holder
private System.WeakReference _dispatcher;
private string _event;
private object[] _params;
private Action<EventObject> _onDestroyCallback;
private List<ListenerObject> _listenerList;
public object Dispatcher
{
get
{
return _dispatcher.Target;
}
}
public bool IsAlive
{
get
{
return (bool)(this._dispatcher != null && this._dispatcher.IsAlive && !this._dispatcher.Target.Equals(null));
}
}
public string EventName
{
get
{
return _event;
}
}
public object[] Params
{
get
{
return _params;
}
}
//Management
internal EventObject(string aEvent, object aDispatcher)
{
this._event = aEvent;
this._dispatcher = new System.WeakReference(aDispatcher);
Init();
}
internal void Init()
{
_listenerList = new List<ListenerObject>();
}
internal void RegisterDestroyCallback(Action<EventObject> aDestroyCallback)
{
_onDestroyCallback = aDestroyCallback;
}
internal void Destroy()
{
if (_onDestroyCallback != null)
{
_onDestroyCallback(this);
_onDestroyCallback = null;
}
_event = "";
_params = null;
_dispatcher = null;
_listenerList.Clear();
}
internal void RegisterEventListener(object aListener, Action<EventObject> aMethod, int aPriority = 1)
{
ListenerObject listener = new ListenerObject(aListener, aMethod, aPriority);
_listenerList.Add(listener);
_listenerList = _listenerList.OrderBy(o => o.Priority).ToList<ListenerObject>();
}
internal void UnregisterEventListenerObject(ListenerObject aListener)
{
if (aListener != null && _listenerList.Contains(aListener))
{
_listenerList.Remove(aListener);
}
}
internal void UnregisterEventListener(object aListener, Action<EventObject> aMethod = null)
{
int index = 0;
bool eventRemoved = false;
while (!eventRemoved)
{
if (index >= _listenerList.Count)
{
break;
}
ListenerObject listener = _listenerList[index];
if ((listener.ObjectReference.Equals(aListener) && aMethod != null && listener.Callback.Equals(aMethod)) ||
(listener.ObjectReference.Equals(aListener) && aMethod == null))
{
UnregisterEventListenerObject(listener);
eventRemoved = true;
}
else
{
if (listener.IsAlive)
{
index++;
}
else
{
UnregisterEventListenerObject(listener);
}
}
}
if (!eventRemoved)
{
string prefix = "";
if (aMethod != null)
{
prefix += "The method [" + aMethod.ToString() + "] from ";
}
Debug.LogWarning("[EventObject.cs] - UnregisterEventListener() - " + prefix + aListener.ToString() + "] doesn't seem to be registered for the event: " + _event + ".");
}
if (_listenerList.Count <= 0)
{
Destroy();
}
}
internal void Dispatch(object[] aParams)
{
_params = aParams;
Dispatch();
Array.Clear(_params, 0, _params.Length);
}
internal void Dispatch()
{
if (!this.IsAlive)
{
Destroy();
return;
}
int index = 0;
while (index < _listenerList.Count)
{
ListenerObject listener = _listenerList[index];
if (listener.IsAlive)
{
listener.Callback(this);
index++;
}
else
{
UnregisterEventListenerObject(listener);
}
}
if (_listenerList.Count <= 0)
{
Destroy();
}
}
public object GetParam(int aIndex)
{
if (aIndex < _params.Length)
{
return _params[aIndex];
}
else
{
Debug.LogWarning("[EventObject.cs] - GetParam() - Can't retrieve param at index [" + aIndex + "] in the param list [Length = " + _params.Length + "]");
return null;
}
}
public override string ToString()
{
return ("[" + _dispatcher + "] - " + _event);
}
}