#include "DuiHuanScene.h" #include "GlobalJosn.h" #include "GlobalUserInfo.h" #include "PopScene.h" #include "Utility.h" DuiHuanScene::DuiHuanScene() { #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) m_layerLoading = nullptr; m_sprLoading = nullptr; #endif } DuiHuanScene::~DuiHuanScene() { } bool DuiHuanScene::init() { if (!Node::init()) { return false; }; auto rootPanel = CSLoader::createNode("Platform/DuiHuanScene.csb"); CC_ASSERT(rootPanel != nullptr); this->addChild(rootPanel); m_ImageView = (ImageView*)rootPanel->getChildByName("Imagebg"); CC_ASSERT(nullptr != m_ImageView); m_ImageTop = (ImageView*)rootPanel->getChildByName("top"); CC_ASSERT(nullptr != m_ImageTop); //获取关闭按钮 auto btnClose = (Button*)m_ImageTop->getChildByName("btnClose"); CC_ASSERT(btnClose != nullptr); //注册关闭按钮事件 btnClose->addClickEventListener(CC_CALLBACK_1(DuiHuanScene::onClickClose, this)); //对手机返回键的监听 auto keyListener = EventListenerKeyboard::create(); //和回调函数绑定 keyListener->onKeyReleased = CC_CALLBACK_2(DuiHuanScene::onKeyReleased, this); //添加到事件分发器中 _eventDispatcher->addEventListenerWithSceneGraphPriority(keyListener, this); return true; } void DuiHuanScene::onEnter() { Node::onEnter(); } void DuiHuanScene::onEnterTransitionDidFinish() { Node::onEnterTransitionDidFinish(); } void DuiHuanScene::onExit() { Node::onExit(); } void DuiHuanScene::initRule() { cocos2d::log("DuiHuanScene::initRule()..."); auto webInfo = (Layout*)m_ImageView->getChildByName("info"); CC_ASSERT(webInfo != nullptr); const Size& size = webInfo->getContentSize(); CGlobalUserInfo * pGlobalUserInfo = CGlobalUserInfo::GetInstance(); tagGlobalUserData * pGlobalUserData = pGlobalUserInfo->GetGlobalUserData(); std::string strCode = utility::getIdentifyingCode(pGlobalUserInfo->getGameID()); std::string strTextUrl = GlobalJosn::getInstance()->m_strDuiHuanUrl + StringUtils::format("?gameid=%d&md5=%s", pGlobalUserData->dwGameID, strCode.c_str()); #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) auto label = Label::createWithSystemFont(strTextUrl, "", 26); label->setColor(Color3B(0, 0, 0)); label->setPosition(size / 2); webInfo->addChild(label); #else _webView = cocos2d::experimental::ui::WebView::create(); _webView->setPosition(size / 2); _webView->setContentSize(size); _webView->loadURL(strTextUrl); _webView->setScalesPageToFit(true); _webView->setOnShouldStartLoading(CC_CALLBACK_2(DuiHuanScene::onWebViewShouldStartLoading, this)); _webView->setOnDidFinishLoading(CC_CALLBACK_2(DuiHuanScene::onWebViewDidFinishLoading, this)); _webView->setOnDidFailLoading(CC_CALLBACK_2(DuiHuanScene::onWebViewDidFailLoading, this)); webInfo->addChild(_webView); //Loading层 m_layerLoading = LayerColor::create(Color4B(0, 0, 0, 50), size.width, size.height); m_layerLoading->setPosition(Vec2(0, 0)); webInfo->addChild(m_layerLoading); //加入loading图片 m_sprLoading = Sprite::create("common/Img/ico_loding.png"); m_sprLoading->setPosition(size / 2); m_layerLoading->addChild(m_sprLoading); m_sprLoading->runAction(RepeatForever::create(RotateBy::create(1.6f, 360))); #endif } void DuiHuanScene::pushScene() { float height = Director::getInstance()->getWinSize().height; if (m_ImageTop) { float top_h = m_ImageTop->getContentSize().height; m_ImageTop->setPositionY(height + top_h/2); m_ImageTop->runAction(MoveBy::create(0.2f, Vec2(0, 0 - top_h))); } if (m_ImageView) { float b_h = m_ImageView->getContentSize().height - 20; m_ImageView->setPositionY(0); m_ImageView->setOpacity(0); auto act = MoveBy::create(0.2f, Vec2(0, b_h / 2)); auto spawn = Spawn::create(FadeIn::create(0.2f), act, nullptr); m_ImageView->runAction(Sequence::create(spawn, CallFunc::create([&]() { this->initRule(); }), nullptr)); } } void DuiHuanScene::popScene() { float height = Director::getInstance()->getWinSize().height; if (m_ImageTop) { float top_h = m_ImageTop->getContentSize().height; m_ImageTop->runAction(MoveBy::create(0.2f, Vec2(0, top_h))); } if (m_ImageView) { float b_h = m_ImageView->getContentSize().height - 20; auto act = MoveBy::create(0.2f, Vec2(0, 0 - b_h/2)); auto spawn = Spawn::create(FadeOut::create(0.2f), act, nullptr); m_ImageView->runAction(Sequence::create(spawn, CallFunc::create([&]() { this->removeFromParent(); }), nullptr)); } } // 关闭弹窗; void DuiHuanScene::onClickClose(Ref*) { YSAudioEngine::Instance().playBtnClickEffect(); this->popScene(); } //按键处理 void DuiHuanScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event * pEvent) { //返回值处理 if (keyCode == EventKeyboard::KeyCode::KEY_ESCAPE) { pEvent->stopPropagation(); onClickClose(nullptr); } } #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) bool DuiHuanScene::onWebViewShouldStartLoading(experimental::ui::WebView *sender, const std::string &url) { CCLOG("onWebViewShouldStartLoading, url is %s", url.c_str()); //don't do any OpenGL operation here!! It's forbidden! return true; } void DuiHuanScene::onWebViewDidFinishLoading(experimental::ui::WebView *sender, const std::string &url) { CCLOG("onWebViewDidFinishLoading, url is %s", url.c_str()); hideLoadingLayer(); } void DuiHuanScene::onWebViewDidFailLoading(experimental::ui::WebView *sender, const std::string &url) { CCLOG("onWebViewDidFailLoading, url is %s", url.c_str()); hideLoadingLayer(); } void DuiHuanScene::hideLoadingLayer() { if (m_layerLoading != nullptr) { if (m_sprLoading != nullptr) { m_sprLoading->stopAllActions(); } m_layerLoading->removeFromParent(); } } #endif