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

#include <stdio.h>
#include "SettingsLayer.h"
#include "MiscUtils.h"
#include "ScalingUtils.h"
#include "Strings.h"
#include "SimpleButton.h"

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

bool SettingsLayer::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 = 50*ScalingUtils::scaleAspectFillToDesignIpadProSize();

    std::vector<std::string> levelImagePaths = { "graphics/levels/level_1.png", "graphics/levels/level_2.png", "graphics/levels/level_3.png" };
    _levelPickerView = LevelPickerView::create(levelImagePaths, std::vector<std::string>());
    upperStuffContainer->addChild(_levelPickerView);
    _levelPickerView->addOnLevelChangedCallback([&](int pickedLevel){
        MiscUtils::saveLastLevel(pickedLevel);
        _onLevelPicked(pickedLevel);
    });
    _levelPickerView->selectLevelIndex((int)MiscUtils::lastLevel(), false);
    
    upperStuffContainer->setAnchorPoint(cocos2d::Vec2(0.5, 0.5));
    upperStuffContainer->setContentSize(_levelPickerView->getContentSize());
//    upperStuffContainer->setContentSize(cocos2d::Size(instructionLabel->getBoundingBox().size.width + lpPaddingLeft + _levelPickerView->getBoundingBox().size.width, _levelPickerView->getBoundingBox().size.height));
    upperStuffContainer->setPosition(width/2, height/2 + paddingTop);
    
    _levelPickerView->setAnchorPoint(cocos2d::Vec2(1, 0.5));
    _levelPickerView->setPosition(upperStuffContainer->getContentSize().width, upperStuffContainer->getContentSize().height/2);
//    instructionLabel->setAnchorPoint(cocos2d::Vec2(0, 0.5));
//    instructionLabel->setPosition(0, _levelPickerView->getBoundingBox().getMidY());
    
    auto lowerStuffContainer = cocos2d::Node::create();
    wholeContainer->addChild(lowerStuffContainer);

    auto menuItemColor = cocos2d::Color3B(140, 140, 140);
    cocos2d::MenuItemFont::setFontSize(120*ScalingUtils::getScaleForFont()); //TODO MAGIC NUMBER
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    auto paddingBottom = 20*ScalingUtils::scaleAspectFillToDesignIpadProSize(); //TODO  padding is a magicnumber scaling!
    cocos2d::MenuItemFont::setFontName("ComicSansMS-Bold");
#else
    auto paddingBottom = 70*ScalingUtils::scaleAspectFillToDesignIpadProSize(); //TODO  padding is a magicnumber scaling!
    cocos2d::MenuItemFont::setFontName("fonts/ComicSansMSBold.ttf");
#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    // setupAppLinksView(cocos2d::Point(getContentSize().width/2, upperStuffContainer->getBoundingBox().getMinY() - paddingBottom - paddingTop));
    // wholeContainer->addChild(m_appLinksView); //TODO messy messy
    // auto leftAppLinkSprite = cocos2d::Sprite::create("app_links/witch_halloween_app.png");
    // leftAppLinkSprite->setAnchorPoint(cocos2d::Vec2(1,1));
    // leftAppLinkSprite->setPosition(cocos2d::Vec2(m_appLinksView->getBoundingBox().getMinX(), m_appLinksView->getBoundingBox().getMaxY()));
    // auto rightAppLinkSprite = cocos2d::Sprite::create("app_links/doll_toy_app.png");
    // rightAppLinkSprite->setAnchorPoint(cocos2d::Vec2(0,0));
    // rightAppLinkSprite->setPosition(cocos2d::Vec2(m_appLinksView->getBoundingBox().getMaxX(), m_appLinksView->getBoundingBox().getMinY()));
    // wholeContainer->addChild(leftAppLinkSprite);
    // wholeContainer->addChild(rightAppLinkSprite);
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    // auto webLinkButton = SimpleButton::create();
    // wholeContainer->addChild(webLinkButton);
    // auto buttonTexturePath = "app_links/steve_maggie.png";
    // webLinkButton->loadTextures(buttonTexturePath, buttonTexturePath, buttonTexturePath);
    // webLinkButton->setPosition(cocos2d::Point(getContentSize().width/2, upperStuffContainer->getBoundingBox().getMinY() - paddingTop*3 - 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();
    //     });
    // });
