HParentalGateView.cpp
8.2 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
//
// Created by Katarzyna Kalinowska-Górska on 2019-10-07.
//
#include "HParentalGateView.h"
#include "HScalingUtils.h"
#include "HalloweenSimpleButton.h"
#include "HMathUtils.h"
#include "KKGLabelButton.h"
const std::vector< char*> HParentalGateView::KKTextDigit::DigitNames = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
HParentalGateView* HParentalGateView::create(float width, float height, std::function<void()> onSuccessCallback,std::function<void()> onFailureCallback){
HParentalGateView * view = new (std::nothrow) HParentalGateView();
if(view && view->init(width, height, onSuccessCallback, onFailureCallback))
{
view->autorelease();
return view;
}
CC_SAFE_DELETE(view);
return nullptr;
}
bool HParentalGateView::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 HParentalGateView::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*HScalingUtils::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*HScalingUtils::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*HScalingUtils::getScaleForFont());
digitsLabel->setColor(cocos2d::Color3B(160, 130, 160));
wholeContainer->addChild(instructionLabel1);
wholeContainer->addChild(instructionLabel2);
wholeContainer->addChild(digitsLabel);
auto buttonPadding = 20*HScalingUtils::scaleAspectFillToDesignIpadProSize();
float digitButtonSide = 0;
auto buttonContainer = cocos2d::Node::create();
buttonContainer->setCascadeOpacityEnabled(true);
for(int i = 1; i <= 9; ++i){
auto digitButtonBgTexturePath = randomButtonBgImagePath();
auto digitButton = KKGLabelButton::create(digitButtonBgTexturePath,
std::to_string(i),
"fonts/ComicSansMSBold.ttf",
110*HScalingUtils::getScaleForFont(),
cocos2d::Color4B(0, 0, 0, 255));
digitButton->setCascadeOpacityEnabled(true);
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));
auto digitName = KKTextDigit::DigitNames[i];
digitButton->setOnTouchEndedCallback([&, digitName](KKGLabelButton* button, cocos2d::ui::Widget::TouchEventType eventType){
digitButtonPressed(digitName);
});
}
auto buttonContainerSide = 3*digitButtonSide+5*buttonPadding;
buttonContainer->setContentSize(cocos2d::Size(buttonContainerSide, buttonContainerSide));
wholeContainer->addChild(buttonContainer);
// auto nevermindButton = HalloweenSimpleButton::create();
// auto nevermindLabel = cocos2d::Label::createWithTTF("Never mind", "fonts/ComicSansMSRegular.ttf", 110*HScalingUtils::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 HParentalGateView::setupLogic(){
correctDigitSequence = generateTextDigits(3);
currentIndex = 0;
updateDigitsLabel();
}
std::vector<HParentalGateView::KKTextDigit> HParentalGateView::generateTextDigits(int digitsCount){
std::vector<KKTextDigit> digits;
for(int i = 0; i < digitsCount; ++i){
auto newDigit = KKTextDigit(HMathUtils::getRandomInt(1, 9));
digits.push_back(newDigit);
}
return digits;
}
void HParentalGateView::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 HParentalGateView::randomButtonBgImagePath(){
auto index = HMathUtils::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 HParentalGateView::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;
// WOWHScalingUtils.scaleAllDistances(inView: self.view, withScale: scale)
// WOWHScalingUtils.scaleFonts(inView: self.view, withScale: scale)
// } else {
// WOWHScalingUtils.ultimateScaleAllToIpadProDesign(inView: self.view)
// }
//}
//
//}