axmol/cocos/gui/UISlider.cpp

566 lines
17 KiB
C++
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "gui/UISlider.h"
#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h"
2013-09-13 22:20:20 +08:00
namespace gui {
2013-09-13 22:20:20 +08:00
UISlider::UISlider():
2013-09-16 20:54:13 +08:00
_barRenderer(NULL),
_progressBarRenderer(NULL),
2013-11-06 16:04:06 +08:00
_progressBarTextureSize(cocos2d::Size::ZERO),
2013-09-16 20:54:13 +08:00
_slidBallNormalRenderer(NULL),
_slidBallPressedRenderer(NULL),
_slidBallDisabledRenderer(NULL),
_slidBallRenderer(NULL),
_barLength(0.0),
_percent(0),
_scale9Enabled(false),
_prevIgnoreSize(true),
_textureFile(""),
_progressBarTextureFile(""),
_slidBallNormalTextureFile(""),
_slidBallPressedTextureFile(""),
_slidBallDisabledTextureFile(""),
2013-11-06 16:04:06 +08:00
_capInsetsBarRenderer(cocos2d::Rect::ZERO),
_capInsetsProgressBarRenderer(cocos2d::Rect::ZERO),
2013-11-08 18:43:06 +08:00
_sliderEventListener(NULL),
_sliderEventSelector(NULL),
2013-09-16 20:54:13 +08:00
_barTexType(UI_TEX_TYPE_LOCAL),
_progressBarTexType(UI_TEX_TYPE_LOCAL),
_ballNTexType(UI_TEX_TYPE_LOCAL),
_ballPTexType(UI_TEX_TYPE_LOCAL),
_ballDTexType(UI_TEX_TYPE_LOCAL)
2013-09-13 22:20:20 +08:00
{
}
UISlider::~UISlider()
{
2013-11-08 18:43:06 +08:00
_sliderEventListener = NULL;
_sliderEventSelector = NULL;
2013-09-13 22:20:20 +08:00
}
UISlider* UISlider::create()
{
UISlider* widget = new UISlider();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return NULL;
}
void UISlider::initRenderer()
{
UIWidget::initRenderer();
2013-11-06 16:04:06 +08:00
_barRenderer = cocos2d::Sprite::create();
_progressBarRenderer = cocos2d::Sprite::create();
_progressBarRenderer->setAnchorPoint(cocos2d::Point(0.0f, 0.5f));
2013-09-16 20:54:13 +08:00
_renderer->addChild(_barRenderer, -1);
_renderer->addChild(_progressBarRenderer, -1);
2013-11-06 16:04:06 +08:00
_slidBallNormalRenderer = cocos2d::Sprite::create();
_slidBallPressedRenderer = cocos2d::Sprite::create();
2013-09-16 20:54:13 +08:00
_slidBallPressedRenderer->setVisible(false);
2013-11-06 16:04:06 +08:00
_slidBallDisabledRenderer = cocos2d::Sprite::create();
2013-09-16 20:54:13 +08:00
_slidBallDisabledRenderer->setVisible(false);
2013-11-06 16:04:06 +08:00
_slidBallRenderer = cocos2d::Node::create();
2013-09-16 20:54:13 +08:00
_slidBallRenderer->addChild(_slidBallNormalRenderer);
_slidBallRenderer->addChild(_slidBallPressedRenderer);
_slidBallRenderer->addChild(_slidBallDisabledRenderer);
_renderer->addChild(_slidBallRenderer);
2013-09-13 22:20:20 +08:00
}
void UISlider::loadBarTexture(const char* fileName, TextureResType texType)
{
if (!fileName || strcmp(fileName, "") == 0)
{
return;
}
2013-09-16 20:54:13 +08:00
_textureFile = fileName;
_barTexType = texType;
switch (_barTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->initWithFile(fileName);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->initWithFile(fileName);
2013-09-13 22:20:20 +08:00
}
break;
case UI_TEX_TYPE_PLIST:
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->initWithSpriteFrameName(fileName);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->initWithSpriteFrameName(fileName);
2013-09-13 22:20:20 +08:00
}
break;
default:
break;
}
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->setColor(getColor());
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->setColor(getColor());
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
barRendererScaleChangedWithSize();
}
void UISlider::loadProgressBarTexture(const char *fileName, TextureResType texType)
{
if (!fileName || strcmp(fileName, "") == 0)
{
return;
}
2013-09-16 20:54:13 +08:00
_progressBarTextureFile = fileName;
_progressBarTexType = texType;
switch (_progressBarTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_progressBarRenderer)->initWithFile(fileName);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_progressBarRenderer)->initWithFile(fileName);
2013-09-13 22:20:20 +08:00
}
break;
case UI_TEX_TYPE_PLIST:
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_progressBarRenderer)->initWithSpriteFrameName(fileName);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_progressBarRenderer)->initWithSpriteFrameName(fileName);
2013-09-13 22:20:20 +08:00
}
break;
default:
break;
}
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_progressBarRenderer)->setColor(getColor());
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_progressBarRenderer)->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_progressBarRenderer)->setColor(getColor());
dynamic_cast<cocos2d::Sprite*>(_progressBarRenderer)->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
_progressBarRenderer->setAnchorPoint(cocos2d::Point(0.0f, 0.5f));
2013-09-16 20:54:13 +08:00
_progressBarTextureSize = _progressBarRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
progressBarRendererScaleChangedWithSize();
}
void UISlider::setScale9Enabled(bool able)
{
2013-09-16 20:54:13 +08:00
if (_scale9Enabled == able)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
_scale9Enabled = able;
_renderer->removeChild(_barRenderer, true);
_renderer->removeChild(_progressBarRenderer, true);
_barRenderer = NULL;
_progressBarRenderer = NULL;
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
_barRenderer = cocos2d::extension::Scale9Sprite::create();
_progressBarRenderer = cocos2d::extension::Scale9Sprite::create();
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
_barRenderer = cocos2d::Sprite::create();
_progressBarRenderer = cocos2d::Sprite::create();
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
loadBarTexture(_textureFile.c_str(), _barTexType);
loadProgressBarTexture(_progressBarTextureFile.c_str(), _progressBarTexType);
_renderer->addChild(_barRenderer, -1);
_renderer->addChild(_progressBarRenderer, -1);
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
bool ignoreBefore = _ignoreSize;
2013-09-13 22:20:20 +08:00
ignoreContentAdaptWithSize(false);
2013-09-16 20:54:13 +08:00
_prevIgnoreSize = ignoreBefore;
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
ignoreContentAdaptWithSize(_prevIgnoreSize);
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
setCapInsetsBarRenderer(_capInsetsBarRenderer);
setCapInsetProgressBarRebderer(_capInsetsProgressBarRenderer);
2013-09-13 22:20:20 +08:00
}
void UISlider::ignoreContentAdaptWithSize(bool ignore)
{
2013-09-16 20:54:13 +08:00
if (!_scale9Enabled || (_scale9Enabled && !ignore))
2013-09-13 22:20:20 +08:00
{
UIWidget::ignoreContentAdaptWithSize(ignore);
2013-09-16 20:54:13 +08:00
_prevIgnoreSize = ignore;
2013-09-13 22:20:20 +08:00
}
}
2013-11-06 16:04:06 +08:00
void UISlider::setCapInsets(const cocos2d::Rect &capInsets)
2013-09-13 22:20:20 +08:00
{
setCapInsetsBarRenderer(capInsets);
setCapInsetProgressBarRebderer(capInsets);
}
2013-11-06 16:04:06 +08:00
void UISlider::setCapInsetsBarRenderer(const cocos2d::Rect &capInsets)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_capInsetsBarRenderer = capInsets;
if (!_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->setCapInsets(capInsets);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UISlider::setCapInsetProgressBarRebderer(const cocos2d::Rect &capInsets)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_capInsetsProgressBarRenderer = capInsets;
if (!_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_progressBarRenderer)->setCapInsets(capInsets);
2013-09-13 22:20:20 +08:00
}
void UISlider::loadSlidBallTextures(const char* normal,const char* pressed,const char* disabled,TextureResType texType)
{
loadSlidBallTextureNormal(normal, texType);
loadSlidBallTexturePressed(pressed,texType);
loadSlidBallTextureDisabled(disabled,texType);
}
void UISlider::loadSlidBallTextureNormal(const char* normal,TextureResType texType)
{
if (!normal || strcmp(normal, "") == 0)
{
return;
}
2013-09-16 20:54:13 +08:00
_slidBallNormalTextureFile = normal;
_ballNTexType = texType;
switch (_ballNTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-09-16 20:54:13 +08:00
_slidBallNormalRenderer->initWithFile(normal);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-09-16 20:54:13 +08:00
_slidBallNormalRenderer->initWithSpriteFrameName(normal);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-09-16 20:54:13 +08:00
_slidBallNormalRenderer->setColor(getColor());
_slidBallNormalRenderer->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
void UISlider::loadSlidBallTexturePressed(const char* pressed,TextureResType texType)
{
if (!pressed || strcmp(pressed, "") == 0)
{
return;
}
2013-09-16 20:54:13 +08:00
_slidBallPressedTextureFile = pressed;
_ballPTexType = texType;
switch (_ballPTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-09-16 20:54:13 +08:00
_slidBallPressedRenderer->initWithFile(pressed);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-09-16 20:54:13 +08:00
_slidBallPressedRenderer->initWithSpriteFrameName(pressed);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-09-16 20:54:13 +08:00
_slidBallPressedRenderer->setColor(getColor());
_slidBallPressedRenderer->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
void UISlider::loadSlidBallTextureDisabled(const char* disabled,TextureResType texType)
{
if (!disabled || strcmp(disabled, "") == 0)
{
return;
}
2013-09-16 20:54:13 +08:00
_slidBallDisabledTextureFile = disabled;
_ballDTexType = texType;
switch (_ballDTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-09-16 20:54:13 +08:00
_slidBallDisabledRenderer->initWithFile(disabled);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-09-16 20:54:13 +08:00
_slidBallDisabledRenderer->initWithSpriteFrameName(disabled);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-09-16 20:54:13 +08:00
_slidBallDisabledRenderer->setColor(getColor());
_slidBallDisabledRenderer->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
void UISlider::setPercent(int percent)
{
if (percent > 100)
{
percent = 100;
}
if (percent < 0)
{
percent = 0;
}
2013-09-16 20:54:13 +08:00
_percent = percent;
float dis = _barLength*(percent/100.0f);
2013-11-06 16:04:06 +08:00
_slidBallRenderer->setPosition(cocos2d::Point(-_barLength/2.0f + dis, 0.0f));
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_progressBarRenderer)->setPreferredSize(cocos2d::Size(dis,_progressBarTextureSize.height));
2013-09-13 22:20:20 +08:00
}
else
{
int x = 0, y = 0;
2013-09-16 20:54:13 +08:00
switch (_progressBarTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_PLIST:
{
2013-11-06 16:04:06 +08:00
cocos2d::Sprite* barNode = dynamic_cast<cocos2d::Sprite*>(_progressBarRenderer);
2013-09-13 22:20:20 +08:00
if (barNode)
{
2013-11-06 16:04:06 +08:00
cocos2d::Point to = barNode->getTextureRect().origin;
2013-09-13 22:20:20 +08:00
x = to.x;
y = to.y;
}
break;
}
default:
break;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_progressBarRenderer)->setTextureRect(cocos2d::Rect(x, y, _progressBarTextureSize.width * (percent/100.0f), _progressBarTextureSize.height));
2013-09-13 22:20:20 +08:00
}
}
2013-11-06 16:04:06 +08:00
bool UISlider::onTouchBegan(const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
bool pass = UIWidget::onTouchBegan(touchPoint);
2013-11-06 16:04:06 +08:00
cocos2d::Point nsp = _renderer->convertToNodeSpace(touchPoint);
2013-09-13 22:20:20 +08:00
setPercent(getPercentWithBallPos(nsp.x));
percentChangedEvent();
return pass;
}
2013-11-06 16:04:06 +08:00
void UISlider::onTouchMoved(const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
cocos2d::Point nsp = _renderer->convertToNodeSpace(touchPoint);
_slidBallRenderer->setPosition(cocos2d::Point(nsp.x,0));
2013-09-13 22:20:20 +08:00
setPercent(getPercentWithBallPos(nsp.x));
percentChangedEvent();
}
2013-11-06 16:04:06 +08:00
void UISlider::onTouchEnded(const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
UIWidget::onTouchEnded(touchPoint);
}
2013-11-06 16:04:06 +08:00
void UISlider::onTouchCancelled(const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
UIWidget::onTouchCancelled(touchPoint);
}
float UISlider::getPercentWithBallPos(float px)
{
2013-09-16 20:54:13 +08:00
return (((px-(-_barLength/2.0f))/_barLength)*100.0f);
2013-09-13 22:20:20 +08:00
}
2013-11-08 18:43:06 +08:00
void UISlider::addEventListenerSlider(cocos2d::Object *target, SEL_SlidPercentChangedEvent selector)
2013-09-13 22:20:20 +08:00
{
2013-11-08 18:43:06 +08:00
_sliderEventListener = target;
_sliderEventSelector = selector;
2013-09-13 22:20:20 +08:00
}
void UISlider::percentChangedEvent()
{
2013-11-08 18:43:06 +08:00
if (_sliderEventListener && _sliderEventSelector)
2013-09-13 22:20:20 +08:00
{
2013-11-08 18:43:06 +08:00
(_sliderEventListener->*_sliderEventSelector)(this,SLIDER_PERCENTCHANGED);
2013-09-13 22:20:20 +08:00
}
}
int UISlider::getPercent()
{
2013-09-16 20:54:13 +08:00
return _percent;
2013-09-13 22:20:20 +08:00
}
void UISlider::onSizeChanged()
{
barRendererScaleChangedWithSize();
progressBarRendererScaleChangedWithSize();
}
2013-11-06 16:04:06 +08:00
const cocos2d::Size& UISlider::getContentSize() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
return _barRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
cocos2d::Node* UISlider::getVirtualRenderer()
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
return _barRenderer;
2013-09-13 22:20:20 +08:00
}
void UISlider::barRendererScaleChangedWithSize()
{
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_barRenderer->setScale(1.0f);
_size = _barRenderer->getContentSize();
_barLength = _size.width;
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
_barLength = _size.width;
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->setPreferredSize(_size);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
cocos2d::Size btextureSize = _barRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
if (btextureSize.width <= 0.0f || btextureSize.height <= 0.0f)
{
2013-09-16 20:54:13 +08:00
_barRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
return;
}
2013-09-16 15:32:52 +08:00
float bscaleX = _size.width / btextureSize.width;
float bscaleY = _size.height / btextureSize.height;
2013-09-16 20:54:13 +08:00
_barRenderer->setScaleX(bscaleX);
_barRenderer->setScaleY(bscaleY);
2013-09-13 22:20:20 +08:00
}
}
2013-09-16 20:54:13 +08:00
setPercent(_percent);
2013-09-13 22:20:20 +08:00
}
void UISlider::progressBarRendererScaleChangedWithSize()
{
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
if (!_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
cocos2d::Size ptextureSize = _progressBarTextureSize;
2013-09-16 15:32:52 +08:00
float pscaleX = _size.width / ptextureSize.width;
float pscaleY = _size.height / ptextureSize.height;
2013-09-16 20:54:13 +08:00
_progressBarRenderer->setScaleX(pscaleX);
_progressBarRenderer->setScaleY(pscaleY);
2013-09-13 22:20:20 +08:00
}
}
else
{
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_progressBarRenderer)->setPreferredSize(_size);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
cocos2d::Size ptextureSize = _progressBarTextureSize;
2013-09-13 22:20:20 +08:00
if (ptextureSize.width <= 0.0f || ptextureSize.height <= 0.0f)
{
2013-09-16 20:54:13 +08:00
_progressBarRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
return;
}
2013-09-16 15:32:52 +08:00
float pscaleX = _size.width / ptextureSize.width;
float pscaleY = _size.height / ptextureSize.height;
2013-09-16 20:54:13 +08:00
_progressBarRenderer->setScaleX(pscaleX);
_progressBarRenderer->setScaleY(pscaleY);
2013-09-13 22:20:20 +08:00
}
}
2013-11-06 16:04:06 +08:00
_progressBarRenderer->setPosition(cocos2d::Point(-_barLength * 0.5f, 0.0f));
2013-09-16 20:54:13 +08:00
setPercent(_percent);
2013-09-13 22:20:20 +08:00
}
void UISlider::onPressStateChangedToNormal()
{
2013-09-16 20:54:13 +08:00
_slidBallNormalRenderer->setVisible(true);
_slidBallPressedRenderer->setVisible(false);
_slidBallDisabledRenderer->setVisible(false);
2013-09-13 22:20:20 +08:00
}
void UISlider::onPressStateChangedToPressed()
{
2013-09-16 20:54:13 +08:00
_slidBallNormalRenderer->setVisible(false);
_slidBallPressedRenderer->setVisible(true);
_slidBallDisabledRenderer->setVisible(false);
2013-09-13 22:20:20 +08:00
}
void UISlider::onPressStateChangedToDisabled()
{
2013-09-16 20:54:13 +08:00
_slidBallNormalRenderer->setVisible(false);
_slidBallPressedRenderer->setVisible(false);
_slidBallDisabledRenderer->setVisible(true);
2013-09-13 22:20:20 +08:00
}
2013-09-17 17:59:20 +08:00
const char* UISlider::getDescription() const
{
return "Slider";
}
2013-11-06 16:04:06 +08:00
UIWidget* UISlider::createCloneInstance()
{
return UISlider::create();
}
void UISlider::copySpecialProperties(UIWidget *widget)
{
UISlider* slider = dynamic_cast<UISlider*>(widget);
if (slider)
{
_prevIgnoreSize = slider->_prevIgnoreSize;
setScale9Enabled(slider->_scale9Enabled);
loadBarTexture(slider->_textureFile.c_str(), slider->_barTexType);
loadProgressBarTexture(slider->_progressBarTextureFile.c_str(), slider->_progressBarTexType);
loadSlidBallTextureNormal(slider->_slidBallNormalTextureFile.c_str(), slider->_ballNTexType);
loadSlidBallTexturePressed(slider->_slidBallPressedTextureFile.c_str(), slider->_ballPTexType);
loadSlidBallTextureDisabled(slider->_slidBallDisabledTextureFile.c_str(), slider->_ballDTexType);
setPercent(slider->getPercent());
}
}
}