nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
theme.cc
Go to the documentation of this file.
1#include "client/theme.h"
2
3#include <imgui.h>
4#include <imgui_internal.h>
5#include <raylib.h>
6
7#include <algorithm>
8#include <cfloat>
9#include <cmath>
10#include <cstdint>
11#include <string>
12#include <utility>
13
14#include "client/translations.h"
15
16namespace nanommo {
17
18namespace {
19
20Fonts fonts;
21float scale = 1.0f;
22
23ImVec4 Float4(ImU32 color) { return ImGui::ColorConvertU32ToFloat4(color); }
24
25ImVec4 Float4(ImU32 color, float alpha) {
26 ImVec4 out = Float4(color);
27 out.w = alpha;
28 return out;
29}
30
31// One of the fonts in the folder, or null when it isn't there. Merged, it
32// goes into the font added before it, for the letters that one hasn't got.
33ImFont* AddFont(const std::string& folder, const char* file,
34 bool merge = false) {
35 const std::string path = folder + "/" + file;
36 if (!FileExists(path.c_str())) {
37 return nullptr;
38 }
39 ImFontConfig config;
40 config.PixelSnapH = true;
41 config.MergeMode = merge;
42 return ImGui::GetIO().Fonts->AddFontFromFileTTF(path.c_str(), kTextSize,
43 &config);
44}
45
46// ImGui's own, for a face that isn't there, so that a client without its
47// fonts still has words.
48ImFont* OrDefault(ImFont* font) {
49 if (font != nullptr) {
50 return font;
51 }
52 ImFontConfig config;
53 config.SizePixels = kTextSize;
54 return ImGui::GetIO().Fonts->AddFontDefaultVector(&config);
55}
56
57} // namespace
58
59ImU32 Faded(ImU32 color, float share) {
60 const float alpha = static_cast<float>((color >> IM_COL32_A_SHIFT) & 0xFF) *
61 std::clamp(share, 0.0f, 1.0f);
62 return (color & ~IM_COL32_A_MASK) |
63 (static_cast<ImU32>(alpha) << IM_COL32_A_SHIFT);
64}
65
66const Fonts& GetFonts() {
67 static Fonts fallback;
68 if (fonts.text == nullptr) {
69 fallback.text = fallback.bold = fallback.title = ImGui::GetFont();
70 return fallback;
71 }
72 return fonts;
73}
74
75void LoadFonts(const std::string& folder) {
76 fonts.bold = OrDefault(AddFont(folder, "AlegreyaSans-Bold.ttf"));
77 fonts.title = AddFont(folder, "Cinzel-Bold.ttf");
78 // Cinzel has no ș, ț or ă, which Romanian needs, so those, and any other
79 // letter it hasn't got, come from Alegreya Sans's bold.
80 if (fonts.title != nullptr) {
81 AddFont(folder, "AlegreyaSans-Bold.ttf", true);
82 }
83 fonts.title = OrDefault(fonts.title);
84 // Last, as rlImGui merges Font Awesome's icons into the last font added.
85 fonts.text = OrDefault(AddFont(folder, "AlegreyaSans-Medium.ttf"));
86 ImGui::GetIO().FontDefault = fonts.text;
87}
88
89std::string FontFolder() {
90 return std::string(GetApplicationDirectory()) + "fonts";
91}
92
93float InterfaceScale(float height, float player) {
94 constexpr float kReference = 1080.0f;
95 constexpr float kSmallest = 0.75f;
96 const float screen = std::max(height / kReference, kSmallest);
97 return std::clamp(screen * std::clamp(player, 0.5f, 2.0f), 0.5f, 4.0f);
98}
99
100float UiScale() { return scale; }
101
102void ApplyTheme(float at) {
103 ImGuiStyle style;
104 style.WindowPadding = {16.0f, 12.0f};
105 style.FramePadding = {10.0f, 6.0f};
106 style.ItemSpacing = {8.0f, 7.0f};
107 style.ItemInnerSpacing = {6.0f, 5.0f};
108 style.CellPadding = {6.0f, 4.0f};
109 style.IndentSpacing = 18.0f;
110 style.ScrollbarSize = 10.0f;
111 style.GrabMinSize = 12.0f;
112 style.WindowRounding = 0.0f;
113 style.ChildRounding = 0.0f;
114 style.FrameRounding = 2.0f;
115 style.PopupRounding = 2.0f;
116 style.ScrollbarRounding = 2.0f;
117 style.GrabRounding = 2.0f;
118 style.TabRounding = 2.0f;
119 // Our panels draw their own borders.
120 style.WindowBorderSize = 0.0f;
121 style.ChildBorderSize = 0.0f;
122 style.FrameBorderSize = 1.0f;
123 style.PopupBorderSize = 1.0f;
124 style.WindowTitleAlign = {0.5f, 0.5f};
125 style.SeparatorTextAlign = {0.5f, 0.5f};
126 style.SeparatorTextBorderSize = 1.0f;
127
128 ImVec4* c = style.Colors;
129 c[ImGuiCol_Text] = Float4(colors::kPaper);
130 c[ImGuiCol_TextDisabled] = Float4(colors::kMuted);
131 c[ImGuiCol_WindowBg] = Float4(colors::kNight, 0.94f);
132 c[ImGuiCol_ChildBg] = Float4(colors::kNight, 0.0f);
133 c[ImGuiCol_PopupBg] = Float4(IM_COL32(22, 10, 12, 255), 0.97f);
134 c[ImGuiCol_Border] = Float4(colors::kBrass, 0.8f);
135 c[ImGuiCol_BorderShadow] = Float4(colors::kNight, 0.0f);
136 c[ImGuiCol_FrameBg] = Float4(colors::kNight, 0.85f);
137 c[ImGuiCol_FrameBgHovered] = Float4(IM_COL32(40, 10, 15, 255));
138 c[ImGuiCol_FrameBgActive] = Float4(IM_COL32(56, 12, 19, 255));
139 c[ImGuiCol_TitleBg] = Float4(colors::kWine);
140 c[ImGuiCol_TitleBgActive] = Float4(colors::kCrimson);
141 c[ImGuiCol_TitleBgCollapsed] = Float4(colors::kWine, 0.6f);
142 c[ImGuiCol_MenuBarBg] = Float4(colors::kCoal);
143 c[ImGuiCol_ScrollbarBg] = Float4(colors::kNight, 0.0f);
144 c[ImGuiCol_ScrollbarGrab] = Float4(colors::kWine);
145 c[ImGuiCol_ScrollbarGrabHovered] = Float4(colors::kCrimson);
146 c[ImGuiCol_ScrollbarGrabActive] = Float4(colors::kRed);
147 c[ImGuiCol_CheckMark] = Float4(colors::kGold);
148 c[ImGuiCol_CheckboxSelectedBg] = Float4(colors::kWine);
149 c[ImGuiCol_SliderGrab] = Float4(colors::kGoldDark);
150 c[ImGuiCol_SliderGrabActive] = Float4(colors::kGold);
151 c[ImGuiCol_Button] = Float4(colors::kWine, 0.9f);
152 c[ImGuiCol_ButtonHovered] = Float4(colors::kCrimson);
153 c[ImGuiCol_ButtonActive] = Float4(colors::kRed);
154 c[ImGuiCol_Header] = Float4(colors::kWine, 0.85f);
155 c[ImGuiCol_HeaderHovered] = Float4(colors::kCrimson, 0.7f);
156 c[ImGuiCol_HeaderActive] = Float4(colors::kCrimson);
157 c[ImGuiCol_Separator] = Float4(colors::kBrass, 0.6f);
158 c[ImGuiCol_SeparatorHovered] = Float4(colors::kGoldDark);
159 c[ImGuiCol_SeparatorActive] = Float4(colors::kGold);
160 c[ImGuiCol_ResizeGrip] = Float4(colors::kBrass, 0.35f);
161 c[ImGuiCol_ResizeGripHovered] = Float4(colors::kGoldDark, 0.7f);
162 c[ImGuiCol_ResizeGripActive] = Float4(colors::kGold);
163 c[ImGuiCol_InputTextCursor] = Float4(colors::kGold);
164 c[ImGuiCol_Tab] = Float4(colors::kWine, 0.6f);
165 c[ImGuiCol_TabHovered] = Float4(colors::kCrimson);
166 c[ImGuiCol_TabSelected] = Float4(colors::kCrimson, 0.9f);
167 c[ImGuiCol_TabSelectedOverline] = Float4(colors::kGold);
168 c[ImGuiCol_TabDimmed] = Float4(colors::kWine, 0.4f);
169 c[ImGuiCol_TabDimmedSelected] = Float4(colors::kWine);
170 c[ImGuiCol_TabDimmedSelectedOverline] = Float4(colors::kGoldDark);
171 c[ImGuiCol_PlotLines] = Float4(colors::kGold);
172 c[ImGuiCol_PlotLinesHovered] = Float4(colors::kFlame);
173 c[ImGuiCol_PlotHistogram] = Float4(colors::kGoldDark);
174 c[ImGuiCol_PlotHistogramHovered] = Float4(colors::kGold);
175 c[ImGuiCol_TableHeaderBg] = Float4(colors::kWine, 0.7f);
176 c[ImGuiCol_TableBorderStrong] = Float4(colors::kBrass, 0.6f);
177 c[ImGuiCol_TableBorderLight] = Float4(colors::kBrass, 0.3f);
178 c[ImGuiCol_TableRowBg] = Float4(colors::kNight, 0.0f);
179 c[ImGuiCol_TableRowBgAlt] = ImVec4(1.0f, 1.0f, 1.0f, 0.03f);
180 c[ImGuiCol_TextLink] = Float4(colors::kFlame);
181 c[ImGuiCol_TextSelectedBg] = Float4(colors::kCrimson, 0.6f);
182 c[ImGuiCol_TreeLines] = Float4(colors::kBrass, 0.5f);
183 c[ImGuiCol_DragDropTarget] = Float4(colors::kGold);
184 c[ImGuiCol_DragDropTargetBg] = Float4(colors::kGold, 0.1f);
185 c[ImGuiCol_NavCursor] = Float4(colors::kGold);
186 c[ImGuiCol_NavWindowingHighlight] = Float4(colors::kGold, 0.7f);
187 c[ImGuiCol_NavWindowingDimBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.5f);
188 c[ImGuiCol_ModalWindowDimBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.6f);
189
190 style.ScaleAllSizes(at);
191 // Scaling rounds sizes down, which takes a one-point border to nothing under
192 // a scale of one, so the borders keep a pixel at the least.
193 style.FrameBorderSize = std::max(style.FrameBorderSize, 1.0f);
194 style.PopupBorderSize = std::max(style.PopupBorderSize, 1.0f);
195 style.SeparatorTextBorderSize = std::max(style.SeparatorTextBorderSize, 1.0f);
196 style.FontSizeBase = kTextSize;
197 style.FontScaleMain = at;
198 ImGui::GetStyle() = style;
199 scale = at;
200}
201
202void PathPanel(ImDrawList* draw, ImVec2 min, ImVec2 max, float cut) {
203 draw->PathLineTo({min.x + cut, min.y});
204 draw->PathLineTo({max.x - cut, min.y});
205 draw->PathLineTo({max.x, min.y + cut});
206 draw->PathLineTo({max.x, max.y - cut});
207 draw->PathLineTo({max.x - cut, max.y});
208 draw->PathLineTo({min.x + cut, max.y});
209 draw->PathLineTo({min.x, max.y - cut});
210 draw->PathLineTo({min.x, min.y + cut});
211}
212
213void DrawDiamond(ImDrawList* draw, ImVec2 centre, float radius, ImU32 fill,
214 ImU32 outline) {
215 const ImVec2 top{centre.x, centre.y - radius};
216 const ImVec2 right{centre.x + radius, centre.y};
217 const ImVec2 bottom{centre.x, centre.y + radius};
218 const ImVec2 left{centre.x - radius, centre.y};
219 draw->AddQuadFilled(top, right, bottom, left, fill);
220 draw->AddQuad(top, right, bottom, left, outline, std::max(1.0f, Ui(1.0f)));
221}
222
223void DrawPanel(ImDrawList* draw, ImVec2 min, ImVec2 max, float alpha) {
224 const float cut = Ui(10.0f);
225 // A shadow, a little below and to the right, so that it lifts off the world.
226 const ImVec2 drop = Ui(ImVec2{3.0f, 4.0f});
227 PathPanel(draw, {min.x + drop.x, min.y + drop.y},
228 {max.x + drop.x, max.y + drop.y}, cut);
229 draw->PathFillConvex(Faded(IM_COL32_BLACK, 0.45f * alpha));
230
231 // Dark, a little lighter at the top, as if lit from above.
232 const int first = draw->VtxBuffer.Size;
233 PathPanel(draw, min, max, cut);
234 draw->PathFillConvex(Faded(colors::kNight, alpha));
235 ImGui::ShadeVertsLinearColorGradientKeepAlpha(
236 draw, first, draw->VtxBuffer.Size, min, {min.x, max.y}, colors::kCoal,
238
239 // A red glow under the title.
240 const float glow = std::min(Ui(46.0f), (max.y - min.y) * 0.5f);
241 draw->AddRectFilledMultiColor(
242 {min.x + cut * 0.6f, min.y + Ui(2.0f)},
243 {max.x - cut * 0.6f, min.y + glow}, Faded(colors::kWine, 0.55f * alpha),
244 Faded(colors::kWine, 0.55f * alpha), Faded(colors::kWine, 0.0f),
245 Faded(colors::kWine, 0.0f));
246
247 // Brass round it, and a thin light inside the brass.
248 PathPanel(draw, min, max, cut);
249 draw->PathStroke(Faded(colors::kBrass, alpha), ImDrawFlags_Closed, Ui(1.6f));
250 const float inset = Ui(3.5f);
251 PathPanel(draw, {min.x + inset, min.y + inset},
252 {max.x - inset, max.y - inset}, cut * 0.75f);
253 draw->PathStroke(Faded(colors::kGoldLight, 0.12f * alpha), ImDrawFlags_Closed,
254 std::max(1.0f, Ui(0.8f)));
255}
256
257void DrawDivider(ImDrawList* draw, ImVec2 left, float width) {
258 const float middle = left.x + width / 2.0f;
259 const float thickness = std::max(1.0f, Ui(1.0f));
260 draw->AddRectFilledMultiColor({left.x, left.y}, {middle, left.y + thickness},
263 Faded(colors::kGoldDark, 0.0f));
264 draw->AddRectFilledMultiColor(
265 {middle, left.y}, {left.x + width, left.y + thickness}, colors::kGoldDark,
268 DrawDiamond(draw, {middle, left.y + thickness / 2.0f}, Ui(3.5f),
270}
271
272void DrawShadowedText(ImDrawList* draw, ImFont* font, float size, ImVec2 at,
273 ImU32 color, const char* text) {
274 const float offset = std::max(1.0f, size / 16.0f);
275 draw->AddText(
276 font, size, {at.x + offset, at.y + offset},
278 static_cast<float>((color >> IM_COL32_A_SHIFT) & 0xFF) / 255.0f),
279 text);
280 draw->AddText(font, size, at, color, text);
281}
282
283void DrawGradientText(ImDrawList* draw, ImFont* font, float size, ImVec2 at,
284 ImU32 top, ImU32 bottom, const char* text) {
285 const float offset = std::max(1.0f, size / 14.0f);
286 const float alpha =
287 static_cast<float>((top >> IM_COL32_A_SHIFT) & 0xFF) / 255.0f;
288 draw->AddText(font, size, {at.x + offset, at.y + offset},
289 Faded(IM_COL32_BLACK, 0.85f * alpha), text);
290 const int first = draw->VtxBuffer.Size;
291 draw->AddText(font, size, at, top, text);
292 // The glyphs' boxes are taller than their capitals, so the gradient runs over
293 // the middle of the line, where the letters are.
294 ImGui::ShadeVertsLinearColorGradientKeepAlpha(
295 draw, first, draw->VtxBuffer.Size, {at.x, at.y + size * 0.2f},
296 {at.x, at.y + size * 0.85f}, top, bottom);
297}
298
299void DrawTitle(ImDrawList* draw, ImVec2 top_centre, float size,
300 const char* text) {
301 ImFont* font = GetFonts().title;
302 const ImVec2 extent = font->CalcTextSizeA(size, FLT_MAX, 0.0f, text);
303 DrawGradientText(draw, font, size,
304 {top_centre.x - extent.x / 2.0f, top_centre.y},
306}
307
308std::string Shown(const char* label) {
309 const std::string all = label;
310 return all.substr(0, all.find("##"));
311}
312
313namespace {
314
315// The height of a panel's title, above what's in it.
316float TitleHeight() { return Ui(34.0f); }
317
318} // namespace
319
320bool BeginPanel(const char* name, bool* open, ImGuiWindowFlags flags,
321 float alpha) {
322 // Its background, title and close button are ours, drawn here.
323 const bool shown = ImGui::Begin(
324 name, nullptr,
325 flags | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBackground |
326 ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar);
327 ImDrawList* draw = ImGui::GetWindowDrawList();
328 const ImVec2 min = ImGui::GetWindowPos();
329 const ImVec2 size = ImGui::GetWindowSize();
330 const ImVec2 max{min.x + size.x, min.y + size.y};
331 // Under everything in the window, as nothing else is drawn in it yet.
332 draw->PushClipRectFullScreen();
333 DrawPanel(draw, min, max, alpha);
334 // In the player's language, while the window keeps its English name as its
335 // ID.
336 const std::string title = Tr(Shown(name).c_str());
337 const float title_size = Ui(19.0f);
338 if (!title.empty()) {
339 DrawTitle(draw, {min.x + size.x / 2.0f, min.y + Ui(9.0f)}, title_size,
340 title.c_str());
341 DrawDivider(draw, {min.x + Ui(18.0f), min.y + TitleHeight()},
342 size.x - Ui(36.0f));
343 }
344 draw->PopClipRect();
345
346 if (open != nullptr) {
347 // A small square with an X in the top right corner, as far in as the
348 // window's padding: any further out, and a window that fits what's in it
349 // would grow to fit the button, a little more each frame.
350 const float button = Ui(20.0f);
351 const float inset = ImGui::GetStyle().WindowPadding.x;
352 const ImVec2 corner{max.x - button - inset, min.y + Ui(8.0f)};
353 const ImVec2 saved = ImGui::GetCursorPos();
354 ImGui::SetCursorScreenPos(corner);
355 if (ImGui::InvisibleButton("##close", {button, button})) {
356 *open = false;
357 }
358 const bool hovered = ImGui::IsItemHovered();
359 const ImVec2 middle{corner.x + button / 2.0f, corner.y + button / 2.0f};
360 const float arm = button * 0.22f;
361 const ImU32 ink = hovered ? colors::kEmber : colors::kMuted;
362 if (hovered) {
363 draw->AddCircleFilled(middle, button * 0.5f, Faded(colors::kWine, 0.9f));
364 }
365 draw->AddLine({middle.x - arm, middle.y - arm},
366 {middle.x + arm, middle.y + arm}, ink, Ui(2.0f));
367 draw->AddLine({middle.x - arm, middle.y + arm},
368 {middle.x + arm, middle.y - arm}, ink, Ui(2.0f));
369 ImGui::SetCursorPos(saved);
370 }
371 if (!title.empty()) {
372 // What's in the panel starts under its title.
373 ImGui::SetCursorPosY(
374 std::max(ImGui::GetCursorPosY(), TitleHeight() + Ui(10.0f)));
375 }
376 return shown;
377}
378
379void EndPanel() { ImGui::End(); }
380
381bool GameButton(const char* label, ImVec2 size, bool primary, bool enabled) {
382 const std::string text = Tr(Shown(label).c_str());
383 ImFont* font = GetFonts().title;
384 const float text_size = primary ? Ui(17.0f) : Ui(15.0f);
385 const ImVec2 extent =
386 font->CalcTextSizeA(text_size, FLT_MAX, 0.0f, text.c_str());
387 if (size.x < 0.0f) {
388 size.x = ImGui::GetContentRegionAvail().x;
389 } else if (size.x == 0.0f) {
390 size.x = extent.x + Ui(36.0f);
391 }
392 if (size.y <= 0.0f) {
393 size.y = primary ? Ui(44.0f) : Ui(34.0f);
394 }
395 ImGui::BeginDisabled(!enabled);
396 const bool clicked = ImGui::InvisibleButton(label, size) && enabled;
397 ImGui::EndDisabled();
398 const bool hovered = enabled && ImGui::IsItemHovered();
399 const bool held = enabled && ImGui::IsItemActive();
400 const ImVec2 min = ImGui::GetItemRectMin();
401 const ImVec2 max = ImGui::GetItemRectMax();
402 ImDrawList* draw = ImGui::GetWindowDrawList();
403 const float cut = std::min(Ui(7.0f), size.y * 0.3f);
404
405 ImU32 top = primary ? colors::kRed : IM_COL32(64, 14, 21, 255);
406 ImU32 bottom = primary ? colors::kWine : IM_COL32(28, 8, 11, 255);
407 if (primary && hovered) {
408 top = colors::kEmber;
409 bottom = colors::kCrimson;
410 } else if (hovered) {
411 top = IM_COL32(110, 20, 30, 255);
412 bottom = IM_COL32(48, 10, 15, 255);
413 }
414 if (held) {
415 std::swap(top, bottom);
416 }
417 const float alpha = enabled ? 1.0f : 0.45f;
418 if (primary && hovered) {
419 // A glow round it, like a fire's.
420 for (int ring = 3; ring >= 1; --ring) {
421 const float spread = Ui(2.5f) * static_cast<float>(ring);
422 PathPanel(draw, {min.x - spread, min.y - spread},
423 {max.x + spread, max.y + spread}, cut + spread);
424 draw->PathFillConvex(Faded(colors::kEmber, 0.08f));
425 }
426 }
427 const int first = draw->VtxBuffer.Size;
428 PathPanel(draw, min, max, cut);
429 draw->PathFillConvex(Faded(top, alpha));
430 ImGui::ShadeVertsLinearColorGradientKeepAlpha(
431 draw, first, draw->VtxBuffer.Size, min, {min.x, max.y}, top, bottom);
432 // A sheen along its top half.
433 draw->AddRectFilledMultiColor(
434 {min.x + cut, min.y + Ui(1.5f)}, {max.x - cut, (min.y + max.y) / 2.0f},
435 Faded(IM_COL32_WHITE, 0.10f * alpha),
436 Faded(IM_COL32_WHITE, 0.10f * alpha), Faded(IM_COL32_WHITE, 0.0f),
437 Faded(IM_COL32_WHITE, 0.0f));
438 PathPanel(draw, min, max, cut);
439 draw->PathStroke(
440 Faded(hovered ? colors::kGold
441 : (primary ? colors::kGoldDark : colors::kBrass),
442 alpha),
443 ImDrawFlags_Closed, Ui(primary ? 1.6f : 1.2f));
444 const ImVec2 at{(min.x + max.x - extent.x) / 2.0f,
445 (min.y + max.y - extent.y) / 2.0f + (held ? Ui(1.0f) : 0.0f)};
446 DrawGradientText(draw, font, text_size, at, Faded(colors::kGoldLight, alpha),
447 Faded(hovered ? colors::kGold : colors::kGoldDark, alpha),
448 text.c_str());
449 return clicked;
450}
451
452void DrawBar(ImDrawList* draw, ImVec2 min, ImVec2 max, float share, ImU32 color,
453 ImU32 dark, const char* text) {
454 share = std::clamp(share, 0.0f, 1.0f);
455 draw->AddRectFilled(min, max, Faded(colors::kNight, 0.9f));
456 if (share > 0.0f) {
457 const ImVec2 full{min.x + (max.x - min.x) * share, max.y};
458 draw->AddRectFilledMultiColor(min, full, color, color, dark, dark);
459 // Light along the top of what's filled.
460 draw->AddRectFilledMultiColor(
461 min, {full.x, min.y + (max.y - min.y) * 0.45f},
462 Faded(IM_COL32_WHITE, 0.25f), Faded(IM_COL32_WHITE, 0.25f),
463 Faded(IM_COL32_WHITE, 0.0f), Faded(IM_COL32_WHITE, 0.0f));
464 // A bright edge where it ends.
465 draw->AddLine({full.x, min.y}, {full.x, max.y},
466 Faded(colors::kGoldLight, 0.6f), std::max(1.0f, Ui(1.0f)));
467 }
468 draw->AddRect(min, max, colors::kBrass, 0.0f, 0, std::max(1.0f, Ui(1.0f)));
469 if (text != nullptr && text[0] != '\0') {
470 ImFont* font = GetFonts().bold;
471 const float size = std::min(Ui(15.0f), (max.y - min.y) * 0.95f);
472 const ImVec2 extent = font->CalcTextSizeA(size, FLT_MAX, 0.0f, text);
474 draw, font, size,
475 {(min.x + max.x - extent.x) / 2.0f, (min.y + max.y - extent.y) / 2.0f},
476 colors::kPaper, text);
477 }
478}
479
480void DrawSlot(ImDrawList* draw, ImVec2 min, ImVec2 max, ImU32 edge) {
481 draw->AddRectFilled({min.x + Ui(1.0f), min.y + Ui(2.0f)},
482 {max.x + Ui(1.0f), max.y + Ui(2.0f)},
483 Faded(IM_COL32_BLACK, 0.5f));
484 // Darker at the top, as a well's shadow falls.
485 draw->AddRectFilledMultiColor(
486 min, max, IM_COL32(6, 3, 4, 240), IM_COL32(6, 3, 4, 240),
487 IM_COL32(34, 16, 18, 240), IM_COL32(34, 16, 18, 240));
488 draw->AddRect(min, max, colors::kBrass, 0.0f, 0, std::max(1.0f, Ui(1.4f)));
489 const float inset = Ui(2.5f);
490 draw->AddRect({min.x + inset, min.y + inset}, {max.x - inset, max.y - inset},
491 Faded(colors::kNight, 0.9f), 0.0f, 0, std::max(1.0f, Ui(1.0f)));
492 if (((edge >> IM_COL32_A_SHIFT) & 0xFF) != 0) {
493 draw->AddRect({min.x - Ui(1.0f), min.y - Ui(1.0f)},
494 {max.x + Ui(1.0f), max.y + Ui(1.0f)}, edge, 0.0f, 0,
495 Ui(2.2f));
496 draw->AddRectFilledMultiColor({min.x + inset, min.y + inset},
497 {max.x - inset, max.y - inset},
498 Faded(edge, 0.0f), Faded(edge, 0.0f),
499 Faded(edge, 0.22f), Faded(edge, 0.22f));
500 }
501}
502
503void DrawRoundButton(ImDrawList* draw, ImVec2 centre, float radius,
504 const char* icon, bool hovered, bool on) {
505 draw->AddCircleFilled(centre, radius,
506 hovered || on ? colors::kCrimson : colors::kWine);
507 const ImU32 rim =
508 on ? colors::kEmber : (hovered ? colors::kGold : colors::kBrass);
509 draw->AddCircle(centre, radius, rim, 0, Ui(2.5f));
510 // The icon, a little under half as wide as the disc.
511 ImFont* font = GetFonts().text;
512 const float size = radius * 0.83f;
513 const ImVec2 extent = font->CalcTextSizeA(size, FLT_MAX, 0.0f, icon);
514 DrawShadowedText(draw, font, size,
515 {centre.x - extent.x / 2.0f, centre.y - extent.y / 2.0f},
516 colors::kGoldLight, icon);
517}
518
519void DrawSlotIcon(ImDrawList* draw, ImVec2 min, ImVec2 max, const char* icon,
520 ImU32 color, float alpha) {
521 const ImVec2 middle{(min.x + max.x) / 2.0f, (min.y + max.y) / 2.0f};
522 const float size = (max.y - min.y) * 0.48f;
523 // A glow of the icon's colour behind it, fading out from the middle.
524 const float glow = (max.y - min.y) * 0.42f;
525 for (int ring = 4; ring >= 1; --ring) {
526 draw->AddCircleFilled(middle, glow * static_cast<float>(ring) / 4.0f,
527 Faded(color, 0.05f * alpha));
528 }
529 ImFont* font = GetFonts().text;
530 const ImVec2 extent = font->CalcTextSizeA(size, FLT_MAX, 0.0f, icon);
531 DrawShadowedText(draw, font, size,
532 {middle.x - extent.x / 2.0f, middle.y - extent.y / 2.0f},
533 Faded(color, alpha), icon);
534}
535
536void DrawDragged(const char* icon, ImU32 color, float size, ImVec2 grab) {
537 ImDrawList* draw = ImGui::GetForegroundDrawList();
538 const ImVec2 mouse = ImGui::GetMousePos();
539 const ImVec2 min{mouse.x - grab.x, mouse.y - grab.y};
540 const ImVec2 max{min.x + size, min.y + size};
541 // Lifted off the screen: a deeper shadow, and a gold edge.
542 draw->AddRectFilled({min.x + Ui(4.0f), min.y + Ui(6.0f)},
543 {max.x + Ui(4.0f), max.y + Ui(6.0f)},
544 Faded(IM_COL32_BLACK, 0.45f));
545 DrawSlot(draw, min, max, colors::kGold);
546 DrawSlotIcon(draw, min, max, icon, color);
547}
548
549namespace {
550
551// A number from 0 to 1 that looks random, the same for each seed.
552float Scatter(uint32_t seed) {
553 seed ^= seed >> 16;
554 seed *= 0x7feb352dU;
555 seed ^= seed >> 15;
556 seed *= 0x846ca68bU;
557 seed ^= seed >> 16;
558 return static_cast<float>(seed & 0xFFFFFF) / static_cast<float>(0xFFFFFF);
559}
560
561} // namespace
562
563void DrawBackdrop(ImDrawList* draw, ImVec2 size, float time) {
564 draw->AddRectFilledMultiColor({0.0f, 0.0f}, size, colors::kNight,
565 colors::kNight, IM_COL32(40, 8, 12, 255),
566 IM_COL32(40, 8, 12, 255));
567 // The fire's glow, from below the middle of the bottom edge.
568 const ImVec2 fire{size.x / 2.0f, size.y * 1.05f};
569 for (int ring = 10; ring >= 1; --ring) {
570 const float share = static_cast<float>(ring) / 10.0f;
571 draw->AddEllipseFilled(fire,
572 {size.x * 0.7f * share, size.y * 0.55f * share},
573 Faded(colors::kCrimson, 0.045f));
574 }
575 // Embers, each rising at its own pace, drifting, and flickering.
576 constexpr int kEmbers = 90;
577 for (int i = 0; i < kEmbers; ++i) {
578 const auto seed = static_cast<uint32_t>(i) * 4u;
579 const float speed = 0.03f + 0.07f * Scatter(seed);
580 const float rise = std::fmod(time * speed + Scatter(seed + 1), 1.0f);
581 const float x = size.x * Scatter(seed + 2) +
582 std::sin(time * 0.7f + static_cast<float>(i)) * Ui(14.0f);
583 const float y = size.y * (1.05f - 1.1f * rise);
584 const float radius = Ui(1.0f + 2.2f * Scatter(seed + 3));
585 // Brightest low down, gone by the top.
586 const float flicker =
587 0.6f + 0.4f * std::sin(time * 6.0f + static_cast<float>(i) * 1.7f);
588 const float alpha = (1.0f - rise) * flicker;
589 const ImU32 color =
590 Scatter(seed + 3) > 0.5f ? colors::kEmber : colors::kFlame;
591 draw->AddCircleFilled({x, y}, radius * 3.0f, Faded(color, 0.12f * alpha));
592 draw->AddCircleFilled({x, y}, radius, Faded(color, alpha));
593 }
594 // Darker at the edges, so that the eye goes to the middle.
595 const float edge = std::min(size.x, size.y) * 0.25f;
596 const ImU32 dark = Faded(IM_COL32_BLACK, 0.6f);
597 const ImU32 clear = Faded(IM_COL32_BLACK, 0.0f);
598 draw->AddRectFilledMultiColor({0.0f, 0.0f}, {edge, size.y}, dark, clear,
599 clear, dark);
600 draw->AddRectFilledMultiColor({size.x - edge, 0.0f}, size, clear, dark, dark,
601 clear);
602 draw->AddRectFilledMultiColor({0.0f, 0.0f}, {size.x, edge}, dark, dark, clear,
603 clear);
604}
605
606void DrawLogo(ImDrawList* draw, ImVec2 top_centre, float size) {
607 ImFont* font = GetFonts().title;
608 constexpr const char* kName = "nanoMMO";
609 const ImVec2 extent = font->CalcTextSizeA(size, FLT_MAX, 0.0f, kName);
610 const ImVec2 at{top_centre.x - extent.x / 2.0f, top_centre.y};
611 // A glow behind it, like the light of the embers on it.
612 const ImVec2 middle{top_centre.x, top_centre.y + extent.y * 0.55f};
613 for (int ring = 6; ring >= 1; --ring) {
614 const float share = static_cast<float>(ring) / 6.0f;
615 draw->AddEllipseFilled(middle,
616 {extent.x * 0.75f * share, extent.y * 0.7f * share},
617 Faded(colors::kEmber, 0.035f));
618 }
620 kName);
621 DrawDivider(draw,
622 {middle.x - extent.x * 0.6f, at.y + extent.y + size * 0.05f},
623 extent.x * 1.2f);
624}
625
626} // namespace nanommo
constexpr ImU32 kEmber
Definition theme.h:19
constexpr ImU32 kCrimson
Definition theme.h:17
constexpr ImU32 kRed
Definition theme.h:18
constexpr ImU32 kGoldDark
Definition theme.h:23
constexpr ImU32 kShadow
Definition theme.h:29
constexpr ImU32 kGoldLight
Definition theme.h:22
constexpr ImU32 kFlame
Definition theme.h:20
constexpr ImU32 kNight
Definition theme.h:14
constexpr ImU32 kMuted
Definition theme.h:27
constexpr ImU32 kWine
Definition theme.h:16
constexpr ImU32 kPaper
Definition theme.h:26
constexpr ImU32 kBrass
The trim round every panel: gold gone dark with age.
Definition theme.h:25
constexpr ImU32 kCoal
Definition theme.h:15
constexpr ImU32 kGold
Definition theme.h:21
Definition chat.cc:9
bool BeginPanel(const char *name, bool *open, ImGuiWindowFlags flags, float alpha)
Definition theme.cc:320
void DrawPanel(ImDrawList *draw, ImVec2 min, ImVec2 max, float alpha)
Definition theme.cc:223
void DrawLogo(ImDrawList *draw, ImVec2 top_centre, float size)
Definition theme.cc:606
void DrawBar(ImDrawList *draw, ImVec2 min, ImVec2 max, float share, ImU32 color, ImU32 dark, const char *text)
Definition theme.cc:452
constexpr float kTextSize
In points, at a scale of one.
Definition theme.h:56
void DrawDivider(ImDrawList *draw, ImVec2 left, float width)
Definition theme.cc:257
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
void DrawSlotIcon(ImDrawList *draw, ImVec2 min, ImVec2 max, const char *icon, ImU32 color, float alpha)
An icon in the middle of a slot, in its colour, on a glow of that colour.
Definition theme.cc:519
std::string FontFolder()
Where the client's fonts are, next to it.
Definition theme.cc:89
void LoadFonts(const std::string &folder)
Definition theme.cc:75
void DrawDragged(const char *icon, ImU32 color, float size, ImVec2 grab)
Definition theme.cc:536
void ApplyTheme(float at)
ImGui's style in our colours, with its sizes and its text at the scale.
Definition theme.cc:102
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
Definition theme.cc:59
float InterfaceScale(float height, float player)
Definition theme.cc:93
void DrawDiamond(ImDrawList *draw, ImVec2 centre, float radius, ImU32 fill, ImU32 outline)
A diamond, filled, with an outline.
Definition theme.cc:213
void DrawBackdrop(ImDrawList *draw, ImVec2 size, float time)
Definition theme.cc:563
void DrawTitle(ImDrawList *draw, ImVec2 top_centre, float size, const char *text)
A title in carved gold capitals, centred on the point at its top.
Definition theme.cc:299
bool GameButton(const char *label, ImVec2 size, bool primary, bool enabled)
Definition theme.cc:381
void DrawGradientText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 top, ImU32 bottom, const char *text)
Definition theme.cc:283
void DrawShadowedText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 color, const char *text)
Text with a shadow under it.
Definition theme.cc:272
void DrawSlot(ImDrawList *draw, ImVec2 min, ImVec2 max, ImU32 edge)
Definition theme.cc:480
float UiScale()
Definition theme.cc:100
void PathPanel(ImDrawList *draw, ImVec2 min, ImVec2 max, float cut)
A panel's shape: a rectangle with its corners cut off.
Definition theme.cc:202
const Fonts & GetFonts()
Definition theme.cc:66
std::string Shown(const char *label)
The part of an ImGui label that's shown: what's before any "##".
Definition theme.cc:308
ImFont * text
Definition theme.h:42
ImFont * bold
Definition theme.h:43
ImFont * title
Definition theme.h:44