AniAlertView.cpp
5.03 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// 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));
}