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