axmol/cocos/ui/UIScale9Sprite.cpp

1312 lines
40 KiB
C++
Raw Normal View History

2014-07-29 10:26:53 +08:00
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
2014-07-29 10:26:53 +08:00
http://www.cocos2d-x.org
2014-07-29 10:26:53 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2014-07-29 10:26:53 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2014-07-29 10:26:53 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2014-07-29 10:26:53 +08:00
#include "UIScale9Sprite.h"
2014-07-29 10:39:38 +08:00
#include "2d/CCSprite.h"
#include "2d/CCSpriteFrameCache.h"
#include "base/CCVector.h"
#include "base/CCDirector.h"
2014-12-23 15:03:31 +08:00
#include "renderer/CCGLProgram.h"
#include "renderer/ccShaders.h"
#include "platform/CCImage.h"
#include "base/CCNinePatchImageParser.h"
2014-07-29 10:39:38 +08:00
NS_CC_BEGIN
namespace ui {
2014-07-29 10:39:38 +08:00
Scale9Sprite::Scale9Sprite()
: _spritesGenerated(false)
, _spriteFrameRotated(false)
, _scale9Image(nullptr)
, _scale9Enabled(true)
, _insetLeft(0)
, _insetTop(0)
, _insetRight(0)
, _insetBottom(0)
,_flippedX(false)
,_flippedY(false)
,_isPatch9(false)
,_brightState(State::NORMAL)
,_nonSliceSpriteAnchor(Vec2::ANCHOR_MIDDLE)
2015-10-29 10:41:58 +08:00
,_sliceVertices(nullptr)
,_sliceIndices(nullptr)
2015-10-29 21:53:58 +08:00
,_sliceSpriteDirty(false)
2015-10-29 23:13:36 +08:00
,_renderingType(RenderingType::SLICE)
2014-07-29 10:39:38 +08:00
{
this->setAnchorPoint(Vec2(0.5,0.5));
}
2014-07-29 10:39:38 +08:00
Scale9Sprite::~Scale9Sprite()
{
this->cleanupSlicedSprites();
CC_SAFE_RELEASE(_scale9Image);
}
bool Scale9Sprite::initWithFile(const Rect& capInsets, const std::string& file)
{
bool pReturn = this->initWithFile(file, Rect::ZERO, capInsets);
return pReturn;
}
bool Scale9Sprite::initWithFile(const std::string& file)
{
bool pReturn = this->initWithFile(file, Rect::ZERO);
return pReturn;
}
bool Scale9Sprite::initWithSpriteFrame(SpriteFrame* spriteFrame,
const Rect& capInsets)
{
Texture2D* texture = spriteFrame->getTexture();
CCASSERT(texture != NULL, "CCTexture must be not nil");
Sprite *sprite = Sprite::createWithSpriteFrame(spriteFrame);
CCASSERT(sprite != NULL, "sprite must be not nil");
bool pReturn = this->init(sprite,
spriteFrame->getRect(),
spriteFrame->isRotated(),
spriteFrame->getOffset(),
spriteFrame->getOriginalSize(),
capInsets);
return pReturn;
}
bool Scale9Sprite::initWithSpriteFrame(SpriteFrame* spriteFrame)
{
CCASSERT(spriteFrame != NULL, "Invalid spriteFrame for sprite");
bool pReturn = this->initWithSpriteFrame(spriteFrame, Rect::ZERO);
return pReturn;
}
bool Scale9Sprite::initWithSpriteFrameName(const std::string& spriteFrameName,
const Rect& capInsets)
{
CCASSERT((SpriteFrameCache::getInstance()) != NULL,
"SpriteFrameCache::getInstance() must be non-NULL");
SpriteFrame *frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFrameName);
CCASSERT(frame != NULL, "CCSpriteFrame must be non-NULL");
if (NULL == frame) return false;
bool pReturn = this->initWithSpriteFrame(frame, capInsets);
return pReturn;
}
bool Scale9Sprite::initWithSpriteFrameName(const std::string& spriteFrameName)
{
bool pReturn = this->initWithSpriteFrameName(spriteFrameName, Rect::ZERO);
return pReturn;
}
bool Scale9Sprite::init()
{
return this->init(NULL, Rect::ZERO, Rect::ZERO);
}
bool Scale9Sprite::init(Sprite* sprite, const Rect& rect, const Rect& capInsets)
{
return this->init(sprite, rect, false, capInsets);
}
bool Scale9Sprite::init(Sprite* sprite,
const Rect& rect,
bool rotated,
const Rect& capInsets)
{
return init(sprite, rect, rotated, Vec2::ZERO, rect.size, capInsets);
}
bool Scale9Sprite::init(Sprite* sprite,
const Rect& rect,
bool rotated,
const Vec2 &offset,
const Size &originalSize,
const Rect& capInsets)
{
bool ret = true;
if(sprite)
{
auto texture = sprite->getTexture();
auto spriteFrame = sprite->getSpriteFrame();
Rect actualCapInsets = capInsets;
if (texture->isContain9PatchInfo())
{
auto& parsedCapInset = texture->getSpriteFrameCapInset(spriteFrame);
if(!parsedCapInset.equals(Rect::ZERO))
{
this->_isPatch9 = true;
if(capInsets.equals(Rect::ZERO))
{
actualCapInsets = parsedCapInset;
}
}
}
ret = this->updateWithSprite(sprite,
rect,
rotated,
offset,
originalSize,
actualCapInsets);
}
return ret;
}
bool Scale9Sprite::initWithBatchNode(cocos2d::SpriteBatchNode *batchnode,
const cocos2d::Rect &rect,
bool rotated,
const cocos2d::Rect &capInsets)
{
Sprite *sprite = Sprite::createWithTexture(batchnode->getTexture());
return init(sprite, rect, rotated, capInsets);
}
bool Scale9Sprite::initWithBatchNode(cocos2d::SpriteBatchNode *batchnode,
const cocos2d::Rect &rect,
const cocos2d::Rect &capInsets)
{
auto sprite = Sprite::createWithTexture(batchnode->getTexture());
return init(sprite, rect, false, capInsets);
}
bool Scale9Sprite::initWithFile(const std::string& file,
const Rect& rect,
const Rect& capInsets)
{
Sprite *sprite = nullptr;
sprite = Sprite::create(file);
bool pReturn = this->init(sprite, rect, capInsets);
return pReturn;
}
bool Scale9Sprite::initWithFile(const std::string& file, const Rect& rect)
{
bool pReturn = this->initWithFile(file, rect, Rect::ZERO);
return pReturn;
}
Scale9Sprite* Scale9Sprite::create()
{
Scale9Sprite *pReturn = new (std::nothrow) Scale9Sprite();
if (pReturn && pReturn->init())
{
pReturn->autorelease();
return pReturn;
}
CC_SAFE_DELETE(pReturn);
return NULL;
}
Scale9Sprite* Scale9Sprite::create(const std::string& file,
const Rect& rect,
const Rect& capInsets)
{
Scale9Sprite* pReturn = new (std::nothrow) Scale9Sprite();
if ( pReturn && pReturn->initWithFile(file, rect, capInsets) )
{
pReturn->autorelease();
return pReturn;
}
CC_SAFE_DELETE(pReturn);
return NULL;
}
Scale9Sprite* Scale9Sprite::create(const std::string& file, const Rect& rect)
{
Scale9Sprite* pReturn = new (std::nothrow) Scale9Sprite();
if ( pReturn && pReturn->initWithFile(file, rect) )
{
pReturn->autorelease();
return pReturn;
}
CC_SAFE_DELETE(pReturn);
return NULL;
}
Scale9Sprite* Scale9Sprite::create(const Rect& capInsets,
const std::string& file)
{
Scale9Sprite* pReturn = new (std::nothrow) Scale9Sprite();
if ( pReturn && pReturn->initWithFile(capInsets, file) )
{
pReturn->autorelease();
return pReturn;
}
CC_SAFE_DELETE(pReturn);
return NULL;
}
Scale9Sprite* Scale9Sprite::create(const std::string& file)
{
Scale9Sprite* pReturn = new (std::nothrow) Scale9Sprite();
if ( pReturn && pReturn->initWithFile(file) )
{
pReturn->autorelease();
return pReturn;
}
CC_SAFE_DELETE(pReturn);
return NULL;
}
Scale9Sprite* Scale9Sprite::createWithSpriteFrame(SpriteFrame* spriteFrame,
const Rect& capInsets)
{
Scale9Sprite* pReturn = new (std::nothrow) Scale9Sprite();
if ( pReturn && pReturn->initWithSpriteFrame(spriteFrame, capInsets) )
{
pReturn->autorelease();
return pReturn;
}
CC_SAFE_DELETE(pReturn);
return NULL;
}
Scale9Sprite* Scale9Sprite::createWithSpriteFrame(SpriteFrame* spriteFrame)
{
Scale9Sprite* pReturn = new (std::nothrow) Scale9Sprite();
if ( pReturn && pReturn->initWithSpriteFrame(spriteFrame) )
{
pReturn->autorelease();
return pReturn;
}
CC_SAFE_DELETE(pReturn);
return NULL;
}
Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const std::string& spriteFrameName,
const Rect& capInsets)
{
Scale9Sprite* pReturn = new (std::nothrow) Scale9Sprite();
if ( pReturn && pReturn->initWithSpriteFrameName(spriteFrameName, capInsets) )
{
pReturn->autorelease();
return pReturn;
}
CC_SAFE_DELETE(pReturn);
return NULL;
}
Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const std::string& spriteFrameName)
{
Scale9Sprite* pReturn = new (std::nothrow) Scale9Sprite();
if ( pReturn && pReturn->initWithSpriteFrameName(spriteFrameName) )
{
pReturn->autorelease();
return pReturn;
}
CC_SAFE_DELETE(pReturn);
log("Could not allocate Scale9Sprite()");
return NULL;
}
2014-07-29 10:39:38 +08:00
void Scale9Sprite::cleanupSlicedSprites()
{
2015-10-29 10:41:58 +08:00
CC_SAFE_DELETE_ARRAY(_sliceIndices);
CC_SAFE_DELETE_ARRAY(_sliceVertices);
2014-07-29 10:39:38 +08:00
}
2014-12-04 03:17:47 +08:00
void Scale9Sprite::setBlendFunc(const BlendFunc &blendFunc)
{
_blendFunc = blendFunc;
applyBlendFunc();
}
const BlendFunc &Scale9Sprite::getBlendFunc() const
{
return _blendFunc;
}
2014-12-04 03:17:47 +08:00
void Scale9Sprite::updateBlendFunc(Texture2D *texture)
{
2014-12-04 03:17:47 +08:00
// 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);
}
}
2014-12-04 03:17:47 +08:00
void Scale9Sprite::applyBlendFunc()
{
if(_scale9Image)
_scale9Image->setBlendFunc(_blendFunc);
}
2014-09-01 17:06:22 +08:00
bool Scale9Sprite::updateWithBatchNode(cocos2d::SpriteBatchNode *batchnode,
const cocos2d::Rect &originalRect,
bool rotated,
const cocos2d::Rect &capInsets)
2014-09-01 17:06:22 +08:00
{
Sprite *sprite = Sprite::createWithTexture(batchnode->getTexture());
return this->updateWithSprite(sprite,
originalRect,
rotated,
Vec2::ZERO,
originalRect.size,
capInsets);
2014-09-01 17:06:22 +08:00
}
bool Scale9Sprite::updateWithSprite(Sprite* sprite,
const Rect& rect,
bool rotated,
const Rect& capInsets)
{
return updateWithSprite(sprite, rect, rotated, Vec2::ZERO, rect.size, capInsets);
}
bool Scale9Sprite::updateWithSprite(Sprite* sprite,
const Rect& textureRect,
bool rotated,
const Vec2 &offset,
const Size &originalSize,
const Rect& capInsets)
2014-07-29 10:39:38 +08:00
{
2014-07-29 10:39:38 +08:00
GLubyte opacity = getOpacity();
Color3B color = getColor();
2014-07-29 10:39:38 +08:00
// Release old sprites
this->cleanupSlicedSprites();
2014-12-04 03:17:47 +08:00
updateBlendFunc(sprite?sprite->getTexture():nullptr);
if(nullptr != sprite)
2014-07-29 10:39:38 +08:00
{
if (nullptr == sprite->getSpriteFrame())
{
return false;
}
if (nullptr == _scale9Image)
{
_scale9Image = sprite;
_scale9Image->retain();
}
else
{
_scale9Image->setSpriteFrame(sprite->getSpriteFrame());
}
2014-07-29 10:39:38 +08:00
}
else
{
CC_SAFE_RELEASE_NULL(_scale9Image);
}
2014-07-29 10:39:38 +08:00
if (!_scale9Image)
{
return false;
}
Rect rect(textureRect);
Size size(originalSize);
2014-07-29 10:39:38 +08:00
// If there is no given rect
if ( rect.equals(Rect::ZERO) )
{
// Get the texture size as original
Size textureSize = _scale9Image->getTexture()->getContentSize();
2014-07-29 10:39:38 +08:00
rect = Rect(0, 0, textureSize.width, textureSize.height);
}
if( size.equals(Size::ZERO) )
{
size = rect.size;
}
2014-07-29 10:39:38 +08:00
// Set the given rect's size as original size
_spriteRect = rect;
_spriteFrameRotated = rotated;
_originalSize = size;
_preferredSize = size;
_capInsetsInternal = capInsets;
if (_scale9Enabled)
{
_scale9Image->setAnchorPoint(Vec2::ZERO);
_scale9Image->setPosition(Vec2::ZERO);
2015-10-29 21:53:58 +08:00
_sliceSpriteDirty = true;
2014-07-31 11:07:20 +08:00
}
2014-12-04 03:17:47 +08:00
applyBlendFunc();
this->setState(_brightState);
if(this->_isPatch9)
{
size.width = size.width - 2;
size.height = size.height - 2;
}
this->setContentSize(size);
2014-07-29 10:39:38 +08:00
if (_spritesGenerated)
{
// Restore color and opacity
this->setOpacity(opacity);
this->setColor(color);
}
_spritesGenerated = true;
2014-07-29 10:39:38 +08:00
return true;
}
2015-10-30 09:58:02 +08:00
void Scale9Sprite::configureSimpleModeRendering()
{
this->setInsetTop(0);
this->setInsetBottom(0);
this->setInsetLeft(0);
this->setInsetRight(0);
}
void Scale9Sprite::createSlicedSprites()
2014-07-29 10:39:38 +08:00
{
2015-10-29 10:41:58 +08:00
//todo create sliced sprite
if (_scale9Enabled)
{
2015-10-29 10:41:58 +08:00
Texture2D *tex = _scale9Image ? _scale9Image->getTexture() : nullptr;
2015-10-29 10:41:58 +08:00
if (tex == nullptr)
{
2015-10-29 10:41:58 +08:00
return;
}
2015-10-30 09:58:02 +08:00
if (_renderingType == RenderingType::SIMPLE)
{
2015-10-30 09:58:02 +08:00
this->configureSimpleModeRendering();
}
2015-10-29 10:41:58 +08:00
auto capInsets = CC_RECT_POINTS_TO_PIXELS(_capInsetsInternal);
auto textureRect = CC_RECT_POINTS_TO_PIXELS(_spriteRect);
auto spriteRectSize = CC_SIZE_POINTS_TO_PIXELS(_originalSize);
2015-10-29 10:41:58 +08:00
//handle .9.png
if (_isPatch9)
{
2015-10-29 10:41:58 +08:00
spriteRectSize = Size(spriteRectSize.width - 2, spriteRectSize.height-2);
}
2015-10-29 10:41:58 +08:00
if(capInsets.equals(Rect::ZERO))
{
capInsets = Rect(spriteRectSize.width/3, spriteRectSize.height/3,
spriteRectSize.width/3, spriteRectSize.height/3);
}
2015-10-29 10:41:58 +08:00
auto uv = this->calculateUV(tex, capInsets, spriteRectSize);
auto vertices = this->calculateVertices(capInsets, spriteRectSize);
auto triangles = this->calculateTriangles(uv, vertices);
2015-10-29 10:41:58 +08:00
_scale9Image->getPolygonInfo().setTriangles(triangles);
}
2014-07-29 10:39:38 +08:00
}
2014-07-29 10:39:38 +08:00
void Scale9Sprite::setContentSize(const Size &size)
{
2015-10-29 10:41:58 +08:00
if (_contentSize.equals(size))
{
2015-10-29 10:41:58 +08:00
return;
}
2015-10-29 10:41:58 +08:00
Node::setContentSize(size);
_preferredSize = size;
2015-10-29 21:53:58 +08:00
_sliceSpriteDirty = true;
this->adjustNoneScale9ImagePosition();
2014-07-29 10:39:38 +08:00
}
Scale9Sprite* Scale9Sprite::resizableSpriteWithCapInsets(const Rect& capInsets) const
2014-07-29 10:39:38 +08:00
{
Scale9Sprite* pReturn = new (std::nothrow) Scale9Sprite();
if ( pReturn && pReturn->init(_scale9Image,
_spriteRect,
_spriteFrameRotated,
2015-10-29 10:41:58 +08:00
Vec2::ZERO,
_originalSize,
2015-10-29 21:53:58 +08:00
capInsets) )
2014-07-29 10:39:38 +08:00
{
pReturn->autorelease();
return pReturn;
}
CC_SAFE_DELETE(pReturn);
return NULL;
}
Scale9Sprite::State Scale9Sprite::getState()const
{
return _brightState;
}
2014-12-23 15:03:31 +08:00
void Scale9Sprite::setState(cocos2d::ui::Scale9Sprite::State state)
{
GLProgramState *glState = nullptr;
switch (state)
{
case State::NORMAL:
{
glState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP);
}
break;
case State::GRAY:
{
glState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_GRAYSCALE);
}
default:
break;
2014-12-23 15:03:31 +08:00
}
2014-12-23 15:03:31 +08:00
if (nullptr != _scale9Image)
{
_scale9Image->setGLProgramState(glState);
}
_brightState = state;
2014-12-23 15:03:31 +08:00
}
/** sets the opacity.
@warning If the texture has premultiplied alpha then, the R, G and B channels will be modifed.
Values goes from 0 to 255, where 255 means fully opaque.
*/
2014-07-29 10:39:38 +08:00
void Scale9Sprite::updateCapInset()
{
Rect insets;
2015-10-29 10:41:58 +08:00
insets = Rect(_insetLeft,
_insetTop,
_originalSize.width-_insetLeft-_insetRight,
_originalSize.height-_insetTop-_insetBottom);
2014-07-29 10:39:38 +08:00
this->setCapInsets(insets);
}
void Scale9Sprite::setSpriteFrame(SpriteFrame * spriteFrame, const Rect& capInsets)
2014-07-29 10:39:38 +08:00
{
Sprite * sprite = Sprite::createWithTexture(spriteFrame->getTexture());
this->updateWithSprite(sprite,
spriteFrame->getRect(),
spriteFrame->isRotated(),
spriteFrame->getOffset(),
spriteFrame->getOriginalSize(),
capInsets);
2014-07-29 10:39:38 +08:00
// Reset insets
this->_insetLeft = capInsets.origin.x;
this->_insetTop = capInsets.origin.y;
this->_insetRight = _originalSize.width - _insetLeft - capInsets.size.width;
this->_insetBottom = _originalSize.height - _insetTop - capInsets.size.height;
2014-07-29 10:39:38 +08:00
}
2015-10-09 16:59:11 +08:00
void Scale9Sprite::setPreferredSize(const Size& preferredSize)
2014-07-29 10:39:38 +08:00
{
2015-10-09 16:59:11 +08:00
this->setContentSize(preferredSize);
2014-07-29 10:39:38 +08:00
}
2014-07-29 10:39:38 +08:00
void Scale9Sprite::setCapInsets(const Rect& capInsets)
{
Size contentSize = this->_contentSize;
this->updateWithSprite(this->_scale9Image,
_spriteRect,
_spriteFrameRotated,
2015-10-29 10:41:58 +08:00
Vec2::ZERO,
_originalSize,
capInsets);
this->_insetLeft = capInsets.origin.x;
this->_insetTop = capInsets.origin.y;
this->_insetRight = _originalSize.width - _insetLeft - capInsets.size.width;
this->_insetBottom = _originalSize.height - _insetTop - capInsets.size.height;
2014-07-29 10:39:38 +08:00
this->setContentSize(contentSize);
}
2014-07-29 10:39:38 +08:00
void Scale9Sprite::setInsetLeft(float insetLeft)
{
this->_insetLeft = insetLeft;
this->updateCapInset();
}
2014-07-29 10:39:38 +08:00
void Scale9Sprite::setInsetTop(float insetTop)
{
this->_insetTop = insetTop;
this->updateCapInset();
}
2014-07-29 10:39:38 +08:00
void Scale9Sprite::setInsetRight(float insetRight)
{
this->_insetRight = insetRight;
this->updateCapInset();
}
2014-07-29 10:39:38 +08:00
void Scale9Sprite::setInsetBottom(float insetBottom)
{
this->_insetBottom = insetBottom;
this->updateCapInset();
}
2014-07-29 10:39:38 +08:00
void Scale9Sprite::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
{
2014-07-29 10:39:38 +08:00
// quick return if not visible. children won't be drawn.
if (!_visible)
{
return;
}
2015-10-29 21:53:58 +08:00
if (_scale9Enabled && _sliceSpriteDirty) {
this->createSlicedSprites();
_sliceSpriteDirty = false;
}
2014-07-29 10:39:38 +08:00
uint32_t flags = processParentFlags(parentTransform, parentFlags);
2014-07-29 10:39:38 +08:00
// IMPORTANT:
// To ease the migration to v3.0, we still support the Mat4 stack,
// but it is deprecated and your code should not rely on it
Director* director = Director::getInstance();
2015-10-09 16:59:11 +08:00
CCASSERT(nullptr != director, "Director is null when setting matrix stack");
2014-07-29 10:39:38 +08:00
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
2014-07-29 10:39:38 +08:00
int i = 0; // used by _children
2014-07-29 10:39:38 +08:00
sortAllChildren();
2014-07-29 10:39:38 +08:00
//
// draw children and protectedChildren zOrder < 0
//
for( ; i < _children.size(); i++ )
{
auto node = _children.at(i);
2014-07-29 10:39:38 +08:00
if ( node && node->getLocalZOrder() < 0 )
node->visit(renderer, _modelViewTransform, flags);
else
break;
}
2015-10-29 10:41:58 +08:00
if (_scale9Image && _scale9Image->getLocalZOrder() < 0 )
{
2015-10-29 10:41:58 +08:00
_scale9Image->visit(renderer, _modelViewTransform, flags);
2014-07-29 10:39:38 +08:00
}
2015-10-29 10:41:58 +08:00
2014-07-29 10:39:38 +08:00
//
// draw self
//
if (isVisitableByVisitingCamera())
2014-08-13 12:06:51 +08:00
this->draw(renderer, _modelViewTransform, flags);
2014-07-29 10:39:38 +08:00
//
// draw children and protectedChildren zOrder >= 0
//
2015-10-29 10:41:58 +08:00
if (_scale9Image && _scale9Image->getLocalZOrder() >= 0 )
{
2015-10-29 10:41:58 +08:00
_scale9Image->visit(renderer, _modelViewTransform, flags);
2014-07-29 10:39:38 +08:00
}
2014-07-29 10:39:38 +08:00
for(auto it=_children.cbegin()+i; it != _children.cend(); ++it)
(*it)->visit(renderer, _modelViewTransform, flags);
2014-09-11 15:44:31 +08:00
// FIX ME: Why need to set _orderOfArrival to 0??
// Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
// setOrderOfArrival(0);
2014-07-29 10:39:38 +08:00
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
2014-07-29 10:39:38 +08:00
}
2014-07-29 10:39:38 +08:00
Size Scale9Sprite::getOriginalSize()const
{
return _originalSize;
}
2014-07-29 10:39:38 +08:00
Size Scale9Sprite::getPreferredSize() const
{
return _preferredSize;
}
2014-07-29 10:39:38 +08:00
Rect Scale9Sprite::getCapInsets()const
{
2015-10-29 10:41:58 +08:00
return _capInsetsInternal;
2014-07-29 10:39:38 +08:00
}
2014-07-29 10:39:38 +08:00
float Scale9Sprite::getInsetLeft()const
{
return this->_insetLeft;
}
2014-07-29 10:39:38 +08:00
float Scale9Sprite::getInsetTop()const
{
return this->_insetTop;
}
2014-07-29 10:39:38 +08:00
float Scale9Sprite::getInsetRight()const
{
return this->_insetRight;
}
2014-07-29 10:39:38 +08:00
float Scale9Sprite::getInsetBottom()const
{
return this->_insetBottom;
}
2014-07-29 10:39:38 +08:00
void Scale9Sprite::setScale9Enabled(bool enabled)
{
if (_scale9Enabled == enabled)
{
return;
}
2014-07-29 10:39:38 +08:00
_scale9Enabled = enabled;
this->cleanupSlicedSprites();
2014-11-07 11:09:22 +08:00
//we must invalide the transform when toggling scale9enabled
_transformUpdated = _transformDirty = _inverseDirty = true;
if (_scale9Enabled)
{
if (_scale9Image)
{
this->updateWithSprite(this->_scale9Image,
_spriteRect,
_spriteFrameRotated,
2015-10-29 10:41:58 +08:00
Vec2::ZERO,
_originalSize,
2015-10-29 10:41:58 +08:00
_capInsetsInternal);
}
2014-07-31 11:07:20 +08:00
}
2015-10-29 10:41:58 +08:00
else
{
2015-10-29 10:41:58 +08:00
if (_scale9Image)
{
auto quad = _scale9Image->getQuad();
PolygonInfo polyInfo;
polyInfo.setQuad(&quad);
_scale9Image->setPolygonInfo(polyInfo);
}
2014-07-29 10:39:38 +08:00
}
2015-10-29 21:53:58 +08:00
this->adjustNoneScale9ImagePosition();
2014-07-29 10:39:38 +08:00
}
2015-10-29 10:41:58 +08:00
bool Scale9Sprite::isScale9Enabled() const
2014-07-29 10:39:38 +08:00
{
2015-10-29 10:41:58 +08:00
return _scale9Enabled;
}
2015-10-29 10:41:58 +08:00
void Scale9Sprite::setAnchorPoint(const cocos2d::Vec2 &position)
{
Node::setAnchorPoint(position);
if (!_scale9Enabled)
{
if (_scale9Image)
{
2015-10-29 10:41:58 +08:00
_nonSliceSpriteAnchor = position;
_scale9Image->setAnchorPoint(position);
2015-10-29 21:53:58 +08:00
this->adjustNoneScale9ImagePosition();
}
2014-07-29 10:39:38 +08:00
}
}
2015-10-29 21:53:58 +08:00
void Scale9Sprite::adjustNoneScale9ImagePosition()
2014-07-29 10:39:38 +08:00
{
2015-10-29 10:41:58 +08:00
if (_scale9Image)
{
2015-10-29 10:41:58 +08:00
if (!_scale9Enabled) {
_scale9Image->setAnchorPoint(_nonSliceSpriteAnchor);
_scale9Image->setPosition(_contentSize.width * _scale9Image->getAnchorPoint().x,
_contentSize.height * _scale9Image->getAnchorPoint().y);
2015-10-29 10:41:58 +08:00
}
}
2014-07-29 10:39:38 +08:00
}
2014-07-29 10:39:38 +08:00
void Scale9Sprite::updateDisplayedColor(const cocos2d::Color3B &parentColor)
{
_displayedColor.r = _realColor.r * parentColor.r/255.0;
_displayedColor.g = _realColor.g * parentColor.g/255.0;
_displayedColor.b = _realColor.b * parentColor.b/255.0;
updateColor();
2014-07-30 15:19:39 +08:00
if (_scale9Image)
{
2014-07-29 10:39:38 +08:00
_scale9Image->updateDisplayedColor(_displayedColor);
}
2015-10-29 10:41:58 +08:00
2014-07-29 10:39:38 +08:00
if (_cascadeColorEnabled)
{
2014-07-30 15:19:39 +08:00
for(const auto &child : _children)
{
2014-07-29 10:39:38 +08:00
child->updateDisplayedColor(_displayedColor);
}
}
}
2014-07-29 10:39:38 +08:00
void Scale9Sprite::updateDisplayedOpacity(GLubyte parentOpacity)
{
_displayedOpacity = _realOpacity * parentOpacity/255.0;
updateColor();
2014-07-30 15:19:39 +08:00
if (_scale9Image)
{
2014-07-29 10:39:38 +08:00
_scale9Image->updateDisplayedOpacity(_displayedOpacity);
}
2014-07-29 10:39:38 +08:00
if (_cascadeOpacityEnabled)
{
2014-07-30 15:19:39 +08:00
for(auto child : _children)
{
2014-07-29 10:39:38 +08:00
child->updateDisplayedOpacity(_displayedOpacity);
}
}
}
2014-07-29 10:39:38 +08:00
void Scale9Sprite::disableCascadeColor()
{
2014-07-30 15:19:39 +08:00
for(auto child : _children)
{
2014-07-29 10:39:38 +08:00
child->updateDisplayedColor(Color3B::WHITE);
}
2015-10-29 10:41:58 +08:00
2014-07-30 15:19:39 +08:00
if (_scale9Image)
{
2014-07-29 10:39:38 +08:00
_scale9Image->updateDisplayedColor(Color3B::WHITE);
}
}
void Scale9Sprite::disableCascadeOpacity()
{
_displayedOpacity = _realOpacity;
for(auto child : _children){
child->updateDisplayedOpacity(255);
}
}
2014-07-29 10:39:38 +08:00
Sprite* Scale9Sprite::getSprite()const
{
return _scale9Image;
}
2014-07-29 14:33:22 +08:00
void Scale9Sprite::setFlippedX(bool flippedX)
{
2014-11-07 11:09:22 +08:00
float realScale = this->getScaleX();
2014-07-29 14:33:22 +08:00
_flippedX = flippedX;
2014-11-07 11:09:22 +08:00
this->setScaleX(realScale);
2014-07-29 14:33:22 +08:00
}
2014-07-29 14:33:22 +08:00
void Scale9Sprite::setFlippedY(bool flippedY)
{
2014-11-07 11:09:22 +08:00
float realScale = this->getScaleY();
2014-07-29 14:33:22 +08:00
_flippedY = flippedY;
2014-11-07 11:09:22 +08:00
this->setScaleY(realScale);
2014-07-29 14:33:22 +08:00
}
2014-07-29 14:33:22 +08:00
bool Scale9Sprite::isFlippedX()const
{
return _flippedX;
}
2014-07-29 14:33:22 +08:00
bool Scale9Sprite::isFlippedY()const
{
return _flippedY;
}
2014-11-07 11:09:22 +08:00
void Scale9Sprite::setScaleX(float scaleX)
{
if (_flippedX) {
scaleX = scaleX * -1;
}
Node::setScaleX(scaleX);
}
2014-11-07 11:09:22 +08:00
void Scale9Sprite::setScaleY(float scaleY)
{
if (_flippedY) {
scaleY = scaleY * -1;
}
Node::setScaleY(scaleY);
}
2014-11-07 11:09:22 +08:00
void Scale9Sprite::setScale(float scale)
{
this->setScaleX(scale);
this->setScaleY(scale);
this->setScaleZ(scale);
}
2014-11-07 11:09:22 +08:00
void Scale9Sprite::setScale(float scaleX, float scaleY)
{
this->setScaleX(scaleX);
this->setScaleY(scaleY);
}
2014-11-07 11:09:22 +08:00
float Scale9Sprite::getScaleX()const
{
float originalScale = Node::getScaleX();
if (_flippedX)
{
originalScale = originalScale * -1.0;
}
return originalScale;
}
2014-11-07 11:09:22 +08:00
float Scale9Sprite::getScaleY()const
{
float originalScale = Node::getScaleY();
if (_flippedY)
{
originalScale = originalScale * -1.0;
}
return originalScale;
}
2014-11-07 11:09:22 +08:00
float Scale9Sprite::getScale()const
{
CCASSERT(this->getScaleX() == this->getScaleY(),
"Scale9Sprite#scale. ScaleX != ScaleY. Don't know which one to return");
2014-11-07 11:09:22 +08:00
return this->getScaleX();
}
2015-02-12 09:24:04 +08:00
void Scale9Sprite::setCameraMask(unsigned short mask, bool applyChildren)
{
Node::setCameraMask(mask, applyChildren);
2015-02-12 09:24:04 +08:00
if(_scale9Image)
_scale9Image->setCameraMask(mask,applyChildren);
2015-10-29 10:41:58 +08:00
}
// (0,0) O = capInsets.origin
// v0----------------------
// | | | |
// | | | |
// v1-------O------+------|
// | | | |
// | | | |
// v2-------+------+------|
// | | | |
// | | | |
// v3-------------------- (1,1) (texture coordinate is flipped)
// u0 u1 u2 u3
std::vector<Vec2> Scale9Sprite::calculateUV(Texture2D *tex,
const Rect& capInsets,
const Size& spriteRectSize)
{
auto atlasWidth = tex->getPixelsWide();
auto atlasHeight = tex->getPixelsHigh();
//caculate texture coordinate
float leftWidth = 0, centerWidth = 0, rightWidth = 0;
float topHeight = 0, centerHeight = 0, bottomHeight = 0;
if (_spriteFrameRotated)
{
rightWidth = capInsets.origin.y;
centerWidth = capInsets.size.height;
leftWidth = spriteRectSize.height - centerWidth - rightWidth;
topHeight = capInsets.origin.x;
centerHeight = capInsets.size.width;
bottomHeight = spriteRectSize.width - (topHeight + centerHeight);
}
else
{
leftWidth = capInsets.origin.x;
centerWidth = capInsets.size.width;
rightWidth = spriteRectSize.width - (leftWidth + centerWidth);
2015-10-29 10:41:58 +08:00
topHeight = capInsets.origin.y;
centerHeight = capInsets.size.height;
bottomHeight =spriteRectSize.height - (topHeight + centerHeight);
}
auto textureRect = CC_RECT_POINTS_TO_PIXELS(_spriteRect);
//handle .9.png
if (_isPatch9)
2015-02-12 09:24:04 +08:00
{
2015-10-29 10:41:58 +08:00
//This magic number is used to avoiding artifact with .9.png format.
float offset = 1.3f;
textureRect = Rect(textureRect.origin.x + offset,
textureRect.origin.y + offset,
textureRect.size.width - 2,
textureRect.size.height - 2);
2015-02-12 09:24:04 +08:00
}
2015-10-29 10:41:58 +08:00
//uv computation should take spritesheet into account.
float u0, u1, u2, u3;
float v0, v1, v2, v3;
if (_spriteFrameRotated)
{
u0 = textureRect.origin.x / atlasWidth;
u1 = (leftWidth + textureRect.origin.x) / atlasWidth;
u2 = (leftWidth + centerWidth + textureRect.origin.x) / atlasWidth;
u3 = (textureRect.origin.x + textureRect.size.height) / atlasWidth;
v3 = textureRect.origin.y / atlasHeight;
v2 = (topHeight + textureRect.origin.y) / atlasHeight;
v1 = (topHeight + centerHeight + textureRect.origin.y) / atlasHeight;
v0 = (textureRect.origin.y + textureRect.size.width) / atlasHeight;
}
else
{
u0 = textureRect.origin.x / atlasWidth;
u1 = (leftWidth + textureRect.origin.x) / atlasWidth;
u2 = (leftWidth + centerWidth + textureRect.origin.x) / atlasWidth;
u3 = (textureRect.origin.x + textureRect.size.width) / atlasWidth;
v0 = textureRect.origin.y / atlasHeight;
v1 = (topHeight + textureRect.origin.y) / atlasHeight;
v2 = (topHeight + centerHeight + textureRect.origin.y) / atlasHeight;
v3 = (textureRect.origin.y + textureRect.size.height) / atlasHeight;
}
2015-10-29 23:13:36 +08:00
std::vector<Vec2> uvCoordinates;
if (_renderingType == RenderingType::SIMPLE)
{
uvCoordinates = {Vec2(u0,v3), Vec2(u3,v0)};
}
else
{
uvCoordinates = {Vec2(u0,v3), Vec2(u1,v2), Vec2(u2,v1), Vec2(u3,v0)};
}
2015-10-29 10:41:58 +08:00
return uvCoordinates;
}
//
// y3----------------------(preferedSize.width, preferedSize.height)
// | | | |
// | | | |
// y2-------O------+------|
// | | | |
// | | | |
// y1-------+------+------|
// | | | |
// | | | |
//x0,y0--------------------
// x1 x2 x3
std::vector<Vec2> Scale9Sprite::calculateVertices(const Rect& capInsets,
const Size& spriteRectSize)
{
float leftWidth = 0, centerWidth = 0, rightWidth = 0;
float topHeight = 0, centerHeight = 0, bottomHeight = 0;
leftWidth = capInsets.origin.x;
centerWidth = capInsets.size.width;
rightWidth = spriteRectSize.width - (leftWidth + centerWidth);
topHeight = capInsets.origin.y;
centerHeight = capInsets.size.height;
bottomHeight = spriteRectSize.height - (topHeight + centerHeight);
leftWidth = leftWidth / CC_CONTENT_SCALE_FACTOR();
rightWidth = rightWidth / CC_CONTENT_SCALE_FACTOR();
topHeight = topHeight / CC_CONTENT_SCALE_FACTOR();
bottomHeight = bottomHeight / CC_CONTENT_SCALE_FACTOR();
float sizableWidth = _preferredSize.width - leftWidth - rightWidth;
float sizableHeight = _preferredSize.height - topHeight - bottomHeight;
float x0,x1,x2,x3;
float y0,y1,y2,y3;
if(sizableWidth >= 0)
{
x0 = 0;
x1 = leftWidth;
x2 = leftWidth + sizableWidth;
x3 = _preferredSize.width;
}
else
{
float xScale = _preferredSize.width / (leftWidth + rightWidth);
x0 = 0;
x1 = x2 = leftWidth * xScale;
x3 = (leftWidth + rightWidth) * xScale;
}
if(sizableHeight >= 0)
{
y0 = 0;
y1 = bottomHeight;
y2 = bottomHeight + sizableHeight;
y3 = _preferredSize.height;
}
else
{
float yScale = _preferredSize.height / (topHeight + bottomHeight);
y0 = 0;
y1 = y2= bottomHeight * yScale;
y3 = (bottomHeight + topHeight) * yScale;
}
2015-10-29 23:13:36 +08:00
std::vector<Vec2> vertices;
if (_renderingType == RenderingType::SIMPLE)
{
vertices = {Vec2(x0,y0), Vec2(x3,y3)};
}
else
{
vertices = {Vec2(x0,y0), Vec2(x1,y1), Vec2(x2,y2), Vec2(x3,y3)};
}
2015-10-29 10:41:58 +08:00
return vertices;
}
TrianglesCommand::Triangles Scale9Sprite::calculateTriangles(const std::vector<Vec2>& uv,
const std::vector<Vec2>& vertices)
{
2015-10-29 23:13:36 +08:00
const unsigned short slicedTotalVertexCount = powf(uv.size(),2);
const unsigned short slicedTotalIndices = 6 * powf(uv.size() -1, 2);
2015-10-29 10:41:58 +08:00
CC_SAFE_DELETE_ARRAY(_sliceVertices);
CC_SAFE_DELETE_ARRAY(_sliceIndices);
2015-10-29 10:41:58 +08:00
_sliceVertices = new V3F_C4B_T2F[slicedTotalVertexCount];
_sliceIndices = new unsigned short[slicedTotalIndices];
unsigned short indicesStart = 0;
const unsigned short indicesOffset = 6;
2015-10-29 23:13:36 +08:00
const unsigned short sliceQuadIndices[] = {4,0,5, 1,5,0};
const unsigned short simpleQuadIndices[] = {0,1,2, 3,2,1};
auto displayedColor = _scale9Image->getDisplayedColor();
auto displayedOpacity = _scale9Image->getDisplayedOpacity();
Color4B color4( displayedColor.r, displayedColor.g, displayedColor.b, displayedOpacity );
// special opacity for premultiplied textures
if (_scale9Image->isOpacityModifyRGB())
{
color4.r *= displayedOpacity/255.0f;
color4.g *= displayedOpacity/255.0f;
color4.b *= displayedOpacity/255.0f;
}
2015-10-29 10:41:58 +08:00
2015-10-29 23:13:36 +08:00
int vertexCount = (int)(vertices.size() - 1);
2015-10-29 10:41:58 +08:00
2015-10-29 23:13:36 +08:00
for (int j = 0; j <= vertexCount; ++j)
2015-02-12 09:24:04 +08:00
{
2015-10-29 23:13:36 +08:00
for (int i = 0; i <= vertexCount; ++i)
2015-10-29 10:41:58 +08:00
{
V3F_C4B_T2F vertextData;
vertextData.vertices.x = vertices[i].x;
vertextData.vertices.y = vertices[j].y;
if (_spriteFrameRotated)
{
vertextData.texCoords.u = uv[j].x;
vertextData.texCoords.v = uv[i].y;
}
else
{
vertextData.texCoords.u = uv[i].x;
vertextData.texCoords.v = uv[j].y;
}
vertextData.colors = color4;
2015-10-29 23:13:36 +08:00
//if slice mode
if (_renderingType == RenderingType::SLICE)
2015-10-29 10:41:58 +08:00
{
2015-10-29 23:13:36 +08:00
memcpy(_sliceVertices + i + j * 4, &vertextData, sizeof(V3F_C4B_T2F));
}
else
{
memcpy(_sliceVertices + i + j * 2, &vertextData, sizeof(V3F_C4B_T2F));
}
}
2015-02-12 09:24:04 +08:00
}
2015-10-29 23:13:36 +08:00
if (_renderingType == RenderingType::SLICE)
{
for (int j = 0; j <= vertexCount; ++j)
{
for (int i = 0; i <= vertexCount; ++i)
{
if (i < 3 && j < 3)
2015-10-29 10:41:58 +08:00
{
2015-10-29 23:13:36 +08:00
memcpy(_sliceIndices + indicesStart, sliceQuadIndices, indicesOffset * sizeof(unsigned short));
for (int k = 0; k < indicesOffset; ++k)
{
unsigned short actualIndex = (i + j * 3) * indicesOffset;
_sliceIndices[k + actualIndex] = _sliceIndices[k + actualIndex] + j * 4 + i;
}
indicesStart = indicesStart + indicesOffset;
2015-10-29 10:41:58 +08:00
}
2015-10-29 23:13:36 +08:00
2015-10-29 10:41:58 +08:00
}
}
}
2015-10-29 23:13:36 +08:00
if (_renderingType == RenderingType::SIMPLE)
{
memcpy(_sliceIndices, simpleQuadIndices, indicesOffset * sizeof(unsigned short));
}
2015-10-29 10:41:58 +08:00
TrianglesCommand::Triangles triangles;
triangles.vertCount = slicedTotalVertexCount;
triangles.indexCount = slicedTotalIndices;
triangles.verts = _sliceVertices;
triangles.indices = _sliceIndices;
return triangles;
2015-02-12 09:24:04 +08:00
}
2015-10-29 23:13:36 +08:00
void Scale9Sprite::setRenderingType(cocos2d::ui::Scale9Sprite::RenderingType type)
{
2015-10-30 14:01:01 +08:00
if (_renderingType == type)
{
return;
}
2015-10-29 23:13:36 +08:00
_renderingType = type;
2015-10-30 14:01:01 +08:00
_sliceSpriteDirty = true;
2015-10-29 23:13:36 +08:00
}
Scale9Sprite::RenderingType Scale9Sprite::getRenderingType()const
{
return _renderingType;
2015-02-12 09:24:04 +08:00
}
void Scale9Sprite::resetRender()
{
// Release old sprites
this->cleanupSlicedSprites();
CC_SAFE_RELEASE_NULL(this->_scale9Image);
}
2014-07-29 10:39:38 +08:00
}}