2013-09-13 22:20:20 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +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.
|
|
|
|
****************************************************************************/
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "gui/UISlider.h"
|
2013-10-16 11:19:01 +08:00
|
|
|
#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h"
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-02-24 18:56:45 +08:00
|
|
|
namespace ui {
|
2013-12-24 20:22:14 +08:00
|
|
|
|
2014-01-03 15:46:42 +08:00
|
|
|
static const int BASEBAR_RENDERER_Z = (-2);
|
|
|
|
static const int PROGRESSBAR_RENDERER_Z = (-2);
|
|
|
|
static const int SLIDBALL_RENDERER_Z = (-1);
|
2013-12-24 20:22:14 +08:00
|
|
|
|
2014-03-04 16:51:35 +08:00
|
|
|
IMPLEMENT_CLASS_GUI_INFO(Slider)
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Slider::Slider():
|
2013-11-14 11:37:46 +08:00
|
|
|
_barRenderer(nullptr),
|
|
|
|
_progressBarRenderer(nullptr),
|
2013-12-23 15:02:52 +08:00
|
|
|
_progressBarTextureSize(Size::ZERO),
|
2013-11-14 11:37:46 +08:00
|
|
|
_slidBallNormalRenderer(nullptr),
|
|
|
|
_slidBallPressedRenderer(nullptr),
|
|
|
|
_slidBallDisabledRenderer(nullptr),
|
|
|
|
_slidBallRenderer(nullptr),
|
2013-09-16 20:54:13 +08:00
|
|
|
_barLength(0.0),
|
|
|
|
_percent(0),
|
|
|
|
_scale9Enabled(false),
|
|
|
|
_prevIgnoreSize(true),
|
|
|
|
_textureFile(""),
|
|
|
|
_progressBarTextureFile(""),
|
|
|
|
_slidBallNormalTextureFile(""),
|
|
|
|
_slidBallPressedTextureFile(""),
|
|
|
|
_slidBallDisabledTextureFile(""),
|
2013-12-23 15:02:52 +08:00
|
|
|
_capInsetsBarRenderer(Rect::ZERO),
|
|
|
|
_capInsetsProgressBarRenderer(Rect::ZERO),
|
2013-11-14 11:37:46 +08:00
|
|
|
_sliderEventListener(nullptr),
|
|
|
|
_sliderEventSelector(nullptr),
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Slider::~Slider()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-14 11:37:46 +08:00
|
|
|
_sliderEventListener = nullptr;
|
|
|
|
_sliderEventSelector = nullptr;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Slider* Slider::create()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Slider* widget = new Slider();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
2013-11-14 11:37:46 +08:00
|
|
|
return nullptr;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2014-03-07 12:34:19 +08:00
|
|
|
bool Slider::init()
|
|
|
|
{
|
|
|
|
if (Widget::init())
|
|
|
|
{
|
|
|
|
setTouchEnabled(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::initRenderer()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_barRenderer = Sprite::create();
|
|
|
|
_progressBarRenderer = Sprite::create();
|
|
|
|
_progressBarRenderer->setAnchorPoint(Point(0.0f, 0.5f));
|
2014-01-03 15:46:42 +08:00
|
|
|
Node::addChild(_barRenderer, BASEBAR_RENDERER_Z, -1);
|
|
|
|
Node::addChild(_progressBarRenderer, PROGRESSBAR_RENDERER_Z, -1);
|
2013-12-23 15:02:52 +08:00
|
|
|
_slidBallNormalRenderer = Sprite::create();
|
|
|
|
_slidBallPressedRenderer = Sprite::create();
|
2013-09-16 20:54:13 +08:00
|
|
|
_slidBallPressedRenderer->setVisible(false);
|
2013-12-23 15:02:52 +08:00
|
|
|
_slidBallDisabledRenderer = Sprite::create();
|
2013-09-16 20:54:13 +08:00
|
|
|
_slidBallDisabledRenderer->setVisible(false);
|
2013-12-23 15:02:52 +08:00
|
|
|
_slidBallRenderer = Node::create();
|
2013-09-16 20:54:13 +08:00
|
|
|
_slidBallRenderer->addChild(_slidBallNormalRenderer);
|
|
|
|
_slidBallRenderer->addChild(_slidBallPressedRenderer);
|
|
|
|
_slidBallRenderer->addChild(_slidBallDisabledRenderer);
|
2014-01-03 15:46:42 +08:00
|
|
|
Node::addChild(_slidBallRenderer, SLIDBALL_RENDERER_Z, -1);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::loadBarTexture(const char* fileName, TextureResType texType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_barRenderer)->initWithFile(fileName);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
static_cast<Sprite*>(_barRenderer)->setTexture(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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_barRenderer)->initWithSpriteFrameName(fileName);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
static_cast<Sprite*>(_barRenderer)->setSpriteFrame(fileName);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-03 23:24:57 +08:00
|
|
|
updateRGBAToRenderer(_barRenderer);
|
2013-09-13 22:20:20 +08:00
|
|
|
barRendererScaleChangedWithSize();
|
2014-03-04 16:51:35 +08:00
|
|
|
progressBarRendererScaleChangedWithSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::loadProgressBarTexture(const char *fileName, TextureResType texType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_progressBarRenderer)->initWithFile(fileName);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
static_cast<Sprite*>(_progressBarRenderer)->setTexture(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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_progressBarRenderer)->initWithSpriteFrameName(fileName);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
static_cast<Sprite*>(_progressBarRenderer)->setSpriteFrame(fileName);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-03 23:24:57 +08:00
|
|
|
updateRGBAToRenderer(_progressBarRenderer);
|
2013-12-23 15:02:52 +08:00
|
|
|
_progressBarRenderer->setAnchorPoint(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();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::setScale9Enabled(bool able)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
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;
|
2013-12-24 20:22:14 +08:00
|
|
|
Node::removeChild(_barRenderer);
|
|
|
|
Node::removeChild(_progressBarRenderer);
|
2013-11-14 11:37:46 +08:00
|
|
|
_barRenderer = nullptr;
|
|
|
|
_progressBarRenderer = nullptr;
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_barRenderer = extension::Scale9Sprite::create();
|
|
|
|
_progressBarRenderer = extension::Scale9Sprite::create();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_barRenderer = Sprite::create();
|
|
|
|
_progressBarRenderer = 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);
|
2014-01-03 15:46:42 +08:00
|
|
|
Node::addChild(_barRenderer, BASEBAR_RENDERER_Z, -1);
|
|
|
|
Node::addChild(_progressBarRenderer, PROGRESSBAR_RENDERER_Z, -1);
|
2013-09-16 20:54:13 +08:00
|
|
|
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
|
|
|
}
|
2014-03-01 22:34:25 +08:00
|
|
|
|
|
|
|
bool Slider::isScale9Enabled()
|
|
|
|
{
|
|
|
|
return _scale9Enabled;
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::ignoreContentAdaptWithSize(bool ignore)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_scale9Enabled || (_scale9Enabled && !ignore))
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::ignoreContentAdaptWithSize(ignore);
|
2013-09-16 20:54:13 +08:00
|
|
|
_prevIgnoreSize = ignore;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::setCapInsets(const Rect &capInsets)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
setCapInsetsBarRenderer(capInsets);
|
|
|
|
setCapInsetProgressBarRebderer(capInsets);
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::setCapInsetsBarRenderer(const 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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_barRenderer)->setCapInsets(capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2014-03-01 22:34:25 +08:00
|
|
|
|
|
|
|
const Rect& Slider::getCapInsetsBarRenderer()
|
|
|
|
{
|
|
|
|
return _capInsetsBarRenderer;
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::setCapInsetProgressBarRebderer(const 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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_progressBarRenderer)->setCapInsets(capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2014-03-01 22:34:25 +08:00
|
|
|
|
|
|
|
const Rect& Slider::getCapInsetsProgressBarRebderer()
|
|
|
|
{
|
|
|
|
return _capInsetsProgressBarRenderer;
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::loadSlidBallTextures(const char* normal,const char* pressed,const char* disabled,TextureResType texType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
loadSlidBallTextureNormal(normal, texType);
|
|
|
|
loadSlidBallTexturePressed(pressed,texType);
|
|
|
|
loadSlidBallTextureDisabled(disabled,texType);
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::loadSlidBallTextureNormal(const char* normal,TextureResType texType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
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-11-14 07:55:36 +08:00
|
|
|
_slidBallNormalRenderer->setTexture(normal);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-11-14 07:55:36 +08:00
|
|
|
_slidBallNormalRenderer->setSpriteFrame(normal);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-03 23:24:57 +08:00
|
|
|
updateRGBAToRenderer(_slidBallNormalRenderer);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::loadSlidBallTexturePressed(const char* pressed,TextureResType texType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
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-11-14 07:55:36 +08:00
|
|
|
_slidBallPressedRenderer->setTexture(pressed);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-11-14 07:55:36 +08:00
|
|
|
_slidBallPressedRenderer->setSpriteFrame(pressed);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-03 23:24:57 +08:00
|
|
|
updateRGBAToRenderer(_slidBallPressedRenderer);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::loadSlidBallTextureDisabled(const char* disabled,TextureResType texType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
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-11-14 07:55:36 +08:00
|
|
|
_slidBallDisabledRenderer->setTexture(disabled);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-11-14 07:55:36 +08:00
|
|
|
_slidBallDisabledRenderer->setSpriteFrame(disabled);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-03 23:24:57 +08:00
|
|
|
updateRGBAToRenderer(_slidBallDisabledRenderer);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::setPercent(int percent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
if (percent > 100)
|
|
|
|
{
|
|
|
|
percent = 100;
|
|
|
|
}
|
|
|
|
if (percent < 0)
|
|
|
|
{
|
|
|
|
percent = 0;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
_percent = percent;
|
2013-12-27 17:58:00 +08:00
|
|
|
float res = percent / 100.0f;
|
|
|
|
float dis = _barLength * res;
|
2013-12-23 15:02:52 +08:00
|
|
|
_slidBallRenderer->setPosition(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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_progressBarRenderer)->setPreferredSize(Size(dis,_progressBarTextureSize.height));
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-27 17:58:00 +08:00
|
|
|
Sprite* spriteRenderer = static_cast<Sprite*>(_progressBarRenderer);
|
|
|
|
Rect rect = spriteRenderer->getTextureRect();
|
|
|
|
rect.size.width = _progressBarTextureSize.width * res;
|
|
|
|
spriteRenderer->setTextureRect(rect, spriteRenderer->isTextureRectRotated(), rect.size);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool Slider::onTouchBegan(Touch *touch, Event *unusedEvent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
bool pass = Widget::onTouchBegan(touch, unusedEvent);
|
|
|
|
if (_hitted)
|
|
|
|
{
|
|
|
|
Point nsp = convertToNodeSpace(_touchStartPos);
|
|
|
|
setPercent(getPercentWithBallPos(nsp.x));
|
|
|
|
percentChangedEvent();
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::onTouchMoved(Touch *touch, Event *unusedEvent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_touchMovePos = touch->getLocation();
|
|
|
|
Point nsp = convertToNodeSpace(_touchMovePos);
|
|
|
|
_slidBallRenderer->setPosition(Point(nsp.x,0));
|
2013-09-13 22:20:20 +08:00
|
|
|
setPercent(getPercentWithBallPos(nsp.x));
|
|
|
|
percentChangedEvent();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::onTouchEnded(Touch *touch, Event *unusedEvent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::onTouchEnded(touch, unusedEvent);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::onTouchCancelled(Touch *touch, Event *unusedEvent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::onTouchCancelled(touch, unusedEvent);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
float Slider::getPercentWithBallPos(float px)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return (((px-(-_barLength/2.0f))/_barLength)*100.0f);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void Slider::addEventListenerSlider(Ref *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
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::percentChangedEvent()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
int Slider::getPercent()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _percent;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::onSizeChanged()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::onSizeChanged();
|
2013-09-13 22:20:20 +08:00
|
|
|
barRendererScaleChangedWithSize();
|
|
|
|
progressBarRendererScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
const Size& Slider::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-12-23 15:02:52 +08:00
|
|
|
Node* Slider::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
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::barRendererScaleChangedWithSize()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_barRenderer)->setPreferredSize(_size);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +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
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::progressBarRendererScaleChangedWithSize()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
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-12-23 15:02:52 +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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_progressBarRenderer)->setPreferredSize(_size);
|
2014-03-04 16:51:35 +08:00
|
|
|
_progressBarTextureSize = _progressBarRenderer->getContentSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +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-12-23 15:02:52 +08:00
|
|
|
_progressBarRenderer->setPosition(Point(-_barLength * 0.5f, 0.0f));
|
2013-09-16 20:54:13 +08:00
|
|
|
setPercent(_percent);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::onPressStateChangedToNormal()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::onPressStateChangedToPressed()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::onPressStateChangedToDisabled()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
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
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
std::string Slider::getDescription() const
|
2013-09-17 17:59:20 +08:00
|
|
|
{
|
|
|
|
return "Slider";
|
|
|
|
}
|
2014-03-03 23:24:57 +08:00
|
|
|
|
|
|
|
void Slider::updateTextureColor()
|
|
|
|
{
|
|
|
|
updateColorToRenderer(_barRenderer);
|
|
|
|
updateColorToRenderer(_progressBarRenderer);
|
|
|
|
updateColorToRenderer(_slidBallNormalRenderer);
|
|
|
|
updateColorToRenderer(_slidBallPressedRenderer);
|
|
|
|
updateColorToRenderer(_slidBallDisabledRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::updateTextureOpacity()
|
|
|
|
{
|
|
|
|
updateOpacityToRenderer(_barRenderer);
|
|
|
|
updateOpacityToRenderer(_progressBarRenderer);
|
|
|
|
updateOpacityToRenderer(_slidBallNormalRenderer);
|
|
|
|
updateOpacityToRenderer(_slidBallPressedRenderer);
|
|
|
|
updateOpacityToRenderer(_slidBallDisabledRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::updateTextureRGBA()
|
|
|
|
{
|
|
|
|
updateRGBAToRenderer(_barRenderer);
|
|
|
|
updateRGBAToRenderer(_progressBarRenderer);
|
|
|
|
updateRGBAToRenderer(_slidBallNormalRenderer);
|
|
|
|
updateRGBAToRenderer(_slidBallPressedRenderer);
|
|
|
|
updateRGBAToRenderer(_slidBallDisabledRenderer);
|
|
|
|
}
|
2013-09-17 17:59:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* Slider::createCloneInstance()
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
return Slider::create();
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Slider::copySpecialProperties(Widget *widget)
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Slider* slider = dynamic_cast<Slider*>(widget);
|
2013-11-06 16:04:06 +08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
}
|
|
|
|
|
2014-02-24 18:56:45 +08:00
|
|
|
NS_CC_END
|