AniAlertView.cpp 5.03 KB
//
//  AniAlertView.cpp
//  Steve and Maggie Halloween
//
//  Created by Katarzyna Kalinowska-Górska on 09/10/2019.
//

#include <stdio.h>
#include "AniAlertView.h"
#include "AniScalingUtils.h"
#include "../../cocos2d/cocos/2d/CCActionInterval.h"
#include "../../cocos2d/cocos/2d/CCActionInstant.h"
#include "../../cocos2d/cocos/2d/CCNode.h"
#include "AniMiscUtils.h"

//AniAlertView* AniAlertView::create(std::string message, std::string okText, cocos2d::Color3B okColor, std::function<void()> onConfirmCallback,){
//
//    AniAlertView * view = new (std::nothrow) AniAlertView();
//    if(view && view->init(message, okText, okColor, onConfirmCallback))
//    {
//        view->autorelease();
//        return view;
//    }
//    CC_SAFE_DELETE(view);
//    return nullptr;
//}

AniAlertView* AniAlertView::create(std::string message, std::string okText, std::string cancelText, cocos2d::Color3B okColor, cocos2d::Color3B cancelColor, std::function<void()> onConfirmCallback,std::function<void()> onCancelCallback){
    
    AniAlertView * view = new (std::nothrow) AniAlertView();
    if(view && view->init(message, okText, cancelText, okColor, cancelColor, onConfirmCallback, onCancelCallback))
    {
        view->autorelease();
        return view;
    }
    CC_SAFE_DELETE(view);
    return nullptr;
}

bool AniAlertView::init(std::string message, std::string okText, std::string cancelText, cocos2d::Color3B okColor, cocos2d::Color3B cancelColor, std::function<void()> onConfirmCallback,std::function<void()> onCancelCallback){
    
    auto winSize = cocos2d::Director::getInstance()->getWinSize();
    if(!cocos2d::LayerColor::initWithColor(cocos2d::Color4B(0,0,0,220), winSize.width, winSize.height)){
        return false;
    }
    
    _onConfirmCallback = onConfirmCallback;
    _onCancelCallback = onCancelCallback;
    setupAppearance(message, okText, cancelText, okColor, cancelColor);

    return true;
}

void AniAlertView::setupAppearance(std::string message, std::string okText, std::string cancelText, cocos2d::Color3B okColor, cocos2d::Color3B cancelColor){

    setCascadeOpacityEnabled(true);
;
    auto wholeContainer = cocos2d::Node::create();
    addChild(wholeContainer);
    
    //    // add the instruction label
    auto messageLabel = cocos2d::Label::createWithTTF(message, "fonts/ComicSansMSBold.ttf", 90*AniScalingUtils::getScaleForFont()); //TODO magic number, hard-coded text
    messageLabel->setColor(cocos2d::Color3B(200, 200, 200));
    wholeContainer->addChild(messageLabel);
    wholeContainer->setCascadeOpacityEnabled(true);

    cocos2d::MenuItemFont::setFontSize(80*AniScalingUtils::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 = 140*AniScalingUtils::scaleAspectFillToDesignIpadProSize();//TODO magic number
    auto menuItemCancel = cocos2d::MenuItemFont::create(cancelText, CC_CALLBACK_1(AniAlertView::onCancelClicked, this));
    menuItemCancel->setColor(cancelColor);
    auto menuItemOK = cocos2d::MenuItemFont::create(okText, CC_CALLBACK_1(AniAlertView::onOKClicked, this));
//    menuItemOK->setColor(cocos2d::Color3B(200, 100, 100));
    menuItemOK->setColor(okColor);
    auto menuCenter = cocos2d::Menu::create(menuItemCancel,menuItemOK, nullptr);
    menuCenter->setContentSize(cocos2d::Size(menuItemOK->getBoundingBox().size.width + menuItemCancel->getBoundingBox().size.width + menuPadding, MAX(menuItemOK->getBoundingBox().size.height, menuItemCancel->getBoundingBox().size.height)));
    menuCenter->alignItemsHorizontallyWithPadding(menuPadding);
    menuCenter->setCascadeOpacityEnabled(true);

    wholeContainer->addChild(menuCenter);
    wholeContainer->setContentSize(cocos2d::Size(MAX(menuCenter->getBoundingBox().size.width, messageLabel->getBoundingBox().size.width), menuCenter->getBoundingBox().size.height + menuPadding + messageLabel->getBoundingBox().size.height));
    
    menuCenter->setPositionX(wholeContainer->getContentSize().width/2);
    menuCenter->setPositionY(messageLabel->getBoundingBox().getMinY() + menuPadding);
    messageLabel->setPositionY(wholeContainer->getContentSize().height - messageLabel->getBoundingBox().size.height/2);
    messageLabel->setPositionX(wholeContainer->getContentSize().width/2);
    
    wholeContainer->setAnchorPoint(cocos2d::Vec2(0.5, 0.5));
    wholeContainer->setPosition(getContentSize().width/2, getContentSize().height/2);
}

void AniAlertView::onCancelClicked(cocos2d::Ref* pSender){
    _onCancelCallback();
        runAction(cocos2d::Sequence::create(cocos2d::FadeOut::create(AniMiscUtils::StandardAnimationTime), cocos2d::CallFunc::create([&](){
            AniMiscUtils::hideAndRemoveView(this,true);
        }), nullptr));
}

void AniAlertView::onOKClicked(cocos2d::Ref* pSender){
    _onConfirmCallback();
    runAction(cocos2d::Sequence::create(cocos2d::FadeOut::create(AniMiscUtils::StandardAnimationTime), cocos2d::CallFunc::create([&](){
        AniMiscUtils::hideAndRemoveView(this,true);
    }), nullptr));
}