void GHBoneSkin::transform()
{
    if(sprite == NULL || bone == NULL)return;
    
    
    float degrees = bone->degrees();
    
    
    CCPoint posOffset = positionOffset;


    CCPoint bonePos = ccp(((GHBone*)bone->getParent())->getPosition().x,
                          ((GHBone*)bone->getParent())->getPosition().y);

    
    CCAffineTransform transformOffset = CCAffineTransformTranslate(CCAffineTransformMakeIdentity(), bonePos.x, bonePos.y);
   
    CCAffineTransform transform = CCAffineTransformRotate(CCAffineTransformMakeIdentity(),
                                                          CC_DEGREES_TO_RADIANS(degrees - angleOffset));
    
    
    //we need 2 points
    //origin - upward point, in order to calculate new position and new angle
    CCPoint origin = ccp(0,0);
    CCPoint upward = ccp(0, -10);
    
    
    CCAffineTransform transform3 = CCAffineTransformRotate(CCAffineTransformMakeIdentity(),
                                                           CC_DEGREES_TO_RADIANS(degrees - angleOffset - connectionAngle));
    
    posOffset = CCPointApplyAffineTransform(posOffset, transform3);


    origin = CCPointApplyAffineTransform(origin, transform);
    upward = CCPointApplyAffineTransform(upward, transform);
    
    
    origin = CCPointApplyAffineTransform(origin, transformOffset);
    upward = CCPointApplyAffineTransform(upward, transformOffset);

    
    //now that we have the 2 points - lets calculate the angle
    float newAngle = (atan2(upward.y - origin.y,
                            upward.x - origin.x)*180.0)/M_PI + 90.0;
    
    sprite->setPosition(ccp(origin.x + posOffset.x, origin.y - posOffset.y));
    sprite->setRotation(newAngle);
}
示例#2
0
CCNode::CCNode(void)
: m_fRotationX(0.0f)
, m_fRotationY(0.0f)
, m_fScaleX(1.0f)
, m_fScaleY(1.0f)
, m_fVertexZ(0.0f)
, m_obPosition(CCPointZero)
, m_fSkewX(0.0f)
, m_fSkewY(0.0f)
, m_obAnchorPointInPoints(CCPointZero)
, m_obAnchorPoint(CCPointZero)
, m_obContentSize(CCSizeZero)
, m_sAdditionalTransform(CCAffineTransformMakeIdentity())
, m_pCamera(NULL)
// children (lazy allocs)
// lazy alloc
, m_pGrid(NULL)
, m_nZOrder(0)
, m_pChildren(NULL)
, m_pParent(NULL)
// "whole screen" objects. like Scenes and Layers, should set m_bIgnoreAnchorPointForPosition to true
, m_nTag(kCCNodeTagInvalid)
// userData is always inited as nil
, m_pUserData(NULL)
, m_pUserObject(NULL)
, m_pShaderProgram(NULL)
, m_eGLServerState(ccGLServerState(0))
, m_uOrderOfArrival(0)
, m_bRunning(false)
, m_bTransformDirty(true)
, m_bInverseDirty(true)
, m_bAdditionalTransformDirty(false)
, m_bVisible(true)
, m_bIgnoreAnchorPointForPosition(false)
, m_bReorderChildDirty(false)
, m_pComponentContainer(NULL)
// merge CCNodeRGBA
, m_displayedOpacity(255)
, m_realOpacity(255)
, m_isOpacityModifyRGB(false)
, m_displayedColor(ccWHITE)
, m_realColor(ccWHITE)
, m_cascadeColorEnabled(false)
, m_cascadeOpacityEnabled(false)
, m_drawOrder(0)
// touch
, m_bTouchCaptureEnabled(true)
, m_bTouchSwallowEnabled(true)
, m_bTouchEnabled(false)
, m_eTouchMode(kCCTouchesOneByOne)
{
    // set default scheduler and actionManager
    CCDirector *director = CCDirector::sharedDirector();
    m_pActionManager = director->getActionManager();
    m_pActionManager->retain();
    m_pScheduler = director->getScheduler();
    m_pScheduler->retain();
    m_pComponentContainer = new CCComponentContainer(this);
}
示例#3
0
CCNode::CCNode(void)
: m_fRotationX(0.0f)
, m_fRotationY(0.0f)
, m_fScaleX(1.0f)
, m_fScaleY(1.0f)
, m_fVertexZ(0.0f)
, m_obPosition(CCPointZero)
, m_fSkewX(0.0f)
, m_fSkewY(0.0f)
, m_obAnchorPointInPoints(CCPointZero)
, m_obAnchorPoint(CCPointZero)
, m_obContentSize(CCSizeZero)
, m_sAdditionalTransform(CCAffineTransformMakeIdentity())
, m_pCamera(NULL)
// children (lazy allocs)
// lazy alloc
, m_pGrid(NULL)
, m_nZOrder(0)
, m_pChildren(NULL)
, m_pParent(NULL)
// "whole screen" objects. like Scenes and Layers, should set m_bIgnoreAnchorPointForPosition to true
, m_nTag(kCCNodeTagInvalid)
// userData is always inited as nil
, m_pUserData(NULL)
, m_pUserObject(NULL)
, m_pUserObjectNR(NULL)
, m_pShaderProgram(NULL)
, m_eGLServerState(ccGLServerState(0))
, m_uOrderOfArrival(0)
, m_bRunning(false)
, m_bTransformDirty(true)
, m_bInverseDirty(true)
, m_bAdditionalTransformDirty(false)
, m_bVisible(true)
, m_bIgnoreAnchorPointForPosition(false)
, m_bReorderChildDirty(false)
, m_pComponentContainer(NULL)
, m_bInformDetach(false)
{
    // set default scheduler and actionManager
    CCDirector *director = CCDirector::sharedDirector();
    m_pActionManager = director->getActionManager();
    CC_SAFE_RETAIN(m_pActionManager);
    m_pScheduler = director->getScheduler();
    CC_SAFE_RETAIN(m_pScheduler);

    CCScriptEngineProtocol* pEngine = CCScriptEngineManager::sharedManager()->getScriptEngine();
    m_eScriptType = pEngine != NULL ? pEngine->getScriptType() : kScriptTypeNone;
    m_pComponentContainer = new CCComponentContainer(this);
    memset(&m_nUpdateScriptHandler, 0, sizeof(ccScriptFunction));
    memset(&m_nScriptHandler, 0, sizeof(ccScriptFunction));
}
示例#4
0
void HoleDemo::setup()
{
    CCSprite *target = CCSprite::create(s_pPathBlock);
    target->setAnchorPoint(CCPointZero);
    target->setScale(3);
    
    m_pOuterClipper = CCClippingNode::create();
    m_pOuterClipper->retain();
    CCAffineTransform tranform = CCAffineTransformMakeIdentity();
    tranform = CCAffineTransformScale(tranform, target->getScale(), target->getScale());

    m_pOuterClipper->setContentSize( CCSizeApplyAffineTransform(target->getContentSize(), tranform));
    m_pOuterClipper->setAnchorPoint( ccp(0.5, 0.5) );
    m_pOuterClipper->setPosition( ccpMult(ccpFromSize(this->getContentSize()), 0.5f) );
    m_pOuterClipper->runAction(CCRepeatForever::create(CCRotateBy::create(1, 45)));
    
    m_pOuterClipper->setStencil( target );
    
    CCClippingNode *holesClipper = CCClippingNode::create();
    holesClipper->setInverted(true);
    holesClipper->setAlphaThreshold( 0.05f );
    
    holesClipper->addChild(target);
    
    m_pHoles = CCNode::create();
    m_pHoles->retain();
    
    holesClipper->addChild(m_pHoles);
    
    m_pHolesStencil = CCNode::create();
    m_pHolesStencil->retain();
    
    holesClipper->setStencil( m_pHolesStencil);
    
    m_pOuterClipper->addChild(holesClipper);
    
    this->addChild(m_pOuterClipper);
        
    this->setTouchEnabled(true);
}
示例#5
0
bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, bool rotated, CCRect capInsets)
{
    GLubyte opacity = getOpacity();
    ccColor3B color = getColor();

    // Release old sprites
    this->removeAllChildrenWithCleanup(true);

    CC_SAFE_RELEASE(this->_centre);
    CC_SAFE_RELEASE(this->_top);
    CC_SAFE_RELEASE(this->_topLeft);
    CC_SAFE_RELEASE(this->_topRight);
    CC_SAFE_RELEASE(this->_left);
    CC_SAFE_RELEASE(this->_right);
    CC_SAFE_RELEASE(this->_bottomLeft);
    CC_SAFE_RELEASE(this->_bottom);
    CC_SAFE_RELEASE(this->_bottomRight);

    
    if(this->_scale9Image != batchnode)
    {
        CC_SAFE_RELEASE(this->_scale9Image);
        _scale9Image = batchnode;
        CC_SAFE_RETAIN(_scale9Image);
    }

    _scale9Image->removeAllChildrenWithCleanup(true);

    m_capInsets = capInsets;
    
    // If there is no given rect
    if ( rect.equals(CCRectZero) )
    {
        // Get the texture size as original
        CCSize textureSize = _scale9Image->getTextureAtlas()->getTexture()->getContentSize();
    
        rect = CCRectMake(0, 0, textureSize.width, textureSize.height);
    }
    
    // Set the given rect's size as original size
    m_spriteRect = rect;
    m_originalSize = rect.size;
    m_preferredSize = m_originalSize;
    m_capInsetsInternal = capInsets;
    
    float w = rect.size.width;
    float h = rect.size.height;

    // If there is no specified center region
    if ( m_capInsetsInternal.equals(CCRectZero) )
    {
        // CCLog("... cap insets not specified : using default cap insets ...");
        m_capInsetsInternal = CCRectMake(w/3, h/3, w/3, h/3);
    }

    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 = 0.0;
    float y = 0.0;

    // 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 = 0.0;
    y = 0.0;
    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 = 0.0;
    y = 0.0;
    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);

    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);
        _scale9Image->addChild(_centre, 0, pCentre);
        
        // Top
        _top = new CCSprite();
        _top->initWithTexture(_scale9Image->getTexture(), centertopbounds);
        _scale9Image->addChild(_top, 1, pTop);
        
        // Bottom
        _bottom = new CCSprite();
        _bottom->initWithTexture(_scale9Image->getTexture(), centerbottombounds);
        _scale9Image->addChild(_bottom, 1, pBottom);
        
        // Left
        _left = new CCSprite();
        _left->initWithTexture(_scale9Image->getTexture(), leftcenterbounds);
        _scale9Image->addChild(_left, 1, pLeft);
        
        // Right
        _right = new CCSprite();
        _right->initWithTexture(_scale9Image->getTexture(), rightcenterbounds);
        _scale9Image->addChild(_right, 1, pRight);
        
        // Top left
        _topLeft = new CCSprite();
        _topLeft->initWithTexture(_scale9Image->getTexture(), lefttopbounds);
        _scale9Image->addChild(_topLeft, 2, pTopLeft);
        
        // Top right
        _topRight = new CCSprite();
        _topRight->initWithTexture(_scale9Image->getTexture(), righttopbounds);
        _scale9Image->addChild(_topRight, 2, pTopRight);
        
        // Bottom left
        _bottomLeft = new CCSprite();
        _bottomLeft->initWithTexture(_scale9Image->getTexture(), leftbottombounds);
        _scale9Image->addChild(_bottomLeft, 2, pBottomLeft);
        
        // Bottom right
        _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");

        CCAffineTransform t = CCAffineTransformMakeIdentity();

        CCRect rotatedcenterbounds = centerbounds;
        CCRect rotatedrightbottombounds = rightbottombounds;
        CCRect rotatedleftbottombounds = leftbottombounds;
        CCRect rotatedrighttopbounds = righttopbounds;
        CCRect rotatedlefttopbounds = lefttopbounds;
        CCRect rotatedrightcenterbounds = rightcenterbounds;
        CCRect rotatedleftcenterbounds = leftcenterbounds;
        CCRect rotatedcenterbottombounds = centerbottombounds;
        CCRect rotatedcentertopbounds = centertopbounds;
        
        t = CCAffineTransformTranslate(t, rect.size.height+rect.origin.x, rect.origin.y);
        t = CCAffineTransformRotate(t, 1.57079633f);
        
        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);

        rotatedcenterbounds.origin = centerbounds.origin;
        rotatedrightbottombounds.origin = rightbottombounds.origin;
        rotatedleftbottombounds.origin = leftbottombounds.origin;
        rotatedrighttopbounds.origin = righttopbounds.origin;
        rotatedlefttopbounds.origin = lefttopbounds.origin;
        rotatedrightcenterbounds.origin = rightcenterbounds.origin;
        rotatedleftcenterbounds.origin = leftcenterbounds.origin;
        rotatedcenterbottombounds.origin = centerbottombounds.origin;
        rotatedcentertopbounds.origin = centertopbounds.origin;

        // Centre
        _centre = new CCSprite();
        _centre->initWithTexture(_scale9Image->getTexture(), rotatedcenterbounds, true);
        _scale9Image->addChild(_centre, 0, pCentre);
        
        // Top
        _top = new CCSprite();
        _top->initWithTexture(_scale9Image->getTexture(), rotatedcentertopbounds, true);
        _scale9Image->addChild(_top, 1, pTop);
        
        // Bottom
        _bottom = new CCSprite();
        _bottom->initWithTexture(_scale9Image->getTexture(), rotatedcenterbottombounds, true);
        _scale9Image->addChild(_bottom, 1, pBottom);
        
        // Left
        _left = new CCSprite();
        _left->initWithTexture(_scale9Image->getTexture(), rotatedleftcenterbounds, true);
        _scale9Image->addChild(_left, 1, pLeft);
        
        // Right
        _right = new CCSprite();
        _right->initWithTexture(_scale9Image->getTexture(), rotatedrightcenterbounds, true);
        _scale9Image->addChild(_right, 1, pRight);
        
        // Top left
        _topLeft = new CCSprite();
        _topLeft->initWithTexture(_scale9Image->getTexture(), rotatedlefttopbounds, true);
        _scale9Image->addChild(_topLeft, 2, pTopLeft);
        
        // Top right
        _topRight = new CCSprite();
        _topRight->initWithTexture(_scale9Image->getTexture(), rotatedrighttopbounds, true);
        _scale9Image->addChild(_topRight, 2, pTopRight);
        
        // Bottom left
        _bottomLeft = new CCSprite();
        _bottomLeft->initWithTexture(_scale9Image->getTexture(), rotatedleftbottombounds, true);
        _scale9Image->addChild(_bottomLeft, 2, pBottomLeft);
        
        // Bottom right
        _bottomRight = new CCSprite();
        _bottomRight->initWithTexture(_scale9Image->getTexture(), rotatedrightbottombounds, true);
        _scale9Image->addChild(_bottomRight, 2, pBottomRight);
    }

    this->setContentSize(rect.size);
    this->addChild(_scale9Image);
    
    if (m_bSpritesGenerated)
        {
            // Restore color and opacity
            this->setOpacity(opacity);
            this->setColor(color);
        }
    m_bSpritesGenerated = true;

    return true;
}
namespace cocos2d {

