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-12-17 16:46:48 +08:00
|
|
|
#include "ui/UIHelper.h"
|
2014-06-04 14:26:21 +08:00
|
|
|
#include "2d/CCSprite.h"
|
2015-06-18 10:23:52 +08:00
|
|
|
#include "2d/CCCamera.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),
|
2015-01-20 20:57:54 +08:00
|
|
|
_barTextureSize(Size::ZERO),
|
2014-03-11 17:13:54 +08:00
|
|
|
_progressBarTextureSize(Size::ZERO),
|
|
|
|
_slidBallNormalRenderer(nullptr),
|
|
|
|
_slidBallPressedRenderer(nullptr),
|
|
|
|
_slidBallDisabledRenderer(nullptr),
|
|
|
|
_slidBallRenderer(nullptr),
|
|
|
|
_barLength(0.0),
|
|
|
|
_percent(0),
|
2015-06-15 18:31:21 +08:00
|
|
|
_maxPercent(100),
|
2014-03-11 17:13:54 +08:00
|
|
|
_scale9Enabled(false),
|
|
|
|
_prevIgnoreSize(true),
|
2014-12-23 15:03:31 +08:00
|
|
|
_zoomScale(0.1f),
|
|
|
|
_sliderBallNormalTextureScaleX(1.0),
|
|
|
|
_sliderBallNormalTextureScaleY(1.0),
|
2015-06-05 17:16:10 +08:00
|
|
|
_isSliderBallPressedTextureLoaded(false),
|
|
|
|
_isSliderBallDisabledTexturedLoaded(false),
|
2014-03-11 17:13:54 +08:00
|
|
|
_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;
|
|
|
|
}
|
2014-12-23 15:03:31 +08:00
|
|
|
|
|
|
|
Slider* Slider::create(const std::string& barTextureName,
|
|
|
|
const std::string& normalBallTextureName,
|
|
|
|
TextureResType resType)
|
|
|
|
{
|
|
|
|
Slider* widget = new (std::nothrow) Slider();
|
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
2015-08-27 23:34:39 +08:00
|
|
|
widget->loadBarTexture(barTextureName, resType);
|
|
|
|
widget->loadSlidBallTextureNormal(normalBallTextureName, resType);
|
2014-12-23 15:03:31 +08:00
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
_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;
|
|
|
|
}
|
2015-06-05 17:16:10 +08:00
|
|
|
this->setupBarTexture();
|
|
|
|
}
|
|
|
|
void Slider::loadBarTexture(SpriteFrame* spriteframe)
|
|
|
|
{
|
|
|
|
_barRenderer->initWithSpriteFrame(spriteframe);
|
|
|
|
this->setupBarTexture();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::setupBarTexture()
|
|
|
|
{
|
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());
|
2015-01-20 20:57:54 +08:00
|
|
|
_barTextureSize = _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;
|
|
|
|
}
|
|
|
|
_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;
|
|
|
|
}
|
2015-06-05 17:16:10 +08:00
|
|
|
this->setupProgressBarTexture();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::loadProgressBarTexture(SpriteFrame* spriteframe)
|
|
|
|
{
|
|
|
|
_progressBarRenderer->initWithSpriteFrame(spriteframe);
|
|
|
|
this->setupProgressBarTexture();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::setupProgressBarTexture()
|
|
|
|
{
|
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);
|
2015-01-20 17:24:04 +08:00
|
|
|
_barRendererAdaptDirty = true;
|
2015-01-20 20:57:54 +08:00
|
|
|
_progressBarRendererDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2014-12-17 16:46:48 +08:00
|
|
|
_capInsetsBarRenderer = ui::Helper::restrictCapInsetRect(capInsets, _barRenderer->getContentSize());
|
2014-03-11 17:13:54 +08:00
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-12-17 16:46:48 +08:00
|
|
|
_barRenderer->setCapInsets(_capInsetsBarRenderer);
|
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)
|
|
|
|
{
|
2014-12-17 16:46:48 +08:00
|
|
|
_capInsetsProgressBarRenderer = ui::Helper::restrictCapInsetRect(capInsets, _progressBarRenderer->getContentSize());
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-12-17 16:46:48 +08:00
|
|
|
_progressBarRenderer->setCapInsets(_capInsetsProgressBarRenderer);
|
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-12-16 16:44:04 +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;
|
|
|
|
}
|
|
|
|
_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
|
|
|
}
|
2015-06-05 17:16:10 +08:00
|
|
|
void Slider::loadSlidBallTextureNormal(SpriteFrame* spriteframe)
|
|
|
|
{
|
|
|
|
_slidBallNormalRenderer->setSpriteFrame(spriteframe);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
_ballPTexType = texType;
|
2015-06-05 17:16:10 +08:00
|
|
|
_isSliderBallPressedTextureLoaded = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-06-05 17:16:10 +08:00
|
|
|
|
|
|
|
void Slider::loadSlidBallTexturePressed(SpriteFrame* spriteframe)
|
|
|
|
{
|
|
|
|
_slidBallPressedRenderer->setSpriteFrame(spriteframe);
|
|
|
|
this->updateChildrenDisplayedRGBA();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-06-05 17:16:10 +08:00
|
|
|
_isSliderBallDisabledTexturedLoaded = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
_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
|
|
|
}
|
|
|
|
|
2015-06-05 17:16:10 +08:00
|
|
|
void Slider::loadSlidBallTextureDisabled(SpriteFrame* spriteframe)
|
|
|
|
{
|
|
|
|
_slidBallDisabledRenderer->setSpriteFrame(spriteframe);
|
|
|
|
this->updateChildrenDisplayedRGBA();
|
|
|
|
}
|
|
|
|
|
2015-06-15 18:31:21 +08:00
|
|
|
void Slider::setMaxPercent(int percent)
|
|
|
|
{
|
|
|
|
_maxPercent = percent;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Slider::getMaxPercent()const
|
|
|
|
{
|
|
|
|
return _maxPercent;
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void Slider::setPercent(int percent)
|
|
|
|
{
|
2015-06-15 18:31:21 +08:00
|
|
|
if (percent > _maxPercent)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-15 18:31:21 +08:00
|
|
|
percent = _maxPercent;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
if (percent < 0)
|
|
|
|
{
|
|
|
|
percent = 0;
|
|
|
|
}
|
|
|
|
_percent = percent;
|
2015-06-15 18:31:21 +08:00
|
|
|
float res = 1.0 * percent / _maxPercent;
|
2014-03-11 17:13:54 +08:00
|
|
|
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)
|
|
|
|
{
|
2015-01-22 15:53:33 +08:00
|
|
|
_progressBarRenderer->setPreferredSize(Size(dis,_contentSize.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
|
|
|
|
2015-06-18 10:23:52 +08:00
|
|
|
bool Slider::hitTest(const cocos2d::Vec2 &pt, const Camera *camera, Vec3 *p) const
|
2014-03-26 14:41:53 +08:00
|
|
|
{
|
2015-06-18 10:23:52 +08:00
|
|
|
Rect rect;
|
|
|
|
rect.size = _slidBallNormalRenderer->getContentSize();
|
|
|
|
auto w2l = _slidBallNormalRenderer->getWorldToNodeTransform();
|
2015-09-15 14:13:52 +08:00
|
|
|
|
|
|
|
Rect sliderBarRect;
|
|
|
|
sliderBarRect.size = this->_barRenderer->getContentSize();
|
|
|
|
auto barW2l = this->_barRenderer->getWorldToNodeTransform();
|
|
|
|
return isScreenPointInRect(pt, camera, w2l, rect, nullptr) || isScreenPointInRect(pt, camera, barW2l, sliderBarRect, nullptr);
|
2014-03-26 14:41:53 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
bool Slider::onTouchBegan(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
bool pass = Widget::onTouchBegan(touch, unusedEvent);
|
|
|
|
if (_hitted)
|
|
|
|
{
|
2015-06-18 10:23:52 +08:00
|
|
|
setPercent(getPercentWithBallPos(_touchBeganPosition));
|
2015-06-16 09:42:06 +08:00
|
|
|
percentChangedEvent(EventType::ON_SLIDEBALL_DOWN);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::onTouchMoved(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
_touchMovePosition = touch->getLocation();
|
2015-06-18 10:23:52 +08:00
|
|
|
setPercent(getPercentWithBallPos(_touchMovePosition));
|
2015-06-15 18:31:21 +08:00
|
|
|
percentChangedEvent(EventType::ON_PERCENTAGE_CHANGED);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::onTouchEnded(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
Widget::onTouchEnded(touch, unusedEvent);
|
2015-06-16 09:42:06 +08:00
|
|
|
percentChangedEvent(EventType::ON_SLIDEBALL_UP);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::onTouchCancelled(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
Widget::onTouchCancelled(touch, unusedEvent);
|
2015-06-16 09:42:06 +08:00
|
|
|
percentChangedEvent(EventType::ON_SLIDEBALL_CANCEL);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-06-18 10:23:52 +08:00
|
|
|
float Slider::getPercentWithBallPos(const Vec2 &pt) const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-18 10:23:52 +08:00
|
|
|
Vec3 p;
|
|
|
|
Widget::hitTest(pt, _hittedByCamera, &p);
|
2015-06-30 14:32:14 +08:00
|
|
|
return ((p.x/_barLength) * static_cast<float>(_maxPercent));
|
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
|
|
|
|
2015-06-15 18:31:21 +08:00
|
|
|
void Slider::percentChangedEvent(EventType event)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
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-12-16 16:44:04 +08:00
|
|
|
if (_eventCallback)
|
|
|
|
{
|
2015-06-15 18:31:21 +08:00
|
|
|
_eventCallback(this,event);
|
2014-05-12 11:17:06 +08:00
|
|
|
}
|
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);
|
2015-01-20 20:57:54 +08:00
|
|
|
_barRenderer->setScale(1.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-20 20:57:54 +08:00
|
|
|
Size btextureSize = _barTextureSize;
|
2014-03-11 17:13:54 +08:00
|
|
|
if (btextureSize.width <= 0.0f || btextureSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_barRenderer->setScale(1.0f);
|
|
|
|
}
|
2015-07-10 17:00:16 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
float bscaleX = _contentSize.width / btextureSize.width;
|
|
|
|
float bscaleY = _contentSize.height / btextureSize.height;
|
|
|
|
_barRenderer->setScaleX(bscaleX);
|
|
|
|
_barRenderer->setScaleY(bscaleY);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
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);
|
2015-01-21 13:41:40 +08:00
|
|
|
_progressBarRenderer->setScale(1.0);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
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);
|
2014-12-23 15:03:31 +08:00
|
|
|
|
|
|
|
_slidBallNormalRenderer->setGLProgramState(this->getNormalGLProgramState());
|
|
|
|
_slidBallNormalRenderer->setScale(_sliderBallNormalTextureScaleX, _sliderBallNormalTextureScaleY);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::onPressStateChangedToPressed()
|
|
|
|
{
|
2014-12-23 15:03:31 +08:00
|
|
|
_slidBallNormalRenderer->setGLProgramState(this->getNormalGLProgramState());
|
|
|
|
|
|
|
|
|
2015-06-05 17:16:10 +08:00
|
|
|
if (!_isSliderBallPressedTextureLoaded)
|
2014-12-23 15:03:31 +08:00
|
|
|
{
|
|
|
|
_slidBallNormalRenderer->setScale(_sliderBallNormalTextureScaleX + _zoomScale,
|
|
|
|
_sliderBallNormalTextureScaleY + _zoomScale);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_slidBallNormalRenderer->setVisible(false);
|
|
|
|
_slidBallPressedRenderer->setVisible(true);
|
|
|
|
_slidBallDisabledRenderer->setVisible(false);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::onPressStateChangedToDisabled()
|
|
|
|
{
|
2015-06-05 17:16:10 +08:00
|
|
|
if (!_isSliderBallDisabledTexturedLoaded)
|
2014-12-23 15:03:31 +08:00
|
|
|
{
|
|
|
|
_slidBallNormalRenderer->setGLProgramState(this->getGrayGLProgramState());
|
2015-07-02 16:45:17 +08:00
|
|
|
_slidBallNormalRenderer->setVisible(true);
|
2014-12-23 15:03:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_slidBallNormalRenderer->setVisible(false);
|
|
|
|
_slidBallDisabledRenderer->setVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
_slidBallNormalRenderer->setScale(_sliderBallNormalTextureScaleX, _sliderBallNormalTextureScaleY);
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_slidBallPressedRenderer->setVisible(false);
|
|
|
|
}
|
2014-12-23 15:03:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
void Slider::setZoomScale(float scale)
|
|
|
|
{
|
|
|
|
_zoomScale = scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Slider::getZoomScale()const
|
|
|
|
{
|
|
|
|
return _zoomScale;
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
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);
|
2015-06-05 17:16:10 +08:00
|
|
|
auto barSprite = slider->_barRenderer->getSprite();
|
|
|
|
if (nullptr != barSprite)
|
|
|
|
{
|
|
|
|
loadBarTexture(barSprite->getSpriteFrame());
|
|
|
|
}
|
|
|
|
auto progressSprite = slider->_progressBarRenderer->getSprite();
|
|
|
|
if (nullptr != progressSprite)
|
|
|
|
{
|
|
|
|
loadProgressBarTexture(progressSprite->getSpriteFrame());
|
|
|
|
}
|
|
|
|
loadSlidBallTextureNormal(slider->_slidBallNormalRenderer->getSpriteFrame());
|
|
|
|
loadSlidBallTexturePressed(slider->_slidBallPressedRenderer->getSpriteFrame());
|
|
|
|
loadSlidBallTextureDisabled(slider->_slidBallDisabledRenderer->getSpriteFrame());
|
2014-03-11 17:13:54 +08:00
|
|
|
setPercent(slider->getPercent());
|
2015-06-15 18:31:21 +08:00
|
|
|
setMaxPercent(slider->getMaxPercent());
|
2015-06-05 17:16:10 +08:00
|
|
|
_isSliderBallPressedTextureLoaded = slider->_isSliderBallPressedTextureLoaded;
|
|
|
|
_isSliderBallDisabledTexturedLoaded = slider->_isSliderBallDisabledTexturedLoaded;
|
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
|