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.LABELRENDERERZ = -1; 26 /** 27 * Base class for ccs.Button 28 * @class 29 * @extends ccs.Widget 30 */ 31 ccs.Label = ccs.Widget.extend(/** @lends ccs.Label# */{ 32 _touchScaleChangeEnabled: false, 33 _normalScaleValueX: 0, 34 _normalScaleValueY: 0, 35 _fontName: "", 36 _fontSize: 0, 37 _onSelectedScaleOffset: 0, 38 _labelRenderer: "", 39 _textAreaSize:null, 40 _textVerticalAlignment:0, 41 _textHorizontalAlignment:0, 42 ctor: function () { 43 ccs.Widget.prototype.ctor.call(this); 44 this._touchScaleChangeEnabled = false; 45 this._normalScaleValueX = 0; 46 this._normalScaleValueY = 0; 47 this._fontName = "Thonburi"; 48 this._fontSize = 10; 49 this._onSelectedScaleOffset = 0.5; 50 this._labelRenderer = ""; 51 this._textAreaSize = cc.size(0, 0); 52 this._textVerticalAlignment = 0; 53 this._textHorizontalAlignment = 0; 54 }, 55 56 init: function () { 57 if (ccs.Widget.prototype.init.call(this)) { 58 return true; 59 } 60 return false; 61 }, 62 63 initRenderer: function () { 64 this._labelRenderer = cc.LabelTTF.create(); 65 cc.Node.prototype.addChild.call(this, this._labelRenderer, ccs.LABELRENDERERZ, -1); 66 }, 67 68 /** 69 * Changes the string value of label. 70 * @param {String} text 71 */ 72 setText: function (text) { 73 this._labelRenderer.setString(text); 74 this.labelScaleChangedWithSize(); 75 }, 76 77 /** 78 * Gets the string value of label. 79 * @returns {String} 80 */ 81 getStringValue: function () { 82 return this._labelRenderer.getString(); 83 }, 84 85 /** 86 * Gets the string length of label. 87 * @returns {Number} 88 */ 89 getStringLength: function () { 90 var str = this._labelRenderer.getString(); 91 return str.length; 92 }, 93 94 /** 95 * set fontSize 96 * @param {Number} size 97 */ 98 setFontSize: function (size) { 99 this._fontSize = size; 100 this._labelRenderer.setFontSize(size); 101 this.labelScaleChangedWithSize(); 102 }, 103 104 /** 105 * Get font Size 106 * @returns {Number} 107 */ 108 getFontSize:function(){ 109 return this._fontSize; 110 }, 111 112 /** 113 * set fontName 114 * @param {String} name 115 */ 116 setFontName: function (name) { 117 this._fontName = name; 118 this._labelRenderer.setFontName(name); 119 this.labelScaleChangedWithSize(); 120 }, 121 122 /** 123 * Get font name 124 * @returns {string} 125 */ 126 getFontName:function(){ 127 return this._fontName; 128 }, 129 130 /** 131 * set textAreaSize 132 * @param {cc.Size} size 133 */ 134 setTextAreaSize: function (size) { 135 this._textAreaSize.width = size.width; 136 this._textAreaSize.height = size.height; 137 this._labelRenderer.setDimensions(size); 138 this.labelScaleChangedWithSize(); 139 }, 140 141 /** 142 * Get textArea size 143 * @returns {cc.Size} 144 */ 145 getTextAreaSize:function(){ 146 return this._labelRenderer.getDimensions(); 147 }, 148 149 /** 150 * set Horizontal Alignment of cc.LabelTTF 151 * @param {cc.TEXT_ALIGNMENT_LEFT|cc.TEXT_ALIGNMENT_CENTER|cc.TEXT_ALIGNMENT_RIGHT} alignment Horizontal Alignment 152 */ 153 setTextHorizontalAlignment: function (alignment) { 154 this._textHorizontalAlignment = alignment; 155 this._labelRenderer.setHorizontalAlignment(alignment); 156 this.labelScaleChangedWithSize(); 157 }, 158 159 /** 160 * return Horizontal Alignment of cc.LabelTTF 161 * @returns {TEXT_ALIGNMENT_LEFT|TEXT_ALIGNMENT_CENTER|TEXT_ALIGNMENT_RIGHT} 162 */ 163 getTextHorizontalAlignment:function(){ 164 return this._labelRenderer.getHorizontalAlignment(); 165 }, 166 167 /** 168 * set Vertical Alignment of cc.LabelTTF 169 * @param {cc.VERTICAL_TEXT_ALIGNMENT_TOP|cc.VERTICAL_TEXT_ALIGNMENT_CENTER|cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM} verticalAlignment 170 */ 171 setTextVerticalAlignment: function (alignment) { 172 this._textVerticalAlignment = alignment; 173 this._labelRenderer.setVerticalAlignment(alignment); 174 this.labelScaleChangedWithSize(); 175 }, 176 177 /** 178 * Get text vertical alignment. 179 * @returns {VERTICAL_TEXT_ALIGNMENT_TOP|VERTICAL_TEXT_ALIGNMENT_CENTER|VERTICAL_TEXT_ALIGNMENT_BOTTOM} 180 */ 181 getTextVerticalAlignment:function(){ 182 return this._labelRenderer.getVerticalAlignment(); 183 }, 184 185 /** 186 * Gets the touch scale enabled of label. 187 * @returns {Boolean} 188 */ 189 getTouchScaleChangeAble: function () { 190 return this.isTouchScaleChangeEnabled(); 191 }, 192 193 /** 194 * Sets the touch scale enabled of label. 195 * @param {Boolean} enable 196 */ 197 setTouchScaleChangeEnabled: function (enable) { 198 this._touchScaleChangeEnabled = enable; 199 }, 200 201 /** 202 * Gets the touch scale enabled of label. 203 * @returns {Boolean} 204 */ 205 isTouchScaleChangeEnabled: function () { 206 return this._touchScaleChangeEnabled; 207 }, 208 209 onPressStateChangedToNormal: function () { 210 if (!this._touchScaleChangeEnabled) { 211 return; 212 } 213 this._labelRenderer.setScaleX(this._normalScaleValueX); 214 this._labelRenderer.setScaleY(this._normalScaleValueY); 215 }, 216 217 onPressStateChangedToPressed: function () { 218 if (!this._touchScaleChangeEnabled) { 219 return; 220 } 221 this._labelRenderer.setScaleX(this._normalScaleValueX + this._onSelectedScaleOffset); 222 this._labelRenderer.setScaleY(this._normalScaleValueY + this._onSelectedScaleOffset); 223 }, 224 225 onPressStateChangedToDisabled: function () { 226 227 }, 228 229 updateFlippedX: function () { 230 this._labelRenderer.setFlippedX(this._flippedX); 231 }, 232 233 updateFlippedY: function () { 234 this._labelRenderer.setFlippedY(this._flippedY); 235 }, 236 237 /** 238 * override "setAnchorPoint" of widget. 239 * @param {cc.Point|Number} point The anchor point of UILabel or The anchor point.x of UILabel. 240 * @param {Number} [y] The anchor point.y of UILabel. 241 */ 242 setAnchorPoint: function (point, y) { 243 if(y === undefined){ 244 ccs.Widget.prototype.setAnchorPoint.call(this, point); 245 this._labelRenderer.setAnchorPoint(point); 246 } else { 247 ccs.Widget.prototype.setAnchorPoint.call(this, point, y); 248 this._labelRenderer.setAnchorPoint(point, y); 249 } 250 }, 251 252 onSizeChanged: function () { 253 ccs.Widget.prototype.onSizeChanged.call(this); 254 this.labelScaleChangedWithSize(); 255 }, 256 257 /** 258 * override "getContentSize" method of widget. 259 * @returns {cc.Size} 260 */ 261 getContentSize: function () { 262 return this._labelRenderer.getContentSize(); 263 }, 264 265 /** 266 * override "getVirtualRenderer" method of widget. 267 * @returns {cc.Node} 268 */ 269 getVirtualRenderer: function () { 270 return this._labelRenderer; 271 }, 272 273 labelScaleChangedWithSize: function () { 274 if (this._ignoreSize) { 275 this._labelRenderer.setScale(1.0); 276 var renderSize = this._labelRenderer.getContentSize(); 277 this._size.width = renderSize.width; 278 this._size.height = renderSize.height; 279 this._normalScaleValueX = this._normalScaleValueY = 1; 280 } 281 else { 282 var textureSize = this._labelRenderer.getContentSize(); 283 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) { 284 this._labelRenderer.setScale(1.0); 285 return; 286 } 287 var scaleX = this._size.width / textureSize.width; 288 var scaleY = this._size.height / textureSize.height; 289 this._labelRenderer.setScaleX(scaleX); 290 this._labelRenderer.setScaleY(scaleY); 291 this._normalScaleValueX = scaleX; 292 this._normalScaleValueY = scaleY; 293 } 294 }, 295 296 updateTextureColor: function () { 297 this.updateColorToRenderer(this._labelRenderer); 298 }, 299 300 updateTextureOpacity: function () { 301 this.updateOpacityToRenderer(this._labelRenderer); 302 }, 303 304 updateTextureRGBA: function () { 305 this.updateRGBAToRenderer(this._labelRenderer); 306 }, 307 308 /** 309 * Returns the "class name" of widget. 310 * @returns {string} 311 */ 312 getDescription: function () { 313 return "Label"; 314 }, 315 316 createCloneInstance: function () { 317 return ccs.Label.create(); 318 }, 319 320 copySpecialProperties: function (uiLabel) { 321 this.setFontName(uiLabel._fontName); 322 this.setFontSize(uiLabel._labelRenderer.getFontSize()); 323 this.setText(uiLabel.getStringValue()); 324 this.setTouchScaleChangeEnabled(uiLabel._touchScaleChangeEnabled); 325 this.setTextAreaSize(uiLabel._size); 326 this.setTextHorizontalAlignment(uiLabel._textHorizontalAlignment); 327 this.setTextVerticalAlignment(uiLabel._textVerticalAlignment); 328 } 329 }); 330 /** 331 * allocates and initializes a UILabel. 332 * @constructs 333 * @return {ccs.Label} 334 * @example 335 * // example 336 * var uiLabel = ccs.Label.create(); 337 */ 338 ccs.Label.create = function () { 339 var uiLabel = new ccs.Label(); 340 if (uiLabel && uiLabel.init()) { 341 return uiLabel; 342 } 343 return null; 344 }; 345