From d1a9f0a76a97510a53cfb63bb100ac649dfad157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 26 Feb 2023 12:12:08 +0100 Subject: [PATCH] Fix erratic smooth scrolling when scroll time is changed 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. --- src/game/client/ui_scrollregion.cpp | 25 +++++++++++++------------ src/game/client/ui_scrollregion.h | 1 + 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/game/client/ui_scrollregion.cpp b/src/game/client/ui_scrollregion.cpp index 03798e0da..9f7664e5a 100644 --- a/src/game/client/ui_scrollregion.cpp +++ b/src/game/client/ui_scrollregion.cpp @@ -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 diff --git a/src/game/client/ui_scrollregion.h b/src/game/client/ui_scrollregion.h index 072c9e8ed..37230bac9 100644 --- a/src/game/client/ui_scrollregion.h +++ b/src/game/client/ui_scrollregion.h @@ -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;