// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Where the board sits in a window of a given size, and which dot a point //! on the screen is over. //! //! The board is scaled to fit and centered, keeping its dots square, with a //! dot's spacing of margin all round, room for an arrowhead pointing off //! the edge, and a band across the top, one spacing tall, for the lives. const std = @import("std"); const rules = @import("rules"); const Dot = rules.Board.Dot; const Point = @import("draw.zig").Point; const Layout = @This(); /// Where dot (0, 0) is drawn, in pixels. origin: Point, /// The distance from one dot to the next, in pixels. spacing: f32, /// The board's size in dots. columns: u16, rows: u16, /// Where the lives are drawn: the middle of the band above the board. lives_center: Point, pub fn fit(columns: u16, rows: u16, width: f32, height: f32) Layout { return fitIn(columns, rows, .{ 0, 0 }, width, height); } /// The same, within the part of a window that is `width` by `height` with /// its top left corner at `corner`: what a phone leaves clear of its /// camera cutout and system bars. pub fn fitIn(columns: u16, rows: u16, corner: Point, width: f32, height: f32) Layout { const cols: f32 = @floatFromInt(columns); const rws: f32 = @floatFromInt(rows); // From the first dot to the last is one spacing fewer than there are // dots; a spacing either side of that is the margin, and one more, // above the top margin, is the band of lives. const spacing = @max(@min(width / (cols + 1), height / (rws + 2)), 0); const board_w = spacing * (cols - 1); const board_h = spacing * (rws - 1); const top = (height - board_h - 3 * spacing) / 2 + 2 * spacing; const left = (width - board_w) / 2; return .{ .origin = corner + Point{ left, top }, .spacing = spacing, .columns = columns, .rows = rows, .lives_center = corner + Point{ width / 2, top - 1.5 * spacing }, }; } /// The center of `dot`, in pixels. pub fn center(l: Layout, dot: Dot) Point { return l.at(.{ @floatFromInt(dot.x), @floatFromInt(dot.y) }); } /// A point in dot coordinates, which need not be whole, in pixels. pub fn at(l: Layout, p: Point) Point { return l.origin + p * @as(Point, @splat(l.spacing)); } /// The dot nearest `p`, if it is on the board: anywhere in the square of /// the board the dot is the middle of. pub fn dotAt(l: Layout, p: Point) ?Dot { if (l.spacing <= 0) return null; const rel = (p - l.origin) / @as(Point, @splat(l.spacing)); const x = @round(rel[0]); const y = @round(rel[1]); if (!(x >= 0 and y >= 0)) return null; if (x >= @as(f32, @floatFromInt(l.columns)) or y >= @as(f32, @floatFromInt(l.rows))) return null; return .{ .x = @intFromFloat(x), .y = @intFromFloat(y) }; } const testing = std.testing; test "a board fitted inside part of a window is moved with it" { const whole = fit(4, 4, 1000, 600); const part = fitIn(4, 4, .{ 40, 30 }, 1000, 600); try testing.expectEqual(whole.spacing, part.spacing); try testing.expectEqual(whole.origin + Point{ 40, 30 }, part.origin); try testing.expectEqual(whole.lives_center + Point{ 40, 30 }, part.lives_center); try testing.expectEqual(rules.Board.Dot{ .x = 0, .y = 0 }, part.dotAt(.{ 390, 230 }).?); } test "a square board in a wide window is centered and fits the height" { const l = fit(4, 4, 1000, 600); try testing.expectEqual(100, l.spacing); // 300 from the first dot to the last, centered in 1000; and below the // band of lives and a margin, which take the top 200. try testing.expectEqual(Point{ 350, 200 }, l.origin); try testing.expectEqual(Point{ 500, 50 }, l.lives_center); try testing.expectEqual(Point{ 650, 500 }, l.center(.{ .x = 3, .y = 3 })); } test "a point is over the dot whose square it is in" { const l = fit(4, 4, 1000, 600); try testing.expectEqual(Dot{ .x = 0, .y = 0 }, l.dotAt(.{ 350, 200 }).?); try testing.expectEqual(Dot{ .x = 1, .y = 0 }, l.dotAt(.{ 401, 249 }).?); try testing.expectEqual(Dot{ .x = 3, .y = 3 }, l.dotAt(.{ 699, 549 }).?); try testing.expectEqual(null, l.dotAt(.{ 299, 200 })); try testing.expectEqual(null, l.dotAt(.{ 701, 200 })); // The band of lives is not the board. try testing.expectEqual(null, l.dotAt(.{ 500, 50 })); } test "a window too small to draw in has no dots" { const l = fit(4, 4, 0, 0); try testing.expectEqual(null, l.dotAt(.{ 0, 0 })); }