ToyParentalGateView.cpp
8.38 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
//
// Created by Katarzyna Kalinowska-Górska on 2019-10-07.
//
#include "ToyParentalGateView.h"
#include "ToyScalingUtils.h"
#include "ToySimpleButton.h"
#include "ToyMathUtils.h"
const std::vector<const char*> ToyParentalGateView::KKTextDigit::DigitNames = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
ToyParentalGateView* ToyParentalGateView::create(float width, float height, std::function<void()> onSuccessCallback,std::function<void()> onFailureCallback){
ToyParentalGateView * view = new (std::nothrow) ToyParentalGateView();
if(view && view->init(width, height, onSuccessCallback, onFailureCallback))
{
view->autorelease();
return view;
}
CC_SAFE_DELETE(view);
return nullptr;
}
bool ToyParentalGateView::init(float width, float height, std::function<void()> onSuccessCallback,std::function<void()> onFailureCallback){
if(!cocos2d::LayerColor::initWithColor(cocos2d::Color4B(40, 40, 70, 240), width, height)){
return false;
}
_onSuccessCallback = onSuccessCallback;
_onFailureCallback = onFailureCallback;
setupAppearance();
setupLogic();
setCascadeOpacityEnabled(true);
return true;
}
void ToyParentalGateView::setupAppearance(){
auto wholeContainer = cocos2d::Node::create();
addChild(wholeContainer);
wholeContainer->setCascadeOpacityEnabled(true);
// // add the instruction label
auto instructionLabel1 = cocos2d::Label::createWithTTF("Are you a parent?", "fonts/ComicSansMSBold.ttf", 120*ToyScalingUtils::getScaleForFont()); //TODO magic number, hard-coded text
instructionLabel1->setColor(cocos2d::Color3B(200, 200, 200));
auto instructionLabel2 = cocos2d::Label::createWithTTF("To continue, tap:", "fonts/ComicSansMSRegular.ttf", 100*ToyScalingUtils::getScaleForFont()); //TODO magic number, hard-coded text
instructionLabel2->setColor(cocos2d::Color3B(200, 200, 200));
digitsLabel = cocos2d::Label::createWithTTF("seven, seven, seven", "fonts/ComicSansMSBold.ttf", 110*ToyScalingUtils::getScaleForFont());
digitsLabel->setColor(cocos2d::Color3B(160, 130, 160));
wholeContainer->addChild(instructionLabel1);
wholeContainer->addChild(instructionLabel2);
wholeContainer->addChild(digitsLabel);
auto buttonPadding = 20*ToyScalingUtils::scaleAspectFillToDesignIpadProSize();
float digitButtonSide = 0;
auto buttonContainer = cocos2d::Node::create();
buttonContainer->setCascadeOpacityEnabled(true);
for(int i = 1; i <= 9; ++i){
auto digitButton = ToySimpleButton::create();
auto digitButtonBgTexturePath = randomButtonBgImagePath();
digitButton->loadTextures(digitButtonBgTexturePath, digitButtonBgTexturePath, digitButtonBgTexturePath);
digitButton->setCascadeOpacityEnabled(true);
auto label = cocos2d::Label::createWithTTF(std::to_string(i), "fonts/ComicSansMSBold.ttf", 110*ToyScalingUtils::getScaleForFont());
label->setColor(cocos2d::Color3B(0, 0, 0));
label->setAnchorPoint(cocos2d::Vec2(0.5, 0.5));
label->setPosition(cocos2d::Vec2(digitButton->getBoundingBox().size.width/2, digitButton->getBoundingBox().size.height/2));
digitButton->addChild(label);
buttonContainer->addChild(digitButton);
auto iMult = (i-1)%3+1;
digitButton->setPositionX(buttonPadding*iMult + digitButton->getBoundingBox().size.width*(iMult-0.5));
auto yMult = 1;
if(i > 6) yMult = 3;
else if(i > 3) yMult = 2;
yMult = 4 - yMult;
digitButton->setPositionY(buttonPadding*yMult + digitButton->getBoundingBox().size.height*(yMult-0.5));
digitButtonSide = MAX(digitButtonSide, MAX(digitButton->getBoundingBox().size.width, digitButton->getBoundingBox().size.height));
digitButton->objectName = KKTextDigit::DigitNames[i];
digitButton->setOnTouchEndedCallback([&](std::string name, cocos2d::ui::Widget::TouchEventType eventType){
digitButtonPressed(name);
});
}
auto buttonContainerSide = 3*digitButtonSide+5*buttonPadding;
buttonContainer->setContentSize(cocos2d::Size(buttonContainerSide, buttonContainerSide));
wholeContainer->addChild(buttonContainer);
// auto nevermindButton = ToySimpleButton::create();
// auto nevermindLabel = cocos2d::Label::createWithTTF("Never mind", "fonts/ComicSansMSRegular.ttf", 110*ToyScalingUtils::getScaleForFont()); //TODO magic number, hard-coded text
// nevermindLabel->setAnchorPoint(cocos2d::Vec2(0.5, 0.5));
// nevermindButton->addChild(nevermindLabel);
//// nevermindButton->loadText
// nevermindButton->setContentSize(nevermindLabel->getContentSize());
// nevermindLabel->setPosition(nevermindButton->getContentSize().width/2, nevermindButton->getContentSize().height/2);
// wholeContainer->addChild(nevermindButton);
auto wholeWidth = MAX(buttonContainer->getContentSize().width, instructionLabel1->getBoundingBox().size.width);
auto wholeHeight = buttonContainer->getContentSize().height + instructionLabel1->getBoundingBox().size.height + instructionLabel2->getBoundingBox().size.height + digitsLabel->getBoundingBox().size.height;// + nevermindButton->getBoundingBox().size.height;
wholeContainer->setContentSize(cocos2d::Size(wholeWidth, wholeHeight));
wholeContainer->setAnchorPoint(cocos2d::Vec2(0.5, 0.5));
wholeContainer->setPosition(getContentSize().width/2, getContentSize().height/2);
buttonContainer->setAnchorPoint(cocos2d::Vec2(0.5,0));
buttonContainer->setPositionX(wholeWidth/2);
buttonContainer->setPositionY(0);//nevermindButton->getBoundingBox().size.height);
instructionLabel1->setPosition(wholeWidth/2, wholeHeight - instructionLabel1->getBoundingBox().size.height/2);
instructionLabel2->setPosition(wholeWidth/2, instructionLabel1->getBoundingBox().getMinY() - instructionLabel2->getBoundingBox().size.height/2);
digitsLabel->setPosition(wholeWidth/2, instructionLabel2->getBoundingBox().getMinY() - digitsLabel->getBoundingBox().size.height/2);
// nevermindButton->setAnchorPoint(cocos2d::Vec2(1, 0));
// nevermindButton->setPosition(cocos2d::Vec2(wholeWidth, 0));
}
void ToyParentalGateView::setupLogic(){
correctDigitSequence = generateTextDigits(3);
currentIndex = 0;
updateDigitsLabel();
}
std::vector<ToyParentalGateView::KKTextDigit> ToyParentalGateView::generateTextDigits(int digitsCount){
std::vector<KKTextDigit> digits;
for(int i = 0; i < digitsCount; ++i){
auto newDigit = KKTextDigit(ToyMathUtils::getRandomInt(1, 9));
digits.push_back(newDigit);
}
return digits;
}
void ToyParentalGateView::updateDigitsLabel(){
std::string digitString = "";
for(int i = currentIndex; i < correctDigitSequence.size(); ++i){
digitString += correctDigitSequence[i].text;
if(i < correctDigitSequence.size()-1){
digitString += ", ";
}
}
digitsLabel->setString(digitString);
}
std::string ToyParentalGateView::randomButtonBgImagePath(){
auto index = ToyMathUtils::getRandomInt(1, 16);
switch (index%4) {
case 1:
return "buttons/graphics/button_yellow.png";
break;
case 2:
return "buttons/graphics/button_orange.png";
break;
case 3:
return "buttons/graphics/button_grey.png";
break;
default:
return "buttons/graphics/button_purple.png";
break;
}
}
void ToyParentalGateView::digitButtonPressed(std::string digitName){
if(currentIndex < 0 || currentIndex >= correctDigitSequence.size()){
return;
}
if(digitName == correctDigitSequence[currentIndex].text){
if(currentIndex == correctDigitSequence.size()-1){
currentIndex = -1;
_onSuccessCallback();
} else {
++currentIndex;
updateDigitsLabel();
}
} else {
currentIndex = -1;
_onFailureCallback();
}
}
//private func scaleSelfToHeight() {
// let screenHeight = UIScreen.main.bounds.height
// if(screenHeight < windowHeightConstraint.constant){
// let scale : CGFloat = screenHeight / windowHeightConstraint.constant;
// WOWToyScalingUtils.scaleAllDistances(inView: self.view, withScale: scale)
// WOWToyScalingUtils.scaleFonts(inView: self.view, withScale: scale)
// } else {
// WOWToyScalingUtils.ultimateScaleAllToIpadProDesign(inView: self.view)
// }
//}
//
//}