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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

峻峰飞阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值