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