@moonwitness/ui 7.3.1 → 9.0.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 (260) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/package.json +2 -3
  3. package/scripts/consumer-smoke.mjs +0 -45
  4. package/scripts/generate-icons.mjs +0 -110
  5. package/scripts/generate-token-artifacts.mjs +0 -90
  6. package/scripts/icon-svg-to-jsx.mjs +0 -249
  7. package/scripts/verification-diagnostics.mjs +0 -40
  8. package/scripts/verify-package.mjs +0 -412
  9. package/src/cartoon/anime-avatar.tsx +0 -628
  10. package/src/cartoon/anime-stage.tsx +0 -458
  11. package/src/cartoon/index.tsx +0 -542
  12. package/src/cartoon.tsx +0 -645
  13. package/src/characters.ts +0 -625
  14. package/src/components/accordion.tsx +0 -77
  15. package/src/components/alert.tsx +0 -139
  16. package/src/components/appearance-menu.tsx +0 -181
  17. package/src/components/artwork-frame.tsx +0 -49
  18. package/src/components/audio.tsx +0 -97
  19. package/src/components/avatar.tsx +0 -75
  20. package/src/components/badge.tsx +0 -94
  21. package/src/components/brand-mark.tsx +0 -73
  22. package/src/components/breadcrumbs.tsx +0 -98
  23. package/src/components/button.tsx +0 -340
  24. package/src/components/card.tsx +0 -171
  25. package/src/components/character-badge.tsx +0 -58
  26. package/src/components/character-preview.tsx +0 -104
  27. package/src/components/character-stage.tsx +0 -225
  28. package/src/components/chart.tsx +0 -481
  29. package/src/components/checkbox.tsx +0 -64
  30. package/src/components/comic-panel.tsx +0 -301
  31. package/src/components/comic-theater.tsx +0 -170
  32. package/src/components/command.tsx +0 -189
  33. package/src/components/confirm-dialog.tsx +0 -129
  34. package/src/components/copy-button.tsx +0 -62
  35. package/src/components/date-picker.tsx +0 -126
  36. package/src/components/dialog.tsx +0 -164
  37. package/src/components/dna-icon.tsx +0 -77
  38. package/src/components/dropdown-menu.tsx +0 -87
  39. package/src/components/effects.tsx +0 -1021
  40. package/src/components/empty-state.tsx +0 -93
  41. package/src/components/epistemic-branch-panel.tsx +0 -101
  42. package/src/components/error-state.tsx +0 -137
  43. package/src/components/field.tsx +0 -96
  44. package/src/components/filter-facet-bar.tsx +0 -91
  45. package/src/components/inbox-popover.tsx +0 -146
  46. package/src/components/input.tsx +0 -25
  47. package/src/components/kanban-board.tsx +0 -190
  48. package/src/components/label.tsx +0 -19
  49. package/src/components/layered-scene.tsx +0 -555
  50. package/src/components/log-console.tsx +0 -261
  51. package/src/components/metric-tile.tsx +0 -99
  52. package/src/components/mode-picker.tsx +0 -149
  53. package/src/components/motion-timeline.tsx +0 -848
  54. package/src/components/number-input.tsx +0 -104
  55. package/src/components/page-header.tsx +0 -123
  56. package/src/components/pagination.tsx +0 -83
  57. package/src/components/panel.tsx +0 -121
  58. package/src/components/paper-note.tsx +0 -77
  59. package/src/components/popover.tsx +0 -72
  60. package/src/components/progress.tsx +0 -127
  61. package/src/components/quest-card.tsx +0 -319
  62. package/src/components/radio-group.tsx +0 -50
  63. package/src/components/riot-sticker.tsx +0 -151
  64. package/src/components/scene-particles.tsx +0 -196
  65. package/src/components/scene-transitions.tsx +0 -130
  66. package/src/components/section-ribbon.tsx +0 -94
  67. package/src/components/select-field.tsx +0 -27
  68. package/src/components/select.tsx +0 -81
  69. package/src/components/separator.tsx +0 -58
  70. package/src/components/sheet.tsx +0 -96
  71. package/src/components/skeleton.tsx +0 -35
  72. package/src/components/sketch-annotation.tsx +0 -87
  73. package/src/components/smart-button-box.tsx +0 -114
  74. package/src/components/speech-bubble.tsx +0 -116
  75. package/src/components/spinner.tsx +0 -69
  76. package/src/components/sprite-player.tsx +0 -412
  77. package/src/components/status-bar.tsx +0 -63
  78. package/src/components/status-pill.tsx +0 -68
  79. package/src/components/switch.tsx +0 -76
  80. package/src/components/table.tsx +0 -163
  81. package/src/components/tabs.tsx +0 -101
  82. package/src/components/tag-input.tsx +0 -170
  83. package/src/components/textarea.tsx +0 -25
  84. package/src/components/toast.tsx +0 -14
  85. package/src/components/toolbar.tsx +0 -39
  86. package/src/components/tooltip.tsx +0 -49
  87. package/src/components/update-banner.tsx +0 -220
  88. package/src/components/xv-primitives.tsx +0 -256
  89. package/src/css-exports.ts +0 -142
  90. package/src/dna.tsx +0 -137
  91. package/src/game/boss-hud.tsx +0 -384
  92. package/src/game/index.tsx +0 -886
  93. package/src/game/tactical.tsx +0 -245
  94. package/src/game.tsx +0 -770
  95. package/src/icons/activity.tsx +0 -38
  96. package/src/icons/add.tsx +0 -38
  97. package/src/icons/addon.tsx +0 -37
  98. package/src/icons/anime-anger.tsx +0 -37
  99. package/src/icons/anime-shock.tsx +0 -37
  100. package/src/icons/anime-sparkle.tsx +0 -38
  101. package/src/icons/anime-sweatdrop.tsx +0 -38
  102. package/src/icons/archive.tsx +0 -37
  103. package/src/icons/arrow-left.tsx +0 -37
  104. package/src/icons/arrow-right.tsx +0 -37
  105. package/src/icons/attachment.tsx +0 -38
  106. package/src/icons/bank.tsx +0 -37
  107. package/src/icons/bolt.tsx +0 -37
  108. package/src/icons/book-open.tsx +0 -37
  109. package/src/icons/bookmark.tsx +0 -37
  110. package/src/icons/boss-skull.tsx +0 -40
  111. package/src/icons/calendar-clock.tsx +0 -40
  112. package/src/icons/calendar.tsx +0 -38
  113. package/src/icons/check-circle.tsx +0 -38
  114. package/src/icons/check-square.tsx +0 -38
  115. package/src/icons/check.tsx +0 -37
  116. package/src/icons/chevron-right.tsx +0 -37
  117. package/src/icons/chevrons-left.tsx +0 -37
  118. package/src/icons/chevrons-right.tsx +0 -37
  119. package/src/icons/clapperboard.tsx +0 -38
  120. package/src/icons/clock.tsx +0 -38
  121. package/src/icons/close.tsx +0 -37
  122. package/src/icons/code.tsx +0 -37
  123. package/src/icons/columns.tsx +0 -38
  124. package/src/icons/combo-flame.tsx +0 -38
  125. package/src/icons/company.tsx +0 -38
  126. package/src/icons/credit-card.tsx +0 -38
  127. package/src/icons/critical-star.tsx +0 -37
  128. package/src/icons/currency.tsx +0 -38
  129. package/src/icons/delete.tsx +0 -37
  130. package/src/icons/document.tsx +0 -38
  131. package/src/icons/download.tsx +0 -37
  132. package/src/icons/dpad.tsx +0 -38
  133. package/src/icons/edit.tsx +0 -38
  134. package/src/icons/element-fire.tsx +0 -37
  135. package/src/icons/element-ice.tsx +0 -38
  136. package/src/icons/element-solar.tsx +0 -38
  137. package/src/icons/element-void.tsx +0 -38
  138. package/src/icons/element-volt.tsx +0 -38
  139. package/src/icons/element-wind.tsx +0 -37
  140. package/src/icons/error.tsx +0 -38
  141. package/src/icons/external-link.tsx +0 -38
  142. package/src/icons/field.tsx +0 -38
  143. package/src/icons/file-spreadsheet.tsx +0 -38
  144. package/src/icons/filter.tsx +0 -37
  145. package/src/icons/gacha-ticket.tsx +0 -38
  146. package/src/icons/gamepad.tsx +0 -40
  147. package/src/icons/globe.tsx +0 -38
  148. package/src/icons/grid.tsx +0 -40
  149. package/src/icons/hash.tsx +0 -37
  150. package/src/icons/heart-hp.tsx +0 -38
  151. package/src/icons/history.tsx +0 -37
  152. package/src/icons/home.tsx +0 -37
  153. package/src/icons/image.tsx +0 -39
  154. package/src/icons/impact-slash.tsx +0 -38
  155. package/src/icons/inbox.tsx +0 -38
  156. package/src/icons/index.ts +0 -120
  157. package/src/icons/info.tsx +0 -38
  158. package/src/icons/jobs.tsx +0 -38
  159. package/src/icons/katana.tsx +0 -38
  160. package/src/icons/key.tsx +0 -38
  161. package/src/icons/keyboard.tsx +0 -38
  162. package/src/icons/keyframe-diamond.tsx +0 -38
  163. package/src/icons/knowledge.tsx +0 -38
  164. package/src/icons/language.tsx +0 -39
  165. package/src/icons/lock.tsx +0 -38
  166. package/src/icons/logout.tsx +0 -37
  167. package/src/icons/loot-chest.tsx +0 -39
  168. package/src/icons/mail.tsx +0 -38
  169. package/src/icons/mana-crystal.tsx +0 -38
  170. package/src/icons/map-pin.tsx +0 -38
  171. package/src/icons/mecha-helm.tsx +0 -38
  172. package/src/icons/menu.tsx +0 -37
  173. package/src/icons/message.tsx +0 -38
  174. package/src/icons/model.tsx +0 -38
  175. package/src/icons/moon.tsx +0 -37
  176. package/src/icons/notification.tsx +0 -38
  177. package/src/icons/partner.tsx +0 -41
  178. package/src/icons/pause.tsx +0 -37
  179. package/src/icons/phone.tsx +0 -37
  180. package/src/icons/play.tsx +0 -37
  181. package/src/icons/potion-flask.tsx +0 -40
  182. package/src/icons/quest-scroll.tsx +0 -39
  183. package/src/icons/radar-ping.tsx +0 -40
  184. package/src/icons/refresh.tsx +0 -38
  185. package/src/icons/relation.tsx +0 -40
  186. package/src/icons/save.tsx +0 -38
  187. package/src/icons/search.tsx +0 -38
  188. package/src/icons/security.tsx +0 -38
  189. package/src/icons/send.tsx +0 -38
  190. package/src/icons/settings.tsx +0 -39
  191. package/src/icons/shield-crest.tsx +0 -38
  192. package/src/icons/shuriken.tsx +0 -38
  193. package/src/icons/signal-off.tsx +0 -37
  194. package/src/icons/signal.tsx +0 -37
  195. package/src/icons/skill-node.tsx +0 -40
  196. package/src/icons/smartphone.tsx +0 -38
  197. package/src/icons/sort-ascending.tsx +0 -37
  198. package/src/icons/sort-descending.tsx +0 -37
  199. package/src/icons/sort.tsx +0 -37
  200. package/src/icons/sparkle.tsx +0 -38
  201. package/src/icons/speech-burst.tsx +0 -37
  202. package/src/icons/story.tsx +0 -37
  203. package/src/icons/sun.tsx +0 -38
  204. package/src/icons/tag.tsx +0 -38
  205. package/src/icons/team.tsx +0 -40
  206. package/src/icons/trophy-crown.tsx +0 -39
  207. package/src/icons/upload.tsx +0 -37
  208. package/src/icons/user.tsx +0 -38
  209. package/src/icons/users.tsx +0 -39
  210. package/src/icons/visibility-off.tsx +0 -38
  211. package/src/icons/visibility.tsx +0 -38
  212. package/src/icons/volume-off.tsx +0 -37
  213. package/src/icons/volume.tsx +0 -37
  214. package/src/icons/warning.tsx +0 -38
  215. package/src/icons/workflow.tsx +0 -40
  216. package/src/icons.tsx +0 -965
  217. package/src/index.ts +0 -89
  218. package/src/lib/asset-registry.ts +0 -534
  219. package/src/lib/audio-runtime.ts +0 -314
  220. package/src/lib/character-adapter.ts +0 -370
  221. package/src/lib/character-registry.ts +0 -2368
  222. package/src/lib/cn.ts +0 -6
  223. package/src/lib/compose-refs.ts +0 -16
  224. package/src/lib/sanitize-url.ts +0 -51
  225. package/src/lib/scene-camera.ts +0 -483
  226. package/src/lib/scene-playback.ts +0 -622
  227. package/src/lib/scene-soundscape.ts +0 -151
  228. package/src/lib/scene-test-harness.ts +0 -200
  229. package/src/lib/scene-timeline.ts +0 -398
  230. package/src/lib/use-animation-visibility.ts +0 -45
  231. package/src/lib/use-modal-focus.ts +0 -270
  232. package/src/lib/use-reduced-motion.ts +0 -37
  233. package/src/motion/easing-presets.ts +0 -262
  234. package/src/motion/index.tsx +0 -894
  235. package/src/motion/physics.ts +0 -259
  236. package/src/motion/timeline-math.ts +0 -358
  237. package/src/motion.tsx +0 -639
  238. package/src/pixelfx-boundary.ts +0 -188
  239. package/src/pixelfx-registry.ts +0 -1190
  240. package/src/pixelfx-types.d.ts +0 -73
  241. package/src/pixelfx.tsx +0 -1022
  242. package/src/provider.tsx +0 -63
  243. package/src/skin-catalog.ts +0 -306
  244. package/src/skin-tokens.ts +0 -425
  245. package/src/ssr/init-script.ts +0 -281
  246. package/src/ssr/resolve-skin.ts +0 -217
  247. package/src/ssr/theme-mismatch-dev.ts +0 -181
  248. package/src/templates/battle-arena.tsx +0 -688
  249. package/src/templates/index.tsx +0 -314
  250. package/src/templates.tsx +0 -225
  251. package/src/themes/dna-presets.ts +0 -119
  252. package/src/themes/index.ts +0 -56
  253. package/src/themes/provider.tsx +0 -183
  254. package/src/themes/types.ts +0 -111
  255. package/src/themes.ts +0 -9
  256. package/src/token-exporter.ts +0 -12
  257. package/src/token-exporters.ts +0 -343
  258. package/src/token-seeds.ts +0 -335
  259. package/src/token-snapshot.ts +0 -179
  260. package/src/utils.ts +0 -58
