// // 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 onSuccessCallback,std::function 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 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 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::generateTextDigits(int digitsCount){ std::vector 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) // } //} // //}