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  * checkBoxEvent type
 27  * @type {Object}
 28  */
 29 ccs.CheckBoxEventType = {
 30     selected: 0,
 31     unselected: 1
 32 };
 33 
 34 ccs.BACKGROUNDBOXRENDERERZ = -1;
 35 ccs.BACKGROUNDBOXSELECTEDRENDERERZ = -1;
 36 ccs.FRONTCROSSRENDERERZ = -1;
 37 ccs.BACKGROUNDBOXDISABLEDRENDERER = -1;
 38 ccs.FRONTCROSSDISABLEDRENDERER = -1;
 39 /**
 40  * Base class for ccs.CheckBox
 41  * @class
 42  * @extends ccs.Widget
 43  */
 44 ccs.CheckBox = ccs.Widget.extend(/** @lends ccs.CheckBox# */{
 45     _backGroundBoxRenderer: null,
 46     _backGroundSelectedBoxRenderer: null,
 47     _frontCrossRenderer: null,
 48     _backGroundBoxDisabledRenderer: null,
 49     _frontCrossDisabledRenderer: null,
 50     _isSelected: true,
 51     _checkBoxEventListener: null,
 52     _checkBoxEventSelector: null,
 53     _backGroundTexType: null,
 54     _backGroundSelectedTexType: null,
 55     _frontCrossTexType: null,
 56     _backGroundDisabledTexType: null,
 57     _frontCrossDisabledTexType: null,
 58     _backGroundFileName: "",
 59     _backGroundSelectedFileName: "",
 60     _frontCrossFileName: "",
 61     _backGroundDisabledFileName: "",
 62     _frontCrossDisabledFileName: "",
 63     ctor: function () {
 64         ccs.Widget.prototype.ctor.call(this);
 65         this._backGroundBoxRenderer = null;
 66         this._backGroundSelectedBoxRenderer = null;
 67         this._frontCrossRenderer = null;
 68         this._backGroundBoxDisabledRenderer = null;
 69         this._frontCrossDisabledRenderer = null;
 70         this._isSelected = true;
 71         this._checkBoxEventListener = null;
 72         this._checkBoxEventSelector = null;
 73         this._backGroundTexType = ccs.TextureResType.local;
 74         this._backGroundSelectedTexType = ccs.TextureResType.local;
 75         this._frontCrossTexType = ccs.TextureResType.local;
 76         this._backGroundDisabledTexType = ccs.TextureResType.local;
 77         this._frontCrossDisabledTexType = ccs.TextureResType.local;
 78         this._backGroundFileName = "";
 79         this._backGroundSelectedFileName = "";
 80         this._frontCrossFileName = "";
 81         this._backGroundDisabledFileName = "";
 82         this._frontCrossDisabledFileName = "";
 83     },
 84     init: function () {
 85         if (ccs.Widget.prototype.init.call(this)) {
 86             this.setSelectedState(false);
 87             return true;
 88         }
 89         return false;
 90     },
 91 
 92     initRenderer: function () {
 93         this._backGroundBoxRenderer = cc.Sprite.create();
 94         this._backGroundSelectedBoxRenderer = cc.Sprite.create();
 95         this._frontCrossRenderer = cc.Sprite.create();
 96         this._backGroundBoxDisabledRenderer = cc.Sprite.create();
 97         this._frontCrossDisabledRenderer = cc.Sprite.create();
 98         cc.NodeRGBA.prototype.addChild.call(this, this._backGroundBoxRenderer, ccs.BACKGROUNDBOXRENDERERZ, -1);
 99         cc.NodeRGBA.prototype.addChild.call(this, this._backGroundSelectedBoxRenderer, ccs.BACKGROUNDBOXSELECTEDRENDERERZ, -1);
100         cc.NodeRGBA.prototype.addChild.call(this, this._frontCrossRenderer, ccs.FRONTCROSSRENDERERZ, -1);
101         cc.NodeRGBA.prototype.addChild.call(this, this._backGroundBoxDisabledRenderer, ccs.BACKGROUNDBOXDISABLEDRENDERER, -1);
102         cc.NodeRGBA.prototype.addChild.call(this, this._frontCrossDisabledRenderer, ccs.FRONTCROSSDISABLEDRENDERER, -1);
103     },
104 
105     /**
106      * Load textures for checkbox.
107      * @param {String} backGround
108      * @param {String} backGroundSelected
109      * @param {String} cross
110      * @param {String} backGroundDisabled
111      * @param {String} frontCrossDisabled
112      * @param {ccs.TextureResType} texType
113      */
114     loadTextures: function (backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType) {
115         this.loadTextureBackGround(backGround, texType);
116         this.loadTextureBackGroundSelected(backGroundSelected, texType);
117         this.loadTextureFrontCross(cross, texType);
118         this.loadTextureBackGroundDisabled(backGroundDisabled, texType);
119         this.loadTextureFrontCrossDisabled(frontCrossDisabled, texType);
120     },
121 
122     /**
123      * Load backGround texture for checkbox.
124      * @param {String} backGround
125      * @param {ccs.TextureResType} texType
126      */
127     loadTextureBackGround: function (backGround, texType) {
128         if (!backGround) {
129             return;
130         }
131         texType = texType || ccs.TextureResType.local;
132         this._backGroundFileName = backGround;
133         this._backGroundTexType = texType;
134         var bgBoxRenderer = this._backGroundBoxRenderer;
135         switch (this._backGroundTexType) {
136             case ccs.TextureResType.local:
137                 bgBoxRenderer.initWithFile(backGround);
138                 break;
139             case ccs.TextureResType.plist:
140                 bgBoxRenderer.initWithSpriteFrameName(backGround);
141                 break;
142             default:
143                 break;
144         }
145 
146         this._updateDisplay();
147 
148         if(!bgBoxRenderer.textureLoaded()){
149             this._backGroundBoxRenderer.setContentSize(this._customSize);
150             bgBoxRenderer.addLoadedEventListener(function(){
151                 this.backGroundTextureScaleChangedWithSize();
152             },this);
153         }
154         this.backGroundTextureScaleChangedWithSize();
155     },
156     /**
157      * Load backGroundSelected texture for checkbox.
158      * @param {String} backGroundSelected
159      * @param {ccs.TextureResType} texType
160      */
161     loadTextureBackGroundSelected: function (backGroundSelected, texType) {
162         if (!backGroundSelected) {
163             return;
164         }
165         texType = texType || ccs.TextureResType.local;
166         this._backGroundSelectedFileName = backGroundSelected;
167         this._backGroundSelectedTexType = texType;
168         switch (this._backGroundSelectedTexType) {
169             case ccs.TextureResType.local:
170                 this._backGroundSelectedBoxRenderer.initWithFile(backGroundSelected);
171                 break;
172             case ccs.TextureResType.plist:
173                 this._backGroundSelectedBoxRenderer.initWithSpriteFrameName(backGroundSelected);
174                 break;
175             default:
176                 break;
177         }
178         this._updateDisplay();
179         this.backGroundSelectedTextureScaleChangedWithSize();
180     },
181 
182     /**
183      * Load cross texture for checkbox.
184      * @param {String} cross
185      * @param {ccs.TextureResType} texType
186      */
187     loadTextureFrontCross: function (cross, texType) {
188         if (!cross) {
189             return;
190         }
191         texType = texType || ccs.TextureResType.local;
192         this._frontCrossFileName = cross;
193         this._frontCrossTexType = texType;
194         switch (this._frontCrossTexType) {
195             case ccs.TextureResType.local:
196                 this._frontCrossRenderer.initWithFile(cross);
197                 break;
198             case ccs.TextureResType.plist:
199                 this._frontCrossRenderer.initWithSpriteFrameName(cross);
200                 break;
201             default:
202                 break;
203         }
204         this._updateDisplay();
205         this.frontCrossTextureScaleChangedWithSize();
206     },
207 
208     /**
209      * Load backGroundDisabled texture for checkbox.
210      * @param {String} backGroundDisabled
211      * @param {ccs.TextureResType} texType
212      */
213     loadTextureBackGroundDisabled: function (backGroundDisabled, texType) {
214         if (!backGroundDisabled) {
215             return;
216         }
217         texType = texType || ccs.TextureResType.local;
218         this._backGroundDisabledFileName = backGroundDisabled;
219         this._backGroundDisabledTexType = texType;
220         switch (this._backGroundDisabledTexType) {
221             case ccs.TextureResType.local:
222                 this._backGroundBoxDisabledRenderer.initWithFile(backGroundDisabled);
223                 break;
224             case ccs.TextureResType.plist:
225                 this._backGroundBoxDisabledRenderer.initWithSpriteFrameName(backGroundDisabled);
226                 break;
227             default:
228                 break;
229         }
230         this._updateDisplay();
231         this.backGroundDisabledTextureScaleChangedWithSize();
232     },
233 
234     /**
235      * Load frontCrossDisabled texture for checkbox.
236      * @param {String} frontCrossDisabled
237      * @param {ccs.TextureResType} texType
238      */
239     loadTextureFrontCrossDisabled: function (frontCrossDisabled, texType) {
240         if (!frontCrossDisabled) {
241             return;
242         }
243         texType = texType || ccs.TextureResType.local;
244         this._frontCrossDisabledFileName = frontCrossDisabled;
245         this._frontCrossDisabledTexType = texType;
246         switch (this._frontCrossDisabledTexType) {
247             case ccs.TextureResType.local:
248                 this._frontCrossDisabledRenderer.initWithFile(frontCrossDisabled);
249                 break;
250             case ccs.TextureResType.plist:
251                 this._frontCrossDisabledRenderer.initWithSpriteFrameName(frontCrossDisabled);
252                 break;
253             default:
254                 break;
255         }
256         this._updateDisplay();
257         this.frontCrossDisabledTextureScaleChangedWithSize();
258     },
259 
260     _updateDisplay:function(){
261         this.updateDisplayedColor(this.getColor());
262         this.updateDisplayedOpacity(this.getOpacity());
263         this.updateAnchorPoint();
264     },
265 
266     onTouchEnded: function (touch , event) {
267         var touchPoint = touch.getLocation();
268         this._touchEndPos.x = touchPoint.x;
269         this._touchEndPos.y = touchPoint.y;
270         if (this._focus) {
271             this.releaseUpEvent();
272             if (this._isSelected) {
273                 this.setSelectedState(false);
274                 this.unSelectedEvent();
275             }
276             else {
277                 this.setSelectedState(true);
278                 this.selectedEvent();
279             }
280         }
281         this.setFocused(false);
282         var widgetParent = this.getWidgetParent();
283         if(widgetParent){
284             widgetParent.checkChildInfo(2, this, touchPoint);
285         }
286     },
287 
288     onPressStateChangedToNormal: function () {
289         this._backGroundBoxRenderer.setVisible(true);
290         this._backGroundSelectedBoxRenderer.setVisible(false);
291         this._backGroundBoxDisabledRenderer.setVisible(false);
292         this._frontCrossDisabledRenderer.setVisible(false);
293     },
294 
295     onPressStateChangedToPressed: function () {
296         this._backGroundBoxRenderer.setVisible(false);
297         this._backGroundSelectedBoxRenderer.setVisible(true);
298         this._backGroundBoxDisabledRenderer.setVisible(false);
299         this._frontCrossDisabledRenderer.setVisible(false);
300     },
301 
302     onPressStateChangedToDisabled: function () {
303         this._backGroundBoxRenderer.setVisible(false);
304         this._backGroundSelectedBoxRenderer.setVisible(false);
305         this._backGroundBoxDisabledRenderer.setVisible(true);
306         this._frontCrossRenderer.setVisible(false);
307         if (this._isSelected) {
308             this._frontCrossDisabledRenderer.setVisible(true);
309         }
310     },
311 
312     setSelectedState: function (selected) {
313         if (selected == this._isSelected) {
314             return;
315         }
316         this._isSelected = selected;
317         this._frontCrossRenderer.setVisible(this._isSelected);
318     },
319 
320     getSelectedState: function () {
321         return this._isSelected;
322     },
323 
324     selectedEvent: function () {
325         if (this._checkBoxEventListener && this._checkBoxEventSelector) {
326             this._checkBoxEventSelector.call(this._checkBoxEventListener, this, ccs.CheckBoxEventType.selected);
327         }
328     },
329 
330     unSelectedEvent: function () {
331         if (this._checkBoxEventListener && this._checkBoxEventSelector) {
332             this._checkBoxEventSelector.call(this._checkBoxEventListener, this, ccs.CheckBoxEventType.unselected);
333         }
334     },
335 
336     /**
337      * add event listener
338      * @param {Function} selector
339      * @param {Object} target
340      */
341     addEventListenerCheckBox: function (selector, target) {
342         this._checkBoxEventSelector = selector;
343         this._checkBoxEventListener = target;
344     },
345 
346     /**
347      * Sets whether the widget should be flipped horizontally or not.
348      * @param {Boolean} flipX
349      */
350     setFlippedX: function (flipX) {
351         this._backGroundBoxRenderer.setFlippedX(flipX);
352         this._backGroundSelectedBoxRenderer.setFlippedX(flipX);
353         this._frontCrossRenderer.setFlippedX(flipX);
354         this._backGroundBoxDisabledRenderer.setFlippedX(flipX);
355         this._frontCrossDisabledRenderer.setFlippedX(flipX);
356     },
357 
358     /**
359      * override "setFlippedY" of widget.
360      * @param {Boolean} flipY
361      */
362     setFlippedY: function (flipY) {
363         this._backGroundBoxRenderer.setFlippedY(flipY);
364         this._backGroundSelectedBoxRenderer.setFlippedY(flipY);
365         this._frontCrossRenderer.setFlippedY(flipY);
366         this._backGroundBoxDisabledRenderer.setFlippedY(flipY);
367         this._frontCrossDisabledRenderer.setFlippedY(flipY);
368     },
369 
370     /**
371      * override "isFlippedX" of widget.
372      * @returns {Boolean}
373      */
374     isFlippedX: function () {
375         return this._backGroundBoxRenderer.isFlippedX();
376     },
377 
378     /**
379      * override "isFlippedY" of widget.
380      * @returns {Boolean}
381      */
382     isFlippedY: function () {
383         return this._backGroundBoxRenderer.isFlippedY();
384     },
385 
386     /**
387      * override "setAnchorPoint" of widget.
388      * @param {cc.Point|Number} point The anchor point of UICheckBox or The anchor point.x of UICheckBox.
389      * @param {Number} [y] The anchor point.y of UICheckBox.
390      */
391     setAnchorPoint: function (point, y) {
392         if(arguments.length === 2){
393             ccs.Widget.prototype.setAnchorPoint.call(this, point, y);
394             this._backGroundBoxRenderer.setAnchorPoint(point, y);
395             this._backGroundSelectedBoxRenderer.setAnchorPoint(point, y);
396             this._backGroundBoxDisabledRenderer.setAnchorPoint(point, y);
397             this._frontCrossRenderer.setAnchorPoint(point, y);
398             this._frontCrossDisabledRenderer.setAnchorPoint(point, y);
399         }else{
400             ccs.Widget.prototype.setAnchorPoint.call(this, point);
401             this._backGroundBoxRenderer.setAnchorPoint(point);
402             this._backGroundSelectedBoxRenderer.setAnchorPoint(point);
403             this._backGroundBoxDisabledRenderer.setAnchorPoint(point);
404             this._frontCrossRenderer.setAnchorPoint(point);
405             this._frontCrossDisabledRenderer.setAnchorPoint(point);
406         }
407     },
408 
409     onSizeChanged: function () {
410         ccs.Widget.prototype.onSizeChanged.call(this);
411         this.backGroundTextureScaleChangedWithSize();
412         this.backGroundSelectedTextureScaleChangedWithSize();
413         this.frontCrossTextureScaleChangedWithSize();
414         this.backGroundDisabledTextureScaleChangedWithSize();
415         this.frontCrossDisabledTextureScaleChangedWithSize();
416     },
417 
418     /**
419      * override "getContentSize" method of widget.
420      * @returns {cc.Size}
421      */
422     getContentSize: function () {
423         return this._backGroundBoxRenderer.getContentSize();
424     },
425 
426     /**
427      * override "getVirtualRenderer" method of widget.
428      * @returns {cc.Node}
429      */
430     getVirtualRenderer: function () {
431         return this._backGroundBoxRenderer;
432     },
433 
434     backGroundTextureScaleChangedWithSize: function () {
435         if (this._ignoreSize) {
436             this._backGroundBoxRenderer.setScale(1.0);
437             var locBackSize = this._backGroundBoxRenderer.getContentSize();
438             this._size.width = locBackSize.width;
439             this._size.height = locBackSize.height;
440         }
441         else {
442             var textureSize = this._backGroundBoxRenderer.getContentSize();
443             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
444                 this._backGroundBoxRenderer.setScale(1.0);
445                 return;
446             }
447             var scaleX = this._size.width / textureSize.width;
448             var scaleY = this._size.height / textureSize.height;
449             this._backGroundBoxRenderer.setScaleX(scaleX);
450             this._backGroundBoxRenderer.setScaleY(scaleY);
451         }
452     },
453 
454     backGroundSelectedTextureScaleChangedWithSize: function () {
455         if (this._ignoreSize) {
456             this._backGroundSelectedBoxRenderer.setScale(1.0);
457         }
458         else {
459             var textureSize = this._backGroundSelectedBoxRenderer.getContentSize();
460             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
461                 this._backGroundSelectedBoxRenderer.setScale(1.0);
462                 return;
463             }
464             var scaleX = this._size.width / textureSize.width;
465             var scaleY = this._size.height / textureSize.height;
466             this._backGroundSelectedBoxRenderer.setScaleX(scaleX);
467             this._backGroundSelectedBoxRenderer.setScaleY(scaleY);
468         }
469     },
470 
471     frontCrossTextureScaleChangedWithSize: function () {
472         if (this._ignoreSize) {
473             this._frontCrossRenderer.setScale(1.0);
474         }
475         else {
476             var textureSize = this._frontCrossRenderer.getContentSize();
477             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
478                 this._frontCrossRenderer.setScale(1.0);
479                 return;
480             }
481             var scaleX = this._size.width / textureSize.width;
482             var scaleY = this._size.height / textureSize.height;
483             this._frontCrossRenderer.setScaleX(scaleX);
484             this._frontCrossRenderer.setScaleY(scaleY);
485         }
486     },
487 
488     backGroundDisabledTextureScaleChangedWithSize: function () {
489         if (this._ignoreSize) {
490             this._backGroundBoxDisabledRenderer.setScale(1.0);
491         }
492         else {
493             var textureSize = this._backGroundBoxDisabledRenderer.getContentSize();
494             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
495                 this._backGroundBoxDisabledRenderer.setScale(1.0);
496                 return;
497             }
498             var scaleX = this._size.width / textureSize.width;
499             var scaleY = this._size.height / textureSize.height;
500             this._backGroundBoxDisabledRenderer.setScaleX(scaleX);
501             this._backGroundBoxDisabledRenderer.setScaleY(scaleY);
502         }
503     },
504 
505     frontCrossDisabledTextureScaleChangedWithSize: function () {
506         if (this._ignoreSize) {
507             this._frontCrossDisabledRenderer.setScale(1.0);
508         }
509         else {
510             var textureSize = this._frontCrossDisabledRenderer.getContentSize();
511             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
512                 this._frontCrossDisabledRenderer.setScale(1.0);
513                 return;
514             }
515             var scaleX = this._size.width / textureSize.width;
516             var scaleY = this._size.height / textureSize.height;
517             this._frontCrossDisabledRenderer.setScaleX(scaleX);
518             this._frontCrossDisabledRenderer.setScaleY(scaleY);
519         }
520     },
521 
522     /**
523      * Returns the "class name" of widget.
524      * @returns {string}
525      */
526     getDescription: function () {
527         return "CheckBox";
528     },
529 
530     createCloneInstance: function () {
531         return ccs.CheckBox.create();
532     },
533 
534     copySpecialProperties: function (uiCheckBox) {
535         this.loadTextureBackGround(uiCheckBox._backGroundFileName, uiCheckBox._backGroundTexType);
536         this.loadTextureBackGroundSelected(uiCheckBox._backGroundSelectedFileName, uiCheckBox._backGroundSelectedTexType);
537         this.loadTextureFrontCross(uiCheckBox._frontCrossFileName, uiCheckBox._frontCrossTexType);
538         this.loadTextureBackGroundDisabled(uiCheckBox._backGroundDisabledFileName, uiCheckBox._backGroundDisabledTexType);
539         this.loadTextureFrontCrossDisabled(uiCheckBox._frontCrossDisabledFileName, uiCheckBox._frontCrossDisabledTexType);
540         this.setSelectedState(uiCheckBox._isSelected);
541     }
542 });
543 /**
544  * allocates and initializes a UICheckBox.
545  * @constructs
546  * @return {ccs.CheckBox}
547  * @example
548  * // example
549  * var uiCheckBox = ccs.CheckBox.create();
550  */
551 ccs.CheckBox.create = function () {
552     var uiCheckBox = new ccs.CheckBox();
553     if (uiCheckBox && uiCheckBox.init()) {
554         return uiCheckBox;
555     }
556     return null;
557 };
558