ToyTOSAcceptPopupView.cpp
4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// Created by Katarzyna Kalinowska-Górska on 2019-10-14.
//
#include "ToyTOSAcceptPopupView.h"
#include "ToyTouchInterceptingLayer.h"
#include "ToySimpleButton.h"
#include "ToyScalingUtils.h"
#include "ToyParentalGateView.h"
#include "ToyMiscUtils.h"
#include "ToyStrings.h"
#include <stdio.h>
ToyTOSAcceptPopupView * ToyTOSAcceptPopupView::create(std::function<void()> onAcceptCallback){
    ToyTOSAcceptPopupView * view = new (std::nothrow) ToyTOSAcceptPopupView();
    if(view && view->init(onAcceptCallback))
    {
        view->autorelease();
        return view;
    }
    CC_SAFE_DELETE(view);
    return nullptr;
}
bool ToyTOSAcceptPopupView::init(std::function<void()> onAcceptCallback){
    if(!cocos2d::LayerColor::initWithColor(cocos2d::Color4B(0, 0, 0, 220))){
        return false;
    }
    _onAcceptCallback = onAcceptCallback;
    setupAppearance();
    
    return true;
}
void ToyTOSAcceptPopupView::setupAppearance(){
    auto popup = cocos2d::Sprite::create("graphics/tos_popup/accept_popup.png"); //TODO hard-coded stirng
    auto wholeSize = cocos2d::Director::getInstance()->getWinSize();
    setContentSize(wholeSize);
    addChild(popup);
    popup->setPosition(cocos2d::Vec2(wholeSize.width/2, wholeSize.height/2));
    
    auto menuItemColor = cocos2d::Color3B(40, 80, 160);
    cocos2d::MenuItemFont::setFontSize(120*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 paddingBottom = 40*ToyScalingUtils::scaleAspectFillToDesignIpadProSize();
    auto menuPadding = 40*ToyScalingUtils::scaleAspectFillToDesignIpadProSize();//TODO magic number
    auto menuItemAbout = cocos2d::MenuItemFont::create("Terms of Use", CC_CALLBACK_1(ToyTOSAcceptPopupView::aboutMenuButtonTapped, this));
    auto menuItemPrivacyPolicy = cocos2d::MenuItemFont::create("Privacy Policy", CC_CALLBACK_1(ToyTOSAcceptPopupView::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);
    addChild(menuCenter);
    menuCenter->setPosition(wholeSize.width/2, wholeSize.height/2);
    menuCenter->setVisible(false);
    
    auto buttonAccept = ToySimpleButton::create();
    auto acceptButtonTexturePath = "graphics/tos_popup/accept_button.png";
    buttonAccept->loadTextures(acceptButtonTexturePath, acceptButtonTexturePath, acceptButtonTexturePath);
    addChild(buttonAccept);
    buttonAccept->setAnchorPoint(cocos2d::Vec2(0.5, 1));
    buttonAccept->cocos2d::Node::setPosition(wholeSize.width/2, menuCenter->getPositionY() - menuCenter->getContentSize().height/2 - menuPadding);
    buttonAccept->setOnTouchEndedCallback([&](std::string name, cocos2d::ui::Widget::TouchEventType eventType){
        _onAcceptCallback();
        ToyMiscUtils::hideAndRemoveView(this, true);
//        presentParentalGate([&](){
//            hideParentalGate();
//            _onAcceptCallback();
//            ToyMiscUtils::hideAndRemoveView(this, true);
//        }, [&](){
//            hideParentalGate();
//        });
    });
}
void ToyTOSAcceptPopupView::aboutMenuButtonTapped(cocos2d::Ref* pSender){
    presentParentalGate([&](){
        hideParentalGate();
        cocos2d::Application::getInstance()->openURL(ToyStrings::LINK_TERMS_OF_SERVICE);
    }, [&](){
        hideParentalGate();
    });
}
void ToyTOSAcceptPopupView::privacyPolicyMenuButtonTapped(cocos2d::Ref* pSender){
    presentParentalGate([&](){
        hideParentalGate();
        cocos2d::Application::getInstance()->openURL(ToyStrings::LINK_PRIVACY_POLICY);
    }, [&](){
        hideParentalGate();
    });
}