Merge pull request #7371 from minggo/enumerateChildren-fix

Node::enumerateChildren  just searches from itself
This commit is contained in:
minggo 2014-07-10 15:46:04 +08:00
commit 831b0b332b
3 changed files with 20 additions and 60 deletions

View File

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

View File

@ -714,23 +714,19 @@ public:
virtual Node* getChildByName(const std::string& name) const; virtual Node* getChildByName(const std::string& name) const;
/** Search the children of the receiving node to perform processing for nodes which share a name. /** 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: * 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 it will search recursively.
* `//`: 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.
* `..`: The search should move up to the node's parent. Can only be placed at the end of string * `..`: 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 * @code
* enumerateChildren("/MyName", ...): This searches the root's children and matches any node with the name `MyName`. * enumerateChildren("//MyName", ...): This searches the children recursively 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("[[:alnum:]]+", ...): This search string matches every node of its children. * 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("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` * enumerateChildren("Abby/Normal", ...): This searches the node's grandchildren and returns any node whose name is `Normal`
* and whose parent is named `Abby`. * 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`. * parent is named `Abby`.
* @endcode * @endcode
* *

View File

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