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.NodeRGBA.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      * set fontName
106      * @param {String} name
107      */
108     setFontName: function (name) {
109         this._fontName = name;
110         this._labelRenderer.setFontName(name);
111         this.labelScaleChangedWithSize();
112     },
113 
114     /**
115      * set textAreaSize
116      * @param {cc.Size} size
117      */
118     setTextAreaSize: function (size) {
119         this._textAreaSize.width = size.width;
120         this._textAreaSize.height = size.height;
121         this._labelRenderer.setDimensions(size);
122         this.labelScaleChangedWithSize();
123     },
124 
125     /**
126      * set Horizontal Alignment of cc.LabelTTF
127      * @param {cc.TEXT_ALIGNMENT_LEFT|cc.TEXT_ALIGNMENT_CENTER|cc.TEXT_ALIGNMENT_RIGHT} alignment Horizontal Alignment
128      */
129     setTextHorizontalAlignment: function (alignment) {
130         this._textHorizontalAlignment = alignment;
131         this._labelRenderer.setHorizontalAlignment(alignment);
132         this.labelScaleChangedWithSize();
133     },
134 
135     /**
136      * set Vertical Alignment of cc.LabelTTF
137      * @param {cc.VERTICAL_TEXT_ALIGNMENT_TOP|cc.VERTICAL_TEXT_ALIGNMENT_CENTER|cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM} verticalAlignment
138      */
139     setTextVerticalAlignment: function (alignment) {
140         this._textVerticalAlignment = alignment;
141         this._labelRenderer.setVerticalAlignment(alignment);
142         this.labelScaleChangedWithSize();
143     },
144 
145     /**
146      * Sets the touch scale enabled of label.
147      * @param {Boolean} enable
148      */
149     setTouchScaleChangeAble: function (enable) {
150         this.setTouchScaleChangeEnabled(enable);
151     },
152 
153     /**
154      * Gets the touch scale enabled of label.
155      * @returns {Boolean}
156      */
157     getTouchScaleChangeAble: function () {
158         return this.isTouchScaleChangeEnabled();
159     },
160 
161     /**
162      * Sets the touch scale enabled of label.
163      * @param {Boolean} enable
164      */
165     setTouchScaleChangeEnabled: function (enable) {
166         this._touchScaleChangeEnabled = enable;
167         this._normalScaleValueX = this.getScaleX();
168         this._normalScaleValueY = this.getScaleY();
169     },
170 
171     /**
172      * Gets the touch scale enabled of label.
173      * @returns {Boolean}
174      */
175     isTouchScaleChangeEnabled: function () {
176         return this._touchScaleChangeEnabled;
177     },
178 
179     onPressStateChangedToNormal: function () {
180         if (!this._touchScaleChangeEnabled) {
181             return;
182         }
183         this.clickScale(this._normalScaleValueX,this._normalScaleValueY);
184     },
185 
186     onPressStateChangedToPressed: function () {
187         if (!this._touchScaleChangeEnabled) {
188             return;
189         }
190         this.clickScale(this._normalScaleValueX + this._onSelectedScaleOffset,this._normalScaleValueY + this._onSelectedScaleOffset);
191     },
192 
193     onPressStateChangedToDisabled: function () {
194 
195     },
196 
197     /**
198      * set scale
199      * @param {Number} scale
200      */
201     setScale: function (scale) {
202         ccs.Widget.prototype.setScale.call(this, scale);
203         this._normalScaleValueX = this._normalScaleValueY = scale;
204     },
205 
206     /**
207      * set scaleX
208      * @param {Number} scaleX
209      */
210     setScaleX: function (scaleX) {
211         ccs.Widget.prototype.setScaleX.call(this, scaleX);
212         this._normalScaleValueX = scaleX;
213     },
214 
215     /**
216      * set scaleY
217      * @param {Number} scaleY
218      */
219     setScaleY: function (scaleY) {
220         ccs.Widget.prototype.setScaleY.call(this, scaleY);
221         this._normalScaleValueY = scaleY;
222     },
223 
224     clickScale: function (scale, scaleY) {
225         this.setScale(scale, scaleY);
226     },
227 
228     /**
229      * override "setFlippedX" of widget.
230      * @param {Boolean} flipX
231      */
232     setFlippedX: function (flipX) {
233         this._labelRenderer.setFlippedX(flipX);
234     },
235 
236     /**
237      * override "setFlippedY" of widget.
238      * @param {Boolean} flipY
239      */
240     setFlippedY: function (flipY) {
241         this._labelRenderer.setFlippedY(flipY);
242     },
243 
244     /**
245      * override "isFlippedX" of widget.
246      * @returns {Boolean}
247      */
248     isFlippedX: function () {
249         return this._labelRenderer.isFlippedX();
250     },
251 
252     /**
253      * override "isFlippedY" of widget.
254      * @returns {Boolean}
255      */
256     isFlippedY: function () {
257         return this._labelRenderer.isFlippedY();
258     },
259 
260     /**
261      * override "setAnchorPoint" of widget.
262      * @param {cc.Point|Number} point The anchor point of UILabel or The anchor point.x of UILabel.
263      * @param {Number} [y] The anchor point.y of UILabel.
264      */
265     setAnchorPoint: function (point, y) {
266         if(arguments.length === 2){
267             ccs.Widget.prototype.setAnchorPoint.call(this, point, y);
268             this._labelRenderer.setAnchorPoint(point, y);
269         } else {
270             ccs.Widget.prototype.setAnchorPoint.call(this, point);
271             this._labelRenderer.setAnchorPoint(point);
272         }
273     },
274 
275     onSizeChanged: function () {
276         ccs.Widget.prototype.onSizeChanged.call(this);
277         this.labelScaleChangedWithSize();
278     },
279 
280     /**
281      * override "getContentSize" method of widget.
282      * @returns {cc.Size}
283      */
284     getContentSize: function () {
285         return this._labelRenderer.getContentSize();
286     },
287 
288     /**
289      * override "getVirtualRenderer" method of widget.
290      * @returns {cc.Node}
291      */
292     getVirtualRenderer: function () {
293         return this._labelRenderer;
294     },
295 
296     labelScaleChangedWithSize: function () {
297         if (this._ignoreSize) {
298             this._labelRenderer.setScale(1.0);
299             var renderSize = this._labelRenderer.getContentSize();
300             this._size.width = renderSize.width;
301             this._size.height = renderSize.height;
302         }
303         else {
304             var textureSize = this._labelRenderer.getContentSize();
305             if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
306                 this._labelRenderer.setScale(1.0);
307                 return;
308             }
309             var scaleX = this._size.width / textureSize.width;
310             var scaleY = this._size.height / textureSize.height;
311             this._labelRenderer.setScaleX(scaleX);
312             this._labelRenderer.setScaleY(scaleY);
313         }
314     },
315 
316     /**
317      * Returns the "class name" of widget.
318      * @returns {string}
319      */
320     getDescription: function () {
321         return "Label";
322     },
323 
324     createCloneInstance: function () {
325         return ccs.Label.create();
326     },
327 
328     copySpecialProperties: function (uiLabel) {
329         this.setFontName(uiLabel._fontName);
330         this.setFontSize(uiLabel._labelRenderer.getFontSize());
331         this.setText(uiLabel.getStringValue());
332         this.setTouchScaleChangeEnabled(uiLabel._touchScaleChangeEnabled);
333         this.setTextAreaSize(uiLabel._size);
334         this.setTextHorizontalAlignment(uiLabel._textHorizontalAlignment);
335         this.setTextVerticalAlignment(uiLabel._textVerticalAlignment);
336     }
337 });
338 /**
339  * allocates and initializes a UILabel.
340  * @constructs
341  * @return {ccs.Label}
342  * @example
343  * // example
344  * var uiLabel = ccs.Label.create();
345  */
346 ccs.Label.create = function () {
347     var uiLabel = new ccs.Label();
348     if (uiLabel && uiLabel.init()) {
349         return uiLabel;
350     }
351     return null;
352 };
353