2012-09-25 16:57:51 +08:00
|
|
|
/*
|
2012-09-25 17:26:09 +08:00
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
|
|
|
* http://www.cocos2d-x.org
|
2012-09-25 16:57:51 +08:00
|
|
|
*
|
|
|
|
* Copyright 2012 Yannick Loriot. All rights reserved.
|
|
|
|
* http://yannickloriot.com
|
|
|
|
*
|
|
|
|
* 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 "CCControlPotentiometer.h"
|
|
|
|
#include "cocos2d.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlPotentiometer::ControlPotentiometer()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _thumbSprite(NULL)
|
|
|
|
, _progressTimer(NULL)
|
|
|
|
, _value(0.0f)
|
|
|
|
, _minimumValue(0.0f)
|
|
|
|
, _maximumValue(0.0f)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlPotentiometer::~ControlPotentiometer()
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_thumbSprite);
|
|
|
|
CC_SAFE_RELEASE(_progressTimer);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlPotentiometer* ControlPotentiometer::create(const char* backgroundFile, const char* progressFile, const char* thumbFile)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlPotentiometer* pRet = new ControlPotentiometer();
|
2012-09-25 16:57:51 +08:00
|
|
|
if (pRet != NULL)
|
|
|
|
{
|
|
|
|
// Prepare track for potentiometer
|
2013-06-20 14:15:53 +08:00
|
|
|
Sprite *backgroundSprite = Sprite::create(backgroundFile);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Prepare thumb for potentiometer
|
2013-06-20 14:15:53 +08:00
|
|
|
Sprite *thumbSprite = Sprite::create(thumbFile);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Prepare progress for potentiometer
|
2013-06-20 14:15:53 +08:00
|
|
|
ProgressTimer *progressTimer = ProgressTimer::create(Sprite::create(progressFile));
|
2013-07-25 15:11:36 +08:00
|
|
|
//progressTimer.type = ProgressTimer::RADIALCW;
|
2012-09-25 16:57:51 +08:00
|
|
|
if (pRet->initWithTrackSprite_ProgressTimer_ThumbSprite(backgroundSprite, progressTimer, thumbSprite))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlPotentiometer::initWithTrackSprite_ProgressTimer_ThumbSprite(Sprite* trackSprite, ProgressTimer* progressTimer, Sprite* thumbSprite)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if (Control::init())
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
|
|
|
setTouchEnabled(true);
|
|
|
|
|
|
|
|
setProgressTimer(progressTimer);
|
|
|
|
setThumbSprite(thumbSprite);
|
|
|
|
thumbSprite->setPosition(progressTimer->getPosition());
|
|
|
|
|
|
|
|
addChild(thumbSprite, 2);
|
|
|
|
addChild(progressTimer, 1);
|
|
|
|
addChild(trackSprite);
|
|
|
|
|
|
|
|
setContentSize(trackSprite->getContentSize());
|
|
|
|
|
|
|
|
// Init default values
|
2013-06-15 14:03:30 +08:00
|
|
|
_minimumValue = 0.0f;
|
|
|
|
_maximumValue = 1.0f;
|
|
|
|
setValue(_minimumValue);
|
2012-09-25 16:57:51 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlPotentiometer::setEnabled(bool enabled)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Control::setEnabled(enabled);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_thumbSprite != NULL)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_thumbSprite->setOpacity((enabled) ? 255 : 128);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlPotentiometer::setValue(float value)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
|
|
|
// set new value with sentinel
|
2013-06-15 14:03:30 +08:00
|
|
|
if (value < _minimumValue)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
value = _minimumValue;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (value > _maximumValue)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
value = _maximumValue;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_value = value;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Update thumb and progress position for new value
|
2013-06-15 14:03:30 +08:00
|
|
|
float percent = (value - _minimumValue) / (_maximumValue - _minimumValue);
|
|
|
|
_progressTimer->setPercentage(percent * 100.0f);
|
|
|
|
_thumbSprite->setRotation(percent * 360.0f);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float ControlPotentiometer::getValue()
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _value;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlPotentiometer::setMinimumValue(float minimumValue)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_minimumValue = minimumValue;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_minimumValue >= _maximumValue)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_maximumValue = _minimumValue + 1.0f;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
setValue(_maximumValue);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float ControlPotentiometer::getMinimumValue()
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _minimumValue;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlPotentiometer::setMaximumValue(float maximumValue)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_maximumValue = maximumValue;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_maximumValue <= _minimumValue)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_minimumValue = _maximumValue - 1.0f;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
setValue(_minimumValue);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float ControlPotentiometer::getMaximumValue()
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _maximumValue;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlPotentiometer::isTouchInside(Touch * touch)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point touchLocation = this->getTouchLocation(touch);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float distance = this->distanceBetweenPointAndPoint(_progressTimer->getPosition(), touchLocation);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
return distance < MIN(getContentSize().width / 2, getContentSize().height / 2);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlPotentiometer::ccTouchBegan(Touch *pTouch, Event *pEvent)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2012-10-30 23:10:05 +08:00
|
|
|
if (!this->isTouchInside(pTouch) || !this->isEnabled() || !isVisible())
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_previousLocation = this->getTouchLocation(pTouch);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
this->potentiometerBegan(_previousLocation);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlPotentiometer::ccTouchMoved(Touch *pTouch, Event *pEvent)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point location = this->getTouchLocation(pTouch);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
this->potentiometerMoved(location);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlPotentiometer::ccTouchEnded(Touch *pTouch, Event *pEvent)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
this->potentiometerEnded(Point::ZERO);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float ControlPotentiometer::distanceBetweenPointAndPoint(Point point1, Point point2)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
|
|
|
float dx = point1.x - point2.x;
|
|
|
|
float dy = point1.y - point2.y;
|
|
|
|
return sqrt(dx*dx + dy*dy);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float ControlPotentiometer::angleInDegreesBetweenLineFromPoint_toPoint_toLineFromPoint_toPoint(
|
|
|
|
Point beginLineA,
|
|
|
|
Point endLineA,
|
|
|
|
Point beginLineB,
|
|
|
|
Point endLineB)
|
2012-09-25 16:57:51 +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;
|
|
|
|
|
|
|
|
float atanA = atan2(a, b);
|
|
|
|
float atanB = atan2(c, d);
|
|
|
|
|
|
|
|
// convert radiants to degrees
|
|
|
|
return (atanA - atanB) * 180 / M_PI;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlPotentiometer::potentiometerBegan(Point location)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
|
|
|
setSelected(true);
|
2013-07-08 18:11:32 +08:00
|
|
|
getThumbSprite()->setColor(Color3B::GRAY);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlPotentiometer::potentiometerMoved(Point location)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
|
|
|
float angle = this->angleInDegreesBetweenLineFromPoint_toPoint_toLineFromPoint_toPoint(
|
2013-06-15 14:03:30 +08:00
|
|
|
_progressTimer->getPosition(),
|
2012-09-25 16:57:51 +08:00
|
|
|
location,
|
2013-06-15 14:03:30 +08:00
|
|
|
_progressTimer->getPosition(),
|
|
|
|
_previousLocation);
|
2012-09-25 16:57:51 +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;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
setValue(_value + angle / 360.0f * (_maximumValue - _minimumValue));
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_previousLocation = location;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlPotentiometer::potentiometerEnded(Point location)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-07-08 18:11:32 +08:00
|
|
|
getThumbSprite()->setColor(Color3B::WHITE);
|
2012-09-25 16:57:51 +08:00
|
|
|
setSelected(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|