Merge pull request #1888 from folecr/s9sprite

Fix some bugs in CCScale9Sprite
This commit is contained in:
minggo 2013-01-25 02:14:59 -08:00
commit f8c4a5638d
1 changed files with 112 additions and 55 deletions

View File

@ -102,6 +102,12 @@ bool CCScale9Sprite::initWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect
return true;
}
#define TRANSLATE_X(x, y, xtranslate) \
x+=xtranslate; \
#define TRANSLATE_Y(x, y, ytranslate) \
y+=ytranslate; \
bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, bool rotated, CCRect capInsets)
{
GLubyte opacity = m_cOpacity;
@ -159,11 +165,11 @@ bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect re
// Apply the 3x3 grid format
if (rotated)
{
m_capInsetsInternal = CCRectMake(l+h/3, t+w/3, w/3, h/3);
m_capInsetsInternal = CCRectMake(h/3, w/3, h/3, w/3);
}
else
{
m_capInsetsInternal = CCRectMake(l+w/3, t+h/3, w/3, h/3);
m_capInsetsInternal = CCRectMake(w/3, h/3, w/3, h/3);
}
}
@ -262,90 +268,120 @@ bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect re
else
{
// Sprite frame is not rotated
float origin_x = rect.origin.x;
float origin_y = rect.origin.y;
float left_w = m_capInsetsInternal.origin.x;
float center_w = m_capInsetsInternal.size.width;
float right_w = rect.size.width - (left_w + center_w);
float top_h = m_capInsetsInternal.origin.y;
float center_h = m_capInsetsInternal.size.height;
float bottom_h = rect.size.height - (top_h + center_h);
// calculate rects
// ... top row
float x = origin_x;
float y = origin_y;
// top left
CCRect lefttopbounds = CCRectMake(x, y,
left_w, top_h);
// top center
TRANSLATE_X(x, y, left_w);
CCRect centertopbounds = CCRectMake(x, y,
center_w, top_h);
// top right
TRANSLATE_X(x, y, center_w);
CCRect righttopbounds = CCRectMake(x, y,
right_w, top_h);
// ... center row
x = origin_x;
y = origin_y;
TRANSLATE_Y(x, y, top_h);
// center left
CCRect leftcenterbounds = CCRectMake(x, y,
left_w, center_h);
// center center
TRANSLATE_X(x, y, left_w);
CCRect centerbounds = CCRectMake(x, y,
center_w, center_h);
// center right
TRANSLATE_X(x, y, center_w);
CCRect rightcenterbounds = CCRectMake(x, y,
right_w, center_h);
// ... bottom row
x = origin_x;
y = origin_y;
TRANSLATE_Y(x, y, top_h);
TRANSLATE_Y(x, y, center_h);
// bottom left
CCRect leftbottombounds = CCRectMake(x, y,
left_w, bottom_h);
// bottom center
TRANSLATE_X(x, y, left_w);
CCRect centerbottombounds = CCRectMake(x, y,
center_w, bottom_h);
// bottom right
TRANSLATE_X(x, y, center_w);
CCRect rightbottombounds = CCRectMake(x, y,
right_w, bottom_h);
// Centre
centre = new CCSprite();
centre->initWithTexture(scale9Image->getTexture(), m_capInsetsInternal, rotated);
centre->initWithTexture(scale9Image->getTexture(), centerbounds);
scale9Image->addChild(centre, 0, pCentre);
// Top
top = new CCSprite();
top->initWithTexture(scale9Image->getTexture(), CCRectMake(m_capInsetsInternal.origin.x,
t,
m_capInsetsInternal.size.width,
m_capInsetsInternal.origin.y - t),
rotated
);
top->initWithTexture(scale9Image->getTexture(), centertopbounds);
scale9Image->addChild(top, 1, pTop);
// Bottom
bottom = new CCSprite();
bottom->initWithTexture(scale9Image->getTexture(), CCRectMake( m_capInsetsInternal.origin.x,
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.height,
m_capInsetsInternal.size.width,
h - (m_capInsetsInternal.origin.y - t + m_capInsetsInternal.size.height)),
rotated);
bottom->initWithTexture(scale9Image->getTexture(), centerbottombounds);
scale9Image->addChild(bottom, 1, pBottom);
// Left
left = new CCSprite();
left->initWithTexture(scale9Image->getTexture(), CCRectMake(
l,
m_capInsetsInternal.origin.y,
m_capInsetsInternal.origin.x - l,
m_capInsetsInternal.size.height),
rotated);
left->initWithTexture(scale9Image->getTexture(), leftcenterbounds);
scale9Image->addChild(left, 1, pLeft);
// Right
right = new CCSprite();
right->initWithTexture(scale9Image->getTexture(), CCRectMake(
m_capInsetsInternal.origin.x + m_capInsetsInternal.size.width,
m_capInsetsInternal.origin.y,
w - (m_capInsetsInternal.origin.x - l + m_capInsetsInternal.size.width),
m_capInsetsInternal.size.height),
rotated);
right->initWithTexture(scale9Image->getTexture(), rightcenterbounds);
scale9Image->addChild(right, 1, pRight);
// Top left
topLeft = new CCSprite();
topLeft->initWithTexture(scale9Image->getTexture(), CCRectMake(
l,
t,
m_capInsetsInternal.origin.x - l,
m_capInsetsInternal.origin.y - t),
rotated);
topLeft->initWithTexture(scale9Image->getTexture(), lefttopbounds);
scale9Image->addChild(topLeft, 2, pTopLeft);
// Top right
topRight = new CCSprite();
topRight->initWithTexture(scale9Image->getTexture(), CCRectMake(
m_capInsetsInternal.origin.x + m_capInsetsInternal.size.width,
t,
w - (m_capInsetsInternal.origin.x - l + m_capInsetsInternal.size.width),
m_capInsetsInternal.origin.y - t),
rotated);
topRight->initWithTexture(scale9Image->getTexture(), righttopbounds);
scale9Image->addChild(topRight, 2, pTopRight);
// Bottom left
bottomLeft = new CCSprite();
bottomLeft->initWithTexture(scale9Image->getTexture(), CCRectMake(
l,
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.height,
m_capInsetsInternal.origin.x - l,
h - (m_capInsetsInternal.origin.y - t + m_capInsetsInternal.size.height)),
rotated);
bottomLeft->initWithTexture(scale9Image->getTexture(), leftbottombounds);
scale9Image->addChild(bottomLeft, 2, pBottomLeft);
// Bottom right
bottomRight = new CCSprite();
bottomRight->initWithTexture(scale9Image->getTexture(), CCRectMake(
m_capInsetsInternal.origin.x + m_capInsetsInternal.size.width,
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.height,
w - (m_capInsetsInternal.origin.x - l + m_capInsetsInternal.size.width),
h - (m_capInsetsInternal.origin.y - t + m_capInsetsInternal.size.height)),
rotated);
bottomRight->initWithTexture(scale9Image->getTexture(), rightbottombounds);
scale9Image->addChild(bottomRight, 2, pBottomRight);
}
@ -371,6 +407,16 @@ void CCScale9Sprite::setContentSize(const CCSize &size)
void CCScale9Sprite::updatePositions()
{
// Check that instances are non-NULL
if(!((topLeft) &&
(topRight) &&
(bottomRight) &&
(bottomLeft) &&
(centre))) {
// if any of the above sprites are NULL, return
return;
}
CCSize size = this->m_obContentSize;
float sizableWidth = size.width - topLeft->getContentSize().width - topRight->getContentSize().width;
@ -498,9 +544,12 @@ CCScale9Sprite* CCScale9Sprite::create(const char* file)
bool CCScale9Sprite::initWithSpriteFrame(CCSpriteFrame* spriteFrame, CCRect capInsets)
{
CCAssert(spriteFrame != NULL, "Sprite frame must be not nil");
CCTexture2D* texture = spriteFrame->getTexture();
CCAssert(texture != NULL, "CCTexture must be not nil");
CCSpriteBatchNode *batchnode = CCSpriteBatchNode::createWithTexture(texture, 9);
CCAssert(batchnode != NULL, "CCSpriteBatchNode must be not nil");
CCSpriteBatchNode *batchnode = CCSpriteBatchNode::createWithTexture(spriteFrame->getTexture(), 9);
bool pReturn = this->initWithBatchNode(batchnode, spriteFrame->getRect(), spriteFrame->isRotated(), capInsets);
return pReturn;
}
@ -537,9 +586,13 @@ CCScale9Sprite* CCScale9Sprite::createWithSpriteFrame(CCSpriteFrame* spriteFrame
bool CCScale9Sprite::initWithSpriteFrameName(const char* spriteFrameName, CCRect capInsets)
{
CCAssert(spriteFrameName != NULL, "Invalid spriteFrameName for sprite");
CCAssert((CCSpriteFrameCache::sharedSpriteFrameCache()) != NULL, "sharedSpriteFrameCache must be non-NULL");
CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(spriteFrameName);
CCAssert(frame != NULL, "CCSpriteFrame must be non-NULL");
if (NULL == frame) return false;
bool pReturn = this->initWithSpriteFrame(frame, capInsets);
return pReturn;
}
@ -564,6 +617,8 @@ bool CCScale9Sprite::initWithSpriteFrameName(const char* spriteFrameName)
CCScale9Sprite* CCScale9Sprite::createWithSpriteFrameName(const char* spriteFrameName)
{
CCAssert(spriteFrameName != NULL, "spriteFrameName must be non-NULL");
CCScale9Sprite* pReturn = new CCScale9Sprite();
if ( pReturn && pReturn->initWithSpriteFrameName(spriteFrameName) )
{
@ -571,6 +626,8 @@ CCScale9Sprite* CCScale9Sprite::createWithSpriteFrameName(const char* spriteFram
return pReturn;
}
CC_SAFE_DELETE(pReturn);
CCLog("Could not allocate CCScale9Sprite()");
return NULL;
}