Skip to content

Commit

Permalink
Simplify scrolling limits
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Mar 19, 2021
1 parent 0e2ea97 commit 10cfcec
Showing 1 changed file with 16 additions and 40 deletions.
56 changes: 16 additions & 40 deletions library/lib/views/scrolling_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,54 +41,16 @@ ScrollingFrame::ScrollingFrame()
if (state.state == GestureState::START)
startY = this->scrollY * contentHeight;

float newScroll = (startY - (state.position.y - state.startPosition.y)) / contentHeight;
float bottomLimit = (contentHeight - this->getScrollingAreaHeight()) / contentHeight;

// Bottom boundary
if (newScroll > bottomLimit)
newScroll = bottomLimit + (newScroll - bottomLimit) / 5.0f;

// Top boundary
if (newScroll < 0.0f)
newScroll = newScroll / 5.0f;
float newScroll = (startY - (state.position.y - state.startPosition.y)) / contentHeight;

// Start animation
if (state.state != GestureState::END)
startScrolling(false, newScroll);
else
{
if (this->scrollY < 0)
{
startScrolling(true, 0);
return;
}

float bottomLimit = contentHeight - this->getScrollingAreaHeight();
float bottomLimitNormal = bottomLimit / contentHeight;

if (this->scrollY > bottomLimitNormal)
{
startScrolling(true, bottomLimitNormal);
return;
}

float time = state.acceleration.time.y * 1000.0f;
float newPos = this->scrollY * contentHeight + state.acceleration.distance.y;

// Bottom boundary
if (newPos > bottomLimit)
{
time = time * (1 - fabs(newPos - bottomLimit) / fabs(state.acceleration.distance.y));
newPos = bottomLimit;
}

// Top boundary
if (newPos < 0)
{
time = time * (1 - fabs(newPos) / fabs(state.acceleration.distance.y));
newPos = 0;
}

newScroll = newPos / contentHeight;

if (newScroll == this->scrollY || time < 100)
Expand Down Expand Up @@ -249,7 +211,21 @@ float ScrollingFrame::getContentHeight()
void ScrollingFrame::scrollAnimationTick()
{
if (this->contentView)
this->contentView->setTranslationY(-(this->scrollY * this->getContentHeight()));
{
float contentHeight = this->getContentHeight();
float bottomLimit = (contentHeight - this->getScrollingAreaHeight()) / contentHeight;

if (this->scrollY < 0)
this->scrollY = 0;

if (this->scrollY > bottomLimit)
this->scrollY = bottomLimit;

if (contentHeight <= getHeight())
this->scrollY = 0;

this->contentView->setTranslationY(-(this->scrollY * contentHeight));
}
}

void ScrollingFrame::onChildFocusGained(View* directChild, View* focusedView)
Expand Down

0 comments on commit 10cfcec

Please sign in to comment.