Add selected image to slider

This commit is contained in:
Victor Buldakov 2013-11-04 01:17:32 +04:00
parent 2bffc959fd
commit 550245c6dc
2 changed files with 90 additions and 12 deletions

View File

@ -40,6 +40,7 @@ ControlSlider::ControlSlider()
, _minimumAllowedValue(0.0f)
, _maximumAllowedValue(0.0f)
, _thumbSprite(NULL)
, _selectedThumbSprite(NULL)
, _progressSprite(NULL)
, _backgroundSprite(NULL)
{
@ -49,6 +50,7 @@ ControlSlider::ControlSlider()
ControlSlider::~ControlSlider()
{
CC_SAFE_RELEASE(_thumbSprite);
CC_SAFE_RELEASE(_selectedThumbSprite);
CC_SAFE_RELEASE(_progressSprite);
CC_SAFE_RELEASE(_backgroundSprite);
}
@ -57,6 +59,21 @@ ControlSlider* ControlSlider::create(const char* bgFile, const char* progressFil
{
// Prepare background for slider
Sprite *backgroundSprite = Sprite::create(bgFile);
// Prepare progress for slider
Sprite *progressSprite = Sprite::create(progressFile);
// Prepare thumb (menuItem) for slider
Sprite *thumbSprite = Sprite::create(thumbFile);
return ControlSlider::create(backgroundSprite, progressSprite, thumbSprite);
}
ControlSlider* ControlSlider::create(const char* bgFile, const char* progressFile, const char* thumbFile,
const char* selectedThumbSpriteFile)
{
// Prepare background for slider
Sprite *backgroundSprite = Sprite::create(bgFile);
// Prepare progress for slider
Sprite *progressSprite = Sprite::create(progressFile);
@ -64,7 +81,10 @@ ControlSlider* ControlSlider::create(const char* bgFile, const char* progressFil
// Prepare thumb (menuItem) for slider
Sprite *thumbSprite = Sprite::create(thumbFile);
return ControlSlider::create(backgroundSprite, progressSprite, thumbSprite);
// Prepare selected thumb (menuItem) for slider
Sprite *selectedThumbSprite = Sprite::create(selectedThumbSpriteFile);
return ControlSlider::create(backgroundSprite, progressSprite, thumbSprite, selectedThumbSprite);
}
ControlSlider* ControlSlider::create(Sprite * backgroundSprite, Sprite* pogressSprite, Sprite* thumbSprite)
@ -75,13 +95,32 @@ ControlSlider* ControlSlider::create(Sprite * backgroundSprite, Sprite* pogressS
return pRet;
}
bool ControlSlider::initWithSprites(Sprite * backgroundSprite, Sprite* progressSprite, Sprite* thumbSprite)
ControlSlider* ControlSlider::create(Sprite * backgroundSprite, Sprite* pogressSprite, Sprite* thumbSprite,
Sprite* selectedThumbSprite)
{
ControlSlider *pRet = new ControlSlider();
pRet->initWithSprites(backgroundSprite, pogressSprite, thumbSprite, selectedThumbSprite);
pRet->autorelease();
return pRet;
}
bool ControlSlider::initWithSprites(Sprite * backgroundSprite, Sprite* progressSprite, Sprite* thumbSprite)
{
Sprite* selectedThumbSprite = Sprite::createWithTexture(thumbSprite->getTexture(),
thumbSprite->getTextureRect());
selectedThumbSprite->setColor(Color3B::GRAY);
this->initWithSprites(backgroundSprite, progressSprite, thumbSprite, selectedThumbSprite);
}
bool ControlSlider::initWithSprites(Sprite * backgroundSprite, Sprite* progressSprite, Sprite* thumbSprite,
Sprite* selectedThumbSprite)
{
if (Control::init())
{
CCASSERT(backgroundSprite, "Background sprite must be not nil");
CCASSERT(progressSprite, "Progress sprite must be not nil");
CCASSERT(thumbSprite, "Thumb sprite must be not nil");
CCASSERT(backgroundSprite, "Background sprite must be not nil");
CCASSERT(progressSprite, "Progress sprite must be not nil");
CCASSERT(thumbSprite, "Thumb sprite must be not nil");
CCASSERT(selectedThumbSprite, "Thumb sprite must be not nil");
ignoreAnchorPointForPosition(false);
setTouchEnabled(true);
@ -89,6 +128,7 @@ ControlSlider* ControlSlider::create(Sprite * backgroundSprite, Sprite* pogressS
this->setBackgroundSprite(backgroundSprite);
this->setProgressSprite(progressSprite);
this->setThumbSprite(thumbSprite);
this->setSelectedThumbSprite(selectedThumbSprite);
// Defines the content size
Rect maxRect = ControlUtils::RectUnion(backgroundSprite->getBoundingBox(), thumbSprite->getBoundingBox());
@ -109,6 +149,10 @@ ControlSlider* ControlSlider::create(Sprite * backgroundSprite, Sprite* pogressS
_thumbSprite->setPosition(Point(0.0f, this->getContentSize().height / 2));
addChild(_thumbSprite);
_selectedThumbSprite->setPosition(Point(0.0f, this->getContentSize().height / 2));
_selectedThumbSprite->setVisible(false);
addChild(_selectedThumbSprite);
// Init default values
_minimumValue = 0.0f;
_maximumValue = 1.0f;
@ -228,7 +272,8 @@ void ControlSlider::onTouchEnded(Touch *pTouch, Event *pEvent)
void ControlSlider::needsLayout()
{
if (NULL == _thumbSprite || NULL == _backgroundSprite || NULL == _progressSprite)
if (NULL == _thumbSprite || NULL == _selectedThumbSprite || NULL == _backgroundSprite
|| NULL == _progressSprite)
{
return;
}
@ -238,6 +283,7 @@ void ControlSlider::needsLayout()
Point pos = _thumbSprite->getPosition();
pos.x = percent * _backgroundSprite->getContentSize().width;
_thumbSprite->setPosition(pos);
_selectedThumbSprite->setPosition(pos);
// Stretches content proportional to newLevel
Rect textureRect = _progressSprite->getTextureRect();
@ -248,7 +294,8 @@ void ControlSlider::needsLayout()
void ControlSlider::sliderBegan(Point location)
{
this->setSelected(true);
this->getThumbSprite()->setColor(Color3B::GRAY);
_thumbSprite->setVisible(false);
_selectedThumbSprite->setVisible(true);
setValue(valueForLocation(location));
}
@ -263,7 +310,8 @@ void ControlSlider::sliderEnded(Point location)
{
setValue(valueForLocation(_thumbSprite->getPosition()));
}
this->getThumbSprite()->setColor(Color3B::WHITE);
_thumbSprite->setVisible(true);
_selectedThumbSprite->setVisible(false);
this->setSelected(false);
}

View File

@ -58,6 +58,22 @@ public:
* @see initWithSprites
*/
static ControlSlider* create(Sprite * backgroundSprite, Sprite* pogressSprite, Sprite* thumbSprite);
/**
* Creates slider with a background filename, a progress filename, a thumb
* and a selected thumb image filename.
*/
static ControlSlider* create(const char* bgFile, const char* progressFile, const char* thumbFile,
const char* selectedThumbSpriteFile);
/**
* Creates a slider with a given background sprite and a progress bar, a thumb
* and a selected thumb .
*
* @see initWithSprites
*/
static ControlSlider* create(Sprite * backgroundSprite, Sprite* pogressSprite, Sprite* thumbSprite,
Sprite* selectedThumbSprite);
/**
* @js ctor
*/
@ -68,15 +84,27 @@ public:
*/
virtual ~ControlSlider();
/**
* Initializes a slider with a background sprite, a progress bar and a thumb
* item.
*
* @param backgroundSprite Sprite, that is used as a background.
* @param progressSprite Sprite, that is used as a progress bar.
* @param thumbSprite Sprite, that is used as a thumb.
*/
virtual bool initWithSprites(Sprite * backgroundSprite, Sprite* progressSprite, Sprite* thumbSprite);
/**
* Initializes a slider with a background sprite, a progress bar and a thumb
* item.
*
* @param backgroundSprite Sprite, that is used as a background.
* @param progressSprite Sprite, that is used as a progress bar.
* @param thumbSprite Sprite, that is used as a thumb.
* @param backgroundSprite Sprite, that is used as a background.
* @param progressSprite Sprite, that is used as a progress bar.
* @param thumbSprite Sprite, that is used as a thumb.
* @param selectedThumbSprite Sprite, that is used as a selected thumb.
*/
virtual bool initWithSprites(Sprite * backgroundSprite, Sprite* progressSprite, Sprite* thumbSprite);
virtual bool initWithSprites(Sprite * backgroundSprite, Sprite* progressSprite, Sprite* thumbSprite,
Sprite* selectedThumbSprite);
virtual void needsLayout();
@ -116,8 +144,10 @@ protected:
// maybe this should be read-only
CC_SYNTHESIZE_RETAIN(Sprite*, _thumbSprite, ThumbSprite);
CC_SYNTHESIZE_RETAIN(Sprite*, _selectedThumbSprite, SelectedThumbSprite);
CC_SYNTHESIZE_RETAIN(Sprite*, _progressSprite, ProgressSprite);
CC_SYNTHESIZE_RETAIN(Sprite*, _backgroundSprite, BackgroundSprite);
};
// end of GUI group