@catchlight/editor-web 0.0.0-stage → 0.5.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.
Files changed (111) hide show
  1. package/LICENSE-APACHE +201 -0
  2. package/LICENSE-MIT +21 -0
  3. package/README.md +27 -2
  4. package/THIRD-PARTY-NOTICES.md +331 -0
  5. package/dist/editor/CatchlightEditor.d.ts +21 -0
  6. package/dist/editor/CatchlightEditor.js +720 -0
  7. package/dist/editor/dialogs.d.ts +18 -0
  8. package/dist/editor/dialogs.js +71 -0
  9. package/dist/editor/import-panel.d.ts +6 -0
  10. package/dist/editor/import-panel.js +98 -0
  11. package/dist/editor/logo.svg +11 -0
  12. package/dist/editor/notification.d.ts +4 -0
  13. package/dist/editor/notification.js +24 -0
  14. package/dist/editor/stage.d.ts +13 -0
  15. package/dist/editor/stage.js +24 -0
  16. package/dist/editor/test/setup.d.ts +5 -0
  17. package/dist/editor/test/setup.js +5 -0
  18. package/dist/index.d.ts +11 -0
  19. package/dist/index.js +10 -0
  20. package/dist/lib/animations-panel.d.ts +10 -0
  21. package/dist/lib/animations-panel.js +48 -0
  22. package/dist/lib/authoring.d.ts +17 -0
  23. package/dist/lib/authoring.js +116 -0
  24. package/dist/lib/binding-grid.d.ts +19 -0
  25. package/dist/lib/binding-grid.js +92 -0
  26. package/dist/lib/bindings.d.ts +21 -0
  27. package/dist/lib/bindings.js +100 -0
  28. package/dist/lib/camera.d.ts +21 -0
  29. package/dist/lib/camera.js +61 -0
  30. package/dist/lib/controls-panel.d.ts +36 -0
  31. package/dist/lib/controls-panel.js +201 -0
  32. package/dist/lib/controls.d.ts +103 -0
  33. package/dist/lib/controls.js +162 -0
  34. package/dist/lib/editing.d.ts +126 -0
  35. package/dist/lib/editing.js +415 -0
  36. package/dist/lib/editor-context.d.ts +9 -0
  37. package/dist/lib/editor-context.js +13 -0
  38. package/dist/lib/expressions-panel.d.ts +10 -0
  39. package/dist/lib/expressions-panel.js +120 -0
  40. package/dist/lib/file-open.d.ts +11 -0
  41. package/dist/lib/file-open.js +25 -0
  42. package/dist/lib/file-save.d.ts +29 -0
  43. package/dist/lib/file-save.js +68 -0
  44. package/dist/lib/gizmo.d.ts +13 -0
  45. package/dist/lib/gizmo.js +247 -0
  46. package/dist/lib/image-markup.d.ts +12 -0
  47. package/dist/lib/image-markup.js +153 -0
  48. package/dist/lib/index.d.ts +71 -0
  49. package/dist/lib/index.js +44 -0
  50. package/dist/lib/inspector.d.ts +19 -0
  51. package/dist/lib/inspector.js +161 -0
  52. package/dist/lib/latest.d.ts +3 -0
  53. package/dist/lib/latest.js +9 -0
  54. package/dist/lib/layer-import.d.ts +38 -0
  55. package/dist/lib/layer-import.js +187 -0
  56. package/dist/lib/mesh-editor.d.ts +13 -0
  57. package/dist/lib/mesh-editor.js +337 -0
  58. package/dist/lib/model-panels.d.ts +23 -0
  59. package/dist/lib/model-panels.js +180 -0
  60. package/dist/lib/node-actions.d.ts +42 -0
  61. package/dist/lib/node-actions.js +115 -0
  62. package/dist/lib/node-drag.d.ts +18 -0
  63. package/dist/lib/node-drag.js +65 -0
  64. package/dist/lib/node-patch.d.ts +8 -0
  65. package/dist/lib/node-patch.js +10 -0
  66. package/dist/lib/node-tree.d.ts +36 -0
  67. package/dist/lib/node-tree.js +221 -0
  68. package/dist/lib/official.d.ts +82 -0
  69. package/dist/lib/official.js +197 -0
  70. package/dist/lib/param-actions.d.ts +48 -0
  71. package/dist/lib/param-actions.js +106 -0
  72. package/dist/lib/param-list.d.ts +36 -0
  73. package/dist/lib/param-list.js +73 -0
  74. package/dist/lib/param-slider.d.ts +44 -0
  75. package/dist/lib/param-slider.js +149 -0
  76. package/dist/lib/pose.d.ts +13 -0
  77. package/dist/lib/pose.js +32 -0
  78. package/dist/lib/posing-panel.d.ts +40 -0
  79. package/dist/lib/posing-panel.js +154 -0
  80. package/dist/lib/presence.d.ts +27 -0
  81. package/dist/lib/presence.js +98 -0
  82. package/dist/lib/properties-panel.d.ts +13 -0
  83. package/dist/lib/properties-panel.js +63 -0
  84. package/dist/lib/recording-panel.d.ts +4 -0
  85. package/dist/lib/recording-panel.js +161 -0
  86. package/dist/lib/replica.d.ts +18 -0
  87. package/dist/lib/replica.js +34 -0
  88. package/dist/lib/review.d.ts +113 -0
  89. package/dist/lib/review.js +529 -0
  90. package/dist/lib/rigging.d.ts +10 -0
  91. package/dist/lib/rigging.js +116 -0
  92. package/dist/lib/selection.d.ts +3 -0
  93. package/dist/lib/selection.js +2 -0
  94. package/dist/lib/session-list.d.ts +16 -0
  95. package/dist/lib/session-list.js +7 -0
  96. package/dist/lib/sessions.d.ts +8 -0
  97. package/dist/lib/sessions.js +29 -0
  98. package/dist/lib/test/harness.d.ts +32 -0
  99. package/dist/lib/test/harness.js +139 -0
  100. package/dist/lib/test/setup.d.ts +4 -0
  101. package/dist/lib/test/setup.js +5 -0
  102. package/dist/lib/versions.d.ts +69 -0
  103. package/dist/lib/versions.js +250 -0
  104. package/dist/lib/viewport.d.ts +57 -0
  105. package/dist/lib/viewport.js +315 -0
  106. package/dist/lib/workspace-layout.d.ts +24 -0
  107. package/dist/lib/workspace-layout.js +145 -0
  108. package/dist/lib/workspace.d.ts +29 -0
  109. package/dist/lib/workspace.js +263 -0
  110. package/dist/theme.css +4573 -0
  111. package/package.json +41 -4
