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