Skip to content

Commit 289944b

Browse files
committed
Issue cocos2d#1267: refactor ccui's visit
1 parent 5dde1df commit 289944b

File tree

12 files changed

+162
-126
lines changed

12 files changed

+162
-126
lines changed

cocos2d/core/layers/CCLayerCanvasRenderCmd.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,13 @@
125125
return;
126126
}
127127

128-
var _t = this, node = this._node;
129-
var children = node._children;
128+
var node = this._node, children = node._children;
130129
var len = children.length;
131130
// quick return if not visible
132131
if (!node._visible || len === 0)
133132
return;
134133

135-
_t._syncStatus(parentCmd);
134+
this._syncStatus(parentCmd);
136135
cc.renderer.pushRenderCommand(this);
137136

138137
//the bakeSprite is drawing

extensions/ccui/base-classes/CCProtectedNode.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,6 @@ cc.ProtectedNode = cc.Node.extend(/** @lends cc.ProtectedNode# */{
224224
}
225225
},
226226

227-
/**
228-
* transforms and draws itself, and visit its children and protected children.
229-
* @override
230-
* @function
231-
* @param {CanvasRenderingContext2D|WebGLRenderingContext} ctx context of renderer
232-
*/
233-
visit: function(parentCmd){
234-
this._renderCmd._visit(parentCmd);
235-
},
236-
237227
_changePosition: function(){},
238228

