2020-10-18 12:31:45 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
2022-07-09 22:23:34 +08:00
|
|
|
* https://axis-project.github.io/
|
2020-10-18 12:31:45 +08:00
|
|
|
*
|
|
|
|
* Copyright 2011 Yannick Loriot.
|
|
|
|
* http://yannickloriot.com
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
2020-10-18 12:31:45 +08:00
|
|
|
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
2020-10-18 12:31:45 +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:
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
2020-10-18 12:31:45 +08:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
2020-10-18 12:31:45 +08:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Converted to c++ / cocos2d-x by Angus C
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
2020-10-18 12:31:45 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CCControlSlider.h"
|
|
|
|
#include "base/CCTouch.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_EXT_BEGIN
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
ControlSlider::ControlSlider()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _value(0.0f)
|
|
|
|
, _minimumValue(0.0f)
|
|
|
|
, _maximumValue(0.0f)
|
|
|
|
, _minimumAllowedValue(0.0f)
|
|
|
|
, _maximumAllowedValue(0.0f)
|
|
|
|
, _thumbSprite(nullptr)
|
|
|
|
, _selectedThumbSprite(nullptr)
|
|
|
|
, _progressSprite(nullptr)
|
|
|
|
, _backgroundSprite(nullptr)
|
|
|
|
{}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
ControlSlider::~ControlSlider()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_thumbSprite);
|
|
|
|
AX_SAFE_RELEASE(_selectedThumbSprite);
|
|
|
|
AX_SAFE_RELEASE(_progressSprite);
|
|
|
|
AX_SAFE_RELEASE(_backgroundSprite);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ControlSlider* ControlSlider::create(const char* bgFile, const char* progressFile, const char* thumbFile)
|
|
|
|
{
|
|
|
|
// Prepare background for slider
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* backgroundSprite = Sprite::create(bgFile);
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
// Prepare progress for slider
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* progressSprite = Sprite::create(progressFile);
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
// Prepare thumb (menuItem) for slider
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* thumbSprite = Sprite::create(thumbFile);
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
return ControlSlider::create(backgroundSprite, progressSprite, thumbSprite);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ControlSlider* ControlSlider::create(const char* bgFile,
|
|
|
|
const char* progressFile,
|
|
|
|
const char* thumbFile,
|
|
|
|
const char* selectedThumbSpriteFile)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
// Prepare background for slider
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* backgroundSprite = Sprite::create(bgFile);
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// Prepare progress for slider
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* progressSprite = Sprite::create(progressFile);
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// Prepare thumb (menuItem) for slider
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* thumbSprite = Sprite::create(thumbFile);
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// Prepare selected thumb (menuItem) for slider
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* selectedThumbSprite = Sprite::create(selectedThumbSpriteFile);
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
return ControlSlider::create(backgroundSprite, progressSprite, thumbSprite, selectedThumbSprite);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ControlSlider* ControlSlider::create(Sprite* backgroundSprite, Sprite* pogressSprite, Sprite* thumbSprite)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ControlSlider* pRet = new ControlSlider();
|
2020-10-18 12:31:45 +08:00
|
|
|
pRet->initWithSprites(backgroundSprite, pogressSprite, thumbSprite);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ControlSlider* ControlSlider::create(Sprite* backgroundSprite,
|
|
|
|
Sprite* pogressSprite,
|
|
|
|
Sprite* thumbSprite,
|
|
|
|
Sprite* selectedThumbSprite)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ControlSlider* pRet = new ControlSlider();
|
2020-10-18 12:31:45 +08:00
|
|
|
pRet->initWithSprites(backgroundSprite, pogressSprite, thumbSprite, selectedThumbSprite);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ControlSlider::initWithSprites(Sprite* backgroundSprite, Sprite* progressSprite, Sprite* thumbSprite)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* selectedThumbSprite = Sprite::createWithTexture(thumbSprite->getTexture(), thumbSprite->getTextureRect());
|
2020-10-18 12:31:45 +08:00
|
|
|
selectedThumbSprite->setColor(Color3B::GRAY);
|
|
|
|
return this->initWithSprites(backgroundSprite, progressSprite, thumbSprite, selectedThumbSprite);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ControlSlider::initWithSprites(Sprite* backgroundSprite,
|
|
|
|
Sprite* progressSprite,
|
|
|
|
Sprite* thumbSprite,
|
|
|
|
Sprite* selectedThumbSprite)
|
|
|
|
{
|
|
|
|
if (Control::init())
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(backgroundSprite, "Background sprite must be not nil");
|
|
|
|
AXASSERT(progressSprite, "Progress sprite must be not nil");
|
|
|
|
AXASSERT(thumbSprite, "Thumb sprite must be not nil");
|
|
|
|
AXASSERT(selectedThumbSprite, "Thumb sprite must be not nil");
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
setIgnoreAnchorPointForPosition(false);
|
|
|
|
|
|
|
|
this->setBackgroundSprite(backgroundSprite);
|
|
|
|
this->setProgressSprite(progressSprite);
|
|
|
|
this->setThumbSprite(thumbSprite);
|
|
|
|
this->setSelectedThumbSprite(selectedThumbSprite);
|
|
|
|
|
|
|
|
// Defines the content size
|
2021-12-25 10:04:45 +08:00
|
|
|
Rect maxRect = ControlUtils::RectUnion(backgroundSprite->getBoundingBox(), thumbSprite->getBoundingBox());
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
setContentSize(Size(maxRect.size.width, maxRect.size.height));
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// Add the slider background
|
|
|
|
_backgroundSprite->setAnchorPoint(Vec2(0.5f, 0.5f));
|
|
|
|
_backgroundSprite->setPosition(this->getContentSize().width / 2, this->getContentSize().height / 2);
|
|
|
|
addChild(_backgroundSprite);
|
|
|
|
|
|
|
|
// Add the progress bar
|
|
|
|
_progressSprite->setAnchorPoint(Vec2(0.0f, 0.5f));
|
|
|
|
_progressSprite->setPosition(0.0f, this->getContentSize().height / 2);
|
|
|
|
addChild(_progressSprite);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// Add the slider thumb
|
2020-10-18 12:31:45 +08:00
|
|
|
_thumbSprite->setPosition(0.0f, this->getContentSize().height / 2);
|
|
|
|
addChild(_thumbSprite);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_selectedThumbSprite->setPosition(0.0f, this->getContentSize().height / 2);
|
|
|
|
_selectedThumbSprite->setVisible(false);
|
|
|
|
addChild(_selectedThumbSprite);
|
|
|
|
|
|
|
|
// Init default values
|
2021-12-25 10:04:45 +08:00
|
|
|
_minimumValue = 0.0f;
|
|
|
|
_maximumValue = 1.0f;
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
setValue(_minimumValue);
|
|
|
|
return true;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
void ControlSlider::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
Control::setEnabled(enabled);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_thumbSprite != nullptr)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
_thumbSprite->setOpacity((enabled) ? 255 : 128);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ControlSlider::setValue(float value)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// set new value with sentinel
|
|
|
|
if (value < _minimumValue)
|
|
|
|
{
|
|
|
|
value = _minimumValue;
|
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (value > _maximumValue)
|
|
|
|
{
|
|
|
|
value = _maximumValue;
|
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_value = value;
|
|
|
|
|
|
|
|
this->needsLayout();
|
|
|
|
|
|
|
|
this->sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ControlSlider::setMinimumValue(float minimumValue)
|
|
|
|
{
|
|
|
|
_minimumValue = minimumValue;
|
|
|
|
_minimumAllowedValue = minimumValue;
|
|
|
|
if (_minimumValue >= _maximumValue)
|
|
|
|
{
|
|
|
|
_maximumValue = _minimumValue + 1.0f;
|
|
|
|
}
|
|
|
|
setValue(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlSlider::setMaximumValue(float maximumValue)
|
|
|
|
{
|
|
|
|
_maximumValue = maximumValue;
|
|
|
|
_maximumAllowedValue = maximumValue;
|
|
|
|
if (_maximumValue <= _minimumValue)
|
|
|
|
{
|
|
|
|
_minimumValue = _maximumValue - 1.0f;
|
|
|
|
}
|
|
|
|
setValue(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlSlider::isTouchInside(Touch* touch)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 touchLocation = touch->getLocation();
|
|
|
|
touchLocation = this->getParent()->convertToNodeSpace(touchLocation);
|
|
|
|
|
|
|
|
Rect rect = this->getBoundingBox();
|
|
|
|
rect.size.width += _thumbSprite->getContentSize().width;
|
|
|
|
rect.origin.x -= _thumbSprite->getContentSize().width / 2;
|
|
|
|
|
|
|
|
return rect.containsPoint(touchLocation);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 ControlSlider::locationFromTouch(Touch* touch)
|
|
|
|
{
|
|
|
|
Vec2 touchLocation = touch->getLocation(); // Get the touch position
|
|
|
|
touchLocation = this->convertToNodeSpace(touchLocation); // Convert to the node space of this class
|
|
|
|
|
|
|
|
if (touchLocation.x < 0)
|
|
|
|
{
|
|
|
|
touchLocation.x = 0;
|
|
|
|
}
|
|
|
|
else if (touchLocation.x > _backgroundSprite->getContentSize().width)
|
|
|
|
{
|
|
|
|
touchLocation.x = _backgroundSprite->getContentSize().width;
|
|
|
|
}
|
|
|
|
|
|
|
|
return touchLocation;
|
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
bool ControlSlider::onTouchBegan(Touch* touch, Event* /*pEvent*/)
|
|
|
|
{
|
|
|
|
if (!isTouchInside(touch) || !isEnabled() || !isVisible())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 location = locationFromTouch(touch);
|
|
|
|
sliderBegan(location);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ControlSlider::onTouchMoved(Touch* pTouch, Event* /*pEvent*/)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
Vec2 location = locationFromTouch(pTouch);
|
|
|
|
sliderMoved(location);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlSlider::onTouchEnded(Touch* /*pTouch*/, Event* /*pEvent*/)
|
|
|
|
{
|
|
|
|
sliderEnded(Vec2::ZERO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlSlider::needsLayout()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (nullptr == _thumbSprite || nullptr == _selectedThumbSprite || nullptr == _backgroundSprite ||
|
|
|
|
nullptr == _progressSprite)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Update thumb position for new value
|
2021-12-25 10:04:45 +08:00
|
|
|
float percent = (_value - _minimumValue) / (_maximumValue - _minimumValue);
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 pos = _thumbSprite->getPosition();
|
|
|
|
pos.x = percent * _backgroundSprite->getContentSize().width;
|
2020-10-18 12:31:45 +08:00
|
|
|
_thumbSprite->setPosition(pos);
|
|
|
|
_selectedThumbSprite->setPosition(pos);
|
|
|
|
|
|
|
|
// Stretches content proportional to newLevel
|
2021-12-25 10:04:45 +08:00
|
|
|
Rect textureRect = _progressSprite->getTextureRect();
|
|
|
|
textureRect = Rect(textureRect.origin.x, textureRect.origin.y, pos.x, textureRect.size.height);
|
2020-10-18 12:31:45 +08:00
|
|
|
_progressSprite->setTextureRect(textureRect, _progressSprite->isTextureRectRotated(), textureRect.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlSlider::sliderBegan(Vec2 location)
|
|
|
|
{
|
|
|
|
this->setSelected(true);
|
|
|
|
_thumbSprite->setVisible(false);
|
|
|
|
_selectedThumbSprite->setVisible(true);
|
|
|
|
setValue(valueForLocation(location));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlSlider::sliderMoved(Vec2 location)
|
|
|
|
{
|
|
|
|
setValue(valueForLocation(location));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlSlider::sliderEnded(Vec2 /*location*/)
|
|
|
|
{
|
|
|
|
if (this->isSelected())
|
|
|
|
{
|
|
|
|
setValue(valueForLocation(_thumbSprite->getPosition()));
|
|
|
|
}
|
|
|
|
_thumbSprite->setVisible(true);
|
|
|
|
_selectedThumbSprite->setVisible(false);
|
|
|
|
this->setSelected(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
float ControlSlider::valueForLocation(Vec2 location)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float percent = location.x / _backgroundSprite->getContentSize().width;
|
|
|
|
return MAX(MIN(_minimumValue + percent * (_maximumValue - _minimumValue), _maximumAllowedValue),
|
|
|
|
_minimumAllowedValue);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_EXT_END
|