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

#include "ParentalGateShowInterface.h"
#include "MiscUtils.h"
#include "ParentalGateView.h"

static int ParentalGateViewTag = 90;

void ParentalGateShowInterface::presentParentalGate(std::function<void()> onSuccessCallback, std::function<void()> onFailureCallback){
    _isPresentingParentalGate = true;
    auto screenSize = cocos2d::Director::getInstance()->getWinSize();
    auto parentalGate = ParentalGateView::create(screenSize.width, screenSize.height, onSuccessCallback, onFailureCallback);
    parentalGate->setTag(ParentalGateViewTag);
    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->stopAllActions();
    parentalGate->setOpacity(0);
    parentalGate->runAction(cocos2d::FadeIn::create(MiscUtils::StandardAnimationTime));
}

void ParentalGateShowInterface::hideParentalGate(){
    _isPresentingParentalGate = false;
    auto parentalGate = PGSI_getChildByTag(ParentalGateViewTag);
    if(parentalGate != nullptr){
        parentalGate->stopAllActions();
        PGSI_getEventDispatcher()->removeEventListenersForTarget(parentalGate);
        MiscUtils::hideAndRemoveView(parentalGate, true);
    }
}