nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
menu.cc
Go to the documentation of this file.
1#include "client/menu.h"
2
3#include <imgui.h>
4
5#include <algorithm>
6#include <array>
7#include <cfloat>
8#include <cmath>
9#include <cstddef>
10#include <string>
11#include <utility>
12
13#include "absl/strings/str_cat.h"
14#include "client/theme.h"
15#include "client/translations.h"
16#include "extras/IconsFontAwesome6.h"
17
18namespace nanommo {
19
20namespace {
21
22// A slider from 0 to 100 percent, for a setting from nothing to full.
23void SliderPercent(const char* label, float& value) {
24 int percent = static_cast<int>(std::lround(value * 100.0f));
25 // Ctrl and a click lets a player type a number, which ImGui takes past the
26 // slider's ends unless it's told to clamp it, and 500% would be far too loud.
27 if (ImGui::SliderInt(label, &percent, 0, 100, "%d%%",
28 ImGuiSliderFlags_AlwaysClamp)) {
29 value = static_cast<float>(percent) / 100.0f;
30 }
31}
32
33// The keys, and what each does, for the keys' page and the help's, translated
34// as they're shown. A key's letter is the same in any language.
35constexpr std::array<std::pair<const char*, const char*>, 21> kKeys = {{
36 {N_("W A S D, or the arrows"), N_("Walk")},
37 {N_("Shift"), N_("Run")},
38 {N_("Space"), N_("Swing what you hold")},
39 {N_("1 to 9, and with Shift"), N_("The hotbar's two rows of skills")},
40 {N_("Click a monster"), N_("Pick it, to fight")},
41 {N_("Double-click the ground"), N_("Walk there")},
42 {N_("Click someone"), N_("Talk, learn or take a quest")},
43 {N_("Q and E, or hold the right button"), N_("Turn the camera")},
44 {N_("Mouse wheel"), N_("Zoom in and out")},
45 {"M", N_("The map")},
46 {"C", N_("Your character")},
47 {"I", N_("Your bag")},
48 {"T", N_("Your quests")},
49 {"G", N_("Your guild")},
50 {"K", N_("Crafting")},
51 {"H", N_("Help")},
52 {"O", N_("Settings")},
53 {N_("Enter"), N_("Chat")},
54 {N_("Enter, then /g and a space"), N_("Talk to your guild")},
55 {N_("Escape"), N_("Close what's open, or this menu")},
56 {N_("Drag an item out of the bag"), N_("Throw it away")},
57}};
58
59// In points: a round button's radius, as the chat's is, and the room round
60// the row for the buttons' rims, which the window would cut.
61constexpr float kRound = 23.0f;
62constexpr float kRimRoom = 4.0f;
63
64// A button in the bottom right row, with an icon, round, as the chat's is, and
65// a tooltip in the player's language.
66bool BarButton(const char* id, const char* icon, bool on, const char* tip) {
67 const float radius = Ui(kRound);
68 const bool clicked =
69 ImGui::InvisibleButton(id, {radius * 2.0f, radius * 2.0f});
70 const bool hovered = ImGui::IsItemHovered();
71 const ImVec2 min = ImGui::GetItemRectMin();
72 DrawRoundButton(ImGui::GetWindowDrawList(), {min.x + radius, min.y + radius},
73 radius, icon, hovered, on);
74 if (hovered) {
75 ImGui::SetTooltip("%s", Tr(tip));
76 }
77 return clicked;
78}
79
80} // namespace
81
82void BuildKeys() {
83 if (ImGui::BeginTable("Keys", 2, ImGuiTableFlags_RowBg)) {
84 for (const auto& [key, does] : kKeys) {
85 ImGui::TableNextRow();
86 ImGui::TableNextColumn();
87 ImGui::PushFont(GetFonts().bold, 0.0f);
88 ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(colors::kGold), "%s",
89 Tr(key));
90 ImGui::PopFont();
91 ImGui::TableNextColumn();
92 ImGui::TextUnformatted(Tr(does));
93 }
94 ImGui::EndTable();
95 }
96}
97
98Menu::Choice Menu::Build(Settings& settings, bool* reset_hotbar) {
99 Choice choice = Choice::kNothing;
100 if (open_) {
101 // The world, dimmed behind it.
102 ImGui::GetBackgroundDrawList()->AddRectFilled(
103 {0.0f, 0.0f}, ImGui::GetIO().DisplaySize, Faded(IM_COL32_BLACK, 0.45f));
104 const ImVec2 display = ImGui::GetIO().DisplaySize;
105 ImGui::SetNextWindowPos({display.x / 2.0f, display.y * 0.45f},
106 ImGuiCond_Always, {0.5f, 0.5f});
107 ImGui::SetNextWindowSize({Ui(300.0f), 0.0f});
108 BeginPanel("Menu", &open_,
109 ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize |
110 ImGuiWindowFlags_NoSavedSettings);
111 const ImVec2 button{-1.0f, Ui(40.0f)};
112 if (GameButton("Back to the game", button, true)) {
113 open_ = false;
114 }
115 if (GameButton("Settings", button)) {
116 settings_ = true;
117 open_ = false;
118 }
119 if (GameButton("Log out", button)) {
120 choice = Choice::kLogOut;
121 open_ = false;
122 }
123 if (GameButton("Quit", button)) {
124 choice = Choice::kQuit;
125 }
126 ImGui::Spacing();
127 EndPanel();
128 }
129 if (settings_) {
130 BuildSettings(settings, reset_hotbar);
131 }
132 return choice;
133}
134
135void Menu::BuildSettings(Settings& settings, bool* reset_hotbar) {
136 const ImVec2 display = ImGui::GetIO().DisplaySize;
137 ImGui::SetNextWindowPos({display.x / 2.0f, display.y / 2.0f},
138 ImGuiCond_Appearing, {0.5f, 0.5f});
139 ImGui::SetNextWindowSize(Ui(ImVec2{700.0f, 470.0f}));
140 BeginPanel("Settings", &settings_,
141 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings);
142
143 // The pages down the left, each with its English name as its ID, and the
144 // one picked on the right.
145 struct Tab {
146 Page page;
147 const char* icon;
148 const char* name;
149 };
150 constexpr std::array<Tab, 4> kPages = {{
151 {Page::kInterface, ICON_FA_TABLE_COLUMNS, N_("Interface")},
152 {Page::kSound, ICON_FA_VOLUME_HIGH, N_("Sound")},
153 {Page::kGraphics, ICON_FA_DISPLAY, N_("Graphics")},
154 {Page::kKeys, ICON_FA_KEYBOARD, N_("Keys")},
155 }};
156 ImGui::BeginChild("Pages", {Ui(170.0f), 0.0f});
157 for (const auto& [page, icon, name] : kPages) {
158 ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, {0.0f, 0.5f});
159 const std::string label = absl::StrCat(icon, " ", Tr(name), "###", name);
160 if (ImGui::Selectable(label.c_str(), page_ == page, 0, {0.0f, Ui(34.0f)})) {
161 page_ = page;
162 }
163 ImGui::PopStyleVar();
164 }
165 ImGui::EndChild();
166 ImGui::SameLine(0.0f, Ui(16.0f));
167 ImGui::BeginChild("Page");
168 ImGui::PushItemWidth(Ui(260.0f));
169 switch (page_) {
170 case Page::kInterface: {
171 ImGui::SeparatorText(Tr("Interface"));
172 // Each language by its own name, and it changes as it's picked.
173 const auto chosen =
174 std::ranges::find(kLanguages, settings.ui.language, &Language::code);
175 if (ImGui::BeginCombo(TrLabel("Language"),
176 chosen != kLanguages.end()
177 ? chosen->name
178 : settings.ui.language.c_str())) {
179 for (const Language& language : kLanguages) {
180 if (ImGui::Selectable(language.name,
181 settings.ui.language == language.code)) {
182 settings.ui.language = language.code;
183 }
184 }
185 ImGui::EndCombo();
186 }
187 if (!ImGui::IsAnyItemActive() || scale_ == 0.0f) {
188 scale_ = settings.ui.scale;
189 }
190 int percent = static_cast<int>(std::lround(scale_ * 100.0f));
191 if (ImGui::SliderInt(TrLabel("Size"), &percent, 75, 150, "%d%%",
192 ImGuiSliderFlags_AlwaysClamp)) {
193 scale_ = static_cast<float>(percent) / 100.0f;
194 }
195 if (ImGui::IsItemDeactivatedAfterEdit()) {
196 settings.ui.scale = scale_;
197 }
198 ImGui::TextDisabled(
199 "%s", Tr("On top of its size for your screen, which grows with it."));
200 ImGui::Spacing();
201 ImGui::Checkbox(TrLabel("Show the minimap"), &settings.ui.minimap);
202 ImGui::BeginDisabled(!settings.ui.minimap);
203 ImGui::Checkbox(TrLabel("Turn the minimap with the camera"),
204 &settings.ui.minimap_turns);
205 ImGui::EndDisabled();
206 ImGui::Spacing();
207 if (GameButton("Put the hotbar back in order", {0.0f, 0.0f})) {
208 settings.ui.hotbar.clear();
209 *reset_hotbar = true;
210 }
211 ImGui::TextDisabled("%s", Tr("Drag a skill onto another to swap them."));
212
213 // Whose names show over their heads, and what with.
214 ImGui::SeparatorText(Tr("Names over heads"));
215 ImGui::TextDisabled(
216 "%s", Tr("Each shows while you point at it, or it's picked."));
217 ImGui::TextDisabled("%s", Tr("Always show them over:"));
218 NameplateSettings& plates = settings.ui.nameplates;
219 ImGui::Checkbox(TrLabel("Monsters"), &plates.monsters);
220 ImGui::SameLine(Ui(150.0f));
221 ImGui::Checkbox(TrLabel("Other players"), &plates.players);
222 ImGui::Checkbox(TrLabel("NPCs"), &plates.npcs);
223 ImGui::SameLine(Ui(150.0f));
224 ImGui::Checkbox(TrLabel("You"), &plates.yours);
225 ImGui::TextDisabled("%s", Tr("With them:"));
226 ImGui::Checkbox(TrLabel("Health"), &plates.health);
227 ImGui::SameLine(Ui(150.0f));
228 ImGui::Checkbox(TrLabel("Levels"), &plates.levels);
229 ImGui::Checkbox(TrLabel("Guilds"), &plates.guilds);
230 break;
231 }
232 case Page::kSound:
233 ImGui::SeparatorText(Tr("Sound"));
234 SliderPercent(TrLabel("Volume"), settings.sound.master);
235 SliderPercent(TrLabel("Music"), settings.sound.music);
236 SliderPercent(TrLabel("Effects"), settings.sound.effects);
237 ImGui::Checkbox(TrLabel("Mute"), &settings.sound.muted);
238 ImGui::Spacing();
239 ImGui::TextDisabled(
240 ICON_FA_MUSIC " %s",
241 Tr("\"Background Music\" by Pavel Bekirov, from Pixabay"));
242 break;
243 case Page::kGraphics:
244 ImGui::SeparatorText(Tr("Graphics"));
245 ImGui::Checkbox(TrLabel("Bloom"), &settings.graphics.bloom);
246 ImGui::Checkbox(TrLabel("Cel shading"), &settings.graphics.cel_shading);
247 ImGui::Checkbox(TrLabel("Anti-aliasing"), &settings.graphics.antialias);
248 ImGui::SeparatorText(
249 absl::StrCat(ICON_FA_CLOUD_SUN " ", Tr("Sky")).c_str());
250 ImGui::Checkbox(TrLabel("Weather"), &settings.graphics.weather);
251 ImGui::Checkbox(TrLabel("Day and night"), &settings.graphics.day_night);
252 break;
253 case Page::kKeys:
254 ImGui::SeparatorText(Tr("Keys"));
255 BuildKeys();
256 break;
257 }
258 ImGui::PopItemWidth();
259 ImGui::EndChild();
260 EndPanel();
261}
262
263void Menu::BuildButtons(bool* character, bool* bag, bool* quests, bool* guild,
264 bool* map, bool* help) {
265 const ImVec2 display = ImGui::GetIO().DisplaySize;
266 // As far in from the corner as the chat's button is from its own, with room
267 // round them for their rims, and nothing behind them.
268 const float room = Ui(kRimRoom);
269 ImGui::SetNextWindowPos(
270 {display.x - Ui(18.0f) + room, display.y - Ui(18.0f) + room},
271 ImGuiCond_Always, {1.0f, 1.0f});
272 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {room, room});
273 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, Ui(ImVec2{10.0f, 10.0f}));
274 ImGui::Begin("Buttons", nullptr,
275 ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
276 ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoNav |
277 ImGuiWindowFlags_AlwaysAutoResize |
278 ImGuiWindowFlags_NoFocusOnAppearing |
279 ImGuiWindowFlags_NoSavedSettings);
280 if (BarButton("##character", ICON_FA_USER, *character, "Character (C)")) {
281 *character = !*character;
282 }
283 ImGui::SameLine();
284 if (BarButton("##bag", ICON_FA_BAG_SHOPPING, *bag, "Bag (I)")) {
285 *bag = !*bag;
286 }
287 ImGui::SameLine();
288 if (BarButton("##quests", ICON_FA_SCROLL, *quests, "Quests (T)")) {
289 *quests = !*quests;
290 }
291 ImGui::SameLine();
292 if (BarButton("##guild", ICON_FA_SHIELD_HALVED, *guild, "Guild (G)")) {
293 *guild = !*guild;
294 }
295 ImGui::SameLine();
296 if (BarButton("##map", ICON_FA_MAP, *map, "Map (M)")) {
297 *map = !*map;
298 }
299 ImGui::SameLine();
300 if (BarButton("##help", ICON_FA_CIRCLE_QUESTION, *help, "Help (H)")) {
301 *help = !*help;
302 }
303 ImGui::SameLine();
304 if (BarButton("##menu", ICON_FA_BARS, open_ || settings_, "Menu (Escape)")) {
305 open_ = !open_;
306 }
307 ImGui::End();
308 ImGui::PopStyleVar(2);
309}
310
311} // namespace nanommo
Choice Build(Settings &settings, bool *reset_hotbar)
Definition menu.cc:98
void BuildButtons(bool *character, bool *bag, bool *quests, bool *guild, bool *map, bool *help)
Definition menu.cc:263
constexpr ImU32 kGold
Definition theme.h:21
Definition chat.cc:9
constexpr const char * N_(const char *text)
bool BeginPanel(const char *name, bool *open, ImGuiWindowFlags flags, float alpha)
Definition theme.cc:320
constexpr std::array< Language, 3 > kLanguages
float Ui(float points)
Definition theme.h:67
void EndPanel()
Definition theme.cc:379
const char * Tr(const char *text)
void DrawRoundButton(ImDrawList *draw, ImVec2 centre, float radius, const char *icon, bool hovered, bool on)
Definition theme.cc:503
const char * TrLabel(const char *label)
void BuildKeys()
Definition menu.cc:82
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
Definition theme.cc:59
bool GameButton(const char *label, ImVec2 size, bool primary, bool enabled)
Definition theme.cc:381
const Fonts & GetFonts()
Definition theme.cc:66
bool antialias
Smooths the world's edges, with FXAA.
Definition graphics.h:14
std::vector< uint32_t > hotbar
Definition settings.h:44
NameplateSettings nameplates
Definition settings.h:45
const char * code
What the player sets, which the client keeps between runs.
Definition settings.h:54
InterfaceSettings ui
Definition settings.h:57
SoundSettings sound
Definition settings.h:55
GraphicsSettings graphics
Definition settings.h:56
float effects
The sound effects, apart from the music.
Definition sound.h:12
float master
Where the sliders are, from nothing to full.
Definition sound.h:9