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