axmol/extensions/CocoStudio/GUI/UIWidgets/UISlider.cpp

538 lines
15 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 "UISlider.h"
#include "../../../GUI/CCControlExtension/CCScale9Sprite.h"
NS_CC_EXT_BEGIN
UISlider::UISlider():
2013-09-16 20:54:13 +08:00
_barRenderer(NULL),
_progressBarRenderer(NULL),
_progressBarTextureSize(Size::ZERO),
_slidBallNormalRenderer(NULL),
_slidBallPressedRenderer(NULL),
_slidBallDisabledRenderer(NULL),
_slidBallRenderer(NULL),
_barLength(0.0),
_percent(0),
_scale9Enabled(false),
_prevIgnoreSize(true),
_textureFile(""),
_progressBarTextureFile(""),
_slidBallNormalTextureFile(""),
_slidBallPressedTextureFile(""),
_slidBallDisabledTextureFile(""),
_capInsetsBarRenderer(Rect::ZERO),
_capInsetsProgressBarRenderer(Rect::ZERO),
_slidPercentListener(NULL),
_slidPercentSelector(NULL),
_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()
{
}
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-09-16 20:54:13 +08:00
_barRenderer = CCSprite::create();
_progressBarRenderer = CCSprite::create();
_progressBarRenderer->setAnchorPoint(Point(0.0f, 0.5f));
_renderer->addChild(_barRenderer, -1);
_renderer->addChild(_progressBarRenderer, -1);
_slidBallNormalRenderer = CCSprite::create();
_slidBallPressedRenderer = CCSprite::create();
_slidBallPressedRenderer->setVisible(false);
_slidBallDisabledRenderer = CCSprite::create();
_slidBallDisabledRenderer->setVisible(false);
_slidBallRenderer = CCNode::create();
_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-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_barRenderer)->initWithFile(fileName);
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
dynamic_cast<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-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_barRenderer)->initWithSpriteFrameName(fileName);
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
dynamic_cast<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-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_barRenderer)->setColor(getColor());
dynamic_cast<Scale9Sprite*>(_barRenderer)->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
dynamic_cast<Sprite*>(_barRenderer)->setColor(getColor());
dynamic_cast<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-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_progressBarRenderer)->initWithFile(fileName);
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
dynamic_cast<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-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_progressBarRenderer)->initWithSpriteFrameName(fileName);
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
dynamic_cast<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-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_progressBarRenderer)->setColor(getColor());
dynamic_cast<Scale9Sprite*>(_progressBarRenderer)->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
dynamic_cast<Sprite*>(_progressBarRenderer)->setColor(getColor());
dynamic_cast<Sprite*>(_progressBarRenderer)->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
_progressBarRenderer->setAnchorPoint(Point(0.0f, 0.5f));
_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-09-16 20:54:13 +08:00
_barRenderer = Scale9Sprite::create();
_progressBarRenderer = Scale9Sprite::create();
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
_barRenderer = CCSprite::create();
_progressBarRenderer = CCSprite::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
}
}
void UISlider::setCapInsets(const Rect &capInsets)
{
setCapInsetsBarRenderer(capInsets);
setCapInsetProgressBarRebderer(capInsets);
}
void UISlider::setCapInsetsBarRenderer(const Rect &capInsets)
{
2013-09-16 20:54:13 +08:00
_capInsetsBarRenderer = capInsets;
if (!_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_barRenderer)->setCapInsets(capInsets);
2013-09-13 22:20:20 +08:00
}
void UISlider::setCapInsetProgressBarRebderer(const Rect &capInsets)
{
2013-09-16 20:54:13 +08:00
_capInsetsProgressBarRenderer = capInsets;
if (!_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
dynamic_cast<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);
_slidBallRenderer->setPosition(Point(-_barLength/2.0f + dis, 0.0f));
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_progressBarRenderer)->setPreferredSize(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-09-16 20:54:13 +08:00
Sprite* barNode = dynamic_cast<Sprite*>(_progressBarRenderer);
2013-09-13 22:20:20 +08:00
if (barNode)
{
Point to = barNode->getTextureRect().origin;
x = to.x;
y = to.y;
}
break;
}
default:
break;
}
2013-09-16 20:54:13 +08:00
dynamic_cast<Sprite*>(_progressBarRenderer)->setTextureRect(Rect(x, y, _progressBarTextureSize.width * (percent/100.0f), _progressBarTextureSize.height));
2013-09-13 22:20:20 +08:00
}
}
bool UISlider::onTouchBegan(const Point &touchPoint)
{
bool pass = UIWidget::onTouchBegan(touchPoint);
2013-09-16 15:32:52 +08:00
Point nsp = _renderer->convertToNodeSpace(touchPoint);
2013-09-13 22:20:20 +08:00
setPercent(getPercentWithBallPos(nsp.x));
percentChangedEvent();
return pass;
}
void UISlider::onTouchMoved(const Point &touchPoint)
{
2013-09-16 15:32:52 +08:00
Point nsp = _renderer->convertToNodeSpace(touchPoint);
2013-09-16 20:54:13 +08:00
_slidBallRenderer->setPosition(Point(nsp.x,0));
2013-09-13 22:20:20 +08:00
setPercent(getPercentWithBallPos(nsp.x));
percentChangedEvent();
}
void UISlider::onTouchEnded(const Point &touchPoint)
{
UIWidget::onTouchEnded(touchPoint);
}
void UISlider::onTouchCancelled(const Point &touchPoint)
{
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
}
void UISlider::addPercentEvent(Object *target, SEL_SlidPercentChangedEvent selector)
{
2013-09-16 20:54:13 +08:00
_slidPercentListener = target;
_slidPercentSelector = selector;
2013-09-13 22:20:20 +08:00
}
void UISlider::percentChangedEvent()
{
2013-09-16 20:54:13 +08:00
if (_slidPercentListener && _slidPercentSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
(_slidPercentListener->*_slidPercentSelector)(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();
}
const Size& UISlider::getContentSize() const
{
2013-09-16 20:54:13 +08:00
return _barRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
}
Node* UISlider::getVirtualRenderer()
{
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-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_barRenderer)->setPreferredSize(_size);
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
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-09-16 20:54:13 +08:00
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-09-16 20:54:13 +08:00
dynamic_cast<Scale9Sprite*>(_progressBarRenderer)->setPreferredSize(_size);
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
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-09-16 20:54:13 +08:00
_progressBarRenderer->setPosition(Point(-_barLength * 0.5f, 0.0f));
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
}
NS_CC_EXT_END