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