forked from cocos2d/cocos2d-html5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIText.js
498 lines (444 loc) · 15.8 KB
/
UIText.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/****************************************************************************
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
/**
* The text control of Cocos UI.
* @class
* @extends ccui.Widget
*
* @property {Number} boundingWidth - Width of the bounding area of label, the real content width is limited by boundingWidth
* @property {Number} boundingHeight - Height of the bounding area of label, the real content height is limited by boundingHeight
* @property {String} string - The content string of the label
* @property {Number} stringLength - <@readonly> The content string length of the label
* @property {String} font - The label font with a style string: e.g. "18px Verdana"
* @property {String} fontName - The label font name
* @property {Number} fontSize - The label font size
* @property {Number} textAlign - Horizontal Alignment of label, cc.TEXT_ALIGNMENT_LEFT|cc.TEXT_ALIGNMENT_CENTER|cc.TEXT_ALIGNMENT_RIGHT
* @property {Number} verticalAlign - Vertical Alignment of label: cc.VERTICAL_TEXT_ALIGNMENT_TOP|cc.VERTICAL_TEXT_ALIGNMENT_CENTER|cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM
* @property {Boolean} touchScaleEnabled - Indicate whether the label will scale when touching
*/
ccui.Text = ccui.Widget.extend(/** @lends ccui.Text# */{
_touchScaleChangeEnabled: false,
_normalScaleValueX: 1,
_normalScaleValueY: 1,
_fontName: "Arial",
_fontSize: 16,
_onSelectedScaleOffset:0.5,
_labelRenderer: "",
_textAreaSize: null,
_textVerticalAlignment: 0,
_textHorizontalAlignment: 0,
_className: "Text",
_type: null,
_labelRendererAdaptDirty: true,
/**
* allocates and initializes a UILabel.
* Constructor of ccui.Text. override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
* @param {String} textContent
* @param {String} fontName
* @param {Number} fontSize
* @example
* // example
* var uiLabel = new ccui.Text();
*/
ctor: function (textContent, fontName, fontSize) {
this._type = ccui.Text.Type.SYSTEM;
this._textAreaSize = cc.size(0, 0);
ccui.Widget.prototype.ctor.call(this);
if (fontSize !== undefined) {
this.setFontName(fontName);
this.setFontSize(fontSize);
this.setString(textContent);
} else {
this.setFontName(this._fontName);
}
},
_initRenderer: function () {
this._labelRenderer = new cc.LabelTTF();
this.addProtectedChild(this._labelRenderer, ccui.Text.RENDERER_ZORDER, -1);
},
/**
* Changes the value of ccui.Text.
* @deprecated since v3.0, please use setString() instead.
* @param {String} text
*/
setText: function (text) {
cc.log("Please use the setString");
this.setString(text);
},
/**
* Changes the value of ccui.Text.
* @param {String} text
*/
setString: function (text) {
if(text === this._labelRenderer.getString())
return;
this._labelRenderer.setString(text);
this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
this._labelRendererAdaptDirty = true;
},
/**
* Gets the string value of ccui.Text.
* @deprecated since v3.0, please use getString instead.
* @returns {String}
*/
getStringValue: function () {
cc.log("Please use the getString");
return this._labelRenderer.getString();
},
/**
* Gets the string value of ccui.Text.
* @returns {String}
*/
getString: function () {
return this._labelRenderer.getString();
},
/**
* Gets the string length of ccui.Text.
* @returns {Number}
*/
getStringLength: function () {
return this._labelRenderer.getStringLength();
},
/**
* Sets fontSize
* @param {Number} size
*/
setFontSize: function (size) {
this._labelRenderer.setFontSize(size);
this._fontSize = size;
this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
this._labelRendererAdaptDirty = true;
},
/**
* Returns font Size of ccui.Text
* @returns {Number}
*/
getFontSize: function () {
return this._fontSize;
},
/**
* Sets font name
* @return {String} name
*/
setFontName: function (name) {
this._fontName = name;
this._labelRenderer.setFontName(name);
this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
this._labelRendererAdaptDirty = true;
},
/**
* Returns font name of ccui.Text.
* @returns {string}
*/
getFontName: function () {
return this._fontName;
},
_setFont: function (font) {
var res = cc.LabelTTF._fontStyleRE.exec(font);
if (res) {
this._fontSize = parseInt(res[1]);
this._fontName = res[2];
this._labelRenderer._setFont(font);
this._labelScaleChangedWithSize();
}
},
_getFont: function () {
return this._labelRenderer._getFont();
},
/**
* Returns the type of ccui.Text.
* @returns {null}
*/
getType: function(){
return this._type;
},
/**
* Sets text Area Size
* @param {cc.Size} size
*/
setTextAreaSize: function (size) {
this._labelRenderer.setDimensions(size);
if (!this._ignoreSize){
this._customSize = size;
}
this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
this._labelRendererAdaptDirty = true;
},
/**
* Returns renderer's dimension.
* @returns {cc.Size}
*/
getTextAreaSize: function(){
return this._labelRenderer.getDimensions();
},
/**
* Sets Horizontal Alignment of cc.LabelTTF
* @param {cc.TEXT_ALIGNMENT_LEFT|cc.TEXT_ALIGNMENT_CENTER|cc.TEXT_ALIGNMENT_RIGHT} alignment Horizontal Alignment
*/
setTextHorizontalAlignment: function (alignment) {
this._labelRenderer.setHorizontalAlignment(alignment);
this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
this._labelRendererAdaptDirty = true;
},
/**
* Returns Horizontal Alignment of label
* @returns {TEXT_ALIGNMENT_LEFT|TEXT_ALIGNMENT_CENTER|TEXT_ALIGNMENT_RIGHT}
*/
getTextHorizontalAlignment: function () {
return this._labelRenderer.getHorizontalAlignment();
},
/**
* Sets Vertical Alignment of label
* @param {cc.VERTICAL_TEXT_ALIGNMENT_TOP|cc.VERTICAL_TEXT_ALIGNMENT_CENTER|cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM} alignment
*/
setTextVerticalAlignment: function (alignment) {
this._labelRenderer.setVerticalAlignment(alignment);
this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
this._labelRendererAdaptDirty = true;
},
/**
* Gets text vertical alignment.
* @returns {VERTICAL_TEXT_ALIGNMENT_TOP|VERTICAL_TEXT_ALIGNMENT_CENTER|VERTICAL_TEXT_ALIGNMENT_BOTTOM}
*/
getTextVerticalAlignment: function () {
return this._labelRenderer.getVerticalAlignment();
},
/**
* Sets the touch scale enabled of label.
* @param {Boolean} enable
*/
setTouchScaleChangeEnabled: function (enable) {
this._touchScaleChangeEnabled = enable;
},
/**
* Gets the touch scale enabled of label.
* @returns {Boolean}
*/
isTouchScaleChangeEnabled: function () {
return this._touchScaleChangeEnabled;
},
_onPressStateChangedToNormal: function () {
if (!this._touchScaleChangeEnabled)
return;
this._labelRenderer.setScaleX(this._normalScaleValueX);
this._labelRenderer.setScaleY(this._normalScaleValueY);
},
_onPressStateChangedToPressed: function () {
if (!this._touchScaleChangeEnabled)
return;
this._labelRenderer.setScaleX(this._normalScaleValueX + this._onSelectedScaleOffset);
this._labelRenderer.setScaleY(this._normalScaleValueY + this._onSelectedScaleOffset);
},
_onPressStateChangedToDisabled: function () {
},
_onSizeChanged: function () {
ccui.Widget.prototype._onSizeChanged.call(this);
this._labelRendererAdaptDirty = true;
},
_adaptRenderers: function(){
if (this._labelRendererAdaptDirty) {
this._labelScaleChangedWithSize();
this._labelRendererAdaptDirty = false;
}
},
/**
* Returns the renderer's content size.
* @override
* @returns {cc.Size}
*/
getVirtualRendererSize: function(){
return this._labelRenderer.getContentSize();
},
/**
* Returns the renderer of ccui.Text.
* @returns {cc.Node}
*/
getVirtualRenderer: function () {
return this._labelRenderer;
},
//@since v3.3
getAutoRenderSize: function(){
var virtualSize = this._labelRenderer.getContentSize();
if (!this._ignoreSize) {
this._labelRenderer.setDimensions(0, 0);
virtualSize = this._labelRenderer.getContentSize();
this._labelRenderer.setDimensions(this._contentSize.width, this._contentSize.height);
}
return virtualSize;
},
_labelScaleChangedWithSize: function () {
var locContentSize = this._contentSize;
if (this._ignoreSize) {
this._labelRenderer.setDimensions(0,0);
this._labelRenderer.setScale(1.0);
this._normalScaleValueX = this._normalScaleValueY = 1;
} else {
this._labelRenderer.setDimensions(cc.size(locContentSize.width, locContentSize.height));
var textureSize = this._labelRenderer.getContentSize();
if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
this._labelRenderer.setScale(1.0);
return;
}
var scaleX = locContentSize.width / textureSize.width;
var scaleY = locContentSize.height / textureSize.height;
this._labelRenderer.setScaleX(scaleX);
this._labelRenderer.setScaleY(scaleY);
this._normalScaleValueX = scaleX;
this._normalScaleValueY = scaleY;
}
this._labelRenderer.setPosition(locContentSize.width / 2.0, locContentSize.height / 2.0);
},
/**
* Returns the "class name" of ccui.Text.
* @returns {string}
*/
getDescription: function () {
return "Label";
},
/**
* Enables shadow style and sets color, offset and blur radius styles.
* @param {cc.Color} shadowColor
* @param {cc.Size} offset
* @param {Number} blurRadius
*/
enableShadow: function(shadowColor, offset, blurRadius){
this._labelRenderer.enableShadow(shadowColor, offset, blurRadius);
},
/**
* Enables outline style and sets outline's color and size.
* @param {cc.Color} outlineColor
* @param {cc.Size} outlineSize
*/
enableOutline: function(outlineColor, outlineSize){
this._labelRenderer.enableStroke(outlineColor, outlineSize);
},
/**
* Enables glow color
* @param glowColor
*/
enableGlow: function(glowColor){
if (this._type === ccui.Text.Type.TTF)
this._labelRenderer.enableGlow(glowColor);
},
/**
* Disables renderer's effect.
*/
disableEffect: function(){
if(this._labelRenderer.disableEffect)
this._labelRenderer.disableEffect();
},
_createCloneInstance: function () {
return new ccui.Text();
},
_copySpecialProperties: function (uiLabel) {
if(uiLabel instanceof ccui.Text){
this.setFontName(uiLabel._fontName);
this.setFontSize(uiLabel.getFontSize());
this.setString(uiLabel.getString());
this.setTouchScaleChangeEnabled(uiLabel.touchScaleEnabled);
this.setTextAreaSize(uiLabel._textAreaSize);
this.setTextHorizontalAlignment(uiLabel._labelRenderer.getHorizontalAlignment());
this.setTextVerticalAlignment(uiLabel._labelRenderer.getVerticalAlignment());
this.setContentSize(uiLabel.getContentSize());
this.setTextColor(uiLabel.getTextColor());
}
},
_setBoundingWidth: function (value) {
this._textAreaSize.width = value;
this._labelRenderer._setBoundingWidth(value);
this._labelScaleChangedWithSize();
},
_setBoundingHeight: function (value) {
this._textAreaSize.height = value;
this._labelRenderer._setBoundingHeight(value);
this._labelScaleChangedWithSize();
},
_getBoundingWidth: function () {
return this._textAreaSize.width;
},
_getBoundingHeight: function () {
return this._textAreaSize.height;
},
_changePosition: function(){
this._adaptRenderers();
},
setColor: function(color){
cc.ProtectedNode.prototype.setColor.call(this, color);
this._labelRenderer.setColor(color);
},
setTextColor: function(color){
this._labelRenderer.setFontFillColor(color);
},
getTextColor: function(){
return this._labelRenderer._getFillStyle();
}
});
var _p = ccui.Text.prototype;
// Extended properties
/** @expose */
_p.boundingWidth;
cc.defineGetterSetter(_p, "boundingWidth", _p._getBoundingWidth, _p._setBoundingWidth);
/** @expose */
_p.boundingHeight;
cc.defineGetterSetter(_p, "boundingHeight", _p._getBoundingHeight, _p._setBoundingHeight);
/** @expose */
_p.string;
cc.defineGetterSetter(_p, "string", _p.getString, _p.setString);
/** @expose */
_p.stringLength;
cc.defineGetterSetter(_p, "stringLength", _p.getStringLength);
/** @expose */
_p.font;
cc.defineGetterSetter(_p, "font", _p._getFont, _p._setFont);
/** @expose */
_p.fontSize;
cc.defineGetterSetter(_p, "fontSize", _p.getFontSize, _p.setFontSize);
/** @expose */
_p.fontName;
cc.defineGetterSetter(_p, "fontName", _p.getFontName, _p.setFontName);
/** @expose */
_p.textAlign;
cc.defineGetterSetter(_p, "textAlign", _p.getTextHorizontalAlignment, _p.setTextHorizontalAlignment);
/** @expose */
_p.verticalAlign;
cc.defineGetterSetter(_p, "verticalAlign", _p.getTextVerticalAlignment, _p.setTextVerticalAlignment);
_p = null;
/**
* allocates and initializes a UILabel.
* @deprecated since v3.0, please use new ccui.Text() instead.
* @return {ccui.Text}
*/
ccui.Label = ccui.Text.create = function (textContent, fontName, fontSize) {
return new ccui.Text(textContent, fontName, fontSize);
};
/**
* The zOrder value of ccui.Text's renderer.
* @constant
* @type {number}
*/
ccui.Text.RENDERER_ZORDER = -1;
/**
* @ignore
*/
ccui.Text.Type = {
SYSTEM: 0,
TTF: 1
};