Skip to content

Commit c93c8e1

Browse files
author
pandamicro
committed
Improve UI and support lazy loading
1 parent 523b231 commit c93c8e1

File tree

4 files changed

+97
-24
lines changed

4 files changed

+97
-24
lines changed

extensions/ccui/base-classes/UIWidget.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
204204
cc.eventManager.addListener(locListener, this);
205205
if(!this._usingLayoutComponent)
206206
this.updateSizeAndPosition();
207+
if (this._sizeDirty)
208+
this._onSizeChanged();
207209
cc.ProtectedNode.prototype.onEnter.call(this);
208210
},
209211

@@ -347,9 +349,10 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
347349
* @override
348350
*/
349351
setContentSize: function(contentSize, height){
350-
var locWidth = (height === undefined) ? contentSize.width : contentSize;
351-
var locHeight = (height === undefined) ? contentSize.height : height;
352-
cc.Node.prototype.setContentSize.call(this, locWidth, locHeight);
352+
cc.Node.prototype.setContentSize.call(this, contentSize, height);
353+
354+
var locWidth = this._contentSize.width;
355+
var locHeight = this._contentSize.height;
353356

354357
this._customSize.width = locWidth;
355358
this._customSize.height = locHeight;
@@ -364,10 +367,18 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
364367
this._sizePercent.x = (pSize.width > 0.0) ? locWidth / pSize.width : 0.0;
365368
this._sizePercent.y = (pSize.height > 0.0) ? locHeight / pSize.height : 0.0;
366369
}
367-
this._onSizeChanged();
370+
371+
if (this._running) {
372+
this._onSizeChanged();
373+
} else {
374+
this._sizeDirty = true;
375+
}
368376
},
369377

370378
_setWidth: function (w) {
379+
if (w === this._contentSize.width) {
380+
return;
381+
}
371382
cc.Node.prototype._setWidth.call(this, w);
372383
this._customSize.width = w;
373384
if(this._unifySize){
@@ -381,9 +392,18 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
381392
var locWidth = widgetParent ? widgetParent.width : this._parent.width;
382393
this._sizePercent.x = locWidth > 0 ? this._customSize.width / locWidth : 0;
383394
}
384-
this._onSizeChanged();
395+
396+
if (this._running) {
397+
this._onSizeChanged();
398+
} else {
399+
this._sizeDirty = true;
400+
}
385401
},
386402
_setHeight: function (h) {
403+
if (h === this._contentSize.height) {
404+
return;
405+
}
406+
387407
cc.Node.prototype._setHeight.call(this, h);
388408
this._customSize.height = h;
389409
if(this._unifySize){
@@ -397,7 +417,12 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
397417
var locH = widgetParent ? widgetParent.height : this._parent.height;
398418
this._sizePercent.y = locH > 0 ? this._customSize.height / locH : 0;
399419
}
400-
this._onSizeChanged();
420+
421+
if (this._running) {
422+
this._onSizeChanged();
423+
} else {
424+
this._sizeDirty = true;
425+
}
401426
},
402427

403428
/**
@@ -629,12 +654,13 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
629654
*/
630655
_onSizeChanged: function () {
631656
if(!this._usingLayoutComponent){
632-
var locChildren = this.getChildren();
657+
var locChildren = this.getChildren();
633658
for (var i = 0, len = locChildren.length; i < len; i++) {
634659
var child = locChildren[i];
635-
if(child instanceof ccui.Widget)
660+
if (child instanceof ccui.Widget)
636661
child.updateSizeAndPosition();
637662
}
663+
this._sizeDirty = false;
638664
}
639665
},
640666

@@ -1196,6 +1222,7 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
11961222
return;
11971223
}
11981224
this._positionPercent.x = percent;
1225+
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
11991226
},
12001227
_setYPercent: function (percent) {
12011228
if (this._usingLayoutComponent){
@@ -1205,6 +1232,7 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
12051232
return;
12061233
}
12071234
this._positionPercent.y = percent;
1235+
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
12081236
},
12091237

12101238
/**
@@ -1501,7 +1529,6 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
15011529
if (parameter)
15021530
this.setLayoutParameter(parameter.clone());
15031531
}
1504-
this._onSizeChanged();
15051532
},
15061533

15071534
/*temp action*/

