1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
<!--
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 { 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,
onEdit
}: {
session: GameSession;
item: NoteRowItem;
onEdit: (item: NoteRowItem) => void;
} = $props();
const gameState = $derived(session.state);
// Base tones for monster targets, which have no per-figure colour.
const ENEMY_BASE = 0x8e2a2a;
const ALLY_BASE = 0x2a4a8e;
/** 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;
};
}
});
/** 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);
}
/** 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="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>
|