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.UILabelBMFont
 27  * @class
 28  * @extends ccs.UIWidget
 29  */
 30 ccs.UILabelBMFont = ccs.UIWidget.extend(/** @lends ccs.UILabelBMFont# */{
 31     _labelBMFontRenderer: null,
 32     _fileHasInit: false,
 33     _fntFileName: "",
 34     _stringValue: "",
 35     ctor: function () {
 36         ccs.UIWidget.prototype.ctor.call(this);
 37         this._labelBMFontRenderer = null;
 38         this._fileHasInit = false;
 39     },
 40     initRenderer: function () {
 41         ccs.UIWidget.prototype.initRenderer.call(this);
 42         this._labelBMFontRenderer = cc.LabelBMFont.create();
 43         this._renderer.addChild(this._labelBMFontRenderer);
 44     },
 45 
 46     /**
 47      * init a bitmap font atlas with an initial string and the FNT file
 48      * @param {String} fileName
 49      */
 50     setFntFile: function (fileName) {
 51         if (!fileName) {
 52             return;
 53         }
 54         this._fntFileName = fileName;
 55         this._labelBMFontRenderer.initWithString("", fileName);
 56         this.updateAnchorPoint();
 57         this.labelBMFontScaleChangedWithSize();
 58         this._fileHasInit = true;
 59         this.setText(this._stringValue);
 60     },
 61 
 62     /**
 63      * set string value for labelbmfont
 64      * @param {String} value
 65      */
 66     setText: function (value) {
 67         if (!value) {
 68             return;
 69         }
 70         this._strStringValue = value;
 71         this._labelBMFontRenderer.setString(value);
 72         this.labelBMFontScaleChangedWithSize();
 73     },
 74 
 75     /**
 76      * get string value for labelbmfont.
 77      * @returns {String}
 78      */
 79     getStringValue: function () {
 80         return this._strStringValue;
 81     },
 82 
 83     /**
 84      * override "setAnchorPoint" of widget.
 85      * @param {cc.Point} pt
 86      */
 87     setAnchorPoint: function (pt) {
 88         ccs.UIWidget.prototype.setAnchorPoint.call(this, pt);
 89         this._labelBMFontRenderer.setAnchorPoint(pt);
 90     },
 91 
 92     onSizeChanged: function () {
 93         this.labelBMFontScaleChangedWithSize();
 94     },
 95 
 96     /**
 97      * get content size
 98      * @returns {cc.Size}
 99      */
100     getContentSize: function () {
101         return this._labelBMFontRenderer.getContentSize();
102     },
103 
104     /**
105      * override "getVirtualRenderer" method of widget.
106      * @returns {cc.Node}
107      */
108     getVirtualRenderer: function () {
109         return this._labelBMFontRenderer;
110     },
111 
112     labelBMFontScaleChangedWithSize: function () {
113         if (this._ignoreSize) {
114             this._labelBMFontRenderer.setScale(1.0);
115             this._size = this._labelBMFontRenderer.getContentSize();
116         }
117         else {
118             var textureSize = this._labelBMFontRenderer.getContentSize();
119             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
120                 this._labelBMFontRenderer.setScale(1.0);
121                 return;
122             }
123             var scaleX = this._size.width / textureSize.width;
124             var scaleY = this._size.height / textureSize.height;
125             this._labelBMFontRenderer.setScaleX(scaleX);
126             this._labelBMFontRenderer.setScaleY(scaleY);
127         }
128     },
129 
130     /**
131      * Returns the "class name" of widget.
132      * @returns {string}
133      */
134     getDescription: function () {
135         return "LabelBMFont";
136     },
137 
138     createCloneInstance: function () {
139         return ccs.UILabelBMFont.create();
140     },
141 
142     copySpecialProperties: function (labelBMFont) {
143         this.setFntFile(labelBMFont._fntFileName);
144         this.setText(labelBMFont._stringValue);
145     }
146 });
147 /**
148  * allocates and initializes a UILabelBMFont.
149  * @constructs
150  * @return {ccs.UILabelBMFont}
151  * @example
152  * // example
153  * var uiLabelBMFont = ccs.UILabelBMFont.create();
154  */
155 ccs.UILabelBMFont.create = function () {
156     var uiLabelBMFont = new ccs.UILabelBMFont();
157     if (uiLabelBMFont && uiLabelBMFont.init()) {
158         return uiLabelBMFont;
159     }
160     return null;
161 };