@lute-lang/player-web 0.0.0-stage → 0.40.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.
- package/README.md +44 -2
- package/dist/assets/pixiAssets.d.ts +47 -0
- package/dist/assets/pixiAssetsBackend.d.ts +3 -0
- package/dist/audio/pixiAudio.d.ts +61 -0
- package/dist/audio/pixiSoundBackend.d.ts +3 -0
- package/dist/choice/choiceMenu.d.ts +56 -0
- package/dist/effects/catalogue.d.ts +52 -0
- package/dist/engine/app.d.ts +14 -0
- package/dist/engine/behaviours.d.ts +35 -0
- package/dist/engine/easing.d.ts +24 -0
- package/dist/engine/types.d.ts +70 -0
- package/dist/engine/world.d.ts +10 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3361 -0
- package/dist/input/gamepad.d.ts +22 -0
- package/dist/input/index.d.ts +7 -0
- package/dist/input/map.d.ts +42 -0
- package/dist/mount.d.ts +31 -0
- package/dist/presenter/index.d.ts +1 -0
- package/dist/presenter/pixiPresenter.d.ts +66 -0
- package/dist/scene/actor.d.ts +26 -0
- package/dist/scene/background.d.ts +24 -0
- package/dist/scene/camera.d.ts +37 -0
- package/dist/scene/cgLayer.d.ts +21 -0
- package/dist/scene/index.d.ts +7 -0
- package/dist/scene/shared.d.ts +36 -0
- package/dist/scene/vfx.d.ts +14 -0
- package/dist/scene/videoLayer.d.ts +21 -0
- package/dist/text/dialogueWindow.d.ts +52 -0
- package/dist/text/richText.d.ts +37 -0
- package/dist/text/segments.d.ts +31 -0
- package/dist/theme.d.ts +7 -0
- package/dist/ui/index.d.ts +3 -0
- package/dist/ui/screens/backlog-screen.d.ts +1 -0
- package/dist/ui/screens/backlog.d.ts +25 -0
- package/dist/ui/screens/common.d.ts +68 -0
- package/dist/ui/screens/confirm.d.ts +1 -0
- package/dist/ui/screens/ended.d.ts +1 -0
- package/dist/ui/screens/halted.d.ts +1 -0
- package/dist/ui/screens/hud.d.ts +1 -0
- package/dist/ui/screens/idle.d.ts +1 -0
- package/dist/ui/screens/index.d.ts +4 -0
- package/dist/ui/screens/loading.d.ts +1 -0
- package/dist/ui/screens/menu.d.ts +1 -0
- package/dist/ui/screens/save-load-screen.d.ts +1 -0
- package/dist/ui/screens/save-load.d.ts +14 -0
- package/dist/ui/screens/settings-screen.d.ts +1 -0
- package/dist/ui/screens/settings.d.ts +13 -0
- package/dist/ui/screens/title.d.ts +1 -0
- package/dist/ui/ui.d.ts +65 -0
- package/dist/ui/widgets/base.d.ts +54 -0
- package/dist/ui/widgets/button.d.ts +15 -0
- package/dist/ui/widgets/controls.d.ts +47 -0
- package/dist/ui/widgets/focus-manager.d.ts +1 -0
- package/dist/ui/widgets/index.d.ts +4 -0
- package/dist/ui/widgets/label.d.ts +1 -0
- package/dist/ui/widgets/list.d.ts +50 -0
- package/dist/ui/widgets/panel.d.ts +1 -0
- package/dist/ui/widgets/scroll-list.d.ts +1 -0
- package/dist/ui/widgets/segmented.d.ts +1 -0
- package/dist/ui/widgets/slider.d.ts +1 -0
- package/dist/ui/widgets/toggle.d.ts +1 -0
- package/package.json +48 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,3361 @@
|
|
|
1
|
+
// src/mount.ts
|
|
2
|
+
import { Context as Context5, Effect as Effect19, Exit, Layer as Layer5, ManagedRuntime, Scope as Scope2, Stream as Stream8, SubscriptionRef as SubscriptionRef3 } from "effect";
|
|
3
|
+
import * as KeyValueStore from "effect/persistence/KeyValueStore";
|
|
4
|
+
import * as BrowserKeyValueStore from "@effect/platform-browser/BrowserKeyValueStore";
|
|
5
|
+
import * as FetchHttpClient from "effect/http/FetchHttpClient";
|
|
6
|
+
import * as RuntimeBrowser from "@lute-lang/runtime/browser";
|
|
7
|
+
import * as Runtime from "@lute-lang/runtime";
|
|
8
|
+
import { LutePlayer, BindingsLoader } from "@lute-lang/player";
|
|
9
|
+
|
|
10
|
+
// src/engine/app.ts
|
|
11
|
+
import { Context, Effect, Layer, PubSub, Stream } from "effect";
|
|
12
|
+
import { Application, Container } from "pixi.js";
|
|
13
|
+
var makeLayers = () => {
|
|
14
|
+
const camera = new Container;
|
|
15
|
+
const actors = new Container;
|
|
16
|
+
camera.addChild(actors);
|
|
17
|
+
return {
|
|
18
|
+
background: new Container,
|
|
19
|
+
cg: new Container,
|
|
20
|
+
camera,
|
|
21
|
+
actors,
|
|
22
|
+
video: new Container,
|
|
23
|
+
dialogue: new Container,
|
|
24
|
+
choice: new Container,
|
|
25
|
+
overlay: new Container,
|
|
26
|
+
transition: new Container
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
class PixiApp extends Context.Service()("Lute.Web.PixiApp") {
|
|
31
|
+
static layer = (element, design, background = 0) => Layer.effect(PixiApp, Effect.gen(function* () {
|
|
32
|
+
const app = new Application;
|
|
33
|
+
yield* Effect.tryPromise({
|
|
34
|
+
try: () => app.init({
|
|
35
|
+
preference: "webgl",
|
|
36
|
+
antialias: true,
|
|
37
|
+
resizeTo: element,
|
|
38
|
+
backgroundColor: background,
|
|
39
|
+
autoDensity: true,
|
|
40
|
+
resolution: typeof window === "undefined" ? 1 : window.devicePixelRatio
|
|
41
|
+
}),
|
|
42
|
+
catch: (cause) => cause
|
|
43
|
+
});
|
|
44
|
+
element.appendChild(app.canvas);
|
|
45
|
+
const root = new Container;
|
|
46
|
+
const layers = makeLayers();
|
|
47
|
+
root.addChild(layers.background, layers.cg, layers.camera, layers.video, layers.dialogue, layers.choice, layers.overlay, layers.transition);
|
|
48
|
+
layers.camera.pivot.set(design.width / 2, design.height / 2);
|
|
49
|
+
layers.camera.position.set(design.width / 2, design.height / 2);
|
|
50
|
+
app.stage.addChild(root);
|
|
51
|
+
const resize = (width, height) => {
|
|
52
|
+
const viewWidth = (width ?? element.clientWidth) || app.screen.width / app.renderer.resolution;
|
|
53
|
+
const viewHeight = (height ?? element.clientHeight) || app.screen.height / app.renderer.resolution;
|
|
54
|
+
const scale = Math.min(viewWidth / design.width, viewHeight / design.height);
|
|
55
|
+
root.scale.set(scale);
|
|
56
|
+
root.position.set((viewWidth - design.width * scale) / 2, (viewHeight - design.height * scale) / 2);
|
|
57
|
+
};
|
|
58
|
+
app.renderer.on("resize", resize);
|
|
59
|
+
resize();
|
|
60
|
+
const framesPubSub = yield* PubSub.unbounded();
|
|
61
|
+
const frame = (ticker) => {
|
|
62
|
+
PubSub.publishUnsafe(framesPubSub, Math.min(0.1, Math.max(0, ticker.deltaMS / 1000)));
|
|
63
|
+
};
|
|
64
|
+
app.ticker.add(frame);
|
|
65
|
+
const frames = Stream.fromPubSub(framesPubSub);
|
|
66
|
+
const service = { app, layers, design, frames };
|
|
67
|
+
return yield* Effect.acquireRelease(Effect.succeed(service), () => Effect.sync(() => {
|
|
68
|
+
app.ticker.remove(frame);
|
|
69
|
+
app.renderer.off("resize", resize);
|
|
70
|
+
Effect.runPromise(PubSub.shutdown(framesPubSub));
|
|
71
|
+
app.destroy(true, {
|
|
72
|
+
children: true,
|
|
73
|
+
texture: true,
|
|
74
|
+
textureSource: true,
|
|
75
|
+
context: true
|
|
76
|
+
});
|
|
77
|
+
}));
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/engine/world.ts
|
|
82
|
+
import { Context as Context2, Deferred, Effect as Effect2, Layer as Layer2, Stream as Stream2 } from "effect";
|
|
83
|
+
var complete = (entry) => {
|
|
84
|
+
Effect2.runSync(Deferred.succeed(entry.done, undefined));
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
class World extends Context2.Service()("Lute.Web.World") {
|
|
88
|
+
static layer = Layer2.effect(World, Effect2.gen(function* () {
|
|
89
|
+
const pixi = yield* PixiApp;
|
|
90
|
+
const entries = [];
|
|
91
|
+
const world = {
|
|
92
|
+
run: (target, behaviour) => {
|
|
93
|
+
const done = Deferred.makeUnsafe();
|
|
94
|
+
entries.push({ target, behaviour, done });
|
|
95
|
+
return Deferred.await(done);
|
|
96
|
+
},
|
|
97
|
+
finishAll: (target) => Effect2.sync(() => {
|
|
98
|
+
let index = entries.length - 1;
|
|
99
|
+
while (index >= 0) {
|
|
100
|
+
const entry = entries[index];
|
|
101
|
+
if (entry === undefined) {
|
|
102
|
+
index -= 1;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (target === undefined || entry.target === target) {
|
|
106
|
+
entry.behaviour.finish();
|
|
107
|
+
entry.behaviour.dispose();
|
|
108
|
+
complete(entry);
|
|
109
|
+
const last = entries.pop();
|
|
110
|
+
if (last !== undefined && index < entries.length)
|
|
111
|
+
entries[index] = last;
|
|
112
|
+
}
|
|
113
|
+
index -= 1;
|
|
114
|
+
}
|
|
115
|
+
}),
|
|
116
|
+
cancel: (target) => Effect2.sync(() => {
|
|
117
|
+
let index = entries.length - 1;
|
|
118
|
+
while (index >= 0) {
|
|
119
|
+
const entry = entries[index];
|
|
120
|
+
if (entry === undefined) {
|
|
121
|
+
index -= 1;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (target === undefined || entry.target === target) {
|
|
125
|
+
entry.behaviour.dispose();
|
|
126
|
+
complete(entry);
|
|
127
|
+
const last = entries.pop();
|
|
128
|
+
if (last !== undefined && index < entries.length)
|
|
129
|
+
entries[index] = last;
|
|
130
|
+
}
|
|
131
|
+
index -= 1;
|
|
132
|
+
}
|
|
133
|
+
}),
|
|
134
|
+
size: Effect2.sync(() => entries.length)
|
|
135
|
+
};
|
|
136
|
+
const ticker = Stream2.runForEach(pixi.frames, (dt) => Effect2.sync(() => {
|
|
137
|
+
let index = entries.length - 1;
|
|
138
|
+
while (index >= 0) {
|
|
139
|
+
const entry = entries[index];
|
|
140
|
+
if (entry === undefined) {
|
|
141
|
+
index -= 1;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (entry.behaviour.update(dt) === "done") {
|
|
145
|
+
entry.behaviour.dispose();
|
|
146
|
+
complete(entry);
|
|
147
|
+
const last = entries.pop();
|
|
148
|
+
if (last !== undefined && index < entries.length)
|
|
149
|
+
entries[index] = last;
|
|
150
|
+
}
|
|
151
|
+
index -= 1;
|
|
152
|
+
}
|
|
153
|
+
}));
|
|
154
|
+
yield* Effect2.forkScoped(ticker);
|
|
155
|
+
return world;
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/ui/ui.ts
|
|
160
|
+
import { Context as Context3, Effect as Effect3, Layer as Layer3, Queue, Stream as Stream3 } from "effect";
|
|
161
|
+
import { Container as Container6, Graphics as Graphics6 } from "pixi.js";
|
|
162
|
+
|
|
163
|
+
// src/ui/widgets/base.ts
|
|
164
|
+
import { Container as Container2, Graphics, Rectangle, Text } from "pixi.js";
|
|
165
|
+
|
|
166
|
+
class FocusManager {
|
|
167
|
+
items = [];
|
|
168
|
+
index = -1;
|
|
169
|
+
ring;
|
|
170
|
+
register(item) {
|
|
171
|
+
if (!this.items.includes(item))
|
|
172
|
+
this.items.push(item);
|
|
173
|
+
if (this.index < 0 && item.focusable)
|
|
174
|
+
this.focus(this.items.indexOf(item));
|
|
175
|
+
}
|
|
176
|
+
unregister(item) {
|
|
177
|
+
const at = this.items.indexOf(item);
|
|
178
|
+
if (at < 0)
|
|
179
|
+
return;
|
|
180
|
+
this.items.splice(at, 1);
|
|
181
|
+
if (this.index >= this.items.length)
|
|
182
|
+
this.index = this.items.length - 1;
|
|
183
|
+
}
|
|
184
|
+
move(delta) {
|
|
185
|
+
if (this.items.length === 0)
|
|
186
|
+
return;
|
|
187
|
+
let next = this.index;
|
|
188
|
+
for (let count = 0;count < this.items.length; count += 1) {
|
|
189
|
+
next = (next + delta + this.items.length) % this.items.length;
|
|
190
|
+
if (this.items[next]?.focusable) {
|
|
191
|
+
this.focus(next);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
handleKey(key) {
|
|
197
|
+
const current = this.items[this.index];
|
|
198
|
+
if (current?.handleKey?.(key) === true)
|
|
199
|
+
return true;
|
|
200
|
+
if (key === "Tab" || key === "ArrowDown" || key === "ArrowRight") {
|
|
201
|
+
this.move(1);
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
if (key === "ArrowUp" || key === "ArrowLeft") {
|
|
205
|
+
this.move(-1);
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
if (key === "Enter" || key === " ") {
|
|
209
|
+
current?.activate();
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
focus(index) {
|
|
215
|
+
const item = this.items[index];
|
|
216
|
+
if (!item?.focusable)
|
|
217
|
+
return;
|
|
218
|
+
if (this.index >= 0) {
|
|
219
|
+
const previous = this.items[this.index];
|
|
220
|
+
if (previous)
|
|
221
|
+
previous.focused = false;
|
|
222
|
+
}
|
|
223
|
+
this.index = index;
|
|
224
|
+
item.focused = true;
|
|
225
|
+
this.drawRing(item);
|
|
226
|
+
}
|
|
227
|
+
focusFirst() {
|
|
228
|
+
const index = this.items.findIndex((item) => item.focusable);
|
|
229
|
+
if (index >= 0)
|
|
230
|
+
this.focus(index);
|
|
231
|
+
}
|
|
232
|
+
get current() {
|
|
233
|
+
return this.items[this.index];
|
|
234
|
+
}
|
|
235
|
+
drawRing(item) {
|
|
236
|
+
const parent = item.focusNode.parent;
|
|
237
|
+
if (!parent)
|
|
238
|
+
return;
|
|
239
|
+
this.ring?.removeFromParent();
|
|
240
|
+
const bounds = item.focusNode.getLocalBounds();
|
|
241
|
+
const ring = new Graphics().roundRect(item.focusNode.x + bounds.x * item.focusNode.scale.x - 3, item.focusNode.y + bounds.y * item.focusNode.scale.y - 3, bounds.width * item.focusNode.scale.x + 6, bounds.height * item.focusNode.scale.y + 6, 5).stroke({ color: 16777215, width: 2, alpha: 0.9 });
|
|
242
|
+
parent.addChild(ring);
|
|
243
|
+
this.ring = ring;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
class Widget extends Container2 {
|
|
248
|
+
focusable = true;
|
|
249
|
+
hasFocus = false;
|
|
250
|
+
hit;
|
|
251
|
+
theme;
|
|
252
|
+
enabled = true;
|
|
253
|
+
constructor(theme, width, height) {
|
|
254
|
+
super();
|
|
255
|
+
this.theme = theme;
|
|
256
|
+
this.eventMode = "static";
|
|
257
|
+
this.hitArea = new Rectangle(0, 0, width, height);
|
|
258
|
+
this.cursor = "pointer";
|
|
259
|
+
this.on("pointerdown", (event) => {
|
|
260
|
+
if ("preventDefault" in event.nativeEvent)
|
|
261
|
+
event.nativeEvent.preventDefault();
|
|
262
|
+
event.stopPropagation();
|
|
263
|
+
});
|
|
264
|
+
this.on("pointerup", (event) => {
|
|
265
|
+
if ("preventDefault" in event.nativeEvent)
|
|
266
|
+
event.nativeEvent.preventDefault();
|
|
267
|
+
event.stopPropagation();
|
|
268
|
+
});
|
|
269
|
+
this.hit = new Graphics().rect(0, 0, width, height).fill({ color: 16777215, alpha: 0.001 });
|
|
270
|
+
this.hit.eventMode = "static";
|
|
271
|
+
this.hit.cursor = "pointer";
|
|
272
|
+
this.addChild(this.hit);
|
|
273
|
+
this.hit.on("pointertap", () => this.activate());
|
|
274
|
+
this.on("pointerout", () => this.setHover(false));
|
|
275
|
+
}
|
|
276
|
+
get focused() {
|
|
277
|
+
return this.hasFocus;
|
|
278
|
+
}
|
|
279
|
+
set focused(value) {
|
|
280
|
+
this.hasFocus = value;
|
|
281
|
+
this.setHover(value);
|
|
282
|
+
}
|
|
283
|
+
get focusNode() {
|
|
284
|
+
return this;
|
|
285
|
+
}
|
|
286
|
+
setEnabled(enabled) {
|
|
287
|
+
this.enabled = enabled;
|
|
288
|
+
this.alpha = enabled ? 1 : 0.45;
|
|
289
|
+
this.eventMode = enabled ? "static" : "none";
|
|
290
|
+
this.hit.eventMode = enabled ? "static" : "none";
|
|
291
|
+
}
|
|
292
|
+
get isEnabled() {
|
|
293
|
+
return this.enabled;
|
|
294
|
+
}
|
|
295
|
+
setHover(_hovered) {}
|
|
296
|
+
setTheme(theme) {
|
|
297
|
+
this.theme = theme;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
// src/ui/widgets/button.ts
|
|
301
|
+
import { Graphics as Graphics2, Text as Text2 } from "pixi.js";
|
|
302
|
+
class Button extends Widget {
|
|
303
|
+
background;
|
|
304
|
+
caption;
|
|
305
|
+
onPress;
|
|
306
|
+
hovered = false;
|
|
307
|
+
constructor(text, theme, onPress, icon) {
|
|
308
|
+
super(theme, 240, 48);
|
|
309
|
+
this.onPress = onPress;
|
|
310
|
+
this.background = new Graphics2;
|
|
311
|
+
this.caption = new Text2({
|
|
312
|
+
text: icon ? `${icon} ${text}` : text,
|
|
313
|
+
style: { fontFamily: theme.font, fontSize: 20, fill: theme.text }
|
|
314
|
+
});
|
|
315
|
+
this.caption.position.set(14, 11);
|
|
316
|
+
this.addChild(this.background, this.caption);
|
|
317
|
+
this.draw(false);
|
|
318
|
+
}
|
|
319
|
+
activate() {
|
|
320
|
+
if (this.isEnabled)
|
|
321
|
+
this.onPress();
|
|
322
|
+
}
|
|
323
|
+
setHover(hovered) {
|
|
324
|
+
this.hovered = hovered;
|
|
325
|
+
this.draw(hovered);
|
|
326
|
+
}
|
|
327
|
+
setTheme(theme) {
|
|
328
|
+
super.setTheme(theme);
|
|
329
|
+
this.caption.style = { ...this.caption.style, fontFamily: theme.font, fill: theme.text };
|
|
330
|
+
this.draw(this.hovered);
|
|
331
|
+
}
|
|
332
|
+
draw(hovered) {
|
|
333
|
+
const color = hovered || this.focused ? this.theme.accent : this.theme.panel;
|
|
334
|
+
this.background.clear().roundRect(0, 0, 240, 48, 8).fill({ color, alpha: this.focused ? 0.95 : 0.85 });
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
// src/ui/widgets/controls.ts
|
|
338
|
+
import { Graphics as Graphics3, Text as Text3 } from "pixi.js";
|
|
339
|
+
class Slider extends Widget {
|
|
340
|
+
track;
|
|
341
|
+
knob;
|
|
342
|
+
onChange;
|
|
343
|
+
value;
|
|
344
|
+
dragging = false;
|
|
345
|
+
trackWidth;
|
|
346
|
+
constructor(value, theme, onChange, width = 260) {
|
|
347
|
+
super(theme, width, 36);
|
|
348
|
+
this.trackWidth = width;
|
|
349
|
+
this.value = clamp(value);
|
|
350
|
+
this.onChange = onChange;
|
|
351
|
+
this.track = new Graphics3;
|
|
352
|
+
this.knob = new Graphics3;
|
|
353
|
+
this.addChild(this.track, this.knob);
|
|
354
|
+
this.on("pointerdown", (event) => {
|
|
355
|
+
this.dragging = true;
|
|
356
|
+
this.setFromPointer(event.global.x);
|
|
357
|
+
});
|
|
358
|
+
this.on("globalpointermove", (event) => {
|
|
359
|
+
if (this.dragging)
|
|
360
|
+
this.setFromPointer(event.global.x);
|
|
361
|
+
});
|
|
362
|
+
this.on("pointerup", () => {
|
|
363
|
+
this.dragging = false;
|
|
364
|
+
});
|
|
365
|
+
this.on("pointerupoutside", () => {
|
|
366
|
+
this.dragging = false;
|
|
367
|
+
});
|
|
368
|
+
this.draw();
|
|
369
|
+
}
|
|
370
|
+
get current() {
|
|
371
|
+
return this.value;
|
|
372
|
+
}
|
|
373
|
+
setValue(value) {
|
|
374
|
+
this.value = clamp(value);
|
|
375
|
+
this.draw();
|
|
376
|
+
}
|
|
377
|
+
activate() {
|
|
378
|
+
this.onChange(this.value);
|
|
379
|
+
}
|
|
380
|
+
handleKey(key) {
|
|
381
|
+
const amount = 0.05;
|
|
382
|
+
if (key === "ArrowLeft" || key === "ArrowDown")
|
|
383
|
+
this.change(this.value - amount);
|
|
384
|
+
else if (key === "ArrowRight" || key === "ArrowUp")
|
|
385
|
+
this.change(this.value + amount);
|
|
386
|
+
else if (key === "Home")
|
|
387
|
+
this.change(0);
|
|
388
|
+
else if (key === "End")
|
|
389
|
+
this.change(1);
|
|
390
|
+
else
|
|
391
|
+
return false;
|
|
392
|
+
return true;
|
|
393
|
+
}
|
|
394
|
+
setFromPointer(x) {
|
|
395
|
+
this.change(this.toLocal({ x, y: this.getGlobalPosition().y }).x / this.trackWidth);
|
|
396
|
+
}
|
|
397
|
+
change(value) {
|
|
398
|
+
this.value = clamp(value);
|
|
399
|
+
this.draw();
|
|
400
|
+
this.onChange(this.value);
|
|
401
|
+
}
|
|
402
|
+
draw() {
|
|
403
|
+
this.track.clear().roundRect(0, 15, this.trackWidth, 6, 3).fill({ color: this.theme.panel, alpha: 0.9 });
|
|
404
|
+
this.track.roundRect(0, 15, this.trackWidth * this.value, 6, 3).fill({ color: this.theme.accent });
|
|
405
|
+
this.knob.clear().circle(this.trackWidth * this.value, 18, 10).fill({ color: this.theme.accent });
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
class Toggle extends Widget {
|
|
410
|
+
caption;
|
|
411
|
+
background;
|
|
412
|
+
onChange;
|
|
413
|
+
value;
|
|
414
|
+
constructor(label, value, theme, onChange) {
|
|
415
|
+
super(theme, 260, 44);
|
|
416
|
+
this.value = value;
|
|
417
|
+
this.onChange = onChange;
|
|
418
|
+
this.background = new Graphics3;
|
|
419
|
+
this.caption = new Text3({ text: label, style: { fontFamily: theme.font, fontSize: 19, fill: theme.text } });
|
|
420
|
+
this.caption.position.set(0, 10);
|
|
421
|
+
this.addChild(this.background, this.caption);
|
|
422
|
+
this.draw();
|
|
423
|
+
}
|
|
424
|
+
get current() {
|
|
425
|
+
return this.value;
|
|
426
|
+
}
|
|
427
|
+
setValue(value) {
|
|
428
|
+
this.value = value;
|
|
429
|
+
this.draw();
|
|
430
|
+
}
|
|
431
|
+
activate() {
|
|
432
|
+
if (!this.isEnabled)
|
|
433
|
+
return;
|
|
434
|
+
this.value = !this.value;
|
|
435
|
+
this.draw();
|
|
436
|
+
this.onChange(this.value);
|
|
437
|
+
}
|
|
438
|
+
draw() {
|
|
439
|
+
this.background.clear().roundRect(205, 8, 48, 26, 13).fill({ color: this.value ? this.theme.accent : this.theme.panel });
|
|
440
|
+
this.background.circle(this.value ? 240 : 218, 21, 8).fill({ color: this.theme.text });
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
class Segmented extends Widget {
|
|
445
|
+
options;
|
|
446
|
+
buttons;
|
|
447
|
+
onChange;
|
|
448
|
+
value;
|
|
449
|
+
background;
|
|
450
|
+
constructor(options, value, theme, onChange) {
|
|
451
|
+
super(theme, Math.max(options.length, 1) * 130, 44);
|
|
452
|
+
this.options = options;
|
|
453
|
+
this.value = value;
|
|
454
|
+
this.onChange = onChange;
|
|
455
|
+
this.background = new Graphics3;
|
|
456
|
+
this.addChild(this.background);
|
|
457
|
+
this.buttons = options.map((option, index) => {
|
|
458
|
+
const text = new Text3({ text: option, style: { fontFamily: theme.font, fontSize: 18, fill: theme.text } });
|
|
459
|
+
text.position.set(index * 130 + 12, 10);
|
|
460
|
+
text.eventMode = "static";
|
|
461
|
+
text.on("pointertap", (event) => {
|
|
462
|
+
event.stopPropagation();
|
|
463
|
+
this.select(option);
|
|
464
|
+
});
|
|
465
|
+
this.addChild(text);
|
|
466
|
+
return text;
|
|
467
|
+
});
|
|
468
|
+
this.draw();
|
|
469
|
+
}
|
|
470
|
+
get current() {
|
|
471
|
+
return this.value;
|
|
472
|
+
}
|
|
473
|
+
activate() {
|
|
474
|
+
const index = this.options.indexOf(this.value);
|
|
475
|
+
const next = this.options[(index + 1) % this.options.length];
|
|
476
|
+
if (next !== undefined)
|
|
477
|
+
this.select(next);
|
|
478
|
+
}
|
|
479
|
+
select(value) {
|
|
480
|
+
this.value = value;
|
|
481
|
+
this.draw();
|
|
482
|
+
this.onChange(value);
|
|
483
|
+
}
|
|
484
|
+
draw() {
|
|
485
|
+
this.background.clear();
|
|
486
|
+
this.options.forEach((option, index) => {
|
|
487
|
+
const selected = option === this.value;
|
|
488
|
+
this.background.roundRect(index * 130, 0, 130, 44, 5).fill({ color: selected ? this.theme.accent : this.theme.panel, alpha: 0.9 });
|
|
489
|
+
});
|
|
490
|
+
this.buttons.forEach((text, index) => {
|
|
491
|
+
text.style = { ...text.style, fill: this.theme.text, fontFamily: this.theme.font };
|
|
492
|
+
text.alpha = this.options[index] === this.value ? 1 : 0.75;
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
function clamp(value) {
|
|
497
|
+
return Math.max(0, Math.min(1, Number.isFinite(value) ? value : 0));
|
|
498
|
+
}
|
|
499
|
+
// src/ui/widgets/list.ts
|
|
500
|
+
import { Container as Container3, Graphics as Graphics4, Text as Text4 } from "pixi.js";
|
|
501
|
+
function visibleRange(total, rowHeight, viewportHeight, offset) {
|
|
502
|
+
const safeHeight = Math.max(rowHeight, 1);
|
|
503
|
+
const start = Math.min(Math.max(0, Math.floor(Math.max(0, offset) / safeHeight)), Math.max(0, total));
|
|
504
|
+
const end = Math.min(total, Math.max(start, Math.ceil((Math.max(0, offset) + Math.max(0, viewportHeight)) / safeHeight) + 1));
|
|
505
|
+
return { start, end };
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
class ScrollList extends Widget {
|
|
509
|
+
viewport;
|
|
510
|
+
content;
|
|
511
|
+
scrollbar;
|
|
512
|
+
rows = new Map;
|
|
513
|
+
values;
|
|
514
|
+
renderRow;
|
|
515
|
+
rowHeight;
|
|
516
|
+
viewportHeight;
|
|
517
|
+
offset = 0;
|
|
518
|
+
dragging = false;
|
|
519
|
+
dragStart = 0;
|
|
520
|
+
dragOffset = 0;
|
|
521
|
+
constructor(values, theme, renderRow, rowHeight = 52, viewportHeight = 360, width = 600) {
|
|
522
|
+
super(theme, width, viewportHeight);
|
|
523
|
+
this.values = values;
|
|
524
|
+
this.renderRow = renderRow;
|
|
525
|
+
this.rowHeight = rowHeight;
|
|
526
|
+
this.viewportHeight = viewportHeight;
|
|
527
|
+
this.viewport = new Graphics4().rect(0, 0, width, viewportHeight).fill({ color: 16777215, alpha: 0.001 });
|
|
528
|
+
this.viewport.eventMode = "static";
|
|
529
|
+
this.content = new Container3;
|
|
530
|
+
this.scrollbar = new Graphics4;
|
|
531
|
+
this.addChild(this.content, this.viewport, this.scrollbar);
|
|
532
|
+
this.viewport.on("wheel", (event) => {
|
|
533
|
+
this.scrollBy(event.deltaY);
|
|
534
|
+
});
|
|
535
|
+
this.viewport.on("pointerdown", (event) => {
|
|
536
|
+
this.dragging = true;
|
|
537
|
+
this.dragStart = event.global.y;
|
|
538
|
+
this.dragOffset = this.offset;
|
|
539
|
+
});
|
|
540
|
+
this.viewport.on("pointermove", (event) => {
|
|
541
|
+
if (this.dragging)
|
|
542
|
+
this.setOffset(this.dragOffset + this.dragStart - event.global.y);
|
|
543
|
+
});
|
|
544
|
+
this.viewport.on("pointerup", () => {
|
|
545
|
+
this.dragging = false;
|
|
546
|
+
});
|
|
547
|
+
this.viewport.on("pointerupoutside", () => {
|
|
548
|
+
this.dragging = false;
|
|
549
|
+
});
|
|
550
|
+
this.refresh();
|
|
551
|
+
}
|
|
552
|
+
get scrollOffset() {
|
|
553
|
+
return this.offset;
|
|
554
|
+
}
|
|
555
|
+
get maxOffset() {
|
|
556
|
+
return Math.max(0, this.values.length * this.rowHeight - this.viewportHeight);
|
|
557
|
+
}
|
|
558
|
+
scrollBy(delta) {
|
|
559
|
+
this.setOffset(this.offset + delta);
|
|
560
|
+
}
|
|
561
|
+
setOffset(offset) {
|
|
562
|
+
this.offset = Math.max(0, Math.min(this.maxOffset, offset));
|
|
563
|
+
this.refresh();
|
|
564
|
+
}
|
|
565
|
+
handleKey(key) {
|
|
566
|
+
if (key === "ArrowDown")
|
|
567
|
+
this.scrollBy(this.rowHeight);
|
|
568
|
+
else if (key === "ArrowUp")
|
|
569
|
+
this.scrollBy(-this.rowHeight);
|
|
570
|
+
else if (key === "PageDown")
|
|
571
|
+
this.scrollBy(this.viewportHeight);
|
|
572
|
+
else if (key === "PageUp")
|
|
573
|
+
this.scrollBy(-this.viewportHeight);
|
|
574
|
+
else if (key === "Home")
|
|
575
|
+
this.setOffset(0);
|
|
576
|
+
else if (key === "End")
|
|
577
|
+
this.setOffset(this.maxOffset);
|
|
578
|
+
else
|
|
579
|
+
return false;
|
|
580
|
+
return true;
|
|
581
|
+
}
|
|
582
|
+
activate() {}
|
|
583
|
+
refresh() {
|
|
584
|
+
const range = visibleRange(this.values.length, this.rowHeight, this.viewportHeight, this.offset);
|
|
585
|
+
for (const [index, row] of this.rows) {
|
|
586
|
+
if (index < range.start || index >= range.end) {
|
|
587
|
+
row.removeFromParent();
|
|
588
|
+
this.rows.delete(index);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
for (let index = range.start;index < range.end; index += 1) {
|
|
592
|
+
if (!this.rows.has(index)) {
|
|
593
|
+
const value = this.values[index];
|
|
594
|
+
if (value !== undefined) {
|
|
595
|
+
const row = this.renderRow(value, index);
|
|
596
|
+
row.position.y = index * this.rowHeight;
|
|
597
|
+
this.rows.set(index, row);
|
|
598
|
+
this.content.addChild(row);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
this.content.position.y = -this.offset;
|
|
603
|
+
const ratio = this.values.length === 0 ? 1 : this.viewportHeight / (this.values.length * this.rowHeight);
|
|
604
|
+
const thumbHeight = Math.max(24, this.viewportHeight * Math.min(1, ratio));
|
|
605
|
+
const thumbY = this.maxOffset === 0 ? 0 : this.offset / this.maxOffset * (this.viewportHeight - thumbHeight);
|
|
606
|
+
this.scrollbar.clear().roundRect(this.width - 10, thumbY, 8, thumbHeight, 4).fill({ color: this.theme.accent, alpha: 0.8 });
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
class Panel extends Graphics4 {
|
|
611
|
+
panelWidth;
|
|
612
|
+
panelHeight;
|
|
613
|
+
theme;
|
|
614
|
+
constructor(width, height, theme, radius = 14) {
|
|
615
|
+
super();
|
|
616
|
+
this.panelWidth = width;
|
|
617
|
+
this.panelHeight = height;
|
|
618
|
+
this.theme = theme;
|
|
619
|
+
this.draw(radius);
|
|
620
|
+
}
|
|
621
|
+
setTheme(theme) {
|
|
622
|
+
this.theme = theme;
|
|
623
|
+
this.draw(14);
|
|
624
|
+
}
|
|
625
|
+
draw(radius) {
|
|
626
|
+
this.clear().roundRect(0, 0, this.panelWidth, this.panelHeight, radius).fill({ color: this.theme.panel, alpha: this.theme.panelAlpha });
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
class Label extends Text4 {
|
|
631
|
+
constructor(text, theme, size = 22) {
|
|
632
|
+
super({ text, style: { fontFamily: theme.font, fontSize: size, fill: theme.text } });
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
// src/ui/screens/backlog.ts
|
|
636
|
+
import { Container as Container5, Text as Text6 } from "pixi.js";
|
|
637
|
+
|
|
638
|
+
// src/ui/screens/common.ts
|
|
639
|
+
import { Container as Container4, Graphics as Graphics5, Sprite, Text as Text5 } from "pixi.js";
|
|
640
|
+
class ScreenView extends Container4 {
|
|
641
|
+
focus = new FocusManager;
|
|
642
|
+
theme;
|
|
643
|
+
emitAction;
|
|
644
|
+
panel;
|
|
645
|
+
title;
|
|
646
|
+
panelWidth;
|
|
647
|
+
panelHeight;
|
|
648
|
+
constructor(heading, theme, emit, width = 900, height = 650) {
|
|
649
|
+
super();
|
|
650
|
+
this.theme = theme;
|
|
651
|
+
this.emitAction = emit;
|
|
652
|
+
this.panelWidth = width;
|
|
653
|
+
this.panelHeight = height;
|
|
654
|
+
this.panel = new Panel(width, height, theme);
|
|
655
|
+
this.title = new Text5({ text: heading, style: { fontFamily: theme.font, fontSize: 34, fill: theme.text } });
|
|
656
|
+
this.title.position.set(32, 24);
|
|
657
|
+
this.addChild(this.panel, this.title);
|
|
658
|
+
}
|
|
659
|
+
layout(design, full = false) {
|
|
660
|
+
if (full) {
|
|
661
|
+
this.panel.clear().rect(0, 0, design.width, design.height).fill({ color: 527128, alpha: 0.94 });
|
|
662
|
+
this.position.set(0, 0);
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
this.position.set((design.width - this.panelWidth) / 2, (design.height - this.panelHeight) / 2);
|
|
666
|
+
}
|
|
667
|
+
button(text, action, x, y, disabled = false) {
|
|
668
|
+
const button = new Button(text, this.theme, () => this.emitAction(action));
|
|
669
|
+
button.position.set(x, y);
|
|
670
|
+
button.setEnabled(!disabled);
|
|
671
|
+
this.addChild(button);
|
|
672
|
+
this.focus.register(button);
|
|
673
|
+
return button;
|
|
674
|
+
}
|
|
675
|
+
handleKey(key) {
|
|
676
|
+
return this.focus.handleKey(key);
|
|
677
|
+
}
|
|
678
|
+
clearContent() {
|
|
679
|
+
while (this.children.length > 2)
|
|
680
|
+
this.removeChildAt(2);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
class TitleScreen extends ScreenView {
|
|
685
|
+
constructor(model, theme, emit, gameTitle = "Lute", textureFor) {
|
|
686
|
+
super(gameTitle, theme, emit, 1280, 720);
|
|
687
|
+
if (model.background !== undefined) {
|
|
688
|
+
const texture = textureFor?.(model.background);
|
|
689
|
+
if (texture !== undefined) {
|
|
690
|
+
const background = new Sprite(texture);
|
|
691
|
+
background.width = 1280;
|
|
692
|
+
background.height = 720;
|
|
693
|
+
background.alpha = 0.95;
|
|
694
|
+
this.addChildAt(background, 1);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
const wash = new Graphics5().rect(0, 0, 1280, 720).fill({ color: 329492, alpha: 0.36 });
|
|
698
|
+
const gradient = new Graphics5().rect(0, 0, 1280, 720).fill({ color: 131850, alpha: 0.2 });
|
|
699
|
+
this.addChildAt(wash, 2);
|
|
700
|
+
this.addChildAt(gradient, 3);
|
|
701
|
+
this.title.position.set(84, 92);
|
|
702
|
+
this.title.style = { ...this.title.style, fontSize: 68, fontWeight: "700", dropShadow: { color: 0, alpha: 0.65, blur: 8, distance: 3 } };
|
|
703
|
+
this.button("New game", { type: "newGame" }, 84, 185);
|
|
704
|
+
this.button("Continue", { type: "continue" }, 84, 253, !model.canContinue);
|
|
705
|
+
this.button("Load", { type: "openLoad" }, 84, 321);
|
|
706
|
+
this.button("Settings", { type: "openSettings" }, 84, 389);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
class MenuScreen extends ScreenView {
|
|
711
|
+
constructor(theme, emit) {
|
|
712
|
+
super("Menu", theme, emit, 620, 500);
|
|
713
|
+
this.button("Resume", { type: "close" }, 50, 100);
|
|
714
|
+
this.button("Save", { type: "openSave" }, 50, 160);
|
|
715
|
+
this.button("Load", { type: "openLoad" }, 50, 220);
|
|
716
|
+
this.button("Settings", { type: "openSettings" }, 50, 280);
|
|
717
|
+
this.button("Title", { type: "toTitle" }, 50, 340);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
class ConfirmScreen extends ScreenView {
|
|
722
|
+
constructor(model, theme, emit) {
|
|
723
|
+
super("Confirm", theme, emit, 680, 320);
|
|
724
|
+
const message = new Text5({ text: model.message, style: { fontFamily: theme.font, fontSize: 22, fill: theme.text, wordWrap: true, wordWrapWidth: 600 } });
|
|
725
|
+
message.position.set(36, 90);
|
|
726
|
+
this.addChild(message);
|
|
727
|
+
this.button("Yes", { type: "confirm", yes: true }, 36, 220);
|
|
728
|
+
this.button("No", { type: "confirm", yes: false }, 300, 220);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
class EndedScreen extends ScreenView {
|
|
733
|
+
constructor(model, theme, emit) {
|
|
734
|
+
super("The End", theme, emit);
|
|
735
|
+
this.title.anchor.set(0.5);
|
|
736
|
+
this.title.position.set(this.panelWidth / 2, 72);
|
|
737
|
+
this.title.style = { ...this.title.style, fontSize: 56, fontWeight: "700", align: "center" };
|
|
738
|
+
const subtitle = model.reason === "terminal" ? "Thank you for playing" : model.reason;
|
|
739
|
+
const text = new Text5({ text: subtitle, style: { fontFamily: theme.font, fontSize: 26, fill: theme.text, align: "center", wordWrap: true, wordWrapWidth: 780 } });
|
|
740
|
+
text.anchor.set(0.5);
|
|
741
|
+
text.position.set(this.panelWidth / 2, 220);
|
|
742
|
+
this.addChild(text);
|
|
743
|
+
this.button("Back to title", { type: "toTitle" }, (this.panelWidth - 240) / 2, 510);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
class HaltedScreen extends ScreenView {
|
|
748
|
+
constructor(model, theme, emit) {
|
|
749
|
+
super("Playback halted", theme, emit);
|
|
750
|
+
this.title.anchor.set(0.5);
|
|
751
|
+
this.title.position.set(this.panelWidth / 2, 72);
|
|
752
|
+
this.title.style = { ...this.title.style, fontSize: 48, fontWeight: "700", align: "center" };
|
|
753
|
+
const text = new Text5({ text: model.message, style: { fontFamily: theme.font, fontSize: 24, fill: theme.text, align: "center", wordWrap: true, wordWrapWidth: 780 } });
|
|
754
|
+
text.anchor.set(0.5);
|
|
755
|
+
text.position.set(this.panelWidth / 2, 220);
|
|
756
|
+
this.addChild(text);
|
|
757
|
+
this.button("Back to title", { type: "toTitle" }, (this.panelWidth - 240) / 2, 510);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
class LoadingScreen extends ScreenView {
|
|
762
|
+
constructor(model, theme, emit) {
|
|
763
|
+
super("Loading", theme, emit);
|
|
764
|
+
const bar = new Graphics5().roundRect(45, 220, 810, 26, 13).fill({ color: theme.panel });
|
|
765
|
+
bar.roundRect(45, 220, 810 * Math.max(0, Math.min(1, model.progress)), 26, 13).fill({ color: theme.accent });
|
|
766
|
+
this.addChild(bar);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
class IdleScreen extends ScreenView {
|
|
771
|
+
constructor(model, theme, emit) {
|
|
772
|
+
super("Continue", theme, emit, 620, 420);
|
|
773
|
+
model.options.forEach((option, index) => {
|
|
774
|
+
const x = 190 + index % 2 * 270;
|
|
775
|
+
const y = 130 + Math.floor(index / 2) * 60;
|
|
776
|
+
this.button(option.label, { type: "pickIdle", index }, x, y);
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// src/ui/screens/backlog.ts
|
|
782
|
+
class BacklogScreen extends ScreenView {
|
|
783
|
+
list;
|
|
784
|
+
constructor(model, theme, emit) {
|
|
785
|
+
super("Backlog", theme, emit, 1000, 660);
|
|
786
|
+
this.list = new ScrollList(model.entries, theme, (entry) => this.row(entry, theme, emit), 72, 520, 900);
|
|
787
|
+
this.list.position.set(36, 90);
|
|
788
|
+
this.addChild(this.list);
|
|
789
|
+
this.list.setOffset(this.list.maxOffset);
|
|
790
|
+
this.button("Close", { type: "close" }, 36, 600);
|
|
791
|
+
}
|
|
792
|
+
handleKey(key) {
|
|
793
|
+
return this.list.handleKey(key) || super.handleKey(key);
|
|
794
|
+
}
|
|
795
|
+
row(entry, theme, emit) {
|
|
796
|
+
const row = new Container5;
|
|
797
|
+
const name = new Label(entry.speakerName, theme, 18);
|
|
798
|
+
const text = new Text6({ text: entry.text, style: { fontFamily: theme.font, fontSize: 18, fill: theme.text, wordWrap: true, wordWrapWidth: entry.hasVoice ? 690 : 810 } });
|
|
799
|
+
text.position.set(0, 25);
|
|
800
|
+
row.addChild(name, text);
|
|
801
|
+
if (entry.hasVoice) {
|
|
802
|
+
const voice = new Button("Voice", theme, () => emit({ type: "replayVoice", lineId: entry.lineId }));
|
|
803
|
+
voice.position.set(720, 14);
|
|
804
|
+
row.addChild(voice);
|
|
805
|
+
}
|
|
806
|
+
return row;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
class HudScreen extends Container5 {
|
|
811
|
+
static buttonWidth = 101;
|
|
812
|
+
static gap = 4;
|
|
813
|
+
static totalWidth = 9 * HudScreen.buttonWidth + 8 * HudScreen.gap;
|
|
814
|
+
constructor(theme, emit, state) {
|
|
815
|
+
super();
|
|
816
|
+
const actions = [
|
|
817
|
+
{ label: state.auto ? "Auto: on" : "Auto", action: { type: "toggleAuto" } },
|
|
818
|
+
{ label: state.skip ? "Skip: on" : "Skip", action: { type: "toggleSkip" } },
|
|
819
|
+
{ label: "Log", action: { type: "openBacklog" } },
|
|
820
|
+
{ label: "Save", action: { type: "openSave" } },
|
|
821
|
+
{ label: "Load", action: { type: "openLoad" } },
|
|
822
|
+
{ label: "Q.Save", action: { type: "quickSave" } },
|
|
823
|
+
{ label: "Q.Load", action: { type: "quickLoad" } },
|
|
824
|
+
{ label: "Config", action: { type: "openSettings" } },
|
|
825
|
+
{ label: state.hidden ? "Show" : "Hide", action: { type: "hideUI" } }
|
|
826
|
+
];
|
|
827
|
+
actions.forEach((item, index) => {
|
|
828
|
+
const button = new Button(item.label, theme, () => emit(item.action));
|
|
829
|
+
button.scale.set(HudScreen.buttonWidth / 240);
|
|
830
|
+
button.position.set(index * (HudScreen.buttonWidth + HudScreen.gap), 0);
|
|
831
|
+
this.addChild(button);
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
// src/ui/screens/save-load.ts
|
|
837
|
+
import { Sprite as Sprite2, Text as Text7, Texture as Texture2 } from "pixi.js";
|
|
838
|
+
var SLOTS_PER_PAGE = 6;
|
|
839
|
+
function pageSlots(slots, page) {
|
|
840
|
+
const offset = Math.max(0, page) * SLOTS_PER_PAGE;
|
|
841
|
+
return Array.from({ length: SLOTS_PER_PAGE }, (_, index) => slots[offset + index]);
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
class SaveLoadScreen extends ScreenView {
|
|
845
|
+
constructor(model, theme, emit) {
|
|
846
|
+
super(model.mode === "save" ? "Save game" : "Load game", theme, emit, 1000, 650);
|
|
847
|
+
pageSlots(model.slots, model.page).forEach((slot, index) => {
|
|
848
|
+
const x = 36 + index % 2 * 460;
|
|
849
|
+
const y = 90 + Math.floor(index / 2) * 130;
|
|
850
|
+
this.slot(slot, model.mode, theme, emit, x, y, model.page * SLOTS_PER_PAGE + index);
|
|
851
|
+
});
|
|
852
|
+
if (model.page > 0)
|
|
853
|
+
this.button("Previous", { type: "choose", option: `page:${model.page - 1}` }, 36, 595);
|
|
854
|
+
if ((model.page + 1) * SLOTS_PER_PAGE < model.slots.length)
|
|
855
|
+
this.button("Next", { type: "choose", option: `page:${model.page + 1}` }, 210, 595);
|
|
856
|
+
this.button("Close", { type: "close" }, 770, 595);
|
|
857
|
+
}
|
|
858
|
+
slot(slot, mode, theme, emit, x, y, emptyIndex) {
|
|
859
|
+
const panel = new Panel(420, 130, theme, 8);
|
|
860
|
+
panel.position.set(x, y);
|
|
861
|
+
this.addChild(panel);
|
|
862
|
+
if (slot?.thumbnail !== undefined) {
|
|
863
|
+
const image = new Sprite2(Texture2.from(slot.thumbnail));
|
|
864
|
+
image.width = 110;
|
|
865
|
+
image.height = 80;
|
|
866
|
+
image.position.set(x + 10, y + 10);
|
|
867
|
+
this.addChild(image);
|
|
868
|
+
}
|
|
869
|
+
const label = new Text7({ text: slot?.label ?? "Empty slot", style: { fontFamily: theme.font, fontSize: 18, fill: theme.text } });
|
|
870
|
+
label.position.set(x + 135, y + 20);
|
|
871
|
+
this.addChild(label);
|
|
872
|
+
const date = new Text7({ text: slot?.savedAt ?? "", style: { fontFamily: theme.font, fontSize: 14, fill: theme.text } });
|
|
873
|
+
date.position.set(x + 135, y + 48);
|
|
874
|
+
this.addChild(date);
|
|
875
|
+
if (slot !== undefined) {
|
|
876
|
+
const action = mode === "save" ? { type: "saveSlot", slot: slot.slot } : { type: "loadSlot", slot: slot.slot };
|
|
877
|
+
const button = new Button(mode === "save" ? "Save" : "Load", theme, () => emit(action));
|
|
878
|
+
button.scale.set(0.6);
|
|
879
|
+
button.position.set(x + 270, y + 72);
|
|
880
|
+
this.addChild(button);
|
|
881
|
+
this.focus.register(button);
|
|
882
|
+
if (mode === "save") {
|
|
883
|
+
const remove = new Button("Delete", theme, () => emit({ type: "deleteSlot", slot: slot.slot }));
|
|
884
|
+
remove.scale.set(0.6);
|
|
885
|
+
remove.position.set(x + 330, y + 72);
|
|
886
|
+
this.addChild(remove);
|
|
887
|
+
this.focus.register(remove);
|
|
888
|
+
}
|
|
889
|
+
} else if (mode === "save") {
|
|
890
|
+
const button = new Button("Save", theme, () => emit({ type: "saveSlot", slot: emptyIndex }));
|
|
891
|
+
button.scale.set(0.6);
|
|
892
|
+
button.position.set(x + 270, y + 72);
|
|
893
|
+
this.addChild(button);
|
|
894
|
+
this.focus.register(button);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
// src/ui/screens/settings.ts
|
|
900
|
+
import { Text as Text8 } from "pixi.js";
|
|
901
|
+
class SettingsScreen extends ScreenView {
|
|
902
|
+
settings;
|
|
903
|
+
sample;
|
|
904
|
+
constructor(model, theme, emit) {
|
|
905
|
+
super("Settings", theme, emit, 1100, 680);
|
|
906
|
+
this.settings = model.settings;
|
|
907
|
+
this.sample = new Text8({ text: "The quick brown fox jumps over the lazy dog.", style: { fontFamily: theme.font, fontSize: 20, fill: theme.text } });
|
|
908
|
+
this.sample.position.set(40, 76);
|
|
909
|
+
this.addChild(this.sample);
|
|
910
|
+
this.row("Text speed", 125, new Slider(normalizeSpeed(model.settings.textSpeed), theme, (value) => {
|
|
911
|
+
const textSpeed = value === 0 ? 0 : Math.round(10 + value * 190);
|
|
912
|
+
emit({ type: "setSettings", settings: { textSpeed } });
|
|
913
|
+
this.sample.text = textSpeed === 0 ? "Instant text" : "The quick brown fox jumps over the lazy dog.";
|
|
914
|
+
}));
|
|
915
|
+
this.row("Auto delay", 180, new Slider(normalizeDelay(model.settings.autoDelay), theme, (value) => emit({ type: "setSettings", settings: { autoDelay: Math.round(value * 20) } })));
|
|
916
|
+
this.row("Skip mode", 235, new Segmented(["read", "all"], model.settings.skipMode, theme, (skipMode) => emit({ type: "setSettings", settings: { skipMode } })));
|
|
917
|
+
this.row("Master volume", 290, this.volumeSlider(model.settings.volumes.master, theme, emit, "master"));
|
|
918
|
+
this.row("Music volume", 345, this.volumeSlider(model.settings.volumes.music, theme, emit, "music"));
|
|
919
|
+
this.row("SFX volume", 400, this.volumeSlider(model.settings.volumes.sfx, theme, emit, "sfx"));
|
|
920
|
+
this.row("Voice volume", 455, this.volumeSlider(model.settings.volumes.voice, theme, emit, "voice"));
|
|
921
|
+
this.row("Window opacity", 510, new Slider(model.settings.windowOpacity, theme, (windowOpacity) => emit({ type: "setSettings", settings: { windowOpacity } })));
|
|
922
|
+
this.row("Read text colour", 565, new Toggle("Enabled", model.settings.readColour, theme, (readColour) => emit({ type: "setSettings", settings: { readColour } })));
|
|
923
|
+
this.row("Locale", 620, new Segmented(model.locales, model.settings.locale, theme, (locale) => emit({ type: "setSettings", settings: { locale } })));
|
|
924
|
+
this.button("Close", { type: "close" }, 850, 620);
|
|
925
|
+
}
|
|
926
|
+
row(name, y, control) {
|
|
927
|
+
const label = new Label(name, this.theme, 18);
|
|
928
|
+
label.position.set(40, y + 7);
|
|
929
|
+
control.position.set(250, y);
|
|
930
|
+
this.addChild(label, control);
|
|
931
|
+
this.focus.register(control);
|
|
932
|
+
}
|
|
933
|
+
volumeSlider(value, theme, emit, channel) {
|
|
934
|
+
return new Slider(value, theme, (next) => emit({ type: "setSettings", settings: { volumes: { ...this.settings.volumes, [channel]: next } } }));
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
function normalizeSpeed(speed) {
|
|
938
|
+
if (speed <= 0)
|
|
939
|
+
return 0;
|
|
940
|
+
return Math.max(0, Math.min(1, (speed - 10) / 190));
|
|
941
|
+
}
|
|
942
|
+
function normalizeDelay(delay) {
|
|
943
|
+
return Math.max(0, Math.min(1, delay / 20));
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
// src/ui/ui.ts
|
|
947
|
+
var UiTag = Context3.Service("Lute.Web.Ui");
|
|
948
|
+
|
|
949
|
+
class Ui {
|
|
950
|
+
actions;
|
|
951
|
+
root;
|
|
952
|
+
theme;
|
|
953
|
+
textureFor;
|
|
954
|
+
current;
|
|
955
|
+
base;
|
|
956
|
+
overlay;
|
|
957
|
+
hud;
|
|
958
|
+
hudState = "";
|
|
959
|
+
design = { width: 1280, height: 720 };
|
|
960
|
+
titleName = "Lute";
|
|
961
|
+
push = () => {};
|
|
962
|
+
constructor(root, theme, _world, textureFor) {
|
|
963
|
+
this.root = root;
|
|
964
|
+
this.theme = theme;
|
|
965
|
+
this.textureFor = textureFor;
|
|
966
|
+
this.actions = Stream3.callback((queue) => Effect3.sync(() => {
|
|
967
|
+
this.push = (action) => {
|
|
968
|
+
Queue.offerUnsafe(queue, action);
|
|
969
|
+
};
|
|
970
|
+
}));
|
|
971
|
+
}
|
|
972
|
+
show(screen) {
|
|
973
|
+
return Effect3.sync(() => {
|
|
974
|
+
const modal = ["Menu", "Backlog", "SaveLoad", "Settings", "Confirm"].includes(screen._tag);
|
|
975
|
+
if (this.overlay) {
|
|
976
|
+
this.overlay.removeFromParent();
|
|
977
|
+
this.overlay.visible = false;
|
|
978
|
+
this.overlay = undefined;
|
|
979
|
+
}
|
|
980
|
+
const next = this.build(screen);
|
|
981
|
+
if (modal) {
|
|
982
|
+
const overlay = new Container6;
|
|
983
|
+
const dim = new Graphics6().rect(0, 0, this.design.width, this.design.height).fill({ color: 0, alpha: 0.6 });
|
|
984
|
+
dim.eventMode = "static";
|
|
985
|
+
overlay.eventMode = "static";
|
|
986
|
+
overlay.on("pointerdown", (event) => {
|
|
987
|
+
if ("preventDefault" in event.nativeEvent)
|
|
988
|
+
event.nativeEvent.preventDefault();
|
|
989
|
+
if ("stopImmediatePropagation" in event.nativeEvent)
|
|
990
|
+
event.nativeEvent.stopImmediatePropagation();
|
|
991
|
+
event.stopPropagation();
|
|
992
|
+
});
|
|
993
|
+
overlay.on("pointerup", (event) => {
|
|
994
|
+
if ("preventDefault" in event.nativeEvent)
|
|
995
|
+
event.nativeEvent.preventDefault();
|
|
996
|
+
if ("stopImmediatePropagation" in event.nativeEvent)
|
|
997
|
+
event.nativeEvent.stopImmediatePropagation();
|
|
998
|
+
event.stopPropagation();
|
|
999
|
+
if (event.button === 2)
|
|
1000
|
+
this.push({ type: "close" });
|
|
1001
|
+
});
|
|
1002
|
+
next.layout(this.design, false);
|
|
1003
|
+
overlay.addChild(dim, next);
|
|
1004
|
+
this.root.addChild(overlay);
|
|
1005
|
+
this.overlay = overlay;
|
|
1006
|
+
} else {
|
|
1007
|
+
if (this.base) {
|
|
1008
|
+
this.base.removeFromParent();
|
|
1009
|
+
this.base.visible = false;
|
|
1010
|
+
}
|
|
1011
|
+
next.layout(this.design, true);
|
|
1012
|
+
this.root.addChild(next);
|
|
1013
|
+
this.base = next;
|
|
1014
|
+
}
|
|
1015
|
+
this.current = next;
|
|
1016
|
+
if (this.hud)
|
|
1017
|
+
this.hud.visible = !this.current;
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
1020
|
+
hide = Effect3.sync(() => {
|
|
1021
|
+
if (this.overlay) {
|
|
1022
|
+
this.overlay.removeFromParent();
|
|
1023
|
+
this.overlay.visible = false;
|
|
1024
|
+
this.overlay = undefined;
|
|
1025
|
+
this.current = this.base;
|
|
1026
|
+
} else {
|
|
1027
|
+
if (this.base) {
|
|
1028
|
+
this.base.removeFromParent();
|
|
1029
|
+
this.base.visible = false;
|
|
1030
|
+
}
|
|
1031
|
+
this.base = undefined;
|
|
1032
|
+
this.current = undefined;
|
|
1033
|
+
}
|
|
1034
|
+
if (this.hud)
|
|
1035
|
+
this.hud.visible = !this.current;
|
|
1036
|
+
});
|
|
1037
|
+
setTheme(theme) {
|
|
1038
|
+
this.theme = theme;
|
|
1039
|
+
}
|
|
1040
|
+
setDesign(design, title = "Lute") {
|
|
1041
|
+
this.design = { width: Math.max(1, design.width), height: Math.max(1, design.height) };
|
|
1042
|
+
this.titleName = title;
|
|
1043
|
+
}
|
|
1044
|
+
resize(viewWidth, viewHeight) {
|
|
1045
|
+
const scale = Math.min(viewWidth / this.design.width, viewHeight / this.design.height);
|
|
1046
|
+
this.root.scale.set(Math.max(0, scale));
|
|
1047
|
+
this.root.position.set((viewWidth - this.design.width * scale) / 2, (viewHeight - this.design.height * scale) / 2);
|
|
1048
|
+
}
|
|
1049
|
+
showHud(state) {
|
|
1050
|
+
if (this.hud) {
|
|
1051
|
+
this.hud.removeFromParent();
|
|
1052
|
+
this.hud.visible = false;
|
|
1053
|
+
}
|
|
1054
|
+
this.hud = new HudScreen(this.theme, this.push, state);
|
|
1055
|
+
this.hud.position.set(Math.max(16, this.design.width - HudScreen.totalWidth - 20), this.design.height - 32);
|
|
1056
|
+
this.root.addChild(this.hud);
|
|
1057
|
+
}
|
|
1058
|
+
setPlaying(playing, state) {
|
|
1059
|
+
if (playing && this.base) {
|
|
1060
|
+
this.base.removeFromParent();
|
|
1061
|
+
this.base.visible = false;
|
|
1062
|
+
if (this.current === this.base)
|
|
1063
|
+
this.current = undefined;
|
|
1064
|
+
this.base = undefined;
|
|
1065
|
+
}
|
|
1066
|
+
const key = `${state.auto}:${state.skip}:${state.hidden}`;
|
|
1067
|
+
if (playing && key !== this.hudState) {
|
|
1068
|
+
this.showHud(state);
|
|
1069
|
+
this.hudState = key;
|
|
1070
|
+
}
|
|
1071
|
+
if (this.hud)
|
|
1072
|
+
this.hud.visible = playing && !state.hidden && !this.current;
|
|
1073
|
+
}
|
|
1074
|
+
get hasScreen() {
|
|
1075
|
+
return this.current !== undefined;
|
|
1076
|
+
}
|
|
1077
|
+
pointerAction(clientX, clientY) {
|
|
1078
|
+
if (!this.current)
|
|
1079
|
+
return false;
|
|
1080
|
+
const visit = (node) => {
|
|
1081
|
+
const bounds = node.getBounds();
|
|
1082
|
+
if (node instanceof Button && node.isEnabled && clientX >= bounds.x && clientX <= bounds.x + bounds.width && clientY >= bounds.y && clientY <= bounds.y + bounds.height) {
|
|
1083
|
+
node.activate();
|
|
1084
|
+
return true;
|
|
1085
|
+
}
|
|
1086
|
+
return node.children.some((child) => child instanceof Container6 && visit(child));
|
|
1087
|
+
};
|
|
1088
|
+
return visit(this.current);
|
|
1089
|
+
}
|
|
1090
|
+
handleKey(key) {
|
|
1091
|
+
if (this.overlay && key === "Escape") {
|
|
1092
|
+
this.push({ type: "close" });
|
|
1093
|
+
return true;
|
|
1094
|
+
}
|
|
1095
|
+
return this.current?.handleKey(key) ?? false;
|
|
1096
|
+
}
|
|
1097
|
+
disposeLater(node) {
|
|
1098
|
+
node.removeFromParent();
|
|
1099
|
+
node.visible = false;
|
|
1100
|
+
}
|
|
1101
|
+
build(screen) {
|
|
1102
|
+
const emit = (action) => this.push(action);
|
|
1103
|
+
switch (screen._tag) {
|
|
1104
|
+
case "Title":
|
|
1105
|
+
return new TitleScreen(screen, this.theme, emit, this.titleName, this.textureFor);
|
|
1106
|
+
case "Backlog":
|
|
1107
|
+
return new BacklogScreen(screen, this.theme, emit);
|
|
1108
|
+
case "SaveLoad":
|
|
1109
|
+
return new SaveLoadScreen(screen, this.theme, emit);
|
|
1110
|
+
case "Settings":
|
|
1111
|
+
return new SettingsScreen(screen, this.theme, emit);
|
|
1112
|
+
case "Confirm":
|
|
1113
|
+
return new ConfirmScreen(screen, this.theme, emit);
|
|
1114
|
+
case "Loading":
|
|
1115
|
+
return new LoadingScreen(screen, this.theme, emit);
|
|
1116
|
+
case "Halted":
|
|
1117
|
+
return new HaltedScreen(screen, this.theme, emit);
|
|
1118
|
+
case "Idle":
|
|
1119
|
+
return new IdleScreen(screen, this.theme, emit);
|
|
1120
|
+
case "Menu":
|
|
1121
|
+
return new MenuScreen(this.theme, emit);
|
|
1122
|
+
case "Ended":
|
|
1123
|
+
return new EndedScreen(screen, this.theme, emit);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
// src/assets/pixiAssets.ts
|
|
1129
|
+
import { Effect as Effect4, Fiber, Stream as Stream4, SubscriptionRef } from "effect";
|
|
1130
|
+
import { AssetMissing } from "@lute-lang/player";
|
|
1131
|
+
var AUDIO_EXTENSIONS = /\.(?:aac|flac|m4a|mp3|ogg|wav|webm)(?:[?#].*)?$/i;
|
|
1132
|
+
var VIDEO_EXTENSIONS = /\.(?:avi|m4v|mov|mp4|webm)(?:[?#].*)?$/i;
|
|
1133
|
+
var uniqueFiles = (files) => {
|
|
1134
|
+
const result = [];
|
|
1135
|
+
const seen = new Set;
|
|
1136
|
+
for (const file of files) {
|
|
1137
|
+
if (file.length > 0 && !seen.has(file)) {
|
|
1138
|
+
seen.add(file);
|
|
1139
|
+
result.push(file);
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
return result;
|
|
1143
|
+
};
|
|
1144
|
+
var pathKind = (file) => {
|
|
1145
|
+
if (VIDEO_EXTENSIONS.test(file))
|
|
1146
|
+
return "video";
|
|
1147
|
+
if (AUDIO_EXTENSIONS.test(file))
|
|
1148
|
+
return "audio";
|
|
1149
|
+
return "texture";
|
|
1150
|
+
};
|
|
1151
|
+
var resolveAssetUrl = (file, base) => {
|
|
1152
|
+
try {
|
|
1153
|
+
return new URL(file, base).href;
|
|
1154
|
+
} catch {
|
|
1155
|
+
const origin = typeof globalThis.location === "object" ? globalThis.location.href : "http://localhost/";
|
|
1156
|
+
return new URL(file, new URL(base, origin)).href;
|
|
1157
|
+
}
|
|
1158
|
+
};
|
|
1159
|
+
|
|
1160
|
+
class PixiAssets {
|
|
1161
|
+
progress;
|
|
1162
|
+
backend;
|
|
1163
|
+
base;
|
|
1164
|
+
maxConcurrency;
|
|
1165
|
+
graceSeconds;
|
|
1166
|
+
progressRef;
|
|
1167
|
+
records = new Map;
|
|
1168
|
+
unloadFibers = new Map;
|
|
1169
|
+
constructor(options) {
|
|
1170
|
+
const base = options.assetsBase ?? "assets/";
|
|
1171
|
+
this.base = base;
|
|
1172
|
+
this.backend = options.backend;
|
|
1173
|
+
this.maxConcurrency = Math.max(1, Math.floor(options.concurrency ?? 4));
|
|
1174
|
+
this.graceSeconds = Math.max(0, options.graceSeconds ?? 0.25);
|
|
1175
|
+
this.progressRef = Effect4.runSync(SubscriptionRef.make(0));
|
|
1176
|
+
this.progress = Stream4.concat(Stream4.succeed(0), SubscriptionRef.changes(this.progressRef));
|
|
1177
|
+
}
|
|
1178
|
+
ensure = (files) => {
|
|
1179
|
+
const requested = uniqueFiles(files);
|
|
1180
|
+
return Effect4.gen({ self: this }, function* () {
|
|
1181
|
+
const self = this;
|
|
1182
|
+
yield* Effect4.addFinalizer(() => self.release(requested));
|
|
1183
|
+
yield* SubscriptionRef.set(self.progressRef, requested.length === 0 ? 1 : 0);
|
|
1184
|
+
let completed = 0;
|
|
1185
|
+
yield* Effect4.forEach(requested, (file) => self.acquire(file).pipe(Effect4.tap(() => Effect4.sync(() => {
|
|
1186
|
+
completed += 1;
|
|
1187
|
+
})), Effect4.tap(() => SubscriptionRef.set(self.progressRef, requested.length === 0 ? 1 : completed / requested.length))), { concurrency: self.maxConcurrency, discard: true });
|
|
1188
|
+
yield* SubscriptionRef.set(self.progressRef, 1);
|
|
1189
|
+
});
|
|
1190
|
+
};
|
|
1191
|
+
texture = (file) => {
|
|
1192
|
+
const record = this.records.get(file);
|
|
1193
|
+
return record?.kind === "texture" || record?.kind === "video" ? record.value : undefined;
|
|
1194
|
+
};
|
|
1195
|
+
video = (file) => {
|
|
1196
|
+
const record = this.records.get(file);
|
|
1197
|
+
return record?.kind === "video" ? record.value : undefined;
|
|
1198
|
+
};
|
|
1199
|
+
acquire = (file) => Effect4.gen({ self: this }, function* () {
|
|
1200
|
+
const self = this;
|
|
1201
|
+
const pending = self.unloadFibers.get(file);
|
|
1202
|
+
if (pending !== undefined) {
|
|
1203
|
+
yield* Fiber.interrupt(pending);
|
|
1204
|
+
self.unloadFibers.delete(file);
|
|
1205
|
+
}
|
|
1206
|
+
const existing = self.records.get(file);
|
|
1207
|
+
if (existing !== undefined) {
|
|
1208
|
+
existing.refs += 1;
|
|
1209
|
+
return;
|
|
1210
|
+
}
|
|
1211
|
+
const url = resolveAssetUrl(file, self.base);
|
|
1212
|
+
const kind = pathKind(file);
|
|
1213
|
+
const value = kind === "audio" ? undefined : yield* self.backend.loadTexture(url).pipe(Effect4.mapError((error) => new AssetMissing({
|
|
1214
|
+
file,
|
|
1215
|
+
message: error instanceof Error ? error.message : String(error)
|
|
1216
|
+
})));
|
|
1217
|
+
yield* kind === "audio" ? self.backend.loadAudio(url).pipe(Effect4.mapError((error) => new AssetMissing({
|
|
1218
|
+
file,
|
|
1219
|
+
message: error instanceof Error ? error.message : String(error)
|
|
1220
|
+
}))) : Effect4.void;
|
|
1221
|
+
self.records.set(file, { file, url, kind, value, refs: 1 });
|
|
1222
|
+
});
|
|
1223
|
+
release = (files) => Effect4.gen({ self: this }, function* () {
|
|
1224
|
+
const self = this;
|
|
1225
|
+
for (const file of files) {
|
|
1226
|
+
const record = self.records.get(file);
|
|
1227
|
+
if (record === undefined)
|
|
1228
|
+
continue;
|
|
1229
|
+
record.refs -= 1;
|
|
1230
|
+
if (record.refs > 0)
|
|
1231
|
+
continue;
|
|
1232
|
+
if (self.graceSeconds === 0) {
|
|
1233
|
+
yield* self.unload(record);
|
|
1234
|
+
continue;
|
|
1235
|
+
}
|
|
1236
|
+
const fiber = yield* Effect4.forkDetach(Effect4.sleep(self.graceSeconds).pipe(Effect4.flatMap(() => self.unload(record))));
|
|
1237
|
+
self.unloadFibers.set(file, fiber);
|
|
1238
|
+
}
|
|
1239
|
+
});
|
|
1240
|
+
unload = (record) => Effect4.gen({ self: this }, function* () {
|
|
1241
|
+
const self = this;
|
|
1242
|
+
const current = self.records.get(record.file);
|
|
1243
|
+
if (current !== record || record.refs > 0)
|
|
1244
|
+
return;
|
|
1245
|
+
self.records.delete(record.file);
|
|
1246
|
+
self.unloadFibers.delete(record.file);
|
|
1247
|
+
if (record.kind === "audio") {
|
|
1248
|
+
yield* self.backend.unloadAudio(record.url);
|
|
1249
|
+
} else if (record.value !== undefined) {
|
|
1250
|
+
yield* self.backend.unloadTexture(record.url, record.value);
|
|
1251
|
+
}
|
|
1252
|
+
});
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
// src/assets/pixiAssetsBackend.ts
|
|
1256
|
+
import { Assets } from "pixi.js";
|
|
1257
|
+
import { Effect as Effect5 } from "effect";
|
|
1258
|
+
import { sound } from "@pixi/sound";
|
|
1259
|
+
var pixiAssetsBackend = {
|
|
1260
|
+
loadTexture: (url) => Effect5.tryPromise(() => Assets.load(url)),
|
|
1261
|
+
unloadTexture: (_url, _texture) => Effect5.void,
|
|
1262
|
+
loadAudio: (url) => Effect5.callback((resume) => {
|
|
1263
|
+
if (sound.exists(url)) {
|
|
1264
|
+
resume(Effect5.succeed(undefined));
|
|
1265
|
+
return;
|
|
1266
|
+
}
|
|
1267
|
+
try {
|
|
1268
|
+
sound.add(url, {
|
|
1269
|
+
url,
|
|
1270
|
+
preload: true,
|
|
1271
|
+
loaded: (error) => resume(error == null ? Effect5.succeed(undefined) : Effect5.fail(error))
|
|
1272
|
+
});
|
|
1273
|
+
} catch (error) {
|
|
1274
|
+
resume(Effect5.fail(error));
|
|
1275
|
+
}
|
|
1276
|
+
}),
|
|
1277
|
+
unloadAudio: (url) => Effect5.sync(() => {
|
|
1278
|
+
if (sound.exists(url, false))
|
|
1279
|
+
sound.remove(url);
|
|
1280
|
+
})
|
|
1281
|
+
};
|
|
1282
|
+
|
|
1283
|
+
// src/audio/pixiAudio.ts
|
|
1284
|
+
import { Deferred as Deferred2, Effect as Effect6, Fiber as Fiber2, Option, SubscriptionRef as SubscriptionRef2 } from "effect";
|
|
1285
|
+
|
|
1286
|
+
class AudioDecodeError extends Error {
|
|
1287
|
+
file;
|
|
1288
|
+
_tag = "AudioDecodeError";
|
|
1289
|
+
constructor(file, cause) {
|
|
1290
|
+
super(`Unable to decode or play audio ${file}: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
1291
|
+
this.file = file;
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
var clamp2 = (value) => Math.max(0, Math.min(1, value));
|
|
1295
|
+
var positive = (value) => Number.isFinite(value) && value > 0 ? value : 0;
|
|
1296
|
+
|
|
1297
|
+
class PixiAudio {
|
|
1298
|
+
unlocked;
|
|
1299
|
+
unlock;
|
|
1300
|
+
backend;
|
|
1301
|
+
logger;
|
|
1302
|
+
active = {
|
|
1303
|
+
music: [],
|
|
1304
|
+
sfx: [],
|
|
1305
|
+
voice: []
|
|
1306
|
+
};
|
|
1307
|
+
currentVolumes;
|
|
1308
|
+
gate = Deferred2.makeUnsafe();
|
|
1309
|
+
constructor(options) {
|
|
1310
|
+
this.backend = options.backend;
|
|
1311
|
+
this.logger = options.logger ?? ((message, error) => console.warn(message, error));
|
|
1312
|
+
this.currentVolumes = options.initialVolumes ?? { master: 1, music: 1, sfx: 1, voice: 1 };
|
|
1313
|
+
this.unlocked = Effect6.runSync(SubscriptionRef2.make(false));
|
|
1314
|
+
const self = this;
|
|
1315
|
+
this.unlock = Effect6.gen(function* () {
|
|
1316
|
+
if (!SubscriptionRef2.getUnsafe(self.unlocked)) {
|
|
1317
|
+
yield* self.backend.unlock.pipe(Effect6.catch((error) => {
|
|
1318
|
+
self.logger("Audio context could not be resumed", error);
|
|
1319
|
+
return Effect6.void;
|
|
1320
|
+
}));
|
|
1321
|
+
yield* SubscriptionRef2.set(self.unlocked, true);
|
|
1322
|
+
yield* Deferred2.succeed(self.gate, undefined);
|
|
1323
|
+
}
|
|
1324
|
+
});
|
|
1325
|
+
}
|
|
1326
|
+
play = (channel, soundToPlay) => Effect6.gen({ self: this }, function* () {
|
|
1327
|
+
const self = this;
|
|
1328
|
+
if (!SubscriptionRef2.getUnsafe(self.unlocked)) {
|
|
1329
|
+
yield* Deferred2.await(self.gate);
|
|
1330
|
+
}
|
|
1331
|
+
const loaded = yield* self.backend.load(soundToPlay.file).pipe(Effect6.catch((error) => {
|
|
1332
|
+
self.logger(`Audio file missing or undecodable: ${soundToPlay.file}`, error);
|
|
1333
|
+
return Effect6.fail(error);
|
|
1334
|
+
}), Effect6.option);
|
|
1335
|
+
if (Option.isNone(loaded))
|
|
1336
|
+
return;
|
|
1337
|
+
const inputVolume = clamp2(soundToPlay.volume);
|
|
1338
|
+
if (channel === "voice")
|
|
1339
|
+
yield* self.stopVoiceImmediately();
|
|
1340
|
+
const playback = yield* self.backend.play(soundToPlay.file, {
|
|
1341
|
+
loop: soundToPlay.loop,
|
|
1342
|
+
volume: self.effectiveVolume(channel, inputVolume)
|
|
1343
|
+
}).pipe(Effect6.catch((error) => {
|
|
1344
|
+
self.logger(`Audio playback failed: ${soundToPlay.file}`, error);
|
|
1345
|
+
return Effect6.fail(error);
|
|
1346
|
+
}), Effect6.option);
|
|
1347
|
+
if (Option.isNone(playback))
|
|
1348
|
+
return;
|
|
1349
|
+
if (channel === "music")
|
|
1350
|
+
yield* self.replaceMusic(soundToPlay.fade);
|
|
1351
|
+
const record = {
|
|
1352
|
+
channel,
|
|
1353
|
+
inputVolume,
|
|
1354
|
+
playback: playback.value,
|
|
1355
|
+
fade: undefined
|
|
1356
|
+
};
|
|
1357
|
+
self.active[channel].push(record);
|
|
1358
|
+
record.playback.setVolume(soundToPlay.fade > 0 ? 0 : self.effectiveVolume(channel, inputVolume));
|
|
1359
|
+
if (positive(soundToPlay.fade) > 0) {
|
|
1360
|
+
record.fade = yield* self.startFade(record, 0, 1, soundToPlay.fade, false);
|
|
1361
|
+
}
|
|
1362
|
+
if (!soundToPlay.loop) {
|
|
1363
|
+
yield* record.playback.ended;
|
|
1364
|
+
self.removeRecord(record);
|
|
1365
|
+
}
|
|
1366
|
+
});
|
|
1367
|
+
stop = (channel, fade) => Effect6.gen({ self: this }, function* () {
|
|
1368
|
+
const self = this;
|
|
1369
|
+
const records = self.active[channel].slice();
|
|
1370
|
+
if (records.length === 0)
|
|
1371
|
+
return;
|
|
1372
|
+
for (const record of records)
|
|
1373
|
+
yield* self.interruptFade(record);
|
|
1374
|
+
const duration = positive(fade);
|
|
1375
|
+
if (duration === 0) {
|
|
1376
|
+
for (const record of records) {
|
|
1377
|
+
record.playback.stop();
|
|
1378
|
+
self.removeRecord(record);
|
|
1379
|
+
}
|
|
1380
|
+
return;
|
|
1381
|
+
}
|
|
1382
|
+
yield* Effect6.all(records.map((record) => self.fadeOutAndStop(record, duration)), { concurrency: "unbounded" });
|
|
1383
|
+
});
|
|
1384
|
+
setVolumes = (volumes) => Effect6.sync(() => {
|
|
1385
|
+
this.currentVolumes = {
|
|
1386
|
+
master: clamp2(volumes.master),
|
|
1387
|
+
music: clamp2(volumes.music),
|
|
1388
|
+
sfx: clamp2(volumes.sfx),
|
|
1389
|
+
voice: clamp2(volumes.voice)
|
|
1390
|
+
};
|
|
1391
|
+
for (const channel of ["music", "sfx", "voice"]) {
|
|
1392
|
+
for (const record of this.active[channel]) {
|
|
1393
|
+
record.playback.setVolume(this.effectiveVolume(channel, record.inputVolume));
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
});
|
|
1397
|
+
effectiveVolume(channel, inputVolume) {
|
|
1398
|
+
return clamp2(inputVolume * this.currentVolumes.master * this.currentVolumes[channel]);
|
|
1399
|
+
}
|
|
1400
|
+
removeRecord(record) {
|
|
1401
|
+
const records = this.active[record.channel];
|
|
1402
|
+
const index = records.indexOf(record);
|
|
1403
|
+
if (index >= 0)
|
|
1404
|
+
records.splice(index, 1);
|
|
1405
|
+
}
|
|
1406
|
+
interruptFade = (record) => record.fade === undefined ? Effect6.void : Fiber2.interrupt(record.fade).pipe(Effect6.asVoid, Effect6.tap(() => Effect6.sync(() => {
|
|
1407
|
+
record.fade = undefined;
|
|
1408
|
+
})));
|
|
1409
|
+
startFade = (record, from, to, duration, stopAtEnd) => {
|
|
1410
|
+
const steps = Math.max(1, Math.ceil(duration * 20));
|
|
1411
|
+
const interval = duration / steps;
|
|
1412
|
+
const fade = Effect6.forEach(Array.from({ length: steps }, (_, index) => index + 1), (step) => Effect6.sleep(interval).pipe(Effect6.flatMap(() => Effect6.sync(() => {
|
|
1413
|
+
const factor = from + (to - from) * (step / steps);
|
|
1414
|
+
record.playback.setVolume(this.effectiveVolume(record.channel, record.inputVolume) * factor);
|
|
1415
|
+
}))), { discard: true }).pipe(Effect6.flatMap(() => Effect6.sync(() => {
|
|
1416
|
+
record.playback.setVolume(this.effectiveVolume(record.channel, record.inputVolume) * to);
|
|
1417
|
+
if (stopAtEnd)
|
|
1418
|
+
record.playback.stop();
|
|
1419
|
+
})));
|
|
1420
|
+
return Effect6.forkDetach(fade);
|
|
1421
|
+
};
|
|
1422
|
+
fadeOutAndStop = (record, duration) => Effect6.gen({ self: this }, function* () {
|
|
1423
|
+
const self = this;
|
|
1424
|
+
const fiber = yield* self.startFade(record, 1, 0, duration, true);
|
|
1425
|
+
yield* Fiber2.join(fiber);
|
|
1426
|
+
self.removeRecord(record);
|
|
1427
|
+
});
|
|
1428
|
+
stopVoiceImmediately = () => Effect6.gen({ self: this }, function* () {
|
|
1429
|
+
const self = this;
|
|
1430
|
+
const records = self.active.voice.slice();
|
|
1431
|
+
for (const record of records) {
|
|
1432
|
+
yield* self.interruptFade(record);
|
|
1433
|
+
record.playback.stop();
|
|
1434
|
+
self.removeRecord(record);
|
|
1435
|
+
}
|
|
1436
|
+
});
|
|
1437
|
+
replaceMusic = (fade) => Effect6.gen({ self: this }, function* () {
|
|
1438
|
+
const self = this;
|
|
1439
|
+
const old = self.active.music.slice();
|
|
1440
|
+
for (const record of old) {
|
|
1441
|
+
yield* self.interruptFade(record);
|
|
1442
|
+
if (positive(fade) === 0) {
|
|
1443
|
+
record.playback.stop();
|
|
1444
|
+
self.removeRecord(record);
|
|
1445
|
+
} else {
|
|
1446
|
+
const fiber = yield* self.startFade(record, 1, 0, fade, true);
|
|
1447
|
+
record.fade = fiber;
|
|
1448
|
+
yield* Effect6.forkDetach(Fiber2.join(fiber).pipe(Effect6.flatMap(() => Effect6.sync(() => self.removeRecord(record)))));
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
});
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
// src/audio/pixiSoundBackend.ts
|
|
1455
|
+
import { Effect as Effect7 } from "effect";
|
|
1456
|
+
import { sound as sound2, webaudio } from "@pixi/sound";
|
|
1457
|
+
var clamp3 = (value) => Math.max(0, Math.min(1, value));
|
|
1458
|
+
var waitForMediaEnd = (instance) => Effect7.callback((resume) => {
|
|
1459
|
+
const finish = () => resume(Effect7.succeed(undefined));
|
|
1460
|
+
instance.once("end", finish);
|
|
1461
|
+
instance.once("stop", finish);
|
|
1462
|
+
return Effect7.sync(() => {
|
|
1463
|
+
instance.off("end", finish);
|
|
1464
|
+
instance.off("stop", finish);
|
|
1465
|
+
});
|
|
1466
|
+
});
|
|
1467
|
+
var pixiSoundBackend = {
|
|
1468
|
+
load: (file) => Effect7.callback((resume) => {
|
|
1469
|
+
if (sound2.exists(file)) {
|
|
1470
|
+
resume(Effect7.succeed(undefined));
|
|
1471
|
+
return;
|
|
1472
|
+
}
|
|
1473
|
+
try {
|
|
1474
|
+
sound2.add(file, {
|
|
1475
|
+
url: file,
|
|
1476
|
+
preload: true,
|
|
1477
|
+
loaded: (error) => resume(error == null ? Effect7.succeed(undefined) : Effect7.fail(new AudioDecodeError(file, error)))
|
|
1478
|
+
});
|
|
1479
|
+
} catch (error) {
|
|
1480
|
+
resume(Effect7.fail(new AudioDecodeError(file, error)));
|
|
1481
|
+
}
|
|
1482
|
+
}),
|
|
1483
|
+
play: (file, options) => Effect7.tryPromise({
|
|
1484
|
+
try: () => Promise.resolve(sound2.play(file, {
|
|
1485
|
+
loop: options.loop,
|
|
1486
|
+
volume: options.volume
|
|
1487
|
+
})).then((instance) => ({
|
|
1488
|
+
setVolume: (volume) => {
|
|
1489
|
+
instance.volume = clamp3(volume);
|
|
1490
|
+
},
|
|
1491
|
+
stop: () => instance.stop(),
|
|
1492
|
+
ended: options.loop ? Effect7.never : waitForMediaEnd(instance)
|
|
1493
|
+
})),
|
|
1494
|
+
catch: (error) => new AudioDecodeError(file, error)
|
|
1495
|
+
}),
|
|
1496
|
+
unlock: Effect7.tryPromise({
|
|
1497
|
+
try: () => {
|
|
1498
|
+
const context = sound2.context;
|
|
1499
|
+
if (context instanceof webaudio.WebAudioContext && context.audioContext.state === "suspended") {
|
|
1500
|
+
return context.audioContext.resume().then(() => {
|
|
1501
|
+
return;
|
|
1502
|
+
});
|
|
1503
|
+
}
|
|
1504
|
+
return Promise.resolve(undefined);
|
|
1505
|
+
},
|
|
1506
|
+
catch: (error) => new AudioDecodeError("<audio-context>", error)
|
|
1507
|
+
})
|
|
1508
|
+
};
|
|
1509
|
+
|
|
1510
|
+
// src/presenter/pixiPresenter.ts
|
|
1511
|
+
import { Context as Context4, Effect as Effect17, Layer as Layer4, PubSub as PubSub2, Stream as Stream6 } from "effect";
|
|
1512
|
+
|
|
1513
|
+
// src/choice/choiceMenu.ts
|
|
1514
|
+
import { Deferred as Deferred3, Effect as Effect8, Stream as Stream5 } from "effect";
|
|
1515
|
+
import { Container as Container7, Graphics as Graphics7, Text as Text9, TextStyle } from "pixi.js";
|
|
1516
|
+
var isChoiceEnabled = (verdict) => verdict === "open";
|
|
1517
|
+
var choiceNavigation = (options) => {
|
|
1518
|
+
const enabled = options.map((option) => isChoiceEnabled(option.verdict));
|
|
1519
|
+
const first = enabled.findIndex(Boolean);
|
|
1520
|
+
return { ids: options.map((option) => option.id), enabled, focused: first < 0 ? -1 : first };
|
|
1521
|
+
};
|
|
1522
|
+
var navigateChoice = (state, key) => {
|
|
1523
|
+
if (key === "enter")
|
|
1524
|
+
return state.focused >= 0 && state.enabled[state.focused] === true ? state.ids[state.focused] : undefined;
|
|
1525
|
+
if (/^[1-9]$/u.test(key)) {
|
|
1526
|
+
const index = Number(key) - 1;
|
|
1527
|
+
return state.enabled[index] === true ? state.ids[index] : undefined;
|
|
1528
|
+
}
|
|
1529
|
+
if (state.focused < 0 || key !== "up" && key !== "down")
|
|
1530
|
+
return state;
|
|
1531
|
+
const direction = key === "up" ? -1 : 1;
|
|
1532
|
+
let index = state.focused;
|
|
1533
|
+
for (let count = 0;count < state.ids.length; count += 1) {
|
|
1534
|
+
index = (index + direction + state.ids.length) % state.ids.length;
|
|
1535
|
+
if (state.enabled[index] === true)
|
|
1536
|
+
return { ...state, focused: index };
|
|
1537
|
+
}
|
|
1538
|
+
return state;
|
|
1539
|
+
};
|
|
1540
|
+
var hasLabel = (option) => typeof option.text === "string";
|
|
1541
|
+
|
|
1542
|
+
class ChoiceMenu {
|
|
1543
|
+
target;
|
|
1544
|
+
theme;
|
|
1545
|
+
panel;
|
|
1546
|
+
prompt;
|
|
1547
|
+
optionsLayer;
|
|
1548
|
+
countdown;
|
|
1549
|
+
rendered = [];
|
|
1550
|
+
navigation = { ids: [], enabled: [], focused: -1 };
|
|
1551
|
+
deferred;
|
|
1552
|
+
resolveChoice;
|
|
1553
|
+
maxRemaining = 0;
|
|
1554
|
+
remainingValue = 0;
|
|
1555
|
+
pulsePhase = 0;
|
|
1556
|
+
constructor(parent, theme) {
|
|
1557
|
+
this.theme = theme;
|
|
1558
|
+
this.target = new Container7;
|
|
1559
|
+
this.target.label = "ChoiceMenu";
|
|
1560
|
+
this.panel = new Graphics7;
|
|
1561
|
+
this.prompt = new Text9({ text: "", style: new TextStyle({ fontFamily: theme.font, fontSize: 24, fill: theme.text, wordWrap: true, wordWrapWidth: 632 }) });
|
|
1562
|
+
this.optionsLayer = new Container7;
|
|
1563
|
+
this.countdown = new Graphics7;
|
|
1564
|
+
this.target.addChild(this.panel, this.prompt, this.optionsLayer, this.countdown);
|
|
1565
|
+
parent.addChild(this.target);
|
|
1566
|
+
this.target.position.set(300, 0);
|
|
1567
|
+
this.target.eventMode = "static";
|
|
1568
|
+
this.target.on("pointerdown", (event) => {
|
|
1569
|
+
if ("preventDefault" in event.nativeEvent)
|
|
1570
|
+
event.nativeEvent.preventDefault();
|
|
1571
|
+
if ("stopImmediatePropagation" in event.nativeEvent)
|
|
1572
|
+
event.nativeEvent.stopImmediatePropagation();
|
|
1573
|
+
event.stopPropagation();
|
|
1574
|
+
});
|
|
1575
|
+
this.target.on("pointerup", (event) => {
|
|
1576
|
+
if ("preventDefault" in event.nativeEvent)
|
|
1577
|
+
event.nativeEvent.preventDefault();
|
|
1578
|
+
if ("stopImmediatePropagation" in event.nativeEvent)
|
|
1579
|
+
event.nativeEvent.stopImmediatePropagation();
|
|
1580
|
+
event.stopPropagation();
|
|
1581
|
+
});
|
|
1582
|
+
this.target.on("pointertap", (event) => this.pointerAt(event.global.y));
|
|
1583
|
+
if (typeof window !== "undefined")
|
|
1584
|
+
window.addEventListener("keydown", this.keydown);
|
|
1585
|
+
this.target.visible = false;
|
|
1586
|
+
}
|
|
1587
|
+
show = (choice, remaining) => (() => {
|
|
1588
|
+
const self = this;
|
|
1589
|
+
return Effect8.gen(function* () {
|
|
1590
|
+
yield* self.hide;
|
|
1591
|
+
self.target.visible = true;
|
|
1592
|
+
const options = choice.options.filter((option) => option.verdict !== "spent").map((option) => ({ ...option, text: "text" in option && typeof option.text === "string" ? option.text : option.id }));
|
|
1593
|
+
self.navigation = choiceNavigation(options);
|
|
1594
|
+
self.prompt.text = choice.prompt ?? "";
|
|
1595
|
+
self.renderOptions(options);
|
|
1596
|
+
self.deferred = yield* Deferred3.make();
|
|
1597
|
+
const deferred = self.deferred;
|
|
1598
|
+
self.resolveChoice = (id) => {
|
|
1599
|
+
if (self.deferred === deferred)
|
|
1600
|
+
Effect8.runSync(Deferred3.succeed(deferred, id));
|
|
1601
|
+
};
|
|
1602
|
+
yield* Stream5.runForEach(remaining, (seconds) => Effect8.sync(() => {
|
|
1603
|
+
if (self.maxRemaining === 0)
|
|
1604
|
+
self.maxRemaining = Math.max(0, seconds);
|
|
1605
|
+
self.remainingValue = Math.max(0, seconds);
|
|
1606
|
+
self.drawCountdown();
|
|
1607
|
+
})).pipe(Effect8.forkDetach);
|
|
1608
|
+
return yield* Deferred3.await(deferred);
|
|
1609
|
+
}).pipe(Effect8.ensuring(self.hide));
|
|
1610
|
+
})();
|
|
1611
|
+
hide = Effect8.sync(() => {
|
|
1612
|
+
this.target.visible = false;
|
|
1613
|
+
this.resolveChoice = undefined;
|
|
1614
|
+
this.deferred = undefined;
|
|
1615
|
+
this.maxRemaining = 0;
|
|
1616
|
+
this.remainingValue = 0;
|
|
1617
|
+
for (const item of this.rendered) {
|
|
1618
|
+
item.button.removeFromParent();
|
|
1619
|
+
item.button.visible = false;
|
|
1620
|
+
item.label.removeFromParent();
|
|
1621
|
+
item.label.visible = false;
|
|
1622
|
+
}
|
|
1623
|
+
this.rendered.length = 0;
|
|
1624
|
+
this.optionsLayer.removeChildren();
|
|
1625
|
+
});
|
|
1626
|
+
setRemaining = (seconds) => {
|
|
1627
|
+
if (this.maxRemaining === 0)
|
|
1628
|
+
this.maxRemaining = Math.max(0, seconds);
|
|
1629
|
+
this.remainingValue = Math.max(0, seconds);
|
|
1630
|
+
this.drawCountdown();
|
|
1631
|
+
};
|
|
1632
|
+
tick = (dt) => {
|
|
1633
|
+
this.pulsePhase += Math.max(0, dt);
|
|
1634
|
+
if (this.remainingValue < 3)
|
|
1635
|
+
this.drawCountdown();
|
|
1636
|
+
};
|
|
1637
|
+
keydown = (event) => {
|
|
1638
|
+
if (!this.target.visible)
|
|
1639
|
+
return;
|
|
1640
|
+
const key = event.key === "ArrowUp" ? "up" : event.key === "ArrowDown" ? "down" : event.key === "Enter" ? "enter" : event.key;
|
|
1641
|
+
const result = navigateChoice(this.navigation, key);
|
|
1642
|
+
if (typeof result === "string")
|
|
1643
|
+
this.resolveChoice?.(result);
|
|
1644
|
+
else if (result !== undefined) {
|
|
1645
|
+
this.navigation = result;
|
|
1646
|
+
this.updateFocus();
|
|
1647
|
+
}
|
|
1648
|
+
};
|
|
1649
|
+
pointerAt = (y) => {
|
|
1650
|
+
for (const [index, item] of this.rendered.entries()) {
|
|
1651
|
+
if (y >= item.button.y && y <= item.button.y + item.button.height && this.navigation.enabled[index] === true) {
|
|
1652
|
+
this.resolveChoice?.(item.option.id);
|
|
1653
|
+
return;
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
};
|
|
1657
|
+
renderOptions = (options) => {
|
|
1658
|
+
const width = 680;
|
|
1659
|
+
this.panel.clear().roundRect(0, 0, width, 460, 12).fill({ color: this.theme.panel, alpha: this.theme.panelAlpha });
|
|
1660
|
+
this.prompt.x = 24;
|
|
1661
|
+
this.prompt.y = 20;
|
|
1662
|
+
options.forEach((option, index) => {
|
|
1663
|
+
const button = new Graphics7;
|
|
1664
|
+
const label = new Text9({
|
|
1665
|
+
text: hasLabel(option) ? option.text : option.id,
|
|
1666
|
+
style: new TextStyle({ fontFamily: this.theme.font, fontSize: 22, fill: this.theme.text })
|
|
1667
|
+
});
|
|
1668
|
+
button.y = 70 + index * 48;
|
|
1669
|
+
button.roundRect(20, 0, width - 40, 38, 6).fill({
|
|
1670
|
+
color: this.theme.accent,
|
|
1671
|
+
alpha: this.navigation.enabled[index] === true ? 0.35 : 0.25
|
|
1672
|
+
});
|
|
1673
|
+
label.x = 32;
|
|
1674
|
+
label.y = button.y + 6;
|
|
1675
|
+
button.eventMode = this.navigation.enabled[index] === true ? "static" : "none";
|
|
1676
|
+
button.on("pointertap", () => {
|
|
1677
|
+
if (this.navigation.enabled[index] === true)
|
|
1678
|
+
this.resolveChoice?.(option.id);
|
|
1679
|
+
});
|
|
1680
|
+
this.optionsLayer.addChild(button, label);
|
|
1681
|
+
this.rendered.push({ option, button, label });
|
|
1682
|
+
});
|
|
1683
|
+
this.updateFocus();
|
|
1684
|
+
this.drawCountdown();
|
|
1685
|
+
};
|
|
1686
|
+
updateFocus = () => {
|
|
1687
|
+
for (const [index, item] of this.rendered.entries()) {
|
|
1688
|
+
const active = index === this.navigation.focused;
|
|
1689
|
+
item.button.alpha = active ? 1 : 0.8;
|
|
1690
|
+
}
|
|
1691
|
+
};
|
|
1692
|
+
drawCountdown = () => {
|
|
1693
|
+
const ratio = this.maxRemaining <= 0 ? 0 : Math.min(1, this.remainingValue / this.maxRemaining);
|
|
1694
|
+
const pulse = this.remainingValue < 3 ? 0.7 + Math.sin(this.pulsePhase * 8) * 0.3 : 1;
|
|
1695
|
+
this.countdown.alpha = pulse;
|
|
1696
|
+
this.countdown.clear().rect(20, 450, 640 * ratio, 6).fill({ color: this.theme.accent, alpha: pulse });
|
|
1697
|
+
};
|
|
1698
|
+
}
|
|
1699
|
+
|
|
1700
|
+
// src/text/dialogueWindow.ts
|
|
1701
|
+
import { Effect as Effect9 } from "effect";
|
|
1702
|
+
import { Container as Container9, Graphics as Graphics8, Text as Text11, TextStyle as TextStyle3 } from "pixi.js";
|
|
1703
|
+
|
|
1704
|
+
// src/text/segments.ts
|
|
1705
|
+
var segmenter = (locale) => new Intl.Segmenter(locale || undefined, { granularity: "grapheme" });
|
|
1706
|
+
var buildRevealTimeline = (segments, charactersPerSecond, locale = "") => {
|
|
1707
|
+
const graphemes = [];
|
|
1708
|
+
const pauses = [];
|
|
1709
|
+
let time = 0;
|
|
1710
|
+
const speed = Math.max(0, charactersPerSecond);
|
|
1711
|
+
const iterator = segmenter(locale);
|
|
1712
|
+
segments.forEach((segment, segmentIndex) => {
|
|
1713
|
+
if (segment._tag === "Pause") {
|
|
1714
|
+
const seconds = Math.max(0, segment.seconds);
|
|
1715
|
+
pauses.push({ segment: segmentIndex, start: time, end: time + seconds });
|
|
1716
|
+
time += seconds;
|
|
1717
|
+
return;
|
|
1718
|
+
}
|
|
1719
|
+
const rate = segment.rate === undefined ? 1 : Math.max(0, segment.rate);
|
|
1720
|
+
const secondsPerGrapheme = speed === 0 || rate === 0 ? 0 : 1 / (speed * rate);
|
|
1721
|
+
for (const part of iterator.segment(segment.text)) {
|
|
1722
|
+
const start = time;
|
|
1723
|
+
time += secondsPerGrapheme;
|
|
1724
|
+
graphemes.push({
|
|
1725
|
+
grapheme: part.segment,
|
|
1726
|
+
styles: segment.styles,
|
|
1727
|
+
segment: segmentIndex,
|
|
1728
|
+
start,
|
|
1729
|
+
end: time
|
|
1730
|
+
});
|
|
1731
|
+
}
|
|
1732
|
+
});
|
|
1733
|
+
return { graphemes, pauses, duration: time, locale };
|
|
1734
|
+
};
|
|
1735
|
+
|
|
1736
|
+
// src/text/richText.ts
|
|
1737
|
+
import {
|
|
1738
|
+
CanvasTextMetrics,
|
|
1739
|
+
Text as Text10,
|
|
1740
|
+
TextStyle as TextStyle2
|
|
1741
|
+
} from "pixi.js";
|
|
1742
|
+
var noStart = new Set(["、", "。", "!", "?", "」", "』", ")", "】", "〉", "》", "〕", "]", "}"]);
|
|
1743
|
+
var noEnd = new Set(["「", "『", "(", "【", "〈", "《", "〔", "[", "{"]);
|
|
1744
|
+
var mapTextStyle = (names, bindings, base = {}) => {
|
|
1745
|
+
const result = { ...base };
|
|
1746
|
+
for (const name of names) {
|
|
1747
|
+
const binding = bindings[name];
|
|
1748
|
+
if (binding !== undefined)
|
|
1749
|
+
Object.assign(result, binding);
|
|
1750
|
+
}
|
|
1751
|
+
return result;
|
|
1752
|
+
};
|
|
1753
|
+
|
|
1754
|
+
// src/text/dialogueWindow.ts
|
|
1755
|
+
var textStyle = (font, fill, size, italic = false) => new TextStyle3({ fontFamily: font, fontSize: size, fill, fontStyle: italic ? "italic" : "normal" });
|
|
1756
|
+
var rolePrefix = (role) => role === "os" ? "(" : "";
|
|
1757
|
+
var roleSuffix = (role) => role === "os" ? ")" : "";
|
|
1758
|
+
|
|
1759
|
+
class DialogueWindow {
|
|
1760
|
+
target;
|
|
1761
|
+
panel;
|
|
1762
|
+
namePlate;
|
|
1763
|
+
nameText;
|
|
1764
|
+
textArea;
|
|
1765
|
+
indicator;
|
|
1766
|
+
world;
|
|
1767
|
+
theme;
|
|
1768
|
+
options;
|
|
1769
|
+
graphemeTexts = [];
|
|
1770
|
+
timeline = { graphemes: [], pauses: [], duration: 0, locale: "" };
|
|
1771
|
+
currentLine;
|
|
1772
|
+
revealed = false;
|
|
1773
|
+
panelOpacity = 1;
|
|
1774
|
+
constructor(parent, world, theme, options = {}) {
|
|
1775
|
+
this.world = world;
|
|
1776
|
+
this.theme = theme;
|
|
1777
|
+
this.options = options;
|
|
1778
|
+
this.target = new Container9;
|
|
1779
|
+
this.target.label = "DialogueWindow";
|
|
1780
|
+
this.panel = new Graphics8;
|
|
1781
|
+
this.namePlate = new Container9;
|
|
1782
|
+
this.nameText = new Text11({ text: "", style: textStyle(theme.font, theme.text, options.fontSize ?? 24) });
|
|
1783
|
+
this.textArea = new Container9;
|
|
1784
|
+
this.indicator = new Text11({ text: "▼", style: textStyle(theme.font, theme.accent, 18) });
|
|
1785
|
+
this.indicator.visible = false;
|
|
1786
|
+
this.namePlate.addChild(this.nameText);
|
|
1787
|
+
this.target.addChild(this.panel, this.namePlate, this.textArea, this.indicator);
|
|
1788
|
+
parent.addChild(this.target);
|
|
1789
|
+
this.target.visible = false;
|
|
1790
|
+
this.relayout({ width: 1280, height: 720 });
|
|
1791
|
+
}
|
|
1792
|
+
show = (line, settings, mode) => {
|
|
1793
|
+
const self = this;
|
|
1794
|
+
return Effect9.gen(function* () {
|
|
1795
|
+
yield* self.world.cancel(self.target);
|
|
1796
|
+
self.target.visible = true;
|
|
1797
|
+
self.currentLine = line;
|
|
1798
|
+
self.timeline = buildRevealTimeline(line.segments, settings.textSpeed, settings.locale);
|
|
1799
|
+
self.revealed = mode === "skip" || self.timeline.duration === 0;
|
|
1800
|
+
self.indicator.visible = false;
|
|
1801
|
+
self.namePlate.visible = line.speakerName.length > 0;
|
|
1802
|
+
const role = line.role;
|
|
1803
|
+
const displayName = `${rolePrefix(role)}${line.speakerName}${roleSuffix(role)}`;
|
|
1804
|
+
self.nameText.text = displayName;
|
|
1805
|
+
self.nameText.style = textStyle(self.theme.font, self.options.nameColors?.[line.speaker] ?? self.theme.accent, self.options.fontSize ?? 24, role === "mono" || role === "vo");
|
|
1806
|
+
self.clearTextChildren();
|
|
1807
|
+
self.buildTextChildren(line, settings);
|
|
1808
|
+
self.setOpacity(settings.windowOpacity);
|
|
1809
|
+
if (line.read && settings.readColour)
|
|
1810
|
+
self.tintText(self.theme.readText);
|
|
1811
|
+
else
|
|
1812
|
+
self.tintText(self.theme.text);
|
|
1813
|
+
const behaviour = self.typewriterBehaviour();
|
|
1814
|
+
if (self.revealed)
|
|
1815
|
+
behaviour.finish();
|
|
1816
|
+
yield* self.world.run(self.target, behaviour);
|
|
1817
|
+
self.indicator.visible = true;
|
|
1818
|
+
});
|
|
1819
|
+
};
|
|
1820
|
+
get reveal() {
|
|
1821
|
+
const self = this;
|
|
1822
|
+
return Effect9.gen(function* () {
|
|
1823
|
+
yield* self.world.finishAll(self.target);
|
|
1824
|
+
self.revealed = true;
|
|
1825
|
+
self.indicator.visible = self.currentLine !== undefined;
|
|
1826
|
+
});
|
|
1827
|
+
}
|
|
1828
|
+
get clear() {
|
|
1829
|
+
const self = this;
|
|
1830
|
+
return Effect9.gen(function* () {
|
|
1831
|
+
yield* self.world.cancel(self.target);
|
|
1832
|
+
self.target.visible = true;
|
|
1833
|
+
self.currentLine = undefined;
|
|
1834
|
+
self.timeline = { graphemes: [], pauses: [], duration: 0, locale: "" };
|
|
1835
|
+
self.clearTextChildren();
|
|
1836
|
+
self.nameText.text = "";
|
|
1837
|
+
self.namePlate.visible = false;
|
|
1838
|
+
self.indicator.visible = false;
|
|
1839
|
+
});
|
|
1840
|
+
}
|
|
1841
|
+
setOpacity = (alpha) => {
|
|
1842
|
+
this.panelOpacity = Math.min(1, Math.max(0, alpha));
|
|
1843
|
+
this.panel.alpha = this.panelOpacity;
|
|
1844
|
+
};
|
|
1845
|
+
setHidden = (hidden) => {
|
|
1846
|
+
this.target.visible = !hidden;
|
|
1847
|
+
};
|
|
1848
|
+
relayout = (design) => {
|
|
1849
|
+
const width = this.options.boxWidth ?? Math.min(1180, design.width - 100);
|
|
1850
|
+
const height = this.options.boxHeight ?? 190;
|
|
1851
|
+
const x = (design.width - width) / 2;
|
|
1852
|
+
const y = design.height - height - 30;
|
|
1853
|
+
this.panel.clear().roundRect(x, y, width, height, 18).fill({ color: this.theme.panel, alpha: this.panelOpacity * this.theme.panelAlpha });
|
|
1854
|
+
const existingPlate = this.namePlate.children[1];
|
|
1855
|
+
const plate = existingPlate instanceof Graphics8 ? existingPlate : new Graphics8;
|
|
1856
|
+
if (!(existingPlate instanceof Graphics8))
|
|
1857
|
+
this.namePlate.addChild(plate);
|
|
1858
|
+
plate.clear().roundRect(0, 0, 190, 38, 10).fill({ color: this.theme.panel, alpha: 0.92 });
|
|
1859
|
+
this.namePlate.x = x + 26;
|
|
1860
|
+
this.namePlate.y = y - 40;
|
|
1861
|
+
this.nameText.x = 14;
|
|
1862
|
+
this.nameText.y = 5;
|
|
1863
|
+
this.textArea.x = x + 28;
|
|
1864
|
+
this.textArea.y = y + 52;
|
|
1865
|
+
this.indicator.x = x + width - 42;
|
|
1866
|
+
this.indicator.y = y + height - 36;
|
|
1867
|
+
};
|
|
1868
|
+
clearTextChildren = () => {
|
|
1869
|
+
for (const child of this.textArea.removeChildren())
|
|
1870
|
+
child.visible = false;
|
|
1871
|
+
this.graphemeTexts.length = 0;
|
|
1872
|
+
};
|
|
1873
|
+
buildTextChildren = (line, settings) => {
|
|
1874
|
+
const fontSize = this.options.fontSize ?? 24;
|
|
1875
|
+
const byStyle = [];
|
|
1876
|
+
for (const segment of line.segments) {
|
|
1877
|
+
if (segment._tag !== "Text")
|
|
1878
|
+
continue;
|
|
1879
|
+
const previous = byStyle[byStyle.length - 1];
|
|
1880
|
+
if (previous !== undefined && previous.styles.length === segment.styles.length && previous.styles.every((s, i) => s === segment.styles[i])) {
|
|
1881
|
+
previous.text += segment.text;
|
|
1882
|
+
} else
|
|
1883
|
+
byStyle.push({ styles: segment.styles, text: segment.text });
|
|
1884
|
+
}
|
|
1885
|
+
for (const run of byStyle) {
|
|
1886
|
+
const style = mapTextStyle(run.styles, this.options.textStyles ?? {}, {
|
|
1887
|
+
fontFamily: this.theme.font,
|
|
1888
|
+
fontSize,
|
|
1889
|
+
color: this.theme.text
|
|
1890
|
+
});
|
|
1891
|
+
const text = new Text11({
|
|
1892
|
+
text: "",
|
|
1893
|
+
style: {
|
|
1894
|
+
...textStyle(style.fontFamily ?? this.theme.font, typeof style.color === "number" ? style.color : this.theme.text, style.fontSize ?? fontSize, style.fontStyle === "italic"),
|
|
1895
|
+
wordWrap: true,
|
|
1896
|
+
wordWrapWidth: 1110,
|
|
1897
|
+
breakWords: true
|
|
1898
|
+
}
|
|
1899
|
+
});
|
|
1900
|
+
text.alpha = style.alpha ?? 1;
|
|
1901
|
+
text.scale.set(style.scale ?? 1);
|
|
1902
|
+
this.textArea.addChild(text);
|
|
1903
|
+
const count = Array.from(new Intl.Segmenter(settings.locale || undefined, { granularity: "grapheme" }).segment(run.text)).length;
|
|
1904
|
+
for (let i = 0;i < count; i += 1)
|
|
1905
|
+
this.graphemeTexts.push(text);
|
|
1906
|
+
}
|
|
1907
|
+
};
|
|
1908
|
+
tintText = (colour) => {
|
|
1909
|
+
for (const text of this.textArea.children) {
|
|
1910
|
+
if (text instanceof Text11)
|
|
1911
|
+
text.style.fill = colour;
|
|
1912
|
+
}
|
|
1913
|
+
};
|
|
1914
|
+
typewriterBehaviour = () => {
|
|
1915
|
+
let elapsed = 0;
|
|
1916
|
+
let revealedCount = 0;
|
|
1917
|
+
return {
|
|
1918
|
+
update: (dt) => {
|
|
1919
|
+
elapsed += Math.max(0, dt);
|
|
1920
|
+
while (revealedCount < this.timeline.graphemes.length) {
|
|
1921
|
+
const item = this.timeline.graphemes[revealedCount];
|
|
1922
|
+
if (item === undefined || item.end > elapsed)
|
|
1923
|
+
break;
|
|
1924
|
+
const text = this.graphemeTexts[revealedCount];
|
|
1925
|
+
if (text !== undefined)
|
|
1926
|
+
text.text += item.grapheme;
|
|
1927
|
+
revealedCount += 1;
|
|
1928
|
+
}
|
|
1929
|
+
if (revealedCount >= this.timeline.graphemes.length && elapsed >= this.timeline.duration) {
|
|
1930
|
+
this.revealed = true;
|
|
1931
|
+
return "done";
|
|
1932
|
+
}
|
|
1933
|
+
return "running";
|
|
1934
|
+
},
|
|
1935
|
+
finish: () => {
|
|
1936
|
+
for (let index = revealedCount;index < this.timeline.graphemes.length; index += 1) {
|
|
1937
|
+
const item = this.timeline.graphemes[index];
|
|
1938
|
+
const text = this.graphemeTexts[index];
|
|
1939
|
+
if (item !== undefined && text !== undefined)
|
|
1940
|
+
text.text += item.grapheme;
|
|
1941
|
+
}
|
|
1942
|
+
revealedCount = this.timeline.graphemes.length;
|
|
1943
|
+
elapsed = this.timeline.duration;
|
|
1944
|
+
this.revealed = true;
|
|
1945
|
+
},
|
|
1946
|
+
dispose: () => {
|
|
1947
|
+
return;
|
|
1948
|
+
}
|
|
1949
|
+
};
|
|
1950
|
+
};
|
|
1951
|
+
}
|
|
1952
|
+
|
|
1953
|
+
// src/scene/actor.ts
|
|
1954
|
+
import { Effect as Effect11 } from "effect";
|
|
1955
|
+
import { Container as Container13, Sprite as Sprite4 } from "pixi.js";
|
|
1956
|
+
|
|
1957
|
+
// src/effects/catalogue.ts
|
|
1958
|
+
import { Effect as Effect10, Schema } from "effect";
|
|
1959
|
+
import { BlurFilter } from "pixi.js";
|
|
1960
|
+
|
|
1961
|
+
// src/engine/behaviours.ts
|
|
1962
|
+
import { Graphics as Graphics9 } from "pixi.js";
|
|
1963
|
+
|
|
1964
|
+
// src/engine/easing.ts
|
|
1965
|
+
var clamp4 = (progress) => Math.min(1, Math.max(0, progress));
|
|
1966
|
+
var linear = (progress) => clamp4(progress);
|
|
1967
|
+
var quadOut = (progress) => {
|
|
1968
|
+
const value = clamp4(progress);
|
|
1969
|
+
return 1 - (1 - value) * (1 - value);
|
|
1970
|
+
};
|
|
1971
|
+
|
|
1972
|
+
// src/engine/behaviours.ts
|
|
1973
|
+
var pool = [];
|
|
1974
|
+
var applyTween = (target, props, starts, to, p) => {
|
|
1975
|
+
if (to.x !== undefined && starts.x !== undefined)
|
|
1976
|
+
target.x = starts.x + (to.x - starts.x) * p;
|
|
1977
|
+
if (to.y !== undefined && starts.y !== undefined)
|
|
1978
|
+
target.y = starts.y + (to.y - starts.y) * p;
|
|
1979
|
+
if (to.alpha !== undefined && starts.alpha !== undefined)
|
|
1980
|
+
target.alpha = starts.alpha + (to.alpha - starts.alpha) * p;
|
|
1981
|
+
if (to.scaleX !== undefined && starts.scaleX !== undefined)
|
|
1982
|
+
target.scale.x = starts.scaleX + (to.scaleX - starts.scaleX) * p;
|
|
1983
|
+
if (to.scaleY !== undefined && starts.scaleY !== undefined)
|
|
1984
|
+
target.scale.y = starts.scaleY + (to.scaleY - starts.scaleY) * p;
|
|
1985
|
+
if (to.rotation !== undefined && starts.rotation !== undefined)
|
|
1986
|
+
target.rotation = starts.rotation + (to.rotation - starts.rotation) * p;
|
|
1987
|
+
if (to.tint !== undefined && starts.tint !== undefined)
|
|
1988
|
+
target.tint = Math.round(starts.tint + (to.tint - starts.tint) * p);
|
|
1989
|
+
};
|
|
1990
|
+
|
|
1991
|
+
class TweenBehaviour {
|
|
1992
|
+
target;
|
|
1993
|
+
props;
|
|
1994
|
+
starts;
|
|
1995
|
+
to;
|
|
1996
|
+
duration = 0;
|
|
1997
|
+
elapsed = 0;
|
|
1998
|
+
easing = linear;
|
|
1999
|
+
pooled = false;
|
|
2000
|
+
update(dt) {
|
|
2001
|
+
this.elapsed = Math.min(this.duration, this.elapsed + Math.max(0, dt));
|
|
2002
|
+
const p = this.duration <= 0 ? 1 : this.easing(this.elapsed / this.duration);
|
|
2003
|
+
applyTween(this.target, this.props, this.starts, this.to, p);
|
|
2004
|
+
return this.elapsed >= this.duration ? "done" : "running";
|
|
2005
|
+
}
|
|
2006
|
+
finish() {
|
|
2007
|
+
applyTween(this.target, this.props, this.starts, this.to, 1);
|
|
2008
|
+
this.elapsed = this.duration;
|
|
2009
|
+
}
|
|
2010
|
+
dispose() {
|
|
2011
|
+
if (this.pooled)
|
|
2012
|
+
return;
|
|
2013
|
+
this.pooled = true;
|
|
2014
|
+
pool.push(this);
|
|
2015
|
+
}
|
|
2016
|
+
}
|
|
2017
|
+
var tween = (target, props, to, duration, easing = linear) => {
|
|
2018
|
+
const value = pool.pop() ?? new TweenBehaviour;
|
|
2019
|
+
value.target = target;
|
|
2020
|
+
value.props = props;
|
|
2021
|
+
const starts = {};
|
|
2022
|
+
if (props.x !== undefined)
|
|
2023
|
+
starts.x = target.x;
|
|
2024
|
+
if (props.y !== undefined)
|
|
2025
|
+
starts.y = target.y;
|
|
2026
|
+
if (props.alpha !== undefined)
|
|
2027
|
+
starts.alpha = target.alpha;
|
|
2028
|
+
if (props.scaleX !== undefined)
|
|
2029
|
+
starts.scaleX = target.scale.x;
|
|
2030
|
+
if (props.scaleY !== undefined)
|
|
2031
|
+
starts.scaleY = target.scale.y;
|
|
2032
|
+
if (props.rotation !== undefined)
|
|
2033
|
+
starts.rotation = target.rotation;
|
|
2034
|
+
if (props.tint !== undefined)
|
|
2035
|
+
starts.tint = target.tint;
|
|
2036
|
+
value.starts = starts;
|
|
2037
|
+
value.to = to;
|
|
2038
|
+
value.duration = Math.max(0, duration);
|
|
2039
|
+
value.elapsed = 0;
|
|
2040
|
+
value.easing = easing;
|
|
2041
|
+
value.pooled = false;
|
|
2042
|
+
return value;
|
|
2043
|
+
};
|
|
2044
|
+
var fade = (target, to, duration) => tween(target, { alpha: target.alpha }, { alpha: to }, duration, linear);
|
|
2045
|
+
|
|
2046
|
+
class CrossfadeBehaviour {
|
|
2047
|
+
from;
|
|
2048
|
+
to;
|
|
2049
|
+
fromStart = 1;
|
|
2050
|
+
toStart = 0;
|
|
2051
|
+
duration = 0;
|
|
2052
|
+
elapsed = 0;
|
|
2053
|
+
pooled = false;
|
|
2054
|
+
update(dt) {
|
|
2055
|
+
this.elapsed = Math.min(this.duration, this.elapsed + Math.max(0, dt));
|
|
2056
|
+
const p = this.duration <= 0 ? 1 : this.elapsed / this.duration;
|
|
2057
|
+
this.from.alpha = this.fromStart * (1 - p);
|
|
2058
|
+
this.to.alpha = this.toStart + (1 - this.toStart) * p;
|
|
2059
|
+
return this.elapsed >= this.duration ? "done" : "running";
|
|
2060
|
+
}
|
|
2061
|
+
finish() {
|
|
2062
|
+
this.from.alpha = 0;
|
|
2063
|
+
this.to.alpha = 1;
|
|
2064
|
+
this.elapsed = this.duration;
|
|
2065
|
+
}
|
|
2066
|
+
dispose() {
|
|
2067
|
+
if (this.pooled)
|
|
2068
|
+
return;
|
|
2069
|
+
this.pooled = true;
|
|
2070
|
+
crossfadePool.push(this);
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
var crossfadePool = [];
|
|
2074
|
+
var crossfade = (from, to, duration) => {
|
|
2075
|
+
const value = crossfadePool.pop() ?? new CrossfadeBehaviour;
|
|
2076
|
+
value.from = from;
|
|
2077
|
+
value.to = to;
|
|
2078
|
+
value.fromStart = from.alpha;
|
|
2079
|
+
value.toStart = to.alpha;
|
|
2080
|
+
value.duration = Math.max(0, duration);
|
|
2081
|
+
value.elapsed = 0;
|
|
2082
|
+
value.pooled = false;
|
|
2083
|
+
return value;
|
|
2084
|
+
};
|
|
2085
|
+
|
|
2086
|
+
class ShakeBehaviour {
|
|
2087
|
+
target;
|
|
2088
|
+
baseX = 0;
|
|
2089
|
+
baseY = 0;
|
|
2090
|
+
strength = 0;
|
|
2091
|
+
duration = 0;
|
|
2092
|
+
frequency = 0;
|
|
2093
|
+
elapsed = 0;
|
|
2094
|
+
pooled = false;
|
|
2095
|
+
update(dt) {
|
|
2096
|
+
this.elapsed = Math.min(this.duration, this.elapsed + Math.max(0, dt));
|
|
2097
|
+
if (this.elapsed >= this.duration) {
|
|
2098
|
+
this.target.x = this.baseX;
|
|
2099
|
+
this.target.y = this.baseY;
|
|
2100
|
+
return "done";
|
|
2101
|
+
}
|
|
2102
|
+
const fadeOut = 1 - this.elapsed / this.duration;
|
|
2103
|
+
const angle = this.elapsed * this.frequency * Math.PI * 2;
|
|
2104
|
+
this.target.x = this.baseX + Math.sin(angle * 1.13) * this.strength * fadeOut;
|
|
2105
|
+
this.target.y = this.baseY + Math.cos(angle * 0.97) * this.strength * fadeOut;
|
|
2106
|
+
return "running";
|
|
2107
|
+
}
|
|
2108
|
+
finish() {
|
|
2109
|
+
this.elapsed = this.duration;
|
|
2110
|
+
this.target.x = this.baseX;
|
|
2111
|
+
this.target.y = this.baseY;
|
|
2112
|
+
}
|
|
2113
|
+
dispose() {
|
|
2114
|
+
if (this.pooled)
|
|
2115
|
+
return;
|
|
2116
|
+
this.pooled = true;
|
|
2117
|
+
shakePool.push(this);
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
var shakePool = [];
|
|
2121
|
+
var shake = (target, strength, duration, frequency = 18) => {
|
|
2122
|
+
const value = shakePool.pop() ?? new ShakeBehaviour;
|
|
2123
|
+
value.target = target;
|
|
2124
|
+
value.baseX = target.x;
|
|
2125
|
+
value.baseY = target.y;
|
|
2126
|
+
value.strength = strength;
|
|
2127
|
+
value.duration = Math.max(0, duration);
|
|
2128
|
+
value.frequency = Math.max(0, frequency);
|
|
2129
|
+
value.elapsed = 0;
|
|
2130
|
+
value.pooled = false;
|
|
2131
|
+
return value;
|
|
2132
|
+
};
|
|
2133
|
+
|
|
2134
|
+
class HopBehaviour {
|
|
2135
|
+
target;
|
|
2136
|
+
baseY = 0;
|
|
2137
|
+
height = 0;
|
|
2138
|
+
duration = 0;
|
|
2139
|
+
elapsed = 0;
|
|
2140
|
+
pooled = false;
|
|
2141
|
+
update(dt) {
|
|
2142
|
+
this.elapsed = Math.min(this.duration, this.elapsed + Math.max(0, dt));
|
|
2143
|
+
const p = this.duration <= 0 ? 1 : this.elapsed / this.duration;
|
|
2144
|
+
this.target.y = this.baseY - Math.sin(Math.PI * p) * this.height;
|
|
2145
|
+
return this.elapsed >= this.duration ? "done" : "running";
|
|
2146
|
+
}
|
|
2147
|
+
finish() {
|
|
2148
|
+
this.target.y = this.baseY;
|
|
2149
|
+
this.elapsed = this.duration;
|
|
2150
|
+
}
|
|
2151
|
+
dispose() {
|
|
2152
|
+
if (this.pooled)
|
|
2153
|
+
return;
|
|
2154
|
+
this.pooled = true;
|
|
2155
|
+
hopPool.push(this);
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
var hopPool = [];
|
|
2159
|
+
var hop = (target, height, duration) => {
|
|
2160
|
+
const value = hopPool.pop() ?? new HopBehaviour;
|
|
2161
|
+
value.target = target;
|
|
2162
|
+
value.baseY = target.y;
|
|
2163
|
+
value.height = height;
|
|
2164
|
+
value.duration = Math.max(0, duration);
|
|
2165
|
+
value.elapsed = 0;
|
|
2166
|
+
value.pooled = false;
|
|
2167
|
+
return value;
|
|
2168
|
+
};
|
|
2169
|
+
var slide = (target, fromX, toX, duration) => {
|
|
2170
|
+
target.x = fromX;
|
|
2171
|
+
return tween(target, { x: fromX }, { x: toX }, duration, quadOut);
|
|
2172
|
+
};
|
|
2173
|
+
|
|
2174
|
+
class WipeBehaviour {
|
|
2175
|
+
target;
|
|
2176
|
+
maskGraphic;
|
|
2177
|
+
direction = "left";
|
|
2178
|
+
reveal = true;
|
|
2179
|
+
width = 1;
|
|
2180
|
+
height = 1;
|
|
2181
|
+
duration = 0;
|
|
2182
|
+
elapsed = 0;
|
|
2183
|
+
pooled = false;
|
|
2184
|
+
updateMask(progress) {
|
|
2185
|
+
const p = this.reveal ? progress : 1 - progress;
|
|
2186
|
+
const x = this.direction === "right" ? this.width * (1 - p) : 0;
|
|
2187
|
+
const y = this.direction === "down" ? this.height * (1 - p) : 0;
|
|
2188
|
+
const width = this.direction === "left" || this.direction === "right" ? this.width * p : this.width;
|
|
2189
|
+
const height = this.direction === "up" || this.direction === "down" ? this.height * p : this.height;
|
|
2190
|
+
this.maskGraphic.clear().rect(x, y, Math.max(0, width), Math.max(0, height)).fill(16777215);
|
|
2191
|
+
}
|
|
2192
|
+
update(dt) {
|
|
2193
|
+
this.elapsed = Math.min(this.duration, this.elapsed + Math.max(0, dt));
|
|
2194
|
+
this.updateMask(this.duration <= 0 ? 1 : this.elapsed / this.duration);
|
|
2195
|
+
return this.elapsed >= this.duration ? "done" : "running";
|
|
2196
|
+
}
|
|
2197
|
+
finish() {
|
|
2198
|
+
this.elapsed = this.duration;
|
|
2199
|
+
this.updateMask(1);
|
|
2200
|
+
}
|
|
2201
|
+
dispose() {
|
|
2202
|
+
if (this.pooled)
|
|
2203
|
+
return;
|
|
2204
|
+
this.target.mask = null;
|
|
2205
|
+
this.maskGraphic.clear();
|
|
2206
|
+
this.pooled = true;
|
|
2207
|
+
wipePool.push(this);
|
|
2208
|
+
}
|
|
2209
|
+
}
|
|
2210
|
+
var wipePool = [];
|
|
2211
|
+
var wipe = (target, direction, duration, reveal = true) => {
|
|
2212
|
+
const value = wipePool.pop() ?? new WipeBehaviour;
|
|
2213
|
+
value.target = target;
|
|
2214
|
+
value.maskGraphic = value.maskGraphic ?? new Graphics9;
|
|
2215
|
+
value.direction = direction;
|
|
2216
|
+
value.reveal = reveal;
|
|
2217
|
+
value.width = Math.max(1, target.width);
|
|
2218
|
+
value.height = Math.max(1, target.height);
|
|
2219
|
+
value.duration = Math.max(0, duration);
|
|
2220
|
+
value.elapsed = 0;
|
|
2221
|
+
value.pooled = false;
|
|
2222
|
+
target.mask = value.maskGraphic;
|
|
2223
|
+
value.updateMask(0);
|
|
2224
|
+
return value;
|
|
2225
|
+
};
|
|
2226
|
+
|
|
2227
|
+
class FlashBehaviour {
|
|
2228
|
+
target;
|
|
2229
|
+
colour = 16777215;
|
|
2230
|
+
duration = 0;
|
|
2231
|
+
elapsed = 0;
|
|
2232
|
+
pooled = false;
|
|
2233
|
+
update(dt) {
|
|
2234
|
+
this.elapsed = Math.min(this.duration, this.elapsed + Math.max(0, dt));
|
|
2235
|
+
this.target.alpha = this.duration <= 0 ? 0 : 1 - this.elapsed / this.duration;
|
|
2236
|
+
return this.elapsed >= this.duration ? "done" : "running";
|
|
2237
|
+
}
|
|
2238
|
+
finish() {
|
|
2239
|
+
this.elapsed = this.duration;
|
|
2240
|
+
this.target.alpha = 0;
|
|
2241
|
+
}
|
|
2242
|
+
dispose() {
|
|
2243
|
+
if (this.pooled)
|
|
2244
|
+
return;
|
|
2245
|
+
this.pooled = true;
|
|
2246
|
+
flashPool.push(this);
|
|
2247
|
+
}
|
|
2248
|
+
}
|
|
2249
|
+
var flashPool = [];
|
|
2250
|
+
var flash = (overlay, colour, duration) => {
|
|
2251
|
+
const value = flashPool.pop() ?? new FlashBehaviour;
|
|
2252
|
+
value.target = overlay;
|
|
2253
|
+
value.colour = colour;
|
|
2254
|
+
value.duration = Math.max(0, duration);
|
|
2255
|
+
value.elapsed = 0;
|
|
2256
|
+
value.pooled = false;
|
|
2257
|
+
overlay.tint = colour;
|
|
2258
|
+
overlay.alpha = 1;
|
|
2259
|
+
return value;
|
|
2260
|
+
};
|
|
2261
|
+
|
|
2262
|
+
// src/effects/catalogue.ts
|
|
2263
|
+
class EffectDecodeError extends Schema.TaggedError()("EffectDecodeError", {
|
|
2264
|
+
effect: Schema.String,
|
|
2265
|
+
parameter: Schema.String,
|
|
2266
|
+
message: Schema.String
|
|
2267
|
+
}) {
|
|
2268
|
+
}
|
|
2269
|
+
|
|
2270
|
+
class UnknownEffect extends Schema.TaggedError()("UnknownEffect", { name: Schema.String }) {
|
|
2271
|
+
}
|
|
2272
|
+
|
|
2273
|
+
class UnknownEffectParameter extends Schema.TaggedError()("UnknownEffectParameter", {
|
|
2274
|
+
effect: Schema.String,
|
|
2275
|
+
parameter: Schema.String
|
|
2276
|
+
}) {
|
|
2277
|
+
}
|
|
2278
|
+
|
|
2279
|
+
class InvalidEffectParameter extends Schema.TaggedError()("InvalidEffectParameter", {
|
|
2280
|
+
effect: Schema.String,
|
|
2281
|
+
parameter: Schema.String,
|
|
2282
|
+
message: Schema.String
|
|
2283
|
+
}) {
|
|
2284
|
+
}
|
|
2285
|
+
var accepted = {
|
|
2286
|
+
fade: ["duration"],
|
|
2287
|
+
dissolve: ["duration"],
|
|
2288
|
+
wipeLeft: ["duration"],
|
|
2289
|
+
wipeRight: ["duration"],
|
|
2290
|
+
wipeUp: ["duration"],
|
|
2291
|
+
wipeDown: ["duration"],
|
|
2292
|
+
slideLeft: ["duration"],
|
|
2293
|
+
slideRight: ["duration"],
|
|
2294
|
+
cut: [],
|
|
2295
|
+
fadeIn: ["duration"],
|
|
2296
|
+
fadeOut: ["duration"],
|
|
2297
|
+
slideIn: ["duration"],
|
|
2298
|
+
slideOut: ["duration"],
|
|
2299
|
+
hop: ["duration"],
|
|
2300
|
+
shake: ["duration", "strength"],
|
|
2301
|
+
none: [],
|
|
2302
|
+
zoom: ["duration", "scale"],
|
|
2303
|
+
pan: ["duration", "x", "y"],
|
|
2304
|
+
flash: ["duration", "color"],
|
|
2305
|
+
blur: ["duration", "strength"],
|
|
2306
|
+
tint: ["duration", "color"]
|
|
2307
|
+
};
|
|
2308
|
+
var numberSchema = Schema.Number;
|
|
2309
|
+
var stringSchema = Schema.String;
|
|
2310
|
+
var decodeNumber = (effect, parameter, value) => typeof value !== "number" ? Effect10.fail(new EffectDecodeError({ effect, parameter, message: "expected a number" })) : Schema.decodeUnknownEffect(numberSchema)(value).pipe(Effect10.mapError(() => new EffectDecodeError({ effect, parameter, message: "expected a number" })));
|
|
2311
|
+
var decodeString = (effect, parameter, value) => typeof value !== "string" ? Effect10.fail(new EffectDecodeError({ effect, parameter, message: "expected a string" })) : Schema.decodeUnknownEffect(stringSchema)(value).pipe(Effect10.mapError(() => new EffectDecodeError({ effect, parameter, message: "expected a string" })));
|
|
2312
|
+
var parse = (name, params) => Effect10.gen(function* () {
|
|
2313
|
+
const allowed = accepted[name];
|
|
2314
|
+
for (const parameter of Object.keys(params)) {
|
|
2315
|
+
if (!allowed.includes(parameter)) {
|
|
2316
|
+
return yield* Effect10.fail(new EffectDecodeError({ effect: name, parameter, message: "unknown parameter" }));
|
|
2317
|
+
}
|
|
2318
|
+
}
|
|
2319
|
+
for (const [parameter, value] of Object.entries(params)) {
|
|
2320
|
+
if (parameter === "duration" || parameter === "strength" || parameter === "scale" || parameter === "x" || parameter === "y") {
|
|
2321
|
+
yield* decodeNumber(name, parameter, value);
|
|
2322
|
+
} else if (parameter === "color") {
|
|
2323
|
+
yield* decodeString(name, parameter, value);
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
});
|
|
2327
|
+
var valueNumber = (effect, params, parameter, fallback) => {
|
|
2328
|
+
const value = params[parameter];
|
|
2329
|
+
return value === undefined ? Effect10.succeed(fallback) : decodeNumber(effect, parameter, value);
|
|
2330
|
+
};
|
|
2331
|
+
var colour = (effect, params) => decodeString(effect, "color", params.color ?? "#ffffff").pipe(Effect10.flatMap((source) => {
|
|
2332
|
+
const trimmed = source.trim();
|
|
2333
|
+
const text = trimmed.startsWith("#") ? trimmed.slice(1) : trimmed.startsWith("0x") ? trimmed.slice(2) : trimmed;
|
|
2334
|
+
return /^[0-9a-fA-F]{6}$/.test(text) ? Effect10.succeed(Number.parseInt(text, 16)) : Effect10.fail(new EffectDecodeError({ effect, parameter: "color", message: "expected #RRGGBB" }));
|
|
2335
|
+
}));
|
|
2336
|
+
var dimension = (target, ctx) => Math.max(1, ctx.design?.width ?? target.width, ctx.design?.height ?? target.height);
|
|
2337
|
+
|
|
2338
|
+
class BlurBehaviour {
|
|
2339
|
+
filter;
|
|
2340
|
+
target;
|
|
2341
|
+
end = 0;
|
|
2342
|
+
duration = 0;
|
|
2343
|
+
elapsed = 0;
|
|
2344
|
+
constructor(target, strength, duration) {
|
|
2345
|
+
this.target = target;
|
|
2346
|
+
this.filter = new BlurFilter({ strength: 0 });
|
|
2347
|
+
this.end = strength;
|
|
2348
|
+
this.duration = Math.max(0, duration);
|
|
2349
|
+
this.target.filters = [this.filter];
|
|
2350
|
+
}
|
|
2351
|
+
update(dt) {
|
|
2352
|
+
this.elapsed = Math.min(this.duration, this.elapsed + Math.max(0, dt));
|
|
2353
|
+
const progress = this.duration <= 0 ? 1 : this.elapsed / this.duration;
|
|
2354
|
+
this.filter.strength = this.end * progress;
|
|
2355
|
+
return this.elapsed >= this.duration ? "done" : "running";
|
|
2356
|
+
}
|
|
2357
|
+
finish() {
|
|
2358
|
+
this.elapsed = this.duration;
|
|
2359
|
+
this.filter.strength = this.end;
|
|
2360
|
+
}
|
|
2361
|
+
dispose() {
|
|
2362
|
+
this.filter.strength = this.end;
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
var parseName = (name) => {
|
|
2366
|
+
switch (name) {
|
|
2367
|
+
case "fade":
|
|
2368
|
+
case "dissolve":
|
|
2369
|
+
case "wipeLeft":
|
|
2370
|
+
case "wipeRight":
|
|
2371
|
+
case "wipeUp":
|
|
2372
|
+
case "wipeDown":
|
|
2373
|
+
case "slideLeft":
|
|
2374
|
+
case "slideRight":
|
|
2375
|
+
case "cut":
|
|
2376
|
+
case "fadeIn":
|
|
2377
|
+
case "fadeOut":
|
|
2378
|
+
case "slideIn":
|
|
2379
|
+
case "slideOut":
|
|
2380
|
+
case "hop":
|
|
2381
|
+
case "shake":
|
|
2382
|
+
case "none":
|
|
2383
|
+
case "zoom":
|
|
2384
|
+
case "pan":
|
|
2385
|
+
case "flash":
|
|
2386
|
+
case "blur":
|
|
2387
|
+
case "tint":
|
|
2388
|
+
return Effect10.succeed(name);
|
|
2389
|
+
default:
|
|
2390
|
+
return Effect10.fail(new EffectDecodeError({ effect: name, parameter: "", message: "unknown effect" }));
|
|
2391
|
+
}
|
|
2392
|
+
};
|
|
2393
|
+
var wipeDirection = (name) => {
|
|
2394
|
+
switch (name) {
|
|
2395
|
+
case "wipeLeft":
|
|
2396
|
+
return "left";
|
|
2397
|
+
case "wipeRight":
|
|
2398
|
+
return "right";
|
|
2399
|
+
case "wipeUp":
|
|
2400
|
+
return "up";
|
|
2401
|
+
case "wipeDown":
|
|
2402
|
+
return "down";
|
|
2403
|
+
default:
|
|
2404
|
+
return "left";
|
|
2405
|
+
}
|
|
2406
|
+
};
|
|
2407
|
+
var build = (name, target, params = {}, duration = 0, ctx = {}) => Effect10.gen(function* () {
|
|
2408
|
+
const effect = yield* parseName(name);
|
|
2409
|
+
yield* parse(effect, params);
|
|
2410
|
+
const seconds = Math.max(0, duration);
|
|
2411
|
+
switch (effect) {
|
|
2412
|
+
case "fade":
|
|
2413
|
+
case "dissolve":
|
|
2414
|
+
return [fade(target, 0, seconds)];
|
|
2415
|
+
case "wipeLeft":
|
|
2416
|
+
case "wipeRight":
|
|
2417
|
+
case "wipeUp":
|
|
2418
|
+
case "wipeDown":
|
|
2419
|
+
return [wipe(target, wipeDirection(effect), seconds, true)];
|
|
2420
|
+
case "slideLeft":
|
|
2421
|
+
return [slide(target, target.x + dimension(target, ctx), target.x, seconds)];
|
|
2422
|
+
case "slideRight":
|
|
2423
|
+
return [slide(target, target.x - dimension(target, ctx), target.x, seconds)];
|
|
2424
|
+
case "cut":
|
|
2425
|
+
case "none":
|
|
2426
|
+
return [];
|
|
2427
|
+
case "fadeIn":
|
|
2428
|
+
return [fade(target, 1, seconds)];
|
|
2429
|
+
case "fadeOut":
|
|
2430
|
+
return [fade(target, 0, seconds)];
|
|
2431
|
+
case "slideIn":
|
|
2432
|
+
return [slide(target, target.x + dimension(target, ctx), target.x, seconds)];
|
|
2433
|
+
case "slideOut":
|
|
2434
|
+
return [slide(target, target.x, target.x - dimension(target, ctx), seconds)];
|
|
2435
|
+
case "hop":
|
|
2436
|
+
return [hop(target, dimension(target, ctx) * 0.08, seconds)];
|
|
2437
|
+
case "shake":
|
|
2438
|
+
return [shake(target, yield* valueNumber(effect, params, "strength", 12), seconds)];
|
|
2439
|
+
case "zoom": {
|
|
2440
|
+
const scale = yield* valueNumber(effect, params, "scale", 1);
|
|
2441
|
+
return [tween(target, { scaleX: target.scale.x, scaleY: target.scale.y }, { scaleX: scale, scaleY: scale }, seconds, linear)];
|
|
2442
|
+
}
|
|
2443
|
+
case "pan": {
|
|
2444
|
+
const toX = yield* valueNumber(effect, params, "x", target.x);
|
|
2445
|
+
const toY = yield* valueNumber(effect, params, "y", target.y);
|
|
2446
|
+
return [tween(target, { x: target.x, y: target.y }, { x: toX, y: toY }, seconds, linear)];
|
|
2447
|
+
}
|
|
2448
|
+
case "flash":
|
|
2449
|
+
return [flash(ctx.overlay ?? target, yield* colour(effect, params), seconds)];
|
|
2450
|
+
case "blur":
|
|
2451
|
+
return [new BlurBehaviour(target, yield* valueNumber(effect, params, "strength", 8), seconds)];
|
|
2452
|
+
case "tint":
|
|
2453
|
+
return [tween(target, { tint: target.tint }, { tint: yield* colour(effect, params) }, seconds, linear)];
|
|
2454
|
+
}
|
|
2455
|
+
});
|
|
2456
|
+
var EffectCatalogue = { build };
|
|
2457
|
+
|
|
2458
|
+
// src/scene/shared.ts
|
|
2459
|
+
import { Container as Container12, Graphics as Graphics10, Text as Text12, TextStyle as TextStyle4 } from "pixi.js";
|
|
2460
|
+
var assetFile = (bindings, assetId) => assetId === undefined ? undefined : bindings.assets?.[assetId] ?? assetId;
|
|
2461
|
+
var finite = (value, fallback) => value !== null && value !== undefined && Number.isFinite(value) ? value : fallback;
|
|
2462
|
+
var textureFor = (assets, file) => file === undefined ? undefined : assets.texture(file);
|
|
2463
|
+
var placeholder = (parent, name, theme, width, height) => {
|
|
2464
|
+
const target = new Container12;
|
|
2465
|
+
target.label = `Placeholder:${name}`;
|
|
2466
|
+
const hash = Array.from(name).reduce((sum, char) => sum * 33 + char.charCodeAt(0) & 16777215, 0);
|
|
2467
|
+
const block = new Graphics10().rect(0, 0, Math.max(1, width), Math.max(1, height)).fill({ color: hash || theme.accent, alpha: 0.8 });
|
|
2468
|
+
const text = new Text12({ text: name, style: new TextStyle4({ fontFamily: theme.font, fontSize: Math.max(12, Math.min(32, height / 5)), fill: theme.text }) });
|
|
2469
|
+
text.anchor?.set?.(0.5);
|
|
2470
|
+
text.x = width / 2;
|
|
2471
|
+
text.y = height / 2;
|
|
2472
|
+
target.addChild(block, text);
|
|
2473
|
+
parent.addChild(target);
|
|
2474
|
+
return target;
|
|
2475
|
+
};
|
|
2476
|
+
var spriteTexture = (bindings, actor) => {
|
|
2477
|
+
const character = bindings.characters?.[actor.character];
|
|
2478
|
+
if (character === undefined)
|
|
2479
|
+
return;
|
|
2480
|
+
const sprite = actor.costume === undefined || actor.costume === null ? character.sprite : character.costumes?.[actor.costume] ?? character.sprite;
|
|
2481
|
+
if (actor.emotion !== undefined && actor.emotion !== null)
|
|
2482
|
+
return sprite.emotions?.[actor.emotion] ?? sprite.default;
|
|
2483
|
+
return sprite.default;
|
|
2484
|
+
};
|
|
2485
|
+
var anchorPlacement = (anchor, design, texture) => {
|
|
2486
|
+
const scale = finite(anchor?.scale, 1);
|
|
2487
|
+
const x = finite(anchor?.x, 0.5) * design.width - texture.width * scale / 2;
|
|
2488
|
+
const y = finite(anchor?.y, 1) * design.height - texture.height * scale;
|
|
2489
|
+
return { x, y, scale };
|
|
2490
|
+
};
|
|
2491
|
+
var cgPlacement = (fit, design, texture, scale = 1, x, y) => {
|
|
2492
|
+
const ratio = fit === "cover" ? Math.max(design.width / Math.max(1, texture.width), design.height / Math.max(1, texture.height)) : Math.min(design.width / Math.max(1, texture.width), design.height / Math.max(1, texture.height));
|
|
2493
|
+
const actualScale = ratio * Math.max(0, scale);
|
|
2494
|
+
const width = texture.width * actualScale;
|
|
2495
|
+
const height = texture.height * actualScale;
|
|
2496
|
+
return { x: x ?? (design.width - width) / 2, y: y ?? (design.height - height) / 2, scale: actualScale, width, height };
|
|
2497
|
+
};
|
|
2498
|
+
var clearContainer = (container) => {
|
|
2499
|
+
for (const child of container.removeChildren())
|
|
2500
|
+
child.destroy({ children: true });
|
|
2501
|
+
};
|
|
2502
|
+
var transitionDuration = (duration, fallback = 0) => Math.max(0, duration ?? fallback);
|
|
2503
|
+
|
|
2504
|
+
// src/scene/actor.ts
|
|
2505
|
+
var resolveActorTexture = spriteTexture;
|
|
2506
|
+
|
|
2507
|
+
class ActorSprite {
|
|
2508
|
+
target;
|
|
2509
|
+
actor;
|
|
2510
|
+
world;
|
|
2511
|
+
bindings;
|
|
2512
|
+
assets;
|
|
2513
|
+
design;
|
|
2514
|
+
theme;
|
|
2515
|
+
logger;
|
|
2516
|
+
logged = new Set;
|
|
2517
|
+
constructor(parent, actor, world, bindings, assets, design, theme, logger = console.warn) {
|
|
2518
|
+
this.actor = actor;
|
|
2519
|
+
this.world = world;
|
|
2520
|
+
this.bindings = bindings;
|
|
2521
|
+
this.assets = assets;
|
|
2522
|
+
this.design = design;
|
|
2523
|
+
this.theme = theme;
|
|
2524
|
+
this.logger = logger;
|
|
2525
|
+
this.target = new Container13;
|
|
2526
|
+
this.target.label = `Actor:${actor.character}`;
|
|
2527
|
+
parent.addChild(this.target);
|
|
2528
|
+
this.updateTexture(actor);
|
|
2529
|
+
}
|
|
2530
|
+
updateTexture(actor) {
|
|
2531
|
+
this.target.removeChildren().forEach((child) => child.destroy({ children: true }));
|
|
2532
|
+
const file = resolveActorTexture(this.bindings, actor);
|
|
2533
|
+
const texture = this.assets.texture(file ?? "");
|
|
2534
|
+
if (texture === undefined) {
|
|
2535
|
+
const key = `character:${actor.character}`;
|
|
2536
|
+
if (!this.logged.has(key)) {
|
|
2537
|
+
this.logged.add(key);
|
|
2538
|
+
this.logger(`Missing binding ${key}`);
|
|
2539
|
+
}
|
|
2540
|
+
placeholder(this.target, key, this.theme, this.design.width / 3, this.design.height * 0.8);
|
|
2541
|
+
} else {
|
|
2542
|
+
const sprite = new Sprite4(texture);
|
|
2543
|
+
sprite.label = `Sprite:${file}`;
|
|
2544
|
+
const anchor = actor.anchor === null || actor.anchor === undefined ? undefined : this.bindings.anchors?.[actor.anchor];
|
|
2545
|
+
const heightScale = Math.min(1, this.design.height * 0.85 / Math.max(1, texture.height));
|
|
2546
|
+
const placement = anchorPlacement(anchor === undefined ? { x: 0.5, y: 1, scale: heightScale } : { ...anchor, scale: anchor.scale ?? heightScale }, this.design, { width: texture.width, height: texture.height });
|
|
2547
|
+
sprite.position.set(placement.x, placement.y);
|
|
2548
|
+
sprite.scale.set(placement.scale);
|
|
2549
|
+
this.target.addChild(sprite);
|
|
2550
|
+
}
|
|
2551
|
+
}
|
|
2552
|
+
play = (action, instant = false, durationOverride) => {
|
|
2553
|
+
if (action === undefined || action.effect === "none" || action.effect === "cut" || instant)
|
|
2554
|
+
return Effect11.void;
|
|
2555
|
+
return EffectCatalogue.build(action.effect, this.target, action.params, durationOverride ?? (typeof action.params.duration === "number" ? action.params.duration : 0)).pipe(Effect11.catch(() => Effect11.succeed([])), Effect11.flatMap((behaviours) => Effect11.forEach(behaviours, (behaviour) => this.world.run(this.target, behaviour), { discard: true })));
|
|
2556
|
+
};
|
|
2557
|
+
destroy() {
|
|
2558
|
+
this.target.destroy({ children: true });
|
|
2559
|
+
}
|
|
2560
|
+
}
|
|
2561
|
+
|
|
2562
|
+
// src/scene/background.ts
|
|
2563
|
+
import { Effect as Effect12 } from "effect";
|
|
2564
|
+
import { Container as Container14, Sprite as Sprite5 } from "pixi.js";
|
|
2565
|
+
var backgroundFile = (bindings, bg) => {
|
|
2566
|
+
if (bg === null || bg === undefined)
|
|
2567
|
+
return;
|
|
2568
|
+
const bound = bg.location === undefined || bg.location === null ? undefined : bindings.backgrounds?.[bg.location];
|
|
2569
|
+
return bg.time !== undefined && bg.time !== null ? bound?.times?.[bg.time] ?? bound?.image ?? assetFile(bindings, bg.assetId ?? undefined) : bound?.image ?? assetFile(bindings, bg.assetId ?? undefined);
|
|
2570
|
+
};
|
|
2571
|
+
|
|
2572
|
+
class Background {
|
|
2573
|
+
target;
|
|
2574
|
+
world;
|
|
2575
|
+
bindings;
|
|
2576
|
+
assets;
|
|
2577
|
+
theme;
|
|
2578
|
+
design;
|
|
2579
|
+
logger;
|
|
2580
|
+
logged = new Set;
|
|
2581
|
+
current;
|
|
2582
|
+
constructor(parent, world, bindings, assets, design, theme, logger = console.warn) {
|
|
2583
|
+
this.target = parent;
|
|
2584
|
+
this.world = world;
|
|
2585
|
+
this.bindings = bindings;
|
|
2586
|
+
this.assets = assets;
|
|
2587
|
+
this.design = design;
|
|
2588
|
+
this.theme = theme;
|
|
2589
|
+
this.logger = logger;
|
|
2590
|
+
parent.label = parent.label || "BackgroundLayer";
|
|
2591
|
+
}
|
|
2592
|
+
set = (bg, transition, instant = false, durationOverride) => {
|
|
2593
|
+
const file = backgroundFile(this.bindings, bg);
|
|
2594
|
+
const texture = textureFor(this.assets, file);
|
|
2595
|
+
if (file === undefined || texture === undefined) {
|
|
2596
|
+
const key = `background:${bg?.location ?? bg?.assetId ?? "missing"}`;
|
|
2597
|
+
if (!this.logged.has(key)) {
|
|
2598
|
+
this.logged.add(key);
|
|
2599
|
+
this.logger(`Missing binding ${key}`);
|
|
2600
|
+
}
|
|
2601
|
+
clearContainer(this.target);
|
|
2602
|
+
const next2 = placeholder(this.target, key, this.theme, this.design.width, this.design.height);
|
|
2603
|
+
next2.alpha = 1;
|
|
2604
|
+
this.current = next2;
|
|
2605
|
+
return Effect12.void;
|
|
2606
|
+
}
|
|
2607
|
+
const next = new Container14;
|
|
2608
|
+
next.label = `Background:${file}`;
|
|
2609
|
+
const sprite = new Sprite5(texture);
|
|
2610
|
+
sprite.label = `Texture:${file}`;
|
|
2611
|
+
sprite.width = this.design.width;
|
|
2612
|
+
sprite.height = this.design.height;
|
|
2613
|
+
next.addChild(sprite);
|
|
2614
|
+
this.target.addChild(next);
|
|
2615
|
+
const previous = this.current;
|
|
2616
|
+
this.current = next;
|
|
2617
|
+
const duration = instant ? 0 : transitionDuration(durationOverride, typeof transition?.params.duration === "number" ? transition.params.duration : 0);
|
|
2618
|
+
if (previous === undefined || duration <= 0) {
|
|
2619
|
+
if (previous !== undefined)
|
|
2620
|
+
previous.destroy({ children: true });
|
|
2621
|
+
next.alpha = 1;
|
|
2622
|
+
return Effect12.void;
|
|
2623
|
+
}
|
|
2624
|
+
const behaviour = crossfade(previous, next, duration);
|
|
2625
|
+
return this.world.run(this.target, behaviour).pipe(Effect12.tap(() => Effect12.sync(() => previous.destroy({ children: true }))));
|
|
2626
|
+
};
|
|
2627
|
+
}
|
|
2628
|
+
|
|
2629
|
+
// src/scene/camera.ts
|
|
2630
|
+
import { Effect as Effect13 } from "effect";
|
|
2631
|
+
var framingPlacement = (framing, design, focus) => {
|
|
2632
|
+
const zoom = framing?.zoom ?? 1;
|
|
2633
|
+
const centreX = design.width / 2;
|
|
2634
|
+
const centreY = design.height / 2;
|
|
2635
|
+
const x = focus === undefined ? centreX + (framing?.x ?? 0) : centreX + (centreX - focus.x) * zoom + (framing?.x ?? 0);
|
|
2636
|
+
const y = focus === undefined ? centreY + (framing?.y ?? 0) : centreY + (centreY - focus.y) * zoom + (framing?.y ?? 0);
|
|
2637
|
+
return { scale: zoom, x, y };
|
|
2638
|
+
};
|
|
2639
|
+
|
|
2640
|
+
class Camera {
|
|
2641
|
+
target;
|
|
2642
|
+
world;
|
|
2643
|
+
bindings;
|
|
2644
|
+
design;
|
|
2645
|
+
actors;
|
|
2646
|
+
logger;
|
|
2647
|
+
constructor(target, actors, world, bindings, design, logger = console.warn) {
|
|
2648
|
+
this.target = target;
|
|
2649
|
+
this.actors = actors;
|
|
2650
|
+
this.world = world;
|
|
2651
|
+
this.bindings = bindings;
|
|
2652
|
+
this.design = design;
|
|
2653
|
+
this.logger = logger;
|
|
2654
|
+
}
|
|
2655
|
+
clear = () => {
|
|
2656
|
+
this.target.scale.set(1);
|
|
2657
|
+
this.target.position.set(this.design.width / 2, this.design.height / 2);
|
|
2658
|
+
this.target.pivot.set(this.design.width / 2, this.design.height / 2);
|
|
2659
|
+
};
|
|
2660
|
+
apply = (framingName, focusName) => {
|
|
2661
|
+
const framing = framingName === undefined ? undefined : this.bindings.framing?.[framingName];
|
|
2662
|
+
const focus = focusName === undefined ? undefined : this.findActor(focusName);
|
|
2663
|
+
if (focusName !== undefined && focus === undefined)
|
|
2664
|
+
this.logger(`Missing actor focus:${focusName}`);
|
|
2665
|
+
const placement = framingPlacement(framing, this.design, focus);
|
|
2666
|
+
this.target.scale.set(placement.scale);
|
|
2667
|
+
this.target.position.set(placement.x, placement.y);
|
|
2668
|
+
};
|
|
2669
|
+
move = (move, duration, instant = false) => {
|
|
2670
|
+
if (move === undefined || instant)
|
|
2671
|
+
return Effect13.void;
|
|
2672
|
+
return EffectCatalogue.build(move.effect, this.target, move.params, duration ?? (typeof move.params.duration === "number" ? move.params.duration : 0), { design: this.design, camera: this.target }).pipe(Effect13.catch(() => Effect13.succeed([])), Effect13.flatMap((behaviours) => Effect13.forEach(behaviours, (behaviour) => this.world.run(this.target, behaviour), { discard: true })));
|
|
2673
|
+
};
|
|
2674
|
+
findActor = (character) => {
|
|
2675
|
+
const actor = this.actors.children.find((child) => child.label === `Actor:${character}`);
|
|
2676
|
+
return actor === undefined ? undefined : { x: actor.x + actor.width / 2, y: actor.y + actor.height / 2 };
|
|
2677
|
+
};
|
|
2678
|
+
}
|
|
2679
|
+
|
|
2680
|
+
// src/scene/cgLayer.ts
|
|
2681
|
+
import { Effect as Effect14 } from "effect";
|
|
2682
|
+
import { Container as Container15, Sprite as Sprite6 } from "pixi.js";
|
|
2683
|
+
class CgLayer {
|
|
2684
|
+
target;
|
|
2685
|
+
world;
|
|
2686
|
+
bindings;
|
|
2687
|
+
assets;
|
|
2688
|
+
design;
|
|
2689
|
+
theme;
|
|
2690
|
+
logger;
|
|
2691
|
+
current;
|
|
2692
|
+
constructor(parent, world, bindings, assets, design, theme, logger = console.warn) {
|
|
2693
|
+
this.target = parent;
|
|
2694
|
+
this.world = world;
|
|
2695
|
+
this.bindings = bindings;
|
|
2696
|
+
this.assets = assets;
|
|
2697
|
+
this.design = design;
|
|
2698
|
+
this.theme = theme;
|
|
2699
|
+
this.logger = logger;
|
|
2700
|
+
}
|
|
2701
|
+
show = (cg, transition, instant = false, durationOverride) => {
|
|
2702
|
+
if (cg === null || cg === undefined) {
|
|
2703
|
+
const old = this.current;
|
|
2704
|
+
this.current = undefined;
|
|
2705
|
+
return old === undefined ? Effect14.void : Effect14.sync(() => {
|
|
2706
|
+
clearContainer(this.target);
|
|
2707
|
+
old.destroy({ children: true });
|
|
2708
|
+
});
|
|
2709
|
+
}
|
|
2710
|
+
const file = assetFile(this.bindings, cg.assetId);
|
|
2711
|
+
const texture = textureFor(this.assets, file);
|
|
2712
|
+
const next = new Container15;
|
|
2713
|
+
next.label = `Cg:${cg.assetId}`;
|
|
2714
|
+
if (texture === undefined) {
|
|
2715
|
+
this.logger(`Missing binding cg:${cg.assetId}`);
|
|
2716
|
+
placeholder(next, `cg:${cg.assetId}`, this.theme, this.design.width, this.design.height);
|
|
2717
|
+
} else {
|
|
2718
|
+
const config = cg.layout === null || cg.layout === undefined ? undefined : this.bindings.cgLayouts?.[cg.layout];
|
|
2719
|
+
const placement = cgPlacement(config?.fit ?? "contain", this.design, { width: texture.width, height: texture.height }, config?.scale ?? 1, config?.x, config?.y);
|
|
2720
|
+
const sprite = new Sprite6(texture);
|
|
2721
|
+
sprite.label = `Texture:${file}`;
|
|
2722
|
+
sprite.position.set(placement.x, placement.y);
|
|
2723
|
+
sprite.scale.set(placement.scale);
|
|
2724
|
+
next.addChild(sprite);
|
|
2725
|
+
}
|
|
2726
|
+
this.target.addChild(next);
|
|
2727
|
+
const previous = this.current;
|
|
2728
|
+
this.current = next;
|
|
2729
|
+
const duration = instant ? 0 : transitionDuration(durationOverride, typeof transition?.params.duration === "number" ? transition.params.duration : 0);
|
|
2730
|
+
if (previous === undefined || duration <= 0) {
|
|
2731
|
+
previous?.destroy({ children: true });
|
|
2732
|
+
return Effect14.void;
|
|
2733
|
+
}
|
|
2734
|
+
return this.world.run(this.target, crossfade(previous, next, duration)).pipe(Effect14.tap(() => Effect14.sync(() => previous.destroy({ children: true }))));
|
|
2735
|
+
};
|
|
2736
|
+
}
|
|
2737
|
+
|
|
2738
|
+
// src/scene/videoLayer.ts
|
|
2739
|
+
import { Effect as Effect15 } from "effect";
|
|
2740
|
+
import { Sprite as Sprite7 } from "pixi.js";
|
|
2741
|
+
class VideoLayer {
|
|
2742
|
+
target;
|
|
2743
|
+
assets;
|
|
2744
|
+
bindings;
|
|
2745
|
+
design;
|
|
2746
|
+
theme;
|
|
2747
|
+
logger;
|
|
2748
|
+
active;
|
|
2749
|
+
constructor(parent, _world, bindings, assets, design, theme, logger = console.warn) {
|
|
2750
|
+
this.target = parent;
|
|
2751
|
+
this.bindings = bindings;
|
|
2752
|
+
this.assets = assets;
|
|
2753
|
+
this.design = design;
|
|
2754
|
+
this.theme = theme;
|
|
2755
|
+
this.logger = logger;
|
|
2756
|
+
parent.label = parent.label || "VideoLayer";
|
|
2757
|
+
}
|
|
2758
|
+
show = (video, blocking, skip = false) => {
|
|
2759
|
+
clearContainer(this.target);
|
|
2760
|
+
this.active = undefined;
|
|
2761
|
+
if (video === null || video === undefined)
|
|
2762
|
+
return Effect15.void;
|
|
2763
|
+
const file = assetFile(this.bindings, video.assetId);
|
|
2764
|
+
const texture = file === undefined ? undefined : this.assets.video(file) ?? this.assets.texture(file);
|
|
2765
|
+
if (texture === undefined) {
|
|
2766
|
+
this.logger(`Missing binding video:${video.assetId}`);
|
|
2767
|
+
placeholder(this.target, `video:${video.assetId}`, this.theme, this.design.width, this.design.height);
|
|
2768
|
+
return Effect15.void;
|
|
2769
|
+
}
|
|
2770
|
+
const sprite = new Sprite7(texture);
|
|
2771
|
+
sprite.width = this.design.width;
|
|
2772
|
+
sprite.height = this.design.height;
|
|
2773
|
+
this.target.addChild(sprite);
|
|
2774
|
+
const resource = texture.source.resource;
|
|
2775
|
+
if (!(resource instanceof HTMLVideoElement))
|
|
2776
|
+
return Effect15.void;
|
|
2777
|
+
this.active = resource;
|
|
2778
|
+
resource.currentTime = 0;
|
|
2779
|
+
resource.loop = false;
|
|
2780
|
+
if (skip) {
|
|
2781
|
+
if (Number.isFinite(resource.duration) && resource.duration > 0)
|
|
2782
|
+
resource.currentTime = resource.duration;
|
|
2783
|
+
resource.pause();
|
|
2784
|
+
return Effect15.void;
|
|
2785
|
+
}
|
|
2786
|
+
const play = resource.play();
|
|
2787
|
+
if (!blocking)
|
|
2788
|
+
return Effect15.promise(() => play.then(() => {
|
|
2789
|
+
return;
|
|
2790
|
+
}).catch(() => {
|
|
2791
|
+
return;
|
|
2792
|
+
}));
|
|
2793
|
+
return Effect15.callback((resume) => {
|
|
2794
|
+
const finish = () => resume(Effect15.succeed(undefined));
|
|
2795
|
+
resource.addEventListener("ended", finish, { once: true });
|
|
2796
|
+
resource.addEventListener("error", finish, { once: true });
|
|
2797
|
+
play.catch(finish);
|
|
2798
|
+
return Effect15.sync(() => {
|
|
2799
|
+
resource.removeEventListener("ended", finish);
|
|
2800
|
+
resource.removeEventListener("error", finish);
|
|
2801
|
+
});
|
|
2802
|
+
});
|
|
2803
|
+
};
|
|
2804
|
+
hide = () => Effect15.sync(() => {
|
|
2805
|
+
if (this.active !== undefined)
|
|
2806
|
+
this.active.pause();
|
|
2807
|
+
clearContainer(this.target);
|
|
2808
|
+
this.active = undefined;
|
|
2809
|
+
});
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
// src/scene/vfx.ts
|
|
2813
|
+
import { Effect as Effect16 } from "effect";
|
|
2814
|
+
import { Container as Container17 } from "pixi.js";
|
|
2815
|
+
class Vfx {
|
|
2816
|
+
target;
|
|
2817
|
+
world;
|
|
2818
|
+
design;
|
|
2819
|
+
constructor(parent, world, design) {
|
|
2820
|
+
this.world = world;
|
|
2821
|
+
this.design = design;
|
|
2822
|
+
this.target = new Container17;
|
|
2823
|
+
this.target.label = "Vfx";
|
|
2824
|
+
parent.addChild(this.target);
|
|
2825
|
+
}
|
|
2826
|
+
play = (effect, duration, instant = false) => {
|
|
2827
|
+
const target = new Container17;
|
|
2828
|
+
target.label = `Vfx:${effect.effect}`;
|
|
2829
|
+
this.target.alpha = 1;
|
|
2830
|
+
this.target.addChild(target);
|
|
2831
|
+
const seconds = duration ?? (typeof effect.params.duration === "number" ? effect.params.duration : 0);
|
|
2832
|
+
if (instant)
|
|
2833
|
+
return Effect16.sync(() => {
|
|
2834
|
+
target.alpha = 0;
|
|
2835
|
+
target.removeFromParent();
|
|
2836
|
+
target.visible = false;
|
|
2837
|
+
this.target.alpha = 0;
|
|
2838
|
+
});
|
|
2839
|
+
return EffectCatalogue.build(effect.effect, target, effect.params, seconds, { design: this.design, overlay: target }).pipe(Effect16.catch(() => Effect16.succeed([])), Effect16.flatMap((behaviours) => Effect16.forEach(behaviours, (behaviour) => this.world.run(target, behaviour), { discard: true })), Effect16.tap(() => Effect16.sync(() => {
|
|
2840
|
+
target.alpha = 0;
|
|
2841
|
+
target.removeFromParent();
|
|
2842
|
+
target.visible = false;
|
|
2843
|
+
this.target.alpha = 0;
|
|
2844
|
+
})));
|
|
2845
|
+
};
|
|
2846
|
+
}
|
|
2847
|
+
|
|
2848
|
+
// src/presenter/pixiPresenter.ts
|
|
2849
|
+
var PixiPresenterTag = Context4.Service("Lute.Web.PixiPresenter");
|
|
2850
|
+
|
|
2851
|
+
class PixiPresenter {
|
|
2852
|
+
static layer = (bindings, theme, assets) => Layer4.effect(PixiPresenterTag, Effect17.gen(function* () {
|
|
2853
|
+
const pixi = yield* PixiApp;
|
|
2854
|
+
const world = yield* World;
|
|
2855
|
+
const ui = yield* UiTag;
|
|
2856
|
+
return new PixiPresenter({ pixi, world, ui, assets, bindings, theme });
|
|
2857
|
+
}));
|
|
2858
|
+
stage;
|
|
2859
|
+
showLine;
|
|
2860
|
+
revealLine;
|
|
2861
|
+
clearLine;
|
|
2862
|
+
restore;
|
|
2863
|
+
showChoice;
|
|
2864
|
+
hideChoice;
|
|
2865
|
+
showScreen;
|
|
2866
|
+
hideScreen;
|
|
2867
|
+
setUiHidden;
|
|
2868
|
+
applySettings;
|
|
2869
|
+
capture;
|
|
2870
|
+
actions;
|
|
2871
|
+
pixi;
|
|
2872
|
+
world;
|
|
2873
|
+
ui;
|
|
2874
|
+
assets;
|
|
2875
|
+
bindings;
|
|
2876
|
+
theme;
|
|
2877
|
+
logger;
|
|
2878
|
+
actors = new Map;
|
|
2879
|
+
background;
|
|
2880
|
+
cg;
|
|
2881
|
+
video;
|
|
2882
|
+
camera;
|
|
2883
|
+
vfx;
|
|
2884
|
+
dialogue;
|
|
2885
|
+
choice;
|
|
2886
|
+
choiceActions;
|
|
2887
|
+
missing = new Set;
|
|
2888
|
+
uiHidden = false;
|
|
2889
|
+
constructor(options) {
|
|
2890
|
+
this.pixi = options.pixi;
|
|
2891
|
+
this.world = options.world;
|
|
2892
|
+
this.ui = options.ui;
|
|
2893
|
+
this.assets = options.assets;
|
|
2894
|
+
this.bindings = options.bindings;
|
|
2895
|
+
this.theme = options.theme;
|
|
2896
|
+
this.logger = options.logger ?? ((message, error) => console.warn(message, error));
|
|
2897
|
+
const design = this.pixi.design;
|
|
2898
|
+
this.background = new Background(this.pixi.layers.background, this.world, this.bindings, this.assets, design, this.theme, (message) => this.logMissing(message));
|
|
2899
|
+
this.cg = new CgLayer(this.pixi.layers.cg, this.world, this.bindings, this.assets, design, this.theme, (message) => this.logMissing(message));
|
|
2900
|
+
this.video = new VideoLayer(this.pixi.layers.video, this.world, this.bindings, this.assets, design, this.theme, (message) => this.logMissing(message));
|
|
2901
|
+
this.camera = new Camera(this.pixi.layers.camera, this.pixi.layers.actors, this.world, this.bindings, design, (message) => this.logMissing(message));
|
|
2902
|
+
this.vfx = new Vfx(this.pixi.layers.overlay, this.world, design);
|
|
2903
|
+
const nameColors = {};
|
|
2904
|
+
for (const [character, binding] of Object.entries(this.bindings.characters ?? {})) {
|
|
2905
|
+
if (binding.nameColor !== null && binding.nameColor !== undefined) {
|
|
2906
|
+
const text = binding.nameColor.replace(/^#/, "");
|
|
2907
|
+
if (/^[0-9a-f]{6}$/i.test(text))
|
|
2908
|
+
nameColors[character] = Number.parseInt(text, 16);
|
|
2909
|
+
}
|
|
2910
|
+
}
|
|
2911
|
+
const textStyles = Object.fromEntries(Object.entries(this.bindings.textStyles ?? {}).map(([name, style]) => [name, {
|
|
2912
|
+
...style.fontWeight === null ? {} : { fontWeight: style.fontWeight },
|
|
2913
|
+
...style.fontStyle === null ? {} : { fontStyle: style.fontStyle },
|
|
2914
|
+
...style.color === null ? {} : { color: style.color },
|
|
2915
|
+
...style.alpha === null ? {} : { alpha: style.alpha },
|
|
2916
|
+
...style.scale === null ? {} : { scale: style.scale },
|
|
2917
|
+
...style.letterSpacing === null ? {} : { letterSpacing: style.letterSpacing }
|
|
2918
|
+
}]));
|
|
2919
|
+
this.dialogue = new DialogueWindow(this.pixi.layers.dialogue, this.world, this.theme, { nameColors, textStyles });
|
|
2920
|
+
this.dialogue.relayout(design);
|
|
2921
|
+
this.choice = new ChoiceMenu(this.pixi.layers.choice, this.theme);
|
|
2922
|
+
this.choiceActions = Effect17.runSync(PubSub2.unbounded());
|
|
2923
|
+
this.actions = Stream6.merge(this.ui.actions, Stream6.fromPubSub(this.choiceActions));
|
|
2924
|
+
this.stage = (cue, mode) => this.stageCue(cue, mode);
|
|
2925
|
+
this.showLine = (line, mode) => this.dialogue.show(line, this.defaultSettings, mode);
|
|
2926
|
+
this.revealLine = this.dialogue.reveal;
|
|
2927
|
+
this.clearLine = this.dialogue.clear;
|
|
2928
|
+
this.restore = (stage) => this.restoreStage(stage);
|
|
2929
|
+
this.showChoice = (choice, remaining) => this.showChoiceInternal(choice, remaining);
|
|
2930
|
+
this.hideChoice = this.choice.hide;
|
|
2931
|
+
this.showScreen = (screen) => {
|
|
2932
|
+
const modal = ["Menu", "Backlog", "SaveLoad", "Settings", "Confirm"].includes(screen._tag);
|
|
2933
|
+
const hideDialogue = modal ? Effect17.void : Effect17.andThen(this.dialogue.clear, Effect17.sync(() => {
|
|
2934
|
+
this.dialogue.setHidden(true);
|
|
2935
|
+
}));
|
|
2936
|
+
return Effect17.andThen(hideDialogue, screen._tag === "Title" && options.titleBackground !== undefined ? this.ui.show({ ...screen, background: options.titleBackground }) : this.ui.show(screen));
|
|
2937
|
+
};
|
|
2938
|
+
this.hideScreen = Effect17.andThen(this.ui.hide, Effect17.sync(() => {
|
|
2939
|
+
this.dialogue.setHidden(this.uiHidden);
|
|
2940
|
+
}));
|
|
2941
|
+
this.setUiHidden = (hidden) => Effect17.sync(() => {
|
|
2942
|
+
this.uiHidden = hidden;
|
|
2943
|
+
this.dialogue.setHidden(hidden);
|
|
2944
|
+
});
|
|
2945
|
+
this.applySettings = (settings) => Effect17.sync(() => {
|
|
2946
|
+
this.defaultSettings = { ...this.defaultSettings, ...settings };
|
|
2947
|
+
this.dialogue.setOpacity(this.defaultSettings.windowOpacity);
|
|
2948
|
+
});
|
|
2949
|
+
this.capture = Effect17.tryPromise(() => this.pixi.app.renderer.extract.base64({ target: this.pixi.app.stage, format: "png", resolution: Math.min(320 / design.width, 180 / design.height) })).pipe(Effect17.catch(() => Effect17.succeed("data:image/png;base64,")));
|
|
2950
|
+
}
|
|
2951
|
+
defaultSettings = { locale: "", textSpeed: 30, windowOpacity: 1, autoDelay: 1, readColour: true, skipMode: "all", rollbackDepth: 0, volumes: { master: 1, music: 1, sfx: 1, voice: 1 } };
|
|
2952
|
+
stageCue = (cue, mode) => {
|
|
2953
|
+
const instant = mode === "skip";
|
|
2954
|
+
switch (cue._tag) {
|
|
2955
|
+
case "Background":
|
|
2956
|
+
return this.background.set(cue.bg, cue.transition, instant, cue.duration);
|
|
2957
|
+
case "ActorShow":
|
|
2958
|
+
return this.upsertActor(cue.actor, cue.action, instant, cue.duration);
|
|
2959
|
+
case "ActorExit":
|
|
2960
|
+
return this.exitActor(cue.character, cue.action, instant, cue.duration);
|
|
2961
|
+
case "Cg":
|
|
2962
|
+
return this.cg.show(cue.cg, cue.transition, instant, cue.duration);
|
|
2963
|
+
case "Video":
|
|
2964
|
+
return cue.video === null ? this.video.hide() : this.video.show(cue.video, cue.blocking, instant);
|
|
2965
|
+
case "Sequence": {
|
|
2966
|
+
const file = cue.video === undefined ? this.bindings.sequences?.[cue.name]?.video : cue.video;
|
|
2967
|
+
const stageVideo = file === undefined ? null : { assetId: file };
|
|
2968
|
+
return stageVideo === null ? this.video.hide() : this.video.show(stageVideo, true, instant);
|
|
2969
|
+
}
|
|
2970
|
+
case "Camera": {
|
|
2971
|
+
return Effect17.sync(() => this.camera.apply(cue.framing, cue.focus)).pipe(Effect17.flatMap(() => this.camera.move(cue.move ?? cue.transition, cue.duration, instant)));
|
|
2972
|
+
}
|
|
2973
|
+
case "Vfx":
|
|
2974
|
+
return this.vfx.play(cue.effect, cue.duration, instant);
|
|
2975
|
+
}
|
|
2976
|
+
};
|
|
2977
|
+
upsertActor = (actor, action, instant, duration) => {
|
|
2978
|
+
const current = this.actors.get(actor.character);
|
|
2979
|
+
if (current === undefined) {
|
|
2980
|
+
const created = new ActorSprite(this.pixi.layers.actors, actor, this.world, this.bindings, this.assets, this.pixi.design, this.theme, (message) => this.logMissing(message));
|
|
2981
|
+
this.actors.set(actor.character, created);
|
|
2982
|
+
return created.play(action, instant, duration);
|
|
2983
|
+
}
|
|
2984
|
+
current.updateTexture(actor);
|
|
2985
|
+
return current.play(action, instant, duration);
|
|
2986
|
+
};
|
|
2987
|
+
exitActor = (character, action, instant, duration) => {
|
|
2988
|
+
const actor = this.actors.get(character);
|
|
2989
|
+
if (actor === undefined)
|
|
2990
|
+
return Effect17.void;
|
|
2991
|
+
this.actors.delete(character);
|
|
2992
|
+
return actor.play(action, instant, duration).pipe(Effect17.tap(() => Effect17.sync(() => actor.destroy())));
|
|
2993
|
+
};
|
|
2994
|
+
restoreStage = (stage) => Effect17.sync(() => {
|
|
2995
|
+
clearContainer(this.pixi.layers.background);
|
|
2996
|
+
clearContainer(this.pixi.layers.cg);
|
|
2997
|
+
clearContainer(this.pixi.layers.video);
|
|
2998
|
+
clearContainer(this.pixi.layers.actors);
|
|
2999
|
+
this.actors.clear();
|
|
3000
|
+
this.camera.clear();
|
|
3001
|
+
}).pipe(Effect17.flatMap(() => this.background.set(stage.bg, undefined, true)), Effect17.flatMap(() => Effect17.forEach(stage.actors, (actor) => this.upsertActor(actor, undefined, true), { discard: true })), Effect17.flatMap(() => this.cg.show(stage.cg, undefined, true)), Effect17.flatMap(() => this.video.show(stage.video, false, true)));
|
|
3002
|
+
showChoiceInternal = (choice, remaining) => this.choice.show(choice, remaining).pipe(Effect17.catch(() => Effect17.succeed("")), Effect17.tap((option) => Effect17.sync(() => {
|
|
3003
|
+
Effect17.runSync(PubSub2.publish(this.choiceActions, { type: "choose", option }));
|
|
3004
|
+
})));
|
|
3005
|
+
logMissing = (message) => {
|
|
3006
|
+
if (this.missing.has(message))
|
|
3007
|
+
return;
|
|
3008
|
+
this.missing.add(message);
|
|
3009
|
+
this.logger(message);
|
|
3010
|
+
};
|
|
3011
|
+
}
|
|
3012
|
+
|
|
3013
|
+
// src/input/map.ts
|
|
3014
|
+
import { Effect as Effect18, Queue as Queue2, Stream as Stream7 } from "effect";
|
|
3015
|
+
var defaultInputMap = {
|
|
3016
|
+
keyboard: {
|
|
3017
|
+
Enter: { type: "advance" },
|
|
3018
|
+
" ": { type: "advance" },
|
|
3019
|
+
a: { type: "toggleAuto" },
|
|
3020
|
+
A: { type: "toggleAuto" },
|
|
3021
|
+
s: { type: "toggleSkip" },
|
|
3022
|
+
S: { type: "toggleSkip" },
|
|
3023
|
+
Escape: { type: "openMenu" },
|
|
3024
|
+
PageUp: { type: "openBacklog" },
|
|
3025
|
+
F5: { type: "quickSave" },
|
|
3026
|
+
F9: { type: "quickLoad" },
|
|
3027
|
+
h: { type: "hideUI" },
|
|
3028
|
+
H: { type: "hideUI" },
|
|
3029
|
+
Backspace: { type: "rollback" }
|
|
3030
|
+
},
|
|
3031
|
+
pointer: { click: { type: "advance" }, contextmenu: { type: "openMenu" }, middle: { type: "hideUI" } }
|
|
3032
|
+
};
|
|
3033
|
+
function resolveKeyboardAction(event, map = defaultInputMap) {
|
|
3034
|
+
if (event.key === "Control")
|
|
3035
|
+
return;
|
|
3036
|
+
const binding = map.keyboard[event.key];
|
|
3037
|
+
if (binding === undefined)
|
|
3038
|
+
return;
|
|
3039
|
+
if (isKeyBinding(binding)) {
|
|
3040
|
+
if (binding.ctrl !== undefined && binding.ctrl !== event.ctrlKey)
|
|
3041
|
+
return;
|
|
3042
|
+
if (binding.shift !== undefined && binding.shift !== event.shiftKey)
|
|
3043
|
+
return;
|
|
3044
|
+
if (binding.alt !== undefined && binding.alt !== event.altKey)
|
|
3045
|
+
return;
|
|
3046
|
+
return binding.action;
|
|
3047
|
+
}
|
|
3048
|
+
return binding;
|
|
3049
|
+
}
|
|
3050
|
+
|
|
3051
|
+
class LongPressRecognizer {
|
|
3052
|
+
started = -1;
|
|
3053
|
+
emitted = false;
|
|
3054
|
+
start(at) {
|
|
3055
|
+
this.started = at;
|
|
3056
|
+
this.emitted = false;
|
|
3057
|
+
}
|
|
3058
|
+
end(at, threshold = 500) {
|
|
3059
|
+
const result = this.started >= 0 && at - this.started >= threshold && !this.emitted;
|
|
3060
|
+
this.started = -1;
|
|
3061
|
+
this.emitted = result;
|
|
3062
|
+
return result;
|
|
3063
|
+
}
|
|
3064
|
+
check(at, threshold = 500) {
|
|
3065
|
+
if (this.started < 0 || this.emitted || at - this.started < threshold)
|
|
3066
|
+
return false;
|
|
3067
|
+
this.emitted = true;
|
|
3068
|
+
return true;
|
|
3069
|
+
}
|
|
3070
|
+
cancel() {
|
|
3071
|
+
this.started = -1;
|
|
3072
|
+
this.emitted = false;
|
|
3073
|
+
}
|
|
3074
|
+
}
|
|
3075
|
+
|
|
3076
|
+
class SwipeRecognizer {
|
|
3077
|
+
startY = 0;
|
|
3078
|
+
active = false;
|
|
3079
|
+
start(y) {
|
|
3080
|
+
this.startY = y;
|
|
3081
|
+
this.active = true;
|
|
3082
|
+
}
|
|
3083
|
+
end(y, threshold = 48) {
|
|
3084
|
+
if (!this.active)
|
|
3085
|
+
return;
|
|
3086
|
+
this.active = false;
|
|
3087
|
+
const delta = y - this.startY;
|
|
3088
|
+
if (Math.abs(delta) < threshold)
|
|
3089
|
+
return;
|
|
3090
|
+
return delta < 0 ? "up" : "down";
|
|
3091
|
+
}
|
|
3092
|
+
}
|
|
3093
|
+
function layer(element, map = defaultInputMap, uiConsumes, uiPointer) {
|
|
3094
|
+
let emit = () => {};
|
|
3095
|
+
return Stream7.callback((queue) => Effect18.acquireRelease(Effect18.sync(() => {
|
|
3096
|
+
emit = (action) => {
|
|
3097
|
+
Queue2.offerUnsafe(queue, action);
|
|
3098
|
+
};
|
|
3099
|
+
const longPress = new LongPressRecognizer;
|
|
3100
|
+
const swipe = new SwipeRecognizer;
|
|
3101
|
+
let controlHeld = false;
|
|
3102
|
+
const down = (event) => {
|
|
3103
|
+
if (uiConsumes?.() === true || event.defaultPrevented) {
|
|
3104
|
+
longPress.cancel();
|
|
3105
|
+
return;
|
|
3106
|
+
}
|
|
3107
|
+
longPress.start(event.timeStamp);
|
|
3108
|
+
swipe.start(event.clientY);
|
|
3109
|
+
window.setTimeout(() => {
|
|
3110
|
+
if (longPress.check(performance.now()))
|
|
3111
|
+
emit({ type: "openMenu" });
|
|
3112
|
+
}, 500);
|
|
3113
|
+
};
|
|
3114
|
+
const up = (event) => {
|
|
3115
|
+
if (event.button !== 0)
|
|
3116
|
+
return;
|
|
3117
|
+
if (uiConsumes?.() === true) {
|
|
3118
|
+
longPress.cancel();
|
|
3119
|
+
uiPointer?.(event);
|
|
3120
|
+
return;
|
|
3121
|
+
}
|
|
3122
|
+
const long = longPress.end(event.timeStamp);
|
|
3123
|
+
if (long)
|
|
3124
|
+
emit({ type: "openMenu" });
|
|
3125
|
+
else if (swipe.end(event.clientY) === "down")
|
|
3126
|
+
emit({ type: "openBacklog" });
|
|
3127
|
+
else
|
|
3128
|
+
emit(map.pointer.click ?? { type: "advance" });
|
|
3129
|
+
};
|
|
3130
|
+
const key = (event) => {
|
|
3131
|
+
if (event.defaultPrevented)
|
|
3132
|
+
return;
|
|
3133
|
+
if (uiConsumes?.() === true) {
|
|
3134
|
+
event.preventDefault();
|
|
3135
|
+
return;
|
|
3136
|
+
}
|
|
3137
|
+
if (event.key === "Control" && !controlHeld) {
|
|
3138
|
+
controlHeld = true;
|
|
3139
|
+
emit({ type: "skipHold", held: true });
|
|
3140
|
+
}
|
|
3141
|
+
const action = resolveKeyboardAction(event, map);
|
|
3142
|
+
if (action !== undefined) {
|
|
3143
|
+
event.preventDefault();
|
|
3144
|
+
emit(action);
|
|
3145
|
+
}
|
|
3146
|
+
};
|
|
3147
|
+
const keyup = (event) => {
|
|
3148
|
+
if (event.key === "Control" && controlHeld) {
|
|
3149
|
+
controlHeld = false;
|
|
3150
|
+
emit({ type: "skipHold", held: false });
|
|
3151
|
+
}
|
|
3152
|
+
};
|
|
3153
|
+
const wheel = (event) => {
|
|
3154
|
+
if (event.deltaY < 0) {
|
|
3155
|
+
event.preventDefault();
|
|
3156
|
+
emit({ type: "openBacklog" });
|
|
3157
|
+
}
|
|
3158
|
+
if (event.defaultPrevented)
|
|
3159
|
+
return;
|
|
3160
|
+
};
|
|
3161
|
+
const pointer = (event) => {
|
|
3162
|
+
if (event.button === 1) {
|
|
3163
|
+
event.preventDefault();
|
|
3164
|
+
emit(map.pointer.middle ?? { type: "hideUI" });
|
|
3165
|
+
}
|
|
3166
|
+
if (event.button === 2) {
|
|
3167
|
+
event.preventDefault();
|
|
3168
|
+
emit(map.pointer.contextmenu ?? { type: "openMenu" });
|
|
3169
|
+
}
|
|
3170
|
+
};
|
|
3171
|
+
const cancel = () => longPress.cancel();
|
|
3172
|
+
const context = (event) => event.preventDefault();
|
|
3173
|
+
element.addEventListener("keydown", key);
|
|
3174
|
+
element.addEventListener("keyup", keyup);
|
|
3175
|
+
element.addEventListener("pointerdown", down);
|
|
3176
|
+
element.addEventListener("pointerup", up);
|
|
3177
|
+
element.addEventListener("pointercancel", cancel);
|
|
3178
|
+
element.addEventListener("pointerup", pointer);
|
|
3179
|
+
element.addEventListener("wheel", wheel, { passive: false });
|
|
3180
|
+
element.addEventListener("contextmenu", context);
|
|
3181
|
+
return { key, keyup, down, up, pointer, wheel, cancel, context, longPress };
|
|
3182
|
+
}), (registered) => Effect18.sync(() => {
|
|
3183
|
+
element.removeEventListener("keydown", registered.key);
|
|
3184
|
+
element.removeEventListener("keyup", registered.keyup);
|
|
3185
|
+
element.removeEventListener("pointerdown", registered.down);
|
|
3186
|
+
element.removeEventListener("pointerup", registered.up);
|
|
3187
|
+
element.removeEventListener("pointercancel", registered.cancel);
|
|
3188
|
+
element.removeEventListener("pointerup", registered.pointer);
|
|
3189
|
+
element.removeEventListener("wheel", registered.wheel);
|
|
3190
|
+
element.removeEventListener("contextmenu", registered.context);
|
|
3191
|
+
registered.longPress.cancel();
|
|
3192
|
+
emit = () => {};
|
|
3193
|
+
})));
|
|
3194
|
+
}
|
|
3195
|
+
function isKeyBinding(value) {
|
|
3196
|
+
return "action" in value;
|
|
3197
|
+
}
|
|
3198
|
+
// src/input/index.ts
|
|
3199
|
+
var Input = { layer };
|
|
3200
|
+
|
|
3201
|
+
// src/theme.ts
|
|
3202
|
+
var DEFAULTS = {
|
|
3203
|
+
font: "sans-serif",
|
|
3204
|
+
text: 16777215,
|
|
3205
|
+
accent: 14729344,
|
|
3206
|
+
panel: 1052696,
|
|
3207
|
+
panelAlpha: 0.82,
|
|
3208
|
+
readText: 10132138
|
|
3209
|
+
};
|
|
3210
|
+
var parseColour = (value, fallback) => {
|
|
3211
|
+
if (typeof value !== "string")
|
|
3212
|
+
return fallback;
|
|
3213
|
+
const text = value.trim().replace(/^#/, "").replace(/^0x/i, "");
|
|
3214
|
+
return /^[0-9a-f]{6}$/i.test(text) ? Number.parseInt(text, 16) : fallback;
|
|
3215
|
+
};
|
|
3216
|
+
var themeFromBindings = (bindings) => {
|
|
3217
|
+
const ui = bindings?.ui;
|
|
3218
|
+
const colours = ui?.theme;
|
|
3219
|
+
const alpha = typeof colours?.panelAlpha === "string" ? Number(colours.panelAlpha) : Number.NaN;
|
|
3220
|
+
return {
|
|
3221
|
+
font: ui?.font ?? DEFAULTS.font,
|
|
3222
|
+
text: parseColour(colours?.text, DEFAULTS.text),
|
|
3223
|
+
accent: parseColour(colours?.accent, DEFAULTS.accent),
|
|
3224
|
+
panel: parseColour(colours?.panel, DEFAULTS.panel),
|
|
3225
|
+
panelAlpha: Number.isFinite(alpha) ? Math.max(0, Math.min(1, alpha)) : DEFAULTS.panelAlpha,
|
|
3226
|
+
readText: parseColour(colours?.readText, DEFAULTS.readText)
|
|
3227
|
+
};
|
|
3228
|
+
};
|
|
3229
|
+
var defaultTheme = DEFAULTS;
|
|
3230
|
+
|
|
3231
|
+
// src/mount.ts
|
|
3232
|
+
var slash = (value) => value.endsWith("/") ? value : `${value}/`;
|
|
3233
|
+
var isUrl = (value) => /^[a-z][a-z\d+.-]*:/i.test(value);
|
|
3234
|
+
var absoluteUrl = (value) => {
|
|
3235
|
+
try {
|
|
3236
|
+
return new URL(value, typeof globalThis.location === "object" ? globalThis.location.href : "http://localhost/").href;
|
|
3237
|
+
} catch {
|
|
3238
|
+
return value;
|
|
3239
|
+
}
|
|
3240
|
+
};
|
|
3241
|
+
var resolveBase = (source, bindings, assetsUrl) => {
|
|
3242
|
+
if (assetsUrl !== undefined)
|
|
3243
|
+
return slash(assetsUrl);
|
|
3244
|
+
const configured = bindings.assetsBase ?? "assets/";
|
|
3245
|
+
if (isUrl(configured))
|
|
3246
|
+
return slash(configured);
|
|
3247
|
+
if (typeof source === "object" && source !== null && "url" in source)
|
|
3248
|
+
return new URL(configured, source.url).href;
|
|
3249
|
+
const origin = typeof globalThis.location === "object" ? globalThis.location.href : "http://localhost/";
|
|
3250
|
+
return new URL(configured, origin).href;
|
|
3251
|
+
};
|
|
3252
|
+
var resolveAsset = (bindings, base, file) => {
|
|
3253
|
+
const mapped = bindings.assets?.[file] ?? file;
|
|
3254
|
+
if (isUrl(mapped))
|
|
3255
|
+
return mapped;
|
|
3256
|
+
try {
|
|
3257
|
+
return new URL(mapped.replace(/^\/+/, ""), slash(base)).href;
|
|
3258
|
+
} catch {
|
|
3259
|
+
const origin = typeof globalThis.location === "object" ? globalThis.location.href : "http://localhost/";
|
|
3260
|
+
return new URL(mapped.replace(/^\/+/, ""), new URL(slash(base), origin)).href;
|
|
3261
|
+
}
|
|
3262
|
+
};
|
|
3263
|
+
var loadBindings = (source) => {
|
|
3264
|
+
if (typeof source === "string")
|
|
3265
|
+
return BindingsLoader.decode(source);
|
|
3266
|
+
if (typeof source === "object" && source !== null && "url" in source) {
|
|
3267
|
+
return BindingsLoader.loadUrl(source.url).pipe(Effect19.provide(FetchHttpClient.layer));
|
|
3268
|
+
}
|
|
3269
|
+
return Effect19.succeed(source);
|
|
3270
|
+
};
|
|
3271
|
+
var namespacedStorage = (key) => Effect19.map(Layer5.build(BrowserKeyValueStore.layerLocalStorage), (context) => Layer5.succeed(KeyValueStore.KeyValueStore, KeyValueStore.prefix(Context5.get(context, KeyValueStore.KeyValueStore), `${key}:`)));
|
|
3272
|
+
var runMount = (element, options, bindings, assetsBase) => {
|
|
3273
|
+
const theme = themeFromBindings(bindings);
|
|
3274
|
+
const pixiLayer = PixiApp.layer(element, bindings.canvas);
|
|
3275
|
+
const worldLayer = World.layer.pipe(Layer5.provideMerge(pixiLayer));
|
|
3276
|
+
return Effect19.gen(function* () {
|
|
3277
|
+
const services = yield* Layer5.build(worldLayer);
|
|
3278
|
+
const pixi = Context5.get(services, PixiApp);
|
|
3279
|
+
const world = Context5.get(services, World);
|
|
3280
|
+
const assetsOptions = { assetsBase, backend: pixiAssetsBackend };
|
|
3281
|
+
const assets = new PixiAssets(assetsOptions);
|
|
3282
|
+
const ui = new Ui(pixi.layers.overlay, theme, world, (file) => assets.texture(file));
|
|
3283
|
+
ui.setDesign(bindings.canvas, "Lute");
|
|
3284
|
+
const resolveSound = (file) => resolveAsset(bindings, assetsBase, file);
|
|
3285
|
+
const audio = new PixiAudio({
|
|
3286
|
+
backend: {
|
|
3287
|
+
load: (file) => pixiSoundBackend.load(resolveSound(file)),
|
|
3288
|
+
play: (file, playOptions) => pixiSoundBackend.play(resolveSound(file), playOptions),
|
|
3289
|
+
unlock: pixiSoundBackend.unlock
|
|
3290
|
+
}
|
|
3291
|
+
});
|
|
3292
|
+
const title = bindings.ui?.title;
|
|
3293
|
+
const titleBackground = title === undefined || title === null ? undefined : resolveAsset(bindings, assetsBase, title.background);
|
|
3294
|
+
if (titleBackground !== undefined)
|
|
3295
|
+
yield* assets.ensure([titleBackground]);
|
|
3296
|
+
const presenterOptions = titleBackground === undefined ? { pixi, world, ui, assets, bindings, theme } : { pixi, world, ui, assets, bindings, theme, titleBackground };
|
|
3297
|
+
const presenter = new PixiPresenter(presenterOptions);
|
|
3298
|
+
const storage = yield* namespacedStorage(options.storageKey ?? "lute");
|
|
3299
|
+
const requirements = Layer5.mergeAll(RuntimeBrowser.runtime(absoluteUrl(options.wasmUrl)), RuntimeBrowser.bundle(absoluteUrl(slash(options.bundleUrl))), Runtime.InputLog.memory, storage);
|
|
3300
|
+
const playerOptions = options.host === undefined ? { bindings, presenter, audio, assets } : { bindings, presenter, audio, assets, host: options.host };
|
|
3301
|
+
const player = yield* LutePlayer.make(playerOptions).pipe(Effect19.provide(requirements));
|
|
3302
|
+
let unlocked = false;
|
|
3303
|
+
const dispatch = (action) => Effect19.gen(function* () {
|
|
3304
|
+
if (!unlocked) {
|
|
3305
|
+
unlocked = true;
|
|
3306
|
+
yield* audio.unlock.pipe(Effect19.catch(() => Effect19.void));
|
|
3307
|
+
}
|
|
3308
|
+
yield* player.actions(action);
|
|
3309
|
+
});
|
|
3310
|
+
const keys = (event) => {
|
|
3311
|
+
if (ui.handleKey(event.key))
|
|
3312
|
+
event.preventDefault();
|
|
3313
|
+
};
|
|
3314
|
+
element.addEventListener("keydown", keys, true);
|
|
3315
|
+
yield* Effect19.addFinalizer(() => Effect19.sync(() => element.removeEventListener("keydown", keys, true)));
|
|
3316
|
+
yield* Effect19.forkScoped(Stream8.runForEach(Input.layer(element, undefined, () => ui.hasScreen, (event) => ui.pointerAction(event.clientX, event.clientY)), dispatch));
|
|
3317
|
+
yield* Effect19.forkScoped(Stream8.runForEach(SubscriptionRef3.changes(player.view), (view) => Effect19.sync(() => {
|
|
3318
|
+
ui.setPlaying(view.flow === "playing" && view.story !== "idle", { auto: view.mode === "auto", skip: view.mode === "skip", hidden: view.uiHidden });
|
|
3319
|
+
})));
|
|
3320
|
+
if (title !== undefined && title !== null) {
|
|
3321
|
+
const file = resolveAsset(bindings, assetsBase, title.music);
|
|
3322
|
+
yield* Effect19.forkScoped(audio.play("music", { file, loop: true, volume: 1, fade: 0 }).pipe(Effect19.catch(() => Effect19.void)));
|
|
3323
|
+
}
|
|
3324
|
+
return player;
|
|
3325
|
+
});
|
|
3326
|
+
};
|
|
3327
|
+
var mount = (element, options) => Effect19.flatMap(loadBindings(options.bindings), (bindings) => runMount(element, options, bindings, resolveBase(options.bindings, bindings, options.assetsUrl)));
|
|
3328
|
+
var mountPlayer = async (element, options) => {
|
|
3329
|
+
const runtime = ManagedRuntime.make(Layer5.empty);
|
|
3330
|
+
const scope = Scope2.makeUnsafe("parallel");
|
|
3331
|
+
try {
|
|
3332
|
+
const handle = await runtime.runPromise(Effect19.provideService(mount(element, options), Scope2.Scope, scope));
|
|
3333
|
+
let closed = false;
|
|
3334
|
+
return {
|
|
3335
|
+
unmount: async () => {
|
|
3336
|
+
if (closed)
|
|
3337
|
+
return;
|
|
3338
|
+
closed = true;
|
|
3339
|
+
await runtime.runPromise(handle.shutdown);
|
|
3340
|
+
await runtime.runPromise(Scope2.close(scope, Exit.void));
|
|
3341
|
+
await runtime.dispose();
|
|
3342
|
+
},
|
|
3343
|
+
view: () => runtime.runPromise(SubscriptionRef3.get(handle.view)),
|
|
3344
|
+
act: (action) => runtime.runPromise(handle.actions(action))
|
|
3345
|
+
};
|
|
3346
|
+
} catch (error) {
|
|
3347
|
+
await runtime.dispose();
|
|
3348
|
+
await Scope2.close(scope, Exit.fail(error));
|
|
3349
|
+
throw error;
|
|
3350
|
+
}
|
|
3351
|
+
};
|
|
3352
|
+
var LuteWeb = { mount, mountPlayer };
|
|
3353
|
+
export {
|
|
3354
|
+
Input,
|
|
3355
|
+
LuteWeb,
|
|
3356
|
+
defaultTheme,
|
|
3357
|
+
mount,
|
|
3358
|
+
mountPlayer,
|
|
3359
|
+
parseColour,
|
|
3360
|
+
themeFromBindings
|
|
3361
|
+
};
|