@yagejs-addons/dialogue 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2920 @@
1
+ import {
2
+ EMPTY_PARSED,
3
+ LineReveal,
4
+ splitGraphemes
5
+ } from "./chunk-CU47RPEB.js";
6
+ import {
7
+ __name
8
+ } from "./chunk-7QVYU63E.js";
9
+
10
+ // src/render/DialogueTextView.ts
11
+ import { MathUtils, Transform } from "@yagejs/core";
12
+ import {
13
+ SplitTextComponent
14
+ } from "@yagejs/renderer";
15
+
16
+ // src/render/textEffects.ts
17
+ function evaluateEffect(effect, timeMs, phase, out = { dx: 0, dy: 0, scale: 1, tint: void 0 }) {
18
+ out.dx = 0;
19
+ out.dy = 0;
20
+ out.scale = 1;
21
+ out.tint = void 0;
22
+ switch (effect) {
23
+ case "wave":
24
+ out.dy = Math.sin(timeMs / 260 + phase / 14) * 1.6;
25
+ break;
26
+ case "shake":
27
+ out.dx = pseudoNoise(timeMs, phase) * 1.3;
28
+ out.dy = pseudoNoise(timeMs, phase + 99) * 1.3;
29
+ break;
30
+ case "pulse":
31
+ out.scale = 1 + 0.09 * Math.sin(timeMs / 220 + phase / 18);
32
+ break;
33
+ case "rainbow":
34
+ out.tint = hsv((timeMs / 18 + phase * 4) % 360, 0.55, 1);
35
+ break;
36
+ }
37
+ return out;
38
+ }
39
+ __name(evaluateEffect, "evaluateEffect");
40
+ function effectDrivesTint(effect) {
41
+ return effect === "rainbow";
42
+ }
43
+ __name(effectDrivesTint, "effectDrivesTint");
44
+ function pseudoNoise(timeMs, seed) {
45
+ const t = Math.floor(timeMs / 33);
46
+ const x = Math.sin(t * 12.9898 + seed * 78.233) * 43758.5453;
47
+ return x - Math.floor(x) - 0.5;
48
+ }
49
+ __name(pseudoNoise, "pseudoNoise");
50
+ function hsv(h, s, v) {
51
+ const c = v * s;
52
+ const hp = h / 60;
53
+ const x = c * (1 - Math.abs(hp % 2 - 1));
54
+ let rgb;
55
+ if (hp < 1) rgb = [c, x, 0];
56
+ else if (hp < 2) rgb = [x, c, 0];
57
+ else if (hp < 3) rgb = [0, c, x];
58
+ else if (hp < 4) rgb = [0, x, c];
59
+ else if (hp < 5) rgb = [x, 0, c];
60
+ else rgb = [c, 0, x];
61
+ const [r, g, b] = rgb;
62
+ const m = v - c;
63
+ return Math.round((r + m) * 255) << 16 | Math.round((g + m) * 255) << 8 | Math.round((b + m) * 255);
64
+ }
65
+ __name(hsv, "hsv");
66
+
67
+ // src/render/DialogueTextView.ts
68
+ var ITALIC_SKEW = -0.21;
69
+ var BOLD_OFFSET = 0.6;
70
+ var DialogueTextView = class {
71
+ constructor(cfg) {
72
+ this.cfg = cfg;
73
+ this.reveal = new LineReveal(cfg.charsPerSec);
74
+ this.reveal.setCompletionListener(() => this.revealListener?.());
75
+ this.reveal.setBeatListener((beat) => this.beatListener?.(beat));
76
+ if (cfg.box) this.setBox(cfg.box.x, cfg.box.y, cfg.box.width);
77
+ }
78
+ cfg;
79
+ static {
80
+ __name(this, "DialogueTextView");
81
+ }
82
+ scene;
83
+ line;
84
+ parsed;
85
+ boxX = 0;
86
+ boxY = 0;
87
+ wrapWidth = 200;
88
+ /** Top-left the split container sits at (box origin). */
89
+ layoutOriginX = 0;
90
+ layoutOriginY = 0;
91
+ /** Optional per-frame origin (a moving NPC's head) for diegetic bubbles. */
92
+ originProvider;
93
+ /** `nonSpacePrefix[k]` = count of non-space graphemes among the first `k`. */
94
+ nonSpacePrefix = new Int32Array(1);
95
+ /** Non-space glyphs currently visible. */
96
+ shownCount = -1;
97
+ /** Headless reveal clock — owns the grapheme cursor, `[pause]` arming, hold +
98
+ * per-line + per-run speed, and the fired-once completion. This view keeps
99
+ * only the pixi-`SplitText` concerns (glyph prefix mapping + per-glyph style
100
+ * fan-out) and maps the clock's grapheme cursor onto them. */
101
+ reveal;
102
+ /** Elapsed ms for animated per-glyph EFFECTS (wave/shake/…) — distinct from
103
+ * the reveal cursor, which LineReveal owns. */
104
+ elapsedMs = 0;
105
+ /** Scratch for {@link evaluateEffect} — one object reused across all glyphs. */
106
+ effectScratch = { dx: 0, dy: 0, scale: 1, tint: void 0 };
107
+ /** Reveal-completed listener, registered by the Session through
108
+ * {@link setRevealListener} (a private seam, not a public field, so a
109
+ * game can't clobber the session's wiring). */
110
+ revealListener;
111
+ /** Reveal-beat listener (ticks + inline markers), registered by the Session
112
+ * through {@link setBeatListener} — same private-seam discipline. */
113
+ beatListener;
114
+ /** Master visibility gate ({@link setVisible}); hides the line WITHOUT
115
+ * clearing it, so a hide/show round-trip resumes mid-typewriter. */
116
+ hidden = false;
117
+ /** Attach to a scene (host lifecycle). Must run before the first `present`. */
118
+ mount(scene) {
119
+ this.scene = scene;
120
+ }
121
+ /** Top-left of the text region, in screen px, plus the wrap width. */
122
+ setBox(x, y, width) {
123
+ this.boxX = x;
124
+ this.boxY = y;
125
+ this.wrapWidth = width;
126
+ }
127
+ /**
128
+ * Make the text track a per-frame origin (a diegetic bubble following an
129
+ * NPC). The provider returns the top-left the laid-out box should sit at;
130
+ * pass `undefined` to pin the text (the default box-dialogue behaviour).
131
+ */
132
+ setOrigin(provider) {
133
+ this.originProvider = provider;
134
+ }
135
+ /** TextChannel entry point: render + reveal a fully-resolved line. */
136
+ present(line) {
137
+ this.show(line.text, line.speed);
138
+ }
139
+ /** TextChannel: reveal everything now. */
140
+ completeReveal() {
141
+ this.skipToEnd();
142
+ }
143
+ /** TextChannel: true once the line is fully revealed. */
144
+ isRevealComplete() {
145
+ return this.reveal.isComplete();
146
+ }
147
+ /** Hold-to-speed multiplier (1 = normal, e.g. 3 while the skip key is held). */
148
+ setSpeedMultiplier(m) {
149
+ this.reveal.setSpeedMultiplier(m);
150
+ }
151
+ isRevealing() {
152
+ return this.reveal.isRevealing();
153
+ }
154
+ /**
155
+ * Show or hide the body text WITHOUT disturbing reveal progress. Toggles
156
+ * the laid-out split container's visibility; the per-glyph reveal cursor,
157
+ * timers, and styling are untouched, so a cutscene can hide mid-typewriter and
158
+ * show again to resume exactly where it left off.
159
+ */
160
+ setVisible(visible) {
161
+ this.hidden = !visible;
162
+ this.applyHidden();
163
+ }
164
+ /** Register the reveal-completed listener. Session-owned (a private seam, not a
165
+ * public field a game could clobber); pass `undefined` to clear. */
166
+ setRevealListener(listener) {
167
+ this.revealListener = listener;
168
+ }
169
+ /** Register the reveal-beat listener (ticks + inline markers). Session-owned;
170
+ * pass `undefined` to clear. The clock emits in char order as glyphs reveal. */
171
+ setBeatListener(listener) {
172
+ this.beatListener = listener;
173
+ }
174
+ /** Build the split for a parsed line and start revealing. */
175
+ show(parsed, lineSpeed = 1) {
176
+ this.clearLine();
177
+ this.parsed = parsed;
178
+ this.elapsedMs = 0;
179
+ this.shownCount = -1;
180
+ if (parsed.length > 0) this.buildLine(parsed);
181
+ this.reveal.begin(parsed, lineSpeed);
182
+ this.applyReveal();
183
+ this.reposition();
184
+ this.applyHidden();
185
+ }
186
+ /** Apply the master visibility gate to the laid-out line — toggles the split
187
+ * container, leaving the per-glyph reveal state intact. */
188
+ applyHidden() {
189
+ if (this.line) this.line.comp.splitText.visible = !this.hidden;
190
+ }
191
+ /** Reveal everything immediately (jump-to-end on a click/tap). */
192
+ skipToEnd() {
193
+ this.reveal.complete();
194
+ this.applyReveal();
195
+ }
196
+ update(dt) {
197
+ if (!this.parsed) return;
198
+ this.elapsedMs += dt;
199
+ this.reveal.update(dt);
200
+ this.applyReveal();
201
+ this.reposition();
202
+ }
203
+ clear() {
204
+ this.clearLine();
205
+ this.originProvider = void 0;
206
+ }
207
+ /** Per-line teardown (also the first step of `show()`). The reveal clock is
208
+ * re-armed by the next `show()` via {@link LineReveal.begin}, so there is no
209
+ * reveal state to reset here. */
210
+ clearLine() {
211
+ this.line?.entity.destroy();
212
+ this.line = void 0;
213
+ this.parsed = void 0;
214
+ this.shownCount = -1;
215
+ }
216
+ /** Permanent teardown. (No measurer nodes to free — SplitText owns layout.) */
217
+ dispose() {
218
+ this.clear();
219
+ }
220
+ // ── build ───────────────────────────────────────────────────────────────────
221
+ buildLine(parsed) {
222
+ if (!this.scene) return;
223
+ const text = parsed.runs.map((r) => r.text).join("");
224
+ this.layoutOriginX = this.boxX;
225
+ this.layoutOriginY = this.boxY;
226
+ const entity = this.scene.spawn("dlg-line");
227
+ entity.add(new Transform()).setPosition(this.layoutOriginX, this.layoutOriginY);
228
+ const comp = entity.add(new SplitTextComponent(this.lineSplitOptions(text)));
229
+ const chars = comp.chars;
230
+ const root = comp.splitText;
231
+ const styles = this.buildRevealTables(parsed);
232
+ const effectMetas = [];
233
+ chars.forEach((node, i) => {
234
+ const style = styles[i] ?? {};
235
+ node.tint = style.color ?? this.cfg.textColor;
236
+ node.visible = false;
237
+ this.applyWeight(node, style);
238
+ if (style.effect) {
239
+ effectMetas.push({
240
+ node,
241
+ effect: style.effect,
242
+ baseX: node.position.x,
243
+ baseY: node.position.y,
244
+ splitX: localInSplit(node, root).x
245
+ });
246
+ }
247
+ });
248
+ this.line = { entity, comp, chars, effectMetas };
249
+ }
250
+ /**
251
+ * One grapheme-segmentation pass per line (build-time only — nothing
252
+ * re-segments per frame), producing both reveal tables:
253
+ * - `nonSpacePrefix`: grapheme cursor → count of non-space glyphs shown
254
+ * - returned styles: the run-style for each NON-SPACE glyph, in reading
255
+ * order (1:1 with SplitText's `chars`, which drops whitespace)
256
+ * Segmenting per run matches how markup.ts counted `length`/`atChar`, so
257
+ * the cursor, pauses, and styles all share one basis.
258
+ */
259
+ buildRevealTables(parsed) {
260
+ const styles = [];
261
+ const prefix = [];
262
+ let shown = 0;
263
+ for (const run of parsed.runs) {
264
+ for (const g of splitGraphemes(run.text)) {
265
+ prefix.push(shown);
266
+ if (/\s/.test(g)) continue;
267
+ shown++;
268
+ styles.push(run.style);
269
+ }
270
+ }
271
+ prefix.push(shown);
272
+ this.nonSpacePrefix = Int32Array.from(prefix);
273
+ return styles;
274
+ }
275
+ /**
276
+ * Apply a run's bold/italic to one glyph. On the bitmap path we must NOT swap
277
+ * to the baked variant atlas — each atlas has its own `baseLineOffset`, which
278
+ * lifts/drops a swapped glyph out of line with the regular runs. So we keep the
279
+ * regular-atlas glyph (baseline intact) and synthesise:
280
+ * - italic → shear it in place (`skew.x`)
281
+ * - bold → overlay a 1px-offset copy as a CHILD, so it rides the parent's
282
+ * reveal/visibility, tint cascade, skew, and per-frame effects for
283
+ * free (no separate bookkeeping).
284
+ * On the canvas path, real bold/italic of the same family is baseline-safe, so
285
+ * we just set the style flags.
286
+ */
287
+ applyWeight(node, style) {
288
+ if (this.cfg.bitmapFont) {
289
+ if (style.italic) node.skew.x = ITALIC_SKEW;
290
+ if (style.bold) {
291
+ const Ctor = node.constructor;
292
+ const dup = new Ctor({ text: node.text, style: node.style });
293
+ dup.position.set(BOLD_OFFSET, 0);
294
+ dup.tint = 16777215;
295
+ node.addChild(dup);
296
+ }
297
+ return;
298
+ }
299
+ if (style.bold || style.italic) {
300
+ const s = { fontSize: this.cfg.textSize, fill: 16777215, lineHeight: this.cfg.lineHeight };
301
+ if (this.cfg.fontFamily) s.fontFamily = this.cfg.fontFamily;
302
+ if (style.bold) s.fontWeight = "bold";
303
+ if (style.italic) s.fontStyle = "italic";
304
+ node.style = s;
305
+ }
306
+ }
307
+ // ── reveal + effects ─────────────────────────────────────────────────────────
308
+ applyReveal() {
309
+ if (!this.line) return;
310
+ const cursor = MathUtils.clamp(Math.floor(this.reveal.revealed), 0, this.nonSpacePrefix.length - 1);
311
+ const shown = this.nonSpacePrefix[cursor];
312
+ if (shown === this.shownCount) return;
313
+ const prev = this.shownCount < 0 ? 0 : this.shownCount;
314
+ const chars = this.line.chars;
315
+ const lo = Math.min(prev, shown);
316
+ const hi = Math.max(prev, shown);
317
+ for (let i = lo; i < hi; i++) chars[i].visible = i < shown;
318
+ this.shownCount = shown;
319
+ }
320
+ /**
321
+ * Per-frame placement: follow a moving origin (bubble mode) by moving the
322
+ * split container's `Transform` — every glyph inherits it — then apply
323
+ * per-glyph animated effects on top, in the glyph's own (parent) space.
324
+ * Only effect-bearing glyphs are walked; a static pinned line costs nothing.
325
+ */
326
+ reposition() {
327
+ const line = this.line;
328
+ if (!line) return;
329
+ if (this.originProvider) {
330
+ const o = this.originProvider();
331
+ line.entity.get(Transform).setPosition(o.x, o.y);
332
+ }
333
+ for (const m of line.effectMetas) {
334
+ if (!m.node.visible) continue;
335
+ const out = evaluateEffect(m.effect, this.elapsedMs, m.splitX, this.effectScratch);
336
+ m.node.position.set(m.baseX + out.dx, m.baseY + out.dy);
337
+ if (out.scale !== 1) m.node.scale.set(out.scale, out.scale);
338
+ if (effectDrivesTint(m.effect) && out.tint !== void 0) m.node.tint = out.tint;
339
+ }
340
+ }
341
+ // ── node construction ─────────────────────────────────────────────────────────
342
+ /** Options for the per-line split: regular atlas, wrap to the box, white fill
343
+ * (per-glyph `tint` carries the real colour). */
344
+ lineSplitOptions(text) {
345
+ const style = {
346
+ fontSize: this.cfg.textSize,
347
+ fill: 16777215,
348
+ wordWrap: true,
349
+ wordWrapWidth: this.wrapWidth,
350
+ lineHeight: this.cfg.lineHeight
351
+ };
352
+ const font = this.cfg.bitmapFont ?? this.cfg.fontFamily;
353
+ if (font) style.fontFamily = font;
354
+ const base = {
355
+ text,
356
+ style,
357
+ layer: this.cfg.layer,
358
+ visible: true
359
+ };
360
+ if (this.cfg.bitmapFont) base.bitmap = true;
361
+ return base;
362
+ }
363
+ };
364
+ function localInSplit(node, root) {
365
+ let x = 0;
366
+ let y = 0;
367
+ let n = node;
368
+ while (n && n !== root) {
369
+ x += n.position.x;
370
+ y += n.position.y;
371
+ n = n.parent ?? void 0;
372
+ }
373
+ return { x, y };
374
+ }
375
+ __name(localInSplit, "localInSplit");
376
+
377
+ // src/render/BubbleTextView.ts
378
+ var BubbleTextView = class extends DialogueTextView {
379
+ constructor(cfg, layout) {
380
+ super({
381
+ ...cfg,
382
+ // Initial wrap width; updated per line in present() as the bubble widens.
383
+ box: { x: 0, y: 0, width: 0 }
384
+ });
385
+ this.layout = layout;
386
+ }
387
+ layout;
388
+ static {
389
+ __name(this, "BubbleTextView");
390
+ }
391
+ sceneRef;
392
+ mount(scene) {
393
+ super.mount(scene);
394
+ this.sceneRef = scene;
395
+ }
396
+ /** Route the missing-actor warning to the engine Logger (the layout owns the
397
+ * shared anchor resolver). The base view has no diagnostics of its own. */
398
+ setDiagnostics(warn) {
399
+ this.layout.setDiagnostics(warn);
400
+ }
401
+ present(line) {
402
+ const size = this.layout.sizeFor(line);
403
+ this.setBox(0, 0, this.layout.textWrapWidth(size));
404
+ const speakerId = line.speaker?.id;
405
+ this.setOrigin(() => {
406
+ const anchor = this.sceneRef ? this.layout.anchorFor(this.sceneRef, speakerId) : { x: 0, y: 0 };
407
+ return this.layout.originFor(anchor, size);
408
+ });
409
+ super.present(line);
410
+ }
411
+ };
412
+
413
+ // src/render/BoxTextView.ts
414
+ var BoxTextView = class extends DialogueTextView {
415
+ constructor(cfg, layout) {
416
+ super({ ...cfg, box: { x: 0, y: 0, width: 0 } });
417
+ this.layout = layout;
418
+ }
419
+ layout;
420
+ static {
421
+ __name(this, "BoxTextView");
422
+ }
423
+ present(line) {
424
+ const region = this.layout.textRegion();
425
+ this.setBox(0, 0, region.width);
426
+ this.setOrigin(() => {
427
+ const r = this.layout.textRegion();
428
+ return { x: r.x, y: r.y };
429
+ });
430
+ super.present(line);
431
+ }
432
+ };
433
+
434
+ // src/render/layers.ts
435
+ var DIALOGUE_LAYER_FRAME = "dialogue-frame";
436
+ var DIALOGUE_LAYER_TEXT = "dialogue-text";
437
+ var DIALOGUE_LAYER_AVATAR = "dialogue-avatar";
438
+ var DIALOGUE_LAYERS = [
439
+ { name: DIALOGUE_LAYER_FRAME, order: 1100, space: "screen" },
440
+ // Avatar between frame and text so a portrait can tuck behind the box edge.
441
+ { name: DIALOGUE_LAYER_AVATAR, order: 1105, space: "screen" },
442
+ { name: DIALOGUE_LAYER_TEXT, order: 1110, space: "screen" }
443
+ ];
444
+
445
+ // src/actor/DialogueActor.ts
446
+ import { Component, Transform as Transform2 } from "@yagejs/core";
447
+
448
+ // src/actor/ActorRegistry.ts
449
+ var ActorRegistry = class {
450
+ static {
451
+ __name(this, "ActorRegistry");
452
+ }
453
+ actors = /* @__PURE__ */ new Map();
454
+ register(speaker, actor) {
455
+ this.actors.set(speaker, actor);
456
+ }
457
+ unregister(speaker, actor) {
458
+ if (this.actors.get(speaker) === actor) this.actors.delete(speaker);
459
+ }
460
+ resolve(speaker) {
461
+ return speaker ? this.actors.get(speaker) : void 0;
462
+ }
463
+ };
464
+ var registries = /* @__PURE__ */ new WeakMap();
465
+ function actorRegistryFor(scene) {
466
+ let registry = registries.get(scene);
467
+ if (!registry) {
468
+ registry = new ActorRegistry();
469
+ registries.set(scene, registry);
470
+ }
471
+ return registry;
472
+ }
473
+ __name(actorRegistryFor, "actorRegistryFor");
474
+
475
+ // src/actor/DialogueActor.ts
476
+ var DialogueActor = class extends Component {
477
+ constructor(opts) {
478
+ super();
479
+ this.opts = opts;
480
+ }
481
+ opts;
482
+ static {
483
+ __name(this, "DialogueActor");
484
+ }
485
+ get speaker() {
486
+ return this.opts.speaker;
487
+ }
488
+ onAdd() {
489
+ actorRegistryFor(this.scene).register(this.opts.speaker, this);
490
+ }
491
+ onDestroy() {
492
+ actorRegistryFor(this.scene).unregister(this.opts.speaker, this);
493
+ }
494
+ /** Bubble anchor in world space: the entity position plus the configured offset. */
495
+ anchorWorld() {
496
+ const t = this.entity.tryGet(Transform2);
497
+ const p = t?.position ?? { x: 0, y: 0 };
498
+ const a = this.opts.anchor ?? { x: 0, y: 0 };
499
+ return { x: p.x + a.x, y: p.y + a.y };
500
+ }
501
+ setExpression(expression) {
502
+ this.opts.onExpression?.(this.entity, expression);
503
+ }
504
+ setSpeaking(speaking) {
505
+ this.opts.onSpeaking?.(this.entity, speaking);
506
+ }
507
+ };
508
+
509
+ // src/render/bubbleAnchor.ts
510
+ var BubbleAnchorResolver = class {
511
+ /**
512
+ * @param fallback Ultimate anchor when nothing better is known (a speaker
513
+ * never seen and no prior bubble). Defaults to the world origin; a
514
+ * pure-bubble bundle that shows narrator lines should point this at its
515
+ * camera centre so a speakerless line lands on screen.
516
+ */
517
+ constructor(fallback = () => ({ x: 0, y: 0 })) {
518
+ this.fallback = fallback;
519
+ }
520
+ fallback;
521
+ static {
522
+ __name(this, "BubbleAnchorResolver");
523
+ }
524
+ lastKnown = /* @__PURE__ */ new Map();
525
+ lastAnchor;
526
+ warned = /* @__PURE__ */ new Set();
527
+ warn;
528
+ /** Wire the diagnostics sink (the controller injects the engine-Logger one). */
529
+ setDiagnostics(warn) {
530
+ this.warn = warn;
531
+ }
532
+ /**
533
+ * World anchor for `speakerId`. Live actor → its anchor (caches refreshed).
534
+ * Missing → last-known for that speaker, else the most recent any-speaker
535
+ * anchor, else {@link fallback}. Warns at most once per declared speaker id
536
+ * (per resolver instance); a speakerless line never warns.
537
+ */
538
+ resolve(scene, speakerId) {
539
+ const actor = actorRegistryFor(scene).resolve(speakerId);
540
+ if (actor) {
541
+ const anchor = actor.anchorWorld();
542
+ if (speakerId !== void 0) this.lastKnown.set(speakerId, anchor);
543
+ this.lastAnchor = anchor;
544
+ return anchor;
545
+ }
546
+ if (speakerId !== void 0 && !this.warned.has(speakerId)) {
547
+ this.warned.add(speakerId);
548
+ this.warn?.(
549
+ `no DialogueActor is registered for speaker "${speakerId}"; anchoring its bubble at the last-known / fallback position. Register a DialogueActor (even on an invisible entity) to place it deliberately.`
550
+ );
551
+ }
552
+ const known = speakerId !== void 0 ? this.lastKnown.get(speakerId) : void 0;
553
+ return known ?? this.lastAnchor ?? this.fallback();
554
+ }
555
+ };
556
+
557
+ // src/render/bubbleSizing.ts
558
+ import { measureWrappedText } from "@yagejs/renderer";
559
+ function bubbleSize(plainText, cfg) {
560
+ const oneLine = cfg.lineHeight + 2 * cfg.padding;
561
+ const font = cfg.bitmapFont ?? cfg.fontFamily;
562
+ const fontOpt = font !== void 0 ? { fontFamily: font } : {};
563
+ const bitmapOpt = cfg.bitmapFont !== void 0 ? { bitmap: true } : {};
564
+ const natural = measureWrappedText(plainText, {
565
+ fontSize: cfg.textSize,
566
+ lineHeight: cfg.lineHeight,
567
+ ...fontOpt,
568
+ ...bitmapOpt
569
+ });
570
+ const wantWidth = natural.width + 2 * cfg.padding;
571
+ if (wantWidth <= cfg.maxWidth) {
572
+ return {
573
+ width: Math.max(cfg.minWidth, wantWidth),
574
+ height: Math.max(cfg.minHeight, oneLine)
575
+ };
576
+ }
577
+ const inner = cfg.maxWidth - 2 * cfg.padding;
578
+ const wrapped = measureWrappedText(plainText, {
579
+ fontSize: cfg.textSize,
580
+ lineHeight: cfg.lineHeight,
581
+ wordWrapWidth: inner,
582
+ ...fontOpt,
583
+ ...bitmapOpt
584
+ });
585
+ return {
586
+ width: cfg.maxWidth,
587
+ height: Math.max(cfg.minHeight, wrapped.lineCount * cfg.lineHeight + 2 * cfg.padding)
588
+ };
589
+ }
590
+ __name(bubbleSize, "bubbleSize");
591
+
592
+ // src/render/BubbleLayout.ts
593
+ var BubbleLayout = class {
594
+ constructor(cfg) {
595
+ this.cfg = cfg;
596
+ this.anchors = new BubbleAnchorResolver(cfg.fallbackAnchor);
597
+ }
598
+ cfg;
599
+ static {
600
+ __name(this, "BubbleLayout");
601
+ }
602
+ anchors;
603
+ /** One-line memo: the session presents one line to chrome then text, so a
604
+ * one-deep cache makes the second `sizeFor` free (no redundant measure pass). */
605
+ memoLine;
606
+ memoSize;
607
+ /** Reserved portrait column for the current line (set by the in-bubble avatar
608
+ * before the chrome/text present). */
609
+ inset;
610
+ /** The current bubble content size — the say bubble (from {@link sizeFor}) or a
611
+ * choice panel (from {@link setChoicePanelSize}). The in-bubble avatar centres
612
+ * in this, so it follows whichever is on screen. */
613
+ active;
614
+ listeners = [];
615
+ /** Reserve (or clear with `undefined`) a portrait column inside the bubble.
616
+ * The bubble (and a bubble choice panel) then grows to contain it and the
617
+ * text/rows reflow to the narrowed column; the avatar sets this per line
618
+ * before the chrome/text/choices present. */
619
+ setPortraitInset(inset) {
620
+ this.inset = inset;
621
+ this.memoLine = void 0;
622
+ }
623
+ /** The reserved portrait column (or undefined) — a bubble choice presenter
624
+ * reads it to reflow its panel around the portrait. */
625
+ portraitInset() {
626
+ return this.inset;
627
+ }
628
+ /** Register a callback fired when the active bubble content size changes (a
629
+ * say line sizes its bubble, or a choice commits its panel) — the in-bubble
630
+ * avatar re-places. */
631
+ onChange(listener) {
632
+ this.listeners.push(listener);
633
+ }
634
+ /** The current bubble content size the avatar centres in (say bubble or choice
635
+ * panel). */
636
+ activeSize() {
637
+ return this.active;
638
+ }
639
+ /** A bubble choice presenter commits its (inset-grown) panel size here so the
640
+ * in-bubble avatar follows the panel, not the say bubble. */
641
+ setChoicePanelSize(size) {
642
+ this.setActive(size);
643
+ }
644
+ setActive(size) {
645
+ if (this.active && this.active.width === size.width && this.active.height === size.height) return;
646
+ this.active = size;
647
+ for (const fn of this.listeners) fn();
648
+ }
649
+ /** Inner padding (px) — the presenters position the name/caret/text by it. */
650
+ get padding() {
651
+ return this.cfg.padding;
652
+ }
653
+ /** Gap between the speaker anchor and the bubble's bottom edge (px). */
654
+ get offsetY() {
655
+ return this.cfg.offsetY;
656
+ }
657
+ /** Wire the missing-actor warning to the engine Logger (the controller's sink). */
658
+ setDiagnostics(warn) {
659
+ this.anchors.setDiagnostics(warn);
660
+ }
661
+ /** Outer bubble size to fit this line's text (+ a reserved portrait column,
662
+ * if one is registered) — measured once, then memoized for the companion
663
+ * presenter's read of the same line. */
664
+ sizeFor(line) {
665
+ if (line === this.memoLine && this.memoSize) return this.memoSize;
666
+ const plain = line.text.runs.map((r) => r.text).join("");
667
+ const reserve = this.inset?.width ?? 0;
668
+ const textSize = bubbleSize(plain, {
669
+ minWidth: this.cfg.minWidth,
670
+ maxWidth: Math.max(this.cfg.minWidth, this.cfg.maxWidth - reserve),
671
+ padding: this.cfg.padding,
672
+ minHeight: this.cfg.height,
673
+ textSize: this.cfg.textSize,
674
+ lineHeight: this.cfg.lineHeight,
675
+ fontFamily: this.cfg.fontFamily,
676
+ bitmapFont: this.cfg.bitmapFont
677
+ });
678
+ const size = {
679
+ width: textSize.width + reserve,
680
+ height: Math.max(textSize.height, (this.inset?.height ?? 0) + 2 * this.cfg.padding)
681
+ };
682
+ this.memoLine = line;
683
+ this.memoSize = size;
684
+ this.setActive(size);
685
+ return size;
686
+ }
687
+ /** Body-text wrap width inside the bubble — the inner width minus the
688
+ * reserved portrait column (so the text reflows past an in-bubble avatar). */
689
+ textWrapWidth(size) {
690
+ return size.width - 2 * this.cfg.padding - (this.inset?.width ?? 0);
691
+ }
692
+ /** World anchor for a speaker: a live {@link DialogueActor}'s head, else the
693
+ * last-known / fallback position (and a once-per-speaker warning). Shared by
694
+ * all three bubble presenters so they track the same actor. */
695
+ anchorFor(scene, speakerId) {
696
+ return this.anchors.resolve(scene, speakerId);
697
+ }
698
+ /** Inner top-left a content-sized bubble's body sits at, from the speaker
699
+ * anchor + the bubble size (the once-derived origin formula). Shifts past a
700
+ * left-side portrait column so the text reflows beside it. */
701
+ originFor(anchor, size) {
702
+ let x = anchor.x - size.width / 2 + this.cfg.padding;
703
+ if (this.inset?.side === "left") x += this.inset.width;
704
+ return { x, y: anchor.y - (this.cfg.offsetY + size.height) + this.cfg.padding };
705
+ }
706
+ };
707
+
708
+ // src/render/BoxLayout.ts
709
+ import { measureWrappedText as measureWrappedText2 } from "@yagejs/renderer";
710
+ var TEXT_GAP = 4;
711
+ function stackChoiceRows(rowHeights, box, padding) {
712
+ const x = box.x + padding;
713
+ const width = box.width - 2 * padding;
714
+ const rects = [];
715
+ let bottom = box.y + box.height - padding;
716
+ for (let i = rowHeights.length - 1; i >= 0; i--) {
717
+ const h = rowHeights[i] ?? 0;
718
+ bottom -= h;
719
+ rects[i] = { x, y: bottom, width, height: h };
720
+ }
721
+ return rects;
722
+ }
723
+ __name(stackChoiceRows, "stackChoiceRows");
724
+ var BoxLayout = class {
725
+ constructor(cfg) {
726
+ this.cfg = cfg;
727
+ this.frame = this.frameAt("bottom", cfg.box.height);
728
+ }
729
+ cfg;
730
+ static {
731
+ __name(this, "BoxLayout");
732
+ }
733
+ /** Design viewport (the renderer's `virtualSize`) the box is placed within —
734
+ * bound at mount via {@link setViewport}. Defaults to a sane size so headless
735
+ * use (no renderer) and pre-mount calls still produce a valid frame. */
736
+ viewW = 800;
737
+ viewH = 600;
738
+ insets = /* @__PURE__ */ new Map();
739
+ listeners = [];
740
+ /** The committed frame — moved by `meta.position`, grown for a choice. */
741
+ frame;
742
+ /** The line currently laid out (for the choice panel's prompt + nameplate). */
743
+ line;
744
+ /**
745
+ * Bind the design viewport (the renderer's `virtualSize`), read at mount, so
746
+ * the box is a full-width bottom bar at any resolution and `meta.position`
747
+ * places the frame against the true screen. Recomputes the resting frame.
748
+ */
749
+ setViewport(width, height) {
750
+ this.viewW = width;
751
+ this.viewH = height;
752
+ this.commit(this.frameAt(positionOf(this.line), this.cfg.box.height));
753
+ }
754
+ /** Register a callback fired when the committed frame changes (a choice grows
755
+ * it, or an inset reflows the text) — the chrome redraws, the text re-places. */
756
+ onChange(listener) {
757
+ this.listeners.push(listener);
758
+ }
759
+ /** The frame rect for the current line (read by the chrome to draw + place). */
760
+ frameRect() {
761
+ return this.frame;
762
+ }
763
+ /** Inner content width — choice rows wrap to this. Frame minus padding minus
764
+ * any registered insets, so choices reflow around an in-box avatar the same
765
+ * way the body text does. */
766
+ contentWidth() {
767
+ return this.viewW - 2 * this.cfg.box.marginX - 2 * this.cfg.padding - this.insetWidth("left") - this.insetWidth("right");
768
+ }
769
+ /** Inner padding between the frame and its contents — an in-box presenter
770
+ * aligns its column to this so it sits inside the border, like the text. */
771
+ padding() {
772
+ return this.cfg.padding;
773
+ }
774
+ /** Lay out a say/prompt line: place the base-height frame at its
775
+ * `meta.position`. Commits the frame (firing {@link onChange} if it moved). */
776
+ layoutLine(line) {
777
+ this.line = line;
778
+ this.commit(this.frameAt(positionOf(line), this.cfg.box.height));
779
+ return this.frame;
780
+ }
781
+ /**
782
+ * Grow the frame to fit a choice: nameplate band + optional prompt + the
783
+ * rows, capped at the field. Commits the grown frame (firing {@link onChange}
784
+ * so the chrome/nameplate/prompt follow) and returns the row rects stacked
785
+ * inside it.
786
+ */
787
+ layoutChoicePanel(rowHeights) {
788
+ const promptH = this.promptHeight();
789
+ const headH = promptH > 0 ? promptH + this.cfg.choiceGap : 0;
790
+ const rows = rowHeights.reduce((a, h) => a + h, 0);
791
+ const content = this.cfg.padding + this.bodyOffset() + headH + rows + this.cfg.padding;
792
+ const maxH = this.viewH - 2 * this.cfg.box.marginY;
793
+ const height = Math.min(Math.max(this.cfg.box.height, content), maxH);
794
+ this.commit(this.frameAt(positionOf(this.line), height));
795
+ const insetL = this.insetWidth("left");
796
+ const insetR = this.insetWidth("right");
797
+ const inner = {
798
+ x: this.frame.x + insetL,
799
+ y: this.frame.y,
800
+ width: this.frame.width - insetL - insetR,
801
+ height: this.frame.height
802
+ };
803
+ return stackChoiceRows(rowHeights, inner, this.cfg.padding);
804
+ }
805
+ /** Body-text region inside the current frame: below the nameplate band, inset
806
+ * by padding, minus any registered insets (so text reflows around an avatar).
807
+ * For a choice, this is the prompt region above the rows. */
808
+ textRegion() {
809
+ let x = this.frame.x + this.cfg.padding;
810
+ let width = this.frame.width - 2 * this.cfg.padding;
811
+ for (const inset of this.insets.values()) {
812
+ width -= inset.width;
813
+ if (inset.side === "left") x += inset.width;
814
+ }
815
+ return { x, y: this.frame.y + this.cfg.padding + this.bodyOffset(), width };
816
+ }
817
+ /** Top-left of the nameplate inside the current frame. */
818
+ nameplatePos() {
819
+ return { x: this.frame.x + this.cfg.padding, y: this.frame.y + this.cfg.padding - 1 };
820
+ }
821
+ /** Bottom-right continue-caret position inside the current frame. */
822
+ caretPos(size) {
823
+ return {
824
+ x: this.frame.x + this.frame.width - this.cfg.padding - size.width,
825
+ y: this.frame.y + this.frame.height - this.cfg.padding - size.height - 1
826
+ };
827
+ }
828
+ /**
829
+ * Reserve (or clear with `undefined`) a left/right column the body text
830
+ * reflows around — the avatar-reflow seam. The reference in-box avatar
831
+ * presenter registers one keyed by its own id. Fires {@link onChange} so a
832
+ * text view already showing this line reflows.
833
+ */
834
+ setInset(key, inset) {
835
+ const prev = this.insets.get(key);
836
+ if (inset) this.insets.set(key, inset);
837
+ else this.insets.delete(key);
838
+ if (!sameInset(prev, inset)) this.notify();
839
+ }
840
+ /** The reserved width on a side (0 if none) — an avatar presenter reads it to
841
+ * place itself in the column it reserved. */
842
+ insetWidth(side) {
843
+ let w = 0;
844
+ for (const inset of this.insets.values()) if (inset.side === side) w += inset.width;
845
+ return w;
846
+ }
847
+ /** Distance from the frame top to the body text (nameplate band + gap). */
848
+ bodyOffset() {
849
+ return this.cfg.nameSize + TEXT_GAP;
850
+ }
851
+ /** Measure the current choice line's prompt (0 when there is none). */
852
+ promptHeight() {
853
+ const text = this.line?.text;
854
+ if (!text || text.length === 0) return 0;
855
+ const plain = text.runs.map((r) => r.text).join("");
856
+ const font = this.cfg.bitmapFont ?? this.cfg.fontFamily;
857
+ const measured = measureWrappedText2(plain, {
858
+ fontSize: this.cfg.textSize,
859
+ lineHeight: this.cfg.lineHeight,
860
+ wordWrapWidth: this.textRegion().width,
861
+ ...font !== void 0 ? { fontFamily: font } : {},
862
+ ...this.cfg.bitmapFont !== void 0 ? { bitmap: true } : {}
863
+ });
864
+ return measured.height;
865
+ }
866
+ /** Place a full-width frame of `height` at `position` within the design
867
+ * viewport: `bottom` anchors `marginY` from the bottom edge, `top` mirrors it
868
+ * to the top, `center` centres. The width is the viewport minus side margins. */
869
+ frameAt(position, height) {
870
+ const { marginX, marginY } = this.cfg.box;
871
+ let y;
872
+ if (position === "top") y = marginY;
873
+ else if (position === "center") y = (this.viewH - height) / 2;
874
+ else y = this.viewH - marginY - height;
875
+ return { x: marginX, y, width: this.viewW - 2 * marginX, height };
876
+ }
877
+ commit(frame) {
878
+ if (sameRect(this.frame, frame)) return;
879
+ this.frame = frame;
880
+ this.notify();
881
+ }
882
+ notify() {
883
+ for (const fn of this.listeners) fn();
884
+ }
885
+ };
886
+ function positionOf(line) {
887
+ const p = line?.meta?.["position"];
888
+ return p === "top" || p === "center" ? p : "bottom";
889
+ }
890
+ __name(positionOf, "positionOf");
891
+ function sameRect(a, b) {
892
+ return a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
893
+ }
894
+ __name(sameRect, "sameRect");
895
+ function sameInset(a, b) {
896
+ if (a === void 0 || b === void 0) return a === b;
897
+ return a.side === b.side && a.width === b.width;
898
+ }
899
+ __name(sameInset, "sameInset");
900
+
901
+ // src/chrome/DialogueChrome.ts
902
+ import { Transform as Transform3 } from "@yagejs/core";
903
+ import {
904
+ createNineSlice,
905
+ GraphicsComponent,
906
+ RendererKey,
907
+ TextComponent
908
+ } from "@yagejs/renderer";
909
+
910
+ // src/factory/theme.ts
911
+ var DEFAULT_CARET_BLINK_MS = 260;
912
+ var DEFAULT_CARET_SIZE = {
913
+ width: 7,
914
+ height: 5
915
+ };
916
+ var DEFAULT_CHOICE_GAP = 6;
917
+ var DEFAULT_TAIL_LEAN = { x: -3, y: -2 };
918
+ var CHROME_STYLE_DEFAULT = "default";
919
+ var CHROME_STYLE_NONE = "none";
920
+ function boxFrameStyles(textured) {
921
+ if (!textured) return void 0;
922
+ const out = {};
923
+ for (const [name, style] of Object.entries(textured)) out[name] = style.frame;
924
+ return out;
925
+ }
926
+ __name(boxFrameStyles, "boxFrameStyles");
927
+ function defaultBubbleFrame(textured) {
928
+ return textured?.[CHROME_STYLE_DEFAULT]?.bubble;
929
+ }
930
+ __name(defaultBubbleFrame, "defaultBubbleFrame");
931
+
932
+ // src/chrome/caret.ts
933
+ function caretAlpha(timeMs, blinkMs = DEFAULT_CARET_BLINK_MS) {
934
+ return 0.35 + 0.65 * (0.5 + 0.5 * Math.sin(timeMs / blinkMs));
935
+ }
936
+ __name(caretAlpha, "caretAlpha");
937
+ function drawCaret(g, color, size = DEFAULT_CARET_SIZE) {
938
+ const w = size.width;
939
+ const h = size.height;
940
+ g.poly([0, 0, w, 0, w / 2, h]).fill({ color, alpha: 1 });
941
+ }
942
+ __name(drawCaret, "drawCaret");
943
+
944
+ // src/chrome/textOptions.ts
945
+ function makeTextOptions(fonts, text, size, color, layer, anchor = { x: 0, y: 0 }) {
946
+ const style = { fontSize: size, fill: color };
947
+ if (fonts.bitmapFont) style.fontFamily = fonts.bitmapFont;
948
+ else if (fonts.fontFamily) style.fontFamily = fonts.fontFamily;
949
+ const base = { text, style, layer, anchor };
950
+ if (fonts.bitmapFont) base.bitmap = true;
951
+ else if (fonts.resolution !== void 0) base.resolution = fonts.resolution;
952
+ return base;
953
+ }
954
+ __name(makeTextOptions, "makeTextOptions");
955
+
956
+ // src/chrome/DialogueChrome.ts
957
+ function resolveActiveFrame(styleKey, styles) {
958
+ if (styleKey === CHROME_STYLE_NONE) return { kind: "none" };
959
+ if (styleKey !== void 0 && styles.has(styleKey)) return { kind: "nineSlice", key: styleKey };
960
+ if (styles.has(CHROME_STYLE_DEFAULT)) return { kind: "nineSlice", key: CHROME_STYLE_DEFAULT };
961
+ return { kind: "graphics" };
962
+ }
963
+ __name(resolveActiveFrame, "resolveActiveFrame");
964
+ var DialogueChrome = class {
965
+ constructor(cfg, layout) {
966
+ this.cfg = cfg;
967
+ this.layout = layout;
968
+ this.layout.onChange(() => this.applyGeometry());
969
+ }
970
+ cfg;
971
+ layout;
972
+ static {
973
+ __name(this, "DialogueChrome");
974
+ }
975
+ frame;
976
+ frameGfx;
977
+ /** Separate entity hosting the nine-slice sprites (one per textured style);
978
+ * only spawned when {@link DialogueChromeConfig.frameStyles} has entries. Its
979
+ * Transform tracks the per-line frame origin so the sprites draw at local 0. */
980
+ frameTex;
981
+ frameTexTransform;
982
+ nineSliceHost;
983
+ nineSlices = /* @__PURE__ */ new Map();
984
+ name;
985
+ indicator;
986
+ indicatorTime = 0;
987
+ /** Selected textured-style name from the line's `meta.chrome`, or undefined
988
+ * when the line names none. */
989
+ styleKey;
990
+ warn;
991
+ warnedKeys = /* @__PURE__ */ new Set();
992
+ /** Master gate (from {@link setVisible}); the Session drives it. Hidden at
993
+ * mount until a line shows. The name/caret also need their own content
994
+ * sub-state — each renders only when shown AND its content is present. */
995
+ visible = false;
996
+ nameShown = false;
997
+ caretShown = false;
998
+ /** Route the unknown-`meta.chrome` warning to the engine Logger. */
999
+ setDiagnostics(warn) {
1000
+ this.warn = warn;
1001
+ }
1002
+ mount(scene) {
1003
+ const cfg = this.cfg;
1004
+ const renderer = scene.context.tryResolve(RendererKey);
1005
+ if (renderer) this.layout.setViewport(renderer.virtualSize.width, renderer.virtualSize.height);
1006
+ const frame = scene.spawn("dlg-frame");
1007
+ frame.add(new Transform3()).setPosition(0, 0);
1008
+ this.frameGfx = frame.add(new GraphicsComponent({ layer: cfg.layerFrame }));
1009
+ this.frameGfx.graphics.visible = false;
1010
+ this.frame = frame;
1011
+ const styles = cfg.frameStyles;
1012
+ if (styles && Object.keys(styles).length > 0) {
1013
+ const texEntity = scene.spawn("dlg-frame-tex");
1014
+ this.frameTexTransform = texEntity.add(new Transform3());
1015
+ const host = texEntity.add(new GraphicsComponent({ layer: cfg.layerFrame }));
1016
+ for (const [key, spec] of Object.entries(styles)) {
1017
+ const sprite = createNineSlice({
1018
+ texture: spec.texture,
1019
+ leftWidth: spec.insets.left,
1020
+ topHeight: spec.insets.top,
1021
+ rightWidth: spec.insets.right,
1022
+ bottomHeight: spec.insets.bottom,
1023
+ width: this.layout.frameRect().width,
1024
+ height: this.layout.frameRect().height
1025
+ });
1026
+ sprite.visible = false;
1027
+ host.graphics.addChild(sprite);
1028
+ this.nineSlices.set(key, sprite);
1029
+ }
1030
+ this.frameTex = texEntity;
1031
+ this.nineSliceHost = host;
1032
+ }
1033
+ const nameEntity = scene.spawn("dlg-name");
1034
+ const nameTransform = nameEntity.add(new Transform3());
1035
+ const nameComp = nameEntity.add(
1036
+ new TextComponent(makeTextOptions(cfg, "", cfg.nameSize, cfg.nameColor, cfg.layerText))
1037
+ );
1038
+ this.name = { entity: nameEntity, transform: nameTransform, comp: nameComp };
1039
+ const caretSize = cfg.caret?.size ?? DEFAULT_CARET_SIZE;
1040
+ const ind = scene.spawn("dlg-indicator");
1041
+ const indTransform = ind.add(new Transform3());
1042
+ const indGfx = ind.add(new GraphicsComponent({ layer: cfg.layerFrame }));
1043
+ indGfx.draw((g) => drawCaret(g, cfg.indicatorColor, caretSize));
1044
+ indGfx.graphics.visible = false;
1045
+ this.indicator = { entity: ind, transform: indTransform, gfx: indGfx };
1046
+ this.applyGeometry();
1047
+ }
1048
+ setNameplate(name, color) {
1049
+ if (!this.name) return;
1050
+ this.nameShown = name !== void 0;
1051
+ if (name !== void 0) {
1052
+ this.name.comp.text.style.fill = color ?? this.cfg.nameColor;
1053
+ this.name.comp.setText(name);
1054
+ }
1055
+ this.apply();
1056
+ }
1057
+ setContinueVisible(visible) {
1058
+ this.caretShown = visible;
1059
+ this.indicatorTime = 0;
1060
+ this.apply();
1061
+ }
1062
+ /** Place this line's frame at its `meta.position` and pick its `meta.chrome`
1063
+ * style. Box only; the bubble ignores both. `undefined` (no line) resets to
1064
+ * the default look at the resting position. */
1065
+ present(line) {
1066
+ const metaChrome = line?.meta?.["chrome"];
1067
+ this.styleKey = typeof metaChrome === "string" ? metaChrome : void 0;
1068
+ if (this.styleKey !== void 0 && this.styleKey !== CHROME_STYLE_NONE && this.nineSlices.get(this.styleKey) === void 0 && !this.warnedKeys.has(this.styleKey)) {
1069
+ this.warnedKeys.add(this.styleKey);
1070
+ this.warn?.(`unknown meta.chrome style "${this.styleKey}" \u2014 using the default frame`);
1071
+ }
1072
+ this.layout.layoutLine(line);
1073
+ this.applyGeometry();
1074
+ this.apply();
1075
+ }
1076
+ /** Show or hide the whole box. State-preserving — the name/caret content
1077
+ * sub-state survives, so showing again restores exactly what was up. */
1078
+ setVisible(visible) {
1079
+ this.visible = visible;
1080
+ this.apply();
1081
+ }
1082
+ /** Redraw the frame + reposition the nameplate and caret from the owner's
1083
+ * current geometry (per line, and when a choice grows the frame). */
1084
+ applyGeometry() {
1085
+ const r = this.layout.frameRect();
1086
+ this.frameGfx?.draw((g) => {
1087
+ g.clear();
1088
+ g.roundRect(r.x, r.y, r.width, r.height, this.cfg.cornerRadius).fill({ color: this.cfg.frameColor, alpha: this.cfg.frameAlpha }).stroke({ color: this.cfg.borderColor, alpha: 1, width: 2 });
1089
+ });
1090
+ if (this.frameTexTransform) this.frameTexTransform.setPosition(r.x, r.y);
1091
+ for (const sprite of this.nineSlices.values()) {
1092
+ sprite.width = r.width;
1093
+ sprite.height = r.height;
1094
+ }
1095
+ if (this.name) {
1096
+ const p = this.layout.nameplatePos();
1097
+ this.name.transform.setPosition(p.x, p.y);
1098
+ }
1099
+ if (this.indicator) {
1100
+ const p = this.layout.caretPos(this.cfg.caret?.size ?? DEFAULT_CARET_SIZE);
1101
+ this.indicator.transform.setPosition(p.x, p.y);
1102
+ }
1103
+ }
1104
+ /** Render each piece = master-visible AND its own content present. */
1105
+ apply() {
1106
+ const active = resolveActiveFrame(this.styleKey, this.nineSlices);
1107
+ if (this.frameGfx) {
1108
+ this.frameGfx.graphics.visible = this.visible && active.kind === "graphics";
1109
+ }
1110
+ if (this.nineSliceHost) {
1111
+ this.nineSliceHost.graphics.visible = this.visible && active.kind === "nineSlice";
1112
+ for (const [key, sprite] of this.nineSlices) {
1113
+ sprite.visible = active.kind === "nineSlice" && active.key === key;
1114
+ }
1115
+ }
1116
+ if (this.name) this.name.comp.text.visible = this.visible && this.nameShown;
1117
+ if (this.indicator) {
1118
+ this.indicator.gfx.graphics.visible = this.visible && this.caretShown;
1119
+ }
1120
+ }
1121
+ update(dt) {
1122
+ const gfx = this.indicator?.gfx.graphics;
1123
+ if (gfx?.visible) {
1124
+ this.indicatorTime += dt;
1125
+ gfx.alpha = caretAlpha(this.indicatorTime, this.cfg.caret?.blinkMs);
1126
+ }
1127
+ }
1128
+ dispose() {
1129
+ this.frame?.destroy();
1130
+ this.frameTex?.destroy();
1131
+ this.name?.entity.destroy();
1132
+ this.indicator?.entity.destroy();
1133
+ this.nineSlices.clear();
1134
+ this.frame = void 0;
1135
+ this.frameTex = void 0;
1136
+ this.frameTexTransform = void 0;
1137
+ this.frameGfx = void 0;
1138
+ this.nineSliceHost = void 0;
1139
+ this.name = void 0;
1140
+ this.indicator = void 0;
1141
+ }
1142
+ };
1143
+
1144
+ // src/chrome/ChoiceListPresenter.ts
1145
+ import { Transform as Transform4 } from "@yagejs/core";
1146
+ import { GraphicsComponent as GraphicsComponent2, TextComponent as TextComponent2 } from "@yagejs/renderer";
1147
+
1148
+ // src/chrome/choiceRow.ts
1149
+ var DISABLED_CHOICE_ALPHA = 0.4;
1150
+ function choiceRowLabel(choice) {
1151
+ return choice.disabled && choice.disabledReason ? `${choice.label} (${choice.disabledReason})` : choice.label;
1152
+ }
1153
+ __name(choiceRowLabel, "choiceRowLabel");
1154
+ function firstEnabledIndex(rows) {
1155
+ const i = rows.findIndex((r) => !r.disabled);
1156
+ return i < 0 ? 0 : i;
1157
+ }
1158
+ __name(firstEnabledIndex, "firstEnabledIndex");
1159
+ function clampSelection(position, count) {
1160
+ return Math.min(Math.max(position, 0), count - 1);
1161
+ }
1162
+ __name(clampSelection, "clampSelection");
1163
+ function applyChoiceTint(rows, selected, color, selectedColor) {
1164
+ rows.forEach((row, i) => {
1165
+ const active = i === selected && !row.disabled;
1166
+ row.comp.text.style.fill = active ? selectedColor : color;
1167
+ row.comp.text.alpha = row.disabled ? DISABLED_CHOICE_ALPHA : 1;
1168
+ });
1169
+ }
1170
+ __name(applyChoiceTint, "applyChoiceTint");
1171
+
1172
+ // src/chrome/ChoiceListPresenter.ts
1173
+ var DEFAULT_SOFT_MAX_CHOICES = 8;
1174
+ var ROW_TEXT_INDENT = 6;
1175
+ var ChoiceListPresenter = class {
1176
+ constructor(cfg, layout) {
1177
+ this.cfg = cfg;
1178
+ this.layout = layout;
1179
+ }
1180
+ cfg;
1181
+ layout;
1182
+ static {
1183
+ __name(this, "ChoiceListPresenter");
1184
+ }
1185
+ scene;
1186
+ highlightBar;
1187
+ rows = [];
1188
+ selected = -1;
1189
+ warn;
1190
+ /** Master visibility gate — hides the list WITHOUT clearing it, so a
1191
+ * hide/show round-trip keeps the rows + selection. */
1192
+ hidden = false;
1193
+ onChoiceChosen;
1194
+ /** Route the soft-cap advisory to the engine Logger. */
1195
+ setDiagnostics(warn) {
1196
+ this.warn = warn;
1197
+ }
1198
+ mount(scene) {
1199
+ this.scene = scene;
1200
+ const hl = scene.spawn("dlg-highlight");
1201
+ hl.add(new Transform4()).setPosition(0, 0);
1202
+ const hlGfx = hl.add(new GraphicsComponent2({ layer: this.cfg.layerFrame }));
1203
+ this.highlightBar = { entity: hl, gfx: hlGfx };
1204
+ }
1205
+ // Screen-space box list — ignores the choice context (the box frame is its
1206
+ // backing; routing is the composite's job).
1207
+ present(choices) {
1208
+ this.clear();
1209
+ if (!this.scene) return;
1210
+ const cfg = this.cfg;
1211
+ const gap = cfg.choiceGap ?? DEFAULT_CHOICE_GAP;
1212
+ const softMax = cfg.softMaxChoices ?? DEFAULT_SOFT_MAX_CHOICES;
1213
+ if (choices.length > softMax) {
1214
+ this.warn?.(
1215
+ `choice list: ${choices.length} options exceeds the soft max of ${softMax} \u2014 the list grows to fit, but a shorter menu (or a sub-menu) reads better`
1216
+ );
1217
+ }
1218
+ const innerWidth = this.layout.contentWidth();
1219
+ const wrapWidth = innerWidth - ROW_TEXT_INDENT - 2;
1220
+ const built = choices.map((choice) => {
1221
+ const entity = this.scene.spawn("dlg-choice");
1222
+ entity.add(new Transform4());
1223
+ const comp = entity.add(new TextComponent2(this.textOptions(choiceRowLabel(choice), wrapWidth)));
1224
+ comp.text.visible = true;
1225
+ const slotHeight = Math.ceil(comp.text.height) + gap;
1226
+ return { entity, comp, disabled: choice.disabled ?? false, slotHeight };
1227
+ });
1228
+ const rects = this.layout.layoutChoicePanel(built.map((b) => b.slotHeight));
1229
+ this.rows = built.map((b, i) => {
1230
+ const rect = rects[i] ?? { x: 0, y: 0, width: innerWidth, height: b.slotHeight };
1231
+ b.entity.get(Transform4).setPosition(rect.x + ROW_TEXT_INDENT, rect.y);
1232
+ return { entity: b.entity, comp: b.comp, disabled: b.disabled, rect };
1233
+ });
1234
+ this.highlightAt(firstEnabledIndex(this.rows));
1235
+ this.applyHidden();
1236
+ }
1237
+ highlight(position) {
1238
+ this.highlightAt(position);
1239
+ }
1240
+ /** Show or hide the list without clearing it — state-preserving. */
1241
+ setVisible(visible) {
1242
+ this.hidden = !visible;
1243
+ this.applyHidden();
1244
+ }
1245
+ /** Render = rows present AND not hidden. Disabled rows still show (greyed). */
1246
+ applyHidden() {
1247
+ for (const row of this.rows) row.comp.text.visible = !this.hidden;
1248
+ if (this.highlightBar) {
1249
+ const onEnabled = this.selected >= 0 && !this.rows[this.selected]?.disabled;
1250
+ this.highlightBar.gfx.graphics.visible = !this.hidden && onEnabled;
1251
+ }
1252
+ }
1253
+ /** {@link PointerChoiceTarget}: which row (if any) a screen point falls in —
1254
+ * from the same grown rects the rows are drawn at. */
1255
+ choiceAtPoint(x, y) {
1256
+ for (let i = 0; i < this.rows.length; i++) {
1257
+ const r = this.rows[i]?.rect;
1258
+ if (r && x >= r.x && x <= r.x + r.width && y >= r.y && y <= r.y + r.height) return i;
1259
+ }
1260
+ return void 0;
1261
+ }
1262
+ clear() {
1263
+ for (const row of this.rows) row.entity.destroy();
1264
+ this.rows = [];
1265
+ this.selected = -1;
1266
+ this.highlightBar?.gfx.draw((g) => g.clear());
1267
+ }
1268
+ dispose() {
1269
+ this.clear();
1270
+ this.highlightBar?.entity.destroy();
1271
+ this.highlightBar = void 0;
1272
+ }
1273
+ highlightAt(position) {
1274
+ if (this.rows.length === 0) return;
1275
+ this.selected = clampSelection(position, this.rows.length);
1276
+ applyChoiceTint(this.rows, this.selected, this.cfg.choiceColor, this.cfg.choiceSelectedColor);
1277
+ this.drawHighlight();
1278
+ }
1279
+ drawHighlight() {
1280
+ if (!this.highlightBar || this.selected < 0) return;
1281
+ const row = this.rows[this.selected];
1282
+ if (!row || row.disabled) {
1283
+ this.highlightBar.gfx.draw((g) => g.clear());
1284
+ return;
1285
+ }
1286
+ const r = row.rect;
1287
+ this.highlightBar.gfx.draw((g) => {
1288
+ g.clear();
1289
+ g.roundRect(r.x, r.y, r.width, r.height - 1, 3).fill({
1290
+ color: this.cfg.highlightColor,
1291
+ alpha: 0.3
1292
+ });
1293
+ });
1294
+ }
1295
+ textOptions(text, wrapWidth) {
1296
+ const opts = makeTextOptions(
1297
+ this.cfg,
1298
+ text,
1299
+ this.cfg.choiceSize,
1300
+ this.cfg.choiceColor,
1301
+ this.cfg.layerText
1302
+ );
1303
+ opts.style.wordWrap = true;
1304
+ opts.style.wordWrapWidth = wrapWidth;
1305
+ return opts;
1306
+ }
1307
+ };
1308
+
1309
+ // src/chrome/BubbleChrome.ts
1310
+ import { Transform as Transform5 } from "@yagejs/core";
1311
+ import {
1312
+ createNineSlice as createNineSlice2,
1313
+ GraphicsComponent as GraphicsComponent3,
1314
+ TextComponent as TextComponent3
1315
+ } from "@yagejs/renderer";
1316
+ var BubbleChrome = class {
1317
+ constructor(cfg, layout) {
1318
+ this.cfg = cfg;
1319
+ this.layout = layout;
1320
+ this.currentWidth = 0;
1321
+ this.currentHeight = 0;
1322
+ }
1323
+ cfg;
1324
+ layout;
1325
+ static {
1326
+ __name(this, "BubbleChrome");
1327
+ }
1328
+ scene;
1329
+ root;
1330
+ gfx;
1331
+ transform;
1332
+ /** Nine-slice body sprite (child of {@link gfx}) when textured; resized per
1333
+ * line. The tail stays a drawn triangle on {@link gfx}. */
1334
+ bubbleSlice;
1335
+ name;
1336
+ nameTransform;
1337
+ caret;
1338
+ caretTransform;
1339
+ caretTime = 0;
1340
+ /** Current (content-sized) bubble size; recomputed per line from the layout. */
1341
+ currentWidth;
1342
+ currentHeight;
1343
+ // Master visibility + content sub-state; rendered = visible && hasLine.
1344
+ visible = false;
1345
+ // master (from setVisible)
1346
+ hasLine = false;
1347
+ // a line is up (from present)
1348
+ nameShown = false;
1349
+ // the speaker has a name to show
1350
+ caretShown = false;
1351
+ // continue caret requested
1352
+ /** Speaker id of the line on screen — re-resolved each frame by `follow()` so
1353
+ * the bubble tracks a live actor and falls back when one is missing. */
1354
+ speakerId;
1355
+ /** Route the missing-actor warning to the engine Logger (the layout owns the
1356
+ * shared anchor resolver). */
1357
+ setDiagnostics(warn) {
1358
+ this.layout.setDiagnostics(warn);
1359
+ }
1360
+ mount(scene) {
1361
+ this.scene = scene;
1362
+ const c = this.cfg;
1363
+ const root = scene.spawn("dlg-bubble");
1364
+ this.transform = root.add(new Transform5());
1365
+ this.gfx = root.add(new GraphicsComponent3({ layer: c.layer }));
1366
+ if (c.frame) {
1367
+ const slice = createNineSlice2({
1368
+ texture: c.frame.texture,
1369
+ leftWidth: c.frame.insets.left,
1370
+ topHeight: c.frame.insets.top,
1371
+ rightWidth: c.frame.insets.right,
1372
+ bottomHeight: c.frame.insets.bottom,
1373
+ width: this.currentWidth,
1374
+ height: this.currentHeight
1375
+ });
1376
+ this.gfx.graphics.addChild(slice);
1377
+ this.bubbleSlice = slice;
1378
+ }
1379
+ this.drawBubble();
1380
+ this.gfx.graphics.visible = false;
1381
+ const nameEntity = scene.spawn("dlg-bubble-name");
1382
+ this.nameTransform = nameEntity.add(new Transform5());
1383
+ this.name = nameEntity.add(
1384
+ new TextComponent3(makeTextOptions(c, "", c.nameSize, c.nameColor, c.layer))
1385
+ );
1386
+ this.name.text.visible = false;
1387
+ const caretEntity = scene.spawn("dlg-bubble-caret");
1388
+ this.caretTransform = caretEntity.add(new Transform5());
1389
+ this.caret = caretEntity.add(new GraphicsComponent3({ layer: c.layer }));
1390
+ this.caret.draw((g) => drawCaret(g, c.indicatorColor, c.caret?.size));
1391
+ this.caret.graphics.visible = false;
1392
+ this.root = root;
1393
+ }
1394
+ /** Re-anchor to the line's speaker, grow to fit the text, and reveal. The
1395
+ * bubble stays visible even when the speaker has no live actor — the layout
1396
+ * anchors it at the last-known / fallback position instead of vanishing. */
1397
+ present(line) {
1398
+ this.hasLine = line !== void 0;
1399
+ this.speakerId = line?.speaker?.id;
1400
+ if (line) {
1401
+ const size = this.layout.sizeFor(line);
1402
+ this.currentWidth = size.width;
1403
+ this.currentHeight = size.height;
1404
+ this.drawBubble();
1405
+ const label = line.speaker?.name;
1406
+ this.nameShown = label !== void 0 && label.length > 0;
1407
+ if (label && this.name) {
1408
+ this.name.text.style.fill = line.speaker?.color ?? this.cfg.nameColor;
1409
+ this.name.setText(label);
1410
+ }
1411
+ } else {
1412
+ this.nameShown = false;
1413
+ }
1414
+ this.apply();
1415
+ this.follow();
1416
+ }
1417
+ setNameplate(name) {
1418
+ if (name === void 0) {
1419
+ this.nameShown = false;
1420
+ this.apply();
1421
+ }
1422
+ }
1423
+ setContinueVisible(visible) {
1424
+ this.caretShown = visible;
1425
+ this.caretTime = 0;
1426
+ this.apply();
1427
+ }
1428
+ /** Show or hide the whole bubble — state-preserving: the line, name, and
1429
+ * caret content survive a hide, so showing again restores them in place
1430
+ * (used by a composite chrome to hide the bubble while a box line plays). */
1431
+ setVisible(visible) {
1432
+ this.visible = visible;
1433
+ this.apply();
1434
+ }
1435
+ /** Render each piece = master-visible AND a line is up AND its content present. */
1436
+ apply() {
1437
+ if (this.gfx) this.gfx.graphics.visible = this.visible && this.hasLine;
1438
+ if (this.name) {
1439
+ this.name.text.visible = this.visible && this.hasLine && this.nameShown;
1440
+ }
1441
+ if (this.caret) {
1442
+ this.caret.graphics.visible = this.visible && this.hasLine && this.caretShown;
1443
+ }
1444
+ }
1445
+ update(dt) {
1446
+ this.follow();
1447
+ const gfx = this.caret?.graphics;
1448
+ if (gfx?.visible) {
1449
+ this.caretTime += dt;
1450
+ gfx.alpha = caretAlpha(this.caretTime, this.cfg.caret?.blinkMs);
1451
+ }
1452
+ }
1453
+ dispose() {
1454
+ this.root?.destroy();
1455
+ this.name?.entity.destroy();
1456
+ this.caret?.entity.destroy();
1457
+ this.root = void 0;
1458
+ this.gfx = void 0;
1459
+ this.transform = void 0;
1460
+ this.bubbleSlice = void 0;
1461
+ this.name = void 0;
1462
+ this.nameTransform = void 0;
1463
+ this.caret = void 0;
1464
+ this.caretTransform = void 0;
1465
+ }
1466
+ /** Move the bubble + name + caret to sit above the speaker — its live actor,
1467
+ * or the last-known / fallback anchor when the actor is missing. */
1468
+ follow() {
1469
+ if (!this.scene || !this.hasLine) return;
1470
+ const a = this.layout.anchorFor(this.scene, this.speakerId);
1471
+ const c = this.cfg;
1472
+ const padding = this.layout.padding;
1473
+ const offsetY = this.layout.offsetY;
1474
+ const w = this.currentWidth;
1475
+ const h = this.currentHeight;
1476
+ const caretSize = c.caret?.size ?? DEFAULT_CARET_SIZE;
1477
+ this.transform?.setPosition(a.x, a.y);
1478
+ this.nameTransform?.setPosition(a.x - w / 2 + padding, a.y - (offsetY + h) - c.nameSize - 1);
1479
+ this.caretTransform?.setPosition(
1480
+ a.x + w / 2 - padding - caretSize.width,
1481
+ a.y - offsetY - padding - caretSize.height + 3
1482
+ );
1483
+ }
1484
+ drawBubble() {
1485
+ const c = this.cfg;
1486
+ const offsetY = this.layout.offsetY;
1487
+ const w = this.currentWidth;
1488
+ const h = this.currentHeight;
1489
+ const L = -w / 2;
1490
+ const R = w / 2;
1491
+ const T = -(offsetY + h);
1492
+ const B = -offsetY;
1493
+ const half = c.tail;
1494
+ const lean = c.tailLean ?? DEFAULT_TAIL_LEAN;
1495
+ const tipX = lean.x;
1496
+ const tipY = lean.y;
1497
+ this.gfx?.graphics.clear();
1498
+ if (this.bubbleSlice) {
1499
+ this.bubbleSlice.position.set(L, T);
1500
+ this.bubbleSlice.width = w;
1501
+ this.bubbleSlice.height = h;
1502
+ this.gfx?.draw((g) => {
1503
+ g.poly([-half, B, half, B, tipX, tipY]).fill({ color: c.frameColor, alpha: c.frameAlpha });
1504
+ });
1505
+ return;
1506
+ }
1507
+ const r = Math.max(0, Math.min(c.cornerRadius, w / 2 - 1, h / 2 - 1));
1508
+ this.gfx?.draw((g) => {
1509
+ g.moveTo(L + r, T).lineTo(R - r, T).arcTo(R, T, R, T + r, r).lineTo(R, B - r).arcTo(R, B, R - r, B, r).lineTo(half, B).lineTo(tipX, tipY).lineTo(-half, B).lineTo(L + r, B).arcTo(L, B, L, B - r, r).lineTo(L, T + r).arcTo(L, T, L + r, T, r).closePath().fill({ color: c.frameColor, alpha: c.frameAlpha }).stroke({ color: c.borderColor, alpha: 1, width: 2 });
1510
+ });
1511
+ }
1512
+ };
1513
+
1514
+ // src/chrome/BubbleChoicePresenter.ts
1515
+ import { Transform as Transform6 } from "@yagejs/core";
1516
+ import {
1517
+ GraphicsComponent as GraphicsComponent4,
1518
+ TextComponent as TextComponent4
1519
+ } from "@yagejs/renderer";
1520
+ var BubbleChoicePresenter = class {
1521
+ constructor(cfg, layout) {
1522
+ this.cfg = cfg;
1523
+ this.layout = layout;
1524
+ }
1525
+ cfg;
1526
+ layout;
1527
+ static {
1528
+ __name(this, "BubbleChoicePresenter");
1529
+ }
1530
+ pointerSpace = "world";
1531
+ scene;
1532
+ bg;
1533
+ highlightBar;
1534
+ prompt;
1535
+ rows = [];
1536
+ selected = -1;
1537
+ /** Master visibility gate — state-preserving hide/show. */
1538
+ hidden = false;
1539
+ onChoiceChosen;
1540
+ /** Route the missing-actor warning to the engine Logger (the layout owns the
1541
+ * shared anchor resolver). */
1542
+ setDiagnostics(warn) {
1543
+ this.layout.setDiagnostics(warn);
1544
+ }
1545
+ /** This panel is self-contained (own bg + prompt header), so it owns the
1546
+ * prompt — the Session hides the chrome/body prompt for these choices. */
1547
+ ownsPrompt() {
1548
+ return true;
1549
+ }
1550
+ mount(scene) {
1551
+ this.scene = scene;
1552
+ const bg = scene.spawn("dlg-bchoice-bg");
1553
+ bg.add(new Transform6()).setPosition(0, 0);
1554
+ this.bg = { entity: bg, gfx: bg.add(new GraphicsComponent4({ layer: this.cfg.layer })) };
1555
+ const hl = scene.spawn("dlg-bchoice-hl");
1556
+ hl.add(new Transform6()).setPosition(0, 0);
1557
+ this.highlightBar = { entity: hl, gfx: hl.add(new GraphicsComponent4({ layer: this.cfg.layer })) };
1558
+ }
1559
+ present(choices, context) {
1560
+ this.clear();
1561
+ if (!this.scene) return;
1562
+ const c = this.cfg;
1563
+ const a = this.layout.anchorFor(this.scene, context?.speaker?.id);
1564
+ const innerW = c.width - 2 * c.padding;
1565
+ const inset = this.layout.portraitInset();
1566
+ const reserve = inset?.width ?? 0;
1567
+ const panelWidth = c.width + reserve;
1568
+ const promptStr = context?.prompt && context.prompt.length > 0 ? context.prompt.runs.map((r) => r.text).join("") : "";
1569
+ let promptH = 0;
1570
+ if (promptStr) {
1571
+ const e = this.scene.spawn("dlg-bchoice-prompt");
1572
+ e.add(new Transform6());
1573
+ const comp = e.add(new TextComponent4(this.textOptions(promptStr, c.textColor, innerW)));
1574
+ comp.text.visible = true;
1575
+ promptH = Math.ceil(comp.text.height);
1576
+ this.prompt = { entity: e, comp };
1577
+ }
1578
+ const n = choices.length;
1579
+ const gap = c.choiceGap ?? DEFAULT_CHOICE_GAP;
1580
+ const lineH = c.choiceSize + gap;
1581
+ const headH = promptH > 0 ? promptH + gap : 0;
1582
+ const contentH = c.padding + headH + n * lineH + c.padding;
1583
+ const panelH = Math.max(contentH, (inset?.height ?? 0) + 2 * c.padding);
1584
+ const left = a.x - panelWidth / 2;
1585
+ const bottom = a.y - c.offsetY;
1586
+ const top = bottom - panelH;
1587
+ const contentX = left + c.padding + (inset?.side === "left" ? reserve : 0);
1588
+ this.drawPanel(left, top, panelWidth, panelH, a.x, bottom);
1589
+ this.prompt?.entity.get(Transform6).setPosition(contentX, top + c.padding);
1590
+ const optionsTop = top + c.padding + headH;
1591
+ choices.forEach((choice, i) => {
1592
+ const rowY = optionsTop + i * lineH;
1593
+ const entity = this.scene.spawn("dlg-bchoice");
1594
+ entity.add(new Transform6()).setPosition(contentX + 4, rowY);
1595
+ const comp = entity.add(new TextComponent4(this.textOptions(choiceRowLabel(choice), c.choiceColor)));
1596
+ comp.text.visible = true;
1597
+ this.rows.push({
1598
+ entity,
1599
+ comp,
1600
+ x0: contentX,
1601
+ x1: contentX + innerW,
1602
+ y0: rowY,
1603
+ y1: rowY + lineH,
1604
+ disabled: choice.disabled ?? false
1605
+ });
1606
+ });
1607
+ this.highlight(firstEnabledIndex(this.rows));
1608
+ this.applyHidden();
1609
+ this.layout.setChoicePanelSize({ width: panelWidth, height: panelH });
1610
+ }
1611
+ /** Show or hide the whole panel without clearing it — state-preserving. */
1612
+ setVisible(visible) {
1613
+ this.hidden = !visible;
1614
+ this.applyHidden();
1615
+ }
1616
+ /** Render every piece (bg, prompt, rows, highlight) gated by the master.
1617
+ * Disabled rows still show (greyed); only the highlight bar is suppressed. */
1618
+ applyHidden() {
1619
+ const shown = !this.hidden;
1620
+ if (this.bg) this.bg.gfx.graphics.visible = shown && this.rows.length > 0;
1621
+ if (this.highlightBar) {
1622
+ const onEnabled = this.selected >= 0 && !this.rows[this.selected]?.disabled;
1623
+ this.highlightBar.gfx.graphics.visible = shown && onEnabled;
1624
+ }
1625
+ if (this.prompt) this.prompt.comp.text.visible = shown;
1626
+ for (const row of this.rows) row.comp.text.visible = shown;
1627
+ }
1628
+ highlight(position) {
1629
+ if (this.rows.length === 0) return;
1630
+ this.selected = clampSelection(position, this.rows.length);
1631
+ applyChoiceTint(this.rows, this.selected, this.cfg.choiceColor, this.cfg.choiceSelectedColor);
1632
+ this.drawHighlight();
1633
+ }
1634
+ /** {@link ChoicePresenter}: which option a *world* point falls in. */
1635
+ choiceAtPoint(x, y) {
1636
+ for (let i = 0; i < this.rows.length; i++) {
1637
+ const r = this.rows[i];
1638
+ if (x >= r.x0 && x <= r.x1 && y >= r.y0 && y <= r.y1) return i;
1639
+ }
1640
+ return void 0;
1641
+ }
1642
+ clear() {
1643
+ for (const r of this.rows) r.entity.destroy();
1644
+ this.rows = [];
1645
+ this.prompt?.entity.destroy();
1646
+ this.prompt = void 0;
1647
+ this.selected = -1;
1648
+ this.bg?.gfx.draw((g) => g.clear());
1649
+ this.highlightBar?.gfx.draw((g) => g.clear());
1650
+ }
1651
+ dispose() {
1652
+ this.clear();
1653
+ this.bg?.entity.destroy();
1654
+ this.highlightBar?.entity.destroy();
1655
+ this.bg = void 0;
1656
+ this.highlightBar = void 0;
1657
+ }
1658
+ drawPanel(left, top, width, h, tailX, bottom) {
1659
+ const c = this.cfg;
1660
+ this.bg?.gfx.draw((g) => {
1661
+ g.clear();
1662
+ g.roundRect(left, top, width, h, c.cornerRadius).fill({ color: c.frameColor, alpha: c.frameAlpha }).stroke({ color: c.borderColor, alpha: 1, width: 2 });
1663
+ g.poly([tailX - c.tail, bottom, tailX + c.tail, bottom, tailX, bottom + c.tail]).fill({
1664
+ color: c.frameColor,
1665
+ alpha: c.frameAlpha
1666
+ });
1667
+ });
1668
+ }
1669
+ drawHighlight() {
1670
+ const row = this.rows[this.selected];
1671
+ if (!this.highlightBar || !row) return;
1672
+ if (row.disabled) {
1673
+ this.highlightBar.gfx.draw((g) => g.clear());
1674
+ return;
1675
+ }
1676
+ this.highlightBar.gfx.draw((g) => {
1677
+ g.clear();
1678
+ g.roundRect(row.x0, row.y0, row.x1 - row.x0, row.y1 - row.y0 - 1, 3).fill({
1679
+ color: this.cfg.highlightColor,
1680
+ alpha: 0.3
1681
+ });
1682
+ });
1683
+ }
1684
+ textOptions(text, color, wrapWidth) {
1685
+ const opts = makeTextOptions(this.cfg, text, this.cfg.choiceSize, color, this.cfg.layer);
1686
+ if (wrapWidth != null) {
1687
+ opts.style.wordWrap = true;
1688
+ opts.style.wordWrapWidth = wrapWidth;
1689
+ }
1690
+ return opts;
1691
+ }
1692
+ };
1693
+
1694
+ // src/chrome/RadialChoicePresenter.ts
1695
+ import { Transform as Transform7 } from "@yagejs/core";
1696
+ import { GraphicsComponent as GraphicsComponent5, TextComponent as TextComponent5 } from "@yagejs/renderer";
1697
+ var RadialChoicePresenter = class {
1698
+ constructor(cfg) {
1699
+ this.cfg = cfg;
1700
+ }
1701
+ cfg;
1702
+ static {
1703
+ __name(this, "RadialChoicePresenter");
1704
+ }
1705
+ scene;
1706
+ hub;
1707
+ spokes = [];
1708
+ selected = -1;
1709
+ /** Master visibility gate — state-preserving hide/show. */
1710
+ hidden = false;
1711
+ onChoiceChosen;
1712
+ mount(scene) {
1713
+ this.scene = scene;
1714
+ const hub = scene.spawn("dlg-radial-hub");
1715
+ hub.add(new Transform7()).setPosition(0, 0);
1716
+ this.hub = { entity: hub, gfx: hub.add(new GraphicsComponent5({ layer: this.cfg.layerFrame })) };
1717
+ }
1718
+ present(choices) {
1719
+ this.clear();
1720
+ if (!this.scene) return;
1721
+ const { center: c, radius } = this.cfg;
1722
+ const n = choices.length;
1723
+ choices.forEach((choice, i) => {
1724
+ const angle = -Math.PI / 2 + i / n * Math.PI * 2;
1725
+ const x = c.x + Math.cos(angle) * radius;
1726
+ const y = c.y + Math.sin(angle) * radius;
1727
+ const entity = this.scene.spawn("dlg-radial");
1728
+ entity.add(new Transform7()).setPosition(x, y);
1729
+ const comp = entity.add(
1730
+ new TextComponent5(
1731
+ makeTextOptions(
1732
+ this.cfg,
1733
+ choice.label,
1734
+ this.cfg.choiceSize,
1735
+ this.cfg.choiceColor,
1736
+ this.cfg.layerText,
1737
+ { x: 0.5, y: 0.5 }
1738
+ )
1739
+ )
1740
+ );
1741
+ comp.text.visible = true;
1742
+ this.spokes.push({ entity, comp, x, y, disabled: choice.disabled ?? false });
1743
+ });
1744
+ this.highlight(firstEnabledIndex(this.spokes));
1745
+ this.applyHidden();
1746
+ }
1747
+ highlight(position) {
1748
+ if (this.spokes.length === 0) return;
1749
+ this.selected = clampSelection(position, this.spokes.length);
1750
+ applyChoiceTint(this.spokes, this.selected, this.cfg.choiceColor, this.cfg.choiceSelectedColor);
1751
+ this.spokes.forEach((s, i) => {
1752
+ const scale = i === this.selected && !s.disabled ? 1.15 : 1;
1753
+ s.entity.get(Transform7).setScale(scale, scale);
1754
+ });
1755
+ this.drawHub();
1756
+ }
1757
+ /** Show or hide the wheel without clearing it — state-preserving. */
1758
+ setVisible(visible) {
1759
+ this.hidden = !visible;
1760
+ this.applyHidden();
1761
+ }
1762
+ applyHidden() {
1763
+ for (const s of this.spokes) s.comp.text.visible = !this.hidden;
1764
+ if (this.hub) this.hub.gfx.graphics.visible = !this.hidden && this.spokes.length > 0;
1765
+ }
1766
+ /** {@link ChoicePresenter}: nearest spoke within a small radius. */
1767
+ choiceAtPoint(x, y) {
1768
+ let best;
1769
+ let bestD = 22;
1770
+ this.spokes.forEach((s, i) => {
1771
+ const d = Math.hypot(s.x - x, s.y - y);
1772
+ if (d < bestD) {
1773
+ bestD = d;
1774
+ best = i;
1775
+ }
1776
+ });
1777
+ return best;
1778
+ }
1779
+ clear() {
1780
+ for (const s of this.spokes) s.entity.destroy();
1781
+ this.spokes = [];
1782
+ this.selected = -1;
1783
+ this.hub?.gfx.draw((g) => g.clear());
1784
+ }
1785
+ dispose() {
1786
+ this.clear();
1787
+ this.hub?.entity.destroy();
1788
+ this.hub = void 0;
1789
+ }
1790
+ drawHub() {
1791
+ if (!this.hub) return;
1792
+ const { center: c } = this.cfg;
1793
+ const sel = this.spokes[this.selected];
1794
+ const active = sel && !sel.disabled ? sel : void 0;
1795
+ this.hub.gfx.draw((g) => {
1796
+ g.clear();
1797
+ if (active) {
1798
+ g.moveTo(c.x, c.y).lineTo(active.x, active.y).stroke({ color: this.cfg.choiceSelectedColor, width: 2, alpha: 0.7 });
1799
+ }
1800
+ g.circle(c.x, c.y, 4).fill({ color: this.cfg.hubColor, alpha: 1 });
1801
+ });
1802
+ }
1803
+ };
1804
+
1805
+ // src/composite/route.ts
1806
+ function routeWithActor(line, hasActor) {
1807
+ const speaker = line?.speaker;
1808
+ if (speaker === void 0) return "box";
1809
+ const view = line?.view;
1810
+ if (view === "bubble") return "bubble";
1811
+ if (view === "box") return "box";
1812
+ return hasActor(speaker.id) ? "bubble" : "box";
1813
+ }
1814
+ __name(routeWithActor, "routeWithActor");
1815
+ function makeDefaultRoute() {
1816
+ let hasActor = /* @__PURE__ */ __name(() => false, "hasActor");
1817
+ return {
1818
+ bind(scene) {
1819
+ const registry = actorRegistryFor(scene);
1820
+ hasActor = /* @__PURE__ */ __name((id) => registry.resolve(id) !== void 0, "hasActor");
1821
+ },
1822
+ route: /* @__PURE__ */ __name((line) => routeWithActor(line, hasActor), "route")
1823
+ };
1824
+ }
1825
+ __name(makeDefaultRoute, "makeDefaultRoute");
1826
+ function fixedRoute(route) {
1827
+ return { route, bind() {
1828
+ } };
1829
+ }
1830
+ __name(fixedRoute, "fixedRoute");
1831
+ function choiceAsLine(context) {
1832
+ return {
1833
+ view: context?.view,
1834
+ speaker: context?.speaker,
1835
+ meta: context?.meta,
1836
+ text: context?.prompt ?? EMPTY_PARSED,
1837
+ speed: 1
1838
+ };
1839
+ }
1840
+ __name(choiceAsLine, "choiceAsLine");
1841
+ function lineRoutesToBubble(route, line) {
1842
+ return route(line) === "bubble";
1843
+ }
1844
+ __name(lineRoutesToBubble, "lineRoutesToBubble");
1845
+ function choiceRoutesToBubble(route, context) {
1846
+ return route(choiceAsLine(context)) === "bubble";
1847
+ }
1848
+ __name(choiceRoutesToBubble, "choiceRoutesToBubble");
1849
+
1850
+ // src/composite/CompositeTextPresenter.ts
1851
+ var CompositeTextPresenter = class {
1852
+ constructor(box, bubble, routing = makeDefaultRoute()) {
1853
+ this.box = box;
1854
+ this.bubble = bubble;
1855
+ this.routing = routing;
1856
+ this.box.setRevealListener(() => this.fireReveal(this.box));
1857
+ this.bubble.setRevealListener(() => this.fireReveal(this.bubble));
1858
+ this.box.setBeatListener((beat) => this.fireBeat(this.box, beat));
1859
+ this.bubble.setBeatListener((beat) => this.fireBeat(this.bubble, beat));
1860
+ }
1861
+ box;
1862
+ bubble;
1863
+ routing;
1864
+ static {
1865
+ __name(this, "CompositeTextPresenter");
1866
+ }
1867
+ active;
1868
+ revealListener;
1869
+ beatListener;
1870
+ /** Master visibility gate from the Session's setVisible. */
1871
+ visible = false;
1872
+ /** Register the Session's reveal-completed listener. */
1873
+ setRevealListener(listener) {
1874
+ this.revealListener = listener;
1875
+ }
1876
+ /** Register the Session's reveal-beat listener (ticks + inline markers). */
1877
+ setBeatListener(listener) {
1878
+ this.beatListener = listener;
1879
+ }
1880
+ fireReveal(view) {
1881
+ if (this.active === view) this.revealListener?.();
1882
+ }
1883
+ fireBeat(view, beat) {
1884
+ if (this.active === view) this.beatListener?.(beat);
1885
+ }
1886
+ setDiagnostics(warn) {
1887
+ this.box.setDiagnostics?.(warn);
1888
+ this.bubble.setDiagnostics?.(warn);
1889
+ }
1890
+ mount(scene) {
1891
+ this.routing.bind(scene);
1892
+ this.box.mount(scene);
1893
+ this.bubble.mount(scene);
1894
+ }
1895
+ present(line) {
1896
+ const target = lineRoutesToBubble(this.routing.route, line) ? this.bubble : this.box;
1897
+ const other = target === this.box ? this.bubble : this.box;
1898
+ other.clear();
1899
+ this.active = target;
1900
+ target.present(line);
1901
+ target.setVisible(this.visible);
1902
+ }
1903
+ /** Show/hide the body text — forwarded to both views (the inactive one is
1904
+ * cleared, so its setVisible is a no-op); state-preserving on the active. */
1905
+ setVisible(visible) {
1906
+ this.visible = visible;
1907
+ this.box.setVisible(visible);
1908
+ this.bubble.setVisible(visible);
1909
+ }
1910
+ completeReveal() {
1911
+ this.active?.completeReveal();
1912
+ }
1913
+ isRevealComplete() {
1914
+ return this.active ? this.active.isRevealComplete() : true;
1915
+ }
1916
+ isRevealing() {
1917
+ return this.active ? this.active.isRevealing() : false;
1918
+ }
1919
+ setSpeedMultiplier(multiplier) {
1920
+ this.box.setSpeedMultiplier(multiplier);
1921
+ this.bubble.setSpeedMultiplier(multiplier);
1922
+ }
1923
+ update(dt) {
1924
+ this.box.update(dt);
1925
+ this.bubble.update(dt);
1926
+ }
1927
+ clear() {
1928
+ this.box.clear();
1929
+ this.bubble.clear();
1930
+ this.active = void 0;
1931
+ }
1932
+ dispose() {
1933
+ this.box.dispose();
1934
+ this.bubble.dispose();
1935
+ }
1936
+ };
1937
+
1938
+ // src/composite/CompositeChrome.ts
1939
+ var CompositeChrome = class {
1940
+ constructor(box, bubble, routing = makeDefaultRoute()) {
1941
+ this.box = box;
1942
+ this.bubble = bubble;
1943
+ this.routing = routing;
1944
+ }
1945
+ box;
1946
+ bubble;
1947
+ routing;
1948
+ static {
1949
+ __name(this, "CompositeChrome");
1950
+ }
1951
+ active;
1952
+ pendingName = {};
1953
+ pendingContinue = false;
1954
+ /** Master gate from the Session's setVisible — composed with the active
1955
+ * variant's own content state. Hidden at mount. */
1956
+ visible = false;
1957
+ mount(scene) {
1958
+ this.routing.bind(scene);
1959
+ this.box.mount(scene);
1960
+ this.bubble.mount(scene);
1961
+ this.box.setVisible(false);
1962
+ this.bubble.setVisible(false);
1963
+ }
1964
+ setDiagnostics(warn) {
1965
+ this.box.setDiagnostics?.(warn);
1966
+ this.bubble.setDiagnostics?.(warn);
1967
+ }
1968
+ setNameplate(name, color) {
1969
+ this.pendingName = { name, color };
1970
+ this.active?.setNameplate(name, color);
1971
+ }
1972
+ setContinueVisible(visible) {
1973
+ this.pendingContinue = visible;
1974
+ this.active?.setContinueVisible(visible);
1975
+ }
1976
+ present(line) {
1977
+ if (line === void 0) {
1978
+ this.active?.present?.(void 0);
1979
+ return;
1980
+ }
1981
+ const target = lineRoutesToBubble(this.routing.route, line) ? this.bubble : this.box;
1982
+ const other = target === this.box ? this.bubble : this.box;
1983
+ other.setVisible(false);
1984
+ this.active = target;
1985
+ target.setNameplate(this.pendingName.name, this.pendingName.color);
1986
+ target.setContinueVisible(this.pendingContinue);
1987
+ target.present?.(line);
1988
+ target.setVisible(this.visible);
1989
+ }
1990
+ /** Show/hide the chrome. On show, restore ONLY the active variant
1991
+ * and re-apply the buffered caret; the other stays hidden. On hide, hide both
1992
+ * but RETAIN `active` so the next show brings back the right one. */
1993
+ setVisible(visible) {
1994
+ this.visible = visible;
1995
+ if (visible) {
1996
+ if (this.active) {
1997
+ const other = this.active === this.box ? this.bubble : this.box;
1998
+ other.setVisible(false);
1999
+ this.active.setContinueVisible(this.pendingContinue);
2000
+ this.active.setVisible(true);
2001
+ }
2002
+ } else {
2003
+ this.box.setVisible(false);
2004
+ this.bubble.setVisible(false);
2005
+ }
2006
+ }
2007
+ update(dt) {
2008
+ this.box.update(dt);
2009
+ this.bubble.update(dt);
2010
+ }
2011
+ dispose() {
2012
+ this.box.dispose();
2013
+ this.bubble.dispose();
2014
+ }
2015
+ };
2016
+
2017
+ // src/composite/CompositeChoicePresenter.ts
2018
+ var CompositeChoicePresenter = class {
2019
+ constructor(box, bubble, routing = makeDefaultRoute()) {
2020
+ this.box = box;
2021
+ this.bubble = bubble;
2022
+ this.routing = routing;
2023
+ this.box.onChoiceChosen = (p) => this.onChoiceChosen?.(p);
2024
+ this.bubble.onChoiceChosen = (p) => this.onChoiceChosen?.(p);
2025
+ }
2026
+ box;
2027
+ bubble;
2028
+ routing;
2029
+ static {
2030
+ __name(this, "CompositeChoicePresenter");
2031
+ }
2032
+ active;
2033
+ /** Master visibility gate from the Session's setVisible. */
2034
+ visible = false;
2035
+ onChoiceChosen;
2036
+ /** The active list's pointer space (so the binding hit-tests correctly). */
2037
+ get pointerSpace() {
2038
+ return this.active?.pointerSpace ?? "screen";
2039
+ }
2040
+ setDiagnostics(warn) {
2041
+ this.box.setDiagnostics?.(warn);
2042
+ this.bubble.setDiagnostics?.(warn);
2043
+ }
2044
+ /** Routes to the variant this choice will use, so the Session knows whether
2045
+ * to suppress its chrome/body prompt before `present` picks the active one. */
2046
+ ownsPrompt(context) {
2047
+ const target = choiceRoutesToBubble(this.routing.route, context) ? this.bubble : this.box;
2048
+ return target.ownsPrompt?.(context) ?? false;
2049
+ }
2050
+ mount(scene) {
2051
+ this.routing.bind(scene);
2052
+ this.box.mount(scene);
2053
+ this.bubble.mount(scene);
2054
+ }
2055
+ present(choices, context) {
2056
+ const target = choiceRoutesToBubble(this.routing.route, context) ? this.bubble : this.box;
2057
+ const other = target === this.box ? this.bubble : this.box;
2058
+ other.clear();
2059
+ this.active = target;
2060
+ target.present(choices, context);
2061
+ target.setVisible(this.visible);
2062
+ }
2063
+ /** Show/hide the choices — forwarded to both (the inactive one is
2064
+ * cleared, so its setVisible is a no-op); state-preserving on the active. */
2065
+ setVisible(visible) {
2066
+ this.visible = visible;
2067
+ this.box.setVisible(visible);
2068
+ this.bubble.setVisible(visible);
2069
+ }
2070
+ highlight(position) {
2071
+ this.active?.highlight(position);
2072
+ }
2073
+ choiceAtPoint(x, y) {
2074
+ return this.active?.choiceAtPoint?.(x, y);
2075
+ }
2076
+ clear() {
2077
+ this.box.clear();
2078
+ this.bubble.clear();
2079
+ this.active = void 0;
2080
+ }
2081
+ dispose() {
2082
+ this.box.dispose();
2083
+ this.bubble.dispose();
2084
+ }
2085
+ };
2086
+
2087
+ // src/composite/CompositeAvatarPresenter.ts
2088
+ var CompositeAvatarPresenter = class {
2089
+ constructor(box, bubble, routing) {
2090
+ this.box = box;
2091
+ this.bubble = bubble;
2092
+ this.routing = routing;
2093
+ }
2094
+ box;
2095
+ bubble;
2096
+ routing;
2097
+ static {
2098
+ __name(this, "CompositeAvatarPresenter");
2099
+ }
2100
+ mount(scene) {
2101
+ this.routing.bind(scene);
2102
+ this.box.mount(scene);
2103
+ this.bubble.mount(scene);
2104
+ }
2105
+ setSpeaker(speaker) {
2106
+ this.box.setSpeaker(speaker);
2107
+ this.bubble.setSpeaker(speaker);
2108
+ }
2109
+ setExpression(expression) {
2110
+ this.box.setExpression(expression);
2111
+ this.bubble.setExpression(expression);
2112
+ }
2113
+ /** Forward an inline reveal marker to both (cheap + idempotent, like
2114
+ * setExpression — the inactive side's setExpression no-ops with no speaker). */
2115
+ marker(marker) {
2116
+ this.box.marker?.(marker);
2117
+ this.bubble.marker?.(marker);
2118
+ }
2119
+ setSpeaking(speaking) {
2120
+ this.box.setSpeaking(speaking);
2121
+ this.bubble.setSpeaking(speaking);
2122
+ }
2123
+ present(line) {
2124
+ if (line === void 0) {
2125
+ this.box.present?.(void 0);
2126
+ this.bubble.present?.(void 0);
2127
+ return;
2128
+ }
2129
+ const toBubble = lineRoutesToBubble(this.routing.route, line);
2130
+ const active = toBubble ? this.bubble : this.box;
2131
+ const other = toBubble ? this.box : this.bubble;
2132
+ other.present?.(void 0);
2133
+ active.present?.(line);
2134
+ }
2135
+ setVisible(visible) {
2136
+ this.box.setVisible?.(visible);
2137
+ this.bubble.setVisible?.(visible);
2138
+ }
2139
+ update(dt) {
2140
+ this.box.update(dt);
2141
+ this.bubble.update(dt);
2142
+ }
2143
+ dispose() {
2144
+ this.box.dispose();
2145
+ this.bubble.dispose();
2146
+ }
2147
+ };
2148
+
2149
+ // src/avatar/AvatarPresenter.ts
2150
+ function applyExpressionMarker(avatar, marker) {
2151
+ if (marker.name === "expression") avatar.setExpression(marker.props["expression"]);
2152
+ }
2153
+ __name(applyExpressionMarker, "applyExpressionMarker");
2154
+ var NullAvatarPresenter = class {
2155
+ static {
2156
+ __name(this, "NullAvatarPresenter");
2157
+ }
2158
+ mount() {
2159
+ }
2160
+ setSpeaker() {
2161
+ }
2162
+ setExpression() {
2163
+ }
2164
+ setSpeaking() {
2165
+ }
2166
+ update() {
2167
+ }
2168
+ dispose() {
2169
+ }
2170
+ };
2171
+
2172
+ // src/avatar/PortraitPresenter.ts
2173
+ import { Transform as Transform8 } from "@yagejs/core";
2174
+ import { SpriteComponent, texture } from "@yagejs/renderer";
2175
+ var PortraitPresenter = class {
2176
+ constructor(cfg) {
2177
+ this.cfg = cfg;
2178
+ }
2179
+ cfg;
2180
+ static {
2181
+ __name(this, "PortraitPresenter");
2182
+ }
2183
+ // Explicit `| undefined` (not `?`) so reassigning `undefined` in dispose() /
2184
+ // setSpeaker() is legal under the repo's exactOptionalPropertyTypes.
2185
+ scene;
2186
+ entity;
2187
+ sprite;
2188
+ transform;
2189
+ current;
2190
+ speaking = false;
2191
+ bobMs = 0;
2192
+ baseX = 0;
2193
+ baseY = 0;
2194
+ /** Host-hidden gate — a cutscene hides the portrait with the rest of the
2195
+ * UI. Composes with "is a portrait speaker active": shown = current && !hidden. */
2196
+ hidden = false;
2197
+ handles = /* @__PURE__ */ new Map();
2198
+ mount(scene) {
2199
+ this.scene = scene;
2200
+ }
2201
+ setSpeaker(speaker) {
2202
+ const av = speaker?.avatar;
2203
+ if (!av || av.kind !== "portrait") {
2204
+ this.hide();
2205
+ this.current = void 0;
2206
+ return;
2207
+ }
2208
+ this.current = av;
2209
+ this.ensureSprite(av.ref);
2210
+ this.baseX = av.side === "right" ? this.cfg.rightX : this.cfg.leftX;
2211
+ this.baseY = this.cfg.y;
2212
+ this.transform?.setPosition(this.baseX, this.baseY);
2213
+ this.applyTexture(av.ref);
2214
+ this.applyVisibility();
2215
+ }
2216
+ /** Host-hidden gate — hide the portrait during a cutscene, restore on
2217
+ * show. Composes with the active-speaker state, so showing again only
2218
+ * re-reveals the portrait if a portrait speaker is still current. */
2219
+ setVisible(visible) {
2220
+ this.hidden = !visible;
2221
+ this.applyVisibility();
2222
+ }
2223
+ applyVisibility() {
2224
+ if (this.sprite) {
2225
+ this.sprite.sprite.visible = this.current !== void 0 && !this.hidden;
2226
+ }
2227
+ }
2228
+ setExpression(expression) {
2229
+ if (!this.current) return;
2230
+ const variant = expression ? this.current.expressions?.[expression] : void 0;
2231
+ this.applyTexture(variant ?? this.current.ref);
2232
+ }
2233
+ /** Interpret a mid-line `[expression=…/]` reveal marker as a face swap (the
2234
+ * Session name-matches nothing — the presenter owns the convention). */
2235
+ marker(marker) {
2236
+ applyExpressionMarker(this, marker);
2237
+ }
2238
+ setSpeaking(speaking) {
2239
+ this.speaking = speaking;
2240
+ if (!speaking) {
2241
+ this.bobMs = 0;
2242
+ this.transform?.setPosition(this.baseX, this.baseY);
2243
+ }
2244
+ }
2245
+ update(dt) {
2246
+ if (!this.speaking || !this.transform) return;
2247
+ this.bobMs += dt;
2248
+ this.transform.setPosition(this.baseX, this.baseY + Math.sin(this.bobMs / 110) * 1.5);
2249
+ }
2250
+ dispose() {
2251
+ this.entity?.destroy();
2252
+ this.entity = void 0;
2253
+ this.sprite = void 0;
2254
+ this.transform = void 0;
2255
+ }
2256
+ ensureSprite(initialPath) {
2257
+ if (this.sprite || !this.scene) return;
2258
+ const entity = this.scene.spawn("dlg-portrait");
2259
+ this.transform = entity.add(new Transform8());
2260
+ this.transform.setScale(this.cfg.scale, this.cfg.scale);
2261
+ this.sprite = entity.add(
2262
+ new SpriteComponent({
2263
+ texture: this.handle(initialPath),
2264
+ layer: this.cfg.layer,
2265
+ anchor: { x: 0.5, y: 0.5 },
2266
+ visible: false
2267
+ })
2268
+ );
2269
+ this.entity = entity;
2270
+ }
2271
+ applyTexture(path) {
2272
+ this.sprite?.setTexture(this.handle(path));
2273
+ }
2274
+ handle(path) {
2275
+ let h = this.handles.get(path);
2276
+ if (!h) {
2277
+ h = texture(path);
2278
+ this.handles.set(path, h);
2279
+ }
2280
+ return h;
2281
+ }
2282
+ hide() {
2283
+ if (this.sprite) this.sprite.sprite.visible = false;
2284
+ }
2285
+ };
2286
+
2287
+ // src/avatar/SceneFigurePresenter.ts
2288
+ import { Transform as Transform9 } from "@yagejs/core";
2289
+ var SceneFigurePresenter = class {
2290
+ constructor(cfg = {}) {
2291
+ this.cfg = cfg;
2292
+ }
2293
+ cfg;
2294
+ static {
2295
+ __name(this, "SceneFigurePresenter");
2296
+ }
2297
+ // Explicit `| undefined` (not `?`) so reassigning `undefined` in dispose() /
2298
+ // setSpeaker() is legal under the repo's exactOptionalPropertyTypes.
2299
+ scene;
2300
+ figure;
2301
+ actor;
2302
+ transform;
2303
+ speaking = false;
2304
+ bobMs = 0;
2305
+ /** Bob displacement currently applied to the figure's Transform. The bob is
2306
+ * a *relative* offset (delta-translated each frame), so external movement —
2307
+ * an NPC walking mid-line — is preserved instead of being pinned back to a
2308
+ * position captured at setSpeaker time. */
2309
+ bobOffset = 0;
2310
+ mount(scene) {
2311
+ this.scene = scene;
2312
+ }
2313
+ setSpeaker(speaker) {
2314
+ this.releaseBob();
2315
+ const av = speaker?.avatar;
2316
+ if (!av || av.kind !== "scene" || !this.scene) {
2317
+ this.figure = void 0;
2318
+ this.actor = void 0;
2319
+ this.transform = void 0;
2320
+ return;
2321
+ }
2322
+ this.actor = actorRegistryFor(this.scene).resolve(speaker?.id);
2323
+ this.figure = this.actor?.entity ?? this.scene.findEntity(av.ref);
2324
+ this.transform = this.figure?.tryGet(Transform9);
2325
+ }
2326
+ setExpression(expression) {
2327
+ if (this.actor) this.actor.setExpression(expression);
2328
+ else if (this.figure) this.cfg.onExpression?.(this.figure, expression);
2329
+ }
2330
+ /** Mid-line `[expression=…/]` reveal marker → the figure's own expression
2331
+ * (actor or the `onExpression` callback). The Session name-matches nothing. */
2332
+ marker(marker) {
2333
+ applyExpressionMarker(this, marker);
2334
+ }
2335
+ setSpeaking(speaking) {
2336
+ this.speaking = speaking;
2337
+ if (this.actor) this.actor.setSpeaking(speaking);
2338
+ else if (this.figure) this.cfg.onSpeaking?.(this.figure, speaking);
2339
+ if (!speaking) this.releaseBob();
2340
+ }
2341
+ update(dt) {
2342
+ if (!this.speaking || !this.transform || this.cfg.bob === false) return;
2343
+ this.bobMs += dt;
2344
+ const next = Math.sin(this.bobMs / 130) * 1.2;
2345
+ this.transform.translate(0, next - this.bobOffset);
2346
+ this.bobOffset = next;
2347
+ }
2348
+ dispose() {
2349
+ this.releaseBob();
2350
+ this.figure = void 0;
2351
+ this.transform = void 0;
2352
+ }
2353
+ /** Remove only the residual bob displacement — never teleport to a captured
2354
+ * base, which would undo legitimate movement since speaking began. */
2355
+ releaseBob() {
2356
+ if (this.transform && this.bobOffset !== 0) {
2357
+ this.transform.translate(0, -this.bobOffset);
2358
+ }
2359
+ this.bobOffset = 0;
2360
+ this.bobMs = 0;
2361
+ }
2362
+ };
2363
+
2364
+ // src/avatar/InBoxAvatarPresenter.ts
2365
+ import { Transform as Transform10 } from "@yagejs/core";
2366
+ import {
2367
+ GraphicsComponent as GraphicsComponent6,
2368
+ SpriteComponent as SpriteComponent2,
2369
+ texture as texture2
2370
+ } from "@yagejs/renderer";
2371
+ var nextId = 0;
2372
+ var InBoxAvatarPresenter = class {
2373
+ constructor(layout, cfg) {
2374
+ this.layout = layout;
2375
+ this.cfg = cfg;
2376
+ this.layout.onChange(() => this.place());
2377
+ }
2378
+ layout;
2379
+ cfg;
2380
+ static {
2381
+ __name(this, "InBoxAvatarPresenter");
2382
+ }
2383
+ insetKey = `avatar:${nextId++}`;
2384
+ // Explicit `| undefined` (not `?`) so reassigning `undefined` is legal under
2385
+ // the repo's exactOptionalPropertyTypes.
2386
+ scene;
2387
+ entity;
2388
+ sprite;
2389
+ transform;
2390
+ /** Optional background panel (behind the portrait), its own entity so it draws
2391
+ * under the sprite on the same layer. */
2392
+ bgEntity;
2393
+ bg;
2394
+ bgTransform;
2395
+ side = "left";
2396
+ /** A portrait is up for the current line (from `meta.portrait` + presence). */
2397
+ shown = false;
2398
+ /** Host-hidden gate (a cutscene hides the avatar with the rest of the UI). */
2399
+ hidden = false;
2400
+ handles = /* @__PURE__ */ new Map();
2401
+ mount(scene) {
2402
+ this.scene = scene;
2403
+ }
2404
+ // Image/side/presence are line-driven via `present`, not the speaker def — so
2405
+ // setSpeaker / setExpression / setSpeaking are intentionally inert here.
2406
+ setSpeaker() {
2407
+ }
2408
+ setExpression() {
2409
+ }
2410
+ setSpeaking() {
2411
+ }
2412
+ /** Routes a mid-line `[expression=…/]` marker to its own setExpression (inert
2413
+ * here — this avatar is portrait-by-`meta`, not expression-mapped — so it's
2414
+ * the uniform contract, not a visible face swap). */
2415
+ marker(marker) {
2416
+ applyExpressionMarker(this, marker);
2417
+ }
2418
+ /** Read the line's `meta` to show/hide the portrait and reserve (or clear) the
2419
+ * text-reflow inset. Called before the body text presents, so the text wraps
2420
+ * to the narrowed region. */
2421
+ present(line) {
2422
+ const meta = line?.meta;
2423
+ const portrait = typeof meta?.["portrait"] === "string" ? meta["portrait"] : void 0;
2424
+ const visible = portrait !== void 0 && meta?.["presence"] !== false;
2425
+ this.side = meta?.["side"] === "right" ? "right" : "left";
2426
+ if (visible && portrait !== void 0) {
2427
+ this.ensureSprite(portrait);
2428
+ this.applyTexture(portrait);
2429
+ this.shown = true;
2430
+ this.layout.setInset(this.insetKey, { side: this.side, width: this.cfg.width + (this.cfg.gap ?? 8) });
2431
+ } else {
2432
+ this.shown = false;
2433
+ this.layout.setInset(this.insetKey, void 0);
2434
+ }
2435
+ this.place();
2436
+ this.applyVisibility();
2437
+ }
2438
+ /** Host-hidden gate — hide with a cutscene, restore on show (only if a
2439
+ * portrait is still current). */
2440
+ setVisible(visible) {
2441
+ this.hidden = !visible;
2442
+ this.applyVisibility();
2443
+ }
2444
+ update() {
2445
+ }
2446
+ dispose() {
2447
+ this.layout.setInset(this.insetKey, void 0);
2448
+ this.entity?.destroy();
2449
+ this.bgEntity?.destroy();
2450
+ this.entity = void 0;
2451
+ this.bgEntity = void 0;
2452
+ this.sprite = void 0;
2453
+ this.bg = void 0;
2454
+ this.transform = void 0;
2455
+ this.bgTransform = void 0;
2456
+ }
2457
+ /** Centre the portrait (+ its panel) in its reserved column, inset by the box
2458
+ * padding so it sits inside the border like the text — at the frame's current
2459
+ * rect, so it follows `meta.position` and a grown choice panel. */
2460
+ place() {
2461
+ if (!this.transform) return;
2462
+ const frame = this.layout.frameRect();
2463
+ const pad = this.layout.padding();
2464
+ const half = this.cfg.width / 2;
2465
+ const x = this.side === "left" ? frame.x + pad + half : frame.x + frame.width - pad - half;
2466
+ const y = this.cfg.align === "top" ? this.layout.textRegion().y + half : frame.y + frame.height / 2;
2467
+ this.transform.setPosition(x, y);
2468
+ this.bgTransform?.setPosition(x, y);
2469
+ }
2470
+ applyVisibility() {
2471
+ const shown = this.shown && !this.hidden;
2472
+ if (this.sprite) this.sprite.sprite.visible = shown;
2473
+ if (this.bg) this.bg.graphics.visible = shown;
2474
+ }
2475
+ ensureSprite(initialKey) {
2476
+ if (this.sprite || !this.scene) return;
2477
+ const bgCfg = this.cfg.background;
2478
+ if (bgCfg) {
2479
+ const bgEntity = this.scene.spawn("dlg-inbox-avatar-bg");
2480
+ this.bgTransform = bgEntity.add(new Transform10());
2481
+ const w = this.cfg.width;
2482
+ const bg = bgEntity.add(new GraphicsComponent6({ layer: this.cfg.layer }));
2483
+ bg.draw(
2484
+ (g) => g.roundRect(-w / 2, -w / 2, w, w, bgCfg.radius ?? 8).fill({ color: bgCfg.color, alpha: bgCfg.alpha ?? 1 })
2485
+ );
2486
+ bg.graphics.visible = false;
2487
+ this.bg = bg;
2488
+ this.bgEntity = bgEntity;
2489
+ }
2490
+ const entity = this.scene.spawn("dlg-inbox-avatar");
2491
+ this.transform = entity.add(new Transform10());
2492
+ const scale = this.cfg.scale ?? 1;
2493
+ this.transform.setScale(scale, scale);
2494
+ this.sprite = entity.add(
2495
+ new SpriteComponent2({
2496
+ texture: this.handle(initialKey),
2497
+ layer: this.cfg.layer,
2498
+ anchor: { x: 0.5, y: 0.5 },
2499
+ visible: false
2500
+ })
2501
+ );
2502
+ this.entity = entity;
2503
+ }
2504
+ applyTexture(key) {
2505
+ this.sprite?.setTexture(this.handle(key));
2506
+ }
2507
+ handle(key) {
2508
+ let h = this.handles.get(key);
2509
+ if (!h) {
2510
+ h = texture2(key);
2511
+ this.handles.set(key, h);
2512
+ }
2513
+ return h;
2514
+ }
2515
+ };
2516
+
2517
+ // src/avatar/BubbleAvatarPresenter.ts
2518
+ import { Transform as Transform11 } from "@yagejs/core";
2519
+ import {
2520
+ GraphicsComponent as GraphicsComponent7,
2521
+ SpriteComponent as SpriteComponent3,
2522
+ texture as texture3
2523
+ } from "@yagejs/renderer";
2524
+ var BubbleAvatarPresenter = class {
2525
+ constructor(layout, cfg) {
2526
+ this.layout = layout;
2527
+ this.cfg = cfg;
2528
+ this.layout.onChange(() => this.follow());
2529
+ }
2530
+ layout;
2531
+ cfg;
2532
+ static {
2533
+ __name(this, "BubbleAvatarPresenter");
2534
+ }
2535
+ // Explicit `| undefined` (not `?`) for exactOptionalPropertyTypes reassignment.
2536
+ scene;
2537
+ entity;
2538
+ sprite;
2539
+ transform;
2540
+ bgEntity;
2541
+ bg;
2542
+ bgTransform;
2543
+ side = "left";
2544
+ speakerId;
2545
+ shown = false;
2546
+ hidden = false;
2547
+ handles = /* @__PURE__ */ new Map();
2548
+ mount(scene) {
2549
+ this.scene = scene;
2550
+ }
2551
+ // Line-driven (via present), so the speaker-def hooks are inert here.
2552
+ setSpeaker() {
2553
+ }
2554
+ setExpression() {
2555
+ }
2556
+ setSpeaking() {
2557
+ }
2558
+ /** Uniform marker contract: routes `[expression=…/]` to its own setExpression
2559
+ * (inert here — this avatar is portrait-by-`meta`). */
2560
+ marker(marker) {
2561
+ applyExpressionMarker(this, marker);
2562
+ }
2563
+ present(line) {
2564
+ const meta = line?.meta;
2565
+ const portrait = typeof meta?.["portrait"] === "string" ? meta["portrait"] : void 0;
2566
+ const visible = portrait !== void 0 && meta?.["presence"] !== false;
2567
+ this.side = meta?.["side"] === "right" ? "right" : "left";
2568
+ this.speakerId = line?.speaker?.id;
2569
+ if (visible && portrait !== void 0 && line) {
2570
+ const gap = this.cfg.gap ?? 8;
2571
+ this.layout.setPortraitInset({ side: this.side, width: this.cfg.size + gap, height: this.cfg.size });
2572
+ this.ensureSprite(portrait);
2573
+ this.applyTexture(portrait);
2574
+ this.shown = true;
2575
+ } else {
2576
+ this.layout.setPortraitInset(void 0);
2577
+ this.shown = false;
2578
+ }
2579
+ this.follow();
2580
+ this.applyVisibility();
2581
+ }
2582
+ setVisible(visible) {
2583
+ this.hidden = !visible;
2584
+ this.applyVisibility();
2585
+ }
2586
+ update() {
2587
+ this.follow();
2588
+ }
2589
+ dispose() {
2590
+ this.entity?.destroy();
2591
+ this.bgEntity?.destroy();
2592
+ this.entity = void 0;
2593
+ this.bgEntity = void 0;
2594
+ this.sprite = void 0;
2595
+ this.bg = void 0;
2596
+ this.transform = void 0;
2597
+ this.bgTransform = void 0;
2598
+ }
2599
+ /** Place the portrait in its reserved column INSIDE the active bubble (say
2600
+ * bubble or choice panel), vertically centred on the body, tracking the
2601
+ * speaker's (moving) anchor. */
2602
+ follow() {
2603
+ const size = this.layout.activeSize();
2604
+ if (!this.transform || !this.scene || !size || !this.shown) return;
2605
+ const a = this.layout.anchorFor(this.scene, this.speakerId);
2606
+ const pad = this.layout.padding;
2607
+ const half = this.cfg.size / 2;
2608
+ const x = this.side === "left" ? a.x - size.width / 2 + pad + half : a.x + size.width / 2 - pad - half;
2609
+ const bodyTop = a.y - this.layout.offsetY - size.height;
2610
+ const y = this.cfg.align === "top" ? bodyTop + pad + half : a.y - this.layout.offsetY - size.height / 2;
2611
+ this.transform.setPosition(x, y);
2612
+ this.bgTransform?.setPosition(x, y);
2613
+ }
2614
+ applyVisibility() {
2615
+ const shown = this.shown && !this.hidden;
2616
+ if (this.sprite) this.sprite.sprite.visible = shown;
2617
+ if (this.bg) this.bg.graphics.visible = shown;
2618
+ }
2619
+ ensureSprite(initialKey) {
2620
+ if (this.sprite || !this.scene) return;
2621
+ const bgCfg = this.cfg.background;
2622
+ if (bgCfg) {
2623
+ const bgEntity = this.scene.spawn("dlg-bubble-avatar-bg");
2624
+ this.bgTransform = bgEntity.add(new Transform11());
2625
+ const w = this.cfg.size;
2626
+ const bg = bgEntity.add(new GraphicsComponent7({ layer: this.cfg.layer }));
2627
+ bg.draw(
2628
+ (g) => g.roundRect(-w / 2, -w / 2, w, w, bgCfg.radius ?? 8).fill({ color: bgCfg.color, alpha: bgCfg.alpha ?? 1 })
2629
+ );
2630
+ bg.graphics.visible = false;
2631
+ this.bg = bg;
2632
+ this.bgEntity = bgEntity;
2633
+ }
2634
+ const entity = this.scene.spawn("dlg-bubble-avatar");
2635
+ this.transform = entity.add(new Transform11());
2636
+ const scale = this.cfg.scale ?? 1;
2637
+ this.transform.setScale(scale, scale);
2638
+ this.sprite = entity.add(
2639
+ new SpriteComponent3({
2640
+ texture: this.handle(initialKey),
2641
+ layer: this.cfg.layer,
2642
+ anchor: { x: 0.5, y: 0.5 },
2643
+ visible: false
2644
+ })
2645
+ );
2646
+ this.entity = entity;
2647
+ }
2648
+ applyTexture(key) {
2649
+ this.sprite?.setTexture(this.handle(key));
2650
+ }
2651
+ handle(key) {
2652
+ let h = this.handles.get(key);
2653
+ if (!h) {
2654
+ h = texture3(key);
2655
+ this.handles.set(key, h);
2656
+ }
2657
+ return h;
2658
+ }
2659
+ };
2660
+
2661
+ // src/factory/defaultTheme.ts
2662
+ function defaultTheme() {
2663
+ return {
2664
+ // Viewport-relative: a full-width bottom bar resolved against the renderer's
2665
+ // design size at mount, so this works at any resolution with no override.
2666
+ box: { marginX: 32, marginY: 24, height: 160 },
2667
+ padding: 16,
2668
+ frameColor: 1710638,
2669
+ frameAlpha: 0.92,
2670
+ borderColor: 4868746,
2671
+ cornerRadius: 8,
2672
+ nameColor: 16767078,
2673
+ nameSize: 16,
2674
+ indicatorColor: 16777215,
2675
+ textSize: 18,
2676
+ lineHeight: 24,
2677
+ textColor: 15790320,
2678
+ charsPerSec: 45,
2679
+ choiceSize: 16,
2680
+ choiceColor: 11184810,
2681
+ choiceSelectedColor: 16777215,
2682
+ highlightColor: 4868746,
2683
+ // No bitmapFont → canvas SplitText/Text path (zero assets).
2684
+ fontFamily: "sans-serif",
2685
+ layerFrame: DIALOGUE_LAYER_FRAME,
2686
+ layerText: DIALOGUE_LAYER_TEXT
2687
+ };
2688
+ }
2689
+ __name(defaultTheme, "defaultTheme");
2690
+
2691
+ // src/factory/themeFonts.ts
2692
+ function themeFonts(theme) {
2693
+ return {
2694
+ bitmapFont: theme.bitmapFont,
2695
+ fontFamily: theme.fontFamily,
2696
+ resolution: theme.resolution
2697
+ };
2698
+ }
2699
+ __name(themeFonts, "themeFonts");
2700
+
2701
+ // src/factory/createBoxDialogue.ts
2702
+ function createBoxDialogue(theme = defaultTheme(), opts = {}) {
2703
+ const fonts = themeFonts(theme);
2704
+ const layout = new BoxLayout({
2705
+ box: theme.box,
2706
+ padding: theme.padding,
2707
+ nameSize: theme.nameSize,
2708
+ textSize: theme.textSize,
2709
+ lineHeight: theme.lineHeight,
2710
+ choiceGap: theme.choiceGap ?? DEFAULT_CHOICE_GAP,
2711
+ ...fonts
2712
+ });
2713
+ const chrome = new DialogueChrome(
2714
+ {
2715
+ frameColor: theme.frameColor,
2716
+ frameAlpha: theme.frameAlpha,
2717
+ borderColor: theme.borderColor,
2718
+ cornerRadius: theme.cornerRadius,
2719
+ nameColor: theme.nameColor,
2720
+ nameSize: theme.nameSize,
2721
+ indicatorColor: theme.indicatorColor,
2722
+ caret: theme.caret,
2723
+ frameStyles: boxFrameStyles(theme.textured),
2724
+ layerFrame: theme.layerFrame,
2725
+ layerText: theme.layerText,
2726
+ ...fonts
2727
+ },
2728
+ layout
2729
+ );
2730
+ const choices = new ChoiceListPresenter(
2731
+ {
2732
+ choiceSize: theme.choiceSize,
2733
+ choiceColor: theme.choiceColor,
2734
+ choiceSelectedColor: theme.choiceSelectedColor,
2735
+ highlightColor: theme.highlightColor,
2736
+ choiceGap: theme.choiceGap,
2737
+ layerFrame: theme.layerFrame,
2738
+ layerText: theme.layerText,
2739
+ ...fonts
2740
+ },
2741
+ layout
2742
+ );
2743
+ const text = new BoxTextView(
2744
+ {
2745
+ textSize: theme.textSize,
2746
+ lineHeight: theme.lineHeight,
2747
+ textColor: theme.textColor,
2748
+ charsPerSec: theme.charsPerSec,
2749
+ layer: theme.layerText,
2750
+ ...fonts
2751
+ },
2752
+ layout
2753
+ );
2754
+ const avatar = opts.avatar?.(layout);
2755
+ return {
2756
+ chrome,
2757
+ text,
2758
+ choices,
2759
+ ...avatar ? { avatar } : {},
2760
+ skipMultiplier: theme.skipMultiplier
2761
+ };
2762
+ }
2763
+ __name(createBoxDialogue, "createBoxDialogue");
2764
+
2765
+ // src/factory/createBubbleDialogue.ts
2766
+ var DEFAULT_BUBBLE = {
2767
+ minWidth: 90,
2768
+ maxWidth: 260,
2769
+ height: 40,
2770
+ padding: 8,
2771
+ offsetY: 24,
2772
+ tail: 6
2773
+ };
2774
+ function createBubbleDialogue(theme = defaultTheme(), opts) {
2775
+ const geo = { ...DEFAULT_BUBBLE, ...opts.bubble };
2776
+ const fonts = themeFonts(theme);
2777
+ const layout = new BubbleLayout({
2778
+ minWidth: geo.minWidth,
2779
+ maxWidth: geo.maxWidth,
2780
+ height: geo.height,
2781
+ padding: geo.padding,
2782
+ offsetY: geo.offsetY,
2783
+ textSize: theme.textSize,
2784
+ lineHeight: theme.lineHeight,
2785
+ fallbackAnchor: opts.fallbackAnchor,
2786
+ ...fonts
2787
+ });
2788
+ const chrome = new BubbleChrome(
2789
+ {
2790
+ layer: opts.worldLayer,
2791
+ tail: geo.tail,
2792
+ tailLean: theme.tailLean,
2793
+ frameColor: theme.frameColor,
2794
+ frameAlpha: theme.frameAlpha,
2795
+ borderColor: theme.borderColor,
2796
+ cornerRadius: theme.cornerRadius,
2797
+ nameColor: theme.nameColor,
2798
+ nameSize: theme.nameSize,
2799
+ indicatorColor: theme.indicatorColor,
2800
+ caret: theme.caret,
2801
+ frame: defaultBubbleFrame(theme.textured),
2802
+ ...fonts
2803
+ },
2804
+ layout
2805
+ );
2806
+ const text = new BubbleTextView(
2807
+ {
2808
+ textSize: theme.textSize,
2809
+ lineHeight: theme.lineHeight,
2810
+ textColor: theme.textColor,
2811
+ charsPerSec: theme.charsPerSec,
2812
+ layer: opts.worldLayer,
2813
+ ...fonts
2814
+ },
2815
+ layout
2816
+ );
2817
+ const choices = new BubbleChoicePresenter(
2818
+ {
2819
+ layer: opts.worldLayer,
2820
+ width: geo.maxWidth,
2821
+ padding: geo.padding,
2822
+ offsetY: geo.offsetY,
2823
+ tail: geo.tail,
2824
+ choiceSize: theme.choiceSize,
2825
+ choiceColor: theme.choiceColor,
2826
+ choiceSelectedColor: theme.choiceSelectedColor,
2827
+ highlightColor: theme.highlightColor,
2828
+ choiceGap: theme.choiceGap,
2829
+ textColor: theme.textColor,
2830
+ frameColor: theme.frameColor,
2831
+ frameAlpha: theme.frameAlpha,
2832
+ borderColor: theme.borderColor,
2833
+ cornerRadius: theme.cornerRadius,
2834
+ ...fonts
2835
+ },
2836
+ layout
2837
+ );
2838
+ const avatar = opts.avatar?.(layout);
2839
+ return {
2840
+ chrome,
2841
+ text,
2842
+ choices,
2843
+ ...avatar ? { avatar } : {},
2844
+ skipMultiplier: theme.skipMultiplier
2845
+ };
2846
+ }
2847
+ __name(createBubbleDialogue, "createBubbleDialogue");
2848
+
2849
+ // src/factory/createMixedDialogue.ts
2850
+ function createMixedDialogue(theme = defaultTheme(), opts) {
2851
+ const box = createBoxDialogue(theme, opts.avatar?.box ? { avatar: opts.avatar.box } : {});
2852
+ const bubble = createBubbleDialogue(theme, {
2853
+ worldLayer: opts.worldLayer,
2854
+ ...opts.bubble !== void 0 ? { bubble: opts.bubble } : {},
2855
+ ...opts.fallbackAnchor !== void 0 ? { fallbackAnchor: opts.fallbackAnchor } : {},
2856
+ ...opts.avatar?.bubble ? { avatar: opts.avatar.bubble } : {}
2857
+ });
2858
+ const routing = opts.route ? fixedRoute(opts.route) : makeDefaultRoute();
2859
+ let avatar;
2860
+ if (box.avatar && bubble.avatar) {
2861
+ avatar = new CompositeAvatarPresenter(box.avatar, bubble.avatar, routing);
2862
+ } else {
2863
+ avatar = box.avatar ?? bubble.avatar;
2864
+ }
2865
+ return {
2866
+ text: new CompositeTextPresenter(box.text, bubble.text, routing),
2867
+ chrome: new CompositeChrome(box.chrome, bubble.chrome, routing),
2868
+ choices: new CompositeChoicePresenter(box.choices, bubble.choices, routing),
2869
+ ...avatar ? { avatar } : {},
2870
+ skipMultiplier: theme.skipMultiplier
2871
+ };
2872
+ }
2873
+ __name(createMixedDialogue, "createMixedDialogue");
2874
+ export {
2875
+ ActorRegistry,
2876
+ BoxLayout,
2877
+ BoxTextView,
2878
+ BubbleAnchorResolver,
2879
+ BubbleAvatarPresenter,
2880
+ BubbleChoicePresenter,
2881
+ BubbleChrome,
2882
+ BubbleLayout,
2883
+ BubbleTextView,
2884
+ CHROME_STYLE_DEFAULT,
2885
+ CHROME_STYLE_NONE,
2886
+ ChoiceListPresenter,
2887
+ CompositeAvatarPresenter,
2888
+ CompositeChoicePresenter,
2889
+ CompositeChrome,
2890
+ CompositeTextPresenter,
2891
+ DEFAULT_BUBBLE,
2892
+ DEFAULT_CARET_BLINK_MS,
2893
+ DEFAULT_CARET_SIZE,
2894
+ DEFAULT_CHOICE_GAP,
2895
+ DEFAULT_TAIL_LEAN,
2896
+ DIALOGUE_LAYERS,
2897
+ DIALOGUE_LAYER_AVATAR,
2898
+ DIALOGUE_LAYER_FRAME,
2899
+ DIALOGUE_LAYER_TEXT,
2900
+ DialogueActor,
2901
+ DialogueChrome,
2902
+ DialogueTextView,
2903
+ InBoxAvatarPresenter,
2904
+ NullAvatarPresenter,
2905
+ PortraitPresenter,
2906
+ RadialChoicePresenter,
2907
+ SceneFigurePresenter,
2908
+ actorRegistryFor,
2909
+ createBoxDialogue,
2910
+ createBubbleDialogue,
2911
+ createMixedDialogue,
2912
+ defaultTheme,
2913
+ effectDrivesTint,
2914
+ evaluateEffect,
2915
+ fixedRoute,
2916
+ makeDefaultRoute,
2917
+ routeWithActor,
2918
+ stackChoiceRows
2919
+ };
2920
+ //# sourceMappingURL=presenters.js.map