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 text atlas control of Cocos UI.
 28  * @class
 29  * @extends ccui.Widget
 30  *
 31  * @property {String}   string  - Content string of the label
 32  */
 33 ccui.TextAtlas = ccui.Widget.extend(/** @lends ccui.TextAtlas# */{
 34     _labelAtlasRenderer: null,
 35     _stringValue: "",
 36     _charMapFileName: "",
 37     _itemWidth: 0,
 38     _itemHeight: 0,
 39     _startCharMap: "",
 40     _className: "TextAtlas",
 41     _labelAtlasRendererAdaptDirty: null,
 42 
 43     /**
 44      * Allocates and initializes a UILabelAtlas.                  <br/>
 45      * Constructor of ccui.TextAtlas, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
 46      * @param {String} stringValue
 47      * @param {String} charMapFile
 48      * @param {number} itemWidth
 49      * @param {number} itemHeight
 50      * @param {String} startCharMap
 51      * @example
 52      * // example
 53      * var uiLabelAtlas = new ccui.TextAtlas();
 54      */
 55     ctor: function (stringValue, charMapFile, itemWidth, itemHeight, startCharMap) {
 56         ccui.Widget.prototype.ctor.call(this);
 57         startCharMap !== undefined && this.setProperty(stringValue, charMapFile, itemWidth, itemHeight, startCharMap);
 58     },
 59 
 60     _initRenderer: function () {
 61         this._labelAtlasRenderer = new cc.LabelAtlas();
 62         this._labelAtlasRenderer.setAnchorPoint(cc.p(0.5, 0.5));
 63         this.addProtectedChild(this._labelAtlasRenderer, ccui.TextAtlas.RENDERER_ZORDER, -1);
 64     },
 65 
 66     /**
 67      * 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
 68      * @param {String} stringValue
 69      * @param {String} charMapFile
 70      * @param {number} itemWidth
 71      * @param {number} itemHeight
 72      * @param {String} startCharMap
 73      */
 74     setProperty: function (stringValue, charMapFile, itemWidth, itemHeight, startCharMap) {
 75         this._stringValue = stringValue;
 76         this._charMapFileName = charMapFile;
 77         this._itemWidth = itemWidth;
 78         this._itemHeight = itemHeight;
 79         this._startCharMap = startCharMap;
 80 
 81         this._labelAtlasRenderer.initWithString(
 82             stringValue,
 83             this._charMapFileName,
 84             this._itemWidth,
 85             this._itemHeight,
 86             this._startCharMap[0]
 87         );
 88 
 89         this._updateContentSizeWithTextureSize(this._labelAtlasRenderer.getContentSize());
 90         this._labelAtlasRendererAdaptDirty = true;
 91     },
 92 
 93     /**
 94      * Sets string value for ui text atlas.
 95      * @param {String} value
 96      */
 97     setString: function (value) {
 98         if(value === this._labelAtlasRenderer.getString())
 99             return;
100         this._stringValue = value;
101         this._labelAtlasRenderer.setString(value);
102         this._updateContentSizeWithTextureSize(this._labelAtlasRenderer.getContentSize());
103         this._labelAtlasRendererAdaptDirty = true;
104     },
105 
106     /**
107      * Sets string value for text atlas.
108      * @deprecated since v3.0, please use setString instead.
109      * @param {String} value
110      */
111     setStringValue: function (value) {
112         cc.log("Please use the setString");
113         this.setString(value);
114     },
115 
116     /**
117      * get string value for text atlas.
118      * @deprecated since v3.0, please use getString instead.
119      * @returns {String}
120      */
121     getStringValue: function () {
122         cc.log("Please use the getString");
123         return this.getString();
124     },
125 
126     /**
127      * get string value for ui text atlas.
128      * @returns {String}
129      */
130     getString: function () {
131         return this._labelAtlasRenderer.getString();
132     },
133 
134     /**
135      * Returns the length of string.
136      * @returns {*|Number|long|int}
137      */
138     getStringLength: function(){
139         return this._labelAtlasRenderer.getStringLength();
140     },
141 
142     _onSizeChanged: function () {
143         ccui.Widget.prototype._onSizeChanged.call(this);
144         this._labelAtlasRendererAdaptDirty = true;
145     },
146 
147     _adaptRenderers: function(){
148         if (this._labelAtlasRendererAdaptDirty){
149             this._labelAtlasScaleChangedWithSize();
150             this._labelAtlasRendererAdaptDirty = false;
151         }
152     },
153 
154     /**
155      * Returns the renderer's content size
156      * @overrider
157      * @returns {cc.Size}
158      */
159     getVirtualRendererSize: function(){
160         return this._labelAtlasRenderer.getContentSize();
161     },
162 
163     /**
164      * Returns the renderer of ccui.TextAtlas.
165      * @returns {cc.Node}
166      */
167     getVirtualRenderer: function () {
168         return this._labelAtlasRenderer;
169     },
170 
171     _labelAtlasScaleChangedWithSize: function () {
172         var locRenderer = this._labelAtlasRenderer;
173         if (this._ignoreSize) {
174             locRenderer.setScale(1.0);
175         } else {
176             var textureSize = locRenderer.getContentSize();
177             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
178                 locRenderer.setScale(1.0);
179                 return;
180             }
181             locRenderer.setScaleX(this._contentSize.width / textureSize.width);
182             locRenderer.setScaleY(this._contentSize.height / textureSize.height);
183         }
184         locRenderer.setPosition(this._contentSize.width / 2.0, this._contentSize.height / 2.0);
185     },
186 
187     /**
188      * Returns the "class name" of ccui.TextAtlas.
189      * @returns {string}
190      */
191     getDescription: function () {
192         return "LabelAtlas";
193     },
194 
195     _copySpecialProperties: function (labelAtlas) {
196         if (labelAtlas){
197             this.setProperty(labelAtlas._stringValue, labelAtlas._charMapFileName, labelAtlas._itemWidth, labelAtlas._itemHeight, labelAtlas._startCharMap);
198         }
199     },
200 
201     _createCloneInstance: function () {
202         return new ccui.TextAtlas();
203     }
204 });
205 
206 var _p = ccui.TextAtlas.prototype;
207 
208 // Extended properties
209 /** @expose */
210 _p.string;
211 cc.defineGetterSetter(_p, "string", _p.getString, _p.setString);
212 
213 _p = null;
214 
215 /**
216  * allocates and initializes a UILabelAtlas.
217  * @deprecated since v3.0, please use new ccui.TextAtlas() instead.
218  * @return {ccui.TextAtlas}
219  */
220 ccui.TextAtlas.create = function (stringValue, charMapFile, itemWidth, itemHeight, startCharMap) {
221     return new ccui.TextAtlas(stringValue, charMapFile, itemWidth, itemHeight, startCharMap);
222 };
223 
224 // Constants
225 /**
226  * The zOrder value of ccui.TextAtlas's renderer.
227  * @type {number}
228  */
229 ccui.TextAtlas.RENDERER_ZORDER = -1;