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 ccs.LABELBMFONTRENDERERZ = -1; 26 /** 27 * Base class for ccs.LabelBMFont 28 * @class 29 * @extends ccs.Widget 30 */ 31 ccs.LabelBMFont = ccs.Widget.extend(/** @lends ccs.LabelBMFont# */{ 32 _labelBMFontRenderer: null, 33 _fileHasInit: false, 34 _fntFileName: "", 35 _stringValue: "", 36 ctor: function () { 37 ccs.Widget.prototype.ctor.call(this); 38 this._labelBMFontRenderer = null; 39 this._fileHasInit = false; 40 }, 41 initRenderer: function () { 42 this._labelBMFontRenderer = cc.LabelBMFont.create(); 43 cc.NodeRGBA.prototype.addChild.call(this, this._labelBMFontRenderer, ccs.LABELBMFONTRENDERERZ, -1); 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 if (!this._labelBMFontRenderer.textureLoaded()) { 62 this._labelBMFontRenderer.addLoadedEventListener(function () { 63 this.labelBMFontScaleChangedWithSize(); 64 }, this); 65 } 66 }, 67 68 /** 69 * set string value for labelbmfont 70 * @param {String} value 71 */ 72 setText: function (value) { 73 if (!value) { 74 return; 75 } 76 this._strStringValue = value; 77 this._labelBMFontRenderer.setString(value); 78 this.labelBMFontScaleChangedWithSize(); 79 }, 80 81 /** 82 * get string value for labelbmfont. 83 * @returns {String} 84 */ 85 getStringValue: function () { 86 return this._strStringValue; 87 }, 88 89 /** 90 * override "setAnchorPoint" of widget. 91 * @param {cc.Point|Number} point The anchor point of UILabelBMFont or The anchor point.x of UILabelBMFont. 92 * @param {Number} [y] The anchor point.y of UILabelBMFont. 93 */ 94 setAnchorPoint: function (point, y) { 95 if(arguments.length === 2){ 96 ccs.Widget.prototype.setAnchorPoint.call(this, point, y); 97 this._labelBMFontRenderer.setAnchorPoint(point, y); 98 } else { 99 ccs.Widget.prototype.setAnchorPoint.call(this, point); 100 this._labelBMFontRenderer.setAnchorPoint(point); 101 } 102 }, 103 104 onSizeChanged: function () { 105 ccs.Widget.prototype.onSizeChanged.call(this); 106 this.labelBMFontScaleChangedWithSize(); 107 }, 108 109 /** 110 * get content size 111 * @returns {cc.Size} 112 */ 113 getContentSize: function () { 114 return this._labelBMFontRenderer.getContentSize(); 115 }, 116 117 /** 118 * override "getVirtualRenderer" method of widget. 119 * @returns {cc.Node} 120 */ 121 getVirtualRenderer: function () { 122 return this._labelBMFontRenderer; 123 }, 124 125 labelBMFontScaleChangedWithSize: function () { 126 if (this._ignoreSize) { 127 this._labelBMFontRenderer.setScale(1.0); 128 var rendererSize = this._labelBMFontRenderer.getContentSize(); 129 this._size.width = rendererSize.width; 130 this._size.height = rendererSize.height; 131 } 132 else { 133 var textureSize = this._labelBMFontRenderer.getContentSize(); 134 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) { 135 this._labelBMFontRenderer.setScale(1.0); 136 return; 137 } 138 var scaleX = this._size.width / textureSize.width; 139 var scaleY = this._size.height / textureSize.height; 140 this._labelBMFontRenderer.setScaleX(scaleX); 141 this._labelBMFontRenderer.setScaleY(scaleY); 142 } 143 }, 144 145 /** 146 * Returns the "class name" of widget. 147 * @returns {string} 148 */ 149 getDescription: function () { 150 return "LabelBMFont"; 151 }, 152 153 createCloneInstance: function () { 154 return ccs.LabelBMFont.create(); 155 }, 156 157 copySpecialProperties: function (labelBMFont) { 158 this.setFntFile(labelBMFont._fntFileName); 159 this.setText(labelBMFont._stringValue); 160 } 161 }); 162 /** 163 * allocates and initializes a UILabelBMFont. 164 * @constructs 165 * @return {ccs.LabelBMFont} 166 * @example 167 * // example 168 * var uiLabelBMFont = ccs.LabelBMFont.create(); 169 */ 170 ccs.LabelBMFont.create = function () { 171 var uiLabelBMFont = new ccs.LabelBMFont(); 172 if (uiLabelBMFont && uiLabelBMFont.init()) { 173 return uiLabelBMFont; 174 } 175 return null; 176 };