Skip to content

Commit 717d516

Browse files
committed
issue #4174 refactor addNode and addChild for widget
1 parent be6bacd commit 717d516

File tree

3 files changed

+122
-48
lines changed

3 files changed

+122
-48
lines changed

extensions/CocoStudio/GUI/BaseClasses/UIWidget.js

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,6 @@ ccs.Widget = ccs.NodeRGBA.extend(/** @lends ccs.Widget# */{
181181
}
182182
},
183183

184-
/**
185-
* Adds a child to the container.
186-
* @param {ccs.Widget} child
187-
*/
188-
addChild: function (child, zOrder, tag) {
189-
if(!(child instanceof ccs.Widget)){
190-
cc.log("Widget only supports Widgets as children");
191-
return;
192-
}
193-
cc.NodeRGBA.prototype.addChild.call(this, child, zOrder, tag);
194-
this._widgetChildren.push(child);
195-
},
196-
197184
sortAllChildren: function () {
198185
this._reorderWidgetChildDirty = this._reorderChildDirty;
199186
cc.NodeRGBA.prototype.sortAllChildren.call(this);
@@ -223,6 +210,41 @@ ccs.Widget = ccs.NodeRGBA.extend(/** @lends ccs.Widget# */{
223210
}
224211
},
225212

213+
/**
214+
* Adds a child to the container.
215+
* @param {ccs.Widget} widget
216+
* @param {Number} zOrder
217+
* @param {Number} tag
218+
*/
219+
addChild: function (widget, zOrder, tag) {
220+
if(widget instanceof ccs.Widget){
221+
cc.NodeRGBA.prototype.addChild.call(this, widget, zOrder, tag);
222+
this._widgetChildren.push(widget);
223+
return;
224+
}
225+
if(widget instanceof cc.Node){
226+
cc.log("Please use addNode to add a CCNode.");
227+
return;
228+
}
229+
},
230+
231+
/**
232+
*
233+
* @param tag
234+
* @returns {ccs.Widget}
235+
*/
236+
getChildByTag:function(tag){
237+
var __children = this._widgetChildren;
238+
if (__children != null) {
239+
for (var i = 0; i < __children.length; i++) {
240+
var node = __children[i];
241+
if (node && node._tag == tag)
242+
return node;
243+
}
244+
}
245+
return null;
246+
},
247+
226248
/**
227249
* Return an array of children
228250
* @returns {Array}
@@ -236,7 +258,7 @@ ccs.Widget = ccs.NodeRGBA.extend(/** @lends ccs.Widget# */{
236258
* @returns {Number}
237259
*/
238260
getChildrenCount: function () {
239-
return this._widgetChildren ? this._widgetChildren.length : 0;
261+
return this._widgetChildren.length;
240262
},
241263

242264
getWidgetParent: function () {
@@ -247,22 +269,18 @@ ccs.Widget = ccs.NodeRGBA.extend(/** @lends ccs.Widget# */{
247269
return null;
248270
},
249271

250-
removeFromParent: function (cleanup) {
251-
cc.NodeRGBA.prototype.removeFromParent.call(this, cleanup);
252-
},
253-
254-
removeFromParentAndCleanup: function (cleanup) {
255-
cc.NodeRGBA.prototype.removeFromParent.call(this, cleanup);
256-
},
257-
258272
/**
259273
* remove child
260-
* @param {ccs.Widget} child
274+
* @param {ccs.Widget} widget
261275
* @param {Boolean} cleanup
262276
*/
263-
removeChild: function (child, cleanup) {
264-
cc.NodeRGBA.prototype.removeChild.call(this, child, cleanup);
265-
cc.ArrayRemoveObject(this._widgetChildren, child);
277+
removeChild: function (widget, cleanup) {
278+
if(!(widget instanceof ccs.Widget)){
279+
cc.log("child must a type of ccs.Widget");
280+
return;
281+
}
282+
cc.NodeRGBA.prototype.removeChild.call(this, widget, cleanup);
283+
cc.ArrayRemoveObject(this._widgetChildren, widget);
266284
},
267285

268286
removeChildByTag: function (tag, cleanup) {
@@ -280,12 +298,11 @@ ccs.Widget = ccs.NodeRGBA.extend(/** @lends ccs.Widget# */{
280298
* Removes all children from the container, and do a cleanup to all running actions depending on the cleanup parameter.
281299
*/
282300
removeAllChildren: function (cleanup) {
283-
var childrenLength = this._widgetChildren.length;
284-
if (childrenLength <= 0) {
285-
return
301+
for (var i = 0; i < this._widgetChildren.length; i++) {
302+
var widget = this._widgetChildren[i];
303+
cc.NodeRGBA.prototype.removeChild.call(this, widget, cleanup);
286304
}
287-
cc.NodeRGBA.prototype.removeAllChildren.call(this, cleanup);
288-
this._widgetChildren = [];
305+
this._widgetChildren.length = 0;
289306
},
290307

291308
/**
@@ -332,7 +349,7 @@ ccs.Widget = ccs.NodeRGBA.extend(/** @lends ccs.Widget# */{
332349
*/
333350
addNode: function (node, zOrder, tag) {
334351
if (node instanceof ccs.Widget) {
335-
cc.log("Widget only supports Nodes as renderer");
352+
cc.log("Please use addChild to add a Widget.");
336353
return;
337354
}
338355
cc.NodeRGBA.prototype.addChild.call(this, node, zOrder, tag);
@@ -345,8 +362,9 @@ ccs.Widget = ccs.NodeRGBA.extend(/** @lends ccs.Widget# */{
345362
* @returns {cc.Node}
346363
*/
347364
getNodeByTag: function (tag) {
348-
for (var i = 0; i < this._nodes.length; i++) {
349-
var node = this._nodes[i];
365+
var _nodes = this._nodes;
366+
for (var i = 0; i < _nodes.length; i++) {
367+
var node = _nodes[i];
350368
if (node && node.getTag() == tag) {
351369
return node;
352370
}
@@ -365,17 +383,19 @@ ccs.Widget = ccs.NodeRGBA.extend(/** @lends ccs.Widget# */{
365383
/**
366384
* remove node
367385
* @param {cc.Node} node
386+
* @param {Boolean} cleanup
368387
*/
369-
removeNode: function (node) {
388+
removeNode: function (node, cleanup) {
370389
cc.NodeRGBA.prototype.removeChild.call(this, node);
371390
cc.ArrayRemoveObject(this._nodes, node);
372391
},
373392

374393
/**
375394
* remove node by tag
376-
* @param tag
395+
* @param {Number} tag
396+
* @param {Boolean} cleanup
377397
*/
378-
removeNodeByTag: function (tag) {
398+
removeNodeByTag: function (tag, cleanup) {
379399
var node = this.getNodeByTag(tag);
380400
if (!node) {
381401
cc.log("cocos2d: removeNodeByTag(tag = %d): child not found!", tag);
@@ -393,7 +413,7 @@ ccs.Widget = ccs.NodeRGBA.extend(/** @lends ccs.Widget# */{
393413
var node = this._nodes[i];
394414
cc.NodeRGBA.prototype.removeChild.call(this, node);
395415
}
396-
this._nodes = [];
416+
this._nodes.length = 0;
397417
},
398418

399419
/**

extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,12 @@ ccs.PageView = ccs.Layout.extend(/** @lends ccs.PageView# */{
270270
/**
271271
* remove widget child override
272272
* @param {ccs.Widget} child
273+
* @param {Boolean} cleanup
273274
*/
274-
removeChild: function (child) {
275-
cc.ArrayRemoveObject(this._pages, child);
276-
ccs.Layout.prototype.removeChild.call(this, child);
275+
removeChild: function (child, cleanup) {
276+
if(cleanup)
277+
cc.ArrayRemoveObject(this._pages, child);
278+
ccs.Layout.prototype.removeChild.call(this, child, cleanup);
277279
},
278280

279281
onSizeChanged: function () {
@@ -316,9 +318,10 @@ ccs.PageView = ccs.Layout.extend(/** @lends ccs.PageView# */{
316318
}
317319
},
318320

319-
removeAllChildren: function () {
320-
this._pages = [];
321-
ccs.Layout.prototype.removeAllChildren.call(this);
321+
removeAllChildren: function (cleanup) {
322+
if(cleanup)
323+
this._pages.length = 0;
324+
ccs.Layout.prototype.removeAllChildren.call(this, cleanup);
322325
},
323326

324327
/**

extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.js

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,17 +253,18 @@ ccs.ScrollView = ccs.Layout.extend(/** @lends ccs.ScrollView# */{
253253
return this._innerContainer.addChild(widget, zOrder, tag);
254254
},
255255

256-
removeAllChildren: function () {
257-
this._innerContainer.removeAllChildren();
256+
removeAllChildren: function (cleanup) {
257+
this._innerContainer.removeAllChildren(cleanup);
258258
},
259259

260260
/**
261261
* remove widget child override
262262
* @param {ccs.Widget} child
263+
* @param {Boolean} cleanup
263264
* @returns {boolean}
264265
*/
265-
removeChild: function (child) {
266-
return this._innerContainer.removeChild(child);
266+
removeChild: function (child, cleanup) {
267+
return this._innerContainer.removeChild(child,cleanup);
267268
},
268269

269270
/**
@@ -300,6 +301,56 @@ ccs.ScrollView = ccs.Layout.extend(/** @lends ccs.ScrollView# */{
300301
return this._innerContainer.getChildByName(name);
301302
},
302303

304+
/**
305+
* Add node for scrollView
306+
* @param {cc.Node}node
307+
* @param {Number} zOrder
308+
* @param {Number} tag
309+
*/
310+
addNode: function (node, zOrder, tag) {
311+
this._innerContainer.addNode(node, zOrder, tag);
312+
},
313+
314+
/**
315+
* Get node by tag
316+
* @param {Number} tag
317+
* @returns {cc.Node}
318+
*/
319+
getNodeByTag: function (tag) {
320+
return this._innerContainer.getNodeByTag(tag);
321+
},
322+
323+
/**
324+
* Get all node
325+
* @returns {Array}
326+
*/
327+
getNodes: function () {
328+
return this._innerContainer.getNodes();
329+
},
330+
331+
/**
332+
* Remove a node
333+
* @param {cc.Node} node
334+
*/
335+
removeNode: function (node) {
336+
this._innerContainer.removeNode(node);
337+
},
338+
339+
/**
340+
* Remove a node by tag
341+
* @param {Number} tag
342+
*/
343+
removeNodeByTag: function (tag) {
344+
this._innerContainer.removeNodeByTag(tag);
345+
},
346+
347+
/**
348+
* Remove all node
349+
*/
350+
removeAllNodes: function () {
351+
this._innerContainer.removeAllNodes();
352+
},
353+
303354
moveChildren: function (offsetX, offsetY) {
304355
var pos = this._innerContainer.getPosition();
305356
this._moveChildPoint.x = pos.x + offsetX;

0 commit comments

Comments
 (0)