AniAlertUtils.cpp 2.55 KB

//
//  AniAlertUtils.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 17.05.2017.
//
//

#include <stdio.h>
#include "AniAlertUtils.h"
#include "AniAlertView.h"
#include "AniMiscUtils.h"
#include "HelloWorldScene.h"
static const int AniAlertViewTagDefault = 999;
static const int AniAlertViewTagCloseConfirm = 998;

void AniAlertUtils::showTwoButtonDialog(int tag, std::string text, std::string okButtonText, std::string cancelButtonText, std::function<void()> onOKCallback, std::function<void()> onCancelCallback){
    if(cocos2d::Director::getInstance()->getRunningScene()->getChildByTag(tag) == nullptr) { //TODO handle it better,present on top or something
        auto alpha = 220;
        
               auto cancelColor = cocos2d::Color3B(68, 200, 220);
               auto okColor = cocos2d::Color3B(200, 100, 100);

               auto alert = AniAlertView::create(text, okButtonText, cancelButtonText, okColor, cancelColor, onOKCallback, onCancelCallback);
               auto touchListener = cocos2d::EventListenerTouchOneByOne::create();
               touchListener->setSwallowTouches(true);
               touchListener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event) {
                   return true;
               };
               cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, alert);
               alert->setTag(tag);
               alert->setLocalZOrder(1000); //TODO make general zorders, named
               alert->setOpacity(0);
               cocos2d::Director::getInstance()->getRunningScene()->addChild(alert);
               alert->runAction(cocos2d::FadeTo::create(AniMiscUtils::StandardAnimationTime, alpha));
    }
}

bool AniAlertUtils::closeDialogIfNecessary(int tag){
    auto alertNode = cocos2d::Director::getInstance()->getRunningScene()->getChildByTag(tag);
    if(alertNode != nullptr) {
        alertNode->removeFromParent();
        return true;
    }
    return false;
}

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

void AniAlertUtils::showAppCloseConfirmDialog(std::function<void()> onCancelCallback){
    showTwoButtonDialog(AniAlertViewTagCloseConfirm, "Are you sure you want to quit?", "Yes, bye!", "No, let's play!", [](){
        //cocos2d::Director::getInstance()->end();
        auto scene = HelloWorld::createScene();
        cocos2d::Director::getInstance()->replaceScene(scene);



    }, onCancelCallback);
}

bool AniAlertUtils::closeAppCloseConfirmDialogIfNecessary(){
    return closeDialogIfNecessary(AniAlertViewTagCloseConfirm);
}

#endif