ToySettingsLayer.cpp 6.92 KB
//
//  ToySettingsLayer.cpp
//  Steve and Maggie Halloween
//
//  Created by Katarzyna Kalinowska-Górska on 06/10/2019.
//

#include <stdio.h>
#include "ToySettingsLayer.h"
#include "ToyMiscUtils.h"
#include "ToyScalingUtils.h"
#include "ToyStrings.h"
#include "ToySimpleButton.h"

ToySettingsLayer* ToySettingsLayer::create(float width, float height, std::function<void(int)> onLevelPickedCallback){
    ToySettingsLayer * view = new (std::nothrow) ToySettingsLayer();
    if(view && view->init(width, height, onLevelPickedCallback))
    {
        view->autorelease();
        return view;
    }
    CC_SAFE_DELETE(view);
    return nullptr;
}

bool ToySettingsLayer::init(float width, float height, std::function<void(int)> onLevelPickedCallback){
    
    if(!cocos2d::LayerColor::initWithColor(cocos2d::Color4B(0,0,0,230), width, height)){
        return false;
    }
    
    _isPresentingParentalGate = false;
    _onLevelPicked = onLevelPickedCallback;
    
    auto wholeContainer = cocos2d::Node::create();
    addChild(wholeContainer);
    
    auto upperStuffContainer = cocos2d::Node::create();
    wholeContainer->addChild(upperStuffContainer);
    
    auto paddingTop = 60*ToyScalingUtils::scaleAspectFillToDesignIpadProSize();
//    auto lpPaddingLeft = 60*ToyScalingUtils::scaleAspectFillToDesignIpadProSize();
    
    // add the instruction label
//    auto instructionLabel = cocos2d::Label::createWithTTF("LEVEL:", "fonts/ComicSansMSBold.ttf", 120*ToyScalingUtils::getScaleForFont()); //magic number
//    instructionLabel->setColor(cocos2d::Color3B(200, 200, 200));
//    upperStuffContainer->addChild(instructionLabel);

    std::vector<std::string> levelImagePaths = { "graphics/levels/level_1.png", "graphics/levels/level_2.png", "graphics/levels/level_3.png" };
    _levelPickerView = ToyLevelPickerView::create(levelImagePaths, std::vector<std::string>());
    upperStuffContainer->addChild(_levelPickerView);
    _levelPickerView->addOnLevelChangedCallback([&](int pickedLevel){
        ToyMiscUtils::saveLastLevel(pickedLevel);
        _onLevelPicked(pickedLevel);
    });
    _levelPickerView->selectLevelIndex((int)ToyMiscUtils::lastLevel(), false);
    
    upperStuffContainer->setAnchorPoint(cocos2d::Vec2(0.5, 0.5));
    upperStuffContainer->setContentSize(_levelPickerView->getContentSize());
    upperStuffContainer->setPosition(width/2, height/2 + paddingTop);
    
    _levelPickerView->setAnchorPoint(cocos2d::Vec2(1, 0.5));
    _levelPickerView->setPosition(upperStuffContainer->getContentSize().width, upperStuffContainer->getContentSize().height/2);

    
    auto lowerStuffContainer = cocos2d::Node::create();
    wholeContainer->addChild(lowerStuffContainer);
    
    auto menuItemColor = cocos2d::Color3B(140, 140, 140);
    cocos2d::MenuItemFont::setFontSize(140*ToyScalingUtils::getScaleForFont()); //TODO MAGIC NUMBER
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    cocos2d::MenuItemFont::setFontName("ComicSansMS-Bold");
#else
    cocos2d::MenuItemFont::setFontName("fonts/ComicSansMSBold.ttf");
#endif
    
    auto webLinkButton = ToySimpleButton::create();
    wholeContainer->addChild(webLinkButton);
    webLinkButton->setVisible(false);
    auto buttonTexturePath = "settings_buttons/steve_maggie.png";
    webLinkButton->loadTextures(buttonTexturePath, buttonTexturePath, buttonTexturePath);
    webLinkButton->setPosition(cocos2d::Point(getContentSize().width/2, upperStuffContainer->getBoundingBox().getMinY() - paddingTop*2 - webLinkButton->getBoundingBox().size.height/2));
    webLinkButton->setOnTouchEndedCallback([&, webLinkButton](std::string name, cocos2d::ui::Widget::TouchEventType eventType){
//       presentParentalGate([&](){
//            hideParentalGate();
//            cocos2d::Application::getInstance()->openURL("https://www.steveandmaggie.com"); //TODO hardcoded
//        }, [&](){
//            hideParentalGate();
//        });
    });
    
    auto menuPadding = 40*ToyScalingUtils::scaleAspectFillToDesignIpadProSize();//TODO magic number
//    auto menuContainer = cocos2d::Node::create();
    auto menuItemAbout = cocos2d::MenuItemFont::create("Terms of Use", CC_CALLBACK_1(ToySettingsLayer::aboutMenuButtonTapped, this));
    auto menuItemPrivacyPolicy = cocos2d::MenuItemFont::create("Privacy Policy", CC_CALLBACK_1(ToySettingsLayer::privacyPolicyMenuButtonTapped, this));
    auto menuCenter = cocos2d::Menu::create(menuItemAbout,menuItemPrivacyPolicy, NULL);
    menuCenter->setContentSize(cocos2d::Size(MAX(menuItemAbout->getBoundingBox().size.width, menuItemPrivacyPolicy->getBoundingBox().size.width), menuItemAbout->getBoundingBox().size.height + menuItemPrivacyPolicy->getBoundingBox().size.height + 3*menuPadding));
    menuCenter->setColor(menuItemColor);
    menuCenter->alignItemsVerticallyWithPadding(menuPadding);
//    menuContainer->addChild(menuCenter);
//    menuContainer->setContentSize(menuCenter->getContentSize());
//    menuContainer->setAnchorPoint(cocos2d::Point(1, 0.5));
//    menuContainer->setPosition(getContentSize().width/2, weLogo->getPositionY());
    lowerStuffContainer->addChild(menuCenter);//addChild(menuContainer, 3);
    
    lowerStuffContainer->setContentSize(cocos2d::Size(menuCenter->getContentSize().width, menuCenter->getBoundingBox().size.height));
    lowerStuffContainer->setAnchorPoint(cocos2d::Vec2(0.5, 1));
    lowerStuffContainer->setPosition(getContentSize().width/2, webLinkButton->getBoundingBox().getMinY() - paddingTop*0.8f);
    
    menuCenter->setPosition(lowerStuffContainer->getContentSize().width/2, lowerStuffContainer->getContentSize().height/2);
    
    menuCenter->setVisible(false);
    
    wholeContainer->setContentSize(cocos2d::Size(getContentSize().width, upperStuffContainer->getContentSize().height + lowerStuffContainer->getContentSize().height + paddingTop));
    wholeContainer->setAnchorPoint(cocos2d::Vec2(0.5, 0.5));
    wholeContainer->setPosition(width/2, height/2);
    
    wholeContainer->setCascadeOpacityEnabled(true);
    upperStuffContainer->setCascadeOpacityEnabled(true);
    lowerStuffContainer->setCascadeOpacityEnabled(true);
    
    //        auto bg = cocos2d::LayerColor::create(cocos2d::Color4B(100,100,255, 255));
//        bg->setContentSize(lowerStuffContainer->getContentSize());
//        lowerStuffContainer->addChild(bg);
//        bg->_setLocalZOrder(-10);
    
    return true;
}

void ToySettingsLayer::prepareForShowing(){
    _levelPickerView->selectLevelIndex((int)(ToyMiscUtils::lastLevel()), false);
}

void ToySettingsLayer::aboutMenuButtonTapped(cocos2d::Ref* pSender){
//    presentParentalGate([&](){
//        hideParentalGate();
        cocos2d::Application::getInstance()->openURL(ToyStrings::LINK_TERMS_OF_SERVICE);
//    }, [&](){
//        hideParentalGate();
//    });
}

void ToySettingsLayer::privacyPolicyMenuButtonTapped(cocos2d::Ref* pSender){
//    presentParentalGate([&](){
//        hideParentalGate();
        cocos2d::Application::getInstance()->openURL(ToyStrings::LINK_PRIVACY_POLICY);
//    }, [&](){
//        hideParentalGate();
//    });
}