diff options
Diffstat (limited to 'web/src/lib/ui/NoteRow.svelte')
| -rw-r--r-- | web/src/lib/ui/NoteRow.svelte | 187 |
1 files changed, 152 insertions, 35 deletions
diff --git a/web/src/lib/ui/NoteRow.svelte b/web/src/lib/ui/NoteRow.svelte index 34af63e..f237c13 100644 --- a/web/src/lib/ui/NoteRow.svelte +++ b/web/src/lib/ui/NoteRow.svelte @@ -1,57 +1,174 @@ <!-- - A note row (the reference `NoteRowWidget`, Phase 3 placeholder): a - free-form coloured row. Tapping it edits the text inline and commits - through `setNoteRowText`. + A note row (the reference `NoteRowWidget`): a coloured row in the main + list. When linked it takes a gradient derived from its target row's + colour (the character's class colour, or a red/blue base for + enemy/ally monsters) with an indent and connector; monster notes are + prefixed with the standee number (or "All"). Unlinked notes use their + own colour and a note icon. Tapping opens the editor. --> <script lang="ts"> import type { GameSession } from '$lib/state/session'; - import type { NoteRowItem } from '$lib/protocol/types'; + import { isCharacter, isMonster, type NoteRowItem } from '$lib/protocol/types'; + import { classColor } from '$lib/game/art'; + import { classInfo } from '$lib/data/classInfo'; + import { t } from '$lib/i18n'; let { session, - item + item, + onEdit }: { session: GameSession; item: NoteRowItem; + onEdit: (item: NoteRowItem) => void; } = $props(); - const player = session.player; + const gameState = session.state; - let editing = $state(false); - let draft = $state(item.text); + // Base tones for monster targets, which have no per-figure colour. + const ENEMY_BASE = 0x8e2a2a; + const ALLY_BASE = 0x2a4a8e; - function colorCss(color: number): string { - return `#${color.toString(16).padStart(8, '0').slice(2)}`; - } + /** The target row, or null when unlinked / target absent. */ + const target = $derived.by(() => { + const gs = $gameState; + if (item.linkedId === '' || gs == null) return null; + return gs.currentList.find((i) => i.id === item.linkedId) ?? null; + }); + const linked = $derived(target != null); + const targetIsMonster = $derived(target != null && isMonster(target)); + + // The linked character's class colour (the reference reads it from the + // character model; the web resolves it from the campaign data). + let targetColor = $state<string | null>(null); + $effect(() => { + const tgt = target; + targetColor = null; + if (tgt != null && isCharacter(tgt)) { + let alive = true; + classInfo(tgt.edition, tgt.characterClass) + .then((c) => { + if (alive) targetColor = c ? c.color : null; + }) + .catch(() => {}); + return () => { + alive = false; + }; + } + }); - function startEdit() { - if (!$player) return; - draft = item.text; - editing = true; + /** ARGB int from a class ARGB hex string, or null. */ + function hexToArgb(hex: string | null): number | null { + if (hex == null || !/^[0-9a-f]{8}$/i.test(hex)) return null; + return parseInt(hex, 16); } - function commit() { - editing = false; - if (draft !== item.text) { - session.command('setNoteRowText', { text: draft, noteId: item.id }); + /** The base colour a note derives from (target colour or its own). */ + const base = $derived.by((): number => { + if (target != null) { + if (isCharacter(target)) { + const c = hexToArgb(targetColor); + if (c != null) return c; + } else if (isMonster(target)) { + return target.isAlly ? ALLY_BASE : ENEMY_BASE; + } } + return item.color; + }); + + /** `Color.lerp(base, white, amount)` — mix toward white. */ + function lighten(argb: number, amount: number): string { + const r = argb >> 16 & 0xff; + const g = argb >> 8 & 0xff; + const b = argb & 0xff; + const m = (c: number) => Math.round(c + (255 - c) * amount); + return `rgb(${m(r)}, ${m(g)}, ${m(b)})`; } + // Inverted gradient (darker at top) echoes the figure rows' framed look. + const gradTop = $derived(lighten(base, 0.3)); + const gradBottom = $derived(lighten(base, 0.6)); + const borderCss = $derived(colorCss(base, 0.8)); + + function colorCss(argb: number, alpha = 1): string { + const r = argb >> 16 & 0xff; + const g = argb >> 8 & 0xff; + const b = argb & 0xff; + return alpha >= 1 ? `rgb(${r}, ${g}, ${b})` : `rgba(${r}, ${g}, ${b}, ${alpha})`; + } + + // Contrast against the darker (top) part of the gradient. + const darkTop = $derived.by(() => { + const r = base >> 16 & 0xff; + const g = base >> 8 & 0xff; + const b = base & 0xff; + return 0.299 * r + 0.587 * g + 0.114 * b < 128; + }); + const fg = $derived(darkTop ? '#fff' : 'rgba(0, 0, 0, 0.87)'); + const fgFaint = $derived(darkTop ? 'rgba(255, 255, 255, 0.6)' : 'rgba(0, 0, 0, 0.54)'); + + const empty = $derived(item.text.trim() === ''); </script> -<div class="row row--note" style:background-color={colorCss(item.color)}> - <div class="note__text"> - {#if editing} - <input - class="search" - style="margin: 0; width: 100%" - bind:value={draft} - onblur={commit} - onkeydown={(e) => { - if (e.key === 'Enter') commit(); - }} - /> - {:else} - <button class="note__edit" onclick={startEdit}>{item.text || '…'}</button> - {/if} - </div> +<div + class="note__row" + role="button" + tabindex={0} + aria-label={t('noteRowTitle')} + onclick={() => onEdit(item)} + onkeydown={(e) => { + if (e.key === 'Enter' || e.key === ' ') onEdit(item); + }} + style="margin-left: {linked ? 27 : 3}px; margin-right: 3px; + background: linear-gradient(180deg, {gradTop} 0%, {gradBottom} 100%); + border: 1.3px solid {borderCss}; + color: {fg}" +> + <span class="note__icon">{linked ? '›' : '✎'}</span> + {#if linked && targetIsMonster && target} + <span class="note__standee" style="color: {fg}"> + {item.standeeNr > 0 ? item.standeeNr + ':' : t('noteRowAll') + ':'} + </span> + {/if} + <span class="note__text" style="color: {empty ? fgFaint : fg}"> + {empty ? t('noteRowHint') : item.text} + </span> </div> + +<style> + .note__row { + display: flex; + align-items: center; + gap: 6px; + height: 40px; + margin-bottom: 1px; + padding: 0 8px; + border-radius: 6px; + box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35); + cursor: pointer; + min-width: 0; + box-sizing: border-box; + } + .note__row:focus-visible { + outline: 2px solid #7ec8ff; + } + .note__icon { + flex: 0 0 auto; + font-size: 14px; + opacity: 0.75; + } + .note__standee { + flex: 0 0 auto; + font-size: 12px; + font-weight: 700; + } + .note__text { + flex: 1; + min-width: 0; + font-size: 12px; + overflow: hidden; + display: -webkit-box; + -webkit-line-clamp: 2; + -webkit-box-orient: vertical; + text-overflow: ellipsis; + } +</style> |
