Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Platform/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ let package = Package(
.library(name: "PlatformAndroid", targets: ["PlatformAndroid"]),
.library(name: "PlatformAndroidHost", targets: ["PlatformAndroidHost"]),
.library(name: "PlatformSprite", targets: ["PlatformSprite"]),
.library(name: "PlatformLayout", targets: ["PlatformLayout"]),
.library(name: "PlatformGameCenter", targets: ["PlatformGameCenter"]),
.library(name: "PlatformStore", targets: ["PlatformStore"]),
.library(name: "PlatformShare", targets: ["PlatformShare"]),
Expand Down Expand Up @@ -270,6 +271,27 @@ let package = Package(
],
swiftSettings: settings
),
// Optional, adopted screen by screen: columns, rows, tiles and
// overlays with gaps, padding and alignment, written in a result
// builder and placed in a rect instead of worked out position by
// position. Text blocks (`PlatformSprite`) know their own size.
.target(
name: "PlatformLayout",
dependencies: [
"PlatformCore",
"PlatformSprite",
.product(name: "SpriteKitShim", package: "spritekit-android", condition: onShim),
],
swiftSettings: settings
),
.testTarget(
name: "PlatformLayoutTests",
dependencies: [
"PlatformLayout",
.product(name: "SpriteKitShim", package: "spritekit-android", condition: onShim),
],
swiftSettings: settings
),
// Game Center: sign-in, achievements reported as they move, scores,
// the dashboard. GameKit on Apple; Play Games (PlatformShim) on
// Android.
Expand Down
150 changes: 150 additions & 0 deletions Platform/Sources/PlatformLayout/Items.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import Foundation
#if SPRITEKIT_SHIM
import SpriteKitShim
#else
import SpriteKit
import UIKit
#endif

// The items a layout is written in, inside a stack's braces:
//
// Column(gap: .alertPadding) {
// Item(content, height: contentHeight)
// Row(gap: .alertButtonSpacing) {
// for button in buttons { Item(button, width: button.width, height: .buttonHeight) }
// }
// }

/// Lists a stack's items, with `if`, `switch` and `for` among them.
@resultBuilder
public enum LayoutBuilder {
public static func buildExpression(_ item: some LayoutItem) -> [Layout] { [item.layout] }
public static func buildExpression(_ items: [some LayoutItem]) -> [Layout] { items.map(\.layout) }
public static func buildBlock(_ parts: [Layout]...) -> [Layout] { parts.flatMap { $0 } }
public static func buildOptional(_ part: [Layout]?) -> [Layout] { part ?? [] }
public static func buildEither(first part: [Layout]) -> [Layout] { part }
public static func buildEither(second part: [Layout]) -> [Layout] { part }
public static func buildArray(_ parts: [[Layout]]) -> [Layout] { parts.flatMap { $0 } }
public static func buildLimitedAvailability(_ part: [Layout]) -> [Layout] { part }
}

/// Items top down, `gap` apart (a `Gap` or `Spacer` between two stands in
/// for the gap), each standing across the column as `align` says: by
/// default given the column's whole width, in the middle of which an item
/// that doesn't stretch stands.
public struct Column: LayoutItem {
public let layout: Layout

public init(gap: CGFloat = 0, align: Layout.Horizontal = .fill, @LayoutBuilder _ items: () -> [Layout]) {
layout = .column(items(), gap: gap, align: align)
}
}

/// Items from the leading edge, `gap` apart (a `Gap` or `Spacer` between
/// two stands in for the gap), each standing across the row as `align`
/// says: by default given the row's whole height, so a column in it can
/// stretch its gaps.
public struct Row: LayoutItem {
public let layout: Layout

public init(gap: CGFloat = 0, align: Layout.Vertical = .fill, @LayoutBuilder _ items: () -> [Layout]) {
layout = .row(items(), gap: gap, align: align)
}
}

/// Items in `columns` across, filled a row at a time from the leading top
/// corner, each cell as big as the biggest item and `gap` apart (across,
/// then down). A short last row starts at the leading edge.
public struct Tiles: LayoutItem {
public let layout: Layout

public init(columns: Int, gap: CGSize = .zero, @LayoutBuilder _ items: () -> [Layout]) {
layout = .grid(items(), columns: columns, gap: gap)
}
}

/// Items one over another in the same rect, as big as the biggest.
public struct Overlay: LayoutItem {
public let layout: Layout

public init(@LayoutBuilder _ items: () -> [Layout]) {
layout = .overlay(items())
}
}

/// A node that knows the room it takes, so `Item(node)` needs no size: a
/// game's button, its board, a text block. A size given to `Item` wins.
public protocol LayoutSized: SKNode {
/// The room it asks for, read as the layout is made.
var layoutSize: CGSize { get }
/// Where its position falls in that room, as a share of it, read as it's
/// placed; nil (the default) for an item's usual default.
var layoutAnchor: CGPoint? { get }
}

extension LayoutSized {
public var layoutAnchor: CGPoint? { nil }
}

/// A node standing in the middle of the room it's given, its position where
/// `anchor` (a share of its size) falls. By default, read as it's placed: a
/// `LayoutSized` node's `layoutAnchor`, where a label's alignment modes put
/// its text (a baseline on the foot), a sprite's anchor point, or the
/// middle, as a container node draws round its origin.
public struct Item: LayoutItem {
public let layout: Layout

/// A node that knows its size (`LayoutSized`).
public init(_ node: some LayoutSized, anchor: CGPoint? = nil) {
layout = .node(node, size: node.layoutSize, anchor: anchor)
}

public init(_ node: SKNode, size: CGSize, anchor: CGPoint? = nil) {
layout = .node(node, size: size, anchor: anchor)
}

/// A node where only its height counts (in a column), or its width too.
public init(_ node: SKNode, width: CGFloat = 0, height: CGFloat, anchor: CGPoint? = nil) {
layout = .node(node, size: sz(width, height), anchor: anchor)
}

/// A node where only its width counts (in a row).
public init(_ node: SKNode, width: CGFloat, anchor: CGPoint? = nil) {
layout = .node(node, size: sz(width, 0), anchor: anchor)
}

/// Asks for `size` and hands `place` the rect it's given, whole: for
/// what's sized to its room (a backdrop, a card's face) or placed by
/// rules of its own.
public init(size: CGSize = .zero, place: @escaping (CGRect) -> Void) {
layout = .custom(size: size, place)
}
}

/// Room along a stack, in place of its gap.
public struct Gap: LayoutItem {
public let layout: Layout

/// `length`, fixed.
public init(_ length: CGFloat) {
layout = .space(length)
}

/// `length`, growing into the stack's spare room or giving it back
/// where it's short, by a factor within `scales`. A stack's scaled gaps
/// keep their proportions.
public init(_ length: CGFloat, scales: ClosedRange<CGFloat>) {
layout = .space(length, scales: scales)
}
}

/// Room that takes a share of a stack's spare room, `weight` against the
/// stack's other spacers: spacers at both ends and between spread a row
/// evenly. Never less than `minimum`.
public struct Spacer: LayoutItem {
public let layout: Layout

public init(_ weight: CGFloat = 1, minimum: CGFloat = 0) {
layout = .spacer(weight, minimum: minimum)
}
}
Loading
Loading