#include "ui/overlay/overlay.h" #include "engine/data.h" #include "engine/gl.h" #include "engine/platform.h" #include "game/manager.h" #include "game/mod_manager.h" #include "stb_image.h" #include "ui/menu/manager.h" #include #include constexpr float MOD_THUMBNAIL = 144; // A card's smallest thumbnail side, in Unifont pixels. constexpr int MOD_BLURB_LINES = 4; // The description's room on a card. Longer ones scroll. constexpr float MOD_TOOLTIP_COLUMNS = 32; // The tooltip's wrap width, in font sizes. constexpr const char* MODS_FOLDER = "mods/"; constexpr const char* THUMBNAIL_FILE = "/thumb.png"; constexpr const char* NO_THUMBNAIL = "No Thumbnail"; constexpr const char* RELOAD = "Reload"; constexpr const char* VIEWS[] = {"Thumbnail", "Compact"}; // The mod's thumb.png as a texture, loaded once. 0 when it has none or it will not load. unsigned int Overlay::thumbnail_get(ModEntry& mod) { std::string path = resource_path(MODS_FOLDER + mod.directory + THUMBNAIL_FILE); auto found = thumbnails.find(path); if (found != thumbnails.end()) return found->second; unsigned int texture = 0; int bound = 0; int height = 0; int width = 0; if (uint8_t* pixels = KAGE::Graphics::texels_load(path, width, height)) { gl.GetIntegerv(GL_TEXTURE_BINDING_2D, &bound); gl.GenTextures(1, &texture); gl.BindTexture(GL_TEXTURE_2D, texture); gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); gl.BindTexture(GL_TEXTURE_2D, bound); stbi_image_free(pixels); } return thumbnails[path] = texture; } // Text cut to width with an ellipsis. static std::string text_fit(std::string text, float width) { constexpr const char* ELLIPSIS = "..."; if (ImGui::CalcTextSize(text.c_str()).x <= width) return text; while (!text.empty() && ImGui::CalcTextSize((text + ELLIPSIS).c_str()).x > width) text.pop_back(); return text + ELLIPSIS; } // Turns a mod on or off as the Mods menu does, through its disable.it, for the next Reload. static void mod_toggle(ModEntry& mod, bool& isModsChanged) { mod.isEnabled = !mod.isEnabled; mod.disable_marker_save(); isModsChanged = true; } // The mod's whole metadata, shown while its card or line is hovered. static void mod_tooltip_render(ModEntry& mod) { if (!ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort) || !ImGui::BeginTooltip()) return; ImGui::PushTextWrapPos(MOD_TOOLTIP_COLUMNS * ImGui::GetFontSize()); ImGui::TextUnformatted(mod.name.c_str()); ImGui::TextDisabled("Version %s, folder %s", mod.metadataVersion.c_str(), mod.directory.c_str()); ImGui::Separator(); ImGui::TextDisabled("%s", mod.metadataDescription.c_str()); ImGui::PopTextWrapPos(); ImGui::EndTooltip(); } // The mod as a card width wide: its square thumbnail, name and version, scrolling description, and the checkbox that // turns it on. A mod that is off is greyed, and an outdated one says so. void Overlay::mod_render(ModEntry& mod, float width) { const ImGuiStyle& style = ImGui::GetStyle(); float side = width - style.WindowPadding.x * 2; float line = ImGui::GetTextLineHeightWithSpacing(); float height = side + line * (2 + MOD_BLURB_LINES) + ImGui::GetFrameHeight() + style.WindowPadding.y * 2 + style.ItemSpacing.y * 2; bool isGreyed = !mod.isEnabled; ImGui::PushID(mod.directory.c_str()); if (isGreyed) ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::GetStyleColorVec4(ImGuiCol_FrameBg)); ImGui::BeginChild("##mod", {width, height}, ImGuiChildFlags_Borders, ImGuiWindowFlags_NoScrollbar); ImGui::PushStyleVar(ImGuiStyleVar_Alpha, isGreyed ? style.DisabledAlpha : 1); if (unsigned int texture = thumbnail_get(mod)) ImGui::Image((ImTextureID)texture, {side, side}); else { ImVec2 corner = ImGui::GetCursorScreenPos(); float wrap = side - style.FramePadding.x * 2; ImVec2 label = ImGui::CalcTextSize(NO_THUMBNAIL, nullptr, false, wrap); ImDrawList* draw = ImGui::GetWindowDrawList(); draw->AddRectFilled(corner, {corner.x + side, corner.y + side}, ImGui::GetColorU32(ImGuiCol_Button)); draw->AddText(nullptr, 0, {corner.x + (side - label.x) / 2, corner.y + (side - label.y) / 2}, ImGui::GetColorU32(ImGuiCol_TextDisabled), NO_THUMBNAIL, nullptr, wrap); ImGui::Dummy({side, side}); } ImGui::TextUnformatted(mod.name.c_str()); ImGui::TextDisabled("%s%s", mod.metadataVersion.c_str(), mod.isOutdated ? " [Old]" : ""); ImGui::BeginChild("##blurb", {side, line * MOD_BLURB_LINES}, ImGuiChildFlags_None, ImGuiWindowFlags_NoScrollbar); ImGui::PushTextWrapPos(0); ImGui::TextDisabled("%s", mod.metadataDescription.c_str()); ImGui::PopTextWrapPos(); ImGui::EndChild(); ImGui::PopStyleVar(); bool isOn = mod.isEnabled; if (ImGui::Checkbox("On", &isOn)) mod_toggle(mod, isModsChanged); ImGui::EndChild(); mod_tooltip_render(mod); if (isGreyed) ImGui::PopStyleColor(); ImGui::PopID(); } // The mods ModManager found, as cards or a line each, then a strip with the switch for every mod (the Mods menu's tab), // the view, and Reload. Reload does what leaving the Mods menu after a change does, so it waits for the menus. void Overlay::mods_render() { const ImGuiStyle& style = ImGui::GetStyle(); std::vector>& mods = g_manager->modManager.mods; bool& isModsEnabled = g_manager->optionsConfig.isModsEnabled; float stripHeight = ImGui::GetFrameHeight() + style.WindowPadding.y * 2 + style.ItemSpacing.y; ImGui::BeginChild("##mods", {0, -stripHeight}); if (mods.empty()) ImGui::TextDisabled("**No mods in %s", platform.modsDirectory.c_str()); ImGui::BeginDisabled(!isModsEnabled); if (modsView == MODS_VIEW_COMPACT) for (std::unique_ptr& mod : mods) { bool isOn = mod->isEnabled; ImGui::PushID(mod->directory.c_str()); if (ImGui::Checkbox("##on", &isOn)) mod_toggle(*mod, isModsChanged); ImGui::SameLine(); ImGui::TextUnformatted(mod->name.c_str()); mod_tooltip_render(*mod); ImGui::SameLine(); ImGui::TextDisabled("%s%s", mod->metadataVersion.c_str(), mod->isOutdated ? " [Old]" : ""); ImGui::PopID(); } else { // As many cards to a row as fit at their smallest, stretched together to fill the row. float space = ImGui::GetContentRegionAvail().x; float gap = style.ItemSpacing.x; float smallest = MOD_THUMBNAIL * scale + style.WindowPadding.x * 2; int columns = std::max(1, (int)((space + gap) / (smallest + gap))); float width = std::max(smallest, (space - gap * (float)(columns - 1)) / (float)columns); for (size_t index = 0; index < mods.size(); index++) { if (index % columns != 0) ImGui::SameLine(); mod_render(*mods[index], width); } } ImGui::EndDisabled(); ImGui::EndChild(); ImGui::BeginChild("##strip", {0, 0}, ImGuiChildFlags_Borders, ImGuiWindowFlags_NoScrollbar); if (ImGui::Checkbox("Mods on", &isModsEnabled)) { isModsChanged = true; g_manager->optionsConfig.Save(); } for (int view = 0; view < (int)std::size(VIEWS); view++) { ImGui::SameLine(); ImGui::RadioButton(VIEWS[view], (int*)&modsView, view); } bool isMenu = g_menuManager && g_manager->state == MANAGER_STATE_MENU; float reloadLeft = ImGui::GetContentRegionMax().x - ImGui::CalcTextSize(RELOAD).x - style.FramePadding.x * 2; ImGui::SameLine(); ImGui::AlignTextToFramePadding(); std::string hint = isMenu ? "**Changes apply on Reload" : "**Reload from the main menu"; ImGui::TextDisabled("%s", text_fit(hint, reloadLeft - ImGui::GetCursorPosX() - style.ItemSpacing.x).c_str()); ImGui::SameLine(reloadLeft); ImGui::BeginDisabled(!isModsChanged || !isMenu); if (ImGui::Button(RELOAD)) { g_manager->modManager.Reset(); if (g_manager->currentSaveSlot != 0) g_manager->LoadGameState(g_manager->currentSaveSlot); isModsChanged = false; } ImGui::EndDisabled(); ImGui::EndChild(); }