#include "PopScene.h" SINGLETON_STORAGE(PopScene); PopScene::PopScene():m_fnOKHander(nullptr), m_fnCancelHander(nullptr) { m_ImgBg = nullptr; init(); } PopScene::~PopScene() { } bool PopScene::init() { if (!Node::init()) { return false; }; auto rootPanel = CSLoader::createNode("Platform/PopScene.csb"); CC_ASSERT(rootPanel != nullptr); this->addChild(rootPanel); //获取背景节点 m_ImgBg = (ImageView*)rootPanel->getChildByName("imgBg"); CC_ASSERT(m_ImgBg != nullptr); m_fWndWidth = m_ImgBg->getContentSize().width; ////获取关闭按钮 //auto btnClose = (Button*)pImgBg->getChildByName("btnClose"); //CC_ASSERT(btnClose != nullptr); // ////注册关闭按钮事件 //btnClose->addClickEventListener([this](Ref*){ // YSAudioEngine::Instance().playBtnClickEffect(); // this->removeFromParent(); //}); //获取确定按钮 m_btnOK = (Button*)m_ImgBg->getChildByName("btnOK"); CC_ASSERT(m_btnOK != nullptr); //注册确定按钮事件 m_btnOK->addClickEventListener(CC_CALLBACK_1(PopScene::onClickOK, this)); //获取取消按钮 m_btnCancel = (Button*)m_ImgBg->getChildByName("btnCancel"); CC_ASSERT(m_btnCancel != nullptr); //注册取消按钮事件 m_btnCancel->addClickEventListener(CC_CALLBACK_1(PopScene::onClickCancel, this)); //获取取消按钮 m_txtContent = (Text*)m_ImgBg->getChildByName("txtContent"); CC_ASSERT(m_txtContent != nullptr); return true; } void PopScene::onEnter() { Node::onEnter(); } void PopScene::onEnterTransitionDidFinish() { Node::onEnterTransitionDidFinish(); } void PopScene::onExit() { Node::onExit(); } void PopScene::show(std::string strContent, const std::function& fnOKHander) { //如果没有被加入场景则加入当前运行场景; if (getParent() == nullptr) { Scene* pScene = Director::getInstance()->getRunningScene(); pScene->addChild(this, 999); } cocos2d::log("%s", strContent.c_str()); m_txtContent->setString(strContent); m_fnOKHander = fnOKHander; m_fnCancelHander = nullptr; m_btnCancel->setVisible(false); m_btnOK->setVisible(true); float fButnWidth = m_btnOK->getContentSize().width; m_btnOK->setPositionX((m_fWndWidth - fButnWidth) / 2.f); this->pushScene(); } void PopScene::show(std::string strContent, const std::function& fnOKHander, const std::function& fnCancelHander) { if (getParent() == nullptr) { Scene* pScene = Director::getInstance()->getRunningScene(); pScene->addChild(this, 999); } m_txtContent->setString(strContent); m_fnOKHander = fnOKHander; m_fnCancelHander = fnCancelHander; m_btnCancel->setVisible(true); m_btnOK->setVisible(true); float fButnWidth = m_btnOK->getContentSize().width; m_btnOK->setPositionX((m_fWndWidth / 2.0f - fButnWidth) / 2.0f); m_btnCancel->setPositionX(m_fWndWidth / 2.0f + (m_fWndWidth / 2.0f - fButnWidth) / 2.0f); this->pushScene(); } void PopScene::onClickCancel(Ref*) { YSAudioEngine::Instance().playBtnClickEffect(); if (m_fnCancelHander) { m_fnCancelHander(); } this->removeFromParent(); } void PopScene::onClickOK(Ref*) { YSAudioEngine::Instance().playBtnClickEffect(); if (m_fnOKHander) { m_fnOKHander(); } this->removeFromParent(); } void PopScene::pushScene() { if (m_ImgBg) { m_ImgBg->setScale(0.8f); m_ImgBg->runAction(Sequence::create(ScaleTo::create(0.2f, 1.2f), ScaleTo::create(0.1f, 1.f), nullptr)); } } void PopScene::popScene() { if (m_ImgBg) { m_ImgBg->runAction(Sequence::create(Hide::create(), ScaleTo::create(0.1f, 0.1f), CallFunc::create([&]() { this->removeFromParent(); }), nullptr)); } }