From 8270355ad6a851736ab90d25eab26abbcaa19495 Mon Sep 17 00:00:00 2001 From: Sergey Date: Wed, 3 Dec 2014 22:17:47 +0300 Subject: [PATCH] blending function for Scale9Sprite --- cocos/ui/UIScale9Sprite.cpp | 54 +++++++++++++++++++++++++++++++++++++ cocos/ui/UIScale9Sprite.h | 24 ++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/cocos/ui/UIScale9Sprite.cpp b/cocos/ui/UIScale9Sprite.cpp index 32562578aa..91d3caf464 100644 --- a/cocos/ui/UIScale9Sprite.cpp +++ b/cocos/ui/UIScale9Sprite.cpp @@ -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) diff --git a/cocos/ui/UIScale9Sprite.h b/cocos/ui/UIScale9Sprite.h index 2fd95c0e93..04ff5bd852 100644 --- a/cocos/ui/UIScale9Sprite.h +++ b/cocos/ui/UIScale9Sprite.h @@ -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;