diff --git a/cocos/ui/UIListView.cpp b/cocos/ui/UIListView.cpp index b08deea27c..b63ca2ad2b 100644 --- a/cocos/ui/UIListView.cpp +++ b/cocos/ui/UIListView.cpp @@ -27,6 +27,8 @@ THE SOFTWARE. NS_CC_BEGIN +static const float DEFAULT_TIME_IN_SEC_FOR_SCROLL_TO_ITEM = 1.0f; + namespace ui { IMPLEMENT_CLASS_GUI_INFO(ListView) @@ -642,14 +644,14 @@ Widget* ListView::getClosestItemToPositionInCurrentView(const Vec2& positionRati Widget* ListView::getCenterItemInCurrentView() const { - return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE); + return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE); } Widget* ListView::getLeftmostItemInCurrentView() const { if (_direction == Direction::HORIZONTAL) { - return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_LEFT); + return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_LEFT, Vec2::ANCHOR_MIDDLE); } return nullptr; } @@ -658,7 +660,7 @@ Widget* ListView::getRightmostItemInCurrentView() const { if (_direction == Direction::HORIZONTAL) { - return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_RIGHT); + return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_RIGHT, Vec2::ANCHOR_MIDDLE); } return nullptr; } @@ -667,7 +669,7 @@ Widget* ListView::getTopmostItemInCurrentView() const { if (_direction == Direction::VERTICAL) { - return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_TOP); + return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_TOP, Vec2::ANCHOR_MIDDLE); } return nullptr; } @@ -676,12 +678,17 @@ Widget* ListView::getBottommostItemInCurrentView() const { if (_direction == Direction::VERTICAL) { - return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_BOTTOM); + return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_BOTTOM, Vec2::ANCHOR_MIDDLE); } return nullptr; } -void ListView::scrollToItem(int itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint, float duration) +void ListView::scrollToItem(int itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint) +{ + scrollToItem(itemIndex, positionRatioInView, itemAnchorPoint, DEFAULT_TIME_IN_SEC_FOR_SCROLL_TO_ITEM); +} + +void ListView::scrollToItem(int itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint, float timeInSec) { Widget* item = getItem(itemIndex); if (item == nullptr) @@ -697,7 +704,7 @@ void ListView::scrollToItem(int itemIndex, const Vec2& positionRatioInView, cons Vec2 itemPosition = calculateItemPositionWithAnchor(item, itemAnchorPoint); Vec2 destination = -(itemPosition - positionInView); - startAutoScrollToDestination(destination, duration, true); + startAutoScrollToDestination(destination, timeInSec, true); } ssize_t ListView::getCurSelectedIndex() const diff --git a/cocos/ui/UIListView.h b/cocos/ui/UIListView.h index 81ec81c1b9..0ecba7d1fd 100644 --- a/cocos/ui/UIListView.h +++ b/cocos/ui/UIListView.h @@ -263,16 +263,17 @@ public: * @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance. * @return A item instance if list view is not empty. Otherwise, returns null. */ - Widget* getClosestItemToPosition(const Vec2& targetPosition, const Vec2& itemAnchorPoint = Vec2::ANCHOR_MIDDLE) const; + Widget* getClosestItemToPosition(const Vec2& targetPosition, const Vec2& itemAnchorPoint) const; /** * @brief Query the closest item to a specific position in current view. + * For instance, to find the item in the center of view, call 'getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE)'. * * @param positionRatioInView Specifies the target position with ratio in list view's content size. * @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance. * @return A item instance if list view is not empty. Otherwise, returns null. */ - Widget* getClosestItemToPositionInCurrentView(const Vec2& positionRatioInView, const Vec2& itemAnchorPoint = Vec2::ANCHOR_MIDDLE) const; + Widget* getClosestItemToPositionInCurrentView(const Vec2& positionRatioInView, const Vec2& itemAnchorPoint) const; /** * @brief Query the center item @@ -308,9 +309,10 @@ public: * @brief Scroll to specific item * @param positionRatioInView Specifies the position with ratio in list view's content size. * @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance. - * @param duration Scroll time + * @param timeInSec Scroll time */ - void scrollToItem(int itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint, float duration = 1.0f); + void scrollToItem(int itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint); + void scrollToItem(int itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint, float timeInSec); /** * @brief Query current selected widget's idnex. diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index a8b70db9b0..9296afb087 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -448,9 +448,9 @@ bool ScrollView::isOutOfBoundary() return getHowMuchOutOfBoundary() != Vec2::ZERO; } -void ScrollView::startAutoScrollToDestination(const Vec2& destination, float time, bool attenuated) +void ScrollView::startAutoScrollToDestination(const Vec2& destination, float timeInSec, bool attenuated) { - startAutoScroll(destination - _innerContainer->getPosition(), time, attenuated); + startAutoScroll(destination - _innerContainer->getPosition(), timeInSec, attenuated); } static float calculateAutoScrollTimeByInitialSpeed(float initialSpeed) @@ -466,7 +466,7 @@ void ScrollView::startAttenuatingAutoScroll(const Vec2& deltaMove, const Vec2& i startAutoScroll(deltaMove, time, true); } -void ScrollView::startAutoScroll(const Vec2& deltaMove, float time, bool attenuated) +void ScrollView::startAutoScroll(const Vec2& deltaMove, float timeInSec, bool attenuated) { Vec2 adjustedDeltaMove = flattenVectorByDirection(deltaMove); @@ -474,7 +474,7 @@ void ScrollView::startAutoScroll(const Vec2& deltaMove, float time, bool attenua _autoScrollTargetDelta = adjustedDeltaMove; _autoScrollAttenuate = attenuated; _autoScrollStartPosition = _innerContainer->getPosition(); - _autoScrollTotalTime = time; + _autoScrollTotalTime = timeInSec; _autoScrollAccumulatedTime = 0; _autoScrollBraking = false; _autoScrollBrakingStartPosition = Vec2::ZERO; @@ -675,39 +675,39 @@ bool ScrollView::scrollChildren(float touchOffsetX, float touchOffsetY) return scrollEnabledUpDown || scrollEnabledLeftRight; } -void ScrollView::scrollToBottom(float time, bool attenuated) +void ScrollView::scrollToBottom(float timeInSec, bool attenuated) { - startAutoScrollToDestination(Vec2(_innerContainer->getPosition().x, 0.0f), time, attenuated); + startAutoScrollToDestination(Vec2(_innerContainer->getPosition().x, 0.0f), timeInSec, attenuated); } -void ScrollView::scrollToTop(float time, bool attenuated) +void ScrollView::scrollToTop(float timeInSec, bool attenuated) { startAutoScrollToDestination(Vec2(_innerContainer->getPosition().x, - _contentSize.height - _innerContainer->getContentSize().height), time, attenuated); + _contentSize.height - _innerContainer->getContentSize().height), timeInSec, attenuated); } -void ScrollView::scrollToLeft(float time, bool attenuated) +void ScrollView::scrollToLeft(float timeInSec, bool attenuated) { - startAutoScrollToDestination(Vec2(0.0f, _innerContainer->getPosition().y), time, attenuated); + startAutoScrollToDestination(Vec2(0.0f, _innerContainer->getPosition().y), timeInSec, attenuated); } -void ScrollView::scrollToRight(float time, bool attenuated) +void ScrollView::scrollToRight(float timeInSec, bool attenuated) { startAutoScrollToDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width, - _innerContainer->getPosition().y), time, attenuated); + _innerContainer->getPosition().y), timeInSec, attenuated); } -void ScrollView::scrollToTopLeft(float time, bool attenuated) +void ScrollView::scrollToTopLeft(float timeInSec, bool attenuated) { if (_direction != Direction::BOTH) { CCLOG("Scroll direction is not both!"); return; } - startAutoScrollToDestination(Vec2(0.0f, _contentSize.height - _innerContainer->getContentSize().height), time, attenuated); + startAutoScrollToDestination(Vec2(0.0f, _contentSize.height - _innerContainer->getContentSize().height), timeInSec, attenuated); } -void ScrollView::scrollToTopRight(float time, bool attenuated) +void ScrollView::scrollToTopRight(float timeInSec, bool attenuated) { if (_direction != Direction::BOTH) { @@ -715,43 +715,43 @@ void ScrollView::scrollToTopRight(float time, bool attenuated) return; } startAutoScrollToDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width, - _contentSize.height - _innerContainer->getContentSize().height), time, attenuated); + _contentSize.height - _innerContainer->getContentSize().height), timeInSec, attenuated); } -void ScrollView::scrollToBottomLeft(float time, bool attenuated) +void ScrollView::scrollToBottomLeft(float timeInSec, bool attenuated) { if (_direction != Direction::BOTH) { CCLOG("Scroll direction is not both!"); return; } - startAutoScrollToDestination(Vec2::ZERO, time, attenuated); + startAutoScrollToDestination(Vec2::ZERO, timeInSec, attenuated); } -void ScrollView::scrollToBottomRight(float time, bool attenuated) +void ScrollView::scrollToBottomRight(float timeInSec, bool attenuated) { if (_direction != Direction::BOTH) { CCLOG("Scroll direction is not both!"); return; } - startAutoScrollToDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width, 0.0f), time, attenuated); + startAutoScrollToDestination(Vec2(_contentSize.width - _innerContainer->getContentSize().width, 0.0f), timeInSec, attenuated); } -void ScrollView::scrollToPercentVertical(float percent, float time, bool attenuated) +void ScrollView::scrollToPercentVertical(float percent, float timeInSec, bool attenuated) { float minY = _contentSize.height - _innerContainer->getContentSize().height; float h = - minY; - startAutoScrollToDestination(Vec2(_innerContainer->getPosition().x, minY + percent * h / 100.0f), time, attenuated); + startAutoScrollToDestination(Vec2(_innerContainer->getPosition().x, minY + percent * h / 100.0f), timeInSec, attenuated); } -void ScrollView::scrollToPercentHorizontal(float percent, float time, bool attenuated) +void ScrollView::scrollToPercentHorizontal(float percent, float timeInSec, bool attenuated) { float w = _innerContainer->getContentSize().width - _contentSize.width; - startAutoScrollToDestination(Vec2(-(percent * w / 100.0f), _innerContainer->getPosition().y), time, attenuated); + startAutoScrollToDestination(Vec2(-(percent * w / 100.0f), _innerContainer->getPosition().y), timeInSec, attenuated); } -void ScrollView::scrollToPercentBothDirection(const Vec2& percent, float time, bool attenuated) +void ScrollView::scrollToPercentBothDirection(const Vec2& percent, float timeInSec, bool attenuated) { if (_direction != Direction::BOTH) { @@ -760,7 +760,7 @@ void ScrollView::scrollToPercentBothDirection(const Vec2& percent, float time, b float minY = _contentSize.height - _innerContainer->getContentSize().height; float h = - minY; float w = _innerContainer->getContentSize().width - _contentSize.width; - startAutoScrollToDestination(Vec2(-(percent.x * w / 100.0f), minY + percent.y * h / 100.0f), time, attenuated); + startAutoScrollToDestination(Vec2(-(percent.x * w / 100.0f), minY + percent.y * h / 100.0f), timeInSec, attenuated); } void ScrollView::jumpToBottom() @@ -850,7 +850,7 @@ void ScrollView::jumpToPercentBothDirection(const Vec2& percent) jumpToDestination(Vec2(-(percent.x * w / 100.0f), minY + percent.y * h / 100.0f)); } -bool ScrollView::calculateCurrPrevTouchPoints(Touch* touch, Vec3* currPt, Vec3* prevPt) +bool ScrollView::calculateCurrAndPrevTouchPoints(Touch* touch, Vec3* currPt, Vec3* prevPt) { if (nullptr == _hittedByCamera || false == hitTest(touch->getLocation(), _hittedByCamera, currPt) || @@ -900,7 +900,7 @@ void ScrollView::handlePressLogic(Touch *touch) void ScrollView::handleMoveLogic(Touch *touch) { Vec3 currPt, prevPt; - if(!calculateCurrPrevTouchPoints(touch, &currPt, &prevPt)) + if(!calculateCurrAndPrevTouchPoints(touch, &currPt, &prevPt)) { return; } @@ -917,7 +917,7 @@ void ScrollView::handleReleaseLogic(Touch *touch) // Gather the last touch information when released { Vec3 currPt, prevPt; - if(calculateCurrPrevTouchPoints(touch, &currPt, &prevPt)) + if(calculateCurrAndPrevTouchPoints(touch, &currPt, &prevPt)) { Vec3 delta3 = currPt - prevPt; Vec2 delta(delta3.x, delta3.y); diff --git a/cocos/ui/UIScrollView.h b/cocos/ui/UIScrollView.h index 8196f5d01a..63a1cc4611 100644 --- a/cocos/ui/UIScrollView.h +++ b/cocos/ui/UIScrollView.h @@ -156,83 +156,83 @@ public: /** * Scroll inner container to bottom boundary of scrollview. - * @param time Time in seconds. + * @param timeInSec Time in seconds. * @param attenuated Whether scroll speed attenuate or not. */ - void scrollToBottom(float time, bool attenuated); + void scrollToBottom(float timeInSec, bool attenuated); /** * Scroll inner container to top boundary of scrollview. - * @param time Time in seconds. + * @param timeInSec Time in seconds. * @param attenuated Whether scroll speed attenuate or not. */ - void scrollToTop(float time, bool attenuated); + void scrollToTop(float timeInSec, bool attenuated); /** * Scroll inner container to left boundary of scrollview. - * @param time Time in seconds. + * @param timeInSec Time in seconds. * @param attenuated Whether scroll speed attenuate or not. */ - void scrollToLeft(float time, bool attenuated); + void scrollToLeft(float timeInSec, bool attenuated); /** * Scroll inner container to right boundary of scrollview. - * @param time Time in seconds. + * @param timeInSec Time in seconds. * @param attenuated Whether scroll speed attenuate or not. */ - void scrollToRight(float time, bool attenuated); + void scrollToRight(float timeInSec, bool attenuated); /** * Scroll inner container to top and left boundary of scrollview. - * @param time Time in seconds. + * @param timeInSec Time in seconds. * @param attenuated Whether scroll speed attenuate or not. */ - void scrollToTopLeft(float time, bool attenuated); + void scrollToTopLeft(float timeInSec, bool attenuated); /** * Scroll inner container to top and right boundary of scrollview. - * @param time Time in seconds. + * @param timeInSec Time in seconds. * @param attenuated Whether scroll speed attenuate or not. */ - void scrollToTopRight(float time, bool attenuated); + void scrollToTopRight(float timeInSec, bool attenuated); /** * Scroll inner container to bottom and left boundary of scrollview. - * @param time Time in seconds. + * @param timeInSec Time in seconds. * @param attenuated Whether scroll speed attenuate or not. */ - void scrollToBottomLeft(float time, bool attenuated); + void scrollToBottomLeft(float timeInSec, bool attenuated); /** * Scroll inner container to bottom and right boundary of scrollview. - * @param time Time in seconds + * @param timeInSec Time in seconds * @param attenuated Whether scroll speed attenuate or not. */ - void scrollToBottomRight(float time, bool attenuated); + void scrollToBottomRight(float timeInSec, bool attenuated); /** * Scroll inner container to vertical percent position of scrollview. * @param percent A value between 0 and 100. - * @param time Time in seconds. + * @param timeInSec Time in seconds. * @param attenuated Whether scroll speed attenuate or not. */ - void scrollToPercentVertical(float percent, float time, bool attenuated); + void scrollToPercentVertical(float percent, float timeInSec, bool attenuated); /** * Scroll inner container to horizontal percent position of scrollview. * @param percent A value between 0 and 100. - * @param time Time in seconds. + * @param timeInSec Time in seconds. * @param attenuated Whether scroll speed attenuate or not. */ - void scrollToPercentHorizontal(float percent, float time, bool attenuated); + void scrollToPercentHorizontal(float percent, float timeInSec, bool attenuated); /** * Scroll inner container to both direction percent position of scrollview. * @param percent A value between 0 and 100. - * @param time Time in seconds. + * @param timeInSec Time in seconds. * @param attenuated Whether scroll speed attenuate or not. */ - void scrollToPercentBothDirection(const Vec2& percent, float time, bool attenuated); + void scrollToPercentBothDirection(const Vec2& percent, float timeInSec, bool attenuated); /** * Move inner container to bottom boundary of scrollview. @@ -574,13 +574,13 @@ protected: void moveChildren(float offsetX, float offsetY); void moveChildrenToPosition(const Vec2& position); - bool calculateCurrPrevTouchPoints(Touch* touch, Vec3* currPt, Vec3* prevPt); + bool calculateCurrAndPrevTouchPoints(Touch* touch, Vec3* currPt, Vec3* prevPt); void gatherTouchMove(const Vec2& delta); Vec2 calculateTouchMoveVelocity() const; virtual void startAttenuatingAutoScroll(const Vec2& deltaMove, const Vec2& initialVelocity); - void startAutoScroll(const Vec2& deltaMove, float time, bool attenuated); - void startAutoScrollToDestination(const Vec2& des, float time, bool attenuated); + void startAutoScroll(const Vec2& deltaMove, float timeInSec, bool attenuated); + void startAutoScrollToDestination(const Vec2& des, float timeInSec, bool attenuated); bool isNecessaryAutoScrollBrake(); void processAutoScrolling(float deltaTime);