// SPDX-License-Identifier: GPL-3.0-only // Exercise the production control builder and stylesheet with synthetic labels. #include "modern/ui/controls.hpp" #include "modern/ui/script_locator.hpp" #include #include #include #include #include #include #include #include using namespace bmh::modern_ui; namespace { void require(bool value, const char *message) { if (!value) throw std::runtime_error(message); } struct Renderer final : Rml::RenderInterface { Rml::CompiledGeometryHandle CompileGeometry(Rml::Span, Rml::Span) override { return 1; } void RenderGeometry(Rml::CompiledGeometryHandle, Rml::Vector2f, Rml::TextureHandle) override {} void ReleaseGeometry(Rml::CompiledGeometryHandle) override {} Rml::TextureHandle LoadTexture(Rml::Vector2i &, const Rml::String &) override { return 0; } Rml::TextureHandle GenerateTexture(Rml::Span, Rml::Vector2i) override { return 1; } void ReleaseTexture(Rml::TextureHandle) override {} void EnableScissorRegion(bool) override {} void SetScissorRegion(Rml::Rectanglei) override {} }; struct System final : Rml::SystemInterface { size_t stylesheet_errors = 0; bool LogMessage(Rml::Log::Type type, const Rml::String &message) override { if ((type == Rml::Log::LT_WARNING || type == Rml::Log::LT_ERROR) && (message.find("property") != message.npos || message.find("style") != message.npos || message.find("Syntax error") != message.npos)) { ++stylesheet_errors; std::cerr << message << '\n'; } return true; } }; void text_centers(Rml::Element &node, float cx, float cy, bool horizontal, size_t &lines) { if (auto *text = dynamic_cast(&node)) { const auto origin = text->GetAbsoluteOffset(); const auto metrics = Rml::GetFontEngineInterface()->GetFontMetrics( text->GetFontFaceHandle()); for (const auto &line : text->GetLines()) { if (line.text.empty()) continue; ++lines; const float x = origin.x + line.position.x + line.width / 2.f; const float y = origin.y + line.position.y - metrics.ascent + (metrics.ascent + metrics.descent) / 2.f; require(std::abs(y - cy) <= .8f, "Control text is not vertically centered"); if (horizontal) require(std::abs(x - cx) <= .8f, "Control text is not horizontally centered"); } } for (int i = 0; i < node.GetNumChildren(); ++i) text_centers(*node.GetChild(i), cx, cy, horizontal, lines); } void centered_text(Rml::Element &text_root, Rml::Element &control, bool horizontal = true) { const auto p = control.GetAbsoluteOffset(Rml::BoxArea::Border), s = control.GetBox().GetSize(Rml::BoxArea::Border); size_t lines = 0; text_centers(text_root, p.x + s.x / 2, p.y + s.y / 2, horizontal, lines); require(lines == 1, "A control label disappeared or wrapped"); } size_t visible_text_lines(Rml::Element &node) { size_t count = 0; if (auto *text = dynamic_cast(&node)) for (const auto &line : text->GetLines()) if (!line.text.empty() && line.width > 0) ++count; for (int i = 0; i < node.GetNumChildren(); ++i) count += visible_text_lines(*node.GetChild(i)); return count; } struct ClickListener final : Rml::EventListener { int clicks = 0; void ProcessEvent(Rml::Event &event) override { if (event.GetType() == "click") ++clicks; } }; void script_locator_cases(Rml::Context &context, float density) { struct Case { const char *transform; Rml::Vector2f expected; bool blocked; }; for (const auto &test : {Case{"none", {700, 730}, false}, Case{"translate(70dp,-160dp)", {770, 570}, false}, Case{"scale(1.5)", {800, 795}, false}, Case{"rotate(90deg)", {370, 800}, false}, Case{"scale(0)", {}, true}, Case{"none", {}, true}}) { const bool covered = test.blocked && std::string(test.transform) == "none"; auto *doc = context.LoadDocumentFromMemory( "
" + (covered ? std::string("
") : std::string()) + "
"); require(doc, "Script locator document missing"); doc->Show(); context.Update(); context.Render(); auto *target = doc->GetElementById("target"); const auto point = script_click_point(context, *target); if (test.blocked) require(!point, "Singular/covered locator silently clicks another element"); else { require(bool(point), "Transformed script locator rejected visible control"); require(std::abs(point->x - test.expected.x * density) <= 1.f && std::abs(point->y - test.expected.y * density) <= 1.f, "Script locator did not map the visible transformed center"); ClickListener listener; target->AddEventListener("click", &listener); context.ProcessMouseMove(point->x, point->y, 0); context.ProcessMouseButtonDown(0, 0); context.ProcessMouseButtonUp(0, 0); require(listener.clicks == 1, "Script locator point did not click requested control"); target->RemoveEventListener("click", &listener); } doc->Close(); context.Update(); } } } // namespace int main(int argc, char **argv) try { require(argc == 2, "Pass the Modern asset directory"); const auto root = std::filesystem::path(argv[1]); Renderer renderer; System system; Rml::SetRenderInterface(&renderer); Rml::SetSystemInterface(&system); require(Rml::Initialise(), "Rml initialization"); require(Rml::LoadFontFace((root / "LatoLatin-Regular.ttf").string(), "Modern", Rml::Style::FontStyle::Normal, Rml::Style::FontWeight::Normal), "Font missing"); require(Rml::LoadFontFace((root / "LatoLatin-Bold.ttf").string(), "Modern", Rml::Style::FontStyle::Normal, Rml::Style::FontWeight::Bold), "Bold font missing"); require(Rml::LoadFontFace((root / "LatoLatin-BoldItalic.ttf").string(), "Modern", Rml::Style::FontStyle::Italic, Rml::Style::FontWeight::Bold), "Bold italic font missing"); std::ostringstream css; for (const auto *name : {"theme.rcss", "ticket.rcss", "sports.rcss", "calendar.rcss", "reports.rcss", "report_details.rcss", "report_pickers.rcss", "indoor.rcss", "planning.rcss", "almanac.rcss", "finance.rcss", "bank.rcss", "contracts.rcss", "amount.rcss", "environment.rcss", "advertising.rcss", "toto.rcss", "lineup.rcss", "transfer.rcss", "career_reports.rcss", "rankings.rcss", "stadium.rcss", "statistics.rcss", "news.rcss", "settings.rcss", "widgets.rcss", "files.rcss", "front.rcss"}) { std::ifstream input(root / name); require(bool(input), "Production stylesheet missing"); css << input.rdbuf() << '\n'; } for (const auto *theme : {"midnight", "silver", "matchday"}) for (float density : {1.f / 3, .5f, 1.f, 2.f}) { auto *context = Rml::CreateContext( "controls", {int(3840 * density), int(2160 * density)}); require(context, "Context missing"); context->SetDensityIndependentPixelRatio(density); const auto select = [](const std::string &id) { return ""; }; std::string content = button_markup("decrease", "−", {}, "id='step' " "style='position:absolute;left:40dp;top:40dp;width:" "94dp;height:124dp;font-size:72dp;padding:0;' "); content += button_markup( "region", "Region", {}, "id='region' " "style='position:absolute;left:200dp;top:40dp;min-height:86dp;' "); content += button_markup("list", "Sample slot", "choice", "id='list' " "style='position:absolute;left:40dp;top:220dp;" "width:600dp;height:120dp;text-align:left;' "); content += button_markup( "rich", "
Image

