blending function for Scale9Sprite

This commit is contained in:
Sergey 2014-12-03 22:17:47 +03:00
parent e792b6aa9a
commit 8270355ad6
2 changed files with 77 additions and 1 deletions

View File

@ -158,6 +158,56 @@ namespace ui {
auto sprite = Sprite::createWithTexture(batchnode->getTexture());
return init(sprite, rect, false, capInsets);
}
void Scale9Sprite::setBlendFunc(const BlendFunc &blendFunc)
{
_blendFunc = blendFunc;
applyBlendFunc();
}
const BlendFunc &Scale9Sprite::getBlendFunc() const
{
return _blendFunc;
}
void Scale9Sprite::updateBlendFunc(Texture2D *texture)
{
// it is possible to have an untextured sprite
if (! texture || ! texture->hasPremultipliedAlpha())
{
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
setOpacityModifyRGB(false);
}
else
{
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
setOpacityModifyRGB(true);
}
}
void Scale9Sprite::applyBlendFunc()
{
if(_scale9Image)
_scale9Image->setBlendFunc(_blendFunc);
if(_topLeftSprite)
_topLeftSprite->setBlendFunc(_blendFunc);
if(_topSprite)
_topSprite->setBlendFunc(_blendFunc);
if(_topRightSprite)
_topRightSprite->setBlendFunc(_blendFunc);
if(_leftSprite)
_leftSprite->setBlendFunc(_blendFunc);
if(_centerSprite)
_centerSprite->setBlendFunc(_blendFunc);
if(_rightSprite)
_rightSprite->setBlendFunc(_blendFunc);
if(_bottomLeftSprite)
_bottomLeftSprite->setBlendFunc(_blendFunc);
if(_bottomSprite)
_bottomSprite->setBlendFunc(_blendFunc);
if(_bottomRightSprite)
_bottomRightSprite->setBlendFunc(_blendFunc);
}
bool Scale9Sprite::updateWithBatchNode(cocos2d::SpriteBatchNode *batchnode, const cocos2d::Rect &originalRect, bool rotated, const cocos2d::Rect &capInsets)
{
@ -193,6 +243,8 @@ namespace ui {
this->cleanupSlicedSprites();
_protectedChildren.clear();
updateBlendFunc(sprite?sprite->getTexture():nullptr);
if(nullptr != sprite)
{
if (nullptr == sprite->getSpriteFrame())
@ -255,6 +307,8 @@ namespace ui {
this->createSlicedSprites();
}
applyBlendFunc();
this->setContentSize(size);
if (_spritesGenerated)

View File

@ -45,7 +45,7 @@ namespace ui {
* Then you could call any methods of Sprite class with the return pointers.
*
*/
class CC_GUI_DLL Scale9Sprite : public Node
class CC_GUI_DLL Scale9Sprite : public Node, public cocos2d::BlendProtocol
{
public:
/**
@ -243,6 +243,25 @@ namespace ui {
virtual bool init(Sprite* sprite, const Rect& rect, bool rotated, const Vec2 &offset, const Size &originalSize, const Rect& capInsets);
CC_DEPRECATED_ATTRIBUTE virtual bool initWithBatchNode(SpriteBatchNode* batchnode, const Rect& rect, bool rotated, const Rect& capInsets);
CC_DEPRECATED_ATTRIBUTE virtual bool initWithBatchNode(SpriteBatchNode* batchnode, const Rect& rect, const Rect& capInsets);
/**
* Sets the source blending function.
*
* @param blendFunc A structure with source and destination factor to specify pixel arithmetic,
* e.g. {GL_ONE, GL_ONE}, {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA}.
* @js NA
* @lua NA
*/
virtual void setBlendFunc(const BlendFunc &blendFunc) override;
/**
* Returns the blending function that is currently being used.
*
* @return A BlendFunc structure with source and destination factor which specified pixel arithmetic.
* @js NA
* @lua NA
*/
virtual const BlendFunc &getBlendFunc() const override;
/**
* Creates and returns a new sprite object with the specified cap insets.
@ -383,6 +402,8 @@ namespace ui {
void createSlicedSprites();
void cleanupSlicedSprites();
void adjustScale9ImagePosition();
void applyBlendFunc();
void updateBlendFunc(Texture2D *texture);
/**
* Sorts the children array once before drawing, instead of every time when a child is added or reordered.
* This approach can improves the performance massively.
@ -408,6 +429,7 @@ namespace ui {
Sprite* _bottomRightSprite;
bool _scale9Enabled;
BlendFunc _blendFunc;
Size _topLeftSize;
Size _centerSize;