// // 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 ToyParentalGateView::KKTextDigit::DigitNames = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; ToyParentalGateView* ToyParentalGateView::create(float width, float height, std::function onSuccessCallback,std::function 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 onSuccessCallback,std::function 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::generateTextDigits(int digitsCount){ std::vector 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) // } //} // //}