mirror of https://github.com/axmolengine/axmol.git
add utils::findChildren()
This commit is contained in:
parent
eb67d9deff
commit
2917596d5e
|
@ -117,6 +117,19 @@ void captureScreen(const std::function<void(bool, const std::string&)>& afterCap
|
||||||
captureScreenCommand.func = std::bind(onCaptureScreen, afterCaptured, filename);
|
captureScreenCommand.func = std::bind(onCaptureScreen, afterCaptured, filename);
|
||||||
Director::getInstance()->getRenderer()->addCommand(&captureScreenCommand);
|
Director::getInstance()->getRenderer()->addCommand(&captureScreenCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<Node*> findChildren(const Node &node, const std::string &name)
|
||||||
|
{
|
||||||
|
std::vector<Node*> vec;
|
||||||
|
|
||||||
|
node.enumerateChildren(name, [&vec](Node* nodeFound) -> bool {
|
||||||
|
vec.push_back(nodeFound);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
return vec;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
|
@ -24,6 +24,10 @@ THE SOFTWARE.
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
#ifndef __SUPPORT_CC_UTILS_H__
|
#ifndef __SUPPORT_CC_UTILS_H__
|
||||||
#define __SUPPORT_CC_UTILS_H__
|
#define __SUPPORT_CC_UTILS_H__
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include "2d/CCNode.h"
|
||||||
#include "base/ccMacros.h"
|
#include "base/ccMacros.h"
|
||||||
|
|
||||||
/** @file ccUtils.h
|
/** @file ccUtils.h
|
||||||
|
@ -58,6 +62,8 @@ namespace utils
|
||||||
* @since v3.2
|
* @since v3.2
|
||||||
*/
|
*/
|
||||||
void captureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename);
|
void captureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename);
|
||||||
|
|
||||||
|
std::vector<Node*> findChildren(const Node &node, const std::string &name);
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
|
@ -1266,23 +1266,9 @@ void NodeNameTest::onEnter()
|
||||||
log("find child: %s", node->getName().c_str());
|
log("find child: %s", node->getName().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// enumerateChildren()
|
|
||||||
int i = 0;
|
|
||||||
parent->enumerateChildren("test", [&i](Node* node) -> bool {
|
|
||||||
++i;
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
CCAssert(i == 1, "");
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
parent->enumerateChildren("test", [&i](Node* node) -> bool {
|
|
||||||
++i;
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
CCAssert(i == 2, "");
|
|
||||||
|
|
||||||
// enumerateChildren()
|
// enumerateChildren()
|
||||||
// name = regular expression
|
// name = regular expression
|
||||||
|
int i = 0;
|
||||||
parent = Node::create();
|
parent = Node::create();
|
||||||
for (int i = 0; i < 100; ++i)
|
for (int i = 0; i < 100; ++i)
|
||||||
{
|
{
|
||||||
|
@ -1422,6 +1408,19 @@ void NodeNameTest::onEnter()
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
CCAssert(i == 10000, "");
|
CCAssert(i == 10000, "");
|
||||||
|
|
||||||
|
// utils::findChildren()
|
||||||
|
|
||||||
|
parent = Node::create();
|
||||||
|
for (int i = 0; i < 50; ++i)
|
||||||
|
{
|
||||||
|
auto child = Node::create();
|
||||||
|
child->setName("node");
|
||||||
|
parent->addChild(child);
|
||||||
|
}
|
||||||
|
auto findChildren = utils::findChildren(*parent, "node");
|
||||||
|
CCAssert(findChildren.size() == 50, "");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
|
|
Loading…
Reference in New Issue