头文件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,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,bool autorelease)
{
if (!this->initWithColor(Color4B(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,size.width,size.height);
Point touchPoint = convertTouchToNodeSpace(pTouch);
if (!boundingRect.containsPoint(touchPoint))
{
showEnd();
}
}
return true;
}
void Popup::show(float seconds,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,std::function<void(void)> showEndCallback)
{
setScale(0);
auto action = Sequence::create(
ScaleTo::create(0.8f,1.25f),DelayTime::create(seconds),showEndCallback);
}
void Popup::showWithAction(ActionInterval* action,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();
}