// // CustomLayerView.cpp // SteveMaggieCpp // // Created by Katarzyna Kalinowska-Górska on 19.05.2017. // // #include #include "CustomLayerView.h" #include "LayoutParser.h" CustomLayerView* CustomLayerView::create(std::string layoutFilePath, std::string scenarioFilePath) { CustomLayerView * view = new (std::nothrow) CustomLayerView(); if(view && view->init(layoutFilePath, scenarioFilePath)) { view->autorelease(); return view; } CC_SAFE_DELETE(view); return nullptr; } bool CustomLayerView::init(std::string layoutFilePath, std::string scenarioFilePath) { if(!Layer::init()){ return false; } _layoutFilePath = layoutFilePath; _layoutLoaded = false; _loadFromAssets = false; _widgetTouchEventCallback = [](std::string, cocos2d::ui::Widget::TouchEventType){}; if(scenarioFilePath != ""){ auto fileUtils = cocos2d::FileUtils::getInstance(); if (fileUtils->isFileExist(scenarioFilePath)) { std::string jsonString = fileUtils->getStringFromFile(scenarioFilePath); rapidjson::Document doc; doc.Parse(jsonString.c_str()); _actionSequenceHandler = new (std::nothrow) ActionSequenceHandler(this, doc); } } return true; } CustomLayerView::~CustomLayerView() { if(_actionSequenceHandler){ delete _actionSequenceHandler; } } void CustomLayerView::onEnter() { cocos2d::Layer::onEnter(); this->loadLayout(false); if(_actionSequenceHandler){ _actionSequenceHandler->runNext(); } } void CustomLayerView::onExit() { cocos2d::Layer::onExit(); } void CustomLayerView::loadLayout(bool forceLoad) { if(!_layoutLoaded || forceLoad){ auto& layoutParser = LayoutParser::getInstance(); if (_loadFromAssets && cocos2d::FileUtils::getInstance()->isFileExist(this->_layoutFilePath)) { layoutParser.loadLayoutFromJSONFile(this->_layoutFilePath, this); } else if(!_loadFromAssets){ layoutParser.loadLayoutFromDownloadedJSONFile(this->_layoutFilePath, this); } _layoutLoaded = true; this->layoutLoaded(); } } void CustomLayerView::layoutLoaded() { // to override in subclasses if needed } bool CustomLayerView::touchHandlerForWidget(std::string objectName, cocos2d::ui::Widget::TouchEventType touchEventType) { _widgetTouchEventCallback(objectName, touchEventType); // auto object = _scenarioObjects[objectName]; // if(objectName == "backButton" && touchEventType == ccui.Widget.TOUCH_ENDED){ // // log("gugugug"); // } //todo add animation to dismiss dialog // todo make a parser outside return false; } void CustomLayerView::resetActionSequence() { if(_actionSequenceHandler){ _actionSequenceHandler->reset(); } } void CustomLayerView::setWidgetTouchEventCallback(std::function widgetTouchEventCallback) { _widgetTouchEventCallback = widgetTouchEventCallback; } // layout parse delegate void CustomLayerView::addLayer(cocos2d::Layer* layer) { _layers.push_back(layer); } void CustomLayerView::addObject(std::string objectName, cocos2d::Node* object) { _objects.insert({objectName, object}); } void CustomLayerView::addScenarioObject(std::string objectName, ScenarioObject* object) { _scenarioObjects.insert({objectName, object}); } ScenarioObject* CustomLayerView::getScenarioObjectByName(std::string objectName) { if(_scenarioObjects.find(objectName) != _scenarioObjects.end()){ return _scenarioObjects[objectName]; } return nullptr; }