// Cotton Story's SCENE AS A CHAT (`scene.chat`, the owner's own idea; contract // 17 Nov 2026): the scene read as a conversation. Each speech is a bubble // under its speaker's name; in a two-hander the first speaker's bubbles sit // on the left and the other's on the right; with more voices every bubble // sits left under its name. Action is narration, centred between the // bubbles; an aside rides in its speech's bubble, quieter. Read-only: the // writer is the same writer whichever face the scene wears. // // Slots: `number`, `title` (the heading), `lines` (as the core read them) // or `body`. Every word is the core's. import CottonPaper import CottonValues import SwiftUI enum StorySceneChatKnobs { static let bubbleCorner: CGFloat = 18 static let bubblePad = EdgeInsets(top: 9, leading: 13, bottom: 9, trailing: 13) static let bubbleWidth: CGFloat = 0.78 static let gap: CGFloat = 10 } /// One beat of the conversation: a speech by a speaker, or narration. struct StoryChatBeat: Equatable { enum Side { case left, right } var speaker: String? var aside: String? var words: String var side: Side var narration: Bool /// The scene's lines folded into beats: a name, its asides and speech /// make one bubble; action and cuts make narration. static func of(_ lines: [StoryLine]) -> [StoryChatBeat] { let speakers = lines.filter { $0.kind == .character }.map { name(of: $0.text) } let voices = speakers.reduce(into: [String]()) { if !$0.contains($1) { $0.append($1) } } let twoHander = voices.count == 2 var beats: [StoryChatBeat] = [] var speaker: String? var aside: String? var speech: [String] = [] func close() { if let who = speaker, !speech.isEmpty { let side: Side = twoHander && who == voices[1] ? .right : .left beats.append(StoryChatBeat(speaker: who, aside: aside, words: speech.joined(separator: "\n"), side: side, narration: false)) } speaker = nil aside = nil speech = [] } for line in lines { switch line.kind { case .character: close() speaker = name(of: line.text) case .parenthetical: if speaker != nil, speech.isEmpty { aside = line.text } else if speaker != nil { speech.append(line.text) } case .dialogue: if speaker != nil { speech.append(line.text) } case .blank: close() case .action, .transition, .centered, .heading: close() if !line.text.isEmpty { if let last = beats.last, last.narration, beats.count > 0, line.kind == .action { beats[beats.count - 1].words += " " + line.text } else { beats.append(StoryChatBeat(speaker: nil, aside: nil, words: line.text, side: .left, narration: true)) } } case .note: continue } } close() return beats } /// A speaker's name without its extension: "MEERA (V.O.)" speaks as MEERA. static func name(of cue: String) -> String { let trimmed = cue.trimmingCharacters(in: .whitespaces) if let open = trimmed.firstIndex(of: "(") { return String(trimmed[..