#endif
    auto paddingExitButton = 20*ScalingUtils::scaleAspectFillToDesignIpadProSize();
    auto scaleExitButton = 1.0f;
    if(ScalingUtils::isSmallDevice()){
        paddingExitButton *= ScalingUtils::getScaleForSmallDevice();
        scaleExitButton = 1.6f;
    }

    auto appExitButton = SimpleButton::create();
    addChild(appExitButton);
    auto exitButtonTexturePath = "buttons/graphics/button_x.png";
    appExitButton->loadTextures(exitButtonTexturePath, exitButtonTexturePath, exitButtonTexturePath);
    appExitButton->setScale(scaleExitButton);
    appExitButton->setOriginalScale(scaleExitButton);
    appExitButton->setPosition(cocos2d::Point(getContentSize().width - appExitButton->getBoundingBox().size.width/2 - paddingExitButton, getContentSize().height - appExitButton->getBoundingBox().size.height/2 - paddingExitButton));
    appExitButton->setOnTouchEndedCallback([&, appExitButton](std::string name, cocos2d::ui::Widget::TouchEventType eventType){
        MiscUtils::showAppCloseConfirmDialog([&](){
        });
    });






    auto menuPadding = 30*ScalingUtils::scaleAspectFillToDesignIpadProSize();//TODO magic number

    auto menuItemAbout = cocos2d::MenuItemFont::create("Terms of Use", CC_CALLBACK_1(SettingsLayer::aboutMenuButtonTapped, this));
    auto menuItemPrivacyPolicy = cocos2d::MenuItemFont::create("Privacy Policy", CC_CALLBACK_1(SettingsLayer::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);
    lowerStuffContainer->addChild(menuCenter);//addChild(menuContainer, 3);
    
    lowerStuffContainer->setContentSize(cocos2d::Size(menuCenter->getContentSize().width, menuCenter->getBoundingBox().size.height));
    lowerStuffContainer->setAnchorPoint(cocos2d::Vec2(0.5, 1));

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    lowerStuffContainer->setPosition(getContentSize().width/2, upperStuffContainer->getBoundingBox().getMinY() - paddingBottom);
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    lowerStuffContainer->setPosition(getContentSize().width/2, upperStuffContainer->getBoundingBox().getMinY() - paddingBottom);
#endif

    menuCenter->setPosition(lowerStuffContainer->getContentSize().width/2, lowerStuffContainer->getContentSize().height/2);
    
    wholeContainer->setContentSize(cocos2d::Size(getContentSize().width, upperStuffContainer->getContentSize().height + lowerStuffContainer->getContentSize().height + paddingBottom + paddingTop));
    wholeContainer->setAnchorPoint(cocos2d::Vec2(0.5, 0.5));
    wholeContainer->setPosition(width/2, height/2);
    
    wholeContainer->setCascadeOpacityEnabled(true);
    upperStuffContainer->setCascadeOpacityEnabled(true);
    lowerStuffContainer->setCascadeOpacityEnabled(true);
    
    return true;
}

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

void SettingsLayer::aboutMenuButtonTapped(cocos2d::Ref* pSender){
    presentParentalGate([&](){
        hideParentalGate();
//        cocos2d::Application::getInstance()->openURL(Strings::LINK_TERMS_OF_SERVICE);
        cocos2d::Application::getInstance()->openURL("https://www.wattsenglish.com/steve-and-maggie-halloween/terms-of-use.html");
    }, [&](){
        hideParentalGate();
    });
}

void SettingsLayer::privacyPolicyMenuButtonTapped(cocos2d::Ref* pSender){
    presentParentalGate([&](){
        hideParentalGate();
//        cocos2d::Application::getInstance()->openURL(Strings::LINK_PRIVACY_POLICY);
        cocos2d::Application::getInstance()->openURL("https://www.wattsenglish.com/steve-and-maggie-halloween/principles-of-personal-data-processing.html");
    }, [&](){
        hideParentalGate();
    });
}

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
void SettingsLayer::setupAppLinksView(const cocos2d::Point& position){
    // TODO remove hard-code and add loading from a settings json file maybe
    auto contentScaleF = ScalingUtils::getAdjustedContentScaleFactor();
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
//    static std::vector<AppLinksView::AppLinkData> apps {
//            {"app_links/app_link_halloween.png", "com.wattsenglish.halloweengameapp"},
//            {"app_links/app_link_toy.png", "com.wattsenglish.toyapp"}
//    };

    static std::vector<AppLinksView::AppLinkData> apps {
        {"app_links/app_link_halloween.png", "id1482385753"},
        {"app_links/app_link_toy.png", "id1496563219"}
    };

    m_appLinksView = AppLinksView::create(apps, this);
    m_appLinksView->setAnchorPoint(cocos2d::Vec2(0.5, 1));
    m_appLinksView->setPosition(position);
}
#endif