/
event.js
363 lines (327 loc) · 10.2 KB
/
event.js
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
var JS = require("../platform/js");
/**
* !#en Base class of all kinds of events.
* !#zh 包含事件相关信息的对象。
* @class Event
*/
/**
* @method constructor
* @param {String} type - The name of the event (case-sensitive), e.g. "click", "fire", or "submit"
* @param {Boolean} bubbles - A boolean indicating whether the event bubbles up through the tree or not
*/
cc.Event = function(type, bubbles) {
/**
* !#en The name of the event (case-sensitive), e.g. "click", "fire", or "submit".
* !#zh 事件类型。
* @property type
* @type {String}
*/
this.type = type;
/**
* !#en Indicate whether the event bubbles up through the tree or not.
* !#zh 表示该事件是否进行冒泡。
* @property bubbles
* @type {Boolean}
*/
this.bubbles = !!bubbles;
/**
* !#en A reference to the target to which the event was originally dispatched.
* !#zh 最初事件触发的目标
* @property target
* @type {Object}
*/
this.target = null;
/**
* !#en A reference to the currently registered target for the event.
* !#zh 当前目标
* @property currentTarget
* @type {Object}
*/
this.currentTarget = null;
/**
* !#en
* Indicates which phase of the event flow is currently being evaluated.
* Returns an integer value represented by 4 constants:
* - Event.NONE = 0
* - Event.CAPTURING_PHASE = 1
* - Event.AT_TARGET = 2
* - Event.BUBBLING_PHASE = 3
* The phases are explained in the [section 3.1, Event dispatch and DOM event flow]
* (http://www.w3.org/TR/DOM-Level-3-Events/#event-flow), of the DOM Level 3 Events specification.
* !#zh 事件阶段
* @property eventPhase
* @type {Number}
*/
this.eventPhase = 0;
/*
* Indicates whether or not event.stopPropagation() has been called on the event.
* @property _propagationStopped
* @type {Boolean}
* @private
*/
this._propagationStopped = false;
/*
* Indicates whether or not event.stopPropagationImmediate() has been called on the event.
* @property _propagationImmediateStopped
* @type {Boolean}
* @private
*/
this._propagationImmediateStopped = false;
};
cc.Event.prototype = {
constructor: cc.Event,
/**
* !#en Reset the event for being stored in the object pool.
* !#zh 重置对象池中存储的事件。
* @method unuse
* @returns {String}
*/
unuse: function () {
this.type = cc.Event.NO_TYPE;
this.target = null;
this.currentTarget = null;
this.eventPhase = cc.Event.NONE;
this._propagationStopped = false;
this._propagationImmediateStopped = false;
},
/**
* !#en Reuse the event for being used again by the object pool.
* !#zh 用于对象池再次使用的事件。
* @method reuse
* @returns {String}
*/
reuse: function (type, bubbles) {
this.type = type;
this.bubbles = bubbles || false;
},
/**
* !#en Stops propagation for current event.
* !#zh 停止传递当前事件。
* @method stopPropagation
*/
stopPropagation: function () {
this._propagationStopped = true;
},
/**
* !#en Stops propagation for current event immediately,
* the event won't even be dispatched to the listeners attached in the current target.
* !#zh 立即停止当前事件的传递,事件甚至不会被分派到所连接的当前目标。
* @method stopPropagationImmediate
*/
stopPropagationImmediate: function () {
this._propagationImmediateStopped = true;
},
/**
* !#en Checks whether the event has been stopped.
* !#zh 检查该事件是否已经停止传递.
* @method isStopped
* @returns {Boolean}
*/
isStopped: function () {
return this._propagationStopped || this._propagationImmediateStopped;
},
/**
* !#en
* <p>
* Gets current target of the event <br/>
* note: It only be available when the event listener is associated with node. <br/>
* It returns 0 when the listener is associated with fixed priority.
* </p>
* !#zh 获取当前目标节点
* @method getCurrentTarget
* @returns {Node} The target with which the event associates.
*/
getCurrentTarget: function () {
return this.currentTarget;
},
/**
* !#en Gets the event type.
* !#zh 获取事件类型
* @method getType
* @returns {String}
*/
getType: function () {
return this.type;
}
};
//event type
/**
* !#en Code for event without type.
* !#zh 没有类型的事件
* @property NO_TYPE
* @static
* @type {string}
*/
cc.Event.NO_TYPE = 'no_type';
/**
* !#en The type code of Touch event.
* !#zh 触摸事件类型
* @property TOUCH
* @static
* @type {String}
*/
cc.Event.TOUCH = 'touch';
/**
* !#en The type code of Mouse event.
* !#zh 鼠标事件类型
* @property MOUSE
* @static
* @type {String}
*/
cc.Event.MOUSE = 'mouse';
/**
* !#en The type code of Keyboard event.
* !#zh 键盘事件类型
* @property KEYBOARD
* @static
* @type {String}
*/
cc.Event.KEYBOARD = 'keyboard';
/**
* !#en The type code of Acceleration event.
* !#zh 加速器事件类型
* @property ACCELERATION
* @static
* @type {String}
*/
cc.Event.ACCELERATION = 'acceleration';
//event phase
/**
* !#en Events not currently dispatched are in this phase
* !#zh 尚未派发事件阶段
* @property NONE
* @type {Number}
* @static
*/
cc.Event.NONE = 0;
/**
* !#en
* The capturing phase comprises the journey from the root to the last node before the event target's node
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
* !#zh 捕获阶段,包括事件目标节点之前从根节点到最后一个节点的过程。
* @property CAPTURING_PHASE
* @type {Number}
* @static
*/
cc.Event.CAPTURING_PHASE = 1;
/**
* !#en
* The target phase comprises only the event target node
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
* !#zh 目标阶段仅包括事件目标节点。
* @property AT_TARGET
* @type {Number}
* @static
*/
cc.Event.AT_TARGET = 2;
/**
* !#en
* The bubbling phase comprises any subsequent nodes encountered on the return trip to the root of the hierarchy
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
* !#zh 冒泡阶段, 包括回程遇到到层次根节点的任何后续节点。
* @property BUBBLING_PHASE
* @type {Number}
* @static
*/
cc.Event.BUBBLING_PHASE = 3;
/**
* !#en The Custom event
* !#zh 自定义事件
* @class Event.EventCustom
*
* @extends Event
*/
/**
* @method constructor
* @param {String} type - The name of the event (case-sensitive), e.g. "click", "fire", or "submit"
* @param {Boolean} bubbles - A boolean indicating whether the event bubbles up through the tree or not
*/
var EventCustom = function (type, bubbles) {
cc.Event.call(this, type, bubbles);
/**
* !#en A reference to the detailed data of the event
* !#zh 事件的详细数据
* @property detail
* @type {Object}
*/
this.detail = null;
};
JS.extend(EventCustom, cc.Event);
EventCustom.prototype.reset = EventCustom;
/**
* !#en Sets user data
* !#zh 设置用户数据
* @method setUserData
* @param {*} data
*/
EventCustom.prototype.setUserData = function (data) {
this.detail = data;
};
/**
* !#en Gets user data
* !#zh 获取用户数据
* @method getUserData
* @returns {*}
*/
EventCustom.prototype.getUserData = function () {
return this.detail;
};
/**
* !#en Gets event name
* !#zh 获取事件名称
* @method getEventName
* @returns {String}
*/
EventCustom.prototype.getEventName = cc.Event.prototype.getType;
/**
* !#en
* The keyCode read-only property represents a system and implementation dependent numerical code identifying the unmodified value of the pressed key. </br>
* This is usually the decimal ASCII (RFC 20) or Windows 1252 code corresponding to the key.</br>
* If the key can't be identified, this value is 0.</br>
*
* !#zh
* keyCode 是只读属性它表示一个系统和依赖于实现的数字代码,可以识别按键的未修改值。</br>
* 这通常是十进制 ASCII (RFC20) 或者 Windows 1252 代码,所对应的密钥。</br>
* 如果无法识别该键,则该值为 0。
*
* @property keyCode
* @type {Number}
*/
var MAX_POOL_SIZE = 10;
var _eventPool = new JS.Pool(MAX_POOL_SIZE);
EventCustom.put = function (event) {
_eventPool.put(event);
};
EventCustom.get = function (type, bubbles) {
var event = _eventPool._get();
if (event) {
event.reset(type, bubbles);
}
else {
event = new EventCustom(type, bubbles);
}
return event;
};
cc.Event.EventCustom = EventCustom;
module.exports = cc.Event;