2012-04-16 18:58:43 +08:00
|
|
|
//
|
|
|
|
// CCScale9Sprite.cpp
|
|
|
|
// PlantCaring
|
|
|
|
//
|
|
|
|
// Created by Jung Sang-Taik on 12. 3. 16..
|
|
|
|
// Copyright (c) 2012 Neofect. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "CCScale9Sprite.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "sprite_nodes/CCSpriteBatchNode.h"
|
|
|
|
#include "sprite_nodes/CCSpriteFrame.h"
|
|
|
|
#include "sprite_nodes/CCSpriteFrameCache.h"
|
|
|
|
#include "sprite_nodes/CCSprite.h"
|
|
|
|
#include "support/CCPointExtension.h"
|
2012-04-17 11:12:05 +08:00
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_BEGIN
|
2012-04-16 18:58:43 +08:00
|
|
|
|
|
|
|
CCScale9Sprite::CCScale9Sprite()
|
2012-06-01 03:03:33 +08:00
|
|
|
: m_insetLeft(0)
|
|
|
|
, m_insetTop(0)
|
|
|
|
, m_insetRight(0)
|
|
|
|
, m_insetBottom(0)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-06-01 03:37:28 +08:00
|
|
|
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCScale9Sprite::~CCScale9Sprite()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-06-01 02:47:16 +08:00
|
|
|
bool CCScale9Sprite::init()
|
|
|
|
{
|
|
|
|
return this->initWithBatchNode(NULL, CCRectZero, CCRectZero);
|
|
|
|
}
|
|
|
|
|
2012-04-16 18:58:43 +08:00
|
|
|
bool CCScale9Sprite::initWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, CCRect capInsets)
|
|
|
|
{
|
2012-06-01 02:47:16 +08:00
|
|
|
if(batchnode)
|
|
|
|
{
|
|
|
|
this->updateWithBatchNode(batchnode, rect, capInsets);
|
2012-06-01 03:37:28 +08:00
|
|
|
this->setAnchorPoint(ccp(0.5f, 0.5f));
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
this->m_positionsAreDirty = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-15 15:36:36 +08:00
|
|
|
return true;
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, CCRect capInsets)
|
|
|
|
{
|
|
|
|
// Release old sprites
|
|
|
|
this->removeAllChildrenWithCleanup(true);
|
|
|
|
|
2012-06-01 03:03:33 +08:00
|
|
|
// TODO Is this needed?
|
|
|
|
/*
|
|
|
|
if(this->topLeft != NULL)
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(this->topLeft);
|
|
|
|
CC_SAFE_RELEASE(this->top);
|
|
|
|
CC_SAFE_RELEASE(this->topRight);
|
|
|
|
CC_SAFE_RELEASE(this->left);
|
|
|
|
CC_SAFE_RELEASE(this->centre);
|
|
|
|
CC_SAFE_RELEASE(this->right);
|
|
|
|
CC_SAFE_RELEASE(this->bottomLeft);
|
|
|
|
CC_SAFE_RELEASE(this->bottom);
|
|
|
|
CC_SAFE_RELEASE(this->bottomRight);
|
|
|
|
}
|
2012-06-01 03:03:33 +08:00
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-01 02:47:16 +08:00
|
|
|
if(this->scale9Image != batchnode)
|
|
|
|
{
|
2012-06-01 03:03:33 +08:00
|
|
|
// TODO Is this needed?
|
|
|
|
/*
|
|
|
|
if(this->scale9Image != NULL)
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(this->scale9Image);
|
|
|
|
}
|
|
|
|
*/
|
2012-06-01 02:47:16 +08:00
|
|
|
scale9Image = batchnode; // TODO No retain on purpose?
|
|
|
|
}
|
|
|
|
|
|
|
|
scale9Image->removeAllChildrenWithCleanup(true);
|
|
|
|
|
|
|
|
m_capInsets = capInsets;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// If there is no given rect
|
|
|
|
if ( CCRect::CCRectEqualToRect(rect, 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;
|
2012-06-01 02:47:16 +08:00
|
|
|
m_capInsetsInternal = capInsets;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// If there is no specified center region
|
2012-06-01 02:47:16 +08:00
|
|
|
if ( CCRect::CCRectEqualToRect(m_capInsetsInternal, CCRectZero) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Apply the 3x3 grid format
|
2012-06-01 02:47:16 +08:00
|
|
|
m_capInsetsInternal = CCRectMake(
|
2012-04-19 14:35:52 +08:00
|
|
|
rect.origin.x + m_originalSize.width / 3,
|
|
|
|
rect.origin.y + m_originalSize.height / 3,
|
|
|
|
m_originalSize.width / 3,
|
|
|
|
m_originalSize.height / 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the image edges
|
|
|
|
float l = rect.origin.x;
|
|
|
|
float t = rect.origin.y;
|
|
|
|
float h = rect.size.height;
|
|
|
|
float w = rect.size.width;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set up the image
|
|
|
|
//
|
|
|
|
|
|
|
|
// Centre
|
2012-07-23 22:49:11 +08:00
|
|
|
centre = CCSprite::createWithTexture(scale9Image->getTexture(), m_capInsetsInternal);
|
2012-06-01 03:03:33 +08:00
|
|
|
scale9Image->addChild(centre, 0, pCentre);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Top
|
2012-07-23 22:49:11 +08:00
|
|
|
top = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake(m_capInsetsInternal.origin.x,
|
2012-04-19 14:35:52 +08:00
|
|
|
t,
|
2012-06-01 02:47:16 +08:00
|
|
|
m_capInsetsInternal.size.width,
|
|
|
|
m_capInsetsInternal.origin.y - t));
|
2012-04-19 14:35:52 +08:00
|
|
|
scale9Image->addChild(top, 1, pTop);
|
|
|
|
|
|
|
|
// Bottom
|
2012-07-23 22:49:11 +08:00
|
|
|
bottom = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake( m_capInsetsInternal.origin.x,
|
2012-06-01 02:47:16 +08:00
|
|
|
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.height,
|
|
|
|
m_capInsetsInternal.size.width,
|
|
|
|
h - (m_capInsetsInternal.origin.y - t + m_capInsetsInternal.size.height) ));
|
2012-04-19 14:35:52 +08:00
|
|
|
scale9Image->addChild(bottom, 1, pBottom);
|
|
|
|
|
|
|
|
// Left
|
2012-07-23 22:49:11 +08:00
|
|
|
left = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake(
|
2012-04-19 14:35:52 +08:00
|
|
|
l,
|
2012-06-01 02:47:16 +08:00
|
|
|
m_capInsetsInternal.origin.y,
|
|
|
|
m_capInsetsInternal.origin.x - l,
|
|
|
|
m_capInsetsInternal.size.height) );
|
2012-04-19 14:35:52 +08:00
|
|
|
scale9Image->addChild(left, 1, pLeft);
|
|
|
|
|
|
|
|
// Right
|
2012-07-23 22:49:11 +08:00
|
|
|
right = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake(
|
2012-06-01 02:47:16 +08:00
|
|
|
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));
|
2012-04-19 14:35:52 +08:00
|
|
|
scale9Image->addChild(right, 1, pRight);
|
|
|
|
|
|
|
|
// Top left
|
2012-07-23 22:49:11 +08:00
|
|
|
topLeft = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake(
|
2012-04-19 14:35:52 +08:00
|
|
|
l,
|
|
|
|
t,
|
2012-06-01 02:47:16 +08:00
|
|
|
m_capInsetsInternal.origin.x - l,
|
|
|
|
m_capInsetsInternal.origin.y - t));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
scale9Image->addChild(topLeft, 2, pTopLeft);
|
|
|
|
|
|
|
|
// Top right
|
2012-07-23 22:49:11 +08:00
|
|
|
topRight = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake(
|
2012-06-01 02:47:16 +08:00
|
|
|
m_capInsetsInternal.origin.x + m_capInsetsInternal.size.width,
|
2012-04-19 14:35:52 +08:00
|
|
|
t,
|
2012-06-01 02:47:16 +08:00
|
|
|
w - (m_capInsetsInternal.origin.x - l + m_capInsetsInternal.size.width),
|
|
|
|
m_capInsetsInternal.origin.y - t));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
scale9Image->addChild(topRight, 2, pTopRight);
|
|
|
|
|
|
|
|
// Bottom left
|
2012-07-23 22:49:11 +08:00
|
|
|
bottomLeft = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake(
|
2012-04-19 14:35:52 +08:00
|
|
|
l,
|
2012-06-01 02:47:16 +08:00
|
|
|
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.height,
|
|
|
|
m_capInsetsInternal.origin.x - l,
|
|
|
|
h - (m_capInsetsInternal.origin.y - t + m_capInsetsInternal.size.height)) );
|
2012-04-19 14:35:52 +08:00
|
|
|
scale9Image->addChild(bottomLeft, 2, pBottomLeft);
|
|
|
|
|
|
|
|
// Bottom right
|
2012-07-23 22:49:11 +08:00
|
|
|
bottomRight = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake(
|
2012-06-01 02:47:16 +08:00
|
|
|
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)) );
|
2012-04-19 14:35:52 +08:00
|
|
|
scale9Image->addChild(bottomRight, 2, pBottomRight);
|
|
|
|
|
|
|
|
|
|
|
|
this->setContentSize(rect.size);
|
|
|
|
this->addChild(scale9Image);
|
|
|
|
|
|
|
|
return true;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCScale9Sprite::setContentSize(const CCSize &size)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCNode::setContentSize(size);
|
2012-06-01 02:47:16 +08:00
|
|
|
this->m_positionsAreDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCScale9Sprite::updatePositions()
|
|
|
|
{
|
|
|
|
CCSize size = this->m_tContentSize;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
float sizableWidth = size.width - topLeft->getContentSize().width - topRight->getContentSize().width;
|
|
|
|
float sizableHeight = size.height - topLeft->getContentSize().height - bottomRight->getContentSize().height;
|
|
|
|
float horizontalScale = sizableWidth/centre->getContentSize().width;
|
|
|
|
float verticalScale = sizableHeight/centre->getContentSize().height;
|
|
|
|
centre->setScaleX(horizontalScale);
|
|
|
|
centre->setScaleY(verticalScale);
|
|
|
|
float rescaledWidth = centre->getContentSize().width * horizontalScale;
|
|
|
|
float rescaledHeight = centre->getContentSize().height * verticalScale;
|
2012-06-01 02:47:16 +08:00
|
|
|
|
|
|
|
float leftWidth = bottomLeft->getContentSize().width;
|
|
|
|
float bottomHeight = bottomLeft->getContentSize().height;
|
|
|
|
|
|
|
|
bottomLeft->setAnchorPoint(ccp(0,0));
|
|
|
|
bottomRight->setAnchorPoint(ccp(0,0));
|
|
|
|
topLeft->setAnchorPoint(ccp(0,0));
|
|
|
|
topRight->setAnchorPoint(ccp(0,0));
|
|
|
|
left->setAnchorPoint(ccp(0,0));
|
|
|
|
right->setAnchorPoint(ccp(0,0));
|
|
|
|
top->setAnchorPoint(ccp(0,0));
|
|
|
|
bottom->setAnchorPoint(ccp(0,0));
|
|
|
|
centre->setAnchorPoint(ccp(0,0));
|
|
|
|
|
|
|
|
// Position corners
|
|
|
|
bottomLeft->setPosition(ccp(0,0));
|
|
|
|
bottomRight->setPosition(ccp(leftWidth+rescaledWidth,0));
|
|
|
|
topLeft->setPosition(ccp(0, bottomHeight+rescaledHeight));
|
|
|
|
topRight->setPosition(ccp(leftWidth+rescaledWidth, bottomHeight+rescaledHeight));
|
|
|
|
|
|
|
|
// Scale and position borders
|
|
|
|
left->setPosition(ccp(0, bottomHeight));
|
|
|
|
left->setScaleY(verticalScale);
|
|
|
|
right->setPosition(ccp(leftWidth+rescaledWidth,bottomHeight));
|
|
|
|
right->setScaleY(verticalScale);
|
|
|
|
bottom->setPosition(ccp(leftWidth,0));
|
|
|
|
bottom->setScaleX(horizontalScale);
|
|
|
|
top->setPosition(ccp(leftWidth,bottomHeight+rescaledHeight));
|
2012-04-19 14:35:52 +08:00
|
|
|
top->setScaleX(horizontalScale);
|
2012-06-01 02:47:16 +08:00
|
|
|
|
|
|
|
// Position centre
|
|
|
|
centre->setPosition(ccp(leftWidth, bottomHeight));
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCScale9Sprite::initWithFile(const char* file, CCRect rect, CCRect capInsets)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(file != NULL, "Invalid file for sprite");
|
2012-04-16 18:58:43 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCSpriteBatchNode *batchnode = CCSpriteBatchNode::create(file, 9);
|
2012-04-16 18:58:43 +08:00
|
|
|
bool pReturn = this->initWithBatchNode(batchnode, rect, capInsets);
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCScale9Sprite* CCScale9Sprite::spriteWithFile(const char* file, CCRect rect, CCRect capInsets)
|
|
|
|
{
|
|
|
|
return CCScale9Sprite::create(file, rect, capInsets);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCScale9Sprite* CCScale9Sprite::create(const char* file, CCRect rect, CCRect capInsets)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScale9Sprite* pReturn = new CCScale9Sprite();
|
|
|
|
if ( pReturn && pReturn->initWithFile(file, rect, capInsets) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCScale9Sprite::initWithFile(const char* file, CCRect rect)
|
|
|
|
{
|
|
|
|
bool pReturn = this->initWithFile(file, rect, CCRectZero);
|
|
|
|
return pReturn;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCScale9Sprite* CCScale9Sprite::spriteWithFile(const char* file, CCRect rect)
|
|
|
|
{
|
|
|
|
return CCScale9Sprite::create(file, rect);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCScale9Sprite* CCScale9Sprite::create(const char* file, CCRect rect)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScale9Sprite* pReturn = new CCScale9Sprite();
|
|
|
|
if ( pReturn && pReturn->initWithFile(file, rect) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CCScale9Sprite::initWithFile(CCRect capInsets, const char* file)
|
|
|
|
{
|
|
|
|
bool pReturn = this->initWithFile(file, CCRectZero, capInsets);
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCScale9Sprite* CCScale9Sprite::spriteWithFile(CCRect capInsets, const char* file)
|
|
|
|
{
|
|
|
|
return CCScale9Sprite::create(capInsets, file);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCScale9Sprite* CCScale9Sprite::create(CCRect capInsets, const char* file)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScale9Sprite* pReturn = new CCScale9Sprite();
|
|
|
|
if ( pReturn && pReturn->initWithFile(file, capInsets) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCScale9Sprite::initWithFile(const char* file)
|
|
|
|
{
|
|
|
|
bool pReturn = this->initWithFile(file, CCRectZero);
|
|
|
|
return pReturn;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCScale9Sprite* CCScale9Sprite::spriteWithFile(const char* file)
|
|
|
|
{
|
|
|
|
return CCScale9Sprite::create(file);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCScale9Sprite* CCScale9Sprite::create(const char* file)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScale9Sprite* pReturn = new CCScale9Sprite();
|
|
|
|
if ( pReturn && pReturn->initWithFile(file) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCScale9Sprite::initWithSpriteFrame(CCSpriteFrame* spriteFrame, CCRect capInsets)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(spriteFrame != NULL, "Sprite frame must be not nil");
|
2012-06-01 02:47:16 +08:00
|
|
|
|
2012-07-23 22:49:11 +08:00
|
|
|
CCSpriteBatchNode *batchnode = CCSpriteBatchNode::createWithTexture(spriteFrame->getTexture(), 9);
|
2012-04-16 18:58:43 +08:00
|
|
|
bool pReturn = this->initWithBatchNode(batchnode, spriteFrame->getRect(), capInsets);
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCScale9Sprite* CCScale9Sprite::spriteWithSpriteFrame(CCSpriteFrame* spriteFrame, CCRect capInsets)
|
|
|
|
{
|
|
|
|
return CCScale9Sprite::createWithSpriteFrame(spriteFrame, capInsets);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCScale9Sprite* CCScale9Sprite::createWithSpriteFrame(CCSpriteFrame* spriteFrame, CCRect capInsets)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScale9Sprite* pReturn = new CCScale9Sprite();
|
|
|
|
if ( pReturn && pReturn->initWithSpriteFrame(spriteFrame, capInsets) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
bool CCScale9Sprite::initWithSpriteFrame(CCSpriteFrame* spriteFrame)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool pReturn = this->initWithSpriteFrame(spriteFrame, CCRectZero);
|
2012-04-16 18:58:43 +08:00
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCScale9Sprite* CCScale9Sprite::spriteWithSpriteFrame(CCSpriteFrame* spriteFrame)
|
|
|
|
{
|
|
|
|
return CCScale9Sprite::createWithSpriteFrame(spriteFrame);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCScale9Sprite* CCScale9Sprite::createWithSpriteFrame(CCSpriteFrame* spriteFrame)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScale9Sprite* pReturn = new CCScale9Sprite();
|
|
|
|
if ( pReturn && pReturn->initWithSpriteFrame(spriteFrame) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCScale9Sprite::initWithSpriteFrameName(const char* spriteFrameName, CCRect capInsets)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(spriteFrameName != NULL, "Invalid spriteFrameName for sprite");
|
2012-06-01 02:47:16 +08:00
|
|
|
|
2012-04-16 18:58:43 +08:00
|
|
|
CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(spriteFrameName);
|
|
|
|
bool pReturn = this->initWithSpriteFrame(frame, capInsets);
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCScale9Sprite* CCScale9Sprite::spriteWithSpriteFrameName(const char* spriteFrameName, CCRect capInsets)
|
|
|
|
{
|
|
|
|
return CCScale9Sprite::createWithSpriteFrameName(spriteFrameName, capInsets);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCScale9Sprite* CCScale9Sprite::createWithSpriteFrameName(const char* spriteFrameName, CCRect capInsets)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScale9Sprite* pReturn = new CCScale9Sprite();
|
|
|
|
if ( pReturn && pReturn->initWithSpriteFrameName(spriteFrameName, capInsets) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCScale9Sprite::initWithSpriteFrameName(const char* spriteFrameName)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool pReturn = this->initWithSpriteFrameName(spriteFrameName, CCRectZero);
|
2012-04-16 18:58:43 +08:00
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCScale9Sprite* CCScale9Sprite::spriteWithSpriteFrameName(const char* spriteFrameName)
|
|
|
|
{
|
|
|
|
return CCScale9Sprite::createWithSpriteFrameName(spriteFrameName);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCScale9Sprite* CCScale9Sprite::createWithSpriteFrameName(const char* spriteFrameName)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScale9Sprite* pReturn = new CCScale9Sprite();
|
|
|
|
if ( pReturn && pReturn->initWithSpriteFrameName(spriteFrameName) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
|
|
|
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCScale9Sprite* CCScale9Sprite::resizableSpriteWithCapInsets(CCRect capInsets)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScale9Sprite* pReturn = new CCScale9Sprite();
|
|
|
|
if ( pReturn && pReturn->initWithBatchNode(scale9Image, m_spriteRect, capInsets) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCScale9Sprite* CCScale9Sprite::node()
|
|
|
|
{
|
|
|
|
return CCScale9Sprite::create();
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCScale9Sprite* CCScale9Sprite::create()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-14 05:19:13 +08:00
|
|
|
CCScale9Sprite *pReturn = new CCScale9Sprite();
|
|
|
|
if (pReturn)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-14 05:19:13 +08:00
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
2012-06-14 05:19:13 +08:00
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//LabelBMFont - CCRGBAProtocol protocol
|
|
|
|
void CCScale9Sprite::setColor(const ccColor3B& color3)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tColor = color3;
|
|
|
|
if (scale9Image->getChildren() && scale9Image->getChildren()->count() != 0)
|
|
|
|
{
|
|
|
|
CCObject* child;
|
|
|
|
CCARRAY_FOREACH(scale9Image->getChildren(), child)
|
|
|
|
{
|
|
|
|
CCRGBAProtocol* pNode = dynamic_cast<CCRGBAProtocol*>(child);
|
|
|
|
if (pNode)
|
|
|
|
{
|
|
|
|
pNode->setColor(m_tColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
const ccColor3B& CCScale9Sprite::getColor(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_tColor;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCScale9Sprite::setOpacity(GLubyte var)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_cOpacity = var;
|
2012-06-01 02:47:16 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (scale9Image->getChildren() && scale9Image->getChildren()->count() != 0)
|
|
|
|
{
|
|
|
|
CCObject* child;
|
|
|
|
CCARRAY_FOREACH(scale9Image->getChildren(), child)
|
|
|
|
{
|
|
|
|
CCRGBAProtocol* pNode = dynamic_cast<CCRGBAProtocol*>(child);
|
|
|
|
if (pNode)
|
|
|
|
{
|
|
|
|
pNode->setOpacity(m_cOpacity);
|
|
|
|
}
|
|
|
|
//CCNode* pNode = (CCNode*) child;
|
|
|
|
//if (pNode)
|
|
|
|
//{
|
|
|
|
// CCRGBAProtocol *pRGBAProtocol = (CCRGBAProtocol *)pNode;
|
|
|
|
// if (pRGBAProtocol)
|
|
|
|
// {
|
|
|
|
// pRGBAProtocol->setOpacity(m_cOpacity);
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** sets the opacity.
|
|
|
|
@warning If the the texture has premultiplied alpha then, the R, G and B channels will be modifed.
|
|
|
|
Values goes from 0 to 255, where 255 means fully opaque.
|
|
|
|
*/
|
|
|
|
GLubyte CCScale9Sprite::getOpacity()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_cOpacity;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2012-06-01 02:47:16 +08:00
|
|
|
void CCScale9Sprite::setPreferredSize(CCSize preferedSize)
|
|
|
|
{
|
|
|
|
this->setContentSize(preferedSize);
|
|
|
|
this->m_preferredSize = preferedSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCSize CCScale9Sprite::getPreferredSize()
|
|
|
|
{
|
|
|
|
return this->m_preferredSize;
|
|
|
|
}
|
2012-04-16 18:58:43 +08:00
|
|
|
|
|
|
|
void CCScale9Sprite::setCapInsets(CCRect capInsets)
|
|
|
|
{
|
2012-06-01 02:47:16 +08:00
|
|
|
CCSize contentSize = this->m_tContentSize;
|
|
|
|
this->updateWithBatchNode(this->scale9Image, this->m_spriteRect, capInsets);
|
|
|
|
this->setContentSize(contentSize);
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCRect CCScale9Sprite::getCapInsets()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_capInsets;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2012-06-01 02:47:16 +08:00
|
|
|
void CCScale9Sprite::updateCapInset()
|
|
|
|
{
|
|
|
|
CCRect insets;
|
|
|
|
if (this->m_insetLeft == 0 && this->m_insetTop == 0 && this->m_insetRight == 0 && this->m_insetBottom == 0)
|
|
|
|
{
|
|
|
|
insets = CCRectZero;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
insets = CCRectMake(this->m_insetLeft,
|
|
|
|
this->m_insetTop,
|
|
|
|
this->m_spriteRect.size.width-this->m_insetLeft-this->m_insetRight,
|
|
|
|
this->m_spriteRect.size.height-this->m_insetTop-this->m_insetBottom);
|
|
|
|
}
|
|
|
|
this->setCapInsets(insets);
|
|
|
|
}
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
void CCScale9Sprite::setOpacityModifyRGB(bool var)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bIsOpacityModifyRGB = var;
|
|
|
|
if (scale9Image->getChildren() && scale9Image->getChildren()->count() != 0)
|
|
|
|
{
|
|
|
|
CCObject* child;
|
|
|
|
CCARRAY_FOREACH(scale9Image->getChildren(), child)
|
|
|
|
{
|
|
|
|
CCRGBAProtocol* pNode = dynamic_cast<CCRGBAProtocol*>(child);
|
|
|
|
if (pNode)
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
pNode->setOpacityModifyRGB(m_bIsOpacityModifyRGB);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
//CCNode* pNode = (CCNode*) child;
|
|
|
|
//if (pNode)
|
|
|
|
//{
|
|
|
|
// CCRGBAProtocol *pRGBAProtocol = (CCRGBAProtocol *)pNode;
|
|
|
|
// if (pRGBAProtocol)
|
|
|
|
// {
|
2012-06-15 16:47:30 +08:00
|
|
|
// pRGBAProtocol->setOpacityModifyRGB(m_bIsOpacityModifyRGB);
|
2012-04-19 14:35:52 +08:00
|
|
|
// }
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
2012-06-15 15:10:40 +08:00
|
|
|
bool CCScale9Sprite::isOpacityModifyRGB()
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_bIsOpacityModifyRGB;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2012-06-01 02:47:16 +08:00
|
|
|
void CCScale9Sprite::setSpriteFrame(CCSpriteFrame * spriteFrame)
|
|
|
|
{
|
2012-07-23 22:49:11 +08:00
|
|
|
CCSpriteBatchNode * batchnode = CCSpriteBatchNode::createWithTexture(spriteFrame->getTexture(), 9);
|
2012-06-01 02:47:16 +08:00
|
|
|
this->updateWithBatchNode(batchnode, spriteFrame->getRect(), CCRectZero);
|
|
|
|
|
|
|
|
// Reset insets
|
|
|
|
this->m_insetLeft = 0;
|
|
|
|
this->m_insetTop = 0;
|
|
|
|
this->m_insetRight = 0;
|
|
|
|
this->m_insetBottom = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCScale9Sprite::getInsetLeft()
|
|
|
|
{
|
|
|
|
return this->m_insetLeft;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCScale9Sprite::getInsetTop()
|
|
|
|
{
|
|
|
|
return this->m_insetTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCScale9Sprite::getInsetRight()
|
|
|
|
{
|
|
|
|
return this->m_insetRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCScale9Sprite::getInsetBottom()
|
|
|
|
{
|
|
|
|
return this->m_insetBottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCScale9Sprite::setInsetLeft(float insetLeft)
|
|
|
|
{
|
|
|
|
this->m_insetLeft = insetLeft;
|
|
|
|
this->updateCapInset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCScale9Sprite::setInsetTop(float insetTop)
|
|
|
|
{
|
|
|
|
this->m_insetTop = insetTop;
|
|
|
|
this->updateCapInset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCScale9Sprite::setInsetRight(float insetRight)
|
|
|
|
{
|
|
|
|
this->m_insetRight = insetRight;
|
|
|
|
this->updateCapInset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCScale9Sprite::setInsetBottom(float insetBottom)
|
|
|
|
{
|
|
|
|
this->m_insetBottom = insetBottom;
|
|
|
|
this->updateCapInset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCScale9Sprite::visit()
|
|
|
|
{
|
|
|
|
if(this->m_positionsAreDirty)
|
|
|
|
{
|
|
|
|
this->updatePositions();
|
|
|
|
this->m_positionsAreDirty = false;
|
|
|
|
}
|
|
|
|
CCNode::visit();
|
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_END
|