[ui] Thorns: Touch her over her picture; Gently. I have thorns.

This commit is contained in:
2026-11-02 09:33:23 +01:00
parent ae1169ed6a
commit 1070017d2c
4 changed files with 190 additions and 5 deletions
+6
View File
@@ -50,6 +50,12 @@ export const en = {
tooFull: 'That was a lot of water. I will be fine by tomorrow.'
},
notKept: 'This device could not keep today’s care, so it lasts only until the garden closes.',
/** Touching her (thorns.md). Her thorns are about herself, never about the player. */
touch: {
label: 'Touch her',
fond: ['Oh. Hello.', 'That tickles.'],
thorns: 'Gently. I have thorns.'
},
/**
* The day's sentence (day-sentence.md). She offers; she never counts, and never
* mentions a day the petal was not opened.
+57 -5
View File
@@ -1,7 +1,7 @@
<script lang="ts">
import {
careEvent, goodMoment, localDay, petalAt, pour, roseAt, ROSE, sentenceOpened,
type Mood, type PetalNow, type Place, type RoseNow, type Stage
careEvent, goodMoment, localDay, petalAt, pour, roseAt, ROSE, sentenceOpened, touch, TOUCH, UNTOUCHED,
type Mood, type PetalNow, type Place, type RoseNow, type Stage, type TouchState
} from '@rosea/engine';
import type { CareEvent, SentenceOpened } from '@rosea/schema';
import { en } from '../copy/en.js';
@@ -126,11 +126,35 @@
}
$effect(() => () => clearInterval(pourTimer));
// --- Touching her (thorns.md): nothing is recorded ----------------------
let touchState = $state.raw<TouchState>(UNTOUCHED);
let touchLine = $state('');
let touchBlush = $state(false);
let fondSaid = 0;
let touchTimer: ReturnType<typeof setTimeout> | undefined;
function touchHer(): void {
const { reply, state } = touch(touchState, wiring.now());
touchState = state;
if (reply.kind === 'nothing') return;
const fond = en.rose.touch.fond;
touchLine = reply.kind === 'thorns' ? en.rose.touch.thorns : (fond[fondSaid++ % fond.length] ?? '');
if (reply.kind === 'fond' && reply.blush) touchBlush = true;
clearTimeout(touchTimer);
// Her words hold the bubble while she would rest, then her usual line returns (T6).
touchTimer = setTimeout(() => {
touchLine = '';
touchBlush = false;
}, TOUCH.restMs);
}
$effect(() => () => clearTimeout(touchTimer));
const isPourKey = (e: KeyboardEvent): boolean => e.key === ' ' || e.key === 'Enter';
// --- What she says: one line, or nothing --------------------------------
const says = $derived.by(() => {
if (!rose) return '';
if (touchLine) return touchLine;
const s = en.rose.says;
if (rose.mood === 'blushing') return s.thanks;
if (rose.mood === 'too-full') return s.tooFull;
@@ -146,15 +170,30 @@
return [rose.drift > 0 ? s.back : '', need || offer].filter(Boolean).join(' ');
});
const picture = $derived(rose ? (rose.stage === 'full-bloom' ? MOOD_PICTURE[rose.mood] : STAGE_PICTURE[rose.stage]) : bud);
const alt = $derived(rose ? en.rose.picture(en.rose.stageWords[rose.stage], en.rose.moodWords[rose.mood]) : '');
// A touch's blush shows as her mood does: on her face in full bloom (thorns.md T4).
const mood = $derived<Mood | null>(rose ? (touchBlush ? 'blushing' : rose.mood) : null);
const picture = $derived(rose && mood ? (rose.stage === 'full-bloom' ? MOOD_PICTURE[mood] : STAGE_PICTURE[rose.stage]) : bud);
const alt = $derived(rose && mood ? en.rose.picture(en.rose.stageWords[rose.stage], en.rose.moodWords[mood]) : '');
const percent = (x: number): string => `${Math.round(Math.max(0, Math.min(1, x)) * 100)} %`;
</script>
{#if rose}
<section class="rose">
<figure>
<img src={picture} {alt} width="120" height="320" class:turned={rose.drift > 0} />
<div class="picture">
<img src={picture} {alt} width="120" height="320" class:turned={rose.drift > 0} />
<!-- Her picture keeps its description; the touch is its own button over it (thorns.md T5). -->
<button
type="button"
class="touch"
aria-label={en.rose.touch.label}
onclick={touchHer}
onkeydown={(e) => {
// A held key is one touch, not a rush.
if (e.repeat) e.preventDefault();
}}
></button>
</div>
{#if says}
<figcaption class="bubble" aria-live="polite">{says}</figcaption>
{/if}
@@ -246,6 +285,19 @@
transition: transform 0.6s ease;
}
.picture {
position: relative;
}
.touch {
position: absolute;
inset: 0;
border: none;
border-radius: 1rem;
background: transparent;
cursor: pointer;
}
/* Drift is drawn as waiting: turned a little away, never hurt (CLAUDE.md §8). */
img.turned {
transform: rotate(-4deg) scaleX(-1);
+109
View File
@@ -0,0 +1,109 @@
import { careEvent, TOUCH } from '@rosea/engine';
import type { CareEvent } from '@rosea/schema';
import { cleanup, fireEvent, render, screen } from '@testing-library/svelte';
import { tick } from 'svelte';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { en } from '../src/copy/en.js';
import Rose from '../src/rose/Rose.svelte';
import type { RoseWiring } from '../src/rose/types.js';
const ZONE = 'Europe/Prague';
const ME = 'M'.repeat(87);
const MORNING = Date.parse('2026-03-01T08:00:00+01:00');
const DAY = 86_400_000;
/** A clock the test moves, and every write the rose tries. */
function wiring(log: CareEvent[] = []): RoseWiring & { clock: { now: number }; writes: unknown[] } {
const clock = { now: MORNING };
const writes: unknown[] = [];
return {
identity: async () => ({ publicKey: ME, kept: true }),
loadCare: async () => log,
appendCare: async (e) => void writes.push(e),
sentences: [],
loadOpened: async () => [],
appendOpened: async (e) => void writes.push(e),
clientVersion: '0.1.0',
deviceZone: ZONE,
now: () => clock.now,
pastKeys: async () => [],
exportRose: async () => ({}),
importRose: async () => ({ added: 0, petals: 0 }),
saveFile: () => undefined,
keeping: { persisted: async () => false, persist: async () => true },
installHint: false,
clock,
writes
};
}
const bubble = (): string => document.querySelector('.bubble')?.textContent ?? '';
async function touchAt(w: ReturnType<typeof wiring>, at: number): Promise<void> {
w.clock.now = at;
await fireEvent.click(screen.getByRole('button', { name: en.rose.touch.label }));
await tick();
}
afterEach(() => {
vi.useRealTimers();
cleanup();
});
describe('touching her (thorns.md)', () => {
it('a touch is answered fondly, then her usual line returns; nothing is written', async () => {
const w = wiring();
render(Rose, { wiring: w, timeZone: ZONE });
await screen.findByRole('button', { name: en.rose.touch.label });
const usual = bubble();
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
await touchAt(w, MORNING);
expect(bubble()).toBe(en.rose.touch.fond[0]);
await touchAt(w, MORNING + 5000);
expect(bubble()).toBe(en.rose.touch.fond[1]);
vi.advanceTimersByTime(TOUCH.restMs);
await tick();
expect(bubble()).toBe(usual);
expect(w.writes).toEqual([]);
});
it('three quick taps: Gently. I have thorns. Taps while she rests change nothing', async () => {
const w = wiring();
render(Rose, { wiring: w, timeZone: ZONE });
await screen.findByRole('button', { name: en.rose.touch.label });
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
await touchAt(w, MORNING);
await touchAt(w, MORNING + 500);
await touchAt(w, MORNING + 1000);
expect(bubble()).toBe(en.rose.touch.thorns);
await touchAt(w, MORNING + 2000);
expect(bubble()).toBe(en.rose.touch.thorns);
expect(w.writes).toEqual([]);
});
it('a held key is one touch, not a rush', async () => {
const w = wiring();
render(Rose, { wiring: w, timeZone: ZONE });
const button = await screen.findByRole('button', { name: en.rose.touch.label });
const repeat = new KeyboardEvent('keydown', { key: 'Enter', repeat: true, cancelable: true, bubbles: true });
button.dispatchEvent(repeat);
expect(repeat.defaultPrevented).toBe(true);
const first = new KeyboardEvent('keydown', { key: 'Enter', cancelable: true, bubbles: true });
button.dispatchEvent(first);
expect(first.defaultPrevented).toBe(false);
});
it('in full bloom the first touch of a visit makes her blush; her picture keeps its description', async () => {
// 25 days of helpful morning watering: full bloom.
const log = Array.from({ length: 25 }, (_, i) =>
careEvent({ now: MORNING + i * DAY, timeZone: ZONE, from: ME, to: ME, act: 'water', amount: 0.5, clientVersion: '0.1.0' })
);
const w = wiring(log);
w.clock.now = MORNING + 25 * DAY + 3 * 3_600_000;
render(Rose, { wiring: w, timeZone: ZONE });
const img = await screen.findByRole('img', { name: /full bloom/ });
expect(img.getAttribute('alt')).not.toMatch(/blushing/);
await touchAt(w, w.clock.now);
expect(screen.getByRole('img', { name: /full bloom, blushing/ })).toBeTruthy();
});
});