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