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