From bb43660cd43abbc40ea27823adcee645b5ed21f8 Mon Sep 17 00:00:00 2001 From: def Date: Sat, 21 Nov 2020 18:30:24 +0100 Subject: [PATCH] Fix scroll bar (fixes #3347) --- src/game/client/components/menus.cpp | 15 +++------------ src/game/client/components/menus_browser.cpp | 2 +- src/game/client/components/menus_demo.cpp | 9 ++------- src/game/client/components/menus_ingame.cpp | 2 +- 4 files changed, 7 insertions(+), 21 deletions(-) diff --git a/src/game/client/components/menus.cpp b/src/game/client/components/menus.cpp index a73942bfc..4e80c3628 100644 --- a/src/game/client/components/menus.cpp +++ b/src/game/client/components/menus.cpp @@ -2650,7 +2650,7 @@ bool CMenus::HandleListInputs(const CUIRect &View, float &ScrollValue, const flo { int NewIndex = -1; int Num = (int)(View.h / ElemHeight) + 1; - int ScrollNum = NumElems - Num + 1; + int ScrollNum = maximum(NumElems - Num + 1, 0); if(ScrollNum > 0) { if(pScrollOffset && *pScrollOffset >= 0) @@ -2663,18 +2663,9 @@ bool CMenus::HandleListInputs(const CUIRect &View, float &ScrollValue, const flo if(Input()->KeyPress(KEY_MOUSE_WHEEL_DOWN) && UI()->MouseInside(&View)) ScrollValue += 3.0f / ScrollNum; } - else - ScrollNum = 0; - if(ScrollValue < 0) - ScrollValue = 0; - if(ScrollValue > 1) - ScrollValue = 1; - - if(SelectedIndex < 0) - SelectedIndex = 0; - if(SelectedIndex >= NumElems) - SelectedIndex = NumElems; + ScrollValue = clamp(ScrollValue, 0.0f, 1.0f); + SelectedIndex = clamp(SelectedIndex, 0, NumElems); for(int i = 0; i < m_NumInputEvents; i++) { diff --git a/src/game/client/components/menus_browser.cpp b/src/game/client/components/menus_browser.cpp index f62919660..97168d024 100644 --- a/src/game/client/components/menus_browser.cpp +++ b/src/game/client/components/menus_browser.cpp @@ -190,7 +190,7 @@ void CMenus::RenderServerbrowserServerList(CUIRect View) CUIRect OriginalView = View; int Num = (int)(View.h / s_aCols[0].m_Rect.h) + 1; - int ScrollNum = NumServers - Num + 1; + int ScrollNum = maximum(NumServers - Num + 1, 0); View.y -= s_ScrollValue * ScrollNum * s_aCols[0].m_Rect.h; int NewSelected = -1; diff --git a/src/game/client/components/menus_demo.cpp b/src/game/client/components/menus_demo.cpp index e0df0c4cb..c363cb4d9 100644 --- a/src/game/client/components/menus_demo.cpp +++ b/src/game/client/components/menus_demo.cpp @@ -569,13 +569,8 @@ void CMenus::UiDoListboxStart(const void *pID, const CUIRect *pRect, float RowHe gs_ListBoxScrollValue += Num == 1 ? 0.1f : 3.0f / Num; } - if(gs_ListBoxScrollValue < 0.0f) - gs_ListBoxScrollValue = 0.0f; - if(gs_ListBoxScrollValue > 1.0f) - gs_ListBoxScrollValue = 1.0f; - Scroll.HMargin(5.0f, &Scroll); - gs_ListBoxScrollValue = DoScrollbarV(pID, &Scroll, gs_ListBoxScrollValue); + gs_ListBoxScrollValue = clamp(DoScrollbarV(pID, &Scroll, gs_ListBoxScrollValue), 0.0f, 1.0f); // the list gs_ListBoxView = gs_ListBoxOriginalView; @@ -1078,7 +1073,7 @@ void CMenus::RenderDemoList(CUIRect MainView) CUIRect OriginalView = ListBox; int Num = (int)(ListBox.h / s_aCols[0].m_Rect.h) + 1; - int ScrollNum = m_lDemos.size() - Num + 1; + int ScrollNum = maximum(m_lDemos.size() - Num + 1, 0); ListBox.y -= s_ScrollValue * ScrollNum * s_aCols[0].m_Rect.h; int NewSelected = -1; diff --git a/src/game/client/components/menus_ingame.cpp b/src/game/client/components/menus_ingame.cpp index 2bdc78a3c..450e17ab6 100644 --- a/src/game/client/components/menus_ingame.cpp +++ b/src/game/client/components/menus_ingame.cpp @@ -1029,7 +1029,7 @@ void CMenus::RenderGhost(CUIRect MainView) CUIRect OriginalView = View; int Num = (int)(View.h / s_aCols[0].m_Rect.h) + 1; - int ScrollNum = NumGhosts - Num + 1; + int ScrollNum = maximum(NumGhosts - Num + 1, 0); View.y -= s_ScrollValue * ScrollNum * s_aCols[0].m_Rect.h; int NewSelected = -1;