Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .changeset/christmas-theme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
25 changes: 24 additions & 1 deletion apps/viewer/src/__tests__/theme.test.ts
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand All @@ -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');
Expand Down
6 changes: 5 additions & 1 deletion apps/viewer/src/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 2 additions & 5 deletions apps/viewer/src/human.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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<string, Paint>;
const HAT: Record<'felt' | 'fur', Paint> = SANTA_HAT;

/**
* A Santa hat, in head-local space facing +x: pulled down over the crown of
Expand Down
1 change: 1 addition & 0 deletions apps/viewer/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async function boot(): Promise<void> {
const renderer = new CartoonRenderer(undefined, {
page: true,
link: { label: 'agentopia.town', href: SITE_URL },
calendar: systemClock,
});
await renderer.mount(stage);
mountCredits(stage, { clock: systemClock });
Expand Down
70 changes: 70 additions & 0 deletions apps/viewer/src/pet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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();

Expand Down
56 changes: 53 additions & 3 deletions apps/viewer/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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();
Expand All @@ -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. */
Expand Down Expand Up @@ -1065,8 +1083,9 @@ export class CartoonRenderer {
async mount(host: HTMLElement): Promise<void> {
// 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({
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 21 additions & 6 deletions apps/viewer/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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';
Expand All @@ -361,7 +373,7 @@ export function mountSettings(office: Settable): () => void {
styleGrid,
gap3,
themeLabel,
themeGrid
themeRow
);

/** Draw the bar at the sky's time now. */
Expand Down Expand Up @@ -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));
}
Expand Down
9 changes: 9 additions & 0 deletions apps/viewer/src/sweater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading