// UI framework smoke test: the extracted bitmap font loads with sane metrics and proportional widths, // and a dialog renders its Win95 chrome (face colour + text) into a framebuffer. Skips if the font // assets aren't extracted yet (run extract_assets.py / extract_font.py). #include "sc2k/render/asset_loader.hpp" #include "sc2k/sim/bonds.hpp" #include "sc2k/ui/dialog.hpp" #include "sc2k/ui/font.hpp" #include "sc2k/ui/menu.hpp" #include "sc2k/ui/menu_defs.hpp" #include "sc2k/ui/query.hpp" #include "sc2k/ui/toolbar.hpp" #include "sc2k/ui/terrain_recipe.hpp" #include "sc2k/ui/tooltip.hpp" #include #include #include #include #include "test_harness.hpp" #include "test_paths.hpp" int main() { using namespace sc2k; const std::string adir = render::assets_dir(); if (!exists(adir + "/fonts/NotoSans-Regular.ttf")) { std::printf("ui: SKIP (no font under %s/fonts)\n", adir.c_str()); return 0; } i18n::load_locale(adir + "/lang", "en"); // captions are catalog keys; assert the English text CHECK(i18n::tr("ord.group.safety") == "Safety & Health"); ui::Font font(adir + "/fonts/NotoSans-Regular.ttf"); CHECK(font.height() > 4 && font.height() < 64); CHECK(font.avg_width() > 2); CHECK(font.text_width("OK") > 0); CHECK(font.text_width("WWW") > font.text_width("iii")); // proportional font // Icon hints can anchor to any side and align along that edge; viewport clamping keeps them visible. { const ui::TooltipRect control{100, 80, 40, 30}; auto p = ui::tooltip_position(control, 60, 20, 300, 200, ui::TooltipAnchor::Left, ui::TooltipAlign::Center); CHECK(p.x == 33 && p.y == 85); p = ui::tooltip_position(control, 60, 20, 300, 200, ui::TooltipAnchor::Right, ui::TooltipAlign::End); CHECK(p.x == 147 && p.y == 90); p = ui::tooltip_position(control, 60, 20, 300, 200, ui::TooltipAnchor::Top, ui::TooltipAlign::Start); CHECK(p.x == 100 && p.y == 53); p = ui::tooltip_position({2, 2, 10, 10}, 60, 20, 300, 200, ui::TooltipAnchor::Left, ui::TooltipAlign::Center); CHECK(p.x == 4 && p.y == 4); } // New-city setup is deliberately one-way: the only exit command is the explicit Done button. { std::ifstream input(adir + "/rml/new_city.rml"); const std::string rml((std::istreambuf_iterator(input)), std::istreambuf_iterator()); std::ifstream styleInput(adir + "/rml/forms.rcss"); const std::string style((std::istreambuf_iterator(styleInput)), std::istreambuf_iterator()); CHECK(rml.find("data-cmd=\"1\"") != std::string::npos); CHECK(rml.find("data-cmd=\"-1\"") == std::string::npos); CHECK(rml.find("common.cancel") == std::string::npos); CHECK(rml.find("href=\"chrome.rcss\"") != std::string::npos); CHECK(rml.find("class=\"choice-row chrome-tabs\"") != std::string::npos); CHECK(rml.find("class=\"choice classic-choice chrome-white\"") != std::string::npos); CHECK(rml.find("class=\"choice-label\"") != std::string::npos); CHECK(rml.find("class=\"choice-detail\"") != std::string::npos); CHECK(rml.find("class=\"chrome-green\" data-cmd=\"1\"") != std::string::npos); CHECK(style.find("form > label.field-label:first-child") != std::string::npos); CHECK(style.find("justify-content: center", style.find("button.choice")) != std::string::npos); CHECK(style.find("height: 68dp", style.find("button.classic-choice")) != std::string::npos); for (int command = 112; command <= 117; ++command) CHECK(rml.find("data-cmd=\"" + std::to_string(command) + "\"") != std::string::npos); CHECK(i18n::tr("dialog.newcity.terrain") == "Terrain type"); CHECK(i18n::tr("dialog.newcity.default_name") == "New City"); CHECK(i18n::tr("terrain.classic") == "Classic"); CHECK(i18n::tr("terrain.grassland") == "Grassland"); } // Persistent game chrome shares one material sheet; component RCSS files own layout only. { CHECK(exists(adir + "/rml/chrome.rcss")); for (const char* document : {"startup", "toolbox", "hud", "terrain_editor"}) { std::ifstream input(adir + "/rml/" + document + ".rml"); const std::string rml((std::istreambuf_iterator(input)), std::istreambuf_iterator()); CHECK(rml.find("href=\"chrome.rcss\"") != std::string::npos); } } // The terrain recipe control uses one geometric oracle for its isometric centre and four rails. // World directions advance clockwise through the visible rails as the map rotates. { using ui::TerrainRecipeEdge; using ui::TerrainRecipeRegion; CHECK(ui::terrain_recipe_region(40, 30, 200, 112) == TerrainRecipeRegion::UpperLeft); CHECK(ui::terrain_recipe_region(160, 30, 200, 112) == TerrainRecipeRegion::UpperRight); CHECK(ui::terrain_recipe_region(160, 70, 200, 112) == TerrainRecipeRegion::LowerRight); CHECK(ui::terrain_recipe_region(40, 70, 200, 112) == TerrainRecipeRegion::LowerLeft); CHECK(ui::terrain_recipe_region(100, 50, 200, 112) == TerrainRecipeRegion::Centre); CHECK(ui::terrain_recipe_region(100, 110, 200, 112) == TerrainRecipeRegion::None); CHECK(ui::terrain_recipe_region(-1, 10, 200, 200) == TerrainRecipeRegion::None); const auto upperRight = ui::terrain_recipe_polygon(TerrainRecipeRegion::UpperRight, 200, 112); CHECK(upperRight[0].x == 100 && upperRight[0].y == 0); CHECK(upperRight[1].x == 200 && upperRight[1].y == 50); const ui::TerrainRecipeBounds overlay = ui::terrain_recipe_centre_overlay(200, 112); CHECK(overlay.x == 50 && overlay.y == 25); CHECK(overlay.width == 100 && overlay.height == 50); const ui::TerrainRecipeBounds smallOverlay = ui::terrain_recipe_scaled_overlay(200, 112, .5f); CHECK(smallOverlay.x == 75 && smallOverlay.y == 37.5f); CHECK(smallOverlay.width == 50 && smallOverlay.height == 25); const ui::TerrainRecipeBounds landArtwork = ui::terrain_recipe_artwork_bounds(ui::TerrainRecipeArtwork::Land, 200, 112); CHECK(landArtwork.x == 0 && landArtwork.y == 0); CHECK(landArtwork.width == 200 && landArtwork.height == 100); const ui::TerrainRecipeBounds lakeArtwork = ui::terrain_recipe_artwork_bounds(ui::TerrainRecipeArtwork::Lake, 200, 112); CHECK(lakeArtwork.x == 80 && lakeArtwork.y == 37.5f); CHECK(lakeArtwork.width == 40 && lakeArtwork.height == 25); const ui::TerrainRecipeBounds mountainArtwork = ui::terrain_recipe_artwork_bounds(ui::TerrainRecipeArtwork::Mountain, 200, 112); CHECK(mountainArtwork.x == 75 && mountainArtwork.y == 37.5f); CHECK(mountainArtwork.width == 50 && mountainArtwork.height == 25); const ui::TerrainRecipeBounds riverArtwork = ui::terrain_recipe_artwork_bounds(ui::TerrainRecipeArtwork::RiverNS, 200, 112); CHECK(riverArtwork.x == 50 && riverArtwork.y == 25); CHECK(riverArtwork.width == 100 && riverArtwork.height == 50); struct ArtworkAsset { const char* name; int width; int height; }; constexpr ArtworkAsset artworkAssets[] = { {"land.png", 196, 100}, {"lake.png", 40, 25}, {"mountain.png", 49, 25}, {"river_ns.png", 98, 50}, {"river_ew.png", 98, 50}, }; for (const ArtworkAsset& asset : artworkAssets) { const render::Sprite sprite = render::load_sprite_png(adir + "/rml/terrain_recipe/" + asset.name); CHECK(sprite.width == asset.width && sprite.height == asset.height); } const render::Sprite northSouthRiver = render::load_sprite_png(adir + "/rml/terrain_recipe/river_ns.png"); int matchingWaterPixels = 0; for (const render::Rgba pixel : northSouthRiver.rgba) if (pixel.a == 255 && pixel.r == ui::kTerrainRecipeWaterRgb[0] && pixel.g == ui::kTerrainRecipeWaterRgb[1] && pixel.b == ui::kTerrainRecipeWaterRgb[2]) ++matchingWaterPixels; CHECK(matchingWaterPixels == 121); for (int rotation = 0; rotation < 4; ++rotation) { for (int edge = 0; edge < 4; ++edge) { const auto world = static_cast(edge); const TerrainRecipeRegion visible = ui::terrain_recipe_region(world, rotation); CHECK(ui::terrain_recipe_edge(visible, rotation) == world); } } CHECK(ui::terrain_recipe_edge(TerrainRecipeRegion::UpperLeft, 0) == TerrainRecipeEdge::West); CHECK(ui::terrain_recipe_edge(TerrainRecipeRegion::UpperRight, 0) == TerrainRecipeEdge::North); CHECK(ui::terrain_recipe_edge(TerrainRecipeRegion::UpperLeft, 1) == TerrainRecipeEdge::South); std::ifstream input(adir + "/rml/terrain_editor.rml"); const std::string editor((std::istreambuf_iterator(input)), std::istreambuf_iterator()); std::ifstream styleInput(adir + "/rml/terrain_editor.rcss"); const std::string editorStyle((std::istreambuf_iterator(styleInput)), std::istreambuf_iterator()); CHECK(editor.find("(input)), std::istreambuf_iterator()); std::ifstream styleInput(adir + "/rml/catalog_selector.rcss"); const std::string style((std::istreambuf_iterator(styleInput)), std::istreambuf_iterator()); std::ifstream chromeInput(adir + "/rml/chrome.rcss"); const std::string chrome((std::istreambuf_iterator(chromeInput)), std::istreambuf_iterator()); CHECK(catalog.find("href=\"chrome.rcss\"") != std::string::npos); CHECK(catalog.find("id=\"catalog-confirm\" class=\"chrome-green\"") != std::string::npos); const std::size_t previewStyle = style.find("div.catalog-preview {"); const std::size_t listStyle = style.find("div.catalog-list {"); const std::size_t detailStyle = style.find("div.catalog-detail {"); const std::size_t factsStyle = style.find("div.catalog-facts {"); CHECK(style.find("border-radius: 8dp", previewStyle) < style.find('}', previewStyle)); CHECK(style.find("border-radius: 8dp", listStyle) < style.find('}', listStyle)); CHECK(style.find("border-radius: 8dp", detailStyle) < style.find('}', detailStyle)); CHECK(style.find("border-top: 4dp", detailStyle) > style.find('}', detailStyle)); CHECK(style.find("justify-content: flex-start", factsStyle) < style.find('}', factsStyle)); const std::size_t disabledGreenStyle = chrome.find("button.chrome-green.disabled,"); CHECK(disabledGreenStyle != std::string::npos); CHECK(chrome.find("decorator: none", disabledGreenStyle) < chrome.find('}', disabledGreenStyle)); CHECK(chrome.find("background-color: #d8dee2", disabledGreenStyle) < chrome.find('}', disabledGreenStyle)); CHECK(chrome.find("button.chrome-red:active,\nbutton.chrome-red.selected") != std::string::npos); CHECK(chrome.find("div.side-menu button.chrome-red:active,\ndiv.side-menu button.chrome-red.selected") != std::string::npos); CHECK(i18n::tr("plant.coal.description") != i18n::tr("plant.wind.description")); CHECK(i18n::tr("catalog.water.pump.description") != i18n::tr("catalog.water.tower.description")); CHECK(i18n::tr("tool.buildings.mayor.description") != i18n::tr("tool.buildings.statue.description")); CHECK(i18n::tr("arco.plymouth.description") != i18n::tr("arco.launch.description")); } // Select is now the implicit fallback mode. Sign and Query live at the far-right edge of the HUD, // while the debug launcher is an independent floating control rather than toolbox content. { std::ifstream toolboxInput(adir + "/rml/toolbox.rml"); const std::string toolbox((std::istreambuf_iterator(toolboxInput)), std::istreambuf_iterator()); std::ifstream toolboxStyleInput(adir + "/rml/toolbox.rcss"); const std::string toolboxStyle((std::istreambuf_iterator(toolboxStyleInput)), std::istreambuf_iterator()); std::ifstream hudInput(adir + "/rml/hud.rml"); const std::string hud((std::istreambuf_iterator(hudInput)), std::istreambuf_iterator()); CHECK(toolbox.find("data-cmd=\"41015\"") == std::string::npos); CHECK(hud.find("id=\"tool-sign\"") != std::string::npos); CHECK(hud.find("id=\"tool-query\"") != std::string::npos); CHECK(hud.find("icons/map-pin.svg") != std::string::npos); CHECK(hud.find("icons/question.svg") != std::string::npos); CHECK(hud.find("id=\"tool-query\" class=\"mode-tool chrome-green\"") != std::string::npos); CHECK(toolbox.find("id=\"debug-widget\"") > toolbox.find("")); for (int group = 0; group < 8; ++group) CHECK(toolbox.find("id=\"tool-group-wrap-" + std::to_string(group) + "\" class=\"department-item\"") != std::string::npos); CHECK(toolbox.find("flyout-join-top") != std::string::npos); CHECK(toolbox.find("flyout-join-bottom") != std::string::npos); CHECK(toolbox.find("side-flyout-join-top") == std::string::npos); CHECK(toolbox.find("side-flyout-join-bottom") != std::string::npos); CHECK(toolbox.find("id=\"ui-tooltip\"") != std::string::npos); CHECK(toolbox.find("data-tooltip-anchor=\"left\"") != std::string::npos); CHECK(toolbox.find("data-tooltip=\"sidebar.landscape\"") != std::string::npos); for (const char* icon : {"tree.svg", "water.svg", "terrain_down.svg", "terrain_up.svg", "terrain_flatten.svg"}) CHECK(toolbox.find(std::string("icons/") + icon) != std::string::npos); std::size_t bulldozerCount = 0; for (std::size_t at = 0; (at = toolbox.find("icons/bulldozer.svg", at)) != std::string::npos; at += 21) ++bulldozerCount; CHECK(bulldozerCount == 5); CHECK(toolbox.find("data-cmd=\"32836\" data-tooltip=\"sidebar.plant_trees\"") != std::string::npos); CHECK(toolbox.find("data-cmd=\"32837\" data-tooltip=\"sidebar.surface_water\"") != std::string::npos); CHECK(toolbox.find("data-cmd=\"32834\" data-tooltip=\"tool.bulldoze.lower\"") != std::string::npos); CHECK(toolbox.find("data-cmd=\"32833\" data-tooltip=\"tool.bulldoze.raise\"") != std::string::npos); CHECK(toolbox.find("data-cmd=\"32832\" data-tooltip=\"tool.bulldoze.level\"") != std::string::npos); CHECK(toolbox.find("[[sidebar.plant_trees]]") == std::string::npos); CHECK(toolbox.find("[[sidebar.surface_water]]") == std::string::npos); CHECK(toolbox.find("[[tool.bulldoze.demolish]]") == std::string::npos); CHECK(toolboxStyle.find("button.icon-tool-button img.tool-icon") != std::string::npos); for (int submenu : {0, 1, 2, 10, 11, 12, 13, 20, 21, 22, 23}) CHECK(toolbox.find("id=\"side-sub-wrap-" + std::to_string(submenu) + "\" class=\"side-menu-item\"") != std::string::npos); CHECK(toolbox.find("