加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx源码:popup实现

发布时间:2020-12-14 17:09:55 所属栏目:百科 来源:网络整理
导读:头文件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::functionvoid(void) showEnd_t; public : Popup(); ~Popup(); b

头文件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();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读