コード例 #1
0
void ScrollView::scrollChildren(const Vec2& deltaMove)
{
    Vec2 realMove = deltaMove;
    if(_bounceEnabled)
    {
        // If the position of the inner container is out of the boundary, the offsets should be divided by two.
        Vec2 outOfBoundary = getHowMuchOutOfBoundary();
        realMove.x *= (outOfBoundary.x == 0 ? 1 : 0.5f);
        realMove.y *= (outOfBoundary.y == 0 ? 1 : 0.5f);
    }

    if(!_bounceEnabled)
    {
        Vec2 outOfBoundary = getHowMuchOutOfBoundary(realMove);
        realMove += outOfBoundary;
    }

    bool scrolledToLeft = false;
    bool scrolledToRight = false;
    bool scrolledToTop = false;
    bool scrolledToBottom = false;
    if (realMove.y > 0.0f) // up
    {
        float icBottomPos = _innerContainer->getBottomBoundary();
        if (icBottomPos + realMove.y >= _bottomBoundary)
        {
            scrolledToBottom = true;
        }
    }
    else if (realMove.y < 0.0f) // down
    {
        float icTopPos = _innerContainer->getTopBoundary();
        if (icTopPos + realMove.y <= _topBoundary)
        {
            scrolledToTop = true;
        }
    }

    if (realMove.x < 0.0f) // left
    {
        float icRightPos = _innerContainer->getRightBoundary();
        if (icRightPos + realMove.x <= _rightBoundary)
        {
            scrolledToRight = true;
        }
    }
    else if (realMove.x > 0.0f) // right
    {
        float icLeftPos = _innerContainer->getLeftBoundary();
        if (icLeftPos + realMove.x >= _leftBoundary)
        {
            scrolledToLeft = true;
        }
    }
    moveInnerContainer(realMove, false);

    if(realMove.x != 0 || realMove.y != 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);
    }
}
コード例 #2
0
ファイル: UIScrollView.cpp プロジェクト: fnz/PatterGenerator
bool ScrollView::scrollChildren(float touchOffsetX, float touchOffsetY)
{
    touchOffsetX = (_direction == Direction::VERTICAL ? 0 : touchOffsetX);
    touchOffsetY = (_direction == Direction::HORIZONTAL ? 0 : touchOffsetY);
    if(_bounceEnabled)
    {
        // If the position of the inner container is out of the boundary, the offsets should be divided by two.
        touchOffsetX *= (isOutOfBoundaryLeftOrRight() ? 0.5f : 1);
        touchOffsetY *= (isOutOfBoundaryTopOrBottom() ? 0.5f : 1);
    }
    
    float realOffsetX = touchOffsetX;
    float realOffsetY = touchOffsetY;
    
    bool scrolledToLeft = false;
    bool scrolledToRight = false;
    bool scrolledToTop = false;
    bool scrolledToBottom = false;
    if (touchOffsetY > 0.0f) // up
    {
        float icBottomPos = _innerContainer->getBottomBoundary();
        if (icBottomPos + touchOffsetY >= _bottomBoundary)
        {
            if(!_bounceEnabled)
            {
                realOffsetY = _bottomBoundary - icBottomPos;
            }
            scrolledToBottom = true;
        }
    }
    else if (touchOffsetY < 0.0f) // down
    {
        float icTopPos = _innerContainer->getTopBoundary();
        if (icTopPos + touchOffsetY <= _topBoundary)
        {
            if(!_bounceEnabled)
            {
                realOffsetY = _topBoundary - icTopPos;
            }
            scrolledToTop = true;
        }
    }
    
    if (touchOffsetX < 0.0f) // left
    {
        float icRightPos = _innerContainer->getRightBoundary();
        if (icRightPos + touchOffsetX <= _rightBoundary)
        {
            if(!_bounceEnabled)
            {
                realOffsetX = _rightBoundary - icRightPos;
            }
            scrolledToRight = true;
        }
    }
    else if (touchOffsetX > 0.0f) // right
    {
        float icLeftPos = _innerContainer->getLeftBoundary();
        if (icLeftPos + touchOffsetX >= _leftBoundary)
        {
            if(!_bounceEnabled)
            {
                realOffsetX = _leftBoundary - icLeftPos;
            }
            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;
}