VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
PanoramaMenuLayout.cpp
Go to the documentation of this file.
2#include <cstddef>
3#include <format>
4#include <utility>
5
6namespace VoltMod
7{
8
9PanoramaMenuLayout::PanoramaMenuLayout(ScreenManager& screens, std::string_view screen, std::size_t tabs,
10 std::size_t rows, std::span<const std::string_view> iconNames)
11 : _screens(screens, std::string(screen)),
12 _root(screen),
13 _subtitle(std::format("{}_subtitle", screen)),
14 _close(std::format("{}_close", screen)),
15 _empty(std::format("{}_empty", screen)),
16 _prompt(std::format("{}_prompt", screen)),
17 _cancel(std::format("{}_cancel", screen)),
18 _back(std::format("{}_back", screen)),
19 _page(std::format("{}_page", screen)),
20 _pagePrevious(std::format("{}_page_previous", screen)),
21 _pageNext(std::format("{}_page_next", screen)),
22 _iconNames(iconNames)
23{
24 for (std::size_t index = 0; index < tabs; ++index)
25 {
26 _tabs.push_back(TabIds{
27 .Id = std::format("{}_tab{}", screen, index),
28 .Icon = std::format("{}_tab{}_icon", screen, index),
29 .LabelVar = std::format("tab{}", index),
30 });
31 }
32
33 for (std::size_t index = 0; index < rows; ++index)
34 {
35 _rows.push_back(RowIds{
36 .Id = std::format("{}_row{}", screen, index),
37 .Button = std::format("{}_row{}_button", screen, index),
38 .Decrease = std::format("{}_row{}_decrease", screen, index),
39 .Increase = std::format("{}_row{}_increase", screen, index),
40 .LabelVar = std::format("row{}_label", index),
41 .HintVar = std::format("row{}_hint", index),
42 .ValueVar = std::format("row{}_value", index),
43 });
44 }
45}
46
48{
49 return _screens.Open(slot) != nullptr;
50}
51
53{
54 _screens.Close(slot);
55}
56
57void PanoramaMenuLayout::SetHeader(int slot, const MenuHeader& header)
58{
59 Screen& screen = _screens.For(slot);
60 screen.SetText(slot, "brand", header.Brand);
61 screen.SetText(slot, "brand_subtitle", header.BrandSubtitle);
62 screen.SetText(slot, "breadcrumb", header.Breadcrumb);
63 screen.SetText(slot, "title", header.Title);
64 screen.SetText(slot, "subtitle", header.Subtitle);
65 screen.SetHidden(slot, _subtitle, header.Subtitle.empty());
66
67 for (const ScreenText& text : _texts)
68 {
69 screen.SetText(slot, text.Variable, text.Value(slot));
70 }
71}
72
73void PanoramaMenuLayout::SetSidebarVisible(int slot, bool visible)
74{
75 _screens.For(slot).SetClass(slot, _root, "screen--no-sidebar", !visible);
76}
77
78void PanoramaMenuLayout::SetHomeVisible(int slot, bool visible)
79{
80 _screens.For(slot).SetClass(slot, _root, "screen--home", visible);
81}
82
83void PanoramaMenuLayout::SetTab(int slot, int index, const MenuTab* tab)
84{
85 const TabIds& ids = _tabs[static_cast<std::size_t>(index)];
86 Screen& screen = _screens.For(slot);
87
88 screen.SetHidden(slot, ids.Id, !tab);
89 if (!tab)
90 {
91 return;
92 }
93
94 screen.SetText(slot, ids.LabelVar, tab->Label);
95 screen.SetClass(slot, ids.Id, "tab--selected", tab->Selected);
96
97 screen.SetModifier(slot, ids.Icon, "icon-set", _iconNames, tab->Icon);
98}
99
100void PanoramaMenuLayout::SetRow(int slot, int index, const MenuRow* row, std::string_view pendingHint)
101{
102 const RowIds& ids = _rows[static_cast<std::size_t>(index)];
103 Screen& screen = _screens.For(slot);
104
105 screen.SetHidden(slot, ids.Id, !row);
106 if (!row)
107 {
108 return;
109 }
110
111 const bool toggle = row->Kind == MenuRowKind::Toggle;
112 screen.SetText(slot, ids.LabelVar, row->Label);
113 screen.SetText(slot, ids.ValueVar, row->Value);
114 screen.SetClass(slot, ids.Id, "row--value", !row->Value.empty());
115 screen.SetClass(slot, ids.Id, "row--disabled", !row->Enabled);
116 screen.SetClass(slot, ids.Id, "row--on", row->State.value_or(false));
117 screen.SetClass(slot, ids.Id, "row--toggle", toggle);
118 screen.SetClass(slot, ids.Id, "row--chevron", row->Kind == MenuRowKind::Submenu || row->Kind == MenuRowKind::Input);
119 // An inert row still ships a live button, so without this it hovers and clicks like any other.
120 screen.SetClass(slot, ids.Id, "row--static", !row->Selectable);
121 screen.SetClass(slot, ids.Id, "row--steppers", row->Steppable && row->Enabled && !toggle);
122 screen.SetClass(slot, ids.Id, "row--pending", row->Pending);
123 if (row->Pending)
124 {
125 screen.SetText(slot, ids.HintVar, pendingHint);
126 }
127}
128
129void PanoramaMenuLayout::SetEmpty(int slot, std::string_view text)
130{
131 Screen& screen = _screens.For(slot);
132 screen.SetText(slot, "empty", text);
133 screen.SetHidden(slot, _empty, text.empty());
134}
135
136void PanoramaMenuLayout::SetPager(int slot, std::string_view text)
137{
138 Screen& screen = _screens.For(slot);
139 screen.SetText(slot, "page", text);
140 screen.SetHidden(slot, _page, text.empty());
141}
142
143void PanoramaMenuLayout::SetPrompt(int slot, std::string_view text, std::string_view hint)
144{
145 Screen& screen = _screens.For(slot);
146 screen.SetText(slot, "prompt_text", text);
147 screen.SetText(slot, "prompt_hint", hint);
148 screen.SetHidden(slot, _prompt, text.empty());
149 screen.SetClass(slot, _root, "screen--prompting", !text.empty());
150}
151
152void PanoramaMenuLayout::SetFooter(int slot, std::string_view back, std::string_view cancel)
153{
154 Screen& screen = _screens.For(slot);
155 screen.SetText(slot, "back", back);
156 screen.SetHidden(slot, _back, back.empty());
157 screen.SetText(slot, "cancel", cancel);
158}
159
160void PanoramaMenuLayout::AddText(std::string_view variable, std::function<std::string(int slot)> text)
161{
162 _texts.push_back(ScreenText{.Variable = std::string{variable}, .Value = std::move(text)});
163}
164
165std::optional<MenuButton> PanoramaMenuLayout::FindButton(std::string_view id) const
166{
167 if (id == _cancel)
168 {
170 }
171 if (id == _back)
172 {
174 }
175 if (id == _close)
176 {
178 }
179 if (id == _pagePrevious)
180 {
182 }
183 if (id == _pageNext)
184 {
186 }
187
188 for (int index = 0; index < TabCount(); ++index)
189 {
190 if (id == _tabs[static_cast<std::size_t>(index)].Id)
191 {
192 return MenuButton{MenuButtonKind::Tab, index};
193 }
194 }
195
196 for (int index = 0; index < RowCount(); ++index)
197 {
198 const RowIds& row = _rows[static_cast<std::size_t>(index)];
199 if (id == row.Button)
200 {
201 return MenuButton{MenuButtonKind::Row, index};
202 }
203 if (id == row.Decrease)
204 {
206 }
207 if (id == row.Increase)
208 {
209 return MenuButton{MenuButtonKind::StepUp, index};
210 }
211 }
212
213 return std::nullopt;
214}
215
216} // namespace VoltMod
void SetEmpty(int slot, std::string_view text)
void SetHomeVisible(int slot, bool visible)
PanoramaMenuLayout(ScreenManager &screens, std::string_view screen, std::size_t tabs, std::size_t rows, std::span< const std::string_view > iconNames)
void AddText(std::string_view variable, std::function< std::string(int slot)> text)
void SetFooter(int slot, std::string_view back, std::string_view cancel)
void SetPrompt(int slot, std::string_view text, std::string_view hint)
void SetHeader(int slot, const MenuHeader &header)
void SetRow(int slot, int index, const MenuRow *row, std::string_view pendingHint)
std::optional< MenuButton > FindButton(std::string_view id) const
void SetPager(int slot, std::string_view text)
void SetTab(int slot, int index, const MenuTab *tab)
void SetSidebarVisible(int slot, bool visible)
Create Screen objects and report their button presses.
A spawned Panorama layout created by ScreenManager.
Definition Screen.hpp:23
void SetHidden(int slot, std::string_view elementId, bool hidden)
Definition Screen.cpp:45
void SetClass(int slot, std::string_view elementId, std::string_view className, bool on)
Definition Screen.cpp:37
void SetText(int slot, std::string_view variable, std::string_view value)
Definition Screen.cpp:29
@ Input
Carries a value the player types in chat.
Definition MenuModel.hpp:33
@ Submenu
Opens another menu.
Definition MenuModel.hpp:30
@ Toggle
Carries an on/off value.
Definition MenuModel.hpp:31
std::string_view Subtitle
std::string_view BrandSubtitle
std::string_view Breadcrumb
std::optional< bool > State
Definition MenuModel.hpp:50
std::string Value
Definition MenuModel.hpp:41
std::string Label
Definition MenuModel.hpp:39
MenuRowKind Kind
Definition MenuModel.hpp:42
std::string_view Label
std::string_view Icon