CustomLayerView.cpp
3.66 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// CustomLayerView.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 19.05.2017.
//
//
#include <stdio.h>
#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<void(std::string widgetName, cocos2d::ui::Widget::TouchEventType touchEventType)> 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;
}