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 Custom event.
111  * @constant
112  * @type {number}
113  */
114 cc.Event.CUSTOM = 4;
115 
116 /**
117  * The Custom event
118  * @class
119  * @extends cc.Event
120  */
121 cc.EventCustom = cc.Event.extend(/** @lends cc.EventCustom# */{
122     _eventName: null,
123     _userData: null,                                 // User data
124 
125     ctor: function (eventName) {
126         cc.Event.prototype.ctor.call(this, cc.Event.CUSTOM);
127         this._eventName = eventName;
128     },
129 
130     /**
131      * Sets user data
132      * @param {*} data
133      */
134     setUserData: function (data) {
135         this._userData = data;
136     },
137 
138     /**
139      * Gets user data
140      * @returns {*}
141      */
142     getUserData: function () {
143         return this._userData;
144     },
145 
146     /**
147      * Gets event name
148      * @returns {String}
149      */
150     getEventName: function () {
151         return this._eventName;
152     }
153 });
154 
155 /**
156  * The mouse event
157  * @class
158  * @extends cc.Event
159  */
160 cc.EventMouse = cc.Event.extend(/** @lends cc.EventMouse# */{
161     _eventType: 0,
162     _button: 0,
163     _x: 0,
164     _y: 0,
165     _prevX: 0,
166     _prevY: 0,
167     _scrollX: 0,
168     _scrollY: 0,
169 
170     ctor: function (eventType) {
171         cc.Event.prototype.ctor.call(this, cc.Event.MOUSE);
172         this._eventType = eventType;
173     },
174 
175     /**
176      * Sets scroll data
177      * @param {number} scrollX
178      * @param {number} scrollY
179      */
180     setScrollData: function (scrollX, scrollY) {
181         this._scrollX = scrollX;
182         this._scrollY = scrollY;
183     },
184 
185     /**
186      * Returns the x axis scroll value
187      * @returns {number}
188      */
189     getScrollX: function () {
190         return this._scrollX;
191     },
192 
193     /**
194      * Returns the y axis scroll value
195      * @returns {number}
196      */
197     getScrollY: function () {
198         return this._scrollY;
199     },
200 
201     /**
202      * Sets cursor location
203      * @param {number} x
204      * @param {number} y
205      */
206     setLocation: function (x, y) {
207         this._x = x;
208         this._y = y;
209     },
210 
211 	/**
212 	 * Returns cursor location
213 	 * @return {cc.Point} location
214 	 */
215     getLocation: function () {
216         return {x: this._x, y: this._y};
217     },
218 
219 	/**
220 	 * Returns the current cursor location in screen coordinates
221 	 * @return {cc.Point}
222 	 */
223 	getLocationInView: function() {
224 		return {x: this._x, y: cc.view._designResolutionSize.height - this._y};
225 	},
226 
227     _setPrevCursor: function (x, y) {
228         this._prevX = x;
229         this._prevY = y;
230     },
231 
232     /**
233      * Returns the delta distance from the previous location to current location
234      * @return {cc.Point}
235      */
236     getDelta: function () {
237         return {x: this._x - this._prevX, y: this._y - this._prevY};
238     },
239 
240     /**
241      * Returns the X axis delta distance from the previous location to current location
242      * @return {Number}
243      */
244     getDeltaX: function () {
245         return this._x - this._prevX;
246     },
247 
248     /**
249      * Returns the Y axis delta distance from the previous location to current location
250      * @return {Number}
251      */
252     getDeltaY: function () {
253         return this._y - this._prevY;
254     },
255 
256     /**
257      * Sets mouse button
258      * @param {number} button
259      */
260     setButton: function (button) {
261         this._button = button;
262     },
263 
264     /**
265      * Returns mouse button
266      * @returns {number}
267      */
268     getButton: function () {
269         return this._button;
270     },
271 
272     /**
273      * Returns location X axis data
274      * @returns {number}
275      */
276     getLocationX: function () {
277         return this._x;
278     },
279 
280     /**
281      * Returns location Y axis data
282      * @returns {number}
283      */
284     getLocationY: function () {
285         return this._y;
286     }
287 });
288 
289 //Different types of MouseEvent
290 /**
291  * The none event code of  mouse event.
292  * @constant
293  * @type {number}
294  */
295 cc.EventMouse.NONE = 0;
296 /**
297  * The event type code of mouse down event.
298  * @constant
299  * @type {number}
300  */
301 cc.EventMouse.DOWN = 1;
302 /**
303  * The event type code of mouse up event.
304  * @constant
305  * @type {number}
306  */
307 cc.EventMouse.UP = 2;
308 /**
309  * The event type code of mouse move event.
310  * @constant
311  * @type {number}
312  */
313 cc.EventMouse.MOVE = 3;
314 /**
315  * The event type code of mouse scroll event.
316  * @constant
317  * @type {number}
318  */
319 cc.EventMouse.SCROLL = 4;
320 
321 /**
322  * The tag of Mouse left button
323  * @constant
324  * @type {Number}
325  */
326 cc.EventMouse.BUTTON_LEFT = 0;
327 
328 /**
329  * The tag of Mouse right button  (The right button number is 2 on browser)
330  * @constant
331  * @type {Number}
332  */
333 cc.EventMouse.BUTTON_RIGHT = 2;
334 
335 /**
336  * The tag of Mouse middle button  (The right button number is 1 on browser)
337  * @constant
338  * @type {Number}
339  */
340 cc.EventMouse.BUTTON_MIDDLE = 1;
341 
342 /**
343  * The tag of Mouse button 4
344  * @constant
345  * @type {Number}
346  */
347 cc.EventMouse.BUTTON_4 = 3;
348 
349 /**
350  * The tag of Mouse button 5
351  * @constant
352  * @type {Number}
353  */
354 cc.EventMouse.BUTTON_5 = 4;
355 
356 /**
357  * The tag of Mouse button 6
358  * @constant
359  * @type {Number}
360  */
361 cc.EventMouse.BUTTON_6 = 5;
362 
363 /**
364  * The tag of Mouse button 7
365  * @constant
366  * @type {Number}
367  */
368 cc.EventMouse.BUTTON_7 = 6;
369 
370 /**
371  * The tag of Mouse button 8
372  * @constant
373  * @type {Number}
374  */
375 cc.EventMouse.BUTTON_8 = 7;
376 
377 /**
378  * The touch event
379  * @class
380  * @extends cc.Event
381  */
382 cc.EventTouch = cc.Event.extend(/** @lends cc.EventTouch# */{
383     _eventCode: 0,
384     _touches: null,
385 
386     ctor: function (arr) {
387         cc.Event.prototype.ctor.call(this, cc.Event.TOUCH);
388         this._touches = arr || [];
389     },
390 
391     /**
392      * Returns event code
393      * @returns {number}
394      */
395     getEventCode: function () {
396         return this._eventCode;
397     },
398 
399     /**
400      * Returns touches of event
401      * @returns {Array}
402      */
403     getTouches: function () {
404         return this._touches;
405     },
406 
407     _setEventCode: function (eventCode) {
408         this._eventCode = eventCode;
409     },
410 
411     _setTouches: function (touches) {
412         this._touches = touches;
413     }
414 });
415 
416 /**
417  * The maximum touch numbers
418  * @constant
419  * @type {Number}
420  */
421 cc.EventTouch.MAX_TOUCHES = 5;
422 
423 cc.EventTouch.EventCode = {BEGAN: 0, MOVED: 1, ENDED: 2, CANCELLED: 3};