From 952e52365bab88c4a2b0cd92b6c4bfe8d2306d59 Mon Sep 17 00:00:00 2001 From: andyque Date: Fri, 17 Oct 2014 14:02:49 +0800 Subject: [PATCH] fix scrollView rotate issue --- cocos/ui/UIScrollView.cpp | 8 +- .../CocoStudioGUITest/CocosGUIScene.cpp | 2 +- .../CocoStudioGUITest/UISceneManager.cpp | 4 + .../UITest/CocoStudioGUITest/UISceneManager.h | 2 + .../UIScrollViewTest/UIScrollViewTest.cpp | 78 +++++++++++++++++++ .../UIScrollViewTest/UIScrollViewTest.h | 12 +++ 6 files changed, 102 insertions(+), 4 deletions(-) diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index 1e8772b91f..95bab8ed39 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -1422,10 +1422,12 @@ void ScrollView::endRecordSlidAction() } float totalDis = 0.0f; Vec2 dir; + Vec2 touchEndPositionInNodeSpace = this->convertToNodeSpace(_touchEndPosition); + Vec2 touchBeganPositionInNodeSpace = this->convertToNodeSpace(_touchBeganPosition); switch (_direction) { case Direction::VERTICAL: - totalDis = _touchEndPosition.y - _touchBeganPosition.y; + totalDis = touchEndPositionInNodeSpace.y - touchBeganPositionInNodeSpace.y; if (totalDis < 0.0f) { dir = SCROLLDIR_DOWN; @@ -1436,7 +1438,7 @@ void ScrollView::endRecordSlidAction() } break; case Direction::HORIZONTAL: - totalDis = _touchEndPosition.x - _touchBeganPosition.x; + totalDis = touchEndPositionInNodeSpace.x - touchBeganPositionInNodeSpace.x; if (totalDis < 0.0f) { dir = SCROLLDIR_LEFT; @@ -1448,7 +1450,7 @@ void ScrollView::endRecordSlidAction() break; case Direction::BOTH: { - Vec2 subVector = _touchEndPosition - _touchBeganPosition; + Vec2 subVector = touchEndPositionInNodeSpace - touchBeganPositionInNodeSpace; totalDis = subVector.getLength(); dir = subVector.getNormalized(); break; diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp index 6d44ef0536..c6fb2138d5 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp @@ -211,7 +211,7 @@ g_guisTests[] = UISceneManager* sceneManager = UISceneManager::sharedUISceneManager(); sceneManager->setCurrentUISceneId(kUIScrollViewTest_Vertical); sceneManager->setMinUISceneId(kUIScrollViewTest_Vertical); - sceneManager->setMaxUISceneId(kUIScrollViewNestTest); + sceneManager->setMaxUISceneId(kUIScrollViewRotated); Scene* scene = sceneManager->currentUIScene(); Director::getInstance()->replaceScene(scene); } diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp index 5219f6f0e1..c00944cd14 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp @@ -91,6 +91,7 @@ static const char* s_testArray[] = "UIScrollViewTest_ScrollToPercentBothDirection", "UIScrollViewTest_ScrollToPercentBothDirection_Bounce", "UIScrollViewNestTest", + "UIScrollViewRotated", "UIPageViewTest", "UIPageViewButtonTest", @@ -323,6 +324,9 @@ Scene *UISceneManager::currentUIScene() return UIScrollViewTest_ScrollToPercentBothDirection_Bounce::sceneWithTitle(s_testArray[_currentUISceneId]); case kUIScrollViewNestTest: return UIScrollViewNestTest::sceneWithTitle(s_testArray[_currentUISceneId]); + case kUIScrollViewRotated: + return UIScrollViewRotated::sceneWithTitle(s_testArray[_currentUISceneId]); + case kUIPageViewTest: return UIPageViewTest::sceneWithTitle(s_testArray[_currentUISceneId]); case kUIPageViewButtonTest: diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h index bcaf85bf37..cc49fb4f40 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h @@ -82,6 +82,8 @@ enum kUIScrollViewTest_ScrollToPercentBothDirection, kUIScrollViewTest_ScrollToPercentBothDirection_Bounce, kUIScrollViewNestTest, + kUIScrollViewRotated, + kUIPageViewTest, kUIPageViewButtonTest, kUIPageViewCustomScrollThreshold, diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.cpp index 89461e1c21..1c57acae30 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.cpp @@ -426,3 +426,81 @@ bool UIScrollViewNestTest::init() return false; } +// UIScrollViewRotated + +UIScrollViewRotated::UIScrollViewRotated() +: _displayValueLabel(nullptr) +{ +} + +UIScrollViewRotated::~UIScrollViewRotated() +{ +} + +bool UIScrollViewRotated::init() +{ + if (UIScene::init()) + { + Size widgetSize = _widget->getContentSize(); + + // 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)); + _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, + widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f)); + _uiLayer->addChild(_displayValueLabel); + + // Add the alert + Text* alert = Text::create("ScrollView vertical", "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(_uiLayer->getChildByTag(81)); + + Layout* background = dynamic_cast(root->getChildByName("background_Panel")); + + // Create the scrollview by vertical + ui::ScrollView* scrollView = ui::ScrollView::create(); + scrollView->setContentSize(Size(280.0f, 150.0f)); + scrollView->setDirection(ui::ScrollView::Direction::BOTH); + Size backgroundSize = background->getContentSize(); + scrollView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + + (backgroundSize.width - scrollView->getContentSize().width) / 2.0f, + (widgetSize.height - backgroundSize.height) / 2.0f + + (backgroundSize.height - scrollView->getContentSize().height) / 2.0f + 100) ); + scrollView->setRotation(45); + _uiLayer->addChild(scrollView); + + ImageView* imageView = ImageView::create("cocosui/ccicon.png"); + + float innerWidth = scrollView->getContentSize().width; + float innerHeight = scrollView->getContentSize().height + imageView->getContentSize().height; + + scrollView->setInnerContainerSize(Size(innerWidth, innerHeight)); + + Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); + button->setPosition(Vec2(innerWidth / 2.0f, scrollView->getInnerContainerSize().height - button->getContentSize().height / 2.0f)); + scrollView->addChild(button); + + Button* titleButton = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png"); + titleButton->setTitleText("Title Button"); + titleButton->setPosition(Vec2(innerWidth / 2.0f, button->getBottomBoundary() - button->getContentSize().height)); + 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)); + button_scale9->setPosition(Vec2(innerWidth / 2.0f, titleButton->getBottomBoundary() - titleButton->getContentSize().height)); + scrollView->addChild(button_scale9); + + imageView->setPosition(Vec2(innerWidth / 2.0f, imageView->getContentSize().height / 2.0f)); + scrollView->addChild(imageView); + + + + return true; + } + + return false; +} \ No newline at end of file diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.h index bbb78949d3..766a11002b 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.h @@ -99,4 +99,16 @@ protected: Text* _displayValueLabel; }; +class UIScrollViewRotated : public UIScene +{ +public: + UIScrollViewRotated(); + ~UIScrollViewRotated(); + bool init(); + +protected: + UI_SCENE_CREATE_FUNC(UIScrollViewRotated) + Text* _displayValueLabel; +}; + #endif /* defined(__TestCpp__UIScrollViewTest__) */