-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathView.cpp
More file actions
151 lines (131 loc) · 3.72 KB
/
Copy pathView.cpp
File metadata and controls
151 lines (131 loc) · 3.72 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
144
145
146
147
148
149
150
#include "View.h"
#include "cocostudio/CocoStudio.h"
#include "Factory.h"
#include "ViewModel.h"
#include "PluginManager.h"
#include "Env.h"
using namespace cocos2d::plugin;
using namespace cocostudio;
View::View()
: _pRootViewModel(nullptr)
, _pRootNode(nullptr)
{
}
View::~View()
{
if(_pRootViewModel){
_pRootViewModel->release();
_pRootViewModel = nullptr;
}
}
bool View::initWithFactory(const std::string fname, Factory<ViewModel>& factory)
{
if ( !Layer::init() ) {
return false;
}
auto ext = fname.substr(fname.find_last_of("."));
if(ext == ".csb"){
_pRootNode = CSLoader::createNode(fname.c_str());
}else{
_pRootNode = SceneReader::getInstance()->createNodeWithSceneFile(fname.c_str());
}
setName(fname);
_pRootViewModel = factory.create("root");
if(_pRootViewModel == nullptr){
factory.sign("root", new Creator<ViewModel, ViewModel>);
_pRootViewModel = factory.create("root");
}
_pRootViewModel->retain();
_pRootViewModel->setRoot(_pRootViewModel);
_pRootViewModel->setNode(this);
_pRootViewModel->bind(_pRootNode, factory);
_pRootViewModel->init();
addChild(_pRootNode);
// setTouchParticle();
// this->schedule(schedule_selector(View::update));
return true;
}
void View::setTouchParticle()
{
if(_pRootNode->getChildrenCount() < 1){
return;
}
const auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [&](Touch* pTouch, Event* pEvent){
const auto location = pTouch->getLocation();
auto* pParticle = ParticleSystemQuad::create("ui/Particles/Galaxy.plist");
pParticle->setPosition(location);
pParticle->setGlobalZOrder(100.0f);
pParticle->setScale(0.5);
pParticle->setDuration(0.2f);
pParticle->setAutoRemoveOnFinish(true);
addChild(pParticle);
return true;
};
auto pBackground = _pRootNode->getChildren().at(0);
getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, pBackground);
}
void View::onExit()
{
Layer::onExit();
if(_pRootViewModel != nullptr){
_pRootViewModel->onExit();
}
}
void View::onEnter()
{
Layer::onEnter();
if(_pRootViewModel != nullptr){
_pRootViewModel->onEnter();
}
}
void View::onEnterTransitionDidFinish()
{
Layer::onEnterTransitionDidFinish();
if(_pRootViewModel != nullptr){
_pRootViewModel->onEnterTransitionDidFinish();
}
}
void View::showAds()
{
auto pluginManager = PluginManager::getInstance();
auto plugin = pluginManager->loadPlugin("AdsAdmob");
_admob = dynamic_cast<ProtocolAds*>(plugin);
TAdsDeveloperInfo devInfo;
devInfo["AdmobID"] = ADMOB_ID;
_admob->configDeveloperInfo(devInfo);
#ifdef DEBUG
_admob->setDebugMode(true);
#endif
_adInfo["AdmobType"] = "1";
_adInfo["AdmobSizeEnum"] = "1";
_admob->showAds(_adInfo, ProtocolAds::kPosBottom);
}
void View::hideAds()
{
_admob->hideAds(_adInfo);
}
ViewModel* View::getRootViewModel()
{
return _pRootViewModel;
}
void View::setName(const std::string& name)
{
auto start = name.find_last_of("/") + 1;
auto end = name.find_last_of(".");
Layer::setName(name.substr(start, end - start));
}
void MyAdsListener::onAdsResult(AdsResultCode code, const char* msg)
{
log("OnAdsResult, code : %d, msg : %s", code, msg);
}
void MyAdsListener::onPlayerGetPoints(cocos2d::plugin::ProtocolAds* pAdsPlugin, int points)
{
log("Player get points : %d", points);
// @warning should add code to give game-money to player here
// spend the points of player
if (pAdsPlugin != NULL) {
pAdsPlugin->spendPoints(points);
}
}