Cotton code
171 lines · 6.4 KB · SwiftRaw
// The parts Cotton Story's faces share (contract, 17 Nov 2026): the flat// sheet a card is laid on, a line of a scene as the core read it, and that// line drawn the way a screenplay lays it. No face is declared here.import CottonPaperimport CottonValuesimport SwiftUIenum StoryCardKnobs {    static let corner: CGFloat = 14    static let inset = EdgeInsets(top: 22, leading: 22, bottom: 20, trailing: 22)    static let gap: CGFloat = 12    static let hairline: CGFloat = 1    /// How far down a card the scene's lines fade out, from the bottom.    static let fade: CGFloat = 56}/// A card's flat sheet: the sheet tone on the ground, a hairline round it,/// nothing painted.struct StorySheet: ViewModifier {    func body(content: Content) -> some View {        content            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)            .background(                RoundedRectangle(cornerRadius: StoryCardKnobs.corner, style: .continuous)                    .fill(StoryMaterial.sheet.colour)            )            .overlay(                RoundedRectangle(cornerRadius: StoryCardKnobs.corner, style: .continuous)                    .strokeBorder(StoryMaterial.hairline.colour, lineWidth: StoryCardKnobs.hairline)            )            .clipShape(RoundedRectangle(cornerRadius: StoryCardKnobs.corner, style: .continuous))    }}extension View {    func storySheet() -> some View { modifier(StorySheet()) }    /// Fades the bottom of a part that may run past the card, so a long    /// scene ends softly rather than cut through a line.    func storyFadesOut() -> some View {        mask(            VStack(spacing: 0) {                Rectangle()                LinearGradient(colors: [.black, .clear], startPoint: .top, endPoint: .bottom)                    .frame(height: StoryCardKnobs.fade)            }        )    }}/// One line of a scene as the core read it (`story.lines`): its kind and its/// words. The card carries them under `lines` (a list of `{kind, text}`);/// a card without them is drawn from its body, every line as action.public struct StoryLine: Equatable, Sendable {    public enum Kind: String, Sendable, Codable {        case action, character, dialogue, parenthetical, transition, heading, centered, note, blank    }    public let kind: Kind    public let text: String    public init(_ kind: Kind, _ text: String) {        self.kind = kind        self.text = text    }    /// The card's lines: `lines` as the core read them, else its body's    /// lines as action, blank lines kept.    static func of(_ card: DeckCard) -> [StoryLine] {        if case .list(let rows)? = card.fields["lines"] {            return rows.compactMap { row in                guard case .object(let fields) = row,                      let kind = fields["kind"]?.text.flatMap(Kind.init(rawValue:)) else { return nil }                return StoryLine(kind, fields["text"]?.text ?? "")            }        }        guard let body = card.text("body") else { return [] }        return body.components(separatedBy: "\n").map { line in            line.trimmingCharacters(in: .whitespaces).isEmpty ? StoryLine(.blank, "") : StoryLine(.action, line)        }    }    /// Lines as the fields a specimen card carries.    static func fields(_ lines: [StoryLine]) -> RecordValue {        .list(lines.map { .object(["kind": .text($0.kind.rawValue), "text": .text($0.text)]) })    }}/// A scene's lines laid out as a screenplay, small: names centred, speech in/// its narrower column, asides indented, cuts to the right.struct StoryScriptLines: View {    let lines: [StoryLine]    let size: CGFloat    var body: some View {        GeometryReader { box in            VStack(alignment: .leading, spacing: size * 0.25) {                ForEach(Array(lines.enumerated()), id: \.offset) { _, line in                    row(line, width: box.size.width)                }            }            .frame(width: box.size.width, alignment: .topLeading)        }    }    @ViewBuilder    private func row(_ line: StoryLine, width: CGFloat) -> some View {        let font = StoryType.script(size)        switch line.kind {        case .blank:            Color.clear.frame(height: size * 0.3)        case .character:            Text(verbatim: line.text)                .font(font)                .foregroundStyle(StoryMaterial.ink.colour)                .frame(width: width)                .multilineTextAlignment(.center)        case .dialogue:            Text(verbatim: line.text)                .font(font)                .foregroundStyle(StoryMaterial.ink.colour)                .frame(width: width * 0.72, alignment: .leading)                .padding(.leading, width * 0.14)        case .parenthetical:            Text(verbatim: line.text)                .font(font)                .foregroundStyle(StoryMaterial.quiet.colour)                .frame(width: width * 0.56, alignment: .leading)                .padding(.leading, width * 0.22)        case .transition:            Text(verbatim: line.text)                .font(font)                .foregroundStyle(StoryMaterial.ink.colour)                .frame(width: width, alignment: .trailing)        case .centered:            Text(verbatim: line.text)                .font(font)                .foregroundStyle(StoryMaterial.ink.colour)                .frame(width: width)                .multilineTextAlignment(.center)        case .heading:            Text(verbatim: line.text)                .font(font.bold())                .foregroundStyle(StoryMaterial.ink.colour)                .frame(width: width, alignment: .leading)        case .note:            Text(verbatim: line.text)                .font(font.italic())                .foregroundStyle(StoryMaterial.quiet.colour)                .frame(width: width, alignment: .leading)        case .action:            Text(verbatim: line.text)                .font(font)                .foregroundStyle(StoryMaterial.ink.colour)                .frame(width: width, alignment: .leading)        }    }}/// The small line at the top of a card: a scene's number, a status.struct StoryQuietLine: View {    let text: String    var body: some View {        Text(verbatim: text)            .font(StoryType.sans(.footnote, weight: .medium))            .foregroundStyle(StoryMaterial.quiet.colour)            .fixedSize(horizontal: false, vertical: true)    }}