Merge pull request #1974 from folecr/s9sprite

fixed #1710: Simpler, correct handling of spritesheet sources for CCScale9Sprite.
This commit is contained in:
James Chen 2013-02-15 19:50:02 -08:00
commit fd5fcf6726
1 changed files with 159 additions and 179 deletions

View File

@ -153,124 +153,15 @@ bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect re
m_preferredSize = m_originalSize;
m_capInsetsInternal = capInsets;
// Get the image edges
float l = rect.origin.x;
float t = rect.origin.y;
float h = rect.size.height;
float w = rect.size.width;
float h = rect.size.height;
// If there is no specified center region
if ( m_capInsetsInternal.equals(CCRectZero) )
{
// Apply the 3x3 grid format
if (rotated)
{
m_capInsetsInternal = CCRectMake(h/3, w/3, h/3, w/3);
}
else
{
// CCLog("... cap insets not specified : using default cap insets ...");
m_capInsetsInternal = CCRectMake(w/3, h/3, w/3, h/3);
}
}
//
// Set up the image
//
if (rotated)
{
// Sprite frame is rotated
// Centre
centre = new CCSprite();
centre->initWithTexture(scale9Image->getTexture(), m_capInsetsInternal, true);
scale9Image->addChild(centre, 0, pCentre);
// Bottom
bottom = new CCSprite();
bottom->initWithTexture(scale9Image->getTexture(), CCRectMake(l,
m_capInsetsInternal.origin.y,
m_capInsetsInternal.size.width,
m_capInsetsInternal.origin.x - l),
rotated
);
scale9Image->addChild(bottom, 1, pBottom);
// Top
top = new CCSprite();
top->initWithTexture(scale9Image->getTexture(),CCRectMake(m_capInsetsInternal.origin.x + m_capInsetsInternal.size.height,
m_capInsetsInternal.origin.y,
m_capInsetsInternal.size.width,
h - m_capInsetsInternal.size.height - (m_capInsetsInternal.origin.x - l)),
rotated
);
scale9Image->addChild(top, 1, pTop);
// Right
right = new CCSprite();
right->initWithTexture(scale9Image->getTexture(), CCRectMake(m_capInsetsInternal.origin.x,
m_capInsetsInternal.origin.y+m_capInsetsInternal.size.width,
w - (m_capInsetsInternal.origin.y-t)-m_capInsetsInternal.size.width,
m_capInsetsInternal.size.height),
rotated
);
scale9Image->addChild(right, 1, pRight);
// Left
left = new CCSprite();
left->initWithTexture(scale9Image->getTexture(), CCRectMake(m_capInsetsInternal.origin.x,
t,
m_capInsetsInternal.origin.y - t,
m_capInsetsInternal.size.height),
rotated
);
scale9Image->addChild(left ,1, pLeft);
// Top right
topRight = new CCSprite();
topRight->initWithTexture(scale9Image->getTexture(), CCRectMake(m_capInsetsInternal.origin.x + m_capInsetsInternal.size.height,
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.width,
w - (m_capInsetsInternal.origin.y-t)-m_capInsetsInternal.size.width,
h - m_capInsetsInternal.size.height - (m_capInsetsInternal.origin.x - l)),
rotated
);
scale9Image->addChild(topRight ,2 ,pTopRight);
// Top left
topLeft = new CCSprite();
topLeft->initWithTexture(scale9Image->getTexture(), CCRectMake(m_capInsetsInternal.origin.x + m_capInsetsInternal.size.height,
t,
m_capInsetsInternal.origin.y - t,
h - m_capInsetsInternal.size.height - (m_capInsetsInternal.origin.x - l)),
rotated
);
scale9Image->addChild(topLeft, 2, pTopLeft);
// Bottom right
bottomRight = new CCSprite();
bottomRight->initWithTexture(scale9Image->getTexture(),CCRectMake(l,
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.width,
w - (m_capInsetsInternal.origin.y-t)-m_capInsetsInternal.size.width,
m_capInsetsInternal.origin.x - l),
rotated
);
scale9Image->addChild(bottomRight, 2, pBottomRight);
// Bottom left
bottomLeft = new CCSprite();
bottomLeft->initWithTexture(scale9Image->getTexture(), CCRectMake(l,
t,
m_capInsetsInternal.origin.y - t,
m_capInsetsInternal.origin.x - l),
rotated
);
scale9Image->addChild(bottomLeft, 2, pBottomLeft);
}
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;
@ -283,8 +174,8 @@ bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect re
// calculate rects
// ... top row
float x = origin_x;
float y = origin_y;
float x = 0.0;
float y = 0.0;
// top left
CCRect lefttopbounds = CCRectMake(x, y,
@ -301,8 +192,8 @@ bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect re
right_w, top_h);
// ... center row
x = origin_x;
y = origin_y;
x = 0.0;
y = 0.0;
TRANSLATE_Y(x, y, top_h);
// center left
@ -320,8 +211,8 @@ bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect re
right_w, center_h);
// ... bottom row
x = origin_x;
y = origin_y;
x = 0.0;
y = 0.0;
TRANSLATE_Y(x, y, top_h);
TRANSLATE_Y(x, y, center_h);
@ -339,6 +230,22 @@ bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect re
CCRect rightbottombounds = CCRectMake(x, y,
right_w, bottom_h);
if (!rotated) {
// CCLog("!rotated");
CCAffineTransform t = CCAffineTransformMakeIdentity();
t = CCAffineTransformTranslate(t, rect.origin.x, rect.origin.y);
centerbounds = CCRectApplyAffineTransform(centerbounds, t);
rightbottombounds = CCRectApplyAffineTransform(rightbottombounds, t);
leftbottombounds = CCRectApplyAffineTransform(leftbottombounds, t);
righttopbounds = CCRectApplyAffineTransform(righttopbounds, t);
lefttopbounds = CCRectApplyAffineTransform(lefttopbounds, t);
rightcenterbounds = CCRectApplyAffineTransform(rightcenterbounds, t);
leftcenterbounds = CCRectApplyAffineTransform(leftcenterbounds, t);
centerbottombounds = CCRectApplyAffineTransform(centerbottombounds, t);
centertopbounds = CCRectApplyAffineTransform(centertopbounds, t);
// Centre
centre = new CCSprite();
centre->initWithTexture(scale9Image->getTexture(), centerbounds);
@ -383,6 +290,79 @@ bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect re
bottomRight = new CCSprite();
bottomRight->initWithTexture(scale9Image->getTexture(), rightbottombounds);
scale9Image->addChild(bottomRight, 2, pBottomRight);
} else {
// set up transformation of coordinates
// to handle the case where the sprite is stored rotated
// in the spritesheet
// CCLog("rotated");
float midpointx = rect.origin.x + (rect.size.width/2);
float midpointy = rect.origin.y + (rect.size.height/2);
CCAffineTransform t = CCAffineTransformMakeIdentity();
// RIGHT TO LEFT!
t = CCAffineTransformTranslate(t, midpointx, midpointy);
t = CCAffineTransformScale(t, -1.0, -1.0);
t = CCAffineTransformRotate(t, -1.57079633);
t = CCAffineTransformTranslate(t, -midpointx, -midpointy);
t = CCAffineTransformTranslate(t, rect.origin.x, rect.origin.y);
centerbounds = CCRectApplyAffineTransform(centerbounds, t);
rightbottombounds = CCRectApplyAffineTransform(rightbottombounds, t);
leftbottombounds = CCRectApplyAffineTransform(leftbottombounds, t);
righttopbounds = CCRectApplyAffineTransform(righttopbounds, t);
lefttopbounds = CCRectApplyAffineTransform(lefttopbounds, t);
rightcenterbounds = CCRectApplyAffineTransform(rightcenterbounds, t);
leftcenterbounds = CCRectApplyAffineTransform(leftcenterbounds, t);
centerbottombounds = CCRectApplyAffineTransform(centerbottombounds, t);
centertopbounds = CCRectApplyAffineTransform(centertopbounds, t);
// Centre
centre = new CCSprite();
centre->initWithTexture(scale9Image->getTexture(), centerbounds, true);
scale9Image->addChild(centre, 0, pCentre);
// Top
top = new CCSprite();
top->initWithTexture(scale9Image->getTexture(), centertopbounds, true);
scale9Image->addChild(top, 1, pTop);
// Bottom
bottom = new CCSprite();
bottom->initWithTexture(scale9Image->getTexture(), centerbottombounds, true);
scale9Image->addChild(bottom, 1, pBottom);
// Left
left = new CCSprite();
left->initWithTexture(scale9Image->getTexture(), leftcenterbounds, true);
scale9Image->addChild(left, 1, pLeft);
// Right
right = new CCSprite();
right->initWithTexture(scale9Image->getTexture(), rightcenterbounds, true);
scale9Image->addChild(right, 1, pRight);
// Top left
topLeft = new CCSprite();
topLeft->initWithTexture(scale9Image->getTexture(), lefttopbounds, true);
scale9Image->addChild(topLeft, 2, pTopLeft);
// Top right
topRight = new CCSprite();
topRight->initWithTexture(scale9Image->getTexture(), righttopbounds, true);
scale9Image->addChild(topRight, 2, pTopRight);
// Bottom left
bottomLeft = new CCSprite();
bottomLeft->initWithTexture(scale9Image->getTexture(), leftbottombounds, true);
scale9Image->addChild(bottomLeft, 2, pBottomLeft);
// Bottom right
bottomRight = new CCSprite();
bottomRight->initWithTexture(scale9Image->getTexture(), rightbottombounds, true);
scale9Image->addChild(bottomRight, 2, pBottomRight);
}
this->setContentSize(rect.size);