VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
CenterHtmlRender.cpp
Go to the documentation of this file.
2
7#include <algorithm>
8#include <sstream>
9#include <string>
10#include <string_view>
11
12namespace VoltMod
13{
14
15std::string RenderHeader(const CenterHtmlHeader& header)
16{
17 std::ostringstream html;
18
19 // Show the path before the submenu title.
20 if (!header.Breadcrumb.empty())
21 {
22 html << "<font class='fontSize-s' color='" << Palette::TextMuted << "'>"
23 << Strings::EscapeHtml(header.Breadcrumb) << " › </font>";
24 }
25
26 // Titles may contain player input, so escape them before inserting markup.
27 html << "<font color='" << Palette::TextDefault << "'><b>" << Strings::EscapeHtml(header.Title) << "</b></font>";
28
29 if (!header.Subtitle.empty())
30 {
31 html << " <font class='fontSize-s' color='" << Palette::TextMuted << "'>"
32 << Strings::EscapeHtml(header.Subtitle) << "</font>";
33 }
34
35 if (header.Pages > 1)
36 {
37 html << " <font class='fontSize-s' color='" << Palette::TextMuted << "'>(" << (header.Page + 1) << "/"
38 << header.Pages << ")</font>";
39 }
40
41 html << "<br>";
42 return html.str();
43}
44
45static std::string FooterChunk(std::string_view keyColor, std::string_view keyText, const std::string& label)
46{
47 std::ostringstream html;
48 html << "<font color='" << keyColor << "'>" << keyText << "</font> "
49 << "<font color='" << Palette::TextMuted << "'>" << label << "</font>";
50 return html.str();
51}
52
53std::string RenderFooter(bool isSubmenu, bool isPaginated, bool selectedRowSteps, int slot, Translations& translations)
54{
55 // Keep rendering when a consumer has no nav.* translation.
56 auto label = [&](std::string_view key, std::string_view fallback) {
57 return translations.GetOr(key, slot, fallback);
58 };
59
60 const std::string_view closeColor = isSubmenu ? Palette::Highlight : Palette::Accent;
61 std::string closeLabel = isSubmenu ? label("nav.back", "Back") : label("nav.close", "Close");
62
63 // First row: W/S, the A/D hint for the current row (value-change or paging), and E.
64 std::ostringstream row1;
65 row1 << FooterChunk(Palette::Highlight, "[W/S]", label("nav.navigate", "Navigate"));
66
67 bool hasStepHint = selectedRowSteps || isPaginated;
68 if (selectedRowSteps)
69 {
70 row1 << " · " << FooterChunk(Palette::Highlight, "[A/D]", label("nav.change", "Change"));
71 }
72 else if (isPaginated)
73 {
74 row1 << " · " << FooterChunk(Palette::Highlight, "[A/D]", label("nav.page", "Page"));
75 }
76
77 const std::string_view selectKey = selectedRowSteps ? "nav.confirm" : "nav.select";
78 const std::string_view selectFallback = selectedRowSteps ? "Confirm" : "Select";
79 row1 << " · " << FooterChunk(Palette::Highlight, "[E]", label(selectKey, selectFallback));
80
81 std::string closeChunk = FooterChunk(closeColor, "[R]", closeLabel);
82
83 std::ostringstream html;
84 html << "<font class='fontSize-s'>" << row1.str();
85
86 // With an A/D hint there are four chunks - splitting onto two short rows is more reliable
87 // than relying on the HUD's word wrap, which sometimes pushes [R] past the visible area.
88 if (hasStepHint)
89 {
90 html << "<br>" << closeChunk;
91 }
92 else
93 {
94 html << " · " << closeChunk;
95 }
96
97 html << "</font>";
98 return html.str();
99}
100
101// One row as this renderer spells it: "Title", "Title: [ON]", "Title: &lt; 100 HP… &gt; *". The
102// model carries the two halves as plain text, so the decoration and the escaping are decided here.
103// The two markers mirror what the Panorama driver draws as classes: `…` while a stepped value is
104// still waiting to be applied, `*` for a moment after a value moved.
105static std::string RowText(const MenuRow& row)
106{
107 std::string text = Strings::EscapeHtml(row.Label);
108 if (row.Value.empty())
109 {
110 return text;
111 }
112
113 std::string value = Strings::EscapeHtml(row.Value);
114 if (row.Pending)
115 {
116 value += "…";
117 }
118
119 // The brackets say "this is a switch" and the arrows "A and D change this"; nothing else in a
120 // line of center HTML does.
121 if (row.Kind == MenuRowKind::Toggle)
122 {
123 value = "[" + value + "]";
124 }
125 else if (row.Kind == MenuRowKind::Choice)
126 {
127 value = "&lt; " + value + " &gt;";
128 }
129
130 text += ": " + value;
131 if (row.Changed)
132 {
133 text += " *";
134 }
135 return text;
136}
137
138// @p selected takes the cursor's row as it was described for the page, so the footer that needs it
139// does not describe the same row a second time.
140static std::string RenderItems(const CenterHtmlView& view, int pageStart, int pageEnd, MenuRow& selected)
141{
142 std::ostringstream html;
143
144 for (int i = pageStart; i < pageEnd; ++i)
145 {
146 const MenuRow row = view.Describe(i);
147 if (i == view.SelectedIndex)
148 {
149 selected = row;
150 }
151 std::string title = RowText(row);
152 bool selectable = row.Selectable;
153 bool enabled = row.Enabled;
154
155 if (!enabled)
156 {
157 html << "<font color='" << Palette::TextDisabled << "'>- " << title << "</font><br>";
158 }
159 else if (!selectable)
160 {
161 // Rendered without a cursor mark - the row is informational, not a target.
162 html << "<font color='" << Palette::TextMuted << "'>" << title << "</font><br>";
163 }
164 else if (i == view.SelectedIndex)
165 {
166 // No per-row [E]: the cursor signals selection, the footer carries the hint, and a
167 // shorter line avoids wrapping in long locales.
168 html << "<font color='" << Palette::AccentBright << "'><b>&gt; " << title << "</b></font><br>";
169 }
170 else
171 {
172 html << "<font color='" << Palette::TextDefault << "'> " << title << "</font><br>";
173 }
174 }
175
176 return html.str();
177}
178
179std::string RenderMenuHtml(const Menu* menu, const CenterHtmlView& view, Translations& translations)
180{
181 if (!menu || !view.Describe)
182 {
183 return "";
184 }
185
186 int itemCount = static_cast<int>(menu->Items.size());
187 int totalPages = PageCount(itemCount, CenterHtmlRowsPerPage);
188 int currentPage = itemCount == 0 ? 0 : view.SelectedIndex / CenterHtmlRowsPerPage;
189 int pageStart = currentPage * CenterHtmlRowsPerPage;
190 int pageEnd = std::min(itemCount, pageStart + CenterHtmlRowsPerPage);
191
192 std::ostringstream html;
193
194 html << RenderHeader({.Title = menu->Title,
195 .Subtitle = menu->Subtitle,
196 .Breadcrumb = view.Breadcrumb,
197 .Page = currentPage,
198 .Pages = totalPages});
199
200 // Only the branch that draws it pays for it: the empty line is looked up here rather than by
201 // every caller on every frame, and the cursor's row comes back from the page that drew it.
202 MenuRow selected{.Enabled = false};
203 if (itemCount == 0)
204 {
205 const std::string empty = translations.GetOr("menu.empty", view.Slot, "Nothing here");
206 html << "<font color='" << Palette::TextMuted << "'>" << Strings::EscapeHtml(empty) << "</font><br>";
207 }
208 else
209 {
210 html << RenderItems(view, pageStart, pageEnd, selected);
211 }
212
213 html << RenderFooter(view.IsSubmenu, totalPages > 1, selected.Enabled && selected.Steppable, view.Slot,
214 translations);
215
216 return html.str();
217}
218
219std::string RenderCaptureOverlay(const std::string& menuTitle, std::string_view prompt)
220{
221 std::ostringstream html;
222 // Escape the title and prompt before inserting caller-supplied text into markup.
223 html << "<font color='" << Palette::TextDefault << "'><b>" << Strings::EscapeHtml(menuTitle) << "</b></font><br>"
224 << "<font color='" << Palette::TextDefault << "'>" << Strings::EscapeHtml(prompt) << "</font><br>"
225 << "<font class='fontSize-s' color='" << Palette::TextMuted << "'>Type your answer in chat</font><br>"
226 << "<font class='fontSize-s'>"
227 << "<font color='" << Palette::Accent << "'>[R]</font> "
228 << "<font color='" << Palette::TextMuted << "'>Cancel</font>"
229 << "</font>";
230 return html.str();
231}
232
233} // namespace VoltMod
static std::string EscapeHtml(std::string_view text)
Definition Strings.cpp:185
Localization system. Loads one JSON file per language; nested objects flatten into dotted keys (categ...
std::string GetOr(std::string_view key, int slot, std::string_view fallback) const
constexpr std::string_view TextDisabled
Definition Palette.hpp:20
constexpr std::string_view Highlight
Definition Palette.hpp:16
constexpr std::string_view TextDefault
Definition Palette.hpp:18
constexpr std::string_view AccentBright
Definition Palette.hpp:15
constexpr std::string_view TextMuted
Definition Palette.hpp:19
constexpr std::string_view Accent
Definition Palette.hpp:14
@ Choice
Carries one of several values, stepped left and right.
Definition MenuModel.hpp:32
@ Toggle
Carries an on/off value.
Definition MenuModel.hpp:31
std::string RenderMenuHtml(const Menu *menu, const CenterHtmlView &view, Translations &translations)
std::string RenderCaptureOverlay(const std::string &menuTitle, std::string_view prompt)
constexpr int PageCount(int items, int perPage)
Definition MenuModel.hpp:14
std::string RenderFooter(bool isSubmenu, bool isPaginated, bool selectedRowSteps, int slot, Translations &translations)
std::string RenderHeader(const CenterHtmlHeader &header)
static std::string RowText(const MenuRow &row)
constexpr int CenterHtmlRowsPerPage
static std::string FooterChunk(std::string_view keyColor, std::string_view keyText, const std::string &label)
static std::string RenderItems(const CenterHtmlView &view, int pageStart, int pageEnd, MenuRow &selected)
std::function< MenuRow(int index)> Describe
std::string Value
Definition MenuModel.hpp:41
std::string Label
Definition MenuModel.hpp:39
MenuRowKind Kind
Definition MenuModel.hpp:42
std::string Subtitle
std::string Title
std::vector< MenuItem > Items