Skip to content

Instantly share code, notes, and snippets.

@kyokomi
Last active August 29, 2015 14:02
Show Gist options
  • Save kyokomi/2d2c99787868763a7853 to your computer and use it in GitHub Desktop.
Save kyokomi/2d2c99787868763a7853 to your computer and use it in GitHub Desktop.
CocoStudioのUIEditorで作ったLayoutから特定のWidgetを再帰的に取得します
/**
@file WidgetUtil.h
@author kyokomi
@date 2014/06/21
*/
#ifndef __Kyokomi__WidgetUtil__
#define __Kyokomi__WidgetUtil__
#include "cocos2d.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
class WidgetUtil
{
public:
static cocos2d::ui::Widget* getChildByNameRecursion(cocos2d::ui::Widget* rootWidget, std::string name)
{
for (auto& child : rootWidget->getChildren())
{
if (child)
{
cocos2d::ui::Widget* widgetChild = dynamic_cast<cocos2d::ui::Widget*>(child);
if (widgetChild)
{
if (widgetChild->getName() == name)
{
return widgetChild;
}
else
{
auto widgetGrandChild = getChildByNameRecursion(widgetChild, name);
if (widgetGrandChild)
{
return widgetGrandChild;
}
}
}
}
}
return nullptr;
}
};
#endif /* defined(__Kyokomi__WidgetUtil__) */
@kyokomi
Copy link
Author

kyokomi commented Jun 21, 2014

使い方

xxxxxx.jsonは、各自UIEditorで作成してExportしたもの。

auto layout = GUIReader::getInstance()->widgetFromJsonFile("xxxxxx.json");
auto button = dynamic_cast<Button*>(WidgetUtil::getChildByNameRecursion(layout, "xxxxx_button"));
button->addTouchEventListener([](Ref *ref, Widget::TouchEventType type) {
    if (type == Widget::TouchEventType::ENDED) {
        // 処理
        CCLOG("xxxxx_button touch end");
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment