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()
{
_imageRenderer = Scale9Sprite::create();
_imageRenderer->setScale9Enabled(false);
addProtectedChild(_imageRenderer, IMAGE_RENDERER_Z, -1);
}

View File

@ -390,6 +390,25 @@ y+=ytranslate; \
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()
{
// Check that instances are non-NULL
@ -866,7 +885,8 @@ y+=ytranslate; \
void Scale9Sprite::adjustScale9ImagePosition()
{
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
virtual void setContentSize(const Size & size) override;
virtual void setAnchorPoint(const Vec2& anchorPoint) override;
Size getOriginalSize() const;
void setPreferredSize(const Size& size);

View File

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

View File

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