2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
#include "UISliderTest.h"
|
|
|
|
|
|
|
|
|
|
|
|
// UISliderTest
|
|
|
|
|
|
|
|
UISliderTest::UISliderTest()
|
2013-12-23 15:35:35 +08:00
|
|
|
: _displayValueLabel(nullptr)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UISliderTest::~UISliderTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UISliderTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
Size widgetSize = _widget->getSize();
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add a label in which the slider alert will be displayed
|
2014-04-03 15:33:57 +08:00
|
|
|
_displayValueLabel = Text::create("Move the slider thumb","Move the slider thumb",32);
|
2014-04-15 18:23:40 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vector2(0.5f, -1));
|
|
|
|
_displayValueLabel->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add the alert
|
2014-04-03 15:33:57 +08:00
|
|
|
Text* alert = Text::create("Slider","fonts/Marker Felt.ttf",30);
|
2013-09-16 20:54:13 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2014-04-15 18:23:40 +08:00
|
|
|
alert->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 1.75f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Create the slider
|
2013-12-23 15:35:35 +08:00
|
|
|
Slider* slider = Slider::create();
|
2014-03-11 17:13:54 +08:00
|
|
|
slider->loadBarTexture("cocosui/sliderTrack.png");
|
|
|
|
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
|
|
|
|
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
slider->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f/* + slider->getSize().height * 2.0f*/));
|
2013-12-23 15:35:35 +08:00
|
|
|
slider->addEventListenerSlider(this, sliderpercentchangedselector(UISliderTest::sliderEvent));
|
|
|
|
_uiLayer->addChild(slider);
|
2014-03-04 16:51:35 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void UISliderTest::sliderEvent(Ref *pSender, SliderEventType type)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
if (type == SLIDER_PERCENTCHANGED)
|
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
Slider* slider = dynamic_cast<Slider*>(pSender);
|
2013-09-16 20:54:13 +08:00
|
|
|
int percent = slider->getPercent();
|
2013-12-23 15:35:35 +08:00
|
|
|
_displayValueLabel->setText(String::createWithFormat("Percent %d", percent)->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UISliderTest_Scale9
|
|
|
|
|
|
|
|
UISliderTest_Scale9::UISliderTest_Scale9()
|
2013-12-23 15:35:35 +08:00
|
|
|
: _displayValueLabel(nullptr)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UISliderTest_Scale9::~UISliderTest_Scale9()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UISliderTest_Scale9::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
Size widgetSize = _widget->getSize();
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add a label in which the slider alert will be displayed
|
2014-04-03 15:33:57 +08:00
|
|
|
_displayValueLabel = Text::create("Move the slider thumb","fonts/Marker Felt.ttf",32);
|
2014-04-15 18:23:40 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vector2(0.5f, -1));
|
|
|
|
_displayValueLabel->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add the alert
|
2014-04-03 15:33:57 +08:00
|
|
|
Text *alert = Text::create("Slider scale9 render","fonts/Marker Felt.ttf",30);
|
2013-09-16 20:54:13 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2014-04-15 18:23:40 +08:00
|
|
|
alert->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 1.75f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Create the slider
|
2013-12-23 15:35:35 +08:00
|
|
|
Slider* slider = Slider::create();
|
2014-03-11 17:13:54 +08:00
|
|
|
slider->loadBarTexture("cocosui/sliderTrack2.png");
|
|
|
|
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
|
|
|
|
slider->loadProgressBarTexture("cocosui/slider_bar_active_9patch.png");
|
2013-09-16 20:54:13 +08:00
|
|
|
slider->setScale9Enabled(true);
|
|
|
|
slider->setCapInsets(Rect(0, 0, 0, 0));
|
2014-03-04 16:51:35 +08:00
|
|
|
slider->setSize(Size(250.0f, 19));
|
2014-04-15 18:23:40 +08:00
|
|
|
slider->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f/* + slider->getSize().height * 3.0f*/));
|
2013-12-23 15:35:35 +08:00
|
|
|
slider->addEventListenerSlider(this, sliderpercentchangedselector(UISliderTest_Scale9::sliderEvent));
|
|
|
|
_uiLayer->addChild(slider);
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void UISliderTest_Scale9::sliderEvent(Ref *pSender, SliderEventType type)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
if (type == SLIDER_PERCENTCHANGED)
|
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
Slider* slider = dynamic_cast<Slider*>(pSender);
|
2013-09-16 20:54:13 +08:00
|
|
|
int percent = slider->getPercent();
|
2013-12-23 15:35:35 +08:00
|
|
|
_displayValueLabel->setText(String::createWithFormat("Percent %d", percent)->getCString());
|
2013-09-16 20:54:13 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|