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 var NORMAL_RENDERER_ZORDER = -2; 26 var PRESSED_RENDERER_ZORDER = -2; 27 var DISABLED_RENDERER_ZORDER = -2; 28 var TITLE_RENDERER_ZORDER = -1; 29 30 /** 31 * Base class for ccs.Button 32 * @class 33 * @extends ccs.Widget 34 */ 35 ccs.Button = ccs.Widget.extend(/** @lends ccs.Button# */{ 36 _buttonNormalRenderer: null, 37 _buttonClickedRenderer: null, 38 _buttonDisableRenderer: null, 39 _titleRenderer: null, 40 _normalFileName: "", 41 _clickedFileName: "", 42 _disabledFileName: "", 43 _prevIgnoreSize: true, 44 _scale9Enabled: false, 45 // CCRect _capInsets:null, 46 _capInsetsNormal: null, 47 _capInsetsPressed: null, 48 _capInsetsDisabled: null, 49 _normalTexType: null, 50 _pressedTexType: null, 51 _disabledTexType: null, 52 _normalTextureSize: null, 53 _pressedTextureSize: null, 54 _disabledTextureSize: null, 55 _pressedActionEnabled: false, 56 _titleColor: null, 57 _normalTextureScaleXInSize: 1, 58 _normalTextureScaleYInSize: 1, 59 _pressedTextureScaleXInSize: 1, 60 _pressedTextureScaleYInSize: 1, 61 _normalTextureLoaded: false, 62 _pressedTextureLoaded: false, 63 _disabledTextureLoaded: false, 64 ctor: function () { 65 ccs.Widget.prototype.ctor.call(this); 66 this._buttonNormalRenderer = null; 67 this._buttonClickedRenderer = null; 68 this._buttonDisableRenderer = null; 69 this._titleRenderer = null; 70 this._normalFileName = ""; 71 this._clickedFileName = ""; 72 this._disabledFileName = ""; 73 this._prevIgnoreSize = true; 74 this._scale9Enabled = false; 75 this._capInsetsNormal = cc.RectZero(); 76 this._capInsetsPressed = cc.RectZero(); 77 this._capInsetsDisabled = cc.RectZero(); 78 this._normalTexType = ccs.TextureResType.local; 79 this._pressedTexType = ccs.TextureResType.local; 80 this._disabledTexType = ccs.TextureResType.local; 81 var locSize = this._size; 82 this._normalTextureSize = cc.size(locSize.width, locSize.height); 83 this._pressedTextureSize = cc.size(locSize.width, locSize.height); 84 this._disabledTextureSize = cc.size(locSize.width, locSize.height); 85 this._pressedActionEnabled = false; 86 this._titleColor = cc.white(); 87 this._normalTextureScaleXInSize = 1; 88 this._normalTextureScaleYInSize = 1; 89 this._pressedTextureScaleXInSize = 1; 90 this._pressedTextureScaleYInSize = 1; 91 this._normalTextureLoaded = false; 92 this._pressedTextureLoaded = false; 93 this._disabledTextureLoaded = false; 94 }, 95 96 init: function () { 97 if (ccs.Widget.prototype.init.call(this)) 98 return true; 99 return false; 100 }, 101 102 initRenderer: function () { 103 this._buttonNormalRenderer = cc.Sprite.create(); 104 this._buttonClickedRenderer = cc.Sprite.create(); 105 this._buttonDisableRenderer = cc.Sprite.create(); 106 this._titleRenderer = cc.LabelTTF.create(""); 107 cc.NodeRGBA.prototype.addChild.call(this, this._buttonNormalRenderer, NORMAL_RENDERER_ZORDER); 108 cc.NodeRGBA.prototype.addChild.call(this, this._buttonClickedRenderer, PRESSED_RENDERER_ZORDER); 109 cc.NodeRGBA.prototype.addChild.call(this, this._buttonDisableRenderer, DISABLED_RENDERER_ZORDER); 110 cc.NodeRGBA.prototype.addChild.call(this, this._titleRenderer, TITLE_RENDERER_ZORDER); 111 }, 112 113 /** 114 * Sets if button is using scale9 renderer. 115 * @param {Boolean} able 116 */ 117 setScale9Enabled: function (able) { 118 if (this._scale9Enabled == able) { 119 return; 120 } 121 this._brightStyle = ccs.BrightStyle.none; 122 this._scale9Enabled = able; 123 124 cc.NodeRGBA.prototype.removeChild.call(this, this._buttonNormalRenderer, true); 125 cc.NodeRGBA.prototype.removeChild.call(this, this._buttonClickedRenderer, true); 126 cc.NodeRGBA.prototype.removeChild.call(this, this._buttonDisableRenderer, true); 127 128 if (this._scale9Enabled) { 129 this._buttonNormalRenderer = cc.Scale9Sprite.create(); 130 this._buttonClickedRenderer = cc.Scale9Sprite.create(); 131 this._buttonDisableRenderer = cc.Scale9Sprite.create(); 132 } 133 else { 134 this._buttonNormalRenderer = cc.Sprite.create(); 135 this._buttonClickedRenderer = cc.Sprite.create(); 136 this._buttonDisableRenderer = cc.Sprite.create(); 137 } 138 139 this.loadTextureNormal(this._normalFileName, this._normalTexType); 140 this.loadTexturePressed(this._clickedFileName, this._pressedTexType); 141 this.loadTextureDisabled(this._disabledFileName, this._disabledTexType); 142 cc.NodeRGBA.prototype.addChild.call(this, this._buttonNormalRenderer, NORMAL_RENDERER_ZORDER); 143 cc.NodeRGBA.prototype.addChild.call(this, this._buttonClickedRenderer, PRESSED_RENDERER_ZORDER); 144 cc.NodeRGBA.prototype.addChild.call(this, this._buttonDisableRenderer, DISABLED_RENDERER_ZORDER); 145 if (this._scale9Enabled) { 146 var ignoreBefore = this._ignoreSize; 147 this.ignoreContentAdaptWithSize(false); 148 this._prevIgnoreSize = ignoreBefore; 149 } 150 else { 151 this.ignoreContentAdaptWithSize(this._prevIgnoreSize); 152 } 153 this.setCapInsetsNormalRenderer(this._capInsetsNormal); 154 this.setCapInsetsPressedRenderer(this._capInsetsPressed); 155 this.setCapInsetsDisabledRenderer(this._capInsetsDisabled); 156 this.setBright(this._bright); 157 }, 158 159 /** 160 * ignoreContentAdaptWithSize 161 * @param {Boolean} ignore 162 */ 163 ignoreContentAdaptWithSize: function (ignore) { 164 if (!this._scale9Enabled || (this._scale9Enabled && !ignore)) { 165 ccs.Widget.prototype.ignoreContentAdaptWithSize.call(this, ignore); 166 this._prevIgnoreSize = ignore; 167 } 168 }, 169 170 /** 171 * Load textures for button. 172 * @param {String} normal 173 * @param {String} selected 174 * @param {String} disabled 175 * @param {ccs.TextureResType} texType 176 */ 177 loadTextures: function (normal, selected, disabled, texType) { 178 this.loadTextureNormal(normal, texType); 179 this.loadTexturePressed(selected, texType); 180 this.loadTextureDisabled(disabled, texType); 181 }, 182 183 /** 184 * Load normal state texture for button. 185 * @param {String} normal 186 * @param {ccs.TextureResType} texType 187 */ 188 loadTextureNormal: function (normal, texType) { 189 if (!normal) { 190 return; 191 } 192 texType = texType||ccs.TextureResType.local; 193 this._normalFileName = normal; 194 this._normalTexType = texType; 195 var buttonNormalRenderer = this._buttonNormalRenderer; 196 switch (this._normalTexType) { 197 case ccs.TextureResType.local: 198 buttonNormalRenderer.initWithFile(normal); 199 break; 200 case ccs.TextureResType.plist: 201 buttonNormalRenderer.initWithSpriteFrameName(normal); 202 break; 203 default: 204 break; 205 } 206 207 var buttonRenderSize = buttonNormalRenderer.getContentSize(); 208 if(buttonNormalRenderer.textureLoaded()){ 209 this._normalTextureSize.width = buttonRenderSize.width; 210 this._normalTextureSize.height = buttonRenderSize.height; 211 }else{ 212 buttonNormalRenderer.addLoadedEventListener(function(){ 213 this._normalTextureSize = buttonNormalRenderer.getContentSize(); 214 if (buttonNormalRenderer.setCapInsets) { 215 buttonNormalRenderer.setCapInsets(this._capInsetsNormal); 216 } 217 this.normalTextureScaleChangedWithSize(); 218 },this); 219 this._normalTextureSize.width = this._customSize.width; 220 this._normalTextureSize.height = this._customSize.height; 221 } 222 if (this._scale9Enabled) { 223 buttonNormalRenderer.setCapInsets(this._capInsetsNormal); 224 } 225 226 this._updateDisplay(); 227 this.normalTextureScaleChangedWithSize(); 228 this._normalTextureLoaded = true; 229 }, 230 231 /** 232 * Load selected state texture for button. 233 * @param {String} selected 234 * @param {ccs.TextureResType} texType 235 */ 236 loadTexturePressed: function (selected, texType) { 237 if (!selected) { 238 return; 239 } 240 texType = texType || ccs.TextureResType.local; 241 this._clickedFileName = selected; 242 this._pressedTexType = texType; 243 var clickedRenderer = this._buttonClickedRenderer; 244 switch (this._pressedTexType) { 245 case ccs.TextureResType.local: 246 clickedRenderer.initWithFile(selected); 247 break; 248 case ccs.TextureResType.plist: 249 clickedRenderer.initWithSpriteFrameName(selected); 250 break; 251 default: 252 break; 253 } 254 255 if(clickedRenderer.textureLoaded()){ 256 this._pressedTextureSize = clickedRenderer.getContentSize(); 257 }else{ 258 clickedRenderer.addLoadedEventListener(function(){ 259 this._pressedTextureSize = clickedRenderer.getContentSize(); 260 if (clickedRenderer.setCapInsets) { 261 clickedRenderer.setCapInsets(this._capInsetsNormal); 262 } 263 this.pressedTextureScaleChangedWithSize(); 264 },this); 265 this._pressedTextureSize.width = this._customSize.width; 266 this._pressedTextureSize.height = this._customSize.height; 267 } 268 269 if (this._scale9Enabled) { 270 clickedRenderer.setCapInsets(this._capInsetsNormal); 271 } 272 this._updateDisplay(); 273 this.pressedTextureScaleChangedWithSize(); 274 this._pressedTextureLoaded = true; 275 }, 276 277 /** 278 * Load dark state texture for button. 279 * @param {String} disabled 280 * @param {ccs.TextureResType} texType 281 */ 282 loadTextureDisabled: function (disabled, texType) { 283 if (!disabled) { 284 return; 285 } 286 texType = texType || ccs.TextureResType.local; 287 this._disabledFileName = disabled; 288 this._disabledTexType = texType; 289 var disableRenderer = this._buttonDisableRenderer; 290 switch (this._disabledTexType) { 291 case ccs.TextureResType.local: 292 disableRenderer.initWithFile(disabled); 293 break; 294 case ccs.TextureResType.plist: 295 disableRenderer.initWithSpriteFrameName(disabled); 296 break; 297 default: 298 break; 299 } 300 301 if(disableRenderer.textureLoaded()){ 302 this._disabledTextureSize = disableRenderer.getContentSize(); 303 }else{ 304 disableRenderer.addLoadedEventListener(function(){ 305 this._disabledTextureSize = disableRenderer.getContentSize(); 306 if (disableRenderer.setCapInsets) { 307 disableRenderer.setCapInsets(this._capInsetsNormal); 308 } 309 this.disabledTextureScaleChangedWithSize(); 310 },this); 311 this._disabledTextureSize.width = this._customSize.width; 312 this._disabledTextureSize.height = this._customSize.height; 313 } 314 315 if (this._scale9Enabled) { 316 disableRenderer.setCapInsets(this._capInsetsNormal); 317 } 318 this._updateDisplay(); 319 this.disabledTextureScaleChangedWithSize(); 320 this._disabledTextureLoaded = true; 321 }, 322 323 _updateDisplay:function(){ 324 this.updateDisplayedColor(this.getColor()); 325 this.updateDisplayedOpacity(this.getOpacity()); 326 this.updateAnchorPoint(); 327 }, 328 329 /** 330 * Sets capinsets for button, if button is using scale9 renderer. 331 * @param {cc.Rect} capInsets 332 */ 333 setCapInsets: function (capInsets) { 334 this.setCapInsetsNormalRenderer(capInsets); 335 this.setCapInsetsPressedRenderer(capInsets); 336 this.setCapInsetsDisabledRenderer(capInsets); 337 }, 338 339 /** 340 * Sets capinsets for button, if button is using scale9 renderer. 341 * @param {cc.Rect} capInsets 342 */ 343 setCapInsetsNormalRenderer: function (capInsets) { 344 this._capInsetsNormal = capInsets; 345 if (!this._scale9Enabled) { 346 return; 347 } 348 this._buttonNormalRenderer.setCapInsets(capInsets); 349 }, 350 351 /** 352 * Sets capinsets for button, if button is using scale9 renderer. 353 * @param {cc.Rect} capInsets 354 */ 355 setCapInsetsPressedRenderer: function (capInsets) { 356 this._capInsetsPressed = capInsets; 357 if (!this._scale9Enabled) { 358 return; 359 } 360 this._buttonClickedRenderer.setCapInsets(capInsets); 361 }, 362 363 /** 364 * Sets capinsets for button, if button is using scale9 renderer. 365 * @param {cc.Rect} capInsets 366 */ 367 setCapInsetsDisabledRenderer: function (capInsets) { 368 this._capInsetsDisabled = capInsets; 369 if (!this._scale9Enabled) { 370 return; 371 } 372 this._buttonDisableRenderer.setCapInsets(capInsets); 373 }, 374 375 onPressStateChangedToNormal: function () { 376 this._buttonNormalRenderer.setVisible(true); 377 this._buttonClickedRenderer.setVisible(false); 378 this._buttonDisableRenderer.setVisible(false); 379 if (this._pressedTextureLoaded) { 380 if (this._pressedActionEnabled) { 381 this._buttonNormalRenderer.stopAllActions(); 382 this._buttonClickedRenderer.stopAllActions(); 383 this._buttonDisableRenderer.stopAllActions(); 384 var zoomAction = cc.ScaleTo.create(0.05, 1.0); 385 var zoomAction1 = cc.ScaleTo.create(0.05, 1.0); 386 var zoomAction2 = cc.ScaleTo.create(0.05, 1.0); 387 this._buttonNormalRenderer.runAction(zoomAction); 388 this._buttonClickedRenderer.runAction(zoomAction1); 389 this._buttonDisableRenderer.runAction(zoomAction2); 390 } 391 } else { 392 this._buttonNormalRenderer.stopAllActions(); 393 var zoomAction = cc.ScaleTo.create(0.05, this._normalTextureScaleXInSize, this._normalTextureScaleYInSize); 394 this._buttonNormalRenderer.runAction(zoomAction); 395 } 396 }, 397 398 onPressStateChangedToPressed: function () { 399 if (this._pressedTextureLoaded) { 400 this._buttonNormalRenderer.setVisible(false); 401 this._buttonClickedRenderer.setVisible(true); 402 this._buttonDisableRenderer.setVisible(false); 403 if (this._pressedActionEnabled) { 404 this._buttonNormalRenderer.stopAllActions(); 405 this._buttonClickedRenderer.stopAllActions(); 406 this._buttonDisableRenderer.stopAllActions(); 407 var zoomAction = cc.ScaleTo.create(0.05, 1.1); 408 var zoomAction1 = cc.ScaleTo.create(0.05, 1.1); 409 var zoomAction2 = cc.ScaleTo.create(0.05, 1.1); 410 this._buttonNormalRenderer.runAction(zoomAction); 411 this._buttonClickedRenderer.runAction(zoomAction1); 412 this._buttonDisableRenderer.runAction(zoomAction2); 413 } 414 } else { 415 this._buttonNormalRenderer.setVisible(true); 416 this._buttonClickedRenderer.setVisible(true); 417 this._buttonDisableRenderer.setVisible(false); 418 this._buttonNormalRenderer.stopAllActions(); 419 var zoomAction = cc.ScaleTo.create(0.05, this._pressedTextureScaleXInSize + 0.1, this._pressedTextureScaleYInSize + 0.1); 420 this._buttonNormalRenderer.runAction(zoomAction); 421 } 422 }, 423 424 onPressStateChangedToDisabled: function () { 425 this._buttonNormalRenderer.setVisible(false); 426 this._buttonClickedRenderer.setVisible(false); 427 this._buttonDisableRenderer.setVisible(true); 428 this._buttonNormalRenderer.setScale(this._normalTextureScaleXInSize, this._normalTextureScaleYInSize); 429 this._buttonClickedRenderer.setScale(this._pressedTextureScaleXInSize, this._pressedTextureScaleYInSize); 430 }, 431 432 /** 433 * override "setFlippedX" of widget. 434 * @param {Boolean} flipX 435 */ 436 setFlippedX: function (flipX) { 437 this._titleRenderer.setFlippedX(flipX); 438 if (this._scale9Enabled) { 439 return; 440 } 441 this._buttonNormalRenderer.setFlippedX(flipX); 442 this._buttonClickedRenderer.setFlippedX(flipX); 443 this._buttonDisableRenderer.setFlippedX(flipX); 444 }, 445 446 /** 447 * override "setFlippedY" of widget. 448 * @param {Boolean} flipY 449 */ 450 setFlippedY: function (flipY) { 451 this._titleRenderer.setFlippedY(flipY); 452 if (this._scale9Enabled) { 453 return; 454 } 455 this._buttonNormalRenderer.setFlippedY(flipY); 456 this._buttonClickedRenderer.setFlippedY(flipY); 457 this._buttonDisableRenderer.setFlippedY(flipY); 458 }, 459 460 /** 461 * override "isFlippedX" of widget. 462 * @returns {Boolean} 463 */ 464 isFlippedX: function () { 465 if (this._scale9Enabled) { 466 return false; 467 } 468 return this._buttonNormalRenderer.isFlippedX(); 469 }, 470 471 /** 472 * override "isFlippedY" of widget. 473 * @returns {Boolean} 474 */ 475 isFlippedY: function () { 476 if (this._scale9Enabled) { 477 return false; 478 } 479 return this._buttonNormalRenderer.isFlippedY(); 480 }, 481 482 /** 483 * override "setAnchorPoint" of widget. 484 * @param {cc.Point|Number} point The anchor point of UIButton or The anchor point.x of UIButton. 485 * @param {Number} [y] The anchor point.y of UIButton. 486 */ 487 setAnchorPoint: function (point, y) { 488 if(arguments.length === 2){ 489 ccs.Widget.prototype.setAnchorPoint.call(this,point, y); 490 this._buttonNormalRenderer.setAnchorPoint(point, y); 491 this._buttonClickedRenderer.setAnchorPoint(point, y); 492 this._buttonDisableRenderer.setAnchorPoint(point, y); 493 this._titleRenderer.setPosition(this._size.width * (0.5 - this._anchorPoint._x), this._size.height * (0.5 - this._anchorPoint._y)); 494 } else { 495 ccs.Widget.prototype.setAnchorPoint.call(this,point); 496 this._buttonNormalRenderer.setAnchorPoint(point); 497 this._buttonClickedRenderer.setAnchorPoint(point); 498 this._buttonDisableRenderer.setAnchorPoint(point); 499 this._titleRenderer.setPosition(this._size.width * (0.5 - this._anchorPoint._x), this._size.height * (0.5 - this._anchorPoint._y)); 500 } 501 }, 502 503 onSizeChanged: function () { 504 ccs.Widget.prototype.onSizeChanged.call(this); 505 this.normalTextureScaleChangedWithSize(); 506 this.pressedTextureScaleChangedWithSize(); 507 this.disabledTextureScaleChangedWithSize(); 508 }, 509 510 /** 511 * override "getContentSize" method of widget. 512 * @returns {cc.Size} 513 */ 514 getContentSize: function () { 515 return this._normalTextureSize; 516 }, 517 518 /** 519 * Gets the Virtual Renderer of widget. 520 * @returns {cc.Node} 521 */ 522 getVirtualRenderer: function () { 523 if (this._bright) { 524 switch (this._brightStyle) { 525 case ccs.BrightStyle.normal: 526 return this._buttonNormalRenderer; 527 case ccs.BrightStyle.highlight: 528 return this._buttonClickedRenderer; 529 default: 530 return null; 531 } 532 } 533 else { 534 return this._buttonDisableRenderer; 535 } 536 }, 537 538 normalTextureScaleChangedWithSize: function () { 539 if (this._ignoreSize) { 540 if (!this._scale9Enabled) { 541 this._buttonNormalRenderer.setScale(1.0); 542 this._normalTextureScaleXInSize = this._normalTextureScaleYInSize = 1; 543 this._size.width = this._normalTextureSize.width; 544 this._size.height = this._normalTextureSize.height; 545 } 546 } 547 else { 548 if (this._scale9Enabled) { 549 this._buttonNormalRenderer.setPreferredSize(this._size); 550 this._normalTextureScaleXInSize = this._normalTextureScaleYInSize = 1; 551 } 552 else { 553 var textureSize = this._normalTextureSize; 554 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) { 555 this._buttonNormalRenderer.setScale(1.0); 556 return; 557 } 558 var scaleX = this._size.width / textureSize.width; 559 var scaleY = this._size.height / textureSize.height; 560 this._buttonNormalRenderer.setScaleX(scaleX); 561 this._buttonNormalRenderer.setScaleY(scaleY); 562 this._normalTextureScaleXInSize = scaleX; 563 this._normalTextureScaleYInSize = scaleY; 564 } 565 } 566 }, 567 568 pressedTextureScaleChangedWithSize: function () { 569 if (this._ignoreSize) { 570 if (!this._scale9Enabled) { 571 this._buttonClickedRenderer.setScale(1.0); 572 this._pressedTextureScaleXInSize = this._pressedTextureScaleYInSize = 1; 573 } 574 } 575 else { 576 if (this._scale9Enabled) { 577 this._buttonClickedRenderer.setPreferredSize(this._size); 578 this._pressedTextureScaleXInSize = this._pressedTextureScaleYInSize = 1; 579 } 580 else { 581 var textureSize = this._pressedTextureSize; 582 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) { 583 this._buttonClickedRenderer.setScale(1.0); 584 return; 585 } 586 var scaleX = this._size.width / textureSize.width; 587 var scaleY = this._size.height / textureSize.height; 588 this._buttonClickedRenderer.setScaleX(scaleX); 589 this._buttonClickedRenderer.setScaleY(scaleY); 590 this._pressedTextureScaleXInSize = scaleX; 591 this._pressedTextureScaleYInSize = scaleY; 592 } 593 } 594 }, 595 596 disabledTextureScaleChangedWithSize: function () { 597 if (this._ignoreSize) { 598 if (!this._scale9Enabled) { 599 this._buttonDisableRenderer.setScale(1.0); 600 } 601 } 602 else { 603 if (this._scale9Enabled) { 604 this._buttonDisableRenderer.setPreferredSize(this._size); 605 } 606 else { 607 var textureSize = this._disabledTextureSize; 608 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) { 609 this._buttonDisableRenderer.setScale(1.0); 610 return; 611 } 612 var scaleX = this._size.width / textureSize.width; 613 var scaleY = this._size.height / textureSize.height; 614 this._buttonDisableRenderer.setScaleX(scaleX); 615 this._buttonDisableRenderer.setScaleY(scaleY); 616 } 617 } 618 }, 619 620 /** 621 * Changes if button can be clicked zoom effect. 622 * @param {Boolean} enabled 623 */ 624 setPressedActionEnabled: function (enabled) { 625 this._pressedActionEnabled = enabled; 626 }, 627 628 /** 629 * set title text 630 * @param {String} text 631 */ 632 setTitleText: function (text) { 633 this._titleRenderer.setString(text); 634 }, 635 636 /** 637 * get title text 638 * @returns {String} text 639 */ 640 getTitleText: function () { 641 return this._titleRenderer.getString(); 642 }, 643 644 /** 645 * set title color 646 * @param {cc.c3b} color 647 */ 648 setTitleColor: function (color) { 649 this._titleColor = color; 650 this._titleRenderer.updateDisplayedColor(color); 651 }, 652 653 /** 654 * get title color 655 * @returns {cc.c3b} 656 */ 657 getTitleColor: function () { 658 return this._titleRenderer.getColor(); 659 }, 660 661 /** 662 * set title fontSize 663 * @param {cc.Size} size 664 */ 665 setTitleFontSize: function (size) { 666 this._titleRenderer.setFontSize(size); 667 }, 668 669 /** 670 * get title fontSize 671 * @returns {cc.Size} 672 */ 673 getTitleFontSize: function () { 674 return this._titleRenderer.getFontSize(); 675 }, 676 677 /** 678 * set title fontName 679 * @param {String} fontName 680 */ 681 setTitleFontName: function (fontName) { 682 this._titleRenderer.setFontName(fontName); 683 }, 684 685 /** 686 * get title fontName 687 * @returns {String} 688 */ 689 getTitleFontName: function () { 690 return this._titleRenderer.getFontName(); 691 }, 692 693 /** 694 * Sets color to widget 695 * It default change the color of widget's children. 696 * @param color 697 */ 698 setColor: function (color) { 699 ccs.Widget.prototype.setColor.call(this,color); 700 this.setTitleColor(this._titleColor); 701 }, 702 703 /** 704 * Returns the "class name" of widget. 705 * @returns {string} 706 */ 707 getDescription: function () { 708 return "Button"; 709 }, 710 711 createCloneInstance:function(){ 712 return ccs.Button.create(); 713 }, 714 715 copySpecialProperties:function(uiButton){ 716 this._prevIgnoreSize = uiButton._prevIgnoreSize; 717 this.setScale9Enabled(uiButton._scale9Enabled); 718 this.loadTextureNormal(uiButton._normalFileName, uiButton._normalTexType); 719 this.loadTexturePressed(uiButton._clickedFileName, uiButton._pressedTexType); 720 this.loadTextureDisabled(uiButton._disabledFileName, uiButton._disabledTexType); 721 this.setCapInsetsNormalRenderer(uiButton._capInsetsNormal); 722 this.setCapInsetsPressedRenderer(uiButton._capInsetsPressed); 723 this.setCapInsetsDisabledRenderer(uiButton._capInsetsDisabled); 724 this.setTitleText(uiButton.getTitleText()); 725 this.setTitleFontName(uiButton.getTitleFontName()); 726 this.setTitleFontSize(uiButton.getTitleFontSize()); 727 this.setTitleColor(uiButton.getTitleColor()); 728 this.setPressedActionEnabled(uiButton._pressedActionEnabled); 729 } 730 731 }); 732 /** 733 * allocates and initializes a UIButton. 734 * @constructs 735 * @return {ccs.Button} 736 * @example 737 * // example 738 * var uiButton = ccs.Button.create(); 739 */ 740 ccs.Button.create = function () { 741 var uiButton = new ccs.Button(); 742 if (uiButton && uiButton.init()) { 743 return uiButton; 744 } 745 return null; 746 };