VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
CenterHtmlMenu.cpp
Go to the documentation of this file.
2#include "Menu/MenuCursor.hpp"
3
10#include <memory>
11#include <utility>
12
13namespace VoltMod
14{
15
17 : _services(services),
18 _stack(*this, _services.Translations, services.Scheduler, services.Slots),
19 _cursors(services.Slots)
20{}
21
22bool CenterHtmlMenu::OpenSession(int slot, std::shared_ptr<Menu> menu, MenuOptions options)
23{
24 if (!IsValidSlot(slot) || !menu)
25 {
26 return false;
27 }
28
29 CloseAll(slot);
30
31 _services.Freeze.Open(slot, options.FreezeMovement);
32
33 Push(slot, std::move(menu));
34 return true;
35}
36
37void CenterHtmlMenu::Open(int slot, std::shared_ptr<Menu> menu)
38{
39 if (!IsValidSlot(slot) || !menu)
40 {
41 return;
42 }
43
44 if (!_stack.IsOpen(slot))
45 {
46 OpenSession(slot, std::move(menu), {});
47 return;
48 }
49
50 Push(slot, std::move(menu));
51}
52
53void CenterHtmlMenu::Push(int slot, std::shared_ptr<Menu> menu)
54{
55 _stack.Push(slot, std::move(menu));
56 ResetCursor(slot);
57
58 if (!_onFrame)
59 {
60 _onFrame = _services.Scheduler.EveryFrame([this] { OnGameFrame(); });
61 }
62}
63
64void CenterHtmlMenu::ResetCursor(int slot)
65{
66 Cursor& cursor = _cursors[slot];
67 cursor.LastSent = {};
68 cursor.LastInputTime = Time::MonotonicMs();
69 cursor.Selected = MenuCursor::First(MenuCursor::Rows(_stack.Current(slot), slot));
70}
71
73{
74 if (!IsValidSlot(slot))
75 {
76 return;
77 }
78
79 // A prompt of a closing menu must not take later chat.
80 _services.ChatInput.CancelCapture(slot);
81
82 if (!_stack.IsOpen(slot))
83 {
84 return;
85 }
86
87 // A parent menu still showing is drawn next frame.
88 _stack.Pop(slot);
89 if (_stack.IsOpen(slot))
90 {
91 ResetCursor(slot);
92 return;
93 }
94
95 _cursors[slot] = {};
96 _services.Freeze.Close(slot);
97 _services.Messages.ClearCenterHtml(slot);
98}
99
101{
102 if (!IsValidSlot(slot))
103 {
104 return;
105 }
106
107 _services.ChatInput.CancelCapture(slot);
108
109 if (!_stack.IsOpen(slot))
110 {
111 return;
112 }
113
114 _stack.Clear(slot);
115 _cursors[slot] = {};
116 _services.Freeze.Close(slot);
117 _services.Messages.ClearCenterHtml(slot);
118}
119
120void CenterHtmlMenu::CloseAll(int slot, std::string_view replyKey)
121{
122 if (auto& reply = _services.Policy.Reply; reply)
123 {
124 reply(slot, _services.Translations.Get(std::string(replyKey), slot));
125 }
126
127 CloseAll(slot);
128}
129
130void CenterHtmlMenu::Prompt(int slot, std::string prompt, std::function<bool(int, std::string_view)> callback)
131{
132 _services.ChatInput.BeginCapture(slot, std::move(prompt), std::move(callback));
133 _cursors[slot].LastSent = {};
134}
135
136std::string CenterHtmlMenu::Translate(int slot, std::string_view key, std::string_view fallback) const
137{
138 return _services.Translations.GetOr(key, slot, fallback);
139}
140
141bool CenterHtmlMenu::IsOpen(int slot) const
142{
143 return _stack.IsOpen(slot);
144}
145
146void CenterHtmlMenu::Draw(int slot, bool changed)
147{
148 Menu* menu = _stack.Current(slot);
149 if (!menu)
150 {
151 return;
152 }
153
154 Page& page = _cursors[slot].LastSent;
155 const int64_t now = Time::MonotonicMs();
156 const bool stale = changed || page.Html.empty() || now - page.RenderedAt >= RenderIntervalMs;
157 if (stale)
158 {
159 std::string html = Render(slot, *menu);
160 page.RenderedAt = now;
161 if (html != page.Html)
162 {
163 page.Html = std::move(html);
164 page.SentAt = 0;
165 }
166 }
167
168 // The game also drops a shown page on a death, a respawn or a team switch.
169 const Body body = CurrentBody(slot);
170 if (body != page.SentTo || now - page.SentAt >= CenterHtmlKeepAliveMs)
171 {
172 _services.Messages.SendCenterHtml(slot, page.Html);
173 page.SentAt = now;
174 page.SentTo = body;
175 }
176}
177
178std::string CenterHtmlMenu::Render(int slot, Menu& menu)
179{
180 // A pending capture replaces the item list with its prompt.
181 if (auto prompt = _services.ChatInput.GetPrompt(slot))
182 {
183 return RenderCaptureOverlay(menu.Title, *prompt);
184 }
185
186 const CenterHtmlView view{
187 .Describe = [this, slot](int index) { return _stack.Describe(slot, index); },
188 .Breadcrumb = _stack.Breadcrumb(slot),
189 .Slot = slot,
190 .SelectedIndex = _cursors[slot].Selected,
191 .IsSubmenu = _stack.Depth(slot) > 1,
192 };
193 return RenderMenuHtml(&menu, view, _services.Translations);
194}
195
196void CenterHtmlMenu::OnGameFrame()
197{
198 for (int slot = 0; slot < MaxPlayers; ++slot)
199 {
200 if (!_stack.IsOpen(slot))
201 {
202 continue;
203 }
204
205 const bool pressed = ReadKeys(slot);
206
207 // Input may have activated a row that closed the menu it was about to draw.
208 if (_stack.IsOpen(slot))
209 {
210 Draw(slot, pressed);
211 }
212 }
213
214 // Slot reset clears stacks without going through Close, so stop per-frame work here.
215 if (!_stack.AnyOpen())
216 {
217 _onFrame.Reset();
218 }
219}
220
221CenterHtmlMenu::Body CenterHtmlMenu::CurrentBody(int slot) const
222{
223 const Controller controller = _services.Entities.Controller(slot);
224 const Pawn pawn = controller.Pawn();
225 return {.Pawn = pawn.Ref(), .Alive = pawn.IsAlive(), .TeamNum = controller.TeamNum()};
226}
227
228} // namespace VoltMod
bool OpenSession(int slot, std::shared_ptr< Menu > menu, MenuOptions options) override
void CloseAll(int slot) override
void Open(int slot, std::shared_ptr< Menu > menu) override
CenterHtmlMenu(const Services &services)
void Close(int slot) override
bool IsOpen(int slot) const override
void Prompt(int slot, std::string prompt, std::function< bool(int slot, std::string_view text)> callback) override
std::string Translate(int slot, std::string_view key, std::string_view fallback) const override
Menu * Current(int slot)
Definition MenuStack.cpp:25
void Push(int slot, std::shared_ptr< Menu > menu)
Definition MenuStack.cpp:70
One-shot delays and repeating timers, run on the game thread.
Definition Scheduler.hpp:18
Subscription EveryFrame(std::function< void()> callback)
Definition Scheduler.cpp:37
static int64_t MonotonicMs()
Definition Durations.hpp:35
Localization system. Loads one JSON file per language; nested objects flatten into dotted keys (categ...
int First(const CursorRows &rows)
CursorRows Rows(Menu *menu, int slot)
std::string RenderMenuHtml(const Menu *menu, const CenterHtmlView &view, Translations &translations)
std::string RenderCaptureOverlay(const std::string &menuTitle, std::string_view prompt)
constexpr bool IsValidSlot(int slot)
Definition Slot.hpp:13
constexpr int64_t CenterHtmlKeepAliveMs
constexpr int MaxPlayers
Definition Slot.hpp:7