2014-07-29 10:26:53 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:26:53 +08:00
|
|
|
http://www.cocos2d-x.org
|
2015-04-20 17:00:40 +08:00
|
|
|
|
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:
|
2015-04-20 17:00:40 +08:00
|
|
|
|
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.
|
2015-04-20 17:00:40 +08:00
|
|
|
|
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.
|
2015-05-21 16:04:37 +08:00
|
|
|
****************************************************************************/
|
2014-07-29 10:26:53 +08:00
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "ui/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"
|
2016-07-22 11:56:41 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-12-23 15:03:31 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
|
|
|
#include "renderer/ccShaders.h"
|
2015-05-21 16:04:37 +08:00
|
|
|
#include "platform/CCImage.h"
|
|
|
|
#include "base/CCNinePatchImageParser.h"
|
2015-12-03 17:53:02 +08:00
|
|
|
#include "2d/CCDrawNode.h"
|
|
|
|
#include "2d/CCCamera.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
2014-07-29 10:39:38 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
namespace ui {
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
Scale9Sprite::Scale9Sprite()
|
2015-12-09 14:21:26 +08:00
|
|
|
: _spriteFrameRotated(false)
|
2015-05-21 16:04:37 +08:00
|
|
|
, _scale9Image(nullptr)
|
|
|
|
, _scale9Enabled(true)
|
|
|
|
, _insetLeft(0)
|
|
|
|
, _insetTop(0)
|
|
|
|
, _insetRight(0)
|
|
|
|
, _insetBottom(0)
|
|
|
|
,_flippedX(false)
|
|
|
|
,_flippedY(false)
|
|
|
|
,_isPatch9(false)
|
2015-07-15 14:35:13 +08:00
|
|
|
,_brightState(State::NORMAL)
|
2015-10-29 14:27:27 +08:00
|
|
|
,_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)
|
2015-12-03 17:53:02 +08:00
|
|
|
,_insideBounds(true)
|
2014-07-29 10:39:38 +08:00
|
|
|
{
|
|
|
|
this->setAnchorPoint(Vec2(0.5,0.5));
|
2015-12-03 17:53:02 +08:00
|
|
|
#if CC_SPRITE_DEBUG_DRAW
|
|
|
|
_debugDrawNode = DrawNode::create();
|
|
|
|
addChild(_debugDrawNode);
|
|
|
|
#endif //CC_SPRITE_DEBUG_DRAW
|
2014-07-29 10:39:38 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
Scale9Sprite::~Scale9Sprite()
|
|
|
|
{
|
|
|
|
this->cleanupSlicedSprites();
|
|
|
|
CC_SAFE_RELEASE(_scale9Image);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-05-21 16:04:37 +08:00
|
|
|
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)
|
|
|
|
{
|
2015-12-16 11:53:59 +08:00
|
|
|
bool ret = false;
|
|
|
|
do {
|
|
|
|
Texture2D* texture = spriteFrame->getTexture();
|
2016-05-25 02:09:11 +08:00
|
|
|
CCASSERT(texture != nullptr, "Texture2D must be not null");
|
2015-12-16 11:53:59 +08:00
|
|
|
if(texture == nullptr) break;
|
|
|
|
|
|
|
|
Sprite *sprite = Sprite::createWithSpriteFrame(spriteFrame);
|
2016-05-25 02:09:11 +08:00
|
|
|
CCASSERT(sprite != nullptr, "Sprite must be not null");
|
2015-12-16 11:53:59 +08:00
|
|
|
if(sprite == nullptr) break;
|
|
|
|
|
|
|
|
ret = this->init(sprite,
|
|
|
|
spriteFrame->getRect(),
|
|
|
|
spriteFrame->isRotated(),
|
|
|
|
spriteFrame->getOffset(),
|
|
|
|
spriteFrame->getOriginalSize(),
|
|
|
|
capInsets);
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return ret;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
bool Scale9Sprite::initWithSpriteFrame(SpriteFrame* spriteFrame)
|
|
|
|
{
|
2016-05-25 02:09:11 +08:00
|
|
|
CCASSERT(spriteFrame != nullptr, "Invalid spriteFrame for sprite");
|
2015-05-21 16:04:37 +08:00
|
|
|
bool pReturn = this->initWithSpriteFrame(spriteFrame, Rect::ZERO);
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
bool Scale9Sprite::initWithSpriteFrameName(const std::string& spriteFrameName,
|
|
|
|
const Rect& capInsets)
|
|
|
|
{
|
2015-12-16 11:53:59 +08:00
|
|
|
bool ret = false;
|
|
|
|
do {
|
|
|
|
auto spriteFrameCache = SpriteFrameCache::getInstance();
|
|
|
|
CCASSERT(spriteFrameCache != nullptr,
|
|
|
|
"SpriteFrameCache::getInstance() must be non-NULL");
|
2015-12-16 15:08:48 +08:00
|
|
|
if(spriteFrameCache == nullptr) break;
|
2015-12-16 11:53:59 +08:00
|
|
|
|
|
|
|
SpriteFrame *frame = spriteFrameCache->getSpriteFrameByName(spriteFrameName);
|
2016-07-22 11:56:41 +08:00
|
|
|
CCASSERT(frame != nullptr, StringUtils::format("CCSpriteFrame: %s must be non-NULL ", spriteFrameName.c_str()).c_str());
|
2015-12-16 15:08:48 +08:00
|
|
|
if (frame == nullptr) break;
|
2015-12-16 11:53:59 +08:00
|
|
|
|
|
|
|
ret = initWithSpriteFrame(frame, capInsets);
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return ret;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
bool Scale9Sprite::initWithSpriteFrameName(const std::string& spriteFrameName)
|
|
|
|
{
|
|
|
|
bool pReturn = this->initWithSpriteFrameName(spriteFrameName, Rect::ZERO);
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::init()
|
|
|
|
{
|
2016-05-25 02:09:11 +08:00
|
|
|
return this->init(nullptr, Rect::ZERO, Rect::ZERO);
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2015-10-29 14:27:27 +08:00
|
|
|
bool ret = true;
|
2015-05-21 16:04:37 +08:00
|
|
|
if(sprite)
|
|
|
|
{
|
|
|
|
auto texture = sprite->getTexture();
|
|
|
|
auto spriteFrame = sprite->getSpriteFrame();
|
2015-08-25 23:31:34 +08:00
|
|
|
Rect actualCapInsets = capInsets;
|
|
|
|
|
2015-05-21 16:04:37 +08:00
|
|
|
if (texture->isContain9PatchInfo())
|
|
|
|
{
|
2015-06-23 15:42:58 +08:00
|
|
|
auto& parsedCapInset = texture->getSpriteFrameCapInset(spriteFrame);
|
2015-05-21 16:04:37 +08:00
|
|
|
if(!parsedCapInset.equals(Rect::ZERO))
|
|
|
|
{
|
|
|
|
this->_isPatch9 = true;
|
|
|
|
if(capInsets.equals(Rect::ZERO))
|
|
|
|
{
|
2015-08-25 23:31:34 +08:00
|
|
|
actualCapInsets = parsedCapInset;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-10-29 14:27:27 +08:00
|
|
|
|
|
|
|
ret = this->updateWithSprite(sprite,
|
|
|
|
rect,
|
|
|
|
rotated,
|
|
|
|
offset,
|
|
|
|
originalSize,
|
|
|
|
actualCapInsets);
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
2015-10-29 14:27:27 +08:00
|
|
|
return ret;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2015-12-16 11:53:59 +08:00
|
|
|
CCASSERT(!file.empty(), "file must not be empty string!");
|
|
|
|
if(file.empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sprite = Sprite::create(file);
|
|
|
|
return init(sprite, rect, capInsets);
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::initWithFile(const std::string& file, const Rect& rect)
|
|
|
|
{
|
2015-12-16 11:53:59 +08:00
|
|
|
return initWithFile(file, rect, Rect::ZERO);
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite* Scale9Sprite::create()
|
|
|
|
{
|
|
|
|
Scale9Sprite *pReturn = new (std::nothrow) Scale9Sprite();
|
|
|
|
if (pReturn && pReturn->init())
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
2016-05-25 02:09:11 +08:00
|
|
|
return nullptr;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2016-05-25 02:09:11 +08:00
|
|
|
return nullptr;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2016-05-25 02:09:11 +08:00
|
|
|
return nullptr;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2016-05-25 02:09:11 +08:00
|
|
|
return nullptr;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2016-05-25 02:09:11 +08:00
|
|
|
return nullptr;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2016-05-25 02:09:11 +08:00
|
|
|
return nullptr;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite* Scale9Sprite::createWithSpriteFrame(SpriteFrame* spriteFrame)
|
|
|
|
{
|
|
|
|
Scale9Sprite* pReturn = new (std::nothrow) Scale9Sprite();
|
|
|
|
if ( pReturn && pReturn->initWithSpriteFrame(spriteFrame) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
2016-05-25 02:09:11 +08:00
|
|
|
return nullptr;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2016-05-25 02:09:11 +08:00
|
|
|
return nullptr;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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()");
|
2016-05-25 02:09:11 +08:00
|
|
|
return nullptr;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2015-04-20 17:00:40 +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;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-12-04 03:17:47 +08:00
|
|
|
void Scale9Sprite::updateBlendFunc(Texture2D *texture)
|
|
|
|
{
|
2015-04-20 17:00:40 +08:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-12-04 03:17:47 +08:00
|
|
|
void Scale9Sprite::applyBlendFunc()
|
|
|
|
{
|
|
|
|
if(_scale9Image)
|
|
|
|
_scale9Image->setBlendFunc(_blendFunc);
|
|
|
|
}
|
2014-09-01 17:06:22 +08:00
|
|
|
|
2015-05-21 16:04:37 +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());
|
2015-05-21 16:04:37 +08:00
|
|
|
return this->updateWithSprite(sprite,
|
|
|
|
originalRect,
|
|
|
|
rotated,
|
|
|
|
Vec2::ZERO,
|
|
|
|
originalRect.size,
|
|
|
|
capInsets);
|
2014-09-01 17:06:22 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-05-21 16:04:37 +08:00
|
|
|
bool Scale9Sprite::updateWithSprite(Sprite* sprite,
|
|
|
|
const Rect& rect,
|
|
|
|
bool rotated,
|
|
|
|
const Rect& capInsets)
|
2014-09-27 20:54:07 +08:00
|
|
|
{
|
|
|
|
return updateWithSprite(sprite, rect, rotated, Vec2::ZERO, rect.size, capInsets);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-05-21 16:04:37 +08:00
|
|
|
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
|
|
|
{
|
|
|
|
// Release old sprites
|
|
|
|
this->cleanupSlicedSprites();
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-12-04 03:17:47 +08:00
|
|
|
updateBlendFunc(sprite?sprite->getTexture():nullptr);
|
|
|
|
|
2015-01-26 16:04:43 +08:00
|
|
|
if(nullptr != sprite)
|
2014-07-29 10:39:38 +08:00
|
|
|
{
|
2015-01-26 16:04:43 +08:00
|
|
|
if (nullptr == sprite->getSpriteFrame())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-01-26 16:04:43 +08:00
|
|
|
if (nullptr == _scale9Image)
|
|
|
|
{
|
|
|
|
_scale9Image = sprite;
|
|
|
|
_scale9Image->retain();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_scale9Image->setSpriteFrame(sprite->getSpriteFrame());
|
|
|
|
}
|
2014-07-29 10:39:38 +08:00
|
|
|
}
|
2015-11-18 13:38:29 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(_scale9Image);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
if (!_scale9Image)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-09-27 20:54:07 +08:00
|
|
|
Rect rect(textureRect);
|
|
|
|
Size size(originalSize);
|
2015-08-25 23:31:34 +08:00
|
|
|
|
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();
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
rect = Rect(0, 0, textureSize.width, textureSize.height);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-09-27 20:54:07 +08:00
|
|
|
if( size.equals(Size::ZERO) )
|
|
|
|
{
|
|
|
|
size = rect.size;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
// Set the given rect's size as original size
|
|
|
|
_spriteRect = rect;
|
2014-09-27 20:54:07 +08:00
|
|
|
_spriteFrameRotated = rotated;
|
|
|
|
_originalSize = size;
|
|
|
|
_preferredSize = size;
|
2015-12-18 05:32:59 +08:00
|
|
|
_offset = offset;
|
2015-08-25 23:31:34 +08:00
|
|
|
|
|
|
|
_capInsetsInternal = capInsets;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-05 08:11:37 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
2015-10-29 14:27:27 +08:00
|
|
|
_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
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-12-04 03:17:47 +08:00
|
|
|
applyBlendFunc();
|
2016-02-11 21:18:41 +08:00
|
|
|
if (getGLProgramState()) {
|
|
|
|
_scale9Image->setGLProgramState(getGLProgramState());
|
|
|
|
} else {
|
|
|
|
this->setState(_brightState);
|
|
|
|
}
|
2015-05-21 16:04:37 +08:00
|
|
|
if(this->_isPatch9)
|
|
|
|
{
|
|
|
|
size.width = size.width - 2;
|
|
|
|
size.height = size.height - 2;
|
|
|
|
}
|
2014-09-27 20:54:07 +08:00
|
|
|
this->setContentSize(size);
|
2015-12-09 14:21:26 +08:00
|
|
|
|
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);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-09-27 20:54:07 +08:00
|
|
|
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)
|
2014-09-30 14:46:22 +08:00
|
|
|
{
|
2015-10-29 10:41:58 +08:00
|
|
|
Texture2D *tex = _scale9Image ? _scale9Image->getTexture() : nullptr;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-10-29 10:41:58 +08:00
|
|
|
if (tex == nullptr)
|
2015-05-21 16:04:37 +08:00
|
|
|
{
|
2015-10-29 10:41:58 +08:00
|
|
|
return;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
2015-10-30 09:58:02 +08:00
|
|
|
|
|
|
|
if (_renderingType == RenderingType::SIMPLE)
|
2015-05-21 16:04:37 +08:00
|
|
|
{
|
2015-10-30 09:58:02 +08:00
|
|
|
this->configureSimpleModeRendering();
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-10-29 10:41:58 +08:00
|
|
|
auto capInsets = CC_RECT_POINTS_TO_PIXELS(_capInsetsInternal);
|
|
|
|
auto textureRect = CC_RECT_POINTS_TO_PIXELS(_spriteRect);
|
2015-12-18 05:32:59 +08:00
|
|
|
auto spriteRectSize = _spriteRect.size;
|
|
|
|
auto originalSize = CC_SIZE_POINTS_TO_PIXELS(_originalSize);
|
|
|
|
auto offset = CC_POINT_POINTS_TO_PIXELS(_offset);
|
|
|
|
|
|
|
|
Vec4 offsets;
|
|
|
|
offsets.x = offset.x + (originalSize.width - textureRect.size.width) / 2;
|
|
|
|
offsets.w = offset.y + (originalSize.height - textureRect.size.height) / 2;
|
|
|
|
offsets.z = originalSize.width - textureRect.size.width - offsets.x;
|
|
|
|
offsets.y = originalSize.height - textureRect.size.height - offsets.w;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-10-29 10:41:58 +08:00
|
|
|
//handle .9.png
|
|
|
|
if (_isPatch9)
|
2015-05-21 16:04:37 +08:00
|
|
|
{
|
2015-12-18 05:32:59 +08:00
|
|
|
originalSize = Size(originalSize.width - 2, originalSize.height-2);
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
|
|
|
|
2015-10-29 10:41:58 +08:00
|
|
|
if(capInsets.equals(Rect::ZERO))
|
|
|
|
{
|
2015-12-18 05:32:59 +08:00
|
|
|
capInsets = Rect(originalSize.width/3, originalSize.height/3,
|
|
|
|
originalSize.width/3, originalSize.height/3);
|
2015-10-29 10:41:58 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-12-18 05:32:59 +08:00
|
|
|
auto uv = this->calculateUV(tex, capInsets, originalSize, offsets);
|
|
|
|
auto vertices = this->calculateVertices(capInsets, originalSize, offsets);
|
2015-10-29 10:41:58 +08:00
|
|
|
auto triangles = this->calculateTriangles(uv, vertices);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2016-05-25 07:57:40 +08:00
|
|
|
auto polyInfo = _scale9Image->getPolygonInfo();
|
|
|
|
polyInfo.setTriangles(triangles);
|
|
|
|
_scale9Image->setPolygonInfo(polyInfo);
|
2014-09-27 20:54:07 +08:00
|
|
|
}
|
2014-07-29 10:39:38 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +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))
|
2014-09-27 20:54:07 +08:00
|
|
|
{
|
2015-10-29 10:41:58 +08:00
|
|
|
return;
|
2014-09-27 20:54:07 +08:00
|
|
|
}
|
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
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-09-27 20:54:07 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::resizableSpriteWithCapInsets(const Rect& capInsets) const
|
2014-07-29 10:39:38 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Scale9Sprite* pReturn = new (std::nothrow) Scale9Sprite();
|
2015-05-21 16:04:37 +08:00
|
|
|
if ( pReturn && pReturn->init(_scale9Image,
|
|
|
|
_spriteRect,
|
|
|
|
_spriteFrameRotated,
|
2015-10-29 10:41:58 +08:00
|
|
|
Vec2::ZERO,
|
2015-05-21 16:04:37 +08:00
|
|
|
_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);
|
2016-05-25 02:09:11 +08:00
|
|
|
return nullptr;
|
2014-07-29 10:39:38 +08:00
|
|
|
}
|
2015-07-15 14:35:13 +08:00
|
|
|
|
|
|
|
Scale9Sprite::State Scale9Sprite::getState()const
|
|
|
|
{
|
|
|
|
return _brightState;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
void Scale9Sprite::setState(cocos2d::ui::Scale9Sprite::State state)
|
|
|
|
{
|
|
|
|
GLProgramState *glState = nullptr;
|
|
|
|
switch (state)
|
|
|
|
{
|
2015-05-21 16:04:37 +08:00
|
|
|
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
|
|
|
}
|
2015-07-15 14:35:13 +08:00
|
|
|
|
2016-02-11 21:18:41 +08:00
|
|
|
setGLProgramState(glState);
|
2015-07-15 14:35:13 +08:00
|
|
|
_brightState = state;
|
2014-12-23 15:03:31 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-05-21 16:04:37 +08:00
|
|
|
/** sets the opacity.
|
2015-12-21 07:21:45 +08:00
|
|
|
@warning If the texture has premultiplied alpha then, the R, G and B channels will be modified.
|
2015-05-21 16:04:37 +08:00
|
|
|
Values goes from 0 to 255, where 255 means fully opaque.
|
|
|
|
*/
|
2015-04-20 17:00:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
|
|
|
|
2014-09-27 20:54:07 +08:00
|
|
|
void Scale9Sprite::setSpriteFrame(SpriteFrame * spriteFrame, const Rect& capInsets)
|
2014-07-29 10:39:38 +08:00
|
|
|
{
|
|
|
|
Sprite * sprite = Sprite::createWithTexture(spriteFrame->getTexture());
|
2015-05-21 16:04:37 +08:00
|
|
|
this->updateWithSprite(sprite,
|
|
|
|
spriteFrame->getRect(),
|
|
|
|
spriteFrame->isRotated(),
|
|
|
|
spriteFrame->getOffset(),
|
|
|
|
spriteFrame->getOriginalSize(),
|
|
|
|
capInsets);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
// Reset insets
|
2014-09-27 20:54:07 +08:00
|
|
|
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-04-20 17:00:40 +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
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
void Scale9Sprite::setCapInsets(const Rect& capInsets)
|
|
|
|
{
|
|
|
|
Size contentSize = this->_contentSize;
|
2015-05-21 16:04:37 +08:00
|
|
|
this->updateWithSprite(this->_scale9Image,
|
|
|
|
_spriteRect,
|
|
|
|
_spriteFrameRotated,
|
2015-12-18 05:32:59 +08:00
|
|
|
_offset,
|
2015-05-21 16:04:37 +08:00
|
|
|
_originalSize,
|
|
|
|
capInsets);
|
2014-09-27 20:54:07 +08:00
|
|
|
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);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
void Scale9Sprite::setInsetLeft(float insetLeft)
|
|
|
|
{
|
|
|
|
this->_insetLeft = insetLeft;
|
|
|
|
this->updateCapInset();
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
void Scale9Sprite::setInsetTop(float insetTop)
|
|
|
|
{
|
|
|
|
this->_insetTop = insetTop;
|
|
|
|
this->updateCapInset();
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
void Scale9Sprite::setInsetRight(float insetRight)
|
|
|
|
{
|
|
|
|
this->_insetRight = insetRight;
|
|
|
|
this->updateCapInset();
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
void Scale9Sprite::setInsetBottom(float insetBottom)
|
|
|
|
{
|
|
|
|
this->_insetBottom = insetBottom;
|
|
|
|
this->updateCapInset();
|
|
|
|
}
|
2015-12-03 17:53:02 +08:00
|
|
|
|
|
|
|
void Scale9Sprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
|
|
|
{
|
2015-12-14 10:09:06 +08:00
|
|
|
if (_scale9Image && _scale9Enabled) {
|
2015-12-03 17:53:02 +08:00
|
|
|
#if CC_USE_CULLING
|
|
|
|
// Don't do calculate the culling if the transform was not updated
|
|
|
|
auto visitingCamera = Camera::getVisitingCamera();
|
|
|
|
auto defaultCamera = Camera::getDefaultCamera();
|
|
|
|
if (visitingCamera == defaultCamera) {
|
|
|
|
_insideBounds = ((flags & FLAGS_TRANSFORM_DIRTY)|| visitingCamera->isViewProjectionUpdated()) ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_insideBounds = renderer->checkVisibility(transform, _contentSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_insideBounds)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
auto textureName = _scale9Image->getTexture()->getName();
|
|
|
|
auto programState = _scale9Image->getGLProgramState();
|
|
|
|
auto blendFunc = _scale9Image->getBlendFunc();
|
|
|
|
auto& polyInfo = _scale9Image->getPolygonInfo();
|
|
|
|
auto globalZOrder = _scale9Image->getGlobalZOrder();
|
|
|
|
_trianglesCommand.init(globalZOrder,textureName, programState, blendFunc, polyInfo.triangles, transform, flags);
|
|
|
|
renderer->addCommand(&_trianglesCommand);
|
|
|
|
|
|
|
|
#if CC_SPRITE_DEBUG_DRAW
|
|
|
|
_debugDrawNode->clear();
|
|
|
|
auto count = polyInfo.triangles.indexCount/3;
|
|
|
|
auto indices = polyInfo.triangles.indices;
|
|
|
|
auto verts = polyInfo.triangles.verts;
|
|
|
|
for(ssize_t i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
//draw 3 lines
|
|
|
|
Vec3 from =verts[indices[i*3]].vertices;
|
|
|
|
Vec3 to = verts[indices[i*3+1]].vertices;
|
|
|
|
_debugDrawNode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::WHITE);
|
|
|
|
|
|
|
|
from =verts[indices[i*3+1]].vertices;
|
|
|
|
to = verts[indices[i*3+2]].vertices;
|
|
|
|
_debugDrawNode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::WHITE);
|
|
|
|
|
|
|
|
from =verts[indices[i*3+2]].vertices;
|
|
|
|
to = verts[indices[i*3]].vertices;
|
|
|
|
_debugDrawNode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::WHITE);
|
|
|
|
}
|
|
|
|
#endif //CC_SPRITE_DEBUG_DRAW
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
void Scale9Sprite::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
uint32_t flags = processParentFlags(parentTransform, parentFlags);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
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);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
int i = 0; // used by _children
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
sortAllChildren();
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
//
|
|
|
|
// draw children and protectedChildren zOrder < 0
|
|
|
|
//
|
|
|
|
for( ; i < _children.size(); i++ )
|
|
|
|
{
|
|
|
|
auto node = _children.at(i);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
if ( node && node->getLocalZOrder() < 0 )
|
|
|
|
node->visit(renderer, _modelViewTransform, flags);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2015-12-14 10:09:06 +08:00
|
|
|
|
|
|
|
if (!_scale9Enabled && _scale9Image && _scale9Image->getLocalZOrder() < 0 )
|
|
|
|
{
|
|
|
|
_scale9Image->visit(renderer, _modelViewTransform, flags);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
// draw self
|
|
|
|
//
|
2014-08-14 10:26:37 +08:00
|
|
|
if (isVisitableByVisitingCamera())
|
2014-08-13 12:06:51 +08:00
|
|
|
this->draw(renderer, _modelViewTransform, flags);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-12-14 10:09:06 +08:00
|
|
|
if (!_scale9Enabled && _scale9Image && _scale9Image->getLocalZOrder() >= 0 )
|
|
|
|
{
|
|
|
|
_scale9Image->visit(renderer, _modelViewTransform, flags);
|
|
|
|
}
|
2015-04-20 17:00:40 +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);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
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);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
Size Scale9Sprite::getOriginalSize()const
|
|
|
|
{
|
|
|
|
return _originalSize;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
Size Scale9Sprite::getPreferredSize() const
|
|
|
|
{
|
|
|
|
return _preferredSize;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
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
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
float Scale9Sprite::getInsetLeft()const
|
|
|
|
{
|
|
|
|
return this->_insetLeft;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
float Scale9Sprite::getInsetTop()const
|
|
|
|
{
|
|
|
|
return this->_insetTop;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
float Scale9Sprite::getInsetRight()const
|
|
|
|
{
|
|
|
|
return this->_insetRight;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
float Scale9Sprite::getInsetBottom()const
|
|
|
|
{
|
|
|
|
return this->_insetBottom;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
void Scale9Sprite::setScale9Enabled(bool enabled)
|
|
|
|
{
|
2014-08-05 08:11:37 +08:00
|
|
|
if (_scale9Enabled == enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-29 10:39:38 +08:00
|
|
|
_scale9Enabled = enabled;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-05 08:11:37 +08:00
|
|
|
this->cleanupSlicedSprites();
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-12-21 07:21:45 +08:00
|
|
|
//we must invalid the transform when toggling scale9enabled
|
2014-11-07 11:09:22 +08:00
|
|
|
_transformUpdated = _transformDirty = _inverseDirty = true;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-05 08:11:37 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
if (_scale9Image)
|
|
|
|
{
|
2015-05-21 16:04:37 +08:00
|
|
|
this->updateWithSprite(this->_scale9Image,
|
|
|
|
_spriteRect,
|
|
|
|
_spriteFrameRotated,
|
2015-10-29 10:41:58 +08:00
|
|
|
Vec2::ZERO,
|
2015-05-21 16:04:37 +08:00
|
|
|
_originalSize,
|
2015-10-29 10:41:58 +08:00
|
|
|
_capInsetsInternal);
|
2014-08-05 08:11:37 +08:00
|
|
|
}
|
2014-07-31 11:07:20 +08:00
|
|
|
}
|
2015-10-29 10:41:58 +08:00
|
|
|
else
|
2014-08-05 08:11:37 +08:00
|
|
|
{
|
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-04-20 17:00:40 +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;
|
2014-08-05 08:11:37 +08:00
|
|
|
}
|
2015-10-29 10:41:58 +08:00
|
|
|
|
2014-08-05 08:11:37 +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;
|
2014-08-05 08:11:37 +08:00
|
|
|
_scale9Image->setAnchorPoint(position);
|
2015-10-29 21:53:58 +08:00
|
|
|
this->adjustNoneScale9ImagePosition();
|
2014-08-05 08:11:37 +08:00
|
|
|
}
|
2014-07-29 10:39:38 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-20 17:00:40 +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-07-28 16:07:14 +08:00
|
|
|
{
|
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-04-20 17:00:40 +08:00
|
|
|
|
2015-10-29 10:41:58 +08:00
|
|
|
}
|
2015-07-28 16:07:14 +08:00
|
|
|
}
|
2014-07-29 10:39:38 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +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();
|
2015-04-20 17:00:40 +08:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
void Scale9Sprite::updateDisplayedOpacity(GLubyte parentOpacity)
|
|
|
|
{
|
|
|
|
_displayedOpacity = _realOpacity * parentOpacity/255.0;
|
|
|
|
updateColor();
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-30 15:19:39 +08:00
|
|
|
if (_scale9Image)
|
|
|
|
{
|
2014-07-29 10:39:38 +08:00
|
|
|
_scale9Image->updateDisplayedOpacity(_displayedOpacity);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-22 11:49:39 +08:00
|
|
|
void Scale9Sprite::disableCascadeOpacity()
|
|
|
|
{
|
|
|
|
_displayedOpacity = _realOpacity;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-22 11:49:39 +08:00
|
|
|
for(auto child : _children){
|
|
|
|
child->updateDisplayedOpacity(255);
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 21:18:41 +08:00
|
|
|
|
|
|
|
void Scale9Sprite::setGLProgram(GLProgram *glprogram) {
|
|
|
|
Node::setGLProgram(glprogram);
|
|
|
|
if (_scale9Image) {
|
|
|
|
_scale9Image->setGLProgram(glprogram);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scale9Sprite::setGLProgramState(GLProgramState *glProgramState) {
|
|
|
|
Node::setGLProgramState(glProgramState);
|
|
|
|
if (_scale9Image) {
|
|
|
|
_scale9Image->setGLProgramState(glProgramState);
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
Sprite* Scale9Sprite::getSprite()const
|
|
|
|
{
|
|
|
|
return _scale9Image;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 14:33:22 +08:00
|
|
|
void Scale9Sprite::setFlippedX(bool flippedX)
|
|
|
|
{
|
2015-04-20 17:00:40 +08:00
|
|
|
|
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
|
|
|
}
|
2015-04-20 17:00:40 +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
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 14:33:22 +08:00
|
|
|
bool Scale9Sprite::isFlippedX()const
|
|
|
|
{
|
|
|
|
return _flippedX;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-29 14:33:22 +08:00
|
|
|
bool Scale9Sprite::isFlippedY()const
|
|
|
|
{
|
|
|
|
return _flippedY;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-07 11:09:22 +08:00
|
|
|
void Scale9Sprite::setScaleX(float scaleX)
|
|
|
|
{
|
|
|
|
if (_flippedX) {
|
|
|
|
scaleX = scaleX * -1;
|
|
|
|
}
|
|
|
|
Node::setScaleX(scaleX);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-07 11:09:22 +08:00
|
|
|
void Scale9Sprite::setScaleY(float scaleY)
|
|
|
|
{
|
|
|
|
if (_flippedY) {
|
|
|
|
scaleY = scaleY * -1;
|
|
|
|
}
|
|
|
|
Node::setScaleY(scaleY);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-07 11:09:22 +08:00
|
|
|
void Scale9Sprite::setScale(float scale)
|
|
|
|
{
|
|
|
|
this->setScaleX(scale);
|
|
|
|
this->setScaleY(scale);
|
|
|
|
this->setScaleZ(scale);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-07 11:09:22 +08:00
|
|
|
void Scale9Sprite::setScale(float scaleX, float scaleY)
|
|
|
|
{
|
|
|
|
this->setScaleX(scaleX);
|
|
|
|
this->setScaleY(scaleY);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-07 11:09:22 +08:00
|
|
|
float Scale9Sprite::getScaleX()const
|
|
|
|
{
|
|
|
|
float originalScale = Node::getScaleX();
|
|
|
|
if (_flippedX)
|
|
|
|
{
|
2016-07-04 22:12:45 +08:00
|
|
|
originalScale = originalScale * -1.0f;
|
2014-11-07 11:09:22 +08:00
|
|
|
}
|
|
|
|
return originalScale;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-07 11:09:22 +08:00
|
|
|
float Scale9Sprite::getScaleY()const
|
|
|
|
{
|
|
|
|
float originalScale = Node::getScaleY();
|
|
|
|
if (_flippedY)
|
|
|
|
{
|
2016-07-04 22:12:45 +08:00
|
|
|
originalScale = originalScale * -1.0f;
|
2014-11-07 11:09:22 +08:00
|
|
|
}
|
|
|
|
return originalScale;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-07 11:09:22 +08:00
|
|
|
float Scale9Sprite::getScale()const
|
|
|
|
{
|
2015-05-21 16:04:37 +08:00
|
|
|
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-04-20 17:00:40 +08:00
|
|
|
|
2015-02-12 09:24:04 +08:00
|
|
|
void Scale9Sprite::setCameraMask(unsigned short mask, bool applyChildren)
|
|
|
|
{
|
|
|
|
Node::setCameraMask(mask, applyChildren);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
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,
|
2015-12-18 05:32:59 +08:00
|
|
|
const Size& originalSize,
|
|
|
|
const Vec4& offsets)
|
2015-10-29 10:41:58 +08:00
|
|
|
{
|
|
|
|
auto atlasWidth = tex->getPixelsWide();
|
|
|
|
auto atlasHeight = tex->getPixelsHigh();
|
|
|
|
|
2015-12-21 07:21:45 +08:00
|
|
|
//calculate texture coordinate
|
2015-10-29 10:41:58 +08:00
|
|
|
float leftWidth = 0, centerWidth = 0, rightWidth = 0;
|
|
|
|
float topHeight = 0, centerHeight = 0, bottomHeight = 0;
|
|
|
|
|
|
|
|
if (_spriteFrameRotated)
|
|
|
|
{
|
2015-12-18 05:32:59 +08:00
|
|
|
rightWidth = capInsets.origin.y - offsets.y;
|
2015-10-29 10:41:58 +08:00
|
|
|
centerWidth = capInsets.size.height;
|
2015-12-18 05:32:59 +08:00
|
|
|
leftWidth = originalSize.height - centerWidth - capInsets.origin.y - offsets.w;
|
2015-10-29 10:41:58 +08:00
|
|
|
|
2015-12-18 05:32:59 +08:00
|
|
|
topHeight = capInsets.origin.x - offsets.x;
|
2015-10-29 10:41:58 +08:00
|
|
|
centerHeight = capInsets.size.width;
|
2015-12-18 05:32:59 +08:00
|
|
|
bottomHeight = originalSize.width - (capInsets.origin.x + centerHeight) - offsets.z;
|
2015-10-29 10:41:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-18 05:32:59 +08:00
|
|
|
leftWidth = capInsets.origin.x - offsets.x;
|
2015-10-29 10:41:58 +08:00
|
|
|
centerWidth = capInsets.size.width;
|
2015-12-18 05:32:59 +08:00
|
|
|
rightWidth = originalSize.width - (capInsets.origin.x + centerWidth) - offsets.z;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-12-18 05:32:59 +08:00
|
|
|
topHeight = capInsets.origin.y - offsets.y;
|
2015-10-29 10:41:58 +08:00
|
|
|
centerHeight = capInsets.size.height;
|
2015-12-19 03:40:12 +08:00
|
|
|
bottomHeight = originalSize.height - (capInsets.origin.y + centerHeight) - offsets.w;
|
2015-12-18 05:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(leftWidth<0)
|
|
|
|
{
|
|
|
|
centerWidth += leftWidth;
|
|
|
|
leftWidth = 0;
|
|
|
|
}
|
|
|
|
if(rightWidth<0)
|
|
|
|
{
|
|
|
|
centerWidth += rightWidth;
|
|
|
|
rightWidth = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(topHeight<0)
|
|
|
|
{
|
|
|
|
centerHeight += topHeight;
|
|
|
|
topHeight = 0;
|
|
|
|
}
|
|
|
|
if(bottomHeight<0)
|
|
|
|
{
|
|
|
|
centerHeight += bottomHeight;
|
|
|
|
bottomHeight = 0;
|
2015-10-29 10:41:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2015-12-18 05:32:59 +08:00
|
|
|
const Size& originalSize,
|
|
|
|
const Vec4& offsets)
|
2015-10-29 10:41:58 +08:00
|
|
|
{
|
2015-10-29 23:13:36 +08:00
|
|
|
|
2015-12-18 05:32:59 +08:00
|
|
|
float offsetLeft = offsets.x / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
float offsetTop = offsets.y / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
float offsetRight = offsets.z / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
float offsetBottom = offsets.w / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
|
|
|
|
std::vector<Vec2> vertices;
|
2015-10-29 23:13:36 +08:00
|
|
|
if (_renderingType == RenderingType::SIMPLE)
|
|
|
|
{
|
2015-12-18 05:32:59 +08:00
|
|
|
float hScale = _preferredSize.width / (originalSize.width / CC_CONTENT_SCALE_FACTOR());
|
|
|
|
float vScale = _preferredSize.height / (originalSize.height / CC_CONTENT_SCALE_FACTOR());
|
|
|
|
|
|
|
|
vertices = {Vec2(offsetLeft * hScale, offsetBottom * vScale),
|
|
|
|
Vec2(_preferredSize.width - offsetRight * hScale, _preferredSize.height - offsetTop * vScale)};
|
2015-10-29 23:13:36 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-18 05:32:59 +08:00
|
|
|
float leftWidth = 0, centerWidth = 0, rightWidth = 0;
|
|
|
|
float topHeight = 0, centerHeight = 0, bottomHeight = 0;
|
|
|
|
|
|
|
|
leftWidth = capInsets.origin.x;
|
|
|
|
centerWidth = capInsets.size.width;
|
|
|
|
rightWidth = originalSize.width - (leftWidth + centerWidth);
|
|
|
|
|
|
|
|
topHeight = capInsets.origin.y;
|
|
|
|
centerHeight = capInsets.size.height;
|
|
|
|
bottomHeight = originalSize.height - (topHeight + centerHeight);
|
|
|
|
|
|
|
|
leftWidth = leftWidth / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
rightWidth = rightWidth / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
centerWidth = centerWidth / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
topHeight = topHeight / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
bottomHeight = bottomHeight / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
centerHeight = centerHeight / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
|
|
|
|
float sizableWidth = _preferredSize.width - leftWidth - rightWidth;
|
|
|
|
float sizableHeight = _preferredSize.height - topHeight - bottomHeight;
|
|
|
|
|
|
|
|
leftWidth -= offsetLeft;
|
|
|
|
rightWidth -= offsetRight;
|
|
|
|
topHeight -= offsetTop;
|
|
|
|
bottomHeight -= offsetBottom;
|
|
|
|
|
|
|
|
float hScale = sizableWidth / centerWidth;
|
|
|
|
float vScale = sizableHeight / centerHeight;
|
|
|
|
|
|
|
|
if(leftWidth<0)
|
|
|
|
{
|
|
|
|
offsetLeft -= leftWidth * (hScale - 1.0f);
|
|
|
|
sizableWidth += leftWidth * hScale;
|
|
|
|
leftWidth = 0;
|
|
|
|
}
|
|
|
|
if(rightWidth<0)
|
|
|
|
{
|
|
|
|
sizableWidth += rightWidth * hScale;
|
|
|
|
rightWidth = 0;
|
|
|
|
}
|
|
|
|
if(topHeight<0)
|
|
|
|
{
|
|
|
|
sizableHeight += topHeight * vScale;
|
|
|
|
topHeight = 0;
|
|
|
|
}
|
|
|
|
if(bottomHeight<0)
|
|
|
|
{
|
|
|
|
offsetBottom -= bottomHeight * (vScale - 1.0f);
|
|
|
|
sizableHeight += bottomHeight * vScale;
|
|
|
|
bottomHeight = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float x0,x1,x2,x3;
|
|
|
|
float y0,y1,y2,y3;
|
|
|
|
if(sizableWidth >= 0)
|
|
|
|
{
|
|
|
|
x0 = offsetLeft;
|
|
|
|
x1 = x0 + leftWidth;
|
|
|
|
x2 = x1 + sizableWidth;
|
|
|
|
x3 = x2 + rightWidth;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float xScale = _preferredSize.width / (leftWidth + rightWidth);
|
|
|
|
x0 = offsetLeft;
|
|
|
|
x1 = x2 = offsetLeft + leftWidth * xScale;
|
|
|
|
x3 = x2 + rightWidth * xScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sizableHeight >= 0)
|
|
|
|
{
|
|
|
|
y0 = offsetBottom;
|
|
|
|
y1 = y0 + bottomHeight;
|
|
|
|
y2 = y1 + sizableHeight;
|
|
|
|
y3 = y2 + topHeight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float yScale = _preferredSize.height / (topHeight + bottomHeight);
|
|
|
|
y0 = offsetBottom;
|
|
|
|
y1 = y2 = y0 + bottomHeight * yScale;
|
|
|
|
y3 = y2 + topHeight * yScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
vertices = {Vec2(x0,y0), Vec2(x1,y1), Vec2(x2,y2), Vec2(x3,y3)};
|
2015-10-29 23:13:36 +08:00
|
|
|
}
|
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-04-20 17:00:40 +08:00
|
|
|
|
2015-12-16 14:02:55 +08:00
|
|
|
_sliceVertices = new (std::nothrow) V3F_C4B_T2F[slicedTotalVertexCount];
|
|
|
|
_sliceIndices = new (std::nothrow) unsigned short[slicedTotalIndices];
|
2015-10-29 10:41:58 +08:00
|
|
|
|
|
|
|
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};
|
|
|
|
|
2015-11-20 17:35:29 +08:00
|
|
|
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
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-11-18 13:38:29 +08:00
|
|
|
void Scale9Sprite::resetRender()
|
|
|
|
{
|
|
|
|
// Release old sprites
|
|
|
|
this->cleanupSlicedSprites();
|
|
|
|
|
|
|
|
CC_SAFE_RELEASE_NULL(this->_scale9Image);
|
|
|
|
}
|
2015-12-14 10:09:06 +08:00
|
|
|
|
|
|
|
void Scale9Sprite::setGlobalZOrder(float globalZOrder)
|
|
|
|
{
|
|
|
|
Node::setGlobalZOrder(globalZOrder);
|
|
|
|
if (_scale9Image)
|
|
|
|
{
|
|
|
|
_scale9Image->setGlobalZOrder(globalZOrder);
|
|
|
|
}
|
|
|
|
}
|
2015-11-18 13:38:29 +08:00
|
|
|
|
2014-07-29 10:39:38 +08:00
|
|
|
}}
|