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"
|
2013-10-15 18:00:03 +08:00
|
|
|
#include "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
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSlider::ControlSlider()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _value(0.0f)
|
|
|
|
, _minimumValue(0.0f)
|
|
|
|
, _maximumValue(0.0f)
|
|
|
|
, _minimumAllowedValue(0.0f)
|
|
|
|
, _maximumAllowedValue(0.0f)
|
|
|
|
, _thumbSprite(NULL)
|
|
|
|
, _progressSprite(NULL)
|
|
|
|
, _backgroundSprite(NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSlider::~ControlSlider()
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_thumbSprite);
|
|
|
|
CC_SAFE_RELEASE(_progressSprite);
|
|
|
|
CC_SAFE_RELEASE(_backgroundSprite);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSlider* ControlSlider::create(const char* bgFile, const char* progressFile, const char* thumbFile)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Prepare background for slider
|
2013-06-20 14:15:53 +08:00
|
|
|
Sprite *backgroundSprite = Sprite::create(bgFile);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Prepare progress for slider
|
2013-06-20 14:15:53 +08:00
|
|
|
Sprite *progressSprite = Sprite::create(progressFile);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Prepare thumb (menuItem) for slider
|
2013-06-20 14:15:53 +08:00
|
|
|
Sprite *thumbSprite = Sprite::create(thumbFile);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
return ControlSlider::create(backgroundSprite, progressSprite, thumbSprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSlider* ControlSlider::create(Sprite * backgroundSprite, Sprite* pogressSprite, Sprite* thumbSprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlSlider *pRet = new ControlSlider();
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlSlider::initWithSprites(Sprite * backgroundSprite, Sprite* progressSprite, Sprite* thumbSprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if (Control::init())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(backgroundSprite, "Background sprite must be not nil");
|
|
|
|
CCASSERT(progressSprite, "Progress sprite must be not nil");
|
|
|
|
CCASSERT(thumbSprite, "Thumb sprite must be not nil");
|
2012-09-25 16:57:51 +08:00
|
|
|
|
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
|
2013-07-17 09:16:04 +08:00
|
|
|
Rect maxRect = ControlUtils::RectUnion(backgroundSprite->getBoundingBox(), thumbSprite->getBoundingBox());
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
setContentSize(Size(maxRect.size.width, maxRect.size.height));
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Add the slider background
|
2013-07-12 14:11:55 +08:00
|
|
|
_backgroundSprite->setAnchorPoint(Point(0.5f, 0.5f));
|
|
|
|
_backgroundSprite->setPosition(Point(this->getContentSize().width / 2, this->getContentSize().height / 2));
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_backgroundSprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Add the progress bar
|
2013-07-12 14:11:55 +08:00
|
|
|
_progressSprite->setAnchorPoint(Point(0.0f, 0.5f));
|
|
|
|
_progressSprite->setPosition(Point(0.0f, this->getContentSize().height / 2));
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_progressSprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Add the slider thumb
|
2013-07-12 14:11:55 +08:00
|
|
|
_thumbSprite->setPosition(Point(0.0f, this->getContentSize().height / 2));
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_thumbSprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Init default values
|
2013-06-15 14:03:30 +08:00
|
|
|
_minimumValue = 0.0f;
|
|
|
|
_maximumValue = 1.0f;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
setValue(_minimumValue);
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSlider::setEnabled(bool enabled)
|
2012-09-25 17:26:09 +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 17:26:09 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_thumbSprite->setOpacity((enabled) ? 255 : 128);
|
2012-09-25 17:26:09 +08:00
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSlider::setValue(float value)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 17:26:09 +08:00
|
|
|
// set new value with sentinel
|
2013-06-15 14:03:30 +08:00
|
|
|
if (value < _minimumValue)
|
2012-09-25 17:26:09 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
value = _minimumValue;
|
2012-09-25 17:26:09 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (value > _maximumValue)
|
2012-09-25 17:26:09 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
value = _maximumValue;
|
2012-09-25 17:26:09 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_value = value;
|
2012-09-25 17:26:09 +08:00
|
|
|
|
|
|
|
this->needsLayout();
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
this->sendActionsForControlEvents(Control::EventType::VALUE_CHANGED);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSlider::setMinimumValue(float minimumValue)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_minimumValue=minimumValue;
|
|
|
|
_minimumAllowedValue = minimumValue;
|
|
|
|
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(_value);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSlider::setMaximumValue(float maximumValue)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_maximumValue=maximumValue;
|
|
|
|
_maximumAllowedValue = maximumValue;
|
|
|
|
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(_value);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-09-25 17:26:09 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlSlider::isTouchInside(Touch * touch)
|
2012-09-25 17:26:09 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point touchLocation = touch->getLocation();
|
2012-09-25 17:26:09 +08:00
|
|
|
touchLocation = this->getParent()->convertToNodeSpace(touchLocation);
|
|
|
|
|
2013-07-17 09:16:04 +08:00
|
|
|
Rect rect = this->getBoundingBox();
|
2013-06-15 14:03:30 +08:00
|
|
|
rect.size.width += _thumbSprite->getContentSize().width;
|
|
|
|
rect.origin.x -= _thumbSprite->getContentSize().width / 2;
|
2012-09-25 17:26:09 +08:00
|
|
|
|
|
|
|
return rect.containsPoint(touchLocation);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Point ControlSlider::locationFromTouch(Touch* touch)
|
2012-09-25 17:26:09 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point touchLocation = touch->getLocation(); // Get the touch position
|
2012-09-25 17:26:09 +08:00
|
|
|
touchLocation = this->convertToNodeSpace(touchLocation); // Convert to the node space of this class
|
|
|
|
|
|
|
|
if (touchLocation.x < 0)
|
|
|
|
{
|
|
|
|
touchLocation.x = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
} else if (touchLocation.x > _backgroundSprite->getContentSize().width)
|
2012-09-25 17:26:09 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
touchLocation.x = _backgroundSprite->getContentSize().width;
|
2012-09-25 17:26:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return touchLocation;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool ControlSlider::onTouchBegan(Touch* touch, Event* pEvent)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2012-10-30 23:10:05 +08:00
|
|
|
if (!isTouchInside(touch) || !isEnabled() || !isVisible())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Point location = locationFromTouch(touch);
|
2012-04-19 14:35:52 +08:00
|
|
|
sliderBegan(location);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void ControlSlider::onTouchMoved(Touch *pTouch, Event *pEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point location = locationFromTouch(pTouch);
|
2012-04-19 14:35:52 +08:00
|
|
|
sliderMoved(location);
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void ControlSlider::onTouchEnded(Touch *pTouch, Event *pEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
sliderEnded(Point::ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSlider::needsLayout()
|
2012-09-25 17:26:09 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (NULL == _thumbSprite || NULL == _backgroundSprite || NULL == _progressSprite)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
|
|
|
return;
|
2012-09-25 17:26:09 +08:00
|
|
|
}
|
|
|
|
// Update thumb position for new value
|
2013-06-15 14:03:30 +08:00
|
|
|
float percent = (_value - _minimumValue) / (_maximumValue - _minimumValue);
|
2012-09-25 17:26:09 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Point pos = _thumbSprite->getPosition();
|
2013-06-15 14:03:30 +08:00
|
|
|
pos.x = percent * _backgroundSprite->getContentSize().width;
|
|
|
|
_thumbSprite->setPosition(pos);
|
2012-09-25 17:26:09 +08:00
|
|
|
|
|
|
|
// Stretches content proportional to newLevel
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect textureRect = _progressSprite->getTextureRect();
|
2013-07-12 14:30:26 +08:00
|
|
|
textureRect = Rect(textureRect.origin.x, textureRect.origin.y, pos.x, textureRect.size.height);
|
2013-06-15 14:03:30 +08:00
|
|
|
_progressSprite->setTextureRect(textureRect, _progressSprite->isTextureRectRotated(), textureRect.size);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSlider::sliderBegan(Point location)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
this->setSelected(true);
|
2013-07-08 18:11:32 +08:00
|
|
|
this->getThumbSprite()->setColor(Color3B::GRAY);
|
2012-04-19 14:35:52 +08:00
|
|
|
setValue(valueForLocation(location));
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSlider::sliderMoved(Point location)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
setValue(valueForLocation(location));
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlSlider::sliderEnded(Point location)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
if (this->isSelected())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
setValue(valueForLocation(_thumbSprite->getPosition()));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-08 18:11:32 +08:00
|
|
|
this->getThumbSprite()->setColor(Color3B::WHITE);
|
2012-09-25 16:57:51 +08:00
|
|
|
this->setSelected(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float ControlSlider::valueForLocation(Point location)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
float percent = location.x/ _backgroundSprite->getContentSize().width;
|
|
|
|
return MAX(MIN(_minimumValue + percent * (_maximumValue - _minimumValue), _maximumAllowedValue), _minimumAllowedValue);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_END
|