extensions/ccui/uiwidgets/UIText.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ ccui.Text = ccui.Widget.extend(/** @lends ccui.Text# */{
105105
this._labelRendererAdaptDirty = true;
106106
},
107107

108+
_setString: function (text) {
109+
if(text === this._labelRenderer.getString()) return;
110+
111+
this._labelRenderer.setString(text);
112+
this._labelRendererAdaptDirty = true;
113+
},
114+
108115
/**
109116
* Gets the string value of ccui.Text.
110117
* @deprecated since v3.0, please use getString instead.
@@ -142,6 +149,12 @@ ccui.Text = ccui.Widget.extend(/** @lends ccui.Text# */{
142149
this._labelRendererAdaptDirty = true;
143150
},
144151

152+
_setFontSize: function (size) {
153+
this._labelRenderer.setFontSize(size);
154+
this._fontSize = size;
155+
this._labelRendererAdaptDirty = true;
156+
},
157+
145158
/**
146159
* Returns font Size of ccui.Text
147160
* @returns {Number}
@@ -161,6 +174,16 @@ ccui.Text = ccui.Widget.extend(/** @lends ccui.Text# */{
161174
this._labelRendererAdaptDirty = true;
162175
},
163176

177+
_setFontName: function (name) {
178+
this._fontName = name;
179+
this._labelRenderer.setFontName(name);
180+
this._labelRendererAdaptDirty = true;
181+
},
182+
183+
_updateUITextContentSize: function () {
184+
this._updateContentSizeWithTextureSize(this._labelRenderer.getContentSize());
185+
},
186+
164187
/**
165188
* Returns font name of ccui.Text.
166189
* @returns {string}
@@ -203,6 +226,14 @@ ccui.Text = ccui.Widget.extend(/** @lends ccui.Text# */{
203226
this._labelRendererAdaptDirty = true;
204227
},
205228

229+
_setTextAreaSize: function (size) {
230+
this._labelRenderer.setDimensions(size);
231+
if (!this._ignoreSize){
232+
this._customSize = size;
233+
}
234+
this._labelRendererAdaptDirty = true;
235+
},
236+
206237
/**
207238
* Returns renderer's dimension.
208239
* @returns {cc.Size}
@@ -221,6 +252,12 @@ ccui.Text = ccui.Widget.extend(/** @lends ccui.Text# */{
221252
this._labelRendererAdaptDirty = true;
222253
},
223254

255+
256+
_setTextHorizontalAlignment: function (alignment) {
257+
this._labelRenderer.setHorizontalAlignment(alignment);
258+
this._labelRendererAdaptDirty = true;
259+
},
260+
224261
/**
225262
* Returns Horizontal Alignment of label
226263
* @returns {TEXT_ALIGNMENT_LEFT|TEXT_ALIGNMENT_CENTER|TEXT_ALIGNMENT_RIGHT}
@@ -239,6 +276,10 @@ ccui.Text = ccui.Widget.extend(/** @lends ccui.Text# */{
239276
this._labelRendererAdaptDirty = true;
240277
},
241278

279+
_setTextVerticalAlignment: function (alignment) {
280+
this._labelRenderer.setVerticalAlignment(alignment);
281+
this._labelRendererAdaptDirty = true;
282+
},
242283
/**
243284
* Gets text vertical alignment.
244285
* @returns {VERTICAL_TEXT_ALIGNMENT_TOP|VERTICAL_TEXT_ALIGNMENT_CENTER|VERTICAL_TEXT_ALIGNMENT_BOTTOM}

extensions/ccui/uiwidgets/UITextAtlas.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ ccui.TextAtlas = ccui.Widget.extend(/** @lends ccui.TextAtlas# */{
6363
this._labelAtlasRenderer = new cc.LabelAtlas();
6464
this._labelAtlasRenderer.setAnchorPoint(cc.p(0.5, 0.5));
6565
this.addProtectedChild(this._labelAtlasRenderer, ccui.TextAtlas.RENDERER_ZORDER, -1);
66+
67+
this._labelAtlasRenderer.addEventListener('load', function () {
68+
this._updateContentSizeWithTextureSize(this._labelAtlasRenderer.getContentSize());
69+
this._findLayout();
70+
}, this);
6671
},
6772

6873
/**
@@ -97,7 +102,7 @@ ccui.TextAtlas = ccui.Widget.extend(/** @lends ccui.TextAtlas# */{
97102
* @param {String} value
98103
*/
99104
setString: function (value) {
100-
if(value === this._labelAtlasRenderer.getString())
105+
if (value === this._labelAtlasRenderer.getString())
101106
return;
102107
this._stringValue = value;
103108
this._labelAtlasRenderer.setString(value);
@@ -137,7 +142,7 @@ ccui.TextAtlas = ccui.Widget.extend(/** @lends ccui.TextAtlas# */{
137142
* Returns the length of string.
138143
* @returns {*|Number|long|int}
139144
*/
140-
getStringLength: function(){
145+
getStringLength: function () {
141146
return this._labelAtlasRenderer.getStringLength();
142147
},
143148

@@ -146,8 +151,8 @@ ccui.TextAtlas = ccui.Widget.extend(/** @lends ccui.TextAtlas# */{
146151
this._labelAtlasRendererAdaptDirty = true;
147152
},
148153

149-
_adaptRenderers: function(){
150-
if (this._labelAtlasRendererAdaptDirty){
154+
_adaptRenderers: function () {
155+
if (this._labelAtlasRendererAdaptDirty) {
151156
this._labelAtlasScaleChangedWithSize();
152157
this._labelAtlasRendererAdaptDirty = false;
153158
}
@@ -158,7 +163,7 @@ ccui.TextAtlas = ccui.Widget.extend(/** @lends ccui.TextAtlas# */{
158163
* @overrider
159164
* @returns {cc.Size}
160165
*/
161-
getVirtualRendererSize: function(){
166+
getVirtualRendererSize: function () {
162167
return this._labelAtlasRenderer.getContentSize();
163168
},
164169

@@ -195,7 +200,7 @@ ccui.TextAtlas = ccui.Widget.extend(/** @lends ccui.TextAtlas# */{
195200
},
196201

197202
_copySpecialProperties: function (labelAtlas) {
198-
if (labelAtlas){
203+
if (labelAtlas) {
199204
this.setProperty(labelAtlas._stringValue, labelAtlas._charMapFileName, labelAtlas._itemWidth, labelAtlas._itemHeight, labelAtlas._startCharMap);
200205
}
201206
},
@@ -228,4 +233,4 @@ ccui.TextAtlas.create = function (stringValue, charMapFile, itemWidth, itemHeigh
228233
* The zOrder value of ccui.TextAtlas's renderer.
229234
* @type {number}
230235
*/
231-
ccui.TextAtlas.RENDERER_ZORDER = -1;
236+
ccui.TextAtlas.RENDERER_ZORDER = -1;

extensions/ccui/uiwidgets/UITextBMFont.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ccui.LabelBMFont = ccui.TextBMFont = ccui.Widget.extend(/** @lends ccui.TextBMFo
4949
*/
5050
ctor: function (text, filename) {
5151
ccui.Widget.prototype.ctor.call(this);
52+
this._loader = new cc.Sprite.LoadManager();
5253

5354
if (filename !== undefined) {
5455
this.setFntFile(filename);
@@ -80,14 +81,6 @@ ccui.LabelBMFont = ccui.TextBMFont = ccui.Widget.extend(/** @lends ccui.TextBMFo
8081
if (!locRenderer._textureLoaded) {
8182
locRenderer.addEventListener("load", function () {
8283
_self.setFntFile(_self._fntFileName);
83-
var parent = _self.parent;
84-
while (parent) {
85-
if (parent.requestDoLayout) {
86-
parent.requestDoLayout();
87-
break;
88-
}
89-
parent = parent.parent;
90-
}
9184
});
9285
}
9386
},
@@ -107,6 +100,13 @@ ccui.LabelBMFont = ccui.TextBMFont = ccui.Widget.extend(/** @lends ccui.TextBMFo
107100
* @param {String} value
108101
*/
109102
setString: function (value) {
103+
this._loader.clear();
104+
if (!this._labelBMFontRenderer._textureLoaded) {
105+
this._loader.add(this._labelBMFontRenderer, function () {
106+
this.setString(value);
107+
}, this);
108+
return;
109+
}
110110
if (value === this._labelBMFontRenderer.getString())
111111
return;
112112
this._stringValue = value;

0 commit comments

Comments
 (0)