cocos2dx原始碼:popup實現
標頭檔案Popup.h
#ifndef _POPUP_H_
#define _POPUP_H_
#include "cocos2d.h"
#include "GmbsCocos.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
class Popup : public LayerColor
{
//typedef std::function<void(void)> showEnd_t;
public:
Popup();
~Popup();
bool create(Size &size, const char* bgFileName, bool autorelease = true);
virtual bool init();
void show(float seconds, bool removedWhenShowEnd = true, std::function<void(void)> showEndCallback = nullptr);
void show01(float seconds, bool removedWhenShowEnd = true, std::function<void(void)> showEndCallback = nullptr);
void showEnd();
void showWithAction(ActionInterval* action, bool removedWhenShowEnd, std::function<void(void)> showEndCallback);
void setVisible(bool visible);
void setHideCallback(std::function<void(void)> showEndCallback)
{
m_showEndCallback = showEndCallback;
}
void setHideWhenTouch(bool hideWhenTouch)
{
m_hideWhenTouch = hideWhenTouch;
}
protected:
virtual bool onTouchBegan(Touch *pTouch, Event *pEvent);
EventListenerTouchOneByOne* m_touchListener;
ui::Scale9Sprite* m_background;
std::function<void(void)> m_showEndCallback;
bool m_removedWhenShowEnd;
bool m_hideWhenTouch;
};
#endif
原始檔Popup.cpp
#include "Popup.h"
Popup::Popup()
{
m_touchListener = nullptr;
m_showEndCallback = nullptr;
m_hideWhenTouch = false;
m_removedWhenShowEnd = false;
}
Popup::~Popup()
{
}
bool Popup::create(Size &size, const char* bgFileName, bool autorelease)
{
if (!this->initWithColor(Color4B(0, 0, 0, 0)))
return false;
if (autorelease)
this->autorelease();
this->setContentSize(size);
m_background = nullptr;
if (bgFileName != nullptr)
{
GmbsPoint pt;
m_background = ui::Scale9Sprite::create(bgFileName);
this->addChild(m_background);
m_background->setAnchorPoint(Vec2(0.5, 0.5));
m_background->setContentSize(size);
m_background->setColor(Color3B(255, 133, 52));
m_background->setOpacity(220);
pt.reset(m_background);
pt.xMiddleAlign(this).yMiddleAlign(this);
m_background->setPosition(pt);
Size frameSize(size.width + 4, size.height + 4);
ui::Scale9Sprite* frame = ui::Scale9Sprite::create(bgFileName);
this->addChild(frame, -1);
frame->setAnchorPoint(Vec2(0.5, 0.5));
frame->setContentSize(frameSize);
frame->setColor(Color3B(255, 255, 255));
pt.reset(frame);
pt.xMiddleAlign(m_background).yMiddleAlign(m_background);
frame->setPosition(pt);
}
setVisible(true);
return this->init();
}
bool Popup::init()
{
return true;
}
void Popup::setVisible(bool visible)
{
if (visible && !m_touchListener)
{
m_touchListener = EventListenerTouchOneByOne::create();
m_touchListener->onTouchBegan = CC_CALLBACK_2(Popup::onTouchBegan, this);;
m_touchListener->setSwallowTouches(true);
_eventDispatcher->addEventListenerWithSceneGraphPriority(m_touchListener, this);
}
else if (!visible && m_touchListener)
{
_eventDispatcher->removeEventListener(m_touchListener);
m_touchListener = nullptr;
}
LayerColor::setVisible(visible);
}
bool Popup::onTouchBegan(Touch *pTouch, Event *pEvent)
{
if (m_hideWhenTouch)
{
Size size = getContentSize();
Rect boundingRect(0, 0, size.width, size.height);
Point touchPoint = convertTouchToNodeSpace(pTouch);
if (!boundingRect.containsPoint(touchPoint))
{
showEnd();
}
}
return true;
}
void Popup::show(float seconds, bool removedWhenShowEnd, std::function<void(void)> showEndCallback)
{
auto action = Sequence::create(
DelayTime::create(seconds),
CallFunc::create(CC_CALLBACK_0(Popup::showEnd, this)), nullptr);
this->showWithAction(action, removedWhenShowEnd, showEndCallback);
}
void Popup::show01(float seconds, bool removedWhenShowEnd, std::function<void(void)> showEndCallback)
{
setScale(0);
auto action = Sequence::create(
ScaleTo::create(0.8f, 1.25f),
DelayTime::create(seconds),
CallFunc::create(CC_CALLBACK_0(Popup::showEnd, this)), nullptr);
this->showWithAction(action, removedWhenShowEnd, showEndCallback);
}
void Popup::showWithAction(ActionInterval* action, bool removedWhenShowEnd, std::function<void(void)> showEndCallback)
{
this->stopActionByTag(1);
action->setTag(1);
this->runAction(action);
setVisible(true);
m_removedWhenShowEnd = removedWhenShowEnd;
m_showEndCallback = showEndCallback;
}
void Popup::showEnd()
{
setVisible(false);
if (m_showEndCallback)
m_showEndCallback();
if (m_removedWhenShowEnd)
this->removeFromParent();
}
相關文章
- 元件 popup 設計和原始碼剖析元件原始碼
- cocos2dx原始碼:相框PhotoFrame原始碼
- cocos2dx 很好的原始碼分析博文原始碼
- cocos2dx原始碼:背景層封裝類原始碼封裝
- Promise原始碼實現Promise原始碼
- Cocos2dx實現象棋之佈局
- HashMap原始碼實現分析HashMap原始碼
- webpack Hmr 原始碼實現Web原始碼
- 仿Express原始碼實現(-)Express原始碼
- PostgreSQL MVCC 原始碼實現SQLMVC原始碼
- Axios 原始碼解讀 —— 原始碼實現篇iOS原始碼
- 鴻蒙HarmonyOS實戰-ArkUI元件(Popup)鴻蒙UI元件
- Vue.js 原始碼實現Vue.js原始碼
- Promise實現原理(附原始碼)Promise原始碼
- 紅黑樹原始碼實現原始碼
- cocos2dx實現經典飛機大戰
- 70行實現Promise核心原始碼Promise原始碼
- 深入原始碼解析 tapable 實現原理原始碼
- 原始碼|ThreadLocal的實現原理原始碼thread
- HashMap 實現原理與原始碼分析HashMap原始碼
- HashMap實現原理及原始碼分析HashMap原始碼
- Vue.watche 原始碼實現Vue原始碼
- RTree原始碼——C語言實現原始碼C語言
- spring原始碼解析 (七) 事務底層原始碼實現Spring原始碼
- weex元件-popup元件
- mongodb核心原始碼實現及效能優化系列:Mongodb write寫(增、刪、改)模組原始碼實現MongoDB原始碼優化
- 手動實現一個promise(原始碼)Promise原始碼
- 原始碼解析.Net中DependencyInjection的實現原始碼
- 從kratos分析BBR限流原始碼實現原始碼
- Vue原始碼探究-核心類的實現Vue原始碼
- 【React原始碼解讀】- 元件的實現React原始碼元件
- Netty原始碼解析 -- PoolSubpage實現原理Netty原始碼
- 《Spring原始碼分析》IOC的實現Spring原始碼
- synchronized實現原理及ReentrantLock原始碼synchronizedReentrantLock原始碼
- Netty原始碼解析 -- PoolChunk實現原理Netty原始碼
- musl中strlen原始碼實現和分析原始碼
- TCC-Transaction 原始碼分析 —— TCC 實現原始碼
- 從原始碼解讀Category實現原理原始碼Go