Skip to content

Commit e80dd1c

Browse files
committed
Improve initialization of UI nodes
1 parent 8992d8e commit e80dd1c

22 files changed

+125
-372
lines changed

Diff for: extensions/ccui/base-classes/UIScale9SpriteCanvasRenderCmd.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
this._cacheScale9Sprite();
121121
this._dirtyFlag = this._dirtyFlag & flags.cacheDirty ^ this._dirtyFlag;
122122
}
123-
}
123+
};
124124

125125
proto._cacheScale9Sprite = function() {
126126
var node = this._node;

Diff for: extensions/ccui/base-classes/UIWidget.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
174174
this._positionPercent = cc.p(0, 0);
175175
this._nodes = [];
176176
this._layoutParameterType = ccui.LayoutParameter.NONE;
177-
this.init(); //TODO
177+
ccui.Widget.prototype.init.call(this);
178178
},
179179

180180
/**

Diff for: extensions/ccui/layouts/UIHBox.js

+3-28
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,12 @@ ccui.HBox = ccui.Layout.extend(/** @lends ccui.HBox# */{
3535
* @param {cc.Size} [size]
3636
*/
3737
ctor: function(size){
38-
ccui.Layout.prototype.ctor.call(this, size);
39-
if(size !== undefined)
40-
this.initWithSize(size);
41-
else
42-
this.init();
43-
},
38+
ccui.Layout.prototype.ctor.call(this);
39+
this.setLayoutType(ccui.Layout.LINEAR_HORIZONTAL);
4440

45-
/**
46-
* Initialize a HBox. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
47-
* @override
48-
* @returns {boolean}
49-
*/
50-
init: function(){
51-
if(ccui.Layout.prototype.init.call(this)){
52-
this.setLayoutType(ccui.Layout.LINEAR_HORIZONTAL);
53-
return true;
54-
}
55-
return false;
56-
},
57-
58-
/**
59-
* Initializes a HBox with size.
60-
* @param size
61-
* @returns {boolean}
62-
*/
63-
initWithSize: function(size){
64-
if(this.init()){
41+
if(size) {
6542
this.setContentSize(size);
66-
return true;
6743
}
68-
return false;
6944
}
7045
});
7146

Diff for: extensions/ccui/layouts/UILayout.js

