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 * <p> 28 * The base class of event listener. <br/> 29 * If you need custom listener which with different callback, you need to inherit this class. <br/> 30 * For instance, you could refer to EventListenerAcceleration, EventListenerKeyboard, <br/> 31 * EventListenerTouchOneByOne, EventListenerCustom. 32 * </p> 33 * @class 34 * @extends cc.Class 35 */ 36 cc.EventListener = cc.Class.extend(/** @lends cc.EventListener# */{ 37 _onEvent: null, // Event callback function 38 _type: 0, // Event listener type 39 _listenerID: null, // Event listener ID 40 _registered: false, // Whether the listener has been added to dispatcher. 41 42 _fixedPriority: 0, // The higher the number, the higher the priority, 0 is for scene graph base priority. 43 _node: null, // scene graph based priority 44 _paused: false, // Whether the listener is paused 45 _isEnabled: true, // Whether the listener is enabled 46 47 /** 48 * Initializes event with type and callback function 49 * @param {number} type 50 * @param {string} listenerID 51 * @param {function} callback 52 */ 53 ctor: function (type, listenerID, callback) { 54 this._onEvent = callback; 55 this._type = type || 0; 56 this._listenerID = listenerID || ""; 57 }, 58 59 /** 60 * <p> 61 * Sets paused state for the listener 62 * The paused state is only used for scene graph priority listeners. 63 * `EventDispatcher::resumeAllEventListenersForTarget(node)` will set the paused state to `true`, 64 * while `EventDispatcher::pauseAllEventListenersForTarget(node)` will set it to `false`. 65 * @note 1) Fixed priority listeners will never get paused. If a fixed priority doesn't want to receive events, 66 * call `setEnabled(false)` instead. 67 * 2) In `Node`'s onEnter and onExit, the `paused state` of the listeners which associated with that node will be automatically updated. 68 * </p> 69 * @param {boolean} paused 70 * @private 71 */ 72 _setPaused: function (paused) { 73 this._paused = paused; 74 }, 75 76 /** 77 * Checks whether the listener is paused 78 * @returns {boolean} 79 * @private 80 */ 81 _isPaused: function () { 82 return this._paused; 83 }, 84 85 /** 86 * Marks the listener was registered by EventDispatcher 87 * @param {boolean} registered 88 * @private 89 */ 90 _setRegistered: function (registered) { 91 this._registered = registered; 92 }, 93 94 /** 95 * Checks whether the listener was registered by EventDispatcher 96 * @returns {boolean} 97 * @private 98 */ 99 _isRegistered: function () { 100 return this._registered; 101 }, 102 103 /** 104 * Gets the type of this listener 105 * @note It's different from `EventType`, e.g. TouchEvent has two kinds of event listeners - EventListenerOneByOne, EventListenerAllAtOnce 106 * @returns {number} 107 * @private 108 */ 109 _getType: function () { 110 return this._type; 111 }, 112 113 /** 114 * Gets the listener ID of this listener 115 * When event is being dispatched, listener ID is used as key for searching listeners according to event type. 116 * @returns {string} 117 * @private 118 */ 119 _getListenerID: function () { 120 return this._listenerID; 121 }, 122 123 /** 124 * Sets the fixed priority for this listener 125 * @note This method is only used for `fixed priority listeners`, it needs to access a non-zero value. 0 is reserved for scene graph priority listeners 126 * @param {number} fixedPriority 127 * @private 128 */ 129 _setFixedPriority: function (fixedPriority) { 130 this._fixedPriority = fixedPriority; 131 }, 132 133 /** 134 * Gets the fixed priority of this listener 135 * @returns {number} 0 if it's a scene graph priority listener, non-zero for fixed priority listener 136 * @private 137 */ 138 _getFixedPriority: function () { 139 return this._fixedPriority; 140 }, 141 142 /** 143 * Sets scene graph priority for this listener 144 * @param {cc.Node} node 145 * @private 146 */ 147 _setSceneGraphPriority: function (node) { 148 this._node = node; 149 }, 150 151 /** 152 * Gets scene graph priority of this listener 153 * @returns {cc.Node} if it's a fixed priority listener, non-null for scene graph priority listener 154 * @private 155 */ 156 _getSceneGraphPriority: function () { 157 return this._node; 158 }, 159 160 /** 161 * Checks whether the listener is available. 162 * @returns {boolean} 163 */ 164 checkAvailable: function () { 165 return this._onEvent != null; 166 }, 167 168 /** 169 * Clones the listener, its subclasses have to override this method. 170 * @returns {cc.EventListener} 171 */ 172 clone: function () { 173 return null; 174 }, 175 176 /** 177 * Enables or disables the listener 178 * @note Only listeners with `enabled` state will be able to receive events. 179 * When an listener was initialized, it's enabled by default. 180 * An event listener can receive events when it is enabled and is not paused. 181 * paused state is always false when it is a fixed priority listener. 182 * @param {boolean} enabled 183 */ 184 setEnabled: function(enabled){ 185 this._isEnabled = enabled; 186 }, 187 188 /** 189 * Checks whether the listener is enabled 190 * @returns {boolean} 191 */ 192 isEnabled: function(){ 193 return this._isEnabled; 194 }, 195 196 /** 197 * <p>Currently JavaScript Bindings (JSB), in some cases, needs to use retain and release. This is a bug in JSB, 198 * and the ugly workaround is to use retain/release. So, these 2 methods were added to be compatible with JSB. 199 * This is a hack, and should be removed once JSB fixes the retain/release bug<br/> 200 * You will need to retain an object if you created a listener and haven't added it any target node during the same frame.<br/> 201 * Otherwise, JSB's native autorelease pool will consider this object a useless one and release it directly,<br/> 202 * when you want to use it later, a "Invalid Native Object" error will be raised.<br/> 203 * The retain function can increase a reference count for the native object to avoid it being released,<br/> 204 * you need to manually invoke release function when you think this object is no longer needed, otherwise, there will be memory learks.<br/> 205 * retain and release function call should be paired in developer's game code.</p> 206 * @function 207 * @see cc.EventListener#release 208 */ 209 retain:function () { 210 }, 211 /** 212 * <p>Currently JavaScript Bindings (JSB), in some cases, needs to use retain and release. This is a bug in JSB, 213 * and the ugly workaround is to use retain/release. So, these 2 methods were added to be compatible with JSB. 214 * This is a hack, and should be removed once JSB fixes the retain/release bug<br/> 215 * You will need to retain an object if you created a listener and haven't added it any target node during the same frame.<br/> 216 * Otherwise, JSB's native autorelease pool will consider this object a useless one and release it directly,<br/> 217 * when you want to use it later, a "Invalid Native Object" error will be raised.<br/> 218 * The retain function can increase a reference count for the native object to avoid it being released,<br/> 219 * you need to manually invoke release function when you think this object is no longer needed, otherwise, there will be memory learks.<br/> 220 * retain and release function call should be paired in developer's game code.</p> 221 * @function 222 * @see cc.EventListener#retain 223 */ 224 release:function () { 225 } 226 }); 227 228 // event listener type 229 /** 230 * The type code of unknown event listener. 231 * @constant 232 * @type {number} 233 */ 234 cc.EventListener.UNKNOWN = 0; 235 /** 236 * The type code of one by one touch event listener. 237 * @constant 238 * @type {number} 239 */ 240 cc.EventListener.TOUCH_ONE_BY_ONE = 1; 241 /** 242 * The type code of all at once touch event listener. 243 * @constant 244 * @type {number} 245 */ 246 cc.EventListener.TOUCH_ALL_AT_ONCE = 2; 247 /** 248 * The type code of keyboard event listener. 249 * @constant 250 * @type {number} 251 */ 252 cc.EventListener.KEYBOARD = 3; 253 /** 254 * The type code of mouse event listener. 255 * @constant 256 * @type {number} 257 */ 258 cc.EventListener.MOUSE = 4; 259 /** 260 * The type code of acceleration event listener. 261 * @constant 262 * @type {number} 263 */ 264 cc.EventListener.ACCELERATION = 5; 265 /** 266 * The type code of custom event listener. 267 * @constant 268 * @type {number} 269 */ 270 cc.EventListener.CUSTOM = 6; 271 272 cc._EventListenerCustom = cc.EventListener.extend({ 273 _onCustomEvent: null, 274 ctor: function (listenerId, callback) { 275 this._onCustomEvent = callback; 276 var selfPointer = this; 277 var listener = function (event) { 278 if (selfPointer._onCustomEvent != null) 279 selfPointer._onCustomEvent(event); 280 }; 281 282 cc.EventListener.prototype.ctor.call(this, cc.EventListener.CUSTOM, listenerId, listener); 283 }, 284 285 checkAvailable: function () { 286 return (cc.EventListener.prototype.checkAvailable.call(this) && this._onCustomEvent != null); 287 }, 288 289 clone: function () { 290 return new cc._EventListenerCustom(this._listenerID, this._onCustomEvent); 291 } 292 }); 293 294 cc._EventListenerCustom.create = function (eventName, callback) { 295 return new cc._EventListenerCustom(eventName, callback); 296 }; 297 298 cc._EventListenerMouse = cc.EventListener.extend({ 299 onMouseDown: null, 300 onMouseUp: null, 301 onMouseMove: null, 302 onMouseScroll: null, 303 304 ctor: function () { 305 var selfPointer = this; 306 var listener = function (event) { 307 var eventType = cc.EventMouse; 308 switch (event._eventType) { 309 case eventType.DOWN: 310 if (selfPointer.onMouseDown) 311 selfPointer.onMouseDown(event); 312 break; 313 case eventType.UP: 314 if (selfPointer.onMouseUp) 315 selfPointer.onMouseUp(event); 316 break; 317 case eventType.MOVE: 318 if (selfPointer.onMouseMove) 319 selfPointer.onMouseMove(event); 320 break; 321 case eventType.SCROLL: 322 if (selfPointer.onMouseScroll) 323 selfPointer.onMouseScroll(event); 324 break; 325 default: 326 break; 327 } 328 }; 329 cc.EventListener.prototype.ctor.call(this, cc.EventListener.MOUSE, cc._EventListenerMouse.LISTENER_ID, listener); 330 }, 331 332 clone: function () { 333 var eventListener = new cc._EventListenerMouse(); 334 eventListener.onMouseDown = this.onMouseDown; 335 eventListener.onMouseUp = this.onMouseUp; 336 eventListener.onMouseMove = this.onMouseMove; 337 eventListener.onMouseScroll = this.onMouseScroll; 338 return eventListener; 339 }, 340 341 checkAvailable: function () { 342 return true; 343 } 344 }); 345 346 cc._EventListenerMouse.LISTENER_ID = "__cc_mouse"; 347 348 cc._EventListenerMouse.create = function () { 349 return new cc._EventListenerMouse(); 350 }; 351 352 cc._EventListenerTouchOneByOne = cc.EventListener.extend({ 353 _claimedTouches: null, 354 swallowTouches: false, 355 onTouchBegan: null, 356 onTouchMoved: null, 357 onTouchEnded: null, 358 onTouchCancelled: null, 359 360 ctor: function () { 361 cc.EventListener.prototype.ctor.call(this, cc.EventListener.TOUCH_ONE_BY_ONE, cc._EventListenerTouchOneByOne.LISTENER_ID, null); 362 this._claimedTouches = []; 363 }, 364 365 setSwallowTouches: function (needSwallow) { 366 this.swallowTouches = needSwallow; 367 }, 368 369 clone: function () { 370 var eventListener = new cc._EventListenerTouchOneByOne(); 371 eventListener.onTouchBegan = this.onTouchBegan; 372 eventListener.onTouchMoved = this.onTouchMoved; 373 eventListener.onTouchEnded = this.onTouchEnded; 374 eventListener.onTouchCancelled = this.onTouchCancelled; 375 eventListener.swallowTouches = this.swallowTouches; 376 return eventListener; 377 }, 378 379 checkAvailable: function () { 380 if(!this.onTouchBegan){ 381 cc.log(cc._LogInfos._EventListenerTouchOneByOne_checkAvailable); 382 return false; 383 } 384 return true; 385 } 386 }); 387 388 cc._EventListenerTouchOneByOne.LISTENER_ID = "__cc_touch_one_by_one"; 389 390 cc._EventListenerTouchOneByOne.create = function () { 391 return new cc._EventListenerTouchOneByOne(); 392 }; 393 394 cc._EventListenerTouchAllAtOnce = cc.EventListener.extend({ 395 onTouchesBegan: null, 396 onTouchesMoved: null, 397 onTouchesEnded: null, 398 onTouchesCancelled: null, 399 400 ctor: function(){ 401 cc.EventListener.prototype.ctor.call(this, cc.EventListener.TOUCH_ALL_AT_ONCE, cc._EventListenerTouchAllAtOnce.LISTENER_ID, null); 402 }, 403 404 clone: function(){ 405 var eventListener = new cc._EventListenerTouchAllAtOnce(); 406 eventListener.onTouchesBegan = this.onTouchesBegan; 407 eventListener.onTouchesMoved = this.onTouchesMoved; 408 eventListener.onTouchesEnded = this.onTouchesEnded; 409 eventListener.onTouchesCancelled = this.onTouchesCancelled; 410 return eventListener; 411 }, 412 413 checkAvailable: function(){ 414 if (this.onTouchesBegan == null && this.onTouchesMoved == null 415 && this.onTouchesEnded == null && this.onTouchesCancelled == null) { 416 cc.log(cc._LogInfos._EventListenerTouchAllAtOnce_checkAvailable); 417 return false; 418 } 419 return true; 420 } 421 }); 422 423 cc._EventListenerTouchAllAtOnce.LISTENER_ID = "__cc_touch_all_at_once"; 424 425 cc._EventListenerTouchAllAtOnce.create = function(){ 426 return new cc._EventListenerTouchAllAtOnce(); 427 }; 428 429 /** 430 * Create a EventListener object by json object 431 * @function 432 * @static 433 * @param {object} argObj a json object 434 * @returns {cc.EventListener} 435 * @example 436 * cc.EventListener.create({ 437 * event: cc.EventListener.TOUCH_ONE_BY_ONE, 438 * swallowTouches: true, 439 * onTouchBegan: function (touch, event) { 440 * //do something 441 * return true; 442 * } 443 * }); 444 */ 445 cc.EventListener.create = function(argObj){ 446 447 cc.assert(argObj&&argObj.event, cc._LogInfos.EventListener_create); 448 449 var listenerType = argObj.event; 450 delete argObj.event; 451 452 var listener = null; 453 if(listenerType === cc.EventListener.TOUCH_ONE_BY_ONE) 454 listener = new cc._EventListenerTouchOneByOne(); 455 else if(listenerType === cc.EventListener.TOUCH_ALL_AT_ONCE) 456 listener = new cc._EventListenerTouchAllAtOnce(); 457 else if(listenerType === cc.EventListener.MOUSE) 458 listener = new cc._EventListenerMouse(); 459 else if(listenerType === cc.EventListener.CUSTOM){ 460 listener = new cc._EventListenerCustom(argObj.eventName, argObj.callback); 461 delete argObj.eventName; 462 delete argObj.callback; 463 } else if(listenerType === cc.EventListener.KEYBOARD) 464 listener = new cc._EventListenerKeyboard(); 465 else if(listenerType === cc.EventListener.ACCELERATION){ 466 listener = new cc._EventListenerAcceleration(argObj.callback); 467 delete argObj.callback; 468 } 469 470 for(var key in argObj) { 471 listener[key] = argObj[key]; 472 } 473 474 return listener; 475 };