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