cortico-world-agent-speed 0.2.0 → 0.2.2
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/CHANGELOG.md +64 -0
- package/LICENSE +0 -14
- package/README.md +29 -11
- package/THIRD_PARTY_NOTICES.md +69 -0
- package/activity.d.ts +15 -0
- package/activity.js +88 -3
- package/describe.json +806 -0
- package/dist/webui.css +2 -0
- package/dist/webui.css.map +7 -0
- package/dist/webui.js +2 -0
- package/dist/webui.js.map +7 -0
- package/host/app-store.cjs +68 -0
- package/host/cursor.cjs +67 -0
- package/host/electron-main.cjs +666 -0
- package/host/input-shape.cjs +63 -0
- package/host/mouse-lease.cjs +77 -0
- package/host/overlay.cjs +139 -0
- package/host/portrait.cjs +52 -0
- package/host/preload.cjs +60 -14
- package/host/renderer-gone.cjs +24 -0
- package/host/windows.cjs +251 -0
- package/package.json +27 -7
- package/scripts/build-webui.mjs +40 -0
- package/scripts/speech-bank/build.py +166 -0
- package/scripts/speech-bank/dicts.py +76 -0
- package/src/ENV_PROMPT.en.md +51 -0
- package/src/ENV_PROMPT.md +51 -0
- package/src/asr/address.ts +25 -0
- package/src/asr/hotkey.ts +335 -0
- package/src/asr/result.ts +51 -0
- package/src/asr/segmenter.ts +269 -0
- package/src/asr/sherpa.ts +280 -0
- package/src/asr/simplify.ts +38 -0
- package/src/asr/system-recognizer.ts +292 -0
- package/src/asr/system-sapi.ps1 +173 -0
- package/src/asr/transcription-api.ts +176 -0
- package/src/chat.ts +212 -0
- package/src/config.ts +481 -0
- package/src/cua/ENV_PROMPT.en.md +29 -0
- package/src/cua/ENV_PROMPT.md +29 -0
- package/src/cua/config.ts +88 -0
- package/src/cua/cua.ts +712 -0
- package/src/cua/engine/darwin.ts +410 -0
- package/src/cua/engine/display.ts +55 -0
- package/src/cua/engine/fail.ts +15 -0
- package/src/cua/engine/foreground.ts +8 -0
- package/src/cua/engine/image.ts +57 -0
- package/src/cua/engine/keys.ts +70 -0
- package/src/cua/engine/linux-keys.ts +29 -0
- package/src/cua/engine/linux.ts +343 -0
- package/src/cua/engine/mac-keys.ts +57 -0
- package/src/cua/engine/user-activity.ts +54 -0
- package/src/cua/engine/wait-idle.ts +31 -0
- package/src/cua/engine/win32.ts +299 -0
- package/src/cua/engine-child.ts +274 -0
- package/src/cua/engine-ipc.ts +140 -0
- package/src/cua/i18n/de.ts +53 -0
- package/src/cua/i18n/en.ts +63 -0
- package/src/cua/i18n/es-419.ts +53 -0
- package/src/cua/i18n/fr.ts +53 -0
- package/src/cua/i18n/index.ts +26 -0
- package/src/cua/i18n/it.ts +53 -0
- package/src/cua/i18n/ja.ts +53 -0
- package/src/cua/i18n/ko.ts +53 -0
- package/src/cua/i18n/pt-BR.ts +53 -0
- package/src/cua/i18n/ru.ts +59 -0
- package/src/cua/i18n/zh-Hant.ts +53 -0
- package/src/cua/i18n/zh.ts +59 -0
- package/src/cua/model-text.ts +252 -0
- package/src/cua/terminal.ts +31 -0
- package/src/cua/tools.ts +153 -0
- package/src/definition.ts +146 -0
- package/src/host-reason.ts +32 -0
- package/src/i18n/de.ts +409 -0
- package/src/i18n/en.ts +443 -0
- package/src/i18n/es-419.ts +409 -0
- package/src/i18n/fr.ts +409 -0
- package/src/i18n/index.ts +56 -0
- package/src/i18n/it.ts +409 -0
- package/src/i18n/ja.ts +409 -0
- package/src/i18n/ko.ts +409 -0
- package/src/i18n/pt-BR.ts +409 -0
- package/src/i18n/ru.ts +415 -0
- package/src/i18n/zh-Hant.ts +409 -0
- package/src/i18n/zh.ts +440 -0
- package/src/index.ts +15 -0
- package/src/model-text.ts +344 -0
- package/src/moves.ts +101 -0
- package/src/pack-import.ts +361 -0
- package/src/packs.ts +449 -0
- package/src/runtime/download.ts +29 -0
- package/src/runtime/store.ts +293 -0
- package/src/script.ts +202 -0
- package/src/self.ts +247 -0
- package/src/server.ts +479 -0
- package/src/speech/data/CMUDICT-LICENSE +33 -0
- package/src/speech/data/IPADIC-COPYING +71 -0
- package/src/speech/data/UNICODE-LICENSE +39 -0
- package/src/speech/data/en.tsv.gz +0 -0
- package/src/speech/data/ja-ipadic.tsv.gz +0 -0
- package/src/speech/data/ja-unihan.tsv.gz +0 -0
- package/src/speech/data/zh.json +1 -0
- package/src/speech/index.ts +204 -0
- package/src/speech/ipa.ts +83 -0
- package/src/speech/japanese.ts +105 -0
- package/src/speech/jukujikun.ts +22 -0
- package/src/speech/korean.ts +35 -0
- package/src/speech/lang.ts +151 -0
- package/src/speech/rules.ts +72 -0
- package/src/speech/syllable.ts +76 -0
- package/src/status.ts +164 -0
- package/src/tools.ts +110 -0
- package/src/webui/api-rows.css +2 -0
- package/src/webui/api-rows.ts +44 -0
- package/src/webui/client.ts +451 -0
- package/src/webui/dress-dialog.css +14 -0
- package/src/webui/dress-dialog.ts +117 -0
- package/src/webui/dress-panel.ts +160 -0
- package/src/webui/key-capture.ts +60 -0
- package/src/webui/shared/modal.css +13 -0
- package/src/webui/shared/modal.ts +77 -0
- package/src/webui/shared/problem.css +15 -0
- package/src/webui/shared/problem.ts +217 -0
- package/src/webui/shared/strings.ts +5 -0
- package/src/webui/style.css +50 -0
- package/src/window-host.ts +116 -0
- package/src/world.ts +2926 -0
- package/web/activity-motion.js +117 -0
- package/web/appearance.js +60 -0
- package/web/body-host.js +257 -0
- package/web/coo/coo.js +384 -0
- package/web/coo/figure.js +15 -0
- package/web/coo/figure.json +1224 -0
- package/web/dist/dress-dialog.css +1 -0
- package/web/dist/dress-dialog.js +1 -0
- package/web/drag-row.js +49 -0
- package/web/drag.js +149 -0
- package/web/dress.css +170 -0
- package/web/dress.html +37 -0
- package/web/dress.js +786 -0
- package/web/figure-frame.html +16 -0
- package/web/figure-frame.js +128 -0
- package/web/frame-loop.js +55 -0
- package/web/hints.js +77 -0
- package/web/i18n/de.js +170 -0
- package/web/i18n/en.js +177 -0
- package/web/i18n/es-419.js +170 -0
- package/web/i18n/fr.js +170 -0
- package/web/i18n/it.js +170 -0
- package/web/i18n/ja.js +170 -0
- package/web/i18n/ko.js +170 -0
- package/web/i18n/pt-BR.js +170 -0
- package/web/i18n/ru.js +170 -0
- package/web/i18n/zh-Hant.js +170 -0
- package/web/i18n/zh.js +175 -0
- package/web/i18n.js +54 -0
- package/{renderer/vendor → web}/kit/body.js +31 -58
- package/web/looks.js +70 -0
- package/web/mic-worklet.js +36 -0
- package/web/mic.js +117 -0
- package/web/motion-frames.js +80 -0
- package/web/motion-level.js +40 -0
- package/web/new-year-2027.svg +238 -0
- package/web/overlay.html +16 -0
- package/web/overlay.js +100 -0
- package/web/pet-app.js +3000 -0
- package/web/pet.css +642 -0
- package/web/pet.html +21 -0
- package/web/scale-slider.js +259 -0
- package/web/show-hide.js +49 -0
- package/web/sound.js +245 -0
- package/web/speech/zh.json +1 -0
- package/web/speech/zh.ogg +0 -0
- package/web/speech.js +197 -0
- package/web/stage.js +160 -0
- package/web/swap-motion.js +119 -0
- package/web/ui.js +195 -0
- package/host/launch.js +0 -187
- package/host/library.cjs +0 -638
- package/host/main.cjs +0 -389
- package/index.js +0 -93
- package/renderer/index.html +0 -22
- package/renderer/pet-sandbox.html +0 -11
- package/renderer/pet.css +0 -38
- package/renderer/pet.js +0 -251
- package/renderer/vendor/activity-motion.js +0 -101
- package/renderer/vendor/figure-frame.js +0 -115
- package/renderer/vendor/pack.css +0 -2
- /package/{renderer/vendor → web}/kit/rig.js +0 -0
package/web/dress.js
ADDED
|
@@ -0,0 +1,786 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dressing page: the body (a figure pack, src/packs.ts; Coo is one); for Coo the palette, four accessory slots
|
|
3
|
+
* and their color channels, for another pack a row per dress-up axis of its manifest; with a live preview
|
|
4
|
+
* of the body run as on the desktop (body-host.js).
|
|
5
|
+
* Every change is saved through `POST /api/skin` (the dark/light switch through `POST /api/prefs`);
|
|
6
|
+
* the World persists it and pushes it to the pet window. Changes made elsewhere arrive over
|
|
7
|
+
* `/socket?role=dress`.
|
|
8
|
+
*
|
|
9
|
+
* Packs are imported here too (src/pack-import.ts): a zip as it is, or the folders a picked or dropped folder
|
|
10
|
+
* holds a pack in (a folder with figure.json, up to `depth` levels down, not looking inside a pack), framed
|
|
11
|
+
* as the server reads them (`bundle`). The server says what it found; the person picks what to install.
|
|
12
|
+
* A pack the server lists as `removable` is uninstalled from its tile (`POST /api/figures/remove`).
|
|
13
|
+
*
|
|
14
|
+
* The size row under the figures sets the size on screen (`POST /api/prefs` `scale`) while its slider moves; the World
|
|
15
|
+
* keeps a size per figure, so picking another figure puts it on at its own. The preview keeps its one size.
|
|
16
|
+
*
|
|
17
|
+
* The round buttons on the preview save the look worn, with the size on screen, as a new one (looks.js), and, once the
|
|
18
|
+
* look picked from the saved ones has changed, save the changes to it; the favourites are listed first in one row, to put
|
|
19
|
+
* on with a click (at their size, when they have one), rename, remove or drag to another place.
|
|
20
|
+
*
|
|
21
|
+
* Text follows the app language (i18n.js): the page's own from its tables, the names of figures, Coo's
|
|
22
|
+
* palettes and accessories, and other packs' picks from the packs' manifests.
|
|
23
|
+
*/
|
|
24
|
+
import { ICONS, themeButton } from './ui.js';
|
|
25
|
+
import { applyText, language, nameIn, t, useLanguage } from './i18n.js';
|
|
26
|
+
import { createSfx } from './sound.js';
|
|
27
|
+
import {
|
|
28
|
+
COO_CSS, mini, normalizeSkin, skinCss, wear,
|
|
29
|
+
PALETTES, HEADS, SIDES, GLASSES, NECKS, ACC_COLORS, LINKED, NO_BODY, ROLES,
|
|
30
|
+
} from './coo/coo.js';
|
|
31
|
+
import { loadBody } from './body-host.js';
|
|
32
|
+
import { fetchLooks, lookAvailable, lookPicture, sameLook, writeLooks } from './looks.js';
|
|
33
|
+
import { dragRow } from './drag-row.js';
|
|
34
|
+
import { SIZE, scaleSlider, sizeEcho } from './scale-slider.js';
|
|
35
|
+
import { restyle, swapIn, swapOut } from './swap-motion.js';
|
|
36
|
+
import { announcePainted, dressFloor, glowColor, lightWall, modeButton, setGlow } from './stage.js';
|
|
37
|
+
|
|
38
|
+
import { bindAppearance, hostDressDialogs } from './appearance.js';
|
|
39
|
+
import { localDressDialogs } from './dist/dress-dialog.js';
|
|
40
|
+
import { mountHints } from './hints.js';
|
|
41
|
+
import { setMotionChoice, watchMotion } from './motion-level.js';
|
|
42
|
+
watchMotion(window);
|
|
43
|
+
bindAppearance(document, window);
|
|
44
|
+
/** The settings window's dialog when the page is embedded in it; null when the page draws its own. */
|
|
45
|
+
const dialogStop = new AbortController();
|
|
46
|
+
window.addEventListener('pagehide', () => dialogStop.abort(), { once: true });
|
|
47
|
+
const dialogs = hostDressDialogs(document, window) ?? localDressDialogs(document, dialogStop.signal);
|
|
48
|
+
mountHints(document);
|
|
49
|
+
|
|
50
|
+
const $ = (s) => document.querySelector(s);
|
|
51
|
+
|
|
52
|
+
// Coo's drawing classes for the tiles; the page's own colours follow Coo's palette
|
|
53
|
+
const cooStyle = document.createElement('style'), skinStyle = document.createElement('style');
|
|
54
|
+
cooStyle.textContent = COO_CSS;
|
|
55
|
+
document.head.append(cooStyle, skinStyle);
|
|
56
|
+
const sfx = createSfx({ storageKey: 'cortico-pet.dress-sound.v1', volume: .35 });
|
|
57
|
+
['pointerdown', 'keydown'].forEach((ev) => document.addEventListener(ev, () => sfx.unlock(), { capture: true }));
|
|
58
|
+
|
|
59
|
+
let skin = normalizeSkin(null);
|
|
60
|
+
const root = document.documentElement;
|
|
61
|
+
/** The pet's night/day setting. */
|
|
62
|
+
let theme = root.dataset.theme;
|
|
63
|
+
const preview = $('#preview');
|
|
64
|
+
/**
|
|
65
|
+
* The pet's night/day shows on the preview only (dress.css themes `#preview` by its own data-theme); the rest of
|
|
66
|
+
* the page follows the settings window when embedded (appearance.js sets data-ui-theme), else the pet's setting.
|
|
67
|
+
*/
|
|
68
|
+
const showTheme = () => { preview.dataset.theme = theme; root.dataset.theme = root.dataset.uiTheme ?? theme; };
|
|
69
|
+
new MutationObserver(showTheme).observe(root, { attributes: true, attributeFilter: ['data-ui-theme'] });
|
|
70
|
+
// the stage as on the 开始 page's preview (stage.js): a wall that answers the pointer, a floor with icons, the night/day button
|
|
71
|
+
const modeBtn = modeButton(preview, () => theme, (next) => { theme = next; showTheme(); body?.set({ theme }); renderLooks(); sfx.tick(); }, (next) => save('/api/prefs', { theme: next }), themeButton);
|
|
72
|
+
lightWall(preview);
|
|
73
|
+
dressFloor($('#floor'));
|
|
74
|
+
/** The page's own text in the app language; the theme button's title is part of it. */
|
|
75
|
+
const showText = () => { applyText(); document.title = t('dress.title'); modeBtn.show(theme); showScale(); };
|
|
76
|
+
const ready = useLanguage().then(showText);
|
|
77
|
+
showTheme();
|
|
78
|
+
/** The floor is the stage's bottom 48 pixels, as in a tab (pet.css .floor). */
|
|
79
|
+
const FLOOR = 48;
|
|
80
|
+
const bounds = () => ({ W: preview.clientWidth, H: preview.clientHeight, floorY: preview.clientHeight - FLOOR, S: .5 });
|
|
81
|
+
/** The body in the preview (body-host.js). */
|
|
82
|
+
let body = null;
|
|
83
|
+
new ResizeObserver(() => body?.set({ bounds: bounds() })).observe(preview);
|
|
84
|
+
const local = (e) => { const r = preview.getBoundingClientRect(); return { x: e.clientX - r.left, y: e.clientY - r.top, t: e.timeStamp }; };
|
|
85
|
+
preview.addEventListener('pointerdown', (e) => { const p = local(e); body?.pointer('down', p); if (body?.hit(p)) preview.setPointerCapture(e.pointerId); });
|
|
86
|
+
preview.addEventListener('pointermove', (e) => body?.pointer('move', local(e)));
|
|
87
|
+
preview.addEventListener('pointerup', (e) => body?.pointer('up', local(e)));
|
|
88
|
+
preview.addEventListener('pointerleave', () => body?.pointer('leave', {}));
|
|
89
|
+
|
|
90
|
+
function save(path, body) {
|
|
91
|
+
return fetch(path, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) })
|
|
92
|
+
.then((r) => { $('#saved').textContent = r.ok ? '' : t('dress.notSaved'); })
|
|
93
|
+
.catch(() => { $('#saved').textContent = t('dress.offline'); });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// the installed figure packs, asked for again whenever the page hears of a look, and the ones that did not load
|
|
97
|
+
let packs = [];
|
|
98
|
+
let packStatus = { importable: false, large: Infinity, depth: 3, problems: [] };
|
|
99
|
+
const loadPacks = () => Promise.all([fetch('/api/figures').then((r) => r.json()), fetch('/api/figures/status').then((r) => r.json())])
|
|
100
|
+
.then(([list, status]) => { packs = list; packStatus = status; lightFor(skin); render(); }).catch(() => {});
|
|
101
|
+
/** The preview's light in the picked body's colors (stage.js glowColor), the settings window's theme color when its pack names none. */
|
|
102
|
+
const lightFor = (s) => setGlow(preview, glowColor(packs.find((p) => p.id === s.figure), s));
|
|
103
|
+
loadPacks();
|
|
104
|
+
let wanted = null, loading = null;
|
|
105
|
+
/** The look last put on the preview's body, so only a change is animated. */
|
|
106
|
+
let shownLook = null;
|
|
107
|
+
// a pack that will not load or breaks previews as Coo, as on the desktop
|
|
108
|
+
function previewFailed(id, err) {
|
|
109
|
+
console.error(err);
|
|
110
|
+
if (id !== 'coo' && wanted === id) void showFigure({ ...skin, figure: 'coo' });
|
|
111
|
+
}
|
|
112
|
+
async function showFigure(s) {
|
|
113
|
+
wanted = s.figure;
|
|
114
|
+
if (loading === s.figure) return;
|
|
115
|
+
try {
|
|
116
|
+
if (body?.pack === s.figure) {
|
|
117
|
+
body.set({ skin: s });
|
|
118
|
+
// another look of the figure shown: a dip, as on the desktop
|
|
119
|
+
const look = JSON.stringify(s);
|
|
120
|
+
if (shownLook && look !== shownLook) restyle(body, preview);
|
|
121
|
+
shownLook = look;
|
|
122
|
+
if (s.figure !== 'coo') await body.setScheme(s.scheme, { fade: .4 });
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
loading = s.figure;
|
|
126
|
+
const pack = packs.find((p) => p.id === s.figure) ?? (await (await fetch('/api/figures')).json()).find((p) => p.id === s.figure);
|
|
127
|
+
if (!pack) throw new Error(t('figure.notInstalled'));
|
|
128
|
+
const was = body?.layout;
|
|
129
|
+
const holder = {};
|
|
130
|
+
const next = await loadBody({
|
|
131
|
+
layer: $('#figureLayer'), pack, theme, bounds: bounds(),
|
|
132
|
+
start: { x: was?.x ?? preview.clientWidth / 2, facing: was?.facing, skin: s, scheme: s.scheme },
|
|
133
|
+
onEvent: () => {},
|
|
134
|
+
onSound: (name, kind, ...args) => { if (body === holder.body) sfx.play(name, kind, ...args); },
|
|
135
|
+
onError: (err) => { if (body === holder.body) previewFailed(pack.id, err); },
|
|
136
|
+
});
|
|
137
|
+
if (wanted !== s.figure) { next.dispose(); return; }
|
|
138
|
+
holder.body = next;
|
|
139
|
+
// the theme may have arrived while the body was loading
|
|
140
|
+
next.set({ roam: 'calm', theme });
|
|
141
|
+
const old = body;
|
|
142
|
+
body = next;
|
|
143
|
+
shownLook = JSON.stringify(s);
|
|
144
|
+
if (old) {
|
|
145
|
+
swapOut(old, preview);
|
|
146
|
+
swapIn(next, preview, next.layout ?? was);
|
|
147
|
+
}
|
|
148
|
+
sfx.usePack(pack.base, pack.sounds);
|
|
149
|
+
} catch (err) {
|
|
150
|
+
previewFailed(s.figure, err);
|
|
151
|
+
} finally {
|
|
152
|
+
if (loading === s.figure) loading = null;
|
|
153
|
+
}
|
|
154
|
+
// a pick that came while the body was loading
|
|
155
|
+
if (body?.pack === skin.figure && skin !== s) await showFigure(skin);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Shows `next`; `persist` saves it, a number saves it at that size on screen. */
|
|
159
|
+
function apply(next, persist) {
|
|
160
|
+
skin = next;
|
|
161
|
+
// the page's tiles follow the page's light/dark, the preview the pet's
|
|
162
|
+
skinStyle.textContent = skinCss(skin) + skinCss(skin, '#preview');
|
|
163
|
+
lightFor(skin);
|
|
164
|
+
showFigure(skin).catch((err) => console.error(err));
|
|
165
|
+
render();
|
|
166
|
+
if (persist) return save('/api/skin', typeof persist === 'number' ? { skin, scale: persist } : { skin });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const CROP = { palette: '18 18 220 220', head: '18 -72 220 220', side: '-52 -4 220 220', glasses: '28 7 220 220', neck: '32 84 220 220' };
|
|
170
|
+
const el = (tag, cls, html) => { const e = document.createElement(tag); if (cls) e.className = cls; if (html != null) e.innerHTML = html; return e; };
|
|
171
|
+
|
|
172
|
+
function colorRow(slot, item) {
|
|
173
|
+
const row = el('div', 'cmap');
|
|
174
|
+
for (const ch of ROLES[item]) {
|
|
175
|
+
const name = t(ch === 'main' ? 'dress.main' : 'dress.accent');
|
|
176
|
+
const grp = el('div', 'cm-group');
|
|
177
|
+
grp.setAttribute('role', 'group'); grp.setAttribute('aria-label', name);
|
|
178
|
+
grp.appendChild(el('span', 'cm-label', name));
|
|
179
|
+
for (const src of [...LINKED.filter((l) => !(l.id === 'body' && NO_BODY[slot])), ...ACC_COLORS]) {
|
|
180
|
+
const linked = src.id === 'body' || src.id === 'eye';
|
|
181
|
+
const b = el('button', 'dot' + (linked ? ' linked ' + src.id : ''));
|
|
182
|
+
if (!linked) b.style.cssText = `--dl:${src.l};--dd:${src.d}`;
|
|
183
|
+
b.title = t(`color.${src.id}`);
|
|
184
|
+
b.setAttribute('aria-label', t('dress.colorOf', { channel: name, color: b.title }));
|
|
185
|
+
b.setAttribute('aria-pressed', String(skin.colors[slot][ch] === src.id));
|
|
186
|
+
b.addEventListener('click', () => {
|
|
187
|
+
const next = { ...skin, colors: JSON.parse(JSON.stringify(skin.colors)) };
|
|
188
|
+
next.colors[slot][ch] = src.id;
|
|
189
|
+
apply(next, true); sfx.tick();
|
|
190
|
+
});
|
|
191
|
+
grp.appendChild(b);
|
|
192
|
+
}
|
|
193
|
+
row.appendChild(grp);
|
|
194
|
+
}
|
|
195
|
+
return row;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const nameOf = nameIn;
|
|
199
|
+
/** The name Coo's manifest gives an option of `axis` (palette or an accessory slot), its id until the packs have loaded. */
|
|
200
|
+
const cooName = (axis, id) => nameIn(packs.find((p) => p.id === 'coo')?.axes.find((a) => a.id === axis)?.options.find((o) => o.id === id)?.name) || id;
|
|
201
|
+
/** The option of each axis that `scheme` picks: a preset id, or the options joined by `-` in axis order. */
|
|
202
|
+
function picksOf(pack, scheme) {
|
|
203
|
+
const preset = pack.presets.find((p) => p.id === scheme);
|
|
204
|
+
const parts = (scheme || '').split('-');
|
|
205
|
+
return Object.fromEntries(pack.axes.map((a, i) => {
|
|
206
|
+
const want = preset ? preset.pick[a.id] : parts[i];
|
|
207
|
+
return [a.id, a.options.some((o) => o.id === want) ? want : a.options[0].id];
|
|
208
|
+
}));
|
|
209
|
+
}
|
|
210
|
+
/** `skin.scheme` for a set of picks: the preset that picks exactly them, else the options joined. */
|
|
211
|
+
function schemeOf(pack, picks) {
|
|
212
|
+
const preset = pack.presets.find((p) => pack.axes.every((a) => p.pick[a.id] === picks[a.id]));
|
|
213
|
+
return preset ? preset.id : pack.axes.map((a) => picks[a.id]).join('-');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function renderFigure() {
|
|
217
|
+
$('#dress').dataset.figure = skin.figure === 'coo' ? 'coo' : 'pack';
|
|
218
|
+
const box = $('#optFigure');
|
|
219
|
+
box.textContent = '';
|
|
220
|
+
const opts = el('div', 'opts');
|
|
221
|
+
const choices = [{ id: 'coo', label: 'Coo', svg: `<svg viewBox="${CROP.palette}" aria-hidden="true">${mini('neutral', skin)}</svg>` },
|
|
222
|
+
// a pack shows its own picture whatever it wears; the rows below show the picks
|
|
223
|
+
...packs.filter((p) => p.id !== 'coo').map((p) => ({ id: p.id, label: nameOf(p.name), thumb: p.thumb ? p.base + p.thumb : null, pack: p }))];
|
|
224
|
+
for (const c of choices) {
|
|
225
|
+
const b = el('button', 'opt wide figure', c.svg);
|
|
226
|
+
b.setAttribute('aria-pressed', String(skin.figure === c.id));
|
|
227
|
+
b.title = c.label;
|
|
228
|
+
if (c.thumb) Object.assign(b.appendChild(el('img')), { src: c.thumb, alt: '' });
|
|
229
|
+
b.appendChild(el('span')).textContent = c.label;
|
|
230
|
+
b.addEventListener('click', () => {
|
|
231
|
+
if (skin.figure === c.id) return;
|
|
232
|
+
const pack = packs.find((p) => p.id === c.id);
|
|
233
|
+
apply({ ...skin, figure: c.id, ...(pack && c.id !== 'coo' ? { scheme: schemeOf(pack, picksOf(pack, '')) } : {}) }, true);
|
|
234
|
+
sfx.sparkle(); body?.cue('cheer');
|
|
235
|
+
});
|
|
236
|
+
if (c.pack?.removable) {
|
|
237
|
+
const tile = opts.appendChild(el('span', 'figure-tile'));
|
|
238
|
+
tile.append(b, removeButton(c.pack));
|
|
239
|
+
} else opts.appendChild(b);
|
|
240
|
+
}
|
|
241
|
+
if (packStatus.importable) {
|
|
242
|
+
const b = el('button', 'opt wide figure import', '<svg viewBox="0 0 52 52" aria-hidden="true"><path d="M26 15v22M15 26h22"/></svg><span></span>');
|
|
243
|
+
b.querySelector('span').textContent = t('import.button');
|
|
244
|
+
b.title = t('import.title');
|
|
245
|
+
b.addEventListener('click', () => { sfx.tick(); importDialog.choose(); });
|
|
246
|
+
opts.appendChild(b);
|
|
247
|
+
}
|
|
248
|
+
box.appendChild(opts);
|
|
249
|
+
if (packStatus.problems.length) {
|
|
250
|
+
const d = el('details', 'pack-problems');
|
|
251
|
+
d.appendChild(el('summary')).textContent = t('import.unloaded', { n: packStatus.problems.length });
|
|
252
|
+
const ul = d.appendChild(el('ul'));
|
|
253
|
+
for (const p of packStatus.problems) {
|
|
254
|
+
const li = ul.appendChild(el('li'));
|
|
255
|
+
li.appendChild(el('b')).textContent = p.dir;
|
|
256
|
+
li.append(t('import.reason', { reason: p.reason }));
|
|
257
|
+
}
|
|
258
|
+
box.appendChild(d);
|
|
259
|
+
}
|
|
260
|
+
// a row per axis of the pack on, after the figure row; Coo's own rows are below
|
|
261
|
+
for (const old of document.querySelectorAll('.pack-axis')) old.remove();
|
|
262
|
+
const pack = skin.figure === 'coo' ? null : packs.find((p) => p.id === skin.figure);
|
|
263
|
+
if (!pack) return;
|
|
264
|
+
const picks = picksOf(pack, skin.scheme);
|
|
265
|
+
let after = $('#optSize');
|
|
266
|
+
for (const axis of pack.axes) {
|
|
267
|
+
const label = el('div', 'row-label pack-axis');
|
|
268
|
+
label.id = `lbAxis-${axis.id}`;
|
|
269
|
+
label.textContent = nameOf(axis.name);
|
|
270
|
+
const slot = el('div', 'slot pack-axis');
|
|
271
|
+
slot.setAttribute('role', 'group');
|
|
272
|
+
slot.setAttribute('aria-labelledby', label.id);
|
|
273
|
+
const list = el('div', 'opts');
|
|
274
|
+
for (const o of axis.options) {
|
|
275
|
+
const b = el('button', 'opt wide');
|
|
276
|
+
b.setAttribute('aria-pressed', String(picks[axis.id] === o.id));
|
|
277
|
+
if (o.thumb) Object.assign(b.appendChild(el('img')), { src: pack.base + o.thumb, alt: '' });
|
|
278
|
+
b.appendChild(el('span')).textContent = nameOf(o.name);
|
|
279
|
+
b.addEventListener('click', () => {
|
|
280
|
+
apply({ ...skin, scheme: schemeOf(pack, { ...picks, [axis.id]: o.id }) }, true);
|
|
281
|
+
sfx.sparkle(); body?.cue('cheer'); body?.cue('bounce');
|
|
282
|
+
});
|
|
283
|
+
list.appendChild(b);
|
|
284
|
+
}
|
|
285
|
+
slot.appendChild(list);
|
|
286
|
+
after.after(label, slot);
|
|
287
|
+
after = slot;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/* ---------- uninstalling packs ---------- */
|
|
292
|
+
|
|
293
|
+
/** The trash button in an installed pack's tile corner, shown while the tile is hovered or focused; it asks in the dialog. */
|
|
294
|
+
function removeButton(pack) {
|
|
295
|
+
const b = el('button', 'pack-erase', ICONS.trash);
|
|
296
|
+
b.type = 'button';
|
|
297
|
+
b.title = t('remove.label', { name: nameOf(pack.name) });
|
|
298
|
+
b.setAttribute('aria-label', b.title);
|
|
299
|
+
b.addEventListener('click', () => { sfx.tick(); importDialog.remove(pack); });
|
|
300
|
+
return b;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/* ---------- size on screen ---------- */
|
|
304
|
+
|
|
305
|
+
/** Milliseconds between the sizes sent while the slider moves, so the pet on the desktop follows it. */
|
|
306
|
+
const SCALE_SEND_MS = 120;
|
|
307
|
+
/** The size on screen, as the World last said or this page last set it. */
|
|
308
|
+
let scale = 1;
|
|
309
|
+
const scaleText = el('span', 'size-value');
|
|
310
|
+
const scaleBox = $('#optSize');
|
|
311
|
+
const scaleRow = scaleBox.appendChild(el('div', 'size-row'));
|
|
312
|
+
const sizer = scaleSlider(document, {
|
|
313
|
+
...SIZE, label: '',
|
|
314
|
+
room: () => scaleRow.clientWidth - scaleText.offsetWidth - 14,
|
|
315
|
+
onInput: (v) => { scale = v; showScale(); scaleTimer ??= setTimeout(sendScale, SCALE_SEND_MS); },
|
|
316
|
+
// the favourites' changed marks follow the size once the drag ends, not on every send
|
|
317
|
+
onChange: () => { clearTimeout(scaleTimer); sendScale(); renderLooks(); },
|
|
318
|
+
});
|
|
319
|
+
scaleRow.append(sizer.el, scaleText);
|
|
320
|
+
new ResizeObserver(() => sizer.relayout()).observe(scaleRow);
|
|
321
|
+
let scaleTimer = null;
|
|
322
|
+
/** Sizes the World pushes, held while the slider is in use or this page's newer size is on its way (scale-slider.js sizeEcho). */
|
|
323
|
+
const scaleHeard = sizeEcho((v) => { if (v === scale) return; scale = v; sizer.set(v); showScale(); renderLooks(); });
|
|
324
|
+
function showScale() { scaleText.textContent = `${Math.round(scale * 100)}%`; sizer.el.setAttribute('aria-label', t('dress.size')); }
|
|
325
|
+
function sendScale() {
|
|
326
|
+
scaleTimer = null;
|
|
327
|
+
scaleHeard.sent(scale);
|
|
328
|
+
void save('/api/prefs', { scale });
|
|
329
|
+
}
|
|
330
|
+
showScale();
|
|
331
|
+
|
|
332
|
+
/* ---------- saved looks ---------- */
|
|
333
|
+
|
|
334
|
+
let looks = [];
|
|
335
|
+
/** Whether a read of the saved looks has succeeded: `POST /api/looks` replaces the whole list, so nothing is written before. */
|
|
336
|
+
let looksRead = false;
|
|
337
|
+
/** The saved look picked last, whose changes the update button saves; null when none is picked. */
|
|
338
|
+
let activeLook = null;
|
|
339
|
+
const loadLooks = () => fetchLooks().then((list) => {
|
|
340
|
+
if (list) {
|
|
341
|
+
looks = list;
|
|
342
|
+
looksRead = true;
|
|
343
|
+
if (!looks.some((l) => l.id === activeLook)) activeLook = looks.find(isWorn)?.id ?? null;
|
|
344
|
+
}
|
|
345
|
+
renderLooks();
|
|
346
|
+
});
|
|
347
|
+
/** Whether `look` is what the pet wears now: its skin, at its size when it keeps one. */
|
|
348
|
+
const isWorn = (look) => sameLook(look.skin, skin) && (look.scale ?? scale) === scale;
|
|
349
|
+
const lookId = () => Date.now().toString(36) + Math.random().toString(36).slice(2, 6);
|
|
350
|
+
|
|
351
|
+
/** A new look's name: the pack and its preset, or Coo and its palette. */
|
|
352
|
+
function lookName(s) {
|
|
353
|
+
const pack = s.figure && s.figure !== 'coo' ? packs.find((p) => p.id === s.figure) : null;
|
|
354
|
+
if (!pack) return `Coo · ${cooName('palette', s.palette)}`;
|
|
355
|
+
const preset = pack.presets.find((p) => p.id === s.scheme);
|
|
356
|
+
return preset ? `${nameOf(pack.name)} · ${nameOf(preset.name)}` : nameOf(pack.name);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const saveLookBtn = $('#saveLook'), updateLookBtn = $('#updateLook');
|
|
360
|
+
saveLookBtn.innerHTML = ICONS.addLook;
|
|
361
|
+
updateLookBtn.innerHTML = ICONS.saveLook;
|
|
362
|
+
const bump = (btn) => { btn.classList.remove('done'); void btn.offsetWidth; btn.classList.add('done'); };
|
|
363
|
+
saveLookBtn.addEventListener('click', async () => {
|
|
364
|
+
sfx.tick();
|
|
365
|
+
if (!looksRead) await loadLooks();
|
|
366
|
+
if (!looksRead) { $('#saved').textContent = t('dress.notSaved'); return; }
|
|
367
|
+
const had = looks.find((l) => sameLook(l.skin, skin) && l.scale === scale);
|
|
368
|
+
if (had) { $('#saved').textContent = t('looks.exists', { name: had.name }); return; }
|
|
369
|
+
const id = lookId();
|
|
370
|
+
const next = await writeLooks([...looks, { id, name: lookName(skin), skin, scale }]);
|
|
371
|
+
if (!next) { $('#saved').textContent = t('dress.notSaved'); return; }
|
|
372
|
+
$('#saved').textContent = '';
|
|
373
|
+
looks = next;
|
|
374
|
+
activeLook = id;
|
|
375
|
+
renderLooks();
|
|
376
|
+
sfx.sparkle(); body?.cue('cheer');
|
|
377
|
+
bump(saveLookBtn);
|
|
378
|
+
});
|
|
379
|
+
updateLookBtn.addEventListener('click', async () => {
|
|
380
|
+
sfx.tick();
|
|
381
|
+
const next = await writeLooks(looks.map((l) => (l.id === activeLook ? { ...l, skin, scale } : l)));
|
|
382
|
+
if (!next) { $('#saved').textContent = t('dress.notSaved'); return; }
|
|
383
|
+
$('#saved').textContent = '';
|
|
384
|
+
looks = next;
|
|
385
|
+
renderLooks();
|
|
386
|
+
sfx.sparkle(); body?.cue('cheer');
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
function renderLooks() {
|
|
390
|
+
const box = $('#optLooks');
|
|
391
|
+
const active = looks.find((l) => l.id === activeLook);
|
|
392
|
+
const changed = !!active && !isWorn(active);
|
|
393
|
+
saveLookBtn.title = t('looks.save');
|
|
394
|
+
saveLookBtn.setAttribute('aria-label', saveLookBtn.title);
|
|
395
|
+
updateLookBtn.hidden = !changed;
|
|
396
|
+
if (changed) { updateLookBtn.title = t('looks.update', { name: active.name }); updateLookBtn.setAttribute('aria-label', updateLookBtn.title); }
|
|
397
|
+
box.textContent = '';
|
|
398
|
+
if (!looks.length) { box.appendChild(el('p', 'looks-empty')).textContent = t('looks.empty'); return; }
|
|
399
|
+
const opts = el('div', 'looks-strip');
|
|
400
|
+
for (const look of looks) {
|
|
401
|
+
const tile = el('span', 'look-tile');
|
|
402
|
+
tile.dataset.id = look.id;
|
|
403
|
+
const b = el('button', 'opt wide look', lookPicture(look.skin, packs, theme === 'dark', look.scale));
|
|
404
|
+
if (typeof look.scale === 'number') b.appendChild(el('b', 'look-size')).append(`${Math.round(look.scale * 100)}%`);
|
|
405
|
+
b.type = 'button';
|
|
406
|
+
const gone = packs.length > 0 && !lookAvailable(look.skin, packs);
|
|
407
|
+
b.disabled = gone;
|
|
408
|
+
b.classList.toggle('gone', gone);
|
|
409
|
+
b.setAttribute('aria-pressed', String(look.id === activeLook));
|
|
410
|
+
b.classList.toggle('changed', changed && look.id === activeLook);
|
|
411
|
+
b.appendChild(el('span')).textContent = b.title = look.name;
|
|
412
|
+
// picking a look again lets it go: the save button then saves a new one
|
|
413
|
+
b.addEventListener('click', () => {
|
|
414
|
+
if (look.id === activeLook) { activeLook = null; renderLooks(); sfx.tick(); return; }
|
|
415
|
+
activeLook = look.id;
|
|
416
|
+
if (isWorn(look)) { renderLooks(); sfx.tick(); return; }
|
|
417
|
+
if (typeof look.scale === 'number') { scale = look.scale; sizer.set(scale); showScale(); scaleHeard.sent(scale); }
|
|
418
|
+
apply(normalizeSkin(look.skin), typeof look.scale === 'number' ? look.scale : true);
|
|
419
|
+
sfx.sparkle(); body?.cue('cheer');
|
|
420
|
+
});
|
|
421
|
+
const acts = el('span', 'look-acts');
|
|
422
|
+
const act = (icon, label, cls, fn) => {
|
|
423
|
+
const a = acts.appendChild(el('button', cls, icon));
|
|
424
|
+
a.type = 'button'; a.title = label; a.setAttribute('aria-label', label);
|
|
425
|
+
a.addEventListener('click', (e) => { e.stopPropagation(); sfx.tick(); fn(); });
|
|
426
|
+
};
|
|
427
|
+
act(ICONS.pencil, t('looks.rename'), 'look-act', () => renameLook(tile, look));
|
|
428
|
+
act(ICONS.trash, t('looks.delete', { name: look.name }), 'look-act danger', () => importDialog.forget(look, async () => {
|
|
429
|
+
const next = await writeLooks(looks.filter((l) => l.id !== look.id));
|
|
430
|
+
if (next) { looks = next; if (activeLook === look.id) activeLook = null; renderLooks(); } else $('#saved').textContent = t('dress.notSaved');
|
|
431
|
+
}));
|
|
432
|
+
tile.append(b, acts);
|
|
433
|
+
opts.appendChild(tile);
|
|
434
|
+
}
|
|
435
|
+
box.appendChild(gallery(opts));
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// a favourite is dragged to another place in the row; the row scrolls when it is held near an end
|
|
439
|
+
dragRow($('#optLooks'), {
|
|
440
|
+
item: (target) => (target.closest('.look-act, .look-name') ? null : target.closest('.looks-strip > .look-tile')),
|
|
441
|
+
key: (tile) => tile.dataset.id,
|
|
442
|
+
copyClass: 'look-drag-copy',
|
|
443
|
+
drop: async (order) => {
|
|
444
|
+
const byId = new Map(looks.map((l) => [l.id, l]));
|
|
445
|
+
const next = await writeLooks(order.map((id) => byId.get(id)).filter(Boolean));
|
|
446
|
+
if (next) looks = next;
|
|
447
|
+
else { $('#saved').textContent = t('dress.notSaved'); renderLooks(); }
|
|
448
|
+
},
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
/** A row with an arrow at each end, shown once it holds more than fit; each moves it by what shows. */
|
|
452
|
+
function gallery(strip) {
|
|
453
|
+
const row = el('div', 'looks-gallery');
|
|
454
|
+
const arrow = (dir) => {
|
|
455
|
+
const b = el('button', 'g-arrow', `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="${dir < 0 ? 'M14.5 6l-6 6 6 6' : 'M9.5 6l6 6-6 6'}"/></svg>`);
|
|
456
|
+
b.type = 'button';
|
|
457
|
+
b.setAttribute('aria-label', t(dir < 0 ? 'looks.prev' : 'looks.next'));
|
|
458
|
+
b.addEventListener('click', () => { sfx.tick(); strip.scrollBy({ left: dir * strip.clientWidth * .8 }); });
|
|
459
|
+
return b;
|
|
460
|
+
};
|
|
461
|
+
const prev = arrow(-1), next = arrow(1);
|
|
462
|
+
const sync = () => {
|
|
463
|
+
const more = strip.scrollWidth > strip.clientWidth + 1;
|
|
464
|
+
prev.hidden = next.hidden = !more;
|
|
465
|
+
prev.disabled = strip.scrollLeft <= 1;
|
|
466
|
+
next.disabled = strip.scrollLeft + strip.clientWidth >= strip.scrollWidth - 1;
|
|
467
|
+
};
|
|
468
|
+
strip.addEventListener('scroll', sync, { passive: true });
|
|
469
|
+
new ResizeObserver(sync).observe(strip);
|
|
470
|
+
row.append(prev, strip, next);
|
|
471
|
+
// the look worn comes into view
|
|
472
|
+
requestAnimationFrame(() => {
|
|
473
|
+
const on = strip.querySelector('[aria-pressed="true"]')?.parentElement;
|
|
474
|
+
if (on && on.offsetLeft + on.offsetWidth > strip.clientWidth) { strip.style.scrollBehavior = 'auto'; strip.scrollLeft = on.offsetLeft; strip.style.scrollBehavior = ''; }
|
|
475
|
+
sync();
|
|
476
|
+
});
|
|
477
|
+
return row;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** A field over the name; Enter or leaving it keeps the new name, Escape keeps the old one. */
|
|
481
|
+
function renameLook(tile, look) {
|
|
482
|
+
const input = el('input', 'look-name');
|
|
483
|
+
input.value = look.name;
|
|
484
|
+
input.maxLength = 40;
|
|
485
|
+
input.setAttribute('aria-label', t('looks.rename'));
|
|
486
|
+
tile.appendChild(input);
|
|
487
|
+
input.focus();
|
|
488
|
+
input.select();
|
|
489
|
+
let done = false;
|
|
490
|
+
const finish = async (keep) => {
|
|
491
|
+
if (done) return;
|
|
492
|
+
done = true;
|
|
493
|
+
const name = input.value.trim();
|
|
494
|
+
if (keep && name && name !== look.name) {
|
|
495
|
+
const next = await writeLooks(looks.map((l) => (l.id === look.id ? { ...l, name } : l)));
|
|
496
|
+
if (next) looks = next; else $('#saved').textContent = t('dress.notSaved');
|
|
497
|
+
}
|
|
498
|
+
renderLooks();
|
|
499
|
+
};
|
|
500
|
+
input.addEventListener('keydown', (e) => {
|
|
501
|
+
if (e.key === 'Enter') void finish(true);
|
|
502
|
+
else if (e.key === 'Escape') void finish(false);
|
|
503
|
+
});
|
|
504
|
+
input.addEventListener('blur', () => void finish(true));
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/* ---------- importing packs ---------- */
|
|
508
|
+
|
|
509
|
+
const MB = (n) => `${Math.round(n / 1048576)} MB`;
|
|
510
|
+
|
|
511
|
+
/** The page's framing of files for `POST /api/figures/import` (src/pack-import.ts `filesFromBundle`). */
|
|
512
|
+
function bundle(files) {
|
|
513
|
+
const enc = new TextEncoder();
|
|
514
|
+
const u32 = (n) => { const b = new Uint8Array(4); new DataView(b.buffer).setUint32(0, n, true); return b; };
|
|
515
|
+
return new Blob(files.flatMap(({ path, file }) => { const name = enc.encode(path); return [u32(name.length), name, u32(file.size), file]; }));
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* The files of the packs among `files` ({ path, file }, each path starting with the folder picked): the folders
|
|
520
|
+
* with a figure.json at most `depth` levels below the picked one, and not inside another of them.
|
|
521
|
+
*/
|
|
522
|
+
function packFiles(files, depth) {
|
|
523
|
+
const dirs = files.map((f) => f.path.split('/')).filter((p) => p[p.length - 1] === 'figure.json' && p.length - 2 <= depth)
|
|
524
|
+
.map((p) => p.slice(0, -1).join('/')).sort((a, b) => a.length - b.length);
|
|
525
|
+
const roots = dirs.filter((d, i) => !dirs.slice(0, i).some((r) => d.startsWith(r + '/')));
|
|
526
|
+
return files.filter((f) => roots.some((r) => f.path.startsWith(r + '/')));
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/** The files under a dropped folder (a FileSystemDirectoryEntry), as `packFiles` takes them: only what could be in a pack is read. */
|
|
530
|
+
async function droppedFiles(entry, path, depth) {
|
|
531
|
+
const list = [];
|
|
532
|
+
const reader = entry.createReader();
|
|
533
|
+
for (let batch; (batch = await new Promise((ok, bad) => reader.readEntries(ok, bad))).length;) list.push(...batch);
|
|
534
|
+
const inPack = depth === Infinity || list.some((e) => e.isFile && e.name === 'figure.json');
|
|
535
|
+
const out = [];
|
|
536
|
+
for (const e of list) {
|
|
537
|
+
if (e.isFile && inPack) out.push({ path: `${path}/${e.name}`, file: await new Promise((ok, bad) => e.file(ok, bad)) });
|
|
538
|
+
// inside a pack every folder is read; above one, only as deep as a pack is looked for
|
|
539
|
+
else if (e.isDirectory && (inPack || depth > 0)) out.push(...await droppedFiles(e, `${path}/${e.name}`, inPack ? Infinity : depth - 1));
|
|
540
|
+
}
|
|
541
|
+
return out;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const importDialog = (() => {
|
|
545
|
+
let token = '', revision = 0, working = false;
|
|
546
|
+
const cancel = { key: 'cancel', label: t('import.cancel') };
|
|
547
|
+
const show = (view, answer = () => close()) => {
|
|
548
|
+
const current = ++revision;
|
|
549
|
+
dialogs.show(view, result => { if (current === revision && !dialogStop.signal.aborted) answer(result); });
|
|
550
|
+
};
|
|
551
|
+
const close = () => {
|
|
552
|
+
revision++; dialogs.close();
|
|
553
|
+
if (token) void fetch('/api/figures/import/cancel', { method: 'POST', keepalive: true }).catch(() => {});
|
|
554
|
+
token = '';
|
|
555
|
+
};
|
|
556
|
+
dialogStop.signal.addEventListener('abort', close, { once: true });
|
|
557
|
+
const note = (title, text) => show({ title, body: text, actions: [{ key: 'cancel', label: t('import.ok'), kind: 'primary' }] });
|
|
558
|
+
const none = () => note(t('import.noneTitle'), t('import.noneText', { depth: packStatus.depth }));
|
|
559
|
+
const busy = title => show({ title, busy: true, actions: [] });
|
|
560
|
+
|
|
561
|
+
async function send(body, type) {
|
|
562
|
+
if (working || dialogStop.signal.aborted) return;
|
|
563
|
+
working = true; busy(t('import.reading'));
|
|
564
|
+
let res;
|
|
565
|
+
try {
|
|
566
|
+
res = await (await fetch('/api/figures/import', { method: 'POST', headers: { 'content-type': type }, body, signal: dialogStop.signal })).json();
|
|
567
|
+
} catch (err) {
|
|
568
|
+
if (!dialogStop.signal.aborted) note(t('import.failed'), t('import.offline'));
|
|
569
|
+
return;
|
|
570
|
+
} finally { working = false; }
|
|
571
|
+
if (dialogStop.signal.aborted) return;
|
|
572
|
+
if (res.error) return note(t('import.failed'), res.error);
|
|
573
|
+
token = res.token;
|
|
574
|
+
if (!res.packs.length && !res.problems.length) return none();
|
|
575
|
+
confirm(res, body.size);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function confirm({ packs: found, problems }, size) {
|
|
579
|
+
const warnings = [];
|
|
580
|
+
if (found.length && size > packStatus.large) warnings.push(t('import.large', { size: MB(size) }));
|
|
581
|
+
if (problems.length && found.length) warnings.push(t('import.someBad'));
|
|
582
|
+
for (const p of problems) warnings.push((p.dir || t('import.pickedLevel')) + t('import.reason', { reason: p.reason }));
|
|
583
|
+
show({
|
|
584
|
+
title: found.length ? t('import.found', { n: found.length }) : t('import.allBad'),
|
|
585
|
+
selectable: true,
|
|
586
|
+
items: found.map(p => ({
|
|
587
|
+
key: p.dir, label: nameOf(p.name), image: p.thumb ? new URL(p.thumb, location.href).href : undefined,
|
|
588
|
+
meta: [
|
|
589
|
+
[t('import.version', { version: p.version }), p.author].filter(Boolean).join(' · '),
|
|
590
|
+
t('import.contents', { axes: p.axes, words: p.words, sounds: p.sounds }),
|
|
591
|
+
...(p.credits.length ? [p.credits.map(c => t('import.credit', c)).join(t('import.creditSep'))] : []),
|
|
592
|
+
],
|
|
593
|
+
notice: p.installed ? p.installed === p.version ? t('import.reinstall', { version: p.version }) : t('import.upgrade', { installed: p.installed, version: p.version }) : undefined,
|
|
594
|
+
details: p.skipped.length ? { title: t('import.skipped', { n: p.skipped.length }), lines: p.skipped } : undefined,
|
|
595
|
+
})),
|
|
596
|
+
warnings, actions: [cancel, ...(found.length ? [{ key: 'install', label: t('import.button'), kind: 'primary', selection: true }] : [])],
|
|
597
|
+
}, result => {
|
|
598
|
+
if (result.action !== 'install') { close(); return; }
|
|
599
|
+
const chosen = found.filter(p => result.selected.includes(p.dir));
|
|
600
|
+
if (chosen.length) void install(chosen);
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function replace(chosen, conflicts) {
|
|
605
|
+
show({ title: t('import.replaceTitle'), body: t('import.replaceText'),
|
|
606
|
+
items: chosen.filter(p => conflicts.includes(p.id)).map(p => ({ key: p.id, label: nameOf(p.name), meta: [p.id] })),
|
|
607
|
+
actions: [cancel, { key: 'replace', label: t('import.replace'), kind: 'danger' }],
|
|
608
|
+
}, result => { if (result.action === 'replace') void install(chosen, conflicts); else close(); });
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
async function install(chosen, replaceIds = []) {
|
|
612
|
+
if (working || dialogStop.signal.aborted) return;
|
|
613
|
+
working = true; busy(t('import.button') + '…');
|
|
614
|
+
let res;
|
|
615
|
+
try {
|
|
616
|
+
res = await (await fetch('/api/figures/import/install', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ token, dirs: chosen.map(p => p.dir), replace: replaceIds }), signal: dialogStop.signal })).json();
|
|
617
|
+
} catch { res = { error: t('import.offline') }; }
|
|
618
|
+
finally { working = false; }
|
|
619
|
+
if (dialogStop.signal.aborted) return;
|
|
620
|
+
if (res.error) return note(t('import.failed'), res.error);
|
|
621
|
+
if (res.conflicts) return replace(chosen, res.conflicts);
|
|
622
|
+
token = ''; close();
|
|
623
|
+
const replacing = chosen.some(p => p.id === skin.figure);
|
|
624
|
+
$('#saved').textContent = t('import.done', { names: chosen.map(p => nameOf(p.name)).join(t('import.nameSep')) }) + (replacing ? t('import.replacing') : '');
|
|
625
|
+
sfx.sparkle();
|
|
626
|
+
if (replacing) { body?.dispose(); body = null; }
|
|
627
|
+
await loadPacks();
|
|
628
|
+
if (replacing) void showFigure(skin);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
async function uninstall(pack) {
|
|
632
|
+
working = true; busy(t('remove.go') + '…');
|
|
633
|
+
let res;
|
|
634
|
+
try {
|
|
635
|
+
res = await (await fetch('/api/figures/remove', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ id: pack.id }), signal: dialogStop.signal })).json();
|
|
636
|
+
} catch { res = { error: t('import.offline') }; }
|
|
637
|
+
finally { working = false; }
|
|
638
|
+
if (dialogStop.signal.aborted) return;
|
|
639
|
+
if (res.error) return note(t('remove.failed'), res.error);
|
|
640
|
+
close();
|
|
641
|
+
if (skin.figure === pack.id) await apply({ ...skin, figure: 'coo' }, true);
|
|
642
|
+
$('#saved').textContent = t('remove.done', { name: nameOf(pack.name) });
|
|
643
|
+
await loadPacks();
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
return {
|
|
647
|
+
/** Asks before a saved look is deleted; `go` deletes it. */
|
|
648
|
+
forget(look, go) {
|
|
649
|
+
if (working) return;
|
|
650
|
+
close();
|
|
651
|
+
show({ title: t('looks.deleteTitle', { name: look.name }), body: t('looks.deleteLead'),
|
|
652
|
+
actions: [cancel, { key: 'forget', label: t('looks.deleteGo'), kind: 'danger' }],
|
|
653
|
+
}, result => { close(); if (result.action === 'forget') void go(); });
|
|
654
|
+
},
|
|
655
|
+
/** Asks before deleting an installed pack; the pet wearing it changes to Coo. */
|
|
656
|
+
remove(pack) {
|
|
657
|
+
if (working) return;
|
|
658
|
+
close();
|
|
659
|
+
show({ title: t('remove.title', { name: nameOf(pack.name) }), body: t('remove.lead'),
|
|
660
|
+
actions: [cancel, { key: 'remove', label: t('remove.go'), kind: 'danger' }],
|
|
661
|
+
}, result => { if (result.action === 'remove') void uninstall(pack); else close(); });
|
|
662
|
+
},
|
|
663
|
+
choose() {
|
|
664
|
+
if (working) return;
|
|
665
|
+
close();
|
|
666
|
+
show({ title: t('import.title'), body: t('import.lead', { depth: packStatus.depth }) + '\n' + t('import.dropNote'),
|
|
667
|
+
actions: [cancel, { key: 'directory', label: t('import.pickDir'), file: 'directory' }, { key: 'zip', label: t('import.pickZip'), kind: 'primary', file: 'zip' }],
|
|
668
|
+
}, result => {
|
|
669
|
+
if (result.action === 'zip' && result.files?.length === 1) void send(result.files[0].file, 'application/zip');
|
|
670
|
+
else if (result.action === 'directory' && result.files?.length) {
|
|
671
|
+
const found = packFiles(result.files, packStatus.depth);
|
|
672
|
+
if (!found.length) none(); else void send(bundle(found), 'application/x-figure-files');
|
|
673
|
+
} else close();
|
|
674
|
+
});
|
|
675
|
+
},
|
|
676
|
+
async drop(items) {
|
|
677
|
+
if (working) return;
|
|
678
|
+
close();
|
|
679
|
+
const entries = [...items].map(i => i.webkitGetAsEntry?.()).filter(Boolean);
|
|
680
|
+
const zips = entries.filter(e => e.isFile && /\.zip$/i.test(e.name)), dirs = entries.filter(e => e.isDirectory);
|
|
681
|
+
working = true; busy(t('import.reading'));
|
|
682
|
+
let upload;
|
|
683
|
+
try {
|
|
684
|
+
if (zips.length === 1 && !dirs.length) upload = { body: await new Promise((ok, bad) => zips[0].file(ok, bad)), type: 'application/zip' };
|
|
685
|
+
else if (!dirs.length) { note(t('import.cannot'), t('import.dropOne')); return; }
|
|
686
|
+
else {
|
|
687
|
+
const files = [];
|
|
688
|
+
for (const d of dirs) files.push(...await droppedFiles(d, d.name, packStatus.depth));
|
|
689
|
+
const found = packFiles(files, packStatus.depth);
|
|
690
|
+
if (!found.length) { none(); return; }
|
|
691
|
+
upload = { body: bundle(found), type: 'application/x-figure-files' };
|
|
692
|
+
}
|
|
693
|
+
} catch (err) { if (!dialogStop.signal.aborted) note(t('import.readFailed'), String(err?.message ?? err)); return; }
|
|
694
|
+
finally { working = false; }
|
|
695
|
+
return send(upload.body, upload.type);
|
|
696
|
+
},
|
|
697
|
+
};
|
|
698
|
+
})();
|
|
699
|
+
|
|
700
|
+
// a zip or folders dragged onto the page
|
|
701
|
+
const dropHint = $('#dropHint');
|
|
702
|
+
const dragging = (e) => packStatus.importable && e.dataTransfer?.types.includes('Files');
|
|
703
|
+
let dragDepth = 0;
|
|
704
|
+
document.addEventListener('dragenter', (e) => { if (!dragging(e)) return; e.preventDefault(); dragDepth++; dropHint.hidden = false; });
|
|
705
|
+
document.addEventListener('dragover', (e) => { if (dragging(e)) e.preventDefault(); });
|
|
706
|
+
document.addEventListener('dragleave', () => { if (dragDepth && !--dragDepth) dropHint.hidden = true; });
|
|
707
|
+
document.addEventListener('drop', (e) => {
|
|
708
|
+
if (!dragging(e)) return;
|
|
709
|
+
e.preventDefault();
|
|
710
|
+
dragDepth = 0; dropHint.hidden = true;
|
|
711
|
+
void importDialog.drop(e.dataTransfer.items);
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
function render() {
|
|
715
|
+
renderFigure();
|
|
716
|
+
renderLooks();
|
|
717
|
+
const pal = $('#optPalette');
|
|
718
|
+
pal.textContent = '';
|
|
719
|
+
const palOpts = el('div', 'opts');
|
|
720
|
+
for (const p of PALETTES) {
|
|
721
|
+
const b = el('button', 'opt swatch');
|
|
722
|
+
b.setAttribute('aria-pressed', String(skin.palette === p.id));
|
|
723
|
+
b.innerHTML = `<svg viewBox="${CROP.palette}" aria-hidden="true" style="--sl-ink:${p.l[0]};--sl-eye:${p.l[1]};--sd-ink:${p.d[0]};--sd-eye:${p.d[1]}">${mini('neutral', { ...skin, palette: p.id, head: 'none', side: 'none', glasses: 'none', neck: 'none' })}</svg><span></span>`;
|
|
724
|
+
b.querySelector('span').textContent = cooName('palette', p.id);
|
|
725
|
+
b.addEventListener('click', () => { apply({ ...skin, palette: p.id }, true); sfx.sparkle(); body?.cue('cheer'); });
|
|
726
|
+
palOpts.appendChild(b);
|
|
727
|
+
}
|
|
728
|
+
pal.appendChild(palOpts);
|
|
729
|
+
for (const [slot, list, sel] of [['head', HEADS, '#optHead'], ['side', SIDES, '#optSide'], ['glasses', GLASSES, '#optGlasses'], ['neck', NECKS, '#optNeck']]) {
|
|
730
|
+
const box = $(sel);
|
|
731
|
+
box.textContent = '';
|
|
732
|
+
const opts = el('div', 'opts');
|
|
733
|
+
for (const id of list) {
|
|
734
|
+
const b = el('button', 'opt');
|
|
735
|
+
b.setAttribute('aria-pressed', String(skin[slot] === id));
|
|
736
|
+
b.innerHTML = `<svg viewBox="${CROP[slot]}" aria-hidden="true">${mini('neutral', { ...skin, [slot]: id })}</svg><span></span>`;
|
|
737
|
+
b.querySelector('span').textContent = cooName(slot, id);
|
|
738
|
+
b.addEventListener('click', () => {
|
|
739
|
+
apply(wear(skin, slot, id), true);
|
|
740
|
+
sfx.pop(); if (id !== 'none') { sfx.sparkle(); body?.cue('cheer'); }
|
|
741
|
+
body?.cue('bounce');
|
|
742
|
+
});
|
|
743
|
+
opts.appendChild(b);
|
|
744
|
+
}
|
|
745
|
+
box.appendChild(opts);
|
|
746
|
+
if (skin[slot] !== 'none') box.appendChild(colorRow(slot, skin[slot]));
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
function connect() {
|
|
751
|
+
const ws = new WebSocket(`ws://${location.host}/socket?role=dress`);
|
|
752
|
+
ws.onmessage = (e) => {
|
|
753
|
+
const m = JSON.parse(e.data);
|
|
754
|
+
if ((m.t === 'init' || m.t === 'prefs') && (m.theme === 'dark' || m.theme === 'light') && m.theme !== theme) { theme = m.theme; showTheme(); modeBtn.show(theme); body?.set({ theme }); renderLooks(); }
|
|
755
|
+
if (m.t === 'init') { loadPacks(); void loadLooks(); announcePainted(); }
|
|
756
|
+
if ((m.t === 'init' || m.t === 'prefs') && typeof m.motion === 'string') setMotionChoice(window, m.motion);
|
|
757
|
+
if ((m.t === 'init' || m.t === 'prefs') && typeof m.scale === 'number') scaleHeard.heard(m.scale, sizer.active || !!scaleTimer);
|
|
758
|
+
if ((m.t === 'init' || m.t === 'prefs') && m.skin && JSON.stringify(normalizeSkin(m.skin)) !== JSON.stringify(skin)) {
|
|
759
|
+
const worn = looks.find((l) => sameLook(l.skin, m.skin) && (l.scale ?? scale) === scale);
|
|
760
|
+
if (worn) activeLook = worn.id;
|
|
761
|
+
apply(normalizeSkin(m.skin), false);
|
|
762
|
+
}
|
|
763
|
+
if ((m.t === 'init' || m.t === 'prefs') && m.sounds && typeof m.sounds === 'object') sfx.configure({ volume: m.sounds.volume });
|
|
764
|
+
if ((m.t === 'init' || m.t === 'prefs') && typeof m.language === 'string' && m.language !== language()) void useLanguage(m.language).then(() => { showText(); render(); });
|
|
765
|
+
};
|
|
766
|
+
ws.onclose = () => setTimeout(connect, 2000);
|
|
767
|
+
}
|
|
768
|
+
void ready.then(() => { connect(); apply(skin, false); });
|
|
769
|
+
|
|
770
|
+
let last = performance.now();
|
|
771
|
+
/** The last error the frame loop logged, so one that keeps recurring is reported once, not per frame. */
|
|
772
|
+
let frameErr = null;
|
|
773
|
+
function frame(now) {
|
|
774
|
+
const dt = Math.min(.05, (now - last) / 1000); last = now;
|
|
775
|
+
try {
|
|
776
|
+
body?.tick(dt);
|
|
777
|
+
preview.style.cursor = body?.layout?.cursor ?? '';
|
|
778
|
+
} catch (err) {
|
|
779
|
+
// a throwing step must not take the loop with it: the next frame is only asked for below, and
|
|
780
|
+
// without it the preview freezes for good (a broken figure throws again on every frame it draws)
|
|
781
|
+
const msg = err?.message ?? String(err);
|
|
782
|
+
if (msg !== frameErr) { frameErr = msg; console.error(err); }
|
|
783
|
+
}
|
|
784
|
+
requestAnimationFrame(frame);
|
|
785
|
+
}
|
|
786
|
+
requestAnimationFrame(frame);
|