2020-10-18 12:31:45 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
2022-07-10 09:47:41 +08:00
|
|
|
* https://axis-project.github.io/
|
2020-10-18 12:31:45 +08:00
|
|
|
*
|
|
|
|
* Copyright 2012 Yannick Loriot. All rights reserved.
|
|
|
|
* 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 falseT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND falseNINFRINGEMENT. IN false 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 "CCControlStepper.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_EXT_BEGIN
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#define ControlStepperLabelColorEnabled Color3B(55, 55, 55)
|
|
|
|
#define ControlStepperLabelColorDisabled Color3B(147, 147, 147)
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#define ControlStepperLabelFont "CourierNewPSMT"
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#define kAutorepeatDeltaTime 0.15f
|
|
|
|
#define kAutorepeatIncreaseTimeIncrement 12
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
ControlStepper::ControlStepper()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _value(0.0)
|
|
|
|
, _continuous(false)
|
|
|
|
, _autorepeat(false)
|
|
|
|
, _wraps(false)
|
|
|
|
, _minimumValue(0.0)
|
|
|
|
, _maximumValue(0.0)
|
|
|
|
, _stepValue(0.0)
|
|
|
|
, _touchInsideFlag(false)
|
|
|
|
, _touchedPart(Part::NONE)
|
|
|
|
, _autorepeatCount(0)
|
|
|
|
, _minusSprite(nullptr)
|
|
|
|
, _plusSprite(nullptr)
|
|
|
|
, _minusLabel(nullptr)
|
|
|
|
, _plusLabel(nullptr)
|
|
|
|
{}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
ControlStepper::~ControlStepper()
|
|
|
|
{
|
|
|
|
unscheduleAllCallbacks();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_minusSprite);
|
|
|
|
AX_SAFE_RELEASE(_plusSprite);
|
|
|
|
AX_SAFE_RELEASE(_minusLabel);
|
|
|
|
AX_SAFE_RELEASE(_plusLabel);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ControlStepper::initWithMinusSpriteAndPlusSprite(Sprite* minusSprite, Sprite* plusSprite)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
if (Control::init())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCASSERT(minusSprite, "Minus sprite must be not nil");
|
|
|
|
CCASSERT(plusSprite, "Plus sprite must be not nil");
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// Set the default values
|
2021-12-25 10:04:45 +08:00
|
|
|
_autorepeat = true;
|
|
|
|
_continuous = true;
|
|
|
|
_minimumValue = 0;
|
|
|
|
_maximumValue = 100;
|
|
|
|
_value = 0;
|
|
|
|
_stepValue = 1;
|
|
|
|
_wraps = false;
|
|
|
|
this->setIgnoreAnchorPointForPosition(false);
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// Add the minus components
|
|
|
|
this->setMinusSprite(minusSprite);
|
2021-12-25 10:04:45 +08:00
|
|
|
_minusSprite->setPosition(minusSprite->getContentSize().width / 2, minusSprite->getContentSize().height / 2);
|
|
|
|
this->addChild(_minusSprite);
|
|
|
|
|
|
|
|
this->setMinusLabel(Label::createWithSystemFont("-", ControlStepperLabelFont, 40));
|
2020-10-18 12:31:45 +08:00
|
|
|
_minusLabel->setColor(ControlStepperLabelColorDisabled);
|
|
|
|
_minusLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
|
|
|
_minusLabel->setPosition(_minusSprite->getContentSize().width / 2, _minusSprite->getContentSize().height / 2);
|
|
|
|
_minusSprite->addChild(_minusLabel);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// Add the plus components
|
|
|
|
this->setPlusSprite(plusSprite);
|
|
|
|
_plusSprite->setPosition(minusSprite->getContentSize().width + plusSprite->getContentSize().width / 2,
|
|
|
|
minusSprite->getContentSize().height / 2);
|
|
|
|
this->addChild(_plusSprite);
|
|
|
|
|
|
|
|
this->setPlusLabel(Label::createWithSystemFont("+", ControlStepperLabelFont, 40));
|
|
|
|
_plusLabel->setColor(ControlStepperLabelColorEnabled);
|
2020-10-18 12:31:45 +08:00
|
|
|
_plusLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
|
|
|
_plusLabel->setPosition(_plusSprite->getContentSize().width / 2, _plusSprite->getContentSize().height / 2);
|
|
|
|
_plusSprite->addChild(_plusLabel);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// Defines the content size
|
|
|
|
Rect maxRect = ControlUtils::RectUnion(_minusSprite->getBoundingBox(), _plusSprite->getBoundingBox());
|
2021-12-25 10:04:45 +08:00
|
|
|
this->setContentSize(
|
|
|
|
Size(_minusSprite->getContentSize().width + _plusSprite->getContentSize().height, maxRect.size.height));
|
2020-10-18 12:31:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ControlStepper* ControlStepper::create(Sprite* minusSprite, Sprite* plusSprite)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
ControlStepper* pRet = new ControlStepper();
|
|
|
|
if (pRet->initWithMinusSpriteAndPlusSprite(minusSprite, plusSprite))
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(pRet);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
//// Properties
|
|
|
|
|
|
|
|
void ControlStepper::setWraps(bool wraps)
|
|
|
|
{
|
|
|
|
_wraps = wraps;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_wraps)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_minusLabel->setColor(ControlStepperLabelColorEnabled);
|
|
|
|
_plusLabel->setColor(ControlStepperLabelColorEnabled);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
this->setValue(_value);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlStepper::setMinimumValue(double minimumValue)
|
|
|
|
{
|
|
|
|
if (minimumValue >= _maximumValue)
|
|
|
|
{
|
|
|
|
CCASSERT(0, "Must be numerically less than maximumValue.");
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_minimumValue = minimumValue;
|
|
|
|
this->setValue(_value);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlStepper::setMaximumValue(double maximumValue)
|
|
|
|
{
|
|
|
|
if (maximumValue <= _minimumValue)
|
|
|
|
{
|
|
|
|
CCASSERT(0, "Must be numerically greater than minimumValue.");
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_maximumValue = maximumValue;
|
2020-10-18 12:31:45 +08:00
|
|
|
this->setValue(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlStepper::setValue(double value)
|
|
|
|
{
|
|
|
|
this->setValueWithSendingEvent(value, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
double ControlStepper::getValue() const
|
|
|
|
{
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlStepper::setStepValue(double stepValue)
|
|
|
|
{
|
|
|
|
if (stepValue <= 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCASSERT(0, "Must be numerically greater than 0.");
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_stepValue = stepValue;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlStepper::isContinuous() const
|
|
|
|
{
|
|
|
|
return _continuous;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
//// ControlStepper Public Methods
|
|
|
|
|
|
|
|
void ControlStepper::setValueWithSendingEvent(double value, bool send)
|
|
|
|
{
|
|
|
|
if (value < _minimumValue)
|
|
|
|
{
|
|
|
|
value = _wraps ? _maximumValue : _minimumValue;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (value > _maximumValue)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
value = _wraps ? _minimumValue : _maximumValue;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_value = value;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (!_wraps)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_minusLabel->setColor((value == _minimumValue) ? ControlStepperLabelColorDisabled
|
|
|
|
: ControlStepperLabelColorEnabled);
|
|
|
|
_plusLabel->setColor((value == _maximumValue) ? ControlStepperLabelColorDisabled
|
|
|
|
: ControlStepperLabelColorEnabled);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (send)
|
|
|
|
{
|
|
|
|
this->sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlStepper::startAutorepeat()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_autorepeatCount = -1;
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
this->schedule(AX_SCHEDULE_SELECTOR(ControlStepper::update), kAutorepeatDeltaTime, AX_REPEAT_FOREVER,
|
2021-12-25 10:04:45 +08:00
|
|
|
kAutorepeatDeltaTime * 3);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Stop the autorepeat. */
|
|
|
|
void ControlStepper::stopAutorepeat()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
this->unschedule(AX_SCHEDULE_SELECTOR(ControlStepper::update));
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlStepper::update(float /*dt*/)
|
|
|
|
{
|
|
|
|
_autorepeatCount++;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if ((_autorepeatCount < kAutorepeatIncreaseTimeIncrement) && (_autorepeatCount % 3) != 0)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_touchedPart == Part::MINUS)
|
|
|
|
{
|
|
|
|
this->setValueWithSendingEvent(_value - _stepValue, _continuous);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (_touchedPart == Part::PLUS)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
this->setValueWithSendingEvent(_value + _stepValue, _continuous);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//// ControlStepper Private Methods
|
|
|
|
|
|
|
|
void ControlStepper::updateLayoutUsingTouchLocation(Vec2 location)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (location.x < _minusSprite->getContentSize().width && _value > _minimumValue)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_touchedPart = Part::MINUS;
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_minusSprite->setColor(Color3B::GRAY);
|
|
|
|
_plusSprite->setColor(Color3B::WHITE);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (location.x >= _minusSprite->getContentSize().width && _value < _maximumValue)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_touchedPart = Part::PLUS;
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_minusSprite->setColor(Color3B::WHITE);
|
|
|
|
_plusSprite->setColor(Color3B::GRAY);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_touchedPart = Part::NONE;
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_minusSprite->setColor(Color3B::WHITE);
|
|
|
|
_plusSprite->setColor(Color3B::WHITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ControlStepper::onTouchBegan(Touch* pTouch, Event* /*pEvent*/)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
if (!isTouchInside(pTouch) || !isEnabled() || !isVisible())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
Vec2 location = this->getTouchLocation(pTouch);
|
2020-10-18 12:31:45 +08:00
|
|
|
this->updateLayoutUsingTouchLocation(location);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_touchInsideFlag = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_autorepeat)
|
|
|
|
{
|
|
|
|
this->startAutorepeat();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ControlStepper::onTouchMoved(Touch* pTouch, Event* /*pEvent*/)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
if (this->isTouchInside(pTouch))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 location = this->getTouchLocation(pTouch);
|
2020-10-18 12:31:45 +08:00
|
|
|
this->updateLayoutUsingTouchLocation(location);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (!_touchInsideFlag)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_touchInsideFlag = true;
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_autorepeat)
|
|
|
|
{
|
|
|
|
this->startAutorepeat();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_touchInsideFlag = false;
|
|
|
|
|
|
|
|
_touchedPart = Part::NONE;
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_minusSprite->setColor(Color3B::WHITE);
|
|
|
|
_plusSprite->setColor(Color3B::WHITE);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_autorepeat)
|
|
|
|
{
|
|
|
|
this->stopAutorepeat();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ControlStepper::onTouchEnded(Touch* pTouch, Event* /*pEvent*/)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
_minusSprite->setColor(Color3B::WHITE);
|
|
|
|
_plusSprite->setColor(Color3B::WHITE);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_autorepeat)
|
|
|
|
{
|
|
|
|
this->stopAutorepeat();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (this->isTouchInside(pTouch))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 location = this->getTouchLocation(pTouch);
|
|
|
|
|
|
|
|
this->setValue(_value +
|
|
|
|
((location.x < _minusSprite->getContentSize().width) ? (0.0 - _stepValue) : _stepValue));
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_EXT_END
|