2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2011 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2012-04-19 14:35:52 +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.
|
|
|
|
****************************************************************************/
|
2014-05-17 05:36:00 +08:00
|
|
|
|
|
|
|
#include "renderer/CCTextureCache.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSpriteFrame.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2016-11-24 09:59:00 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of SpriteFrame
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
SpriteFrame* SpriteFrame::create(std::string_view filename, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* spriteFrame = new SpriteFrame();
|
2013-12-18 17:47:20 +08:00
|
|
|
spriteFrame->initWithTextureFilename(filename, rect);
|
|
|
|
spriteFrame->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return spriteFrame;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* SpriteFrame::createWithTexture(Texture2D* texture, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* spriteFrame = new SpriteFrame();
|
2013-12-18 17:47:20 +08:00
|
|
|
spriteFrame->initWithTexture(texture, rect);
|
|
|
|
spriteFrame->autorelease();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return spriteFrame;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* SpriteFrame::createWithTexture(Texture2D* texture,
|
|
|
|
const Rect& rect,
|
|
|
|
bool rotated,
|
|
|
|
const Vec2& offset,
|
|
|
|
const Vec2& originalSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* spriteFrame = new SpriteFrame();
|
|
|
|
if (spriteFrame->initWithTexture(texture, rect, rotated, offset, originalSize))
|
|
|
|
{
|
2016-11-24 09:59:00 +08:00
|
|
|
spriteFrame->autorelease();
|
|
|
|
return spriteFrame;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
delete spriteFrame;
|
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +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)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* spriteFrame = new SpriteFrame();
|
|
|
|
if (spriteFrame->initWithTextureFilename(filename, rect, rotated, offset, originalSize))
|
|
|
|
{
|
2016-11-24 09:59:00 +08:00
|
|
|
spriteFrame->autorelease();
|
|
|
|
return spriteFrame;
|
|
|
|
}
|
|
|
|
delete spriteFrame;
|
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame::SpriteFrame() : _rotated(false), _texture(nullptr) {}
|
2014-06-10 11:28:34 +08:00
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
bool SpriteFrame::initWithTexture(Texture2D* texture, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
Rect rectInPixels = AX_RECT_POINTS_TO_PIXELS(rect);
|
2014-05-15 01:07:09 +08:00
|
|
|
return initWithTexture(texture, rectInPixels, false, Vec2::ZERO, rectInPixels.size);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool SpriteFrame::initWithTextureFilename(std::string_view filename, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
Rect rectInPixels = AX_RECT_POINTS_TO_PIXELS(rect);
|
2014-05-15 01:07:09 +08:00
|
|
|
return initWithTextureFilename(filename, rectInPixels, false, Vec2::ZERO, rectInPixels.size);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool SpriteFrame::initWithTexture(Texture2D* texture,
|
|
|
|
const Rect& rect,
|
|
|
|
bool rotated,
|
|
|
|
const Vec2& offset,
|
|
|
|
const Vec2& originalSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-06 11:02:03 +08:00
|
|
|
_texture = texture;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
if (texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-06 11:02:03 +08:00
|
|
|
texture->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_rectInPixels = rect;
|
2022-07-15 19:17:01 +08:00
|
|
|
_rect = AX_RECT_PIXELS_TO_POINTS(rect);
|
2021-12-25 10:04:45 +08:00
|
|
|
_offsetInPixels = offset;
|
2022-07-15 19:17:01 +08:00
|
|
|
_offset = AX_POINT_PIXELS_TO_POINTS(_offsetInPixels);
|
2013-06-15 14:03:30 +08:00
|
|
|
_originalSizeInPixels = originalSize;
|
2022-07-15 19:17:01 +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);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +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)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (FileUtils::getInstance()->isFileExist(filename))
|
|
|
|
{
|
|
|
|
_texture = nullptr;
|
|
|
|
_textureFilename = filename;
|
|
|
|
_rectInPixels = rect;
|
2022-07-15 19:17:01 +08:00
|
|
|
_rect = AX_RECT_PIXELS_TO_POINTS(rect);
|
2021-12-25 10:04:45 +08:00
|
|
|
_offsetInPixels = offset;
|
2022-07-15 19:17:01 +08:00
|
|
|
_offset = AX_POINT_PIXELS_TO_POINTS(_offsetInPixels);
|
2016-11-24 09:59:00 +08:00
|
|
|
_originalSizeInPixels = originalSize;
|
2022-07-15 19:17:01 +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);
|
2016-11-24 09:59:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-11-26 09:53:52 +08:00
|
|
|
SpriteFrame::~SpriteFrame()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGINFO("deallocing SpriteFrame: %p", this);
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteFrame* SpriteFrame::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// no copy constructor
|
|
|
|
SpriteFrame* copy = new SpriteFrame();
|
2016-11-30 11:36:01 +08:00
|
|
|
copy->initWithTexture(_texture, _rectInPixels, _rotated, _offsetInPixels, _originalSizeInPixels);
|
2016-04-20 15:04:19 +08:00
|
|
|
copy->setPolygonInfo(_polygonInfo);
|
2013-09-16 23:28:00 +08:00
|
|
|
copy->autorelease();
|
2013-06-19 06:06:53 +08:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteFrame::setRect(const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_rect = rect;
|
2022-07-15 19:17:01 +08:00
|
|
|
_rectInPixels = AX_RECT_POINTS_TO_PIXELS(_rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteFrame::setRectInPixels(const Rect& rectInPixels)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_rectInPixels = rectInPixels;
|
2022-07-15 19:17:01 +08:00
|
|
|
_rect = AX_RECT_PIXELS_TO_POINTS(rectInPixels);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2016-10-30 08:52:33 +08:00
|
|
|
void SpriteFrame::setCenterRectInPixels(const Rect& centerRect)
|
2016-10-27 09:45:40 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
_centerRect = AX_RECT_PIXELS_TO_POINTS(centerRect);
|
2016-10-27 09:45:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteFrame::hasCenterRect() const
|
|
|
|
{
|
2016-10-30 08:52:33 +08:00
|
|
|
return !std::isnan(_centerRect.origin.x);
|
2016-10-27 09:45:40 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
const Vec2& SpriteFrame::getOffset() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _offset;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void SpriteFrame::setOffset(const Vec2& offsets)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_offset = offsets;
|
2022-07-15 19:17:01 +08:00
|
|
|
_offsetInPixels = AX_POINT_POINTS_TO_PIXELS(_offset);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
const Vec2& SpriteFrame::getOffsetInPixels() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _offsetInPixels;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void SpriteFrame::setOffsetInPixels(const Vec2& offsetInPixels)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_offsetInPixels = offsetInPixels;
|
2022-07-15 19:17:01 +08:00
|
|
|
_offset = AX_POINT_PIXELS_TO_POINTS(_offsetInPixels);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-12-14 22:42:18 +08:00
|
|
|
const Vec2& SpriteFrame::getAnchorPoint() const
|
|
|
|
{
|
|
|
|
return _anchorPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrame::setAnchorPoint(const Vec2& anchorPoint)
|
|
|
|
{
|
|
|
|
_anchorPoint = anchorPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteFrame::hasAnchorPoint() const
|
|
|
|
{
|
2016-04-27 19:15:08 +08:00
|
|
|
return !std::isnan(_anchorPoint.x);
|
2015-12-14 22:42:18 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteFrame::setTexture(Texture2D* texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_texture != texture)
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_texture);
|
|
|
|
AX_SAFE_RETAIN(texture);
|
2013-06-15 14:03:30 +08:00
|
|
|
_texture = texture;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 09:53:52 +08:00
|
|
|
Texture2D* SpriteFrame::getTexture()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_texture)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _texture;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_textureFilename.empty())
|
|
|
|
{
|
2016-02-03 23:12:37 +08:00
|
|
|
return Director::getInstance()->getTextureCache()->addImage(_textureFilename);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
// no texture or texture filename
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteFrame::setPolygonInfo(const PolygonInfo& polygonInfo)
|
2015-09-29 21:42:04 +08:00
|
|
|
{
|
|
|
|
_polygonInfo = polygonInfo;
|
|
|
|
}
|
|
|
|
|
2016-05-25 07:57:40 +08:00
|
|
|
const PolygonInfo& SpriteFrame::getPolygonInfo() const
|
2015-09-29 21:42:04 +08:00
|
|
|
{
|
|
|
|
return _polygonInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteFrame::hasPolygonInfo() const
|
|
|
|
{
|
|
|
|
return _polygonInfo.triangles.vertCount != 0;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|