// // ToyWorksheetScene.cpp // SteveMaggieCpp // // Created by Katarzyna Kalinowska-Górska on 18.05.2017. // // #include #include "ToyWorksheetScene.h" ToyWorksheetScene* ToyWorksheetScene::create(std::string layoutFilePath, std::string scenarioFilePath) { ToyWorksheetScene * scene = new (std::nothrow) ToyWorksheetScene(); if(scene && scene->initWithConfigurationFiles(layoutFilePath, scenarioFilePath)) { scene->autorelease(); return scene; } CC_SAFE_DELETE(scene); return nullptr; } bool ToyWorksheetScene::initWithConfigurationFiles(std::string layoutFilePath, std::string scenarioFilePath) { if (ToyParentScene::initWithConfigurationFiles(layoutFilePath, scenarioFilePath)) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) androidBackButtonDisabled = false; #endif return true; } else { return false; } } void ToyWorksheetScene::onEnter() { ToyParentScene::onEnter(); this->loadLayout(false); this->loadScenario(_alwaysSkipDemo); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) auto keyboardListener = cocos2d::EventListenerKeyboard::create(); keyboardListener->onKeyReleased = [&](cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event){ if(keyCode == cocos2d::EventKeyboard::KeyCode::KEY_BACK) { if (!androidBackButtonDisabled){ //when we have system gesture navigation, we want to disable back button because in most worksheets we have drag and drop. it's not perfefct, but seems the only way for now. ToyParentScene::touchHandlerForWidget("backButton", cocos2d::ui::Widget::TouchEventType::ENDED); } } }; _eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, this); #endif } void ToyWorksheetScene::onEnterTransitionDidFinish() { ToyParentScene::onEnterTransitionDidFinish(); _scenarioHandler->runScenario(); } bool ToyWorksheetScene::touchHandlerForWidget(std::string objectName, cocos2d::ui::Widget::TouchEventType touchEventType) { if(objectName == "backButton" && touchEventType == cocos2d::ui::Widget::TouchEventType::ENDED){ if(this->onBackButtonClicked()){ return true; } } if(ToyParentScene::touchHandlerForWidget(objectName,touchEventType)){ return true; } if(objectName == "replayButton" && touchEventType == cocos2d::ui::Widget::TouchEventType::ENDED){ return this->onReplayButtonClicked(); } else if(objectName == "fastForwardButton" && touchEventType == cocos2d::ui::Widget::TouchEventType::ENDED){ return this->onFastForwardButtonClicked(); } return false; } bool ToyWorksheetScene::onBackButtonClicked() { return false; } bool ToyWorksheetScene::onReplayButtonClicked() { if(_scenarioHandler->getDemoState() == ToyScenarioHandler::DEMO_STATE_FINISHED || _scenarioHandler->getDemoState() == ToyScenarioHandler::DEMO_STATE_NEVER_PLAYED){ _scenarioHandler->resetScenario(true); } else { _scenarioHandler->resetScenario(false); } return true; } bool ToyWorksheetScene::onFastForwardButtonClicked() { //debug jump to the last action // this.scenarioHandler.jumpToLastAction(); _scenarioHandler->resetScenario(true, !_scenarioHandler->shouldSkipLayoutReloadOnFastForward()); return true; } //ToyScenarioObject void ToyWorksheetScene::callFunctionByName(std::string methodName, const rapidjson::Value* arguments, ActionParseDelegate* parseDelegate, std::function callback) { ToyParentScene::callFunctionByName(methodName, arguments, parseDelegate, callback); if(methodName == "onBackButtonClicked"){ this->onBackButtonClicked(); } }