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

563 lines
19 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-28 16:06:23 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.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 "UILoadingBarTest.h"
USING_NS_AX;
using namespace ax::ui;
2015-04-09 12:23:47 +08:00
UILoadingBarTests::UILoadingBarTests()
{
ADD_TEST_CASE(UILoadingBarTest_Left);
ADD_TEST_CASE(UILoadingBarTest_Right);
ADD_TEST_CASE(UILoadingBarTest_Left_Scale9);
ADD_TEST_CASE(UILoadingBarTest_Right_Scale9);
ADD_TEST_CASE(UILoadingBarTest_Scale9_State_Change);
ADD_TEST_CASE(UILoadingBarReloadTexture);
2015-12-09 17:15:09 +08:00
ADD_TEST_CASE(UILoadingBarIssue12249);
2015-12-19 15:59:27 +08:00
ADD_TEST_CASE(UILoadingBarTest_Direction);
}
2013-09-16 20:54:13 +08:00
// UILoadingBarTest_Left
2021-12-28 16:06:23 +08:00
UILoadingBarTest_Left::UILoadingBarTest_Left() : _count(0) {}
2013-09-16 20:54:13 +08:00
UILoadingBarTest_Left::~UILoadingBarTest_Left()
{
unscheduleUpdate();
}
bool UILoadingBarTest_Left::init()
{
if (UIScene::init())
{
scheduleUpdate();
2021-12-28 16:06:23 +08:00
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 the alert
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("Test LoadingBar Change Direction", "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 loading bar
LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderProgress.png");
2013-12-23 15:35:35 +08:00
loadingBar->setTag(0);
2021-12-28 16:06:23 +08:00
loadingBar->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
2015-06-05 15:57:39 +08:00
auto loadingBarCopy = (LoadingBar*)loadingBar->clone();
loadingBarCopy->setTag(1);
2021-12-28 16:06:23 +08:00
loadingBarCopy->setPosition(loadingBar->getPosition() + Vec2(0, -40));
2015-06-05 15:57:39 +08:00
loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
2021-12-28 16:06:23 +08:00
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 50));
button->setTitleText("Click to change direction!");
2015-06-05 15:57:39 +08:00
2021-12-28 16:06:23 +08:00
button->addTouchEventListener([=](Ref*, Widget::TouchEventType type) {
2015-06-05 15:57:39 +08:00
if (type == Widget::TouchEventType::ENDED)
{
if (loadingBar->getDirection() == LoadingBar::Direction::LEFT)
{
loadingBar->setDirection(LoadingBar::Direction::RIGHT);
2015-06-05 15:57:39 +08:00
loadingBarCopy->setDirection(LoadingBar::Direction::LEFT);
}
else
{
loadingBar->setDirection(LoadingBar::Direction::LEFT);
2015-06-05 15:57:39 +08:00
loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
}
}
});
2021-12-28 16:06:23 +08:00
_uiLayer->addChild(loadingBar, 1);
_uiLayer->addChild(loadingBarCopy, 2);
_uiLayer->addChild(button);
2015-11-30 16:17:13 +08:00
_loadingBar = loadingBar;
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(UILoadingBarTest_Left::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, 0));
this->addChild(pMenu1, 10);
2021-12-28 16:06:23 +08:00
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
void UILoadingBarTest_Left::update(float delta)
{
2013-12-23 15:35:35 +08:00
_count++;
if (_count > 100)
2013-09-16 20:54:13 +08:00
{
2013-12-23 15:35:35 +08:00
_count = 0;
2013-09-16 20:54:13 +08:00
}
2021-12-28 16:06:23 +08:00
LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
2015-06-05 15:57:39 +08:00
LoadingBar* loadingBarCopy = static_cast<LoadingBar*>(_uiLayer->getChildByTag(1));
2013-12-23 15:35:35 +08:00
loadingBar->setPercent(_count);
2015-06-05 15:57:39 +08:00
loadingBarCopy->setPercent(_count);
2013-09-16 20:54:13 +08:00
}
void UILoadingBarTest_Left::printWidgetResources(axis::Ref* sender)
2015-11-30 16:17:13 +08:00
{
axis::ResourceData textureFile = _loadingBar->getRenderFile();
2022-07-16 10:43:05 +08:00
AXLOG("textureFile Name : %s, Type: %d", textureFile.file.c_str(), textureFile.type);
2015-11-30 16:17:13 +08:00
}
2013-09-16 20:54:13 +08:00
// UILoadingBarTest_Right
2021-12-28 16:06:23 +08:00
UILoadingBarTest_Right::UILoadingBarTest_Right() : _count(0) {}
2013-09-16 20:54:13 +08:00
UILoadingBarTest_Right::~UILoadingBarTest_Right()
{
unscheduleUpdate();
}
bool UILoadingBarTest_Right::init()
{
if (UIScene::init())
{
scheduleUpdate();
2021-12-28 16:06:23 +08:00
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 the alert
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("LoadingBar right", "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 loading bar
2014-04-03 11:34:06 +08:00
LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderProgress.png");
2013-12-23 15:35:35 +08:00
loadingBar->setTag(0);
loadingBar->setDirection(LoadingBar::Direction::RIGHT);
2021-12-28 16:06:23 +08:00
loadingBar->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(loadingBar);
2021-12-28 16:06:23 +08:00
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
void UILoadingBarTest_Right::update(float delta)
{
2013-12-23 15:35:35 +08:00
_count++;
if (_count > 100)
2013-09-16 20:54:13 +08:00
{
2013-12-23 15:35:35 +08:00
_count = 0;
2013-09-16 20:54:13 +08:00
}
2013-12-23 15:35:35 +08:00
LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
loadingBar->setPercent(_count);
2013-09-16 20:54:13 +08:00
}
// UILoadingBarTest_Left_Scale9
2021-12-28 16:06:23 +08:00
UILoadingBarTest_Left_Scale9::UILoadingBarTest_Left_Scale9() : _count(0) {}
2013-09-16 20:54:13 +08:00
UILoadingBarTest_Left_Scale9::~UILoadingBarTest_Left_Scale9()
{
unscheduleUpdate();
}
bool UILoadingBarTest_Left_Scale9::init()
{
if (UIScene::init())
{
scheduleUpdate();
2021-12-28 16:06:23 +08:00
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 the alert
2014-04-03 11:34:06 +08:00
Text* alert = Text::create("LoadingBar left scale9 render", "fonts/Marker Felt.ttf", 20);
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 * 2.7f));
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 loading bar
2014-04-03 11:34:06 +08:00
LoadingBar* loadingBar = LoadingBar::create("cocosui/slider_bar_active_9patch.png");
2013-12-23 15:35:35 +08:00
loadingBar->setTag(0);
2013-09-16 20:54:13 +08:00
loadingBar->setScale9Enabled(true);
loadingBar->setCapInsets(Rect(0, 0, 0, 0));
loadingBar->setContentSize(Size(300, 13));
2021-12-28 16:06:23 +08:00
loadingBar->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(loadingBar);
2021-12-28 16:06:23 +08:00
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
void UILoadingBarTest_Left_Scale9::update(float delta)
{
2013-12-23 15:35:35 +08:00
_count++;
if (_count > 100)
2013-09-16 20:54:13 +08:00
{
2013-12-23 15:35:35 +08:00
_count = 0;
2013-09-16 20:54:13 +08:00
}
2013-12-23 15:35:35 +08:00
LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
loadingBar->setPercent(_count);
2013-09-16 20:54:13 +08:00
}
// UILoadingBarTest_Right_Scale9
2021-12-28 16:06:23 +08:00
UILoadingBarTest_Right_Scale9::UILoadingBarTest_Right_Scale9() : _count(0) {}
2013-09-16 20:54:13 +08:00
UILoadingBarTest_Right_Scale9::~UILoadingBarTest_Right_Scale9()
{
unscheduleUpdate();
}
bool UILoadingBarTest_Right_Scale9::init()
{
if (UIScene::init())
{
scheduleUpdate();
2021-12-28 16:06:23 +08:00
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 the alert
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("LoadingBar right scale9 render", "fonts/Marker Felt.ttf", 20);
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 * 2.7f));
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 loading bar
2014-04-03 11:34:06 +08:00
LoadingBar* loadingBar = LoadingBar::create("cocosui/slider_bar_active_9patch.png");
2013-12-23 15:35:35 +08:00
loadingBar->setTag(0);
2013-09-16 20:54:13 +08:00
loadingBar->setScale9Enabled(true);
loadingBar->setCapInsets(Rect(0, 0, 0, 0));
loadingBar->setContentSize(Size(300, 13));
loadingBar->setDirection(LoadingBar::Direction::RIGHT);
2021-12-28 16:06:23 +08:00
loadingBar->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(loadingBar);
2021-12-28 16:06:23 +08:00
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
void UILoadingBarTest_Right_Scale9::update(float delta)
{
2013-12-23 15:35:35 +08:00
_count++;
if (_count > 100)
2013-09-16 20:54:13 +08:00
{
2013-12-23 15:35:35 +08:00
_count = 0;
2013-09-16 20:54:13 +08:00
}
2013-12-23 15:35:35 +08:00
LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
loadingBar->setPercent(_count);
2013-09-16 20:54:13 +08:00
}
2015-01-20 20:57:54 +08:00
// UILoadingBarTest_Scale9_State_Change
2021-12-28 16:06:23 +08:00
UILoadingBarTest_Scale9_State_Change::UILoadingBarTest_Scale9_State_Change() : _count(0) {}
2015-01-20 20:57:54 +08:00
2021-12-28 16:06:23 +08:00
UILoadingBarTest_Scale9_State_Change::~UILoadingBarTest_Scale9_State_Change() {}
2015-01-20 20:57:54 +08:00
bool UILoadingBarTest_Scale9_State_Change::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add the alert
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("LoadingBar right scale9 render", "fonts/Marker Felt.ttf", 20);
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 * 2.7f));
2015-01-20 20:57:54 +08:00
_uiLayer->addChild(alert);
// Create the loading bar
LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderThumb.png");
loadingBar->setTag(0);
loadingBar->ignoreContentAdaptWithSize(false);
2021-12-28 16:06:23 +08:00
// loadingBar->setScale9Enabled(true);
2015-01-20 20:57:54 +08:00
loadingBar->setCapInsets(Rect(0, 0, 0, 0));
loadingBar->setContentSize(Size(200, 80));
loadingBar->setDirection(LoadingBar::Direction::LEFT);
loadingBar->setPercent(100);
loadingBar->setTouchEnabled(true);
2021-12-28 16:06:23 +08:00
loadingBar->addTouchEventListener([=](Ref* sender, Widget::TouchEventType type) {
if (type == Widget::TouchEventType::ENDED)
{
2015-01-20 20:57:54 +08:00
if (loadingBar->isScale9Enabled())
{
loadingBar->setScale9Enabled(false);
}
else
loadingBar->setScale9Enabled(true);
}
});
2021-12-28 16:06:23 +08:00
loadingBar->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
2015-01-20 20:57:54 +08:00
_uiLayer->addChild(loadingBar);
return true;
}
return false;
2015-01-26 13:49:54 +08:00
}
// UILoadingBarReloadTexture
2021-12-28 16:06:23 +08:00
UILoadingBarReloadTexture::UILoadingBarReloadTexture() : _count(0) {}
2015-01-26 13:49:54 +08:00
2021-12-28 16:06:23 +08:00
UILoadingBarReloadTexture::~UILoadingBarReloadTexture() {}
2015-01-26 13:49:54 +08:00
bool UILoadingBarReloadTexture::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
2015-01-26 13:49:54 +08:00
// Add the alert
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("Click button to Toggle Scale9 and switch Texture.", "fonts/Marker Felt.ttf", 20);
2015-01-26 13:49: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 * 2.7f));
2015-01-26 13:49:54 +08:00
_uiLayer->addChild(alert);
2021-12-28 16:06:23 +08:00
2015-01-26 13:49:54 +08:00
LoadingBar* loadingBar = LoadingBar::create("cocosui/slider_bar_active_9patch.png");
loadingBar->setTag(0);
loadingBar->ignoreContentAdaptWithSize(false);
2021-12-28 16:06:23 +08:00
// loadingBar->setScale9Enabled(true);
2015-01-26 13:49:54 +08:00
loadingBar->setCapInsets(Rect(0, 0, 0, 0));
loadingBar->setContentSize(Size(300, 13));
loadingBar->setName("texture0");
loadingBar->setDirection(LoadingBar::Direction::RIGHT);
2015-01-26 14:39:44 +08:00
loadingBar->setPercent(70);
2021-12-28 16:06:23 +08:00
loadingBar->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
2015-01-26 13:49:54 +08:00
_uiLayer->addChild(loadingBar);
2021-12-28 16:06:23 +08:00
auto buttonScale9 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
2015-01-26 13:49:54 +08:00
buttonScale9->setTitleText("ToggleScale9");
2021-12-28 16:06:23 +08:00
buttonScale9->addClickEventListener(
[=](Ref*) { loadingBar->setScale9Enabled(!loadingBar->isScale9Enabled()); });
buttonScale9->setPosition(loadingBar->getPosition() + Vec2(-50, 50));
2015-01-26 13:49:54 +08:00
_uiLayer->addChild(buttonScale9);
2021-12-28 16:06:23 +08:00
auto buttonChangeTexture =
Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
2015-01-26 13:49:54 +08:00
buttonChangeTexture->setTitleText("ChangeTexture");
2021-12-28 16:06:23 +08:00
buttonChangeTexture->addClickEventListener([=](Ref*) {
2015-01-26 13:49:54 +08:00
auto name = loadingBar->getName();
if (name == "texture0")
{
loadingBar->loadTexture("cocosui/slider_bar_active_9patch2.png");
loadingBar->setName("texture1");
}
else
{
loadingBar->loadTexture("cocosui/slider_bar_active_9patch.png");
loadingBar->setName("texture0");
}
});
2021-12-28 16:06:23 +08:00
buttonChangeTexture->setPosition(loadingBar->getPosition() + Vec2(50, 50));
2015-01-26 13:49:54 +08:00
_uiLayer->addChild(buttonChangeTexture);
2021-12-28 16:06:23 +08:00
2015-01-26 13:49:54 +08:00
this->scheduleUpdate();
return true;
}
return false;
}
void UILoadingBarReloadTexture::update(float delta)
{
_count++;
if (_count > 100)
{
_count = 0;
}
LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
loadingBar->setPercent(_count);
2015-06-05 15:57:39 +08:00
}
2015-12-09 17:15:09 +08:00
// UILoadingBarIssue12249
2021-12-28 16:06:23 +08:00
UILoadingBarIssue12249::UILoadingBarIssue12249() : _count(0) {}
2015-12-09 17:15:09 +08:00
UILoadingBarIssue12249::~UILoadingBarIssue12249()
{
unscheduleUpdate();
}
bool UILoadingBarIssue12249::init()
{
if (UIScene::init())
{
scheduleUpdate();
2021-12-28 16:06:23 +08:00
2015-12-09 17:15:09 +08:00
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
2015-12-09 17:15:09 +08:00
// Add the alert
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("Test LoadingBar Change Direction", "fonts/Marker Felt.ttf", 30);
2015-12-09 17:15:09 +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-12-09 17:15:09 +08:00
_uiLayer->addChild(alert);
2021-12-28 16:06:23 +08:00
2015-12-09 17:15:09 +08:00
// Create the loading bar
LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderProgress.png");
loadingBar->setScale9Enabled(true);
loadingBar->setContentSize(Size(200, loadingBar->getContentSize().height * 1.5));
loadingBar->setTag(0);
2021-12-28 16:06:23 +08:00
loadingBar->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
2015-12-09 17:15:09 +08:00
LoadingBar* loadingBarCopy = LoadingBar::create();
loadingBarCopy->setScale9Enabled(true);
loadingBarCopy->loadTexture("cocosui/sliderProgress.png");
loadingBarCopy->setContentSize(Size(200, loadingBarCopy->getContentSize().height * 1.5));
loadingBarCopy->setTag(1);
2021-12-28 16:06:23 +08:00
loadingBarCopy->setPosition(loadingBar->getPosition() + Vec2(0, -40));
2015-12-09 17:15:09 +08:00
loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
2021-12-28 16:06:23 +08:00
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
2015-12-09 17:15:09 +08:00
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 50));
button->setTitleText("Click to change direction!");
2021-12-28 16:06:23 +08:00
button->addTouchEventListener([=](Ref*, Widget::TouchEventType type) {
if (type == Widget::TouchEventType::ENDED)
{
if (loadingBar->getDirection() == LoadingBar::Direction::LEFT)
{
loadingBar->setDirection(LoadingBar::Direction::RIGHT);
loadingBarCopy->setDirection(LoadingBar::Direction::LEFT);
}
else
{
loadingBar->setDirection(LoadingBar::Direction::LEFT);
loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
}
}
});
_uiLayer->addChild(loadingBar, 1);
_uiLayer->addChild(loadingBarCopy, 2);
2015-12-09 17:15:09 +08:00
_uiLayer->addChild(button);
2021-12-28 16:06:23 +08:00
2015-12-09 17:15:09 +08:00
return true;
}
return false;
}
void UILoadingBarIssue12249::update(float delta)
{
_count++;
if (_count > 100)
{
_count = 0;
}
2021-12-28 16:06:23 +08:00
LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
2015-12-09 17:15:09 +08:00
LoadingBar* loadingBarCopy = static_cast<LoadingBar*>(_uiLayer->getChildByTag(1));
loadingBar->setPercent(_count);
loadingBarCopy->setPercent(_count);
}
2015-12-19 15:59:27 +08:00
// UILoadingBarTest_Direction
2021-12-28 16:06:23 +08:00
UILoadingBarTest_Direction::UILoadingBarTest_Direction() : _count(0) {}
2015-12-19 15:59:27 +08:00
UILoadingBarTest_Direction::~UILoadingBarTest_Direction()
{
2021-12-28 16:06:23 +08:00
// unscheduleUpdate();
2015-12-19 15:59:27 +08:00
}
bool UILoadingBarTest_Direction::init()
{
if (UIScene::init())
{
2021-12-28 16:06:23 +08:00
// scheduleUpdate();
2015-12-19 15:59:27 +08:00
Size widgetSize = _widget->getContentSize();
// Add the alert
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("Test LoadingBar Change Direction", "fonts/Marker Felt.ttf", 30);
2015-12-19 15:59:27 +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-12-19 15:59:27 +08:00
_uiLayer->addChild(alert);
// Create the loading bar
LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderProgress.png");
loadingBar->setTag(0);
loadingBar->setPercent(80);
2021-12-28 16:06:23 +08:00
loadingBar->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
2015-12-19 15:59:27 +08:00
auto loadingBarCopy = (LoadingBar*)loadingBar->clone();
loadingBarCopy->setTag(1);
2021-12-28 16:06:23 +08:00
loadingBarCopy->setPosition(loadingBar->getPosition() + Vec2(0, -40));
2015-12-19 15:59:27 +08:00
loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
2021-12-28 16:06:23 +08:00
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
2015-12-19 15:59:27 +08:00
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 50));
button->setTitleText("Click to change direction!");
2021-12-28 16:06:23 +08:00
button->addTouchEventListener([=](Ref*, Widget::TouchEventType type) {
2015-12-19 15:59:27 +08:00
if (type == Widget::TouchEventType::ENDED)
{
if (loadingBar->getDirection() == LoadingBar::Direction::LEFT)
{
loadingBar->setDirection(LoadingBar::Direction::RIGHT);
loadingBarCopy->setDirection(LoadingBar::Direction::LEFT);
}
else
{
loadingBar->setDirection(LoadingBar::Direction::LEFT);
loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
}
}
});
_uiLayer->addChild(loadingBar, 1);
_uiLayer->addChild(loadingBarCopy, 2);
_uiLayer->addChild(button);
_loadingBar = loadingBar;
return true;
}
return false;
}