2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-09-05 20:45:32 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-09-05 20:45:32 +08:00
|
|
|
https://adxe.org
|
2019-11-23 20:27:39 +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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ui/UIScale9Sprite.h"
|
|
|
|
#include "2d/CCSprite.h"
|
|
|
|
#include "2d/CCSpriteFrameCache.h"
|
|
|
|
#include "base/CCVector.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
#include "renderer/ccShaders.h"
|
|
|
|
#include "platform/CCImage.h"
|
|
|
|
#include "base/CCNinePatchImageParser.h"
|
|
|
|
#include "2d/CCDrawNode.h"
|
|
|
|
#include "2d/CCCamera.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/backend/ProgramStateRegistry.h"
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
using namespace cocos2d::ui;
|
|
|
|
|
|
|
|
Scale9Sprite* Scale9Sprite::create()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Scale9Sprite* ret = new Scale9Sprite();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite* Scale9Sprite::create(const std::string& filename, const Rect& rect, const Rect& capInsets)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Scale9Sprite* ret = new Scale9Sprite();
|
|
|
|
if (ret->initWithFile(filename, rect, capInsets))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite* Scale9Sprite::create(const std::string& filename, const Rect& rect)
|
|
|
|
{
|
|
|
|
return create(filename, rect, Rect::ZERO);
|
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite* Scale9Sprite::create(const Rect& capInsets, const std::string& file)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Scale9Sprite* ret = new Scale9Sprite();
|
|
|
|
if (ret->initWithFile(capInsets, file))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite* Scale9Sprite::create(const std::string& fileaname)
|
|
|
|
{
|
|
|
|
return create(Rect::ZERO, fileaname);
|
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite* Scale9Sprite::createWithSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Scale9Sprite* ret = new Scale9Sprite();
|
|
|
|
if (ret->initWithSpriteFrame(spriteFrame, capInsets))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite* Scale9Sprite::createWithSpriteFrame(SpriteFrame* spriteFrame)
|
|
|
|
{
|
|
|
|
return createWithSpriteFrame(spriteFrame, Rect::ZERO);
|
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Scale9Sprite* ret = new Scale9Sprite();
|
|
|
|
if (ret->initWithSpriteFrameName(spriteFrameName, capInsets))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const std::string& spriteFrameName)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Scale9Sprite* ret = new Scale9Sprite();
|
|
|
|
if (ret->initWithSpriteFrameName(spriteFrameName, Rect::ZERO))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
|
|
|
|
log("Could not allocate Scale9Sprite()");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite::Scale9Sprite()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _isPatch9(false)
|
|
|
|
, _insetLeft(0)
|
|
|
|
, _insetRight(0)
|
|
|
|
, _insetTop(0)
|
|
|
|
, _insetBottom(0)
|
|
|
|
, _brightState(State::NORMAL)
|
|
|
|
, _renderingType(RenderingType::SLICE)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Scale9Sprite::~Scale9Sprite() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool Scale9Sprite::initWithFile(const Rect& capInsets, const std::string& file)
|
|
|
|
{
|
|
|
|
// calls super
|
|
|
|
bool ret = Sprite::initWithFile(file);
|
|
|
|
setupSlice9(getTexture(), capInsets);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::initWithFile(const std::string& filename)
|
|
|
|
{
|
|
|
|
// calls super
|
|
|
|
auto originalCapInsets = this->getCapInsets();
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ret = Sprite::initWithFile(filename);
|
2019-11-23 20:27:39 +08:00
|
|
|
setupSlice9(getTexture(), originalCapInsets);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::initWithFile(const std::string& filename, const Rect& rect)
|
|
|
|
{
|
|
|
|
// calls super
|
|
|
|
auto originalCapInsets = this->getCapInsets();
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ret = Sprite::initWithFile(filename, rect);
|
2019-11-23 20:27:39 +08:00
|
|
|
setupSlice9(getTexture(), originalCapInsets);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::initWithSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets)
|
|
|
|
{
|
|
|
|
// calls super
|
|
|
|
bool ret = Sprite::initWithSpriteFrame(spriteFrame);
|
|
|
|
setupSlice9(getTexture(), capInsets);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::initWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets)
|
|
|
|
{
|
|
|
|
// calls super
|
|
|
|
bool ret = Sprite::initWithSpriteFrameName(spriteFrameName);
|
|
|
|
setupSlice9(getTexture(), capInsets);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::initWithSpriteFrameName(const std::string& spriteFrameName)
|
|
|
|
{
|
|
|
|
// calls super
|
|
|
|
auto originalCapInsets = this->getCapInsets();
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ret = Sprite::initWithSpriteFrameName(spriteFrameName);
|
2019-11-23 20:27:39 +08:00
|
|
|
setupSlice9(getTexture(), originalCapInsets);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::init()
|
|
|
|
{
|
|
|
|
// calls super
|
|
|
|
bool ret = Sprite::init();
|
|
|
|
setupSlice9(getTexture(), Rect::ZERO);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::init(Sprite* sprite, const Rect& rect, const Rect& capInsets)
|
|
|
|
{
|
|
|
|
return 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);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Scale9Sprite::init(Sprite* sprite,
|
|
|
|
const Rect& origRect,
|
|
|
|
bool rotated,
|
|
|
|
const Vec2& offset,
|
|
|
|
const Vec2& originalSize,
|
|
|
|
const Rect& capInsets)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
Rect rect(origRect);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (sprite)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto texture = sprite->getTexture();
|
|
|
|
|
|
|
|
if (origRect.equals(Rect::ZERO))
|
|
|
|
rect.size = texture->getContentSize();
|
|
|
|
|
|
|
|
auto spriteFrame = SpriteFrame::createWithTexture(texture, rect, rotated, offset, originalSize);
|
2021-12-25 10:04:45 +08:00
|
|
|
ret = initWithSpriteFrame(spriteFrame);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
setupSlice9(texture, capInsets);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
ret = initWithTexture(nullptr, rect, rotated);
|
|
|
|
setupSlice9(nullptr, capInsets);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::initWithFile(const std::string& filename, const Rect& rect, const Rect& capInsets)
|
|
|
|
{
|
|
|
|
// calls super
|
|
|
|
bool ret = false;
|
|
|
|
if (!rect.equals(Rect::ZERO))
|
|
|
|
{
|
|
|
|
ret = Sprite::initWithFile(filename, rect);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else // if rect is zero, use the whole texture size.
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret = Sprite::initWithFile(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
setupSlice9(getTexture(), capInsets);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Scale9Sprite::updateWithSprite(Sprite* sprite, const Rect& rect, bool rotated, const Rect& capInsets)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return updateWithSprite(sprite, rect, rotated, Vec2::ZERO, rect.size, capInsets);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::updateWithSprite(Sprite* sprite,
|
|
|
|
const Rect& textureRect,
|
|
|
|
bool rotated,
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& offset,
|
|
|
|
const Vec2& originalSize,
|
2019-11-23 20:27:39 +08:00
|
|
|
const Rect& capInsets)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* spriteframe =
|
|
|
|
SpriteFrame::createWithTexture(sprite->getTexture(), CC_RECT_POINTS_TO_PIXELS(textureRect), rotated,
|
|
|
|
CC_POINT_POINTS_TO_PIXELS(offset), CC_SIZE_POINTS_TO_PIXELS(originalSize));
|
2019-11-23 20:27:39 +08:00
|
|
|
setSpriteFrame(spriteframe);
|
|
|
|
setCapInsets(capInsets);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite* Scale9Sprite::resizableSpriteWithCapInsets(const Rect& capInsets) const
|
|
|
|
{
|
|
|
|
// FIXME: there are no test cases for this method
|
2021-12-08 00:11:53 +08:00
|
|
|
Scale9Sprite* ret = new Scale9Sprite();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ret->init(const_cast<Scale9Sprite*>(this), _rect, _rectRotated, Vec2::ZERO, _originalContentSize, capInsets))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite::State Scale9Sprite::getState() const
|
|
|
|
{
|
|
|
|
return _brightState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scale9Sprite::setState(Scale9Sprite::State state)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_brightState != state)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_brightState = state;
|
|
|
|
switch (state)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case State::NORMAL:
|
|
|
|
Sprite::setProgramState(backend::ProgramType::POSITION_TEXTURE_COLOR);
|
|
|
|
break;
|
|
|
|
case State::GRAY:
|
|
|
|
Sprite::setProgramState(backend::ProgramType::GRAY_SCALE);
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_brightState = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Scale9Sprite::setSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
setSpriteFrame(spriteFrame);
|
|
|
|
setCapInsets(capInsets);
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
void Scale9Sprite::setPreferredSize(const Vec2& preferredSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
setContentSize(preferredSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scale9Sprite::setInsetLeft(float insetLeft)
|
|
|
|
{
|
|
|
|
_insetLeft = insetLeft;
|
|
|
|
updateCapInset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scale9Sprite::setInsetTop(float insetTop)
|
|
|
|
{
|
|
|
|
_insetTop = insetTop;
|
|
|
|
updateCapInset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scale9Sprite::setInsetRight(float insetRight)
|
|
|
|
{
|
|
|
|
_insetRight = insetRight;
|
|
|
|
updateCapInset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scale9Sprite::setInsetBottom(float insetBottom)
|
|
|
|
{
|
|
|
|
_insetBottom = insetBottom;
|
|
|
|
updateCapInset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scale9Sprite::updateCapInset()
|
|
|
|
{
|
|
|
|
if (_renderingType == RenderingType::SLICE)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Rect capInsets(_insetLeft, _insetTop, _originalContentSize.width - _insetRight - _insetLeft,
|
|
|
|
_originalContentSize.height - _insetBottom - _insetTop);
|
2019-11-23 20:27:39 +08:00
|
|
|
setCapInsets(capInsets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 Scale9Sprite::getOriginalSize() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _originalContentSize;
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 Scale9Sprite::getPreferredSize() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return getContentSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
float Scale9Sprite::getInsetLeft() const
|
|
|
|
{
|
|
|
|
return _insetLeft;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Scale9Sprite::getInsetTop() const
|
|
|
|
{
|
|
|
|
return _insetTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Scale9Sprite::getInsetRight() const
|
|
|
|
{
|
|
|
|
return _insetRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Scale9Sprite::getInsetBottom() const
|
|
|
|
{
|
|
|
|
return _insetBottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scale9Sprite::setScale9Enabled(bool enabled)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_renderMode == RenderMode::POLYGON)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
CCLOGWARN("Scale9Sprite::setScale9Enabled() can't be called when using POLYGON render modes");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderingType type = enabled ? RenderingType::SLICE : RenderingType::SIMPLE;
|
|
|
|
setRenderingType(type);
|
|
|
|
|
|
|
|
// only enable stretch when scale9 is enabled
|
|
|
|
// for backward compatibility, since Sprite stretches the texture no matter the rendering type
|
|
|
|
setStretchEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::isScale9Enabled() const
|
|
|
|
{
|
|
|
|
return (_renderingType == RenderingType::SLICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite* Scale9Sprite::getSprite()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns a copy of the Scale9Sprite
|
|
|
|
*/
|
|
|
|
void Scale9Sprite::copyTo(Scale9Sprite* copy) const
|
|
|
|
{
|
|
|
|
copy->initWithSpriteFrame(getSpriteFrame(), getCapInsets());
|
|
|
|
|
|
|
|
copy->setRenderingType(_renderingType);
|
|
|
|
copy->setScale9Enabled(isScale9Enabled());
|
2021-09-05 20:45:32 +08:00
|
|
|
// setStretchEnabled after setScale9Enabled, fix issue: https://github.com/adxeproject/adxe/issues/491
|
|
|
|
copy->setStretchEnabled(isStretchEnabled());
|
2021-12-25 10:04:45 +08:00
|
|
|
copy->_isPatch9 = _isPatch9;
|
2019-11-23 20:27:39 +08:00
|
|
|
copy->_brightState = _brightState;
|
|
|
|
|
|
|
|
// these properties should be part of Sprite::clone() (or Node::clone())
|
|
|
|
// but cloning is not supported on those nodes
|
|
|
|
copy->setContentSize(getContentSize());
|
|
|
|
copy->setPosition(getPosition());
|
|
|
|
copy->setScale(getScaleX(), getScaleY());
|
|
|
|
copy->setRotation(getRotation());
|
|
|
|
copy->setRotationSkewX(getRotationSkewX());
|
|
|
|
copy->setRotationSkewY(getRotationSkewY());
|
|
|
|
copy->setColor(getColor());
|
|
|
|
copy->setOpacity(getOpacity());
|
|
|
|
copy->_originalContentSize = _originalContentSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// (0,0) O = capInsets.origin
|
|
|
|
// v0----------------------
|
|
|
|
// | | | |
|
|
|
|
// | | | |
|
|
|
|
// v1-------O------+------|
|
|
|
|
// | | | |
|
|
|
|
// | | | |
|
|
|
|
// v2-------+------+------|
|
|
|
|
// | | | |
|
|
|
|
// | | | |
|
|
|
|
// v3-------------------- (1,1) (texture coordinate is flipped)
|
|
|
|
// u0 u1 u2 u3
|
|
|
|
|
|
|
|
//
|
|
|
|
// y3----------------------(preferedSize.width, preferedSize.height)
|
|
|
|
// | | | |
|
|
|
|
// | | | |
|
|
|
|
// y2-------O------+------|
|
|
|
|
// | | | |
|
|
|
|
// | | | |
|
|
|
|
// y1-------+------+------|
|
|
|
|
// | | | |
|
|
|
|
// | | | |
|
2021-12-25 10:04:45 +08:00
|
|
|
// x0,y0--------------------
|
2019-11-23 20:27:39 +08:00
|
|
|
// x1 x2 x3
|
|
|
|
|
|
|
|
void Scale9Sprite::setRenderingType(Scale9Sprite::RenderingType type)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_renderMode == RenderMode::POLYGON)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
CCLOGWARN("Scale9Sprite::setRenderingType() can't be called when using POLYGON render modes");
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_renderingType != type)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_renderingType = type;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_renderingType == RenderingType::SIMPLE)
|
|
|
|
{
|
|
|
|
setCenterRectNormalized(Rect(0, 0, 1, 1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
updateCapInset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Scale9Sprite::RenderingType Scale9Sprite::getRenderingType() const
|
|
|
|
{
|
|
|
|
return _renderingType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scale9Sprite::resetRender()
|
|
|
|
{
|
|
|
|
// nothing. keeping it to be backwards compatible
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scale9Sprite::setupSlice9(Texture2D* texture, const Rect& capInsets)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (texture && texture->isContain9PatchInfo())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto& parsedCapInset = texture->getSpriteFrameCapInset(getSpriteFrame());
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!parsedCapInset.equals(Rect::ZERO))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// adjust texture rect. 1.3f seems to be the magic number
|
|
|
|
// to avoid artifacts
|
|
|
|
auto rect = getTextureRect();
|
|
|
|
rect.origin.x += 1.3f;
|
|
|
|
rect.origin.y += 1.3f;
|
|
|
|
rect.size.width -= 2.0f;
|
|
|
|
rect.size.height -= 2.0f;
|
|
|
|
setTextureRect(rect);
|
|
|
|
|
|
|
|
// and after adjusting the texture, set the new cap insets
|
|
|
|
_isPatch9 = true;
|
|
|
|
setCapInsets(parsedCapInset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_isPatch9)
|
|
|
|
{
|
|
|
|
setCapInsets(capInsets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Scale9Sprite::setCapInsets(const cocos2d::Rect& insetsCopy)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Rect insets = insetsCopy;
|
|
|
|
|
|
|
|
// When Insets == Zero --> we should use a 1/3 of its untrimmed size
|
2021-12-25 10:04:45 +08:00
|
|
|
if (insets.equals(Rect::ZERO))
|
|
|
|
{
|
|
|
|
insets = Rect(_originalContentSize.width / 3.0f, _originalContentSize.height / 3.0f,
|
|
|
|
_originalContentSize.width / 3.0f, _originalContentSize.height / 3.0f);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// emulate invalid insets. shouldn't be supported, but the original code supported it.
|
|
|
|
if (insets.origin.x > _originalContentSize.width)
|
|
|
|
insets.origin.x = 0;
|
|
|
|
if (insets.origin.y > _originalContentSize.height)
|
|
|
|
insets.origin.y = 0;
|
|
|
|
if (insets.size.width > _originalContentSize.width)
|
|
|
|
insets.size.width = 1;
|
|
|
|
if (insets.size.height > _originalContentSize.height)
|
|
|
|
insets.size.height = 1;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_insetLeft = insets.origin.x;
|
|
|
|
_insetTop = insets.origin.y;
|
|
|
|
_insetRight = _originalContentSize.width - _insetLeft - insets.size.width;
|
2019-11-23 20:27:39 +08:00
|
|
|
_insetBottom = _originalContentSize.height - _insetTop - insets.size.height;
|
|
|
|
|
|
|
|
// we have to convert from untrimmed to trimmed
|
|
|
|
// Sprite::setCenterRect is using trimmed values (to be compatible with Cocos Creator)
|
|
|
|
// Scale9Sprite::setCapInsects uses untrimmed values (which makes more sense)
|
|
|
|
|
|
|
|
// use _rect coordinates. recenter origin to calculate the
|
|
|
|
// intersecting rectangle
|
|
|
|
// can't use _offsetPosition since it is calculated using bottom-left as origin,
|
|
|
|
// and the center rect is calculated using top-left
|
|
|
|
insets.origin.x -= (_originalContentSize.width - _rect.size.width) / 2 + _unflippedOffsetPositionFromCenter.x;
|
|
|
|
insets.origin.y -= (_originalContentSize.height - _rect.size.height) / 2 - _unflippedOffsetPositionFromCenter.y;
|
|
|
|
|
|
|
|
// intersecting rectangle
|
|
|
|
const float x1 = std::max(insets.origin.x, 0.0f);
|
|
|
|
const float y1 = std::max(insets.origin.y, 0.0f);
|
|
|
|
const float x2 = std::min(insets.origin.x + insets.size.width, 0.0f + _rect.size.width);
|
|
|
|
const float y2 = std::min(insets.origin.y + insets.size.height, 0.0f + _rect.size.height);
|
|
|
|
|
|
|
|
// centerRect uses the trimmed frame origin as 0,0.
|
|
|
|
// so, recenter inset rect
|
2021-12-25 10:04:45 +08:00
|
|
|
insets.setRect(x1, y1, x2 - x1, y2 - y1);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// Only update center rect while in slice mode.
|
|
|
|
if (_renderingType == RenderingType::SLICE && _renderMode != RenderMode::POLYGON)
|
|
|
|
{
|
|
|
|
setCenterRect(insets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect Scale9Sprite::getCapInsets() const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return Rect(_insetLeft, _insetTop, _originalContentSize.width - _insetLeft - _insetRight,
|
2019-11-23 20:27:39 +08:00
|
|
|
_originalContentSize.height - _insetTop - _insetBottom);
|
|
|
|
}
|