MiscUtils.cpp 8.26 KB
//
//  MiscUtils.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 17.05.2017.
//
//

#include <stdio.h>
#include "MiscUtils.h"
#include "GeometryUtils.h"
#include "AlertView.h"
#include "HelloWorldScene.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "RatePromptHandler_ios.h"
#endif
#include "audio/include/AudioEngine.h"

float MiscUtils::StandardAnimationTime = 0.2;
const std::string MiscUtils::WHICH_LEVEL_UD_KEY = "WHICH_LEVEL_UD_KEY";
const std::string MiscUtils::TOS_ACCEPTED_UD_KEY = "TOS_ACCEPTED_UD_KEY";

void MiscUtils::saveTOSAccepted(){
    cocos2d::UserDefault::getInstance()->setBoolForKey(TOS_ACCEPTED_UD_KEY.c_str(), true);
    cocos2d::UserDefault::getInstance()->flush();
}

bool MiscUtils::wasTOSAccepted(){
    return true;
    return cocos2d::UserDefault::getInstance()->getBoolForKey(TOS_ACCEPTED_UD_KEY.c_str(), false);
}

//void MiscUtils::saveLastLevel(Level level){
//    cocos2d::UserDefault::getInstance()->setIntegerForKey(WHICH_LEVEL_UD_KEY.c_str(), (int)level);
//}

void MiscUtils::saveLastLevel(int level){
    cocos2d::UserDefault::getInstance()->setIntegerForKey(WHICH_LEVEL_UD_KEY.c_str(), level);
    cocos2d::UserDefault::getInstance()->flush();
}

MiscUtils::Level MiscUtils::lastLevel(){
    return (Level)cocos2d::UserDefault::getInstance()->getIntegerForKey(WHICH_LEVEL_UD_KEY.c_str(), (int)Level::TEEN);
}

int MiscUtils::nextLevel(){
    return MIN((int)lastLevel() + 1, (int)Level::ADULT);
}

static int FadeAnimationTag = 88;
void MiscUtils::showView(cocos2d::Node* node, bool animated, float toAlpha){
    if(node != nullptr){
        node->stopActionByTag(FadeAnimationTag);
        node->setVisible(true);
        if(animated){
            auto fadeAction = cocos2d::FadeTo::create(StandardAnimationTime, toAlpha);
            fadeAction->setTag(FadeAnimationTag);
            node->runAction(fadeAction);
        } else {
            node->setOpacity(toAlpha);
        }
    }
}

void MiscUtils::hideView(cocos2d::Node* node, bool animated){
    if(node != nullptr){
        node->stopActionByTag(FadeAnimationTag);
        if(animated){
            auto fadeAction = cocos2d::Sequence::create(cocos2d::FadeOut::create(StandardAnimationTime),
                                                      cocos2d::CallFunc::create(std::bind([&](cocos2d::Node* n){
                n->setVisible(false);
            },node)),
                                                        nullptr);
            fadeAction->setTag(FadeAnimationTag);
            node->runAction(fadeAction);
        } else {
            node->setVisible(false);
        }
    }
}

void MiscUtils::hideAndRemoveView(cocos2d::Node* node, bool animated){
    if(node != nullptr){
        if(animated){
            node->runAction(cocos2d::Sequence::create(cocos2d::FadeOut::create(StandardAnimationTime),
                                                  cocos2d::CallFunc::create(std::bind([&](cocos2d::Node* n){
                                                    n->removeFromParent();
                                                    },node)),
                                                  nullptr));
        } else {
            node->removeFromParent();
        }
    }
}

