refactor Slider control

This commit is contained in:
andyque 2014-07-30 15:52:27 +08:00
parent 6492167974
commit c5d2585049
5 changed files with 51 additions and 63 deletions

View File

@ -105,6 +105,8 @@ bool ImageView::init(const std::string &imageFileName, TextureResType texType)
void ImageView::initRenderer() void ImageView::initRenderer()
{ {
_imageRenderer = Scale9Sprite::create(); _imageRenderer = Scale9Sprite::create();
_imageRenderer->setScale9Enabled(false);
addProtectedChild(_imageRenderer, IMAGE_RENDERER_Z, -1); addProtectedChild(_imageRenderer, IMAGE_RENDERER_Z, -1);
} }

View File

@ -390,6 +390,25 @@ y+=ytranslate; \
this->_positionsAreDirty = true; this->_positionsAreDirty = true;
} }
void Scale9Sprite::setAnchorPoint(const cocos2d::Vec2 &anchorPoint)
{
Node::setAnchorPoint(anchorPoint);
if (_scale9Enabled) {
for(const auto& node : _protectedChildren)
{
node->setAnchorPoint(anchorPoint);
}
}
else
{
if (_scale9Image) {
_scale9Image->setAnchorPoint(anchorPoint);
}
}
}
void Scale9Sprite::updatePositions() void Scale9Sprite::updatePositions()
{ {
// Check that instances are non-NULL // Check that instances are non-NULL
@ -866,7 +885,8 @@ y+=ytranslate; \
void Scale9Sprite::adjustScale9ImagePosition() void Scale9Sprite::adjustScale9ImagePosition()
{ {
if (_scale9Image) { if (_scale9Image) {
_scale9Image->setPosition(_scale9Image->getPosition() + Vec2(_originalSize.width/2, _originalSize.height/2)); _scale9Image->setPosition(_scale9Image->getPosition() + Vec2(_originalSize.width * _anchorPoint.x,
_originalSize.height * _anchorPoint.y));
} }
} }

View File

@ -246,7 +246,7 @@ namespace ui {
// overrides // overrides
virtual void setContentSize(const Size & size) override; virtual void setContentSize(const Size & size) override;
virtual void setAnchorPoint(const Vec2& anchorPoint) override;
Size getOriginalSize() const; Size getOriginalSize() const;
void setPreferredSize(const Size& size); void setPreferredSize(const Size& size);

View File

@ -98,20 +98,28 @@ bool Slider::init()
void Slider::initRenderer() void Slider::initRenderer()
{ {
_barRenderer = Sprite::create(); _barRenderer = Scale9Sprite::create();
_progressBarRenderer = Sprite::create(); _progressBarRenderer = Scale9Sprite::create();
_barRenderer->setScale9Enabled(false);
_progressBarRenderer->setScale9Enabled(false);
_progressBarRenderer->setAnchorPoint(Vec2(0.0f, 0.5f)); _progressBarRenderer->setAnchorPoint(Vec2(0.0f, 0.5f));
addProtectedChild(_barRenderer, BASEBAR_RENDERER_Z, -1); addProtectedChild(_barRenderer, BASEBAR_RENDERER_Z, -1);
addProtectedChild(_progressBarRenderer, PROGRESSBAR_RENDERER_Z, -1); addProtectedChild(_progressBarRenderer, PROGRESSBAR_RENDERER_Z, -1);
_slidBallNormalRenderer = Sprite::create(); _slidBallNormalRenderer = Sprite::create();
_slidBallPressedRenderer = Sprite::create(); _slidBallPressedRenderer = Sprite::create();
_slidBallPressedRenderer->setVisible(false); _slidBallPressedRenderer->setVisible(false);
_slidBallDisabledRenderer = Sprite::create(); _slidBallDisabledRenderer = Sprite::create();
_slidBallDisabledRenderer->setVisible(false); _slidBallDisabledRenderer->setVisible(false);
_slidBallRenderer = Node::create(); _slidBallRenderer = Node::create();
_slidBallRenderer->addChild(_slidBallNormalRenderer); _slidBallRenderer->addChild(_slidBallNormalRenderer);
_slidBallRenderer->addChild(_slidBallPressedRenderer); _slidBallRenderer->addChild(_slidBallPressedRenderer);
_slidBallRenderer->addChild(_slidBallDisabledRenderer); _slidBallRenderer->addChild(_slidBallDisabledRenderer);
addProtectedChild(_slidBallRenderer, SLIDBALL_RENDERER_Z, -1); addProtectedChild(_slidBallRenderer, SLIDBALL_RENDERER_Z, -1);
} }
@ -126,24 +134,10 @@ void Slider::loadBarTexture(const std::string& fileName, TextureResType texType)
switch (_barTexType) switch (_barTexType)
{ {
case TextureResType::LOCAL: case TextureResType::LOCAL:
if (_scale9Enabled) _barRenderer->initWithFile(fileName);
{
static_cast<Scale9Sprite*>(_barRenderer)->initWithFile(fileName);
}
else
{
static_cast<Sprite*>(_barRenderer)->setTexture(fileName);
}
break; break;
case TextureResType::PLIST: case TextureResType::PLIST:
if (_scale9Enabled) _barRenderer->initWithSpriteFrameName(fileName);
{
static_cast<Scale9Sprite*>(_barRenderer)->initWithSpriteFrameName(fileName);
}
else
{
static_cast<Sprite*>(_barRenderer)->setSpriteFrame(fileName);
}
break; break;
default: default:
break; break;
@ -165,24 +159,10 @@ void Slider::loadProgressBarTexture(const std::string& fileName, TextureResType
switch (_progressBarTexType) switch (_progressBarTexType)
{ {
case TextureResType::LOCAL: case TextureResType::LOCAL:
if (_scale9Enabled) _progressBarRenderer->initWithFile(fileName);
{
static_cast<Scale9Sprite*>(_progressBarRenderer)->initWithFile(fileName);
}
else
{
static_cast<Sprite*>(_progressBarRenderer)->setTexture(fileName);
}
break; break;
case TextureResType::PLIST: case TextureResType::PLIST:
if (_scale9Enabled) _progressBarRenderer->initWithSpriteFrameName(fileName);
{
static_cast<Scale9Sprite*>(_progressBarRenderer)->initWithSpriteFrameName(fileName);
}
else
{
static_cast<Sprite*>(_progressBarRenderer)->setSpriteFrame(fileName);
}
break; break;
default: default:
break; break;
@ -201,24 +181,9 @@ void Slider::setScale9Enabled(bool able)
} }
_scale9Enabled = able; _scale9Enabled = able;
removeProtectedChild(_barRenderer); _barRenderer->setScale9Enabled(_scale9Enabled);
removeProtectedChild(_progressBarRenderer); _progressBarRenderer->setScale9Enabled(_scale9Enabled);
_barRenderer = nullptr;
_progressBarRenderer = nullptr;
if (_scale9Enabled)
{
_barRenderer = Scale9Sprite::create();
_progressBarRenderer = Scale9Sprite::create();
}
else
{
_barRenderer = Sprite::create();
_progressBarRenderer = Sprite::create();
}
loadBarTexture(_textureFile, _barTexType);
loadProgressBarTexture(_progressBarTextureFile, _progressBarTexType);
addProtectedChild(_barRenderer, BASEBAR_RENDERER_Z, -1);
addProtectedChild(_progressBarRenderer, PROGRESSBAR_RENDERER_Z, -1);
if (_scale9Enabled) if (_scale9Enabled)
{ {
bool ignoreBefore = _ignoreSize; bool ignoreBefore = _ignoreSize;
@ -260,7 +225,7 @@ void Slider::setCapInsetsBarRenderer(const Rect &capInsets)
{ {
return; return;
} }
static_cast<Scale9Sprite*>(_barRenderer)->setCapInsets(capInsets); _barRenderer->setCapInsets(capInsets);
} }
const Rect& Slider::getCapInsetsBarRenderer()const const Rect& Slider::getCapInsetsBarRenderer()const
@ -275,7 +240,7 @@ void Slider::setCapInsetProgressBarRebderer(const Rect &capInsets)
{ {
return; return;
} }
static_cast<Scale9Sprite*>(_progressBarRenderer)->setCapInsets(capInsets); _progressBarRenderer->setCapInsets(capInsets);
} }
const Rect& Slider::getCapInsetsProgressBarRebderer()const const Rect& Slider::getCapInsetsProgressBarRebderer()const
@ -369,11 +334,11 @@ void Slider::setPercent(int percent)
_slidBallRenderer->setPosition(Vec2(dis, _contentSize.height / 2.0f)); _slidBallRenderer->setPosition(Vec2(dis, _contentSize.height / 2.0f));
if (_scale9Enabled) if (_scale9Enabled)
{ {
static_cast<Scale9Sprite*>(_progressBarRenderer)->setPreferredSize(Size(dis,_progressBarTextureSize.height)); _progressBarRenderer->setPreferredSize(Size(dis,_progressBarTextureSize.height));
} }
else else
{ {
Sprite* spriteRenderer = static_cast<Sprite*>(_progressBarRenderer); Sprite* spriteRenderer = _progressBarRenderer->getSprite();
Rect rect = spriteRenderer->getTextureRect(); Rect rect = spriteRenderer->getTextureRect();
rect.size.width = _progressBarTextureSize.width * res; rect.size.width = _progressBarTextureSize.width * res;
spriteRenderer->setTextureRect(rect, spriteRenderer->isTextureRectRotated(), rect.size); spriteRenderer->setTextureRect(rect, spriteRenderer->isTextureRectRotated(), rect.size);
@ -497,7 +462,7 @@ void Slider::barRendererScaleChangedWithSize()
_barLength = _contentSize.width; _barLength = _contentSize.width;
if (_scale9Enabled) if (_scale9Enabled)
{ {
static_cast<Scale9Sprite*>(_barRenderer)->setPreferredSize(_contentSize); _barRenderer->setPreferredSize(_contentSize);
} }
else else
{ {
@ -534,7 +499,7 @@ void Slider::progressBarRendererScaleChangedWithSize()
{ {
if (_scale9Enabled) if (_scale9Enabled)
{ {
static_cast<Scale9Sprite*>(_progressBarRenderer)->setPreferredSize(_contentSize); _progressBarRenderer->setPreferredSize(_contentSize);
_progressBarTextureSize = _progressBarRenderer->getContentSize(); _progressBarTextureSize = _progressBarRenderer->getContentSize();
} }
else else

View File

@ -33,6 +33,7 @@ NS_CC_BEGIN
class Sprite; class Sprite;
namespace ui { namespace ui {
class Scale9Sprite;
typedef enum typedef enum
{ {
@ -226,8 +227,8 @@ protected:
virtual void copySpecialProperties(Widget* model) override; virtual void copySpecialProperties(Widget* model) override;
virtual void adaptRenderers() override; virtual void adaptRenderers() override;
protected: protected:
Node* _barRenderer; Scale9Sprite* _barRenderer;
Node* _progressBarRenderer; Scale9Sprite* _progressBarRenderer;
Size _progressBarTextureSize; Size _progressBarTextureSize;
Sprite* _slidBallNormalRenderer; Sprite* _slidBallNormalRenderer;