MCPcopy Create free account
hub / github.com/Open-GD/OpenGD / init

Method init

Source/AlertLayer.cpp:33–82  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

31USING_NS_AX;
32
33bool AlertLayer::init(std::string_view title, std::string_view desc, std::string_view btn1Str, std::string_view btn2Str, float width, std::function<void(Node*)> btn1Callback, std::function<void(Node*)> btn2Callback)
34{
35 if (!PopupLayer::init())
36 return false;
37
38 const auto& winSize = Director::getInstance()->getWinSize();
39
40 auto descLabel = Label::createWithBMFont(GameToolbox::getTextureString("chatFont.fnt"), desc, TextHAlignment::CENTER);
41 width = descLabel->getContentSize().width + 32;
42 // descLabel->setAnchorPoint({0.5, 1});
43 descLabel->setDimensions(width, 0);
44 descLabel->setPosition(Point(winSize / 2));
45 this->_mainLayer->addChild(descLabel);
46 auto descHeight = std::max(descLabel->getContentSize().height, 150.f);
47
48 auto bg = ui::Scale9Sprite::create(GameToolbox::getTextureString("square01_001.png"));
49 bg->setStretchEnabled(true);
50 bg->setContentSize({width, descHeight});
51 bg->setPosition(winSize / 2);
52 this->_mainLayer->addChild(bg, -1);
53
54 std::string goldFontStr = GameToolbox::getTextureString("goldFont.fnt");
55
56 auto titleLabel = Label::createWithBMFont(goldFontStr, title);
57 titleLabel->setAnchorPoint({0.5, 1.0});
58 titleLabel->setPosition({winSize.width / 2, (winSize.height - descHeight) / 2 + descHeight - 15});
59 titleLabel->setScale(0.9f);
60 this->_mainLayer->addChild(titleLabel);
61
62 auto menu = Menu::create();
63 this->_mainLayer->addChild(menu);
64 menu->setPositionY((winSize.height - descHeight) / 2 + 60);
65
66 auto _btnCallback = btn1Callback ? btn1Callback : [this](Node*) {this->close(); };
67
68 _btn1 = MenuItemSpriteExtra::create(ButtonSprite::create(btn1Str, 0x60, 0, 1), _btnCallback);
69 menu->addChild(_btn1);
70
71 if (!btn2Str.empty())
72 {
73 auto _btn2Callback = btn2Callback ? btn2Callback : [this](Node*) {this->close(); };
74 _btn2 = MenuItemSpriteExtra::create(ButtonSprite::create(btn2Str, 0x60, 0, 1), _btn2Callback);
75 menu->addChild(_btn2);
76
77 menu->alignItemsHorizontallyWithPadding(MIN((width - (_btn1->getContentSize().width + _btn2->getContentSize().width)) / 2, 30));
78 }
79 menu->setPositionY(descHeight - 32);
80
81 return true;
82}
83
84AlertLayer* AlertLayer::create(std::string_view title, std::string_view desc, std::string_view btn1, std::string_view btn2, float width, std::function<void(Node*)> btn1Callback, std::function<void(Node*)> btn2Callback)
85{

Callers 1

createMethod · 0.45

Calls 6

createFunction · 0.85
setPositionMethod · 0.80
setScaleMethod · 0.80
emptyMethod · 0.80
closeMethod · 0.45

Tested by

no test coverage detected