@@ -1,314 +0,0 @@
1
- export type SfxName = "success" | "alert" | "signal";
2
-
3
- export const SOUND_PREFERENCE_KEY = "mw-ui-sound-enabled";
4
-
5
- const catalog: Record<SfxName, string> = {
6
- success: "/sounds/ui-success.wav",
7
- alert: "/sounds/ui-alert.wav",
8
- signal: "/sounds/ui-signal.wav",
9
- };
10
-
11
- type SoundStorage = {
12
- getItem: (key: string) => string | null;
13
- setItem: (key: string, value: string) => void;
14
- };
15
-
16
- type AudioPlayback = {
17
- preload: string;
18
- volume: number;
19
- ended: boolean;
20
- onended: ((event: Event) => unknown) | null;
21
- play: () => Promise<unknown>;
22
- };
23
-
24
- let activeAudio: AudioPlayback | null = null;
25
- let globalAudioEnabled = false;
26
- const audioListeners = new Set<(enabled: boolean) => void>();
27
-
28
- export function getGlobalAudioEnabled(): boolean {
29
- return globalAudioEnabled;
30
- }
31
-
32
- export function setGlobalAudioEnabled(enabled: boolean): void {
33
- const next = Boolean(enabled);
34
- if (globalAudioEnabled === next) return;
35
- globalAudioEnabled = next;
36
- for (const listener of audioListeners) {
37
- try {
38
- listener(next);
39
- } catch {
40
- // Ignore subscriber errors so audio bus notifications stay resilient.
41
- }
42
- }
43
- }
44
-
45
- export function subscribeAudioPreference(
46
- listener: (enabled: boolean) => void,
47
- ): () => void {
48
- audioListeners.add(listener);
49
- return () => {
50
- audioListeners.delete(listener);
51
- };
52
- }
53
-
54
- export function readSoundPreference(storage: SoundStorage): boolean {
55
- try {
56
- return storage.getItem(SOUND_PREFERENCE_KEY) === "true";
57
- } catch {
58
- return false;
59
- }
60
- }
61
-
62
- export function persistSoundPreference(
63
- storage: SoundStorage,
64
- enabled: boolean,
65
- ): void {
66
- try {
67
- storage.setItem(SOUND_PREFERENCE_KEY, String(enabled));
68
- } catch {
69
- // Preference storage may be unavailable; the in-memory control still works.
70
- }
71
- }
72
-
73
- export function playCatalogSound(
74
- name: SfxName,
75
- enabled: boolean,
76
- ready: boolean,
77
- createAudio: (source: string) => AudioPlayback,
78
- ): void {
79
- if (!enabled || !ready) return;
80
- if (activeAudio && !activeAudio.ended) return;
81
- try {
82
- const audio = createAudio(catalog[name]);
83
- activeAudio = audio;
84
- audio.onended = () => {
85
- if (activeAudio === audio) activeAudio = null;
86
- };
87
- audio.preload = "none";
88
- audio.volume = 0.32;
89
- void audio.play().catch(() => {
90
- if (activeAudio === audio) activeAudio = null;
91
- });
92
- } catch {
93
- activeAudio = null;
94
- // Missing audio never interrupts the action.
95
- }
96
- }
97
-
98
- export type ProceduralSfxPreset =
99
- | "click-soft"
100
- | "toggle-pop"
101
- | "drawer-whoosh"
102
- | "success-chime"
103
- | "error-bonk"
104
- | "hit-impact"
105
- | "coin-pickup"
106
- | "level-up"
107
- | "dialogue-blip"
108
- | "boing"
109
- | "zip"
110
- | "splat"
111
- | "rimshot"
112
- | "cut-in-slash"
113
- | "boss-break"
114
- | "gacha-fanfare"
115
- | "trophy-unlock";
116
-
117
- export const PROCEDURAL_SFX_PRESETS: readonly ProceduralSfxPreset[] =
118
- Object.freeze([
119
- "click-soft",
120
- "toggle-pop",
121
- "drawer-whoosh",
122
- "success-chime",
123
- "error-bonk",
124
- "hit-impact",
125
- "coin-pickup",
126
- "level-up",
127
- "dialogue-blip",
128
- "boing",
129
- "zip",
130
- "splat",
131
- "rimshot",
132
- "cut-in-slash",
133
- "boss-break",
134
- "gacha-fanfare",
135
- "trophy-unlock",
136
- ]);
137
-
138
- type ToneSpec = {
139
- readonly type: OscillatorType;
140
- readonly startHz: number;
141
- readonly endHz: number;
142
- readonly durationSec: number;
143
- readonly gain: number;
144
- };
145
-
146
- const PROCEDURAL_TONES: Readonly<Record<ProceduralSfxPreset, ToneSpec>> = {
147
- "click-soft": {
148
- type: "triangle",
149
- startHz: 520,
150
- endHz: 320,
151
- durationSec: 0.045,
152
- gain: 0.09,
153
- },
154
- "toggle-pop": {
155
- type: "sine",
156
- startHz: 380,
157
- endHz: 680,
158
- durationSec: 0.075,
159
- gain: 0.11,
160
- },
161
- "drawer-whoosh": {
162
- type: "sine",
163
- startHz: 180,
164
- endHz: 420,
165
- durationSec: 0.12,
166
- gain: 0.08,
167
- },
168
- "success-chime": {
169
- type: "triangle",
170
- startHz: 523,
171
- endHz: 784,
172
- durationSec: 0.18,
173
- gain: 0.12,
174
- },
175
- "error-bonk": {
176
- type: "sawtooth",
177
- startHz: 180,
178
- endHz: 95,
179
- durationSec: 0.16,
180
- gain: 0.1,
181
- },
182
- "hit-impact": {
183
- type: "square",
184
- startHz: 190,
185
- endHz: 55,
186
- durationSec: 0.11,
187
- gain: 0.14,
188
- },
189
- "coin-pickup": {
190
- type: "square",
191
- startHz: 988,
192
- endHz: 1318,
193
- durationSec: 0.14,
194
- gain: 0.1,
195
- },
196
- "level-up": {
197
- type: "triangle",
198
- startHz: 440,
199
- endHz: 880,
200
- durationSec: 0.28,
201
- gain: 0.13,
202
- },
203
- "dialogue-blip": {
204
- type: "square",
205
- startHz: 460,
206
- endHz: 510,
207
- durationSec: 0.032,
208
- gain: 0.05,
209
- },
210
- boing: {
211
- type: "sine",
212
- startHz: 190,
213
- endHz: 560,
214
- durationSec: 0.22,
215
- gain: 0.14,
216
- },
217
- zip: {
218
- type: "sawtooth",
219
- startHz: 320,
220
- endHz: 1400,
221
- durationSec: 0.1,
222
- gain: 0.09,
223
- },
224
- splat: {
225
- type: "sawtooth",
226
- startHz: 260,
227
- endHz: 70,
228
- durationSec: 0.15,
229
- gain: 0.12,
230
- },
231
- rimshot: {
232
- type: "triangle",
233
- startHz: 620,
234
- endHz: 210,
235
- durationSec: 0.16,
236
- gain: 0.12,
237
- },
238
- "cut-in-slash": {
239
- type: "sawtooth",
240
- startHz: 980,
241
- endHz: 140,
242
- durationSec: 0.24,
243
- gain: 0.16,
244
- },
245
- "boss-break": {
246
- type: "square",
247
- startHz: 280,
248
- endHz: 42,
249
- durationSec: 0.34,
250
- gain: 0.17,
251
- },
252
- "gacha-fanfare": {
253
- type: "triangle",
254
- startHz: 587,
255
- endHz: 1175,
256
- durationSec: 0.32,
257
- gain: 0.14,
258
- },
259
- "trophy-unlock": {
260
- type: "triangle",
261
- startHz: 659,
262
- endHz: 1318,
263
- durationSec: 0.26,
264
- gain: 0.13,
265
- },
266
- };
267
-
268
- let sharedAudioCtx: AudioContext | null = null;
269
-
270
- export function playProceduralSfx(
271
- preset: ProceduralSfxPreset,
272
- enabled: boolean,
273
- ready = true,
274
- pitchMultiplier = 1,
275
- ): boolean {
276
- if (!enabled || !ready) return false;
277
- if (typeof window === "undefined") return false;
278
- const AudioCtor =
279
- window.AudioContext ??
280
- (window as unknown as { webkitAudioContext?: typeof AudioContext })
281
- .webkitAudioContext;
282
- if (!AudioCtor) return false;
283
- const spec = PROCEDURAL_TONES[preset];
284
- if (!spec) return false;
285
- const pitch = Number.isFinite(pitchMultiplier)
286
- ? Math.min(2.2, Math.max(0.4, pitchMultiplier))
287
- : 1;
288
- try {
289
- if (!sharedAudioCtx || sharedAudioCtx.state === "closed") {
290
- sharedAudioCtx = new AudioCtor();
291
- }
292
- if (sharedAudioCtx.state === "suspended") {
293
- void sharedAudioCtx.resume().catch(() => {});
294
- }
295
- const now = sharedAudioCtx.currentTime;
296
- const osc = sharedAudioCtx.createOscillator();
297
- const gainNode = sharedAudioCtx.createGain();
298
- osc.type = spec.type;
299
- osc.frequency.setValueAtTime(Math.max(24, spec.startHz * pitch), now);
300
- osc.frequency.exponentialRampToValueAtTime(
301
- Math.max(20, spec.endHz * pitch),
302
- now + spec.durationSec,
303
- );
304
- gainNode.gain.setValueAtTime(spec.gain, now);
305
- gainNode.gain.exponentialRampToValueAtTime(0.0001, now + spec.durationSec);
306
- osc.connect(gainNode);
307
- gainNode.connect(sharedAudioCtx.destination);
308
- osc.start(now);
309
- osc.stop(now + spec.durationSec + 0.01);
310
- return true;
311
- } catch {
312
- return false;
313
- }
314
- }
@@ -1,370 +0,0 @@
1
- export type CharacterClipFormat =
2
- "sprite-sequence" | "rig-optional" | (string & {});
3
-
4
- export type CharacterPlaybackMode = "loop" | "ping-pong" | "one-shot";
5
-
6
- export interface CharacterIdentity {
7
- readonly id: string;
8
- readonly slug: string;
9
- readonly label: string;
10
- readonly depictionPolicy: string;
11
- readonly canonicalSheet: string;
12
- }
13
-
14
- export interface CharacterClipFrame {
15
- readonly asset: string;
16
- readonly durationMs?: number;
17
- readonly width?: number;
18
- readonly height?: number;
19
- }
20
-
21
- export interface CharacterClipDescriptor {
22
- readonly id: string;
23
- readonly format: CharacterClipFormat;
24
- readonly frames: readonly (string | CharacterClipFrame)[];
25
- readonly fps?: number;
26
- readonly durationMs?: number;
27
- readonly mode?: CharacterPlaybackMode;
28
- readonly loop?: boolean;
29
- readonly pingPong?: boolean;
30
- readonly fallbackAsset: string;
31
- readonly staticFrameIndex?: number;
32
- }
33
-
34
- export interface CharacterPlaybackRequest {
35
- readonly characterId: string;
36
- readonly clipId: string;
37
- readonly timeMs: number;
38
- readonly reducedMotion?: boolean;
39
- readonly pinnedFrame?: number;
40
- readonly playbackRate?: number;
41
- }
42
-
43
- export interface CharacterResolveOptions {
44
- readonly reducedMotion?: boolean;
45
- readonly pinnedFrame?: number;
46
- readonly playbackRate?: number;
47
- }
48
-
49
- export interface CharacterResolvedFrame {
50
- readonly frameIndex: number;
51
- readonly asset: string;
52
- readonly isStaticFallback: boolean;
53
- readonly completed: boolean;
54
- readonly adapterKind: string;
55
- }
56
-
57
- export interface CharacterAdapter {
58
- readonly kind: CharacterClipFormat;
59
- supports(format: string): boolean;
60
- resolveFrame(
61
- clip: CharacterClipDescriptor,
62
- timeMs: number,
63
- options?: CharacterResolveOptions,
64
- ): CharacterResolvedFrame;
65
- dispose?(): void;
66
- }
67
-
68
- export interface CharacterAdapterRegistry {
69
- register(adapter: CharacterAdapter): void;
70
- unregister(kind: string): boolean;
71
- resolveAdapter(format: string): CharacterAdapter | null;
72
- resolveFrame(
73
- clip: CharacterClipDescriptor,
74
- timeMs: number,
75
- options?: CharacterResolveOptions,
76
- ): CharacterResolvedFrame;
77
- listAdapters(): readonly string[];
78
- dispose(): void;
79
- }
80
-
81
- const DEFAULT_FPS = 12;
82
-
83
- function extractFrameAsset(frame: string | CharacterClipFrame): string {
84
- return typeof frame === "string" ? frame : frame.asset;
85
- }
86
-
87
- function resolvePlaybackMode(
88
- clip: CharacterClipDescriptor,
89
- ): CharacterPlaybackMode {
90
- if (
91
- clip.mode === "loop" ||
92
- clip.mode === "ping-pong" ||
93
- clip.mode === "one-shot"
94
- ) {
95
- return clip.mode;
96
- }
97
- if (clip.pingPong) return "ping-pong";
98
- if (clip.loop === false) return "one-shot";
99
- return "loop";
100
- }
101
-
102
- function resolveFrameDurations(
103
- clip: CharacterClipDescriptor,
104
- ): readonly number[] {
105
- const count = clip.frames.length;
106
- if (count === 0) return [];
107
-
108
- const fps =
109
- typeof clip.fps === "number" && Number.isFinite(clip.fps) && clip.fps > 0
110
- ? clip.fps
111
- : DEFAULT_FPS;
112
- const defaultFrameDurationMs =
113
- typeof clip.durationMs === "number" &&
114
- Number.isFinite(clip.durationMs) &&
115
- clip.durationMs > 0
116
- ? clip.durationMs / count
117
- : 1000 / fps;
118
-
119
- return clip.frames.map((frame) => {
120
- if (
121
- typeof frame !== "string" &&
122
- typeof frame.durationMs === "number" &&
123
- Number.isFinite(frame.durationMs) &&
124
- frame.durationMs > 0
125
- ) {
126
- return frame.durationMs;
127
- }
128
- return defaultFrameDurationMs;
129
- });
130
- }
131
-
132
- function clampFrameIndex(index: number, count: number): number {
133
- if (count <= 0) return 0;
134
- return Math.min(count - 1, Math.max(0, Math.trunc(index)));
135
- }
136
-
137
- export function createSpriteSequenceAdapter(): CharacterAdapter {
138
- return {
139
- kind: "sprite-sequence",
140
-
141
- supports(format: string): boolean {
142
- return format === "sprite-sequence";
143
- },
144
-
145
- resolveFrame(
146
- clip: CharacterClipDescriptor,
147
- timeMs: number,
148
- options?: CharacterResolveOptions,
149
- ): CharacterResolvedFrame {
150
- const frameCount = clip.frames.length;
151
- const staticIndex = clampFrameIndex(
152
- clip.staticFrameIndex ?? 0,
153
- frameCount,
154
- );
155
-
156
- if (frameCount === 0 || options?.reducedMotion) {
157
- return {
158
- frameIndex: staticIndex,
159
- asset: clip.fallbackAsset,
160
- isStaticFallback: true,
161
- completed: false,
162
- adapterKind: "sprite-sequence",
163
- };
164
- }
165
-
166
- if (
167
- typeof options?.pinnedFrame === "number" &&
168
- Number.isFinite(options.pinnedFrame)
169
- ) {
170
- const pinnedIndex = clampFrameIndex(options.pinnedFrame, frameCount);
171
- return {
172
- frameIndex: pinnedIndex,
173
- asset: extractFrameAsset(clip.frames[pinnedIndex]),
174
- isStaticFallback: false,
175
- completed: false,
176
- adapterKind: "sprite-sequence",
177
- };
178
- }
179
-
180
- const playbackRate =
181
- typeof options?.playbackRate === "number" &&
182
- Number.isFinite(options.playbackRate) &&
183
- options.playbackRate > 0
184
- ? options.playbackRate
185
- : 1;
186
- const effectiveTimeMs =
187
- (Number.isFinite(timeMs) && timeMs > 0 ? timeMs : 0) * playbackRate;
188
- const durations = resolveFrameDurations(clip);
189
- const mode = resolvePlaybackMode(clip);
190
-
191
- if (frameCount === 1) {
192
- const singleDuration = durations[0] ?? 1000 / DEFAULT_FPS;
193
- return {
194
- frameIndex: 0,
195
- asset: extractFrameAsset(clip.frames[0]),
196
- isStaticFallback: false,
197
- completed: mode === "one-shot" && effectiveTimeMs >= singleDuration,
198
- adapterKind: "sprite-sequence",
199
- };
200
- }
201
-
202
- if (mode === "one-shot") {
203
- let elapsed = 0;
204
- for (let index = 0; index < frameCount; index += 1) {
205
- const nextElapsed = elapsed + durations[index];
206
- if (effectiveTimeMs < nextElapsed) {
207
- return {
208
- frameIndex: index,
209
- asset: extractFrameAsset(clip.frames[index]),
210
- isStaticFallback: false,
211
- completed: false,
212
- adapterKind: "sprite-sequence",
213
- };
214
- }
215
- elapsed = nextElapsed;
216
- }
217
- return {
218
- frameIndex: frameCount - 1,
219
- asset: extractFrameAsset(clip.frames[frameCount - 1]),
220
- isStaticFallback: false,
221
- completed: true,
222
- adapterKind: "sprite-sequence",
223
- };
224
- }
225
-
226
- if (mode === "ping-pong") {
227
- const sequenceIndices: number[] = [];
228
- for (let index = 0; index < frameCount; index += 1) {
229
- sequenceIndices.push(index);
230
- }
231
- for (let index = frameCount - 2; index >= 1; index -= 1) {
232
- sequenceIndices.push(index);
233
- }
234
- const cycleDurationMs = sequenceIndices.reduce(
235
- (total, frameIdx) => total + durations[frameIdx],
236
- 0,
237
- );
238
- const cycleTimeMs =
239
- cycleDurationMs > 0 ? effectiveTimeMs % cycleDurationMs : 0;
240
- let elapsed = 0;
241
- for (const frameIdx of sequenceIndices) {
242
- const nextElapsed = elapsed + durations[frameIdx];
243
- if (cycleTimeMs < nextElapsed) {
244
- return {
245
- frameIndex: frameIdx,
246
- asset: extractFrameAsset(clip.frames[frameIdx]),
247
- isStaticFallback: false,
248
- completed: false,
249
- adapterKind: "sprite-sequence",
250
- };
251
- }
252
- elapsed = nextElapsed;
253
- }
254
- const lastIndex = sequenceIndices[sequenceIndices.length - 1] ?? 0;
255
- return {
256
- frameIndex: lastIndex,
257
- asset: extractFrameAsset(clip.frames[lastIndex]),
258
- isStaticFallback: false,
259
- completed: false,
260
- adapterKind: "sprite-sequence",
261
- };
262
- }
263
-
264
- const totalDurationMs = durations.reduce(
265
- (total, value) => total + value,
266
- 0,
267
- );
268
- const wrappedTimeMs =
269
- totalDurationMs > 0 ? effectiveTimeMs % totalDurationMs : 0;
270
- let elapsed = 0;
271
- for (let index = 0; index < frameCount; index += 1) {
272
- const nextElapsed = elapsed + durations[index];
273
- if (wrappedTimeMs < nextElapsed) {
274
- return {
275
- frameIndex: index,
276
- asset: extractFrameAsset(clip.frames[index]),
277
- isStaticFallback: false,
278
- completed: false,
279
- adapterKind: "sprite-sequence",
280
- };
281
- }
282
- elapsed = nextElapsed;
283
- }
284
-
285
- return {
286
- frameIndex: 0,
287
- asset: extractFrameAsset(clip.frames[0]),
288
- isStaticFallback: false,
289
- completed: false,
290
- adapterKind: "sprite-sequence",
291
- };
292
- },
293
-
294
- dispose(): void {},
295
- };
296
- }
297
-
298
- export function createCharacterAdapterRegistry(): CharacterAdapterRegistry {
299
- const adapters = new Map<string, CharacterAdapter>();
300
- const baseline = createSpriteSequenceAdapter();
301
- adapters.set(baseline.kind, baseline);
302
-
303
- return {
304
- register(adapter: CharacterAdapter): void {
305
- if (
306
- !adapter ||
307
- typeof adapter.kind !== "string" ||
308
- adapter.kind.trim().length === 0
309
- ) {
310
- throw new Error("Character adapter requires a non-empty kind.");
311
- }
312
- const previous = adapters.get(adapter.kind);
313
- if (previous && previous !== adapter) {
314
- previous.dispose?.();
315
- }
316
- adapters.delete(adapter.kind);
317
- adapters.set(adapter.kind, adapter);
318
- },
319
-
320
- unregister(kind: string): boolean {
321
- const existing = adapters.get(kind);
322
- if (!existing) return false;
323
- existing.dispose?.();
324
- return adapters.delete(kind);
325
- },
326
-
327
- resolveAdapter(format: string): CharacterAdapter | null {
328
- const registered = Array.from(adapters.values());
329
- for (let index = registered.length - 1; index >= 0; index -= 1) {
330
- const candidate = registered[index];
331
- if (candidate.supports(format)) {
332
- return candidate;
333
- }
334
- }
335
- return null;
336
- },
337
-
338
- resolveFrame(
339
- clip: CharacterClipDescriptor,
340
- timeMs: number,
341
- options?: CharacterResolveOptions,
342
- ): CharacterResolvedFrame {
343
- const adapter = this.resolveAdapter(clip.format);
344
- if (!adapter) {
345
- return {
346
- frameIndex: clampFrameIndex(
347
- clip.staticFrameIndex ?? 0,
348
- clip.frames.length,
349
- ),
350
- asset: clip.fallbackAsset,
351
- isStaticFallback: true,
352
- completed: false,
353
- adapterKind: "static-fallback",
354
- };
355
- }
356
- return adapter.resolveFrame(clip, timeMs, options);
357
- },
358
-
359
- listAdapters(): readonly string[] {
360
- return Array.from(adapters.keys());
361
- },
362
-
363
- dispose(): void {
364
- for (const adapter of adapters.values()) {
365
- adapter.dispose?.();
366
- }
367
- adapters.clear();
368
- },
369
- };
370
- }