axmol/tests/Classes/ExtensionsTest/CocoStudioGUITest/UISliderTest/UISliderTest.cpp

161 lines
6.2 KiB
C++
Raw Normal View History

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
_displayValueLabel = ui::Text::create();
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText("Move the slider thumb");
_displayValueLabel->setFontName("Marker Felt");
_displayValueLabel->setFontSize(32);
_displayValueLabel->setAnchorPoint(Point(0.5f, -1));
_displayValueLabel->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
_uiLayer->addChild(_displayValueLabel);
2013-09-16 20:54:13 +08:00
// Add the alert
ui::Text* alert = ui::Text::create();
2013-09-16 20:54:13 +08:00
alert->setText("Slider");
2013-12-23 15:35:35 +08:00
alert->setFontName("Marker Felt");
2013-09-16 20:54:13 +08:00
alert->setFontSize(30);
alert->setColor(Color3B(159, 168, 176));
2013-12-23 15:35:35 +08:00
alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 1.75f));
_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();
2013-09-16 20:54:13 +08:00
slider->setTouchEnabled(true);
slider->loadBarTexture("cocosgui/sliderTrack.png");
slider->loadSlidBallTextures("cocosgui/sliderThumb.png", "cocosgui/sliderThumb.png", "");
slider->loadProgressBarTexture("cocosgui/sliderProgress.png");
2013-12-23 15:35:35 +08:00
slider->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f + slider->getSize().height * 2.0f));
slider->addEventListenerSlider(this, sliderpercentchangedselector(UISliderTest::sliderEvent));
_uiLayer->addChild(slider);
/*
// Create the slider that set allow min progress and allow max progress
Slider* sliderAllow = Slider::create();
sliderAllow->setMinAllowPercent(20);
sliderAllow->setMaxAllowPercent(80);
sliderAllow->setTouchEnabled(true);
sliderAllow->loadBarTexture("cocosgui/sliderTrack.png");
sliderAllow->loadSlidBallTextures("cocosgui/sliderThumb.png", "cocosgui/sliderThumb.png", "");
sliderAllow->loadProgressBarTexture("cocosgui/sliderProgress.png");
sliderAllow->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - sliderAllow->getSize().height * 2.0f));
sliderAllow->addEventListenerSlider(this, sliderpercentchangedselector(UISliderTest::sliderEvent));
_uiLayer->addChild(sliderAllow);
*/
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
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
_displayValueLabel = ui::Text::create();
2013-12-23 15:35:35 +08:00
_displayValueLabel->setText("Move the slider thumb");
_displayValueLabel->setFontName("Marker Felt");
_displayValueLabel->setFontSize(32);
_displayValueLabel->setAnchorPoint(Point(0.5f, -1));
_displayValueLabel->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
_uiLayer->addChild(_displayValueLabel);
2013-09-16 20:54:13 +08:00
// Add the alert
ui::Text *alert = ui::Text::create();
2013-09-16 20:54:13 +08:00
alert->setText("Slider scale9 render");
2013-12-23 15:35:35 +08:00
alert->setFontName("Marker Felt");
2013-09-16 20:54:13 +08:00
alert->setFontSize(30);
alert->setColor(Color3B(159, 168, 176));
2013-12-23 15:35:35 +08:00
alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 1.75f));
_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();
2013-09-16 20:54:13 +08:00
slider->setTouchEnabled(true);
slider->loadBarTexture("cocosgui/sliderTrack2.png");
slider->loadSlidBallTextures("cocosgui/sliderThumb.png", "cocosgui/sliderThumb.png", "");
slider->loadProgressBarTexture("cocosgui/slider_bar_active_9patch.png");
slider->setScale9Enabled(true);
slider->setCapInsets(Rect(0, 0, 0, 0));
2013-12-23 15:35:35 +08:00
slider->setSize(Size(250.0f, 10.0f / Director::getInstance()->getContentScaleFactor()));
slider->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f + slider->getSize().height * 3.0f));
slider->addEventListenerSlider(this, sliderpercentchangedselector(UISliderTest_Scale9::sliderEvent));
_uiLayer->addChild(slider);
/*
// Create the slider that set allow min progress and allow max progress
Slider* sliderAllow = Slider::create();
sliderAllow->setMinAllowPercent(20);
sliderAllow->setMaxAllowPercent(80);
sliderAllow->setTouchEnabled(true);
sliderAllow->loadBarTexture("cocosgui/sliderTrack2.png");
sliderAllow->loadSlidBallTextures("cocosgui/sliderThumb.png", "cocosgui/sliderThumb.png", "");
sliderAllow->loadProgressBarTexture("cocosgui/slider_bar_active_9patch.png");
sliderAllow->setScale9Enabled(true);
sliderAllow->setCapInsets(Rect(0, 0, 0, 0));
sliderAllow->setSize(Size(250.0f, 10.0f / Director::getInstance()->getContentScaleFactor()));
sliderAllow->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - slider->getSize().height * 3.0f));
sliderAllow->addEventListenerSlider(this, sliderpercentchangedselector(UISliderTest_Scale9::sliderEvent));
_uiLayer->addChild(sliderAllow);
*/
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
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
}
}