AppLinksView.cpp
4.43 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// Created by Katarzyna Kalinowska-Górska on 2019-10-07.
//
#include "AppLinksView.h"
#include "SimpleButton.h"
#include "ScalingUtils.h"
AppLinksView* AppLinksView::create(float width, float height, const std::vector<AppLinkData>& appsData, ParentalGateShowInterface* p_delegate){
    AppLinksView * node = new (std::nothrow) AppLinksView();
    if(node && node->init(width, height, appsData, p_delegate))
    {
        node->autorelease();
        return node;
    }
    CC_SAFE_DELETE(node);
    return nullptr;
}
AppLinksView* AppLinksView::create(const std::vector<AppLinkData>& appsData, ParentalGateShowInterface* p_delegate){
    AppLinksView * node = new (std::nothrow) AppLinksView();
    if(node && node->init(-1, -1, appsData, p_delegate))
    {
        node->autorelease();
        return node;
    }
    CC_SAFE_DELETE(node);
    return nullptr;
}
    
bool AppLinksView::init(float width, float height, const std::vector<AppLinkData>& appsData, ParentalGateShowInterface* p_delegate){
    if(!cocos2d::ui::ScrollView::init()){
        return false;
    }
    
    assert(p_delegate != nullptr);
    m_delegate = p_delegate;
    
    setDirection(cocos2d::ui::ScrollView::Direction::HORIZONTAL);
    bool adjustContentSize = width < 0 || height < 0;
    if(!adjustContentSize) {
        setContentSize(cocos2d::Size(width, height));
    }
    setScrollBarEnabled(false);
    auto scaleF = ScalingUtils::getAdjustedContentScaleFactor();
    static float PaddingX = 20/scaleF;
    static float PaddingY = 20/scaleF;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    static std::string PlayStoreBaseLinkApp =  "market://details?id=";
    static std::string PlayStoreBaseLinkWeb =  "https://play.google.com/store/apps/details?id=";
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    static std::string ITunesBaseLink = "itms-apps://itunes.apple.com/app/";
#endif
    auto tempX = PaddingX;
    float contentW = 0, contentH = 0;
    for(auto it = appsData.begin(); it != appsData.end(); ++it){
//        m_links.push_back(it->appStoreId);
        
        auto button = SimpleButton::create();
        auto buttonTexturePath = it->iconFilePath;
        button->loadTextures(buttonTexturePath, buttonTexturePath, buttonTexturePath);
        addChild(button);
        button->setPosition(cocos2d::Point(tempX + button->getBoundingBox().size.width/2, PaddingY + button->getBoundingBox().size.height/2));
        tempX += button->getContentSize().width + PaddingX;
        auto haloSprite = cocos2d::Sprite::create("app_links/halo_icon.png");
        haloSprite->setPosition(button->getPosition());
        addChild(haloSprite);
        haloSprite->setOpacity(0);
        haloSprite->setLocalZOrder(0);
        button->setLocalZOrder(1);
        button->setOnTouchBeganCallback([haloSprite, button](std::string name, cocos2d::ui::Widget::TouchEventType eventType){
            haloSprite->setScale(button->getScale());
            haloSprite->stopAllActions();
            haloSprite->runAction(cocos2d::FadeIn::create(0.1));
        });
        button->setOnTouchEndedCallback([&, haloSprite, button, link = it->storeId](std::string name, cocos2d::ui::Widget::TouchEventType eventType){
            haloSprite->stopAllActions();
            haloSprite->runAction(cocos2d::FadeOut::create(0.1));
            haloSprite->setScale(button->getScale());
            m_delegate->presentParentalGate([&](){
                    m_delegate->hideParentalGate();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
                if(!cocos2d::Application::getInstance()->openURL(PlayStoreBaseLinkApp + link)){
                    cocos2d::Application::getInstance()->openURL(PlayStoreBaseLinkWeb + link);
                }
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
                    cocos2d::Application::getInstance()->openURL(ITunesBaseLink + link);
#endif
                }, [&](){
                    m_delegate->hideParentalGate();
                });
        });
        button->setOnTouchCancelledCallback([haloSprite, button](std::string name, cocos2d::ui::Widget::TouchEventType eventType){
            haloSprite->stopAllActions();
            haloSprite->runAction(cocos2d::FadeOut::create(0.1));
            haloSprite->setScale(button->getScale());
        });
        contentH = haloSprite->getBoundingBox().size.height;
        contentW += button->getBoundingBox().size.width*1.4f;
    }
    if(adjustContentSize){
        setContentSize(cocos2d::Size(tempX, contentH));
    }
    
    return true;
}