1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 * using image file to print text label on the screen, might be a bit slower than cc.Label, similar to cc.LabelBMFont 29 * @class 30 * @extends cc.AtlasNode 31 */ 32 cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{ 33 /** 34 * initializes the cc.LabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas 35 * It accepts two groups of parameters: 36 * a) string, fntFile 37 * b) label, textureFilename, width, height, startChar 38 * @return {Boolean} returns true on success 39 */ 40 initWithString:function (arg) { 41 var label, textureFilename, width, height, startChar; 42 if (arg.length == 2) { 43 var dict = cc.FileUtils.getInstance().dictionaryWithContentsOfFileThreadSafe(arg[1]); 44 cc.Assert(parseInt(dict["version"],10) == 1, "Unsupported version. Upgrade cocos2d version"); 45 46 label = arg[0].toString(); 47 textureFilename = cc.FileUtils.getInstance().fullPathFromRelativeFile(dict["textureFilename"], arg[1]); 48 width = parseInt(dict["itemWidth"],10) / cc.CONTENT_SCALE_FACTOR(); 49 height = parseInt(dict["itemHeight"],10) / cc.CONTENT_SCALE_FACTOR(); 50 startChar = String.fromCharCode(parseInt(dict["firstChar"],10)); 51 } 52 else { 53 label = arg[0].toString(); 54 textureFilename = arg[1]; 55 width = arg[2]; 56 height = arg[3]; 57 //startChar = String.fromCharCode(arg[4]); 58 startChar = arg[4]; 59 cc.Assert(label !== null, "Label must be non-nil"); 60 } 61 62 if (this.initWithTileFile(textureFilename, width, height, label.length)) { 63 this._mapStartChar = startChar; 64 this.setString(label); 65 return true; 66 } 67 return false; 68 }, 69 70 /** 71 * Atlas generation 72 */ 73 updateAtlasValues:function () { 74 var texture = this.getTexture(); 75 76 for (var i = 0; i < this._string.length; i++) { 77 var a = this._string.charCodeAt(i) - this._mapStartChar.charCodeAt(0); 78 var row = parseInt(a % this._itemsPerRow,10) * cc.CONTENT_SCALE_FACTOR(); 79 var col = parseInt(a / this._itemsPerRow,10) * cc.CONTENT_SCALE_FACTOR(); 80 81 var rect = cc.rect(row * this._itemWidth, col * this._itemHeight, this._itemWidth, this._itemHeight); 82 var c = this._string.charCodeAt(i); 83 var fontChar = this.getChildByTag(i); 84 if (!fontChar) { 85 fontChar = new cc.Sprite(); 86 if (c == 32) { 87 fontChar.init(); 88 fontChar.setTextureRect(cc.rect(0, 0, 10, 10), false, cc.SizeZero()); 89 } 90 else { 91 fontChar.initWithTexture(texture, rect); 92 } 93 this.addChild(fontChar, 0, i); 94 } else { 95 if (c == 32) { 96 fontChar.init(); 97 fontChar.setTextureRect(cc.rect(0, 0, 10, 10), false, cc.SizeZero()); 98 } 99 else { 100 // reusing fonts 101 fontChar.initWithTexture(texture, rect); 102 // restore to default in case they were modified 103 fontChar.setVisible(true); 104 fontChar.setOpacity(this._opacity); 105 } 106 } 107 fontChar.setPosition(cc.p(i * this._itemWidth + this._itemWidth / 2, this._itemHeight / 2)); 108 } 109 }, 110 111 /** 112 * set the display string 113 * @param {String} label 114 */ 115 setString:function (label) { 116 this._string = label; 117 var len = label.length; 118 this._textureAtlas.resizeCapacity(len); 119 120 this.setContentSize(new cc.size(len * this._itemWidth, this._itemHeight)); 121 122 if (this._children) { 123 for (var i = 0; i < this._children.length; i++) { 124 var node = this._children[i]; 125 if (node) { 126 node.setVisible(false); 127 } 128 } 129 } 130 this.updateAtlasValues(); 131 }, 132 133 setOpacity:function(opacity){ 134 if(this._opacity != opacity){ 135 this._opacity = opacity; 136 137 for (var i = 0; i < this._children.length; i++) { 138 if (this._children[i]) { 139 this._children[i].setOpacity(opacity); 140 } 141 } 142 } 143 }, 144 145 /** 146 * @param {cc.Color3B} color3 147 */ 148 setColor:function (color3) { 149 this._super(color3); 150 this.updateAtlasValues(); 151 }, 152 /** 153 * return the text of this label 154 * @return {String} 155 */ 156 getString:function () { 157 return this._string; 158 }, 159 160 /** 161 * draw the label 162 */ 163 draw:function () { 164 this._super(); 165 if (cc.LABELATLAS_DEBUG_DRAW) { 166 var s = this.getContentSize(); 167 var vertices = [cc.p(0, 0), cc.p(s.width, 0), 168 cc.p(s.width, s.height), cc.p(0, s.height)]; 169 cc.drawingUtil.drawPoly(vertices, 4, true); 170 } 171 }, 172 173 // string to render 174 _string:null, 175 // the first char in the charmap 176 _mapStartChar:null 177 }); 178 179 /** 180 * It accepts two groups of parameters: 181 * a) string, fntFile 182 * b) label, textureFilename, width, height, startChar 183 * @return {cc.LabelAtlas|Null} returns the LabelAtlas object on success 184 * @example 185 * //Example 186 * //creates the cc.LabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas 187 * var myLabel = cc.LabelAtlas.create('Text to display', 'CharMapfile.png', 12, 20, ' ') 188 * 189 * //creates the cc.LabelAtlas with a string, a fnt file 190 * var myLabel = cc.LabelAtlas.create('Text to display', 'CharMapFile.plist‘); 191 */ 192 cc.LabelAtlas.create = function (/* Multi arguments */) { 193 var ret = new cc.LabelAtlas(); 194 if (ret && ret.initWithString(arguments)) { 195 return ret; 196 } 197 return null; 198 }; 199