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

View File

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