1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
int Register(string eventName, Func<object,object,object,bool> action) {
int listenID = ++mNextListenID;
Listen listen = (mFreeListen.Count == 0) ? new Listen() : mFreeListen.Pop();
listen.mID = listenID;
listen.mAction = action;
mRegistered.Add(listenID, listen);
List<int> eventList;
if(!mRegisteredEvents.TryGetValue(eventName, out eventList))
eventList = mRegisteredEvents[eventName] = new List<int>();
eventList.Add(listenID);
return listenID;
}
|
1
2
3
4
5
|
public void Trigger(string eventName) {
Call(eventName, null, null, null);
}
|
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
|
void Call(string eventName, object arg1, object arg2, object arg3) {
List<int> listenerList;
if(mRegisteredEvents.TryGetValue(eventName, out listenerList)) {
for(int i = listenerList.Count - 1; i >= 0; --i) {
Listen listener;
if(mRegistered.TryGetValue(listenerList[i], out listener)) {
if(!listener.mAction(arg1, arg2, arg3)) {
mRegistered.Remove(listenerList[i]);
mFreeListen.Push(listener);
listenerList.RemoveAt(i);
}
}
else {
listenerList.RemoveAt(i);
}
}
if(listenerList.Count == 0) {
mRegisteredEvents.Remove(eventName);
}
}
}
|
1
|
Dictionary<string,List<int>> mRegisteredEvents = new Dictionary<string, List<int>>();
|
1
2
3
4
5
6
7
|
class Listen {
public int mID;
public Func<object,object,object,bool> mAction;
}
|
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
|
public int On
return Register(eventName, delegate(object arg1, object arg2, object arg3) {
T1 param1;
T2 param2;
T3 param3;
try { param1 = (T1)arg1; } catch { param1 = default(T1); }
try { param2 = (T2)arg2; } catch { param2 = default(T2); }
try { param3 = (T3)arg3; } catch { param3 = default(T3); }
return(action(param1, param2, param3));
});
}
public int On
return Register(eventName, delegate(object arg1, object arg2, object arg3) {
T1 param1;
T2 param2;
T3 param3;
try { param1 = (T1)arg1; } catch { param1 = default(T1); }
try { param2 = (T2)arg2; } catch { param2 = default(T2); }
try { param3 = (T3)arg3; } catch { param3 = default(T3); }
action(param1, param2, param3);
return true;
});
}
|
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
|
using System;
using System.Collections;
using System.Collections.Generic;
namespace EventSystem {
public class Dispatcher {
public void Trigger(string eventName) {
Call(eventName, null, null, null);
}
public void Dispatch(string eventName) {
Dispatched d = (mFreeDispatch.Count == 0) ? new Dispatched() : mFreeDispatch.Pop();
mDispatched.Enqueue(d.Set(eventName));
}
public int On(string eventName, Func<bool> action) {
return Register(eventName, delegate(object arg1, object arg2, object arg3) {
return(action());
});
}
public int On(string eventName, Action action) {
return Register(eventName, delegate(object arg1, object arg2, object arg3) {
action();
return(true);
});
}
public void Trigger(string eventName, object param1) {
Call(eventName, param1, null, null);
}
public void Dispatch(string eventName, object param1) {
Dispatched d = (mFreeDispatch.Count == 0) ? new Dispatched() : mFreeDispatch.Pop();
mDispatched.Enqueue(d.Set(eventName, param1));
}
public int On
return Register(eventName, delegate(object arg1, object arg2, object arg3) {
T param1;
try { param1 = (T)arg1; } catch { param1 = default(T); }
return(action(param1));
});
}
public int On
return Register(eventName, delegate(object arg1, object arg2, object arg3) {
T param1;
try { param1 = (T)arg1; } catch { param1 = default(T); }
action(param1);
return true;
});
}
public void Trigger(string eventName, object param1, object param2) {
Call(eventName, param1, param2, null);
}
public void Dispatch(string eventName, object param1, object param2) {
Dispatched d = (mFreeDispatch.Count == 0) ? new Dispatched() : mFreeDispatch.Pop();
mDispatched.Enqueue(d.Set(eventName, param1, param2));
}
public int On
return Register(eventName, delegate(object arg1, object arg2, object arg3) {
T1 param1;
T2 param2;
try { param1 = (T1)arg1; } catch { param1 = default(T1); }
try { param2 = (T2)arg2; } catch { param2 = default(T2); }
return(action(param1, param2));
});
}
public int On
return Register(eventName, delegate(object arg1, object arg2, object arg3) {
T1 param1;
T2 param2;
try { param1 = (T1)arg1; } catch { param1 = default(T1); }
try { param2 = (T2)arg2; } catch { param2 = default(T2); }
action(param1, param2);
return true;
});
}
public void Trigger(string eventName, object param1, object param2, object param3) {
Call(eventName, param1, param2, param3);
}
public void Dispatch(string eventName, object param1, object param2, object param3){
Dispatched d = (mFreeDispatch.Count == 0) ? new Dispatched() : mFreeDispatch.Pop();
mDispatched.Enqueue(d.Set(eventName, param1, param2, param3));
}
public int On
return Register(eventName, delegate(object arg1, object arg2, object arg3) {
T1 param1;
T2 param2;
T3 param3;
try { param1 = (T1)arg1; } catch { param1 = default(T1); }
try { param2 = (T2)arg2; } catch { param2 = default(T2); }
try { param3 = (T3)arg3; } catch { param3 = default(T3); }
return(action(param1, param2, param3));
});
}
public int On
return Register(eventName, delegate(object arg1, object arg2, object arg3) {
T1 param1;
T2 param2;
T3 param3;
try { param1 = (T1)arg1; } catch { param1 = default(T1); }
try { param2 = (T2)arg2; } catch { param2 = default(T2); }
try { param3 = (T3)arg3; } catch { param3 = default(T3); }
action(param1, param2, param3);
return true;
});
}
public bool Cancel(int listenerID) {
return mRegistered.Remove(listenerID);
}
public void DispatchPending() {
while(mDispatched.Count > 0) {
Dispatched d = mDispatched.Dequeue();
Call(d.mEventName, d.mArg1, d.mArg2, d.mArg3);
mFreeDispatch.Push(d);
}
}
int Register(string eventName, Func<object,object,object,bool> action) {
int listenID = ++mNextListenID;
Listen listen = (mFreeListen.Count == 0) ? new Listen() : mFreeListen.Pop();
listen.mID = listenID;
listen.mAction = action;
mRegistered.Add(listenID, listen);
List<int> eventList;
if(!mRegisteredEvents.TryGetValue(eventName, out eventList))
eventList = mRegisteredEvents[eventName] = new List<int>();
eventList.Add(listenID);
return listenID;
}
void Call(string eventName, object arg1, object arg2, object arg3) {
List<int> listenerList;
if(mRegisteredEvents.TryGetValue(eventName, out listenerList)) {
for(int i = listenerList.Count - 1; i >= 0; --i) {
Listen listener;
if(mRegistered.TryGetValue(listenerList[i], out listener)) {
if(!listener.mAction(arg1, arg2, arg3)) {
mRegistered.Remove(listenerList[i]);
mFreeListen.Push(listener);
listenerList.RemoveAt(i);
}
}
else {
listenerList.RemoveAt(i);
}
}
if(listenerList.Count == 0) {
mRegisteredEvents.Remove(eventName);
}
}
}
class Listen {
public int mID;
public Func<object,object,object,bool> mAction;
}
class Dispatched {
public Dispatched Set(string eventName, object arg1=null, object arg2=null, object arg3=null) {
mEventName = eventName;
mArg1 = arg1;
mArg2 = arg2;
mArg3 = arg3;
return this;
}
public string mEventName;
public object mArg1, mArg2, mArg3;
}
Dictionary<string,List<int>> mRegisteredEvents = new Dictionary<string, List<int>>();
Dictionary<int,Listen> mRegistered = new Dictionary<int, Listen>();
Stack
Stack
Queue
int mNextListenID = 4711;
}
}
|