2012-09-25 16:57:51 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
|
|
|
* Copyright (c) 2012 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CCControlPotentiometerTest.h"
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
ControlPotentiometerTest::ControlPotentiometerTest()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _displayValueLabel(NULL)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
ControlPotentiometerTest::~ControlPotentiometerTest()
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_displayValueLabel);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
bool ControlPotentiometerTest::init()
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
if (ControlScene::init())
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Node *layer = Node::create();
|
2013-07-12 14:11:55 +08:00
|
|
|
layer->setPosition(Point(screenSize.width / 2, screenSize.height / 2));
|
2012-09-25 16:57:51 +08:00
|
|
|
this->addChild(layer, 1);
|
|
|
|
|
|
|
|
double layer_width = 0;
|
|
|
|
|
|
|
|
// Add the black background for the text
|
2013-06-20 14:17:10 +08:00
|
|
|
Scale9Sprite *background = Scale9Sprite::create("extensions/buttonBackground.png");
|
2013-07-12 14:30:26 +08:00
|
|
|
background->setContentSize(Size(80, 50));
|
2013-07-12 14:11:55 +08:00
|
|
|
background->setPosition(Point(layer_width + background->getContentSize().width / 2.0f, 0));
|
2012-09-25 16:57:51 +08:00
|
|
|
layer->addChild(background);
|
|
|
|
|
|
|
|
layer_width += background->getContentSize().width;
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
this->setDisplayValueLabel(LabelTTF::create("", "HelveticaNeue-Bold", 30));
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_displayValueLabel->setPosition(background->getPosition());
|
|
|
|
layer->addChild(_displayValueLabel);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Add the slider
|
2013-06-20 14:17:10 +08:00
|
|
|
ControlPotentiometer *potentiometer = ControlPotentiometer::create("extensions/potentiometerTrack.png"
|
2012-09-25 16:57:51 +08:00
|
|
|
,"extensions/potentiometerProgress.png"
|
|
|
|
,"extensions/potentiometerButton.png");
|
2013-07-12 14:11:55 +08:00
|
|
|
potentiometer->setPosition(Point(layer_width + 10 + potentiometer->getContentSize().width / 2, 0));
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// When the value of the slider will change, the given selector will be call
|
2013-06-20 14:17:10 +08:00
|
|
|
potentiometer->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlPotentiometerTest::valueChanged), ControlEventValueChanged);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
layer->addChild(potentiometer);
|
|
|
|
|
|
|
|
layer_width += potentiometer->getContentSize().width;
|
|
|
|
|
|
|
|
// Set the layer size
|
2013-07-12 14:30:26 +08:00
|
|
|
layer->setContentSize(Size(layer_width, 0));
|
2013-07-12 14:11:55 +08:00
|
|
|
layer->setAnchorPoint(Point(0.5f, 0.5f));
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
// Update the value label
|
2013-06-20 14:17:10 +08:00
|
|
|
this->valueChanged(potentiometer, ControlEventValueChanged);
|
2012-09-25 16:57:51 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void ControlPotentiometerTest::valueChanged(Object *sender, ControlEvent controlEvent)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
ControlPotentiometer* pControl = (ControlPotentiometer*)sender;
|
2012-09-25 16:57:51 +08:00
|
|
|
// Change value of label.
|
2013-06-20 14:17:10 +08:00
|
|
|
_displayValueLabel->setString(String::createWithFormat("%.02f", pControl->getValue())->getCString());
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|