2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2011 Ricardo Quesada
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2024-07-11 23:28:31 +08:00
|
|
|
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
|
2024-08-15 12:14:02 +08:00
|
|
|
|
2024-06-10 02:25:43 +08:00
|
|
|
https://axmol.dev/
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "renderer/TextureCache.h"
|
|
|
|
#include "2d/SpriteFrame.h"
|
|
|
|
#include "base/Director.h"
|
|
|
|
#include "platform/FileUtils.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2024-08-26 00:25:33 +08:00
|
|
|
namespace ax
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// implementation of SpriteFrame
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
SpriteFrame* SpriteFrame::create(std::string_view filename, const Rect& rect)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* spriteFrame = new SpriteFrame();
|
2019-11-23 20:27:39 +08:00
|
|
|
spriteFrame->initWithTextureFilename(filename, rect);
|
|
|
|
spriteFrame->autorelease();
|
|
|
|
|
|
|
|
return spriteFrame;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* SpriteFrame::createWithTexture(Texture2D* texture, const Rect& rect)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* spriteFrame = new SpriteFrame();
|
2019-11-23 20:27:39 +08:00
|
|
|
spriteFrame->initWithTexture(texture, rect);
|
|
|
|
spriteFrame->autorelease();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return spriteFrame;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* SpriteFrame::createWithTexture(Texture2D* texture,
|
|
|
|
const Rect& rect,
|
|
|
|
bool rotated,
|
|
|
|
const Vec2& offset,
|
|
|
|
const Vec2& originalSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* spriteFrame = new SpriteFrame();
|
|
|
|
if (spriteFrame->initWithTexture(texture, rect, rotated, offset, originalSize))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
spriteFrame->autorelease();
|
|
|
|
return spriteFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete spriteFrame;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
SpriteFrame* SpriteFrame::create(std::string_view filename,
|
2021-12-25 10:04:45 +08:00
|
|
|
const Rect& rect,
|
|
|
|
bool rotated,
|
|
|
|
const Vec2& offset,
|
|
|
|
const Vec2& originalSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* spriteFrame = new SpriteFrame();
|
|
|
|
if (spriteFrame->initWithTextureFilename(filename, rect, rotated, offset, originalSize))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
spriteFrame->autorelease();
|
|
|
|
return spriteFrame;
|
|
|
|
}
|
|
|
|
delete spriteFrame;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame::SpriteFrame() : _rotated(false), _texture(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool SpriteFrame::initWithTexture(Texture2D* texture, const Rect& rect)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
Rect rectInPixels = AX_RECT_POINTS_TO_PIXELS(rect);
|
2019-11-23 20:27:39 +08:00
|
|
|
return initWithTexture(texture, rectInPixels, false, Vec2::ZERO, rectInPixels.size);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool SpriteFrame::initWithTextureFilename(std::string_view filename, const Rect& rect)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
Rect rectInPixels = AX_RECT_POINTS_TO_PIXELS(rect);
|
2019-11-23 20:27:39 +08:00
|
|
|
return initWithTextureFilename(filename, rectInPixels, false, Vec2::ZERO, rectInPixels.size);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool SpriteFrame::initWithTexture(Texture2D* texture,
|
|
|
|
const Rect& rect,
|
|
|
|
bool rotated,
|
|
|
|
const Vec2& offset,
|
|
|
|
const Vec2& originalSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_texture = texture;
|
|
|
|
|
|
|
|
if (texture)
|
|
|
|
{
|
|
|
|
texture->retain();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_rectInPixels = rect;
|
2022-07-16 10:43:05 +08:00
|
|
|
_rect = AX_RECT_PIXELS_TO_POINTS(rect);
|
2021-12-25 10:04:45 +08:00
|
|
|
_offsetInPixels = offset;
|
2022-07-16 10:43:05 +08:00
|
|
|
_offset = AX_POINT_PIXELS_TO_POINTS(_offsetInPixels);
|
2019-11-23 20:27:39 +08:00
|
|
|
_originalSizeInPixels = originalSize;
|
2022-07-16 10:43:05 +08:00
|
|
|
_originalSize = AX_SIZE_PIXELS_TO_POINTS(_originalSizeInPixels);
|
2021-12-25 10:04:45 +08:00
|
|
|
_rotated = rotated;
|
|
|
|
_anchorPoint = Vec2(NAN, NAN);
|
|
|
|
_centerRect = Rect(NAN, NAN, NAN, NAN);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool SpriteFrame::initWithTextureFilename(std::string_view filename,
|
2021-12-25 10:04:45 +08:00
|
|
|
const Rect& rect,
|
|
|
|
bool rotated,
|
|
|
|
const Vec2& offset,
|
|
|
|
const Vec2& originalSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (FileUtils::getInstance()->isFileExist(filename))
|
|
|
|
{
|
|
|
|
_texture = nullptr;
|
|
|
|
_textureFilename = filename;
|
|
|
|
_rectInPixels = rect;
|
2022-07-16 10:43:05 +08:00
|
|
|
_rect = AX_RECT_PIXELS_TO_POINTS(rect);
|
2021-12-25 10:04:45 +08:00
|
|
|
_offsetInPixels = offset;
|
2022-07-16 10:43:05 +08:00
|
|
|
_offset = AX_POINT_PIXELS_TO_POINTS(_offsetInPixels);
|
2019-11-23 20:27:39 +08:00
|
|
|
_originalSizeInPixels = originalSize;
|
2022-07-16 10:43:05 +08:00
|
|
|
_originalSize = AX_SIZE_PIXELS_TO_POINTS(_originalSizeInPixels);
|
2021-12-25 10:04:45 +08:00
|
|
|
_rotated = rotated;
|
|
|
|
_anchorPoint = Vec2(NAN, NAN);
|
|
|
|
_centerRect = Rect(NAN, NAN, NAN, NAN);
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpriteFrame::~SpriteFrame()
|
|
|
|
{
|
2024-07-05 07:51:36 +08:00
|
|
|
AXLOGV("deallocing SpriteFrame: {}", fmt::ptr(this));
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_texture);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SpriteFrame* SpriteFrame::clone() const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// no copy constructor
|
|
|
|
SpriteFrame* copy = new SpriteFrame();
|
2019-11-23 20:27:39 +08:00
|
|
|
copy->initWithTexture(_texture, _rectInPixels, _rotated, _offsetInPixels, _originalSizeInPixels);
|
|
|
|
copy->setPolygonInfo(_polygonInfo);
|
|
|
|
copy->autorelease();
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrame::setRect(const Rect& rect)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_rect = rect;
|
2022-07-16 10:43:05 +08:00
|
|
|
_rectInPixels = AX_RECT_POINTS_TO_PIXELS(_rect);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrame::setRectInPixels(const Rect& rectInPixels)
|
|
|
|
{
|
|
|
|
_rectInPixels = rectInPixels;
|
2022-07-16 10:43:05 +08:00
|
|
|
_rect = AX_RECT_PIXELS_TO_POINTS(rectInPixels);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrame::setCenterRectInPixels(const Rect& centerRect)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
_centerRect = AX_RECT_PIXELS_TO_POINTS(centerRect);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteFrame::hasCenterRect() const
|
|
|
|
{
|
|
|
|
return !std::isnan(_centerRect.origin.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vec2& SpriteFrame::getOffset() const
|
|
|
|
{
|
|
|
|
return _offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrame::setOffset(const Vec2& offsets)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_offset = offsets;
|
2022-07-16 10:43:05 +08:00
|
|
|
_offsetInPixels = AX_POINT_POINTS_TO_PIXELS(_offset);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Vec2& SpriteFrame::getOffsetInPixels() const
|
|
|
|
{
|
|
|
|
return _offsetInPixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrame::setOffsetInPixels(const Vec2& offsetInPixels)
|
|
|
|
{
|
|
|
|
_offsetInPixels = offsetInPixels;
|
2022-07-16 10:43:05 +08:00
|
|
|
_offset = AX_POINT_PIXELS_TO_POINTS(_offsetInPixels);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Vec2& SpriteFrame::getAnchorPoint() const
|
|
|
|
{
|
|
|
|
return _anchorPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrame::setAnchorPoint(const Vec2& anchorPoint)
|
|
|
|
{
|
|
|
|
_anchorPoint = anchorPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteFrame::hasAnchorPoint() const
|
|
|
|
{
|
|
|
|
return !std::isnan(_anchorPoint.x);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteFrame::setTexture(Texture2D* texture)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_texture != texture)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_texture);
|
|
|
|
AX_SAFE_RETAIN(texture);
|
2019-11-23 20:27:39 +08:00
|
|
|
_texture = texture;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D* SpriteFrame::getTexture()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_texture)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return _texture;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_textureFilename.empty())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return Director::getInstance()->getTextureCache()->addImage(_textureFilename);
|
|
|
|
}
|
|
|
|
// no texture or texture filename
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteFrame::setPolygonInfo(const PolygonInfo& polygonInfo)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_polygonInfo = polygonInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PolygonInfo& SpriteFrame::getPolygonInfo() const
|
|
|
|
{
|
|
|
|
return _polygonInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteFrame::hasPolygonInfo() const
|
|
|
|
{
|
|
|
|
return _polygonInfo.triangles.vertCount != 0;
|
|
|
|
}
|
|
|
|
|
2024-08-26 00:25:33 +08:00
|
|
|
}
|