ToyMiscUtils.cpp
8.07 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
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
//
// ToyMiscUtils.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 17.05.2017.
//
//
#include <stdio.h>
#include "ToyMiscUtils.h"
#include "ToyGeometryUtils.h"
#include "ToyAlertView.h"
#include "HelloWorldScene.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "RatePromptHandler_ios.h"
#endif
#include "audio/include/AudioEngine.h"
float ToyMiscUtils::StandardAnimationTime = 0.2;
const std::string ToyMiscUtils::MAGGIE_TOUCHED_UD_KEY = "MAGGIE_TOUCHED_UD_KEY";
const std::string ToyMiscUtils::WHICH_LEVEL_UD_KEY = "WHICH_LEVEL_UD_KEY";
const std::string ToyMiscUtils::TOS_ACCEPTED_UD_KEY = "TOS_ACCEPTED_UD_KEY";
const std::string ToyMiscUtils::BG_MUSIC_PLAYING_UD_KEY = "BG_MUSIC_PLAYING_UD_KEY";
void ToyMiscUtils::saveMaggieTouched(){
cocos2d::UserDefault::getInstance()->setBoolForKey(MAGGIE_TOUCHED_UD_KEY.c_str(), true);
cocos2d::UserDefault::getInstance()->flush();
}
bool ToyMiscUtils::wasMaggieTouched(){
return cocos2d::UserDefault::getInstance()->getBoolForKey(MAGGIE_TOUCHED_UD_KEY.c_str(), false);
}
void ToyMiscUtils::saveTOSAccepted(){
cocos2d::UserDefault::getInstance()->setBoolForKey(TOS_ACCEPTED_UD_KEY.c_str(), true);
cocos2d::UserDefault::getInstance()->flush();
}
bool ToyMiscUtils::wasTOSAccepted(){
return true;
return cocos2d::UserDefault::getInstance()->getBoolForKey(TOS_ACCEPTED_UD_KEY.c_str(), false);
}
void ToyMiscUtils::saveBgMusicPlaying(bool bgMusicPlaying){
cocos2d::UserDefault::getInstance()->setBoolForKey(BG_MUSIC_PLAYING_UD_KEY.c_str(), bgMusicPlaying);
cocos2d::UserDefault::getInstance()->flush();
}
bool ToyMiscUtils::shouldBgMusicPlay(){
return cocos2d::UserDefault::getInstance()->getBoolForKey(BG_MUSIC_PLAYING_UD_KEY.c_str(), true);
}
//void ToyMiscUtils::saveLastLevel(Level level){
// cocos2d::UserDefault::getInstance()->setIntegerForKey(WHICH_LEVEL_UD_KEY.c_str(), (int)level);
//}
void ToyMiscUtils::saveLastLevel(int level){
cocos2d::UserDefault::getInstance()->setIntegerForKey(WHICH_LEVEL_UD_KEY.c_str(), level);
cocos2d::UserDefault::getInstance()->flush();
}
ToyMiscUtils::Level ToyMiscUtils::lastLevel(){
return (Level)cocos2d::UserDefault::getInstance()->getIntegerForKey(WHICH_LEVEL_UD_KEY.c_str(), (int)Level::TEEN);
}
int ToyMiscUtils::nextLevel(){
return MIN((int)lastLevel() + 1, (int)Level::ADULT);
}
static int FadeAnimationTag = 88;
void ToyMiscUtils::showView(cocos2d::Node* node, bool animated, float toAlpha){
if(node != nullptr){
node->stopActionByTag(FadeAnimationTag);
node->setVisible(true);
if(animated){
auto fadeAction = cocos2d::FadeTo::create(StandardAnimationTime, toAlpha);
fadeAction->setTag(FadeAnimationTag);
node->runAction(fadeAction);
} else {
node->setOpacity(toAlpha);
}
}
}
void ToyMiscUtils::hideView(cocos2d::Node* node, bool animated){
if(node != nullptr){
node->stopActionByTag(FadeAnimationTag);
if(animated){
auto fadeAction = cocos2d::Sequence::create(cocos2d::FadeOut::create(StandardAnimationTime),
cocos2d::CallFunc::create(std::bind([&](cocos2d::Node* n){
n->setVisible(false);
},node)),
nullptr);
fadeAction->setTag(FadeAnimationTag);
node->runAction(fadeAction);
} else {
node->setVisible(false);
}
}
}
void ToyMiscUtils::hideAndRemoveView(cocos2d::Node* node, bool animated){
if(node != nullptr){
if(animated){
node->runAction(cocos2d::Sequence::create(cocos2d::FadeOut::create(StandardAnimationTime),
cocos2d::CallFunc::create(std::bind([&](cocos2d::Node* n){
n->removeFromParent();
},node)),
nullptr));
} else {
node->removeFromParent();
}
}
}
cocos2d::Rect ToyMiscUtils::getExtendedActiveArea(cocos2d::Node* object, float extendPercentValueX, float extendPercentValueY, ExtendDirectionX extendDirX, ExtendDirectionY extendDirY)
{
auto objectBoundingBox = ToyGeometryUtils::getBoundingBoxToWorld(object);
if(extendPercentValueX == 0 && extendPercentValueY == 0){
return objectBoundingBox;
}
auto extendValueX = objectBoundingBox.size.width*extendPercentValueX/100.0;
auto extendValueY = objectBoundingBox.size.height*extendPercentValueY/100.0;
auto x = objectBoundingBox.origin.x;
auto y = objectBoundingBox.origin.y;
auto w = objectBoundingBox.size.width;
auto h = objectBoundingBox.size.height;
switch(extendDirX){
case ExtendDirectionX::LEFT:
x -= extendValueX;
w += extendValueX;
break;
case ExtendDirectionX::RIGHT:
w += extendValueX;
break;
case ExtendDirectionX::BOTH:
x -= extendValueX/2;
w += extendValueX;
break;
}
switch(extendDirY){
case ExtendDirectionY::DOWN:
y -= extendValueY;
h += extendValueY;
break;
case ExtendDirectionY::UP:
h += extendValueY;
break;
case ExtendDirectionY::BOTH:
y -= extendValueY/2;
h += extendValueY;
break;
}
return cocos2d::Rect(x, y, w, h);
}
bool ToyMiscUtils::isNodeVisible(cocos2d::Node* node){
bool ret = true;
cocos2d::Node* parent = node;
while (parent != nullptr) {
if(!parent->isVisible()){
ret = false;
break;
} else {
parent = parent->getParent();
}
}
return ret;
}
std::string ToyMiscUtils::boolToString(bool value)
{
if(value){
return "true";
} else {
return "false";
}
}
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
static const int ToyAlertViewTag = 999;
void ToyMiscUtils::showAppCloseConfirmDialog(std::function<void()> onCancelCallback){
if(cocos2d::Director::getInstance()->getRunningScene()->getChildByTag(ToyAlertViewTag) == nullptr) {
auto alpha = 220;
auto message = "Are you sure you want to quit?"; //TODO
auto okText = "Yes, bye!";
auto cancelText = "No, let's play!";
auto cancelColor = cocos2d::Color3B(68, 200, 220);
auto okColor = cocos2d::Color3B(200, 100, 100);
auto alert = ToyAlertView::create(message, okText, cancelText, okColor, cancelColor, []() {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::Director::getInstance()->end();
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
cocos2d::AudioEngine::stopAll();
cocos2d::Director::getInstance()->pause();
handler_nativeExitGame();
#else
cocos2d::Director::getInstance()->end();
#endif
// auto scene = HelloWorld::createScene();
// cocos2d::Director::getInstance()->replaceScene(scene);
}, onCancelCallback);
auto touchListener = cocos2d::EventListenerTouchOneByOne::create();
touchListener->setSwallowTouches(true);
touchListener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event) {
return true;
};
cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, alert);
alert->setTag(ToyAlertViewTag);
alert->setLocalZOrder(1000); //TODO make general zorders, named
alert->setOpacity(0);
cocos2d::Director::getInstance()->getRunningScene()->addChild(alert);
alert->runAction(cocos2d::FadeTo::create(ToyMiscUtils::StandardAnimationTime, alpha));
}
}
bool ToyMiscUtils::closeAppCloseConfirmDialogIfNecessary(){
auto alertNode = cocos2d::Director::getInstance()->getRunningScene()->getChildByTag(ToyAlertViewTag);
if(alertNode != nullptr) {
alertNode->removeFromParent();
return true;
}
return false;
}
//#endif