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  * The CheckBox control of Cocos UI.
 28  * @class
 29  * @extends ccui.Widget
 30  *
 31  * @property {Boolean}  selected    - Indicate whether the check box has been selected
 32  */
 33 ccui.CheckBox = ccui.Widget.extend(/** @lends ccui.CheckBox# */{
 34     _backGroundBoxRenderer: null,
 35     _backGroundSelectedBoxRenderer: null,
 36     _frontCrossRenderer: null,
 37     _backGroundBoxDisabledRenderer: null,
 38     _frontCrossDisabledRenderer: null,
 39 
 40     _isSelected: true,
 41 
 42     _checkBoxEventListener: null,
 43     _checkBoxEventSelector:null,
 44 
 45     _backGroundTexType: ccui.Widget.LOCAL_TEXTURE,
 46     _backGroundSelectedTexType: ccui.Widget.LOCAL_TEXTURE,
 47     _frontCrossTexType: ccui.Widget.LOCAL_TEXTURE,
 48     _backGroundDisabledTexType: ccui.Widget.LOCAL_TEXTURE,
 49     _frontCrossDisabledTexType: ccui.Widget.LOCAL_TEXTURE,
 50 
 51     _backGroundFileName: "",
 52     _backGroundSelectedFileName: "",
 53     _frontCrossFileName: "",
 54     _backGroundDisabledFileName: "",
 55     _frontCrossDisabledFileName: "",
 56     _className: "CheckBox",
 57 
 58     _zoomScale: 0.1,
 59     _backgroundTextureScaleX: 0.1,
 60     _backgroundTextureScaleY: 0.1,
 61 
 62     _backGroundBoxRendererAdaptDirty:true,
 63     _backGroundSelectedBoxRendererAdaptDirty:true,
 64     _frontCrossRendererAdaptDirty: true,
 65     _backGroundBoxDisabledRendererAdaptDirty: true,
 66     _frontCrossDisabledRendererAdaptDirty: true,
 67 
 68     /**
 69      * allocates and initializes a UICheckBox.
 70      * Constructor of ccui.CheckBox, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
 71      * @param {String} backGround
 72      * @param {String} backGroundSelected
 73      * @param {String} cross
 74      * @param {String} backGroundDisabled
 75      * @param {String} frontCrossDisabled
 76      * @param {Number} [texType=ccui.Widget.LOCAL_TEXTURE]
 77      * @example
 78      * // example
 79      * var uiCheckBox = new ccui.CheckBox();
 80      */
 81     ctor: function (backGround, backGroundSelected,cross,backGroundDisabled,frontCrossDisabled,texType) {
 82         ccui.Widget.prototype.ctor.call(this);
 83         this.setTouchEnabled(true);
 84         var strNum = 0;
 85         for(var i=0; i<arguments.length; i++){
 86             var type = typeof arguments[i];
 87             if(type === "string"){
 88                 if(isNaN(arguments[i] - 0))
 89                     strNum++;
 90                 else{
 91                     texType = arguments[i];
 92                     arguments[i] = undefined;
 93                 }
 94 
 95             }else if(type === "number")
 96                 strNum++;
 97         }
 98         switch(strNum){
 99             case 2:
100                 texType = cross;
101                 cross = backGroundSelected;
102                 backGroundSelected = undefined;
103         }
104         texType = texType === undefined ? 0 : texType;
105 
106         this._isSelected = true;
107         this.setSelected(false);
108         this.loadTextures(backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType);
109     },
110 
111     _initRenderer: function () {
112         this._backGroundBoxRenderer = new cc.Sprite();
113         this._backGroundSelectedBoxRenderer = new cc.Sprite();
114         this._frontCrossRenderer = new cc.Sprite();
115         this._backGroundBoxDisabledRenderer = new cc.Sprite();
116         this._frontCrossDisabledRenderer = new cc.Sprite();
117 
118         this.addProtectedChild(this._backGroundBoxRenderer, ccui.CheckBox.BOX_RENDERER_ZORDER, -1);
119         this.addProtectedChild(this._backGroundSelectedBoxRenderer, ccui.CheckBox.BOX_SELECTED_RENDERER_ZORDER, -1);
120         this.addProtectedChild(this._frontCrossRenderer, ccui.CheckBox.FRONT_CROSS_RENDERER_ZORDER, -1);
121         this.addProtectedChild(this._backGroundBoxDisabledRenderer, ccui.CheckBox.BOX_DISABLED_RENDERER_ZORDER, -1);
122         this.addProtectedChild(this._frontCrossDisabledRenderer, ccui.CheckBox.FRONT_CROSS_DISABLED_RENDERER_ZORDER, -1);
123     },
124 
125     /**
126      * Loads textures for checkbox.
127      * @param {String} backGround
128      * @param {String} backGroundSelected
129      * @param {String} cross
130      * @param {String} backGroundDisabled
131      * @param {String} frontCrossDisabled
132      * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
133      */
134     loadTextures: function (backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType) {
135         backGround && this.loadTextureBackGround(backGround, texType);
136         backGroundSelected && this.loadTextureBackGroundSelected(backGroundSelected, texType);
137         cross && this.loadTextureFrontCross(cross, texType);
138         backGroundDisabled && this.loadTextureBackGroundDisabled(backGroundDisabled, texType);
139         frontCrossDisabled && this.loadTextureFrontCrossDisabled(frontCrossDisabled, texType);
140     },
141 
142     /**
143      * Loads background texture for checkbox.
144      * @param {String} backGround background filename
145      * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
146      */
147     loadTextureBackGround: function (backGround, texType) {
148         if (!backGround)
149             return;
150 
151         texType = texType || ccui.Widget.LOCAL_TEXTURE;
152         this._backGroundFileName = backGround;
153         this._backGroundTexType = texType;
154 
155         var bgBoxRenderer = this._backGroundBoxRenderer;
156         if(!bgBoxRenderer._textureLoaded){
157             bgBoxRenderer.addEventListener("load", function(){
158                 this._updateContentSizeWithTextureSize(this._backGroundBoxRenderer.getContentSize());
159                 this.loadTextureBackGround(this._backGroundFileName, this._backGroundTexType);
160             }, this);
161         }else{
162             this._backGroundBoxRenderer.setContentSize(this._customSize);
163         }
164 
165         switch (this._backGroundTexType) {
166             case ccui.Widget.LOCAL_TEXTURE:
167                 //SetTexture cannot load resource
168                 bgBoxRenderer.initWithFile(backGround);
169                 break;
170             case ccui.Widget.PLIST_TEXTURE:
171                 //SetTexture cannot load resource
172                 bgBoxRenderer.initWithSpriteFrameName(backGround);
173                 break;
174             default:
175                 break;
176         }
177 
178         this._updateChildrenDisplayedRGBA();
179 
180         this._updateContentSizeWithTextureSize(this._backGroundBoxRenderer.getContentSize());
181         this._backGroundBoxRendererAdaptDirty = true;
182         this._findLayout();
183     },
184 
185     /**
186      * Loads selected state of background texture for checkbox.
187      * @param {String} backGroundSelected
188      * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
189      */
190     loadTextureBackGroundSelected: function (backGroundSelected, texType) {
191         if (!backGroundSelected)
192             return;
193 
194         texType = texType || ccui.Widget.LOCAL_TEXTURE;
195         this._backGroundSelectedFileName = backGroundSelected;
196         this._backGroundSelectedTexType = texType;
197 
198         var backGroundSelectedBoxRenderer = this._backGroundSelectedBoxRenderer;
199         if(!backGroundSelectedBoxRenderer._textureLoaded){
200             backGroundSelectedBoxRenderer.addEventListener("load", function(){
201                 this.loadTextureBackGroundSelected(this._backGroundSelectedFileName, this._backGroundSelectedTexType);
202             }, this);
203         }
204 
205         switch (this._backGroundSelectedTexType) {
206             case ccui.Widget.LOCAL_TEXTURE:
207                 //SetTexture cannot load resource
208                 backGroundSelectedBoxRenderer.initWithFile(backGroundSelected);
209                 break;
210             case ccui.Widget.PLIST_TEXTURE:
211                 //SetTexture cannot load resource
212                 backGroundSelectedBoxRenderer.initWithSpriteFrameName(backGroundSelected);
213                 break;
214             default:
215                 break;
216         }
217 
218 
219         this._updateChildrenDisplayedRGBA();
220 
221         this._backGroundSelectedBoxRendererAdaptDirty = true;
222         this._findLayout();
223     },
224 
225     /**
226      * Loads cross texture for checkbox.
227      * @param {String} cross
228      * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
229      */
230     loadTextureFrontCross: function (cross, texType) {
231         if (!cross)
232             return;
233         texType = texType || ccui.Widget.LOCAL_TEXTURE;
234         this._frontCrossFileName = cross;
235         this._frontCrossTexType = texType;
236 
237         var self = this;
238         var frontCrossRenderer = this._frontCrossRenderer;
239         if(!frontCrossRenderer._textureLoaded){
240             frontCrossRenderer.addEventListener("load", function(){
241                 this.loadTextureFrontCross(this._frontCrossFileName, this._frontCrossTexType);
242             }, this);
243         }
244 
245         switch (this._frontCrossTexType) {
246             case ccui.Widget.LOCAL_TEXTURE:
247                 //SetTexture cannot load resource
248                 frontCrossRenderer.initWithFile(cross);
249                 break;
250             case ccui.Widget.PLIST_TEXTURE:
251                 //SetTexture cannot load resource
252                 frontCrossRenderer.initWithSpriteFrameName(cross);
253                 break;
254             default:
255                 break;
256         }
257 
258         this._updateChildrenDisplayedRGBA();
259 
260         this._frontCrossRendererAdaptDirty = true;
261         this._findLayout();
262     },
263 
264     /**
265      * Loads disabled state of backGround texture for checkbox.
266      * @param {String} backGroundDisabled
267      * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
268      */
269     loadTextureBackGroundDisabled: function (backGroundDisabled, texType) {
270         if (!backGroundDisabled)
271             return;
272         texType = texType || ccui.Widget.LOCAL_TEXTURE;
273         this._backGroundDisabledFileName = backGroundDisabled;
274         this._backGroundDisabledTexType = texType;
275 
276         var self = this;
277         var backGroundBoxDisabledRenderer = this._backGroundBoxDisabledRenderer;
278         if(!backGroundBoxDisabledRenderer._textureLoaded){
279             backGroundBoxDisabledRenderer.addEventListener("load", function(){
280                 this.loadTextureBackGroundDisabled(this._backGroundDisabledFileName, this._backGroundDisabledTexType);
281             }, this);
282         }
283 
284         switch (this._backGroundDisabledTexType) {
285             case ccui.Widget.LOCAL_TEXTURE:
286                 //SetTexture cannot load resource
287                 backGroundBoxDisabledRenderer.initWithFile(backGroundDisabled);
288                 break;
289             case ccui.Widget.PLIST_TEXTURE:
290                 //SetTexture cannot load resource
291                 backGroundBoxDisabledRenderer.initWithSpriteFrameName(backGroundDisabled);
292                 break;
293             default:
294                 break;
295         }
296 
297         this._updateChildrenDisplayedRGBA();
298 
299         this._backGroundBoxDisabledRendererAdaptDirty = true;
300         this._findLayout();
301     },
302 
303     /**
304      * Loads frontCrossDisabled texture for checkbox.
305      * @param {String} frontCrossDisabled
306      * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
307      */
308     loadTextureFrontCrossDisabled: function (frontCrossDisabled, texType) {
309         if (!frontCrossDisabled)
310             return;
311         texType = texType || ccui.Widget.LOCAL_TEXTURE;
312         this._frontCrossDisabledFileName = frontCrossDisabled;
313         this._frontCrossDisabledTexType = texType;
314 
315         var self = this;
316         var frontCrossDisabledRenderer = this._frontCrossDisabledRenderer;
317         if(!frontCrossDisabledRenderer._textureLoaded){
318             frontCrossDisabledRenderer.addEventListener("load", function(){
319                 this.loadTextureFrontCrossDisabled(this._frontCrossDisabledFileName, this._frontCrossDisabledTexType);
320             }, this);
321         }
322 
323         switch (this._frontCrossDisabledTexType) {
324             case ccui.Widget.LOCAL_TEXTURE:
325                 //SetTexture cannot load resource
326                 frontCrossDisabledRenderer.initWithFile(frontCrossDisabled);
327                 break;
328             case ccui.Widget.PLIST_TEXTURE:
329                 //SetTexture cannot load resource
330                 frontCrossDisabledRenderer.initWithSpriteFrameName(frontCrossDisabled);
331                 break;
332             default:
333                 break;
334         }
335 
336         this._updateChildrenDisplayedRGBA();
337 
338         this._frontCrossDisabledRendererAdaptDirty = true;
339         this._findLayout();
340     },
341 
342     _onPressStateChangedToNormal: function () {
343         this._backGroundBoxRenderer.setVisible(true);
344         this._backGroundSelectedBoxRenderer.setVisible(false);
345         this._backGroundBoxDisabledRenderer.setVisible(false);
346         this._frontCrossDisabledRenderer.setVisible(false);
347 
348         this._backGroundBoxRenderer.setScale(this._backgroundTextureScaleX, this._backgroundTextureScaleY);
349         this._frontCrossRenderer.setScale(this._backgroundTextureScaleX, this._backgroundTextureScaleY);
350 
351         if (this._isSelected){
352             this._frontCrossRenderer.setVisible(true);
353             this._frontCrossRendererAdaptDirty = true;
354         }
355     },
356 
357     _onPressStateChangedToPressed: function () {
358         if (!this._backGroundSelectedFileName){
359             this._backGroundBoxRenderer.setScale(this._backgroundTextureScaleX + this._zoomScale, this._backgroundTextureScaleY + this._zoomScale);
360             this._frontCrossRenderer.setScale(this._backgroundTextureScaleX + this._zoomScale, this._backgroundTextureScaleY + this._zoomScale);
361         }else{
362             this._backGroundBoxRenderer.setVisible(false);
363             this._backGroundSelectedBoxRenderer.setVisible(true);
364             this._backGroundBoxDisabledRenderer.setVisible(false);
365             this._frontCrossDisabledRenderer.setVisible(false);
366         }
367     },
368 
369     _onPressStateChangedToDisabled: function () {
370         if (this._backGroundDisabledFileName && this._frontCrossDisabledFileName){
371             this._backGroundBoxRenderer.setVisible(false);
372             this._backGroundBoxDisabledRenderer.setVisible(true);
373         }
374 
375         this._backGroundSelectedBoxRenderer.setVisible(false);
376         this._frontCrossRenderer.setVisible(false);
377         this._backGroundBoxRenderer.setScale(this._backgroundTextureScaleX, this._backgroundTextureScaleY);
378         this._frontCrossRenderer.setScale(this._backgroundTextureScaleX, this._backgroundTextureScaleY);
379 
380         if (this._isSelected) {
381             this._frontCrossDisabledRenderer.setVisible(true);
382             this._frontCrossDisabledRendererAdaptDirty = true;
383         }
384     },
385 
386     setZoomScale: function(scale){
387         this._zoomScale = scale;
388     },
389 
390     getZoomScale: function(){
391         return this._zoomScale;
392     },
393 
394     /**
395      * @deprecated since v3.1, please use setSelected.
396      */
397     setSelectedState: function(selected){
398         this.setSelected(selected);
399     },
400 
401     /**
402      * Sets the selected state to ccui.CheckBox
403      * @param {Boolean} selected
404      */
405     setSelected: function (selected) {
406         if (selected === this._isSelected)
407             return;
408         this._isSelected = selected;
409         this._frontCrossRenderer.setVisible(this._isSelected);
410     },
411 
412     /**
413      * @deprecated since v3.1, please use isSelected.
414      */
415     getSelectedState: function(){
416         return this.isSelected();
417     },
418 
419     /**
420      * Returns the selected state of ccui.CheckBox.
421      * @returns {boolean}
422      */
423     isSelected: function () {
424         return this._isSelected;
425     },
426 
427     _selectedEvent: function () {
428         if(this._checkBoxEventSelector){
429             if (this._checkBoxEventListener)
430                 this._checkBoxEventSelector.call(this._checkBoxEventListener, this, ccui.CheckBox.EVENT_SELECTED);
431             else
432                 this._checkBoxEventSelector(this, ccui.CheckBox.EVENT_SELECTED);
433         }
434     },
435 
436     _unSelectedEvent: function () {
437         if(this._checkBoxEventSelector){
438             if (this._checkBoxEventListener)
439                 this._checkBoxEventSelector.call(this._checkBoxEventListener, this, ccui.CheckBox.EVENT_UNSELECTED);
440             else
441                 this._checkBoxEventSelector(this, ccui.CheckBox.EVENT_UNSELECTED);
442         }
443     },
444 
445     _releaseUpEvent: function(){
446         ccui.Widget.prototype._releaseUpEvent.call(this);
447         if (this._isSelected){
448             this.setSelected(false);
449             this._unSelectedEvent();
450         } else {
451             this.setSelected(true);
452             this._selectedEvent();
453         }
454     },
455 
456     /**
457      * add event listener to ccui.CheckBox. it would called when checkbox is selected or unselected.
458      * @param {Function} selector
459      * @param {Object} [target=]
460      * @deprecated since v3.0, please use addEventListener instead.
461      */
462     addEventListenerCheckBox: function (selector, target) {
463         this.addEventListener(selector, target);
464     },
465 
466     /**
467      * add a call back function would called when checkbox is selected or unselected.
468      * @param {Function} selector
469      * @param {Object} [target=]
470      */
471     addEventListener: function(selector, target){
472         this._checkBoxEventSelector = selector;
473         this._checkBoxEventListener = target;
474     },
475 
476     /**
477      * Returns the content size of Renderer.
478      * @returns {cc.Size}
479      */
480     getVirtualRendererSize: function(){
481         return this._backGroundBoxRenderer.getContentSize();
482     },
483 
484     _onSizeChanged: function () {
485         ccui.Widget.prototype._onSizeChanged.call(this);
486         this._backGroundBoxRendererAdaptDirty = true;
487         this._backGroundSelectedBoxRendererAdaptDirty = true;
488         this._frontCrossRendererAdaptDirty = true;
489         this._backGroundBoxDisabledRendererAdaptDirty = true;
490         this._frontCrossDisabledRendererAdaptDirty = true;
491     },
492 
493     /**
494      * override "getVirtualRenderer" method of widget.
495      * @override
496      * @returns {cc.Node} the renderer of ccui.CheckBox.
497      */
498     getVirtualRenderer: function () {
499         return this._backGroundBoxRenderer;
500     },
501 
502     _backGroundTextureScaleChangedWithSize: function () {
503         var locRenderer = this._backGroundBoxRenderer, locContentSize = this._contentSize;
504         if (this._ignoreSize){
505             locRenderer.setScale(1.0);
506             this._backgroundTextureScaleX = this._backgroundTextureScaleY = 1;
507         }else{
508             var textureSize = locRenderer.getContentSize();
509             if (textureSize.width <= 0.0 || textureSize.height <= 0.0){
510                 locRenderer.setScale(1.0);
511                 this._backgroundTextureScaleX = this._backgroundTextureScaleY = 1;
512                 return;
513             }
514             var scaleX = locContentSize.width / textureSize.width;
515             var scaleY = locContentSize.height / textureSize.height;
516             this._backgroundTextureScaleX = scaleX;
517             this._backgroundTextureScaleY = scaleY;
518             locRenderer.setScaleX(scaleX);
519             locRenderer.setScaleY(scaleY);
520         }
521         locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5);
522     },
523 
524     _backGroundSelectedTextureScaleChangedWithSize: function () {
525         var locRenderer = this._backGroundSelectedBoxRenderer, locContentSize = this._contentSize;
526         if (this._ignoreSize)
527             locRenderer.setScale(1.0);
528         else {
529             var textureSize = locRenderer.getContentSize();
530             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
531                 locRenderer.setScale(1.0);
532                 return;
533             }
534             var scaleX = locContentSize.width / textureSize.width;
535             var scaleY = locContentSize.height / textureSize.height;
536             locRenderer.setScaleX(scaleX);
537             locRenderer.setScaleY(scaleY);
538         }
539         locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5);
540     },
541 
542     _frontCrossTextureScaleChangedWithSize: function () {
543         var locRenderer = this._frontCrossRenderer, locContentSize = this._contentSize;
544         if (this._ignoreSize)
545             locRenderer.setScale(1.0);
546         else {
547             var textureSize = locRenderer.getContentSize();
548             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
549                 locRenderer.setScale(1.0);
550                 return;
551             }
552             var scaleX = locContentSize.width / textureSize.width;
553             var scaleY = locContentSize.height / textureSize.height;
554             locRenderer.setScaleX(scaleX);
555             locRenderer.setScaleY(scaleY);
556         }
557         locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5);
558     },
559 
560     _backGroundDisabledTextureScaleChangedWithSize: function () {
561         var locRenderer = this._backGroundBoxDisabledRenderer, locContentSize = this._contentSize;
562         if (this._ignoreSize)
563             locRenderer.setScale(1.0);
564         else {
565             var textureSize = locRenderer.getContentSize();
566             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
567                 locRenderer.setScale(1.0);
568                 return;
569             }
570             var scaleX = locContentSize.width / textureSize.width;
571             var scaleY = locContentSize.height / textureSize.height;
572             locRenderer.setScaleX(scaleX);
573             locRenderer.setScaleY(scaleY);
574         }
575         locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5);
576     },
577 
578     _frontCrossDisabledTextureScaleChangedWithSize: function () {
579         var locRenderer = this._frontCrossDisabledRenderer, locContentSize = this._contentSize;
580         if (this._ignoreSize) {
581             locRenderer.setScale(1.0);
582         } else {
583             var textureSize = locRenderer.getContentSize();
584             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
585                 locRenderer.setScale(1.0);
586                 return;
587             }
588             var scaleX = locContentSize.width / textureSize.width;
589             var scaleY = locContentSize.height / textureSize.height;
590             locRenderer.setScaleX(scaleX);
591             locRenderer.setScaleY(scaleY);
592         }
593         locRenderer.setPosition(locContentSize.width * 0.5, locContentSize.height * 0.5);
594     },
595 
596     /**
597      * Returns the "class name" of widget.
598      * @override
599      * @returns {string}
600      */
601     getDescription: function () {
602         return "CheckBox";
603     },
604 
605     _createCloneInstance: function () {
606         return new ccui.CheckBox();
607     },
608 
609     _copySpecialProperties: function (uiCheckBox) {
610         if (uiCheckBox instanceof ccui.CheckBox) {
611             this.loadTextureBackGround(uiCheckBox._backGroundFileName, uiCheckBox._backGroundTexType);
612             this.loadTextureBackGroundSelected(uiCheckBox._backGroundSelectedFileName, uiCheckBox._backGroundSelectedTexType);
613             this.loadTextureFrontCross(uiCheckBox._frontCrossFileName, uiCheckBox._frontCrossTexType);
614             this.loadTextureBackGroundDisabled(uiCheckBox._backGroundDisabledFileName, uiCheckBox._backGroundDisabledTexType);
615             this.loadTextureFrontCrossDisabled(uiCheckBox._frontCrossDisabledFileName, uiCheckBox._frontCrossDisabledTexType);
616             this.setSelected(uiCheckBox._isSelected);
617             this._checkBoxEventListener = uiCheckBox._checkBoxEventListener;
618             this._checkBoxEventSelector = uiCheckBox._checkBoxEventSelector;
619             this._ccEventCallback = uiCheckBox._ccEventCallback;
620             this._zoomScale = uiCheckBox._zoomScale;
621             this._backgroundTextureScaleX = uiCheckBox._backgroundTextureScaleX;
622             this._backgroundTextureScaleY = uiCheckBox._backgroundTextureScaleY;
623         }
624     },
625 
626     _adaptRenderers: function(){
627         if (this._backGroundBoxRendererAdaptDirty){
628             this._backGroundTextureScaleChangedWithSize();
629             this._backGroundBoxRendererAdaptDirty = false;
630         }
631         if (this._backGroundSelectedBoxRendererAdaptDirty) {
632             this._backGroundSelectedTextureScaleChangedWithSize();
633             this._backGroundSelectedBoxRendererAdaptDirty = false;
634         }
635         if (this._frontCrossRendererAdaptDirty){
636             this._frontCrossTextureScaleChangedWithSize();
637             this._frontCrossRendererAdaptDirty = false;
638         }
639         if (this._backGroundBoxDisabledRendererAdaptDirty) {
640             this._backGroundDisabledTextureScaleChangedWithSize();
641             this._backGroundBoxDisabledRendererAdaptDirty = false;
642         }
643         if (this._frontCrossDisabledRendererAdaptDirty) {
644             this._frontCrossDisabledTextureScaleChangedWithSize();
645             this._frontCrossDisabledRendererAdaptDirty = false;
646         }
647     }
648 });
649 
650 var _p = ccui.CheckBox.prototype;
651 
652 // Extended properties
653 /** @expose */
654 _p.selected;
655 cc.defineGetterSetter(_p, "selected", _p.isSelected, _p.setSelected);
656 
657 _p = null;
658 
659 /**
660  * allocates and initializes a UICheckBox.
661  * @deprecated since v3.0, please use new ccui.CheckBox() instead.
662  * @param {string} [backGround]     backGround texture.
663  * @param {string} [backGroundSeleted]  backGround selected state texture.
664  * @param {string} [cross]  cross texture.
665  * @param {string} [backGroundDisabled]   cross dark state texture.
666  * @param {string} [frontCrossDisabled]   cross dark state texture.
667  * @param {Number} [texType]
668  * @return {ccui.CheckBox}
669  * @example
670  * // example
671  * var uiCheckBox = new ccui.CheckBox();
672  */
673 ccui.CheckBox.create = function (backGround, backGroundSeleted, cross, backGroundDisabled, frontCrossDisabled, texType) {
674     return new ccui.CheckBox(backGround, backGroundSeleted,cross,backGroundDisabled,frontCrossDisabled,texType);
675 };
676 
677 // Constants
678 //CheckBoxEvent type
679 /**
680  * The selected state of ccui.CheckBox's event.
681  * @constant
682  * @type {number}
683  */
684 ccui.CheckBox.EVENT_SELECTED = 0;
685 /**
686  * The unselected state of ccui.CheckBox's event.
687  * @constant
688  * @type {number}
689  */
690 ccui.CheckBox.EVENT_UNSELECTED = 1;
691 
692 //Render zorder
693 /**
694  * The normal background renderer's zOrder
695  * @constant
696  * @type {number}
697  */
698 ccui.CheckBox.BOX_RENDERER_ZORDER = -1;
699 /**
700  * The selected Background renderer's zOrder
701  * @constant
702  * @type {number}
703  */
704 ccui.CheckBox.BOX_SELECTED_RENDERER_ZORDER = -1;
705 /**
706  * The disabled Background renderer's zOrder
707  * @constant
708  * @type {number}
709  */
710 ccui.CheckBox.BOX_DISABLED_RENDERER_ZORDER = -1;
711 /**
712  * The normal front renderer's zOrder
713  * @constant
714  * @type {number}
715  */
716 ccui.CheckBox.FRONT_CROSS_RENDERER_ZORDER = -1;
717 /**
718  * The disabled front renderer's zOrder
719  * @constant
720  * @type {number}
721  */
722 ccui.CheckBox.FRONT_CROSS_DISABLED_RENDERER_ZORDER = -1;
723