AppLinksView.cpp 4.43 KB
//
// 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;
}