2012-04-19 14:35:52 +08:00
|
|
|
/*
|
2012-09-25 17:26:09 +08:00
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
|
|
|
* http://www.cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
*
|
|
|
|
* Copyright 2011 Yannick Loriot.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Converted to c++ / cocos2d-x by Angus C
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CCControlSlider.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "support/CCPointExtension.h"
|
|
|
|
#include "touch_dispatcher/CCTouch.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCDirector.h"
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_BEGIN
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
CCControlSlider::CCControlSlider()
|
|
|
|
: m_value(0.0f)
|
|
|
|
, m_minimumValue(0.0f)
|
|
|
|
, m_maximumValue(0.0f)
|
|
|
|
, m_minimumAllowedValue(0.0f)
|
|
|
|
, m_maximumAllowedValue(0.0f)
|
|
|
|
, m_thumbSprite(NULL)
|
|
|
|
, m_progressSprite(NULL)
|
|
|
|
, m_backgroundSprite(NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
CCControlSlider::~CCControlSlider()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(m_thumbSprite);
|
|
|
|
CC_SAFE_RELEASE(m_progressSprite);
|
|
|
|
CC_SAFE_RELEASE(m_backgroundSprite);
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCControlSlider* CCControlSlider::create(const char* bgFile, const char* progressFile, const char* thumbFile)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Prepare background for slider
|
2012-06-14 15:13:16 +08:00
|
|
|
CCSprite *backgroundSprite = CCSprite::create(bgFile);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Prepare progress for slider
|
2012-06-14 15:13:16 +08:00
|
|
|
CCSprite *progressSprite = CCSprite::create(progressFile);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Prepare thumb (menuItem) for slider
|
2012-09-25 16:57:51 +08:00
|
|
|
CCSprite *thumbSprite = CCSprite::create(thumbFile);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
return CCControlSlider::create(backgroundSprite, progressSprite, thumbSprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
CCControlSlider* CCControlSlider::create(CCSprite * backgroundSprite, CCSprite* pogressSprite, CCSprite* thumbSprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCControlSlider *pRet = new CCControlSlider();
|
2012-09-25 16:57:51 +08:00
|
|
|
pRet->initWithSprites(backgroundSprite, pogressSprite, thumbSprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
bool CCControlSlider::initWithSprites(CCSprite * backgroundSprite, CCSprite* progressSprite, CCSprite* thumbSprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (CCControl::init())
|
|
|
|
{
|
2012-09-25 17:26:09 +08:00
|
|
|
CCAssert(backgroundSprite, "Background sprite must be not nil");
|
|
|
|
CCAssert(progressSprite, "Progress sprite must be not nil");
|
2012-09-25 16:57:51 +08:00
|
|
|
CCAssert(thumbSprite, "Thumb sprite must be not nil");
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
ignoreAnchorPointForPosition(false);
|
|
|
|
setTouchEnabled(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
this->setBackgroundSprite(backgroundSprite);
|
|
|
|
this->setProgressSprite(progressSprite);
|
|
|
|
this->setThumbSprite(thumbSprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Defines the content size
|
2012-09-25 16:57:51 +08:00
|
|
|
CCRect maxRect = CCControlUtils::CCRectUnion(backgroundSprite->boundingBox(), thumbSprite->boundingBox());
|
|
|
|
|
|
|
|
setContentSize(CCSizeMake(maxRect.size.width, maxRect.size.height));
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Add the slider background
|
|
|
|
m_backgroundSprite->setAnchorPoint(ccp(0.5f, 0.5f));
|
2012-09-25 16:57:51 +08:00
|
|
|
m_backgroundSprite->setPosition(ccp(this->getContentSize().width / 2, this->getContentSize().height / 2));
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(m_backgroundSprite);
|
|
|
|
|
|
|
|
// Add the progress bar
|
|
|
|
m_progressSprite->setAnchorPoint(ccp(0.0f, 0.5f));
|
2012-09-25 16:57:51 +08:00
|
|
|
m_progressSprite->setPosition(ccp(0.0f, this->getContentSize().height / 2));
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(m_progressSprite);
|
|
|
|
|
|
|
|
// Add the slider thumb
|
2012-09-25 16:57:51 +08:00
|
|
|
m_thumbSprite->setPosition(ccp(0.0f, this->getContentSize().height / 2));
|
|
|
|
addChild(m_thumbSprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Init default values
|
|
|
|
m_minimumValue = 0.0f;
|
|
|
|
m_maximumValue = 1.0f;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
setValue(m_minimumValue);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-25 17:26:09 +08:00
|
|
|
void CCControlSlider::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
CCControl::setEnabled(enabled);
|
|
|
|
if (m_thumbSprite != NULL)
|
|
|
|
{
|
|
|
|
m_thumbSprite->setOpacity((enabled) ? 255 : 128);
|
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void CCControlSlider::setValue(float value)
|
|
|
|
{
|
2012-09-25 17:26:09 +08:00
|
|
|
// set new value with sentinel
|
|
|
|
if (value < m_minimumValue)
|
|
|
|
{
|
|
|
|
value = m_minimumValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value > m_maximumValue)
|
|
|
|
{
|
|
|
|
value = m_maximumValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_value = value;
|
|
|
|
|
|
|
|
this->needsLayout();
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
this->sendActionsForControlEvents(CCControlEventValueChanged);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSlider::setMinimumValue(float minimumValue)
|
|
|
|
{
|
|
|
|
m_minimumValue=minimumValue;
|
2012-09-03 05:31:12 +08:00
|
|
|
m_minimumAllowedValue = minimumValue;
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_minimumValue >= m_maximumValue)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_maximumValue = m_minimumValue + 1.0f;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
setValue(m_value);
|
|
|
|
}
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
void CCControlSlider::setMaximumValue(float maximumValue)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
m_maximumValue=maximumValue;
|
2012-09-03 05:31:12 +08:00
|
|
|
m_maximumAllowedValue = maximumValue;
|
2012-09-25 16:57:51 +08:00
|
|
|
if (m_maximumValue <= m_minimumValue)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_minimumValue = m_maximumValue - 1.0f;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
setValue(m_value);
|
|
|
|
}
|
2012-09-25 17:26:09 +08:00
|
|
|
|
|
|
|
bool CCControlSlider::isTouchInside(CCTouch * touch)
|
|
|
|
{
|
|
|
|
CCPoint touchLocation = touch->getLocation();
|
|
|
|
touchLocation = this->getParent()->convertToNodeSpace(touchLocation);
|
|
|
|
|
|
|
|
CCRect rect = this->boundingBox();
|
|
|
|
rect.size.width += m_thumbSprite->getContentSize().width;
|
|
|
|
rect.origin.x -= m_thumbSprite->getContentSize().width / 2;
|
|
|
|
|
|
|
|
return rect.containsPoint(touchLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPoint CCControlSlider::locationFromTouch(CCTouch* touch)
|
|
|
|
{
|
|
|
|
CCPoint 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 > m_backgroundSprite->getContentSize().width)
|
|
|
|
{
|
|
|
|
touchLocation.x = m_backgroundSprite->getContentSize().width;
|
|
|
|
}
|
|
|
|
|
|
|
|
return touchLocation;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
bool CCControlSlider::ccTouchBegan(CCTouch* touch, CCEvent* pEvent)
|
|
|
|
{
|
2012-10-30 23:10:05 +08:00
|
|
|
if (!isTouchInside(touch) || !isEnabled() || !isVisible())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
CCPoint location = locationFromTouch(touch);
|
2012-04-19 14:35:52 +08:00
|
|
|
sliderBegan(location);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSlider::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
CCPoint location = locationFromTouch(pTouch);
|
2012-04-19 14:35:52 +08:00
|
|
|
sliderMoved(location);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSlider::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
sliderEnded(CCPointZero);
|
|
|
|
}
|
|
|
|
|
2012-09-25 17:26:09 +08:00
|
|
|
void CCControlSlider::needsLayout()
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
if (NULL == m_thumbSprite || NULL == m_backgroundSprite || NULL == m_progressSprite)
|
|
|
|
{
|
|
|
|
return;
|
2012-09-25 17:26:09 +08:00
|
|
|
}
|
|
|
|
// Update thumb position for new value
|
|
|
|
float percent = (m_value - m_minimumValue) / (m_maximumValue - m_minimumValue);
|
|
|
|
|
|
|
|
CCPoint pos = m_thumbSprite->getPosition();
|
|
|
|
pos.x = percent * m_backgroundSprite->getContentSize().width;
|
|
|
|
m_thumbSprite->setPosition(pos);
|
|
|
|
|
|
|
|
// Stretches content proportional to newLevel
|
|
|
|
CCRect textureRect = m_progressSprite->getTextureRect();
|
|
|
|
textureRect = CCRectMake(textureRect.origin.x, textureRect.origin.y, pos.x, textureRect.size.height);
|
|
|
|
m_progressSprite->setTextureRect(textureRect, m_progressSprite->isTextureRectRotated(), textureRect.size);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void CCControlSlider::sliderBegan(CCPoint location)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
this->setSelected(true);
|
|
|
|
this->getThumbSprite()->setColor(ccGRAY);
|
2012-04-19 14:35:52 +08:00
|
|
|
setValue(valueForLocation(location));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSlider::sliderMoved(CCPoint location)
|
|
|
|
{
|
|
|
|
setValue(valueForLocation(location));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlSlider::sliderEnded(CCPoint location)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
if (this->isSelected())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
setValue(valueForLocation(m_thumbSprite->getPosition()));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
this->getThumbSprite()->setColor(ccWHITE);
|
|
|
|
this->setSelected(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float CCControlSlider::valueForLocation(CCPoint location)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
float percent = location.x/ m_backgroundSprite->getContentSize().width;
|
2012-09-03 22:46:55 +08:00
|
|
|
return MAX(MIN(m_minimumValue + percent * (m_maximumValue - m_minimumValue), m_maximumAllowedValue), m_minimumAllowedValue);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_END
|