forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventTarget.js
More file actions
284 lines (245 loc) · 7.97 KB
/
Copy pathEventTarget.js
File metadata and controls
284 lines (245 loc) · 7.97 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* @author Mat Groves http://matgroves.com/ @Doormat23
* @author Chad Engler https://github.com/englercj @Rolnaaba
*/
/**
* Originally based on https://github.com/mrdoob/eventtarget.js/ from mr Doob.
* Currently takes inspiration from the nodejs EventEmitter, EventEmitter3, and smokesignals
*/
/**
* Mixins event emitter functionality to a class
*
* @class EventTarget
* @example
* function MyEmitter() {}
*
* Phaser.EventTarget.mixin(MyEmitter.prototype);
*
* var em = new MyEmitter();
* em.emit('eventName', 'some data', 'some more data', {}, null, ...);
*/
Phaser.EventTarget = {
/**
* Backward compat from when this used to be a function
*/
call: function callCompat(obj) {
if(obj) {
obj = obj.prototype || obj;
Phaser.EventTarget.mixin(obj);
}
},
/**
* Mixes in the properties of the EventTarget prototype onto another object
*
* @method mixin
* @param object {Object} The obj to mix into
*/
mixin: function mixin(obj) {
/**
* Return a list of assigned event listeners.
*
* @method listeners
* @param eventName {String} The events that should be listed.
* @return {Array} An array of listener functions
*/
obj.listeners = function listeners(eventName) {
this._listeners = this._listeners || {};
return this._listeners[eventName] ? this._listeners[eventName].slice() : [];
};
/**
* Emit an event to all registered event listeners.
*
* @method emit
* @alias dispatchEvent
* @param eventName {String} The name of the event.
* @return {Boolean} Indication if we've emitted an event.
*/
obj.emit = obj.dispatchEvent = function emit(eventName, data) {
this._listeners = this._listeners || {};
//backwards compat with old method ".emit({ type: 'something' })"
if(typeof eventName === 'object') {
data = eventName;
eventName = eventName.type;
}
//ensure we are using a real pixi event
if(!data || data.__isEventObject !== true) {
data = new Phaser.Event(this, eventName, data);
}
//iterate the listeners
if(this._listeners && this._listeners[eventName]) {
var listeners = this._listeners[eventName].slice(0),
length = listeners.length,
fn = listeners[0],
i;
for(i = 0; i < length; fn = listeners[++i]) {
//call the event listener
fn.call(this, data);
//if "stopImmediatePropagation" is called, stop calling sibling events
if(data.stoppedImmediate) {
return this;
}
}
//if "stopPropagation" is called then don't bubble the event
if(data.stopped) {
return this;
}
}
//bubble this event up the scene graph
if(this.parent && this.parent.emit) {
this.parent.emit.call(this.parent, eventName, data);
}
return this;
};
/**
* Register a new EventListener for the given event.
*
* @method on
* @alias addEventListener
* @param eventName {String} Name of the event.
* @param callback {Functon} fn Callback function.
*/
obj.on = obj.addEventListener = function on(eventName, fn) {
this._listeners = this._listeners || {};
(this._listeners[eventName] = this._listeners[eventName] || [])
.push(fn);
return this;
};
/**
* Add an EventListener that's only called once.
*
* @method once
* @param eventName {String} Name of the event.
* @param callback {Function} Callback function.
*/
obj.once = function once(eventName, fn) {
this._listeners = this._listeners || {};
var self = this;
function onceHandlerWrapper() {
fn.apply(self.off(eventName, onceHandlerWrapper), arguments);
}
onceHandlerWrapper._originalHandler = fn;
return this.on(eventName, onceHandlerWrapper);
};
/**
* Remove event listeners.
*
* @method off
* @alias removeEventListener
* @param eventName {String} The event we want to remove.
* @param callback {Function} The listener that we need to find.
*/
obj.off = obj.removeEventListener = function off(eventName, fn) {
this._listeners = this._listeners || {};
if(!this._listeners[eventName])
return this;
var list = this._listeners[eventName],
i = fn ? list.length : 0;
while(i-- > 0) {
if(list[i] === fn || list[i]._originalHandler === fn) {
list.splice(i, 1);
}
}
if(list.length === 0) {
delete this._listeners[eventName];
}
return this;
};
/**
* Remove all listeners or only the listeners for the specified event.
*
* @method removeAllListeners
* @param eventName {String} The event you want to remove all listeners for.
*/
obj.removeAllListeners = function removeAllListeners(eventName) {
this._listeners = this._listeners || {};
if(!this._listeners[eventName])
return this;
delete this._listeners[eventName];
return this;
};
}
};
/**
* Creates an homogenous object for tracking events so users can know what to expect.
*
* @class Event
* @extends Object
* @constructor
* @param target {Object} The target object that the event is called on
* @param name {String} The string name of the event that was triggered
* @param data {Object} Arbitrary event data to pass along
*/
Phaser.Event = function(target, name, data) {
//for duck typing in the ".on()" function
this.__isEventObject = true;
/**
* Tracks the state of bubbling propagation. Do not
* set this directly, instead use `event.stopPropagation()`
*
* @property stopped
* @type Boolean
* @private
* @readOnly
*/
this.stopped = false;
/**
* Tracks the state of sibling listener propagation. Do not
* set this directly, instead use `event.stopImmediatePropagation()`
*
* @property stoppedImmediate
* @type Boolean
* @private
* @readOnly
*/
this.stoppedImmediate = false;
/**
* The original target the event triggered on.
*
* @property target
* @type Object
* @readOnly
*/
this.target = target;
/**
* The string name of the event that this represents.
*
* @property type
* @type String
* @readOnly
*/
this.type = name;
/**
* The data that was passed in with this event.
*
* @property data
* @type Object
* @readOnly
*/
this.data = data;
//backwards compat with older version of events
this.content = data;
/**
* The timestamp when the event occurred.
*
* @property timeStamp
* @type Number
* @readOnly
*/
this.timeStamp = Date.now();
};
/**
* Stops the propagation of events up the scene graph (prevents bubbling).
*
* @method stopPropagation
*/
Phaser.Event.prototype.stopPropagation = function stopPropagation() {
this.stopped = true;
};
/**
* Stops the propagation of events to sibling listeners (no longer calls any listeners).
*
* @method stopImmediatePropagation
*/
Phaser.Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() {
this.stoppedImmediate = true;
};