Cotton code
131 lines · 5.3 KB · SwiftRaw
// Cotton Story's CHARACTER CARD (`character.card`, contract 17 Nov 2026): the// name large, who they are, what they are after, and how much they speak;// their picture if one is given, their initials in a ring otherwise.//// Slots: `title` (the name), `subtitle` (the role: who they are), `quote`// (what they want), `note` (how much they speak, in the core's words: "In 4// scenes, 23 lines"), `photo` (a picture, when given). Every word is the core's.import CottonPaperimport CottonValuesimport SwiftUIenum StoryCharacterCardKnobs {    static let portrait: CGFloat = 76}struct StoryCharacterCardFace: View {    let card: DeckCard    let picture: FacePicture?    init(card: DeckCard, picture: FacePicture?) {        self.card = card        self.picture = picture    }    var body: some View {        VStack(alignment: .leading, spacing: StoryCardKnobs.gap) {            StoryPortrait(name: card.text("title") ?? "", picture: picture)                .frame(width: StoryCharacterCardKnobs.portrait, height: StoryCharacterCardKnobs.portrait)                .facePart(.picture)            if let name = card.text("title") {                Text(verbatim: name)                    .font(StoryType.sans(.largeTitle, weight: .bold))                    .foregroundStyle(StoryMaterial.ink.colour)                    .fixedSize(horizontal: false, vertical: true)                    .accessibilityIdentifier("deck-title")                    .facePart(.title)            }            if let role = card.text("subtitle") {                Text(verbatim: role)                    .font(StoryType.sans(.body))                    .foregroundStyle(StoryMaterial.ink.colour)                    .fixedSize(horizontal: false, vertical: true)                    .accessibilityIdentifier("character-role")                    .facePart(.subtitle)            }            if let want = card.text("quote") {                HStack(alignment: .top, spacing: 10) {                    Rectangle().fill(StoryMaterial.hairline.colour).frame(width: 2)                    Text(verbatim: want)                        .font(StoryType.sans(.callout).italic())                        .foregroundStyle(StoryMaterial.quiet.colour)                        .fixedSize(horizontal: false, vertical: true)                }                .fixedSize(horizontal: false, vertical: true)                .accessibilityIdentifier("character-want")                .facePart(.body)            }            Spacer(minLength: 0)            if let speaks = card.text("note") {                StoryQuietLine(text: speaks)                    .accessibilityIdentifier("character-speaks")            }        }        .padding(StoryCardKnobs.inset)        .storySheet()        .accessibilityElement(children: .contain)        .accessibilityIdentifier("character-card")    }}/// A character's picture, or their initials in a hairline ring.struct StoryPortrait: View {    let name: String    let picture: FacePicture?    var body: some View {        if let picture {            picture                .clipShape(Circle())        } else {            ZStack {                Circle().fill(StoryMaterial.bubble.colour)                Text(verbatim: Self.initials(name))                    .font(StoryType.sans(.title2, weight: .semibold))                    .foregroundStyle(StoryMaterial.ink.colour)                    .minimumScaleFactor(0.5)                    .lineLimit(1)                    .padding(8)            }        }    }    /// Up to two initials, in whatever script the name is written in.    static func initials(_ name: String) -> String {        let words = name.split(whereSeparator: { $0.isWhitespace }).prefix(2)        return words.compactMap { $0.first.map(String.init) }.joined().uppercased()    }}/// The character card, and the cards it is reviewed in.public enum StoryCharacterCardFaceCatalogue {    public static let stock: FaceStock = .own    public static let ground: FaceGround = .blank    public static let face = Face(id: "character.card", specimens: [        Specimen("full", full), Specimen("named", named), Specimen("telugu", telugu),    ]) { card, picture, _ in        StoryCharacterCardFace(card: card, picture: picture)    }    public static let full = DeckCard(face: "character.card", corner: nil, pressing: false, pace: nil, plate: nil, fields: [        "title": .text("Meera"),        "subtitle": .text("The lighthouse keeper's daughter, back after ten years."),        "quote": .text("To find out why her father never sold the boat."),        "note": .text("In 6 scenes, 41 lines"),    ])    /// Just a name: the story has only mentioned them.    public static let named = DeckCard(face: "character.card", corner: nil, pressing: false, pace: nil, plate: nil, fields: [        "title": .text("Old Sailor"),        "note": .text("In 1 scene, 2 lines"),    ])    public static let telugu = DeckCard(face: "character.card", corner: nil, pressing: false, pace: nil, plate: nil, fields: [        "title": .text("రవి"),        "subtitle": .text("మీర తండ్రి, లైట్‌హౌస్ కాపరి."),        "quote": .text("పడవను ఎవరికీ ఇవ్వకూడదు."),        "note": .text("In 5 scenes, 30 lines"),    ])}