cocos2dx原始碼:相框PhotoFrame

峻峰飛陽發表於2016-08-03

標頭檔案PhotoFrame.h

#ifndef _PHOTOFRAME_H_
#define _PHOTOFRAME_H_

#include "cocos2d.h"
#include "GmbsCocos.h"
#include "extensions/cocos-ext.h"

using namespace cocos2d::ui;

NS_CC_BEGIN

class PhotoFrame : public LayerColor
{
public:
    ~PhotoFrame();

    static PhotoFrame* create(const char* filename, const Color4B& color = Color4B(255, 255, 255, 0));
    virtual bool init(const char* filename);

    void decorate(Node* photo, float horiz = 0, float vert = 0);

    void inflate(float wInf, float hInf);

protected:
    ui::Scale9Sprite* m_frameSprite;

};

NS_CC_END

#endif

cpp檔案PhotoFrame.cpp

#include "PhotoFrame.h"

NS_CC_BEGIN

PhotoFrame::~PhotoFrame()
{

}

PhotoFrame* PhotoFrame::create(const char* filename, const Color4B& color)
{
    PhotoFrame* pobLayer = new PhotoFrame();
    if (pobLayer && pobLayer->initWithColor(color) && pobLayer->init(filename))
    {
        pobLayer->autorelease();
        return pobLayer;
    }
    else
    {
        CC_SAFE_DELETE(pobLayer);
        return NULL;
    }
}

bool PhotoFrame::init(const char* filename)
{
    m_frameSprite =  ui::Scale9Sprite::create(filename);
    this->addChild(m_frameSprite);

    return true;
}

void PhotoFrame::decorate(Node* photo, float wInf, float hInf)
{
    photo->addChild(this);

    Size size = photo->getContentSize();
    size.width += wInf;
    size.height += hInf;
    this->setContentSize(size);
    m_frameSprite->setContentSize(size);

    GmbsPoint pt;
    pt.reset(m_frameSprite);
    pt.xMiddleAlign(this).yMiddleAlign(this);
    m_frameSprite->setPosition(pt);

    pt.reset(this);
    pt.xMiddleAlign(photo).yMiddleAlign(photo);
    this->setPosition(pt);
}

void PhotoFrame::inflate(float wInf, float hInf)
{
    Size size = this->getContentSize();
    size.width += wInf;
    size.height += hInf;
    this->setContentSize(size);
    m_frameSprite->setContentSize(size);

    GmbsPoint pt;
    Node* photo = getParent();
    pt.reset(m_frameSprite);
    pt.xMiddleAlign(photo).yMiddleAlign(photo);
    m_frameSprite->setPosition(pt);

    pt.reset(this);
    pt.xMiddleAlign(photo).yMiddleAlign(photo);
    this->setPosition(pt);
}

NS_CC_END

相關文章