use WorldCoordinates in ArenaView too

This commit is contained in:
2026-08-05 19:42:37 +02:00
parent 2af09d9eb1
commit fa9dbd62ad
7 changed files with 208 additions and 120 deletions

View File

@@ -10,7 +10,7 @@
// 40-tile-wide view, so every expectation below is a whole number.
static WorldCoordinates makeCoordinates(float viewCenterX_tiles)
{
return WorldCoordinates(QSize(800, 400), 20, viewCenterX_tiles);
return WorldCoordinates::scrolling(QSize(800, 400), 20, viewCenterX_tiles);
}
// ---------------------------------------------------------------------------
@@ -22,21 +22,34 @@ TEST_CASE("Tile size makes the world height fill the viewport height", "[coords]
// REQ-GW-TILE-SIZE: tiles are square and sized so the world height exactly
// fills the view's height.
REQUIRE(makeCoordinates(0.0f).getTilePx() == Approx(20.0f));
REQUIRE(WorldCoordinates(QSize(800, 600), 20, 0.0f).getTilePx() == Approx(30.0f));
REQUIRE(WorldCoordinates::scrolling(QSize(800, 600), 20, 0.0f).getTilePx()
== Approx(30.0f));
}
TEST_CASE("A degenerate world height falls back to a unit tile", "[coords]")
{
// Guards the division in every conversion; a zero or negative height would
// otherwise produce infinities.
REQUIRE(WorldCoordinates(QSize(800, 400), 0, 0.0f).getTilePx() == Approx(1.0f));
REQUIRE(WorldCoordinates(QSize(800, 400), -5, 0.0f).getTilePx() == Approx(1.0f));
REQUIRE(WorldCoordinates::scrolling(QSize(800, 400), 0, 0.0f).getTilePx()
== Approx(1.0f));
REQUIRE(WorldCoordinates::scrolling(QSize(800, 400), -5, 0.0f).getTilePx()
== Approx(1.0f));
}
TEST_CASE("A zero-size viewport falls back to a unit tile", "[coords]")
{
// A widget that has not been shown yet still has to answer conversions —
// the arena hit-tests through the same transform.
REQUIRE(WorldCoordinates::scrolling(QSize(0, 0), 20, 0.0f).getTilePx()
== Approx(1.0f));
REQUIRE(WorldCoordinates::fitToWorld(QSize(0, 0), 40, 20).getTilePx()
== Approx(1.0f));
}
TEST_CASE("Viewport width in tiles follows the widget width", "[coords]")
{
REQUIRE(makeCoordinates(0.0f).getViewportWidthTiles() == Approx(40.0f));
REQUIRE(WorldCoordinates(QSize(400, 400), 20, 0.0f).getViewportWidthTiles()
REQUIRE(WorldCoordinates::scrolling(QSize(400, 400), 20, 0.0f).getViewportWidthTiles()
== Approx(20.0f));
}
@@ -143,3 +156,48 @@ TEST_CASE("A fractional scroll position widens the viewport rect outward", "[coo
REQUIRE(rect.left() == -21);
REQUIRE(rect.right() == 21);
}
// ---------------------------------------------------------------------------
// Fitted (non-scrolling) worlds
// ---------------------------------------------------------------------------
TEST_CASE("A fitted world takes the tighter of the two axis fits", "[coords]")
{
// Height-limited: 400/20 = 20 px per tile beats 800/30 = 26.67.
REQUIRE(WorldCoordinates::fitToWorld(QSize(800, 400), 30, 20).getTilePx()
== Approx(20.0f));
// Width-limited: 800/80 = 10 px per tile beats 400/20 = 20.
REQUIRE(WorldCoordinates::fitToWorld(QSize(800, 400), 80, 20).getTilePx()
== Approx(10.0f));
}
TEST_CASE("A fitted world keeps its whole width on screen", "[coords]")
{
// The point of taking the tighter fit: the far edge must land inside the
// viewport, never past it.
const WorldCoordinates coordinates =
WorldCoordinates::fitToWorld(QSize(800, 400), 80, 20);
REQUIRE(coordinates.worldToWidget(QVector2D(80.0f, 0.0f)).x() <= 800.0);
REQUIRE(coordinates.worldToWidget(QVector2D(0.0f, 20.0f)).y() <= 400.0);
}
TEST_CASE("A fitted world puts the origin at the widget's top-left", "[coords]")
{
// No scrolling, so there is no view center to subtract.
const WorldCoordinates coordinates =
WorldCoordinates::fitToWorld(QSize(800, 400), 40, 20);
REQUIRE(coordinates.getViewLeftTiles() == Approx(0.0f));
REQUIRE(coordinates.tileToWidget(QPoint(0, 0)) == QPointF(0.0, 0.0));
REQUIRE(coordinates.tileToWidget(QPoint(3, 2)) == QPointF(60.0, 40.0));
}
TEST_CASE("A fitted world round-trips widget points back to world positions",
"[coords]")
{
const WorldCoordinates coordinates =
WorldCoordinates::fitToWorld(QSize(800, 400), 80, 20);
const QVector2D world = coordinates.widgetToWorld(QPoint(120, 55));
const QPointF back = coordinates.worldToWidget(world);
REQUIRE(back.x() == Approx(120.0));
REQUIRE(back.y() == Approx(55.0));
}