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.LABELATLASRENDERERZ = -1; 26 /** 27 * Base class for ccs.UICCLabelAtlas 28 * @class 29 * @extends cc.LabelAtlas 30 */ 31 ccs.UICCLabelAtlas = cc.LabelAtlas.extend({ 32 33 setProperty: function (string, charMapFile, itemWidth, itemHeight, startCharMap) { 34 this.initWithString(string, charMapFile, itemWidth, itemHeight, startCharMap); 35 }, 36 37 draw: function () { 38 if (!this._textureAtlas) { 39 return; 40 } 41 42 cc.AtlasNode.prototype.draw.call(this); 43 }, 44 updateDisplayedOpacity: function (opacity) { 45 cc.AtlasNode.prototype.updateDisplayedOpacity.call(this, opacity); 46 } 47 }); 48 49 ccs.UICCLabelAtlas.create = function () { 50 var uiCCLabelAtlas = new ccs.UICCLabelAtlas(); 51 if (uiCCLabelAtlas && uiCCLabelAtlas.init()) { 52 return uiCCLabelAtlas; 53 } 54 return null; 55 }; 56 57 /** 58 * Base class for ccs.LabelAtlas 59 * @class 60 * @extends ccs.Widget 61 */ 62 ccs.LabelAtlas = ccs.Widget.extend(/** @lends ccs.LabelAtlas# */{ 63 _labelAtlasRenderer: null, 64 _stringValue: "", 65 _charMapFileName: "", 66 _itemWidth: 0, 67 _itemHeight: 0, 68 _startCharMap: "", 69 ctor: function () { 70 ccs.Widget.prototype.ctor.call(this); 71 this._labelAtlasRenderer = null; 72 }, 73 74 initRenderer: function () { 75 this._labelAtlasRenderer = ccs.UICCLabelAtlas.create(); 76 cc.NodeRGBA.prototype.addChild.call(this, this._labelAtlasRenderer, ccs.LABELATLASRENDERERZ, -1); 77 }, 78 79 /** 80 * initializes the UILabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas 81 * @param {String} stringValue 82 * @param {String} charMapFile 83 * @param {number} itemWidth 84 * @param {number} itemHeight 85 * @param {String} startCharMap 86 */ 87 setProperty: function (stringValue, charMapFile, itemWidth, itemHeight, startCharMap) { 88 this._stringValue = stringValue; 89 this._charMapFileName = charMapFile; 90 this._itemWidth = itemWidth; 91 this._itemHeight = itemHeight; 92 this._startCharMap = startCharMap; 93 var renderer = this._labelAtlasRenderer; 94 renderer.setProperty(stringValue, charMapFile, itemWidth, itemHeight, startCharMap[0]); 95 this.updateAnchorPoint(); 96 this.labelAtlasScaleChangedWithSize(); 97 98 if (!renderer.textureLoaded()) { 99 renderer.addLoadedEventListener(function () { 100 this.labelAtlasScaleChangedWithSize(); 101 }, this); 102 } 103 }, 104 105 /** 106 * set string value for labelatlas. 107 * @param {String} value 108 */ 109 setStringValue: function (value) { 110 this._stringValue = value; 111 this._labelAtlasRenderer.setString(value); 112 this.labelAtlasScaleChangedWithSize(); 113 }, 114 115 /** 116 * get string value for labelatlas. 117 * @returns {String} 118 */ 119 getStringValue: function () { 120 return this._labelAtlasRenderer.getString(); 121 }, 122 123 /** 124 * override "setAnchorPoint" of widget. 125 * @param {cc.Point|Number} point The anchor point of UILabelAtlas or The anchor point.x of UILabelAtlas. 126 * @param {Number} [y] The anchor point.y of UILabelAtlas. 127 */ 128 setAnchorPoint: function (point, y) { 129 if(arguments.length === 2){ 130 ccs.Widget.prototype.setAnchorPoint.call(this, point, y); 131 this._labelAtlasRenderer.setAnchorPoint(point, y); 132 } else { 133 ccs.Widget.prototype.setAnchorPoint.call(this, point); 134 this._labelAtlasRenderer.setAnchorPoint(point); 135 } 136 }, 137 138 onSizeChanged: function () { 139 ccs.Widget.prototype.onSizeChanged.call(this); 140 this.labelAtlasScaleChangedWithSize(); 141 }, 142 143 /** 144 * override "getContentSize" method of widget. 145 * @returns {cc.Size} 146 */ 147 getContentSize: function () { 148 return this._labelAtlasRenderer.getContentSize(); 149 }, 150 151 /** 152 * override "getVirtualRenderer" method of widget. 153 * @returns {cc.Node} 154 */ 155 getVirtualRenderer: function () { 156 return this._labelAtlasRenderer; 157 }, 158 159 labelAtlasScaleChangedWithSize: function () { 160 if (this._ignoreSize) { 161 this._labelAtlasRenderer.setScale(1.0); 162 var atlasRenderSize = this._labelAtlasRenderer.getContentSize(); 163 this._size.width = atlasRenderSize.width; 164 this._size.height = atlasRenderSize.height; 165 } 166 else { 167 var textureSize = this._labelAtlasRenderer.getContentSize(); 168 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) { 169 this._labelAtlasRenderer.setScale(1.0); 170 return; 171 } 172 var scaleX = this._size.width / textureSize.width; 173 var scaleY = this._size.height / textureSize.height; 174 this._labelAtlasRenderer.setScaleX(scaleX); 175 this._labelAtlasRenderer.setScaleY(scaleY); 176 } 177 }, 178 179 /** 180 * Returns the "class name" of widget. 181 * @returns {string} 182 */ 183 getDescription: function () { 184 return "LabelAtlase"; 185 }, 186 187 createCloneInstance: function () { 188 return ccs.LabelAtlas.create(); 189 }, 190 191 copySpecialProperties: function (labelAtlas) { 192 this.setProperty(labelAtlas._stringValue, labelAtlas._charMapFileName, labelAtlas._itemWidth, labelAtlas._itemHeight, labelAtlas._startCharMap); 193 } 194 }); 195 /** 196 * allocates and initializes a UILabelAtlas. 197 * @constructs 198 * @return {ccs.LabelAtlas} 199 * @example 200 * // example 201 * var uiLabelAtlas = ccs.LabelAtlas.create(); 202 */ 203 ccs.LabelAtlas.create = function () { 204 var uiLabelAtlas = new ccs.LabelAtlas(); 205 if (uiLabelAtlas && uiLabelAtlas.init()) { 206 return uiLabelAtlas; 207 } 208 return null; 209 };