6365: Fix erratic smooth scrolling when scroll time is changed r=def- a=Robyt3

Remember the maximum animation time when initiating a smooth scroll, so the smooth scrolling does not change erratically when `ui_smooth_scroll_time` is changed while smooth scrolling is in progress.

## Checklist

- [X] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
This commit is contained in:
bors[bot] 2023-02-26 11:34:11 +00:00 committed by GitHub
commit 8acda309b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 12 deletions

View file

@ -13,6 +13,7 @@ CScrollRegion::CScrollRegion()
{
m_ScrollY = 0.0f;
m_ContentH = 0.0f;
m_AnimTimeMax = 0.0f;
m_AnimTime = 0.0f;
m_AnimInitScrollY = 0.0f;
m_AnimTargetScrollY = 0.0f;
@ -68,23 +69,23 @@ void CScrollRegion::End()
CUIRect RegionRect = m_ClipRect;
RegionRect.w += m_Params.m_ScrollbarWidth;
const float AnimationDuration = g_Config.m_UiSmoothScrollTime / 1000.0f;
if(UI()->Enabled() && UI()->MouseHovered(&RegionRect))
{
const bool IsPageScroll = Input()->AltIsPressed();
const float ScrollUnit = IsPageScroll ? m_ClipRect.h : m_Params.m_ScrollUnit;
float ScrollDirection = 0.0f;
if(UI()->ConsumeHotkey(CUI::HOTKEY_SCROLL_UP))
{
m_AnimTime = AnimationDuration;
m_AnimInitScrollY = m_ScrollY;
m_AnimTargetScrollY -= ScrollUnit;
}
ScrollDirection = -1.0f;
else if(UI()->ConsumeHotkey(CUI::HOTKEY_SCROLL_DOWN))
ScrollDirection = 1.0f;
if(ScrollDirection != 0.0f)
{
m_AnimTime = AnimationDuration;
const bool IsPageScroll = Input()->AltIsPressed();
const float ScrollUnit = IsPageScroll ? m_ClipRect.h : m_Params.m_ScrollUnit;
m_AnimTimeMax = g_Config.m_UiSmoothScrollTime / 1000.0f;
m_AnimTime = m_AnimTimeMax;
m_AnimInitScrollY = m_ScrollY;
m_AnimTargetScrollY += ScrollUnit;
m_AnimTargetScrollY += ScrollDirection * ScrollUnit;
}
}
@ -111,7 +112,7 @@ void CScrollRegion::End()
if(m_AnimTime > 0.0f)
{
m_AnimTime -= Client()->RenderFrameTime();
float AnimProgress = (1.0f - powf(m_AnimTime / AnimationDuration, 3.0f)); // cubic ease out
float AnimProgress = (1.0f - powf(m_AnimTime / m_AnimTimeMax, 3.0f)); // cubic ease out
m_ScrollY = m_AnimInitScrollY + (m_AnimTargetScrollY - m_AnimInitScrollY) * AnimProgress;
}
else

View file

@ -91,6 +91,7 @@ private:
float m_ContentH;
float m_RequestScrollY; // [0, ContentHeight]
float m_AnimTimeMax;
float m_AnimTime;
float m_AnimInitScrollY;
float m_AnimTargetScrollY;