2014-07-29 10:26:53 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-09-03 10:59:08 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
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.
|
2016-11-24 09:59:00 +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/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"
|
2019-11-20 18:19:24 +08:00
|
|
|
#include "renderer/backend/ProgramStateRegistry.h"
|
2014-07-29 10:39:38 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2022-08-08 18:02:17 +08:00
|
|
|
using namespace ax::ui;
|
2015-05-21 16:04:37 +08:00
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
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())
|
2015-05-21 16:04:37 +08:00
|
|
|
{
|
2016-11-24 09:59:00 +08:00
|
|
|
ret->autorelease();
|
2015-12-16 11:53:59 +08:00
|
|
|
return ret;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2016-11-24 09:59:00 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-05-21 16:04:37 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::create(std::string_view filename, const Rect& rect, const Rect& capInsets)
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Scale9Sprite* ret = new Scale9Sprite();
|
|
|
|
if (ret->initWithFile(filename, rect, capInsets))
|
2015-05-21 16:04:37 +08:00
|
|
|
{
|
2016-11-24 09:59:00 +08:00
|
|
|
ret->autorelease();
|
2015-10-29 14:27:27 +08:00
|
|
|
return ret;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2016-11-24 09:59:00 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-05-21 16:04:37 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::create(std::string_view filename, const Rect& rect)
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
|
|
|
return create(filename, rect, Rect::ZERO);
|
|
|
|
}
|
2015-05-21 16:04:37 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::create(const Rect& capInsets, std::string_view file)
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Scale9Sprite* ret = new Scale9Sprite();
|
|
|
|
if (ret->initWithFile(capInsets, file))
|
2015-05-21 16:04:37 +08:00
|
|
|
{
|
2016-11-24 09:59:00 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2015-05-21 16:04:37 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2016-11-24 09:59:00 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-05-21 16:04:37 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::create(std::string_view fileaname)
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
|
|
|
return create(Rect::ZERO, fileaname);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
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))
|
2014-12-04 03:17:47 +08:00
|
|
|
{
|
2016-11-24 09:59:00 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2014-12-04 03:17:47 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2016-11-24 09:59:00 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::createWithSpriteFrame(SpriteFrame* spriteFrame)
|
|
|
|
{
|
|
|
|
return createWithSpriteFrame(spriteFrame, Rect::ZERO);
|
|
|
|
}
|
2014-09-01 17:06:22 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(std::string_view spriteFrameName, const Rect& capInsets)
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Scale9Sprite* ret = new Scale9Sprite();
|
|
|
|
if (ret->initWithSpriteFrameName(spriteFrameName, capInsets))
|
2014-09-01 17:06:22 +08:00
|
|
|
{
|
2016-11-24 09:59:00 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2014-09-01 17:06:22 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2016-11-24 09:59:00 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(std::string_view spriteFrameName)
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Scale9Sprite* ret = new Scale9Sprite();
|
|
|
|
if (ret->initWithSpriteFrameName(spriteFrameName, Rect::ZERO))
|
2014-09-27 20:54:07 +08:00
|
|
|
{
|
2016-11-24 09:59:00 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2014-09-27 20:54:07 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2016-11-24 09:59:00 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
{}
|
2016-11-24 09:59:00 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Scale9Sprite::~Scale9Sprite() {}
|
2016-11-24 09:59:00 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Scale9Sprite::initWithFile(const Rect& capInsets, std::string_view file)
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
|
|
|
// calls super
|
|
|
|
bool ret = Sprite::initWithFile(file);
|
|
|
|
setupSlice9(getTexture(), capInsets);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Scale9Sprite::initWithFile(std::string_view filename)
|
2016-11-30 09:26:40 +08:00
|
|
|
{
|
|
|
|
// calls super
|
2017-07-17 15:52:01 +08:00
|
|
|
auto originalCapInsets = this->getCapInsets();
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ret = Sprite::initWithFile(filename);
|
2017-07-17 15:52:01 +08:00
|
|
|
setupSlice9(getTexture(), originalCapInsets);
|
2016-11-30 09:26:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Scale9Sprite::initWithFile(std::string_view filename, const Rect& rect)
|
2016-11-30 09:26:40 +08:00
|
|
|
{
|
|
|
|
// calls super
|
2017-07-17 15:52:01 +08:00
|
|
|
auto originalCapInsets = this->getCapInsets();
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ret = Sprite::initWithFile(filename, rect);
|
2017-07-17 15:52:01 +08:00
|
|
|
setupSlice9(getTexture(), originalCapInsets);
|
2016-11-30 09:26:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
bool Scale9Sprite::initWithSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets)
|
|
|
|
{
|
|
|
|
// calls super
|
|
|
|
bool ret = Sprite::initWithSpriteFrame(spriteFrame);
|
|
|
|
setupSlice9(getTexture(), capInsets);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Scale9Sprite::initWithSpriteFrameName(std::string_view spriteFrameName, const Rect& capInsets)
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
|
|
|
// calls super
|
|
|
|
bool ret = Sprite::initWithSpriteFrameName(spriteFrameName);
|
|
|
|
setupSlice9(getTexture(), capInsets);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Scale9Sprite::initWithSpriteFrameName(std::string_view spriteFrameName)
|
2016-11-30 09:26:40 +08:00
|
|
|
{
|
|
|
|
// calls super
|
2017-07-17 15:52:01 +08:00
|
|
|
auto originalCapInsets = this->getCapInsets();
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ret = Sprite::initWithSpriteFrameName(spriteFrameName);
|
2017-07-17 15:52:01 +08:00
|
|
|
setupSlice9(getTexture(), originalCapInsets);
|
2016-11-30 09:26:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
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)
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
Rect rect(origRect);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (sprite)
|
|
|
|
{
|
2016-11-24 09:59:00 +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);
|
2016-11-24 09:59:00 +08:00
|
|
|
|
2016-11-30 14:58:53 +08:00
|
|
|
setupSlice9(texture, capInsets);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-24 09:59:00 +08:00
|
|
|
ret = initWithTexture(nullptr, rect, rotated);
|
|
|
|
setupSlice9(nullptr, capInsets);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Scale9Sprite::initWithFile(std::string_view filename, const Rect& rect, const Rect& capInsets)
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
|
|
|
// calls super
|
2016-11-30 09:26:40 +08:00
|
|
|
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.
|
2016-11-30 09:26:40 +08:00
|
|
|
{
|
|
|
|
ret = Sprite::initWithFile(filename);
|
|
|
|
}
|
2016-11-30 14:58:53 +08:00
|
|
|
|
|
|
|
setupSlice9(getTexture(), capInsets);
|
2016-11-24 09:59:00 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Scale9Sprite::updateWithSprite(Sprite* sprite, const Rect& rect, bool rotated, const Rect& capInsets)
|
2016-11-24 09:59:00 +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,
|
2016-11-24 09:59:00 +08:00
|
|
|
const Rect& capInsets)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* spriteframe =
|
2022-07-15 19:17:01 +08:00
|
|
|
SpriteFrame::createWithTexture(sprite->getTexture(), AX_RECT_POINTS_TO_PIXELS(textureRect), rotated,
|
|
|
|
AX_POINT_POINTS_TO_PIXELS(offset), AX_SIZE_POINTS_TO_PIXELS(originalSize));
|
2016-11-24 09:59:00 +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))
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2015-10-30 09:58:02 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2016-11-24 09:59:00 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
Scale9Sprite::State Scale9Sprite::getState() const
|
|
|
|
{
|
|
|
|
return _brightState;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
void Scale9Sprite::setState(Scale9Sprite::State state)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_brightState != state)
|
|
|
|
{
|
2016-11-24 09:59:00 +08:00
|
|
|
_brightState = state;
|
2014-12-23 15:03:31 +08:00
|
|
|
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;
|
2014-07-29 10:39:38 +08:00
|
|
|
}
|
2015-10-29 10:41:58 +08:00
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
_brightState = state;
|
2015-02-12 09:24:04 +08:00
|
|
|
}
|
2016-11-24 09:59:00 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Scale9Sprite::setSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets)
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
|
|
|
setSpriteFrame(spriteFrame);
|
|
|
|
setCapInsets(capInsets);
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
void Scale9Sprite::setPreferredSize(const Vec2& preferredSize)
|
2016-11-24 09:59:00 +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()
|
|
|
|
{
|
2016-11-30 09:26:40 +08:00
|
|
|
if (_renderingType == RenderingType::SLICE)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Rect capInsets(_insetLeft, _insetTop, _originalContentSize.width - _insetRight - _insetLeft,
|
|
|
|
_originalContentSize.height - _insetBottom - _insetTop);
|
2016-11-30 09:26:40 +08:00
|
|
|
setCapInsets(capInsets);
|
|
|
|
}
|
2016-11-24 09:59:00 +08:00
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 Scale9Sprite::getOriginalSize() const
|
2016-11-24 09:59:00 +08:00
|
|
|
{
|
|
|
|
return _originalContentSize;
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 Scale9Sprite::getPreferredSize() const
|
2016-11-24 09:59:00 +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)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGWARN("Scale9Sprite::setScale9Enabled() can't be called when using POLYGON render modes");
|
2017-01-20 09:59:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
RenderingType type = enabled ? RenderingType::SLICE : RenderingType::SIMPLE;
|
|
|
|
setRenderingType(type);
|
|
|
|
|
2017-01-11 09:31:45 +08:00
|
|
|
// only enable stretch when scale9 is enabled
|
|
|
|
// for backward compatibility, since Sprite stretches the texture no matter the rendering type
|
2017-01-23 09:25:56 +08:00
|
|
|
setStretchEnabled(enabled);
|
2016-11-24 09:59:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Scale9Sprite::isScale9Enabled() const
|
|
|
|
{
|
|
|
|
return (_renderingType == RenderingType::SLICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite* Scale9Sprite::getSprite()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:35:22 +08:00
|
|
|
/**
|
|
|
|
* @brief Returns a copy of the Scale9Sprite
|
|
|
|
*/
|
|
|
|
void Scale9Sprite::copyTo(Scale9Sprite* copy) const
|
|
|
|
{
|
|
|
|
copy->initWithSpriteFrame(getSpriteFrame(), getCapInsets());
|
|
|
|
|
|
|
|
copy->setRenderingType(_renderingType);
|
|
|
|
copy->setScale9Enabled(isScale9Enabled());
|
2022-10-01 16:24:52 +08:00
|
|
|
// setStretchEnabled after setScale9Enabled, fix issue: https://github.com/axmolengine/axmol/issues/491
|
2021-09-03 10:49:44 +08:00
|
|
|
copy->setStretchEnabled(isStretchEnabled());
|
2021-12-25 10:04:45 +08:00
|
|
|
copy->_isPatch9 = _isPatch9;
|
2016-12-03 23:35:22 +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;
|
|
|
|
}
|
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
// (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--------------------
|
2016-11-24 09:59:00 +08:00
|
|
|
// x1 x2 x3
|
|
|
|
|
|
|
|
void Scale9Sprite::setRenderingType(Scale9Sprite::RenderingType type)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_renderMode == RenderMode::POLYGON)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGWARN("Scale9Sprite::setRenderingType() can't be called when using POLYGON render modes");
|
2017-01-20 09:59:30 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_renderingType != type)
|
|
|
|
{
|
2015-10-29 23:13:36 +08:00
|
|
|
_renderingType = type;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_renderingType == RenderingType::SIMPLE)
|
|
|
|
{
|
|
|
|
setCenterRectNormalized(Rect(0, 0, 1, 1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-30 09:26:40 +08:00
|
|
|
updateCapInset();
|
2016-11-24 09:59:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
2016-11-24 09:59:00 +08:00
|
|
|
auto& parsedCapInset = texture->getSpriteFrameCapInset(getSpriteFrame());
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!parsedCapInset.equals(Rect::ZERO))
|
2016-11-24 09:59:00 +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);
|
|
|
|
}
|
|
|
|
}
|
2016-11-30 09:26:40 +08:00
|
|
|
|
|
|
|
if (!_isPatch9)
|
|
|
|
{
|
|
|
|
setCapInsets(capInsets);
|
|
|
|
}
|
2016-11-24 09:59:00 +08:00
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void Scale9Sprite::setCapInsets(const ax::Rect& insetsCopy)
|
2016-11-24 09:59:00 +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);
|
2016-11-24 09:59:00 +08:00
|
|
|
}
|
|
|
|
|
2016-12-03 23:35:22 +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;
|
2016-11-30 09:26:40 +08:00
|
|
|
_insetBottom = _originalContentSize.height - _insetTop - insets.size.height;
|
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
// 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
|
2016-11-25 09:27:20 +08:00
|
|
|
// 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;
|
2016-11-24 09:59:00 +08:00
|
|
|
|
|
|
|
// 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);
|
2016-11-30 09:26:40 +08:00
|
|
|
|
|
|
|
// Only update center rect while in slice mode.
|
2017-01-20 09:59:30 +08:00
|
|
|
if (_renderingType == RenderingType::SLICE && _renderMode != RenderMode::POLYGON)
|
2016-11-30 09:26:40 +08:00
|
|
|
{
|
|
|
|
setCenterRect(insets);
|
|
|
|
}
|
2016-11-24 09:59:00 +08:00
|
|
|
}
|
2015-11-18 13:38:29 +08:00
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
Rect Scale9Sprite::getCapInsets() const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return Rect(_insetLeft, _insetTop, _originalContentSize.width - _insetLeft - _insetRight,
|
2017-07-17 15:52:01 +08:00
|
|
|
_originalContentSize.height - _insetTop - _insetBottom);
|
2016-11-24 09:59:00 +08:00
|
|
|
}
|