2012-09-25 17:26:09 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Created by Jung Sang-Taik on 12. 3. 16..
|
|
|
|
Copyright (c) 2012 Neofect. All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2012-04-16 18:58:43 +08:00
|
|
|
|
|
|
|
#include "CCScale9Sprite.h"
|
2014-01-17 13:35:58 +08:00
|
|
|
#include "CCPlatformMacros.h"
|
|
|
|
#include "CCSprite.h"
|
|
|
|
#include "CCSpriteFrameCache.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
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
enum positions
|
|
|
|
{
|
|
|
|
pCentre = 0,
|
|
|
|
pTop,
|
|
|
|
pLeft,
|
|
|
|
pRight,
|
|
|
|
pBottom,
|
|
|
|
pTopRight,
|
|
|
|
pTopLeft,
|
|
|
|
pBottomRight,
|
|
|
|
pBottomLeft
|
|
|
|
};
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite::Scale9Sprite()
|
2013-07-29 14:07:57 +08:00
|
|
|
: _spritesGenerated(false)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _spriteFrameRotated(false)
|
|
|
|
, _positionsAreDirty(false)
|
2013-04-15 16:37:35 +08:00
|
|
|
, _scale9Image(NULL)
|
|
|
|
, _topLeft(NULL)
|
|
|
|
, _top(NULL)
|
|
|
|
, _topRight(NULL)
|
|
|
|
, _left(NULL)
|
|
|
|
, _centre(NULL)
|
|
|
|
, _right(NULL)
|
|
|
|
, _bottomLeft(NULL)
|
|
|
|
, _bottom(NULL)
|
|
|
|
, _bottomRight(NULL)
|
|
|
|
, _opacityModifyRGB(false)
|
2013-07-29 14:07:57 +08:00
|
|
|
, _insetLeft(0)
|
|
|
|
, _insetTop(0)
|
|
|
|
, _insetRight(0)
|
|
|
|
, _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
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite::~Scale9Sprite()
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-04-15 16:37:35 +08:00
|
|
|
CC_SAFE_RELEASE(_topLeft);
|
|
|
|
CC_SAFE_RELEASE(_top);
|
|
|
|
CC_SAFE_RELEASE(_topRight);
|
|
|
|
CC_SAFE_RELEASE(_left);
|
|
|
|
CC_SAFE_RELEASE(_centre);
|
|
|
|
CC_SAFE_RELEASE(_right);
|
|
|
|
CC_SAFE_RELEASE(_bottomLeft);
|
|
|
|
CC_SAFE_RELEASE(_bottom);
|
|
|
|
CC_SAFE_RELEASE(_bottomRight);
|
|
|
|
CC_SAFE_RELEASE(_scale9Image);
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool Scale9Sprite::init()
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
return this->initWithBatchNode(NULL, Rect::ZERO, Rect::ZERO);
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
|
2013-10-08 11:53:38 +08:00
|
|
|
bool Scale9Sprite::initWithBatchNode(SpriteBatchNode* batchnode, const Rect& rect, const Rect& capInsets)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
|
|
|
return this->initWithBatchNode(batchnode, rect, false, capInsets);
|
|
|
|
}
|
|
|
|
|
2013-10-08 11:53:38 +08:00
|
|
|
bool Scale9Sprite::initWithBatchNode(SpriteBatchNode* batchnode, const Rect& rect, bool rotated, const Rect& capInsets)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-06-01 02:47:16 +08:00
|
|
|
if(batchnode)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
this->updateWithBatchNode(batchnode, rect, rotated, capInsets);
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
2013-07-11 13:59:57 +08:00
|
|
|
|
2013-07-12 15:07:44 +08:00
|
|
|
this->setAnchorPoint(Point(0.5f, 0.5f));
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_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
|
|
|
}
|
|
|
|
|
2013-01-24 08:42:14 +08:00
|
|
|
#define TRANSLATE_X(x, y, xtranslate) \
|
|
|
|
x+=xtranslate; \
|
|
|
|
|
|
|
|
#define TRANSLATE_Y(x, y, ytranslate) \
|
|
|
|
y+=ytranslate; \
|
|
|
|
|
2013-10-08 11:53:38 +08:00
|
|
|
bool Scale9Sprite::updateWithBatchNode(SpriteBatchNode* batchnode, const Rect& originalRect, bool rotated, const Rect& capInsets)
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-02-28 11:55:36 +08:00
|
|
|
GLubyte opacity = getOpacity();
|
2013-07-05 16:49:22 +08:00
|
|
|
Color3B color = getColor();
|
2013-10-08 11:53:38 +08:00
|
|
|
Rect rect(originalRect);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2012-06-01 02:47:16 +08:00
|
|
|
// Release old sprites
|
|
|
|
this->removeAllChildrenWithCleanup(true);
|
|
|
|
|
2013-04-15 16:37:35 +08:00
|
|
|
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);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-04-15 16:37:35 +08:00
|
|
|
if(this->_scale9Image != batchnode)
|
2013-03-09 16:23:24 +08:00
|
|
|
{
|
2013-04-15 16:37:35 +08:00
|
|
|
CC_SAFE_RELEASE(this->_scale9Image);
|
|
|
|
_scale9Image = batchnode;
|
|
|
|
CC_SAFE_RETAIN(_scale9Image);
|
2013-03-09 16:23:24 +08:00
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
if (!_scale9Image)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-01 02:47:16 +08:00
|
|
|
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->removeAllChildrenWithCleanup(true);
|
2012-06-01 02:47:16 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_capInsets = capInsets;
|
2013-07-04 00:03:44 +08:00
|
|
|
_spriteFrameRotated = rotated;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// If there is no given rect
|
2013-07-12 14:47:36 +08:00
|
|
|
if ( rect.equals(Rect::ZERO) )
|
2013-03-09 16:23:24 +08:00
|
|
|
{
|
|
|
|
// Get the texture size as original
|
2013-06-20 14:15:53 +08:00
|
|
|
Size textureSize = _scale9Image->getTextureAtlas()->getTexture()->getContentSize();
|
2013-03-09 16:23:24 +08:00
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
rect = Rect(0, 0, textureSize.width, textureSize.height);
|
2013-03-09 16:23:24 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Set the given rect's size as original size
|
2013-06-15 14:03:30 +08:00
|
|
|
_spriteRect = rect;
|
|
|
|
_originalSize = rect.size;
|
|
|
|
_preferredSize = _originalSize;
|
|
|
|
_capInsetsInternal = capInsets;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
float w = rect.size.width;
|
2013-02-12 05:26:40 +08:00
|
|
|
float h = rect.size.height;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// If there is no specified center region
|
2013-07-12 14:47:36 +08:00
|
|
|
if ( _capInsetsInternal.equals(Rect::ZERO) )
|
2013-03-09 16:23:24 +08:00
|
|
|
{
|
2013-07-24 06:20:22 +08:00
|
|
|
// log("... cap insets not specified : using default cap insets ...");
|
2013-07-12 14:30:26 +08:00
|
|
|
_capInsetsInternal = Rect(w/3, h/3, w/3, h/3);
|
2013-03-09 16:23:24 +08:00
|
|
|
}
|
2013-01-24 08:42:14 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float left_w = _capInsetsInternal.origin.x;
|
|
|
|
float center_w = _capInsetsInternal.size.width;
|
2013-02-12 05:26:40 +08:00
|
|
|
float right_w = rect.size.width - (left_w + center_w);
|
2013-01-24 08:42:14 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float top_h = _capInsetsInternal.origin.y;
|
|
|
|
float center_h = _capInsetsInternal.size.height;
|
2013-02-12 05:26:40 +08:00
|
|
|
float bottom_h = rect.size.height - (top_h + center_h);
|
2013-01-24 08:42:14 +08:00
|
|
|
|
2013-02-12 05:26:40 +08:00
|
|
|
// calculate rects
|
2013-01-24 08:42:14 +08:00
|
|
|
|
2013-02-12 05:26:40 +08:00
|
|
|
// ... top row
|
|
|
|
float x = 0.0;
|
|
|
|
float y = 0.0;
|
2013-01-24 08:42:14 +08:00
|
|
|
|
2013-02-12 05:26:40 +08:00
|
|
|
// top left
|
2013-07-12 15:07:44 +08:00
|
|
|
Rect lefttopbounds = Rect(x, y, left_w, top_h);
|
2013-01-24 08:42:14 +08:00
|
|
|
|
2013-02-12 05:26:40 +08:00
|
|
|
// top center
|
|
|
|
TRANSLATE_X(x, y, left_w);
|
2013-07-12 15:07:44 +08:00
|
|
|
Rect centertopbounds = Rect(x, y, center_w, top_h);
|
2013-01-24 08:42:14 +08:00
|
|
|
|
2013-02-12 05:26:40 +08:00
|
|
|
// top right
|
|
|
|
TRANSLATE_X(x, y, center_w);
|
2013-07-12 15:07:44 +08:00
|
|
|
Rect righttopbounds = Rect(x, y, right_w, top_h);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// ... center row
|
|
|
|
x = 0.0;
|
|
|
|
y = 0.0;
|
|
|
|
TRANSLATE_Y(x, y, top_h);
|
|
|
|
|
|
|
|
// center left
|
2013-07-12 15:07:44 +08:00
|
|
|
Rect leftcenterbounds = Rect(x, y, left_w, center_h);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// center center
|
|
|
|
TRANSLATE_X(x, y, left_w);
|
2013-07-12 15:07:44 +08:00
|
|
|
Rect centerbounds = Rect(x, y, center_w, center_h);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// center right
|
|
|
|
TRANSLATE_X(x, y, center_w);
|
2013-07-12 15:07:44 +08:00
|
|
|
Rect rightcenterbounds = Rect(x, y, right_w, center_h);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// ... bottom row
|
|
|
|
x = 0.0;
|
|
|
|
y = 0.0;
|
|
|
|
TRANSLATE_Y(x, y, top_h);
|
|
|
|
TRANSLATE_Y(x, y, center_h);
|
|
|
|
|
|
|
|
// bottom left
|
2013-07-12 15:07:44 +08:00
|
|
|
Rect leftbottombounds = Rect(x, y, left_w, bottom_h);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// bottom center
|
|
|
|
TRANSLATE_X(x, y, left_w);
|
2013-07-12 15:07:44 +08:00
|
|
|
Rect centerbottombounds = Rect(x, y, center_w, bottom_h);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// bottom right
|
|
|
|
TRANSLATE_X(x, y, center_w);
|
2013-07-12 15:07:44 +08:00
|
|
|
Rect rightbottombounds = Rect(x, y, right_w, bottom_h);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
if (!rotated) {
|
2013-07-24 06:20:22 +08:00
|
|
|
// log("!rotated");
|
2013-02-12 05:26:40 +08:00
|
|
|
|
2013-08-27 09:08:21 +08:00
|
|
|
AffineTransform t = AffineTransform::IDENTITY;
|
2013-06-20 14:15:53 +08:00
|
|
|
t = AffineTransformTranslate(t, rect.origin.x, rect.origin.y);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
centerbounds = RectApplyAffineTransform(centerbounds, t);
|
|
|
|
rightbottombounds = RectApplyAffineTransform(rightbottombounds, t);
|
|
|
|
leftbottombounds = RectApplyAffineTransform(leftbottombounds, t);
|
|
|
|
righttopbounds = RectApplyAffineTransform(righttopbounds, t);
|
|
|
|
lefttopbounds = RectApplyAffineTransform(lefttopbounds, t);
|
|
|
|
rightcenterbounds = RectApplyAffineTransform(rightcenterbounds, t);
|
|
|
|
leftcenterbounds = RectApplyAffineTransform(leftcenterbounds, t);
|
|
|
|
centerbottombounds = RectApplyAffineTransform(centerbottombounds, t);
|
|
|
|
centertopbounds = RectApplyAffineTransform(centertopbounds, t);
|
2013-01-24 08:42:14 +08:00
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
// Centre
|
2013-11-14 07:55:36 +08:00
|
|
|
_centre = Sprite::createWithTexture(_scale9Image->getTexture(), centerbounds);
|
|
|
|
_centre->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_centre, 0, pCentre);
|
2013-11-14 07:55:36 +08:00
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Top
|
2013-11-14 07:55:36 +08:00
|
|
|
_top = Sprite::createWithTexture(_scale9Image->getTexture(), centertopbounds);
|
|
|
|
_top->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_top, 1, pTop);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Bottom
|
2013-11-14 07:55:36 +08:00
|
|
|
_bottom = Sprite::createWithTexture(_scale9Image->getTexture(), centerbottombounds);
|
|
|
|
_bottom->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_bottom, 1, pBottom);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Left
|
2013-11-14 07:55:36 +08:00
|
|
|
_left = Sprite::createWithTexture(_scale9Image->getTexture(), leftcenterbounds);
|
|
|
|
_left->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_left, 1, pLeft);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Right
|
2013-11-14 07:55:36 +08:00
|
|
|
_right = Sprite::createWithTexture(_scale9Image->getTexture(), rightcenterbounds);
|
|
|
|
_right->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_right, 1, pRight);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Top left
|
2013-11-14 07:55:36 +08:00
|
|
|
_topLeft = Sprite::createWithTexture(_scale9Image->getTexture(), lefttopbounds);
|
|
|
|
_topLeft->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_topLeft, 2, pTopLeft);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Top right
|
2013-11-14 07:55:36 +08:00
|
|
|
_topRight = Sprite::createWithTexture(_scale9Image->getTexture(), righttopbounds);
|
|
|
|
_topRight->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_topRight, 2, pTopRight);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Bottom left
|
2013-11-14 07:55:36 +08:00
|
|
|
_bottomLeft = Sprite::createWithTexture(_scale9Image->getTexture(), leftbottombounds);
|
|
|
|
_bottomLeft->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_bottomLeft, 2, pBottomLeft);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Bottom right
|
2013-11-14 07:55:36 +08:00
|
|
|
_bottomRight = Sprite::createWithTexture(_scale9Image->getTexture(), rightbottombounds);
|
|
|
|
_bottomRight->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_bottomRight, 2, pBottomRight);
|
2013-02-12 05:26:40 +08:00
|
|
|
} else {
|
|
|
|
// set up transformation of coordinates
|
|
|
|
// to handle the case where the sprite is stored rotated
|
|
|
|
// in the spritesheet
|
2013-07-24 06:20:22 +08:00
|
|
|
// log("rotated");
|
2013-02-12 05:26:40 +08:00
|
|
|
|
2013-08-27 09:08:21 +08:00
|
|
|
AffineTransform t = AffineTransform::IDENTITY;
|
2013-06-20 14:15:53 +08:00
|
|
|
|
|
|
|
Rect rotatedcenterbounds = centerbounds;
|
|
|
|
Rect rotatedrightbottombounds = rightbottombounds;
|
|
|
|
Rect rotatedleftbottombounds = leftbottombounds;
|
|
|
|
Rect rotatedrighttopbounds = righttopbounds;
|
|
|
|
Rect rotatedlefttopbounds = lefttopbounds;
|
|
|
|
Rect rotatedrightcenterbounds = rightcenterbounds;
|
|
|
|
Rect rotatedleftcenterbounds = leftcenterbounds;
|
|
|
|
Rect rotatedcenterbottombounds = centerbottombounds;
|
|
|
|
Rect rotatedcentertopbounds = centertopbounds;
|
2013-03-09 16:23:24 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
t = AffineTransformTranslate(t, rect.size.height+rect.origin.x, rect.origin.y);
|
|
|
|
t = AffineTransformRotate(t, 1.57079633f);
|
2013-03-09 16:23:24 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
centerbounds = RectApplyAffineTransform(centerbounds, t);
|
|
|
|
rightbottombounds = RectApplyAffineTransform(rightbottombounds, t);
|
|
|
|
leftbottombounds = RectApplyAffineTransform(leftbottombounds, t);
|
|
|
|
righttopbounds = RectApplyAffineTransform(righttopbounds, t);
|
|
|
|
lefttopbounds = RectApplyAffineTransform(lefttopbounds, t);
|
|
|
|
rightcenterbounds = RectApplyAffineTransform(rightcenterbounds, t);
|
|
|
|
leftcenterbounds = RectApplyAffineTransform(leftcenterbounds, t);
|
|
|
|
centerbottombounds = RectApplyAffineTransform(centerbottombounds, t);
|
|
|
|
centertopbounds = RectApplyAffineTransform(centertopbounds, t);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
2013-03-09 16:23:24 +08:00
|
|
|
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;
|
|
|
|
|
2013-02-12 05:26:40 +08:00
|
|
|
// Centre
|
2013-11-14 07:55:36 +08:00
|
|
|
_centre = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedcenterbounds, true);
|
|
|
|
_centre->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_centre, 0, pCentre);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// Top
|
2013-11-14 07:55:36 +08:00
|
|
|
_top = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedcentertopbounds, true);
|
|
|
|
_top->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_top, 1, pTop);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// Bottom
|
2013-12-04 15:39:28 +08:00
|
|
|
_bottom = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedcenterbottombounds, true);
|
2013-11-14 07:55:36 +08:00
|
|
|
_bottom->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_bottom, 1, pBottom);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// Left
|
2013-11-14 07:55:36 +08:00
|
|
|
_left = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedleftcenterbounds, true);
|
|
|
|
_left->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_left, 1, pLeft);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// Right
|
2013-11-14 07:55:36 +08:00
|
|
|
_right = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedrightcenterbounds, true);
|
|
|
|
_right->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_right, 1, pRight);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// Top left
|
2013-11-14 07:55:36 +08:00
|
|
|
_topLeft = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedlefttopbounds, true);
|
|
|
|
_topLeft->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_topLeft, 2, pTopLeft);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// Top right
|
2013-11-14 07:55:36 +08:00
|
|
|
_topRight = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedrighttopbounds, true);
|
|
|
|
_topRight->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_topRight, 2, pTopRight);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// Bottom left
|
2013-11-14 07:55:36 +08:00
|
|
|
_bottomLeft = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedleftbottombounds, true);
|
|
|
|
_bottomLeft->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_bottomLeft, 2, pBottomLeft);
|
2013-02-12 05:26:40 +08:00
|
|
|
|
|
|
|
// Bottom right
|
2013-11-14 07:55:36 +08:00
|
|
|
_bottomRight = Sprite::createWithTexture(_scale9Image->getTexture(), rotatedrightbottombounds, true);
|
|
|
|
_bottomRight->retain();
|
2013-04-15 16:37:35 +08:00
|
|
|
_scale9Image->addChild(_bottomRight, 2, pBottomRight);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2013-02-12 05:26:40 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setContentSize(rect.size);
|
2013-04-15 16:37:35 +08:00
|
|
|
this->addChild(_scale9Image);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_spritesGenerated)
|
2013-07-11 17:07:41 +08:00
|
|
|
{
|
|
|
|
// Restore color and opacity
|
|
|
|
this->setOpacity(opacity);
|
|
|
|
this->setColor(color);
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_spritesGenerated = true;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Scale9Sprite::setContentSize(const Size &size)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Node::setContentSize(size);
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_positionsAreDirty = true;
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Scale9Sprite::updatePositions()
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-01-25 07:43:10 +08:00
|
|
|
// Check that instances are non-NULL
|
2013-04-15 16:37:35 +08:00
|
|
|
if(!((_topLeft) &&
|
|
|
|
(_topRight) &&
|
|
|
|
(_bottomRight) &&
|
|
|
|
(_bottomLeft) &&
|
|
|
|
(_centre))) {
|
2013-01-25 07:43:10 +08:00
|
|
|
// if any of the above sprites are NULL, return
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Size size = this->_contentSize;
|
2012-06-01 02:47:16 +08:00
|
|
|
|
2013-04-15 16:37:35 +08:00
|
|
|
float sizableWidth = size.width - _topLeft->getContentSize().width - _topRight->getContentSize().width;
|
|
|
|
float sizableHeight = size.height - _topLeft->getContentSize().height - _bottomRight->getContentSize().height;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-04-15 16:37:35 +08:00
|
|
|
float horizontalScale = sizableWidth/_centre->getContentSize().width;
|
|
|
|
float verticalScale = sizableHeight/_centre->getContentSize().height;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-04-15 16:37:35 +08:00
|
|
|
_centre->setScaleX(horizontalScale);
|
|
|
|
_centre->setScaleY(verticalScale);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-04-15 16:37:35 +08:00
|
|
|
float rescaledWidth = _centre->getContentSize().width * horizontalScale;
|
|
|
|
float rescaledHeight = _centre->getContentSize().height * verticalScale;
|
2012-06-01 02:47:16 +08:00
|
|
|
|
2013-04-15 16:37:35 +08:00
|
|
|
float leftWidth = _bottomLeft->getContentSize().width;
|
|
|
|
float bottomHeight = _bottomLeft->getContentSize().height;
|
2012-06-01 02:47:16 +08:00
|
|
|
|
2013-07-12 14:11:55 +08:00
|
|
|
_bottomLeft->setAnchorPoint(Point(0,0));
|
|
|
|
_bottomRight->setAnchorPoint(Point(0,0));
|
|
|
|
_topLeft->setAnchorPoint(Point(0,0));
|
|
|
|
_topRight->setAnchorPoint(Point(0,0));
|
|
|
|
_left->setAnchorPoint(Point(0,0));
|
|
|
|
_right->setAnchorPoint(Point(0,0));
|
|
|
|
_top->setAnchorPoint(Point(0,0));
|
|
|
|
_bottom->setAnchorPoint(Point(0,0));
|
|
|
|
_centre->setAnchorPoint(Point(0,0));
|
2012-06-01 02:47:16 +08:00
|
|
|
|
|
|
|
// Position corners
|
2013-07-12 14:11:55 +08:00
|
|
|
_bottomLeft->setPosition(Point(0,0));
|
|
|
|
_bottomRight->setPosition(Point(leftWidth+rescaledWidth,0));
|
|
|
|
_topLeft->setPosition(Point(0, bottomHeight+rescaledHeight));
|
|
|
|
_topRight->setPosition(Point(leftWidth+rescaledWidth, bottomHeight+rescaledHeight));
|
2012-06-01 02:47:16 +08:00
|
|
|
|
|
|
|
// Scale and position borders
|
2013-07-12 14:11:55 +08:00
|
|
|
_left->setPosition(Point(0, bottomHeight));
|
2013-04-15 16:37:35 +08:00
|
|
|
_left->setScaleY(verticalScale);
|
2013-07-12 14:11:55 +08:00
|
|
|
_right->setPosition(Point(leftWidth+rescaledWidth,bottomHeight));
|
2013-04-15 16:37:35 +08:00
|
|
|
_right->setScaleY(verticalScale);
|
2013-07-12 14:11:55 +08:00
|
|
|
_bottom->setPosition(Point(leftWidth,0));
|
2013-04-15 16:37:35 +08:00
|
|
|
_bottom->setScaleX(horizontalScale);
|
2013-07-12 14:11:55 +08:00
|
|
|
_top->setPosition(Point(leftWidth,bottomHeight+rescaledHeight));
|
2013-04-15 16:37:35 +08:00
|
|
|
_top->setScaleX(horizontalScale);
|
2012-06-01 02:47:16 +08:00
|
|
|
|
|
|
|
// Position centre
|
2013-07-12 14:11:55 +08:00
|
|
|
_centre->setPosition(Point(leftWidth, bottomHeight));
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
bool Scale9Sprite::initWithFile(const std::string& file, const Rect& rect, const Rect& capInsets)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
SpriteBatchNode *batchnode = SpriteBatchNode::create(file, 9);
|
2012-04-16 18:58:43 +08:00
|
|
|
bool pReturn = this->initWithBatchNode(batchnode, rect, capInsets);
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::create(const std::string& file, const Rect& rect, const Rect& capInsets)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite* pReturn = new Scale9Sprite();
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
bool Scale9Sprite::initWithFile(const std::string& file, const Rect& rect)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
bool pReturn = this->initWithFile(file, rect, Rect::ZERO);
|
2012-04-16 18:58:43 +08:00
|
|
|
return pReturn;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::create(const std::string& file, const Rect& rect)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite* pReturn = new Scale9Sprite();
|
2012-04-19 14:35:52 +08:00
|
|
|
if ( pReturn && pReturn->initWithFile(file, rect) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
bool Scale9Sprite::initWithFile(const Rect& capInsets, const std::string& file)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
bool pReturn = this->initWithFile(file, Rect::ZERO, capInsets);
|
2012-04-16 18:58:43 +08:00
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::create(const Rect& capInsets, const std::string& file)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite* pReturn = new Scale9Sprite();
|
2013-01-21 21:55:45 +08:00
|
|
|
if ( pReturn && pReturn->initWithFile(capInsets, file) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
bool Scale9Sprite::initWithFile(const std::string& file)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
bool pReturn = this->initWithFile(file, Rect::ZERO);
|
2012-04-16 18:58:43 +08:00
|
|
|
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
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::create(const std::string& file)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite* pReturn = new Scale9Sprite();
|
2012-04-19 14:35:52 +08:00
|
|
|
if ( pReturn && pReturn->initWithFile(file) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2013-10-08 11:53:38 +08:00
|
|
|
bool Scale9Sprite::initWithSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Texture2D* texture = spriteFrame->getTexture();
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(texture != NULL, "CCTexture must be not nil");
|
2013-01-24 08:42:14 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
SpriteBatchNode *batchnode = SpriteBatchNode::createWithTexture(texture, 9);
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(batchnode != NULL, "CCSpriteBatchNode must be not nil");
|
2012-06-01 02:47:16 +08:00
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
bool pReturn = this->initWithBatchNode(batchnode, spriteFrame->getRect(), spriteFrame->isRotated(), capInsets);
|
2012-04-16 18:58:43 +08:00
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2013-10-08 11:53:38 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::createWithSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite* pReturn = new Scale9Sprite();
|
2012-04-19 14:35:52 +08:00
|
|
|
if ( pReturn && pReturn->initWithSpriteFrame(spriteFrame, capInsets) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
bool Scale9Sprite::initWithSpriteFrame(SpriteFrame* spriteFrame)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(spriteFrame != NULL, "Invalid spriteFrame for sprite");
|
2013-07-12 14:47:36 +08:00
|
|
|
bool pReturn = this->initWithSpriteFrame(spriteFrame, Rect::ZERO);
|
2012-04-16 18:58:43 +08:00
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::createWithSpriteFrame(SpriteFrame* spriteFrame)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite* pReturn = new Scale9Sprite();
|
2012-04-19 14:35:52 +08:00
|
|
|
if ( pReturn && pReturn->initWithSpriteFrame(spriteFrame) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
bool Scale9Sprite::initWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT((SpriteFrameCache::getInstance()) != NULL, "SpriteFrameCache::getInstance() must be non-NULL");
|
2012-06-01 02:47:16 +08:00
|
|
|
|
2013-07-18 07:56:19 +08:00
|
|
|
SpriteFrame *frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFrameName);
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(frame != NULL, "CCSpriteFrame must be non-NULL");
|
2013-01-24 08:42:14 +08:00
|
|
|
|
|
|
|
if (NULL == frame) return false;
|
|
|
|
|
2012-04-16 18:58:43 +08:00
|
|
|
bool pReturn = this->initWithSpriteFrame(frame, capInsets);
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite* pReturn = new Scale9Sprite();
|
2012-04-19 14:35:52 +08:00
|
|
|
if ( pReturn && pReturn->initWithSpriteFrameName(spriteFrameName, capInsets) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
bool Scale9Sprite::initWithSpriteFrameName(const std::string& spriteFrameName)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
bool pReturn = this->initWithSpriteFrameName(spriteFrameName, Rect::ZERO);
|
2012-04-16 18:58:43 +08:00
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const std::string& spriteFrameName)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite* pReturn = new Scale9Sprite();
|
2012-04-19 14:35:52 +08:00
|
|
|
if ( pReturn && pReturn->initWithSpriteFrameName(spriteFrameName) )
|
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
2013-01-24 08:42:14 +08:00
|
|
|
|
2013-07-24 06:20:22 +08:00
|
|
|
log("Could not allocate Scale9Sprite()");
|
2012-04-19 14:35:52 +08:00
|
|
|
return NULL;
|
|
|
|
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2013-10-08 11:53:38 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::resizableSpriteWithCapInsets(const Rect& capInsets)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite* pReturn = new Scale9Sprite();
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( pReturn && pReturn->initWithBatchNode(_scale9Image, _spriteRect, capInsets) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pReturn->autorelease();
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pReturn);
|
|
|
|
return NULL;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite* Scale9Sprite::create()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite *pReturn = new Scale9Sprite();
|
2013-04-15 11:51:26 +08:00
|
|
|
if (pReturn && pReturn->init())
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/** 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.
|
|
|
|
*/
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Scale9Sprite::setPreferredSize(Size preferedSize)
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
|
|
|
this->setContentSize(preferedSize);
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_preferredSize = preferedSize;
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Size Scale9Sprite::getPreferredSize()
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return this->_preferredSize;
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
2012-04-16 18:58:43 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Scale9Sprite::setCapInsets(Rect capInsets)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Size contentSize = this->_contentSize;
|
2013-06-15 14:03:30 +08:00
|
|
|
this->updateWithBatchNode(this->_scale9Image, this->_spriteRect, _spriteFrameRotated, capInsets);
|
2012-06-01 02:47:16 +08:00
|
|
|
this->setContentSize(contentSize);
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect Scale9Sprite::getCapInsets()
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _capInsets;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Scale9Sprite::updateCapInset()
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect insets;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (this->_insetLeft == 0 && this->_insetTop == 0 && this->_insetRight == 0 && this->_insetBottom == 0)
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
insets = Rect::ZERO;
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-12 15:07:44 +08:00
|
|
|
insets = Rect(_insetLeft,
|
2013-07-08 17:00:27 +08:00
|
|
|
_insetTop,
|
|
|
|
_spriteRect.size.width-_insetLeft-_insetRight,
|
|
|
|
_spriteRect.size.height-_insetTop-_insetBottom);
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
this->setCapInsets(insets);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Scale9Sprite::setOpacityModifyRGB(bool var)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_scale9Image)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-04-15 16:37:35 +08:00
|
|
|
_opacityModifyRGB = var;
|
2013-11-28 18:23:06 +08:00
|
|
|
|
2013-12-19 17:05:59 +08:00
|
|
|
for(auto child : _scale9Image->getChildren()){
|
2013-12-06 18:07:16 +08:00
|
|
|
child->setOpacityModifyRGB(_opacityModifyRGB);
|
2013-12-19 17:05:59 +08:00
|
|
|
}
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
2013-11-28 18:23:06 +08:00
|
|
|
|
2013-07-05 15:06:38 +08:00
|
|
|
bool Scale9Sprite::isOpacityModifyRGB() const
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2013-04-15 16:37:35 +08:00
|
|
|
return _opacityModifyRGB;
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Scale9Sprite::setSpriteFrame(SpriteFrame * spriteFrame)
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
SpriteBatchNode * batchnode = SpriteBatchNode::createWithTexture(spriteFrame->getTexture(), 9);
|
2013-07-12 14:47:36 +08:00
|
|
|
this->updateWithBatchNode(batchnode, spriteFrame->getRect(), spriteFrame->isRotated(), Rect::ZERO);
|
2012-06-01 02:47:16 +08:00
|
|
|
|
|
|
|
// Reset insets
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_insetLeft = 0;
|
|
|
|
this->_insetTop = 0;
|
|
|
|
this->_insetRight = 0;
|
|
|
|
this->_insetBottom = 0;
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float Scale9Sprite::getInsetLeft()
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return this->_insetLeft;
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float Scale9Sprite::getInsetTop()
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return this->_insetTop;
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float Scale9Sprite::getInsetRight()
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return this->_insetRight;
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float Scale9Sprite::getInsetBottom()
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return this->_insetBottom;
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Scale9Sprite::setInsetLeft(float insetLeft)
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_insetLeft = insetLeft;
|
2012-06-01 02:47:16 +08:00
|
|
|
this->updateCapInset();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Scale9Sprite::setInsetTop(float insetTop)
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_insetTop = insetTop;
|
2012-06-01 02:47:16 +08:00
|
|
|
this->updateCapInset();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Scale9Sprite::setInsetRight(float insetRight)
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_insetRight = insetRight;
|
2012-06-01 02:47:16 +08:00
|
|
|
this->updateCapInset();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Scale9Sprite::setInsetBottom(float insetBottom)
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_insetBottom = insetBottom;
|
2012-06-01 02:47:16 +08:00
|
|
|
this->updateCapInset();
|
|
|
|
}
|
|
|
|
|
2014-03-01 08:10:48 +08:00
|
|
|
void Scale9Sprite::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated)
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(this->_positionsAreDirty)
|
2012-06-01 02:47:16 +08:00
|
|
|
{
|
|
|
|
this->updatePositions();
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_positionsAreDirty = false;
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
2014-03-01 08:10:48 +08:00
|
|
|
Node::visit(renderer, parentTransform, parentTransformUpdated);
|
2012-06-01 02:47:16 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void Scale9Sprite::setColor(const Color3B& color)
|
2013-04-15 16:37:35 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_scale9Image)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:07:16 +08:00
|
|
|
Node::setColor(color);
|
2013-11-28 18:23:06 +08:00
|
|
|
|
2013-12-19 17:05:59 +08:00
|
|
|
for(auto child : _scale9Image->getChildren()){
|
2013-12-06 18:07:16 +08:00
|
|
|
child->setColor(color);
|
2013-12-19 17:05:59 +08:00
|
|
|
}
|
2013-04-15 16:37:35 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Scale9Sprite::setOpacity(GLubyte opacity)
|
2013-04-15 16:37:35 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_scale9Image)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-06 18:07:16 +08:00
|
|
|
Node::setOpacity(opacity);
|
2013-11-28 18:23:06 +08:00
|
|
|
|
2013-12-19 17:05:59 +08:00
|
|
|
for(auto child : _scale9Image->getChildren()){
|
2013-12-06 18:07:16 +08:00
|
|
|
child->setOpacity(opacity);
|
2013-12-19 17:05:59 +08:00
|
|
|
}
|
2013-04-15 16:37:35 +08:00
|
|
|
}
|
|
|
|
|
2013-07-15 20:10:55 +08:00
|
|
|
void Scale9Sprite::updateDisplayedColor(const cocos2d::Color3B &parentColor)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_scale9Image)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-06 18:07:16 +08:00
|
|
|
Node::updateDisplayedColor(parentColor);
|
2013-12-19 17:05:59 +08:00
|
|
|
|
|
|
|
for(auto child : _scale9Image->getChildren()){
|
2013-12-06 18:07:16 +08:00
|
|
|
child->updateDisplayedColor(parentColor);
|
2013-12-19 17:05:59 +08:00
|
|
|
}
|
2013-07-15 20:10:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scale9Sprite::updateDisplayedOpacity(GLubyte parentOpacity)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_scale9Image)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-06 18:07:16 +08:00
|
|
|
Node::updateDisplayedOpacity(parentOpacity);
|
2013-11-28 18:23:06 +08:00
|
|
|
|
2013-12-19 17:05:59 +08:00
|
|
|
for(auto child : _scale9Image->getChildren()){
|
2013-12-06 18:07:16 +08:00
|
|
|
child->updateDisplayedOpacity(parentOpacity);
|
2013-12-19 17:05:59 +08:00
|
|
|
}
|
2013-05-08 03:02:32 +08:00
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_END
|