VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
MenuCursor.cpp
Go to the documentation of this file.
1#include "Menu/MenuCursor.hpp"
2
3#include <algorithm>
4#include <cstddef>
5
6namespace VoltMod
7{
8
9static bool IsSelectable(const CursorRows& rows, int index)
10{
11 if (index < 0 || index >= rows.Count)
12 {
13 return false;
14 }
15
16 return !rows.Selectable || rows.Selectable(index);
17}
18
19int MenuCursor::Step(const CursorRows& rows, int index, int step)
20{
21 if (rows.Count <= 0)
22 {
23 return index;
24 }
25
26 // Bound the search so an all-disabled menu cannot loop forever.
27 int attempts = rows.Count;
28 do
29 {
30 index = WrapIndex(index + step, rows.Count);
31 }
32 while (!IsSelectable(rows, index) && --attempts > 0);
33
34 return index;
35}
36
38{
39 if (rows.Count <= 0 || IsSelectable(rows, 0))
40 {
41 return 0;
42 }
43
44 return Step(rows, 0, +1);
45}
46
47int MenuCursor::JumpPage(const CursorRows& rows, int index, int rowsPerPage, int delta)
48{
49 if (rows.Count <= 0 || rowsPerPage <= 0)
50 {
51 return index;
52 }
53
54 const int pages = PageCount(rows.Count, rowsPerPage);
55 const int page = WrapIndex(index / rowsPerPage + delta, pages);
56 const int start = page * rowsPerPage;
57 const int end = std::min(rows.Count, start + rowsPerPage);
58
59 // Preserve the page offset, then skip rows that cannot be selected.
60 int landed = std::min(start + index % rowsPerPage, end - 1);
61 for (int attempts = end - start; attempts > 0; --attempts)
62 {
64 {
65 break;
66 }
67 landed = (landed + 1 < end) ? landed + 1 : start;
68 }
69
70 return landed;
71}
72
73} // namespace VoltMod
int Step(const CursorRows &rows, int index, int step)
int First(const CursorRows &rows)
int JumpPage(const CursorRows &rows, int index, int rowsPerPage, int delta)
constexpr int WrapIndex(int value, int count)
Definition MenuModel.hpp:20
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
constexpr int PageCount(int items, int perPage)
Definition MenuModel.hpp:14
static bool IsSelectable(const CursorRows &rows, int index)
Definition MenuCursor.cpp:9