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 * The CheckBox control of Cocos UI. 28 * @class 29 * @extends ccui.Widget 30 * 31 * @property {Boolean} selected - Indicate whether the check box has been selected 32 */ 33 ccui.CheckBox = ccui.Widget.extend(/** @lends ccui.CheckBox# */{ 34 _backGroundBoxRenderer: null, 35 _backGroundSelectedBoxRenderer: null, 36 _frontCrossRenderer: null, 37 _backGroundBoxDisabledRenderer: null, 38 _frontCrossDisabledRenderer: null, 39 40 _isSelected: true, 41 42 _checkBoxEventListener: null, 43 _checkBoxEventSelector:null, 44 45 _backGroundTexType: ccui.Widget.LOCAL_TEXTURE, 46 _backGroundSelectedTexType: ccui.Widget.LOCAL_TEXTURE, 47 _frontCrossTexType: ccui.Widget.LOCAL_TEXTURE, 48 _backGroundDisabledTexType: ccui.Widget.LOCAL_TEXTURE, 49 _frontCrossDisabledTexType: ccui.Widget.LOCAL_TEXTURE, 50 51 _backGroundFileName: "", 52 _backGroundSelectedFileName: "", 53 _frontCrossFileName: "", 54 _backGroundDisabledFileName: "", 55 _frontCrossDisabledFileName: "", 56 _className: "CheckBox", 57 58 _backGroundBoxRendererAdaptDirty:true, 59 _backGroundSelectedBoxRendererAdaptDirty:true, 60 _frontCrossRendererAdaptDirty: true, 61 _backGroundBoxDisabledRendererAdaptDirty: true, 62 _frontCrossDisabledRendererAdaptDirty: true, 63 64 /** 65 * allocates and initializes a UICheckBox. 66 * Constructor of ccui.CheckBox, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. 67 * @param {String} backGround 68 * @param {String} backGroundSelected 69 * @param {String} cross 70 * @param {String} backGroundDisabled 71 * @param {String} frontCrossDisabled 72 * @param {Number} [texType=ccui.Widget.LOCAL_TEXTURE] 73 * @example 74 * // example 75 * var uiCheckBox = new ccui.CheckBox(); 76 */ 77 ctor: function (backGround, backGroundSelected,cross,backGroundDisabled,frontCrossDisabled,texType) { 78 ccui.Widget.prototype.ctor.call(this); 79 this.setTouchEnabled(true); 80 81 texType && this.init(backGround, backGroundSelected,cross,backGroundDisabled,frontCrossDisabled,texType); 82 }, 83 84 /** 85 * Initializes a checkBox. please do not call this function by yourself, you should pass the parameters to constructor to initialize it. 86 * @param {String} backGround 87 * @param {String} backGroundSelected 88 * @param {String} cross 89 * @param {String} backGroundDisabled 90 * @param {String} frontCrossDisabled 91 * @param {Number} [texType=ccui.Widget.LOCAL_TEXTURE] 92 * @returns {boolean} 93 * @override 94 */ 95 init: function (backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType) { 96 if (ccui.Widget.prototype.init.call(this)) { 97 this._isSelected = true; 98 this.setSelectedState(false); 99 if(backGround === undefined) 100 this.loadTextures(backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType); 101 return true; 102 } 103 return false; 104 }, 105 106 _initRenderer: function () { 107 this._backGroundBoxRenderer = cc.Sprite.create(); 108 this._backGroundSelectedBoxRenderer = cc.Sprite.create(); 109 this._frontCrossRenderer = cc.Sprite.create(); 110 this._backGroundBoxDisabledRenderer = cc.Sprite.create(); 111 this._frontCrossDisabledRenderer = cc.Sprite.create(); 112 113 this.addProtectedChild(this._backGroundBoxRenderer, ccui.CheckBox.BOX_RENDERER_ZORDER, -1); 114 this.addProtectedChild(this._backGroundSelectedBoxRenderer, ccui.CheckBox.BOX_SELECTED_RENDERER_ZORDER, -1); 115 this.addProtectedChild(this._frontCrossRenderer, ccui.CheckBox.FRONT_CROSS_RENDERER_ZORDER, -1); 116 this.addProtectedChild(this._backGroundBoxDisabledRenderer, ccui.CheckBox.BOX_DISABLED_RENDERER_ZORDER, -1); 117 this.addProtectedChild(this._frontCrossDisabledRenderer, ccui.CheckBox.FRONT_CROSS_DISABLED_RENDERER_ZORDER, -1); 118 }, 119 120 /** 121 * Loads textures for checkbox. 122 * @param {String} backGround 123 * @param {String} backGroundSelected 124 * @param {String} cross 125 * @param {String} backGroundDisabled 126 * @param {String} frontCrossDisabled 127 * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType 128 */ 129 loadTextures: function (backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType) { 130 this.loadTextureBackGround(backGround, texType); 131 this.loadTextureBackGroundSelected(backGroundSelected, texType); 132 this.loadTextureFrontCross(cross, texType); 133 this.loadTextureBackGroundDisabled(backGroundDisabled, texType); 134 this.loadTextureFrontCrossDisabled(frontCrossDisabled, texType); 135 }, 136 137 /** 138 * Loads background texture for checkbox. 139 * @param {String} backGround background filename 140 * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType 141 */ 142 loadTextureBackGround: function (backGround, texType) { 143 if (!backGround) 144 return; 145 146 texType = texType || ccui.Widget.LOCAL_TEXTURE; 147 this._backGroundFileName = backGround; 148 this._backGroundTexType = texType; 149 var bgBoxRenderer = this._backGroundBoxRenderer; 150 151 var self = this; 152 if(!bgBoxRenderer.texture || !bgBoxRenderer.texture.isLoaded()){ 153 bgBoxRenderer.addLoadedEventListener(function(){ 154 155 self._findLayout(); 156 157 self._updateFlippedX(); 158 self._updateFlippedY(); 159 self._updateChildrenDisplayedRGBA(); 160 self._updateContentSizeWithTextureSize(self._backGroundBoxRenderer.getContentSize()); 161 self._backGroundBoxRendererAdaptDirty = true; 162 }); 163 } 164 165 switch (this._backGroundTexType) { 166 case ccui.Widget.LOCAL_TEXTURE: 167 //SetTexture cannot load resource 168 bgBoxRenderer.initWithFile(backGround); 169 break; 170 case ccui.Widget.PLIST_TEXTURE: 171 //SetTexture cannot load resource 172 bgBoxRenderer.initWithSpriteFrameName(backGround); 173 break; 174 default: 175 break; 176 } 177 178 if (!bgBoxRenderer.textureLoaded()) { 179 this._backGroundBoxRenderer.setContentSize(this._customSize); 180 bgBoxRenderer.addLoadedEventListener(function () { 181 this._updateContentSizeWithTextureSize(this._backGroundBoxRenderer.getContentSize()); 182 }, this); 183 } 184 this._updateFlippedX(); 185 this._updateFlippedY(); 186 187 this._updateChildrenDisplayedRGBA(); 188 189 this._updateContentSizeWithTextureSize(this._backGroundBoxRenderer.getContentSize()); 190 this._backGroundBoxRendererAdaptDirty = true; 191 }, 192 193 /** 194 * Loads selected state of background texture for checkbox. 195 * @param {String} backGroundSelected 196 * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType 197 */ 198 loadTextureBackGroundSelected: function (backGroundSelected, texType) { 199 if (!backGroundSelected) 200 return; 201 202 texType = texType || ccui.Widget.LOCAL_TEXTURE; 203 this._backGroundSelectedFileName = backGroundSelected; 204 this._backGroundSelectedTexType = texType; 205 206 var self = this; 207 if(!this._backGroundSelectedBoxRenderer.texture || !this._backGroundSelectedBoxRenderer.texture.isLoaded()){ 208 this._backGroundSelectedBoxRenderer.addLoadedEventListener(function(){ 209 210 self._findLayout(); 211 212 self._updateFlippedX(); 213 self._updateFlippedY(); 214 self._updateChildrenDisplayedRGBA(); 215 self._backGroundSelectedBoxRendererAdaptDirty = true; 216 }); 217 } 218 219 switch (this._backGroundSelectedTexType) { 220 case ccui.Widget.LOCAL_TEXTURE: 221 //SetTexture cannot load resource 222 this._backGroundSelectedBoxRenderer.initWithFile(backGroundSelected); 223 break; 224 case ccui.Widget.PLIST_TEXTURE: 225 //SetTexture cannot load resource 226 this._backGroundSelectedBoxRenderer.initWithSpriteFrameName(backGroundSelected); 227 break; 228 default: 229 break; 230 } 231 232 this._updateFlippedX(); 233 this._updateFlippedY(); 234 235 this._updateChildrenDisplayedRGBA(); 236 237 this._backGroundSelectedBoxRendererAdaptDirty = true; 238 }, 239 240 /** 241 * Loads cross texture for checkbox. 242 * @param {String} cross 243 * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType 244 */ 245 loadTextureFrontCross: function (cross, texType) { 246 if (!cross) 247 return; 248 texType = texType || ccui.Widget.LOCAL_TEXTURE; 249 this._frontCrossFileName = cross; 250 this._frontCrossTexType = texType; 251 252 var self = this; 253 if(!this._frontCrossRenderer.texture || !this._frontCrossRenderer.texture.isLoaded()){ 254 this._frontCrossRenderer.addLoadedEventListener(function(){ 255 self._findLayout(); 256 257 self._updateFlippedX(); 258 self._updateFlippedY(); 259 self._updateChildrenDisplayedRGBA(); 260 self._frontCrossRendererAdaptDirty = true; 261 }); 262 } 263 264 switch (this._frontCrossTexType) { 265 case ccui.Widget.LOCAL_TEXTURE: 266 //SetTexture cannot load resource 267 this._frontCrossRenderer.initWithFile(cross); 268 break; 269 case ccui.Widget.PLIST_TEXTURE: 270 //SetTexture cannot load resource 271 this._frontCrossRenderer.initWithSpriteFrameName(cross); 272 break; 273 default: 274 break; 275 } 276 this._updateFlippedX(); 277 this._updateFlippedY(); 278 279 this._updateChildrenDisplayedRGBA(); 280 281 this._frontCrossRendererAdaptDirty = true; 282 }, 283 284 /** 285 * Loads disabled state of backGround texture for checkbox. 286 * @param {String} backGroundDisabled 287 * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType 288 */ 289 loadTextureBackGroundDisabled: function (backGroundDisabled, texType) { 290 if (!backGroundDisabled) 291 return; 292 texType = texType || ccui.Widget.LOCAL_TEXTURE; 293 this._backGroundDisabledFileName = backGroundDisabled; 294 this._backGroundDisabledTexType = texType; 295 296 var self = this; 297 if(!this._backGroundBoxDisabledRenderer.texture || !this._backGroundBoxDisabledRenderer.texture.isLoaded()){ 298 this._backGroundBoxDisabledRenderer.addLoadedEventListener(function(){ 299 self._findLayout(); 300 301 self._updateFlippedX(); 302 self._updateFlippedY(); 303 self._updateChildrenDisplayedRGBA(); 304 self._backGroundBoxDisabledRendererAdaptDirty = true; 305 }); 306 } 307 308 switch (this._backGroundDisabledTexType) { 309 case ccui.Widget.LOCAL_TEXTURE: 310 //SetTexture cannot load resource 311 this._backGroundBoxDisabledRenderer.initWithFile(backGroundDisabled); 312 break; 313 case ccui.Widget.PLIST_TEXTURE: 314 //SetTexture cannot load resource 315 this._backGroundBoxDisabledRenderer.initWithSpriteFrameName(backGroundDisabled); 316 break; 317 default: 318 break; 319 } 320 this._updateFlippedX(); 321 this._updateFlippedY(); 322 323 this._updateChildrenDisplayedRGBA(); 324 325 this._backGroundBoxDisabledRendererAdaptDirty = true; 326 }, 327 328 /** 329 * Loads frontCrossDisabled texture for checkbox. 330 * @param {String} frontCrossDisabled 331 * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType 332 */ 333 loadTextureFrontCrossDisabled: function (frontCrossDisabled, texType) { 334 if (!frontCrossDisabled) 335 return; 336 texType = texType || ccui.Widget.LOCAL_TEXTURE; 337 this._frontCrossDisabledFileName = frontCrossDisabled; 338 this._frontCrossDisabledTexType = texType; 339 340 var self = this; 341 if(!this._frontCrossDisabledRenderer.texture || !this._frontCrossDisabledRenderer.texture.isLoaded()){ 342 this._frontCrossDisabledRenderer.addLoadedEventListener(function(){ 343 344 self._findLayout(); 345 346 self._updateFlippedX(); 347 self._updateFlippedY(); 348 self._updateChildrenDisplayedRGBA(); 349 self._frontCrossDisabledRendererAdaptDirty = true; 350 }); 351 } 352 353 switch (this._frontCrossDisabledTexType) { 354 case ccui.Widget.LOCAL_TEXTURE: 355 //SetTexture cannot load resource 356 this._frontCrossDisabledRenderer.initWithFile(frontCrossDisabled); 357 break; 358 case ccui.Widget.PLIST_TEXTURE: 359 //SetTexture cannot load resource 360 this._frontCrossDisabledRenderer.initWithSpriteFrameName(frontCrossDisabled); 361 break; 362 default: 363 break; 364 } 365 this._updateFlippedX(); 366 this._updateFlippedY(); 367 368 this._updateChildrenDisplayedRGBA(); 369 370 this._frontCrossDisabledRendererAdaptDirty = true; 371 }, 372 373 _onPressStateChangedToNormal: function () { 374 this._backGroundBoxRenderer.setVisible(true); 375 this._backGroundSelectedBoxRenderer.setVisible(false); 376 this._backGroundBoxDisabledRenderer.setVisible(false); 377 this._frontCrossDisabledRenderer.setVisible(false); 378 }, 379 380 _onPressStateChangedToPressed: function () { 381 this._backGroundBoxRenderer.setVisible(false); 382 this._backGroundSelectedBoxRenderer.setVisible(true); 383 this._backGroundBoxDisabledRenderer.setVisible(false); 384 this._frontCrossDisabledRenderer.setVisible(false); 385 }, 386 387 _onPressStateChangedToDisabled: function () { 388 this._backGroundBoxRenderer.setVisible(false); 389 this._backGroundSelectedBoxRenderer.setVisible(false); 390 this._backGroundBoxDisabledRenderer.setVisible(true); 391 this._frontCrossRenderer.setVisible(false); 392 if (this._isSelected) { 393 this._frontCrossDisabledRenderer.setVisible(true); 394 } 395 }, 396 397 /** 398 * Sets the selected state to ccui.CheckBox 399 * @param {Boolean} selected 400 */ 401 setSelectedState: function (selected) { 402 if (selected == this._isSelected) 403 return; 404 this._isSelected = selected; 405 this._frontCrossRenderer.setVisible(this._isSelected); 406 }, 407 408 /** 409 * Returns the selected state of ccui.CheckBox. 410 * @returns {boolean} 411 */ 412 getSelectedState: function () { 413 return this._isSelected; 414 }, 415 416 _selectedEvent: function () { 417 if(this._checkBoxEventSelector){ 418 if (this._checkBoxEventListener) 419 this._checkBoxEventSelector.call(this._checkBoxEventListener, this, ccui.CheckBox.EVENT_SELECTED); 420 else 421 this._checkBoxEventSelector(this, ccui.CheckBox.EVENT_SELECTED); 422 } 423 }, 424 425 _unSelectedEvent: function () { 426 if(this._checkBoxEventSelector){ 427 if (this._checkBoxEventListener) 428 this._checkBoxEventSelector.call(this._checkBoxEventListener, this, ccui.CheckBox.EVENT_UNSELECTED); 429 else 430 this._checkBoxEventSelector(this, ccui.CheckBox.EVENT_UNSELECTED); 431 } 432 }, 433 434 _releaseUpEvent: function(){ 435 ccui.Widget.prototype._releaseUpEvent.call(this); 436 if (this._isSelected){ 437 this.setSelectedState(false); 438 this._unSelectedEvent(); 439 } else { 440 this.setSelectedState(true); 441 this._selectedEvent(); 442 } 443 }, 444 445 /** 446 * add event listener to ccui.CheckBox. it would called when checkbox is selected or unselected. 447 * @param {Function} selector 448 * @param {Object} [target=] 449 * @deprecated since v3.0, please use addEventListener instead. 450 */ 451 addEventListenerCheckBox: function (selector, target) { 452 this.addEventListener(selector, target); 453 }, 454 455 /** 456 * add a call back function would called when checkbox is selected or unselected. 457 * @param {Function} selector 458 * @param {Object} [target=] 459 */ 460 addEventListener: function(selector, target){ 461 this._checkBoxEventSelector = selector; 462 this._checkBoxEventListener = target; 463 }, 464 465 /** 466 * Returns the content size of Renderer. 467 * @returns {cc.Size} 468 */ 469 getVirtualRendererSize: function(){ 470 return this._backGroundBoxRenderer.getContentSize(); 471 }, 472 473 _updateFlippedX: function () { 474 this._backGroundBoxRenderer.setFlippedX(this._flippedX); 475 this._backGroundSelectedBoxRenderer.setFlippedX(this._flippedX); 476 this._frontCrossRenderer.setFlippedX(this._flippedX); 477 this._backGroundBoxDisabledRenderer.setFlippedX(this._flippedX); 478 this._frontCrossDisabledRenderer.setFlippedX(this._flippedX); 479 }, 480 481 _updateFlippedY: function () { 482 this._backGroundBoxRenderer.setFlippedY(this._flippedY); 483 this._backGroundSelectedBoxRenderer.setFlippedY(this._flippedY); 484 this._frontCrossRenderer.setFlippedY(this._flippedY); 485 this._backGroundBoxDisabledRenderer.setFlippedY(this._flippedY); 486 this._frontCrossDisabledRenderer.setFlippedY(this._flippedY); 487 }, 488 489 _onSizeChanged: function () { 490 ccui.Widget.prototype._onSizeChanged.call(this); 491 this._backGroundBoxRendererAdaptDirty = true; 492 this._backGroundSelectedBoxRendererAdaptDirty = true; 493 this._frontCrossRendererAdaptDirty = true; 494 this._backGroundBoxDisabledRendererAdaptDirty = true; 495 this._frontCrossDisabledRendererAdaptDirty = true; 496 }, 497 498 /** 499 * override "getVirtualRenderer" method of widget. 500 * @override 501 * @returns {cc.Node} the renderer of ccui.CheckBox. 502 */ 503 getVirtualRenderer: function () { 504 return this._backGroundBoxRenderer; 505 }, 506 507 _backGroundTextureScaleChangedWithSize: function () { 508 var locRenderer = this._backGroundBoxRenderer, locContentSize = this._contentSize; 509 if (this._ignoreSize) 510 locRenderer.setScale(1.0); 511 else{ 512 var textureSize = locRenderer.getContentSize(); 513 if (textureSize.width <= 0.0 || textureSize.height <= 0.0){ 514 locRenderer.setScale(1.0); 515 return; 516 } 517 var scaleX = locContentSize.width / textureSize.width; 518 var scaleY = locContentSize.height / textureSize.height; 519 locRenderer.setScaleX(scaleX); 520 locRenderer.setScaleY(scaleY); 521 } 522 locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5); 523 }, 524 525 _backGroundSelectedTextureScaleChangedWithSize: function () { 526 var locRenderer = this._backGroundSelectedBoxRenderer, locContentSize = this._contentSize; 527 if (this._ignoreSize) 528 locRenderer.setScale(1.0); 529 else { 530 var textureSize = locRenderer.getContentSize(); 531 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) { 532 locRenderer.setScale(1.0); 533 return; 534 } 535 var scaleX = locContentSize.width / textureSize.width; 536 var scaleY = locContentSize.height / textureSize.height; 537 locRenderer.setScaleX(scaleX); 538 locRenderer.setScaleY(scaleY); 539 } 540 locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5); 541 }, 542 543 _frontCrossTextureScaleChangedWithSize: function () { 544 var locRenderer = this._frontCrossRenderer, locContentSize = this._contentSize; 545 if (this._ignoreSize) 546 locRenderer.setScale(1.0); 547 else { 548 var textureSize = locRenderer.getContentSize(); 549 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) { 550 locRenderer.setScale(1.0); 551 return; 552 } 553 var scaleX = locContentSize.width / textureSize.width; 554 var scaleY = locContentSize.height / textureSize.height; 555 locRenderer.setScaleX(scaleX); 556 locRenderer.setScaleY(scaleY); 557 } 558 locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5); 559 }, 560 561 _backGroundDisabledTextureScaleChangedWithSize: function () { 562 var locRenderer = this._backGroundBoxDisabledRenderer, locContentSize = this._contentSize; 563 if (this._ignoreSize) 564 locRenderer.setScale(1.0); 565 else { 566 var textureSize = locRenderer.getContentSize(); 567 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) { 568 locRenderer.setScale(1.0); 569 return; 570 } 571 var scaleX = locContentSize.width / textureSize.width; 572 var scaleY = locContentSize.height / textureSize.height; 573 locRenderer.setScaleX(scaleX); 574 locRenderer.setScaleY(scaleY); 575 } 576 locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5); 577 }, 578 579 _frontCrossDisabledTextureScaleChangedWithSize: function () { 580 var locRenderer = this._frontCrossDisabledRenderer, locContentSize = this._contentSize; 581 if (this._ignoreSize) { 582 locRenderer.setScale(1.0); 583 } else { 584 var textureSize = locRenderer.getContentSize(); 585 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) { 586 locRenderer.setScale(1.0); 587 return; 588 } 589 var scaleX = locContentSize.width / textureSize.width; 590 var scaleY = locContentSize.height / textureSize.height; 591 locRenderer.setScaleX(scaleX); 592 locRenderer.setScaleY(scaleY); 593 } 594 locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5); 595 }, 596 597 /** 598 * Returns the "class name" of widget. 599 * @override 600 * @returns {string} 601 */ 602 getDescription: function () { 603 return "CheckBox"; 604 }, 605 606 _createCloneInstance: function () { 607 return ccui.CheckBox.create(); 608 }, 609 610 _copySpecialProperties: function (uiCheckBox) { 611 if (uiCheckBox instanceof ccui.CheckBox) { 612 this.loadTextureBackGround(uiCheckBox._backGroundFileName, uiCheckBox._backGroundTexType); 613 this.loadTextureBackGroundSelected(uiCheckBox._backGroundSelectedFileName, uiCheckBox._backGroundSelectedTexType); 614 this.loadTextureFrontCross(uiCheckBox._frontCrossFileName, uiCheckBox._frontCrossTexType); 615 this.loadTextureBackGroundDisabled(uiCheckBox._backGroundDisabledFileName, uiCheckBox._backGroundDisabledTexType); 616 this.loadTextureFrontCrossDisabled(uiCheckBox._frontCrossDisabledFileName, uiCheckBox._frontCrossDisabledTexType); 617 this.setSelectedState(uiCheckBox._isSelected); 618 this._checkBoxEventListener = uiCheckBox._checkBoxEventListener; 619 this._checkBoxEventSelector = uiCheckBox._checkBoxEventSelector; 620 } 621 }, 622 623 _adaptRenderers: function(){ 624 if (this._backGroundBoxRendererAdaptDirty){ 625 this._backGroundTextureScaleChangedWithSize(); 626 this._backGroundBoxRendererAdaptDirty = false; 627 } 628 if (this._backGroundSelectedBoxRendererAdaptDirty) { 629 this._backGroundSelectedTextureScaleChangedWithSize(); 630 this._backGroundSelectedBoxRendererAdaptDirty = false; 631 } 632 if (this._frontCrossRendererAdaptDirty){ 633 this._frontCrossTextureScaleChangedWithSize(); 634 this._frontCrossRendererAdaptDirty = false; 635 } 636 if (this._backGroundBoxDisabledRendererAdaptDirty) { 637 this._backGroundDisabledTextureScaleChangedWithSize(); 638 this._backGroundBoxDisabledRendererAdaptDirty = false; 639 } 640 if (this._frontCrossDisabledRendererAdaptDirty) { 641 this._frontCrossDisabledTextureScaleChangedWithSize(); 642 this._frontCrossDisabledRendererAdaptDirty = false; 643 } 644 } 645 }); 646 647 var _p = ccui.CheckBox.prototype; 648 649 // Extended properties 650 /** @expose */ 651 _p.selected; 652 cc.defineGetterSetter(_p, "selected", _p.getSelectedState, _p.setSelectedState); 653 654 _p = null; 655 656 /** 657 * allocates and initializes a UICheckBox. 658 * @deprecated since v3.0, please use new ccui.CheckBox() instead. 659 * @param {string} [backGround] backGround texture. 660 * @param {string} [backGroundSeleted] backGround selected state texture. 661 * @param {string} [cross] cross texture. 662 * @param {string} [backGroundDisabled] cross dark state texture. 663 * @param {string} [frontCrossDisabled] cross dark state texture. 664 * @param {Number} [texType] 665 * @return {ccui.CheckBox} 666 * @example 667 * // example 668 * var uiCheckBox = ccui.CheckBox.create(); 669 */ 670 ccui.CheckBox.create = function (backGround, backGroundSeleted, cross, backGroundDisabled, frontCrossDisabled, texType) { 671 return new ccui.CheckBox(backGround, backGroundSeleted,cross,backGroundDisabled,frontCrossDisabled,texType); 672 }; 673 674 // Constants 675 //CheckBoxEvent type 676 /** 677 * The selected state of ccui.CheckBox's event. 678 * @constant 679 * @type {number} 680 */ 681 ccui.CheckBox.EVENT_SELECTED = 0; 682 /** 683 * The unselected state of ccui.CheckBox's event. 684 * @constant 685 * @type {number} 686 */ 687 ccui.CheckBox.EVENT_UNSELECTED = 1; 688 689 //Render zorder 690 /** 691 * The normal background renderer's zOrder 692 * @constant 693 * @type {number} 694 */ 695 ccui.CheckBox.BOX_RENDERER_ZORDER = -1; 696 /** 697 * The selected Background renderer's zOrder 698 * @constant 699 * @type {number} 700 */ 701 ccui.CheckBox.BOX_SELECTED_RENDERER_ZORDER = -1; 702 /** 703 * The disabled Background renderer's zOrder 704 * @constant 705 * @type {number} 706 */ 707 ccui.CheckBox.BOX_DISABLED_RENDERER_ZORDER = -1; 708 /** 709 * The normal front renderer's zOrder 710 * @constant 711 * @type {number} 712 */ 713 ccui.CheckBox.FRONT_CROSS_RENDERER_ZORDER = -1; 714 /** 715 * The disabled front renderer's zOrder 716 * @constant 717 * @type {number} 718 */ 719 ccui.CheckBox.FRONT_CROSS_DISABLED_RENDERER_ZORDER = -1; 720