1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3 
  4  http://www.cocos2d-x.org
  5 
  6  Permission is hereby granted, free of charge, to any person obtaining a copy
  7  of this software and associated documentation files (the "Software"), to deal
  8  in the Software without restriction, including without limitation the rights
  9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10  copies of the Software, and to permit persons to whom the Software is
 11  furnished to do so, subject to the following conditions:
 12 
 13  The above copyright notice and this permission notice shall be included in
 14  all copies or substantial portions of the Software.
 15 
 16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22  THE SOFTWARE.
 23  ****************************************************************************/
 24 
 25 /**
 26  * Base class for ccs.UICCLabelAtlas
 27  * @class
 28  * @extends cc.LabelAtlas
 29  */
 30 ccs.UICCLabelAtlas = cc.LabelAtlas.extend({
 31 
 32     setProperty: function (string, charMapFile, itemWidth, itemHeight, startCharMap) {
 33         this.initWithString(string, charMapFile, itemWidth, itemHeight, startCharMap);
 34     },
 35 
 36     draw: function () {
 37         if (!this._textureAtlas) {
 38             return;
 39         }
 40 
 41         cc.AtlasNode.prototype.draw.call(this);
 42     },
 43     updateDisplayedOpacity: function (opacity) {
 44         cc.AtlasNode.prototype.updateDisplayedOpacity.call(this, opacity);
 45     }
 46 });
 47 
 48 ccs.UICCLabelAtlas.create = function () {
 49     var uiCCLabelAtlas = new ccs.UICCLabelAtlas();
 50     if (uiCCLabelAtlas && uiCCLabelAtlas.init()) {
 51         return uiCCLabelAtlas;
 52     }
 53     return null;
 54 };
 55 
 56 /**
 57  * Base class for ccs.UILabelAtlas
 58  * @class
 59  * @extends ccs.UIWidget
 60  */
 61 ccs.UILabelAtlas = ccs.UIWidget.extend(/** @lends ccs.UILabelAtlas# */{
 62     _labelAtlasRenderer: null,
 63     _stringValue: "",
 64     _charMapFileName: "",
 65     _itemWidth: 0,
 66     _itemHeight: 0,
 67     _startCharMap: "",
 68     ctor: function () {
 69         ccs.UIWidget.prototype.ctor.call(this);
 70         this._labelAtlasRenderer = null;
 71     },
 72 
 73     initRenderer: function () {
 74         ccs.UIWidget.prototype.initRenderer.call(this);
 75         this._labelAtlasRenderer = ccs.UICCLabelAtlas.create();
 76         this._renderer.addChild(this._labelAtlasRenderer);
 77     },
 78 
 79     /**
 80      * initializes the UILabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas
 81      * @param {String} stringValue
 82      * @param {String} charMapFile
 83      * @param {number} itemWidth
 84      * @param {number} itemHeight
 85      * @param {String} startCharMap
 86      */
 87     setProperty: function (stringValue, charMapFile, itemWidth, itemHeight, startCharMap) {
 88         this._stringValue = stringValue;
 89         this._charMapFileName = charMapFile;
 90         this._itemWidth = itemWidth;
 91         this._itemHeight = itemHeight;
 92         this._startCharMap = startCharMap;
 93         this._labelAtlasRenderer.setProperty(stringValue, charMapFile, itemWidth, itemHeight, startCharMap[0]);
 94         this.updateAnchorPoint();
 95         this.labelAtlasScaleChangedWithSize();
 96     },
 97 
 98     /**
 99      * set string value for labelatlas.
100      * @param {String} value
101      */
102     setStringValue: function (value) {
103         this._stringValue = value;
104         this._labelAtlasRenderer.setString(value);
105         this.labelAtlasScaleChangedWithSize();
106     },
107 
108     /**
109      * get string value for labelatlas.
110      * @returns {String}
111      */
112     getStringValue: function () {
113         return this._labelAtlasRenderer.getString();
114     },
115 
116     /**
117      * override "setAnchorPoint" of widget.
118      * @param {cc.Point} pt
119      */
120     setAnchorPoint: function (pt) {
121         ccs.UIWidget.prototype.setAnchorPoint.call(this, pt);
122         this._labelAtlasRenderer.setAnchorPoint(cc.p(pt.x, pt.y));
123     },
124 
125     onSizeChanged: function () {
126         this.labelAtlasScaleChangedWithSize();
127     },
128 
129     /**
130      * override "getContentSize" method of widget.
131      * @returns {cc.Size}
132      */
133     getContentSize: function () {
134         return this._labelAtlasRenderer.getContentSize();
135     },
136 
137     /**
138      * override "getVirtualRenderer" method of widget.
139      * @returns {cc.Node}
140      */
141     getVirtualRenderer: function () {
142         return this._labelAtlasRenderer;
143     },
144 
145     labelAtlasScaleChangedWithSize: function () {
146         if (this._ignoreSize) {
147             this._labelAtlasRenderer.setScale(1.0);
148             this._size = this._labelAtlasRenderer.getContentSize();
149         }
150         else {
151             var textureSize = this._labelAtlasRenderer.getContentSize();
152             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
153                 this._labelAtlasRenderer.setScale(1.0);
154                 return;
155             }
156             var scaleX = this._size.width / textureSize.width;
157             var scaleY = this._size.height / textureSize.height;
158             this._labelAtlasRenderer.setScaleX(scaleX);
159             this._labelAtlasRenderer.setScaleY(scaleY);
160         }
161     },
162 
163     /**
164      * Returns the "class name" of widget.
165      * @returns {string}
166      */
167     getDescription: function () {
168         return "LabelAtlase";
169     },
170 
171     createCloneInstance: function () {
172         return ccs.UILabelAtlas.create();
173     },
174 
175     copySpecialProperties: function (labelAtlas) {
176         this.setProperty(labelAtlas._stringValue, labelAtlas._charMapFileName, labelAtlas._itemWidth, labelAtlas._itemHeight, labelAtlas._startCharMap);
177     }
178 });
179 /**
180  * allocates and initializes a UILabelAtlas.
181  * @constructs
182  * @return {ccs.UILabelAtlas}
183  * @example
184  * // example
185  * var uiLabelAtlas = ccs.UILabelAtlas.create();
186  */
187 ccs.UILabelAtlas.create = function () {
188     var uiLabelAtlas = new ccs.UILabelAtlas();
189     if (uiLabelAtlas && uiLabelAtlas.init()) {
190         return uiLabelAtlas;
191     }
192     return null;
193 };