ToyWorksheetScene.cpp
3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// ToyWorksheetScene.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 18.05.2017.
//
//
#include <stdio.h>
#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<void()> callback)
{
ToyParentScene::callFunctionByName(methodName, arguments, parseDelegate, callback);
if(methodName == "onBackButtonClicked"){
this->onBackButtonClicked();
}
}