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