axmol/core/ui/UISlider.cpp

828 lines
23 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
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"
#include "ui/UIScale9Sprite.h"
#include "ui/UIHelper.h"
#include "2d/CCSprite.h"
#include "2d/CCCamera.h"
#include "renderer/ccShaders.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
/* FIXME:
Code could be simplified by using Sprite's setContentSize feature.
Instead of scaling the sprite, set call setContentSize both in scale9 and non-scale9 modes
*/
2021-12-25 10:04:45 +08:00
namespace ui
{
static const int BASEBAR_RENDERER_Z = (-2);
2019-11-23 20:27:39 +08:00
static const int PROGRESSBAR_RENDERER_Z = (-2);
2021-12-25 10:04:45 +08:00
static const int SLIDBALL_RENDERER_Z = (-1);
2019-11-23 20:27:39 +08:00
IMPLEMENT_CLASS_GUI_INFO(Slider)
2021-12-25 10:04:45 +08:00
Slider::Slider()
: _barRenderer(nullptr)
, _progressBarRenderer(nullptr)
, _barTextureSize(Vec2::ZERO)
, _originalBarRect(Rect::ZERO)
, _progressBarTextureSize(Vec2::ZERO)
, _originalProgressBarRect(Rect::ZERO)
, _slidBallNormalRenderer(nullptr)
, _slidBallPressedRenderer(nullptr)
, _slidBallDisabledRenderer(nullptr)
, _slidBallRenderer(nullptr)
, _barLength(0.0)
, _percent(0)
, _maxPercent(100)
, _scale9Enabled(false)
, _prevIgnoreSize(true)
, _zoomScale(0.1f)
, _sliderBallNormalTextureScaleX(1.0)
, _sliderBallNormalTextureScaleY(1.0)
, _isSliderBallPressedTextureLoaded(false)
, _isSliderBallDisabledTexturedLoaded(false)
, _capInsetsBarRenderer(Rect::ZERO)
, _capInsetsProgressBarRenderer(Rect::ZERO)
, _sliderEventListener(nullptr)
, _eventCallback(nullptr)
, _barTexType(TextureResType::LOCAL)
, _progressBarTexType(TextureResType::LOCAL)
, _ballNTexType(TextureResType::LOCAL)
, _ballPTexType(TextureResType::LOCAL)
, _ballDTexType(TextureResType::LOCAL)
, _barRendererAdaptDirty(true)
, _progressBarRendererDirty(true)
, _textureFile("")
, _progressBarTextureFile("")
, _slidBallNormalTextureFile("")
, _slidBallPressedTextureFile("")
, _slidBallDisabledTextureFile("")
2019-11-23 20:27:39 +08:00
{
setTouchEnabled(true);
}
Slider::~Slider()
{
_sliderEventListener = nullptr;
}
Slider* Slider::create()
{
2021-12-08 00:11:53 +08:00
Slider* widget = new Slider();
if (widget->init())
2019-11-23 20:27:39 +08:00
{
widget->autorelease();
return widget;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(widget);
2019-11-23 20:27:39 +08:00
return nullptr;
}
2021-12-25 10:04:45 +08:00
Slider* Slider::create(std::string_view barTextureName, std::string_view normalBallTextureName, TextureResType resType)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
Slider* widget = new Slider();
if (widget->init())
2019-11-23 20:27:39 +08:00
{
widget->loadBarTexture(barTextureName, resType);
widget->loadSlidBallTextureNormal(normalBallTextureName, resType);
widget->autorelease();
return widget;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(widget);
2019-11-23 20:27:39 +08:00
return nullptr;
}
bool Slider::init()
{
if (Widget::init())
{
return true;
}
return false;
}
void Slider::initRenderer()
{
2021-12-25 10:04:45 +08:00
_barRenderer = Scale9Sprite::create();
2019-11-23 20:27:39 +08:00
_progressBarRenderer = Scale9Sprite::create();
_barRenderer->setScale9Enabled(false);
_progressBarRenderer->setScale9Enabled(false);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_progressBarRenderer->setAnchorPoint(Vec2(0.0f, 0.5f));
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
addProtectedChild(_barRenderer, BASEBAR_RENDERER_Z, -1);
addProtectedChild(_progressBarRenderer, PROGRESSBAR_RENDERER_Z, -1);
2021-12-25 10:04:45 +08:00
_slidBallNormalRenderer = Sprite::create();
2019-11-23 20:27:39 +08:00
_slidBallPressedRenderer = Sprite::create();
_slidBallPressedRenderer->setVisible(false);
_slidBallDisabledRenderer = Sprite::create();
_slidBallDisabledRenderer->setVisible(false);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_slidBallRenderer = Node::create();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_slidBallRenderer->addChild(_slidBallNormalRenderer);
_slidBallRenderer->addChild(_slidBallPressedRenderer);
_slidBallRenderer->addChild(_slidBallDisabledRenderer);
_slidBallRenderer->setCascadeColorEnabled(true);
_slidBallRenderer->setCascadeOpacityEnabled(true);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
addProtectedChild(_slidBallRenderer, SLIDBALL_RENDERER_Z, -1);
}
void Slider::loadBarTexture(std::string_view fileName, TextureResType texType)
2019-11-23 20:27:39 +08:00
{
_textureFile = fileName;
2021-12-25 10:04:45 +08:00
_barTexType = texType;
2019-11-23 20:27:39 +08:00
if (fileName.empty())
{
_barRenderer->resetRender();
}
else
{
switch (_barTexType)
{
case TextureResType::LOCAL:
_barRenderer->setTexture(fileName);
break;
case TextureResType::PLIST:
_barRenderer->setSpriteFrame(fileName);
break;
default:
break;
}
}
2021-12-25 10:04:45 +08:00
// FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249
if (!_ignoreSize && _customSize.equals(Vec2::ZERO))
{
2019-11-23 20:27:39 +08:00
_customSize = _barRenderer->getContentSize();
}
this->setupBarTexture();
}
void Slider::loadBarTexture(SpriteFrame* spriteframe)
{
_barRenderer->setSpriteFrame(spriteframe);
this->setupBarTexture();
}
void Slider::setupBarTexture()
{
this->updateChildrenDisplayedRGBA();
2021-12-25 10:04:45 +08:00
_barRendererAdaptDirty = true;
2019-11-23 20:27:39 +08:00
_progressBarRendererDirty = true;
updateContentSizeWithTextureSize(_barRenderer->getContentSize());
2021-12-25 10:04:45 +08:00
_barTextureSize = _barRenderer->getContentSize();
2019-11-23 20:27:39 +08:00
_originalBarRect = _barRenderer->getTextureRect();
}
void Slider::loadProgressBarTexture(std::string_view fileName, TextureResType texType)
2019-11-23 20:27:39 +08:00
{
_progressBarTextureFile = fileName;
2021-12-25 10:04:45 +08:00
_progressBarTexType = texType;
2019-11-23 20:27:39 +08:00
if (fileName.empty())
{
_progressBarRenderer->resetRender();
}
else
{
switch (_progressBarTexType)
{
case TextureResType::LOCAL:
_progressBarRenderer->setTexture(fileName);
break;
case TextureResType::PLIST:
_progressBarRenderer->setSpriteFrame(fileName);
break;
default:
break;
}
}
this->setupProgressBarTexture();
}
void Slider::loadProgressBarTexture(SpriteFrame* spriteframe)
{
_progressBarRenderer->setSpriteFrame(spriteframe);
this->setupProgressBarTexture();
}
void Slider::setupProgressBarTexture()
{
this->updateChildrenDisplayedRGBA();
_progressBarRenderer->setAnchorPoint(Vec2(0.0f, 0.5f));
2021-12-25 10:04:45 +08:00
_progressBarTextureSize = _progressBarRenderer->getContentSize();
_originalProgressBarRect = _progressBarRenderer->getTextureRect();
2019-11-23 20:27:39 +08:00
_progressBarRendererDirty = true;
}
void Slider::setScale9Enabled(bool able)
{
if (_scale9Enabled == able)
{
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_scale9Enabled = able;
_barRenderer->setScale9Enabled(_scale9Enabled);
_progressBarRenderer->setScale9Enabled(_scale9Enabled);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_scale9Enabled)
{
bool ignoreBefore = _ignoreSize;
ignoreContentAdaptWithSize(false);
_prevIgnoreSize = ignoreBefore;
}
else
{
ignoreContentAdaptWithSize(_prevIgnoreSize);
}
setCapInsetsBarRenderer(_capInsetsBarRenderer);
setCapInsetProgressBarRenderer(_capInsetsProgressBarRenderer);
2021-12-25 10:04:45 +08:00
_barRendererAdaptDirty = true;
2019-11-23 20:27:39 +08:00
_progressBarRendererDirty = true;
}
2021-12-25 10:04:45 +08:00
bool Slider::isScale9Enabled() const
2019-11-23 20:27:39 +08:00
{
return _scale9Enabled;
}
void Slider::ignoreContentAdaptWithSize(bool ignore)
{
if (!_scale9Enabled || (_scale9Enabled && !ignore))
{
Widget::ignoreContentAdaptWithSize(ignore);
_prevIgnoreSize = ignore;
}
}
2021-12-25 10:04:45 +08:00
void Slider::setCapInsets(const Rect& capInsets)
2019-11-23 20:27:39 +08:00
{
setCapInsetsBarRenderer(capInsets);
setCapInsetProgressBarRenderer(capInsets);
}
2021-12-25 10:04:45 +08:00
void Slider::setCapInsetsBarRenderer(const Rect& capInsets)
2019-11-23 20:27:39 +08:00
{
_capInsetsBarRenderer = ui::Helper::restrictCapInsetRect(capInsets, _barRenderer->getContentSize());
if (!_scale9Enabled)
{
return;
}
// textureRect should be restored in order to calculate the scale9 correctly
// https://github.com/cocos2d/cocos2d-x/issues/16928
_barRenderer->setTextureRect(_originalBarRect, _barRenderer->isTextureRectRotated(), _barTextureSize);
_barRenderer->setCapInsets(_capInsetsBarRenderer);
}
2021-12-25 10:04:45 +08:00
const Rect& Slider::getCapInsetsBarRenderer() const
2019-11-23 20:27:39 +08:00
{
return _capInsetsBarRenderer;
}
2021-12-25 10:04:45 +08:00
void Slider::setCapInsetProgressBarRenderer(const Rect& capInsets)
2019-11-23 20:27:39 +08:00
{
_capInsetsProgressBarRenderer = ui::Helper::restrictCapInsetRect(capInsets, _progressBarRenderer->getContentSize());
if (!_scale9Enabled)
{
return;
}
// textureRect should be restored in order to calculate the scale9 correctly
// https://github.com/cocos2d/cocos2d-x/issues/16928
2021-12-25 10:04:45 +08:00
_progressBarRenderer->setTextureRect(_originalProgressBarRect, _progressBarRenderer->isTextureRectRotated(),
_progressBarTextureSize);
2019-11-23 20:27:39 +08:00
_progressBarRenderer->setCapInsets(_capInsetsProgressBarRenderer);
}
2021-12-25 10:04:45 +08:00
const Rect& Slider::getCapInsetsProgressBarRenderer() const
2019-11-23 20:27:39 +08:00
{
return _capInsetsProgressBarRenderer;
}
void Slider::loadSlidBallTextures(std::string_view normal,
std::string_view pressed,
std::string_view disabled,
2021-12-25 10:04:45 +08:00
TextureResType texType)
2019-11-23 20:27:39 +08:00
{
loadSlidBallTextureNormal(normal, texType);
2021-12-25 10:04:45 +08:00
loadSlidBallTexturePressed(pressed, texType);
loadSlidBallTextureDisabled(disabled, texType);
2019-11-23 20:27:39 +08:00
}
void Slider::loadSlidBallTextureNormal(std::string_view normal, TextureResType texType)
2019-11-23 20:27:39 +08:00
{
_slidBallNormalTextureFile = normal;
2021-12-25 10:04:45 +08:00
_ballNTexType = texType;
2019-11-23 20:27:39 +08:00
if (normal.empty())
{
_slidBallNormalRenderer->init();
}
else
{
switch (_ballNTexType)
{
case TextureResType::LOCAL:
_slidBallNormalRenderer->setTexture(normal);
break;
case TextureResType::PLIST:
_slidBallNormalRenderer->setSpriteFrame(normal);
break;
default:
break;
}
}
this->updateChildrenDisplayedRGBA();
}
void Slider::loadSlidBallTextureNormal(SpriteFrame* spriteframe)
{
_slidBallNormalRenderer->setSpriteFrame(spriteframe);
this->updateChildrenDisplayedRGBA();
}
void Slider::loadSlidBallTexturePressed(std::string_view pressed, TextureResType texType)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_slidBallPressedTextureFile = pressed;
2019-11-23 20:27:39 +08:00
_isSliderBallPressedTextureLoaded = !pressed.empty();
2021-12-25 10:04:45 +08:00
_ballPTexType = texType;
2019-11-23 20:27:39 +08:00
if (pressed.empty())
{
_slidBallPressedRenderer->init();
}
else
{
switch (_ballPTexType)
{
case TextureResType::LOCAL:
_slidBallPressedRenderer->setTexture(pressed);
break;
case TextureResType::PLIST:
_slidBallPressedRenderer->setSpriteFrame(pressed);
break;
default:
break;
}
}
this->updateChildrenDisplayedRGBA();
}
void Slider::loadSlidBallTexturePressed(SpriteFrame* spriteframe)
{
_slidBallPressedRenderer->setSpriteFrame(spriteframe);
this->updateChildrenDisplayedRGBA();
}
void Slider::loadSlidBallTextureDisabled(std::string_view disabled, TextureResType texType)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_slidBallDisabledTextureFile = disabled;
2019-11-23 20:27:39 +08:00
_isSliderBallDisabledTexturedLoaded = !disabled.empty();
2021-12-25 10:04:45 +08:00
_ballDTexType = texType;
2019-11-23 20:27:39 +08:00
if (disabled.empty())
{
_slidBallDisabledRenderer->init();
}
else
{
switch (_ballDTexType)
{
case TextureResType::LOCAL:
_slidBallDisabledRenderer->setTexture(disabled);
break;
case TextureResType::PLIST:
_slidBallDisabledRenderer->setSpriteFrame(disabled);
break;
default:
break;
}
}
this->updateChildrenDisplayedRGBA();
}
void Slider::loadSlidBallTextureDisabled(SpriteFrame* spriteframe)
{
_slidBallDisabledRenderer->setSpriteFrame(spriteframe);
this->updateChildrenDisplayedRGBA();
}
void Slider::setMaxPercent(int percent)
{
_maxPercent = percent;
}
2021-12-25 10:04:45 +08:00
int Slider::getMaxPercent() const
2019-11-23 20:27:39 +08:00
{
return _maxPercent;
}
void Slider::setPercent(int percent)
{
if (percent > _maxPercent)
{
percent = _maxPercent;
}
if (percent < 0)
{
percent = 0;
}
// Only send event if value has changed
if (_percent != percent)
{
_percent = percent;
updateVisualSlider();
percentChangedEvent(EventType::ON_PERCENTAGE_CHANGED);
}
}
void Slider::updateVisualSlider()
{
float res;
if (_maxPercent > 0)
{
res = 1.0f * _percent / _maxPercent;
}
else
{
res = 0.f;
}
float dis = _barLength * res;
_slidBallRenderer->setPosition(dis, _contentSize.height / 2.0f);
if (_scale9Enabled)
{
2021-10-23 23:27:14 +08:00
_progressBarRenderer->setPreferredSize(Vec2(dis, _contentSize.height));
2019-11-23 20:27:39 +08:00
}
else
{
2021-12-25 10:04:45 +08:00
Rect rect = _progressBarRenderer->getTextureRect();
2019-11-23 20:27:39 +08:00
rect.size.width = _progressBarTextureSize.width * res;
_progressBarRenderer->setTextureRect(rect, _progressBarRenderer->isTextureRectRotated(), rect.size);
}
}
2021-12-25 10:04:45 +08:00
2022-08-08 18:02:17 +08:00
bool Slider::hitTest(const ax::Vec2& pt, const Camera* camera, Vec3* /*p*/) const
2019-11-23 20:27:39 +08:00
{
Rect rect;
rect.size = _slidBallNormalRenderer->getContentSize();
2021-12-25 10:04:45 +08:00
auto w2l = _slidBallNormalRenderer->getWorldToNodeTransform();
2019-11-23 20:27:39 +08:00
Rect sliderBarRect;
sliderBarRect.size = this->_barRenderer->getContentSize();
2021-12-25 10:04:45 +08:00
auto barW2l = this->_barRenderer->getWorldToNodeTransform();
return isScreenPointInRect(pt, camera, w2l, rect, nullptr) ||
isScreenPointInRect(pt, camera, barW2l, sliderBarRect, nullptr);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
bool Slider::onTouchBegan(Touch* touch, Event* unusedEvent)
2019-11-23 20:27:39 +08:00
{
bool pass = Widget::onTouchBegan(touch, unusedEvent);
if (_hitted)
{
setPercent(getPercentWithBallPos(_touchBeganPosition));
percentChangedEvent(EventType::ON_SLIDEBALL_DOWN);
}
return pass;
}
2021-12-25 10:04:45 +08:00
void Slider::onTouchMoved(Touch* touch, Event* /*unusedEvent*/)
2019-11-23 20:27:39 +08:00
{
_touchMovePosition = touch->getLocation();
setPercent(getPercentWithBallPos(_touchMovePosition));
}
2021-12-25 10:04:45 +08:00
void Slider::onTouchEnded(Touch* touch, Event* unusedEvent)
2019-11-23 20:27:39 +08:00
{
Widget::onTouchEnded(touch, unusedEvent);
percentChangedEvent(EventType::ON_SLIDEBALL_UP);
}
2021-12-25 10:04:45 +08:00
void Slider::onTouchCancelled(Touch* touch, Event* unusedEvent)
2019-11-23 20:27:39 +08:00
{
Widget::onTouchCancelled(touch, unusedEvent);
percentChangedEvent(EventType::ON_SLIDEBALL_CANCEL);
}
2021-12-25 10:04:45 +08:00
float Slider::getPercentWithBallPos(const Vec2& pt) const
2019-11-23 20:27:39 +08:00
{
Vec3 p;
Widget::hitTest(pt, _hittedByCamera, &p);
2021-12-25 10:04:45 +08:00
return ((p.x / _barLength) * static_cast<float>(_maxPercent));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Slider::addEventListener(const ccSliderCallback& callback)
{
_eventCallback = callback;
}
void Slider::percentChangedEvent(EventType event)
{
this->retain();
if (_eventCallback)
{
2021-12-25 10:04:45 +08:00
_eventCallback(this, event);
2019-11-23 20:27:39 +08:00
}
if (_ccEventCallback)
{
_ccEventCallback(this, static_cast<int>(EventType::ON_PERCENTAGE_CHANGED));
}
this->release();
}
2021-12-25 10:04:45 +08:00
int Slider::getPercent() const
2019-11-23 20:27:39 +08:00
{
return _percent;
}
void Slider::onSizeChanged()
{
Widget::onSizeChanged();
2021-12-25 10:04:45 +08:00
_barRendererAdaptDirty = true;
2019-11-23 20:27:39 +08:00
_progressBarRendererDirty = true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Slider::adaptRenderers()
{
if (_barRendererAdaptDirty)
{
barRendererScaleChangedWithSize();
_barRendererAdaptDirty = false;
}
if (_progressBarRendererDirty)
{
progressBarRendererScaleChangedWithSize();
_progressBarRendererDirty = false;
}
}
2021-10-23 23:27:14 +08:00
Vec2 Slider::getVirtualRendererSize() const
2019-11-23 20:27:39 +08:00
{
return _barRenderer->getContentSize();
}
Node* Slider::getVirtualRenderer()
{
return _barRenderer;
}
void Slider::barRendererScaleChangedWithSize()
{
if (_unifySize)
{
_barLength = _contentSize.width;
_barRenderer->setPreferredSize(_contentSize);
}
else if (_ignoreSize)
{
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_barRenderer->setScale(1.0f);
_barLength = _contentSize.width;
}
else
{
_barLength = _contentSize.width;
if (_scale9Enabled)
{
_barRenderer->setPreferredSize(_contentSize);
_barRenderer->setScale(1.0f);
}
else
{
2021-10-23 23:27:14 +08:00
Vec2 btextureSize = _barTextureSize;
2019-11-23 20:27:39 +08:00
if (btextureSize.width <= 0.0f || btextureSize.height <= 0.0f)
{
_barRenderer->setScale(1.0f);
}
else
{
float bscaleX = _contentSize.width / btextureSize.width;
float bscaleY = _contentSize.height / btextureSize.height;
_barRenderer->setScaleX(bscaleX);
_barRenderer->setScaleY(bscaleY);
}
}
}
_barRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
updateVisualSlider();
}
void Slider::progressBarRendererScaleChangedWithSize()
{
if (_unifySize)
{
_progressBarRenderer->setPreferredSize(_contentSize);
}
else if (_ignoreSize)
{
if (!_scale9Enabled)
{
2021-10-23 23:27:14 +08:00
Vec2 ptextureSize = _progressBarTextureSize;
2021-12-25 10:04:45 +08:00
float pscaleX = _contentSize.width / ptextureSize.width;
float pscaleY = _contentSize.height / ptextureSize.height;
2019-11-23 20:27:39 +08:00
_progressBarRenderer->setScaleX(pscaleX);
_progressBarRenderer->setScaleY(pscaleY);
}
}
else
{
if (_scale9Enabled)
{
_progressBarRenderer->setPreferredSize(_contentSize);
_progressBarRenderer->setScale(1.0);
}
else
{
2021-10-23 23:27:14 +08:00
Vec2 ptextureSize = _progressBarTextureSize;
2019-11-23 20:27:39 +08:00
if (ptextureSize.width <= 0.0f || ptextureSize.height <= 0.0f)
{
_progressBarRenderer->setScale(1.0f);
return;
}
float pscaleX = _contentSize.width / ptextureSize.width;
float pscaleY = _contentSize.height / ptextureSize.height;
_progressBarRenderer->setScaleX(pscaleX);
_progressBarRenderer->setScaleY(pscaleY);
}
}
_progressBarRenderer->setPosition(0.0f, _contentSize.height / 2.0f);
updateVisualSlider();
}
void Slider::onPressStateChangedToNormal()
{
_slidBallNormalRenderer->setVisible(true);
_slidBallPressedRenderer->setVisible(false);
_slidBallDisabledRenderer->setVisible(false);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_slidBallNormalRenderer->setProgramState(backend::ProgramType::POSITION_TEXTURE_COLOR);
_slidBallNormalRenderer->setScale(_sliderBallNormalTextureScaleX, _sliderBallNormalTextureScaleY);
}
void Slider::onPressStateChangedToPressed()
{
_slidBallNormalRenderer->setProgramState(backend::ProgramType::POSITION_TEXTURE_COLOR);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (!_isSliderBallPressedTextureLoaded)
{
_slidBallNormalRenderer->setScale(_sliderBallNormalTextureScaleX + _zoomScale,
_sliderBallNormalTextureScaleY + _zoomScale);
}
else
{
_slidBallNormalRenderer->setVisible(false);
_slidBallPressedRenderer->setVisible(true);
_slidBallDisabledRenderer->setVisible(false);
}
}
void Slider::onPressStateChangedToDisabled()
{
if (!_isSliderBallDisabledTexturedLoaded)
{
_slidBallNormalRenderer->setProgramState(backend::ProgramType::GRAY_SCALE);
_slidBallNormalRenderer->setVisible(true);
}
else
{
_slidBallNormalRenderer->setVisible(false);
_slidBallDisabledRenderer->setVisible(true);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_slidBallNormalRenderer->setScale(_sliderBallNormalTextureScaleX, _sliderBallNormalTextureScaleY);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_slidBallPressedRenderer->setVisible(false);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void Slider::setZoomScale(float scale)
{
_zoomScale = scale;
}
2021-12-25 10:04:45 +08:00
float Slider::getZoomScale() const
2019-11-23 20:27:39 +08:00
{
return _zoomScale;
}
std::string Slider::getDescription() const
{
return "Slider";
}
Widget* Slider::createCloneInstance()
{
return Slider::create();
}
2021-12-25 10:04:45 +08:00
void Slider::copySpecialProperties(Widget* widget)
2019-11-23 20:27:39 +08:00
{
Slider* slider = dynamic_cast<Slider*>(widget);
if (slider)
{
_prevIgnoreSize = slider->_prevIgnoreSize;
setScale9Enabled(slider->_scale9Enabled);
// clone the inner sprite: https://github.com/cocos2d/cocos2d-x/issues/16928
slider->_barRenderer->copyTo(_barRenderer);
setupBarTexture();
slider->_progressBarRenderer->copyTo(_progressBarRenderer);
setupProgressBarTexture();
loadSlidBallTextureNormal(slider->_slidBallNormalRenderer->getSpriteFrame());
loadSlidBallTexturePressed(slider->_slidBallPressedRenderer->getSpriteFrame());
loadSlidBallTextureDisabled(slider->_slidBallDisabledRenderer->getSpriteFrame());
setPercent(slider->getPercent());
setMaxPercent(slider->getMaxPercent());
2021-12-25 10:04:45 +08:00
_isSliderBallPressedTextureLoaded = slider->_isSliderBallPressedTextureLoaded;
2019-11-23 20:27:39 +08:00
_isSliderBallDisabledTexturedLoaded = slider->_isSliderBallDisabledTexturedLoaded;
2021-12-25 10:04:45 +08:00
_sliderEventListener = slider->_sliderEventListener;
_eventCallback = slider->_eventCallback;
_ccEventCallback = slider->_ccEventCallback;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
Sprite* Slider::getSlidBallNormalRenderer() const
{
2019-11-23 20:27:39 +08:00
return _slidBallNormalRenderer;
}
2021-12-25 10:04:45 +08:00
Sprite* Slider::getSlidBallPressedRenderer() const
{
2019-11-23 20:27:39 +08:00
return _slidBallPressedRenderer;
}
2021-12-25 10:04:45 +08:00
Sprite* Slider::getSlidBallDisabledRenderer() const
{
2019-11-23 20:27:39 +08:00
return _slidBallDisabledRenderer;
}
2021-12-25 10:04:45 +08:00
Node* Slider::getSlidBallRenderer() const
{
2019-11-23 20:27:39 +08:00
return _slidBallRenderer;
}
ResourceData Slider::getBackFile()
{
ResourceData rData;
rData.type = (int)_barTexType;
rData.file = _textureFile;
return rData;
}
ResourceData Slider::getProgressBarFile()
{
ResourceData rData;
rData.type = (int)_progressBarTexType;
rData.file = _progressBarTextureFile;
return rData;
}
ResourceData Slider::getBallNormalFile()
{
ResourceData rData;
rData.type = (int)_ballNTexType;
rData.file = _slidBallNormalTextureFile;
return rData;
}
ResourceData Slider::getBallPressedFile()
{
ResourceData rData;
rData.type = (int)_ballPTexType;
rData.file = _slidBallPressedTextureFile;
return rData;
}
ResourceData Slider::getBallDisabledFile()
{
ResourceData rData;
rData.type = (int)_ballDTexType;
rData.file = _slidBallDisabledTextureFile;
return rData;
}
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
NS_AX_END