Node::enumerateChildren just searches from itself

This commit is contained in:
minggo 2014-07-10 11:07:39 +08:00
parent ddb72228d3
commit 316564d58a
3 changed files with 20 additions and 60 deletions

View File

@ -842,24 +842,14 @@ void Node::enumerateChildren(const std::string &name, std::function<bool (Node *
size_t subStrStartPos = 0; // sub string start index
size_t subStrlength = length; // sub string length
// Starts with '/' or '//'?
bool searchFromRoot = false;
bool searchFromRootRecursive = false;
if (name[0] == '/')
// Starts with '//'?
bool searchRecursively = false;
if (length > 2 && name[0] == '/' && name[1] == '/')
{
if (length > 2 && name[1] == '/')
{
searchFromRootRecursive = true;
searchRecursively = true;
subStrStartPos = 2;
subStrlength -= 2;
}
else
{
searchFromRoot = true;
subStrStartPos = 1;
subStrlength -= 1;
}
}
// End with '/..'?
bool searchFromParent = false;
@ -872,7 +862,7 @@ void Node::enumerateChildren(const std::string &name, std::function<bool (Node *
subStrlength -= 3;
}
// Remove '/', '//', '/..' if exist
// Remove '//', '/..' if exist
std::string newName = name.substr(subStrStartPos, subStrlength);
if (searchFromParent)
@ -880,23 +870,11 @@ void Node::enumerateChildren(const std::string &name, std::function<bool (Node *
newName.insert(0, "[[:alnum:]]+/");
}
if (searchFromRoot)
{
// name is '/xxx'
auto root = getScene();
if (root)
{
root->doEnumerate(newName, callback);
}
}
else if (searchFromRootRecursive)
if (searchRecursively)
{
// name is '//xxx'
auto root = getScene();
if (root)
{
doEnumerateRecursive(root, newName, callback);
}
doEnumerateRecursive(this, newName, callback);
}
else
{

View File

@ -714,23 +714,19 @@ public:
virtual Node* getChildByName(const std::string& name) const;
/** Search the children of the receiving node to perform processing for nodes which share a name.
*
* @param name The name to search for, supports c++11 regular expression
* @param name The name to search for, supports c++11 regular expression.
* Search syntax options:
* `/` : When placed at the start of the search string, this indicates that the search should be performed on the tree's node.
* `//`: Can only be placed at the begin of the search string. This indicates that the search should be performed on the tree's node
* and be performed recursively across the entire node tree.
* `//`: Can only be placed at the begin of the search string. This indicates that it will search recursively.
* `..`: The search should move up to the node's parent. Can only be placed at the end of string
* `/` : When placed anywhere but the start of the search string, this indicates that the search should move to the node's children
* `/` : When placed anywhere but the start of the search string, this indicates that the search should move to the node's children.
*
* @code
* enumerateChildren("/MyName", ...): This searches the root's children and matches any node with the name `MyName`.
* enumerateChildren("//MyName", ...): This searches the root's children recursively and matches any node with the name `MyName`.
* enumerateChildren("//MyName", ...): This searches the children recursively and matches any node with the name `MyName`.
* enumerateChildren("[[:alnum:]]+", ...): This search string matches every node of its children.
* enumerateChildren("/MyName", ...): This searches the node tree and matches the parent node of every node named `MyName`.
* enumerateChildren("A[[:digit:]]", ...): This searches the node's children and returns any child named `A0`, `A1`, ..., `A9`
* enumerateChildren("Abby/Normal", ...): This searches the node's grandchildren and returns any node whose name is `Normal`
* and whose parent is named `Abby`.
* enumerateChildren("//Abby/Normal", ...): This searches the node tree and returns any node whose name is `Normal` and whose
* enumerateChildren("//Abby/Normal", ...): This searches recursively and returns any node whose name is `Normal` and whose
* parent is named `Abby`.
* @endcode
*

View File

@ -1379,8 +1379,8 @@ void NodeNameTest::test(float dt)
});
CCAssert(i == 10000, "");
// name = /xxx : search from root
parent = getScene();
// name = //xxx : search recursively
parent = Node::create();
for (int j = 0; j < 100; j++)
{
auto node = Node::create();
@ -1398,35 +1398,21 @@ void NodeNameTest::test(float dt)
}
i = 0;
enumerateChildren("/node[[:digit:]]+", [&i](Node* node) -> bool {
++i;
return false;
});
CCAssert(i == 100, "");
i = 0;
enumerateChildren("/node[[:digit:]]+", [&i](Node* node) -> bool {
++i;
return true;
});
CCAssert(i == 1, "");
i = 0;
enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool {
parent->enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool {
++i;
return false;
});
CCAssert(i == 10100, ""); // 10000(children) + 100(parent)
i = 0;
enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool {
parent->enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool {
++i;
return true;
});
CCAssert(i == 1, "");
i = 0;
enumerateChildren("//node[[:digit:]]+/..", [&i](Node* node) -> bool {
parent->enumerateChildren("//node[[:digit:]]+/..", [&i](Node* node) -> bool {
++i;
return false;
});