Cotton code
132 lines · 5.4 KB · SwiftRaw
// The MARQUEE over the Films player (the Picture House, 4 Nov 2026,// `cinemas14-materials`' `marquee-plate`): as an episode starts, its caption// ("S2 E5 · The Long Night", the lobby card's own words,// `EpisodeLobbySetting.caption`) typed in the cream on the painted charcoal// plate under its worn brass rule, laid over the top of the player's// picture; it stands three seconds and fades (with Reduce Motion it stands// the same three seconds and goes without fading). The plate is painted// (`Media.xcassets`, imported by `scripts/import-cinemas-materials.py`// `marquee`); the words are live, placed where `layout.json`'s `marquee`// puts them in the 1488 x 195 plate's box.//// The player lays it over its chrome; the plate itself only draws, and// takes no touch.import CottonPaperimport CottonValuesimport CoreTextimport SwiftUIpublic enum CinemaMarqueeKnobs {    // THE PLATE'S BOX, in its painting's pixels (`marquee`'s `size`), drawn    // at most `widest` points wide (its `phoneMaxSize`), `margin` in from    // either side of the player.    static let box = CGSize(width: 1488, height: 195)    public static let widest: CGFloat = 496    public static let margin: CGFloat = 16    static let top: CGFloat = 12    // THE WORDS (`liveTypeRect`), set from its left as the sample is    // (`samplePlacement`: 22 letters across 840 pixels), taken down a step    // at a time to the least before they are cut.    static let words = CGRect(x: 90, y: 35, width: 1308, height: 125)    static let size: CGFloat = 64    static let least: CGFloat = 44    static let shrink: CGFloat = 0.94    /// How long it stands, and how long it takes to fade, in seconds.    public static let stands: Double = 3    public static let fades: Double = 0.6    /// When it is gone either way, and the player may let it go.    public static var gone: Double { stands + fades }}/// How the marquee's words are set.public enum CinemaMarqueeSetting {    /// The caption on one line in the typewriter, at the plate's own size    /// taken down to the least, past it cut with an ellipsis. In the plate's    /// pixels.    public static func line(_ caption: String) -> (line: String?, size: CGFloat) {        let knobs = CinemaMarqueeKnobs.self        let words = caption.split(whereSeparator: \.isWhitespace).joined(separator: " ")        guard !words.isEmpty else { return (nil, knobs.size) }        var size = knobs.size        while HighlightPassageSetting.wide(words, EpisodeLobbySetting.type(size)) > knobs.words.width,              size * knobs.shrink >= knobs.least {            size *= knobs.shrink        }        return (HighlightPassageSetting.lines(words, font: EpisodeLobbySetting.type(size), width: knobs.words.width, limit: 1).first, size)    }}/// The plate with the caption on it, `width` points wide.public struct CinemaMarqueePlate: View {    let caption: String    let width: CGFloat    public init(caption: String, width: CGFloat) {        self.caption = caption        self.width = width    }    public var body: some View {        let knobs = CinemaMarqueeKnobs.self        let s = width / knobs.box.width        let (line, size) = CinemaMarqueeSetting.line(caption)        ZStack(alignment: .topLeading) {            CinemaPainted(name: "marquee-plate")                .frame(width: knobs.box.width * s, height: knobs.box.height * s)            if let line {                Text(line)                    .font(CottonFonts.type(size * s))                    .foregroundStyle(CinemasMaterial.cream.colour)                    .lineLimit(1)                    .fixedSize()                    .frame(width: knobs.words.width * s, height: knobs.words.height * s, alignment: .leading)                    .offset(x: knobs.words.minX * s, y: knobs.words.minY * s)            }        }        .frame(width: knobs.box.width * s, height: knobs.box.height * s, alignment: .topLeading)        .accessibilityElement(children: .ignore)        .accessibilityLabel(line ?? "")        .accessibilityIdentifier("cinema-marquee")    }}/// The marquee as the player lays it over its chrome at the start: the/// plate at the top, as wide as the player allows up to its own width,/// standing three seconds, then fading (Reduce Motion: gone, unfaded).public struct CinemaMarqueeOverlay: View {    let caption: String    /// How far below the overlay's top the screen's status bar reaches: the    /// plate hangs under it, never under the clock.    let below: CGFloat    @State private var shown = true    @Environment(\.accessibilityReduceMotion) private var reduceMotion    public init(caption: String, below: CGFloat = 0) {        self.caption = caption        self.below = below    }    public var body: some View {        let knobs = CinemaMarqueeKnobs.self        GeometryReader { room in            if shown {                CinemaMarqueePlate(caption: caption, width: min(knobs.widest, room.size.width - 2 * knobs.margin))                    .padding(.top, below + knobs.top)                    .frame(width: room.size.width, alignment: .top)                    .transition(.opacity)            }        }        .allowsHitTesting(false)        .task {            try? await Task.sleep(for: .seconds(knobs.stands))            if reduceMotion {                shown = false            } else {                withAnimation(.easeOut(duration: knobs.fades)) { shown = false }            }        }    }}