//! Without Hex, then with it, in one Messages conversation: //! //! 1. The Launch Room's row in a Messages list dings and pulses with an //! unread badge; clicking it opens the conversation ([`super::inbox`]). //! 2. In the `"Launch Room"` group, Jay (CEO) has asked whether we're ready //! to ship, investors in the room. You type the answer by hand, faster as //! panic builds, and a finger slips onto Return: `"Yse, we're dead"`. //! Silence, then `"Read"`. Every fix makes it worse until Jay removes you //! from the group mid-word ([`typing::Scene`]). //! 3. `"With Hex"` in two beats: the disaster rewinds back to Jay's question, //! then the keyboard melts away to `⌥`, Hex's real `Dot scope` HUD //! (`rune_hud.wgsl`) rises, and the answer lands in about 1.5 seconds. //! Jay is delighted. //! 4. The score: what answering by hand cost against Hex's time. //! //! One thing has the eye at a time; the rest dims, deeply only when the //! story freezes. use crate::desktop::i18n::tr; use rune::{Anim, El, MotionIntent, Spring, SpringPair, TextStyle, Transition, col, el, row, text}; use super::super::controls::Paint; use super::cards::{ App, CHAT_W, Caption, Carry, FIELD_FONT, Look, Notice, Notify, OPEN_SPRING, Reply, Sender, Side, bubble, carried_bubble, chat, chat_height, chat_surface, field_text_origin, presenting, revealed, system_line, typing_dots, }; use super::field_text::{self, CHAT}; use super::fixture::Take; use super::flight::{Flight, flicker, flying, typeset}; use super::flows::continue_button; use super::hud_stage::voice_or_idle_slot; use super::inbox::{self, MATCHED}; use super::keyboard::{self, keyboard_lit}; use super::landing::{LEAD, UI_SPRING, keys, language_pick, ready, title_font}; use super::mic_line::mic_line; use super::pill::{after_flight, pill, slot}; use super::scene_script::{Script, fill}; use super::stories::{Flow, Onboard, Step}; use super::triumph::{self, HexBeats, Said}; use super::typing::{Focus, Glyph, Line, Scene, Timing, Who}; pub(crate) use stage_fx::VOICE_CALL_AT; use stage_fx::{camera, camera_focus, laptop_deck, laptop_lid, mode_call, mode_chip, slam}; mod stage_fx; use super::composition::{LAPTOP_BEZEL, Layout, STAGE_H, STAGE_W, keys_map}; const CENTRE_X: f32 = STAGE_W * 0.5; const CARD_X: f32 = CENTRE_X - CHAT_W * 0.5; const CARD_TOP: f32 = 0.0; /// Over the keyboard's upper rows, which melt away, above the lit `⌥`, where /// the keyboard sits today; [`hud_centre`] follows the layout. const HUD_CENTRE: [f32; 2] = [CENTRE_X, 416.0]; /// Where the HUD rises on the stage, in this run's layout. fn hud_centre() -> [f32; 2] { keys_map(Layout::chosen().keys(), HUD_CENTRE) } /// The keys behind the HUD go dark around it, this far out from its centre. const POCKET: [f32; 2] = [136.0, 58.0]; /// The HUD has collapsed away this long after the answer's release. const HUD_GONE: f64 = 0.74; /// How long the pocket takes to fill back in once the HUD has gone. const REFILL: f64 = 0.3; const HUD_SCALE: f32 = 1.05; const CALM: f32 = 0.20; const BURST: f64 = 0.04; const ACTION_H: f32 = 32.0; const TOP_BAR: f32 = 48.0; /// Room under the demo, kept small so the thread can be tall. const BOTTOM: f32 = 24.0; /// The last few lines, while they aren't the focus. const RECENT: f32 = 0.80; /// Older lines. const OLDER: f32 = 0.65; /// Everything but the focus while the story freezes. const FROZEN: f32 = 0.28; /// The message being answered while you type. const CONTEXT: f32 = 0.9; /// How many of the newest lines read as one stack, at [`RECENT`]. const STACK: usize = 5; /// The thread behind the draft in slow motion. const SLOW_THREAD: f32 = 0.42; /// How long a line that cuts in while you type stays at full brightness. const CUT_IN: f64 = 0.8; /// `"Without Hex"`'s field holds one line. const WITHOUT_LINES: usize = 1; /// From the stage settling to the button's clock: Jay's question arrives /// as the stage settles ([`typing::Scene`]'s lines before the button). const PRE_ROLL: f64 = 1.2; /// `"With Hex"`'s first beat: the disaster zips back out, newest first. pub(crate) const REWIND: f64 = 0.48; const REWIND_STAGGER: f64 = 0.045; /// The rewind keeps the field locked until the removal has peeled away. const UNLOCK: f64 = 0.1; /// How long the field stays empty after a send, before its placeholder. const CLEAR: f64 = 0.22; /// The second beat: the keyboard melts to `⌥` and the HUD rises; then `⌥` /// goes down this long after `"With Hex"` begins. pub(crate) const BEFORE_KEY: f64 = 1.5; const MELT: f64 = 0.9; /// The keys start melting as the rewind settles, so the keyboard never /// brightens on its way out. const MELT_FROM: f64 = 0.1; /// The HUD rises once the keys have mostly melted out of its way. const HUD_RISES: f64 = MELT_FROM + MELT * 0.6; /// Keys other than the shortcut once they've melted away. const MELTED: f32 = 0.14; /// The keyboard while Jay talks, and before the button. const IDLE: f32 = 0.52; /// The keyboard while you type. const TYPING: f32 = 0.92; /// The shortcut's ember on the score. const EMBER: f32 = 0.22; /// How long a landed word takes to settle into the field. const SETTLE: f64 = 0.15; /// The scene this run plays; it's chosen once, so the stage's geometry can /// be worked out without an [`Onboard`]. fn script() -> &'static Script<'static> { Script::chosen() } /// Your spoken line in `"With Hex"`. fn take() -> &'static Take { script().take() } fn reply_lines() -> usize { CHAT.wrap(take().text()).len() } fn card_bottom(lines: usize) -> f32 { CARD_TOP + chat_height(lines) } fn field_origin() -> [f32; 2] { let lines = reply_lines(); field_text_origin([CARD_X, card_bottom(lines)], lines) } /// Seconds into `"Without Hex"`, once it has started. fn scene_time(s: &Onboard) -> Option { s.typing_from.map(|from| s.now - from) } /// The thread's time: [`scene_time`], or before the button, Jay's question /// arriving as the stage settles. fn thread_time(s: &Onboard) -> Option { scene_time(s).or_else(|| { s.parked_from .map(|from| (s.now - from - PRE_ROLL).min(-1e-3)) }) } fn scene_over(s: &Onboard) -> bool { scene_time(s).is_some_and(|t| t >= s.scene.duration()) } /// `"Without Hex"` is waiting for the Launch Room's row. pub(crate) fn waiting_to_start(s: &Onboard) -> bool { s.step == Step::Parked && s.typing_from.is_none() } /// When each of `take`'s words is heard after the shortcut. fn heard_for(take: &'static Take, streams: bool) -> Vec { let count = take.text().split_whitespace().count(); (0..count) .map(|i| { if streams { take.updates .iter() .find(|(_, partial)| partial.split_whitespace().count() > i) .map_or(take.release, |(at, _)| *at) } else { take.release + i as f64 * BURST } }) .collect() } fn flights_from(streams: bool, from: f64) -> Vec<(Flight, field_text::Slot<'static>)> { flights_for(take(), streams, from) } fn flights_for( take: &'static Take, streams: bool, from: f64, ) -> Vec<(Flight, field_text::Slot<'static>)> { let origin = field_origin(); // The words fly in the conversation's own space, out of the HUD. let hud = Layout::chosen().card().unmap(hud_centre()); CHAT.slots(take.text()) .into_iter() .zip(heard_for(take, streams)) .enumerate() .map(|(i, (slot, at))| { let to = [origin[0] + slot.centre[0], origin[1] + slot.centre[1]]; ( Flight::new(hud, to, from + at, CALM, FIELD_FONT.size, i), slot, ) }) .collect() } /// When each of the answer's words leaves the HUD, from the shortcut. pub(crate) fn launches(streams: bool) -> Vec { heard_for(take(), streams) } fn flights(s: &Onboard) -> Vec<(Flight, field_text::Slot<'static>)> { s.take_from .map_or_else(Vec::new, |from| flights_from(s.streams(), from)) } /// `"With Hex"`'s beats after the shortcut goes down: the answer sends once /// its words have landed, then the scene's cheers ([`HexBeats::plan`]). pub(crate) fn hex_beats(streams: bool, timing: &Timing) -> HexBeats { HexBeats::plan(script(), send_at(take(), streams, timing), timing) } /// When `take` leaves the field after the shortcut: once its words land. pub(crate) fn send_at(take: &'static Take, streams: bool, timing: &Timing) -> f64 { let landed = flights_for(take, streams, 0.0) .iter() .map(|(f, _)| f.arrives()) .fold(take.release, f64::max); landed + timing.hex_send } /// From `Heard` to the score. /// When `"With Hex"` has finished, from the shortcut going down. pub(crate) fn hex_done_secs(s: &Onboard) -> f64 { hex_beats(s.streams(), &s.timing).done } pub(crate) fn reply_secs(s: &Onboard) -> f64 { hex_beats(s.streams(), &s.timing).done - (take().release + 0.1) } /// Seconds since the shortcut went down on `"With Hex"`. fn take_time(s: &Onboard) -> Option { matches!(s.step, Step::Hearing | Step::Heard | Step::Done) .then_some(s.take_from) .flatten() .map(|from| s.now - from) } /// How long the comedy plays before `Skip →` shows, on a first watch. const SKIP_AFTER: f64 = 2.0; /// The comedy is playing and `Skip →` shows: two seconds in, or at once /// for someone who has seen it before, until the answer sends. pub(super) fn skippable(s: &Onboard) -> bool { let Some(from) = s.typing_from else { return false; }; // Once the answer is out there's nothing left to skip, but for the // first run's celebration on the way to the microphone. let playing = matches!(s.step, Step::Parked | Step::YourTurn | Step::Hearing) && !hex_sent(s) || s.flow == Flow::FirstRun && s.step == Step::Heard; playing && (s.had_model && s.granted || s.now - from >= SKIP_AFTER) } /// The camera is pushed in on the draft (autocorrect's slow motion), the /// stage larger than its place on the page. pub(super) fn pushed_in(s: &Onboard) -> bool { s.step == Step::Parked && scene_time(s).is_some_and(|t| camera(s, t).0 > 1.0) } /// `"With Hex"`'s answer has left the field. pub(crate) fn hex_sent(s: &Onboard) -> bool { take_time(s).is_some_and(|t| t >= hex_beats(s.streams(), &s.timing).send) } /// How many `"Without Hex"` replies have been sent. pub(super) fn sent_count(s: &Onboard) -> usize { match scene_time(s) { Some(t) if s.step == Step::Parked => s.scene.sent(t), _ => 0, } } pub(super) fn view(p: Paint, s: &Onboard) -> Vec { if s.step == Step::Ready { return vec![ top_bar() .justify_center() .child(pill(p, download(s), s.closed)), middle().pb(TOP_BAR).child(ready( p, &s.caps, s.toggle, None, s.ready_age(), s.reduced.get(), s.motion, s.home, )), ]; } let parked = s.step >= Step::Parked; let top = top_bar().justify_end().child(if parked { pill(p, download(s), s.closed) } else { slot() }); let act = match s.step { Step::Pick => language_pick(p, Onboard::pick), Step::Started => pill(p, download(s), s.closed), Step::Heard if !s.downloaded() => text(tr("Finishing the download…")) .key("finishing") .font(p.k.ty.small) .color(p.faint()) .transition(Transition::opacity()), Step::Done => continue_button(p, s), _ if skippable(s) => super::flows::skip_button(p, s), _ => el().key("none"), }; vec![ top, middle() .pb(BOTTOM) .gap(16.0) .child(heading(p, s)) .children(parked.then(|| stage(p, s))) .child( col() .key("try") .gap(10.0) .items_center() .child(row().key("action").h(ACTION_H).center().child(act)) .child(mic_line( p, s.can_hear() && s.step >= Step::YourTurn, s.listening_peak(), )), ), ] } fn top_bar() -> El { row().key("top-bar").w_full().h(TOP_BAR).px(20.0).center_y() } fn middle() -> El { col().key("middle").grow(1.0).shrink().w_full().center() } fn download(s: &Onboard) -> super::fixture::Download { s.download.expect("the download starts at the pick") } pub(super) fn heading(p: Paint, s: &Onboard) -> El { let line = |key: &str, copy: String| -> El { text(copy) .key(key.to_string()) .font(LEAD) .color(p.muted()) .transition(Transition::opacity()) }; let hold = || { row() .key("hold") .gap(8.0) .center_y() .transition(Transition::opacity()) .child(text("Hold").key("hold").font(LEAD).color(p.muted())) .child(keys(p, "keys", &s.caps, s.step == Step::Hearing)) .child(text("and say it.").key("rest").font(LEAD).color(p.muted())) }; let (key, title, lead) = match s.step { Step::Pick | Step::Started => ( "intro", tr("Talk instead of typing").to_string(), line( "lead-intro", tr("Hold a key and talk. Hex types it wherever your cursor is.").into(), ), ), // The chip says it all; the line under it stays, empty, so the // stage doesn't move when `"With Hex"` brings its own. Composed in // two lines (the playground's layouts only, so English), it says what you // do, as `"With Hex"`'s does. Step::Parked if !scene_over(s) || s.demo_hears() => ( "without", tr("Without Hex").to_string(), if Layout::chosen().two_lines() { line("lead-type", "Type it by hand.".to_string()) } else { el().key("lead-none") }, ), Step::Parked => ( "with", tr("With Hex").to_string(), line( "lead-wait", tr("Hex starts listening once the speech model has downloaded.").into(), ), ), Step::YourTurn | Step::Hearing | Step::Heard => { ("with", tr("With Hex").to_string(), hold()) } _ if s.flow == Flow::FirstRun || s.flow == Flow::Versus => { let with = &script().with; let filled = |template| { fill( template, take().release, s.scene.score_secs(), s.scene.typos(), ) }; ( "score", filled(with.score), line("lead-score", filled(with.typed)), ) } _ => ( "heard", "That's it".to_string(), line("lead-done", "The same reply, typed and then spoken.".into()), ), }; let mode = match key { "without" => Some(false), "with" => Some(true), _ => None, }; if Layout::chosen().split() { return split_heading(p, s, key, title, lead, mode); } // The first run's download flies up past it from "Setting up Hex". after_flight(col(), s) .key(key) .gap(8.0) .items_center() .transition(act_break()) .enter_delay(HEADING_GAP) .child( row() .key("title-row") .gap(12.0) .center_y() .child(text(title).key("title").font(title_font(p)).color(p.text())) .children(mode.map(|voice| mode_chip(p, voice))), ) .child(row().key("lead-row").h(24.0).center().child(lead)) } /// The split layout's heading, a column left of the stage: the mode, a big /// title, then what you do. fn split_heading( p: Paint, s: &Onboard, key: &str, title: String, lead: El, mode: Option, ) -> El { let big = TextStyle { size: 44.0, line_height: 50.0, ..title_font(p) } .tracking(-0.8); after_flight(col(), s) .key(key.to_string()) .w(SPLIT_HEADING_W) .gap(14.0) .items_start() .transition(act_break()) .enter_delay(HEADING_GAP) .children(mode.map(|voice| mode_chip(p, voice))) .child(text(title).key("title").font(big).color(p.text()).wrap()) .child(row().key("lead-row").min_h(24.0).child(lead)) } /// The split layout's heading column. pub(super) const SPLIT_HEADING_W: f32 = 320.0; /// How long the outgoing heading has alone before the next sharpens in. const HEADING_GAP: f32 = 0.09; /// One heading gives way to the next in place: the outgoing one blurs away /// fast, mostly gone before the incoming one sharpens in, so two titles /// never sit on each other. fn act_break() -> Transition { let soft = |scale| Transition::blur(3.5).combined(Transition::scale(scale)); Transition::asymmetric(soft(0.98), soft(1.02)).springs(SpringPair::new( Spring::new(0.28, 0.0), Spring::new(0.14, 0.0), )) } fn smoothstep(u: f32) -> f32 { let u = u.clamp(0.0, 1.0); u * u * (3.0 - 2.0 * u) } /// Seconds into `"With Hex"`, from the rewind. fn hex_time(s: &Onboard) -> Option { (s.step >= Step::YourTurn) .then_some(s.voice_from) .flatten() .map(|from| s.now - from) } /// When the Launch Room was clicked open, or came on screen open. fn opened_at(s: &Onboard) -> Option { let lead_in = if s.in_place { s.scene.lead_in() } else { s.timing.opening }; s.typing_from.map(|from| from - lead_in) } /// Where the Launch Room's row waits on the stage. const INBOX_TOP: f32 = 170.0; const INBOX_AT: [f32; 2] = [CENTRE_X - inbox::ROW_W * 0.5, INBOX_TOP]; /// How long the row takes to grow into the conversation: its width on /// `snap`, its height on [`OPEN_SPRING`] with the parts that fly over. const OPEN_SECS: f64 = 0.75; const WIDTH_SPRING: f64 = 0.28; /// How far what doesn't fly over from the row has faded in, `age` seconds /// into the opening: it waits for the flights to clear its place. fn reveal(age: f64) -> f32 { let u = ((age - 0.24) / 0.3).clamp(0.0, 1.0) as f32; u * u * (3.0 - 2.0 * u) } /// The group's faces in the conversation's header, each this wide. const HEADER_FACE: f32 = 17.0; /// Seconds since the conversation opened, while the row still grows into /// it. Reduce Motion opens it with a fade instead. fn opening(s: &Onboard) -> Option { opened_at(s) .map(|at| s.now - at) .filter(|&age| age < OPEN_SECS && s.step == Step::Parked && !s.reduced.get() && !s.in_place) } /// Whether the row finished growing into the conversation after `from` /// and by `to`. pub(super) fn grown(s: &Onboard, from: f64, to: f64) -> bool { opened_at(s).is_some_and(|at| from < at + OPEN_SECS && at + OPEN_SECS <= to) } /// A zero-bounce spring's progress `t` seconds after it starts. fn settled(t: f64, response: f64) -> f32 { if t <= 0.0 { return 0.0; } let w = std::f64::consts::TAU / response; (1.0 - (1.0 + w * t) * (-w * t).exp()) as f32 } /// The surface the pressed row grows into the conversation on, `age` /// seconds after the click: wider first, then taller, its centre on one /// straight line. It lands exactly on the conversation's own surface. /// Otherwise it is there but empty: taking it out remounts the card. fn plate(p: Paint, age: Option) -> El { // It grows out and empties again, its width leading. let intent = MotionIntent::RECOIL | MotionIntent::IMPULSE; let Some(age) = age else { return el() .key("plate") .absolute(0.0, 0.0) .instant_changes() .motion_intent(intent); }; let wide = settled(age, WIDTH_SPRING); let tall = settled(age, OPEN_SPRING.response() as f64); let from = [inbox::ROW_W * inbox::PRESSED, inbox::ROW_H * inbox::PRESSED]; let to = [CHAT_W, chat_height(WITHOUT_LINES)]; let w = from[0] + (to[0] - from[0]) * wide; let h = from[1] + (to[1] - from[1]) * tall; let from_y = INBOX_TOP + inbox::ROW_H * 0.5; let to_y = CARD_TOP + to[1] * 0.5; let y = from_y + (to_y - from_y) * tall; chat_surface( p, el().key("plate") .absolute(CENTRE_X - w * 0.5, y - h * 0.5) .size(w, h) .instant_changes() .motion_intent(intent) .transition(Transition::identity()), ) } /// How the Launch Room's conversation looks, growing out of its row while /// `opening`. fn look(p: Paint, opening: Option) -> Look { Look { icon: Some(inbox::group_faces(p, HEADER_FACE)), matched: Some([MATCHED[0], MATCHED[1]]), bare: opening.is_some(), reveal: opening.map(reveal), } } pub(super) fn stage(p: Paint, s: &Onboard) -> El { let since_parked = s.parked_from.map_or(0.0, |from| s.now - from); if waiting_to_start(s) { return el() .key("stage") .size(STAGE_W, STAGE_H) // Unwrapped, so its own quick exit is the one that plays. .child(inbox::launch_row(p, s, since_parked).absolute(INBOX_AT[0], INBOX_AT[1])) .children(inbox::pointer( INBOX_AT, since_parked, None, s.reduced.get(), )); } let opening = opening(s); let (kw, kh) = keyboard::size(); let voice_phase = s.step >= Step::YourTurn; let reduced = s.reduced.get(); let t = scene_time(s); let focus = t.map_or(Focus::Waiting, |t| s.scene.focus(t)); let frozen = t.is_some_and(|t| s.scene.frozen(t)); let since = hex_time(s); // A single zero-bounce spring carries the deck and its key sweep. let enter_u = if reduced { 1.0 } else { opened_at(s) .or(s.parked_from) .map_or(1.0, |from| s.motion.keyboard_progress(s.now - from)) }; // As the rewind settles, every key but the shortcut melts away. let melt_u = match since { Some(_) if reduced => 1.0, Some(since) => smoothstep(((since - MELT_FROM) / MELT) as f32), None => 0.0, }; let flash = |id: &str| match t { Some(t) if !voice_phase => s.scene.flash(t, id), _ => 0.0, }; // From where the removal left it, the deck only ever goes down as its // keys melt; the shortcut alone lights up, and the keys behind the HUD // go dark. let level = match since { Some(_) => FROZEN + (MELTED - FROZEN) * melt_u, None => keyboard_level(s), }; // Once the HUD has gone, the keys behind it fill back in. let pocket_u = melt_u * (1.0 - take_time(s).map_or(0.0, |take| { smoothstep(((take - self::take().release - HUD_GONE) / REFILL) as f32) })); let deck = [(STAGE_W - kw) * 0.5, STAGE_H - kh]; let pocket = [HUD_CENTRE[0] - deck[0], HUD_CENTRE[1] - deck[1]]; let kb_el = keyboard_lit( p, "keyboard", keyboard::Lighting { enter: enter_u, level, depth: &|id| key_depth(s, id), glow: &|id| key_glow(s, id, melt_u), flash: &flash, hollow: &|[x, y, w, h]| { let dx = (x + w * 0.5 - pocket[0]) / POCKET[0]; let dy = (y + h * 0.5 - pocket[1]) / POCKET[1]; pocket_u * (1.0 - smoothstep(((dx * dx + dy * dy).sqrt() - 1.0) / 0.35)) }, }, ); let kb_shift = (1.0 - enter_u) * 14.0 + melt_u * 6.0; let shudder = match t { Some(t) if !reduced && !voice_phase => s.scene.shudder(t), _ => [0.0, 0.0], }; let layout = Layout::chosen(); let keys_place = layout.keys(); let keys_el = el() .key("keys") .transition( Transition::opacity() .springs(SpringPair::new(UI_SPRING, Spring::new(s.motion.exit, 0.0))), ) .absolute(deck[0], deck[1]) .offset( shudder[0] + keys_place.at[0], kb_shift + shudder[1] + keys_place.at[1], ) .size(kw, kh) .offset_motion(Anim::INSTANT); let keys_el = if keys_place.is_identity() { keys_el } else { keys_el.origin(0.5, 0.0).scale(keys_place.k) } .child(kb_el.motion_intent(MotionIntent::STEP)); // Real `Dot scope` phosphor HUD (`rune_hud.wgsl`) during `With Hex`: it // unfolds as it rises into the space the keys melted out of, above `⌥`. let hud_el = since .filter(|&since| reduced || since >= HUD_RISES) .and_then(|_| { voice_or_idle_slot( "onboard-dot-hud", hud_centre(), HUD_SCALE * keys_place.k, s.step == Step::YourTurn, take(), s.take_from, s, ) .0 }) .map(|hud| hud.transition(s.motion.roll(10.0))); let nudge = landing_nudge(s); let knock = match t { Some(t) if !reduced && !voice_phase => s.scene.knock(t), _ => 0.0, }; // One card throughout, so `"With Hex"` rewinds the same thread. let card = match since { Some(since) => with_hex_card(p, s, since), None => without_hex_card(p, s, focus, frozen, opening), }; // It grows out of the row rather than fading in, unless Reduce Motion; // on screen open, it rises in under the heading. let fade = Transition::opacity(); let enter = if reduced { fade } else if s.in_place { Transition::opacity() .combined(Transition::offset(0.0, 14.0)) .combined(Transition::scale(0.98)) } else { Transition::identity() }; let card = el() .key("card") .absolute(CARD_X, CARD_TOP) .w(CHAT_W) .transition(Transition::asymmetric(enter, fade).springs(SpringPair::new( Spring::new(0.3, 0.0), Spring::new(0.16, 0.0), ))) .child( card.offset(nudge[0] + knock, nudge[1]) .offset_motion(Anim::INSTANT) // Each landing word, or a blow, knocks the card: kicks. .motion_intent(if nudge != [0.0, 0.0] || knock != 0.0 { MotionIntent::KICK } else { MotionIntent::NONE }), ); // The pointer stays to lift off the row it clicked, unless the user's // own click opened it first. let clicked = |at: f64| { s.parked_from .is_some_and(|from| at - from >= inbox::CLICK_AT - 0.05) }; let pointer = opened_at(s) .filter(|&at| clicked(at)) .and_then(|at| inbox::pointer(INBOX_AT, since_parked, Some(s.now - at), reduced)); let (zoom, shake) = match t { Some(t) if !voice_phase => camera(s, t), _ => (1.0, [0.0, 0.0]), }; // The conversation, and the words flying into it, as one group the // layout places. let card_place = layout.card(); let conversation = [ Some(plate(p, opening)), Some(card), pointer, Some(words(p, s, nudge)), ] .into_iter() .flatten(); let camera = el() .key("camera") .absolute(0.0, 0.0) .size(STAGE_W, STAGE_H) .children((layout == Layout::Laptop).then(|| laptop_deck(p))) .child(keys_el) .children(hud_el) .children((layout == Layout::Laptop).then(|| laptop_lid(p))); let camera = if card_place.is_identity() { camera.children(conversation) } else { camera.child( el().key("conversation") .absolute(0.0, 0.0) .size(STAGE_W, STAGE_H) .origin(0.5, 0.0) .scale(card_place.k) .offset(card_place.at[0], card_place.at[1]) .children(conversation), ) }; // The push in and the blow's shake are the camera's own; at rest it // leaves the stage untouched. let camera = if zoom != 1.0 || shake != [0.0, 0.0] { let focus_at = camera_focus(); camera .origin(focus_at[0] / STAGE_W, focus_at[1] / STAGE_H) .scale(zoom) .scale_motion(Anim::INSTANT) .offset(shake[0], shake[1]) .offset_motion(Anim::INSTANT) .motion_intent(MotionIntent::KICK) } else { camera }; // The fireworks burst behind the conversation and light it. let celebration = take_time(s).map_or_else( || { super::fireworks::publish(None); triumph::Celebration::default() }, |t| { triumph::effects( p, &hex_beats(s.streams(), &s.timing), t, [STAGE_W, STAGE_H], reduced, ) }, ); el().key("stage") .size(STAGE_W, STAGE_H) .children(celebration.behind) .child(camera) .children(t.filter(|_| !voice_phase).and_then(|t| slam(p, s, t))) .children(mode_call(p, s)) .children(celebration.over) } /// Caps Lock's light once it's on. const CAPS_LIGHT: f32 = 0.9; /// The red pill in the header, from the first frame, flaring at scene /// time `t`. fn banner(p: Paint, s: &Onboard, scene: &Scene, t: f64) -> Option { scene .script() .pill .map(|copy| presenting(p, copy, s.now, scene.flare(t))) } /// `script`'s conversation header, named `title`. fn room(script: &'static Script<'static>, title: &'static str) -> Notice { Notice { key: "room", app: App::Messages, from: title, person: None, place: script.group, body: "", field: "iMessage", } } /// Who sent one of theirs in a group: a name over the first of a run and a /// face beside the last. fn sender( script: &'static Script<'static>, who: u8, first: bool, last: bool, ) -> Option> { script.group.is_some().then(|| { let person = script.person(who); Sender { name: person.name, colour: person.colour, first, last, } }) } fn caption(script: &'static Script<'static>, who: u8, first: bool, last: bool) -> Caption<'static> { sender(script, who, first, last).map_or(Caption::None, Caption::From) } /// The boss typing, beside their face in a group. fn boss_typing(p: Paint, script: &'static Script<'static>, now: f64, reduced: bool) -> El { typing_dots( p, "dots".into(), now, 1.0, reduced, sender(script, 0, false, true), ) } /// `"Without Hex"`'s thread at `t`: every line so far, each at /// `opacity(line index, line)` with `"Read"` under the one it reads, and /// Jay's typing dots. /// The thread at scene time `t`. While the conversation opens out of its /// row, `reveal` holds back every line but Jay's question, which flies over /// from the row. fn thread_at( p: Paint, s: &Onboard, scene: &Scene, t: f64, reveal: Option, opacity: impl Fn(usize, &Line) -> f32, ) -> Vec { let reduced = s.reduced.get(); let script = scene.script(); let lines = &scene.lines()[..scene.shown(t)]; let asked = scene.lines().iter().rposition(|l| l.at < 0.0); let held = |line: El, o: f32| revealed(line, reveal.map(|reveal| o * reveal)); let receipt_at = scene.read(t); let mut thread = Vec::new(); let mut sent = 0; for (i, line) in lines.iter().enumerate() { let o = opacity(i, line); let taps: Vec = scene .taps(t) .filter(|tap| tap.line == i) .map(|tap| tap.glyph) .collect(); thread.push(match line.who { Who::Them(who) => { // The sender's name over the first of a run, as in a group, // and their face beside the last, unless they're typing more. let first = i == 0 || lines[i - 1].who != line.who; let last = match lines.get(i + 1) { Some(next) => next.who != line.who, None => who != 0 || scene.dots(t).is_none(), }; let carry = reveal.filter(|_| Some(i) == asked).map(|reveal| Carry { id: MATCHED[2], reveal, }); let line = carried_bubble( p, format!("l{i}"), Side::Them, caption(script, who, first, last), &line.text, &taps, o, reduced, carry, ); if carry.is_some() { line } else { held(line, o) } } Who::You => { sent += 1; bubble( p, format!("sent-bubble-{}", sent - 1), Side::You, if receipt_at == Some(i) { Caption::Read } else { Caption::None }, &line.text, &taps, o, reduced, ) } Who::System => system_line(p, format!("l{i}"), &line.text, Notify::Danger, o, reduced), Who::Status => system_line(p, format!("l{i}"), &line.text, Notify::Status, o, reduced), }); } if scene.dots(t).is_some() { thread.push(held(boss_typing(p, script, s.now, reduced), 1.0)); } thread } /// The conversation as `"Without Hex"` stands now, Jay's question already /// there before the button. fn without_hex_card(p: Paint, s: &Onboard, focus: Focus, frozen: bool, opening: Option) -> El { let t = thread_time(s).unwrap_or(-1e-3); scene_card(p, s, &s.scene, t, focus, frozen, opening, look(p, opening)) } /// A sketch's conversation at its time `t`, typed by hand. pub(super) fn sketch_card(p: Paint, s: &Onboard, scene: &Scene, t: f64) -> El { let focus = if t < 0.0 { Focus::Waiting } else { scene.focus(t) }; let look = Look { icon: None, matched: None, bare: false, reveal: None, }; scene_card(p, s, scene, t, focus, scene.frozen(t), None, look) } /// `scene`'s conversation at its time `t`. #[allow(clippy::too_many_arguments)] fn scene_card( p: Paint, s: &Onboard, scene: &Scene, t: f64, focus: Focus, frozen: bool, opening: Option, look: Look, ) -> El { let shown = scene.shown(t); let answering = scene.lines()[..shown] .iter() .rposition(|l| matches!(l.who, Who::Them(_))); let removed = scene.removed(t); let slow = scene.slow(t) > 0.0; let opacity = |i: usize, line: &Line| match focus { Focus::Waiting => 1.0, Focus::Line(j) | Focus::Read(j) if i == j => 1.0, // Jay's last two lines stay readable over the removal: the firing // reads as one stack. _ if removed && i + 2 == shown => 0.82, _ if removed && i + 3 == shown => 0.55, // In slow motion the draft has the eye, but the stack stays legible. _ if slow => SLOW_THREAD, _ if frozen => FROZEN, // Someone cutting in while you type lands bright, then settles. Focus::Field if t - line.at < CUT_IN => 1.0, Focus::Field if Some(i) == answering => CONTEXT, _ if shown - i <= STACK => RECENT, _ => OLDER, }; let thread = thread_at(p, s, scene, t, opening.map(reveal), opacity); let room = room(scene.script(), scene.title(t)); if removed { let locked = scene.locked(); return chat( p, &room, banner(p, s, scene, t), thread, Reply::Locked(&locked), None, 1.0, look, ); } let (draft, caret) = scene.draft(t); // The field stays empty a moment after a send, so the bubble lifts out // of it before the placeholder comes back. let just_sent = scene.lines()[..shown] .iter() .rev() .find(|l| l.who == Who::You) .is_some_and(|l| t - l.at < CLEAR); chat( p, &room, banner(p, s, scene, t), thread, Reply::Lines { sent_bubbles: &[], text: draft, caret, lines: WITHOUT_LINES, placeholder: draft.is_empty() && !just_sent, focus: caret.is_some(), sent: false, }, scene.mark(t), if focus == Focus::Field { 1.0 } else { 0.6 }, look, ) } /// The same question, answered by voice, `since` seconds into `"With Hex"`. /// It opens on the rewind: the disaster zips back out, newest first, until /// Jay's question is all that's left. fn with_hex_card(p: Paint, s: &Onboard, since: f64) -> El { let reduced = s.reduced.get(); let beats = hex_beats(s.streams(), &s.timing); let t = take_time(s); let at = |when: f64| t.is_some_and(|t| t >= when); let sent = at(beats.send); let replied = beats.lines.first().is_some_and(|&(when, _)| at(when)); let typing = beats .dots .is_some_and(|(from, until)| at(from) && !at(until)); let composing = !sent; let asked = s.scene.lines().iter().filter(|l| l.at < 0.0).count(); let rewinding = since < REWIND && !reduced; let mut thread = if rewinding { let end = s.scene.duration(); let mut all = thread_at( p, s, &s.scene, end, None, |i, _| if i < asked { 1.0 } else { 0.5 }, ); let gone = ((since / REWIND_STAGGER) as usize + 1).min(all.len() - asked); all.truncate(all.len() - gone); all } else { let question = if composing { 1.0 } else { OLDER }; let mut thread = thread_at(p, s, &s.scene, -1e-3, None, |_, _| question); // Jay typing at the question gives way to the reply's own beats // once it's sent: one set of dots, one face. if sent && s.scene.dots(-1e-3).is_some() { thread.pop(); } thread }; if sent { let taps: Vec = beats .taps .iter() .filter(|&&(when, _)| at(when)) .map(|&(_, glyph)| glyph) .collect(); thread.push(bubble( p, "hex-bubble".into(), Side::You, Caption::None, take().text(), &taps, if replied { RECENT } else { 1.0 }, reduced, )); } if typing { thread.push(boss_typing(p, script(), s.now, reduced)); } let cheers: Vec<_> = beats .lines .iter() .enumerate() .filter(|(_, (when, _))| at(*when)) .collect(); let by = |said: &Said| match *said { Said::Them(who, _) => Some(who), _ => None, }; let mut last: Option = None; for (n, &(i, (_, said))) in cheers.iter().enumerate() { let key = format!("cheer-{i}"); thread.push(match *said { Said::Them(who, copy) => { let named = last != Some(who); last = Some(who); let ends = cheers .get(n + 1) .is_none_or(|(_, (_, next))| by(next) != Some(who)); bubble( p, key, Side::Them, caption(script(), who, named, ends), copy, &[], 1.0, reduced, ) } Said::You(copy) => { last = None; bubble(p, key, Side::You, Caption::None, copy, &[], 1.0, reduced) } Said::Pill(ref copy) => { last = None; system_line(p, key, copy, Notify::Cheer, 1.0, reduced) } }); } let room = room( script(), beats.title(t.unwrap_or(f64::NEG_INFINITY), script().title), ); let flights = flights(s); let started = flights.iter().any(|(f, _)| s.now >= f.start()); // The field grows a line only once a word heads for it. let lines = flights .iter() .filter(|(f, _)| s.now >= f.start()) .map(|(_, slot)| slot.line + 1) .max() .unwrap_or(1); if rewinding && since < UNLOCK { let locked = s.scene.locked(); return chat( p, &room, banner(p, s, &s.scene, -1e-3), thread, Reply::Locked(&locked), None, 1.0, look(p, None), ); } // The field waits, unfocused, until the shortcut goes down. let listening = s.step >= Step::Hearing && !sent; let cleared = t.is_some_and(|t| t >= beats.send + CLEAR); // The pill can change, or go, as the payoff lands. let banner = match beats.pill(t.unwrap_or(f64::NEG_INFINITY), script().pill) { (Some(copy), flare) => Some(presenting(p, copy, s.now, flare)), (None, _) => None, }; chat( p, &room, banner, thread, Reply::Lines { sent_bubbles: &[], text: "", caret: None, lines: if sent { 1 } else { lines }, placeholder: !started || cleared, focus: listening, sent: false, }, None, if composing { 1.0 } else { 0.6 }, look(p, None), ) } /// What the keyboard is lit to at scene time `t` in `"Without Hex"`: up /// while you type and through Return's flash, back while Jay talks, and down /// only when the story freezes. fn keyboard_target(s: &Onboard, t: f64) -> f32 { if t < 0.0 { return IDLE; } let scene = &s.scene; if scene.focus(t) == Focus::Field || scene.flash(t, "return") > 0.0 { TYPING } else if scene.frozen(t) { FROZEN } else { IDLE } } /// [`keyboard_target`] followed by a critically damped spring, worked out /// from the scene's clock over the last second so any frame can be drawn /// on its own. fn keyboard_level(s: &Onboard) -> f32 { const RESPONSE: f64 = 0.36; const STEP: f64 = 1.0 / 240.0; let Some(t) = scene_time(s) else { return IDLE; }; let omega = std::f64::consts::TAU / RESPONSE; let mut at = t - 1.0; let mut level = f64::from(keyboard_target(s, at)); let mut speed = 0.0; while at < t { let goal = f64::from(keyboard_target(s, at)); speed += (omega * omega * (goal - level) - 2.0 * omega * speed) * STEP; level += speed * STEP; at += STEP; } level as f32 } fn shortcut_keys(s: &Onboard) -> impl Iterator + '_ { s.caps.iter().filter_map(|cap| keyboard::modifier(cap)) } fn key_depth(s: &Onboard, id: &str) -> f32 { if s.step == Step::Hearing && shortcut_keys(s).any(|k| k == id) { return 1.0; } match (s.step, scene_time(s)) { (Step::Parked, Some(t)) if t >= 0.0 => s.scene.depth(t, id), _ => 0.0, } } fn key_glow(s: &Onboard, id: &str, melt_u: f32) -> f32 { // Caps Lock's light, once an elbow knocks it on. if id == "caps" && s.step == Step::Parked { return match scene_time(s) { Some(t) if s.scene.caps(t) => CAPS_LIGHT, _ => 0.0, }; } if !shortcut_keys(s).any(|k| k == id) { return 0.0; } match s.step { Step::YourTurn => 0.85 * melt_u, Step::Hearing => 1.0, // It cools to an ember that stays through the score. Step::Heard => { let fade = s.take_from.map_or(0.0, |from| { 1.0 - smoothstep(((s.now - from - take().release) / 0.45) as f32) }); EMBER + (0.70 - EMBER) * fade } Step::Done => EMBER, _ => 0.0, } } fn landing_nudge(s: &Onboard) -> [f32; 2] { if s.reduced.get() { return [0.0, 0.0]; } flights(s).iter().fold([0.0, 0.0], |[x, y], (f, _)| { let [dx, dy] = f.nudge(s.now); [x + dx, y + dy] }) } fn words(p: Paint, s: &Onboard, nudge: [f32; 2]) -> El { let mut out = el() .key("words") .absolute(0.0, 0.0) .size(STAGE_W, STAGE_H) .instant_changes() // They fly out of the field, land with a kick and burst on the cards. .motion_intent(MotionIntent::KICK | MotionIntent::CUT); let reduced = s.reduced.get(); let origin = field_origin(); let font = FIELD_FONT.lh(super::cards::FIELD_LINE); // Once sent, the answer is its bubble. let flights = if hex_sent(s) { Vec::new() } else { flights(s) }; let weight = 500.0 + 240.0 * CALM; // Just before the placeholder's first glyph until a word lands. let mut caret = -1.5; let mut line = 0; for (i, (flight, slot)) in flights.iter().enumerate() { if let Some(landed) = flight.landed(s.now, reduced) { let at = [ origin[0] + slot.centre[0] + nudge[0], origin[1] + slot.centre[1] + nudge[1], ]; out = out.children(typeset( p, &format!("w{i}"), slot.word, font, at, landed, flicker(s.now, i), )); } if reduced || s.now >= flight.arrives() { line = slot.line; caret = slot.right + 1.5; } if let Some(pose) = flight.pose(s.now, reduced) { out = out.child(flying(p, &format!("f{i}"), slot.word, weight, pose)); } } // The words take over from the caret while they fly in and settle, so // it never sits under one; it waits after the last that landed. let settling = !reduced && flights .iter() .any(|(f, _)| s.now >= f.start() && s.now < f.arrives() + SETTLE); if s.step == Step::Hearing && !settling { out = out.child( el().key("caret-at") .absolute(origin[0], origin[1]) .child(field_text::caret(p, "caret", caret, line)), ); } out } #[cfg(test)] mod tests { use super::*; #[test] fn streamed_words_leave_within_one_and_a_half_seconds() { let at = heard_for(take(), true); assert_eq!(at.len(), 7); assert_eq!(at[0], 0.16); assert!(at.windows(2).all(|w| w[0] <= w[1])); assert!(*at.last().unwrap() <= take().release); let burst = heard_for(take(), false); assert_eq!(burst[0], take().release); } #[test] fn card_keyboard_and_voice_hud_share_the_central_spine() { let (_, kh) = keyboard::size(); assert!(((CARD_X + CHAT_W * 0.5) - STAGE_W * 0.5).abs() < 0.01); assert!((CHAT_W - keyboard::size().0).abs() < 0.01, "one rail"); assert!((HUD_CENTRE[0] - STAGE_W * 0.5).abs() < 0.01); let [x, y] = field_origin(); let bottom = card_bottom(reply_lines()); assert!(x > CARD_X && x < STAGE_W); assert!(y > CARD_TOP && y < bottom); assert!(bottom < STAGE_H - kh, "the chat clears the keyboard"); assert!( bottom < HUD_CENTRE[1] - 40.0 * HUD_SCALE, "the HUD clears the chat" ); } #[test] fn hex_sends_after_the_words_land_and_jay_replies_after() { let beats = hex_beats(true, &Timing::default()); let landed = flights_from(true, 0.0) .iter() .map(|(f, _)| f.arrives()) .fold(0.0, f64::max); assert!(beats.send > landed); let reply = beats.lines[0].0; assert!(reply > beats.send + 0.4); assert!(beats.done > reply + 1.0); assert!(beats.done <= 8.0, "{}", beats.done); } }