diff --git a/.changeset/christmas-theme.md b/.changeset/christmas-theme.md index a3216cb..a8aeebb 100644 --- a/.changeset/christmas-theme.md +++ b/.changeset/christmas-theme.md @@ -2,4 +2,4 @@ 'agentopiahq': minor --- -The settings card has a theme, off by default. Pick "christmas" and the office is dressed for it: fairy lights flashing along the walls, a wreath in every window and on the door, a tree with presents by the bookshelf, poinsettias for plants, a Christmas sweater on every agent, and a sweater and a Santa hat for you. "none" takes it all down again. The choice is remembered. +The settings card has a theme. Pick "christmas" and the office is dressed for it: fairy lights flashing along the walls, a wreath in every window and on the door, a tree with presents by the bookshelf, poinsettias for plants, a Christmas sweater on every agent, a sweater for the dog, a Santa hat for the cat, and a sweater and a Santa hat for you. "none" takes it all down again. The choice is remembered. Until you pick one, the season picks it: the office dresses for Christmas all through December and goes back to plain in January, and the reset key beside the themes hands the choice back to the season. diff --git a/apps/viewer/src/__tests__/theme.test.ts b/apps/viewer/src/__tests__/theme.test.ts index 35e05d0..3cd8f98 100644 --- a/apps/viewer/src/__tests__/theme.test.ts +++ b/apps/viewer/src/__tests__/theme.test.ts @@ -1,4 +1,4 @@ -import { THEMES, activeTheme, themeById, useTheme } from '../theme'; +import { THEMES, activeTheme, pickedTheme, seasonalTheme, themeById, useTheme } from '../theme'; describe('the office theme', () => { afterEach(() => { @@ -21,6 +21,29 @@ describe('the office theme', () => { expect(themeById(stored)).toBe(want); }); + it.each([ + ['christmas', 'christmas'], + ['none', 'none'], + ['easter', null], + ['', null], + [null, null], + [undefined, null], + ] as const)('takes a stored %s as the pick %s', (stored, want) => { + expect(pickedTheme(stored)).toBe(want); + }); + + // Local times, so the season turns at the viewer's own midnight. + it.each([ + ['the last minute of November', new Date(2026, 10, 30, 23, 59), 'none'], + ['the first of December', new Date(2026, 11, 1, 0, 0), 'christmas'], + ['Christmas Day', new Date(2026, 11, 25, 12), 'christmas'], + ["New Year's Eve", new Date(2026, 11, 31, 23, 59), 'christmas'], + ["New Year's Day", new Date(2027, 0, 1, 0, 0), 'none'], + ['midsummer', new Date(2027, 6, 1), 'none'], + ] as const)('dresses %s in %s', (_when, at, want) => { + expect(seasonalTheme(at.getTime())).toBe(want); + }); + it('says whether picking a theme changed anything', () => { expect(useTheme('christmas')).toBe(true); expect(activeTheme()).toBe('christmas'); diff --git a/apps/viewer/src/hero.ts b/apps/viewer/src/hero.ts index 05385fd..c8ce07b 100644 --- a/apps/viewer/src/hero.ts +++ b/apps/viewer/src/hero.ts @@ -43,7 +43,11 @@ export function mountHero(host: HTMLElement, options: HeroOptions = {}): () => v // The script runs on its own timeline; the wall clock still shows the visitor's time. // The office is the whole page: paper edge to edge, its title pinned top // left, with the page's link under the slogan. - const renderer = new CartoonRenderer(systemClock, { page: true, link: options.link }); + const renderer = new CartoonRenderer(systemClock, { + page: true, + link: options.link, + calendar: systemClock, + }); const clock = manualClock(HERO_EPOCH - 1500); const rng = seededRng(seed); const script = new HeroScript(rng); diff --git a/apps/viewer/src/human.ts b/apps/viewer/src/human.ts index f0cd69b..342a36f 100644 --- a/apps/viewer/src/human.ts +++ b/apps/viewer/src/human.ts @@ -4,7 +4,7 @@ import type { RigView, Vec } from './character'; import { SWAY_S, armSwing } from './idlePose'; import { SEAT_Z } from './objects/chair'; import { features } from './styles/context'; -import { HUMAN_SWEATER, knitBand, sweaterLine, sweaterShade } from './sweater'; +import { HUMAN_SWEATER, SANTA_HAT, knitBand, sweaterLine, sweaterShade } from './sweater'; import { activeTheme } from './theme'; /** @@ -953,10 +953,7 @@ function drawHead(g: Ctx, pose: HumanPose, back: boolean, t: number, blink: bool } /** A Santa hat's felt and its fur. */ -const HAT = { - felt: { fill: '#d8453d', shade: '#a32f2c', line: '#5e1c1a' }, - fur: { fill: '#fffaf0', shade: '#e6dfd0', line: '#8a7a6a' }, -} as const satisfies Record; +const HAT: Record<'felt' | 'fur', Paint> = SANTA_HAT; /** * A Santa hat, in head-local space facing +x: pulled down over the crown of diff --git a/apps/viewer/src/main.ts b/apps/viewer/src/main.ts index c80aa37..7116548 100644 --- a/apps/viewer/src/main.ts +++ b/apps/viewer/src/main.ts @@ -43,6 +43,7 @@ async function boot(): Promise { const renderer = new CartoonRenderer(undefined, { page: true, link: { label: 'agentopia.town', href: SITE_URL }, + calendar: systemClock, }); await renderer.mount(stage); mountCredits(stage, { clock: systemClock }); diff --git a/apps/viewer/src/pet.ts b/apps/viewer/src/pet.ts index ebe6874..474c6fc 100644 --- a/apps/viewer/src/pet.ts +++ b/apps/viewer/src/pet.ts @@ -2,6 +2,8 @@ import { PetKind, PetView } from '@agentopia/core'; import { SHEET } from './kit/palette'; import { features } from './styles/context'; +import { DOG_SWEATER, SANTA_HAT, knitBand, sweaterLine } from './sweater'; +import { activeTheme } from './theme'; const INK = SHEET.ink; @@ -17,6 +19,9 @@ const INK = SHEET.ink; * canvas, which the renderer composites as a sprite and sorts among the * furniture like any figure (`petRig.ts`). * + * Dressed for Christmas (`theme.ts`), the dog wears a sweater and the cat a + * Santa hat. + * * Lengths are in sheet pixels at the 32px tile, before the per-pet `scale`; * y is negative upward from the floor. */ @@ -774,11 +779,71 @@ function drawToy(g: Ctx, kind: PetKind, at: Vec, t: number): void { } } +/** + * The dog's sweater, pulled on over its middle: from behind the shoulders + * to short of the rump, knitted across, with a rib at the neck and the hem. + * In the body's own frame, so it follows the body as it sits or lies. + */ +function dogSweater(g: Ctx, body: PetPose['body']): void { + const { rx, ry } = body; + const sweater = DOG_SWEATER; + const back = -rx * 0.55; + const neck = rx * 0.8; + g.save(); + g.translate(body.x, body.y); + g.rotate(body.tilt); + g.clip(ellipsePath(0, 0, rx, ry)); + g.fillStyle = sweater.body; + g.fillRect(back, -ry - 1, neck - back, ry * 2 + 2); + knitBand(g, sweater, back, neck, -ry * 0.5, ry * 0.25, (back + neck) / 2); + // The ribbed hem and collar, in the trim. + g.fillStyle = sweater.trim; + for (const x of [back, neck - 1.3]) { + g.fillRect(x, -ry - 1, 1.3, ry * 2 + 2); + } + g.strokeStyle = sweaterLine(sweater); + g.lineWidth = 0.5; + g.beginPath(); + g.moveTo(back, -ry - 1); + g.lineTo(back, ry + 1); + g.stroke(); + g.restore(); +} + +/** + * The cat's Santa hat, in the head's frame: perched on the crown between + * the ears, its point flopped over the back, a pom on its tip and fur round + * the brim. + */ +function catHat(g: Ctx, r: number): void { + const paint = (path: Path2D, p: { fill: string; line: string }): void => { + g.lineJoin = 'round'; + g.strokeStyle = p.line; + g.lineWidth = LINE * 0.7; + g.fillStyle = p.fill; + g.fill(path); + g.stroke(path); + }; + const cap = new Path2D(); + cap.moveTo(r * 0.42, -r * 0.84); + cap.quadraticCurveTo(r * 0.3, -r * 1.62, -r * 0.3, -r * 1.66); + cap.quadraticCurveTo(-r * 0.86, -r * 1.66, -r * 1.02, -r * 1.12); + cap.quadraticCurveTo(-r * 0.66, -r * 1.24, -r * 0.42, -r * 0.88); + cap.closePath(); + paint(cap, SANTA_HAT.felt); + paint(ellipsePath(-r * 1.02, -r * 1.06, r * 0.27, r * 0.27), SANTA_HAT.fur); + paint( + capsule({ x: -r * 0.48, y: -r * 0.86 }, { x: r * 0.48, y: -r * 0.82 }, r * 0.34), + SANTA_HAT.fur + ); +} + /** Draw a pose, facing +x, with the feet at the origin. */ export function drawPet(g: Ctx, kind: PetKind, pose: PetPose, id: string, clock: number): void { const b = BUILD[kind]; const c = b.coat; const legShape = (leg: Leg): Path2D => capsule(leg.hip, leg.foot, b.legW); + const festive = activeTheme() === 'christmas'; // The far legs, in the coat's shade, behind everything. silhouette(g, pose.legs.filter((l) => l.far).map(legShape), c.d, c.edge); @@ -818,6 +883,9 @@ export function drawPet(g: Ctx, kind: PetKind, pose: PetPose, id: string, clock: g.fill(ellipsePath(leg.foot.x, leg.foot.y - 0.3, b.legW * 0.5, b.legW * 0.42)); } } + if (kind === 'dog' && festive) { + dogSweater(g, pose.body); + } if (kind === 'cat') { // Tabby stripes over the back. g.save(); @@ -843,6 +911,8 @@ export function drawPet(g: Ctx, kind: PetKind, pose: PetPose, id: string, clock: drawHead(g, kind, pose, id, clock); if (kind === 'dog') { dogEar(g, b.head.r, true, pose.flap, c); + } else if (festive) { + catHat(g, b.head.r); } g.restore(); diff --git a/apps/viewer/src/renderer.ts b/apps/viewer/src/renderer.ts index e631299..351cecf 100644 --- a/apps/viewer/src/renderer.ts +++ b/apps/viewer/src/renderer.ts @@ -120,7 +120,15 @@ import { useStyle, } from './styles'; import { lum, parseColor } from './styles/color'; -import { THEMES, activeTheme, themeById, useTheme } from './theme'; +import { + THEMES, + ThemeId, + activeTheme, + pickedTheme, + seasonalTheme, + themeById, + useTheme, +} from './theme'; import { UsageBoardState, glanceOf } from './usage'; import { UsagePanel } from './usagePanel'; import { Wind } from './wind'; @@ -807,6 +815,11 @@ export interface RendererOptions { * started" card, the app's way to the website. */ link?: TitleLink; + /** + * The calendar the theme follows until the viewer picks one: Christmas in + * December. Left out (the stories), the office stays plain unless a theme is picked. + */ + calendar?: Clock; } /** In page mode, how far the office stands in from the view's edges, in sheet pixels. */ @@ -881,6 +894,7 @@ export class CartoonRenderer { this.page = options.page ?? false; this.badge = options.badge; this.link = options.link; + this.calendar = options.calendar; } private app = new Application(); @@ -891,6 +905,10 @@ export class CartoonRenderer { private readonly badge: string | undefined; /** In page mode, the link under the slogan (`RendererOptions.link`). */ private readonly link: TitleLink | undefined; + /** The calendar the theme follows until one is picked (`RendererOptions.calendar`). */ + private readonly calendar: Clock | undefined; + /** The theme this viewer picked, or null while the season picks it. */ + private themePick: ThemeId | null = null; /** In page mode: what stays put while the camera moves, pinned to the view's corners. */ private hud = new Container(); /** In page mode: the title block, as page text pinned to the view's top-left corner. */ @@ -1065,8 +1083,9 @@ export class CartoonRenderer { async mount(host: HTMLElement): Promise { // The drawing style this viewer last picked, before anything is drawn. useStyle(styleById(localStore()?.getItem(STYLE_KEY))); - // And the theme it left the office dressed in. - useTheme(themeById(localStore()?.getItem(THEME_KEY))); + // And the theme it picked, or else the season's. + this.themePick = pickedTheme(localStore()?.getItem(THEME_KEY)); + useTheme(this.themePick ?? this.seasonsTheme()); // And whether its sky keeps its own local time. this.skyClock.keepLocalTime(localStore()?.getItem(DAYLIGHT_KEY) === 'local'); await this.app.init({ @@ -1363,11 +1382,38 @@ export class CartoonRenderer { /** Dress the office in another theme from now on, and remember it for this viewer. */ pickTheme(id: string): void { const theme = themeById(id); + this.themePick = theme; try { localStore()?.setItem(THEME_KEY, theme); } catch { // A viewer with no storage keeps the theme for this visit only. } + this.wearTheme(theme); + } + + /** Whether this viewer picked the theme, rather than the season. */ + themePicked(): boolean { + return this.themePick !== null; + } + + /** Forget the theme picked and let the season dress the office again. */ + followSeason(): void { + this.themePick = null; + try { + localStore()?.removeItem(THEME_KEY); + } catch { + // Nothing was kept to forget. + } + this.wearTheme(this.seasonsTheme()); + } + + /** The theme the season brings now: none without a calendar. */ + private seasonsTheme(): ThemeId { + return this.calendar ? seasonalTheme(this.calendar.now()) : 'none'; + } + + /** Dress the office in `theme`, redrawing it if that changed anything. */ + private wearTheme(theme: ThemeId): void { if (!useTheme(theme) || !this.host) { return; } @@ -2341,6 +2387,10 @@ export class CartoonRenderer { const minute = Math.floor(local); if (minute !== this.clockMinute) { this.clockMinute = minute; + // A season turning while the office is open dresses it for the new one. + if (this.themePick === null) { + this.wearTheme(this.seasonsTheme()); + } for (const baked of this.mounted) { if (baked.piece.name === 'clock') { moved.add(baked.key); diff --git a/apps/viewer/src/settings.ts b/apps/viewer/src/settings.ts index 25270a0..9441ab3 100644 --- a/apps/viewer/src/settings.ts +++ b/apps/viewer/src/settings.ts @@ -24,9 +24,10 @@ import { ICON, LIT, controlButton, stroke } from './zoom'; * - The style, every way the office can be drawn (`styles/`), the one in force * lit. Pick one and the office is redrawn in it at once; the choice is kept * for this viewer. - * - The theme, a season's dressing for the office (`theme.ts`), none by - * default. Pick one and the decorations go up and the cast dresses for it; - * the choice is kept for this viewer. + * - The theme, a season's dressing for the office (`theme.ts`), the season's + * own by default (Christmas in December). Pick one and the decorations go up + * and the cast dresses for it; the choice is kept for this viewer, till the + * reset key beside it hands the theme back to the season. */ /** What the settings need of the office. */ @@ -65,6 +66,10 @@ export interface Settable { themeNow(): string; /** Dress the office in another theme. */ pickTheme(id: string): void; + /** Whether the theme was picked by hand, rather than brought by the season. */ + themePicked(): boolean; + /** Let the season pick the theme again. */ + followSeason(): void; } /** @@ -340,6 +345,13 @@ export function mountSettings(office: Settable): () => void { themeTiles.set(id, tile); themeGrid.append(tile); } + const themeBack = controlButton(ICON.reset, 'Let the season pick the theme again', () => { + office.followSeason(); + show(); + }); + const themeRow = document.createElement('div'); + themeRow.className = 'agentopia-settings-row'; + themeRow.append(themeGrid, themeBack); const gap = document.createElement('div'); gap.className = 'agentopia-settings-gap'; @@ -361,7 +373,7 @@ export function mountSettings(office: Settable): () => void { styleGrid, gap3, themeLabel, - themeGrid + themeRow ); /** Draw the bar at the sky's time now. */ @@ -402,11 +414,14 @@ export function mountSettings(office: Settable): () => void { } const theme = office.themeNow(); - themeState.textContent = + const themePicked = office.themePicked(); + themeState.textContent = `${ office .themes() .find((t) => t.id === theme) - ?.label.toLowerCase() ?? theme; + ?.label.toLowerCase() ?? theme + }${themePicked ? ' ยท picked' : ''}`; + themeBack.disabled = !themePicked; for (const [id, tile] of themeTiles) { tile.setAttribute('aria-pressed', String(id === theme)); } diff --git a/apps/viewer/src/sweater.ts b/apps/viewer/src/sweater.ts index b9a34d4..c80e75b 100644 --- a/apps/viewer/src/sweater.ts +++ b/apps/viewer/src/sweater.ts @@ -38,6 +38,15 @@ export const SWEATERS: readonly Sweater[] = [ /** The human's: the classic red one. */ export const HUMAN_SWEATER: Sweater = { body: RED, trim: CREAM, motif: GREEN, knit: 'zigzag' }; +/** The office dog's: green, to stand out from its sandy coat. */ +export const DOG_SWEATER: Sweater = { body: GREEN, trim: CREAM, motif: RED, knit: 'zigzag' }; + +/** A Santa hat's felt and its fur, for the human and the office cat alike. */ +export const SANTA_HAT = { + felt: { fill: '#d8453d', shade: '#a32f2c', line: '#5e1c1a' }, + fur: { fill: '#fffaf0', shade: '#e6dfd0', line: '#8a7a6a' }, +} as const; + /** The sweater a figure wears: always the same one for the same figure. */ export function sweaterFor(id: string): Sweater { let hash = 2166136261; diff --git a/apps/viewer/src/theme.ts b/apps/viewer/src/theme.ts index 220091b..aa7c2ce 100644 --- a/apps/viewer/src/theme.ts +++ b/apps/viewer/src/theme.ts @@ -6,7 +6,9 @@ * * A theme is the viewer's own choice, like its drawing style, and nothing * about it reaches core: the decorations stand only where core already walks - * nobody (see `THEME_PIECES` in `room.ts`). + * nobody (see `THEME_PIECES` in `room.ts`). Until a viewer picks one, the + * season picks it (`seasonalTheme`): Christmas all December, the plain office + * the rest of the year. */ export type ThemeId = 'none' | 'christmas'; @@ -35,6 +37,16 @@ export function themeById(id: string | null | undefined): ThemeId { return THEMES.find((theme) => theme.id === id)?.id ?? 'none'; } +/** A stored choice of theme, or null when none was made (or it names no theme). */ +export function pickedTheme(stored: string | null | undefined): ThemeId | null { + return THEMES.find((theme) => theme.id === stored)?.id ?? null; +} + +/** The theme the season brings at `at` (ms since the epoch), in the viewer's local time. */ +export function seasonalTheme(at: number): ThemeId { + return new Date(at).getMonth() === 11 ? 'christmas' : 'none'; +} + /** Dress the office in `theme` from now on; returns whether that changed anything. */ export function useTheme(theme: ThemeId): boolean { if (theme === current) {