axmol/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest.cpp

432 lines
17 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-28 16:06:23 +08:00
https://axis-project.github.io/
2021-12-28 16:06:23 +08:00
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:
2021-12-28 16:06:23 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-28 16:06:23 +08:00
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.
****************************************************************************/
2013-09-16 20:54:13 +08:00
#include "UISliderTest.h"
USING_NS_AX;
using namespace axis::ui;
2015-04-09 12:23:47 +08:00
UISliderTests::UISliderTests()
{
ADD_TEST_CASE(UISliderTest);
ADD_TEST_CASE(UISliderTest_Scale9);
ADD_TEST_CASE(UISliderTest_Scale9_State_Change);
ADD_TEST_CASE(UISliderNormalDefaultTest);
ADD_TEST_CASE(UISliderDisabledDefaultTest);
ADD_TEST_CASE(UISliderNewEventCallbackTest);
2015-12-09 17:15:09 +08:00
ADD_TEST_CASE(UISliderIssue12249Test);
}
2013-09-16 20:54:13 +08:00
// UISliderTest
2021-12-28 16:06:23 +08:00
UISliderTest::UISliderTest() : _displayValueLabel(nullptr) {}
2013-09-16 20:54:13 +08:00
2021-12-28 16:06:23 +08:00
UISliderTest::~UISliderTest() {}
2013-09-16 20:54:13 +08:00
bool UISliderTest::init()
{
if (UIScene::init())
{
2014-06-20 11:18:53 +08:00
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
2013-09-16 20:54:13 +08:00
// Add a label in which the slider alert will be displayed
_displayValueLabel = TextBMFont::create("Move the slider thumb", "ccb/markerfelt24shadow.fnt");
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(_displayValueLabel);
2021-12-28 16:06:23 +08:00
2013-09-16 20:54:13 +08:00
// Add the alert
2021-12-28 16:06:23 +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));
2021-12-28 16:06:23 +08:00
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().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();
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");
slider->setMaxPercent(10000);
2021-12-28 16:06:23 +08:00
slider->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f /* + slider->getSize().height * 2.0f*/));
2022-07-15 19:17:01 +08:00
slider->addEventListener(AX_CALLBACK_2(UISliderTest::sliderEvent, this));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(slider);
2014-03-04 16:51:35 +08:00
2015-11-30 16:17:13 +08:00
_slider = slider;
TTFConfig ttfConfig("fonts/arial.ttf", 15);
auto label1 = Label::createWithTTF(ttfConfig, "Print Resources");
2022-07-15 19:17:01 +08:00
auto item1 = MenuItemLabel::create(label1, AX_CALLBACK_1(UISliderTest::printWidgetResources, this));
2021-12-28 16:06:23 +08:00
item1->setPosition(
Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + item1->getContentSize().height * 3));
2015-11-30 16:17:13 +08:00
auto pMenu1 = Menu::create(item1, nullptr);
pMenu1->setPosition(Vec2(0.0f, 0.0f));
2015-11-30 16:17:13 +08:00
this->addChild(pMenu1, 10);
2021-12-28 16:06:23 +08:00
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
2021-12-28 16:06:23 +08:00
void UISliderTest::sliderEvent(Ref* pSender, Slider::EventType type)
2013-09-16 20:54:13 +08:00
{
2014-05-12 11:17:06 +08:00
if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
2013-09-16 20:54:13 +08:00
{
2013-12-23 15:35:35 +08:00
Slider* slider = dynamic_cast<Slider*>(pSender);
2021-12-28 16:06:23 +08:00
int percent = slider->getPercent();
int maxPercent = slider->getMaxPercent();
_displayValueLabel->setString(StringUtils::format("Percent %f", 10000.0 * percent / maxPercent));
2013-09-16 20:54:13 +08:00
}
}
void UISliderTest::printWidgetResources(axis::Ref* /*sender*/)
2015-11-30 16:17:13 +08:00
{
axis::ResourceData textureFile = _slider->getBackFile();
2022-07-16 10:43:05 +08:00
AXLOG("textureFile Name : %s, Type: %d", textureFile.file.c_str(), textureFile.type);
axis::ResourceData progressBarTextureFile = _slider->getProgressBarFile();
2022-07-16 10:43:05 +08:00
AXLOG("progressBarTextureFile Name : %s, Type: %d", progressBarTextureFile.file.c_str(),
2021-12-28 16:06:23 +08:00
progressBarTextureFile.type);
axis::ResourceData slidBallNormalTextureFile = _slider->getBallNormalFile();
2022-07-16 10:43:05 +08:00
AXLOG("slidBallNormalTextureFile Name : %s, Type: %d", slidBallNormalTextureFile.file.c_str(),
2021-12-28 16:06:23 +08:00
slidBallNormalTextureFile.type);
axis::ResourceData slidBallPressedTextureFile = _slider->getBallPressedFile();
2022-07-16 10:43:05 +08:00
AXLOG("slidBallPressedTextureFile Name : %s, Type: %d", slidBallPressedTextureFile.file.c_str(),
2021-12-28 16:06:23 +08:00
slidBallPressedTextureFile.type);
axis::ResourceData slidBallDisabledTextureFile = _slider->getBallDisabledFile();
2022-07-16 10:43:05 +08:00
AXLOG("slidBallDisabledTextureFile Name : %s, Type: %d", slidBallDisabledTextureFile.file.c_str(),
2021-12-28 16:06:23 +08:00
slidBallDisabledTextureFile.type);
2015-11-30 16:17:13 +08:00
}
2013-09-16 20:54:13 +08:00
// UISliderTest_Scale9
2021-12-28 16:06:23 +08:00
UISliderTest_Scale9::UISliderTest_Scale9() : _displayValueLabel(nullptr) {}
2013-09-16 20:54:13 +08:00
2021-12-28 16:06:23 +08:00
UISliderTest_Scale9::~UISliderTest_Scale9() {}
2013-09-16 20:54:13 +08:00
bool UISliderTest_Scale9::init()
{
if (UIScene::init())
{
2014-06-20 11:18:53 +08:00
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
2013-09-16 20:54:13 +08:00
// Add a label in which the slider alert will be displayed
2021-12-28 16:06:23 +08:00
_displayValueLabel = Text::create("Move the slider thumb", "fonts/Marker Felt.ttf", 32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(_displayValueLabel);
2021-12-28 16:06:23 +08:00
2013-09-16 20:54:13 +08:00
// Add the alert
2021-12-28 16:06:23 +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));
2021-12-28 16:06:23 +08:00
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(alert);
2021-12-28 16:06:23 +08:00
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.0f, 0.0f, 0.0f, 0.0f));
slider->setContentSize(Size(250.0f, 19.0f));
2021-12-28 16:06:23 +08:00
slider->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f /* + slider->getSize().height * 3.0f*/));
2022-07-15 19:17:01 +08:00
slider->addEventListener(AX_CALLBACK_2(UISliderTest_Scale9::sliderEvent, this));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(slider);
2021-12-28 16:06:23 +08:00
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
2021-12-28 16:06:23 +08:00
void UISliderTest_Scale9::sliderEvent(Ref* pSender, Slider::EventType type)
2013-09-16 20:54:13 +08:00
{
2014-05-12 11:17:06 +08:00
if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
2013-09-16 20:54:13 +08:00
{
2013-12-23 15:35:35 +08:00
Slider* slider = dynamic_cast<Slider*>(pSender);
2021-12-28 16:06:23 +08:00
int percent = slider->getPercent();
_displayValueLabel->setString(StringUtils::format("Percent %d", percent));
2013-09-16 20:54:13 +08:00
}
2014-03-11 17:13:54 +08:00
}
2014-12-23 15:03:31 +08:00
2015-01-20 20:57:54 +08:00
// UISliderTest_Scale9_State_Change
2021-12-28 16:06:23 +08:00
UISliderTest_Scale9_State_Change::UISliderTest_Scale9_State_Change() : _displayValueLabel(nullptr) {}
2015-01-20 20:57:54 +08:00
2021-12-28 16:06:23 +08:00
UISliderTest_Scale9_State_Change::~UISliderTest_Scale9_State_Change() {}
2015-01-20 20:57:54 +08:00
bool UISliderTest_Scale9_State_Change::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add a label in which the slider alert will be displayed
_displayValueLabel = Text::create("Click the slider thumb", "fonts/Marker Felt.ttf", 32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
2015-01-20 20:57:54 +08:00
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
_uiLayer->addChild(_displayValueLabel);
// Add the alert
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("Slider scale9 render", "fonts/Marker Felt.ttf", 30);
2015-01-20 20:57:54 +08:00
alert->setColor(Color3B(159, 168, 176));
2021-12-28 16:06:23 +08:00
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
2015-01-20 20:57:54 +08:00
_uiLayer->addChild(alert);
// Create the slider
Slider* slider = Slider::create();
slider->loadBarTexture("cocosui/sliderballnormal.png");
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
slider->loadProgressBarTexture("cocosui/slider_bar_active_9patch.png");
slider->ignoreContentAdaptWithSize(false);
slider->setScale9Enabled(true);
slider->setCapInsets(Rect(0.0f, 0.0f, 0.0f, 0.0f));
slider->setContentSize(Size(200.0f, 60.0f));
2021-12-28 16:06:23 +08:00
slider->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f /* + slider->getSize().height * 3.0f*/));
slider->addTouchEventListener([=](Ref* /*sender*/, Widget::TouchEventType type) {
if (type == Widget::TouchEventType::ENDED)
{
2015-01-20 20:57:54 +08:00
if (slider->isScale9Enabled())
{
slider->setScale9Enabled(false);
}
else
slider->setScale9Enabled(true);
}
});
_uiLayer->addChild(slider);
return true;
}
return false;
}
2021-12-28 16:06:23 +08:00
void UISliderTest_Scale9_State_Change::sliderEvent(Ref* pSender, Slider::EventType type)
2015-01-20 20:57:54 +08:00
{
if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
{
Slider* slider = dynamic_cast<Slider*>(pSender);
2021-12-28 16:06:23 +08:00
int percent = slider->getPercent();
_displayValueLabel->setString(StringUtils::format("Percent %d", percent));
2015-01-20 20:57:54 +08:00
}
}
2014-12-23 15:03:31 +08:00
// UISliderNormalDefaultTest
2021-12-28 16:06:23 +08:00
UISliderNormalDefaultTest::UISliderNormalDefaultTest() : _displayValueLabel(nullptr) {}
2014-12-23 15:03:31 +08:00
2021-12-28 16:06:23 +08:00
UISliderNormalDefaultTest::~UISliderNormalDefaultTest() {}
2014-12-23 15:03:31 +08:00
bool UISliderNormalDefaultTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
2014-12-23 15:03:31 +08:00
// Add a label in which the slider alert will be displayed
2021-12-28 16:06:23 +08:00
_displayValueLabel = Text::create("", "Arial", 32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
2014-12-23 15:03:31 +08:00
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 100));
_uiLayer->addChild(_displayValueLabel);
2021-12-28 16:06:23 +08:00
2014-12-23 15:03:31 +08:00
// Add the alert
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("when pressed, the slider ball should scale", "fonts/Marker Felt.ttf", 20);
2014-12-23 15:03:31 +08:00
alert->setColor(Color3B(159, 168, 176));
2021-12-28 16:06:23 +08:00
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.75f));
2014-12-23 15:03:31 +08:00
_uiLayer->addChild(alert);
2021-12-28 16:06:23 +08:00
2014-12-23 15:03:31 +08:00
// Create the slider
Slider* slider = Slider::create();
slider->loadBarTexture("cocosui/sliderTrack.png");
slider->loadSlidBallTextures("cocosui/sliderThumb.png");
2021-12-28 16:06:23 +08:00
slider->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 50));
2014-12-23 15:03:31 +08:00
_uiLayer->addChild(slider);
2021-12-28 16:06:23 +08:00
2015-06-05 17:16:10 +08:00
Slider* sliderScale9 = (Slider*)slider->clone();
2014-12-23 15:03:31 +08:00
sliderScale9->setScale9Enabled(true);
sliderScale9->setCapInsets(Rect(0.0f, 0.0f, 0.0f, 0.0f));
2014-12-23 15:03:31 +08:00
sliderScale9->setZoomScale(1.0);
sliderScale9->setContentSize(Size(250.0f, 19.0f));
2014-12-23 15:03:31 +08:00
sliderScale9->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 20));
_uiLayer->addChild(sliderScale9);
2021-12-28 16:06:23 +08:00
2014-12-23 15:03:31 +08:00
return true;
}
return false;
}
// UISliderDisabledDefaultTest
2021-12-28 16:06:23 +08:00
UISliderDisabledDefaultTest::UISliderDisabledDefaultTest() : _displayValueLabel(nullptr) {}
2014-12-23 15:03:31 +08:00
2021-12-28 16:06:23 +08:00
UISliderDisabledDefaultTest::~UISliderDisabledDefaultTest() {}
2014-12-23 15:03:31 +08:00
bool UISliderDisabledDefaultTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
2014-12-23 15:03:31 +08:00
// Add a label in which the slider alert will be displayed
2021-12-28 16:06:23 +08:00
_displayValueLabel = Text::create("", "Arial", 32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
2014-12-23 15:03:31 +08:00
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 100));
_uiLayer->addChild(_displayValueLabel);
2021-12-28 16:06:23 +08:00
2014-12-23 15:03:31 +08:00
// Add the alert
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("slider ball should be gray.", "fonts/Marker Felt.ttf", 20);
2014-12-23 15:03:31 +08:00
alert->setColor(Color3B(159, 168, 176));
2021-12-28 16:06:23 +08:00
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.75f));
2014-12-23 15:03:31 +08:00
_uiLayer->addChild(alert);
2021-12-28 16:06:23 +08:00
2014-12-23 15:03:31 +08:00
// Create the slider
Slider* slider = Slider::create();
slider->loadBarTexture("cocosui/slidbar.png");
slider->loadSlidBallTextureNormal("cocosui/sliderballnormal.png");
slider->setEnabled(false);
slider->setBright(false);
2021-12-28 16:06:23 +08:00
slider->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 50));
2014-12-23 15:03:31 +08:00
_uiLayer->addChild(slider);
2021-12-28 16:06:23 +08:00
2015-06-05 17:16:10 +08:00
Slider* sliderScale9 = (Slider*)slider->clone();
2014-12-23 15:03:31 +08:00
sliderScale9->setScale9Enabled(true);
sliderScale9->setEnabled(false);
sliderScale9->setBright(false);
sliderScale9->setCapInsets(Rect(0.0f, 0.0f, 0.0f, 0.0f));
sliderScale9->setContentSize(Size(250.0f, 10.0f));
2014-12-23 15:03:31 +08:00
sliderScale9->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 20));
_uiLayer->addChild(sliderScale9);
2021-12-28 16:06:23 +08:00
2014-12-23 15:03:31 +08:00
return true;
}
return false;
}
// UISliderNewEventCallbackTest
2021-12-28 16:06:23 +08:00
UISliderNewEventCallbackTest::UISliderNewEventCallbackTest() : _displayValueLabel(nullptr) {}
2021-12-28 16:06:23 +08:00
UISliderNewEventCallbackTest::~UISliderNewEventCallbackTest() {}
bool UISliderNewEventCallbackTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add a label in which the slider alert will be displayed
2021-12-28 16:06:23 +08:00
_displayValueLabel = Text::create("", "Arial", 32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 100));
_uiLayer->addChild(_displayValueLabel);
// Add the alert
Text* alert = Text::create("See console output for Slider Down and Up event.", "fonts/Marker Felt.ttf", 20);
alert->setColor(Color3B(159, 168, 176));
2021-12-28 16:06:23 +08:00
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.75f));
_uiLayer->addChild(alert);
// Create the slider
Slider* slider = Slider::create();
slider->loadBarTexture("cocosui/sliderTrack.png");
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
slider->setMaxPercent(1000);
2021-12-28 16:06:23 +08:00
slider->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 50));
slider->addEventListener([=](Ref* widget, Slider::EventType type) {
Slider* slider = (Slider*)widget;
2022-07-15 19:17:01 +08:00
AX_UNUSED_PARAM(slider);
2021-12-28 16:06:23 +08:00
if (type == Slider::EventType::ON_SLIDEBALL_DOWN)
{
2022-07-16 10:43:05 +08:00
AXLOG("slider button pressed!");
}
2021-12-28 16:06:23 +08:00
else if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
{
2022-07-16 10:43:05 +08:00
AXLOG("slider is moving! percent = %f", 100.0f * slider->getPercent() / slider->getMaxPercent());
}
2021-12-28 16:06:23 +08:00
else if (type == Slider::EventType::ON_SLIDEBALL_UP)
{
2022-07-16 10:43:05 +08:00
AXLOG("slider button is released.");
}
});
_uiLayer->addChild(slider);
return true;
}
return false;
}
2015-12-09 17:15:09 +08:00
// UISliderIssue12249Test
2021-12-28 16:06:23 +08:00
UISliderIssue12249Test::UISliderIssue12249Test() : _displayValueLabel(nullptr) {}
2015-12-09 17:15:09 +08:00
2021-12-28 16:06:23 +08:00
UISliderIssue12249Test::~UISliderIssue12249Test() {}
2015-12-09 17:15:09 +08:00
bool UISliderIssue12249Test::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
2015-12-09 17:15:09 +08:00
// Add a label in which the slider alert will be displayed
_displayValueLabel = TextBMFont::create("Move the slider thumb", "ccb/markerfelt24shadow.fnt");
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
2015-12-09 17:15:09 +08:00
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
_uiLayer->addChild(_displayValueLabel);
2021-12-28 16:06:23 +08:00
2015-12-09 17:15:09 +08:00
// Create the slider
Slider* slider = Slider::create();
slider->setScale9Enabled(true);
slider->loadBarTexture("cocosui/sliderTrack.png");
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
slider->setContentSize(Size(300, slider->getContentSize().height * 1.5));
slider->setMaxPercent(10000);
2021-12-28 16:06:23 +08:00
slider->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f /* + slider->getSize().height * 2.0f*/));
2022-07-15 19:17:01 +08:00
slider->addEventListener(AX_CALLBACK_2(UISliderIssue12249Test::sliderEvent, this));
2015-12-09 17:15:09 +08:00
_uiLayer->addChild(slider);
2021-12-28 16:06:23 +08:00
2015-12-09 17:15:09 +08:00
return true;
}
return false;
}
2021-12-28 16:06:23 +08:00
void UISliderIssue12249Test::sliderEvent(Ref* pSender, Slider::EventType type)
2015-12-09 17:15:09 +08:00
{
if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
{
Slider* slider = dynamic_cast<Slider*>(pSender);
2021-12-28 16:06:23 +08:00
int percent = slider->getPercent();
2015-12-09 17:15:09 +08:00
int maxPercent = slider->getMaxPercent();
_displayValueLabel->setString(StringUtils::format("Percent %f", 10000.0 * percent / maxPercent));
}
}