axmol/cocos/2d/CCSprite.cpp

1400 lines
40 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
2016-08-05 09:42:15 +08:00
Copyright (c) 2013-2016 Chukong Technologies Inc.
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-04-30 08:37:36 +08:00
#include "2d/CCSprite.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include <algorithm>
#include "2d/CCSpriteBatchNode.h"
2014-04-30 08:37:36 +08:00
#include "2d/CCAnimationCache.h"
#include "2d/CCSpriteFrame.h"
#include "2d/CCSpriteFrameCache.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "renderer/CCTextureCache.h"
#include "renderer/CCTexture2D.h"
2014-05-10 09:39:25 +08:00
#include "renderer/CCRenderer.h"
2014-04-30 08:37:36 +08:00
#include "base/CCDirector.h"
#include "base/ccUTF8.h"
2015-07-30 11:53:23 +08:00
#include "2d/CCCamera.h"
NS_CC_BEGIN
// MARK: create, init, dealloc
Sprite* Sprite::createWithTexture(Texture2D *texture)
{
Sprite *sprite = new (std::nothrow) Sprite();
if (sprite && sprite->initWithTexture(texture))
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;
}
2013-11-14 07:55:36 +08:00
Sprite* Sprite::createWithTexture(Texture2D *texture, const Rect& rect, bool rotated)
{
Sprite *sprite = new (std::nothrow) Sprite();
if (sprite && sprite->initWithTexture(texture, rect, rotated))
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;
}
Sprite* Sprite::create(const std::string& filename)
{
Sprite *sprite = new (std::nothrow) Sprite();
if (sprite && sprite->initWithFile(filename))
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;
}
Sprite* Sprite::create(const PolygonInfo& info)
{
Sprite *sprite = new (std::nothrow) Sprite();
if(sprite && sprite->initWithPolygon(info))
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;
}
Sprite* Sprite::create(const std::string& filename, const Rect& rect)
{
Sprite *sprite = new (std::nothrow) Sprite();
if (sprite && sprite->initWithFile(filename, rect))
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;
}
Sprite* Sprite::createWithSpriteFrame(SpriteFrame *spriteFrame)
{
Sprite *sprite = new (std::nothrow) Sprite();
if (sprite && spriteFrame && sprite->initWithSpriteFrame(spriteFrame))
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;
}
Sprite* Sprite::createWithSpriteFrameName(const std::string& spriteFrameName)
{
SpriteFrame *frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFrameName);
#if COCOS2D_DEBUG > 0
char msg[256] = {0};
sprintf(msg, "Invalid spriteFrameName: %s", spriteFrameName.c_str());
CCASSERT(frame != nullptr, msg);
#endif
return createWithSpriteFrame(frame);
}
Sprite* Sprite::create()
{
Sprite *sprite = new (std::nothrow) Sprite();
if (sprite && sprite->init())
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;
}
2015-12-16 11:53:59 +08:00
bool Sprite::init()
{
2015-12-16 11:53:59 +08:00
initWithTexture(nullptr, Rect::ZERO);
return true;
}
bool Sprite::initWithTexture(Texture2D *texture)
{
CCASSERT(texture != nullptr, "Invalid texture for sprite");
Rect rect = Rect::ZERO;
2015-12-16 11:53:59 +08:00
if (texture) {
rect.size = texture->getContentSize();
}
2015-12-16 11:53:59 +08:00
return initWithTexture(texture, rect, false);
}
bool Sprite::initWithTexture(Texture2D *texture, const Rect& rect)
{
return initWithTexture(texture, rect, false);
}
bool Sprite::initWithFile(const std::string& filename)
{
if (filename.empty())
{
CCLOG("Call Sprite::initWithFile with blank resource filename.");
return false;
}
_fileName = filename;
_fileType = 0;
2015-12-16 11:53:59 +08:00
Texture2D *texture = _director->getTextureCache()->addImage(filename);
if (texture)
{
Rect rect = Rect::ZERO;
rect.size = texture->getContentSize();
return initWithTexture(texture, rect);
}
// don't release here.
// when load texture failed, it's better to get a "transparent" sprite then a crashed program
// this->release();
return false;
}
bool Sprite::initWithFile(const std::string &filename, const Rect& rect)
{
2015-12-16 11:53:59 +08:00
CCASSERT(!filename.empty(), "Invalid filename");
if (filename.empty())
{
return false;
}
_fileName = filename;
_fileType = 0;
2015-12-16 11:53:59 +08:00
Texture2D *texture = _director->getTextureCache()->addImage(filename);
if (texture)
{
return initWithTexture(texture, rect);
}
// don't release here.
// when load texture failed, it's better to get a "transparent" sprite then a crashed program
// this->release();
return false;
}
bool Sprite::initWithSpriteFrameName(const std::string& spriteFrameName)
{
2015-12-16 11:53:59 +08:00
CCASSERT(!spriteFrameName.empty(), "Invalid spriteFrameName");
if (spriteFrameName.empty())
{
return false;
}
_fileName = spriteFrameName;
_fileType = 1;
SpriteFrame *frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFrameName);
return initWithSpriteFrame(frame);
}
bool Sprite::initWithSpriteFrame(SpriteFrame *spriteFrame)
{
CCASSERT(spriteFrame != nullptr, "spriteFrame can't be nullptr!");
2015-12-16 11:53:59 +08:00
if (spriteFrame == nullptr)
{
return false;
}
bool bRet = initWithTexture(spriteFrame->getTexture(), spriteFrame->getRect());
setSpriteFrame(spriteFrame);
return bRet;
}
bool Sprite::initWithPolygon(const cocos2d::PolygonInfo &info)
{
2015-12-16 11:53:59 +08:00
bool ret = false;
Texture2D *texture = _director->getTextureCache()->addImage(info.getFilename());
2015-12-16 11:53:59 +08:00
if(texture && initWithTexture(texture))
2015-06-04 15:43:31 +08:00
{
2015-06-04 16:20:00 +08:00
_polyInfo = info;
Node::setContentSize(_polyInfo.getRect().size / _director->getContentScaleFactor());
2015-12-16 11:53:59 +08:00
ret = true;
2015-06-04 15:43:31 +08:00
}
2015-12-16 11:53:59 +08:00
return ret;
}
// designated initializer
bool Sprite::initWithTexture(Texture2D *texture, const Rect& rect, bool rotated)
{
2015-12-16 11:53:59 +08:00
bool result = false;
if (Node::init())
{
_batchNode = nullptr;
_recursiveDirty = false;
setDirty(false);
_opacityModifyRGB = true;
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
_flippedX = _flippedY = false;
// default transform anchor: center
setAnchorPoint(Vec2::ANCHOR_MIDDLE);
// zwoptex default values
_offsetPosition.setZero();
// clean the Quad
memset(&_quad, 0, sizeof(_quad));
// Atlas: Color
_quad.bl.colors = Color4B::WHITE;
_quad.br.colors = Color4B::WHITE;
_quad.tl.colors = Color4B::WHITE;
_quad.tr.colors = Color4B::WHITE;
2014-05-10 09:39:25 +08:00
// update texture (calls updateBlendFunc)
setTexture(texture);
setTextureRect(rect, rotated, rect.size);
// by default use "Self Render".
// if the sprite is added to a batchnode, then it will automatically switch to "batchnode Render"
setBatchNode(nullptr);
result = true;
}
2015-12-16 11:53:59 +08:00
_recursiveDirty = true;
setDirty(true);
2015-12-16 11:53:59 +08:00
return result;
}
Sprite::Sprite(void)
: _batchNode(nullptr)
2015-08-05 23:17:38 +08:00
, _textureAtlas(nullptr)
, _shouldBeHidden(false)
, _texture(nullptr)
, _spriteFrame(nullptr)
, _insideBounds(true)
, _capInsetsNormalized(0,0,1,1)
, _numberOfSlices(1)
, _quads(nullptr)
, _strechFactor(Vec2::ONE)
, _originalContentSize(Size::ZERO)
{
#if CC_SPRITE_DEBUG_DRAW
2015-08-26 18:14:25 +08:00
_debugDrawNode = DrawNode::create();
addChild(_debugDrawNode);
#endif //CC_SPRITE_DEBUG_DRAW
}
Sprite::~Sprite()
{
CC_SAFE_FREE(_quads);
CC_SAFE_RELEASE(_spriteFrame);
CC_SAFE_RELEASE(_texture);
}
/*
* Texture methods
*/
/*
* This array is the data of a white image with 2 by 2 dimension.
* It's used for creating a default texture when sprite's texture is set to nullptr.
* Supposing codes as follows:
*
* auto sp = new (std::nothrow) Sprite();
* sp->init(); // Texture was set to nullptr, in order to make opacity and color to work correctly, we need to create a 2x2 white texture.
*
* The test is in "TestCpp/SpriteTest/Sprite without texture".
*/
static unsigned char cc_2x2_white_image[] = {
// RGBA8888
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF
};
#define CC_2x2_WHITE_IMAGE_KEY "/cc_2x2_white_image"
2013-11-14 07:55:36 +08:00
// MARK: texture
2013-11-14 07:55:36 +08:00
void Sprite::setTexture(const std::string &filename)
{
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
setTexture(texture);
_unflippedOffsetPositionFromCenter = Vec2::ZERO;
Rect rect = Rect::ZERO;
if (texture)
rect.size = texture->getContentSize();
setTextureRect(rect);
}
void Sprite::setTexture(Texture2D *texture)
{
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP, texture));
// If batchnode, then texture id should be the same
2015-07-09 09:58:14 +08:00
CCASSERT(! _batchNode || (texture && texture->getName() == _batchNode->getTexture()->getName()), "CCSprite: Batched sprites should use the same texture as the batchnode");
// accept texture==nil as argument
CCASSERT( !texture || dynamic_cast<Texture2D*>(texture), "setTexture expects a Texture2D. Invalid argument");
if (texture == nullptr)
{
// Gets the texture by key firstly.
2015-12-16 11:53:59 +08:00
texture = _director->getTextureCache()->getTextureForKey(CC_2x2_WHITE_IMAGE_KEY);
// If texture wasn't in cache, create it from RAW data.
if (texture == nullptr)
{
Image* image = new (std::nothrow) Image();
bool isOK = image->initWithRawData(cc_2x2_white_image, sizeof(cc_2x2_white_image), 2, 2, 8);
2014-03-20 16:34:42 +08:00
CC_UNUSED_PARAM(isOK);
CCASSERT(isOK, "The 2x2 empty texture was created unsuccessfully.");
2015-12-16 11:53:59 +08:00
texture = _director->getTextureCache()->addImage(image, CC_2x2_WHITE_IMAGE_KEY);
CC_SAFE_RELEASE(image);
}
}
if (!_batchNode && _texture != texture)
{
CC_SAFE_RETAIN(texture);
CC_SAFE_RELEASE(_texture);
_texture = texture;
updateBlendFunc();
}
}
Texture2D* Sprite::getTexture() const
{
return _texture;
2013-11-14 07:55:36 +08:00
}
void Sprite::setTextureRect(const Rect& rect)
{
setTextureRect(rect, false, rect.size);
}
void Sprite::setTextureRect(const Rect& rect, bool rotated, const Size& untrimmedSize)
{
_rectRotated = rotated;
Node::setContentSize(untrimmedSize);
_originalContentSize = untrimmedSize;
setVertexRect(rect);
updateStretchFactor();
updatePoly();
}
void Sprite::updatePoly()
{
if (_numberOfSlices == 1) {
setTextureCoords(_rect, &_quad);
const Rect copyRect(0, 0, _rect.size.width * _strechFactor.x, _rect.size.height * _strechFactor.y);
setVertexCoords(copyRect, _rect.size, &_quad);
_polyInfo.setQuad(&_quad);
} else {
// in theory it can support 3 slices as well, but let's stick to 9 only
CCASSERT(_numberOfSlices == 9, "Invalid number of slices");
// center rect
const float x1 = _capInsetsNormalized.origin.x;
const float y1 = _capInsetsNormalized.origin.y;
const float x2 = _capInsetsNormalized.origin.x + _capInsetsNormalized.size.width;
const float y2 = _capInsetsNormalized.origin.y + _capInsetsNormalized.size.height;
// "O"riginal rect
const float oox = _rect.origin.x;
const float ooy = _rect.origin.y;
const float osw = _rect.size.width;
const float osh = _rect.size.height;
// textCoords Data: Y must be inverted.
const float u0 = oox + osw * 0;
const float u1 = oox + osw * x1;
const float u2 = oox + osw * x2;
const float v0 = ooy + osh - (osh * y1);
const float v1 = ooy + osh * (1-y2);
const float v2 = ooy + osh * 0;
const Rect texRects[9] = {
Rect(u0, v0, osw * x1, osh * y1), // bottom-left
Rect(u1, v0, osw * (x2-x1), osh * y1), // bottom
Rect(u2, v0, osw * (1-x2), osh * y1), // bottom-right
Rect(u0, v1, osw * x1, osh * (y2-y1)), // left
Rect(u1, v1, osw * (x2-x1), osh * (y2-y1)), // center
Rect(u2, v1, osw * (1-x2), osh * (y2-y1)), // right
Rect(u0, v2, osw * x1, osh * (1-y2)), // top-left
Rect(u1, v2, osw * (x2-x1), osh * (1-y2)), // top
Rect(u2, v2, osw * (1-x2), osh * (1-y2)), // top-right
};
// vertex Data.
const float x2_x1_strech = osw * (x2-x1) * _strechFactor.x;
const float y2_y1_strech = osh * (y2-y1) * _strechFactor.y;
const Rect verticesRects[9] = {
Rect(osw * 0, osh * 0, osw * x1, osh * y1), // bottom-left
Rect(osw * x1, osh * 0, x2_x1_strech, osh * y1), // bottom
Rect(osw * x1 + x2_x1_strech, osh * 0, osw * (1-x2), osh * y1), // bottom-right
Rect(osw * 0, osh * y1, osw * x1, y2_y1_strech), // left
Rect(osw * x1, osh * y1, x2_x1_strech, y2_y1_strech), // center
Rect(osw * x1 + x2_x1_strech, osh * y1, osw * (1-x2), y2_y1_strech), // right
Rect(osw * 0, osh * y1 + y2_y1_strech, osw * x1, osh * (1-y2)), // top-left
Rect(osw * x1, osh * y1 + y2_y1_strech, x2_x1_strech, osh * (1-y2)), // top
Rect(osw * x1 + x2_x1_strech, osh * y1 + y2_y1_strech, osw * (1-x2), osh * (1-y2)), // top-right
};
const int rotatedIdx[] = {6, 3, 0, 7, 4, 1, 8, 5, 2};
for (int i=0; i<_numberOfSlices; ++i) {
const int texIdx = _rectRotated ? rotatedIdx[i] : i;
setTextureCoords(texRects[texIdx], &_quads[i]);
setVertexCoords(verticesRects[i], _rect.size, &_quads[i]);
}
_polyInfo.setQuads(_quads, _numberOfSlices);
}
}
void Sprite::setCapInsetsNormalized(const cocos2d::Rect &rectTopLeft)
{
// FIMXE: Rect is has origin on top-left (like text coordinate).
// but all the logic has been done using bottom-left as origin. So it is easier to invert Y
// here, than in the rest of the places... but it is not as clean.
Rect rect(rectTopLeft.origin.x, 1 - rectTopLeft.origin.y - rectTopLeft.size.height, rectTopLeft.size.width, rectTopLeft.size.height);
if (!_capInsetsNormalized.equals(rect)) {
_capInsetsNormalized = rect;
// convert it to 1-slice
if (rect.equals(Rect(0,0,1,1))) {
_numberOfSlices = 1;
free(_quads);
_quads = nullptr;
}
else
{
// convert it to 9-slice if it isn't already
if (_numberOfSlices != 9) {
_numberOfSlices = 9;
_quads = (V3F_C4B_T2F_Quad*) malloc(sizeof(*_quads) * 9);
for (int i=0; i<9; ++i) {
_quads[i].bl.colors = Color4B::WHITE;
_quads[i].br.colors = Color4B::WHITE;
_quads[i].tl.colors = Color4B::WHITE;
_quads[i].tr.colors = Color4B::WHITE;
}
}
}
updateStretchFactor();
updatePoly();
}
}
void Sprite::setCapInsets(const cocos2d::Rect &rectInPoints)
{
if (!_originalContentSize.equals(Size::ZERO))
{
Rect rect = rectInPoints;
const float x = rect.origin.x / _rect.size.width;
const float y = rect.origin.y / _rect.size.height;
const float w = rect.size.width / _rect.size.width;
const float h = rect.size.height / _rect.size.height;
setCapInsetsNormalized(Rect(x,y,w,h));
}
}
Rect Sprite::getCapInsetsNormalized() const
{
// FIXME: _capInsetsNormalized is in bottom-left coords, but should converted to top-left
Rect ret(_capInsetsNormalized.origin.x, 1 - _capInsetsNormalized.origin.y - _capInsetsNormalized.size.height, _capInsetsNormalized.size.width, _capInsetsNormalized.size.height);
return ret;
}
Rect Sprite::getCapInsets() const
{
Rect rect = getCapInsetsNormalized();
rect.origin.x *= _rect.size.width;
rect.origin.y *= _rect.size.height;
rect.size.width *= _rect.size.width;
rect.size.height *= _rect.size.height;
return rect;
}
// override this method to generate "double scale" sprites
void Sprite::setVertexRect(const Rect& rect)
{
_rect = rect;
}
void Sprite::setTextureCoords(const Rect& rectInPoints)
{
setTextureCoords(rectInPoints, &_quad);
}
void Sprite::setTextureCoords(const Rect& rectInPoints, V3F_C4B_T2F_Quad* outQuad)
{
Texture2D *tex = _batchNode ? _textureAtlas->getTexture() : _texture;
2015-12-16 11:53:59 +08:00
if (tex == nullptr)
{
return;
}
auto rectInPixels = CC_RECT_POINTS_TO_PIXELS(rectInPoints);
float atlasWidth = (float)tex->getPixelsWide();
float atlasHeight = (float)tex->getPixelsHigh();
float left, right, top, bottom;
if (_rectRotated)
{
#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
left = (2*rectInPixels.origin.x+1) / (2*atlasWidth);
right = left+(rectInPixels.size.height*2-2) / (2*atlasWidth);
top = (2*rectInPixels.origin.y+1) / (2*atlasHeight);
bottom = top+(rectInPixels.size.width*2-2) / (2*atlasHeight);
#else
left = rectInPixels.origin.x / atlasWidth;
right = (rectInPixels.origin.x + rectInPixels.size.height) / atlasWidth;
top = rectInPixels.origin.y / atlasHeight;
bottom = (rectInPixels.origin.y + rectInPixels.size.width) / atlasHeight;
#endif // CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
if (_flippedX)
{
std::swap(top, bottom);
}
if (_flippedY)
{
std::swap(left, right);
}
outQuad->bl.texCoords.u = left;
outQuad->bl.texCoords.v = top;
outQuad->br.texCoords.u = left;
outQuad->br.texCoords.v = bottom;
outQuad->tl.texCoords.u = right;
outQuad->tl.texCoords.v = top;
outQuad->tr.texCoords.u = right;
outQuad->tr.texCoords.v = bottom;
}
else
{
#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
left = (2*rectInPixels.origin.x+1) / (2*atlasWidth);
right = left + (rectInPixels.size.width*2-2) / (2*atlasWidth);
top = (2*rectInPixels.origin.y+1) / (2*atlasHeight);
bottom = top + (rectInPixels.size.height*2-2) / (2*atlasHeight);
#else
left = rectInPixels.origin.x / atlasWidth;
right = (rectInPixels.origin.x + rectInPixels.size.width) / atlasWidth;
top = rectInPixels.origin.y / atlasHeight;
bottom = (rectInPixels.origin.y + rectInPixels.size.height) / atlasHeight;
#endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
if(_flippedX)
{
std::swap(left, right);
}
if(_flippedY)
{
std::swap(top, bottom);
}
outQuad->bl.texCoords.u = left;
outQuad->bl.texCoords.v = bottom;
outQuad->br.texCoords.u = right;
outQuad->br.texCoords.v = bottom;
outQuad->tl.texCoords.u = left;
outQuad->tl.texCoords.v = top;
outQuad->tr.texCoords.u = right;
outQuad->tr.texCoords.v = top;
}
}
void Sprite::setVertexCoords(const Rect& rect, const Size& imageSize, V3F_C4B_T2F_Quad* outQuad)
{
// container size is the Size that contains the "unsliced" sprite
float relativeOffsetX = _unflippedOffsetPositionFromCenter.x;
float relativeOffsetY = _unflippedOffsetPositionFromCenter.y;
// issue #732
if (_flippedX)
{
relativeOffsetX = -relativeOffsetX;
}
if (_flippedY)
{
relativeOffsetY = -relativeOffsetY;
}
_offsetPosition.x = relativeOffsetX + (_originalContentSize.width - imageSize.width) / 2;
_offsetPosition.y = relativeOffsetY + (_originalContentSize.height - imageSize.height) / 2;
// rendering using batch node
if (_batchNode)
{
// update dirty_, don't update recursiveDirty_
setDirty(true);
}
else
{
// self rendering
// Atlas: Vertex
const float x1 = 0.0f + _offsetPosition.x + rect.origin.x;
const float y1 = 0.0f + _offsetPosition.y + rect.origin.y;
const float x2 = x1 + rect.size.width;
const float y2 = y1 + rect.size.height;
// Don't update Z.
outQuad->bl.vertices.set(x1, y1, 0.0f);
outQuad->br.vertices.set(x2, y1, 0.0f);
outQuad->tl.vertices.set(x1, y2, 0.0f);
outQuad->tr.vertices.set(x2, y2, 0.0f);
}
}
// MARK: visit, draw, transform
void Sprite::updateTransform(void)
{
2014-02-10 11:39:56 +08:00
CCASSERT(_batchNode, "updateTransform is only valid when Sprite is being rendered using an SpriteBatchNode");
// recalculate matrix only if it is dirty
if( isDirty() ) {
// If it is not visible, or one of its ancestors is not visible, then do nothing:
if( !_visible || ( _parent && _parent != _batchNode && static_cast<Sprite*>(_parent)->_shouldBeHidden) )
{
_quad.br.vertices.setZero();
_quad.tl.vertices.setZero();
_quad.tr.vertices.setZero();
_quad.bl.vertices.setZero();
_shouldBeHidden = true;
}
2013-11-26 08:33:05 +08:00
else
{
_shouldBeHidden = false;
if( ! _parent || _parent == _batchNode )
{
_transformToBatch = getNodeToParentTransform();
}
2013-11-27 03:48:37 +08:00
else
{
CCASSERT( dynamic_cast<Sprite*>(_parent), "Logic error in Sprite. Parent must be a Sprite");
2014-06-24 14:31:55 +08:00
const Mat4 &nodeToParent = getNodeToParentTransform();
Mat4 &parentTransform = static_cast<Sprite*>(_parent)->_transformToBatch;
_transformToBatch = parentTransform * nodeToParent;
}
//
// calculate the Quad based on the Affine Matrix
//
2014-06-24 18:23:10 +08:00
Size &size = _rect.size;
float x1 = _offsetPosition.x;
float y1 = _offsetPosition.y;
float x2 = x1 + size.width;
float y2 = y1 + size.height;
2015-08-05 23:17:38 +08:00
float x = _transformToBatch.m[12];
float y = _transformToBatch.m[13];
float cr = _transformToBatch.m[0];
float sr = _transformToBatch.m[1];
float cr2 = _transformToBatch.m[5];
float sr2 = -_transformToBatch.m[4];
float ax = x1 * cr - y1 * sr2 + x;
float ay = x1 * sr + y1 * cr2 + y;
float bx = x2 * cr - y1 * sr2 + x;
float by = x2 * sr + y1 * cr2 + y;
float cx = x2 * cr - y2 * sr2 + x;
float cy = x2 * sr + y2 * cr2 + y;
float dx = x1 * cr - y2 * sr2 + x;
float dy = x1 * sr + y2 * cr2 + y;
2015-07-08 15:30:34 +08:00
_quad.bl.vertices.set(SPRITE_RENDER_IN_SUBPIXEL(ax), SPRITE_RENDER_IN_SUBPIXEL(ay), _positionZ);
_quad.br.vertices.set(SPRITE_RENDER_IN_SUBPIXEL(bx), SPRITE_RENDER_IN_SUBPIXEL(by), _positionZ);
_quad.tl.vertices.set(SPRITE_RENDER_IN_SUBPIXEL(dx), SPRITE_RENDER_IN_SUBPIXEL(dy), _positionZ);
_quad.tr.vertices.set(SPRITE_RENDER_IN_SUBPIXEL(cx), SPRITE_RENDER_IN_SUBPIXEL(cy), _positionZ);
2015-12-14 15:35:26 +08:00
setTextureCoords(_rect);
}
// MARMALADE CHANGE: ADDED CHECK FOR nullptr, TO PERMIT SPRITES WITH NO BATCH NODE / TEXTURE ATLAS
if (_textureAtlas)
{
_textureAtlas->updateQuad(&_quad, _atlasIndex);
}
2013-11-26 08:33:05 +08:00
_recursiveDirty = false;
setDirty(false);
}
2012-11-14 18:05:15 +08:00
// MARMALADE CHANGED
// recursively iterate over children
2013-11-27 03:48:37 +08:00
/* if( _hasChildren )
2012-11-14 18:05:15 +08:00
{
// MARMALADE: CHANGED TO USE Node*
// NOTE THAT WE HAVE ALSO DEFINED virtual Node::updateTransform()
arrayMakeObjectsPerformSelector(_children, updateTransform, Sprite*);
2012-11-14 18:05:15 +08:00
}*/
Node::updateTransform();
}
// draw
void Sprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
2015-12-16 11:53:59 +08:00
if (_texture == nullptr)
{
return;
}
2015-01-14 10:56:51 +08:00
#if CC_USE_CULLING
// Don't calculate the culling if the transform was not updated
2015-07-30 11:53:23 +08:00
auto visitingCamera = Camera::getVisitingCamera();
auto defaultCamera = Camera::getDefaultCamera();
if (visitingCamera == defaultCamera) {
_insideBounds = ((flags & FLAGS_TRANSFORM_DIRTY) || visitingCamera->isViewProjectionUpdated()) ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;
2015-07-30 11:53:23 +08:00
}
else
{
// XXX: this always return true since
2015-07-30 11:53:23 +08:00
_insideBounds = renderer->checkVisibility(transform, _contentSize);
}
if(_insideBounds)
2015-01-14 10:56:51 +08:00
#endif
{
_trianglesCommand.init(_globalZOrder,
_texture,
getGLProgramState(),
_blendFunc,
_polyInfo.triangles,
transform,
flags);
2015-05-22 15:54:56 +08:00
renderer->addCommand(&_trianglesCommand);
2015-08-26 18:14:25 +08:00
#if CC_SPRITE_DEBUG_DRAW
_debugDrawNode->clear();
auto count = _polyInfo.triangles.indexCount/3;
auto indices = _polyInfo.triangles.indices;
auto verts = _polyInfo.triangles.verts;
for(ssize_t i = 0; i < count; i++)
{
//draw 3 lines
Vec3 from =verts[indices[i*3]].vertices;
Vec3 to = verts[indices[i*3+1]].vertices;
_debugDrawNode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::WHITE);
from =verts[indices[i*3+1]].vertices;
to = verts[indices[i*3+2]].vertices;
_debugDrawNode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::WHITE);
from =verts[indices[i*3+2]].vertices;
to = verts[indices[i*3]].vertices;
_debugDrawNode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::WHITE);
}
#endif //CC_SPRITE_DEBUG_DRAW
}
2013-11-27 09:13:04 +08:00
}
// MARK: visit, draw, transform
void Sprite::addChild(Node *child, int zOrder, int tag)
{
CCASSERT(child != nullptr, "Argument must be non-nullptr");
2015-12-16 11:53:59 +08:00
if (child == nullptr)
{
return;
}
if (_batchNode)
{
Sprite* childSprite = dynamic_cast<Sprite*>(child);
CCASSERT( childSprite, "CCSprite only supports Sprites as children when using SpriteBatchNode");
CCASSERT(childSprite->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "childSprite's texture name should be equal to _textureAtlas's texture name!");
//put it in descendants array of batch node
_batchNode->appendChild(childSprite);
if (!_reorderChildDirty)
{
setReorderChildDirtyRecursively();
}
}
//CCNode already sets isReorderChildDirty_ so this needs to be after batchNode check
Node::addChild(child, zOrder, tag);
}
2014-06-25 11:27:48 +08:00
void Sprite::addChild(Node *child, int zOrder, const std::string &name)
{
CCASSERT(child != nullptr, "Argument must be non-nullptr");
2015-12-16 11:53:59 +08:00
if (child == nullptr)
{
return;
}
2014-06-25 11:27:48 +08:00
if (_batchNode)
{
Sprite* childSprite = dynamic_cast<Sprite*>(child);
CCASSERT( childSprite, "CCSprite only supports Sprites as children when using SpriteBatchNode");
CCASSERT(childSprite->getTexture()->getName() == _textureAtlas->getTexture()->getName(),
"childSprite's texture name should be equal to _textureAtlas's texture name.");
2014-06-25 11:27:48 +08:00
//put it in descendants array of batch node
_batchNode->appendChild(childSprite);
if (!_reorderChildDirty)
{
setReorderChildDirtyRecursively();
}
}
//CCNode already sets isReorderChildDirty_ so this needs to be after batchNode check
Node::addChild(child, zOrder, name);
}
void Sprite::reorderChild(Node *child, int zOrder)
{
CCASSERT(child != nullptr, "child must be non null");
CCASSERT(_children.contains(child), "child does not belong to this");
if( _batchNode && ! _reorderChildDirty)
{
setReorderChildDirtyRecursively();
_batchNode->reorderBatch(true);
}
Node::reorderChild(child, zOrder);
}
void Sprite::removeChild(Node *child, bool cleanup)
{
if (_batchNode)
{
_batchNode->removeSpriteFromAtlas((Sprite*)(child));
}
Node::removeChild(child, cleanup);
}
void Sprite::removeAllChildrenWithCleanup(bool cleanup)
{
if (_batchNode)
{
for(const auto &child : _children) {
Sprite* sprite = dynamic_cast<Sprite*>(child);
if (sprite)
{
_batchNode->removeSpriteFromAtlas(sprite);
}
}
}
Node::removeAllChildrenWithCleanup(cleanup);
}
void Sprite::sortAllChildren()
{
if (_reorderChildDirty)
{
sortNodes(_children);
if ( _batchNode)
{
for(const auto &child : _children)
child->sortAllChildren();
}
_reorderChildDirty = false;
}
}
//
// Node property overloads
// used only when parent is SpriteBatchNode
//
void Sprite::setReorderChildDirtyRecursively(void)
{
//only set parents flag the first time
if ( ! _reorderChildDirty )
{
_reorderChildDirty = true;
Node* node = static_cast<Node*>(_parent);
while (node && node != _batchNode)
{
static_cast<Sprite*>(node)->setReorderChildDirtyRecursively();
node=node->getParent();
}
}
}
void Sprite::setDirtyRecursively(bool bValue)
{
_recursiveDirty = bValue;
setDirty(bValue);
for(const auto &child: _children) {
Sprite* sp = dynamic_cast<Sprite*>(child);
if (sp)
{
sp->setDirtyRecursively(true);
}
}
}
// FIXME: HACK: optimization
#define SET_DIRTY_RECURSIVELY() { \
if (! _recursiveDirty) { \
_recursiveDirty = true; \
setDirty(true); \
if (!_children.empty()) \
setDirtyRecursively(true); \
} \
}
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
void Sprite::setPosition(const Vec2& pos)
{
Node::setPosition(pos);
SET_DIRTY_RECURSIVELY();
}
void Sprite::setPosition(float x, float y)
{
Node::setPosition(x, y);
SET_DIRTY_RECURSIVELY();
}
void Sprite::setRotation(float rotation)
{
Node::setRotation(rotation);
SET_DIRTY_RECURSIVELY();
}
void Sprite::setRotationSkewX(float fRotationX)
2012-11-14 18:05:15 +08:00
{
Node::setRotationSkewX(fRotationX);
2012-11-14 18:05:15 +08:00
SET_DIRTY_RECURSIVELY();
}
void Sprite::setRotationSkewY(float fRotationY)
2012-11-14 18:05:15 +08:00
{
Node::setRotationSkewY(fRotationY);
2012-11-14 18:05:15 +08:00
SET_DIRTY_RECURSIVELY();
}
void Sprite::setSkewX(float sx)
{
Node::setSkewX(sx);
SET_DIRTY_RECURSIVELY();
}
void Sprite::setSkewY(float sy)
{
Node::setSkewY(sy);
SET_DIRTY_RECURSIVELY();
}
void Sprite::setScaleX(float scaleX)
{
Node::setScaleX(scaleX);
SET_DIRTY_RECURSIVELY();
}
void Sprite::setScaleY(float scaleY)
{
Node::setScaleY(scaleY);
SET_DIRTY_RECURSIVELY();
}
void Sprite::setScale(float fScale)
{
Node::setScale(fScale);
SET_DIRTY_RECURSIVELY();
}
2013-11-06 21:42:31 +08:00
void Sprite::setScale(float scaleX, float scaleY)
{
Node::setScale(scaleX, scaleY);
SET_DIRTY_RECURSIVELY();
}
void Sprite::setPositionZ(float fVertexZ)
{
Node::setPositionZ(fVertexZ);
SET_DIRTY_RECURSIVELY();
}
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
void Sprite::setAnchorPoint(const Vec2& anchor)
{
Node::setAnchorPoint(anchor);
SET_DIRTY_RECURSIVELY();
}
2016-04-22 20:36:02 +08:00
void Sprite::setIgnoreAnchorPointForPosition(bool value)
{
2016-04-22 20:36:02 +08:00
CCASSERT(! _batchNode, "setIgnoreAnchorPointForPosition is invalid in Sprite");
Node::setIgnoreAnchorPointForPosition(value);
}
void Sprite::setVisible(bool bVisible)
{
Node::setVisible(bVisible);
SET_DIRTY_RECURSIVELY();
}
void Sprite::setContentSize(const Size& size)
{
Node::setContentSize(size);
updateStretchFactor();
updatePoly();
}
void Sprite::updateStretchFactor()
{
const Size size = getContentSize();
const float adjustedWidth = size.width - (_originalContentSize.width - _rect.size.width);
const float adjustedHeight = size.height - (_originalContentSize.height - _rect.size.height);
if (_numberOfSlices == 1)
{
const float x_factor = adjustedWidth / _rect.size.width;
const float y_factor = adjustedHeight / _rect.size.height;
_strechFactor = Vec2(x_factor, y_factor);
}
else
{
const float x1 = _rect.size.width * _capInsetsNormalized.origin.x;
const float x2 = _rect.size.width * _capInsetsNormalized.size.width;
const float x3 = _rect.size.width * (1 - _capInsetsNormalized.origin.x - _capInsetsNormalized.size.width);
const float y1 = _rect.size.height * _capInsetsNormalized.origin.y;
const float y2 = _rect.size.height * _capInsetsNormalized.size.height;
const float y3 = _rect.size.height * (1 - _capInsetsNormalized.origin.y - _capInsetsNormalized.size.height);
const float x_factor = (adjustedWidth - x1 - x3) / x2;
const float y_factor = (adjustedHeight - y1 - y3) / y2;
_strechFactor = Vec2(x_factor, y_factor);
}
}
void Sprite::setFlippedX(bool flippedX)
{
if (_flippedX != flippedX)
{
_flippedX = flippedX;
2015-07-31 15:49:44 +08:00
for (ssize_t i = 0; i < _polyInfo.triangles.vertCount; i++) {
auto& v = _polyInfo.triangles.verts[i].vertices;
v.x = _contentSize.width -v.x;
}
2015-08-05 23:17:38 +08:00
if (_textureAtlas) {
setDirty(true);
}
}
}
bool Sprite::isFlippedX(void) const
{
return _flippedX;
}
void Sprite::setFlippedY(bool flippedY)
{
if (_flippedY != flippedY)
{
_flippedY = flippedY;
2015-07-31 15:49:44 +08:00
for (ssize_t i = 0; i < _polyInfo.triangles.vertCount; i++) {
auto& v = _polyInfo.triangles.verts[i].vertices;
v.y = _contentSize.height -v.y;
}
2015-08-05 23:17:38 +08:00
if (_textureAtlas) {
setDirty(true);
}
}
}
bool Sprite::isFlippedY(void) const
{
return _flippedY;
}
//
// MARK: RGBA protocol
//
void Sprite::updateColor(void)
{
Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity );
2013-02-27 15:30:49 +08:00
// special opacity for premultiplied textures
if (_opacityModifyRGB)
2013-02-27 15:30:49 +08:00
{
color4.r *= _displayedOpacity/255.0f;
color4.g *= _displayedOpacity/255.0f;
color4.b *= _displayedOpacity/255.0f;
2013-02-27 15:30:49 +08:00
}
for (ssize_t i = 0; i < _polyInfo.triangles.vertCount; i++) {
_polyInfo.triangles.verts[i].colors = color4;
}
// renders using batch node
if (_batchNode)
{
if (_atlasIndex != INDEX_NOT_INITIALIZED)
{
_textureAtlas->updateQuad(&_quad, _atlasIndex);
}
else
{
// no need to set it recursively
// update dirty_, don't update recursiveDirty_
setDirty(true);
}
}
// self render
// do nothing
}
void Sprite::setOpacityModifyRGB(bool modify)
{
if (_opacityModifyRGB != modify)
{
_opacityModifyRGB = modify;
2013-02-27 15:30:49 +08:00
updateColor();
}
2013-02-27 15:30:49 +08:00
}
bool Sprite::isOpacityModifyRGB(void) const
2013-02-27 15:30:49 +08:00
{
return _opacityModifyRGB;
}
// MARK: Frames
2013-11-14 07:55:36 +08:00
void Sprite::setSpriteFrame(const std::string &spriteFrameName)
{
2015-12-16 11:53:59 +08:00
CCASSERT(!spriteFrameName.empty(), "spriteFrameName must not be empty");
if (spriteFrameName.empty())
{
return;
}
2013-11-14 07:55:36 +08:00
SpriteFrameCache *cache = SpriteFrameCache::getInstance();
SpriteFrame *spriteFrame = cache->getSpriteFrameByName(spriteFrameName);
2014-12-04 19:18:54 +08:00
CCASSERT(spriteFrame, std::string("Invalid spriteFrameName :").append(spriteFrameName).c_str());
2013-11-14 07:55:36 +08:00
setSpriteFrame(spriteFrame);
}
void Sprite::setSpriteFrame(SpriteFrame *spriteFrame)
{
// retain the sprite frame
// do not removed by SpriteFrameCache::removeUnusedSpriteFrames
if (_spriteFrame != spriteFrame)
{
CC_SAFE_RELEASE(_spriteFrame);
_spriteFrame = spriteFrame;
spriteFrame->retain();
}
_unflippedOffsetPositionFromCenter = spriteFrame->getOffset();
Texture2D *texture = spriteFrame->getTexture();
// update texture before updating texture rect
if (texture != _texture)
{
setTexture(texture);
}
// update rect
_rectRotated = spriteFrame->isRotated();
setTextureRect(spriteFrame->getRect(), _rectRotated, spriteFrame->getOriginalSize());
if (spriteFrame->hasPolygonInfo())
{
_polyInfo = spriteFrame->getPolygonInfo();
}
if (spriteFrame->hasAnchorPoint())
{
setAnchorPoint(spriteFrame->getAnchorPoint());
}
if (spriteFrame->hasCenterRect())
{
setCapInsets(spriteFrame->getCapInsets());
}
}
2013-12-12 12:07:20 +08:00
void Sprite::setDisplayFrameWithAnimationName(const std::string& animationName, ssize_t frameIndex)
{
2015-12-16 11:53:59 +08:00
CCASSERT(!animationName.empty(), "CCSprite#setDisplayFrameWithAnimationName. animationName must not be nullptr");
if (animationName.empty())
{
return;
}
Animation *a = AnimationCache::getInstance()->getAnimation(animationName);
CCASSERT(a, "CCSprite#setDisplayFrameWithAnimationName: Frame not found");
AnimationFrame* frame = a->getFrames().at(frameIndex);
CCASSERT(frame, "CCSprite#setDisplayFrame. Invalid frame");
setSpriteFrame(frame->getSpriteFrame());
}
bool Sprite::isFrameDisplayed(SpriteFrame *frame) const
{
Rect r = frame->getRect();
return (r.equals(_rect) &&
frame->getTexture()->getName() == _texture->getName() &&
frame->getOffset().equals(_unflippedOffsetPositionFromCenter));
}
SpriteFrame* Sprite::getSpriteFrame() const
{
if(nullptr != this->_spriteFrame)
{
return this->_spriteFrame;
}
return SpriteFrame::createWithTexture(_texture,
CC_RECT_POINTS_TO_PIXELS(_rect),
_rectRotated,
CC_POINT_POINTS_TO_PIXELS(_unflippedOffsetPositionFromCenter),
CC_SIZE_POINTS_TO_PIXELS(_originalContentSize));
}
SpriteBatchNode* Sprite::getBatchNode() const
{
return _batchNode;
}
void Sprite::setBatchNode(SpriteBatchNode *spriteBatchNode)
{
_batchNode = spriteBatchNode; // weak reference
// self render
if( ! _batchNode ) {
_atlasIndex = INDEX_NOT_INITIALIZED;
setTextureAtlas(nullptr);
_recursiveDirty = false;
setDirty(false);
float x1 = _offsetPosition.x;
float y1 = _offsetPosition.y;
float x2 = x1 + _rect.size.width;
float y2 = y1 + _rect.size.height;
_quad.bl.vertices.set( x1, y1, 0 );
_quad.br.vertices.set(x2, y1, 0);
_quad.tl.vertices.set(x1, y2, 0);
_quad.tr.vertices.set(x2, y2, 0);
} else {
// using batch
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
_transformToBatch = Mat4::IDENTITY;
setTextureAtlas(_batchNode->getTextureAtlas()); // weak ref
}
}
// MARK: Texture protocol
void Sprite::updateBlendFunc(void)
{
CCASSERT(! _batchNode, "CCSprite: updateBlendFunc doesn't work when the sprite is rendered using a SpriteBatchNode");
2015-09-09 11:37:41 +08:00
// it is possible to have an untextured sprite
if (! _texture || ! _texture->hasPremultipliedAlpha())
{
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
setOpacityModifyRGB(false);
}
else
{
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
setOpacityModifyRGB(true);
}
}
std::string Sprite::getDescription() const
{
int texture_id = -1;
if( _batchNode )
texture_id = _batchNode->getTextureAtlas()->getTexture()->getName();
else
texture_id = _texture->getName();
return StringUtils::format("<Sprite | Tag = %d, TextureID = %d>", _tag, texture_id );
}
const PolygonInfo& Sprite::getPolygonInfo() const
2015-06-04 15:43:31 +08:00
{
2015-06-04 16:20:00 +08:00
return _polyInfo;
2015-06-04 15:43:31 +08:00
}
void Sprite::setPolygonInfo(const PolygonInfo& info)
{
2015-06-04 16:20:00 +08:00
_polyInfo = info;
2015-06-04 15:43:31 +08:00
}
NS_CC_END