From f558158667b3324a83c92160a46aec4287fdb598 Mon Sep 17 00:00:00 2001 From: Neo Kim Date: Wed, 8 Jul 2015 13:57:25 +0900 Subject: [PATCH 1/5] Use addProtectedChild() instead of addChild() in ScrollViewBar --- cocos/ui/UIScrollViewBar.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cocos/ui/UIScrollViewBar.cpp b/cocos/ui/UIScrollViewBar.cpp index e005d93d6aa4..79a0e728f24c 100644 --- a/cocos/ui/UIScrollViewBar.cpp +++ b/cocos/ui/UIScrollViewBar.cpp @@ -103,16 +103,16 @@ bool ScrollViewBar::init() _upperHalfCircle = createSpriteFromBase64(HALF_CIRCLE_IMAGE); _upperHalfCircle->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM); - addChild(_upperHalfCircle); + addProtectedChild(_upperHalfCircle); _lowerHalfCircle = Sprite::createWithTexture(_upperHalfCircle->getTexture(), _upperHalfCircle->getTextureRect(), _upperHalfCircle->isTextureRectRotated()); _lowerHalfCircle->setScaleY(-1); _lowerHalfCircle->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM); - addChild(_lowerHalfCircle); + addProtectedChild(_lowerHalfCircle); _body = createSpriteFromBase64(BODY_IMAGE_1_PIXEL_HEIGHT); _body->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM); - addChild(_body); + addProtectedChild(_body); setColor(DEFAULT_COLOR); From 22ea504af8488dc2a05442c9cc857d4b79202c0c Mon Sep 17 00:00:00 2001 From: Neo Kim Date: Wed, 8 Jul 2015 15:16:47 +0900 Subject: [PATCH 2/5] Add methods to manipulate scroll bar's opacity --- cocos/ui/UIScrollView.cpp | 27 +++++++++++++++++++++++++++ cocos/ui/UIScrollView.h | 14 ++++++++++++++ cocos/ui/UIScrollViewBar.cpp | 13 +++++++------ cocos/ui/UIScrollViewBar.h | 4 ++++ 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index f32cac06beed..0cd8587b1078 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -1149,6 +1149,33 @@ const Color3B& ScrollView::getScrollBarColor() const return Color3B::WHITE; } +void ScrollView::setScrollBarOpacity(GLubyte opacity) +{ + CCASSERT(_scrollBarEnabled, "Scroll bar should be enabled!"); + if(_verticalScrollBar != nullptr) + { + _verticalScrollBar->setOpacity(opacity); + } + if(_horizontalScrollBar != nullptr) + { + _horizontalScrollBar->setOpacity(opacity); + } +} + +GLubyte ScrollView::getScrollBarOpacity() const +{ + CCASSERT(_scrollBarEnabled, "Scroll bar should be enabled!"); + if(_verticalScrollBar != nullptr) + { + return _verticalScrollBar->getOpacity(); + } + else if(_horizontalScrollBar != nullptr) + { + return _horizontalScrollBar->getOpacity(); + } + return -1; +} + void ScrollView::setScrollBarAutoHideEnabled(bool autoHideEnabled) { CCASSERT(_scrollBarEnabled, "Scroll bar should be enabled!"); diff --git a/cocos/ui/UIScrollView.h b/cocos/ui/UIScrollView.h index 18f318e3a941..741e1528eee2 100644 --- a/cocos/ui/UIScrollView.h +++ b/cocos/ui/UIScrollView.h @@ -449,6 +449,20 @@ class CC_GUI_DLL ScrollView : public Layout */ const Color3B& getScrollBarColor() const; + /** + * @brief Set the scroll bar's opacity + * + * @param the scroll bar's opacity + */ + void setScrollBarOpacity(GLubyte opacity); + + /** + * @brief Get the scroll bar's opacity + * + * @return the scroll bar's opacity + */ + GLubyte getScrollBarOpacity() const; + /** * @brief Set scroll bar auto hide state * diff --git a/cocos/ui/UIScrollViewBar.cpp b/cocos/ui/UIScrollViewBar.cpp index 79a0e728f24c..21413d3574c6 100644 --- a/cocos/ui/UIScrollViewBar.cpp +++ b/cocos/ui/UIScrollViewBar.cpp @@ -31,8 +31,8 @@ NS_CC_BEGIN namespace ui { -static const char* HALF_CIRCLE_IMAGE = "iVBORw0KGgoAAAANSUhEUgAAAAwAAAAGCAMAAADAMI+zAAAAIVBMVEX///////////////////////////////////////////9/gMdvAAAAC3RSTlMAAgMLLFBTYWNkZuZhN4QAAAAvSURBVAjXRchBDgAgCAPBIi0q/3+wxBiZU7cAjJpTNBSPvMLrf7tqgPkR6hB2xzpFkgIfM9q/8QAAAABJRU5ErkJggg=="; -static const char* BODY_IMAGE_1_PIXEL_HEIGHT = "iVBORw0KGgoAAAANSUhEUgAAAAwAAAABCAMAAADdNb8LAAAAA1BMVEX///+nxBvIAAAAAXRSTlNm5DccCwAAAApJREFUeAFjQAYAAA0AAWHNnKQAAAAASUVORK5CYII="; +static const char* HALF_CIRCLE_IMAGE = "iVBORw0KGgoAAAANSUhEUgAAAAwAAAAGCAMAAADAMI+zAAAAJ1BMVEX///////////////////////////////////////////////////9Ruv0SAAAADHRSTlMABgcbbW7Hz9Dz+PmlcJP5AAAAMElEQVR4AUXHwQ2AQAhFwYcLH1H6r1djzDK3ASxUpTBeK/uTCyz7dx54b44m4p5cD1MwAooEJyk3AAAAAElFTkSuQmCC"; +static const char* BODY_IMAGE_1_PIXEL_HEIGHT = "iVBORw0KGgoAAAANSUhEUgAAAAwAAAABCAMAAADdNb8LAAAAA1BMVEX///+nxBvIAAAACklEQVR4AWNABgAADQABYc2cpAAAAABJRU5ErkJggg=="; static const Color3B DEFAULT_COLOR(52, 65, 87); static const float DEFAULT_MARGIN = 20; @@ -65,6 +65,7 @@ _direction(direction), _upperHalfCircle(nullptr), _lowerHalfCircle(nullptr), _body(nullptr), +_opacity(100), _marginFromBoundary(DEFAULT_MARGIN), _marginForLength(DEFAULT_MARGIN), _touching(false), @@ -123,7 +124,7 @@ bool ScrollViewBar::init() if(_autoHideEnabled) { - setOpacity(0); + ProtectedNode::setOpacity(0); } return true; } @@ -165,7 +166,7 @@ void ScrollViewBar::setWidth(float width) void ScrollViewBar::setAutoHideEnabled(bool autoHideEnabled) { _autoHideEnabled = autoHideEnabled; - setOpacity(255); + ProtectedNode::setOpacity(_opacity); } float ScrollViewBar::getWidth() const @@ -202,7 +203,7 @@ void ScrollViewBar::update(float deltaTime) if(_autoHideRemainingTime <= _autoHideTime) { _autoHideRemainingTime = MAX(0, _autoHideRemainingTime); - this->setOpacity(255 * (_autoHideRemainingTime / _autoHideTime)); + ProtectedNode::setOpacity(_opacity * (_autoHideRemainingTime / _autoHideTime)); } } @@ -236,7 +237,7 @@ void ScrollViewBar::onScrolled(const Vec2& outOfBoundary) if(_autoHideEnabled) { _autoHideRemainingTime = _autoHideTime; - setOpacity(255); + ProtectedNode::setOpacity(_opacity); } Layout* innerContainer = _parent->getInnerContainer(); diff --git a/cocos/ui/UIScrollViewBar.h b/cocos/ui/UIScrollViewBar.h index 8503c6344d6a..2331720c8b1c 100644 --- a/cocos/ui/UIScrollViewBar.h +++ b/cocos/ui/UIScrollViewBar.h @@ -130,6 +130,8 @@ class CC_GUI_DLL ScrollViewBar : public ProtectedNode /** * @lua NA */ + virtual void setOpacity(GLubyte opacity) override { _opacity = opacity; } + virtual GLubyte getOpacity() const override { return _opacity; } virtual void onEnter() override; virtual void update(float deltaTime) override; @@ -158,6 +160,8 @@ class CC_GUI_DLL ScrollViewBar : public ProtectedNode Sprite* _upperHalfCircle; Sprite* _lowerHalfCircle; Sprite* _body; + + GLubyte _opacity; float _marginFromBoundary; float _marginForLength; From 9757ee4ea8d3c69e63b80f83ec383d6bd7bb8490 Mon Sep 17 00:00:00 2001 From: Neo Kim Date: Wed, 8 Jul 2015 20:23:16 +0900 Subject: [PATCH 3/5] Refine scroll event dispatching. And add an event to track every move including jump and so on. --- cocos/ui/UIScrollView.cpp | 73 ++++++++++++++++++++++++++++++--------- cocos/ui/UIScrollView.h | 17 ++++++++- 2 files changed, 72 insertions(+), 18 deletions(-) diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index 0cd8587b1078..7a54a8084735 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -149,7 +149,7 @@ void ScrollView::onSizeChanged() float innerSizeWidth = MAX(orginInnerSizeWidth, _contentSize.width); float innerSizeHeight = MAX(orginInnerSizeHeight, _contentSize.height); _innerContainer->setContentSize(Size(innerSizeWidth, innerSizeHeight)); - _innerContainer->setPosition(Vec2(0, _contentSize.height - _innerContainer->getContentSize().height)); + setInnerContainerPosition(Vec2(0, _contentSize.height - _innerContainer->getContentSize().height)); } void ScrollView::setInnerContainerSize(const Size &size) @@ -193,7 +193,7 @@ void ScrollView::setInnerContainerSize(const Size &size) { pos.y = _contentSize.height - (1.0f - _innerContainer->getAnchorPoint().y) * _innerContainer->getContentSize().height; } - _innerContainer->setPosition(pos); + setInnerContainerPosition(pos); } const Size& ScrollView::getInnerContainerSize() const @@ -201,6 +201,27 @@ const Size& ScrollView::getInnerContainerSize() const return _innerContainer->getContentSize(); } +void ScrollView::setInnerContainerPosition(const Vec2 &position) +{ + _innerContainer->setPosition(position); + + this->retain(); + if (_eventCallback) + { + _eventCallback(this, EventType::CONTAINER_MOVED); + } + if (_ccEventCallback) + { + _ccEventCallback(this, static_cast(EventType::CONTAINER_MOVED)); + } + this->release(); +} + +const Vec2 ScrollView::getInnerContainerPosition() const +{ + return _innerContainer->getPosition(); +} + void ScrollView::addChild(Node* child) { ScrollView::addChild(child, child->getLocalZOrder(), child->getTag()); @@ -269,7 +290,7 @@ void ScrollView::moveChildren(float offsetX, float offsetY) void ScrollView::moveChildrenToPosition(const Vec2& position) { - _innerContainer->setPosition(position); + setInnerContainerPosition(position); Vec2 outOfBoundary = getHowMuchOutOfBoundary(Vec2::ZERO); updateScrollBar(outOfBoundary); @@ -345,6 +366,7 @@ void ScrollView::processAutoScrolling(float deltaTime) percentage = tweenfunc::quintEaseOut(percentage); } Vec2 moveDelta = _autoScrollTargetDelta * percentage; + moveChildrenToPosition(_autoScrollStartPosition + moveDelta); // Dispatch related events if bouncing if(_bouncingBack) @@ -366,7 +388,6 @@ void ScrollView::processAutoScrolling(float deltaTime) processScrollEvent(MoveDirection::BOTTOM, true); } } - moveChildrenToPosition(_autoScrollStartPosition + moveDelta); } } @@ -494,8 +515,6 @@ void ScrollView::processInertiaScrolling(float dt) bool ScrollView::scrollChildren(float touchOffsetX, float touchOffsetY) { - processScrollingEvent(); - touchOffsetX = (_direction == Direction::VERTICAL ? 0 : touchOffsetX); touchOffsetY = (_direction == Direction::HORIZONTAL ? 0 : touchOffsetY); if(_bounceEnabled) @@ -508,7 +527,10 @@ bool ScrollView::scrollChildren(float touchOffsetX, float touchOffsetY) float realOffsetX = touchOffsetX; float realOffsetY = touchOffsetY; - bool scrollEnabledUpDown = true; + bool scrolledToLeft = false; + bool scrolledToRight = false; + bool scrolledToTop = false; + bool scrolledToBottom = false; if (touchOffsetY > 0.0f) // up { float icBottomPos = _innerContainer->getBottomBoundary(); @@ -518,8 +540,7 @@ bool ScrollView::scrollChildren(float touchOffsetX, float touchOffsetY) { realOffsetY = _bottomBoundary - icBottomPos; } - processScrollEvent(MoveDirection::BOTTOM, false); - scrollEnabledUpDown = false; + scrolledToBottom = true; } } else if (touchOffsetY < 0.0f) // down @@ -531,12 +552,10 @@ bool ScrollView::scrollChildren(float touchOffsetX, float touchOffsetY) { realOffsetY = _topBoundary - icTopPos; } - processScrollEvent(MoveDirection::TOP, false); - scrollEnabledUpDown = false; + scrolledToTop = true; } } - bool scrollEnabledLeftRight = true; if (touchOffsetX < 0.0f) // left { float icRightPos = _innerContainer->getRightBoundary(); @@ -546,8 +565,7 @@ bool ScrollView::scrollChildren(float touchOffsetX, float touchOffsetY) { realOffsetX = _rightBoundary - icRightPos; } - processScrollEvent(MoveDirection::RIGHT, false); - scrollEnabledLeftRight = false; + scrolledToRight = true; } } else if (touchOffsetX > 0.0f) // right @@ -559,12 +577,33 @@ bool ScrollView::scrollChildren(float touchOffsetX, float touchOffsetY) { realOffsetX = _leftBoundary - icLeftPos; } - processScrollEvent(MoveDirection::LEFT, false); - scrollEnabledLeftRight = false; + scrolledToLeft = true; } } moveChildren(realOffsetX, realOffsetY); - return scrollEnabledUpDown || scrollEnabledLeftRight; + + if(realOffsetX != 0 || realOffsetY != 0) + { + processScrollingEvent(); + } + if(scrolledToBottom) + { + processScrollEvent(MoveDirection::BOTTOM, false); + } + if(scrolledToTop) + { + processScrollEvent(MoveDirection::TOP, false); + } + if(scrolledToLeft) + { + processScrollEvent(MoveDirection::LEFT, false); + } + if(scrolledToRight) + { + processScrollEvent(MoveDirection::RIGHT, false); + } + + return !scrolledToBottom && !scrolledToTop && !scrolledToLeft && !scrolledToRight; } void ScrollView::scrollToBottom(float second, bool attenuated) diff --git a/cocos/ui/UIScrollView.h b/cocos/ui/UIScrollView.h index 741e1528eee2..e510d62c0019 100644 --- a/cocos/ui/UIScrollView.h +++ b/cocos/ui/UIScrollView.h @@ -99,7 +99,8 @@ class CC_GUI_DLL ScrollView : public Layout BOUNCE_TOP, BOUNCE_BOTTOM, BOUNCE_LEFT, - BOUNCE_RIGHT + BOUNCE_RIGHT, + CONTAINER_MOVED }; /** @@ -308,6 +309,20 @@ class CC_GUI_DLL ScrollView : public Layout * @return The inner container size. */ const Size& getInnerContainerSize() const; + + /** + * Set inner container position + * + * @param position Inner container position. + */ + void setInnerContainerPosition(const Vec2 &pos); + + /** + * Get inner container position + * + * @return The inner container position. + */ + const Vec2 getInnerContainerPosition() const; /** * Add callback function which will be called when scrollview event triggered. From 863c3a0a2a62b42e7689c7b43924c3cfbe4ae0be Mon Sep 17 00:00:00 2001 From: Neo Kim Date: Tue, 14 Jul 2015 10:17:05 +0900 Subject: [PATCH 4/5] Correct a typo --- cocos/ui/UIScrollView.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/ui/UIScrollView.h b/cocos/ui/UIScrollView.h index e510d62c0019..8f311ee458e7 100644 --- a/cocos/ui/UIScrollView.h +++ b/cocos/ui/UIScrollView.h @@ -313,7 +313,7 @@ class CC_GUI_DLL ScrollView : public Layout /** * Set inner container position * - * @param position Inner container position. + * @param pos Inner container position. */ void setInnerContainerPosition(const Vec2 &pos); From 7882a534c17dbc0d4002651eee1f8d3e21ab95d3 Mon Sep 17 00:00:00 2001 From: Neo Kim Date: Tue, 14 Jul 2015 11:58:44 +0900 Subject: [PATCH 5/5] Correct the wrong boolean statement and make it easy to read --- cocos/ui/UIScrollView.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index 7a54a8084735..51b9d1f5f5da 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -603,7 +603,9 @@ bool ScrollView::scrollChildren(float touchOffsetX, float touchOffsetY) processScrollEvent(MoveDirection::RIGHT, false); } - return !scrolledToBottom && !scrolledToTop && !scrolledToLeft && !scrolledToRight; + bool scrollEnabledUpDown = (!scrolledToBottom && !scrolledToTop); + bool scrollEnabledLeftRight = (!scrolledToLeft && !scrolledToRight); + return scrollEnabledUpDown || scrollEnabledLeftRight; } void ScrollView::scrollToBottom(float second, bool attenuated)