From 64921679743205911e0e4053639ab931391d4e7f Mon Sep 17 00:00:00 2001 From: andyque Date: Wed, 30 Jul 2014 15:27:18 +0800 Subject: [PATCH] refactor ImageView --- cocos/ui/UIImageView.cpp | 72 +++++++--------------------------------- cocos/ui/UIImageView.h | 4 +-- 2 files changed, 14 insertions(+), 62 deletions(-) diff --git a/cocos/ui/UIImageView.cpp b/cocos/ui/UIImageView.cpp index 13e374fc4d..e35c1d355f 100644 --- a/cocos/ui/UIImageView.cpp +++ b/cocos/ui/UIImageView.cpp @@ -29,10 +29,6 @@ THE SOFTWARE. NS_CC_BEGIN namespace ui { - - -#define STATIC_CAST_CCSPRITE static_cast(_imageRenderer) -#define STATIC_CAST_SCALE9SPRITE static_cast(_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(_imageRenderer)->setPreferredSize(_contentSize); + _imageRenderer->setPreferredSize(_contentSize); } else { diff --git a/cocos/ui/UIImageView.h b/cocos/ui/UIImageView.h index d0c4f06560..a3150a8de4 100644 --- a/cocos/ui/UIImageView.h +++ b/cocos/ui/UIImageView.h @@ -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;