Portrait

", "choice", "id='rich' " "style='position:absolute;left:700dp;top:40dp;width:250dp;height:" "260dp;' "); content += "
" + button_markup("statistics-absolute", "REL.", "statistics-action statistics-absolute", "style='left:0;top:0;width:100%;height:94dp;'") + "
" + "
" + button_markup("player-detail", "Synthetic player", "statistics-player-link") + "
" + "
" + button_markup("statistics-trophy", "WM 2098", "statistics-world-trophy") + "
" + "
" + button_markup("club-detail", "Synthetic source club", "report-club-button statistics-club name-first") + "
" + "kicker" + "
" + "
Sample winner
" + button_markup("statistics-curve", "Mannschaftsstärke", "statistics-action statistics-series-button", "style='left:2700dp;top:580dp;width:836dp;height:82dp;'"); content += "
" + select("natural") + "
" + select("toolbar") + "
" + select("manager-strength") + "
" + button_markup("continue", "Continue", {}, "id='footer'") + select("tall") + "
"; content += "
" "
" "
" "
"; content += "
" "
Juli 2032
" "" "
" "
Synthetic Club
"; content += ""; content += "
" + button_markup("load", "Spiel laden›", "front-menu-action", "id='front-test-button'") + "
" "
" "Synthetic worldOpenBMHModern
"; auto *doc = context->LoadDocumentFromMemory( "" + content + ""); require(doc, "Document missing"); require(system.stylesheet_errors == 0, "Rml rejected a production stylesheet declaration"); doc->Show(); context->Update(); context->Render(); for (int i = 0; i < 3; ++i) { if (center_selects(*doc)) context->Update(); context->Render(); } require(doc->GetElementById("gallery-scroll-test")->GetScrollHeight() > doc->GetElementById("gallery-scroll-test")->GetClientHeight()*2,"Gallery content does not contribute to scroll extent"); require(!center_selects(*doc), "Control centering does not settle"); for (const auto *id : {"stats-scale-label", "stats-legend-label", "stats-player-label", "stats-world-label", "stats-from-label", "stats-winner-label", "news-brand-test", "settings-row-label", "settings-row-help", "month-label", "warning-label", "fixture-label", "front-test-label", "mod-test-name"}) { auto *label = doc->GetElementById(id); require(label && label->GetBox().GetSize(Rml::BoxArea::Content).x > 0 && visible_text_lines(*label) > 0, "A production statistics/news label has collapsed or lost its font face"); } for (const auto *id : {"step", "region", "footer"}) { auto *button = doc->GetElementById(id); require(button, "Button missing"); centered_text(*button, *button); } centered_text(*doc->GetElementById("month-label"), *doc->GetElementById("month-box")); centered_text(*doc->GetElementById("front-test-label"),*doc->GetElementById("front-test-button"),false); auto *list = doc->GetElementById("list"); centered_text(*list, *list, false); auto *rich = doc->GetElementById("rich"); require(!rich->IsClassSet("control-button"), "Rich card became a horizontal text control"); require(rich->QuerySelector(".caption")->GetAbsoluteOffset().y > rich->QuerySelector(".picture")->GetAbsoluteOffset().y, "Rich card stacking changed"); for (const auto *id : {"natural", "toolbar", "tall", "manager-strength", "settings-control", "settings-jump-test"}) { auto *control = doc->GetElementById(id); auto *caption = select_part(*control, "selectvalue"), *arrow = select_part(*control, "selectarrow"); require(caption && arrow, "Select parts missing"); centered_text(*caption, *control, false); const auto y = control->GetAbsoluteOffset(Rml::BoxArea::Border).y + control->GetBox().GetSize(Rml::BoxArea::Border).y / 2; require(std::abs(arrow->GetAbsoluteOffset(Rml::BoxArea::Border).y + arrow->GetBox().GetSize(Rml::BoxArea::Border).y / 2 - y) < .8f, "Dropdown arrow is off center"); auto *select_control = dynamic_cast(control); select_control->ShowSelectBox(); context->Update(); context->Render(); auto *option = select_control->GetOption(0); require(option->GetComputedValues().font_size() == control->GetComputedValues().font_size(), "Dropdown popup font differs from its caption"); require(option->GetComputedValues().line_height().value < control->GetBox().GetSize(Rml::BoxArea::Content).y || std::string(id) == "natural", "Caption centering leaked into popup rows"); select_control->CancelSelectBox(); context->Update(); } doc->Close(); context->Update(); script_locator_cases(*context,density); Rml::RemoveContext("controls"); } Rml::Shutdown(); std::cout << "Control text, dropdown arrows, popup fonts and rich cards " "passed in 3 themes at 4 densities\n"; } catch (const std::exception &e) { std::cerr << e.what() << '\n'; return 1; }