// SPDX-License-Identifier: GPL-3.0-only #pragma once #include #include #include namespace bmh::modern_ui { // QA command driver only. Layout offsets omit CSS transforms; Project applies // the inverse accumulated transform used by RmlUI's own pointer hit testing. inline std::optional script_click_point(Rml::Context &context, Rml::Element &node) { if (!node.IsVisible(true)) return {}; const auto size = node.GetBox().GetSize(Rml::BoxArea::Border); if (size.x <= 0 || size.y <= 0) return {}; const auto target = node.GetAbsoluteOffset(Rml::BoxArea::Border) + size * .5f; auto point = target; const auto finite = [](Rml::Vector2f p) { return std::isfinite(p.x) && std::isfinite(p.y); }; bool solved = false; // Solve the public inverse projection for the desired layout-space center. // Affine transforms converge in one correction; perspective is bounded too. for (int attempt = 0; attempt < 8; ++attempt) { auto projected = point; if (!finite(point) || !node.Project(projected) || !finite(projected)) return {}; const auto error = target - projected; if (std::abs(error.x) <= .02f && std::abs(error.y) <= .02f) { solved = true; break; } auto x = point + Rml::Vector2f{1, 0}; auto y = point + Rml::Vector2f{0, 1}; if (!node.Project(x) || !node.Project(y) || !finite(x) || !finite(y)) return {}; x -= projected; y -= projected; const double det = double(x.x) * y.y - double(y.x) * x.y; if (!std::isfinite(det) || std::abs(det) < 1e-8) return {}; point += Rml::Vector2f{ float((double(error.x) * y.y - double(error.y) * y.x) / det), float((double(x.x) * error.y - double(x.y) * error.x) / det)}; } const auto bounds = context.GetDimensions(); if (!solved || !finite(point) || point.x < 0 || point.y < 0 || point.x >= bounds.x || point.y >= bounds.y) return {}; const Rml::Vector2i pixel{int(std::lround(point.x)), int(std::lround(point.y))}; if (pixel.x < 0 || pixel.y < 0 || pixel.x >= bounds.x || pixel.y >= bounds.y) return {}; auto *hit = context.GetElementAtPoint(Rml::Vector2f{float(pixel.x), float(pixel.y)}); if (!hit || !node.Contains(hit)) return {}; // Never silently click an overlay or another control. return pixel; } } // namespace bmh::modern_ui