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