From dd674804822b9ed2d0473daa455ceb3037fcddd3 Mon Sep 17 00:00:00 2001 From: Ryan Cartwright Date: Thu, 5 Nov 2026 11:16:58 +1100 Subject: [PATCH] feat(ios): bring bot faces to life with the web's moods and timings, in new bot, Home, replies, new threads and Details, with an Animations setting --- apps/NATIVE.md | 2 +- apps/codegen/generate-face-motion.ts | 374 ++++++++++++++ apps/codegen/generate.ts | 2 + apps/ios/README.md | 1 + apps/ios/Sugabots.xcodeproj/project.pbxproj | 16 + apps/ios/Sugabots/App/SugabotsApp.swift | 7 + .../Sugabots/Conversation/ChatGroups.swift | 44 ++ apps/ios/Sugabots/Conversation/Composer.swift | 6 +- .../Conversation/ConversationDetails.swift | 7 +- .../Conversation/ConversationScreen.swift | 6 +- .../Sugabots/Conversation/MentionEditor.swift | 16 +- .../Sugabots/Conversation/MessageRow.swift | 14 +- .../Conversation/NewThreadScreen.swift | 7 +- .../Conversation/SideThreadSheet.swift | 5 +- .../Design/FaceMotion.generated.swift | 213 ++++++++ apps/ios/Sugabots/Design/FacePaths.swift | 387 ++++++++++++++ apps/ios/Sugabots/Design/FaceRig.swift | 483 ++++++++++++++++++ apps/ios/Sugabots/Design/Faces.swift | 22 +- apps/ios/Sugabots/Design/LivelyBotFace.swift | 418 +++++++++++++++ apps/ios/Sugabots/Home/ConversationList.swift | 4 +- apps/ios/Sugabots/NewBot/NewBotScreen.swift | 14 +- .../Sugabots/Settings/GeneralSettings.swift | 7 +- 22 files changed, 2019 insertions(+), 36 deletions(-) create mode 100644 apps/codegen/generate-face-motion.ts create mode 100644 apps/ios/Sugabots/Design/FaceMotion.generated.swift create mode 100644 apps/ios/Sugabots/Design/FacePaths.swift create mode 100644 apps/ios/Sugabots/Design/FaceRig.swift create mode 100644 apps/ios/Sugabots/Design/LivelyBotFace.swift diff --git a/apps/NATIVE.md b/apps/NATIVE.md index 5f6b5fd77..f93dcda73 100644 --- a/apps/NATIVE.md +++ b/apps/NATIVE.md @@ -68,7 +68,7 @@ The numbers and strings below live in [`native-constants.json`](native-constants - **Value:** the bot's face at the top of its Details moves as the web's faces do where a bot is answering. - **Why:** Details is where a person meets the bot on a phone, and the face is large there. -- **Replaces:** the web's still face in Details. The face's own values come with the face work on its own branch and join `native-constants.json` then. +- **Replaces:** the web's still face in Details. ## Holding a pod's icon brings up its notifications diff --git a/apps/codegen/generate-face-motion.ts b/apps/codegen/generate-face-motion.ts new file mode 100644 index 000000000..098fcb450 --- /dev/null +++ b/apps/codegen/generate-face-motion.ts @@ -0,0 +1,374 @@ +/** + * Writes `Sugabots/Design/FaceMotion.generated.swift` from how + * `@sugabots/avatars` draws bots' faces and brings them to life, and the mood + * loops the web app gives them, so the app's faces are drawn and move by the + * same numbers and paths as the web's. What each act does with them is copied + * by hand, in `FaceRig.swift`. + */ +import * as marks from "../../packages/avatars/src/face-marks.ts"; +import * as rig from "../../packages/avatars/src/face-rig.ts"; +import { pacingByLiveliness } from "../../packages/avatars/src/group-pacing.ts"; +import * as hop from "../../packages/avatars/src/hop.ts"; +import * as layers from "../../packages/avatars/src/layer-motion.ts"; +import { defaultLiveliness, livelinessLevels } from "../../packages/avatars/src/liveliness.ts"; +import * as moodLoop from "../../packages/avatars/src/mood-loop.ts"; +import * as moods from "../../packages/avatars/src/moods.ts"; +import type { Span } from "../../packages/avatars/src/random.ts"; +import * as screenMoods from "../../packages/avatars/src/screen-moods.ts"; +import * as watching from "../../packages/avatars/src/watching.ts"; +import type { GeneratedFiles } from "./generated-files.ts"; + +/** A Swift value: its type, as Swift can't infer it from a whole number, and its code. */ +interface SwiftValue { + type: string; + code: string; +} + +const numbers = (values: readonly number[]) => `[${values.join(", ")}]`; + +const double = (value: number): SwiftValue => ({ type: "Double", code: `${value}` }); +const doubles = (values: readonly number[]): SwiftValue => ({ + type: "[Double]", + code: numbers(values), +}); +const closedRange = ([least, most]: readonly [number, number]): SwiftValue => ({ + type: "ClosedRange", + code: `${least} ... ${most}`, +}); +const span = ({ least, most }: Span) => `Span(least: ${least}, most: ${most})`; +const spring = ({ + stiffness, + damping, + restDelta, + restSpeed, +}: { + stiffness: number; + damping: number; + restDelta?: number; + restSpeed?: number; +}): SwiftValue => ({ + type: "SpringTuning", + code: `SpringTuning(stiffness: ${stiffness}, damping: ${damping}${restDelta === undefined ? "" : `, restDelta: ${restDelta}`}${restSpeed === undefined ? "" : `, restSpeed: ${restSpeed}`})`, +}); +/** A named easing as Swift's `Easing`; the app knows only the names the faces use. */ +function easing(ease: unknown): string { + if (typeof ease !== "string") + throw new Error(`The app's faces take named easings only, not ${String(ease)}`); + return `.${ease}`; +} +const easings = (eases: readonly unknown[]) => `[${eases.map(easing).join(", ")}]`; +const keyframesCode = ({ frames, times, ease, seconds }: layers.KeyframesPath) => + `Keyframes(frames: [${frames.map(numbers).join(", ")}], times: ${numbers(times)}, ease: ${easings(ease)}, seconds: ${seconds})`; +const keyframes = (path: layers.KeyframesPath): SwiftValue => ({ + type: "Keyframes", + code: keyframesCode(path), +}); +/** One value's keyframes as Swift's `Keyframes`, each frame a single value. */ +const valueKeyframes = ({ frames, times, ease, seconds }: rig.ValueKeyframes): SwiftValue => + keyframes({ + kind: "keyframes", + frames: frames.map((frame) => [frame]), + times, + ease, + seconds, + }); +const path = (one: layers.Path) => + one.kind === "keyframes" + ? `.keyframes(${keyframesCode(one)})` + : `.spring(to: ${numbers(one.to)}, ${spring(one).code})`; + +function moodLiteral(mood: moods.BotMood): string { + const { look, gaze, watches, act } = moods.moods[mood]; + const parts = [ + `look: MoodLook(eyesOpen: ${look.eyesOpen}, smile: ${look.smile}, arcsFlat: ${look.arcsFlat}, dots: ${look.dots}, eyesLift: ${look.eyesLift}, headDrop: ${look.headDrop}, headLean: ${look.headLean})`, + `gaze: Gaze(x: ${gaze?.x ?? 0}, y: ${gaze?.y ?? 0})`, + `watches: ${watches === true}`, + `act: ${act ? `MoodAct(pause: ${span(act.pause)}, firstPause: ${span(act.firstPause ?? act.pause)})` : "nil"}`, + ]; + return `Mood(${parts.join(", ")})`; +} + +function moodLoopValue(spells: moodLoop.MoodLoop): SwiftValue { + const literals = spells.map( + (spell) => + `MoodSpell(mood: .${spell.mood}, weight: ${spell.weight}, seconds: ${span(spell.seconds)})`, + ); + return { + type: "MoodLoop", + code: `MoodLoop(rest: ${literals[0]}, others: [${literals.slice(1).join(", ")}])`, + }; +} + +/** + * A Swift constant `name` holding `value`, documented as the constant `source` + * in the avatars' `file`, with what `note` adds, such as a change of unit. + */ +function constant( + name: string, + { source, file, note }: { source: string; file: string; note?: string }, + value: SwiftValue, +): string[] { + const doc = `As \`${source}\` in \`${file}\`${note ? `, ${note}` : ""}.`; + return [`\t/// ${doc}`, `\tstatic let ${name}: ${value.type} = ${value.code}`]; +} + +const fromRig = (source: keyof typeof rig & string, note?: string) => ({ + source, + file: "face-rig.ts", + note, +}); +const fromLayers = (source: keyof typeof layers & string, note?: string) => ({ + source, + file: "layer-motion.ts", + note, +}); +const fromMarks = (source: keyof typeof marks & string) => ({ source, file: "face-marks.ts" }); +const fromMoods = (source: keyof typeof moods & string) => ({ source, file: "moods.ts" }); + +const layerNames = Object.keys(layers.layerKinds) as layers.LayerName[]; + +export function generate(): GeneratedFiles { + const lines = [ + "// Generated by apps/codegen/generate-face-motion.ts from @sugabots/avatars. Do not edit.", + "", + "/// How much bot faces move, as someone has chosen, as `Liveliness`.", + "enum Liveliness: String, CaseIterable {", + ...livelinessLevels.map((level) => `\tcase ${level}`), + "", + "\t/// What someone gets before they choose, as `defaultLiveliness`.", + `\tstatic let chosenByDefault = Liveliness.${defaultLiveliness}`, + "", + "\t/// How the faces in a group take turns to move, as `pacingByLiveliness`; none move when off.", + "\tvar groupPacing: GroupPacing? {", + "\t\tswitch self {", + ...livelinessLevels.map((level) => { + if (level === "off") return "\t\tcase .off: nil"; + const pacing = pacingByLiveliness[level]; + return `\t\tcase .${level}: GroupPacing(movingAtOnce: ${pacing.movingAtOnce}, gapSeconds: ${span(pacing.gapSeconds)}, turnSeconds: ${span(pacing.turnSeconds)})`; + }), + "\t\t}", + "\t}", + "}", + "", + "/// The moods a bot can be in, as `botMoods`.", + "enum BotMood: String, CaseIterable {", + ...moods.botMoods.map((mood) => `\tcase ${mood}`), + "", + "\t/// How the mood holds the face and how often it acts, as `moods`.", + "\tvar mood: Mood {", + "\t\tswitch self {", + ...moods.botMoods.map((mood) => `\t\tcase .${mood}: ${moodLiteral(mood)}`), + "\t\t}", + "\t}", + "}", + "", + "/// The layers a face's parts move on, as `layerKinds` in `layer-motion.ts`.", + "/// Translations are percentages of the face; turns are in degrees.", + "enum FaceLayer: String, CaseIterable {", + ...layerNames.map((name) => `\tcase ${name}`), + "", + "\t/// Where the layer rests, as its kind's `rest`.", + "\tvar rest: [Double] {", + "\t\tswitch self {", + ...layerNames.map((name) => `\t\tcase .${name}: ${numbers(layers.layerKinds[name].rest)}`), + "\t\t}", + "\t}", + "}", + "", + "/// How a face is drawn, in the square of `viewboxSize` units it is drawn in.", + "enum FaceGeometry {", + ...constant("viewboxSize", fromMarks("FACE_VIEWBOX_SIZE"), double(marks.FACE_VIEWBOX_SIZE)), + ...constant( + "arcEyeStrokeWidth", + fromMarks("ARC_EYE_STROKE_WIDTH"), + double(marks.ARC_EYE_STROKE_WIDTH), + ), + ...constant("arcShutRise", fromMarks("ARC_SHUT_RISE"), double(marks.ARC_SHUT_RISE)), + ...constant( + "flattestArcHeight", + fromMarks("FLATTEST_ARC_HEIGHT"), + double(marks.FLATTEST_ARC_HEIGHT), + ), + ...constant( + "smileArcWidening", + fromMarks("SMILE_ARC_WIDENING"), + double(marks.SMILE_ARC_WIDENING), + ), + ...constant( + "smileArcBaselineShare", + fromMarks("SMILE_ARC_BASELINE_SHARE"), + double(marks.SMILE_ARC_BASELINE_SHARE), + ), + ...constant("openEyeFades", fromMarks("OPEN_EYE_FADES"), closedRange(marks.OPEN_EYE_FADES)), + ...constant("smileArcShows", fromMarks("SMILE_ARC_SHOWS"), closedRange(marks.SMILE_ARC_SHOWS)), + ...constant( + "thinkingDotRadius", + fromMarks("THINKING_DOT_RADIUS"), + double(marks.THINKING_DOT_RADIUS), + ), + ...constant("thinkingDotXs", fromMarks("THINKING_DOT_XS"), doubles(marks.THINKING_DOT_XS)), + ...constant("thinkingDotY", fromMarks("THINKING_DOT_Y"), double(marks.THINKING_DOT_Y)), + "}", + "", + "/// The numbers and paths a face moves by. Distances are in the face's", + "/// viewbox units unless they say they're percentages of the face.", + "enum FaceMotion {", + ...constant("tickSeconds", fromLayers("TICK_SECONDS"), double(layers.TICK_SECONDS)), + ...constant( + "springSettleStepMilliseconds", + fromLayers("SPRING_SETTLE_STEP_MILLISECONDS"), + double(layers.SPRING_SETTLE_STEP_MILLISECONDS), + ), + ...constant( + "longestSpringMilliseconds", + fromLayers("LONGEST_SPRING_MILLISECONDS"), + double(layers.LONGEST_SPRING_MILLISECONDS), + ), + ...constant("velocitySeconds", fromLayers("VELOCITY_SECONDS"), double(layers.VELOCITY_SECONDS)), + ...constant("leadInSeconds", fromLayers("LEAD_IN_SECONDS"), double(layers.LEAD_IN_SECONDS)), + "", + ...constant("eyeTravelX", fromRig("EYE_TRAVEL_X"), double(rig.EYE_TRAVEL_X)), + ...constant("eyeTravelY", fromRig("EYE_TRAVEL_Y"), double(rig.EYE_TRAVEL_Y)), + ...constant("eyeEdgeDip", fromRig("EYE_EDGE_DIP"), double(rig.EYE_EDGE_DIP)), + ...constant("eyeForeshortening", fromRig("EYE_FORESHORTENING"), double(rig.EYE_FORESHORTENING)), + ...constant("headTravelX", fromRig("HEAD_TRAVEL_X"), double(rig.HEAD_TRAVEL_X)), + ...constant("headTravelY", fromRig("HEAD_TRAVEL_Y"), double(rig.HEAD_TRAVEL_Y)), + ...constant("headTiltDegrees", fromRig("HEAD_TILT_DEGREES"), double(rig.HEAD_TILT_DEGREES)), + ...constant("eyesSpring", fromRig("EYES_SPRING"), spring(rig.EYES_SPRING)), + ...constant("headSpring", fromRig("HEAD_SPRING"), spring(rig.HEAD_SPRING)), + ...constant("moodSpring", fromRig("MOOD_SPRING"), spring(rig.MOOD_SPRING)), + ...constant( + "eyeShapeMoodSpring", + fromRig("EYE_SHAPE_MOOD_SPRING"), + spring(rig.EYE_SHAPE_MOOD_SPRING), + ), + ...constant("blink", fromRig("BLINK"), valueKeyframes(rig.BLINK)), + ...constant( + "blinkTwiceGapSeconds", + fromRig("BLINK_TWICE_GAP_SECONDS"), + double(rig.BLINK_TWICE_GAP_SECONDS), + ), + ...constant( + "watchingRestSeconds", + fromRig("WATCHING_REST_MS", "in seconds"), + double(rig.WATCHING_REST_MS / 1000), + ), + ...constant("flutter", fromRig("FLUTTER"), valueKeyframes(rig.FLUTTER)), + ...constant("wiggleMove", fromRig("WIGGLE_MOVE"), keyframes(rig.WIGGLE_MOVE)), + ...constant("wiggleTilt", fromRig("WIGGLE_TILT"), keyframes(rig.WIGGLE_TILT)), + ...constant("dotRise", fromRig("DOT_RISE"), keyframes(rig.DOT_RISE)), + ...constant( + "dotStaggerSeconds", + fromRig("DOT_STAGGER_SECONDS"), + double(rig.DOT_STAGGER_SECONDS), + ), + ...constant("outerDotsGrow", fromRig("OUTER_DOTS_GROW"), closedRange(rig.OUTER_DOTS_GROW)), + ...constant("middleDotGrows", fromRig("MIDDLE_DOT_GROWS"), closedRange(rig.MIDDLE_DOT_GROWS)), + ...constant( + "stillingSeconds", + fromRig("STILLING", "its `seconds`"), + double(rig.STILLING.seconds), + ), + ...constant("stillingEase", fromRig("STILLING", "its `ease`"), { + type: "Easing", + code: easing(rig.STILLING.ease), + }), + "", + ...constant("hopSeconds", { source: "HOP_SECONDS", file: "hop.ts" }, double(hop.HOP_SECONDS)), + ...constant( + "fullHop", + { source: "FULL_HOP", file: "hop.ts", note: "by the layer each path list moves" }, + { + type: "[FaceLayer: [FacePath]]", + code: `[${Object.entries(hop.FULL_HOP) + .map(([layer, paths]) => `.${layer}: [${paths.map(path).join(", ")}]`) + .join(", ")}]`, + }, + ), + "", + ...constant("idleBlinkChance", fromMoods("IDLE_BLINK_CHANCE"), double(moods.IDLE_BLINK_CHANCE)), + ...constant( + "doubleBlinkChance", + fromMoods("DOUBLE_BLINK_CHANCE"), + double(moods.DOUBLE_BLINK_CHANCE), + ), + ...constant("lookAheadChance", fromMoods("LOOK_AHEAD_CHANCE"), double(moods.LOOK_AHEAD_CHANCE)), + ...constant("idleGlanceReach", fromMoods("IDLE_GLANCE_REACH"), double(moods.IDLE_GLANCE_REACH)), + ...constant( + "longGlanceDistance", + fromMoods("LONG_GLANCE_DISTANCE"), + double(moods.LONG_GLANCE_DISTANCE), + ), + ...constant( + "longGlanceBlinkChance", + fromMoods("LONG_GLANCE_BLINK_CHANCE"), + double(moods.LONG_GLANCE_BLINK_CHANCE), + ), + ...constant("happyBounce", fromMoods("HAPPY_BOUNCE"), double(moods.HAPPY_BOUNCE)), + ...constant( + "happyBounceChance", + fromMoods("HAPPY_BOUNCE_CHANCE"), + double(moods.HAPPY_BOUNCE_CHANCE), + ), + ...constant( + "happyBlinkChance", + fromMoods("HAPPY_BLINK_CHANCE"), + double(moods.HAPPY_BLINK_CHANCE), + ), + ...constant( + "happyGlanceReach", + fromMoods("HAPPY_GLANCE_REACH"), + double(moods.HAPPY_GLANCE_REACH), + ), + ...constant("excitedBounce", fromMoods("EXCITED_BOUNCE"), { + type: "Span", + code: span(moods.EXCITED_BOUNCE), + }), + ...constant( + "excitedFullHopChance", + fromMoods("EXCITED_FULL_HOP_CHANCE"), + double(moods.EXCITED_FULL_HOP_CHANCE), + ), + ...constant("flutterChance", fromMoods("FLUTTER_CHANCE"), double(moods.FLUTTER_CHANCE)), + ...constant( + "glanceLeastShare", + fromMoods("GLANCE_LEAST_SHARE"), + double(moods.GLANCE_LEAST_SHARE), + ), + ...constant( + "glanceVerticalShare", + fromMoods("GLANCE_VERTICAL_SHARE"), + double(moods.GLANCE_VERTICAL_SHARE), + ), + "", + ...constant( + "oftenRestShare", + { source: "OFTEN_REST_SHARE", file: "mood-loop.ts" }, + double(moodLoop.OFTEN_REST_SHARE), + ), + ...constant( + "watchingReach", + { source: "WATCHING_REACH", file: "watching.ts" }, + double(watching.WATCHING_REACH), + ), + ...constant( + "watchingJumpSeconds", + { source: "JUMP_INTERVAL_MS", file: "watching.ts", note: "in seconds" }, + double(watching.JUMP_INTERVAL_MS / 1000), + ), + "", + ...constant( + "watchingMoods", + { source: "WATCHING_MOODS", file: "screen-moods.ts" }, + moodLoopValue(screenMoods.WATCHING_MOODS), + ), + ...constant( + "creationMoods", + { source: "CREATION_MOODS", file: "screen-moods.ts" }, + moodLoopValue(screenMoods.CREATION_MOODS), + ), + "}", + "", + ]; + return { files: new Map([["Sugabots/Design/FaceMotion.generated.swift", lines.join("\n")]]) }; +} diff --git a/apps/codegen/generate.ts b/apps/codegen/generate.ts index a8e34463b..656a4ae69 100644 --- a/apps/codegen/generate.ts +++ b/apps/codegen/generate.ts @@ -17,6 +17,7 @@ import * as builtInTools from "./generate-built-in-tools.ts"; import * as catalogs from "./generate-catalogs.ts"; import * as connectionMarks from "./generate-connection-marks.ts"; import * as emoji from "./generate-emoji.ts"; +import * as faceMotion from "./generate-face-motion.ts"; import * as fileAndZoneLists from "./generate-file-and-zone-lists.ts"; import * as limits from "./generate-limits.ts"; import * as lucideIcons from "./generate-lucide-icons.ts"; @@ -35,6 +36,7 @@ export const generators: Record = { "generate-catalogs.ts": iosOnly(catalogs.generate), "generate-connection-marks.ts": iosOnly(connectionMarks.generate), "generate-emoji.ts": iosOnly(emoji.generate), + "generate-face-motion.ts": iosOnly(faceMotion.generate), "generate-file-and-zone-lists.ts": iosOnly(fileAndZoneLists.generate), "generate-limits.ts": iosOnly(limits.generate), "generate-lucide-icons.ts": iosOnly(lucideIcons.generate), diff --git a/apps/ios/README.md b/apps/ios/README.md index a2728eb76..f6534369b 100644 --- a/apps/ios/README.md +++ b/apps/ios/README.md @@ -76,6 +76,7 @@ These files are generated, each by its script in `apps/codegen`, from the web's | `generate-artifact-pages.ts` | `Artifacts/ArtifactPages.generated.swift` | `artifact-pages.ts` in `@sugabots/contracts`, `lib/page-frame.ts`, `app.css`'s colours and fonts, and `lib/artifact-kind-catalog.ts` | | `generate-native-constants.ts` | `Design/NativeConstants.generated.swift` | `apps/native-constants.json` | | `generate-bot-looks.ts` | `Design/BotLooks.generated.swift` | `@sugabots/avatars`, the pod palette | +| `generate-face-motion.ts` | `Design/FaceMotion.generated.swift` | how `@sugabots/avatars` moves bots' faces, and the web's mood loops | | `generate-lucide-icons.ts` | `Design/Lucide.generated.swift` | every icon `packages/web` imports from `lucide-react` | | `generate-connection-marks.ts` | `Design/ConnectionMarks.generated.swift` | `ui/connection-mark.tsx` | | `generate-provider-logos.ts` | `Assets.xcassets/ProviderLogos`, `Design/ProviderLogos.generated.swift` | `@sugabots/provider-logos`, contracts | diff --git a/apps/ios/Sugabots.xcodeproj/project.pbxproj b/apps/ios/Sugabots.xcodeproj/project.pbxproj index eaaca8720..1667a84bd 100644 --- a/apps/ios/Sugabots.xcodeproj/project.pbxproj +++ b/apps/ios/Sugabots.xcodeproj/project.pbxproj @@ -30,6 +30,10 @@ BBABD080951E1845220A26E8 /* Controls.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76F6F585620EC11CCDE525A6 /* Controls.swift */; }; 56D6C924863DDEB7437F800F /* Faces.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB9AE8FE1CDBAFCE46BF578E /* Faces.swift */; }; CC29E82252B5FE216850DDE9 /* Avatars.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5F6B58CD306C531CFFC774F /* Avatars.swift */; }; + B88253E33182EA5463C76227 /* LivelyBotFace.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB8652F04125E09A3DEB652D /* LivelyBotFace.swift */; }; + 0CE628BBDD69C47B47E4EA9D /* FaceRig.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC1443F2DA1A66A5223865F5 /* FaceRig.swift */; }; + 12564E0D16D0D1063B5C395D /* FacePaths.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA776EEA704E5A8FDCB7DF25 /* FacePaths.swift */; }; + D20F64DE317082BC7A9854F0 /* FaceMotion.generated.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2DFBEB116BB69534707D317 /* FaceMotion.generated.swift */; }; 6200298A0F341EEBB9CEC47F /* Palette.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B17129B8579BC8CF7E165FE /* Palette.swift */; }; 5519534E4D310800FBD74534 /* SVGPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A1A8E67DCFD5C397203F038 /* SVGPath.swift */; }; 90FE1ACDA6033F0CDD13BFE9 /* HomeScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AF57B4CF1C84550D1950112 /* HomeScreen.swift */; }; @@ -158,6 +162,10 @@ 76F6F585620EC11CCDE525A6 /* Controls.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Controls.swift; sourceTree = ""; }; DB9AE8FE1CDBAFCE46BF578E /* Faces.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Faces.swift; sourceTree = ""; }; A5F6B58CD306C531CFFC774F /* Avatars.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Avatars.swift; sourceTree = ""; }; + AB8652F04125E09A3DEB652D /* LivelyBotFace.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "LivelyBotFace.swift"; sourceTree = ""; }; + CC1443F2DA1A66A5223865F5 /* FaceRig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FaceRig.swift"; sourceTree = ""; }; + FA776EEA704E5A8FDCB7DF25 /* FacePaths.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FacePaths.swift"; sourceTree = ""; }; + C2DFBEB116BB69534707D317 /* FaceMotion.generated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FaceMotion.generated.swift"; sourceTree = ""; }; 6B17129B8579BC8CF7E165FE /* Palette.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Palette.swift; sourceTree = ""; }; 3A1A8E67DCFD5C397203F038 /* SVGPath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SVGPath.swift; sourceTree = ""; }; ABED72F46CC97B327B1A2B9E /* JetBrainsMono.woff2 */ = {isa = PBXFileReference; lastKnownFileType = file; path = JetBrainsMono.woff2; sourceTree = ""; }; @@ -413,6 +421,10 @@ 76F6F585620EC11CCDE525A6 /* Controls.swift */, DB9AE8FE1CDBAFCE46BF578E /* Faces.swift */, A5F6B58CD306C531CFFC774F /* Avatars.swift */, + AB8652F04125E09A3DEB652D /* LivelyBotFace.swift */, + CC1443F2DA1A66A5223865F5 /* FaceRig.swift */, + FA776EEA704E5A8FDCB7DF25 /* FacePaths.swift */, + C2DFBEB116BB69534707D317 /* FaceMotion.generated.swift */, 6B17129B8579BC8CF7E165FE /* Palette.swift */, 3A1A8E67DCFD5C397203F038 /* SVGPath.swift */, ); @@ -722,6 +734,10 @@ BBABD080951E1845220A26E8 /* Controls.swift in Sources */, 56D6C924863DDEB7437F800F /* Faces.swift in Sources */, CC29E82252B5FE216850DDE9 /* Avatars.swift in Sources */, + B88253E33182EA5463C76227 /* LivelyBotFace.swift in Sources */, + 0CE628BBDD69C47B47E4EA9D /* FaceRig.swift in Sources */, + 12564E0D16D0D1063B5C395D /* FacePaths.swift in Sources */, + D20F64DE317082BC7A9854F0 /* FaceMotion.generated.swift in Sources */, 6200298A0F341EEBB9CEC47F /* Palette.swift in Sources */, 5519534E4D310800FBD74534 /* SVGPath.swift in Sources */, 90FE1ACDA6033F0CDD13BFE9 /* HomeScreen.swift in Sources */, diff --git a/apps/ios/Sugabots/App/SugabotsApp.swift b/apps/ios/Sugabots/App/SugabotsApp.swift index 0feca46a7..bc32c3741 100644 --- a/apps/ios/Sugabots/App/SugabotsApp.swift +++ b/apps/ios/Sugabots/App/SugabotsApp.swift @@ -26,9 +26,13 @@ struct SugabotsApp: App { /// Signing in, or the signed-in person's workspace. A view rather than the /// scene's body, since only a view's body redraws when `Session` changes. +@MainActor private struct RootView: View { @Environment(Session.self) private var session @Environment(Toaster.self) private var toaster + @Environment(\.accessibilityReduceMotion) private var reduceMotion + @ChosenLiveliness private var liveliness + @State private var typing = TypingWatch() var body: some View { ZStack(alignment: .bottom) { @@ -52,6 +56,9 @@ private struct RootView: View { } .animation(.easeOut(duration: 0.2), value: toaster.message) .background(Palette.background.ignoresSafeArea()) + // Bots' faces move as chosen on this device, and not at all where the system asks for less motion. + .environment(\.liveliness, reduceMotion ? .off : liveliness) + .environment(typing) } /// Runs `store` until its view goes, signing out when the server refuses diff --git a/apps/ios/Sugabots/Conversation/ChatGroups.swift b/apps/ios/Sugabots/Conversation/ChatGroups.swift index 67accc9df..e2e3e8ebb 100644 --- a/apps/ios/Sugabots/Conversation/ChatGroups.swift +++ b/apps/ios/Sugabots/Conversation/ChatGroups.swift @@ -84,6 +84,50 @@ enum ChatGroups { return message.createdAt.timeIntervalSince(previous.createdAt) >= longQuiet } + /// Whether `message` heads a run drawn with its author's face: after a day + /// marker, when `dividers` mark days, or as `startsGroup` says, as the web's `startsRun`. + static func startsRun(_ previous: Message?, _ message: Message, dividers: Bool) -> Bool { + (dividers && startsNewDay(previous?.createdAt, message.createdAt)) || startsGroup(previous, message) + } + + /// Where the face of the bot answering is drawn, and whether it's typing now. + struct AnsweringFace { + let messageId: String + let botTyping: Bool + } + + /// The face that stands for the bot that wrote the newest of `messages`, + /// as the web's `findAnsweringFace`: the last face drawn for that bot, with + /// a day marker before each new day when `dividers`. A reply just begun + /// shows only as its typing line, so its face may be on the bot's reply before. + static func answeringFace(in messages: [Message], dividers: Bool) -> AnsweringFace? { + answeringFace(among: messages.indices.map { index in + let message = messages[index] + return (message, startsRun(index > 0 ? messages[index - 1] : nil, message, dividers: dividers)) + }) + } + + /// `answeringFace(in:dividers:)` for messages shown in `groups`, where a + /// face is drawn at the start of each group as well as where `startsGroup` + /// says, as a day marker, a thread line or a new run ends the group before. + static func answeringFace(in groups: [Group]) -> AnsweringFace? { + answeringFace(among: groups.flatMap { group -> [(Message, Bool)] in + guard case let .messages(messages) = group.content else { return [] } + return messages.indices.map { index in + (messages[index], startsGroup(index > 0 ? messages[index - 1] : nil, messages[index])) + } + }) + } + + /// The answering face among messages oldest first, each with whether a face is drawn on it. + private static func answeringFace(among shown: [(message: Message, drawsFace: Bool)]) -> AnsweringFace? { + guard let newest = shown.last?.message, case .agent = newest.author else { return nil } + let drawn = shown.last { message, drawsFace in + drawsFace && sameAuthor(message, newest) && !Segments.showsNothingYet(message) + } + return drawn.map { AnsweringFace(messageId: $0.message.id, botTyping: isTyping(newest)) } + } + private static func sameAuthor(_ left: Message, _ right: Message) -> Bool { switch (left.author, right.author) { case let (.person(a), .person(b)): a.id == b.id diff --git a/apps/ios/Sugabots/Conversation/Composer.swift b/apps/ios/Sugabots/Conversation/Composer.swift index 29a23e5ec..abd7dcc2d 100644 --- a/apps/ios/Sugabots/Conversation/Composer.swift +++ b/apps/ios/Sugabots/Conversation/Composer.swift @@ -140,6 +140,7 @@ struct Composer: View { @State private var picking = false @State private var importing = false @State private var photos: [PhotosPickerItem] = [] + @Environment(TypingWatch.self) private var typingWatch: TypingWatch? var body: some View { let shortcode = shortcodeBeingTyped @@ -220,7 +221,10 @@ struct Composer: View { .disabled(attachments == nil || attachments?.isFull == true) .opacity(attachments == nil || attachments?.isFull == true ? 0.4 : 1) .accessibilityLabel("Attach files") - MentionEditor(text: $text, isFocused: $focused, mentionable: mentionable, edited: edited) + MentionEditor(text: $text, isFocused: $focused, mentionable: mentionable) { caret in + typingWatch?.typed(at: caret) + edited() + } .overlay(alignment: .topLeading) { if text.isEmpty { Text(placeholder ?? label) diff --git a/apps/ios/Sugabots/Conversation/ConversationDetails.swift b/apps/ios/Sugabots/Conversation/ConversationDetails.swift index 773997094..824122bc4 100644 --- a/apps/ios/Sugabots/Conversation/ConversationDetails.swift +++ b/apps/ios/Sugabots/Conversation/ConversationDetails.swift @@ -22,7 +22,7 @@ struct ConversationDetails: View { DetailsPage { switch kind { case let .botChannel(agent), let .botThread(agent): - BotDetails(agent: agent, pod: pod, threadId: details.thread.id) + BotDetails(agent: agent, pod: pod, threadId: details.thread.id, replying: model.isRunning) case let .podChat(name): ChannelHeading(model: model, pod: pod, details: details, name: name) case .peopleThread: @@ -137,6 +137,8 @@ private struct BotDetails: View { let agent: Agent let pod: Pod let threadId: String + /// Whether the bot is answering in the chat, which its face shows by thinking. + let replying: Bool @Environment(WorkspaceStore.self) private var store @Environment(\.openPage) private var openPage @State private var activity: ThreadActivity? @@ -159,7 +161,8 @@ private struct BotDetails: View { @ViewBuilder private var sections: some View { DetailsHeading(name: agent.name, line: store.modelName(agent.model) ?? "No model chosen") { - BotFace(look: agent.look, size: 88) + // Alive, unlike the web's, which keeps it still: the one large face here, with nothing else moving. + WatchingFace(look: agent.look, size: 88, busy: replying, watching: true) } actions: { Button { openPage(.settings(.agent(pod, agent))) } label: { RoundActionLabel(icon: .settings, label: "Settings") diff --git a/apps/ios/Sugabots/Conversation/ConversationScreen.swift b/apps/ios/Sugabots/Conversation/ConversationScreen.swift index e2f0e2bbd..91589629e 100644 --- a/apps/ios/Sugabots/Conversation/ConversationScreen.swift +++ b/apps/ios/Sugabots/Conversation/ConversationScreen.swift @@ -187,6 +187,9 @@ private struct ConversationContent: View { let routineFeed = model.isRoutineFeed let byMessage = Self.threadsByMessage(items) let groups = ChatGroups.of(items, byRun: routineFeed, under: Set(byMessage.values.flatMap { $0 }.map(\.id))) + // Found across all the groups, not in each one, or each group would bring a face of its own alive. + let answering = ChatGroups.answeringFace(in: groups) + let viewerWriting = !draft.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty let actions = messageActions(kind: kind, pod: pod, scroller: scroller) if model.messagesFailed { FormAlert(text: "Messages could not be loaded. Reload this page to try again.").padding(.horizontal, 20) @@ -219,7 +222,8 @@ private struct ConversationContent: View { actions: actions, highlighted: highlighted == message.id, heldOpen: heldMessageId == message.id, - startedThreads: byMessage[message.id] ?? [] + startedThreads: byMessage[message.id] ?? [], + liveFace: answering?.messageId == message.id ? LiveFace(botTyping: answering?.botTyping ?? false, viewerWriting: viewerWriting) : nil ) .id(message.id) } diff --git a/apps/ios/Sugabots/Conversation/MentionEditor.swift b/apps/ios/Sugabots/Conversation/MentionEditor.swift index c3606b301..a26633ce5 100644 --- a/apps/ios/Sugabots/Conversation/MentionEditor.swift +++ b/apps/ios/Sugabots/Conversation/MentionEditor.swift @@ -14,8 +14,8 @@ struct MentionEditor: UIViewRepresentable { var maxLength = Limits.messageMaxLength /// How many lines it grows to before it scrolls. var maxLines = 8 - /// Told of each edit the person makes, never of the text being set from outside. - var edited: () -> Void = {} + /// Told where, in the window's points, each edit leaves the caret, for faces that watch typing. + var typed: (CGPoint) -> Void = { _ in } private static let font = UIFont(name: "Rubik", size: 16) ?? .systemFont(ofSize: 16) @@ -94,13 +94,23 @@ struct MentionEditor: UIViewRepresentable { let plain = Self.plainText(of: view.attributedText) let caret = Self.plainOffset(of: view.attributedText, at: view.selectedRange.location) parent.text = plain - parent.edited() + parent.typed(Self.caretPoint(in: view)) // Draw any mention finished by this edit as a chip. if Self.segments(of: plain, mentionable: parent.mentionable).contains(where: \.isMention) { render(plain, in: view, caret: caret) } } + /// Where the caret is, in the window's points: its left edge, halfway down, as the web's `typingPoint`; else the middle of the field. + static func caretPoint(in view: UITextView) -> CGPoint { + guard let caret = view.selectedTextRange?.end else { + let field = view.convert(view.bounds, to: nil) + return CGPoint(x: field.midX, y: field.midY) + } + let rect = view.convert(view.caretRect(for: caret), to: nil) + return CGPoint(x: rect.minX, y: rect.midY) + } + /// Draws `plain` with its finished mentions as chips, and puts the caret back where it was in the text. func render(_ plain: String, in view: UITextView, caret: Int) { let rendered = NSMutableAttributedString() diff --git a/apps/ios/Sugabots/Conversation/MessageRow.swift b/apps/ios/Sugabots/Conversation/MessageRow.swift index ce34a296e..ebd2bf8a3 100644 --- a/apps/ios/Sugabots/Conversation/MessageRow.swift +++ b/apps/ios/Sugabots/Conversation/MessageRow.swift @@ -56,6 +56,8 @@ struct MessageRow: View { var startedThreads: [ChatMessageItem.Thread] = [] /// Smaller, as a side thread's sheet shows its messages. var compact = false + /// How its face comes alive, when it's the face of the bot answering. + var liveFace: LiveFace? @State private var timeTapped = false @State private var pickingReaction = false /// Whether the message is held, with what can be done with it slid up from the foot. @@ -163,7 +165,10 @@ struct MessageRow: View { @ViewBuilder private var face: some View { Group { - if startsGroup { + if startsGroup, let liveFace, case let .agent(agent) = message.author { + WatchingFace(look: agent.look, size: column.faceSize, busy: liveFace.botTyping, watching: liveFace.viewerWriting) + .padding(.top, 2) + } else if startsGroup { ParticipantFace(participant: message.author.participant, size: column.faceSize) .padding(.top, 2) } else { @@ -574,6 +579,13 @@ struct ThreadLine: View { } } +/// How the face of the bot answering comes alive, as the web's `AnsweringFaceState`: +/// thinking while the bot types, watching while the viewer writes back. +struct LiveFace { + let botTyping: Bool + let viewerWriting: Bool +} + /// Who is typing, with the dots, as the web's `TypingIndicator`. struct TypingIndicator: View { let names: [String] diff --git a/apps/ios/Sugabots/Conversation/NewThreadScreen.swift b/apps/ios/Sugabots/Conversation/NewThreadScreen.swift index 6bd2a24c2..f6d446016 100644 --- a/apps/ios/Sugabots/Conversation/NewThreadScreen.swift +++ b/apps/ios/Sugabots/Conversation/NewThreadScreen.swift @@ -42,7 +42,8 @@ struct NewThreadScreen: View { private func chosen(_ agent: Agent) -> some View { VStack(spacing: 24) { VStack(spacing: 8) { - BotFace(look: agent.look, size: 64).padding(.bottom, 12) + // It watches the question being written, and thinks while the thread starts. + WatchingFace(look: agent.look, size: 64, busy: starting, watching: true).padding(.bottom, 12) Text("What should \(agent.name) work on?").font(Typeface.rubik(18, .semibold)).foregroundStyle(Palette.foreground) if let description = agent.description, !description.isEmpty { Text(description).font(Typeface.rubik(13)).foregroundStyle(Palette.mutedForeground) @@ -75,7 +76,7 @@ struct NewThreadScreen: View { composing = true } label: { HStack(spacing: 8) { - BotFace(look: bot.look, size: 20) + TurnTakingFace(look: bot.look, size: 20) Text(bot.name).font(Typeface.rubik(13.5, .medium)).foregroundStyle(Palette.foreground) } .padding(.leading, 6) @@ -86,6 +87,8 @@ struct NewThreadScreen: View { .buttonStyle(.plain) } } + // The bots' faces take turns to move, so the row never fidgets all at once. + .livelyGroup() } } .padding(.horizontal, 20) diff --git a/apps/ios/Sugabots/Conversation/SideThreadSheet.swift b/apps/ios/Sugabots/Conversation/SideThreadSheet.swift index d9c38f7b6..9c50205b8 100644 --- a/apps/ios/Sugabots/Conversation/SideThreadSheet.swift +++ b/apps/ios/Sugabots/Conversation/SideThreadSheet.swift @@ -85,6 +85,7 @@ struct SideThreadSheet: View { if type == .routine, case .routineTrigger = message.author { return nil } return message } + let answering = ChatGroups.answeringFace(in: messages, dividers: false) ScrollView { LazyVStack(alignment: .leading, spacing: 0) { ForEach(Array(messages.enumerated()), id: \.element.id) { index, message in @@ -93,7 +94,9 @@ struct SideThreadSheet: View { startsGroup: ChatGroups.startsGroup(index > 0 ? messages[index - 1] : nil, message), context: context, actions: MessageActions(openThread: openThread), - compact: true + compact: true, + // No one writes back here, so the face only thinks while the bot types. + liveFace: answering?.messageId == message.id ? LiveFace(botTyping: answering?.botTyping ?? false, viewerWriting: false) : nil ) } if details.thread.status == .running, model.notices.isEmpty, messages.last?.author.personId != nil { diff --git a/apps/ios/Sugabots/Design/FaceMotion.generated.swift b/apps/ios/Sugabots/Design/FaceMotion.generated.swift new file mode 100644 index 000000000..2fced28bd --- /dev/null +++ b/apps/ios/Sugabots/Design/FaceMotion.generated.swift @@ -0,0 +1,213 @@ +// Generated by apps/codegen/generate-face-motion.ts from @sugabots/avatars. Do not edit. + +/// How much bot faces move, as someone has chosen, as `Liveliness`. +enum Liveliness: String, CaseIterable { + case off + case sometimes + case often + + /// What someone gets before they choose, as `defaultLiveliness`. + static let chosenByDefault = Liveliness.often + + /// How the faces in a group take turns to move, as `pacingByLiveliness`; none move when off. + var groupPacing: GroupPacing? { + switch self { + case .off: nil + case .sometimes: GroupPacing(movingAtOnce: 1, gapSeconds: Span(least: 4, most: 10), turnSeconds: Span(least: 1, most: 3)) + case .often: GroupPacing(movingAtOnce: 2, gapSeconds: Span(least: 1.5, most: 4), turnSeconds: Span(least: 1.5, most: 4)) + } + } +} + +/// The moods a bot can be in, as `botMoods`. +enum BotMood: String, CaseIterable { + case still + case idle + case thinking + case happy + case excited + case sleepy + + /// How the mood holds the face and how often it acts, as `moods`. + var mood: Mood { + switch self { + case .still: Mood(look: MoodLook(eyesOpen: 1, smile: 0, arcsFlat: 0, dots: 0, eyesLift: 0, headDrop: 0, headLean: 0), gaze: Gaze(x: 0, y: 0), watches: false, act: nil) + case .idle: Mood(look: MoodLook(eyesOpen: 1, smile: 0, arcsFlat: 0, dots: 0, eyesLift: 0, headDrop: 0, headLean: 0), gaze: Gaze(x: 0, y: 0), watches: true, act: MoodAct(pause: Span(least: 1.2, most: 3.8), firstPause: Span(least: 0.2, most: 0.7))) + case .thinking: Mood(look: MoodLook(eyesOpen: 1, smile: 0, arcsFlat: 0, dots: 1, eyesLift: 0, headDrop: 0, headLean: 0), gaze: Gaze(x: 0, y: 0), watches: false, act: nil) + case .happy: Mood(look: MoodLook(eyesOpen: 0, smile: 1, arcsFlat: 0, dots: 0, eyesLift: -1, headDrop: 0, headLean: 0), gaze: Gaze(x: 0, y: 0), watches: true, act: MoodAct(pause: Span(least: 1.4, most: 3.2), firstPause: Span(least: 1.4, most: 3.2))) + case .excited: Mood(look: MoodLook(eyesOpen: 1.15, smile: 0, arcsFlat: 0, dots: 0, eyesLift: -0.5, headDrop: 0, headLean: 0), gaze: Gaze(x: 0, y: 0), watches: false, act: MoodAct(pause: Span(least: 0.12, most: 0.45), firstPause: Span(least: 0.12, most: 0.45))) + case .sleepy: Mood(look: MoodLook(eyesOpen: 0.12, smile: 0, arcsFlat: 0.85, dots: 0, eyesLift: 1.2, headDrop: 1.2, headLean: 5), gaze: Gaze(x: 0, y: 0.4), watches: false, act: MoodAct(pause: Span(least: 3, most: 7), firstPause: Span(least: 3, most: 7))) + } + } +} + +/// The layers a face's parts move on, as `layerKinds` in `layer-motion.ts`. +/// Translations are percentages of the face; turns are in degrees. +enum FaceLayer: String, CaseIterable { + case hopRise + case hopSquash + case gazeMove + case moodMove + case wiggleMove + case gazeTilt + case moodTilt + case wiggleTilt + case hopEyesDrop + case eyesLift + case eyesGaze + case dot + + /// Where the layer rests, as its kind's `rest`. + var rest: [Double] { + switch self { + case .hopRise: [0] + case .hopSquash: [1, 1] + case .gazeMove: [0, 0] + case .moodMove: [0] + case .wiggleMove: [0] + case .gazeTilt: [0] + case .moodTilt: [0] + case .wiggleTilt: [0] + case .hopEyesDrop: [0] + case .eyesLift: [0] + case .eyesGaze: [0, 0, 1] + case .dot: [0, 0.45] + } + } +} + +/// How a face is drawn, in the square of `viewboxSize` units it is drawn in. +enum FaceGeometry { + /// As `FACE_VIEWBOX_SIZE` in `face-marks.ts`. + static let viewboxSize: Double = 40 + /// As `ARC_EYE_STROKE_WIDTH` in `face-marks.ts`. + static let arcEyeStrokeWidth: Double = 1.8 + /// As `ARC_SHUT_RISE` in `face-marks.ts`. + static let arcShutRise: Double = 2 + /// As `FLATTEST_ARC_HEIGHT` in `face-marks.ts`. + static let flattestArcHeight: Double = 0.01 + /// As `SMILE_ARC_WIDENING` in `face-marks.ts`. + static let smileArcWidening: Double = 0.6 + /// As `SMILE_ARC_BASELINE_SHARE` in `face-marks.ts`. + static let smileArcBaselineShare: Double = 0.4 + /// As `OPEN_EYE_FADES` in `face-marks.ts`. + static let openEyeFades: ClosedRange = 0 ... 0.5 + /// As `SMILE_ARC_SHOWS` in `face-marks.ts`. + static let smileArcShows: ClosedRange = 0 ... 0.3 + /// As `THINKING_DOT_RADIUS` in `face-marks.ts`. + static let thinkingDotRadius: Double = 2.4 + /// As `THINKING_DOT_XS` in `face-marks.ts`. + static let thinkingDotXs: [Double] = [14, 20, 26] + /// As `THINKING_DOT_Y` in `face-marks.ts`. + static let thinkingDotY: Double = 20 +} + +/// The numbers and paths a face moves by. Distances are in the face's +/// viewbox units unless they say they're percentages of the face. +enum FaceMotion { + /// As `TICK_SECONDS` in `layer-motion.ts`. + static let tickSeconds: Double = 0.03333333333333333 + /// As `SPRING_SETTLE_STEP_MILLISECONDS` in `layer-motion.ts`. + static let springSettleStepMilliseconds: Double = 8.333333333333334 + /// As `LONGEST_SPRING_MILLISECONDS` in `layer-motion.ts`. + static let longestSpringMilliseconds: Double = 3000 + /// As `VELOCITY_SECONDS` in `layer-motion.ts`. + static let velocitySeconds: Double = 0.004166666666666667 + /// As `LEAD_IN_SECONDS` in `layer-motion.ts`. + static let leadInSeconds: Double = 0.25 + + /// As `EYE_TRAVEL_X` in `face-rig.ts`. + static let eyeTravelX: Double = 5 + /// As `EYE_TRAVEL_Y` in `face-rig.ts`. + static let eyeTravelY: Double = 3 + /// As `EYE_EDGE_DIP` in `face-rig.ts`. + static let eyeEdgeDip: Double = 0.8 + /// As `EYE_FORESHORTENING` in `face-rig.ts`. + static let eyeForeshortening: Double = 0.18 + /// As `HEAD_TRAVEL_X` in `face-rig.ts`. + static let headTravelX: Double = 1.2 + /// As `HEAD_TRAVEL_Y` in `face-rig.ts`. + static let headTravelY: Double = 0.6 + /// As `HEAD_TILT_DEGREES` in `face-rig.ts`. + static let headTiltDegrees: Double = 4 + /// As `EYES_SPRING` in `face-rig.ts`. + static let eyesSpring: SpringTuning = SpringTuning(stiffness: 1000, damping: 60) + /// As `HEAD_SPRING` in `face-rig.ts`. + static let headSpring: SpringTuning = SpringTuning(stiffness: 140, damping: 13) + /// As `MOOD_SPRING` in `face-rig.ts`. + static let moodSpring: SpringTuning = SpringTuning(stiffness: 120, damping: 20) + /// As `EYE_SHAPE_MOOD_SPRING` in `face-rig.ts`. + static let eyeShapeMoodSpring: SpringTuning = SpringTuning(stiffness: 120, damping: 20, restDelta: 0.01, restSpeed: 0.02) + /// As `BLINK` in `face-rig.ts`. + static let blink: Keyframes = Keyframes(frames: [[1], [0.1], [0.1], [1]], times: [0, 0.33, 0.5, 1], ease: [.easeInOut, .easeInOut, .easeInOut], seconds: 0.24) + /// As `BLINK_TWICE_GAP_SECONDS` in `face-rig.ts`. + static let blinkTwiceGapSeconds: Double = 0.08 + /// As `WATCHING_REST_MS` in `face-rig.ts`, in seconds. + static let watchingRestSeconds: Double = 4 + /// As `FLUTTER` in `face-rig.ts`. + static let flutter: Keyframes = Keyframes(frames: [[0], [0.5], [0]], times: [0, 0.3, 1], ease: [.easeOut, .easeIn], seconds: 1.2) + /// As `WIGGLE_MOVE` in `face-rig.ts`. + static let wiggleMove: Keyframes = Keyframes(frames: [[0], [1.5], [0], [-1.5], [0]], times: [0, 0.25, 0.5, 0.75, 1], ease: [.easeInOut, .easeInOut, .easeInOut, .easeInOut], seconds: 0.7) + /// As `WIGGLE_TILT` in `face-rig.ts`. + static let wiggleTilt: Keyframes = Keyframes(frames: [[0], [6], [0], [-6], [0]], times: [0, 0.25, 0.5, 0.75, 1], ease: [.easeInOut, .easeInOut, .easeInOut, .easeInOut], seconds: 0.7) + /// As `DOT_RISE` in `face-rig.ts`. + static let dotRise: Keyframes = Keyframes(frames: [[0, 0.45], [0.5416666666666667, 1], [0, 0.45], [0, 0.45]], times: [0, 0.3, 0.6, 1], ease: [.easeInOut, .easeInOut, .easeInOut], seconds: 1.2) + /// As `DOT_STAGGER_SECONDS` in `face-rig.ts`. + static let dotStaggerSeconds: Double = 0.15 + /// As `OUTER_DOTS_GROW` in `face-rig.ts`. + static let outerDotsGrow: ClosedRange = 0 ... 0.6 + /// As `MIDDLE_DOT_GROWS` in `face-rig.ts`. + static let middleDotGrows: ClosedRange = 0.4 ... 1 + /// As `STILLING` in `face-rig.ts`, its `seconds`. + static let stillingSeconds: Double = 0.25 + /// As `STILLING` in `face-rig.ts`, its `ease`. + static let stillingEase: Easing = .easeOut + + /// As `HOP_SECONDS` in `hop.ts`. + static let hopSeconds: Double = 0.56 + /// As `FULL_HOP` in `hop.ts`, by the layer each path list moves. + static let fullHop: [FaceLayer: [FacePath]] = [.hopRise: [.keyframes(Keyframes(frames: [[0], [0], [-11.25], [-17.5], [0], [0]], times: [0, 0.14285714285714285, 0.35714285714285715, 0.5714285714285714, 0.8928571428571428, 1], ease: [.linear, .linear, .easeOut, .easeIn, .linear], seconds: 0.56))], .hopSquash: [.keyframes(Keyframes(frames: [[1, 1], [1.1, 0.88], [0.92, 1.1], [1, 1], [0.95, 1.06], [1.1, 0.88]], times: [0, 0.14285714285714285, 0.35714285714285715, 0.5714285714285714, 0.8928571428571428, 1], ease: [.easeInOut, .easeInOut, .easeInOut, .easeInOut, .easeInOut], seconds: 0.56)), .spring(to: [1, 1], SpringTuning(stiffness: 500, damping: 12))], .hopEyesDrop: [.keyframes(Keyframes(frames: [[0], [2], [3], [-2], [-1.5], [3.75]], times: [0, 0.14285714285714285, 0.35714285714285715, 0.5714285714285714, 0.8928571428571428, 1], ease: [.easeInOut, .easeInOut, .easeInOut, .easeInOut, .easeInOut], seconds: 0.56)), .spring(to: [0], SpringTuning(stiffness: 500, damping: 12))]] + + /// As `IDLE_BLINK_CHANCE` in `moods.ts`. + static let idleBlinkChance: Double = 0.3 + /// As `DOUBLE_BLINK_CHANCE` in `moods.ts`. + static let doubleBlinkChance: Double = 0.2 + /// As `LOOK_AHEAD_CHANCE` in `moods.ts`. + static let lookAheadChance: Double = 0.35 + /// As `IDLE_GLANCE_REACH` in `moods.ts`. + static let idleGlanceReach: Double = 1 + /// As `LONG_GLANCE_DISTANCE` in `moods.ts`. + static let longGlanceDistance: Double = 0.6 + /// As `LONG_GLANCE_BLINK_CHANCE` in `moods.ts`. + static let longGlanceBlinkChance: Double = 0.5 + /// As `HAPPY_BOUNCE` in `moods.ts`. + static let happyBounce: Double = 0.4 + /// As `HAPPY_BOUNCE_CHANCE` in `moods.ts`. + static let happyBounceChance: Double = 0.45 + /// As `HAPPY_BLINK_CHANCE` in `moods.ts`. + static let happyBlinkChance: Double = 0.25 + /// As `HAPPY_GLANCE_REACH` in `moods.ts`. + static let happyGlanceReach: Double = 0.4 + /// As `EXCITED_BOUNCE` in `moods.ts`. + static let excitedBounce: Span = Span(least: 0.25, most: 0.5) + /// As `EXCITED_FULL_HOP_CHANCE` in `moods.ts`. + static let excitedFullHopChance: Double = 0.3 + /// As `FLUTTER_CHANCE` in `moods.ts`. + static let flutterChance: Double = 0.5 + /// As `GLANCE_LEAST_SHARE` in `moods.ts`. + static let glanceLeastShare: Double = 0.5 + /// As `GLANCE_VERTICAL_SHARE` in `moods.ts`. + static let glanceVerticalShare: Double = 0.6 + + /// As `OFTEN_REST_SHARE` in `mood-loop.ts`. + static let oftenRestShare: Double = 0.5 + /// As `WATCHING_REACH` in `watching.ts`. + static let watchingReach: Double = 2.5 + /// As `JUMP_INTERVAL_MS` in `watching.ts`, in seconds. + static let watchingJumpSeconds: Double = 0.1 + + /// As `WATCHING_MOODS` in `screen-moods.ts`. + static let watchingMoods: MoodLoop = MoodLoop(rest: MoodSpell(mood: .idle, weight: 1, seconds: Span(least: 25, most: 60)), others: [MoodSpell(mood: .happy, weight: 2, seconds: Span(least: 1.5, most: 2.5)), MoodSpell(mood: .excited, weight: 1, seconds: Span(least: 1, most: 1.5))]) + /// As `CREATION_MOODS` in `screen-moods.ts`. + static let creationMoods: MoodLoop = MoodLoop(rest: MoodSpell(mood: .idle, weight: 1, seconds: Span(least: 4, most: 9)), others: [MoodSpell(mood: .happy, weight: 3, seconds: Span(least: 2, most: 4)), MoodSpell(mood: .excited, weight: 1, seconds: Span(least: 1.2, most: 2))]) +} diff --git a/apps/ios/Sugabots/Design/FacePaths.swift b/apps/ios/Sugabots/Design/FacePaths.swift new file mode 100644 index 000000000..7f82d92e9 --- /dev/null +++ b/apps/ios/Sugabots/Design/FacePaths.swift @@ -0,0 +1,387 @@ +import Foundation + +/* + * How a lively face's parts move, as `layer-motion.ts` moves the web's: each + * part rests somewhere and moves along paths, springs and keyframes, whose + * changes fall on the ticks of one clock shared by every face. The paths are + * worked out from the time alone, so a face is drawn by asking where each + * part is at the clock's latest tick. + */ + +/// A span from `least` to `most`, of seconds or a size, that something is picked from at random, as `Span`. +struct Span { + let least: Double + let most: Double + + /// A number picked at random, evenly, from the span, as `randomIn`. + func random() -> Double { + least + Double.random(in: 0 ..< 1) * (most - least) + } +} + +/// A spring's physics, as motion's `spring` takes them; it rests once within +/// `restDelta` of where it's going and slower than `restSpeed`, or motion's +/// defaults for how far it goes when not given. +struct SpringTuning { + let stiffness: Double + let damping: Double + var restDelta: Double? + var restSpeed: Double? +} + +extension ClosedRange { + /// How far `value` is across the range, from 0 at its lower bound to 1 at + /// its upper, held at those beyond it, as motion's `useTransform` maps a + /// value from one range onto 0 to 1. + func share(of value: Double) -> Double { + Swift.min(1, Swift.max(0, (value - lowerBound) / (upperBound - lowerBound))) + } +} + +/// How a keyframe eases into the next, as motion's named easings, by their names. +enum Easing: String { + case linear, easeIn, easeOut, easeInOut + + func callAsFunction(_ progress: Double) -> Double { + switch self { + case .linear: progress + case .easeIn: Self.cubicBezier(0.42, 0, 1, 1, at: progress) + case .easeOut: Self.cubicBezier(0, 0, 0.58, 1, at: progress) + case .easeInOut: Self.cubicBezier(0.42, 0, 0.58, 1, at: progress) + } + } + + /// A CSS cubic Bézier's progress at `x`, found by halving as motion's `cubicBezier` does. + private static func cubicBezier(_ x1: Double, _ y1: Double, _ x2: Double, _ y2: Double, at x: Double) -> Double { + if x <= 0 { return 0 } + if x >= 1 { return 1 } + func along(_ t: Double, _ first: Double, _ second: Double) -> Double { + (((1 - 3 * second + 3 * first) * t + (3 * second - 6 * first)) * t + 3 * first) * t + } + var lower = 0.0 + var upper = 1.0 + var t = 0.5 + for _ in 0 ..< Self.subdivisions { + t = lower + (upper - lower) / 2 + let error = along(t, x1, x2) - x + if abs(error) <= Self.subdivisionPrecision { break } + if error > 0 { upper = t } else { lower = t } + } + return along(t, y1, y2) + } + + private static let subdivisions = 12 + private static let subdivisionPrecision = 0.0000001 +} + +/// Values passing through `frames` at `times` (shares of `seconds`, from 0 to 1), eased between each pair by `ease`. +struct Keyframes { + let frames: [[Double]] + let times: [Double] + let ease: [Easing] + let seconds: Double +} + +/// A way of getting from one set of values to another, as `Path`. A keyframes +/// path's first frame is replaced by wherever the part is when it starts. +enum FacePath { + case spring(to: [Double], SpringTuning) + case keyframes(Keyframes) + + /// The path with each value's distance from `rest` made `size` times as far. + func scaled(by size: Double, about rest: [Double]) -> FacePath { + func about(_ values: [Double]) -> [Double] { + values.indices.map { index in + let resting = index < rest.count ? rest[index] : 0 + return resting + (values[index] - resting) * size + } + } + switch self { + case let .spring(to, tuning): + return .spring(to: about(to), tuning) + case let .keyframes(keyframes): + return .keyframes(Keyframes(frames: keyframes.frames.map(about), times: keyframes.times, ease: keyframes.ease, seconds: keyframes.seconds)) + } + } + + /// The paths of one hop, `size` times as big as a full one, for each layer it moves, as `hopPaths`. + static func hop(size: Double) -> [FaceLayer: [FacePath]] { + FaceMotion.fullHop.reduce(into: [:]) { hop, layer in + hop[layer.key] = layer.value.map { $0.scaled(by: size, about: layer.key.rest) } + } + } +} + +/// Values over time, from 0 to `seconds`, where they stay. +struct Timeline { + let seconds: Double + let valuesAt: (Double) -> [Double] + + /// Paths one after another, each from where the last ends; the first starts at `velocity`, where it's a spring, as `sequence`. + static func of(_ paths: [FacePath], from start: [Double], velocity: [Double]? = nil) -> Timeline { + var parts: [(startsAt: Double, timeline: Timeline)] = [] + var from = start + var total = 0.0 + for (index, path) in paths.enumerated() { + let part = Timeline(path, from: from, velocity: index == 0 ? velocity : nil) + parts.append((total, part)) + total += part.seconds + from = part.valuesAt(part.seconds) + } + return Timeline(seconds: total) { time in + guard let part = parts.last(where: { time >= $0.startsAt }) ?? parts.first else { return start } + return part.timeline.valuesAt(min(time - part.startsAt, part.timeline.seconds)) + } + } + + /// One path from `from`, moving at `velocity` per second where it's a spring, as `pathFrom`. + init(_ path: FacePath, from: [Double], velocity: [Double]?) { + switch path { + case let .spring(to, tuning): + // One spring for each value, so each carries on at the speed it was moving. + let springs = from.enumerated().map { index, value in + SpringCurve(from: value, to: index < to.count ? to[index] : value, velocity: velocity.map { index < $0.count ? $0[index] : 0 } ?? 0, tuning: tuning) + } + let seconds = springs.map(\.settlingSeconds).max() ?? 0 + self.init(seconds: seconds) { time in + springs.map { time >= seconds ? $0.to : $0.value(at: max(0, time)) } + } + case let .keyframes(keyframes): + let frames = [from] + keyframes.frames.dropFirst() + let moments = keyframes.times.map { $0 * keyframes.seconds } + self.init(seconds: keyframes.seconds) { time in + Self.interpolate(frames, at: moments, easing: keyframes.ease, time: time) + } + } + } + + init(seconds: Double, valuesAt: @escaping (Double) -> [Double]) { + self.seconds = seconds + self.valuesAt = valuesAt + } + + /// Where `frames` have got to at `time`, held at the first before it starts and the last after, as motion's `interpolate`. + private static func interpolate(_ frames: [[Double]], at moments: [Double], easing: [Easing], time: Double) -> [Double] { + guard let first = frames.first, let last = frames.last else { return [] } + if time <= moments.first ?? 0 { return first } + guard let segment = moments.indices.dropLast().last(where: { moments[$0] <= time }), segment + 1 < frames.count, time < moments[segment + 1] else { return last } + let span = moments[segment + 1] - moments[segment] + let progress = span > 0 ? (time - moments[segment]) / span : 1 + let eased = (segment < easing.count ? easing[segment] : .linear)(progress) + let start = frames[segment] + let end = frames[segment + 1] + return start.indices.map { index in + let to = index < end.count ? end[index] : start[index] + return start[index] + (to - start[index]) * eased + } + } +} + +/// One value springing from `from` to `to`, as motion's `spring` generator, +/// in seconds rather than its milliseconds. Once it is close enough to `to` +/// and slow enough to rest, it is at `to`. +private struct SpringCurve { + let from: Double + let to: Double + let velocity: Double + let tuning: SpringTuning + + /// What's taken as close enough and slow enough to rest, as motion picks for a change this large when not told. + private var restDelta: Double { tuning.restDelta ?? (abs(to - from) < Self.granularScale ? 0.005 : 0.5) } + private var restSpeed: Double { tuning.restSpeed ?? (abs(to - from) < Self.granularScale ? 0.01 : 2) } + /// Below this change motion counts a spring as fine-grained, and rests it sooner. + private static let granularScale = 5.0 + + private var delta: Double { to - from } + /// Motion's springs count velocity towards the start, hence the sign. + private var startSpeed: Double { -velocity } + private var undampedFrequency: Double { tuning.stiffness.squareRoot() } + private var dampingRatio: Double { tuning.damping / (2 * tuning.stiffness.squareRoot()) } + private var decay: Double { dampingRatio * undampedFrequency } + + func value(at time: Double) -> Double { + isResting(at: time) ? to : unrested(at: time) + } + + /// Where the spring has got to at `time`, before it's taken to rest. + private func unrested(at time: Double) -> Double { + let frequency = undampedFrequency + let ratio = dampingRatio + if ratio < 1 { + let angular = frequency * (1 - ratio * ratio).squareRoot() + return to - exp(-decay * time) * ((startSpeed + decay * delta) / angular * sin(angular * time) + delta * cos(angular * time)) + } + if ratio == 1 { + return to - exp(-frequency * time) * (delta + (startSpeed + frequency * delta) * time) + } + let damped = frequency * (ratio * ratio - 1).squareRoot() + let turned = min(damped * time, Self.hyperbolicLimit) + return to - exp(-decay * time) * ((startSpeed + decay * delta) * sinh(turned) + damped * delta * cosh(turned)) / damped + } + + /// How fast the spring moves at `time`, per second, worked out as motion works it out. + private func speed(at time: Double) -> Double { + let frequency = undampedFrequency + let ratio = dampingRatio + if ratio < 1 { + let angular = frequency * (1 - ratio * ratio).squareRoot() + let sway = (startSpeed + decay * delta) / angular + let sinePart = decay * sway + delta * angular + let cosinePart = decay * delta - sway * angular + return exp(-decay * time) * (sinePart * sin(angular * time) + cosinePart * cos(angular * time)) + } + if ratio == 1 { + return exp(-frequency * time) * (frequency * (startSpeed + frequency * delta) * time - startSpeed) + } + let damped = frequency * (ratio * ratio - 1).squareRoot() + let sway = (startSpeed + decay * delta) / damped + let sinhPart = decay * sway - delta * damped + let coshPart = decay * delta - sway * damped + let turned = min(damped * time, Self.hyperbolicLimit) + return exp(-decay * time) * (sinhPart * sinh(turned) + coshPart * cosh(turned)) + } + + /// Where sinh and cosh are capped, as motion caps them, so they never overflow. + private static let hyperbolicLimit = 300.0 + + /// How long it takes to rest, stepped through in milliseconds as + /// `layer-motion.ts` steps through motion's springs, so it stops on the + /// same step, and cut off at the longest a spring may run. + var settlingSeconds: Double { + var milliseconds = 0.0 + while !isResting(at: milliseconds / 1000), milliseconds < FaceMotion.longestSpringMilliseconds { + milliseconds += FaceMotion.springSettleStepMilliseconds + } + return min(milliseconds, FaceMotion.longestSpringMilliseconds) / 1000 + } + + private func isResting(at time: Double) -> Bool { + abs(speed(at: time)) <= restSpeed && abs(to - unrested(at: time)) <= restDelta + } +} + +/// The one clock every lively face's moves fall on, as `layer-motion.ts`'s: +/// ticks counted from the same moment for every face, so faces moving at once +/// change together, and no more often than a video's frames. +enum FaceClock { + /// Now, in seconds, as every face's paths are timed. + static func now() -> Double { + Date.now.timeIntervalSinceReferenceDate + } + + /// The latest tick at or before `date`. + static func tick(at date: Date) -> Double { + (date.timeIntervalSinceReferenceDate / FaceMotion.tickSeconds).rounded(.down) * FaceMotion.tickSeconds + } + + /// The first tick at or after `time`. + static func tick(from time: Double) -> Double { + (time / FaceMotion.tickSeconds).rounded(.up) * FaceMotion.tickSeconds + } + + /// `seconds` rounded up to a whole number of ticks, so a repeating path stays on the clock. + static func wholeTicks(_ seconds: Double) -> Double { + max(1, (seconds / FaceMotion.tickSeconds - Self.tickTolerance).rounded(.up)) * FaceMotion.tickSeconds + } + + /// How far under a whole number of ticks a length may fall and still count as it, against rounding error. + private static let tickTolerance = 0.000001 +} + +/// One of a face's moving parts, as `LayerMotion`: values resting somewhere, +/// moving along paths started on the clock's ticks. +struct FaceTrack { + let rest: [Double] + private var held: [Double] + private var playing: Playing? + + init(rest: [Double]) { + self.rest = rest + held = rest + } + + /// What's under way: perhaps a lead-in once, and then a timeline, once or again and again. + private struct Playing { + var leadIn: (startsAt: Double, timeline: Timeline)? + let startsAt: Double + let timeline: Timeline + let repeats: Bool + + /// When it comes to rest; never, while it repeats. + var endsAt: Double { + repeats ? .infinity : startsAt + timeline.seconds + } + + func values(at time: Double) -> [Double] { + if let leadIn, time < startsAt { + return leadIn.timeline.valuesAt(min(max(0, time - leadIn.startsAt), leadIn.timeline.seconds)) + } + var elapsed = max(0, time - startsAt) + if repeats { elapsed = elapsed.isFinite ? elapsed.truncatingRemainder(dividingBy: timeline.seconds) : 0 } + return timeline.valuesAt(min(elapsed, timeline.seconds)) + } + } + + /// When the part comes to rest: never while it repeats, and already when nothing is under way. + var endsAt: Double { + playing?.endsAt ?? -.infinity + } + + func values(at time: Double) -> [Double] { + playing?.values(at: time) ?? held + } + + /// How fast each value is changing at `time`, per second, so a new spring carries on at the same speed. + private func velocity(at time: Double) -> [Double] { + guard let playing else { return held.map { _ in 0 } } + let before = playing.values(at: time) + let after = playing.values(at: min(time + FaceMotion.velocitySeconds, playing.endsAt)) + return before.indices.map { index in ((index < after.count ? after[index] : before[index]) - before[index]) / FaceMotion.velocitySeconds } + } + + /// Sets the part straight to `values`, with nothing under way. + mutating func jump(to values: [Double]) { + playing = nil + held = values + } + + /// Stops where it is at `time`, holding that pose. + mutating func stop(at time: Double) { + held = values(at: time) + playing = nil + } + + /// Plays `paths` one after another from wherever the part is at `time` + /// (or from `start`, when given), at the speed it's moving: again and again + /// if `repeating`, after `delaySeconds`. A repeating path first eases to + /// where it starts, so each time round begins from the same place. + mutating func play(_ paths: [FacePath], at time: Double, repeating: Bool = false, delaySeconds: Double = 0, from start: [Double]? = nil) { + let from = start ?? values(at: time) + let velocity = start == nil ? velocity(at: time) : nil + let startsAt = FaceClock.tick(from: time) + if repeating, case let .keyframes(first)? = paths.first, let loopStart = first.frames.first, Self.apart(from, loopStart) { + let leadIn = Timeline.of([.keyframes(Keyframes(frames: [from, loopStart], times: [0, 1], ease: [.easeInOut], seconds: FaceMotion.leadInSeconds))], from: from) + let leadInEnds = startsAt + FaceClock.wholeTicks(leadIn.seconds) + playing = Playing( + leadIn: (startsAt, leadIn), + startsAt: FaceClock.tick(from: leadInEnds + delaySeconds), + timeline: Self.onTicks(Timeline.of(paths, from: loopStart)), + repeats: true + ) + return + } + playing = Playing(startsAt: FaceClock.tick(from: time + delaySeconds), timeline: Self.onTicks(Timeline.of(paths, from: from, velocity: velocity)), repeats: repeating) + } + + /// A timeline a whole number of ticks long, holding its end for the rest. + private static func onTicks(_ timeline: Timeline) -> Timeline { + Timeline(seconds: FaceClock.wholeTicks(timeline.seconds)) { timeline.valuesAt(min($0, timeline.seconds)) } + } + + /// Whether two poses differ by more than rounding. + private static func apart(_ left: [Double], _ right: [Double]) -> Bool { + zip(left, right).contains { abs($0 - $1) > samePoseTolerance } + } + + private static let samePoseTolerance = 0.0001 +} diff --git a/apps/ios/Sugabots/Design/FaceRig.swift b/apps/ios/Sugabots/Design/FaceRig.swift new file mode 100644 index 000000000..1cb9f75ae --- /dev/null +++ b/apps/ios/Sugabots/Design/FaceRig.swift @@ -0,0 +1,483 @@ +import CoreGraphics +import Foundation +import Observation + +/// Where the eyes look, from -1 to 1 on each axis, as a mood's `gaze`. +struct Gaze { + let x: Double + let y: Double + + static let ahead = Gaze(x: 0, y: 0) +} + +/// How a mood holds the face, eased into whenever the mood changes, as `MoodLook`. +/// Distances are in the face's 40-unit viewbox. +struct MoodLook: Equatable { + /// How open the eyes are, 1 being wide open. + let eyesOpen: Double + /// How far each open eye has become a smiling arc, 0 not at all. + let smile: Double + /// How far an arc eye has flattened into a shut lid. + let arcsFlat: Double + /// How far the eyes have given way to three thinking dots, 0 not at all. + let dots: Double + /// How far the eyes sit above (negative) or below their place. + let eyesLift: Double + /// How far the head sinks, and leans in degrees. + let headDrop: Double + let headLean: Double +} + +/// A small act a mood repeats, after a pause in seconds picked from `pause` each time; the first from `firstPause`. +struct MoodAct { + let pause: Span + let firstPause: Span +} + +/// One way a bot carries itself, as an entry in `moods`: how it holds its +/// face, where it looks as the mood starts, whether its eyes may follow +/// typing, and how often it acts. What it does is `BotMood.start` and `BotMood.act`. +struct Mood { + let look: MoodLook + let gaze: Gaze + let watches: Bool + let act: MoodAct? +} + +/* + * What each mood does, copied from the functions in `moods.ts`; its numbers + * and chances are generated. + */ +extension BotMood { + /// Starts what goes on for as long as the mood lasts; returns what eases it to a stop. + @MainActor func start(on face: FaceRig) -> (() -> Void)? { + switch self { + case .thinking: + return face.riseDots() + case .excited: + face.hop() + return face.wiggle() + case .still, .idle, .happy, .sleepy: + return nil + } + } + + /// The small act the mood repeats. + @MainActor func act(on face: FaceRig) { + switch self { + case .idle: glanceOrBlink(face) + case .happy: bounceOrBlink(face) + case .excited: bounceOrHop(face) + case .sleepy: if Self.happens(FaceMotion.flutterChance) { face.flutter() } + case .still, .thinking: break + } + } + + /// Blinks now and then; otherwise glances somewhere new, unless it's watching typing. + @MainActor private func glanceOrBlink(_ face: FaceRig) { + if face.isWatching || Self.happens(FaceMotion.idleBlinkChance) { + face.blink(twice: Self.happens(FaceMotion.doubleBlinkChance)) + return + } + let gaze = Self.happens(FaceMotion.lookAheadChance) ? Gaze.ahead : Self.randomGlance(reach: FaceMotion.idleGlanceReach) + // People blink as their eyes shift a long way. + if face.look(at: gaze) > FaceMotion.longGlanceDistance, Self.happens(FaceMotion.longGlanceBlinkChance) { face.blink() } + } + + @MainActor private func bounceOrBlink(_ face: FaceRig) { + let roll = Double.random(in: 0 ..< 1) + if roll < FaceMotion.happyBounceChance { + face.hop(size: FaceMotion.happyBounce) + } else if roll < FaceMotion.happyBounceChance + FaceMotion.happyBlinkChance { + face.blink() + } else if !face.isWatching { + face.look(at: Self.randomGlance(reach: FaceMotion.happyGlanceReach)) + } + } + + @MainActor private func bounceOrHop(_ face: FaceRig) { + // Never cuts a hop short in the air. + guard !face.isAirborne else { return } + face.hop(size: Self.happens(FaceMotion.excitedFullHopChance) ? 1 : FaceMotion.excitedBounce.random()) + } + + /// Whether something with `chance` of happening, from 0 to 1, happens this time. + private static func happens(_ chance: Double) -> Bool { + Double.random(in: 0 ..< 1) < chance + } + + /// Somewhere to glance, up to `reach` from straight ahead, in any direction, as `randomGlance`. + private static func randomGlance(reach: Double) -> Gaze { + let angle = Double.random(in: 0 ..< 1) * .pi * 2 + let distance = (FaceMotion.glanceLeastShare + Double.random(in: 0 ..< 1) * (1 - FaceMotion.glanceLeastShare)) * reach + return Gaze(x: cos(angle) * distance, y: sin(angle) * distance * FaceMotion.glanceVerticalShare) + } +} + +/// Where each of a face's parts is at one moment, which is all it takes to draw it. +/// Translations are percentages of the face, as the web's layers move. +struct FacePose { + let hopRise: Double + let hopSquash: (width: Double, height: Double) + let gazeMove: (x: Double, y: Double) + let moodMove: Double + let wiggleMove: Double + let gazeTiltDegrees: Double + let moodTiltDegrees: Double + let wiggleTiltDegrees: Double + let hopEyesDrop: Double + let eyesLift: Double + let eyesGaze: (x: Double, y: Double, width: Double) + /// Each thinking dot's rise, as a share of its height, and brightness. + let dots: [(rise: Double, opacity: Double)] + /// How tall an open eye is drawn, before the shut-eye minimum. + let eyeHeight: Double + let eyesShrink: Double + let smile: Double + let arcsFlat: Double + let outerDotsGrow: Double + let middleDotGrows: Double +} + +/// A lively face's moving parts, as `useFaceRig`: held as its mood's look +/// says, and moved by its mood's acts. It knows when anything is moving, so +/// its face is redrawn on the clock's ticks only then. +@MainActor +@Observable +final class FaceRig { + /// Whether any part is moving now. + private(set) var moving = false + /// Counts every change to the parts, so a face at rest is drawn again when one jumps. + private(set) var revision = 0 + /// When the last part to move comes to rest. + private(set) var settledAt = 0.0 + + /// The parts that have been moved or set; any other is at its rest. + @ObservationIgnored private var parts: [Part: FaceTrack] = [:] + @ObservationIgnored private var settling: Task? + @ObservationIgnored private var gaze = Gaze.ahead + @ObservationIgnored private var watchedAt = -Double.infinity + @ObservationIgnored private var landsAt = 0.0 + @ObservationIgnored private var heldLook: MoodLook? + @ObservationIgnored private var stopGoingOn: (() -> Void)? + @ObservationIgnored private var acting: Task? + @ObservationIgnored private var lastJumpToTyping = -Double.infinity + @ObservationIgnored private var jumpingToTyping: Task? + @ObservationIgnored private var typingTarget: (point: CGPoint, face: CGRect)? + + /// The face's parts: the web's layers, and the values its eyes' shapes are drawn with. + private enum Part: Hashable { + /// One of the face's layers, other than a thinking dot's. + case layer(FaceLayer) + /// The thinking dot at this index, from the left, on a layer of the `dot` kind. + case thinkingDot(Int) + case blink, flutter, eyesOpen, smile, arcsFlat, dots + + var rest: [Double] { + let resting = BotMood.still.mood.look + switch self { + case let .layer(layer): return layer.rest + case .thinkingDot: return FaceLayer.dot.rest + case .blink: return FaceMotion.blink.frames[0] + case .flutter: return FaceMotion.flutter.frames[0] + case .eyesOpen: return [resting.eyesOpen] + case .smile: return [resting.smile] + case .arcsFlat: return [resting.arcsFlat] + case .dots: return [resting.dots] + } + } + + static let thinkingDots = FaceGeometry.thinkingDotXs.indices.map(Part.thinkingDot) + } + + // MARK: Moods + + /// Puts the face in `mood`: eases it into the mood's look (or sets it, the + /// first time), turns its eyes where the mood starts looking, and starts + /// what the mood does, ending what the last mood did. + func enter(_ mood: BotMood) { + leaveMood() + let behaviour = mood.mood + hold(behaviour.look) + look(at: behaviour.gaze) + stopGoingOn = mood.start(on: self) + guard let act = behaviour.act else { return } + acting = Task { [weak self] in + var pause = act.firstPause + while (try? await Task.sleep(for: .seconds(pause.random()))) != nil { + guard let self else { return } + mood.act(on: self) + pause = act.pause + } + } + } + + /// Ends what the mood was doing, easing anything it set going to a stop. + func leaveMood() { + acting?.cancel() + acting = nil + stopGoingOn?() + stopGoingOn = nil + } + + /// Stops everything the face does of its own accord, as it goes from + /// view: its mood's acts, easing to a stop what the mood set going, and + /// any wait to look at typing. Parts already moving finish by the clock, + /// so the face is where they end when it shows again. + func stopActing() { + leaveMood() + jumpingToTyping?.cancel() + jumpingToTyping = nil + } + + /// Holds the head and eyes as `look` says: eased there gently when the + /// look changes, and set straight away the first time. + private func hold(_ look: MoodLook) { + let first = heldLook == nil + guard heldLook != look else { return } + heldLook = look + let poses: [(Part, [Double], SpringTuning)] = [ + (.layer(.moodMove), [Self.percent(look.headDrop)], FaceMotion.moodSpring), + (.layer(.moodTilt), [look.headLean], FaceMotion.moodSpring), + (.layer(.eyesLift), [Self.percent(look.eyesLift)], FaceMotion.moodSpring), + (.eyesOpen, [look.eyesOpen], FaceMotion.eyeShapeMoodSpring), + (.smile, [look.smile], FaceMotion.eyeShapeMoodSpring), + (.arcsFlat, [look.arcsFlat], FaceMotion.eyeShapeMoodSpring), + (.dots, [look.dots], FaceMotion.eyeShapeMoodSpring), + ] + for (part, pose, spring) in poses { + if first { parts[part, default: FaceTrack(rest: part.rest)].jump(to: pose) } else { play(part, [.spring(to: pose, spring)]) } + } + changed() + } + + // MARK: Controls, as `FaceControls` + + /// Turns the eyes, and after them the head, to `gaze`; returns how far they turned. + @discardableResult + func look(at target: Gaze) -> Double { + let distance = hypot(target.x - gaze.x, target.y - gaze.y) + gaze = target + let eyes = [ + Self.percent(target.x * FaceMotion.eyeTravelX), + Self.percent(target.y * FaceMotion.eyeTravelY + abs(target.x) * FaceMotion.eyeEdgeDip), + 1 - abs(target.x) * FaceMotion.eyeForeshortening, + ] + play(.layer(.eyesGaze), [.spring(to: eyes, FaceMotion.eyesSpring)]) + play(.layer(.gazeMove), [.spring(to: [Self.percent(target.x * FaceMotion.headTravelX), Self.percent(target.y * FaceMotion.headTravelY)], FaceMotion.headSpring)]) + play(.layer(.gazeTilt), [.spring(to: [target.x * FaceMotion.headTiltDegrees], FaceMotion.headSpring)]) + changed() + return distance + } + + /// Whether the eyes have been on typing lately, which idle glances then leave alone. + var isWatching: Bool { + FaceClock.now() - watchedAt < FaceMotion.watchingRestSeconds + } + + /// Blinks, as `BLINK`; a double blink holds the eye open `blinkTwiceGapSeconds` between the two. + func blink(twice: Bool = false) { + let open = FaceMotion.blink.frames[0] + let once = FacePath.keyframes(FaceMotion.blink) + let gap = FacePath.keyframes(Keyframes(frames: [open, open], times: [0, 1], ease: [.linear], seconds: FaceMotion.blinkTwiceGapSeconds)) + play(.blink, twice ? [once, gap, once] : [once], from: open) + changed() + } + + /// A hop `size` times as big as a full one; a new hop takes over from one in the air. + func hop(size: Double = 1) { + landsAt = FaceClock.now() + FaceMotion.hopSeconds + for (layer, paths) in FacePath.hop(size: size) { + play(.layer(layer), paths) + } + changed() + } + + var isAirborne: Bool { + FaceClock.now() < landsAt + } + + /// Wiggles the head from side to side until the returned function eases it still. + func wiggle() -> () -> Void { + play(.layer(.wiggleMove), [.keyframes(FaceMotion.wiggleMove)], repeating: true) + play(.layer(.wiggleTilt), [.keyframes(FaceMotion.wiggleTilt)], repeating: true) + changed() + return { [weak self] in + self?.easeToRest([.layer(.wiggleMove), .layer(.wiggleTilt)]) + } + } + + /// Raises the thinking dots in turn until the returned function settles them. + func riseDots() -> () -> Void { + for (index, dot) in Part.thinkingDots.enumerated() { + play(dot, [.keyframes(FaceMotion.dotRise)], repeating: true, delaySeconds: Double(index) * FaceMotion.dotStaggerSeconds) + } + changed() + return { [weak self] in + self?.easeToRest(Part.thinkingDots) + } + } + + /// Opens the eyes half way for a moment, however shut its mood holds them. + func flutter() { + play(.flutter, [.keyframes(FaceMotion.flutter)], from: FaceMotion.flutter.frames[0]) + changed() + } + + /// Turns the eyes to where someone types, at `point`, from a face at + /// `face`, both in the window's points, as `useWatching`: at once, then at + /// most every `watchingJumpSeconds` at the latest point. + func watchTyping(at point: CGPoint, from face: CGRect) { + // A face laid out at no size has nowhere to look from. + guard face.width > 0 else { return } + typingTarget = (point, face) + guard jumpingToTyping == nil else { return } + let wait = lastJumpToTyping + FaceMotion.watchingJumpSeconds - FaceClock.now() + guard wait > 0 else { + jumpToTyping() + return + } + jumpingToTyping = Task { [weak self] in + guard (try? await Task.sleep(for: .seconds(wait))) != nil else { return } + self?.jumpingToTyping = nil + self?.jumpToTyping() + } + } + + private func jumpToTyping() { + guard let (point, face) = typingTarget else { return } + lastJumpToTyping = FaceClock.now() + let reach = face.width * FaceMotion.watchingReach + let x = (point.x - face.midX) / reach + let y = (point.y - face.midY) / reach + // Pulled in to the edge of where the eyes can look, if it lies further out. + let distance = max(1, hypot(x, y)) + look(at: Gaze(x: x / distance, y: y / distance)) + watchedAt = FaceClock.now() + } + + // MARK: Drawing + + /// Where every part is at `time`. + func pose(at time: Double) -> FacePose { + Self.pose { part in parts[part]?.values(at: time) ?? part.rest } + } + + /// The face with every part at rest. + nonisolated static let restingPose = pose { $0.rest } + + /// The pose with each part's `values`. + private nonisolated static func pose(_ values: (Part) -> [Double]) -> FacePose { + func value(_ part: Part) -> Double { values(part)[0] } + let squash = values(.layer(.hopSquash)) + let gazeMove = values(.layer(.gazeMove)) + let eyesGaze = values(.layer(.eyesGaze)) + let dots = value(.dots) + return FacePose( + hopRise: value(.layer(.hopRise)), + hopSquash: (squash[0], squash[1]), + gazeMove: (gazeMove[0], gazeMove[1]), + moodMove: value(.layer(.moodMove)), + wiggleMove: value(.layer(.wiggleMove)), + gazeTiltDegrees: value(.layer(.gazeTilt)), + moodTiltDegrees: value(.layer(.moodTilt)), + wiggleTiltDegrees: value(.layer(.wiggleTilt)), + hopEyesDrop: value(.layer(.hopEyesDrop)), + eyesLift: value(.layer(.eyesLift)), + eyesGaze: (eyesGaze[0], eyesGaze[1], eyesGaze[2]), + dots: Part.thinkingDots.map { dot in + let rise = values(dot) + return (rise[0], rise[1]) + }, + eyeHeight: value(.blink) * max(value(.eyesOpen), value(.flutter)), + // The eyes shrink to points as the dots grow from where the eyes were; the middle dot comes last. + eyesShrink: 1 - dots, + smile: value(.smile), + arcsFlat: value(.arcsFlat), + outerDotsGrow: FaceMotion.outerDotsGrow.share(of: dots), + middleDotGrows: FaceMotion.middleDotGrows.share(of: dots) + ) + } + + // MARK: Parts + + private func play(_ part: Part, _ paths: [FacePath], repeating: Bool = false, delaySeconds: Double = 0, from start: [Double]? = nil) { + parts[part, default: FaceTrack(rest: part.rest)].play(paths, at: FaceClock.now(), repeating: repeating, delaySeconds: delaySeconds, from: start) + } + + /// Eases parts back to rest. + private func easeToRest(_ resting: [Part]) { + for part in resting { + let rest = part.rest + play(part, [.keyframes(Keyframes(frames: [rest, rest], times: [0, 1], ease: [FaceMotion.stillingEase], seconds: FaceMotion.stillingSeconds))]) + } + changed() + } + + /// Notes that parts have changed: the face is drawn again, and redrawn on each tick until the last comes to rest. + private func changed() { + revision += 1 + settledAt = max(0, parts.values.map(\.endsAt).max() ?? 0) + let now = FaceClock.now() + moving = settledAt > now + settling?.cancel() + guard moving, settledAt.isFinite else { return } + let wait = settledAt - now + settling = Task { [weak self] in + guard (try? await Task.sleep(for: .seconds(wait))) != nil, let self else { return } + self.moving = self.settledAt > FaceClock.now() + } + } + + /// A distance in viewbox units as a percentage of the face, as `viewboxPercentage`. + static func percent(_ units: Double) -> Double { + units / FaceGeometry.viewboxSize * 100 + } +} + +/// The moods a bot moves between, at random, by weight, as `MoodLoop`. `rest` +/// is the one it starts and rests in; when faces should move often it rests +/// there half as long. A spell never follows itself. +struct MoodLoop { + let rest: MoodSpell + let others: [MoodSpell] + + /// Moves through the loop until cancelled, showing each mood as its spell starts, as `useMoodLoop`. + @MainActor func run(liveliness: Liveliness, show: (BotMood) -> Void) async { + let spells = [rest] + others + var current = 0 + while !Task.isCancelled { + show(spells[current].mood) + let share = current == 0 && liveliness == .often ? FaceMotion.oftenRestShare : 1 + guard (try? await Task.sleep(for: .seconds(spells[current].seconds.random() * share))) != nil else { return } + current = Self.next(after: current, in: spells) + } + } + + /// A spell other than `current`, chosen at random by weight. + private static func next(after current: Int, in spells: [MoodSpell]) -> Int { + let others = spells.indices.filter { $0 != current } + var roll = Double.random(in: 0 ..< 1) * others.map { spells[$0].weight }.reduce(0, +) + for index in others { + roll -= spells[index].weight + if roll <= 0 { return index } + } + return others.last ?? current + } +} + +/// One stretch of a mood in a loop: how likely it is to come next, and how long it lasts. +struct MoodSpell { + let mood: BotMood + let weight: Double + let seconds: Span +} + +/// How the faces in a group take turns to move, as `GroupPacing`. +struct GroupPacing { + /// How many faces may move at once. + let movingAtOnce: Int + /// Seconds between turns starting, and how long a turn lasts. + let gapSeconds: Span + let turnSeconds: Span +} diff --git a/apps/ios/Sugabots/Design/Faces.swift b/apps/ios/Sugabots/Design/Faces.swift index aa23bddc1..94afb1c20 100644 --- a/apps/ios/Sugabots/Design/Faces.swift +++ b/apps/ios/Sugabots/Design/Faces.swift @@ -64,9 +64,6 @@ enum FaceMark { } } -/// The side of the square faces are drawn in, as `FACE_VIEWBOX_SIZE` in `@sugabots/avatars`. -private let faceViewboxSize = 40.0 - /// A bot's face: its head in its colour, with its eyes, `size` points square. struct BotFace: View { let look: BotLook @@ -74,24 +71,7 @@ struct BotFace: View { var body: some View { Canvas { context, canvas in - let palette = BotLooks.palette(for: look.color) - let head = BotLooks.head(for: look.shape) - let marks = BotLooks.faces(for: look.face) - context.scaleBy(x: canvas.width / faceViewboxSize, y: canvas.height / faceViewboxSize) - - for part in head.parts { - context.fill(part.path, with: .color(palette.face)) - } - - let eyes = head.eyes - let centre = faceViewboxSize / 2 - var eyeContext = context - eyeContext.translateBy(x: eyes.x, y: eyes.y) - eyeContext.scaleBy(x: eyes.scale, y: eyes.scale) - eyeContext.translateBy(x: -centre, y: -centre) - for mark in marks { - mark.draw(in: eyeContext, with: .color(palette.eyes), cap: .butt) - } + FaceDrawing.draw(FaceArt(look: look), pose: FaceRig.restingPose, in: &context, canvas: canvas, overflow: 0) } .frame(width: size, height: size) .accessibilityHidden(true) diff --git a/apps/ios/Sugabots/Design/LivelyBotFace.swift b/apps/ios/Sugabots/Design/LivelyBotFace.swift new file mode 100644 index 000000000..ea9b34b66 --- /dev/null +++ b/apps/ios/Sugabots/Design/LivelyBotFace.swift @@ -0,0 +1,418 @@ +import SwiftUI + +/* + * Bots' faces that seem alive, as `LivelyBotFace` and its kin in + * `@sugabots/avatars` and the web's `shell/Agent.tsx`. + */ + +extension Liveliness { + /// What each level is called in settings, as the web's `livelinessNames`. + var label: String { + switch self { + case .off: "Off" + case .sometimes: "Sometimes" + case .often: "Often" + } + } +} + +/// How much someone has chosen to let bots' faces move, kept on this device +/// as the web keeps it in each browser; `Liveliness.chosenByDefault` until they choose. +@propertyWrapper +struct ChosenLiveliness: DynamicProperty { + @AppStorage("sugabots-liveliness") private var chosen = Liveliness.chosenByDefault + + var wrappedValue: Liveliness { + get { chosen } + nonmutating set { chosen = newValue } + } +} + +private struct LivelinessKey: EnvironmentKey { + /// With nothing set, faces stay still, so a preview only gets movement it asks for. + static let defaultValue = Liveliness.off +} + +extension EnvironmentValues { + /// How much the faces inside may move: as chosen, or not at all when the system asks for reduced motion. + var liveliness: Liveliness { + get { self[LivelinessKey.self] } + set { self[LivelinessKey.self] = newValue } + } +} + +/// Where someone last typed, in the window's points, for faces that watch typing, as the web's faces watch `input` events. +@MainActor +@Observable +final class TypingWatch { + private(set) var latest: Typed? + + struct Typed: Equatable { + let point: CGPoint + let at: Double + } + + func typed(at point: CGPoint) { + latest = Typed(point: point, at: FaceClock.now()) + } +} + +/// A bot's face that seems alive, in `mood` (see `BotMood`): its eyes slide +/// across the head and narrow towards its edge while the head leans after +/// them. It hops when tapped. Where faces may not move (see `liveliness`) it +/// is the still `BotFace`. +struct LivelyBotFace: View { + let look: BotLook + var size: CGFloat = 24 + var mood = BotMood.idle + /// Its eyes turn to where someone types, and wander again once they stop. + var watchTyping = false + /// It hops whenever its look changes, as the bot being made does. + var hopsOnNewLook = false + @Environment(\.liveliness) private var liveliness + + var body: some View { + if liveliness == .off { + BotFace(look: look, size: size) + } else { + AnimatedBotFace(look: look, size: size, mood: mood, watchTyping: watchTyping, hopsOnNewLook: hopsOnNewLook) + } + } +} + +@MainActor +private struct AnimatedBotFace: View { + let look: BotLook + let size: CGFloat + let mood: BotMood + let watchTyping: Bool + let hopsOnNewLook: Bool + @State private var rig = FaceRig() + @State private var frame = CGRect.zero + @Environment(TypingWatch.self) private var typing: TypingWatch? + + var body: some View { + let moving = rig.moving + let settledAt = rig.settledAt + // Read so a face at rest is drawn again when a part jumps. + let _ = rig.revision + let art = FaceArt(look: look) + TimelineView(.animation(minimumInterval: FaceMotion.tickSeconds, paused: !moving)) { timeline in + let pose = rig.pose(at: moving ? FaceClock.tick(at: timeline.date) : settledAt) + Canvas { context, canvas in + FaceDrawing.draw(art, pose: pose, in: &context, canvas: canvas, overflow: Self.overflow) + } + .frame(width: size * (1 + 2 * Self.overflow), height: size * (1 + 2 * Self.overflow)) + .allowsHitTesting(false) + } + .frame(width: size, height: size) + .contentShape(Rectangle()) + // Tapped, it hops; it's only a face, so this is play, not an action anyone needs. + .simultaneousGesture(TapGesture().onEnded { rig.hop() }) + .background { + if watchTyping { + GeometryReader { proxy in + Color.clear + .onAppear { frame = proxy.frame(in: .global) } + .onChange(of: proxy.frame(in: .global)) { _, next in frame = next } + } + } + } + .onAppear { rig.enter(mood) } + .onDisappear { rig.stopActing() } + .onChange(of: mood) { _, next in rig.enter(next) } + .onChange(of: look) { if hopsOnNewLook { rig.hop() } } + .onChange(of: watchTyping ? typing?.latest : nil) { _, typed in + guard let typed, mood.mood.watches else { return } + rig.watchTyping(at: typed.point, from: frame) + } + .accessibilityHidden(true) + } + + /// How far past its box a face is drawn on each side, as a share of its size, so a hop or a lean isn't cut off. + private static let overflow = 0.35 +} + +/// What a face is drawn with, by its look. +struct FaceArt { + let palette: BotPalette + let head: HeadShape + let marks: [FaceMark] + + init(look: BotLook) { + palette = BotLooks.palette(for: look.color) + head = BotLooks.head(for: look.shape) + marks = BotLooks.faces(for: look.face) + } +} + +/// Draws a face in a pose, layer by layer as the web's `AnimatedBotFace` +/// nests them: the head's moves, then its turns about the middle of its +/// bottom edge; then, clipped to the head, the eyes' moves and the eyes. +enum FaceDrawing { + /// Draws `art` in `pose` on `canvas`, which reaches `overflow` of the face's size past it on each side. + static func draw(_ art: FaceArt, pose: FacePose, in context: inout GraphicsContext, canvas: CGSize, overflow: Double) { + let viewboxSize = FaceGeometry.viewboxSize + let side = canvas.width / (1 + 2 * overflow) + context.translateBy(x: side * overflow, y: side * overflow) + context.scaleBy(x: side / viewboxSize, y: side / viewboxSize) + let units = { (percent: Double) in percent * viewboxSize / 100 } + let bottom = CGPoint(x: viewboxSize / 2, y: viewboxSize) + let centre = CGPoint(x: viewboxSize / 2, y: viewboxSize / 2) + + var head = context + head.translateBy(x: 0, y: units(pose.hopRise)) + head.about(bottom) { $0.scaleBy(x: pose.hopSquash.width, y: pose.hopSquash.height) } + head.translateBy(x: units(pose.gazeMove.x + pose.wiggleMove), y: units(pose.gazeMove.y + pose.moodMove)) + for degrees in [pose.gazeTiltDegrees, pose.moodTiltDegrees, pose.wiggleTiltDegrees] { + head.about(bottom) { $0.rotate(by: .degrees(degrees)) } + } + var outline = Path() + for part in art.head.parts { + head.fill(part.path, with: .color(art.palette.face)) + outline.addPath(part.path) + } + + // The eyes never show past the head's edge, however far they look. + var eyes = head + eyes.clip(to: outline) + let placement = art.head.eyes + eyes.translateBy(x: placement.x, y: placement.y) + eyes.scaleBy(x: placement.scale, y: placement.scale) + eyes.translateBy(x: -centre.x, y: -centre.y) + eyes.translateBy(x: units(pose.eyesGaze.x), y: units(pose.hopEyesDrop + pose.eyesLift + pose.eyesGaze.y)) + eyes.about(centre) { $0.scaleBy(x: pose.eyesGaze.width, y: 1) } + let colour = GraphicsContext.Shading.color(art.palette.eyes) + for mark in art.marks { + drawEye(mark, pose: pose, colour: colour, in: eyes) + } + drawThinkingDots(pose: pose, colour: colour, in: eyes) + } + + /// One mark, shrunk as the dots grow: an arc eye flattened as the mood + /// says; an open eye as tall as it's held, fading as it closes into a smile. + private static func drawEye(_ mark: FaceMark, pose: FacePose, colour: GraphicsContext.Shading, in context: GraphicsContext) { + var eye = context + eye.about(mark.centre) { $0.scaleBy(x: pose.eyesShrink, y: pose.eyesShrink) } + switch mark { + case let .stroke(data, width): + // An arc is a shut eye already: it only flattens, never blinks or smiles. + eye.translateBy(x: 0, y: -pose.arcsFlat * FaceGeometry.arcShutRise) + eye.stroke(SVGPath.path(flattenArcs(data, by: pose.arcsFlat)), with: colour, lineWidth: width) + case .circle, .rect: + var open = eye + open.opacity = 1 - FaceGeometry.openEyeFades.share(of: pose.smile) + // However shut, it stays as thick as an arc eye's line, so it never thins to a hairline. + open.about(mark.centre) { $0.scaleBy(x: 1, y: max(pose.eyeHeight, FaceGeometry.arcEyeStrokeWidth / mark.drawnHeight)) } + mark.draw(in: open, with: colour, cap: .butt) + drawSmileArc(over: mark, smile: pose.smile, colour: colour, in: eye) + } + } + + /// The upturned arc an open eye closes into as the bot smiles: the top half + /// of a ring a little wider than the eye, growing up from a flat line. + private static func drawSmileArc(over eye: FaceMark, smile: Double, colour: GraphicsContext.Shading, in context: GraphicsContext) { + guard smile > 0 else { return } + let centre = eye.centre + let radius = eye.drawnWidth / 2 + FaceGeometry.smileArcWidening + let baseline = centre.y + radius * FaceGeometry.smileArcBaselineShare + var arc = context + arc.opacity = FaceGeometry.smileArcShows.share(of: smile) + arc.about(CGPoint(x: centre.x, y: baseline)) { $0.scaleBy(x: 1, y: smile) } + let path = Path { path in + path.addArc(center: CGPoint(x: centre.x, y: baseline), radius: radius, startAngle: .degrees(180), endAngle: .degrees(0), clockwise: false) + } + arc.stroke(path, with: colour, lineWidth: FaceGeometry.arcEyeStrokeWidth) + } + + /// A thinking bot's three dots, across where its eyes were, grown in as the mood turns to thinking. + private static func drawThinkingDots(pose: FacePose, colour: GraphicsContext.Shading, in context: GraphicsContext) { + let grows = [pose.outerDotsGrow, pose.middleDotGrows, pose.outerDotsGrow] + for (index, x) in FaceGeometry.thinkingDotXs.enumerated() where index < pose.dots.count && grows[index] > 0 { + let (rise, opacity) = pose.dots[index] + var dot = context + dot.opacity = opacity + let radius = FaceGeometry.thinkingDotRadius * grows[index] + let centre = CGPoint(x: x, y: FaceGeometry.thinkingDotY - rise * FaceGeometry.thinkingDotRadius * 2) + dot.fill(Path(ellipseIn: CGRect(x: centre.x - radius, y: centre.y - radius, width: radius * 2, height: radius * 2)), with: colour) + } + } + + /// The arc eyes' path with each arc's height cut by `flat`, from 0 (as drawn) to 1 (a straight line), as `flattenArcs`. + private static func flattenArcs(_ data: String, by flat: Double) -> String { + guard flat > 0 else { return data } + return data.replacing(arcRadii) { match in + let width = match.output[1].substring ?? "" + let height = Double(match.output[2].substring ?? "") ?? 0 + return "a\(width) \(max(height * (1 - flat), FaceGeometry.flattestArcHeight))" + } + } + + /// An arc's radii in the arc eyes' path: its relative `a` commands' first two numbers. + private static let arcRadii = try! Regex(#"a([\d.]+) ([\d.]+)"#) +} + +private extension GraphicsContext { + /// Applies `transform` about `point` rather than the origin, as a CSS transform origin does. + mutating func about(_ point: CGPoint, _ transform: (inout GraphicsContext) -> Void) { + translateBy(x: point.x, y: point.y) + transform(&self) + translateBy(x: -point.x, y: -point.y) + } +} + +private extension FaceMark { + /// The middle of the mark's outline, which it shrinks and squeezes about. + var centre: CGPoint { + switch self { + case let .circle(cx, cy, _): CGPoint(x: cx, y: cy) + case let .rect(x, y, width, height, _): CGPoint(x: x + width / 2, y: y + height / 2) + case let .stroke(data, _): + CGPoint(x: SVGPath.path(data).boundingRect.midX, y: SVGPath.path(data).boundingRect.midY) + } + } + + var drawnWidth: Double { + switch self { + case let .circle(_, _, r): r * 2 + case let .rect(_, _, width, _, _): width + case let .stroke(data, _): SVGPath.path(data).boundingRect.width + } + } + + var drawnHeight: Double { + switch self { + case let .circle(_, _, r): r * 2 + case let .rect(_, _, _, height, _): height + case let .stroke(data, _): SVGPath.path(data).boundingRect.height + } + } +} + +/// The face of a bot someone is talking to, as the web's `WatchingAvatar`: +/// thinking while the bot is `busy`; else, while `watching`, following the +/// typing and moving through `FaceMotion.watchingMoods`; else still. +struct WatchingFace: View { + let look: BotLook + let size: CGFloat + let busy: Bool + let watching: Bool + @State private var watchingMood = FaceMotion.watchingMoods.rest.mood + + var body: some View { + LivelyBotFace(look: look, size: size, mood: busy ? .thinking : watching ? watchingMood : .still, watchTyping: true) + .moodLoop(FaceMotion.watchingMoods, running: watching && !busy, mood: $watchingMood) + } +} + +/// A bot's face among others in a `LivelyGroup`, as the web's +/// `TurnTakingAvatar`: still except on its turn to move. +struct TurnTakingFace: View { + let look: BotLook + let size: CGFloat + @Environment(FaceTurns.self) private var turns: FaceTurns? + @State private var member = UUID() + + var body: some View { + LivelyBotFace(look: look, size: size, mood: turns?.isMoving(member) == true ? .idle : .still) + .onAppear { turns?.join(member) } + .onDisappear { turns?.leave(member) } + } +} + +extension View { + /// Moves `mood` through `loop` while `running`, as `useMoodLoop`; otherwise holds it at the loop's resting mood. + func moodLoop(_ loop: MoodLoop, running: Bool, mood: Binding) -> some View { + modifier(MoodLoopModifier(loop: loop, running: running, mood: mood)) + } +} + +private struct MoodLoopModifier: ViewModifier { + let loop: MoodLoop + let running: Bool + @Binding var mood: BotMood + @Environment(\.liveliness) private var liveliness + + func body(content: Content) -> some View { + content.task(id: Run(running: running, liveliness: liveliness)) { + mood = loop.rest.mood + guard running, liveliness != .off else { return } + await loop.run(liveliness: liveliness) { mood = $0 } + } + } + + /// What restarts the loop when it changes. + private struct Run: Equatable { + let running: Bool + let liveliness: Liveliness + } +} + +/// Faces in a group, such as a list of bots, mostly sit still and take short +/// turns moving, so only a few move at once and the list doesn't fidget, as +/// the web's `LivelyGroup`. +@MainActor +struct LivelyGroup: View { + let content: Content + @State private var turns = FaceTurns() + @Environment(\.liveliness) private var liveliness + + init(@ViewBuilder content: () -> Content) { + self.content = content() + } + + var body: some View { + content + .environment(turns) + .task(id: liveliness) { + guard let pacing = liveliness.groupPacing else { return } + await turns.takeTurns(pacing) + } + } +} + +extension View { + /// Paces the faces inside so they take turns to move (see `LivelyGroup`). + @MainActor func livelyGroup() -> some View { + LivelyGroup { self } + } +} + +/// The faces in a group, and whose turn it is to move. +@MainActor +@Observable +final class FaceTurns { + private var moving: Set = [] + @ObservationIgnored private var members: Set = [] + @ObservationIgnored private var turnEndings: [UUID: Task] = [:] + + func isMoving(_ member: UUID) -> Bool { + moving.contains(member) + } + + func join(_ member: UUID) { + members.insert(member) + } + + func leave(_ member: UUID) { + members.remove(member) + moving.remove(member) + turnEndings.removeValue(forKey: member)?.cancel() + } + + /// Gives turns to faces not already moving, while fewer than the pacing allows are, until cancelled. + func takeTurns(_ pacing: GroupPacing) async { + while (try? await Task.sleep(for: .seconds(pacing.gapSeconds.random()))) != nil { + guard moving.count < pacing.movingAtOnce, let member = members.subtracting(moving).randomElement() else { continue } + moving.insert(member) + turnEndings[member] = Task { [weak self] in + guard (try? await Task.sleep(for: .seconds(pacing.turnSeconds.random()))) != nil else { return } + self?.moving.remove(member) + self?.turnEndings[member] = nil + } + } + for ending in turnEndings.values { ending.cancel() } + turnEndings = [:] + moving = [] + } +} diff --git a/apps/ios/Sugabots/Home/ConversationList.swift b/apps/ios/Sugabots/Home/ConversationList.swift index b4efdfe85..76bd48736 100644 --- a/apps/ios/Sugabots/Home/ConversationList.swift +++ b/apps/ios/Sugabots/Home/ConversationList.swift @@ -87,6 +87,8 @@ struct ConversationList: View { .padding(.leading, Self.nestedIndent) } } + // The bots' faces take turns to move, so the list never fidgets all at once. + .livelyGroup() .sheet(isPresented: $namingChannel) { NameSheet.channel(title: "New channel", action: "Create") { name in let chat = try await store.startChat(in: pod, named: name) @@ -306,7 +308,7 @@ private struct BotItem: View { NavigationLink(value: destination) { HStack(spacing: 12) { if let agent = group.agent { - BotFace(look: agent.look, size: 18) + TurnTakingFace(look: agent.look, size: 18) } else { Icon(.users, size: 16, strokeWidth: 1.75).padding(.horizontal, 1) } diff --git a/apps/ios/Sugabots/NewBot/NewBotScreen.swift b/apps/ios/Sugabots/NewBot/NewBotScreen.swift index eb0b30443..a73d888da 100644 --- a/apps/ios/Sugabots/NewBot/NewBotScreen.swift +++ b/apps/ios/Sugabots/NewBot/NewBotScreen.swift @@ -49,7 +49,7 @@ struct NewBotScreen: View { .padding(.bottom, 8) VStack(spacing: 16) { Button { customising = true } label: { - BotFace(look: look, size: 112) + CreationFace(look: look) .scaleEffect(naming || customising ? 0.5 : 1, anchor: .top) .frame(height: naming || customising ? 56 : 112, alignment: .top) .overlay(alignment: .bottomTrailing) { @@ -134,3 +134,15 @@ struct NewBotScreen: View { BotLook(color: AgentColor.known.randomElement()!, face: AgentFace.known.randomElement()!, shape: AgentShape.known.randomElement()!) } } + +/// The bot being made, large, as the web's `CreationFace`: hopping each time +/// its look changes, and moving through `FaceMotion.creationMoods`. +private struct CreationFace: View { + let look: BotLook + @State private var mood = FaceMotion.creationMoods.rest.mood + + var body: some View { + LivelyBotFace(look: look, size: 112, mood: mood, hopsOnNewLook: true) + .moodLoop(FaceMotion.creationMoods, running: true, mood: $mood) + } +} diff --git a/apps/ios/Sugabots/Settings/GeneralSettings.swift b/apps/ios/Sugabots/Settings/GeneralSettings.swift index 3b009209e..b84f39795 100644 --- a/apps/ios/Sugabots/Settings/GeneralSettings.swift +++ b/apps/ios/Sugabots/Settings/GeneralSettings.swift @@ -1,12 +1,13 @@ import SwiftUI -/// Your profile, as the web's `ProfileSettings`: your name, email and theme, +/// Your profile, as the web's `ProfileSettings`: your name, email, theme and how much bots' faces move, /// Notifications, your invite link, and Sign out. @MainActor struct ProfileSettingsScreen: View { @Environment(Session.self) private var session @Environment(WorkspaceStore.self) private var store @AppStorage(Theme.storageKey) private var theme = Theme.dark + @ChosenLiveliness private var liveliness var body: some View { let user = store.user @@ -26,6 +27,10 @@ struct ProfileSettingsScreen: View { SettingsRow(label: "Theme", trailing: { SegmentedChoice(label: "Theme", options: Theme.allCases.map { ($0, $0.label) }, value: theme) { theme = $0 } }) + SettingsDivider() + SettingsRow(label: "Animations", sub: "How often bots' faces move", trailing: { + SegmentedChoice(label: "Animations", options: Liveliness.allCases.map { ($0, $0.label) }, value: liveliness) { liveliness = $0 } + }) } SettingsGroup { NavigationLink(value: Route.settings(.notifications)) {