239229
/**

extensions/ccui/base-classes/CCProtectedNodeCanvasRenderCmd.js

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,28 @@
113113
}
114114
}
115115
this._dirtyFlag = this._dirtyFlag & cc.Node._dirtyFlags.opacityDirty ^ this._dirtyFlag;
116+
},
117+
118+
_changeProtectedChild: function (child) {
119+
var cmd = child._renderCmd,
120+
dirty = cmd._dirtyFlag,
121+
flags = cc.Node._dirtyFlags;
122+
123+
if (this._dirtyFlag & flags.colorDirty)
124+
dirty |= flags.colorDirty;
125+
126+
if (this._dirtyFlag & flags.opacityDirty)
127+
dirty |= flags.opacityDirty;
128+
129+
var colorDirty = dirty & flags.colorDirty,
130+
opacityDirty = dirty & flags.opacityDirty;
131+
132+
if (colorDirty)
133+
cmd._updateDisplayColor(this._displayedColor);
134+
if (opacityDirty)
135+
cmd._updateDisplayOpacity(this._displayedOpacity);
136+
if (colorDirty || opacityDirty)
137+
cmd._updateColor();
116138
}
117139
};
118140

@@ -127,10 +149,6 @@
127149
proto.constructor = cc.ProtectedNode.CanvasRenderCmd;
128150

129151
proto.visit = function(parentCmd){
130-
this._node.visit(parentCmd);
131-
};
132-
133-
proto._visit = function(parentCmd){
134152
var node = this._node;
135153
// quick return if not visible
136154
if (!node._visible)
@@ -147,7 +165,6 @@
147165
node.sortAllChildren();
148166
node.sortAllProtectedChildren();
149167

150-
151168
var pChild;
152169
// draw children zOrder < 0
153170
for (i = 0; i < childLen; i++) {
@@ -182,28 +199,6 @@
182199
this._cacheDirty = false;
183200
};
184201

185-
proto._changeProtectedChild = function(child){
186-
var cmd = child._renderCmd,
187-
dirty = cmd._dirtyFlag,
188-
flags = cc.Node._dirtyFlags;
189-
190-
if(this._dirtyFlag & flags.colorDirty)
191-
dirty |= flags.colorDirty;
192-
193-
if(this._dirtyFlag & flags.opacityDirty)
194-
dirty |= flags.opacityDirty;
195-
196-
var colorDirty = dirty & flags.colorDirty,
197-
opacityDirty = dirty & flags.opacityDirty;
198-
199-
if(colorDirty)
200-
cmd._updateDisplayColor(this._displayedColor);
201-
if(opacityDirty)
202-
cmd._updateDisplayOpacity(this._displayedOpacity);
203-
if(colorDirty || opacityDirty)
204-
cmd._updateColor();
205-
};
206-
207202
proto.transform = function(parentCmd, recursive){
208203
var node = this._node;
209204

@@ -224,8 +219,8 @@
224219
worldT.tx = (t.tx * pt.a + t.ty * pt.c + pt.tx + xOffset); //tx
225220
worldT.ty = (t.tx * pt.b + t.ty * pt.d + pt.ty + yOffset); //ty
226221
}else{
227-
worldT.tx = (t.tx * pt.a + t.ty * pt.c + pt.tx); //tx
228-
worldT.ty = (t.tx * pt.b + t.ty * pt.d + pt.ty); //ty
222+
worldT.tx = (t.tx * pt.a + t.ty * pt.c + pt.tx); //tx
223+
worldT.ty = (t.tx * pt.b + t.ty * pt.d + pt.ty); //ty
229224
}
230225
} else {
231226
worldT.a = t.a;

extensions/ccui/base-classes/CCProtectedNodeWebGLRenderCmd.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,13 @@
2525
(function(){
2626
cc.ProtectedNode.WebGLRenderCmd = function (renderable) {
2727
cc.Node.WebGLRenderCmd.call(this, renderable);
28-
this._cachedParent = null;
29-
this._cacheDirty = false;
3028
};
3129

3230
var proto = cc.ProtectedNode.WebGLRenderCmd.prototype = Object.create(cc.Node.WebGLRenderCmd.prototype);
3331
cc.inject(cc.ProtectedNode.RenderCmd, proto);
3432
proto.constructor = cc.ProtectedNode.WebGLRenderCmd;
3533

3634
proto.visit = function(parentCmd){
37-
this._node.visit(parentCmd); //todo refactor late
38-
};
39-
40-
proto._visit = function(parentCmd){
4135
var node = this._node;
4236
// quick return if not visible
4337
if (!node._visible)
@@ -60,7 +54,6 @@
6054
node.sortAllChildren();
6155
node.sortAllProtectedChildren();
6256

63-
6457
var pChild;
6558
// draw children zOrder < 0
6659
for (i = 0; i < childLen; i++) {
@@ -99,28 +92,6 @@
9992
currentStack.top = currentStack.stack.pop();
10093
};
10194

102-
proto._changeProtectedChild = function(child){
103-
var cmd = child._renderCmd,
104-
dirty = cmd._dirtyFlag,
105-
flags = cc.Node._dirtyFlags;
106-
107-
if(this._dirtyFlag & flags.colorDirty)
108-
dirty |= flags.colorDirty;
109-
110-
if(this._dirtyFlag & flags.opacityDirty)
111-
dirty |= flags.opacityDirty;
112-
113-
var colorDirty = dirty & flags.colorDirty,
114-
opacityDirty = dirty & flags.opacityDirty;
115-
116-
if(colorDirty)
117-
cmd._updateDisplayColor(this._displayedColor);
118-
if(opacityDirty)
119-
cmd._updateDisplayOpacity(this._displayedOpacity);
120-
if(colorDirty || opacityDirty)
121-
cmd._updateColor();
122-
};
123-
12495
proto.transform = function(parentCmd, recursive){
12596
var node = this._node;
12697
var t4x4 = this._transform4x4, stackMatrix = this._stackMatrix,

extensions/ccui/base-classes/UIWidget.js

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,12 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
156156
getOrCreateLayoutComponent: function(){
157157
var layoutComponent = this.getComponent(ccui.__LAYOUT_COMPONENT_NAME);
158158
if (null == layoutComponent){
159-
var component = new ccui.LayoutComponent();
159+
layoutComponent = new ccui.LayoutComponent();
160160
this.addComponent(component);
161-
layoutComponent = component;
162161
}
163162
return layoutComponent;
164163
},
165164

166-
/**
167-
* Calls _adaptRenderers(its subClass will override it) before calls its parent's visit.
168-
* @param {CanvasRenderingContext2D|WebGLRenderingContext} ctx
169-
* @override
170-
*/
171-
visit: function (ctx) {
172-
if (this._visible) {
173-
this._adaptRenderers();
174-
cc.ProtectedNode.prototype.visit.call(this, ctx);
175-
}
176-
},
177-
178165
/**
179166
* The direct parent when it's a widget also, otherwise equals null
180167
* @returns {ccui.Widget|null}
@@ -273,8 +260,7 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
273260
/**
274261
* initializes renderer of widget.
275262
*/
276-
_initRenderer: function () {
277-
},
263+
_initRenderer: function () {},
278264

279265
/**
280266
* Sets _customSize of ccui.Widget, if ignoreSize is true, the content size is its renderer's contentSize, otherwise the content size is parameter.
@@ -690,8 +676,7 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
690676
* @note it doesn't implemented on Web
691677
* @param {Boolean} enable set true to enable dpad focus navigation, otherwise disable dpad focus navigation
692678
*/
693-
enableDpadNavigation: function(enable){
694-
},
679+
enableDpadNavigation: function(enable){},
695680

