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 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 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 "CCControlPotentiometer.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_EXT_BEGIN
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
ControlPotentiometer::ControlPotentiometer()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _value(0.0f), _minimumValue(0.0f), _maximumValue(0.0f), _thumbSprite(nullptr), _progressTimer(nullptr)
|
|
|
|
{}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
ControlPotentiometer::~ControlPotentiometer()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_thumbSprite);
|
|
|
|
AX_SAFE_RELEASE(_progressTimer);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ControlPotentiometer* ControlPotentiometer::create(const char* backgroundFile,
|
|
|
|
const char* progressFile,
|
|
|
|
const char* thumbFile)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
ControlPotentiometer* pRet = new ControlPotentiometer();
|
|
|
|
// Prepare track for potentiometer
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* backgroundSprite = Sprite::create(backgroundFile);
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
// Prepare thumb for potentiometer
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* thumbSprite = Sprite::create(thumbFile);
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
// Prepare progress for potentiometer
|
2021-12-25 10:04:45 +08:00
|
|
|
ProgressTimer* progressTimer = ProgressTimer::create(Sprite::create(progressFile));
|
|
|
|
// progressTimer.type = ProgressTimer::RADIALCW;
|
2021-12-08 00:11:53 +08:00
|
|
|
if (pRet->initWithTrackSprite_ProgressTimer_ThumbSprite(backgroundSprite, progressTimer, thumbSprite))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ControlPotentiometer::initWithTrackSprite_ProgressTimer_ThumbSprite(Sprite* trackSprite,
|
|
|
|
ProgressTimer* progressTimer,
|
|
|
|
Sprite* thumbSprite)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
if (Control::init())
|
|
|
|
{
|
|
|
|
setProgressTimer(progressTimer);
|
|
|
|
setThumbSprite(thumbSprite);
|
|
|
|
thumbSprite->setPosition(progressTimer->getPosition());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
addChild(thumbSprite, 2);
|
|
|
|
addChild(progressTimer, 1);
|
|
|
|
addChild(trackSprite);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
setContentSize(trackSprite->getContentSize());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlPotentiometer::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
Control::setEnabled(enabled);
|
|
|
|
if (_thumbSprite != nullptr)
|
|
|
|
{
|
|
|
|
_thumbSprite->setOpacity((enabled) ? 255 : 128);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlPotentiometer::setValue(float value)
|
|
|
|
{
|
|
|
|
// set new value with sentinel
|
|
|
|
if (value < _minimumValue)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
value = _minimumValue;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (value > _maximumValue)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
value = _maximumValue;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_value = value;
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// Update thumb and progress 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
|
|
|
_progressTimer->setPercentage(percent * 100.0f);
|
|
|
|
_thumbSprite->setRotation(percent * 360.0f);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float ControlPotentiometer::getValue()
|
|
|
|
{
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlPotentiometer::setMinimumValue(float minimumValue)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_minimumValue = minimumValue;
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_minimumValue >= _maximumValue)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_maximumValue = _minimumValue + 1.0f;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
setValue(_maximumValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
float ControlPotentiometer::getMinimumValue()
|
|
|
|
{
|
|
|
|
return _minimumValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlPotentiometer::setMaximumValue(float maximumValue)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_maximumValue = maximumValue;
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (_maximumValue <= _minimumValue)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_minimumValue = _maximumValue - 1.0f;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
setValue(_minimumValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
float ControlPotentiometer::getMaximumValue()
|
|
|
|
{
|
|
|
|
return _maximumValue;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ControlPotentiometer::isTouchInside(Touch* touch)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 touchLocation = this->getTouchLocation(touch);
|
|
|
|
|
|
|
|
float distance = this->distanceBetweenPointAndPoint(_progressTimer->getPosition(), touchLocation);
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
return distance < MIN(getContentSize().width / 2, getContentSize().height / 2);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ControlPotentiometer::onTouchBegan(Touch* pTouch, Event* /*pEvent*/)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
if (!this->isTouchInside(pTouch) || !this->isEnabled() || !isVisible())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_previousLocation = this->getTouchLocation(pTouch);
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
this->potentiometerBegan(_previousLocation);
|
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 ControlPotentiometer::onTouchMoved(Touch* pTouch, Event* /*pEvent*/)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 location = this->getTouchLocation(pTouch);
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
this->potentiometerMoved(location);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlPotentiometer::onTouchEnded(Touch* /*pTouch*/, Event* /*pEvent*/)
|
|
|
|
{
|
|
|
|
this->potentiometerEnded(Vec2::ZERO);
|
|
|
|
}
|
|
|
|
|
|
|
|
float ControlPotentiometer::distanceBetweenPointAndPoint(Vec2 point1, Vec2 point2)
|
|
|
|
{
|
|
|
|
float dx = point1.x - point2.x;
|
|
|
|
float dy = point1.y - point2.y;
|
2021-12-25 10:04:45 +08:00
|
|
|
return sqrt(dx * dx + dy * dy);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float ControlPotentiometer::angleInDegreesBetweenLineFromPoint_toPoint_toLineFromPoint_toPoint(Vec2 beginLineA,
|
|
|
|
Vec2 endLineA,
|
|
|
|
Vec2 beginLineB,
|
|
|
|
Vec2 endLineB)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
float a = endLineA.x - beginLineA.x;
|
|
|
|
float b = endLineA.y - beginLineA.y;
|
|
|
|
float c = endLineB.x - beginLineB.x;
|
|
|
|
float d = endLineB.y - beginLineB.y;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
float atanA = atan2(a, b);
|
|
|
|
float atanB = atan2(c, d);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// convert radiants to degrees
|
|
|
|
return (atanA - atanB) * 180 / M_PI;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlPotentiometer::potentiometerBegan(Vec2 /*location*/)
|
|
|
|
{
|
|
|
|
setSelected(true);
|
|
|
|
getThumbSprite()->setColor(Color3B::GRAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlPotentiometer::potentiometerMoved(Vec2 location)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float angle = this->angleInDegreesBetweenLineFromPoint_toPoint_toLineFromPoint_toPoint(
|
|
|
|
_progressTimer->getPosition(), location, _progressTimer->getPosition(), _previousLocation);
|
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
// fix value, if the 12 o'clock position is between location and previousLocation
|
|
|
|
if (angle > 180)
|
|
|
|
{
|
|
|
|
angle -= 360;
|
|
|
|
}
|
|
|
|
else if (angle < -180)
|
|
|
|
{
|
|
|
|
angle += 360;
|
|
|
|
}
|
|
|
|
|
|
|
|
setValue(_value + angle / 360.0f * (_maximumValue - _minimumValue));
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_previousLocation = location;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlPotentiometer::potentiometerEnded(Vec2 /*location*/)
|
|
|
|
{
|
|
|
|
getThumbSprite()->setColor(Color3B::WHITE);
|
|
|
|
setSelected(false);
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_EXT_END
|