diff --git a/AGENTS.md b/AGENTS.md index d5b01ea..3353f0f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -101,6 +101,13 @@ would want. - Lay out from `SceneLayout` (the visible and safe rects), never from the scene's size, so every screen works on phones and tablets, upright and in landscape. +- Lay a screen out with `LayoutItem` (rows, columns, grids, overlays, + insets) rather than position arithmetic, and name horizontal places by + reading order: `leading` and `trailing`, never left and right, so the + screen mirrors in a right-to-left language (`LayoutDirection`). What + shows a physical direction (a board, a track) lays its contents out in + its own with `direction(_:)`, standing where its parent puts it; art that points along the reading order (a back + chevron) flips with `mirrorScale`. - Never share an `SKUniform` between shaders that may be built on different threads; give each shader its own. - Every random draw comes from a named, seedable stream (`Random`), never a diff --git a/Platform/Sources/PlatformSprite/LayoutDirection.swift b/Platform/Sources/PlatformSprite/LayoutDirection.swift new file mode 100644 index 0000000..614b90c --- /dev/null +++ b/Platform/Sources/PlatformSprite/LayoutDirection.swift @@ -0,0 +1,72 @@ +import Foundation +import SpriteKitShim + +/// Which way the interface reads: left to right, or right to left (Arabic, +/// Hebrew, Persian, Urdu). Layout written with leading and trailing edges +/// (`LayoutItem`, `SceneLayout`'s edge helpers) mirrors by it; what shows +/// a physical direction (a board, a slider's track, a clock) stays as drawn. +public nonisolated enum LayoutDirection: Sendable, Equatable { + case leftToRight + case rightToLeft + + /// The direction the game's language reads in, unless `forced`. + public static var current: LayoutDirection { + forced ?? resolve() + } + + /// The direction of the game's language: set by `Typography.follow`, or + /// by a game that resolves its language another way. + public nonisolated(unsafe) static var resolve: () -> LayoutDirection = { .leftToRight } + + /// A direction every layout takes whatever the language (a debug launch + /// argument mirroring the interface in English, say); nil follows it. + public nonisolated(unsafe) static var forced: LayoutDirection? + + /// Languages whose script reads right to left, by language code. + private static let rightToLeftLanguages: Set = ["ar", "arc", "ckb", "dv", "fa", "he", "iw", "ks", "ku-Arab", "ps", "sd", "ug", "ur", "yi"] + + /// The direction a language code (`"ar"`, `"he-IL"`, `"pt-BR"`) reads in. + public static func of(language code: String) -> LayoutDirection { + let parts = code.split(whereSeparator: { $0 == "-" || $0 == "_" }) + guard let language = parts.first.map({ $0.lowercased() }) else { return .leftToRight } + if rightToLeftLanguages.contains(language) { return .rightToLeft } + if let script = parts.dropFirst().first, rightToLeftLanguages.contains("\(language)-\(script)") { return .rightToLeft } + return .leftToRight + } + + public var isRightToLeft: Bool { self == .rightToLeft } + + /// The screen side an edge is on: -1 left, 1 right, as `SceneLayout`'s + /// `side` arguments take it. + public func side(_ edge: HorizontalEdge) -> CGFloat { + let leadingSide: CGFloat = isRightToLeft ? 1 : -1 + return edge == .leading ? leadingSide : -leadingSide + } + + /// The x of `rect`'s `edge`. + public func x(_ edge: HorizontalEdge, of rect: CGRect) -> CGFloat { + side(edge) < 0 ? rect.minX : rect.maxX + } + + /// An `xScale` sign that turns art drawn for left to right (a back + /// chevron, a "next" arrow) to face this direction. + public var mirrorScale: CGFloat { isRightToLeft ? -1 : 1 } + + /// A label's alignment for text set against `edge`. + public func labelAlignment(_ edge: HorizontalEdge) -> SKLabelHorizontalAlignmentMode { + side(edge) < 0 ? .left : .right + } +} + +/// A horizontal edge named by reading order: leading is where lines start +/// (the left in English, the right in Arabic). +public nonisolated enum HorizontalEdge: Sendable, Equatable { + case leading + case trailing +} + +extension LocalizationLanguage { + /// The direction this language reads in, from its raw value as a + /// language code. + public var layoutDirection: LayoutDirection { LayoutDirection.of(language: rawValue) } +} diff --git a/Platform/Sources/PlatformSprite/LayoutItem.swift b/Platform/Sources/PlatformSprite/LayoutItem.swift new file mode 100644 index 0000000..02e042f --- /dev/null +++ b/Platform/Sources/PlatformSprite/LayoutItem.swift @@ -0,0 +1,281 @@ +import Foundation +import SpriteKitShim + +/// A small rect-based layout: rows and columns with gaps, grids, overlays +/// and insets, over the rects `SceneLayout` gives. Items state their own +/// size (design tokens, a label's measured text) rather than being measured, +/// and placing a tree sets each leaf's resting position once, on load and +/// when the layout changes, never per frame. It leaves running actions +/// alone. +/// +/// Horizontal placement is by reading order: rows run from the leading +/// edge, grids fill from it, and `leading`/`trailing` alignment and insets +/// mirror in a right-to-left language (`LayoutDirection.current`). A subtree +/// that shows a physical direction keeps its own with `direction(_:)`. +/// +/// LayoutItem.overlay([ +/// .node(back, size: buttonSize).aligned(.leading, .top), +/// .node(settings, size: buttonSize).aligned(.trailing, .top), +/// .column([.label(title, height: titleHeight), .grid(coins, columns: 5, gap: sz(12))], gap: 24), +/// ]) +/// .aligned(.fill, .fill) +/// .place(in: layout.contentRect) +/// +/// No flexible sizing or wrapping: a row wider than the rect it's given +/// runs past it. +public struct LayoutItem { + /// Where an item stands across the rect it's given. `fill` takes the + /// rect's whole width. + public enum HAlign: Equatable { case leading, center, trailing, fill } + /// Where an item stands up and down the rect it's given. `fill` takes + /// the rect's whole height. + public enum VAlign: Equatable { case top, center, bottom, fill } + + /// Insets by reading order: `leading` is the left in a left-to-right + /// language and the right in a right-to-left one. + public struct Insets: Equatable { + public var top: CGFloat + public var leading: CGFloat + public var bottom: CGFloat + public var trailing: CGFloat + + public init(top: CGFloat = 0, leading: CGFloat = 0, bottom: CGFloat = 0, trailing: CGFloat = 0) { + self.top = top + self.leading = leading + self.bottom = bottom + self.trailing = trailing + } + + public init(_ all: CGFloat) { + self.init(top: all, leading: all, bottom: all, trailing: all) + } + + public init(horizontal: CGFloat = 0, vertical: CGFloat = 0) { + self.init(top: vertical, leading: horizontal, bottom: vertical, trailing: horizontal) + } + + /// `rect` inset, the leading and trailing insets on the sides + /// `direction` puts them. + public func inset(_ rect: CGRect, direction: LayoutDirection) -> CGRect { + let left = direction.isRightToLeft ? trailing : leading + let right = direction.isRightToLeft ? leading : trailing + return CGRect(x: rect.minX + left, y: rect.minY + bottom, width: rect.width - left - right, height: rect.height - top - bottom) + } + } + + /// Where a leaf landed: its frame in the parent's space, and the + /// direction it was laid out in. + public struct Placement { + public let frame: CGRect + public let direction: LayoutDirection + } + + private indirect enum Content { + case leaf((Placement) -> Void) + case row([LayoutItem], gap: CGFloat, align: VAlign) + case column([LayoutItem], gap: CGFloat, align: HAlign) + case grid([LayoutItem], columns: Int, gap: CGSize, cell: CGSize) + case overlay([LayoutItem]) + case inset(LayoutItem, Insets) + } + + private let content: Content + /// The size the item asks for. + public private(set) var size: CGSize + private var hAlign: HAlign? + private var vAlign: VAlign? + private var fixedDirection: LayoutDirection? + + private init(_ content: Content, size: CGSize) { + self.content = content + self.size = size + } + + // MARK: Leaves + + /// A leaf of `size`; `place` hears where it landed. + public static func leaf(_ size: CGSize, place: @escaping (Placement) -> Void) -> LayoutItem { + LayoutItem(.leaf(place), size: size) + } + + /// Empty space of `size`. + public static func space(_ size: CGSize) -> LayoutItem { + leaf(size) { _ in } + } + + /// Empty space `width` wide (in a row) or `height` tall (in a column). + public static func space(width: CGFloat = 0, height: CGFloat = 0) -> LayoutItem { + space(CGSize(width: width, height: height)) + } + + /// `node`, `size` big, its position the point `anchor` names in its + /// frame ((0.5, 0.5) its middle, (0, 0) its bottom left: the node's own + /// anchor, which doesn't mirror). + public static func node(_ node: SKNode, size: CGSize, anchor: CGPoint = CGPoint(x: 0.5, y: 0.5)) -> LayoutItem { + leaf(size) { placement in + let frame = placement.frame + node.position = CGPoint(x: frame.minX + frame.width * anchor.x, y: frame.minY + frame.height * anchor.y) + } + } + + /// A one-line label as wide as its text and `height` tall (a line's + /// band), positioned by its own horizontal alignment and centred up and + /// down, as a label built with `.vAlign(.center)` keeps its band. + public static func label(_ label: SKLabelNode, height: CGFloat) -> LayoutItem { + let anchorX: CGFloat + switch label.horizontalAlignmentMode { + case .left: anchorX = 0 + case .right: anchorX = 1 + default: anchorX = 0.5 + } + return node(label, size: CGSize(width: label.frame.width, height: height), anchor: CGPoint(x: anchorX, y: 0.5)) + } + + // MARK: Containers + + /// `items` side by side from the leading edge, `gap` apart, each + /// standing `align` in the row's height unless it says otherwise. + public static func row(_ items: [LayoutItem], gap: CGFloat = 0, align: VAlign = .center) -> LayoutItem { + let width = items.reduce(0) { $0 + $1.size.width } + gap * CGFloat(max(items.count - 1, 0)) + let height = items.map(\.size.height).max() ?? 0 + return LayoutItem(.row(items, gap: gap, align: align), size: CGSize(width: width, height: height)) + } + + /// `items` top to bottom, `gap` apart, each standing `align` across the + /// column's width unless it says otherwise. + public static func column(_ items: [LayoutItem], gap: CGFloat = 0, align: HAlign = .center) -> LayoutItem { + let width = items.map(\.size.width).max() ?? 0 + let height = items.reduce(0) { $0 + $1.size.height } + gap * CGFloat(max(items.count - 1, 0)) + return LayoutItem(.column(items, gap: gap, align: align), size: CGSize(width: width, height: height)) + } + + /// `items` in a grid `columns` wide, filled a row at a time from the + /// leading edge, top row first. Every cell is `cell` big (the largest + /// item's size by default), each item centred in its own unless it says + /// otherwise. + public static func grid(_ items: [LayoutItem], columns: Int, gap: CGSize = .zero, cell: CGSize? = nil) -> LayoutItem { + let columns = max(columns, 1) + let cell = cell ?? CGSize(width: items.map(\.size.width).max() ?? 0, height: items.map(\.size.height).max() ?? 0) + let across = min(columns, items.count) + let rows = (items.count + columns - 1) / columns + let size = CGSize( + width: cell.width * CGFloat(across) + gap.width * CGFloat(max(across - 1, 0)), + height: cell.height * CGFloat(rows) + gap.height * CGFloat(max(rows - 1, 0)) + ) + return LayoutItem(.grid(items, columns: columns, gap: gap, cell: cell), size: size) + } + + /// `items` over one another, each in the whole of the overlay's frame + /// by its own alignment: as big as the biggest. + public static func overlay(_ items: [LayoutItem]) -> LayoutItem { + let size = CGSize(width: items.map(\.size.width).max() ?? 0, height: items.map(\.size.height).max() ?? 0) + return LayoutItem(.overlay(items), size: size) + } + + // MARK: Modifiers + + /// This item inside `insets`: as big as it is plus them, and laid out + /// in its frame less them. + public func inset(_ insets: Insets) -> LayoutItem { + let size = CGSize(width: self.size.width + insets.leading + insets.trailing, height: self.size.height + insets.top + insets.bottom) + return LayoutItem(.inset(self, insets), size: size) + } + + /// This item standing `horizontal` and `vertical` in the rect it's given. + public func aligned(_ horizontal: HAlign? = nil, _ vertical: VAlign? = nil) -> LayoutItem { + var item = self + if let horizontal { item.hAlign = horizontal } + if let vertical { item.vAlign = vertical } + return item + } + + /// This item asking for `width` and `height` rather than its own size + /// (what it holds lays out in that frame). + public func sized(width: CGFloat? = nil, height: CGFloat? = nil) -> LayoutItem { + var item = self + item.size = CGSize(width: width ?? size.width, height: height ?? size.height) + return item + } + + /// What this item holds laid out in `direction` whatever the language: + /// for what shows a physical direction (a board, a track). Where the + /// item itself stands in its parent still follows the parent's. + public func direction(_ direction: LayoutDirection) -> LayoutItem { + var item = self + item.fixedDirection = direction + return item + } + + // MARK: Placing + + /// Places the tree in `rect`: this item stands in it by its alignment + /// (centred unless it says otherwise) and every leaf hears its frame. + public func place(in rect: CGRect, direction: LayoutDirection = .current) { + place(in: rect, direction: direction, hDefault: .center, vDefault: .center) + } + + /// The frame this item takes in `rect`, standing by its alignment. + public func frame(in rect: CGRect, direction: LayoutDirection = .current) -> CGRect { + frame(in: rect, direction: direction, h: hAlign ?? .center, v: vAlign ?? .center) + } + + private func frame(in rect: CGRect, direction: LayoutDirection, h: HAlign, v: VAlign) -> CGRect { + let width = h == .fill ? rect.width : size.width + let height = v == .fill ? rect.height : size.height + let x: CGFloat + switch h { + case .fill: x = rect.minX + case .center: x = rect.midX - width / 2 + case .leading, .trailing: + let edge: HorizontalEdge = h == .leading ? .leading : .trailing + x = direction.side(edge) < 0 ? rect.minX : rect.maxX - width + } + let y: CGFloat + switch v { + case .fill, .bottom: y = rect.minY + case .center: y = rect.midY - height / 2 + case .top: y = rect.maxY - height + } + return CGRect(x: x, y: y, width: width, height: height) + } + + private func place(in rect: CGRect, direction inherited: LayoutDirection, hDefault: HAlign, vDefault: VAlign) { + let frame = frame(in: rect, direction: inherited, h: hAlign ?? hDefault, v: vAlign ?? vDefault) + let direction = fixedDirection ?? inherited + let rightToLeft = direction.isRightToLeft + switch content { + case .leaf(let place): + place(Placement(frame: frame, direction: direction)) + case let .row(items, gap, align): + var cursor = rightToLeft ? frame.maxX : frame.minX + for item in items { + let width = item.size.width + let x = rightToLeft ? cursor - width : cursor + item.place(in: CGRect(x: x, y: frame.minY, width: width, height: frame.height), direction: direction, hDefault: .center, vDefault: align) + cursor += (rightToLeft ? -1 : 1) * (width + gap) + } + case let .column(items, gap, align): + var top = frame.maxY + for item in items { + let height = item.size.height + item.place(in: CGRect(x: frame.minX, y: top - height, width: frame.width, height: height), direction: direction, hDefault: align, vDefault: .center) + top -= height + gap + } + case let .grid(items, columns, gap, cell): + for (index, item) in items.enumerated() { + let column = CGFloat(index % columns) + let row = CGFloat(index / columns) + let offset = column * (cell.width + gap.width) + let x = rightToLeft ? frame.maxX - offset - cell.width : frame.minX + offset + let y = frame.maxY - row * (cell.height + gap.height) - cell.height + item.place(in: CGRect(x: x, y: y, width: cell.width, height: cell.height), direction: direction, hDefault: .center, vDefault: .center) + } + case .overlay(let items): + for item in items { + item.place(in: frame, direction: direction, hDefault: .center, vDefault: .center) + } + case let .inset(item, insets): + item.place(in: insets.inset(frame, direction: direction), direction: direction, hDefault: .fill, vDefault: .fill) + } + } +} diff --git a/Platform/Sources/PlatformSprite/SceneLayout.swift b/Platform/Sources/PlatformSprite/SceneLayout.swift index cbcfe5d..0045efe 100644 --- a/Platform/Sources/PlatformSprite/SceneLayout.swift +++ b/Platform/Sources/PlatformSprite/SceneLayout.swift @@ -451,3 +451,23 @@ public struct SceneLayout: Equatable { cornerButtonCenter(side: 1, end: -1, size: size).offset(0, CGFloat(index) * metrics.railButtonSpacing) } } + +extension SceneLayout { + /// `edgeX(side:clearsHousing:)` by reading order: the leading edge is + /// the left in a left-to-right language and the right in a + /// right-to-left one. + public func edgeX(_ edge: HorizontalEdge, clearsHousing: Bool, direction: LayoutDirection = .current) -> CGFloat { + edgeX(side: direction.side(edge), clearsHousing: clearsHousing) + } + + /// `edgeButtonX(side:size:clearsHousing:)` by reading order. + public func edgeButtonX(_ edge: HorizontalEdge, size: CGFloat, clearsHousing: Bool, direction: LayoutDirection = .current) -> CGFloat { + edgeButtonX(side: direction.side(edge), size: size, clearsHousing: clearsHousing) + } + + /// `cornerButtonCenter(side:end:size:)` by reading order: a back button + /// stands in the leading top corner. + public func cornerButtonCenter(_ edge: HorizontalEdge, end: CGFloat, size: CGFloat, direction: LayoutDirection = .current) -> CGPoint { + cornerButtonCenter(side: direction.side(edge), end: end, size: size) + } +} diff --git a/Platform/Sources/PlatformSprite/TextHelpers.swift b/Platform/Sources/PlatformSprite/TextHelpers.swift index 5491d28..f17cc61 100644 --- a/Platform/Sources/PlatformSprite/TextHelpers.swift +++ b/Platform/Sources/PlatformSprite/TextHelpers.swift @@ -198,7 +198,8 @@ extension Typography { /// Sets text as `localizer`'s current language asks: in its script's /// own face where it has one, else `base`, with its line height and - /// text band. + /// text band, and lays out in the direction it reads + /// (`LayoutDirection.current`). public static func follow( _ localizer: Localizer, base: @escaping (_ weight: UIFont.Weight, _ size: CGFloat) -> UIFont @@ -211,6 +212,7 @@ extension Typography { } lineHeightRatio = { localizer.current.lineHeightRatio } textBand = { localizer.current.textBand } + LayoutDirection.resolve = { localizer.current.layoutDirection } } } diff --git a/Platform/Tests/PlatformSpriteTests/LayoutItemTests.swift b/Platform/Tests/PlatformSpriteTests/LayoutItemTests.swift new file mode 100644 index 0000000..478922f --- /dev/null +++ b/Platform/Tests/PlatformSpriteTests/LayoutItemTests.swift @@ -0,0 +1,188 @@ +import Foundation +import Testing +import SpriteKitShim +@testable import PlatformSprite + +/// The layout's arithmetic, in both directions: a right-to-left layout is +/// the left-to-right one mirrored about the rect's middle, except where a +/// subtree keeps its own direction. +struct LayoutItemTests { + /// Leaves that write their frames into `frames` under their names. + private final class Frames { + var frames: [String: CGRect] = [:] + + func leaf(_ name: String, _ width: CGFloat, _ height: CGFloat) -> LayoutItem { + .leaf(CGSize(width: width, height: height)) { [unowned self] in frames[name] = $0.frame } + } + } + + private static let screen = CGRect(x: -200, y: -400, width: 400, height: 800) + + private func mirrored(_ frame: CGRect, in rect: CGRect) -> CGRect { + CGRect(x: rect.minX + rect.maxX - frame.maxX, y: frame.minY, width: frame.width, height: frame.height) + } + + @Test func rowRunsFromTheLeadingEdge() { + let f = Frames() + let row = LayoutItem.row([f.leaf("a", 40, 20), f.leaf("b", 60, 40)], gap: 10, align: .top) + #expect(row.size == CGSize(width: 110, height: 40)) + row.aligned(.leading, .top).place(in: Self.screen, direction: .leftToRight) + #expect(f.frames["a"] == CGRect(x: -200, y: 380, width: 40, height: 20)) + #expect(f.frames["b"] == CGRect(x: -150, y: 360, width: 60, height: 40)) + row.aligned(.leading, .top).place(in: Self.screen, direction: .rightToLeft) + #expect(f.frames["a"] == CGRect(x: 160, y: 380, width: 40, height: 20)) + #expect(f.frames["b"] == CGRect(x: 90, y: 360, width: 60, height: 40)) + } + + @Test func columnRunsTopDownAndAlignsByReadingOrder() { + let f = Frames() + let column = LayoutItem.column([f.leaf("a", 40, 20), f.leaf("b", 100, 30).aligned(.center)], gap: 5, align: .leading) + #expect(column.size == CGSize(width: 100, height: 55)) + column.place(in: Self.screen, direction: .leftToRight) + #expect(f.frames["a"] == CGRect(x: -50, y: 7.5, width: 40, height: 20)) + #expect(f.frames["b"] == CGRect(x: -50, y: -27.5, width: 100, height: 30)) + column.place(in: Self.screen, direction: .rightToLeft) + #expect(f.frames["a"] == CGRect(x: 10, y: 7.5, width: 40, height: 20)) + #expect(f.frames["b"] == CGRect(x: -50, y: -27.5, width: 100, height: 30)) + } + + @Test func gridFillsRowsFromTheLeadingEdge() { + let f = Frames() + let items = (0 ..< 5).map { f.leaf("\($0)", 20, 20) } + let grid = LayoutItem.grid(items, columns: 3, gap: CGSize(width: 10, height: 4), cell: CGSize(width: 30, height: 30)) + #expect(grid.size == CGSize(width: 110, height: 64)) + let rect = CGRect(x: 0, y: 0, width: 110, height: 64) + grid.place(in: rect, direction: .leftToRight) + #expect(f.frames["0"] == CGRect(x: 5, y: 39, width: 20, height: 20)) + #expect(f.frames["2"] == CGRect(x: 85, y: 39, width: 20, height: 20)) + #expect(f.frames["3"] == CGRect(x: 5, y: 5, width: 20, height: 20)) + let leftToRight = f.frames + grid.place(in: rect, direction: .rightToLeft) + for (name, frame) in leftToRight { + #expect(f.frames[name] == mirrored(frame, in: rect), "\(name)") + } + } + + @Test func overlayCornersAndFill() { + let f = Frames() + let screen = LayoutItem.overlay([ + f.leaf("back", 44, 44).aligned(.leading, .top), + f.leaf("settings", 44, 44).aligned(.trailing, .top), + f.leaf("shade", 0, 0).aligned(.fill, .fill), + ]).aligned(.fill, .fill) + screen.place(in: Self.screen, direction: .leftToRight) + #expect(f.frames["back"] == CGRect(x: -200, y: 356, width: 44, height: 44)) + #expect(f.frames["settings"] == CGRect(x: 156, y: 356, width: 44, height: 44)) + #expect(f.frames["shade"] == Self.screen) + screen.place(in: Self.screen, direction: .rightToLeft) + #expect(f.frames["back"] == CGRect(x: 156, y: 356, width: 44, height: 44)) + #expect(f.frames["settings"] == CGRect(x: -200, y: 356, width: 44, height: 44)) + } + + @Test func insetsByReadingOrder() { + let f = Frames() + let panel = LayoutItem.overlay([ + f.leaf("face", 0, 0).aligned(.fill, .fill), + f.leaf("text", 100, 20).inset(LayoutItem.Insets(top: 10, leading: 30, bottom: 6, trailing: 12)), + ]) + #expect(panel.size == CGSize(width: 142, height: 36)) + let rect = CGRect(x: 0, y: 0, width: 142, height: 36) + panel.place(in: rect, direction: .leftToRight) + #expect(f.frames["face"] == rect) + #expect(f.frames["text"] == CGRect(x: 30, y: 6, width: 100, height: 20)) + panel.place(in: rect, direction: .rightToLeft) + #expect(f.frames["text"] == CGRect(x: 12, y: 6, width: 100, height: 20)) + } + + @Test func aFixedDirectionKeepsItsSubtree() { + let f = Frames() + let row = LayoutItem.row([ + f.leaf("label", 50, 20), + LayoutItem.row([f.leaf("first", 20, 20), f.leaf("second", 20, 20)], gap: 4).direction(.leftToRight), + ], gap: 10) + row.place(in: CGRect(x: 0, y: 0, width: 104, height: 20), direction: .rightToLeft) + #expect(f.frames["label"] == CGRect(x: 54, y: 0, width: 50, height: 20)) + #expect(f.frames["first"] == CGRect(x: 0, y: 0, width: 20, height: 20)) + #expect(f.frames["second"] == CGRect(x: 24, y: 0, width: 20, height: 20)) + } + + @Test func aFixedDirectionItemStandsByItsParents() { + let f = Frames() + let column = LayoutItem.column([ + f.leaf("title", 40, 20), + LayoutItem.row([f.leaf("start", 20, 10), f.leaf("end", 20, 10)]).direction(.leftToRight), + ], align: .leading) + column.aligned(.leading, .top).place(in: CGRect(x: 0, y: 0, width: 100, height: 30), direction: .rightToLeft) + #expect(f.frames["title"] == CGRect(x: 60, y: 10, width: 40, height: 20)) + #expect(f.frames["start"] == CGRect(x: 60, y: 0, width: 20, height: 10)) + #expect(f.frames["end"] == CGRect(x: 80, y: 0, width: 20, height: 10)) + let board = f.leaf("board", 30, 30).direction(.leftToRight).aligned(.leading, .top) + LayoutItem.overlay([board]).aligned(.fill, .fill).place(in: CGRect(x: 0, y: 0, width: 100, height: 100), direction: .rightToLeft) + #expect(f.frames["board"] == CGRect(x: 70, y: 70, width: 30, height: 30)) + #expect(board.frame(in: CGRect(x: 0, y: 0, width: 100, height: 100), direction: .rightToLeft).minX == 70) + } + + @Test func sizedHoldsItsContentInTheLargerFrame() { + let f = Frames() + let cell = f.leaf("dot", 10, 10).aligned(.trailing, .bottom) + let item = LayoutItem.overlay([cell]).sized(width: 40, height: 30) + #expect(item.size == CGSize(width: 40, height: 30)) + item.place(in: CGRect(x: 0, y: 0, width: 40, height: 30), direction: .leftToRight) + #expect(f.frames["dot"] == CGRect(x: 30, y: 0, width: 10, height: 10)) + } + + @Test func nodesStandAtTheirAnchor() { + let node = SKNode() + let item = LayoutItem.node(node, size: CGSize(width: 40, height: 20), anchor: CGPoint(x: 0, y: 0.5)) + item.aligned(.leading, .bottom).place(in: CGRect(x: 0, y: 0, width: 100, height: 100), direction: .rightToLeft) + #expect(node.position == CGPoint(x: 60, y: 10)) + } + + @Test func emptyContainersAskForNothing() { + #expect(LayoutItem.row([], gap: 10).size == .zero) + #expect(LayoutItem.column([], gap: 10).size == .zero) + #expect(LayoutItem.grid([], columns: 3, gap: CGSize(width: 5, height: 5)).size == .zero) + } +} + +struct LayoutDirectionTests { + @Test func languagesReadTheirWay() { + for code in ["ar", "ar-SA", "he", "fa_IR", "ur", "ku-Arab"] { + #expect(LayoutDirection.of(language: code) == .rightToLeft, "\(code)") + } + for code in ["en", "pt-BR", "zh-Hans", "th", "ku", "ja"] { + #expect(LayoutDirection.of(language: code) == .leftToRight, "\(code)") + } + } + + @Test func edgesFallOnTheirSides() { + #expect(LayoutDirection.leftToRight.side(.leading) == -1) + #expect(LayoutDirection.rightToLeft.side(.leading) == 1) + #expect(LayoutDirection.rightToLeft.side(.trailing) == -1) + let rect = CGRect(x: -10, y: 0, width: 30, height: 5) + #expect(LayoutDirection.rightToLeft.x(.leading, of: rect) == 20) + #expect(LayoutDirection.leftToRight.labelAlignment(.leading) == .left) + #expect(LayoutDirection.rightToLeft.labelAlignment(.leading) == .right) + #expect(LayoutDirection.rightToLeft.mirrorScale == -1) + } + + @Test func forcedWinsOverTheLanguage() { + let resolve = LayoutDirection.resolve + defer { + LayoutDirection.resolve = resolve + LayoutDirection.forced = nil + } + LayoutDirection.resolve = { .leftToRight } + #expect(LayoutDirection.current == .leftToRight) + LayoutDirection.forced = .rightToLeft + #expect(LayoutDirection.current == .rightToLeft) + } + + @Test func cornerButtonsByReadingOrder() { + let phone = CGRect(x: -201, y: -437, width: 402, height: 874) + let layout = SceneLayout(visibleRect: phone, safeRect: phone) + #expect(layout.cornerButtonCenter(.leading, end: 1, size: 44, direction: .rightToLeft) == layout.cornerButtonCenter(side: 1, end: 1, size: 44)) + #expect(layout.cornerButtonCenter(.leading, end: 1, size: 44, direction: .leftToRight) == layout.cornerButtonCenter(side: -1, end: 1, size: 44)) + #expect(layout.edgeButtonX(.trailing, size: 44, clearsHousing: true, direction: .rightToLeft) == layout.edgeButtonX(side: -1, size: 44, clearsHousing: true)) + } +} diff --git a/README.md b/README.md index 5d9250b..a849123 100644 --- a/README.md +++ b/README.md @@ -478,6 +478,18 @@ decides, it hands in: (`DisplayCorners`), a camera's occlusion (iOS 27.1's `reservedRegions`) and where round buttons stand in the corners or a rail, by the game's `SceneLayout.Metrics`. +- **Layout** (`LayoutItem`, `LayoutDirection`): a small rect-based layout + over `SceneLayout`'s rects: rows and columns with gaps, grids, overlays, + insets and alignment. Items state their own size and placing the tree + sets each leaf's resting position, once on load and when the layout + changes. Horizontal placement is by reading order (leading and + trailing), so a right-to-left language mirrors it; `direction(_:)` + keeps what a subtree that shows a physical direction holds as drawn, + while the subtree itself stands where its parent puts it. + `LayoutDirection.current` follows the language (`Typography.follow`, + else a game sets `resolve`), and `forced` mirrors it for a debug launch + argument. `SceneLayout`'s corner and edge helpers take a + `HorizontalEdge` too. - **Launch splash** (`StudioSplashScene`): a studio's logo over a footer line or two, held at least a minimum time and until the game reports what it waits for `ready` (a game service's first answer, a feed), never