AniAlertUtils.cpp
2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// 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