1 /**************************************************************************** 2 Copyright (c) 2010-2012 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 for ccs.ComController 27 * @class 28 * @extends ccs.Component 29 */ 30 ccs.ComController = ccs.Component.extend(/** @lends ccs.ComController# */{ 31 ctor: function () { 32 cc.Component.prototype.ctor.call(this); 33 this._name = "ComController"; 34 }, 35 init: function () { 36 return true; 37 }, 38 39 onEnter: function () { 40 41 }, 42 43 onExit: function () { 44 var director = cc.Director.getInstance(); 45 if (this._isTouchEnabled) 46 cc.unregisterTouchDelegate(this); 47 48 // remove this layer from the delegates who concern Accelerometer Sensor 49 if (this._isAccelerometerEnabled && cc.Accelerometer) 50 director.getAccelerometer().setDelegate(null); 51 52 // remove this layer from the delegates who concern the kaypad msg 53 if (this._isKeyboardEnabled && cc.KeyboardDispatcher) 54 director.getKeyboardDispatcher().removeDelegate(this); 55 56 if (this._isMouseEnabled && cc.MouseDispatcher) 57 director.getMouseDispatcher().removeMouseDelegate(this); 58 }, 59 60 update: function (dt) { 61 }, 62 63 /** 64 * Enabled getter 65 * @returns {Boolean} 66 */ 67 isEnabled: function () { 68 return this._enabled; 69 }, 70 71 /** 72 * Enabled setter 73 * @param {Boolean} bool 74 */ 75 setEnabled: function (bool) { 76 this._enabled = b; 77 }, 78 /** 79 * If isTouchEnabled, this method is called onEnter. 80 */ 81 registerWithTouchDispatcher:function () { 82 if (this._touchMode === cc.TOUCH_ALL_AT_ONCE) 83 cc.registerStandardDelegate(this,this._touchPriority); 84 else 85 cc.registerTargetedDelegate(this._touchPriority, true, this); 86 }, 87 /** 88 * MouseEnabled getter 89 * @returns {Boolean} 90 */ 91 isMouseEnabled:function () { 92 return this._isMouseEnabled; 93 }, 94 95 /** 96 * MouseEnabled setter 97 * @param {Boolean} enabled 98 */ 99 setMouseEnabled:function (enabled) { 100 if(!cc.MouseDispatcher) 101 throw "cc.MouseDispatcher is undefined, maybe it has been removed from js loading list."; 102 103 if (this._isMouseEnabled != enabled) { 104 this._isMouseEnabled = enabled; 105 if (this._running) { 106 if (enabled) 107 cc.Director.getInstance().getMouseDispatcher().addMouseDelegate(this, this._mousePriority); 108 else 109 cc.Director.getInstance().getMouseDispatcher().removeMouseDelegate(this); 110 } 111 } 112 }, 113 114 setMousePriority:function (priority) { 115 if(!cc.MouseDispatcher) 116 throw "cc.MouseDispatcher is undefined, maybe it has been removed from js loading list."; 117 118 if (this._mousePriority !== priority) { 119 this._mousePriority = priority; 120 // Update touch priority with handler 121 if (this._isMouseEnabled) { 122 this.setMouseEnabled(false); 123 this.setMouseEnabled(true); 124 } 125 } 126 }, 127 128 getMousePriority:function () { 129 return this._mousePriority; 130 }, 131 132 /** 133 * whether or not it will receive Touch events.<br/> 134 * You can enable / disable touch events with this property.<br/> 135 * Only the touches of this node will be affected. This "method" is not propagated to it's children.<br/> 136 * @return {Boolean} 137 */ 138 isTouchEnabled:function () { 139 return this._isTouchEnabled; 140 }, 141 142 /** 143 * Enable touch events 144 * @param {Boolean} enabled 145 */ 146 setTouchEnabled:function (enabled) { 147 if (this._isTouchEnabled !== enabled) { 148 this._isTouchEnabled = enabled; 149 if (enabled) { 150 this.registerWithTouchDispatcher(); 151 } else { 152 // have problems? 153 cc.unregisterTouchDelegate(this); 154 } 155 } 156 }, 157 158 /** returns the priority of the touch event handler 159 * @return {Number} 160 */ 161 getTouchPriority:function () { 162 return this._touchPriority; 163 }, 164 165 /** Sets the touch event handler priority. Default is 0. 166 * @param {Number} priority 167 */ 168 setTouchPriority:function (priority) { 169 if (this._touchPriority !== priority) { 170 this._touchPriority = priority; 171 // Update touch priority with handler 172 if (this._isTouchEnabled) { 173 this.setTouchEnabled(false); 174 this.setTouchEnabled(true); 175 } 176 } 177 }, 178 179 /** returns the touch mode. 180 * @return {Number} 181 */ 182 getTouchMode:function () { 183 return this._touchMode; 184 }, 185 186 /** Sets the touch mode. 187 * @param {Number} mode 188 */ 189 setTouchMode:function (mode) { 190 if (this._touchMode !== mode) { 191 this._touchMode = mode; 192 // update the mode with handler 193 if (this._isTouchEnabled) { 194 this.setTouchEnabled(false); 195 this.setTouchEnabled(true); 196 } 197 } 198 }, 199 200 /** 201 * whether or not it will receive Accelerometer events<br/> 202 * You can enable / disable accelerometer events with this property. 203 * @return {Boolean} 204 */ 205 isAccelerometerEnabled:function () { 206 return this._isAccelerometerEnabled; 207 }, 208 209 /** 210 * isAccelerometerEnabled setter 211 * @param {Boolean} enabled 212 */ 213 setAccelerometerEnabled:function (enabled) { 214 if(!cc.Accelerometer) 215 throw "cc.Accelerometer is undefined, maybe it has been removed from js loading list."; 216 if (enabled !== this._isAccelerometerEnabled) { 217 this._isAccelerometerEnabled = enabled; 218 if (this._running) { 219 var director = cc.Director.getInstance(); 220 if (enabled) 221 director.getAccelerometer().setDelegate(this); 222 else 223 director.getAccelerometer().setDelegate(null); 224 } 225 } 226 }, 227 228 /** 229 * accelerometerInterval setter 230 * @param {Number} interval 231 */ 232 setAccelerometerInterval:function (interval) { 233 if (this._isAccelerometerEnabled && cc.Accelerometer) 234 cc.Director.getInstance().getAccelerometer().setAccelerometerInterval(interval); 235 }, 236 237 onAccelerometer:function (accelerationValue) { 238 cc.log("ccs.ComController.onAccelerometer(): should override me."); 239 }, 240 241 /** 242 * whether or not it will receive keyboard events<br/> 243 * You can enable / disable accelerometer events with this property.<br/> 244 * it's new in cocos2d-x 245 * @return {Boolean} 246 */ 247 isKeyboardEnabled:function () { 248 return this._isKeyboardEnabled; 249 }, 250 251 /** 252 * Enable Keyboard interaction 253 * @param {Boolean} enabled 254 */ 255 setKeyboardEnabled:function (enabled) { 256 if(!cc.KeyboardDispatcher) 257 throw "cc.KeyboardDispatcher is undefined, maybe it has been removed from js loading list."; 258 259 if (enabled !== this._isKeyboardEnabled) { 260 this._isKeyboardEnabled = enabled; 261 if (this._running) { 262 var director = cc.Director.getInstance(); 263 if (enabled) { 264 director.getKeyboardDispatcher().addDelegate(this); 265 } else { 266 director.getKeyboardDispatcher().removeDelegate(this); 267 } 268 } 269 } 270 }, 271 272 // ---------------------CCTouchDelegate interface------------------------------ 273 274 /** 275 * default implements are used to call script callback if exist<br/> 276 * you must override these touch functions if you wish to utilize them 277 * @param {cc.Touch} touch 278 * @param {event} event 279 * @return {Boolean} 280 */ 281 onTouchBegan:function (touch, event) { 282 return true; 283 }, 284 285 /** 286 * callback when a touch event moved 287 * @param {cc.Touch} touch 288 * @param {event} event 289 */ 290 onTouchMoved:function (touch, event) { 291 }, 292 293 /** 294 * callback when a touch event finished 295 * @param {cc.Touch} touch 296 * @param {event} event 297 */ 298 onTouchEnded:function (touch, event) { 299 }, 300 301 /** 302 * @param {cc.Touch} touch 303 * @param {event} event 304 */ 305 onTouchCancelled:function (touch, event) { 306 }, 307 308 /** 309 * Touches is the same as Touch, except this one can handle multi-touch 310 * @param {cc.Touch} touch 311 * @param {event} event 312 */ 313 onTouchesBegan:function (touch, event) { 314 }, 315 316 /** 317 * when a touch moved 318 * @param {cc.Touch} touch 319 * @param {event} event 320 */ 321 onTouchesMoved:function (touch, event) { 322 }, 323 324 /** 325 * when a touch finished 326 * @param {cc.Touch} touch 327 * @param {event} event 328 */ 329 onTouchesEnded:function (touch, event) { 330 }, 331 332 /** 333 * @param touch 334 * @param event 335 */ 336 onTouchesCancelled:function (touch, event) { 337 }, 338 339 // ---------------------CCMouseEventDelegate interface------------------------------ 340 341 /** 342 * <p>called when the "mouseDown" event is received. <br/> 343 * Return YES to avoid propagating the event to other delegates. </p> 344 * @param event 345 * @return {Boolean} 346 */ 347 onMouseDown:function (event) { 348 return false; 349 }, 350 351 /** 352 * <p>called when the "mouseDragged" event is received. <br/> 353 * Return YES to avoid propagating the event to other delegates.</p> 354 * @param event 355 * @return {Boolean} 356 */ 357 onMouseDragged:function (event) { 358 return false; 359 }, 360 361 /** 362 * <p> called when the "mouseMoved" event is received. <br/> 363 * Return YES to avoid propagating the event to other delegates. </p> 364 * @param event 365 * @return {Boolean} 366 */ 367 onMouseMoved:function (event) { 368 return false; 369 }, 370 371 /** 372 * <p> called when the "mouseUp" event is received. <br/> 373 * Return YES to avoid propagating the event to other delegates. </p> 374 * @param event 375 * @return {Boolean} 376 */ 377 onMouseUp:function (event) { 378 return false; 379 }, 380 381 //right 382 /** 383 * <p> called when the "rightMouseDown" event is received. <br/> 384 * Return YES to avoid propagating the event to other delegates. </p> 385 * @param event 386 * @return {Boolean} 387 */ 388 onRightMouseDown:function (event) { 389 return false; 390 }, 391 392 /** 393 * <p> called when the "rightMouseDragged" event is received. <br/> 394 * Return YES to avoid propagating the event to other delegates. </p> 395 * @param event 396 * @return {Boolean} 397 */ 398 onRightMouseDragged:function (event) { 399 return false; 400 }, 401 402 /** 403 * <p> called when the "rightMouseUp" event is received. <br/> 404 * Return YES to avoid propagating the event to other delegates. </p> 405 * @param event 406 * @return {Boolean} 407 */ 408 onRightMouseUp:function (event) { 409 return false; 410 }, 411 412 //other 413 /** 414 * <p>called when the "otherMouseDown" event is received. <br/> 415 * Return YES to avoid propagating the event to other delegates. </p> 416 * @param event 417 * @return {Boolean} 418 */ 419 onOtherMouseDown:function (event) { 420 return false; 421 }, 422 423 /** 424 * <p> called when the "otherMouseDragged" event is received. <br/> 425 * Return YES to avoid propagating the event to other delegates. </p> 426 * @param event 427 * @return {Boolean} 428 */ 429 onOtherMouseDragged:function (event) { 430 return false; 431 }, 432 433 /** 434 * <p> called when the "otherMouseUp" event is received. <br/> 435 * Return YES to avoid propagating the event to other delegates. </p> 436 * @param event 437 * @return {Boolean} 438 */ 439 onOtherMouseUp:function (event) { 440 return false; 441 }, 442 443 //scroll wheel 444 /** 445 * <p> called when the "scrollWheel" event is received. <br/> 446 * Return YES to avoid propagating the event to other delegates. </p> 447 * @param event 448 * @return {Boolean} 449 */ 450 onScrollWheel:function (event) { 451 return false; 452 }, 453 454 // enter / exit 455 /** 456 * <p> called when the "mouseEntered" event is received. <br/> 457 * Return YES to avoid propagating the event to other delegates. </p> 458 * @param theEvent 459 * @return {Boolean} 460 */ 461 onMouseEntered:function (theEvent) { 462 return false; 463 }, 464 465 /** 466 * <p> called when the "mouseExited" event is received. <br/> 467 * Return YES to avoid propagating the event to other delegates. </p> 468 * @param theEvent 469 * @return {Boolean} 470 */ 471 onMouseExited:function (theEvent) { 472 return false; 473 }, 474 475 // ---------------------CCKeyboardDelegate interface------------------------------ 476 477 /** 478 * Call back when a key is pressed down 479 * @param {Number} keyCode 480 * @example 481 * // example 482 * if(keyCode == cc.KEY.w){} 483 */ 484 onKeyDown:function (keyCode) { 485 }, 486 487 /** 488 * Call back when a key is released 489 * @param {Number} keyCode 490 * @example 491 * // example 492 * if(keyCode == cc.KEY.w){} 493 */ 494 onKeyUp:function (keyCode) { 495 } 496 }); 497 /** 498 * allocates and initializes a ComController. 499 * @constructs 500 * @return {ccs.ComController} 501 * @example 502 * // example 503 * var com = ccs.ComController.create(); 504 */ 505 ccs.ComController.create = function () { 506 var com = new ccs.ComController(); 507 if (com && com.init()) { 508 return com; 509 } 510 return null; 511 };