	CCAffineTransform __CCAffineTransformMake(float a, float b, float c, float d, float tx, float ty)
	{
		CCAffineTransform t;
		t.a = a; t.b = b; t.c = c; t.d = d; t.tx = tx; t.ty = ty;
		return t;
	}

	CCPoint __CCPointApplyAffineTransform(const CCPoint& point, const CCAffineTransform& t)
	{
		CCPoint p;
		p.x = (float)((double)t.a * point.x + (double)t.c * point.y + t.tx);
		p.y = (float)((double)t.b * point.x + (double)t.d * point.y + t.ty);
		return p;
	}

	CCSize __CCSizeApplyAffineTransform(const CCSize& size, const CCAffineTransform& t)
	{
		CCSize s;
		s.width = (float)((double)t.a * size.width + (double)t.c * size.height);
		s.height = (float)((double)t.b * size.width + (double)t.d * size.height);
		return s;
	}


	CCAffineTransform CCAffineTransformMakeIdentity()
	{
		return __CCAffineTransformMake(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
	}

	extern const CCAffineTransform CCAffineTransformIdentity = CCAffineTransformMakeIdentity();


	CCRect CCRectApplyAffineTransform(const CCRect& rect, const CCAffineTransform& anAffineTransform)
	{
		float top = CCRect::CCRectGetMinY(rect);
		float left = CCRect::CCRectGetMinX(rect);
		float right = CCRect::CCRectGetMaxX(rect);
		float bottom = CCRect::CCRectGetMaxY(rect);

		CCPoint topLeft = CCPointApplyAffineTransform(CCPointMake(left, top), anAffineTransform);
		CCPoint topRight = CCPointApplyAffineTransform(CCPointMake(right, top), anAffineTransform);
		CCPoint bottomLeft = CCPointApplyAffineTransform(CCPointMake(left, bottom), anAffineTransform);
		CCPoint bottomRight = CCPointApplyAffineTransform(CCPointMake(right, bottom), anAffineTransform);

		float minX = std::min(std::min(topLeft.x, topRight.x), std::min(bottomLeft.x, bottomRight.x));
		float maxX = std::max(std::max(topLeft.x, topRight.x), std::max(bottomLeft.x, bottomRight.x));
		float minY = std::min(std::min(topLeft.y, topRight.y), std::min(bottomLeft.y, bottomRight.y));
		float maxY = std::max(std::max(topLeft.y, topRight.y), std::max(bottomLeft.y, bottomRight.y));

		return CCRectMake(minX, minY, (maxX - minX), (maxY - minY));
	}

	CCAffineTransform CCAffineTransformTranslate(const CCAffineTransform& t, float tx, float ty)
	{
		return __CCAffineTransformMake(t.a, t.b, t.c, t.d, t.tx + t.a * tx + t.c * ty, t.ty + t.b * tx + t.d * ty);
	}

	CCAffineTransform CCAffineTransformScale(const CCAffineTransform& t, float sx, float sy)
	{
		return __CCAffineTransformMake(t.a * sx, t.b * sx, t.c * sy, t.d * sy, t.tx, t.ty);
	}

	CCAffineTransform CCAffineTransformRotate(const CCAffineTransform& t, float anAngle)
	{
		float fSin = sin(anAngle);
		float fCos = cos(anAngle);

		return __CCAffineTransformMake(	t.a * fCos + t.c * fSin,
			t.b * fCos + t.d * fSin,
			t.c * fCos - t.a * fSin,
			t.d * fCos - t.b * fSin,
			t.tx,
			t.ty);
	}

	/* Concatenate `t2' to `t1' and return the result:
	t' = t1 * t2 */
	CCAffineTransform CCAffineTransformConcat(const CCAffineTransform& t1, const CCAffineTransform& t2)
	{
		return __CCAffineTransformMake(	t1.a * t2.a + t1.b * t2.c, t1.a * t2.b + t1.b * t2.d, //a,b
			t1.c * t2.a + t1.d * t2.c, t1.c * t2.b + t1.d * t2.d, //c,d
			t1.tx * t2.a + t1.ty * t2.c + t2.tx,				  //tx
			t1.tx * t2.b + t1.ty * t2.d + t2.ty);				  //ty
	}

	/* Return true if `t1' and `t2' are equal, false otherwise. */
	bool CCAffineTransformEqualToTransform(const CCAffineTransform& t1, const CCAffineTransform& t2)
	{
		return (t1.a == t2.a && t1.b == t2.b && t1.c == t2.c && t1.d == t2.d && t1.tx == t2.tx && t1.ty == t2.ty);
	}

	CCAffineTransform CCAffineTransformInvert(const CCAffineTransform& t)
	{
		float determinant = 1 / (t.a * t.d - t.b * t.c);

		return __CCAffineTransformMake(determinant * t.d, -determinant * t.b, -determinant * t.c, determinant * t.a,
			determinant * (t.c * t.ty - t.d * t.tx), determinant * (t.b * t.tx - t.a * t.ty));
	}

}
示例#7
0
bool CCScale9Sprite::updateWithImage(CAImage* image, CCRect rect, CCRect capInsets)
{
    GLubyte opacity = getOpacity();
    ccColor3B color = getColor();

    // Release old sprites
    this->removeAllSubviews();

    CC_SAFE_RELEASE(this->_centre);
    CC_SAFE_RELEASE(this->_top);
    CC_SAFE_RELEASE(this->_topLeft);
    CC_SAFE_RELEASE(this->_topRight);
    CC_SAFE_RELEASE(this->_left);
    CC_SAFE_RELEASE(this->_right);
    CC_SAFE_RELEASE(this->_bottomLeft);
    CC_SAFE_RELEASE(this->_bottom);
    CC_SAFE_RELEASE(this->_bottomRight);

    this->setImage(image);
    
    this->removeAllSubviews();
    m_capInsets = capInsets;
    
    // If there is no given rect
    if ( rect.equals(CCRectZero) )
    {
        // Get the Image size as original
        CCSize textureSize = image->getContentSize();
        
        rect = CCRectMake(0, 0, textureSize.width, textureSize.height);
    }
    
    this->setContentSize(rect.size);
    // Set the given rect's size as original size
    m_spriteRect = rect;
    m_originalSize = rect.size;
    m_preferredSize = m_originalSize;
    m_capInsetsInternal = capInsets;
    
    float w = rect.size.width;
    float h = rect.size.height;

    // If there is no specified center region
    if ( m_capInsetsInternal.equals(CCRectZero) )
    {
        // CCLog("... cap insets not specified : using default cap insets ...");
        m_capInsetsInternal = CCRectMake(w/3, h/3, w/3, h/3);
    }

    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 = 0.0;
    float y = 0.0;

    // top left
    CCRect leftbottombounds = CCRectMake(x, y,
                                      left_w, top_h);

    // top center
    TRANSLATE_X(x, y, left_w);
    CCRect centerbottombounds = CCRectMake(x, y,
                                        center_w, top_h);
        
    // top right
    TRANSLATE_X(x, y, center_w);
    CCRect rightbottombounds = CCRectMake(x, y,
                                       right_w, top_h);

    // ... center row
    x = 0.0;
    y = 0.0;
    TRANSLATE_Y(x, y, bottom_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 = 0.0;
    y = 0.0;
    TRANSLATE_Y(x, y, bottom_h);
    TRANSLATE_Y(x, y, center_h);

    // bottom left
    CCRect lefttopbounds = CCRectMake(x, y,
                                         left_w, bottom_h);

    // bottom center
    TRANSLATE_X(x, y, left_w);
    CCRect centertopbounds = CCRectMake(x, y,
                                           center_w, bottom_h);

    // bottom right
    TRANSLATE_X(x, y, center_w);
    CCRect righttopbounds = CCRectMake(x, y,
                                          right_w, bottom_h);

    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 CAImageView();
    _centre->initWithImage(m_pImage, centerbounds);
    _centre->setTag(pCentre);
    this->insertSubview(_centre, 0);
    
    // Top
    _top = new CAImageView();
    _top->initWithImage(m_pImage, centertopbounds);
    _top->setTag(pTop);
    this->insertSubview(_top, 1);
    
    // Bottom
    _bottom = new CAImageView();
    _bottom->initWithImage(m_pImage, centerbottombounds);
    _bottom->setTag(pBottom);
    this->insertSubview(_bottom, 1);
    
    // Left
    _left = new CAImageView();
    _left->initWithImage(m_pImage, leftcenterbounds);
    _left->setTag(pLeft);
    this->insertSubview(_left, 1);
    
    // Right
    _right = new CAImageView();
    _right->initWithImage(m_pImage, rightcenterbounds);
    _right->setTag(pRight);
    this->insertSubview(_right, 1);
    
    // Top left
    _topLeft = new CAImageView();
    _topLeft->initWithImage(m_pImage, lefttopbounds);
    _topLeft->setTag(pTopLeft);
    this->insertSubview(_topLeft, 2);
    
    // Top right
    _topRight = new CAImageView();
    _topRight->initWithImage(m_pImage, righttopbounds);
    _topLeft->setTag(pTopRight);
    this->insertSubview(_topRight, 2);
    
    // Bottom left
    _bottomLeft = new CAImageView();
    _bottomLeft->initWithImage(m_pImage, leftbottombounds);
    _bottom->setTag(pBottomLeft);
    this->insertSubview(_bottomLeft, 2);
    
    // Bottom right
    _bottomRight = new CAImageView();
    _bottomRight->initWithImage(m_pImage, rightbottombounds);
    _bottomRight->setTag(pBottomRight);
    this->insertSubview(_bottomRight, 2);
    this->setContentSize(rect.size);
    
    _bottomLeft->setAnchorPoint(CCPoint(0,0));
    _bottomRight->setAnchorPoint(CCPoint(0,0));
    _topLeft->setAnchorPoint(CCPoint(0,0));
    _topRight->setAnchorPoint(CCPoint(0,0));
    _left->setAnchorPoint(CCPoint(0,0));
    _right->setAnchorPoint(CCPoint(0,0));
    _top->setAnchorPoint(CCPoint(0,0));
    _bottom->setAnchorPoint(CCPoint(0,0));
    _centre->setAnchorPoint(CCPoint(0,0));
    
    if (m_bSpritesGenerated)
        {
            // Restore color and opacity
            this->setOpacity(opacity);
            this->setColor(color);
        }
    m_bSpritesGenerated = true;

    return true;
}