+11-23
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,16 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{
8181
ctor: function () {
8282
this._layoutType = ccui.Layout.ABSOLUTE;
8383
this._widgetType = ccui.Widget.TYPE_CONTAINER;
84-
this._clippingType = ccui.Layout.CLIPPING_STENCIL;
84+
this._clippingType = ccui.Layout.CLIPPING_SCISSOR;
8585
this._colorType = ccui.Layout.BG_COLOR_NONE;
8686

8787
ccui.Widget.prototype.ctor.call(this);
88+
89+
this.ignoreContentAdaptWithSize(false);
90+
this.setContentSize(cc.size(0, 0));
91+
this.setAnchorPoint(0, 0);
92+
this.onPassFocusToChild = this._findNearestChildWidgetIndex.bind(this);
93+
8894
this._backGroundImageCapInsets = cc.rect(0, 0, 0, 0);
8995

9096
this._color = cc.color(255, 255, 255, 255);
@@ -236,22 +242,6 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{
236242
*/
237243
onPassFocusToChild: null,
238244

239-
/**
240-
* override "init" method of widget. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
241-
* @returns {boolean}
242-
* @override
243-
*/
244-
init: function () {
245-
if (ccui.Widget.prototype.init.call(this)) {
246-
this.ignoreContentAdaptWithSize(false);
247-
this.setContentSize(cc.size(0, 0));
248-
this.setAnchorPoint(0, 0);
249-
this.onPassFocusToChild = this._findNearestChildWidgetIndex.bind(this);
250-
return true;
251-
}
252-
return false;
253-
},
254-
255245
/**
256246
* Adds a widget to the container.
257247
* @param {ccui.Widget} widget
@@ -330,8 +320,9 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{
330320
default:
331321
break;
332322
}
333-
} else
323+
} else {
334324
ccui.Widget.prototype.visit.call(this, parentCmd);
325+
}
335326
},
336327

337328
/**
@@ -344,6 +335,7 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{
344335
return;
345336
this._clippingEnabled = able;
346337
switch (this._clippingType) {
338+
case ccui.Layout.CLIPPING_SCISSOR:
347339
case ccui.Layout.CLIPPING_STENCIL:
348340
if (able){
349341
this._clippingStencil = new cc.DrawNode();
@@ -369,10 +361,6 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{
369361
setClippingType: function (type) {
370362
if (type === this._clippingType)
371363
return;
372-
if(cc._renderType === cc.game.RENDER_TYPE_CANVAS && type === ccui.Layout.CLIPPING_SCISSOR){
373-
cc.log("Only supports STENCIL on canvas mode.");
374-
return;
375-
}
376364
var clippingEnabled = this.isClippingEnabled();
377365
this.setClippingEnabled(false);
378366
this._clippingType = type;
@@ -388,7 +376,7 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{
388376
},
389377

390378
_setStencilClippingSize: function (size) {
391-
if (this._clippingEnabled && this._clippingType === ccui.Layout.CLIPPING_STENCIL) {
379+
if (this._clippingEnabled) {
392380
var rect = [];
393381
rect[0] = cc.p(0, 0);
394382
rect[1] = cc.p(size.width, 0);

Diff for: extensions/ccui/layouts/UILayoutCanvasRenderCmd.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@
5757
default:
5858
break;
5959
}
60-
} else
60+
} else {
6161
ccui.Widget.CanvasRenderCmd.prototype.visit.call(this, parentCmd);
62+
}
6263
};
6364

6465
proto._onRenderSaveCmd = function(ctx, scaleX, scaleY){
@@ -103,7 +104,7 @@
103104
context.globalCompositeOperation = "destination-over";
104105
context.drawImage(this._locCache, 0, 0);
105106
context.restore();
106-
}else{
107+
} else {
107108
wrapper.restore(); //use for restore clip operation
108109
}
109110
};
@@ -126,7 +127,7 @@
126127
}
127128
};
128129

129-
proto.stencilClippingVisit = proto.scissorClippingVisit = function(parentCmd){
130+
proto.stencilClippingVisit = proto.scissorClippingVisit = function (parentCmd) {
130131
var node = this._node;
131132
if (!node._clippingStencil || !node._clippingStencil.isVisible())
132133
return;

Diff for: extensions/ccui/layouts/UIRelativeBox.js

+3-27
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,12 @@ ccui.RelativeBox = ccui.Layout.extend(/** @lends ccui.RelativeBox# */{
3535
* @param {cc.Size} [size]
3636
*/
3737
ctor: function(size){
38-
if(size)
39-
this.initWithSize(size);
40-
else
41-
this.init();
42-
},
38+
ccui.Layout.prototype.ctor.call(this);
39+
this.setLayoutType(ccui.Layout.RELATIVE);
4340

44-
/**
45-
* Initializes a relative box. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
46-
* @override
47-
* @returns {boolean}
48-
*/
49-
init: function(){
50-
if(ccui.Layout.prototype.init.call(this)){
51-
this.setLayoutType(ccui.Layout.RELATIVE);
52-
return true;
53-
}
54-
return false;
55-
},
56-
57-
/**
58-
* Initializes a relative box with size
59-
* @param {cc.Size} [size]
60-
* @returns {boolean}
61-
*/
62-
initWithSize: function(size){
63-
if(this.init()){
41+
if(size) {
6442
this.setContentSize(size);
65-
return true;
6643
}
67-
return false;
6844
}
6945
});
7046

Diff for: extensions/ccui/layouts/UIVBox.js

+5-17
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,12 @@ ccui.VBox = ccui.Layout.extend(/** @lends ccui.VBox# */{
3535
* @param {cc.Size} size
3636
*/
3737
ctor: function(size){
38-
ccui.Layout.prototype.ctor.call(this, size);
39-
if(size !== undefined)
40-
this.initWithSize(size);
41-
else
42-
this.init();
43-
},
38+
ccui.Layout.prototype.ctor.call(this);
39+
this.setLayoutType(ccui.Layout.LINEAR_VERTICAL);
4440

45-
/**
46-
* Initializes a VBox. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
47-
* @override
48-
* @returns {boolean}
49-
*/
50-
init: function(){
51-
if(ccui.Layout.prototype.init.call(this)){
52-
this.setLayoutType(ccui.Layout.LINEAR_VERTICAL);
53-
return true;
41+
if (size) {
42+
this.setContentSize(size);
5443
}
55-
return false;
5644
},
5745

5846
/**
@@ -62,7 +50,7 @@ ccui.VBox = ccui.Layout.extend(/** @lends ccui.VBox# */{
6250
*/
6351
initWithSize: function(size){
6452
if(this.init()){
65-
this.setContentSize(size);
53+
6654
return true;
6755
}
6856
return false;

Diff for: extensions/ccui/uiwidgets/UIButton.js

+1-16
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,10 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{
103103
this._titleColor = cc.color.WHITE;
104104
ccui.Widget.prototype.ctor.call(this);
105105
this.setTouchEnabled(true);
106-
this.init(normalImage, selectedImage, disableImage, texType);
107-
},
108106

109-
/**
110-
* Initializes a button. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
111-
* @param {String} normalImage
112-
* @param {String} [selectedImage=""]
113-
* @param {String} [disableImage=""]
114-
* @param {Number} [texType=ccui.Widget.LOCAL_TEXTURE]
115-
* @returns {boolean}
116-
* @override
117-
*/
118-
init: function (normalImage, selectedImage,disableImage, texType) {
119-
if (ccui.Widget.prototype.init.call(this)) {
120-
if(normalImage === undefined)
121-
return true;
107+
if (normalImage) {
122108
this.loadTextures(normalImage, selectedImage,disableImage, texType);
123109
}
124-
return false;
125110
},
126111

127112
_initRenderer: function () {

Diff for: extensions/ccui/uiwidgets/UICheckBox.js

+3-21
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,10 @@ ccui.CheckBox = ccui.Widget.extend(/** @lends ccui.CheckBox# */{
102102
backGroundSelected = undefined;
103103
}
104104
texType = texType === undefined ? 0 : texType;
105-
this.init(backGround, backGroundSelected,cross,backGroundDisabled,frontCrossDisabled,texType);
106-
},
107105

108-
/**
109-
* Initializes a checkBox. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
110-
* @param {String} backGround
111-
* @param {String} backGroundSelected
112-
* @param {String} cross
113-
* @param {String} backGroundDisabled
114-
* @param {String} frontCrossDisabled
115-
* @param {Number} [texType=ccui.Widget.LOCAL_TEXTURE]
116-
* @returns {boolean}
117-
* @override
118-
*/
119-
init: function (backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType) {
120-
if (ccui.Widget.prototype.init.call(this)) {
121-
this._isSelected = true;
122-
this.setSelected(false);
123-
this.loadTextures(backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType);
124-
return true;
125-
}
126-
return false;
106+
this._isSelected = true;
107+
this.setSelected(false);
108+
this.loadTextures(backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType);
127109
},
128110

129111
_initRenderer: function () {

Diff for: extensions/ccui/uiwidgets/UIImageView.js

+5-12
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,13 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{
5353
this._imageTextureSize = cc.size(this._capInsets.width, this._capInsets.height);
5454
ccui.Widget.prototype.ctor.call(this);
5555
texType = texType === undefined ? 0 : texType;
56-
this._init(imageFileName, texType);
57-
},
5856

59-
/**
60-
* Initializes an imageView. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
61-
* @param {String} imageFileName
62-
* @param {Number} [texType==ccui.Widget.LOCAL_TEXTURE]
63-
* @returns {boolean}
64-
*/
65-
_init: function(imageFileName, texType){
66-
if(imageFileName === undefined)
67-
this._imageTexType = ccui.Widget.LOCAL_TEXTURE;
68-
else
57+
if(imageFileName) {
6958
this.loadTexture(imageFileName, texType);
59+
}
60+
else {
61+
this._imageTexType = ccui.Widget.LOCAL_TEXTURE;
62+
}
7063
},
7164

7265
_initRenderer: function () {

0 commit comments

Comments
 (0)