diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-02-26 20:17:01 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-02-27 10:08:44 -0500 |
| commit | 7cb70ada4f1b2b877ad3f7428aeee4ad58ec0890 (patch) | |
| tree | 121225694a53e321094af8a2414bc99785b61ece /editor | |
| parent | 4cd201da9cd2b26dc39b064e81f17b92e796e532 (diff) | |
pke: generate mtsdf for individual svgs
Diffstat (limited to 'editor')
| -rw-r--r-- | editor/editor.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/editor/editor.cpp b/editor/editor.cpp index 28798d5..ee35d27 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -14,9 +14,11 @@ #include "level.hpp" #include "math-helpers.hpp" #include "msdf-atlas-gen/msdf-atlas-gen.h" +#include "msdfgen.h" #include "player-input.hpp" #include "plugins.hpp" #include "project.hpp" +#include "static-ui.hpp" #include "thread-pool.hpp" #include "vendor-glm-include.hpp" #include "vendor-tinyfiledialogs.h" @@ -884,6 +886,28 @@ void GenerateMTSDF(FontTypeMSDFSettings *msdf_settings, const Asset *a) { FontType_RegisterFont(font_title, ah_image, ah_glyphs, msdf_settings, &ft_spacing); } +void GenerateMTSDFGlyph(MSDFGlyphSettings *settings, const char *name) { + assert(settings != nullptr); + NULL_CHAR_ARR(src_path, 128); + NULL_CHAR_ARR(dst_path, 128); + std::error_code e; + std::filesystem::create_directory("./glyphs", e); + sprintf(src_path, "./assets/glyphs/%s.svg", name); + sprintf(dst_path, "./glyphs/%s.png", name); + msdfgen::Shape shape; + if (!msdfgen::loadSvgShape(shape, src_path)) { + return; + } + shape.normalize(); + msdfgen::edgeColoringSimple(shape, 3.0); + msdfgen::Bitmap<float, 4> mtsdf(settings->width, settings->height); + msdfgen::Vector2 scale(settings->scale); + msdfgen::Vector2 em_translation(settings->translate_em, settings->translate_em); + msdfgen::SDFTransformation t(msdfgen::Projection(scale, em_translation), msdfgen::Range(settings->range_em)); + msdfgen::generateMTSDF(mtsdf, shape, t); + msdfgen::savePng(mtsdf, dst_path); +} + bool RecordImGui_GenerateMTSDFModal() { bool ret_value = false; static struct FontTypeMSDFSettings msdf_settings{}; @@ -921,6 +945,54 @@ bool RecordImGui_GenerateMTSDFModal() { return ret_value; } +bool RecordImGui_GenerateMTSDFGlyphModal() { + bool ret_value = false; + static std::regex reg_svg_file(".+\\.svg$", std::regex_constants::icase); + static MSDFGlyphSettings msdf_glyph_settings = { + .width = 80, + .height = 80, + .scale = 4, + .translate_em = 2, + .range_em = 4, + }; + if (ImGui::BeginPopupModal("MTSDFGlyphModal", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { + + ImGui::InputFloat("width", &msdf_glyph_settings.width); + ImGui::InputFloat("height", &msdf_glyph_settings.height); + ImGui::InputFloat("scale", &msdf_glyph_settings.scale); + ImGui::InputFloat("translate_em", &msdf_glyph_settings.translate_em); + ImGui::InputFloat("range_em", &msdf_glyph_settings.range_em); + + ImGui::Separator(); + + ImGui::Text("%s", "Glyphs in asset dir:"); + std::filesystem::directory_iterator di{"assets/glyphs"}; + for (const std::filesystem::directory_entry &sde : di) { + if (sde.is_regular_file() && std::regex_search(sde.path().c_str(), reg_svg_file)) { + ImGui::Text("%s", sde.path().filename().c_str()); + } + } + + ImGui::Separator(); + + if (ImGui::Button("Generate")) { + di = std::filesystem::directory_iterator {"assets/glyphs"}; + for (const std::filesystem::directory_entry &sde : di) { + if (sde.is_regular_file() && std::regex_search(sde.path().c_str(), reg_svg_file)) { + GenerateMTSDFGlyph(&msdf_glyph_settings, sde.path().stem().c_str()); + } + } + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button("Cancel")) { + ImGui::CloseCurrentPopup(); + } + ImGui::EndPopup(); + } + return ret_value; +} + void RecordImGuiAssets() { if (!ImGui::Begin("AssetList")) { ImGui::End(); @@ -1570,6 +1642,9 @@ void RecordImGuiSceneEditor() { if (ImGui::Button("Generate MTSDF")) { ImGui::OpenPopup("MTSDFModal"); } + if (ImGui::Button("Generate UI MTSDFs")) { + ImGui::OpenPopup("MTSDFGlyphModal"); + } if (ImGui::Button("Clear Selection")) { selectedEntity = nullptr; } @@ -1624,6 +1699,7 @@ void RecordImGuiSceneEditor() { RecordImGuiModalCreateAsset(); RecordImGuiModalCreateEntityType(); RecordImGui_GenerateMTSDFModal(); + RecordImGui_GenerateMTSDFGlyphModal(); ImGui::End(); } |
