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