VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
CenterHtmlControls.cpp
Go to the documentation of this file.
2#include "Menu/MenuCursor.hpp"
3
7#include <cstdint>
8#include <in_buttons.h>
9
10namespace VoltMod
11{
12
13bool CenterHtmlMenu::ReadKeys(int slot)
14{
15 if (!_stack.Current(slot))
16 {
17 return false;
18 }
19
20 Cursor& cursor = _cursors[slot];
21 const uint64_t buttons = _services.Entities.Controller(slot).Buttons();
22 const uint64_t pressed = buttons & ~cursor.PrevButtons;
23 cursor.PrevButtons = buttons;
24
25 if (pressed == 0)
26 {
27 return false;
28 }
29
30 const int64_t now = Time::MonotonicMs();
31 if (now - cursor.LastInputTime < PressGapMs)
32 {
33 return false;
34 }
35
36 if (_services.ChatInput.IsCapturing(slot))
37 {
38 if ((pressed & IN_RELOAD) == 0)
39 {
40 return false;
41 }
42
43 _services.ChatInput.CancelCapture(slot);
44 cursor.LastInputTime = now;
45 return true;
46 }
47
48 if (!RunKey(slot, pressed))
49 {
50 return false;
51 }
52
53 cursor.LastInputTime = now;
54 return true;
55}
56
57bool CenterHtmlMenu::RunKey(int slot, uint64_t pressed)
58{
59 if (pressed & IN_RELOAD)
60 {
61 Close(slot);
62 return true;
63 }
64
65 Menu* menu = _stack.Current(slot);
66 const int itemCount = menu ? static_cast<int>(menu->Items.size()) : 0;
67 if (itemCount == 0)
68 {
69 return false;
70 }
71
72 if (pressed & IN_FORWARD)
73 {
74 MoveCursor(slot, -1);
75 return true;
76 }
77 if (pressed & IN_BACK)
78 {
79 MoveCursor(slot, +1);
80 return true;
81 }
82 if (pressed & (IN_MOVELEFT | IN_MOVERIGHT))
83 {
84 const int direction = (pressed & IN_MOVELEFT) ? -1 : +1;
85 if (_stack.Step(slot, _cursors[slot].Selected, direction))
86 {
87 return true;
88 }
89 if (itemCount <= CenterHtmlRowsPerPage)
90 {
91 return false;
92 }
93 JumpPage(slot, direction);
94 return true;
95 }
96 if (pressed & IN_USE)
97 {
98 _stack.Activate(slot, _cursors[slot].Selected);
99 return true;
100 }
101 return false;
102}
103
104void CenterHtmlMenu::MoveCursor(int slot, int step)
105{
106 Select(slot, MenuCursor::Step(MenuCursor::Rows(_stack.Current(slot), slot), _cursors[slot].Selected, step));
107}
108
109void CenterHtmlMenu::JumpPage(int slot, int delta)
110{
111 const CursorRows rows = MenuCursor::Rows(_stack.Current(slot), slot);
112 Select(slot, MenuCursor::JumpPage(rows, _cursors[slot].Selected, CenterHtmlRowsPerPage, delta));
113}
114
115void CenterHtmlMenu::Select(int slot, int index)
116{
117 // Ignore stale or client-forged row indexes.
118 Menu* menu = _stack.Current(slot);
119 if (index < 0 || !menu || index >= static_cast<int>(menu->Items.size()))
120 {
121 return;
122 }
123
124 // Returning to a stepped row keeps its value pending.
125 if (!_stack.IsPending(slot, index))
126 {
127 _stack.ApplyPending(slot);
128 }
129
130 _cursors[slot].Selected = index;
131}
132
133} // namespace VoltMod
void Close(int slot) override
A player's identity behind a slot: name, money, team, scoreboard row. It survives death and respawn; ...
uint64_t Buttons() const
static int64_t MonotonicMs()
Definition Durations.hpp:35
int Step(const CursorRows &rows, int index, int step)
CursorRows Rows(Menu *menu, int slot)
int JumpPage(const CursorRows &rows, int index, int rowsPerPage, int delta)
constexpr int CenterHtmlRowsPerPage