cocos2d::Rect MiscUtils::getExtendedActiveArea(cocos2d::Node* object, float extendPercentValueX, float extendPercentValueY, ExtendDirectionX extendDirX, ExtendDirectionY extendDirY)
{
    auto ignoreTransoframtions = object->getScale() == 1 && object->getRotation() == 0;
    auto objectBoundingBox = GeometryUtils::getBoundingBoxToWorld(object, ignoreTransoframtions);
    
    if(extendPercentValueX == 0 && extendPercentValueY == 0){
        return objectBoundingBox;
    }
    
    auto extendValueX = objectBoundingBox.size.width*extendPercentValueX/100.0;
    auto extendValueY = objectBoundingBox.size.height*extendPercentValueY/100.0;
    
    auto x = objectBoundingBox.origin.x;
    auto y = objectBoundingBox.origin.y;
    auto w = objectBoundingBox.size.width;
    auto h = objectBoundingBox.size.height;
    
    switch(extendDirX){
        case ExtendDirectionX::LEFT:
            x -= extendValueX;
            w += extendValueX;
            break;
        case ExtendDirectionX::RIGHT:
            w += extendValueX;
            break;
        case ExtendDirectionX::BOTH:
            x -= extendValueX/2;
            w += extendValueX;
            break;
    }
    
    switch(extendDirY){
        case ExtendDirectionY::DOWN:
            y -= extendValueY;
            h += extendValueY;
            break;
        case ExtendDirectionY::UP:
            h += extendValueY;
            break;
        case ExtendDirectionY::BOTH:
            y -= extendValueY/2;
            h += extendValueY;
            break;
    }
    
    return cocos2d::Rect(x, y, w, h);
}

bool MiscUtils::isNodeVisible(cocos2d::Node* node){
    bool ret = true;
    cocos2d::Node* parent = node;
    while (parent != nullptr) {
        if(!parent->isVisible()){
            ret = false;
            break;
        } else {
            parent = parent->getParent();
        }
    }
    return ret;
}

//without moving the position
void MiscUtils::changeAnchorPoint(cocos2d::Node* p_node, cocos2d::Point p_newAnchorPoint){
    auto originalAnchorPoint = p_node->getAnchorPoint();
    p_node->setAnchorPoint(p_newAnchorPoint);
    // when we change the anchor point, we also need to adjust the postion so the sprite stays at the same spot
    auto anchorPointChange = p_node->getAnchorPoint() - originalAnchorPoint;
    p_node->setPositionX(p_node->getPositionX() + anchorPointChange.x*p_node->getBoundingBox().size.width);
    p_node->setPositionY(p_node->getPositionY() + anchorPointChange.y*p_node->getBoundingBox().size.height);
}

std::string MiscUtils::boolToString(bool value)
{
    if(value){
        return "true";
    } else {
        return "false";
    }
}

std::string MiscUtils::clockMinSecTimeString(int seconds){
    auto mins = (int)floor(seconds/60);
    auto secs = seconds%60;
    std::string timeString = std::to_string(mins) + ":";
    if(secs < 10){
        timeString += "0" + std::to_string(secs);
    } else {
        timeString += std::to_string(secs);
    }
    return timeString;
}

//#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

static const int AlertViewTag = 999;

void MiscUtils::showAppCloseConfirmDialog(std::function<void()> onCancelCallback){

    if(cocos2d::Director::getInstance()->getRunningScene()->getChildByTag(AlertViewTag) == nullptr) {

        auto alpha = 220;

        auto message = "Are you sure you want to quit?"; //TODO
        auto okText = "Yes, bye!";
        auto cancelText = "No, let's play!";

        auto cancelColor = cocos2d::Color3B(68, 200, 220);
        auto okColor = cocos2d::Color3B(200, 100, 100);

        auto alert = AlertView::create(message, okText, cancelText, okColor, cancelColor, []() {

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
            cocos2d::Director::getInstance()->end();
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
            cocos2d::AudioEngine::stopAll();
            cocos2d::Director::getInstance()->pause();
            handler_nativeExitGame();
#else
            cocos2d::Director::getInstance()->end();
#endif
           // auto scene = HelloWorld::createScene();
           // cocos2d::Director::getInstance()->replaceScene(scene);


        }, 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(AlertViewTag);
        alert->setLocalZOrder(1000); //TODO make general zorders, named
        alert->setOpacity(0);
        cocos2d::Director::getInstance()->getRunningScene()->addChild(alert);
        alert->runAction(cocos2d::FadeTo::create(MiscUtils::StandardAnimationTime, alpha));
    }
}

bool MiscUtils::closeAppCloseConfirmDialogIfNecessary(){
    auto scene = cocos2d::Director::getInstance()->getRunningScene();
    if(scene != nullptr) {
        auto alertNode = scene->getChildByTag(AlertViewTag);
        if (alertNode != nullptr) {
            alertNode->removeFromParent();
            return true;
        }
    }
    return false;
}

//#endif