696681
/**
697682
* <p>
@@ -785,14 +770,11 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
785770
}
786771
},
787772

788-
_onPressStateChangedToNormal: function () {
789-
},
773+
_onPressStateChangedToNormal: function () {},
790774

791-
_onPressStateChangedToPressed: function () {
792-
},
775+
_onPressStateChangedToPressed: function () {},
793776

794-
_onPressStateChangedToDisabled: function () {
795-
},
777+
_onPressStateChangedToDisabled: function () {},
796778

797779
_updateChildrenDisplayedRGBA: function(){
798780
this.setColor(this.getColor());
@@ -802,8 +784,7 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
802784
/**
803785
* A call back function when widget lost of focus.
804786
*/
805-
didNotSelectSelf: function () {
806-
},
787+
didNotSelectSelf: function () {},
807788

808789
/**
809790
* <p>
@@ -848,8 +829,7 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
848829

849830
propagateTouchEvent: function(event, sender, touch){
850831
var widgetParent = this.getWidgetParent();
851-
if (widgetParent)
852-
{
832+
if (widgetParent){
853833
widgetParent.interceptTouchEvent(event, sender, touch);
854834
}
855835
},
@@ -1192,14 +1172,11 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
11921172
return this._flippedY;
11931173
},
11941174

1195-
_updateFlippedX: function () {
1196-
},
1175+
_updateFlippedX: function () {},
11971176

1198-
_updateFlippedY: function () {
1199-
},
1177+
_updateFlippedY: function () {},
12001178

1201-
_adaptRenderers: function(){
1202-
},
1179+
_adaptRenderers: function(){},
12031180

12041181
/**
12051182
* Determines if the widget is bright
@@ -1334,8 +1311,7 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
13341311
}
13351312
},
13361313

1337-
_copySpecialProperties: function (model) {
1338-
},
1314+
_copySpecialProperties: function (model) {},
13391315

13401316
_copyProperties: function (widget) {
13411317
this.setEnabled(widget.isEnabled());
@@ -1558,11 +1534,11 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
15581534
* @param {Boolean} [cleanup]
15591535
*/
15601536
removeNodeByTag: function (tag, cleanup) {
1561-
var node = this.getNodeByTag(tag);
1537+
var node = this.getChildByTag(tag);
15621538
if (!node)
15631539
cc.log("cocos2d: removeNodeByTag(tag = %d): child not found!", tag);
15641540
else
1565-
this.removeNode(node);
1541+
this.removeChild(node, cleanup);
15661542
},
15671543

15681544
/**
@@ -1595,6 +1571,13 @@ ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
15951571

15961572
setUnifySizeEnabled: function(enable){
15971573
this._unifySize = enable;
1574+
},
1575+
1576+
_createRenderCmd: function(){
1577+
if(cc._renderType === cc._RENDER_TYPE_WEBGL)
1578+
return new ccui.Widget.WebGLRenderCmd(this);
1579+
else
1580+
return new ccui.Widget.CanvasRenderCmd(this);
15981581
}
15991582
});
16001583

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/****************************************************************************
2+
Copyright (c) 2013-2014 Chukong Technologies Inc.
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+
if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
26+
(function () {
27+
ccui.Widget.CanvasRenderCmd = function (renderable) {
28+
cc.ProtectedNode.CanvasRenderCmd.call(this, renderable);
29+
this._needDraw = false;
30+
};
31+
32+
var proto = ccui.Widget.CanvasRenderCmd.prototype = Object.create(cc.ProtectedNode.CanvasRenderCmd.prototype);
33+
proto.constructor = ccui.Widget.CanvasRenderCmd;
34+
35+
proto.visit = function (parentCmd) {
36+
var node = this._node;
37+
if (node._visible) {
38+
node._adaptRenderers();
39+
cc.ProtectedNode.CanvasRenderCmd.prototype.visit.call(this, parentCmd);
40+
}
41+
};
42+
})();
43+
} else {
44+
(function () {
45+
ccui.Widget.WebGLRenderCmd = function (renderable) {
46+
cc.ProtectedNode.WebGLRenderCmd.call(this, renderable);
47+
this._needDraw = false;
48+
};
49+
50+
var proto = ccui.Widget.WebGLRenderCmd.prototype = Object.create(cc.ProtectedNode.WebGLRenderCmd.prototype);
51+
proto.constructor = ccui.Widget.WebGLRenderCmd;
52+
53+
proto.visit = function (parentCmd) {
54+
var node = this._node;
55+
if (node._visible) {
56+
node._adaptRenderers();
57+
cc.ProtectedNode.WebGLRenderCmd.prototype.visit.call(this, parentCmd);
58+
}
59+
};
60+
})();
61+
}
62+

0 commit comments

Comments
 (0)