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                  var parent = _self.parent;
 84                  while (parent) {
 85                      if (parent.requestDoLayout) {
 86                          parent.requestDoLayout();
 87                          break;
 88                      }
 89                      parent = parent.parent;
 90                  }
 91              });
 92         }
 93     },
 94 
 95     /**
 96      * Sets string value for TextBMFont
 97      * @deprecated since v3.0, please use setString instead.
 98      * @param {String} value
 99      */
100     setText: function (value) {
101         cc.log("Please use the setString");
102         this.setString(value);
103     },
104 
105     /**
106      * Sets string value for TextBMFont
107      * @param {String} value
108      */
109     setString: function (value) {
110         if(value === this._labelBMFontRenderer.getString())
111             return;
112         this._stringValue = value;
113         this._labelBMFontRenderer.setString(value);
114         if (!this._fntFileHasInit)
115             return;
116         this._updateContentSizeWithTextureSize(this._labelBMFontRenderer.getContentSize());
117         this._labelBMFontRendererAdaptDirty = true;
118     },
119 
120     /**
121      * Returns string value for TextBMFont.
122      * @returns {String}
123      */
124     getString: function () {
125         return this._stringValue;
126     },
127 
128     /**
129      * Returns the length of TextBMFont's string.
130      * @returns {Number}
131      */
132     getStringLength: function(){
133         return this._labelBMFontRenderer.getStringLength();
134     },
135 
136     _onSizeChanged: function () {
137         ccui.Widget.prototype._onSizeChanged.call(this);
138         this._labelBMFontRendererAdaptDirty = true;
139     },
140 
141     _adaptRenderers: function(){
142         if (this._labelBMFontRendererAdaptDirty){
143             this._labelBMFontScaleChangedWithSize();
144             this._labelBMFontRendererAdaptDirty = false;
145         }
146     },
147 
148     /**
149      * Returns TextBMFont's content size
150      * @override
151      * @returns {cc.Size}
152      */
153     getVirtualRendererSize: function(){
154         return this._labelBMFontRenderer.getContentSize();
155     },
156 
157     /**
158      * Returns the renderer of TextBMFont
159      * @override
160      * @returns {cc.Node}
161      */
162     getVirtualRenderer: function () {
163         return this._labelBMFontRenderer;
164     },
165 
166     _labelBMFontScaleChangedWithSize: function () {
167         var locRenderer = this._labelBMFontRenderer;
168         if (this._ignoreSize)
169             locRenderer.setScale(1.0);
170         else {
171             var textureSize = locRenderer.getContentSize();
172             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
173                 locRenderer.setScale(1.0);
174                 return;
175             }
176             locRenderer.setScaleX(this._contentSize.width / textureSize.width);
177             locRenderer.setScaleY(this._contentSize.height / textureSize.height);
178         }
179         locRenderer.setPosition(this._contentSize.width / 2.0, this._contentSize.height / 2.0);
180     },
181 
182     /**
183      * Returns the "class name" of ccui.TextBMFont.
184      * @returns {string}
185      */
186     getDescription: function () {
187         return "TextBMFont";
188     },
189 
190     _createCloneInstance: function () {
191         return new ccui.TextBMFont();
192     },
193 
194     _copySpecialProperties: function (labelBMFont) {
195         this.setFntFile(labelBMFont._fntFileName);
196         this.setString(labelBMFont._stringValue);
197     }
198 });
199 
200 var _p = ccui.TextBMFont.prototype;
201 
202 // Extended properties
203 /** @expose */
204 _p.string;
205 cc.defineGetterSetter(_p, "string", _p.getString, _p.setString);
206 
207 _p = null;
208 
209 /**
210  * allocates and initializes a UILabelBMFont.
211  * @deprecated since v3.0, please use new ccui.TextBMFont() instead.
212  * @return {ccui.TextBMFont}
213  */
214 ccui.TextBMFont.create = function (text, filename) {
215     return new ccui.TextBMFont(text, filename);
216 };
217 
218 // Constants
219 /**
220  * The zOrder value of TextBMFont's renderer.
221  * @constant
222  * @type {number}
223  */
224 ccui.TextBMFont.RENDERER_ZORDER = -1;
225