Repository navigation
Conversation
… overlays placed in a rect
| ) { _, _ in } | ||
| } | ||
|
|
||
| /// Room that takes a share of a stack's spare room, `weight` against |
There was a problem hiding this comment.
This says a spacer's weight is measured only against the stack's other spacers. Flex.lengths actually splits spare room across every open part by weight, and space(_:scales:) uses its length in points as its weight (line 225). So a unitless .spacer() (weight 1) next to .space(20, scales: 1...10) in a row with 100 spare gets about 4.5 pt, and the scaled space takes about 91. Anyone reading this comment would expect the spacers to split the room evenly. The PR's own margin trick (spacer(1.5 * (20 + 26 + 26))) works only because the weights are compared in points. Either say that here and in the README (a spacer's weight is weighed against scaled spaces' lengths in points), or fill scaled spaces and spacers in separate passes.
|
Claude review (house rules) |
|
Claude review (house rules), correction: CONTRIBUTING.md does exist, and I've now read it too. It doesn't change the inline comment. Its point 2 (no game's names, identifiers, content or reasons in descriptions) backs up the note above about the title and description. |
…, Overlay, Item, Gap, Spacer)
| } | ||
| return pt(x, y) | ||
| } | ||
| return (node as? SKSpriteNode)?.anchorPoint ?? pt(0.5, 0.5) |
There was a problem hiding this comment.
A TextBlockNode falls through to pt(0.5, 0.5) here, but its origin isn't its middle. With the default anchor: .top, the origin is the top of the first line; with .firstBaseline, it's the first baseline. Horizontally it follows the block's alignment. So Item(block, size: sz(block.width, block.height)), which the docs and README invite ("a text block's measured size"), puts the block's top at the middle of its slot. The text then hangs half its height too low and spills into whatever comes next in the column. The PR's own examples only come out right because they pass anchor: pt(0, 1) / pt(0.5, 1) by hand.
PlatformLayout can't see TextBlockNode, so this needs one of these:
- a hook a node can conform to, for example a
LayoutAnchoredprotocol in PlatformCore thatTextBlockNodeadopts; - an
anchorthat's required whenever the node isn't a label or a sprite; - at the least, docs saying text blocks need an explicit anchor.
|
Claude review (house rules) |
…ext blocks know theirs
|
Closing for now: not worth it at this point. The design and the Hue Rings before/after stay here for reference. |
A new optional module,
PlatformLayout, so a screen says its arrangement once (rows, columns, grids, overlays, gaps, padding, alignment) instead of working out each position by hand. A game links it and moves a screen onto it when it next works on that screen; nothing else changes. No game uses it in this PR, so nothing on screen changes.This PR is for settling the API first. The examples below are real Hue Rings code, before and after; fleon/huerings#56 makes these changes, in a draft that points its submodule at this branch. Open questions are at the end.
The API
Layouts are written in a result builder, like SwiftUI:
Column(gap:align:),Row(gap:align:),Tiles(columns:gap:),Overlay. Their braces takeif,if let,switchandfor.Item(node)for a node that knows its size (LayoutSized).Item(node, size:),width:and/orheight:for any other node, and to override. By default the anchor is read when the node is placed:layoutAnchor, a label's alignment modes, a sprite's anchor point, or the middle.Item(size:) { rect in … }gets its rect: a backdrop, a card's face.LayoutSizedlayoutSize, andlayoutAnchorif its origin isn't its middle. Games adopt it for their buttons, boards and pills.TextBlockNodeadopts it here, from its measured lines, its alignment and its.top/.firstBaselinemode; it now keepstopAboveOriginfor that.PlatformLayoutdepends onPlatformSpritefor this.Gap(20)is fixed and replaces the stack's gap.Gap(20, scales: 1.0 / 3 ... 1.5)grows into spare room or gives it back.Spacer(weight)takes a weighted share of what's spare..aligned(_:_:),.padding(…),.frame(width:height:),.direction(_:), written on the line after an item, as in SwiftUILayoutDirection.current. Rows run from the leading edge, andleading/trailingand padding mirror.left/rightstay physical.CGRect.with(minX:maxX:minY:maxY:)gives the room between two thingsEvery stack and item conforms to
LayoutItem, and a modifier returns aLayout. Either can be kept in aletand used inside another stack.Why
Item(…)and not.item(…): inside a builder, a line starting with.is read as a call on the line above, so.item(a)followed by.grid(…)would parse as.item(a).grid(…). That same rule is what lets.padding(…)on the next line work as a modifier. So the items have to be types.Names: these type names don't collide with any top-level type in the five games. The exception was
Grid, because Hue Rings hasclass Grid; that's why the grid is calledTiles. A game type with the same name as one of these would hide it inside that game.Rules:
LayoutSized, or a size given toItem. Nodes are never measured: their frames take in shadows, glows and running animations. A plain node with no size given is a compile error, not a zero.sizeis its whole tree's size, so a card's face is drawn from the same description its content is placed by.place(in:)runs on load and whenSceneLayoutchanges, never per frame. It sets resting positions and leaves running actions alone.SceneLayout(cornerButtonCenter, the rail). A layout places what stands between them.Two things learned from #209 are built in:
direction(_:)changes only how a subtree lays out its contents. Where the subtree stands is still up to its parent.Before / after, from Hue Rings
The buttons, the board, the score's pill and text blocks adopt
LayoutSizedin the game (ButtonNode:sz(buttonWidth, buttonSize.height)hung atanchorX;GridNode:.gridBackgroundSize; and so on), so thoseItems take no size.Each "after" places every node exactly where the "before" does, except one deliberate simplification in 1 and 2.
DesignLayout.columngave the gaps only a quarter of the spare room. The "after" lets them take all of it until they're half again as big, then keeps the column centred. Matching the old rule exactly needed weighted margin spacers, which were hard to read. The result is a shift of up to about 12 points on some screens (fleon/huerings#56 lists them). To check this, I copied each "before" calculation word for word into a throwaway test, ran it beside the "after" with the same numbers, and compared positions to 1e-6.DesignLayout.columnwas checked at four room heights, covering both shrinking and the 1.5× cap, in the exact (weighted) form. That test isn't committed because it is game code. fleon/huerings#56 also checks the real app on an iOS simulator. Every node's position on the title, settings popup and More Games, portrait, landscape and iPad, is identical tomain's. The game screen differs only by the shift above.1. The game screen's column, upright (
GameScene.layoutUpright,DesignLayout.column)The heights, the gaps and the order are written once, next to each other. Neither the index-matched
ys[n]nor the centre x appears anywhere.2. The game screen, turned (
GameScene.layoutTurned,SceneLayout.landscapeColumns)The column works out its own width (
max(barWidth, buttons, overlay)goes away). Spacing "evenly between and at both edges" is three spacers rather than a helper.3. A popup's card (
PopupNode.createContainer)The card's height comes from the same description its content is placed by, so the two can't drift apart. Having one button or two is no longer a separate branch.
4. More Games: the sheet and a tile (
MoreGamesPopupNode)The running
ycursor, the row/column index math and the sheet height add-up all go away. The tile mirrors in a right-to-left language. In the tile, the text blocks' own left alignment andanchor: pt(0, 1)would still need to follow the reading direction.5. The title: Play and More games spread below the block (
TitleScene.placeColumn)The
2.5is now spelled out as the spacers' weights.Open questions
Layout. SwiftUI also has aLayoutprotocol, but the two only clash in a file that imports both. The alignment and insets types are nested (Layout.Horizontal,Layout.Insets)..fill. Each item gets the stack's whole width (or height) and stands in its middle. This is what lets the panel column in 2 stretch its gaps inside the row. Is that the default we want, or should it be.centerwith.fillasked for?LayoutDirection.current. Now that the module depends onPlatformSprite(for text blocks), should it followTypography.follow's language on its own?SceneLayoutcalls, and each scene'supdateLayoutbuilds its layout and places it, usingif layout.isLandscape { … } else { … }inside the braces. Next steps could be corner items that followSceneLayout's rules, and a layout the scene keeps and the module re-places on everySceneLayoutchange.Verification
PlatformLayoutTests, 21 tests (22 on Mac Catalyst), covering:frameandoverlay;if,if let/elseandforinside a stack;LayoutSized(size taken, anchor read when placed, a given size winning), and a text block's size and anchor from its top or its first baseline (on Catalyst, whereTextBlockNodeexists);swift test --package-path PlatformScripts/check-android.sh arm64-v8aPlatformLayoutbuilt for iOS and tvOS