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.
|
2016-08-05 09:42:15 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of SpriteFrame
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
SpriteFrame* SpriteFrame::create(const std::string& filename, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
SpriteFrame *spriteFrame = new (std::nothrow) 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
|
|
|
}
|
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
SpriteFrame* SpriteFrame::createWithTexture(Texture2D *texture, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
SpriteFrame *spriteFrame = new (std::nothrow) SpriteFrame();
|
2013-12-18 17:47:20 +08:00
|
|
|
spriteFrame->initWithTexture(texture, rect);
|
|
|
|
spriteFrame->autorelease();
|
2012-12-06 18:51:33 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return spriteFrame;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
SpriteFrame* SpriteFrame::createWithTexture(Texture2D* texture, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
SpriteFrame *spriteFrame = new (std::nothrow) SpriteFrame();
|
2013-12-18 17:47:20 +08:00
|
|
|
spriteFrame->initWithTexture(texture, rect, rotated, offset, originalSize);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
SpriteFrame* SpriteFrame::create(const std::string& filename, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
SpriteFrame *spriteFrame = new (std::nothrow) SpriteFrame();
|
2013-12-18 17:47:20 +08:00
|
|
|
spriteFrame->initWithTextureFilename(filename, rect, rotated, offset, originalSize);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-26 09:53:52 +08:00
|
|
|
SpriteFrame::SpriteFrame()
|
2014-06-10 11:28:34 +08:00
|
|
|
: _rotated(false)
|
|
|
|
, _texture(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
bool SpriteFrame::initWithTexture(Texture2D* texture, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Rect rectInPixels = CC_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
|
|
|
}
|
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
bool SpriteFrame::initWithTextureFilename(const std::string& filename, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Rect rectInPixels = CC_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
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool SpriteFrame::initWithTexture(Texture2D* texture, const Rect& rect, bool rotated, const Vec2& offset, const Size& 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
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_rectInPixels = rect;
|
|
|
|
_rect = CC_RECT_PIXELS_TO_POINTS(rect);
|
|
|
|
_offsetInPixels = offset;
|
|
|
|
_offset = CC_POINT_PIXELS_TO_POINTS( _offsetInPixels );
|
|
|
|
_originalSizeInPixels = originalSize;
|
|
|
|
_originalSize = CC_SIZE_PIXELS_TO_POINTS( _originalSizeInPixels );
|
|
|
|
_rotated = rotated;
|
2015-12-14 22:42:18 +08:00
|
|
|
_anchorPoint = Vec2(NAN, NAN);
|
2016-10-27 09:45:40 +08:00
|
|
|
_capInsetsNormalized = Rect(NAN, NAN, NAN, NAN);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool SpriteFrame::initWithTextureFilename(const std::string& filename, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
_texture = nullptr;
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureFilename = filename;
|
|
|
|
_rectInPixels = rect;
|
|
|
|
_rect = CC_RECT_PIXELS_TO_POINTS( rect );
|
|
|
|
_offsetInPixels = offset;
|
|
|
|
_offset = CC_POINT_PIXELS_TO_POINTS( _offsetInPixels );
|
|
|
|
_originalSizeInPixels = originalSize;
|
|
|
|
_originalSize = CC_SIZE_PIXELS_TO_POINTS( _originalSizeInPixels );
|
|
|
|
_rotated = rotated;
|
2015-12-14 22:42:18 +08:00
|
|
|
_anchorPoint = Vec2(NAN, NAN);
|
2016-10-27 09:45:40 +08:00
|
|
|
_capInsetsNormalized = Rect(NAN, NAN, NAN, NAN);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-26 09:53:52 +08:00
|
|
|
SpriteFrame::~SpriteFrame()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-08-22 11:05:06 +08:00
|
|
|
CCLOGINFO("deallocing SpriteFrame: %p", this);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_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
|
|
|
{
|
|
|
|
// no copy constructor
|
2014-08-28 07:31:57 +08:00
|
|
|
SpriteFrame *copy = new (std::nothrow) SpriteFrame();
|
2016-02-03 23:12:37 +08:00
|
|
|
copy->initWithTextureFilename(_textureFilename, _rectInPixels, _rotated, _offsetInPixels, _originalSizeInPixels);
|
2013-06-19 06:06:53 +08:00
|
|
|
copy->setTexture(_texture);
|
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
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_rect = rect;
|
|
|
|
_rectInPixels = CC_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;
|
|
|
|
_rect = CC_RECT_PIXELS_TO_POINTS(rectInPixels);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2016-10-27 09:45:40 +08:00
|
|
|
void SpriteFrame::setCapInsets(const Rect& centerRect)
|
|
|
|
{
|
|
|
|
_capInsetsNormalized = CC_RECT_PIXELS_TO_POINTS(centerRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteFrame::hasCenterRect() const
|
|
|
|
{
|
|
|
|
return !std::isnan(_capInsetsNormalized.origin.x);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_offset = offsets;
|
|
|
|
_offsetInPixels = CC_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;
|
|
|
|
_offset = CC_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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteFrame::setTexture(Texture2D * texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _texture != texture ) {
|
|
|
|
CC_SAFE_RELEASE(_texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_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
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _texture ) {
|
|
|
|
return _texture;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-09-01 11:26:09 +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
|
|
|
}
|
|
|
|
|
2015-09-29 21:42:04 +08:00
|
|
|
void SpriteFrame::setPolygonInfo(const PolygonInfo &polygonInfo)
|
|
|
|
{
|
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|