2012-04-16 18:58:43 +08:00
|
|
|
/*
|
|
|
|
* CCControlSliderTest.m
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 Yannick Loriot
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-04-17 16:00:18 +08:00
|
|
|
#include "CCControlSliderTest.h"
|
2012-04-16 18:58:43 +08:00
|
|
|
|
2012-04-17 16:00:18 +08:00
|
|
|
CCControlSliderTest::CCControlSliderTest()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2012-04-16 18:58:43 +08:00
|
|
|
|
2012-04-17 16:00:18 +08:00
|
|
|
CCControlSliderTest::~CCControlSliderTest()
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-17 16:00:18 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(m_pDisplayValueLabel);
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|
2012-04-17 16:00:18 +08:00
|
|
|
bool CCControlSliderTest::init()
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-17 16:00:18 +08:00
|
|
|
if (CCControlScene::init())
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-17 16:00:18 +08:00
|
|
|
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
|
|
|
|
// Add a label in which the slider value will be displayed
|
2012-09-05 11:32:29 +08:00
|
|
|
m_pDisplayValueLabel = CCLabelTTF::create("Move the slider thumb!\nThe lower slider is restricted." ,"Marker Felt", 32);
|
2012-04-17 16:00:18 +08:00
|
|
|
m_pDisplayValueLabel->retain();
|
|
|
|
m_pDisplayValueLabel->setAnchorPoint(ccp(0.5f, -1.0f));
|
2012-09-05 11:32:29 +08:00
|
|
|
m_pDisplayValueLabel->setPosition(ccp(screenSize.width / 1.7f, screenSize.height / 2.0f));
|
2012-04-17 16:00:18 +08:00
|
|
|
addChild(m_pDisplayValueLabel);
|
|
|
|
|
2012-04-16 18:58:43 +08:00
|
|
|
// Add the slider
|
2012-06-14 15:13:16 +08:00
|
|
|
CCControlSlider *slider = CCControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
|
2012-04-17 16:00:18 +08:00
|
|
|
slider->setAnchorPoint(ccp(0.5f, 1.0f));
|
|
|
|
slider->setMinimumValue(0.0f); // Sets the min value of range
|
|
|
|
slider->setMaximumValue(5.0f); // Sets the max value of range
|
2012-09-05 11:32:29 +08:00
|
|
|
slider->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f + 16));
|
2012-09-05 12:52:18 +08:00
|
|
|
slider->setTag(1);
|
2012-04-16 18:58:43 +08:00
|
|
|
|
2012-04-17 16:00:18 +08:00
|
|
|
// When the value of the slider will change, the given selector will be call
|
2012-06-12 05:57:30 +08:00
|
|
|
slider->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlSliderTest::valueChanged), CCControlEventValueChanged);
|
2012-04-16 18:58:43 +08:00
|
|
|
|
2012-09-05 12:52:18 +08:00
|
|
|
CCControlSlider *restrictSlider = CCControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
|
2012-09-05 11:32:29 +08:00
|
|
|
restrictSlider->setAnchorPoint(ccp(0.5f, 1.0f));
|
|
|
|
restrictSlider->setMinimumValue(0.0f); // Sets the min value of range
|
|
|
|
restrictSlider->setMaximumValue(5.0f); // Sets the max value of range
|
|
|
|
restrictSlider->setMaximumAllowedValue(4.0f);
|
|
|
|
restrictSlider->setMinimumAllowedValue(1.5f);
|
|
|
|
restrictSlider->setValue(3.0f);
|
|
|
|
restrictSlider->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f - 24));
|
2012-09-05 12:52:18 +08:00
|
|
|
restrictSlider->setTag(2);
|
2012-09-05 11:32:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(slider);
|
2012-09-05 11:32:29 +08:00
|
|
|
addChild(restrictSlider);
|
2012-04-17 16:00:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-16 18:58:43 +08:00
|
|
|
|
2012-06-14 18:32:44 +08:00
|
|
|
void CCControlSliderTest::valueChanged(CCObject *sender, CCControlEvent controlEvent)
|
2012-04-16 18:58:43 +08:00
|
|
|
{
|
2012-04-17 16:00:18 +08:00
|
|
|
CCControlSlider* pSlider = (CCControlSlider*)sender;
|
2012-04-19 14:35:52 +08:00
|
|
|
// Change value of label.
|
2012-09-05 12:52:18 +08:00
|
|
|
if(pSlider->getTag() == 1)
|
|
|
|
m_pDisplayValueLabel->setString(CCString::createWithFormat("Upper slider value = %.02f", pSlider->getValue())->getCString());
|
|
|
|
if(pSlider->getTag() == 2)
|
|
|
|
m_pDisplayValueLabel->setString(CCString::createWithFormat("Lower slider value = %.02f", pSlider->getValue())->getCString());
|
2012-04-16 18:58:43 +08:00
|
|
|
}
|
|
|
|
|