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