/
HelloWorldScene.cpp
355 lines (287 loc) · 10.6 KB
/
HelloWorldScene.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#if ANDROID
#include "RazerSDK\RazerSDK_Plugin.h"
#include <android/log.h>
#include <iostream>
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "HelloWorldScene"
#endif
USING_NS_CC;
#define LEFT_ANCHOR 50
#define RIGHT_ANCHOR 250
#define VERT_PADDING 5
#define TEXT_PADDING 100
#if ANDROID
HelloWorld* HelloWorld::_sInstance = 0;
Main_CallbacksInitPlugin HelloWorld::_sMain_CallbacksInitPlugin = Main_CallbacksInitPlugin();
Main_CallbacksRequestLogin HelloWorld::_sMain_CallbacksRequestLogin = Main_CallbacksRequestLogin();
Main_CallbacksRequestGamerInfo HelloWorld::_sMain_CallbacksRequestGamerInfo = Main_CallbacksRequestGamerInfo();
Main_CallbacksRequestProducts HelloWorld::_sMain_CallbacksRequestProducts = Main_CallbacksRequestProducts();
Main_CallbacksRequestPurchase HelloWorld::_sMain_CallbacksRequestPurchase = Main_CallbacksRequestPurchase();
Main_CallbacksRequestReceipts HelloWorld::_sMain_CallbacksRequestReceipts = Main_CallbacksRequestReceipts();
Main_CallbacksShutdown HelloWorld::_sMain_CallbacksShutdown = Main_CallbacksShutdown();
#endif
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// '_sInstance' is an autorelease object
HelloWorld::_sInstance = HelloWorld::create();
// add layer as a child to scene
scene->addChild(_sInstance);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add your codes below...
/// app label
_mLabelApp = Label::createWithTTF("Cocos2d IAP Sample App", "fonts/Marker Felt.ttf", 18);
float y = origin.y + visibleSize.height;
/// app label
y -= _mLabelApp->getContentSize().height;
_mLabelApp->setPosition(Vec2(origin.x + visibleSize.width/2, y));
this->addChild(_mLabelApp, 1);
y -= VERT_PADDING;
float x = origin.x + LEFT_ANCHOR;
/// status label
_mLabelStatus = Label::createWithTTF("Status:", "fonts/Marker Felt.ttf", 12);
y -= _mLabelStatus->getContentSize().height;
_mLabelStatus->setPosition(Vec2(x, y));
this->addChild(_mLabelStatus, 1);
/// status text label
x = origin.x + TEXT_PADDING;
_mLabelStatusText = Label::createWithTTF("App Started", "fonts/Marker Felt.ttf", 12);
_mLabelStatusText->setPosition(Vec2(x, y));
_mLabelStatusText->setAnchorPoint(Vec2(0, 0.5f));
this->addChild(_mLabelStatusText, 1);
y -= VERT_PADDING;
/// gamer info label
x = origin.x + LEFT_ANCHOR;
_mLabelGamerInfo = Label::createWithTTF("GamerInfo:", "fonts/Marker Felt.ttf", 12);
y -= _mLabelGamerInfo->getContentSize().height;
_mLabelGamerInfo->setPosition(Vec2(x, y));
this->addChild(_mLabelGamerInfo, 1);
/// gamer info text label
x = origin.x + TEXT_PADDING;
_mLabelGamerInfoText = Label::createWithTTF("Need To Request", "fonts/Marker Felt.ttf", 12);
_mLabelGamerInfoText->setPosition(Vec2(x, y));
_mLabelGamerInfoText->setAnchorPoint(Vec2(0, 0.5f));
this->addChild(_mLabelGamerInfoText, 1);
y -= VERT_PADDING;
/// login button
x = origin.x + LEFT_ANCHOR + 20;
CreateButton(_mButtonRequestLogin, "Login", x, y, 2.f, CC_CALLBACK_1(HelloWorld::requestLoginCallback, this));
/// request products button
x = origin.x + LEFT_ANCHOR + 95;
CreateButton(_mButtonRequestProducts, "GetProducts", x, y, 2.5f, CC_CALLBACK_1(HelloWorld::requestProductsCallback, this));
/// request receipts button
x = origin.x + LEFT_ANCHOR + 175;
CreateButton(_mButtonRequestReceipts, "GetReceipts", x, y, 2.5f, CC_CALLBACK_1(HelloWorld::requestReceiptsCallback, this));
/// request gamer info button
x = origin.x + LEFT_ANCHOR + 265;
CreateButton(_mButtonRequestGamerInfo, "GetGamerInfo", x, y, 3.f, CC_CALLBACK_1(HelloWorld::requestGamerInfoCallback, this));
/// shutdown button
x = origin.x + LEFT_ANCHOR + 360;
CreateButton(_mButtonShutdown, "Shutdown", x, y, 2.5f, CC_CALLBACK_1(HelloWorld::shutdownCallback, this));
y -= _mButtonRequestProducts._mButton->getContentSize().height;
y -= VERT_PADDING;
y -= VERT_PADDING;
y -= VERT_PADDING;
/// products section
x = origin.x + LEFT_ANCHOR;
CreateButtonRequestProduct(x, y);
/// receipts section
x = origin.x + RIGHT_ANCHOR;
CreateButtonRequestReceipts(x, y);
return true;
}
HelloWorld* HelloWorld::GetInstance()
{
return _sInstance;
}
void HelloWorld::UpdateStatusText(const std::string& text)
{
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "%s", text.c_str());
_mLabelStatusText->setString(text);
}
void HelloWorld::UpdateStatusErrorText(const std::string& context, const int errorCode, const std::string& errorMessage)
{
std::ostringstream stream;
stream << context;
stream << ": ErrorCode=" << errorCode;
stream << " Message=" << errorMessage;
std::string text = stream.str();
_mLabelStatusText->setString(text);
}
void HelloWorld::UpdateGamerInfoText(const std::string& text)
{
_mLabelGamerInfoText->setString(text);
}
#if ANDROID
void HelloWorld::InitPlugin()
{
std::string secretApiKey = "eyJkZXZlbG9wZXJfaWQiOiIzMTBhOGY1MS00ZDZlLTRhZTUtYmRhMC1iOTM4";
secretApiKey += "NzhlNWY1ZDAiLCJkZXZlbG9wZXJfcHVibGljX2tleSI6Ik1JR2ZNQTBHQ1Nx";
secretApiKey += "R1NJYjNEUUVCQVFVQUE0R05BRENCaVFLQmdRQ3BkZUs4SDh6NG9qb0czZUI4";
secretApiKey += "azU4SWpWaEpJUkQ5MSt0aGQ1NjJNaXlEa09teEhLSXFMUlFId25OZW4xRHkv";
secretApiKey += "TGxnTStzak1GaEZHL0dERUVWemRIeTRNNkkyc1l6bGR4VmNLWWFpUlhFa0ls";
secretApiKey += "NUNyWjhtRGdLaWgzOFNueDFPY3R3UzFQM0wxcXA3LzZiM2xlejY4ZmIyalVV";
secretApiKey += "WFpIaStaRDZROGlPbzE5V3Rhd0lEQVFBQiJ9";
RazerSDK::Plugin::initPlugin(secretApiKey, &_sMain_CallbacksInitPlugin);
}
void HelloWorld::Shutdown()
{
//Close the cocos2d-x game scene and quit the application
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
/*To navigate back to native iOS screen(if present) without quitting the application, do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/
//EventCustom customEndEvent("game_scene_close_event");
//_eventDispatcher->dispatchEvent(&customEndEvent);
}
void HelloWorld::UpdateProducts(const std::vector<RazerSDK::Product>& products)
{
for (ButtonWithLabel buttonLabel : _mProducts)
{
this->removeChild(buttonLabel._mLabel);
this->removeChild(buttonLabel._mMenu);
this->removeChild(buttonLabel._mButton);
}
_mProducts.clear();
int y = _mLabelProducts->getPosition().y;
for (const RazerSDK::Product& product : products)
{
std::string text = product.Identifier;
ButtonWithLabel buttonWithLabel = ButtonWithLabel();
CreateButton(buttonWithLabel, text, 100, y, 4.5f, CC_CALLBACK_1(HelloWorld::requestPurchaseCallback, this));
_mProducts.push_back(buttonWithLabel);
y -= 35;
}
}
void HelloWorld::UpdateReceipts(const std::vector<RazerSDK::Receipt>& receipts)
{
for (cocos2d::Label* label : _mReceipts)
{
this->removeChild(label);
}
_mReceipts.clear();
int x = _mLabelReceipts->getPosition().x;
int y = _mLabelReceipts->getPosition().y - _mLabelReceipts->getContentSize().height;
for (const RazerSDK::Receipt& receipt : receipts)
{
y -= 16;
std::ostringstream stream;
stream << receipt.Identifier;
stream << " " << receipt.LocalPrice;
std::string text = stream.str();
Label* label = Label::createWithTTF(text, "fonts/Marker Felt.ttf", 12);
label->setPosition(Vec2(x, y));
label->setAnchorPoint(Vec2(0, 0.5f));
this->addChild(label, 1);
_mReceipts.push_back(label);
}
}
#endif
void HelloWorld::CreateButton(ButtonWithLabel& buttonWithLabel, const std::string& text, int x, int y, float hScale, const cocos2d::ccMenuCallback& callback)
{
buttonWithLabel._mButton = MenuItemImage::create(
"ButtonInactive.png",
"ButtonActive.png",
callback);
buttonWithLabel._mButton->setPosition(Vec2(x, y - buttonWithLabel._mButton->getContentSize().height));
buttonWithLabel._mButton->setScaleX(hScale);
this->addChild(buttonWithLabel._mButton, 1);
buttonWithLabel._mMenu = Menu::create(buttonWithLabel._mButton, NULL);
buttonWithLabel._mMenu->setPosition(Vec2::ZERO);
this->addChild(buttonWithLabel._mMenu, 1);
buttonWithLabel._mLabel = Label::createWithTTF(text, "fonts/Marker Felt.ttf", 12);
buttonWithLabel._mLabel->setPosition(Vec2(x, y - buttonWithLabel._mButton->getContentSize().height));
this->addChild(buttonWithLabel._mLabel, 1);
}
void HelloWorld::CreateButtonRequestProduct(int x, int y)
{
/// products label
_mLabelProducts = Label::createWithTTF("Products:", "fonts/Marker Felt.ttf", 12);
y -= _mLabelProducts->getContentSize().height;
_mLabelProducts->setPosition(Vec2(x, y));
this->addChild(_mLabelProducts, 1);
y -= VERT_PADDING;
}
void HelloWorld::CreateButtonRequestReceipts(int x, int y)
{
/// products label
_mLabelReceipts = Label::createWithTTF("Receipts:", "fonts/Marker Felt.ttf", 12);
y -= _mLabelReceipts->getContentSize().height;
_mLabelReceipts->setPosition(Vec2(x, y));
this->addChild(_mLabelReceipts, 1);
y -= VERT_PADDING;
}
void HelloWorld::requestProductsCallback(Ref* pSender)
{
#if ANDROID
std::vector<std::string> products;
products.push_back("long_sword");
products.push_back("sharp_axe");
products.push_back("__DECLINED__THIS_PURCHASE");
RazerSDK::Plugin::requestProducts(products, &_sMain_CallbacksRequestProducts);
#endif
}
void HelloWorld::requestPurchaseCallback(Ref* pSender)
{
#if ANDROID
MenuItemImage* button = dynamic_cast<cocos2d::MenuItemImage*>(pSender);
for (ButtonWithLabel buttonLabel : _mProducts)
{
if (button == buttonLabel._mButton)
{
std::string identifier = buttonLabel._mLabel->getString();
std::string text = "requestPurchase: ";
text += identifier;
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "%s", text.c_str());
HelloWorld::GetInstance()->UpdateStatusText(text);
std::string productType = "ENTITLEMENT";
RazerSDK::Plugin::requestPurchase(identifier, productType, &_sMain_CallbacksRequestPurchase);
break;
}
}
#endif
}
void HelloWorld::requestReceiptsCallback(Ref* pSender)
{
#if ANDROID
RazerSDK::Plugin::requestReceipts(&_sMain_CallbacksRequestReceipts);
#endif
}
void HelloWorld::requestLoginCallback(Ref* pSender)
{
#if ANDROID
RazerSDK::Plugin::requestLogin(&_sMain_CallbacksRequestLogin);
#endif
}
void HelloWorld::requestGamerInfoCallback(Ref* pSender)
{
#if ANDROID
RazerSDK::Plugin::requestGamerInfo(&_sMain_CallbacksRequestGamerInfo);
#endif
}
void HelloWorld::shutdownCallback(Ref* pSender)
{
#if ANDROID
RazerSDK::Plugin::shutdown(&_sMain_CallbacksShutdown);
#endif
}