Refactor some functions and remove redundant codes

This commit is contained in:
Neo Kim 2015-06-19 21:10:08 +09:00
parent 876c94f352
commit a8f77710f5
2 changed files with 86 additions and 145 deletions

View File

@ -271,7 +271,7 @@ void ScrollView::autoScrollChildren(float dt)
if (nowSpeed <= 0.0f) if (nowSpeed <= 0.0f)
{ {
stopAutoScrollChildren(); stopAutoScrollChildren();
checkNeedBounce(); processBounceConditionally();
} }
else else
{ {
@ -282,7 +282,7 @@ void ScrollView::autoScrollChildren(float dt)
if (!scrollChildren(offsetX, offsetY)) if (!scrollChildren(offsetX, offsetY))
{ {
stopAutoScrollChildren(); stopAutoScrollChildren();
checkNeedBounce(); processBounceConditionally();
} }
} }
} }
@ -297,7 +297,7 @@ void ScrollView::autoScrollChildren(float dt)
if (!notDone || !scrollCheck) if (!notDone || !scrollCheck)
{ {
stopAutoScrollChildren(); stopAutoScrollChildren();
checkNeedBounce(); processBounceConditionally();
} }
} }
else else
@ -305,7 +305,7 @@ void ScrollView::autoScrollChildren(float dt)
if (!scrollChildren(_autoScrollDir.x * dt * _autoScrollOriginalSpeed, _autoScrollDir.y * dt * _autoScrollOriginalSpeed)) if (!scrollChildren(_autoScrollDir.x * dt * _autoScrollOriginalSpeed, _autoScrollDir.y * dt * _autoScrollOriginalSpeed))
{ {
stopAutoScrollChildren(); stopAutoScrollChildren();
checkNeedBounce(); processBounceConditionally();
} }
} }
} }
@ -323,76 +323,84 @@ void ScrollView::bounceChildren(float dt)
} }
} }
bool ScrollView::checkNeedBounce() bool ScrollView::processBounceConditionally()
{ {
if (!_bounceEnabled) if (!_bounceEnabled)
{ {
return false; return false;
} }
bool topBounceNeeded, bottomBounceNeeded, leftBounceNeeded, rightBounceNeeded; bool topBounceNeeded = isOutOfBoundary(MoveDirection::TOP);
checkBounceBoundary(&topBounceNeeded, &bottomBounceNeeded, &leftBounceNeeded, &rightBounceNeeded); bool bottomBounceNeeded = isOutOfBoundary(MoveDirection::BOTTOM);
bool leftBounceNeeded = isOutOfBoundary(MoveDirection::LEFT);
bool rightBounceNeeded = isOutOfBoundary(MoveDirection::RIGHT);
if (topBounceNeeded || bottomBounceNeeded || leftBounceNeeded || rightBounceNeeded) // Dispatch scroll event
{ if(topBounceNeeded)
if (topBounceNeeded && leftBounceNeeded) {
{ processScrollEvent(MoveDirection::TOP, false);
Vec2 scrollVector = Vec2(0.0f, _contentSize.height) - Vec2(_innerContainer->getLeftBoundary(), _innerContainer->getTopBoundary()); }
float orSpeed = scrollVector.getLength()/(0.2f); if(bottomBounceNeeded)
_bounceDir = scrollVector.getNormalized(); {
startBounceChildren(orSpeed); processScrollEvent(MoveDirection::BOTTOM, false);
} }
else if (topBounceNeeded && rightBounceNeeded) if(leftBounceNeeded)
{ {
Vec2 scrollVector = Vec2(_contentSize.width, _contentSize.height) - Vec2(_innerContainer->getRightBoundary(), _innerContainer->getTopBoundary()); processScrollEvent(MoveDirection::LEFT, false);
float orSpeed = scrollVector.getLength()/(0.2f); }
_bounceDir = scrollVector.getNormalized(); if(rightBounceNeeded)
startBounceChildren(orSpeed); {
} processScrollEvent(MoveDirection::RIGHT, false);
else if (bottomBounceNeeded && leftBounceNeeded) }
{
Vec2 scrollVector = Vec2::ZERO - Vec2(_innerContainer->getLeftBoundary(), _innerContainer->getBottomBoundary()); // Calculate scroll vector
float orSpeed = scrollVector.getLength()/(0.2f); Vec2 scrollVector;
_bounceDir = scrollVector.getNormalized(); if(topBounceNeeded)
startBounceChildren(orSpeed); {
} if (leftBounceNeeded)
else if (bottomBounceNeeded && rightBounceNeeded) {
{ scrollVector = Vec2(-_innerContainer->getLeftBoundary(), _contentSize.height - _innerContainer->getTopBoundary());
Vec2 scrollVector = Vec2(_contentSize.width, 0.0f) - Vec2(_innerContainer->getRightBoundary(), _innerContainer->getBottomBoundary()); }
float orSpeed = scrollVector.getLength()/(0.2f); else if (rightBounceNeeded)
_bounceDir = scrollVector.getNormalized(); {
startBounceChildren(orSpeed); scrollVector = Vec2(_contentSize.width - _innerContainer->getRightBoundary(), _contentSize.height - _innerContainer->getTopBoundary());
} }
else if (topBounceNeeded) else
{ {
Vec2 scrollVector = Vec2(0.0f, _contentSize.height) - Vec2(0.0f, _innerContainer->getTopBoundary()); scrollVector = Vec2(0.0f, _contentSize.height - _innerContainer->getTopBoundary());
float orSpeed = scrollVector.getLength()/(0.2f); }
_bounceDir = scrollVector.getNormalized(); }
startBounceChildren(orSpeed); else if (bottomBounceNeeded)
} {
else if (bottomBounceNeeded) if (leftBounceNeeded)
{ {
Vec2 scrollVector = Vec2::ZERO - Vec2(0.0f, _innerContainer->getBottomBoundary()); scrollVector = Vec2(-_innerContainer->getLeftBoundary(), -_innerContainer->getBottomBoundary());
float orSpeed = scrollVector.getLength()/(0.2f); }
_bounceDir = scrollVector.getNormalized(); else if (rightBounceNeeded)
startBounceChildren(orSpeed); {
} scrollVector = Vec2(_contentSize.width - _innerContainer->getRightBoundary(), -_innerContainer->getBottomBoundary());
else if (leftBounceNeeded) }
{ else
Vec2 scrollVector = Vec2::ZERO - Vec2(_innerContainer->getLeftBoundary(), 0.0f); {
float orSpeed = scrollVector.getLength()/(0.2f); scrollVector = Vec2(0.0f, -_innerContainer->getBottomBoundary());
_bounceDir = scrollVector.getNormalized(); }
startBounceChildren(orSpeed); }
} else if (leftBounceNeeded)
else if (rightBounceNeeded) {
{ scrollVector = Vec2(-_innerContainer->getLeftBoundary(), 0.0f);
Vec2 scrollVector = Vec2(_contentSize.width, 0.0f) - Vec2(_innerContainer->getRightBoundary(), 0.0f); }
float orSpeed = scrollVector.getLength()/(0.2f); else if (rightBounceNeeded)
_bounceDir = scrollVector.getNormalized(); {
startBounceChildren(orSpeed); scrollVector = Vec2(_contentSize.width - _innerContainer->getRightBoundary(), 0.0f);
} }
return true;
} if(topBounceNeeded || bottomBounceNeeded || leftBounceNeeded || rightBounceNeeded)
return false; {
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
return true;
}
return false;
} }
bool ScrollView::isOutOfBoundary() const bool ScrollView::isOutOfBoundary() const
@ -424,53 +432,6 @@ bool ScrollView::isOutOfBoundaryLeftOrRight() const
return isOutOfBoundary(MoveDirection::LEFT) || isOutOfBoundary(MoveDirection::RIGHT); return isOutOfBoundary(MoveDirection::LEFT) || isOutOfBoundary(MoveDirection::RIGHT);
} }
void ScrollView::checkBounceBoundary(bool* pTopBounceNeeded, bool* pBottomBounceNeeded, bool* pLeftBounceNeeded, bool* pRightBounceNeeded)
{
float icBottomPos = _innerContainer->getBottomBoundary();
if (icBottomPos > _bottomBoundary)
{
processScrollEvent(MoveDirection::BOTTOM, false);
(*pBottomBounceNeeded) = true;
}
else
{
(*pBottomBounceNeeded) = false;
}
float icTopPos = _innerContainer->getTopBoundary();
if (icTopPos < _topBoundary)
{
processScrollEvent(MoveDirection::TOP, false);
(*pTopBounceNeeded) = true;
}
else
{
(*pTopBounceNeeded) = false;
}
float icRightPos = _innerContainer->getRightBoundary();
if (icRightPos < _rightBoundary)
{
processScrollEvent(MoveDirection::RIGHT, false);
(*pRightBounceNeeded) = true;
}
else
{
(*pRightBounceNeeded) = false;
}
float icLeftPos = _innerContainer->getLeftBoundary();
if (icLeftPos > _leftBoundary)
{
processScrollEvent(MoveDirection::LEFT, false);
(*pLeftBounceNeeded) = true;
}
else
{
(*pLeftBounceNeeded) = false;
}
}
void ScrollView::startBounceChildren(float v) void ScrollView::startBounceChildren(float v)
{ {
_bounceOriginalSpeed = v; _bounceOriginalSpeed = v;
@ -518,33 +479,14 @@ void ScrollView::jumpToDestination(const Vec2 &des)
{ {
float finalOffsetX = des.x; float finalOffsetX = des.x;
float finalOffsetY = des.y; float finalOffsetY = des.y;
switch (_direction) if (des.y <= 0 && _direction != Direction::HORIZONTAL)
{ {
case Direction::VERTICAL: finalOffsetY = MAX(des.y, _contentSize.height - _innerContainer->getContentSize().height);
if (des.y <= 0) }
{ if (des.x <= 0 && _direction != Direction::VERTICAL)
finalOffsetY = MAX(des.y, _contentSize.height - _innerContainer->getContentSize().height); {
} finalOffsetX = MAX(des.x, _contentSize.width - _innerContainer->getContentSize().width);
break; }
case Direction::HORIZONTAL:
if (des.x <= 0)
{
finalOffsetX = MAX(des.x, _contentSize.width - _innerContainer->getContentSize().width);
}
break;
case Direction::BOTH:
if (des.y <= 0)
{
finalOffsetY = MAX(des.y, _contentSize.height - _innerContainer->getContentSize().height);
}
if (des.x <= 0)
{
finalOffsetX = MAX(des.x, _contentSize.width - _innerContainer->getContentSize().width);
}
break;
default:
break;
}
_innerContainer->setPosition(Vec2(finalOffsetX, finalOffsetY)); _innerContainer->setPosition(Vec2(finalOffsetX, finalOffsetY));
} }
@ -1081,7 +1023,7 @@ void ScrollView::startRecordSlidAction()
void ScrollView::endRecordSlidAction() void ScrollView::endRecordSlidAction()
{ {
if (!checkNeedBounce() && _inertiaScrollEnabled) if (!processBounceConditionally() && _inertiaScrollEnabled)
{ {
if (_slidTime <= 0.016f) if (_slidTime <= 0.016f)
{ {

View File

@ -433,8 +433,7 @@ protected:
void moveChildren(float offsetX, float offsetY); void moveChildren(float offsetX, float offsetY);
void autoScrollChildren(float dt); void autoScrollChildren(float dt);
void bounceChildren(float dt); void bounceChildren(float dt);
void checkBounceBoundary(bool* pTopBounceNeeded, bool* pBottomBounceNeeded, bool* pLeftBounceNeeded, bool* pRightBounceNeeded); bool processBounceConditionally();
bool checkNeedBounce();
void startAutoScrollChildrenWithOriginalSpeed(const Vec2& dir, float v, bool attenuated, float acceleration); void startAutoScrollChildrenWithOriginalSpeed(const Vec2& dir, float v, bool attenuated, float acceleration);
void startAutoScrollChildrenWithDestination(const Vec2& des, float second, bool attenuated); void startAutoScrollChildrenWithDestination(const Vec2& des, float second, bool attenuated);
void jumpToDestination(const Vec2& des); void jumpToDestination(const Vec2& des);