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