1 /****************************************************************************
  2  Copyright (c) 2011-2012 cocos2d-x.org
  3  Copyright (c) 2013-2014 Chukong Technologies Inc.
  4 
  5  http://www.cocos2d-x.org
  6 
  7  Permission is hereby granted, free of charge, to any person obtaining a copy
  8  of this software and associated documentation files (the "Software"), to deal
  9  in the Software without restriction, including without limitation the rights
 10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  copies of the Software, and to permit persons to whom the Software is
 12  furnished to do so, subject to the following conditions:
 13 
 14  The above copyright notice and this permission notice shall be included in
 15  all copies or substantial portions of the Software.
 16 
 17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  THE SOFTWARE.
 24  ****************************************************************************/
 25 
 26 /**
 27  * Base class of all kinds of events.
 28  * @class
 29  * @extends cc.Class
 30  */
 31 cc.Event = cc.Class.extend(/** @lends cc.Event# */{
 32     _type: 0,                                   //  Event type
 33     _isStopped: false,                         //< whether the event has been stopped.
 34     _currentTarget: null,                       //< Current target
 35 
 36     _setCurrentTarget: function (target) {
 37         this._currentTarget = target;
 38     },
 39 
 40     ctor: function (type) {
 41         this._type = type;
 42     },
 43 
 44     /**
 45      * Gets the event type
 46      * @function
 47      * @returns {Number}
 48      */
 49     getType: function () {
 50         return this._type;
 51     },
 52 
 53     /**
 54      * Stops propagation for current event
 55      * @function
 56      */
 57     stopPropagation: function () {
 58         this._isStopped = true;
 59     },
 60 
 61     /**
 62      * Checks whether the event has been stopped
 63      * @function
 64      * @returns {boolean}
 65      */
 66     isStopped: function () {
 67         return this._isStopped;
 68     },
 69 
 70     /**
 71      * <p>
 72      *     Gets current target of the event                                                            <br/>
 73      *     note: It only be available when the event listener is associated with node.                <br/>
 74      *          It returns 0 when the listener is associated with fixed priority.
 75      * </p>
 76      * @function
 77      * @returns {cc.Node}  The target with which the event associates.
 78      */
 79     getCurrentTarget: function () {
 80         return this._currentTarget;
 81     }
 82 });
 83 
 84 //event type
 85 /**
 86  * The type code of Touch event.
 87  * @constant
 88  * @type {number}
 89  */
 90 cc.Event.TOUCH = 0;
 91 /**
 92  * The type code of Keyboard event.
 93  * @constant
 94  * @type {number}
 95  */
 96 cc.Event.KEYBOARD = 1;
 97 /**
 98  * The type code of Acceleration event.
 99  * @constant
100  * @type {number}
101  */
102 cc.Event.ACCELERATION = 2;
103 /**
104  * The type code of Mouse event.
105  * @constant
106  * @type {number}
107  */
108 cc.Event.MOUSE = 3;
109 /**
110  * The type code of UI focus event.
111  * @constant
112  * @type {number}
113  */
114 cc.Event.FOCUS = 4;
115 /**
116  * The type code of Custom event.
117  * @constant
118  * @type {number}
119  */
120 cc.Event.CUSTOM = 6;
121 
122 /**
123  * The Custom event
124  * @class
125  * @extends cc.Event
126  */
127 cc.EventCustom = cc.Event.extend(/** @lends cc.EventCustom# */{
128     _eventName: null,
129     _userData: null,                                 // User data
130 
131     ctor: function (eventName) {
132         cc.Event.prototype.ctor.call(this, cc.Event.CUSTOM);
133         this._eventName = eventName;
134     },
135 
136     /**
137      * Sets user data
138      * @param {*} data
139      */
140     setUserData: function (data) {
141         this._userData = data;
142     },
143 
144     /**
145      * Gets user data
146      * @returns {*}
147      */
148     getUserData: function () {
149         return this._userData;
150     },
151 
152     /**
153      * Gets event name
154      * @returns {String}
155      */
156     getEventName: function () {
157         return this._eventName;
158     }
159 });
160 
161 /**
162  * The mouse event
163  * @class
164  * @extends cc.Event
165  */
166 cc.EventMouse = cc.Event.extend(/** @lends cc.EventMouse# */{
167     _eventType: 0,
168     _button: 0,
169     _x: 0,
170     _y: 0,
171     _prevX: 0,
172     _prevY: 0,
173     _scrollX: 0,
174     _scrollY: 0,
175 
176     ctor: function (eventType) {
177         cc.Event.prototype.ctor.call(this, cc.Event.MOUSE);
178         this._eventType = eventType;
179     },
180 
181     /**
182      * Sets scroll data
183      * @param {number} scrollX
184      * @param {number} scrollY
185      */
186     setScrollData: function (scrollX, scrollY) {
187         this._scrollX = scrollX;
188         this._scrollY = scrollY;
189     },
190 
191     /**
192      * Returns the x axis scroll value
193      * @returns {number}
194      */
195     getScrollX: function () {
196         return this._scrollX;
197     },
198 
199     /**
200      * Returns the y axis scroll value
201      * @returns {number}
202      */
203     getScrollY: function () {
204         return this._scrollY;
205     },
206 
207     /**
208      * Sets cursor location
209      * @param {number} x
210      * @param {number} y
211      */
212     setLocation: function (x, y) {
213         this._x = x;
214         this._y = y;
215     },
216 
217 	/**
218 	 * Returns cursor location
219 	 * @return {cc.Point} location
220 	 */
221     getLocation: function () {
222         return {x: this._x, y: this._y};
223     },
224 
225 	/**
226 	 * Returns the current cursor location in screen coordinates
227 	 * @return {cc.Point}
228 	 */
229 	getLocationInView: function() {
230 		return {x: this._x, y: cc.view._designResolutionSize.height - this._y};
231 	},
232 
233     _setPrevCursor: function (x, y) {
234         this._prevX = x;
235         this._prevY = y;
236     },
237 
238     /**
239      * Returns the delta distance from the previous location to current location
240      * @return {cc.Point}
241      */
242     getDelta: function () {
243         return {x: this._x - this._prevX, y: this._y - this._prevY};
244     },
245 
246     /**
247      * Returns the X axis delta distance from the previous location to current location
248      * @return {Number}
249      */
250     getDeltaX: function () {
251         return this._x - this._prevX;
252     },
253 
254     /**
255      * Returns the Y axis delta distance from the previous location to current location
256      * @return {Number}
257      */
258     getDeltaY: function () {
259         return this._y - this._prevY;
260     },
261 
262     /**
263      * Sets mouse button
264      * @param {number} button
265      */
266     setButton: function (button) {
267         this._button = button;
268     },
269 
270     /**
271      * Returns mouse button
272      * @returns {number}
273      */
274     getButton: function () {
275         return this._button;
276     },
277 
278     /**
279      * Returns location X axis data
280      * @returns {number}
281      */
282     getLocationX: function () {
283         return this._x;
284     },
285 
286     /**
287      * Returns location Y axis data
288      * @returns {number}
289      */
290     getLocationY: function () {
291         return this._y;
292     }
293 });
294 
295 //Different types of MouseEvent
296 /**
297  * The none event code of  mouse event.
298  * @constant
299  * @type {number}
300  */
301 cc.EventMouse.NONE = 0;
302 /**
303  * The event type code of mouse down event.
304  * @constant
305  * @type {number}
306  */
307 cc.EventMouse.DOWN = 1;
308 /**
309  * The event type code of mouse up event.
310  * @constant
311  * @type {number}
312  */
313 cc.EventMouse.UP = 2;
314 /**
315  * The event type code of mouse move event.
316  * @constant
317  * @type {number}
318  */
319 cc.EventMouse.MOVE = 3;
320 /**
321  * The event type code of mouse scroll event.
322  * @constant
323  * @type {number}
324  */
325 cc.EventMouse.SCROLL = 4;
326 
327 /**
328  * The tag of Mouse left button
329  * @constant
330  * @type {Number}
331  */
332 cc.EventMouse.BUTTON_LEFT = 0;
333 
334 /**
335  * The tag of Mouse right button  (The right button number is 2 on browser)
336  * @constant
337  * @type {Number}
338  */
339 cc.EventMouse.BUTTON_RIGHT = 2;
340 
341 /**
342  * The tag of Mouse middle button  (The right button number is 1 on browser)
343  * @constant
344  * @type {Number}
345  */
346 cc.EventMouse.BUTTON_MIDDLE = 1;
347 
348 /**
349  * The tag of Mouse button 4
350  * @constant
351  * @type {Number}
352  */
353 cc.EventMouse.BUTTON_4 = 3;
354 
355 /**
356  * The tag of Mouse button 5
357  * @constant
358  * @type {Number}
359  */
360 cc.EventMouse.BUTTON_5 = 4;
361 
362 /**
363  * The tag of Mouse button 6
364  * @constant
365  * @type {Number}
366  */
367 cc.EventMouse.BUTTON_6 = 5;
368 
369 /**
370  * The tag of Mouse button 7
371  * @constant
372  * @type {Number}
373  */
374 cc.EventMouse.BUTTON_7 = 6;
375 
376 /**
377  * The tag of Mouse button 8
378  * @constant
379  * @type {Number}
380  */
381 cc.EventMouse.BUTTON_8 = 7;
382 
383 /**
384  * The touch event
385  * @class
386  * @extends cc.Event
387  */
388 cc.EventTouch = cc.Event.extend(/** @lends cc.EventTouch# */{
389     _eventCode: 0,
390     _touches: null,
391 
392     ctor: function (arr) {
393         cc.Event.prototype.ctor.call(this, cc.Event.TOUCH);
394         this._touches = arr || [];
395     },
396 
397     /**
398      * Returns event code
399      * @returns {number}
400      */
401     getEventCode: function () {
402         return this._eventCode;
403     },
404 
405     /**
406      * Returns touches of event
407      * @returns {Array}
408      */
409     getTouches: function () {
410         return this._touches;
411     },
412 
413     _setEventCode: function (eventCode) {
414         this._eventCode = eventCode;
415     },
416 
417     _setTouches: function (touches) {
418         this._touches = touches;
419     }
420 });
421 
422 /**
423  * The maximum touch numbers
424  * @constant
425  * @type {Number}
426  */
427 cc.EventTouch.MAX_TOUCHES = 5;
428 
429 cc.EventTouch.EventCode = {BEGAN: 0, MOVED: 1, ENDED: 2, CANCELLED: 3};
430 
431 /**
432  * Focus change event for UI widget
433  * @class
434  * @extends cc.Event
435  */
436 cc.EventFocus = cc.Event.extend(/** @lends cc.EventTouch# */{
437     _widgetGetFocus: null,
438     _widgetLoseFocus: null,
439     /**
440      * Constructor function.
441      * @param {ccui.Widget} widgetLoseFocus
442      * @param {ccui.Widget} widgetGetFocus
443      */
444     ctor: function(widgetLoseFocus, widgetGetFocus){
445         cc.Event.prototype.ctor.call(this, cc.Event.FOCUS);
446         this._widgetGetFocus = widgetGetFocus;
447         this._widgetLoseFocus = widgetLoseFocus;
448     }
449 });