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

#include <stdio.h>
#include "HSettingsLayer.h"
#include "HMiscUtils.h"
#include "HScalingUtils.h"
#include "HStrings.h"
#include "HalloweenSimpleButton.h"
#include "KKGLinearLayout.h"
#include "KKGLabelOnlyButton.hpp"

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

bool HSettingsLayer::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 = KKGLinearLayout::create(KKGLinearLayout::VERTICAL,
                                                  KKGLinearLayout::ACENTER,
                                                  KKGLinearLayout::GCENTER);
    addChild(wholeContainer);

    auto upperStuffContainer = cocos2d::Node::create();
    wholeContainer->addView(upperStuffContainer);
    
    auto padding = 50*HScalingUtils::scaleAspectFillToDesignIpadProSize();
    wholeContainer->setInterViewMargin(padding, false);

    std::vector<std::string> levelImagePaths = { "graphics/levels/level_1.png", "graphics/levels/level_2.png", "graphics/levels/level_3.png" };
    _HLevelPickerView = HLevelPickerView::create(levelImagePaths, std::vector<std::string>());
    upperStuffContainer->addChild(_HLevelPickerView);
    _HLevelPickerView->addOnLevelChangedCallback([&](int pickedLevel){
        HMiscUtils::saveLastLevel(pickedLevel);
        _onLevelPicked(pickedLevel);
    });
    _HLevelPickerView->selectLevelIndex((int)HMiscUtils::lastLevel(), false);

    auto paddingBottom = padding;
    upperStuffContainer->setContentSize(cocos2d::Size(_HLevelPickerView->getContentSize().width,
                                                      _HLevelPickerView->getContentSize().height + paddingBottom));

    _HLevelPickerView->setAnchorPoint(cocos2d::Vec2(1, 0.5));
    _HLevelPickerView->setPosition(upperStuffContainer->getContentSize().width, upperStuffContainer->getContentSize().height/2 + paddingBottom);

    auto webLinkButton = HalloweenSimpleButton::create();
    wholeContainer->addView(webLinkButton);
    webLinkButton->setVisible(false);
    auto buttonTexturePath = "settings_buttons/steve_maggie.png";
    webLinkButton->loadTextures(buttonTexturePath, buttonTexturePath, buttonTexturePath);
    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 lowerStuffContainer = cocos2d::Node::create();
    wholeContainer->addView(lowerStuffContainer);

    auto menuItemColor = cocos2d::Color3B(140, 140, 140);
    cocos2d::MenuItemFont::setFontSize(140*HScalingUtils::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 menuPadding = 40*HScalingUtils::scaleAspectFillToDesignIpadProSize();//TODO magic number
    auto menuItemAbout = cocos2d::MenuItemFont::create("Terms of Use", CC_CALLBACK_1(HSettingsLayer::aboutMenuButtonTapped, this));
    auto menuItemPrivacyPolicy = cocos2d::MenuItemFont::create("Privacy Policy", CC_CALLBACK_1(HSettingsLayer::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));

    menuCenter->setPosition(lowerStuffContainer->getContentSize().width/2, lowerStuffContainer->getContentSize().height/2);

    menuCenter->setVisible(false);

    wholeContainer->wrapContents();
    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);
    
    auto paddingExitButton = 20*HScalingUtils::scaleAspectFillToDesignIpadProSize();
    auto scaleExitButton = 1.0f;
    if(HScalingUtils::isSmallDevice()){
        paddingExitButton *= HScalingUtils::getScaleForSmallDevice();
        scaleExitButton = 1.6f;
    }
    
    auto appExitButton = HalloweenSimpleButton::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){
        HMiscUtils::showAppCloseConfirmDialog([&](){
        });
    });
    
    return true;
}

void HSettingsLayer::prepareForShowing(){
    _HLevelPickerView->selectLevelIndex((int)(HMiscUtils::lastLevel()), false);
}

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

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