Skip to content

Commit

Permalink
Merge pull request cocos2d#12812 from neokim/refactor_scroll_view_bar
Browse files Browse the repository at this point in the history
Refactor scroll view bar
  • Loading branch information
zilongshanren committed Jul 14, 2015
2 parents e1ae363 + 7882a53 commit 616efbf
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 26 deletions.
100 changes: 84 additions & 16 deletions cocos/ui/UIScrollView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -193,14 +193,35 @@ 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
{
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<int>(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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -366,7 +388,6 @@ void ScrollView::processAutoScrolling(float deltaTime)
processScrollEvent(MoveDirection::BOTTOM, true);
}
}
moveChildrenToPosition(_autoScrollStartPosition + moveDelta);
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -559,11 +577,34 @@ bool ScrollView::scrollChildren(float touchOffsetX, float touchOffsetY)
{
realOffsetX = _leftBoundary - icLeftPos;
}
processScrollEvent(MoveDirection::LEFT, false);
scrollEnabledLeftRight = false;
scrolledToLeft = true;
}
}
moveChildren(realOffsetX, realOffsetY);

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);
}

bool scrollEnabledUpDown = (!scrolledToBottom && !scrolledToTop);
bool scrollEnabledLeftRight = (!scrolledToLeft && !scrolledToRight);
return scrollEnabledUpDown || scrollEnabledLeftRight;
}

Expand Down Expand Up @@ -1149,6 +1190,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!");
Expand Down
31 changes: 30 additions & 1 deletion cocos/ui/UIScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class CC_GUI_DLL ScrollView : public Layout
BOUNCE_TOP,
BOUNCE_BOTTOM,
BOUNCE_LEFT,
BOUNCE_RIGHT
BOUNCE_RIGHT,
CONTAINER_MOVED
};

/**
Expand Down Expand Up @@ -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 pos 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.
Expand Down Expand Up @@ -449,6 +464,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
*
Expand Down
19 changes: 10 additions & 9 deletions cocos/ui/UIScrollViewBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,6 +65,7 @@ _direction(direction),
_upperHalfCircle(nullptr),
_lowerHalfCircle(nullptr),
_body(nullptr),
_opacity(100),
_marginFromBoundary(DEFAULT_MARGIN),
_marginForLength(DEFAULT_MARGIN),
_touching(false),
Expand Down Expand Up @@ -103,16 +104,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);

Expand All @@ -123,7 +124,7 @@ bool ScrollViewBar::init()

if(_autoHideEnabled)
{
setOpacity(0);
ProtectedNode::setOpacity(0);
}
return true;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -236,7 +237,7 @@ void ScrollViewBar::onScrolled(const Vec2& outOfBoundary)
if(_autoHideEnabled)
{
_autoHideRemainingTime = _autoHideTime;
setOpacity(255);
ProtectedNode::setOpacity(_opacity);
}

Layout* innerContainer = _parent->getInnerContainer();
Expand Down
4 changes: 4 additions & 0 deletions cocos/ui/UIScrollViewBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -158,6 +160,8 @@ class CC_GUI_DLL ScrollViewBar : public ProtectedNode
Sprite* _upperHalfCircle;
Sprite* _lowerHalfCircle;
Sprite* _body;

GLubyte _opacity;

float _marginFromBoundary;
float _marginForLength;
Expand Down

0 comments on commit 616efbf

Please sign in to comment.