void ErrorUI::activate(std::string msg) { int errors = 0; auto parent = m_widget->getParent(); parent->enumerateChildren("error_ui", [&errors](cocos2d::Node *node) // can be done without previous line if correct string passed here { ++errors; return false; }); m_number->setString(WkCocos::ToolBox::itoa(errors)); m_label->setString(msg); m_widget->setContentSize(cocos2d::Size(m_label->getContentSize().width + 40, m_label->getContentSize().height + 120)); cocos2d::Size widgetSize = m_widget->getContentSize(); cocos2d::Size visibleSize = cocos2d::Director::getInstance()->getVisibleSize(); m_number->setPositionX(-widgetSize.width / 2 + 20); m_number->setPositionY(widgetSize.height / 2 - 24); m_skipButton->setPositionY(-(m_label->getContentSize().height + m_skipButton->getContentSize().height) / 2); m_refreshButton->setPositionY(-(m_label->getContentSize().height + m_refreshButton->getContentSize().height) / 2); auto fadeBack = cocos2d::DrawNode::create(); auto errorBack = cocos2d::DrawNode::create(); fadeBack->drawTriangle(cocos2d::Vec2(-visibleSize.width / 2, -visibleSize.height / 2), cocos2d::Vec2(visibleSize.width / 2, -visibleSize.height / 2), cocos2d::Vec2(visibleSize.width / 2, visibleSize.height / 2), cocos2d::Color4F(0, 0, 0, 0.6f)); fadeBack->drawTriangle(cocos2d::Vec2(-visibleSize.width / 2, -visibleSize.height / 2), cocos2d::Vec2(visibleSize.width / 2, visibleSize.height / 2), cocos2d::Vec2(-visibleSize.width / 2, visibleSize.height / 2), cocos2d::Color4F(0, 0, 0, 0.6f)); fadeBack->setPositionY(visibleSize.height / 2 - m_widget->getPositionY()); errorBack->drawTriangle(cocos2d::Vec2(-widgetSize.width / 2, -widgetSize.height / 2), cocos2d::Vec2(widgetSize.width / 2, -widgetSize.height / 2), cocos2d::Vec2(widgetSize.width / 2, widgetSize.height / 2), cocos2d::Color4F(0.3f, 0.3f, 0.3f, 1)); errorBack->drawTriangle(cocos2d::Vec2(-widgetSize.width / 2, -widgetSize.height / 2), cocos2d::Vec2(widgetSize.width / 2, widgetSize.height / 2), cocos2d::Vec2(-widgetSize.width / 2, widgetSize.height / 2), cocos2d::Color4F(0.3f, 0.3f, 0.3f, 1)); m_widget->addChild(fadeBack, 1); m_widget->addChild(errorBack, 1); m_widget->setVisible(true); m_widget->setEnabled(true); }
void ClippingToRenderTextureTest::setup() { auto button = MenuItemFont::create("Reproduce bug", [&](Ref *sender) { std::vector<Node*> nodes; enumerateChildren("remove me [0-9]", [&](Node *node) { nodes.push_back(node); return false; }); for (auto node : nodes) { this->removeChild(node); } this->reproduceBug(); }); auto s = Director::getInstance()->getWinSize(); // create menu, it's an autorelease object auto menu = Menu::create(button, nullptr); menu->setPosition(Point(s.width/2, s.height/2)); this->addChild(menu, 1); expectedBehaviour(); }
void NodeNameTest::test(float dt) { auto parent = Node::create(); // setName(), getName() and getChildByName() char name[20]; for (int i = 0; i < 10; ++i) { sprintf(name, "node%d", i); auto node = Node::create(); node->setName(name); parent->addChild(node); } for (int i = 0; i < 10; ++i) { sprintf(name, "node%d", i); auto node = parent->getChildByName(name); log("find child: %s", node->getName().c_str()); } // enumerateChildren() // name = regular expression int i = 0; parent = Node::create(); for (int i = 0; i < 100; ++i) { auto node = Node::create(); sprintf(name, "node%d", i); node->setName(name); parent->addChild(node); } i = 0; parent->enumerateChildren("node[[:digit:]]+", [&i](Node* node) -> bool { ++i; return false; }); CCAssert(i == 100, ""); i = 0; parent->enumerateChildren("node[[:digit:]]+", [&i](Node* node) -> bool { ++i; return true; }); CCAssert(i == 1, ""); // enumerateChildren // name = node[[digit]]+/node parent = Node::create(); for (int i = 0; i < 100; ++i) { auto node = Node::create(); sprintf(name, "node%d", i); node->setName(name); parent->addChild(node); for (int j = 0; j < 100; ++j) { auto child = Node::create(); child->setName("node"); node->addChild(child); } } i = 0; parent->enumerateChildren("node1/node", [&i](Node* node) -> bool { ++i; return false; }); CCAssert(i == 100, ""); i = 0; parent->enumerateChildren("node1/node", [&i](Node* node) -> bool { ++i; return true; }); CCAssert(i == 1, ""); // search from root parent = Node::create(); for (int i = 0; i < 100; ++i) { auto node = Node::create(); sprintf(name, "node%d", i); node->setName(name); parent->addChild(node); for (int j = 0; j < 100; ++j) { auto child = Node::create(); child->setName("node"); node->addChild(child); } } i = 0; parent->enumerateChildren("node[[:digit:]]+/node", [&i](Node* node) -> bool { ++i; return false; }); CCAssert(i == 10000, ""); i = 0; parent->enumerateChildren("node[[:digit:]]+/node", [&i](Node* node) -> bool { ++i; return true; }); CCAssert(i == 1, ""); // search from parent // name is xxx/.. i = 0; parent->enumerateChildren("node/..", [&i](Node* node) -> bool { ++i; return true; }); CCAssert(i == 1, ""); i = 0; parent->enumerateChildren("node/..", [&i](Node* node) -> bool { ++i; return false; }); CCAssert(i == 10000, ""); // name = //xxx : search recursively parent = Node::create(); for (int j = 0; j < 100; j++) { auto node = Node::create(); sprintf(name, "node%d", j); node->setName(name); parent->addChild(node); for (int k = 0; k < 100; ++k) { auto child = Node::create(); sprintf(name, "node%d", k); child->setName(name); node->addChild(child); } } i = 0; parent->enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool { ++i; return false; }); CCAssert(i == 10100, ""); // 10000(children) + 100(parent) i = 0; parent->enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool { ++i; return true; }); CCAssert(i == 1, ""); i = 0; parent->enumerateChildren("//node[[:digit:]]+/..", [&i](Node* node) -> bool { ++i; return false; }); 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, ""); }