2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +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-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "UIScrollViewTest.h"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
using namespace cocos2d::ui;
|
|
|
|
|
|
|
|
UIScrollViewTests::UIScrollViewTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(UIScrollViewTest_Vertical);
|
|
|
|
ADD_TEST_CASE(UIScrollViewTest_Horizontal);
|
|
|
|
ADD_TEST_CASE(UIScrollViewTest_Both);
|
|
|
|
ADD_TEST_CASE(UIScrollViewTest_ScrollToPercentBothDirection);
|
|
|
|
ADD_TEST_CASE(UIScrollViewTest_ScrollToPercentBothDirection_Bounce);
|
|
|
|
ADD_TEST_CASE(UIScrollViewNestTest);
|
|
|
|
ADD_TEST_CASE(UIScrollViewRotated);
|
|
|
|
ADD_TEST_CASE(UIScrollViewDisableTest);
|
|
|
|
ADD_TEST_CASE(UIScrollViewInnerSize);
|
|
|
|
ADD_TEST_CASE(UIScrollViewTestEvents);
|
|
|
|
ADD_TEST_CASE(UIScrollViewStopScrollingTest);
|
|
|
|
}
|
|
|
|
// UIScrollViewTest_Vertical
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewTest_Vertical::UIScrollViewTest_Vertical() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewTest_Vertical::~UIScrollViewTest_Vertical() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UIScrollViewTest_Vertical::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the scrollview alert will be displayed
|
|
|
|
_displayValueLabel = Text::create("Move by vertical direction", "fonts/Marker Felt.ttf", 32);
|
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
|
|
|
Text* alert = Text::create("ScrollView vertical", "fonts/Marker Felt.ttf", 30);
|
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the scrollview by vertical
|
|
|
|
ui::ScrollView* scrollView = ui::ScrollView::create();
|
|
|
|
scrollView->setContentSize(Size(280.0f, 100.0f));
|
|
|
|
Size backgroundSize = background->getContentSize();
|
|
|
|
scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
|
|
|
|
(widgetSize.height - backgroundSize.height) / 2.0f +
|
|
|
|
(backgroundSize.height - scrollView->getContentSize().height) / 2.0f));
|
|
|
|
scrollView->setScrollBarWidth(4);
|
|
|
|
scrollView->setScrollBarPositionFromCorner(Vec2(2, 2));
|
|
|
|
scrollView->setScrollBarColor(Color3B::WHITE);
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->setGlobalZOrder(200);
|
|
|
|
_uiLayer->addChild(scrollView);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ImageView* imageView = ImageView::create("cocosui/ccicon.png");
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
float innerWidth = scrollView->getContentSize().width;
|
2019-11-23 20:27:39 +08:00
|
|
|
float innerHeight = scrollView->getContentSize().height + imageView->getContentSize().height;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2021-12-31 12:12:40 +08:00
|
|
|
button->setPosition(Vec2(innerWidth / 2.0f,
|
|
|
|
scrollView->getInnerContainerSize().height - button->getContentSize().height / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(button);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
|
|
|
|
titleButton->setTitleText("Title Button");
|
2021-12-31 12:12:40 +08:00
|
|
|
titleButton->setPosition(
|
|
|
|
Vec2(innerWidth / 2.0f, button->getBottomBoundary() - button->getContentSize().height));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(titleButton);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
|
|
|
|
button_scale9->setScale9Enabled(true);
|
|
|
|
button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
|
2021-12-31 12:12:40 +08:00
|
|
|
button_scale9->setPosition(
|
|
|
|
Vec2(innerWidth / 2.0f, titleButton->getBottomBoundary() - titleButton->getContentSize().height));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(button_scale9);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
imageView->setPosition(Vec2(innerWidth / 2.0f, imageView->getContentSize().height / 2.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
scrollView->addChild(imageView);
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->scrollToBottom(2.0, true);
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIScrollViewTest_Horizontal
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewTest_Horizontal::UIScrollViewTest_Horizontal() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewTest_Horizontal::~UIScrollViewTest_Horizontal() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UIScrollViewTest_Horizontal::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the scrollview alert will be displayed
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel = Text::create("Move by horizontal direction", "fonts/Marker Felt.ttf", 32);
|
2019-11-23 20:27:39 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
|
|
|
|
|
|
|
Text* alert = Text::create("ScrollView horizontal", "fonts/Marker Felt.ttf", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the scrollview by horizontal
|
|
|
|
ui::ScrollView* scrollView = ui::ScrollView::create();
|
|
|
|
scrollView->setBounceEnabled(true);
|
|
|
|
scrollView->setDirection(ui::ScrollView::Direction::HORIZONTAL);
|
|
|
|
scrollView->setContentSize(Size(280.0f, 150.0f));
|
|
|
|
scrollView->setInnerContainerSize(scrollView->getContentSize());
|
2021-12-31 12:12:40 +08:00
|
|
|
scrollView->setScrollBarPositionFromCorner(Vec2(4, 4));
|
|
|
|
scrollView->setScrollBarColor(Color3B::YELLOW);
|
2019-11-23 20:27:39 +08:00
|
|
|
Size backgroundSize = background->getContentSize();
|
|
|
|
scrollView->setPosition((widgetSize - scrollView->getContentSize()) / 2.0f);
|
|
|
|
_uiLayer->addChild(scrollView);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ImageView* imageView = ImageView::create("cocosui/ccicon.png");
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
float innerWidth = scrollView->getContentSize().width + imageView->getContentSize().width;
|
2019-11-23 20:27:39 +08:00
|
|
|
float innerHeight = scrollView->getContentSize().height;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
|
|
|
button->setPosition(Vec2(button->getContentSize().width / 2.0f,
|
2021-12-31 12:12:40 +08:00
|
|
|
scrollView->getInnerContainerSize().height - button->getContentSize().height / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(button);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
|
|
|
|
titleButton->setTitleText("Title Button");
|
|
|
|
titleButton->setPosition(Vec2(button->getRightBoundary() + button->getContentSize().width / 2.0f,
|
2021-12-31 12:12:40 +08:00
|
|
|
button->getBottomBoundary() - button->getContentSize().height / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(titleButton);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
|
|
|
|
button_scale9->setScale9Enabled(true);
|
|
|
|
button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
|
2021-12-31 12:12:40 +08:00
|
|
|
button_scale9->setPosition(
|
|
|
|
Vec2(titleButton->getRightBoundary() + titleButton->getContentSize().width / 2.0f,
|
|
|
|
titleButton->getBottomBoundary() - titleButton->getContentSize().height / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(button_scale9);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
imageView->setPosition(
|
|
|
|
Vec2(innerWidth - imageView->getContentSize().width / 2.0f,
|
|
|
|
button_scale9->getBottomBoundary() - button_scale9->getContentSize().height / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(imageView);
|
|
|
|
|
|
|
|
// Jump to right
|
|
|
|
scrollView->jumpToRight();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIScrollViewTest_Both
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewTest_Both::UIScrollViewTest_Both() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewTest_Both::~UIScrollViewTest_Both() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UIScrollViewTest_Both::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the dragpanel events will be displayed
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel = Text::create("Move by any direction", "fonts/Marker Felt.ttf", 32);
|
2019-11-23 20:27:39 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
2021-12-31 12:12:40 +08:00
|
|
|
Text* alert = Text::create("ScrollView both", "fonts/Marker Felt.ttf", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the dragpanel
|
|
|
|
ui::ScrollView* scrollView = ui::ScrollView::create();
|
|
|
|
scrollView->setDirection(ui::ScrollView::Direction::BOTH);
|
|
|
|
scrollView->setTouchEnabled(true);
|
|
|
|
scrollView->setBounceEnabled(true);
|
|
|
|
scrollView->setBackGroundImageScale9Enabled(true);
|
|
|
|
scrollView->setBackGroundImage("cocosui/green_edit.png");
|
|
|
|
scrollView->setContentSize(Size(210, 122.5));
|
2021-12-31 12:12:40 +08:00
|
|
|
scrollView->setScrollBarWidth(4);
|
|
|
|
scrollView->setScrollBarPositionFromCorner(Vec2(6, 6));
|
2019-11-23 20:27:39 +08:00
|
|
|
Size backgroundSize = background->getContentSize();
|
|
|
|
scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
|
|
|
|
(widgetSize.height - backgroundSize.height) / 2.0f +
|
|
|
|
(backgroundSize.height - scrollView->getContentSize().height) / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
ImageView* imageView = ImageView::create("Hello.png");
|
|
|
|
scrollView->addChild(imageView);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->setInnerContainerSize(imageView->getContentSize());
|
|
|
|
Size innerSize = scrollView->getInnerContainerSize();
|
|
|
|
imageView->setPosition(Vec2(innerSize.width / 2.0f, innerSize.height / 2.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(scrollView);
|
|
|
|
|
|
|
|
// Jump to right bottom
|
|
|
|
scrollView->jumpToBottomRight();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIScrollViewTest_ScrollToPercentBothDirection
|
|
|
|
UIScrollViewTest_ScrollToPercentBothDirection::UIScrollViewTest_ScrollToPercentBothDirection()
|
2021-12-31 12:12:40 +08:00
|
|
|
: _displayValueLabel(nullptr)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewTest_ScrollToPercentBothDirection::~UIScrollViewTest_ScrollToPercentBothDirection() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UIScrollViewTest_ScrollToPercentBothDirection::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the dragpanel events will be displayed
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
2021-12-31 12:12:40 +08:00
|
|
|
Text* alert =
|
|
|
|
Text::create("ScrollView scroll to percent both direction without scroll bar", "fonts/Marker Felt.ttf", 20);
|
2019-11-23 20:27:39 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ui::ScrollView* sc = ui::ScrollView::create();
|
|
|
|
sc->setBackGroundColor(Color3B::GREEN);
|
|
|
|
sc->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
|
|
|
|
sc->setDirection(ui::ScrollView::Direction::BOTH);
|
|
|
|
sc->setInnerContainerSize(Size(480.0f, 320.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
sc->setContentSize(Size(100.0f, 100.0f));
|
|
|
|
sc->setScrollBarEnabled(false);
|
2019-11-23 20:27:39 +08:00
|
|
|
Size backgroundSize = background->getContentSize();
|
|
|
|
sc->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.width - sc->getContentSize().width) / 2.0f,
|
|
|
|
(widgetSize.height - backgroundSize.height) / 2.0f +
|
|
|
|
(backgroundSize.height - sc->getContentSize().height) / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
sc->scrollToPercentBothDirection(Vec2(50, 50), 1, true);
|
|
|
|
ImageView* iv = ImageView::create("cocosui/Hello.png");
|
|
|
|
iv->setPosition(Vec2(240.0f, 160.0f));
|
|
|
|
sc->addChild(iv);
|
|
|
|
_uiLayer->addChild(sc);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIScrollViewTest_ScrollToPercentBothDirection_Bounce
|
|
|
|
UIScrollViewTest_ScrollToPercentBothDirection_Bounce::UIScrollViewTest_ScrollToPercentBothDirection_Bounce()
|
2021-12-31 12:12:40 +08:00
|
|
|
: _displayValueLabel(nullptr)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewTest_ScrollToPercentBothDirection_Bounce::~UIScrollViewTest_ScrollToPercentBothDirection_Bounce() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UIScrollViewTest_ScrollToPercentBothDirection_Bounce::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the dragpanel events will be displayed
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
2019-11-23 20:27:39 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
2021-12-31 12:12:40 +08:00
|
|
|
Text* alert = Text::create("ScrollView scroll to percent both direction bounce", "fonts/Marker Felt.ttf", 20);
|
2019-11-23 20:27:39 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ui::ScrollView* sc = ui::ScrollView::create();
|
|
|
|
sc->setBackGroundColor(Color3B::GREEN);
|
|
|
|
sc->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
|
|
|
|
sc->setBounceEnabled(true);
|
|
|
|
sc->setDirection(ui::ScrollView::Direction::BOTH);
|
|
|
|
sc->setInnerContainerSize(Size(480.0f, 320.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
sc->setContentSize(Size(100.0f, 100.0f));
|
|
|
|
sc->setScrollBarPositionFromCornerForHorizontal(Vec2(5.0f, sc->getContentSize().height - 5));
|
|
|
|
sc->setScrollBarPositionFromCornerForVertical(Vec2(sc->getContentSize().width - 5, 5.0f));
|
|
|
|
sc->setScrollBarAutoHideEnabled(false);
|
2019-11-23 20:27:39 +08:00
|
|
|
Size backgroundSize = background->getContentSize();
|
|
|
|
sc->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.width - sc->getContentSize().width) / 2.0f,
|
|
|
|
(widgetSize.height - backgroundSize.height) / 2.0f +
|
|
|
|
(backgroundSize.height - sc->getContentSize().height) / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
sc->scrollToPercentBothDirection(Vec2(50.0f, 50.0f), 1, true);
|
|
|
|
ImageView* iv = ImageView::create("cocosui/Hello.png");
|
|
|
|
iv->setPosition(Vec2(240.0f, 160.0f));
|
|
|
|
sc->addChild(iv);
|
|
|
|
_uiLayer->addChild(sc);
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIScrollViewNestTest
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewNestTest::UIScrollViewNestTest() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewNestTest::~UIScrollViewNestTest() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UIScrollViewNestTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the scrollview alert will be displayed
|
|
|
|
_displayValueLabel = Text::create("Move by vertical direction", "fonts/Marker Felt.ttf", 32);
|
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
|
|
|
Text* alert = Text::create("ScrollView vertical", "fonts/Marker Felt.ttf", 30);
|
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the scrollview by vertical
|
|
|
|
ui::ScrollView* scrollView = ui::ScrollView::create();
|
|
|
|
scrollView->setContentSize(Size(280.0f, 150.0f));
|
|
|
|
scrollView->setDirection(ui::ScrollView::Direction::VERTICAL);
|
2021-12-31 12:12:40 +08:00
|
|
|
scrollView->setScrollBarPositionFromCorner(Vec2(4, 4));
|
2019-11-23 20:27:39 +08:00
|
|
|
Size backgroundSize = background->getContentSize();
|
|
|
|
scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
|
2019-11-23 20:27:39 +08:00
|
|
|
(widgetSize.height - backgroundSize.height) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.height - scrollView->getContentSize().height) / 2.0f));
|
|
|
|
scrollView->setScrollBarPositionFromCornerForVertical(Vec2(scrollView->getContentSize().width - 4, 4.0f));
|
|
|
|
scrollView->setScrollBarColor(Color3B::BLUE);
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(scrollView);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ImageView* imageView = ImageView::create("cocosui/ccicon.png");
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
float innerWidth = scrollView->getContentSize().width;
|
2019-11-23 20:27:39 +08:00
|
|
|
float innerHeight = scrollView->getContentSize().height + imageView->getContentSize().height;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2021-12-31 12:12:40 +08:00
|
|
|
button->setPosition(Vec2(innerWidth / 2.0f,
|
|
|
|
scrollView->getInnerContainerSize().height - button->getContentSize().height / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(button);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
|
|
|
|
titleButton->setTitleText("Title Button");
|
2021-12-31 12:12:40 +08:00
|
|
|
titleButton->setPosition(
|
|
|
|
Vec2(innerWidth / 2.0f, button->getBottomBoundary() - button->getContentSize().height));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(titleButton);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
|
|
|
|
button_scale9->setScale9Enabled(true);
|
|
|
|
button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
|
2021-12-31 12:12:40 +08:00
|
|
|
button_scale9->setPosition(
|
|
|
|
Vec2(innerWidth / 2.0f, titleButton->getBottomBoundary() - titleButton->getContentSize().height));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(button_scale9);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
imageView->setPosition(Vec2(innerWidth / 2.0f, imageView->getContentSize().height / 2.0f));
|
|
|
|
scrollView->addChild(imageView);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the scrollview by both
|
|
|
|
ui::ScrollView* sc = ui::ScrollView::create();
|
|
|
|
sc->setBackGroundColor(Color3B::GREEN);
|
|
|
|
sc->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
|
|
|
|
sc->setBounceEnabled(true);
|
|
|
|
sc->setDirection(ui::ScrollView::Direction::BOTH);
|
|
|
|
sc->setInnerContainerSize(Size(480.0f, 320.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
sc->setContentSize(Size(100.0f, 100.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
sc->setPropagateTouchEvents(false);
|
2021-12-31 12:12:40 +08:00
|
|
|
sc->setPosition(Vec2(180.0f, 100.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
sc->scrollToPercentBothDirection(Vec2(50.0f, 50.0f), 1, true);
|
2021-12-31 12:12:40 +08:00
|
|
|
sc->setScrollBarPositionFromCorner(Vec2(4.0f, 4.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
ImageView* iv = ImageView::create("cocosui/Hello.png");
|
|
|
|
iv->setPosition(Vec2(240.0f, 160.0f));
|
|
|
|
sc->addChild(iv);
|
|
|
|
|
|
|
|
scrollView->addChild(sc);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIScrollViewRotated
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewRotated::UIScrollViewRotated() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewRotated::~UIScrollViewRotated() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UIScrollViewRotated::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the scrollview alert will be displayed
|
|
|
|
_displayValueLabel = Text::create("Move by vertical direction", "fonts/Marker Felt.ttf", 32);
|
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
|
|
|
Text* alert = Text::create("ScrollView vertical", "fonts/Marker Felt.ttf", 30);
|
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the scrollview by vertical
|
|
|
|
ui::ScrollView* scrollView = ui::ScrollView::create();
|
|
|
|
scrollView->setContentSize(Size(280.0f, 150.0f));
|
|
|
|
scrollView->setDirection(ui::ScrollView::Direction::VERTICAL);
|
|
|
|
Size backgroundSize = background->getContentSize();
|
|
|
|
scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
|
2019-11-23 20:27:39 +08:00
|
|
|
(widgetSize.height - backgroundSize.height) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.height - scrollView->getContentSize().height) / 2.0f + 100));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->setRotation(45);
|
2021-12-31 12:12:40 +08:00
|
|
|
scrollView->setScrollBarPositionFromCorner(Vec2(4, 4));
|
|
|
|
scrollView->setScrollBarColor(Color3B::WHITE);
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(scrollView);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ImageView* imageView = ImageView::create("cocosui/ccicon.png");
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
float innerWidth = scrollView->getContentSize().width;
|
2019-11-23 20:27:39 +08:00
|
|
|
float innerHeight = scrollView->getContentSize().height + imageView->getContentSize().height;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2021-12-31 12:12:40 +08:00
|
|
|
button->setPosition(Vec2(innerWidth / 2.0f,
|
|
|
|
scrollView->getInnerContainerSize().height - button->getContentSize().height / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(button);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
|
|
|
|
titleButton->setTitleText("Title Button");
|
2021-12-31 12:12:40 +08:00
|
|
|
titleButton->setPosition(
|
|
|
|
Vec2(innerWidth / 2.0f, button->getBottomBoundary() - button->getContentSize().height));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(titleButton);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
|
|
|
|
button_scale9->setScale9Enabled(true);
|
|
|
|
button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
|
2021-12-31 12:12:40 +08:00
|
|
|
button_scale9->setPosition(
|
|
|
|
Vec2(innerWidth / 2.0f, titleButton->getBottomBoundary() - titleButton->getContentSize().height));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(button_scale9);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
imageView->setPosition(Vec2(innerWidth / 2.0f, imageView->getContentSize().height / 2.0f));
|
|
|
|
scrollView->addChild(imageView);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIScrollViewDisableTest
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewDisableTest::UIScrollViewDisableTest() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewDisableTest::~UIScrollViewDisableTest() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UIScrollViewDisableTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
|
|
|
|
|
|
|
// Add a label in which the scrollview alert will be displayed
|
|
|
|
_displayValueLabel = Text::create("ScrollView Disable Test", "fonts/Marker Felt.ttf", 32);
|
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
|
|
|
|
|
|
|
// Add the alert
|
|
|
|
Text* alert = Text::create("ScrollView vertical", "fonts/Marker Felt.ttf", 30);
|
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
|
|
|
|
|
|
|
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
|
|
|
|
|
|
|
|
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
|
|
|
|
|
|
|
|
// Create the scrollview by vertical
|
|
|
|
ui::ScrollView* scrollView = ui::ScrollView::create();
|
|
|
|
scrollView->setContentSize(Size(280.0f, 100.0f));
|
|
|
|
Size backgroundSize = background->getContentSize();
|
|
|
|
scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
|
|
|
|
(widgetSize.height - backgroundSize.height) / 2.0f +
|
|
|
|
(backgroundSize.height - scrollView->getContentSize().height) / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->setScrollBarWidth(4);
|
|
|
|
scrollView->setTouchEnabled(false);
|
|
|
|
scrollView->setScrollBarPositionFromCorner(Vec2(2, 2));
|
|
|
|
scrollView->setScrollBarColor(Color3B::WHITE);
|
|
|
|
_uiLayer->addChild(scrollView);
|
|
|
|
|
|
|
|
ImageView* imageView = ImageView::create("cocosui/ccicon.png");
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
float innerWidth = scrollView->getContentSize().width;
|
2019-11-23 20:27:39 +08:00
|
|
|
float innerHeight = scrollView->getContentSize().height + imageView->getContentSize().height;
|
|
|
|
|
|
|
|
scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
|
|
|
|
|
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2021-12-31 12:12:40 +08:00
|
|
|
button->setPosition(Vec2(innerWidth / 2.0f,
|
|
|
|
scrollView->getInnerContainerSize().height - button->getContentSize().height / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(button);
|
|
|
|
|
|
|
|
Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
|
|
|
|
titleButton->setTitleText("Title Button");
|
2021-12-31 12:12:40 +08:00
|
|
|
titleButton->setPosition(
|
|
|
|
Vec2(innerWidth / 2.0f, button->getBottomBoundary() - button->getContentSize().height));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(titleButton);
|
|
|
|
|
|
|
|
Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
|
|
|
|
button_scale9->setScale9Enabled(true);
|
|
|
|
button_scale9->setContentSize(Size(100.0f, button_scale9->getVirtualRendererSize().height));
|
2021-12-31 12:12:40 +08:00
|
|
|
button_scale9->setPosition(
|
|
|
|
Vec2(innerWidth / 2.0f, titleButton->getBottomBoundary() - titleButton->getContentSize().height));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addChild(button_scale9);
|
|
|
|
|
|
|
|
imageView->setPosition(Vec2(innerWidth / 2.0f, imageView->getContentSize().height / 2.0f));
|
|
|
|
scrollView->addChild(imageView);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIScrollViewInnerSize
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewInnerSize::UIScrollViewInnerSize() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewInnerSize::~UIScrollViewInnerSize() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UIScrollViewInnerSize::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
|
|
|
|
|
|
|
// Add a label in which the scrollview alert will be displayed
|
|
|
|
_displayValueLabel = Text::create("ScrollView InnerSize Test", "fonts/Marker Felt.ttf", 32);
|
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
|
|
|
|
|
|
|
// Add the alert
|
2021-12-31 12:12:40 +08:00
|
|
|
Text* alert = Text::create("ScrollView & InnerContainer should be align to the left and top point",
|
|
|
|
"fonts/Marker Felt.ttf", 10);
|
2019-11-23 20:27:39 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
|
|
|
|
|
|
|
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
|
|
|
|
|
|
|
|
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
|
|
|
|
|
|
|
|
// Create the scrollview by vertical
|
|
|
|
ui::ScrollView* scrollView = ui::ScrollView::create();
|
|
|
|
scrollView->setContentSize(Size(200.0f, 50.0f));
|
|
|
|
Size backgroundSize = background->getContentSize();
|
|
|
|
scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
|
|
|
|
(widgetSize.height - backgroundSize.height) / 2.0f +
|
|
|
|
(backgroundSize.height - scrollView->getContentSize().height) / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->setScrollBarWidth(4);
|
|
|
|
scrollView->setTouchEnabled(false);
|
|
|
|
scrollView->setScrollBarPositionFromCorner(Vec2(2, 2));
|
|
|
|
scrollView->setScrollBarColor(Color3B::WHITE);
|
|
|
|
scrollView->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
|
|
|
|
scrollView->setBackGroundColor(Color3B(150, 150, 100));
|
|
|
|
scrollView->getInnerContainer()->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
|
|
|
|
scrollView->getInnerContainer()->setBackGroundColor(Color3B(50, 100, 50));
|
|
|
|
scrollView->getInnerContainer()->setBackGroundColorOpacity(100);
|
|
|
|
scrollView->setClippingEnabled(false);
|
|
|
|
_uiLayer->addChild(scrollView);
|
|
|
|
|
|
|
|
scrollView->setInnerContainerSize(Size(250.f, 100.f));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIScrollViewTestEvents
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIScrollViewTestEvents::UIScrollViewTestEvents() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UIScrollViewTestEvents::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the dragpanel events will be displayed
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel = Text::create("(no events)", "fonts/Marker Felt.ttf", 32);
|
2019-11-23 20:27:39 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
2021-12-31 12:12:40 +08:00
|
|
|
Text* alert = Text::create("ScrollView events", "fonts/Marker Felt.ttf", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the dragpanel
|
|
|
|
ui::ScrollView* scrollView = ui::ScrollView::create();
|
|
|
|
scrollView->setDirection(ui::ScrollView::Direction::BOTH);
|
|
|
|
scrollView->setTouchEnabled(true);
|
|
|
|
scrollView->setBounceEnabled(true);
|
|
|
|
scrollView->setBackGroundImageScale9Enabled(true);
|
|
|
|
scrollView->setBackGroundImage("cocosui/green_edit.png");
|
|
|
|
scrollView->setContentSize(Size(210, 122.5));
|
|
|
|
scrollView->setScrollBarWidth(4);
|
|
|
|
scrollView->setScrollBarPositionFromCorner(Vec2(6, 6));
|
|
|
|
Size backgroundSize = background->getContentSize();
|
|
|
|
scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.width - scrollView->getContentSize().width) / 2.0f,
|
2019-11-23 20:27:39 +08:00
|
|
|
(widgetSize.height - backgroundSize.height) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.height - scrollView->getContentSize().height) / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
ImageView* imageView = ImageView::create("Hello.png");
|
|
|
|
scrollView->addChild(imageView);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->setInnerContainerSize(imageView->getContentSize());
|
|
|
|
Size innerSize = scrollView->getInnerContainerSize();
|
|
|
|
imageView->setPosition(Vec2(innerSize.width / 2.0f, innerSize.height / 2.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(scrollView);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Jump to right bottom
|
|
|
|
scrollView->jumpToBottomRight();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
auto getRandomColor = [] { return Color4B(random(0, 255), random(0, 255), random(0, 255), 255); };
|
2019-11-23 20:27:39 +08:00
|
|
|
scrollView->addEventListener([&](Ref*, ui::ScrollView::EventType e) {
|
2021-12-31 12:12:40 +08:00
|
|
|
switch (e)
|
|
|
|
{
|
|
|
|
case ui::ScrollView::EventType::SCROLLING_BEGAN:
|
|
|
|
_displayValueLabel->setString("scrolling began!");
|
|
|
|
_displayValueLabel->setTextColor(getRandomColor());
|
|
|
|
break;
|
|
|
|
case ui::ScrollView::EventType::SCROLLING_ENDED:
|
|
|
|
_displayValueLabel->setString("scrolling ended!");
|
|
|
|
_displayValueLabel->setTextColor(getRandomColor());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
});
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIScrollViewStopScrollingTest
|
|
|
|
|
|
|
|
UIScrollViewStopScrollingTest::UIScrollViewStopScrollingTest()
|
2021-12-31 12:12:40 +08:00
|
|
|
: _displayValueLabel(nullptr), _scrollView(nullptr), _remainingTime(0.0f)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UIScrollViewStopScrollingTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
|
|
|
|
|
|
|
// Add a label in which the time remaining till scrolling stop will be displayed.
|
|
|
|
_displayValueLabel = Text::create("Scrolling stop isn't scheduled", "fonts/Marker Felt.ttf", 32);
|
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
|
|
|
|
|
|
|
// Add the alert
|
|
|
|
Text* alert = Text::create("Click the button and start to scroll", "fonts/Marker Felt.ttf", 30);
|
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
|
|
|
|
|
|
|
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
|
|
|
|
|
|
|
|
Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
|
|
|
|
|
|
|
|
// Create the dragpanel
|
|
|
|
_scrollView = ui::ScrollView::create();
|
|
|
|
_scrollView->setDirection(ui::ScrollView::Direction::BOTH);
|
|
|
|
_scrollView->setTouchEnabled(true);
|
|
|
|
_scrollView->setBounceEnabled(true);
|
|
|
|
_scrollView->setBackGroundImageScale9Enabled(true);
|
|
|
|
_scrollView->setBackGroundImage("cocosui/green_edit.png");
|
|
|
|
_scrollView->setContentSize(Size(210, 122.5));
|
|
|
|
_scrollView->setScrollBarWidth(4);
|
|
|
|
_scrollView->setScrollBarPositionFromCorner(Vec2(6, 6));
|
|
|
|
Size backgroundSize = background->getContentSize();
|
|
|
|
_scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
|
2021-12-31 12:12:40 +08:00
|
|
|
(backgroundSize.width - _scrollView->getContentSize().width) / 2.0f,
|
|
|
|
(widgetSize.height - backgroundSize.height) / 2.0f +
|
|
|
|
(backgroundSize.height - _scrollView->getContentSize().height) / 2.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
ImageView* imageView = ImageView::create("Hello.png");
|
|
|
|
_scrollView->addChild(imageView);
|
|
|
|
_scrollView->setInnerContainerSize(imageView->getContentSize());
|
|
|
|
Size innerSize = _scrollView->getInnerContainerSize();
|
|
|
|
imageView->setPosition(Vec2(innerSize.width / 2.0f, innerSize.height / 2.0f));
|
|
|
|
_uiLayer->addChild(_scrollView);
|
|
|
|
|
|
|
|
// Log some ScrollView events.
|
2021-12-31 12:12:40 +08:00
|
|
|
_scrollView->addEventListener([&](Ref*, ui::ScrollView::EventType e) {
|
2019-11-23 20:27:39 +08:00
|
|
|
switch (e)
|
|
|
|
{
|
|
|
|
case ui::ScrollView::EventType::SCROLLING_BEGAN:
|
|
|
|
CCLOG("scrolling began!");
|
|
|
|
break;
|
|
|
|
case ui::ScrollView::EventType::SCROLLING_ENDED:
|
|
|
|
CCLOG("scrolling ended!");
|
|
|
|
break;
|
|
|
|
case ui::ScrollView::EventType::AUTOSCROLL_ENDED:
|
|
|
|
CCLOG("auto-scrolling ended!");
|
|
|
|
break;
|
2021-12-31 12:12:40 +08:00
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Jump to right bottom
|
|
|
|
_scrollView->jumpToBottomRight();
|
|
|
|
|
|
|
|
// Add button that will schedule scrolling stop when it is clicked.
|
|
|
|
Button* button_scale9 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
|
|
|
|
button_scale9->setTitleText("Stop scrolling in 3 sec.");
|
|
|
|
button_scale9->setScale9Enabled(true);
|
|
|
|
button_scale9->setContentSize(Size(120.0f, button_scale9->getVirtualRendererSize().height));
|
|
|
|
button_scale9->setPosition(Vec2(innerSize.width / 2.0f, innerSize.height / 2.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
button_scale9->addClickEventListener([this](Ref*) { this->_remainingTime = 3.0f; });
|
2019-11-23 20:27:39 +08:00
|
|
|
_scrollView->addChild(button_scale9);
|
|
|
|
|
|
|
|
// Schedule update for this scene.
|
|
|
|
Director::getInstance()->getScheduler()->scheduleUpdate(this, 0, false);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIScrollViewStopScrollingTest::update(float dt)
|
|
|
|
{
|
|
|
|
UIScene::update(dt);
|
|
|
|
|
|
|
|
if (_remainingTime > 0.0f)
|
|
|
|
{
|
|
|
|
_remainingTime -= dt;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_remainingTime > 0.0f)
|
|
|
|
{
|
|
|
|
// Update timer caption.
|
|
|
|
char strRemainingTime[100];
|
|
|
|
sprintf(strRemainingTime, "Stop scrolling in %.1f sec.", _remainingTime);
|
|
|
|
_displayValueLabel->setString(std::string(strRemainingTime));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_scrollView->stopOverallScroll();
|
|
|
|
|
|
|
|
// Update timer caption.
|
|
|
|
std::string strRemainingTime = "Scrolling stop isn't scheduled";
|
|
|
|
_displayValueLabel->setString(strRemainingTime);
|
|
|
|
|
|
|
|
// Show hint label.
|
2021-12-31 12:12:40 +08:00
|
|
|
auto hintLabel = Label::createWithTTF("Stopped!", "fonts/Marker Felt.ttf", 32.0f);
|
2019-11-23 20:27:39 +08:00
|
|
|
Size contentSize = _uiLayer->getContentSize();
|
|
|
|
hintLabel->setPosition(Vec2(contentSize.width / 2.0f, contentSize.height / 2.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
hintLabel->runAction(Spawn::createWithTwoActions(FadeOut::create(0.3f), ScaleTo::create(0.3f, 2.0f)));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(hintLabel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|