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