@@ -0,0 +1,161 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { useCallback, useState } from "react";
3
+ import { useNodePatch } from "./node-patch.js";
4
+ import { useNodeInfo, useReplica } from "./replica.js";
5
+ import { useSelection } from "./selection.js";
6
+ /** Wire spellings, in `BlendMode` declaration order; `node_set` rejects any other name. */
7
+ export const BLEND_MODES = [
8
+ "normal",
9
+ "multiply",
10
+ "color_dodge",
11
+ "linear_dodge",
12
+ "screen",
13
+ "clip_to_lower",
14
+ "slice_from_lower",
15
+ "overlay",
16
+ "color_burn",
17
+ "linear_burn",
18
+ "darken",
19
+ "lighten",
20
+ "add",
21
+ "inverse",
22
+ "subtract",
23
+ ];
24
+ export function InspectorRoot({ session, onError, ...rest }) {
25
+ const { node } = useSelection();
26
+ const info = useNodeInfo(session, node);
27
+ const patch = useNodePatch(session);
28
+ const textures = useReplica(session, readTextures);
29
+ const id = info?.id;
30
+ // Never rejects: a control clears its draft once the send settles.
31
+ const submit = useCallback((fields) => {
32
+ if (id === undefined)
33
+ return Promise.resolve();
34
+ return patch(id, fields).catch((cause) => {
35
+ if (onError)
36
+ onError(cause);
37
+ else
38
+ console.warn("catchlight: setting a node field failed", cause);
39
+ });
40
+ }, [patch, id, onError]);
41
+ // A selection is view state and outlives a deleted node.
42
+ if (!info) {
43
+ return (_jsx("div", { "data-catchlight-inspector": "", "data-catchlight-inspector-empty": "", ...rest, children: _jsx("p", { children: "No node selected." }) }));
44
+ }
45
+ return (_jsx("div", { "data-catchlight-inspector": "", ...rest, children: _jsx(Fields, { info: info, textures: textures, submit: submit }, info.id) }));
46
+ }
47
+ export const Inspector = { Root: InspectorRoot };
48
+ function Fields({ info, textures, submit, }) {
49
+ // Destructured so `!= null` narrows inside the handlers too.
50
+ const { translate, rotate, scale, opacity, mask_threshold, blend_mode, tint, screen_tint } = info;
51
+ const { propagate_meshgroup, mg_translate_children } = info;
52
+ return (_jsxs(_Fragment, { children: [_jsx(Row, { label: "Id", field: "id", children: _jsx("span", { "data-catchlight-inspector-value": "", children: info.id }) }), _jsx(Row, { label: "Kind", field: "kind", children: _jsx("span", { "data-catchlight-inspector-value": "", children: info.kind }) }), _jsx(Row, { label: "Parent", field: "parent", children: _jsx("span", { "data-catchlight-inspector-value": "", children: info.parent ?? "—" }) }), _jsx(Row, { label: "Name", field: "name", children: _jsx(TextInput, { field: "name", label: "Name", value: info.name, commit: (next) => submit({ name: next }) }) }), _jsxs("section", { "data-catchlight-inspector-section": "", "aria-label": "Transform", children: [_jsx("h3", { children: "Transform" }), _jsx(Row, { label: "Position", field: "translate", children: _jsx(Axes, { field: "translate", label: "Translate", axes: XYZ, values: translate, commit: (axis, next) => submit({ translate: with3(translate, axis, next) }) }) }), _jsx(Row, { label: "Rotation", field: "rotate", children: _jsx(Axes, { field: "rotate", unit: "\u00B0", label: "Rotate", axes: XYZ, values: rotate.map((v) => (v * 180) / Math.PI), commit: (axis, next) => submit({ rotate: with3(rotate, axis, (next * Math.PI) / 180) }) }) }), _jsx(Row, { label: "Scale", field: "scale", children: _jsx(Axes, { field: "scale", unit: "\u00D7", label: "Scale", axes: XY, values: scale, commit: (axis, next) => submit({ scale: with2(scale, axis, next) }) }) }), _jsx(Row, { label: "Z order", field: "z_order", children: _jsx(NumberInput, { field: "z_order", label: "Z order", value: info.z_order, commit: (next) => submit({ z_order: next }) }) }), _jsx(Row, { label: "Lock to root", field: "lock_to_root", children: _jsx(CheckInput, { field: "lock_to_root", label: "Lock to root", value: info.lock_to_root, commit: (next) => submit({ lock_to_root: next }) }) })] }), _jsxs("section", { "data-catchlight-inspector-section": "", "aria-label": "Appearance", children: [_jsx("h3", { children: "Appearance" }), _jsx(Row, { label: "Enabled", field: "enabled", children: _jsx(CheckInput, { field: "enabled", label: "Enabled", value: info.enabled, commit: (next) => submit({ enabled: next }) }) }), opacity != null && (_jsx(Row, { label: "Opacity", field: "opacity", children: _jsx(NumberInput, { field: "opacity", label: "Opacity", value: opacity, min: 0, max: 1, commit: (next) => submit({ opacity: next }) }) })), mask_threshold != null && (_jsx(Row, { label: "Mask threshold", field: "mask_threshold", children: _jsx(NumberInput, { field: "mask_threshold", label: "Mask threshold", value: mask_threshold, min: 0, max: 1, commit: (next) => submit({ mask_threshold: next }) }) })), blend_mode != null && (_jsx(Row, { label: "Blend", field: "blend_mode", children: _jsx(SelectInput, { field: "blend_mode", label: "Blend mode", value: blend_mode, options: blendOptions(blend_mode), commit: (next) => submit({ blend_mode: next }) }) })), tint != null && (_jsx(Row, { label: "Tint", field: "tint", children: _jsx(Axes, { field: "tint", label: "Tint", axes: RGB, values: tint, min: 0, max: 1, commit: (axis, next) => submit({ tint: with3(tint, axis, next) }) }) })), screen_tint != null && (_jsx(Row, { label: "Screen tint", field: "screen_tint", children: _jsx(Axes, { field: "screen_tint", label: "Screen tint", axes: RGB, values: screen_tint, min: 0, max: 1, commit: (axis, next) => submit({ screen_tint: with3(screen_tint, axis, next) }) }) })), info.kind === "part" && (_jsx(Row, { label: "Texture", field: "texture", children: _jsx(SelectInput, { field: "texture", label: "Texture", value: info.texture ?? NO_TEXTURE, options: textureOptions(textures), commit: (next) => submit({ texture: next === NO_TEXTURE ? null : next }) }) }))] }), propagate_meshgroup != null && (_jsx(Row, { label: "Propagate mesh group", field: "propagate_meshgroup", children: _jsx(CheckInput, { field: "propagate_meshgroup", label: "Propagate mesh group", value: propagate_meshgroup, commit: (next) => submit({ propagate_meshgroup: next }) }) })), mg_translate_children != null && (_jsx(Row, { label: "Translate children", field: "mg_translate_children", children: _jsx(CheckInput, { field: "mg_translate_children", label: "Translate children", value: mg_translate_children, commit: (next) => submit({ mg_translate_children: next }) }) }))] }));
53
+ }
54
+ function Row({ label, field, children, }) {
55
+ return (_jsxs("div", { "data-catchlight-inspector-row": "", "data-field": field, children: [_jsx("span", { "data-catchlight-inspector-label": "", children: label }), _jsx("div", { "data-catchlight-inspector-controls": "", children: children })] }));
56
+ }
57
+ function Axes({ field, label, axes, values, unit, min, max, commit, }) {
58
+ return (_jsx(_Fragment, { children: values.map((value, axis) => {
59
+ const name = axes[axis] ?? String(axis);
60
+ return (_jsxs("label", { "data-catchlight-axis-input": "", children: [_jsx("span", { "data-catchlight-number-prefix": "", "aria-hidden": "true", children: name }), _jsx(NumberInput, { field: field, axis: name, label: `${label} ${name}`, value: value, min: min, max: max, commit: (next) => commit(axis, next) }), unit && _jsx("span", { "data-catchlight-number-unit": "", "aria-hidden": "true", children: unit })] }, name));
61
+ }) }));
62
+ }
63
+ function NumberInput({ field, label, value, axis, min, max, commit, }) {
64
+ const draft = useDraft(String(Math.round(value * 1000) / 1000), (text) => {
65
+ const next = Number(text);
66
+ // Empty or unparseable text is not an edit; the box reverts.
67
+ if (text.trim() === "" ||
68
+ !Number.isFinite(next) ||
69
+ next === value ||
70
+ (min !== undefined && next < min) ||
71
+ (max !== undefined && next > max))
72
+ return undefined;
73
+ return commit(next);
74
+ });
75
+ return (_jsx("input", { type: "number", "data-catchlight-field": field, "data-axis": axis, "aria-label": label, min: min, max: max, step: "any", value: draft.text, onInput: draft.type, onChange: noop, onBlur: draft.commit, onKeyDown: draft.keys }));
76
+ }
77
+ function TextInput({ field, label, value, commit, }) {
78
+ const draft = useDraft(value, (text) => (text === value ? undefined : commit(text)));
79
+ return (_jsx("input", { type: "text", "data-catchlight-field": field, "aria-label": label, value: draft.text, onInput: draft.type, onChange: noop, onBlur: draft.commit, onKeyDown: draft.keys }));
80
+ }
81
+ function CheckInput({ field, label, value, commit, }) {
82
+ return (_jsx("input", { type: "checkbox", "data-catchlight-field": field, "aria-label": label, checked: value, onChange: (event) => {
83
+ void commit(event.currentTarget.checked);
84
+ } }));
85
+ }
86
+ function SelectInput({ field, label, value, options, commit, }) {
87
+ return (_jsx("select", { "data-catchlight-field": field, "aria-label": label, value: value, onChange: (event) => {
88
+ const next = event.currentTarget.value;
89
+ if (next === value)
90
+ return;
91
+ void commit(next);
92
+ }, children: options.map((option) => (_jsx("option", { value: option.value, children: option.label }, option.value))) }));
93
+ }
94
+ // React requires a controlled input's `onChange`; the native input event is
95
+ // taken once through `onInput` so a host's handler does not fire twice.
96
+ function noop() { }
97
+ // The draft outlives the send so the box does not flash the old value for a
98
+ // round trip. `authored` returns `undefined` when the text is not an edit.
99
+ function useDraft(shown, authored) {
100
+ const [draft, setDraft] = useState(undefined);
101
+ const commit = () => {
102
+ if (draft === undefined)
103
+ return;
104
+ const sending = authored(draft);
105
+ if (!sending) {
106
+ setDraft(undefined);
107
+ return;
108
+ }
109
+ void sending.then(() => setDraft(undefined));
110
+ };
111
+ return {
112
+ text: draft ?? shown,
113
+ type: (event) => setDraft(event.currentTarget.value),
114
+ commit,
115
+ keys: (event) => {
116
+ if (event.key === "Enter") {
117
+ event.preventDefault();
118
+ commit();
119
+ }
120
+ else if (event.key === "Escape") {
121
+ setDraft(undefined);
122
+ }
123
+ },
124
+ };
125
+ }
126
+ const XYZ = ["x", "y", "z"];
127
+ const XY = ["x", "y"];
128
+ const RGB = ["r", "g", "b"];
129
+ // `texture` is a merge patch: absent is unchanged, `null` draws none. This
130
+ // option is the one that sends `null`.
131
+ const NO_TEXTURE = "";
132
+ function blendOptions(current) {
133
+ const names = [...BLEND_MODES];
134
+ // A file can carry a mode this build does not list.
135
+ if (!names.includes(current))
136
+ names.unshift(current);
137
+ return names.map((name) => ({
138
+ value: name,
139
+ label: name.replaceAll("_", " ").replace(/^./, (c) => c.toUpperCase()),
140
+ }));
141
+ }
142
+ function textureOptions(textures) {
143
+ return [
144
+ { value: NO_TEXTURE, label: "none" },
145
+ ...textures.map((texture) => ({
146
+ value: texture.id,
147
+ label: `${texture.id} (${texture.width}×${texture.height})`,
148
+ })),
149
+ ];
150
+ }
151
+ function with3(v, axis, next) {
152
+ const out = [v[0], v[1], v[2]];
153
+ out[axis] = next;
154
+ return out;
155
+ }
156
+ function with2(v, axis, next) {
157
+ const out = [v[0], v[1]];
158
+ out[axis] = next;
159
+ return out;
160
+ }
161
+ const readTextures = (session) => session.textures();
@@ -0,0 +1,3 @@
1
+ /** The newest render value, for callbacks that must not be rebuilt when it changes. */
2
+ import type { RefObject } from "react";
3
+ export declare function useLatest<T>(value: T): RefObject<T>;
@@ -0,0 +1,9 @@
1
+ /** The newest render value, for callbacks that must not be rebuilt when it changes. */
2
+ import { useRef } from "react";
3
+ export function useLatest(value) {
4
+ const box = useRef(value);
5
+ // Assigned in render on purpose: an effect would leave a native listener
6
+ // that fires before the commit reading the previous render's value.
7
+ box.current = value;
8
+ return box;
9
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Importing See-through layers: the artwork, the steps to run, and expression
3
+ * art that is drawn or picked. Drawn pictures cost money, so they are kept
4
+ * until other artwork is chosen, even when an import fails.
5
+ */
6
+ import type { ExpressionArtKind, ImageModel, ImageModelChange, Layers, RigOptions, Session } from "@catchlight/editor-core";
7
+ export type ArtSource = "none" | "draw" | "file";
8
+ export interface ArtChoice {
9
+ readonly source: ArtSource;
10
+ readonly drawn?: Uint8Array | undefined;
11
+ readonly picked?: Uint8Array | undefined;
12
+ readonly drawing: boolean;
13
+ readonly error?: string | undefined;
14
+ }
15
+ export interface LayerImport {
16
+ readonly layers: Layers | undefined;
17
+ readonly options: RigOptions;
18
+ readonly art: Readonly<Record<ExpressionArtKind, ArtChoice>>;
19
+ readonly imageModel: ImageModel | undefined;
20
+ /** An import is running, or a picture is being drawn or read. */
21
+ readonly busy: boolean;
22
+ /** Chosen files are being read; `busy` too. */
23
+ readonly reading: boolean;
24
+ readonly rigging: boolean;
25
+ readonly error: string | undefined;
26
+ chooseLayers(files: readonly File[]): Promise<void>;
27
+ /** The drawing or the head cut-out that goes with the layers. */
28
+ choosePicture(name: "drawing" | "head", file: File | undefined): Promise<void>;
29
+ setOption(name: keyof RigOptions, on: boolean): void;
30
+ setArtSource(art: ExpressionArtKind, source: ArtSource): void;
31
+ chooseArt(art: ExpressionArtKind, file: File): Promise<void>;
32
+ drawArt(art: ExpressionArtKind): Promise<Uint8Array | undefined>;
33
+ refreshImageModel(): Promise<void>;
34
+ changeImageModel(change: ImageModelChange): Promise<boolean>;
35
+ /** Draws what is still missing, then rigs; nothing is rigged if a drawing fails. */
36
+ start(): Promise<Session | undefined>;
37
+ }
38
+ export declare function useLayerImport(): LayerImport;
@@ -0,0 +1,187 @@
1
+ /**
2
+ * Importing See-through layers: the artwork, the steps to run, and expression
3
+ * art that is drawn or picked. Drawn pictures cost money, so they are kept
4
+ * until other artwork is chosen, even when an import fails.
5
+ */
6
+ import { useCallback, useEffect, useRef, useState } from "react";
7
+ import { useEditor } from "./editor-context.js";
8
+ const ART = ["mouth_sheet", "closed_eyes"];
9
+ const DEFAULT_ART = { source: "draw", drawing: false };
10
+ export function useLayerImport() {
11
+ const editor = useEditor();
12
+ const [layers, setLayers] = useState();
13
+ const [options, setOptions] = useState({
14
+ blush: true,
15
+ hair_shadow: true,
16
+ clothing_shadows: true,
17
+ tune_hair: true,
18
+ });
19
+ const [art, setArt] = useState({
20
+ mouth_sheet: DEFAULT_ART,
21
+ closed_eyes: DEFAULT_ART,
22
+ });
23
+ const [imageModel, setImageModel] = useState();
24
+ const [reads, setReads] = useState(0);
25
+ const [running, setRunning] = useState(false);
26
+ const [rigging, setRigging] = useState(false);
27
+ const [error, setError] = useState();
28
+ /** Counts changes of artwork, so a picture drawn from older artwork is dropped. */
29
+ const artwork = useRef(0);
30
+ /** Art last turned off by hand. Pictures found beside layers do not turn paid drawing back on. */
31
+ const optedOut = useRef(new Set());
32
+ const lastModelChange = useRef(Promise.resolve());
33
+ const change = (kind, to) => setArt((all) => ({ ...all, [kind]: { ...all[kind], ...to } }));
34
+ const forgetDrawn = () => {
35
+ artwork.current += 1;
36
+ for (const kind of ART)
37
+ change(kind, { drawn: undefined, drawing: false, error: undefined });
38
+ };
39
+ const attempt = async (task) => {
40
+ setError(undefined);
41
+ try {
42
+ return await task();
43
+ }
44
+ catch (cause) {
45
+ setError(messageOf(cause));
46
+ return undefined;
47
+ }
48
+ };
49
+ const read = async (task) => {
50
+ setReads((count) => count + 1);
51
+ try {
52
+ return await attempt(task);
53
+ }
54
+ finally {
55
+ setReads((count) => count - 1);
56
+ }
57
+ };
58
+ const refreshImageModel = useCallback(() => editor.imageModel().then(setImageModel, (cause) => setError(messageOf(cause))), [editor]);
59
+ useEffect(() => {
60
+ artwork.current += 1;
61
+ setLayers(undefined);
62
+ optedOut.current.clear();
63
+ setArt({ mouth_sheet: DEFAULT_ART, closed_eyes: DEFAULT_ART });
64
+ setImageModel(undefined);
65
+ setError(undefined);
66
+ }, [editor]);
67
+ const drawArt = async (kind) => {
68
+ if (!layers)
69
+ return undefined;
70
+ const from = artwork.current;
71
+ const finish = (to) => {
72
+ if (from === artwork.current)
73
+ change(kind, { ...to, drawing: false });
74
+ };
75
+ change(kind, { drawing: true, error: undefined });
76
+ try {
77
+ const drawn = await editor.drawExpressionArt(kind, layers);
78
+ finish({ drawn });
79
+ return drawn;
80
+ }
81
+ catch (cause) {
82
+ finish({ error: messageOf(cause) });
83
+ return undefined;
84
+ }
85
+ };
86
+ return {
87
+ layers,
88
+ options,
89
+ art,
90
+ imageModel,
91
+ busy: running || reads > 0 || ART.some((kind) => art[kind].drawing),
92
+ reading: reads > 0,
93
+ rigging,
94
+ error,
95
+ chooseLayers: async (files) => {
96
+ const found = await read(() => editor.readLayers(files));
97
+ if (!found)
98
+ return;
99
+ const { mouthSheet, closedEyes, ...chosen } = found;
100
+ setLayers(chosen);
101
+ forgetDrawn();
102
+ const beside = { mouth_sheet: mouthSheet, closed_eyes: closedEyes };
103
+ for (const kind of ART) {
104
+ const picked = beside[kind];
105
+ if (picked)
106
+ change(kind, { source: "file", picked });
107
+ else if (layers) {
108
+ if (art[kind].source === "file") {
109
+ change(kind, { source: optedOut.current.has(kind) ? "none" : "draw" });
110
+ }
111
+ change(kind, { picked: undefined });
112
+ }
113
+ }
114
+ },
115
+ choosePicture: async (name, file) => {
116
+ const bytes = file && (await read(() => bytesOf(file)));
117
+ if (file && !bytes)
118
+ return;
119
+ setLayers((held) => {
120
+ if (!held)
121
+ return held;
122
+ const kept = held.artwork.filter(([attachment]) => attachment !== name);
123
+ return { ...held, artwork: bytes ? [...kept, [name, bytes]] : kept };
124
+ });
125
+ forgetDrawn();
126
+ },
127
+ setOption: (name, on) => setOptions((all) => ({ ...all, [name]: on })),
128
+ setArtSource: (kind, source) => {
129
+ if (source === "none")
130
+ optedOut.current.add(kind);
131
+ else
132
+ optedOut.current.delete(kind);
133
+ change(kind, { source, error: undefined });
134
+ },
135
+ chooseArt: async (kind, file) => {
136
+ const picked = await read(() => bytesOf(file));
137
+ if (picked)
138
+ change(kind, { picked, error: undefined });
139
+ },
140
+ drawArt,
141
+ refreshImageModel,
142
+ changeImageModel: async (to) => {
143
+ const changing = lastModelChange.current.then(() => attempt(() => editor.setImageModel(to)));
144
+ lastModelChange.current = changing;
145
+ const changed = await changing;
146
+ if (changed)
147
+ setImageModel(changed);
148
+ return changed !== undefined;
149
+ },
150
+ start: async () => {
151
+ if (!layers)
152
+ return undefined;
153
+ setRunning(true);
154
+ try {
155
+ const pictures = await Promise.all(ART.map((kind) => {
156
+ const { source, drawn, picked } = art[kind];
157
+ if (source === "draw")
158
+ return drawn ?? drawArt(kind);
159
+ return source === "file" ? picked : undefined;
160
+ }));
161
+ const missing = ART.some((kind, i) => art[kind].source !== "none" && !pictures[i]);
162
+ if (missing) {
163
+ setError("An expression picture is missing, so nothing was imported.");
164
+ return undefined;
165
+ }
166
+ const [mouthSheet, closedEyes] = pictures;
167
+ const full = {
168
+ ...layers,
169
+ ...(mouthSheet && { mouthSheet }),
170
+ ...(closedEyes && { closedEyes }),
171
+ };
172
+ setRigging(true);
173
+ return await attempt(() => editor.importLayers(full, options));
174
+ }
175
+ finally {
176
+ setRigging(false);
177
+ setRunning(false);
178
+ }
179
+ },
180
+ };
181
+ }
182
+ async function bytesOf(file) {
183
+ return new Uint8Array(await file.arrayBuffer());
184
+ }
185
+ function messageOf(cause) {
186
+ return cause instanceof Error ? cause.message : String(cause);
187
+ }
@@ -0,0 +1,13 @@
1
+ /** Dragging never touches the posed model's scratch. */
2
+ import type { Camera, MeshDraftView } from "@catchlight/editor-core";
3
+ import type { Bounds, Size } from "./camera.js";
4
+ export declare function meshBounds(mesh: MeshDraftView): [number, number, number, number];
5
+ export declare function MeshCanvas({ camera, onCameraChange, onFit, onResize, panMode, }: {
6
+ camera: Camera;
7
+ onCameraChange: (c: Camera) => void;
8
+ onFit: (c: Camera, bounds?: Bounds) => void;
9
+ onResize?: (size: Size) => void;
10
+ panMode: boolean;
11
+ }): import("react").JSX.Element | null;
12
+ export declare function MeshTools(): import("react").JSX.Element;
13
+ export declare function MeshInspector(): import("react").JSX.Element;