HParentalGateShowInterface.cpp 2.33 KB
//
//  HParentalGateShowInterface.cpp
//  Steve and Maggie Halloween
//
//  Created by Katarzyna Kalinowska-Górska on 14/10/2019.
//

#include "HParentalGateShowInterface.h"
#include "HMiscUtils.h"
#include "HParentalGateView.h"

static int HParentalGateViewTag = 90;

void HParentalGateShowInterface::presentParentalGate(std::function<void()> onSuccessCallback, std::function<void()> onFailureCallback){
    _isPresentingParentalGate = true;
    auto screenSize = cocos2d::Director::getInstance()->getWinSize();
    auto parentalGate = HParentalGateView::create(screenSize.width, screenSize.height, onSuccessCallback, onFailureCallback);
    parentalGate->setTag(HParentalGateViewTag);
    PGSI_addChild(parentalGate);
    parentalGate->setLocalZOrder(1000);
    
    parentalGate->setPosition(cocos2d::Vec2(0, 0));
    parentalGate->setCascadeOpacityEnabled(true);
    
    auto touchListener = cocos2d::EventListenerTouchOneByOne::create();
    touchListener->setSwallowTouches(true);
    touchListener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event){
        return true;
    };
    PGSI_getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, parentalGate);
    //    #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    //        auto keyboardListener = cocos2d::EventListenerKeyboard::create();
    //        keyboardListener->onKeyReleased = [&](cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event){
    //            if(keyCode == cocos2d::EventKeyboard::KeyCode::KEY_BACK){
    //                hideParentalGate();
    //            }
    //        };
    //        _eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, parentalGate);
    //    #endif
    
    parentalGate->setOpacity(0);
    parentalGate->runAction(cocos2d::FadeIn::create(HMiscUtils::StandardAnimationTime));
}

void HParentalGateShowInterface::hideParentalGate(){
    _isPresentingParentalGate = false;
    auto parentalGate = PGSI_getChildByTag(HParentalGateViewTag);
    if(parentalGate != nullptr){
        parentalGate->runAction(cocos2d::Sequence::create(cocos2d::FadeOut::create(HMiscUtils::StandardAnimationTime),
                                                          cocos2d::CallFunc::create(std::bind([&](cocos2d::Node* pg){
            HMiscUtils::hideAndRemoveView(pg, true);
        }, parentalGate)), nullptr));
    }
}