add jumpToPage API to PageView.

1. This commit also fix the default font size issue in ui::Button.
This commit is contained in:
andyque 2015-07-15 17:02:02 +08:00
parent d291afe295
commit 517221a0df
5 changed files with 147 additions and 3 deletions

View File

@ -767,6 +767,7 @@ void Button::setTitleText(const std::string& text)
this->createTitleRenderer();
}
_titleRenderer->setString(text);
this->setTitleFontSize(_fontSize);
updateContentSize();
}

View File

@ -259,6 +259,15 @@ void PageView::updateAllPagesPosition()
}
}
void PageView::setCurPageIndex( ssize_t index )
{
if (index < 0 || index >= this->getPageCount())
{
return;
}
_curPageIdx = index;
_doLayoutDirty = true;
}
void PageView::scrollToPage(ssize_t idx)
{

View File

@ -151,16 +151,24 @@ public:
/**
* Scroll to a page with a given index.
*
* @param idx A given index in the PageView.
* @param idx A given index in the PageView. Index start from 0 to pageCount -1.
*/
void scrollToPage(ssize_t idx);
/**
* Gets current displayed page index.
* @return current page index.
*/
ssize_t getCurPageIndex() const;
/**
* Jump to a page with a given index without scrolling.
* This is the different between scrollToPage.
*
* @param index A given index in PageView. Index start from 0 to pageCount -1.
*/
void setCurPageIndex(ssize_t index);
/**
* @brief Get all the pages in the PageView.

View File

@ -11,6 +11,7 @@ UIPageViewTests::UIPageViewTests()
ADD_TEST_CASE(UIPageViewCustomScrollThreshold);
ADD_TEST_CASE(UIPageViewTouchPropagationTest);
ADD_TEST_CASE(UIPageViewDynamicAddAndRemoveTest);
ADD_TEST_CASE(UIPageViewJumpToPageTest);
}
// UIPageViewTest
@ -691,3 +692,114 @@ void UIPageViewDynamicAddAndRemoveTest::pageViewEvent(Ref *pSender, PageView::Ev
}
}
// UIPageViewJumpToPageTest
UIPageViewJumpToPageTest::UIPageViewJumpToPageTest()
: _displayValueLabel(nullptr)
{
}
UIPageViewJumpToPageTest::~UIPageViewJumpToPageTest()
{
}
bool UIPageViewJumpToPageTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add a label in which the dragpanel events will be displayed
_displayValueLabel = Text::create("setCurPageIndex API Test", "fonts/Marker Felt.ttf", 32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
widgetSize.height / 2.0f +
_displayValueLabel->getContentSize().height * 1.5));
_uiLayer->addChild(_displayValueLabel);
// Add the black background
Text* alert = Text::create("PageView", "fonts/Marker Felt.ttf", 30);
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
_uiLayer->addChild(alert);
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
// Create the page view
PageView* pageView = PageView::create();
pageView->setContentSize(Size(240.0f, 130.0f));
Size backgroundSize = background->getContentSize();
pageView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
(backgroundSize.width - pageView->getContentSize().width) / 2.0f,
(widgetSize.height - backgroundSize.height) / 2.0f +
(backgroundSize.height - pageView->getContentSize().height) / 2.0f));
pageView->removeAllPages();
int pageCount = 4;
for (int i = 0; i < pageCount; ++i)
{
Layout* layout = Layout::create();
layout->setContentSize(Size(240.0f, 130.0f));
ImageView* imageView = ImageView::create("cocosui/scrollviewbg.png");
imageView->setScale9Enabled(true);
imageView->setContentSize(Size(240, 130));
imageView->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
layout->addChild(imageView);
Text* label = Text::create(StringUtils::format("page %d",(i+1)), "fonts/Marker Felt.ttf", 30);
label->setColor(Color3B(192, 192, 192));
label->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
layout->addChild(label);
pageView->insertPage(layout,i);
}
pageView->setCurPageIndex(1);
//add buttons to jump to specific page
auto button1 = ui::Button::create();
button1->setNormalizedPosition(Vec2(0.1, 0.75));
button1->setTitleText("Jump to Page1");
CCLOG("button1 content Size = %f, %f", button1->getContentSize().width,
button1->getContentSize().height);
button1->addClickEventListener([=](Ref*){
pageView->setCurPageIndex(0);
});
_uiLayer->addChild(button1);
auto button2 = static_cast<ui::Button*>(button1->clone());
button2->setTitleText("Jump to Page2");
button2->setNormalizedPosition(Vec2(0.1, 0.65));
CCLOG("button2 content Size = %f, %f", button2->getContentSize().width,
button2->getContentSize().height);
button2->addClickEventListener([=](Ref*){
pageView->setCurPageIndex(1);
});
_uiLayer->addChild(button2);
auto button3 = static_cast<ui::Button*>(button2->clone());
button3->setTitleText("Jump to Page3");
button3->setNormalizedPosition(Vec2(0.9, 0.75));
button3->addClickEventListener([=](Ref*){
pageView->setCurPageIndex(2);
});
_uiLayer->addChild(button3);
auto button4 = static_cast<ui::Button*>(button2->clone());
button4->setTitleText("Jump to Page4");
button4->setNormalizedPosition(Vec2(0.9, 0.65));
button4->addClickEventListener([=](Ref*){
pageView->setCurPageIndex(3);
});
_uiLayer->addChild(button4);
_uiLayer->addChild(pageView);
return true;
}
return false;
}

View File

@ -112,4 +112,18 @@ protected:
cocos2d::ui::Text* _displayValueLabel;
};
class UIPageViewJumpToPageTest : public UIScene
{
public:
CREATE_FUNC(UIPageViewJumpToPageTest);
UIPageViewJumpToPageTest();
~UIPageViewJumpToPageTest();
virtual bool init() override;
protected:
cocos2d::ui::Text* _displayValueLabel;
};
#endif /* defined(__TestCpp__UIPageViewTest__) */