VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
MenuPresets.cpp
Go to the documentation of this file.
4#include <format>
5#include <string>
6#include <string_view>
7#include <utility>
8
9namespace VoltMod
10{
11
12std::vector<std::string> SummaryRows::Take() &&
13{
14 std::vector<std::string> lines;
15 lines.reserve(_rows.size());
16 for (auto& [label, value] : _rows)
17 {
18 lines.push_back(value.empty() ? std::move(label) : std::format("{}: {}", label, value));
19 }
20
21 return lines;
22}
23
24std::shared_ptr<Menu> BuildDurationMenu(DurationMenu spec)
25{
26 MenuBuilder builder(std::move(spec.Title));
27
28 for (const auto& [label, seconds] : spec.Presets)
29 {
30 builder.Button(label, [pick = spec.Pick, seconds](int slot) {
31 if (pick)
32 {
34 }
35 });
36 }
37
38 // An empty label omits the custom row.
39 if (!spec.CustomLabel.empty())
40 {
41 builder.Add(InputRow{.Label = std::move(spec.CustomLabel),
42 .Prompt = std::move(spec.CustomPrompt),
43 .Set =
44 [pick = spec.Pick](int slot, std::string_view text) {
45 const int seconds = ParseDuration(text);
46 if (seconds < 0)
47 {
48 return false; // re-prompt
49 }
50 if (pick)
51 {
53 }
54 return true;
55 },
56 .MaxLength = spec.MaxInputLength});
57 }
58
59 return builder.Build();
60}
61
62std::shared_ptr<Menu> BuildConfirmMenu(ConfirmMenu spec)
63{
64 MenuBuilder builder(std::move(spec.Title));
65
66 for (const auto& line : spec.Lines)
67 {
68 builder.Text(line);
69 }
70
71 // Flow normally translates both labels before building this row.
72 if (spec.ConfirmLabel.empty())
73 {
74 spec.ConfirmLabel = "Confirm";
75 }
76 if (spec.CancelLabel.empty())
77 {
78 spec.CancelLabel = "Cancel";
79 }
80
81 builder.Button(std::move(spec.ConfirmLabel), [confirm = std::move(spec.Confirm)](int slot) {
82 if (confirm)
83 {
85 }
86 });
87
88 // Cancel closes through the surface passed to Activate.
89 builder.Add(MenuItem{.Describe = [label = std::move(spec.CancelLabel)](int) { return MenuRow{.Label = label}; },
90 .Activate =
91 [cancel = std::move(spec.Cancel)](int slot, MenuSurface& surface) {
92 if (cancel)
93 {
94 cancel(slot);
95 }
96 else
97 {
98 surface.CloseAll(slot);
99 }
100 }});
101
102 return builder.Build();
103}
104
105} // namespace VoltMod
std::vector< std::string > Take() &&
int ParseDuration(std::string_view text)
Definition Strings.cpp:35
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
std::shared_ptr< Menu > BuildDurationMenu(DurationMenu spec)
std::shared_ptr< Menu > BuildConfirmMenu(ConfirmMenu spec)
std::function< MenuRow(int slot)> Describe
std::string Label
Definition MenuModel.hpp:39