2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
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 "ui/UISlider.h"
|
2014-07-29 14:57:45 +08:00
|
|
|
#include "ui/UIScale9Sprite.h"
|
2014-06-04 14:26:21 +08:00
|
|
|
#include "2d/CCSprite.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
static const int BASEBAR_RENDERER_Z = (-2);
|
|
|
|
static const int PROGRESSBAR_RENDERER_Z = (-2);
|
|
|
|
static const int SLIDBALL_RENDERER_Z = (-1);
|
|
|
|
|
|
|
|
IMPLEMENT_CLASS_GUI_INFO(Slider)
|
|
|
|
|
|
|
|
Slider::Slider():
|
|
|
|
_barRenderer(nullptr),
|
|
|
|
_progressBarRenderer(nullptr),
|
|
|
|
_progressBarTextureSize(Size::ZERO),
|
|
|
|
_slidBallNormalRenderer(nullptr),
|
|
|
|
_slidBallPressedRenderer(nullptr),
|
|
|
|
_slidBallDisabledRenderer(nullptr),
|
|
|
|
_slidBallRenderer(nullptr),
|
|
|
|
_barLength(0.0),
|
|
|
|
_percent(0),
|
|
|
|
_scale9Enabled(false),
|
|
|
|
_prevIgnoreSize(true),
|
|
|
|
_textureFile(""),
|
|
|
|
_progressBarTextureFile(""),
|
|
|
|
_slidBallNormalTextureFile(""),
|
|
|
|
_slidBallPressedTextureFile(""),
|
|
|
|
_slidBallDisabledTextureFile(""),
|
|
|
|
_capInsetsBarRenderer(Rect::ZERO),
|
|
|
|
_capInsetsProgressBarRenderer(Rect::ZERO),
|
|
|
|
_sliderEventListener(nullptr),
|
|
|
|
_sliderEventSelector(nullptr),
|
2014-10-30 22:25:46 +08:00
|
|
|
_eventCallback(nullptr),
|
2014-05-09 14:56:05 +08:00
|
|
|
_barTexType(TextureResType::LOCAL),
|
|
|
|
_progressBarTexType(TextureResType::LOCAL),
|
|
|
|
_ballNTexType(TextureResType::LOCAL),
|
|
|
|
_ballPTexType(TextureResType::LOCAL),
|
|
|
|
_ballDTexType(TextureResType::LOCAL),
|
2014-04-17 14:08:25 +08:00
|
|
|
_barRendererAdaptDirty(true),
|
2014-10-30 22:25:46 +08:00
|
|
|
_progressBarRendererDirty(true)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-24 15:51:14 +08:00
|
|
|
setTouchEnabled(true);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Slider::~Slider()
|
|
|
|
{
|
|
|
|
_sliderEventListener = nullptr;
|
|
|
|
_sliderEventSelector = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slider* Slider::create()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Slider* widget = new (std::nothrow) Slider();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Slider::init()
|
|
|
|
{
|
|
|
|
if (Widget::init())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::initRenderer()
|
|
|
|
{
|
2014-07-30 15:52:27 +08:00
|
|
|
_barRenderer = Scale9Sprite::create();
|
|
|
|
_progressBarRenderer = Scale9Sprite::create();
|
|
|
|
_barRenderer->setScale9Enabled(false);
|
|
|
|
_progressBarRenderer->setScale9Enabled(false);
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
_progressBarRenderer->setAnchorPoint(Vec2(0.0f, 0.5f));
|
2014-07-30 15:52:27 +08:00
|
|
|
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_barRenderer, BASEBAR_RENDERER_Z, -1);
|
|
|
|
addProtectedChild(_progressBarRenderer, PROGRESSBAR_RENDERER_Z, -1);
|
2014-07-30 15:52:27 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_slidBallNormalRenderer = Sprite::create();
|
|
|
|
_slidBallPressedRenderer = Sprite::create();
|
|
|
|
_slidBallPressedRenderer->setVisible(false);
|
|
|
|
_slidBallDisabledRenderer = Sprite::create();
|
|
|
|
_slidBallDisabledRenderer->setVisible(false);
|
2014-07-30 15:52:27 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_slidBallRenderer = Node::create();
|
2014-07-30 15:52:27 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_slidBallRenderer->addChild(_slidBallNormalRenderer);
|
|
|
|
_slidBallRenderer->addChild(_slidBallPressedRenderer);
|
|
|
|
_slidBallRenderer->addChild(_slidBallDisabledRenderer);
|
2014-07-03 14:08:00 +08:00
|
|
|
_slidBallRenderer->setCascadeColorEnabled(true);
|
2014-07-02 18:27:01 +08:00
|
|
|
_slidBallRenderer->setCascadeOpacityEnabled(true);
|
2014-08-06 07:53:00 +08:00
|
|
|
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_slidBallRenderer, SLIDBALL_RENDERER_Z, -1);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:33:57 +08:00
|
|
|
void Slider::loadBarTexture(const std::string& fileName, TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-03 15:33:57 +08:00
|
|
|
if (fileName.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_textureFile = fileName;
|
|
|
|
_barTexType = texType;
|
|
|
|
switch (_barTexType)
|
|
|
|
{
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::LOCAL:
|
2014-07-30 15:52:27 +08:00
|
|
|
_barRenderer->initWithFile(fileName);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::PLIST:
|
2014-07-30 15:52:27 +08:00
|
|
|
_barRenderer->initWithSpriteFrameName(fileName);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-08-11 15:04:00 +08:00
|
|
|
this->updateChildrenDisplayedRGBA();
|
2014-04-17 14:08:25 +08:00
|
|
|
_barRendererAdaptDirty = true;
|
|
|
|
_progressBarRendererDirty = true;
|
|
|
|
updateContentSizeWithTextureSize(_barRenderer->getContentSize());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:33:57 +08:00
|
|
|
void Slider::loadProgressBarTexture(const std::string& fileName, TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-03 15:33:57 +08:00
|
|
|
if (fileName.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_progressBarTextureFile = fileName;
|
|
|
|
_progressBarTexType = texType;
|
|
|
|
switch (_progressBarTexType)
|
|
|
|
{
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::LOCAL:
|
2014-07-30 15:52:27 +08:00
|
|
|
_progressBarRenderer->initWithFile(fileName);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::PLIST:
|
2014-07-30 15:52:27 +08:00
|
|
|
_progressBarRenderer->initWithSpriteFrameName(fileName);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-08-11 15:04:00 +08:00
|
|
|
this->updateChildrenDisplayedRGBA();
|
2014-05-15 01:07:09 +08:00
|
|
|
_progressBarRenderer->setAnchorPoint(Vec2(0.0f, 0.5f));
|
2014-03-11 17:13:54 +08:00
|
|
|
_progressBarTextureSize = _progressBarRenderer->getContentSize();
|
2014-04-17 14:08:25 +08:00
|
|
|
_progressBarRendererDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::setScale9Enabled(bool able)
|
|
|
|
{
|
|
|
|
if (_scale9Enabled == able)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_scale9Enabled = able;
|
2014-07-30 15:52:27 +08:00
|
|
|
_barRenderer->setScale9Enabled(_scale9Enabled);
|
|
|
|
_progressBarRenderer->setScale9Enabled(_scale9Enabled);
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
bool ignoreBefore = _ignoreSize;
|
|
|
|
ignoreContentAdaptWithSize(false);
|
|
|
|
_prevIgnoreSize = ignoreBefore;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ignoreContentAdaptWithSize(_prevIgnoreSize);
|
|
|
|
}
|
|
|
|
setCapInsetsBarRenderer(_capInsetsBarRenderer);
|
|
|
|
setCapInsetProgressBarRebderer(_capInsetsProgressBarRenderer);
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:52:30 +08:00
|
|
|
bool Slider::isScale9Enabled()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _scale9Enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::ignoreContentAdaptWithSize(bool ignore)
|
|
|
|
{
|
|
|
|
if (!_scale9Enabled || (_scale9Enabled && !ignore))
|
|
|
|
{
|
|
|
|
Widget::ignoreContentAdaptWithSize(ignore);
|
|
|
|
_prevIgnoreSize = ignore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::setCapInsets(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
setCapInsetsBarRenderer(capInsets);
|
|
|
|
setCapInsetProgressBarRebderer(capInsets);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::setCapInsetsBarRenderer(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
_capInsetsBarRenderer = capInsets;
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-30 15:52:27 +08:00
|
|
|
_barRenderer->setCapInsets(capInsets);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 10:52:30 +08:00
|
|
|
const Rect& Slider::getCapInsetsBarRenderer()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _capInsetsBarRenderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::setCapInsetProgressBarRebderer(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
_capInsetsProgressBarRenderer = capInsets;
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-30 15:52:27 +08:00
|
|
|
_progressBarRenderer->setCapInsets(capInsets);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 10:52:30 +08:00
|
|
|
const Rect& Slider::getCapInsetsProgressBarRebderer()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _capInsetsProgressBarRenderer;
|
|
|
|
}
|
|
|
|
|
2014-04-03 15:33:57 +08:00
|
|
|
void Slider::loadSlidBallTextures(const std::string& normal,const std::string& pressed,const std::string& disabled,TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
loadSlidBallTextureNormal(normal, texType);
|
|
|
|
loadSlidBallTexturePressed(pressed,texType);
|
|
|
|
loadSlidBallTextureDisabled(disabled,texType);
|
|
|
|
}
|
|
|
|
|
2014-04-03 15:33:57 +08:00
|
|
|
void Slider::loadSlidBallTextureNormal(const std::string& normal,TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-03 15:33:57 +08:00
|
|
|
if (normal.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_slidBallNormalTextureFile = normal;
|
|
|
|
_ballNTexType = texType;
|
|
|
|
switch (_ballNTexType)
|
|
|
|
{
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::LOCAL:
|
2014-03-11 17:13:54 +08:00
|
|
|
_slidBallNormalRenderer->setTexture(normal);
|
|
|
|
break;
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::PLIST:
|
2014-03-11 17:13:54 +08:00
|
|
|
_slidBallNormalRenderer->setSpriteFrame(normal);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-08-11 15:04:00 +08:00
|
|
|
this->updateChildrenDisplayedRGBA();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:33:57 +08:00
|
|
|
void Slider::loadSlidBallTexturePressed(const std::string& pressed,TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-03 15:33:57 +08:00
|
|
|
if (pressed.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_slidBallPressedTextureFile = pressed;
|
|
|
|
_ballPTexType = texType;
|
|
|
|
switch (_ballPTexType)
|
|
|
|
{
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::LOCAL:
|
2014-03-11 17:13:54 +08:00
|
|
|
_slidBallPressedRenderer->setTexture(pressed);
|
|
|
|
break;
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::PLIST:
|
2014-03-11 17:13:54 +08:00
|
|
|
_slidBallPressedRenderer->setSpriteFrame(pressed);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-08-11 15:04:00 +08:00
|
|
|
this->updateChildrenDisplayedRGBA();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:33:57 +08:00
|
|
|
void Slider::loadSlidBallTextureDisabled(const std::string& disabled,TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-03 15:33:57 +08:00
|
|
|
if (disabled.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_slidBallDisabledTextureFile = disabled;
|
|
|
|
_ballDTexType = texType;
|
|
|
|
switch (_ballDTexType)
|
|
|
|
{
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::LOCAL:
|
2014-03-11 17:13:54 +08:00
|
|
|
_slidBallDisabledRenderer->setTexture(disabled);
|
|
|
|
break;
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::PLIST:
|
2014-03-11 17:13:54 +08:00
|
|
|
_slidBallDisabledRenderer->setSpriteFrame(disabled);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-08-11 15:04:00 +08:00
|
|
|
this->updateChildrenDisplayedRGBA();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::setPercent(int percent)
|
|
|
|
{
|
|
|
|
if (percent > 100)
|
|
|
|
{
|
|
|
|
percent = 100;
|
|
|
|
}
|
|
|
|
if (percent < 0)
|
|
|
|
{
|
|
|
|
percent = 0;
|
|
|
|
}
|
|
|
|
_percent = percent;
|
|
|
|
float res = percent / 100.0f;
|
|
|
|
float dis = _barLength * res;
|
2014-08-28 11:41:18 +08:00
|
|
|
_slidBallRenderer->setPosition(dis, _contentSize.height / 2.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
2014-07-30 15:52:27 +08:00
|
|
|
_progressBarRenderer->setPreferredSize(Size(dis,_progressBarTextureSize.height));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-30 15:52:27 +08:00
|
|
|
Sprite* spriteRenderer = _progressBarRenderer->getSprite();
|
2014-07-30 16:55:16 +08:00
|
|
|
|
|
|
|
if (nullptr != spriteRenderer) {
|
|
|
|
Rect rect = spriteRenderer->getTextureRect();
|
|
|
|
rect.size.width = _progressBarTextureSize.width * res;
|
|
|
|
spriteRenderer->setTextureRect(rect, spriteRenderer->isTextureRectRotated(), rect.size);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-26 14:41:53 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool Slider::hitTest(const cocos2d::Vec2 &pt)
|
2014-03-26 14:41:53 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 nsp = this->_slidBallNormalRenderer->convertToNodeSpace(pt);
|
2014-04-17 10:15:58 +08:00
|
|
|
Size ballSize = this->_slidBallNormalRenderer->getContentSize();
|
|
|
|
Rect ballRect = Rect(0,0, ballSize.width, ballSize.height);
|
2014-03-26 14:41:53 +08:00
|
|
|
if (ballRect.containsPoint(nsp)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
bool Slider::onTouchBegan(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
bool pass = Widget::onTouchBegan(touch, unusedEvent);
|
|
|
|
if (_hitted)
|
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
Vec2 nsp = convertToNodeSpace(_touchBeganPosition);
|
2014-03-11 17:13:54 +08:00
|
|
|
setPercent(getPercentWithBallPos(nsp.x));
|
|
|
|
percentChangedEvent();
|
|
|
|
}
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::onTouchMoved(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
_touchMovePosition = touch->getLocation();
|
|
|
|
Vec2 nsp = convertToNodeSpace(_touchMovePosition);
|
2014-03-11 17:13:54 +08:00
|
|
|
setPercent(getPercentWithBallPos(nsp.x));
|
|
|
|
percentChangedEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::onTouchEnded(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
Widget::onTouchEnded(touch, unusedEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::onTouchCancelled(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
Widget::onTouchCancelled(touch, unusedEvent);
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:52:30 +08:00
|
|
|
float Slider::getPercentWithBallPos(float px)const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-17 14:08:25 +08:00
|
|
|
return ((px/_barLength)*100.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::addEventListenerSlider(Ref *target, SEL_SlidPercentChangedEvent selector)
|
|
|
|
{
|
|
|
|
_sliderEventListener = target;
|
|
|
|
_sliderEventSelector = selector;
|
|
|
|
}
|
2014-05-12 11:17:06 +08:00
|
|
|
|
2014-05-14 16:14:28 +08:00
|
|
|
void Slider::addEventListener(const ccSliderCallback& callback)
|
2014-05-12 11:17:06 +08:00
|
|
|
{
|
|
|
|
_eventCallback = callback;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void Slider::percentChangedEvent()
|
|
|
|
{
|
2014-08-01 20:58:42 +08:00
|
|
|
this->retain();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_sliderEventListener && _sliderEventSelector)
|
|
|
|
{
|
|
|
|
(_sliderEventListener->*_sliderEventSelector)(this,SLIDER_PERCENTCHANGED);
|
|
|
|
}
|
2014-05-12 11:17:06 +08:00
|
|
|
if (_eventCallback) {
|
|
|
|
_eventCallback(this, EventType::ON_PERCENTAGE_CHANGED);
|
|
|
|
}
|
2014-11-27 16:49:19 +08:00
|
|
|
if (_ccEventCallback)
|
|
|
|
{
|
|
|
|
_ccEventCallback(this, static_cast<int>(EventType::ON_PERCENTAGE_CHANGED));
|
|
|
|
}
|
2014-08-01 20:58:42 +08:00
|
|
|
this->release();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 10:52:30 +08:00
|
|
|
int Slider::getPercent()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _percent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::onSizeChanged()
|
|
|
|
{
|
|
|
|
Widget::onSizeChanged();
|
2014-04-17 14:08:25 +08:00
|
|
|
_barRendererAdaptDirty = true;
|
|
|
|
_progressBarRendererDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::adaptRenderers()
|
|
|
|
{
|
|
|
|
if (_barRendererAdaptDirty)
|
|
|
|
{
|
|
|
|
barRendererScaleChangedWithSize();
|
|
|
|
_barRendererAdaptDirty = false;
|
|
|
|
}
|
|
|
|
if (_progressBarRendererDirty)
|
|
|
|
{
|
|
|
|
progressBarRendererScaleChangedWithSize();
|
|
|
|
_progressBarRendererDirty = false;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-09-12 14:39:22 +08:00
|
|
|
Size Slider::getVirtualRendererSize() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _barRenderer->getContentSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* Slider::getVirtualRenderer()
|
|
|
|
{
|
|
|
|
return _barRenderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::barRendererScaleChangedWithSize()
|
|
|
|
{
|
2014-10-09 18:28:09 +08:00
|
|
|
if (_unifySize)
|
|
|
|
{
|
|
|
|
_barLength = _contentSize.width;
|
|
|
|
_barRenderer->setPreferredSize(_contentSize);
|
|
|
|
}
|
|
|
|
else if (_ignoreSize)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
_barRenderer->setScale(1.0f);
|
2014-06-20 10:40:16 +08:00
|
|
|
_barLength = _contentSize.width;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
_barLength = _contentSize.width;
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
2014-07-30 15:52:27 +08:00
|
|
|
_barRenderer->setPreferredSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Size btextureSize = _barRenderer->getContentSize();
|
|
|
|
if (btextureSize.width <= 0.0f || btextureSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_barRenderer->setScale(1.0f);
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
float bscaleX = _contentSize.width / btextureSize.width;
|
|
|
|
float bscaleY = _contentSize.height / btextureSize.height;
|
2014-03-11 17:13:54 +08:00
|
|
|
_barRenderer->setScaleX(bscaleX);
|
|
|
|
_barRenderer->setScaleY(bscaleY);
|
|
|
|
}
|
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
_barRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
setPercent(_percent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::progressBarRendererScaleChangedWithSize()
|
|
|
|
{
|
2014-10-09 18:28:09 +08:00
|
|
|
if (_unifySize)
|
|
|
|
{
|
|
|
|
_progressBarRenderer->setPreferredSize(_contentSize);
|
|
|
|
}
|
|
|
|
else if (_ignoreSize)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
Size ptextureSize = _progressBarTextureSize;
|
2014-06-20 10:40:16 +08:00
|
|
|
float pscaleX = _contentSize.width / ptextureSize.width;
|
|
|
|
float pscaleY = _contentSize.height / ptextureSize.height;
|
2014-03-11 17:13:54 +08:00
|
|
|
_progressBarRenderer->setScaleX(pscaleX);
|
|
|
|
_progressBarRenderer->setScaleY(pscaleY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
2014-07-30 15:52:27 +08:00
|
|
|
_progressBarRenderer->setPreferredSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
_progressBarTextureSize = _progressBarRenderer->getContentSize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Size ptextureSize = _progressBarTextureSize;
|
|
|
|
if (ptextureSize.width <= 0.0f || ptextureSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_progressBarRenderer->setScale(1.0f);
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
float pscaleX = _contentSize.width / ptextureSize.width;
|
|
|
|
float pscaleY = _contentSize.height / ptextureSize.height;
|
2014-03-11 17:13:54 +08:00
|
|
|
_progressBarRenderer->setScaleX(pscaleX);
|
|
|
|
_progressBarRenderer->setScaleY(pscaleY);
|
|
|
|
}
|
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
_progressBarRenderer->setPosition(0.0f, _contentSize.height / 2.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
setPercent(_percent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::onPressStateChangedToNormal()
|
|
|
|
{
|
|
|
|
_slidBallNormalRenderer->setVisible(true);
|
|
|
|
_slidBallPressedRenderer->setVisible(false);
|
|
|
|
_slidBallDisabledRenderer->setVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::onPressStateChangedToPressed()
|
|
|
|
{
|
|
|
|
_slidBallNormalRenderer->setVisible(false);
|
|
|
|
_slidBallPressedRenderer->setVisible(true);
|
|
|
|
_slidBallDisabledRenderer->setVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::onPressStateChangedToDisabled()
|
|
|
|
{
|
|
|
|
_slidBallNormalRenderer->setVisible(false);
|
|
|
|
_slidBallPressedRenderer->setVisible(false);
|
|
|
|
_slidBallDisabledRenderer->setVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Slider::getDescription() const
|
|
|
|
{
|
|
|
|
return "Slider";
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Slider::createCloneInstance()
|
|
|
|
{
|
|
|
|
return Slider::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::copySpecialProperties(Widget *widget)
|
|
|
|
{
|
|
|
|
Slider* slider = dynamic_cast<Slider*>(widget);
|
|
|
|
if (slider)
|
|
|
|
{
|
|
|
|
_prevIgnoreSize = slider->_prevIgnoreSize;
|
|
|
|
setScale9Enabled(slider->_scale9Enabled);
|
2014-04-03 15:33:57 +08:00
|
|
|
loadBarTexture(slider->_textureFile, slider->_barTexType);
|
|
|
|
loadProgressBarTexture(slider->_progressBarTextureFile, slider->_progressBarTexType);
|
|
|
|
loadSlidBallTextureNormal(slider->_slidBallNormalTextureFile, slider->_ballNTexType);
|
|
|
|
loadSlidBallTexturePressed(slider->_slidBallPressedTextureFile, slider->_ballPTexType);
|
|
|
|
loadSlidBallTextureDisabled(slider->_slidBallDisabledTextureFile, slider->_ballDTexType);
|
2014-03-11 17:13:54 +08:00
|
|
|
setPercent(slider->getPercent());
|
2014-05-23 15:03:46 +08:00
|
|
|
_sliderEventListener = slider->_sliderEventListener;
|
|
|
|
_sliderEventSelector = slider->_sliderEventSelector;
|
|
|
|
_eventCallback = slider->_eventCallback;
|
2014-11-27 16:49:19 +08:00
|
|
|
_ccEventCallback = slider->_ccEventCallback;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|