refactor ImageView

This commit is contained in:
andyque 2014-07-30 15:27:18 +08:00
parent a6e376f93c
commit 6492167974
2 changed files with 14 additions and 62 deletions

View File

@ -29,10 +29,6 @@ THE SOFTWARE.
NS_CC_BEGIN
namespace ui {
#define STATIC_CAST_CCSPRITE static_cast<Sprite*>(_imageRenderer)
#define STATIC_CAST_SCALE9SPRITE static_cast<Scale9Sprite*>(_imageRenderer)
static const int IMAGE_RENDERER_Z = (-1);
@ -108,7 +104,7 @@ bool ImageView::init(const std::string &imageFileName, TextureResType texType)
void ImageView::initRenderer()
{
_imageRenderer = Sprite::create();
_imageRenderer = Scale9Sprite::create();
addProtectedChild(_imageRenderer, IMAGE_RENDERER_Z, -1);
}
@ -123,34 +119,15 @@ void ImageView::loadTexture(const std::string& fileName, TextureResType texType)
switch (_imageTexType)
{
case TextureResType::LOCAL:
if (_scale9Enabled)
{
Scale9Sprite* imageRendererScale9 = STATIC_CAST_SCALE9SPRITE;
imageRendererScale9->initWithFile(fileName);
imageRendererScale9->setCapInsets(_capInsets);
}
else
{
Sprite* imageRenderer = STATIC_CAST_CCSPRITE;
imageRenderer->setTexture(fileName);
}
_imageRenderer->initWithFile(fileName);
break;
case TextureResType::PLIST:
if (_scale9Enabled)
{
Scale9Sprite* imageRendererScale9 = STATIC_CAST_SCALE9SPRITE;
imageRendererScale9->initWithSpriteFrameName(fileName);
imageRendererScale9->setCapInsets(_capInsets);
}
else
{
Sprite* imageRenderer = STATIC_CAST_CCSPRITE;
imageRenderer->setSpriteFrame(fileName);
}
_imageRenderer->initWithSpriteFrameName(fileName);
break;
default:
break;
}
_imageTextureSize = _imageRenderer->getContentSize();
updateFlippedX();
updateFlippedY();
@ -161,39 +138,24 @@ void ImageView::loadTexture(const std::string& fileName, TextureResType texType)
void ImageView::setTextureRect(const Rect &rect)
{
//This API should be refactor
if (_scale9Enabled)
{
}
else
{
STATIC_CAST_CCSPRITE->setTextureRect(rect);
_imageRenderer->getSprite()->setTextureRect(rect);
}
}
void ImageView::updateFlippedX()
{
if (_scale9Enabled)
{
int flip = _flippedX ? -1 : 1;
STATIC_CAST_SCALE9SPRITE->setScaleX(flip);
}
else
{
STATIC_CAST_CCSPRITE->setFlippedX(_flippedX);
}
_imageRenderer->setFlippedX(_flippedX);
}
void ImageView::updateFlippedY()
{
if (_scale9Enabled)
{
int flip = _flippedY ? -1 : 1;
STATIC_CAST_SCALE9SPRITE->setScaleY(flip);
}
else
{
STATIC_CAST_CCSPRITE->setFlippedY(_flippedY);
}
_imageRenderer->setFlippedY(_flippedY);
}
@ -206,18 +168,8 @@ void ImageView::setScale9Enabled(bool able)
_scale9Enabled = able;
removeProtectedChild(_imageRenderer);
_imageRenderer = nullptr;
if (_scale9Enabled)
{
_imageRenderer = Scale9Sprite::create();
}
else
{
_imageRenderer = Sprite::create();
}
loadTexture(_textureFile,_imageTexType);
addProtectedChild(_imageRenderer, IMAGE_RENDERER_Z, -1);
_imageRenderer->setScale9Enabled(_scale9Enabled);
if (_scale9Enabled)
{
bool ignoreBefore = _ignoreSize;
@ -252,7 +204,7 @@ void ImageView::setCapInsets(const Rect &capInsets)
{
return;
}
STATIC_CAST_SCALE9SPRITE->setCapInsets(capInsets);
_imageRenderer->setCapInsets(capInsets);
}
const Rect& ImageView::getCapInsets()const
@ -298,7 +250,7 @@ void ImageView::imageTextureScaleChangedWithSize()
{
if (_scale9Enabled)
{
static_cast<Scale9Sprite*>(_imageRenderer)->setPreferredSize(_contentSize);
_imageRenderer->setPreferredSize(_contentSize);
}
else
{

View File

@ -31,7 +31,7 @@ THE SOFTWARE.
NS_CC_BEGIN
namespace ui {
class Scale9Sprite;
/**
* @js NA
* @lua NA
@ -132,7 +132,7 @@ protected:
bool _scale9Enabled;
bool _prevIgnoreSize;
Rect _capInsets;
Node* _imageRenderer;
Scale9Sprite* _imageRenderer;
std::string _textureFile;
TextureResType _imageTexType;
Size _imageTextureSize;