From 03ba508a31a02ee9828ea3ffbc4076e0d25cf5c2 Mon Sep 17 00:00:00 2001 From: RT15548 Date: Sun, 18 Jan 2026 20:04:43 +0800 Subject: [PATCH 1/3] =?UTF-8?q?1.18=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 175 +- bridges/call-generate-service.js | 92 +- bridges/worldbook-bridge.js | 43 +- bridges/wrapper-iframe.js | 53 +- core/iframe-messaging.js | 27 + core/server-storage.js | 6 +- core/wrapper-inline.js | 40 +- docs/COPYRIGHT | 146 +- docs/LICENSE.md | 66 +- docs/NOTICE | 190 +- index.js | 42 +- jsconfig.json | 11 + libs/pixi.min.js | 1162 +++++ modules/control-audio.js | 536 +-- modules/debug-panel/debug-panel.html | 8 +- modules/debug-panel/debug-panel.js | 11 +- modules/fourth-wall/fourth-wall.html | 24 +- modules/fourth-wall/fourth-wall.js | 63 +- modules/fourth-wall/fw-message-enhancer.js | 26 +- modules/iframe-renderer.js | 24 +- modules/immersive-mode.js | 12 - modules/message-preview.js | 1319 ++--- modules/novel-draw/cloud-presets.js | 14 + modules/novel-draw/floating-panel.js | 1809 ++++--- modules/novel-draw/gallery-cache.js | 8 +- modules/novel-draw/image-live-effect.js | 331 ++ modules/novel-draw/novel-draw.html | 57 +- modules/novel-draw/novel-draw.js | 319 +- modules/scheduled-tasks/embedded-tasks.html | 150 +- modules/scheduled-tasks/scheduled-tasks.html | 150 +- modules/scheduled-tasks/scheduled-tasks.js | 47 +- modules/story-outline/story-outline-prompt.js | 1183 +++-- modules/story-outline/story-outline.html | 4266 +++++++++-------- modules/story-outline/story-outline.js | 2384 ++++----- modules/story-summary/llm-service.js | 378 ++ modules/story-summary/story-summary.html | 99 +- modules/story-summary/story-summary.js | 363 +- modules/streaming-generation.js | 21 +- modules/template-editor/template-editor.html | 122 +- modules/template-editor/template-editor.js | 12 +- modules/tts/tts-api.js | 335 ++ modules/tts/tts-auth-provider.js | 311 ++ modules/tts/tts-cache.js | 171 + modules/tts/tts-free-provider.js | 390 ++ modules/tts/tts-overlay.html | 2407 ++++++++++ modules/tts/tts-panel.js | 1025 ++++ modules/tts/tts-player.js | 309 ++ modules/tts/tts-text.js | 317 ++ modules/tts/tts-voices.js | 197 + modules/tts/tts.js | 1334 ++++++ modules/tts/声音复刻.png | Bin 0 -> 46722 bytes modules/tts/开通管理.png | Bin 0 -> 61788 bytes modules/tts/获取ID和KEY.png | Bin 0 -> 44321 bytes modules/variables/var-commands.js | 7 +- modules/variables/varevent-editor.js | 55 +- modules/variables/variables-core.js | 12 +- modules/variables/variables-panel.js | 1359 +++--- package-lock.json | 1488 ++++++ package.json | 15 + settings.html | 57 +- widgets/button-collapse.js | 259 + widgets/message-toolbar.js | 265 + 62 files changed, 18838 insertions(+), 7264 deletions(-) create mode 100644 core/iframe-messaging.js create mode 100644 jsconfig.json create mode 100644 libs/pixi.min.js create mode 100644 modules/novel-draw/image-live-effect.js create mode 100644 modules/story-summary/llm-service.js create mode 100644 modules/tts/tts-api.js create mode 100644 modules/tts/tts-auth-provider.js create mode 100644 modules/tts/tts-cache.js create mode 100644 modules/tts/tts-free-provider.js create mode 100644 modules/tts/tts-overlay.html create mode 100644 modules/tts/tts-panel.js create mode 100644 modules/tts/tts-player.js create mode 100644 modules/tts/tts-text.js create mode 100644 modules/tts/tts-voices.js create mode 100644 modules/tts/tts.js create mode 100644 modules/tts/声音复刻.png create mode 100644 modules/tts/开通管理.png create mode 100644 modules/tts/获取ID和KEY.png create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 widgets/button-collapse.js create mode 100644 widgets/message-toolbar.js diff --git a/README.md b/README.md index 023cafa..a3e0008 100644 --- a/README.md +++ b/README.md @@ -1,89 +1,120 @@ -# LittleWhiteBox - -SillyTavern 扩展插件 - 小白X - -## 📁 目录结构 - -``` +# LittleWhiteBox + +## 📁 目录结构 + +``` LittleWhiteBox/ -├── index.js # 主入口,初始化所有模块,管理总开关 -├── manifest.json # 插件清单,版本、依赖声明 -├── settings.html # 主设置页面,所有模块开关UI +├── index.js # 入口:初始化/注册所有模块 +├── manifest.json # 插件清单:版本/依赖/入口 +├── settings.html # 主设置页:模块开关/UI ├── style.css # 全局样式 ├── README.md # 说明文档 +├── .eslintrc.cjs # ESLint 规则 +├── .eslintignore # ESLint 忽略 +├── .gitignore # Git 忽略 +├── package.json # 开发依赖/脚本 +├── package-lock.json # 依赖锁定 +├── jsconfig.json # 编辑器提示 │ -├── core/ # 核心公共模块 -│ ├── constants.js # 共享常量 EXT_ID, extensionFolderPath -│ ├── event-manager.js # 统一事件管理,createModuleEvents() -│ ├── debug-core.js # 日志 xbLog + 缓存注册 CacheRegistry -│ ├── slash-command.js # 斜杠命令执行封装 -│ ├── variable-path.js # 变量路径解析工具 -│ └── server-storage.js # 服务器文件存储,防抖保存,自动重试 +├── core/ # 核心基础设施(不直接做功能UI) +│ ├── constants.js # 常量/路径 +│ ├── event-manager.js # 统一事件管理 +│ ├── debug-core.js # 日志/缓存注册 +│ ├── slash-command.js # 斜杠命令封装 +│ ├── variable-path.js # 变量路径解析 +│ ├── server-storage.js # 服务器存储(防抖/重试) +│ ├── wrapper-inline.js # iframe 内联脚本 +│ └── iframe-messaging.js # postMessage 封装与 origin 校验 │ -├── modules/ # 功能模块 -│ ├── button-collapse.js # 按钮折叠,消息区按钮收纳 -│ ├── control-audio.js # 音频控制,iframe音频权限 -│ ├── iframe-renderer.js # iframe渲染,代码块转交互界面 -│ ├── immersive-mode.js # 沉浸模式,界面布局优化 -│ ├── message-preview.js # 消息预览,Log记录/拦截 -│ ├── script-assistant.js # 脚本助手,AI写卡知识注入 -│ ├── streaming-generation.js # 流式生成,xbgenraw命令 +├── widgets/ # 通用UI组件(跨功能复用) +│ ├── message-toolbar.js # 消息区工具条注册/管理 +│ └── button-collapse.js # 消息区按钮收纳 +│ +├── modules/ # 功能模块(每个功能自带UI) +│ ├── control-audio.js # 音频权限控制 +│ ├── iframe-renderer.js # iframe 渲染 +│ ├── immersive-mode.js # 沉浸模式 +│ ├── message-preview.js # 消息预览/拦截 +│ ├── streaming-generation.js # 生成相关功能(xbgenraw) │ │ -│ ├── debug-panel/ # 调试面板模块 -│ │ ├── debug-panel.js # 悬浮窗控制,父子通信,懒加载 -│ │ └── debug-panel.html # 三Tab界面:日志/事件/缓存 +│ ├── debug-panel/ # 调试面板 +│ │ ├── debug-panel.js # 悬浮窗控制 +│ │ └── debug-panel.html # UI │ │ -│ ├── fourth-wall/ # 四次元壁模块(皮下交流) -│ │ ├── fourth-wall.js # 悬浮按钮,postMessage通讯 -│ │ └── fourth-wall.html # iframe聊天界面,提示词编辑 +│ ├── fourth-wall/ # 四次元壁 +│ │ ├── fourth-wall.js # 逻辑 +│ │ ├── fourth-wall.html # UI +│ │ ├── fw-image.js # 图像交互 +│ │ ├── fw-message-enhancer.js # 消息增强 +│ │ ├── fw-prompt.js # 提示词编辑 +│ │ └── fw-voice.js # 语音展示 │ │ -│ ├── novel-draw/ # Novel画图模块 -│ │ ├── novel-draw.js # NovelAI画图,预设管理,LLM场景分析 -│ │ ├── novel-draw.html # 参数配置,图片管理(画廊+缓存) -│ │ ├── floating-panel.js # 悬浮面板,状态显示,快捷操作 -│ │ └── gallery-cache.js # IndexedDB缓存,小画廊UI +│ ├── novel-draw/ # 画图 +│ │ ├── novel-draw.js # 主逻辑 +│ │ ├── novel-draw.html # UI +│ │ ├── llm-service.js # LLM 分析 +│ │ ├── floating-panel.js # 悬浮面板 +│ │ ├── gallery-cache.js # 缓存 +│ │ ├── image-live-effect.js # Live 动效 +│ │ ├── cloud-presets.js # 云预设 +│ │ └── TAG编写指南.md # 文档 │ │ -│ ├── scheduled-tasks/ # 定时任务模块 -│ │ ├── scheduled-tasks.js # 全局/角色/预设任务调度 -│ │ ├── scheduled-tasks.html # 任务设置面板 -│ │ └── embedded-tasks.html # 嵌入式任务界面 +│ ├── tts/ # TTS +│ │ ├── tts.js # 主逻辑 +│ │ ├── tts-auth-provider.js # 鉴权 +│ │ ├── tts-free-provider.js # 试用 +│ │ ├── tts-api.js # API +│ │ ├── tts-text.js # 文本处理 +│ │ ├── tts-player.js # 播放器 +│ │ ├── tts-panel.js # 气泡UI +│ │ ├── tts-cache.js # 缓存 +│ │ ├── tts-overlay.html # 设置UI +│ │ ├── tts-voices.js # 音色数据 +│ │ ├── 开通管理.png # 说明图 +│ │ ├── 获取ID和KEY.png # 说明图 +│ │ └── 声音复刻.png # 说明图 │ │ -│ ├── template-editor/ # 模板编辑器模块 -│ │ ├── template-editor.js # 沉浸式模板,流式多楼层渲染 -│ │ └── template-editor.html # 模板编辑界面 +│ ├── scheduled-tasks/ # 定时任务 +│ │ ├── scheduled-tasks.js # 调度 +│ │ ├── scheduled-tasks.html # UI +│ │ └── embedded-tasks.html # 嵌入UI │ │ -│ ├── story-outline/ # 故事大纲模块 -│ │ ├── story-outline.js # 可视化剧情地图 -│ │ ├── story-outline.html # 大纲编辑界面 -│ │ └── story-outline-prompt.js # 大纲生成提示词 +│ ├── template-editor/ # 模板编辑器 +│ │ ├── template-editor.js # 逻辑 +│ │ └── template-editor.html # UI │ │ -│ ├── story-summary/ # 剧情总结模块 -│ │ ├── story-summary.js # 增量总结,时间线,关系图 -│ │ └── story-summary.html # 总结面板界面 +│ ├── story-outline/ # 故事大纲 +│ │ ├── story-outline.js # 逻辑 +│ │ ├── story-outline.html # UI +│ │ └── story-outline-prompt.js # 提示词 │ │ -│ └── variables/ # 变量系统模块 -│ ├── var-commands.js # /xbgetvar /xbsetvar 命令,宏替换 -│ ├── varevent-editor.js # 条件规则编辑器,varevent运行时 -│ ├── variables-core.js # plot-log解析,快照回滚,变量守护 -│ └── variables-panel.js # 变量面板UI +│ ├── story-summary/ # 剧情总结 +│ │ ├── story-summary.js # 逻辑 +│ │ ├── story-summary.html # UI +│ │ └── llm-service.js # LLM 服务 +│ │ +│ └── variables/ # 变量系统 +│ ├── var-commands.js # 命令 +│ ├── varevent-editor.js # 编辑器 +│ ├── variables-core.js # 核心 +│ └── variables-panel.js # 面板 │ ├── bridges/ # 外部服务桥接 -│ ├── call-generate-service.js # 父窗口:调用ST生成服务 -│ ├── worldbook-bridge.js # 父窗口:世界书读写桥接 -│ └── wrapper-iframe.js # iframe内部:提供CallGenerate API +│ ├── call-generate-service.js # ST 生成服务 +│ ├── worldbook-bridge.js # 世界书桥接 +│ └── wrapper-iframe.js # iframe 客户端脚本 │ -└── docs/ # 文档与许可 - ├── script-docs.md # 脚本文档 - ├── COPYRIGHT # 版权声明 - ├── LICENSE.md # 许可证 - └── NOTICE # 通知 +├── libs/ # 第三方库 +│ └── pixi.min.js # PixiJS +│ +└── docs/ # 许可/声明 + ├── COPYRIGHT + ├── LICENSE.md + └── NOTICE -``` - -## 🔄 版本历史 - -- v2.2.2 - 目录结构重构(2025-12-08) - -## 📄 许可证 - -详见 `docs/LICENSE.md` +node_modules/ # 本地依赖(不提交) +``` + +## 📄 许可证 + +详见 `docs/LICENSE.md` \ No newline at end of file diff --git a/bridges/call-generate-service.js b/bridges/call-generate-service.js index 053bc32..a4d0b29 100644 --- a/bridges/call-generate-service.js +++ b/bridges/call-generate-service.js @@ -7,13 +7,17 @@ import { xbLog } from "../core/debug-core.js"; const SOURCE_TAG = 'xiaobaix-host'; -const POSITIONS = Object.freeze({ BEFORE_PROMPT: 'BEFORE_PROMPT', IN_PROMPT: 'IN_PROMPT', IN_CHAT: 'IN_CHAT', AFTER_COMPONENT: 'AFTER_COMPONENT' }); -const KNOWN_KEYS = Object.freeze(new Set([ - 'main', 'chatHistory', 'worldInfo', 'worldInfoBefore', 'worldInfoAfter', - 'charDescription', 'charPersonality', 'scenario', 'personaDescription', - 'dialogueExamples', 'authorsNote', 'vectorsMemory', 'vectorsDataBank', - 'smartContext', 'jailbreak', 'nsfw', 'summary', 'bias', 'impersonate', 'quietPrompt', -])); +const POSITIONS = Object.freeze({ BEFORE_PROMPT: 'BEFORE_PROMPT', IN_PROMPT: 'IN_PROMPT', IN_CHAT: 'IN_CHAT', AFTER_COMPONENT: 'AFTER_COMPONENT' }); +const KNOWN_KEYS = Object.freeze(new Set([ + 'main', 'chatHistory', 'worldInfo', 'worldInfoBefore', 'worldInfoAfter', + 'charDescription', 'charPersonality', 'scenario', 'personaDescription', + 'dialogueExamples', 'authorsNote', 'vectorsMemory', 'vectorsDataBank', + 'smartContext', 'jailbreak', 'nsfw', 'summary', 'bias', 'impersonate', 'quietPrompt', +])); +const resolveTargetOrigin = (origin) => { + if (typeof origin === 'string' && origin) return origin; + try { return window.location.origin; } catch { return '*'; } +}; // @ts-nocheck class CallGenerateService { @@ -44,11 +48,11 @@ class CallGenerateService { } } - sendError(sourceWindow, requestId, streamingEnabled, err, fallbackCode = 'API_ERROR', details = null) { - const e = this.normalizeError(err, fallbackCode, details); - const type = streamingEnabled ? 'generateStreamError' : 'generateError'; - try { sourceWindow?.postMessage({ source: SOURCE_TAG, type, id: requestId, error: e }, '*'); } catch {} - } + sendError(sourceWindow, requestId, streamingEnabled, err, fallbackCode = 'API_ERROR', details = null, targetOrigin = null) { + const e = this.normalizeError(err, fallbackCode, details); + const type = streamingEnabled ? 'generateStreamError' : 'generateError'; + try { sourceWindow?.postMessage({ source: SOURCE_TAG, type, id: requestId, error: e }, resolveTargetOrigin(targetOrigin)); } catch {} + } /** * @param {string|undefined} rawId @@ -253,11 +257,11 @@ class CallGenerateService { * @param {string} type * @param {object} body */ - postToTarget(target, type, body) { - try { - target?.postMessage({ source: SOURCE_TAG, type, ...body }, '*'); - } catch (e) {} - } + postToTarget(target, type, body, targetOrigin = null) { + try { + target?.postMessage({ source: SOURCE_TAG, type, ...body }, resolveTargetOrigin(targetOrigin)); + } catch (e) {} + } // ===== ST Prompt 干跑捕获与组件切换 ===== @@ -759,7 +763,6 @@ class CallGenerateService { async _annotateIdentifiersIfMissing(messages, targetKeys) { const arr = Array.isArray(messages) ? messages.map(m => ({ ...m })) : []; if (!arr.length) return arr; - const hasIdentifier = arr.some(m => typeof m?.identifier === 'string' && m.identifier); // 标注 chatHistory:依据 role + 来源判断 const isFromChat = this._createIsFromChat(); for (const m of arr) { @@ -1005,7 +1008,7 @@ class CallGenerateService { _applyContentFilter(list, filterCfg) { if (!filterCfg) return list; - const { contains, regex, fromUserNames, beforeTs, afterTs } = filterCfg; + const { contains, regex, fromUserNames } = filterCfg; let out = list.slice(); if (contains) { const needles = Array.isArray(contains) ? contains : [contains]; @@ -1044,7 +1047,6 @@ class CallGenerateService { } _applyIndicesRange(list, selector) { - const idxBase = selector?.indexBase === 'all' ? 'all' : 'history'; let result = list.slice(); // indices 优先 if (Array.isArray(selector?.indices?.values) && selector.indices.values.length) { @@ -1130,7 +1132,7 @@ class CallGenerateService { // ===== 发送实现(构建后的统一发送) ===== - async _sendMessages(messages, options, requestId, sourceWindow) { + async _sendMessages(messages, options, requestId, sourceWindow, targetOrigin = null) { const sessionId = this.normalizeSessionId(options?.session?.id || 'xb1'); const session = this.ensureSession(sessionId); const streamingEnabled = options?.streaming?.enabled !== false; // 默认开 @@ -1141,11 +1143,11 @@ class CallGenerateService { const shouldExport = !!(options?.debug?.enabled || options?.debug?.exportPrompt); const already = options?.debug?._exported === true; if (shouldExport && !already) { - this.postToTarget(sourceWindow, 'generatePromptPreview', { id: requestId, messages: (messages || []).map(m => ({ role: m.role, content: m.content })) }); + this.postToTarget(sourceWindow, 'generatePromptPreview', { id: requestId, messages: (messages || []).map(m => ({ role: m.role, content: m.content })) }, targetOrigin); } if (streamingEnabled) { - this.postToTarget(sourceWindow, 'generateStreamStart', { id: requestId, sessionId }); + this.postToTarget(sourceWindow, 'generateStreamStart', { id: requestId, sessionId }, targetOrigin); const streamFn = await ChatCompletionService.sendRequest(payload, false, session.abortController.signal); let last = ''; const generator = typeof streamFn === 'function' ? streamFn() : null; @@ -1153,7 +1155,7 @@ class CallGenerateService { const chunk = text.slice(last.length); last = text; session.accumulated = text; - this.postToTarget(sourceWindow, 'generateStreamChunk', { id: requestId, chunk, accumulated: text, metadata: {} }); + this.postToTarget(sourceWindow, 'generateStreamChunk', { id: requestId, chunk, accumulated: text, metadata: {} }, targetOrigin); } const result = { success: true, @@ -1161,7 +1163,7 @@ class CallGenerateService { sessionId, metadata: { duration: Date.now() - session.startedAt, model: apiCfg.model, finishReason: 'stop' }, }; - this.postToTarget(sourceWindow, 'generateStreamComplete', { id: requestId, result }); + this.postToTarget(sourceWindow, 'generateStreamComplete', { id: requestId, result }, targetOrigin); return result; } else { const extracted = await ChatCompletionService.sendRequest(payload, true, session.abortController.signal); @@ -1171,17 +1173,17 @@ class CallGenerateService { sessionId, metadata: { duration: Date.now() - session.startedAt, model: apiCfg.model, finishReason: 'stop' }, }; - this.postToTarget(sourceWindow, 'generateResult', { id: requestId, result }); + this.postToTarget(sourceWindow, 'generateResult', { id: requestId, result }, targetOrigin); return result; } } catch (err) { - this.sendError(sourceWindow, requestId, streamingEnabled, err); - return null; - } - } + this.sendError(sourceWindow, requestId, streamingEnabled, err, 'API_ERROR', null, targetOrigin); + return null; + } + } // ===== 主流程 ===== - async handleRequestInternal(options, requestId, sourceWindow) { + async handleRequestInternal(options, requestId, sourceWindow, targetOrigin = null) { // 1) 校验 this.validateOptions(options); @@ -1275,10 +1277,10 @@ class CallGenerateService { working = this._appendUserInput(working, options?.userInput); // 8) 调试导出 - this._exportDebugData({ sourceWindow, requestId, working, baseStrategy, orderedRefs, inlineMapped, listLevelOverrides, debug: options?.debug }); + this._exportDebugData({ sourceWindow, requestId, working, baseStrategy, orderedRefs, inlineMapped, listLevelOverrides, debug: options?.debug, targetOrigin }); // 9) 发送 - return await this._sendMessages(working, { ...options, debug: { ...(options?.debug || {}), _exported: true } }, requestId, sourceWindow); + return await this._sendMessages(working, { ...options, debug: { ...(options?.debug || {}), _exported: true } }, requestId, sourceWindow, targetOrigin); } _applyOrderingStrategy(messages, baseStrategy, orderedRefs, unorderedKeys) { @@ -1338,9 +1340,9 @@ class CallGenerateService { return out; } - _exportDebugData({ sourceWindow, requestId, working, baseStrategy, orderedRefs, inlineMapped, listLevelOverrides, debug }) { + _exportDebugData({ sourceWindow, requestId, working, baseStrategy, orderedRefs, inlineMapped, listLevelOverrides, debug, targetOrigin }) { const exportPrompt = !!(debug?.enabled || debug?.exportPrompt); - if (exportPrompt) this.postToTarget(sourceWindow, 'generatePromptPreview', { id: requestId, messages: working.map(m => ({ role: m.role, content: m.content })) }); + if (exportPrompt) this.postToTarget(sourceWindow, 'generatePromptPreview', { id: requestId, messages: working.map(m => ({ role: m.role, content: m.content })) }, targetOrigin); if (debug?.exportBlueprint) { try { const bp = { @@ -1349,7 +1351,7 @@ class CallGenerateService { injections: (debug?.injections || []).concat(inlineMapped || []), overrides: listLevelOverrides || null, }; - this.postToTarget(sourceWindow, 'blueprint', bp); + this.postToTarget(sourceWindow, 'blueprint', bp, targetOrigin); } catch {} } } @@ -1357,7 +1359,7 @@ class CallGenerateService { /** * 入口:处理 generateRequest(统一入口) */ - async handleGenerateRequest(options, requestId, sourceWindow) { + async handleGenerateRequest(options, requestId, sourceWindow, targetOrigin = null) { let streamingEnabled = false; try { streamingEnabled = options?.streaming?.enabled !== false; @@ -1369,10 +1371,10 @@ class CallGenerateService { xbLog.info('callGenerateBridge', `generateRequest id=${requestId} stream=${!!streamingEnabled} comps=${compsCount} userInputLen=${userInputLen}`); } } catch {} - return await this.handleRequestInternal(options, requestId, sourceWindow); + return await this.handleRequestInternal(options, requestId, sourceWindow, targetOrigin); } catch (err) { try { xbLog.error('callGenerateBridge', `generateRequest failed id=${requestId}`, err); } catch {} - this.sendError(sourceWindow, requestId, streamingEnabled, err, 'BAD_REQUEST'); + this.sendError(sourceWindow, requestId, streamingEnabled, err, 'BAD_REQUEST', null, targetOrigin); return null; } } @@ -1392,9 +1394,9 @@ class CallGenerateService { const callGenerateService = new CallGenerateService(); -export async function handleGenerateRequest(options, requestId, sourceWindow) { - return await callGenerateService.handleGenerateRequest(options, requestId, sourceWindow); -} +export async function handleGenerateRequest(options, requestId, sourceWindow, targetOrigin = null) { + return await callGenerateService.handleGenerateRequest(options, requestId, sourceWindow, targetOrigin); +} // Host bridge for handling iframe generateRequest → respond via postMessage let __xb_generate_listener_attached = false; @@ -1410,11 +1412,12 @@ export function initCallGenerateHostBridge() { if (!data || data.type !== 'generateRequest') return; const id = data.id; const options = data.options || {}; - await handleGenerateRequest(options, id, event.source || window); + await handleGenerateRequest(options, id, event.source || window, event.origin); } catch (e) { try { xbLog.error('callGenerateBridge', 'generateRequest listener error', e); } catch {} } }; + // eslint-disable-next-line no-restricted-syntax -- bridge listener; origin can be null for sandboxed iframes. try { window.addEventListener('message', __xb_generate_listener); } catch (e) {} __xb_generate_listener_attached = true; } @@ -1511,7 +1514,8 @@ if (typeof window !== 'undefined') { } }; - window.addEventListener('message', listener); + // eslint-disable-next-line no-restricted-syntax -- local listener for internal request flow. + window.addEventListener('message', listener); // 发送请求 handleGenerateRequest(options, requestId, window).catch(err => { diff --git a/bridges/worldbook-bridge.js b/bridges/worldbook-bridge.js index 866b732..87078cc 100644 --- a/bridges/worldbook-bridge.js +++ b/bridges/worldbook-bridge.js @@ -22,7 +22,11 @@ import { } from "../../../../world-info.js"; import { getCharaFilename, findChar } from "../../../../utils.js"; -const SOURCE_TAG = "xiaobaix-host"; +const SOURCE_TAG = "xiaobaix-host"; +const resolveTargetOrigin = (origin) => { + if (typeof origin === 'string' && origin) return origin; + try { return window.location.origin; } catch { return '*'; } +}; function isString(value) { return typeof value === 'string'; @@ -91,18 +95,18 @@ class WorldbookBridgeService { } } - sendResult(target, requestId, result) { - try { target?.postMessage({ source: SOURCE_TAG, type: 'worldbookResult', id: requestId, result }, '*'); } catch {} - } - - sendError(target, requestId, err, fallbackCode = 'API_ERROR', details = null) { - const e = this.normalizeError(err, fallbackCode, details); - try { target?.postMessage({ source: SOURCE_TAG, type: 'worldbookError', id: requestId, error: e }, '*'); } catch {} - } - - postEvent(event, payload) { - try { window?.postMessage({ source: SOURCE_TAG, type: 'worldbookEvent', event, payload }, '*'); } catch {} - } + sendResult(target, requestId, result, targetOrigin = null) { + try { target?.postMessage({ source: SOURCE_TAG, type: 'worldbookResult', id: requestId, result }, resolveTargetOrigin(targetOrigin)); } catch {} + } + + sendError(target, requestId, err, fallbackCode = 'API_ERROR', details = null, targetOrigin = null) { + const e = this.normalizeError(err, fallbackCode, details); + try { target?.postMessage({ source: SOURCE_TAG, type: 'worldbookError', id: requestId, error: e }, resolveTargetOrigin(targetOrigin)); } catch {} + } + + postEvent(event, payload) { + try { window?.postMessage({ source: SOURCE_TAG, type: 'worldbookEvent', event, payload }, resolveTargetOrigin()); } catch {} + } async ensureWorldExists(name, autoCreate) { if (!isString(name) || !name.trim()) throw new Error('MISSING_PARAMS'); @@ -217,8 +221,8 @@ class WorldbookBridgeService { if (!entry) return ''; if (newWorldInfoEntryTemplate[field] === undefined) return ''; - const ctx = getContext(); - const tags = ctx.tags || []; + const ctx = getContext(); + const tags = ctx.tags || []; let fieldValue; switch (field) { @@ -381,9 +385,7 @@ class WorldbookBridgeService { const entry = data.entries[uid]; if (!entry) throw new Error('NOT_FOUND'); - const ctx = getContext(); - const tags = ctx.tags || []; - const result = {}; + const result = {}; // Get all template fields for (const field of Object.keys(newWorldInfoEntryTemplate)) { @@ -837,13 +839,14 @@ class WorldbookBridgeService { } } catch {} const result = await self.handleRequest(action, params); - self.sendResult(event.source || window, id, result); + self.sendResult(event.source || window, id, result, event.origin); } catch (err) { try { xbLog.error('worldbookBridge', `worldbookRequest failed id=${id} action=${String(action || '')}`, err); } catch {} - self.sendError(event.source || window, id, err); + self.sendError(event.source || window, id, err, 'API_ERROR', null, event.origin); } } catch {} }; + // eslint-disable-next-line no-restricted-syntax -- validated by isOriginAllowed before handling. try { window.addEventListener('message', this._listener); } catch {} this._attached = true; if (forwardEvents) this.attachEventsForwarding(); diff --git a/bridges/wrapper-iframe.js b/bridges/wrapper-iframe.js index 5d59b33..00ce0cf 100644 --- a/bridges/wrapper-iframe.js +++ b/bridges/wrapper-iframe.js @@ -1,5 +1,7 @@ -(function(){ - function defineCallGenerate(){ +(function(){ + function defineCallGenerate(){ + var parentOrigin; + try{parentOrigin=new URL(document.referrer).origin}catch(_){parentOrigin='*'} function sanitizeOptions(options){ try{ return JSON.parse(JSON.stringify(options,function(k,v){return(typeof v==='function')?undefined:v})) @@ -29,12 +31,13 @@ function CallGenerateImpl(options){ return new Promise(function(resolve,reject){ try{ - function post(m){try{parent.postMessage(m,'*')}catch(e){}} + function post(m){try{parent.postMessage(m,parentOrigin)}catch(e){}} if(!options||typeof options!=='object'){reject(new Error('Invalid options'));return} var id=Date.now().toString(36)+Math.random().toString(36).slice(2); - function onMessage(e){ - var d=e&&e.data||{}; - if(d.source!=='xiaobaix-host'||d.id!==id)return; + function onMessage(e){ + if(parentOrigin!=='*'&&e&&e.origin!==parentOrigin)return; + var d=e&&e.data||{}; + if(d.source!=='xiaobaix-host'||d.id!==id)return; if(d.type==='generateStreamStart'&&options.streaming&&options.streaming.onStart){try{options.streaming.onStart(d.sessionId)}catch(_){}} else if(d.type==='generateStreamChunk'&&options.streaming&&options.streaming.onChunk){try{options.streaming.onChunk(d.chunk,d.accumulated)}catch(_){}} else if(d.type==='generateStreamComplete'){try{window.removeEventListener('message',onMessage)}catch(_){} @@ -46,10 +49,14 @@ else if(d.type==='generateError'){try{window.removeEventListener('message',onMessage)}catch(_){} reject(new Error(d.error||'Generation failed'))} } - try{window.addEventListener('message',onMessage)}catch(_){} + // eslint-disable-next-line no-restricted-syntax -- origin checked via parentOrigin. + try{window.addEventListener('message',onMessage)}catch(_){} var sanitized=sanitizeOptions(options); post({type:'generateRequest',id:id,options:sanitized}); - setTimeout(function(){try{window.removeEventListener('message',onMessage)}catch(e){};reject(new Error('Generation timeout'))},300000); + setTimeout(function(){ + try{window.removeEventListener('message',onMessage)}catch(e){} + reject(new Error('Generation timeout')); + },300000); }catch(e){reject(e)} }) } @@ -57,10 +64,12 @@ try{window.callGenerate=CallGenerateImpl}catch(e){} try{window.__xb_callGenerate_loaded=true}catch(e){} } - try{defineCallGenerate()}catch(e){} -})(); - -(function(){ + try{defineCallGenerate()}catch(e){} +})(); + +(function(){ + var parentOrigin; + try{parentOrigin=new URL(document.referrer).origin}catch(_){parentOrigin='*'} function applyAvatarCss(urls){ try{ const root=document.documentElement; @@ -83,18 +92,20 @@ } }catch(_){} } - function requestAvatars(){ - try{parent.postMessage({type:'getAvatars'},'*')}catch(_){} - } - function onMessage(e){ - const d=e&&e.data||{}; - if(d&&d.source==='xiaobaix-host'&&d.type==='avatars'){ + function requestAvatars(){ + try{parent.postMessage({type:'getAvatars'},parentOrigin)}catch(_){} + } + function onMessage(e){ + if(parentOrigin!=='*'&&e&&e.origin!==parentOrigin)return; + const d=e&&e.data||{}; + if(d&&d.source==='xiaobaix-host'&&d.type==='avatars'){ applyAvatarCss(d.urls); try{window.removeEventListener('message',onMessage)}catch(_){} } } - try{ - window.addEventListener('message',onMessage); + try{ + // eslint-disable-next-line no-restricted-syntax -- origin checked via parentOrigin. + window.addEventListener('message',onMessage); if(document.readyState==='loading'){ document.addEventListener('DOMContentLoaded',requestAvatars,{once:true}); }else{ @@ -102,4 +113,4 @@ } window.addEventListener('load',requestAvatars,{once:true}); }catch(_){} -})(); \ No newline at end of file +})(); diff --git a/core/iframe-messaging.js b/core/iframe-messaging.js new file mode 100644 index 0000000..8f3fdcb --- /dev/null +++ b/core/iframe-messaging.js @@ -0,0 +1,27 @@ +export function getTrustedOrigin() { + return window.location.origin; +} + +export function getIframeTargetOrigin(iframe) { + const sandbox = iframe?.getAttribute?.('sandbox') || ''; + if (sandbox && !sandbox.includes('allow-same-origin')) return 'null'; + return getTrustedOrigin(); +} + +export function postToIframe(iframe, payload, source, targetOrigin = null) { + if (!iframe?.contentWindow) return false; + const message = source ? { source, ...payload } : payload; + const origin = targetOrigin || getTrustedOrigin(); + iframe.contentWindow.postMessage(message, origin); + return true; +} + +export function isTrustedIframeEvent(event, iframe) { + return !!iframe && event.origin === getTrustedOrigin() && event.source === iframe.contentWindow; +} + +export function isTrustedMessage(event, iframe, expectedSource) { + if (!isTrustedIframeEvent(event, iframe)) return false; + if (expectedSource && event?.data?.source !== expectedSource) return false; + return true; +} diff --git a/core/server-storage.js b/core/server-storage.js index d05fc50..2459d41 100644 --- a/core/server-storage.js +++ b/core/server-storage.js @@ -81,7 +81,7 @@ class StorageFile { // 🔧 核心修复:非静默模式等待当前保存完成 if (this._saving) { this._pendingSave = true; - + if (!silent) { await this._waitForSaveComplete(); if (this._dirtyVersion > this._savedVersion) { @@ -89,7 +89,7 @@ class StorageFile { } return this._dirtyVersion === this._savedVersion; } - + return true; } @@ -181,3 +181,5 @@ class StorageFile { export const TasksStorage = new StorageFile('LittleWhiteBox_Tasks.json'); export const StoryOutlineStorage = new StorageFile('LittleWhiteBox_StoryOutline.json'); export const NovelDrawStorage = new StorageFile('LittleWhiteBox_NovelDraw.json', { debounceMs: 800 }); +export const TtsStorage = new StorageFile('LittleWhiteBox_TTS.json', { debounceMs: 800 }); +export const CommonSettingStorage = new StorageFile('LittleWhiteBox_CommonSettings.json', { debounceMs: 1000 }); diff --git a/core/wrapper-inline.js b/core/wrapper-inline.js index 16f634c..4c98b2a 100644 --- a/core/wrapper-inline.js +++ b/core/wrapper-inline.js @@ -8,6 +8,31 @@ export function getIframeBaseScript() { return ` (function(){ + // vh 修复:CSS注入(立即生效) + 延迟样式表遍历(不阻塞渲染) + (function(){ + var s=document.createElement('style'); + s.textContent='html,body{height:auto!important;min-height:0!important;max-height:none!important}'; + (document.head||document.documentElement).appendChild(s); + // 延迟遍历样式表,不阻塞初次渲染 + (window.requestIdleCallback||function(cb){setTimeout(cb,50)})(function(){ + try{ + for(var i=0,sheets=document.styleSheets;i-1)st.height='auto'; + if((st.minHeight||'').indexOf('vh')>-1)st.minHeight='0'; + if((st.maxHeight||'').indexOf('vh')>-1)st.maxHeight='none'; + } + }catch(e){} + } + }catch(e){} + }); + })(); + function measureVisibleHeight(){ try{ var doc=document,target=doc.body; @@ -40,7 +65,8 @@ export function getIframeBaseScript() { } } - function post(m){try{parent.postMessage(m,'*')}catch(e){}} + var parentOrigin;try{parentOrigin=new URL(document.referrer).origin}catch(_){parentOrigin='*'} + function post(m){try{parent.postMessage(m,parentOrigin)}catch(e){}} var rafPending=false,lastH=0,HYSTERESIS=2; function send(force){ @@ -88,6 +114,7 @@ export function getIframeBaseScript() { } window.addEventListener('message',function(e){ + if(parentOrigin!=='*'&&e&&e.origin!==parentOrigin)return; var d=e&&e.data||{}; if(d&&d.type==='probe')setTimeout(function(){send(true)},10); }); @@ -99,6 +126,7 @@ export function getIframeBaseScript() { if(command[0]!=='/')command='/'+command; var id=Date.now().toString(36)+Math.random().toString(36).slice(2); function onMessage(e){ + if(parentOrigin!=='*'&&e&&e.origin!==parentOrigin)return; var d=e&&e.data||{}; if(d.source!=='xiaobaix-host')return; if((d.type==='commandResult'||d.type==='commandError')&&d.id===id){ @@ -156,10 +184,12 @@ export function getWrapperScript() { function CallGenerateImpl(options){ return new Promise(function(resolve,reject){ try{ - function post(m){try{parent.postMessage(m,'*')}catch(e){}} + var parentOrigin;try{parentOrigin=new URL(document.referrer).origin}catch(_){parentOrigin='*'} + function post(m){try{parent.postMessage(m,parentOrigin)}catch(e){}} if(!options||typeof options!=='object'){reject(new Error('Invalid options'));return} var id=Date.now().toString(36)+Math.random().toString(36).slice(2); function onMessage(e){ + if(parentOrigin!=='*'&&e&&e.origin!==parentOrigin)return; var d=e&&e.data||{}; if(d.source!=='xiaobaix-host'||d.id!==id)return; if(d.type==='generateStreamStart'&&options.streaming&&options.streaming.onStart){try{options.streaming.onStart(d.sessionId)}catch(_){}} @@ -196,8 +226,10 @@ export function getWrapperScript() { } }catch(_){} } - function requestAvatars(){try{parent.postMessage({type:'getAvatars'},'*')}catch(_){}} + var parentOrigin;try{parentOrigin=new URL(document.referrer).origin}catch(_){parentOrigin='*'} + function requestAvatars(){try{parent.postMessage({type:'getAvatars'},parentOrigin)}catch(_){}} function onMessage(e){ + if(parentOrigin!=='*'&&e&&e.origin!==parentOrigin)return; var d=e&&e.data||{}; if(d&&d.source==='xiaobaix-host'&&d.type==='avatars'){ applyAvatarCss(d.urls); @@ -237,4 +269,4 @@ export function getTemplateExtrasScript() { }; } })();`; -} \ No newline at end of file +} diff --git a/docs/COPYRIGHT b/docs/COPYRIGHT index 8e69415..20be483 100644 --- a/docs/COPYRIGHT +++ b/docs/COPYRIGHT @@ -1,73 +1,73 @@ -LittleWhiteBox (小白X) - Copyright and Attribution Requirements -================================================================ - -Copyright 2025 biex - -This software is licensed under the Apache License 2.0 -with additional custom attribution requirements. - -MANDATORY ATTRIBUTION REQUIREMENTS -================================== - -1. AUTHOR ATTRIBUTION - - The original author "biex" MUST be prominently credited in any derivative work - - This credit must appear in: - * Software user interface (visible to end users) - * Documentation and README files - * Source code headers - * About/Credits sections - * Any promotional or marketing materials - -2. PROJECT ATTRIBUTION - - The project name "LittleWhiteBox" and "小白X" must be credited - - Required attribution format: "Based on LittleWhiteBox by biex" - - Project URL must be included: https://github.com/RT15548/LittleWhiteBox - -3. SOURCE CODE DISCLOSURE - - Any modification, enhancement, or derivative work MUST be open source - - Source code must be publicly accessible under the same license terms - - All changes must be clearly documented and attributed - -4. COMMERCIAL USE - - Commercial use is permitted under the Apache License 2.0 terms - - Attribution requirements still apply for commercial use - - No additional permission required for commercial use - -5. TRADEMARK PROTECTION - - "LittleWhiteBox" and "小白X" are trademarks of the original author - - Derivative works may not use these names without explicit permission - - Alternative naming must clearly indicate the derivative nature - -VIOLATION CONSEQUENCES -===================== - -Any violation of these attribution requirements will result in: -- Immediate termination of the license grant -- Legal action for copyright infringement -- Demand for removal of infringing content - -COMPLIANCE EXAMPLES -================== - -✅ CORRECT Attribution Examples: -- "Powered by LittleWhiteBox by biex" -- "Based on LittleWhiteBox (https://github.com/RT15548/LittleWhiteBox) by biex" -- "Enhanced version of LittleWhiteBox by biex - Original: [repository URL]" - -❌ INCORRECT Examples: -- Using the code without any attribution -- Claiming original authorship -- Using "LittleWhiteBox" name for derivative works -- Commercial use without permission -- Closed-source modifications - -CONTACT INFORMATION -================== - -For licensing inquiries or attribution questions: -- Repository: https://github.com/RT15548/LittleWhiteBox -- Author: biex -- License: Apache-2.0 WITH Custom-Attribution-Requirements - -This copyright notice and attribution requirements must be included in all -copies or substantial portions of the software. +LittleWhiteBox (小白X) - Copyright and Attribution Requirements +================================================================ + +Copyright 2025 biex + +This software is licensed under the Apache License 2.0 +with additional custom attribution requirements. + +MANDATORY ATTRIBUTION REQUIREMENTS +================================== + +1. AUTHOR ATTRIBUTION + - The original author "biex" MUST be prominently credited in any derivative work + - This credit must appear in: + * Software user interface (visible to end users) + * Documentation and README files + * Source code headers + * About/Credits sections + * Any promotional or marketing materials + +2. PROJECT ATTRIBUTION + - The project name "LittleWhiteBox" and "小白X" must be credited + - Required attribution format: "Based on LittleWhiteBox by biex" + - Project URL must be included: https://github.com/RT15548/LittleWhiteBox + +3. SOURCE CODE DISCLOSURE + - Any modification, enhancement, or derivative work MUST be open source + - Source code must be publicly accessible under the same license terms + - All changes must be clearly documented and attributed + +4. COMMERCIAL USE + - Commercial use is permitted under the Apache License 2.0 terms + - Attribution requirements still apply for commercial use + - No additional permission required for commercial use + +5. TRADEMARK PROTECTION + - "LittleWhiteBox" and "小白X" are trademarks of the original author + - Derivative works may not use these names without explicit permission + - Alternative naming must clearly indicate the derivative nature + +VIOLATION CONSEQUENCES +===================== + +Any violation of these attribution requirements will result in: +- Immediate termination of the license grant +- Legal action for copyright infringement +- Demand for removal of infringing content + +COMPLIANCE EXAMPLES +================== + +✅ CORRECT Attribution Examples: +- "Powered by LittleWhiteBox by biex" +- "Based on LittleWhiteBox (https://github.com/RT15548/LittleWhiteBox) by biex" +- "Enhanced version of LittleWhiteBox by biex - Original: [repository URL]" + +❌ INCORRECT Examples: +- Using the code without any attribution +- Claiming original authorship +- Using "LittleWhiteBox" name for derivative works +- Commercial use without permission +- Closed-source modifications + +CONTACT INFORMATION +================== + +For licensing inquiries or attribution questions: +- Repository: https://github.com/RT15548/LittleWhiteBox +- Author: biex +- License: Apache-2.0 WITH Custom-Attribution-Requirements + +This copyright notice and attribution requirements must be included in all +copies or substantial portions of the software. diff --git a/docs/LICENSE.md b/docs/LICENSE.md index 2737c30..9d737a0 100644 --- a/docs/LICENSE.md +++ b/docs/LICENSE.md @@ -1,33 +1,33 @@ -Apache License -Version 2.0, January 2004 -http://www.apache.org/licenses/ - -Copyright 2025 biex - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -ADDITIONAL TERMS: - -In addition to the terms of the Apache License 2.0, the following -attribution requirement applies to any use, modification, or distribution -of this software: - -ATTRIBUTION REQUIREMENT: -If you reference, modify, or distribute any file from this project, -you must include attribution to the original author "biex" in your -project documentation, README, or credits section. - -Simple attribution format: "Based on LittleWhiteBox by biex" - -For the complete Apache License 2.0 text, see: -http://www.apache.org/licenses/LICENSE-2.0 +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +Copyright 2025 biex + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +ADDITIONAL TERMS: + +In addition to the terms of the Apache License 2.0, the following +attribution requirement applies to any use, modification, or distribution +of this software: + +ATTRIBUTION REQUIREMENT: +If you reference, modify, or distribute any file from this project, +you must include attribution to the original author "biex" in your +project documentation, README, or credits section. + +Simple attribution format: "Based on LittleWhiteBox by biex" + +For the complete Apache License 2.0 text, see: +http://www.apache.org/licenses/LICENSE-2.0 diff --git a/docs/NOTICE b/docs/NOTICE index bcdb077..1d189ae 100644 --- a/docs/NOTICE +++ b/docs/NOTICE @@ -1,95 +1,95 @@ -LittleWhiteBox (小白X) - Third-Party Notices and Attributions -================================================================ - -This software contains code and dependencies from various third-party sources. -The following notices and attributions are required by their respective licenses. - -PRIMARY SOFTWARE -================ - -LittleWhiteBox (小白X) -Copyright 2025 biex -Licensed under Apache-2.0 WITH Custom-Attribution-Requirements -Repository: https://github.com/RT15548/LittleWhiteBox - -RUNTIME DEPENDENCIES -==================== - -This extension is designed to work with SillyTavern and relies on the following -SillyTavern modules and APIs: - -1. SillyTavern Core Framework - - Copyright: SillyTavern Contributors - - License: AGPL-3.0 - - Repository: https://github.com/SillyTavern/SillyTavern - -2. SillyTavern Extensions API - - Used modules: extensions.js, script.js - - Provides: Extension framework, settings management, event system - -3. SillyTavern Slash Commands - - Used modules: slash-commands.js, SlashCommandParser.js - - Provides: Command execution framework - -4. SillyTavern UI Components - - Used modules: popup.js, utils.js - - Provides: User interface components and utilities - -BROWSER APIS AND STANDARDS -========================== - -This software uses standard web browser APIs: -- DOM API (Document Object Model) -- Fetch API for HTTP requests -- PostMessage API for iframe communication -- Local Storage API for data persistence -- Mutation Observer API for DOM monitoring - -JAVASCRIPT LIBRARIES -==================== - -The software may interact with the following JavaScript libraries -that are part of the SillyTavern environment: - -1. jQuery - - Copyright: jQuery Foundation and contributors - - License: MIT License - - Used for: DOM manipulation and event handling - -2. Toastr (if available) - - Copyright: CodeSeven - - License: MIT License - - Used for: Notification display - -DEVELOPMENT TOOLS -================= - -The following tools were used in development (not distributed): -- Visual Studio Code -- Git version control -- Various Node.js development tools - -ATTRIBUTION REQUIREMENTS -======================== - -When distributing this software or derivative works, you must: - -1. Include this NOTICE file -2. Maintain all copyright notices in source code -3. Provide attribution to the original author "biex" -4. Include a link to the original repository -5. Comply with Apache-2.0 license requirements -6. Follow the custom attribution requirements in LICENSE.md - -DISCLAIMER -========== - -This software is provided "AS IS" without warranty of any kind. -The author disclaims all warranties, express or implied, including -but not limited to the warranties of merchantability, fitness for -a particular purpose, and non-infringement. - -For complete license terms, see LICENSE.md -For attribution requirements, see COPYRIGHT - -Last updated: 2025-01-14 +LittleWhiteBox (小白X) - Third-Party Notices and Attributions +================================================================ + +This software contains code and dependencies from various third-party sources. +The following notices and attributions are required by their respective licenses. + +PRIMARY SOFTWARE +================ + +LittleWhiteBox (小白X) +Copyright 2025 biex +Licensed under Apache-2.0 WITH Custom-Attribution-Requirements +Repository: https://github.com/RT15548/LittleWhiteBox + +RUNTIME DEPENDENCIES +==================== + +This extension is designed to work with SillyTavern and relies on the following +SillyTavern modules and APIs: + +1. SillyTavern Core Framework + - Copyright: SillyTavern Contributors + - License: AGPL-3.0 + - Repository: https://github.com/SillyTavern/SillyTavern + +2. SillyTavern Extensions API + - Used modules: extensions.js, script.js + - Provides: Extension framework, settings management, event system + +3. SillyTavern Slash Commands + - Used modules: slash-commands.js, SlashCommandParser.js + - Provides: Command execution framework + +4. SillyTavern UI Components + - Used modules: popup.js, utils.js + - Provides: User interface components and utilities + +BROWSER APIS AND STANDARDS +========================== + +This software uses standard web browser APIs: +- DOM API (Document Object Model) +- Fetch API for HTTP requests +- PostMessage API for iframe communication +- Local Storage API for data persistence +- Mutation Observer API for DOM monitoring + +JAVASCRIPT LIBRARIES +==================== + +The software may interact with the following JavaScript libraries +that are part of the SillyTavern environment: + +1. jQuery + - Copyright: jQuery Foundation and contributors + - License: MIT License + - Used for: DOM manipulation and event handling + +2. Toastr (if available) + - Copyright: CodeSeven + - License: MIT License + - Used for: Notification display + +DEVELOPMENT TOOLS +================= + +The following tools were used in development (not distributed): +- Visual Studio Code +- Git version control +- Various Node.js development tools + +ATTRIBUTION REQUIREMENTS +======================== + +When distributing this software or derivative works, you must: + +1. Include this NOTICE file +2. Maintain all copyright notices in source code +3. Provide attribution to the original author "biex" +4. Include a link to the original repository +5. Comply with Apache-2.0 license requirements +6. Follow the custom attribution requirements in LICENSE.md + +DISCLAIMER +========== + +This software is provided "AS IS" without warranty of any kind. +The author disclaims all warranties, express or implied, including +but not limited to the warranties of merchantability, fitness for +a particular purpose, and non-infringement. + +For complete license terms, see LICENSE.md +For attribution requirements, see COPYRIGHT + +Last updated: 2025-01-14 diff --git a/index.js b/index.js index 08762ee..61c90b6 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,15 @@ -import { extension_settings, getContext } from "../../../extensions.js"; +import { extension_settings } from "../../../extensions.js"; import { saveSettingsDebounced, eventSource, event_types, getRequestHeaders } from "../../../../script.js"; -import { EXT_ID, EXT_NAME, extensionFolderPath } from "./core/constants.js"; +import { EXT_ID, extensionFolderPath } from "./core/constants.js"; import { executeSlashCommand } from "./core/slash-command.js"; import { EventCenter } from "./core/event-manager.js"; import { initTasks } from "./modules/scheduled-tasks/scheduled-tasks.js"; import { initMessagePreview, addHistoryButtonsDebounced } from "./modules/message-preview.js"; import { initImmersiveMode } from "./modules/immersive-mode.js"; -import { initTemplateEditor, templateSettings } from "./modules/template-editor/template-editor.js"; +import { initTemplateEditor } from "./modules/template-editor/template-editor.js"; import { initFourthWall, fourthWallCleanup } from "./modules/fourth-wall/fourth-wall.js"; -import { initButtonCollapse } from "./modules/button-collapse.js"; -import { initVariablesPanel, getVariablesPanelInstance, cleanupVariablesPanel } from "./modules/variables/variables-panel.js"; +import { initButtonCollapse } from "./widgets/button-collapse.js"; +import { initVariablesPanel, cleanupVariablesPanel } from "./modules/variables/variables-panel.js"; import { initStreamingGeneration } from "./modules/streaming-generation.js"; import { initVariablesCore, cleanupVariablesCore } from "./modules/variables/variables-core.js"; import { initControlAudio } from "./modules/control-audio.js"; @@ -17,8 +17,6 @@ import { initRenderer, cleanupRenderer, processExistingMessages, - processMessageById, - invalidateAll, clearBlobCaches, renderHtmlInIframe, shrinkRenderedWindowFull @@ -28,8 +26,7 @@ import { initVareventEditor, cleanupVareventEditor } from "./modules/variables/v import { initNovelDraw, cleanupNovelDraw } from "./modules/novel-draw/novel-draw.js"; import "./modules/story-summary/story-summary.js"; import "./modules/story-outline/story-outline.js"; - -const MODULE_NAME = "xiaobaix-memory"; +import { initTts, cleanupTts } from "./modules/tts/tts.js"; extension_settings[EXT_ID] = extension_settings[EXT_ID] || { enabled: true, @@ -46,6 +43,7 @@ extension_settings[EXT_ID] = extension_settings[EXT_ID] || { storySummary: { enabled: true }, storyOutline: { enabled: false }, novelDraw: { enabled: false }, + tts: { enabled: false }, useBlob: false, wrapperIframe: true, renderEnabled: true, @@ -277,7 +275,8 @@ function toggleSettingsControls(enabled) { 'xiaobaix_audio_enabled', 'xiaobaix_variables_panel_enabled', 'xiaobaix_use_blob', 'xiaobaix_variables_core_enabled', 'Wrapperiframe', 'xiaobaix_render_enabled', 'xiaobaix_max_rendered', 'xiaobaix_story_outline_enabled', 'xiaobaix_story_summary_enabled', - 'xiaobaix_novel_draw_enabled', 'xiaobaix_novel_draw_open_settings' + 'xiaobaix_novel_draw_enabled', 'xiaobaix_novel_draw_open_settings', + 'xiaobaix_tts_enabled', 'xiaobaix_tts_open_settings' ]; controls.forEach(id => { $(`#${id}`).prop('disabled', !enabled).closest('.flex-container').toggleClass('disabled-control', !enabled); @@ -311,6 +310,7 @@ async function toggleAllFeatures(enabled) { { condition: extension_settings[EXT_ID].variablesPanel?.enabled, init: initVariablesPanel }, { condition: extension_settings[EXT_ID].variablesCore?.enabled, init: initVariablesCore }, { condition: extension_settings[EXT_ID].novelDraw?.enabled, init: initNovelDraw }, + { condition: extension_settings[EXT_ID].tts?.enabled, init: initTts }, { condition: true, init: initStreamingGeneration }, { condition: true, init: initButtonCollapse } ]; @@ -345,6 +345,7 @@ async function toggleAllFeatures(enabled) { try { cleanupVarCommands(); } catch (e) {} try { cleanupVareventEditor(); } catch (e) {} try { cleanupNovelDraw(); } catch (e) {} + try { cleanupTts(); } catch (e) {} try { clearBlobCaches(); } catch (e) {} toggleSettingsControls(false); try { window.cleanupWorldbookHostBridge && window.cleanupWorldbookHostBridge(); document.getElementById('xb-worldbook')?.remove(); } catch (e) {} @@ -394,7 +395,8 @@ async function setupSettings() { { id: 'xiaobaix_variables_core_enabled', key: 'variablesCore', init: initVariablesCore }, { id: 'xiaobaix_story_summary_enabled', key: 'storySummary' }, { id: 'xiaobaix_story_outline_enabled', key: 'storyOutline' }, - { id: 'xiaobaix_novel_draw_enabled', key: 'novelDraw', init: initNovelDraw } + { id: 'xiaobaix_novel_draw_enabled', key: 'novelDraw', init: initNovelDraw }, + { id: 'xiaobaix_tts_enabled', key: 'tts', init: initTts } ]; moduleConfigs.forEach(({ id, key, init }) => { @@ -407,6 +409,9 @@ async function setupSettings() { if (!enabled && key === 'novelDraw') { try { cleanupNovelDraw(); } catch (e) {} } + if (!enabled && key === 'tts') { + try { cleanupTts(); } catch (e) {} + } settings[key] = extension_settings[EXT_ID][key] || {}; settings[key].enabled = enabled; extension_settings[EXT_ID][key] = settings[key]; @@ -432,6 +437,15 @@ async function setupSettings() { } }); + $("#xiaobaix_tts_open_settings").on("click", function () { + if (!isXiaobaixEnabled) return; + if (settings.tts?.enabled && window.xiaobaixTts?.openSettings) { + window.xiaobaixTts.openSettings(); + } else { + toastr.warning('请先启用 TTS 语音模块'); + } + }); + $("#xiaobaix_use_blob").prop("checked", !!settings.useBlob).on("change", async function () { if (!isXiaobaixEnabled) return; settings.useBlob = $(this).prop("checked"); @@ -493,10 +507,11 @@ async function setupSettings() { fourthWall: 'xiaobaix_fourth_wall_enabled', variablesPanel: 'xiaobaix_variables_panel_enabled', variablesCore: 'xiaobaix_variables_core_enabled', - novelDraw: 'xiaobaix_novel_draw_enabled' + novelDraw: 'xiaobaix_novel_draw_enabled', + tts: 'xiaobaix_tts_enabled' }; const ON = ['templateEditor', 'tasks', 'variablesCore', 'audio', 'storySummary', 'recorded']; - const OFF = ['preview', 'immersive', 'variablesPanel', 'fourthWall', 'storyOutline', 'novelDraw']; + const OFF = ['preview', 'immersive', 'variablesPanel', 'fourthWall', 'storyOutline', 'novelDraw', 'tts']; function setChecked(id, val) { const el = document.getElementById(id); if (el) { @@ -626,6 +641,7 @@ jQuery(async () => { { condition: settings.variablesPanel?.enabled, init: initVariablesPanel }, { condition: settings.variablesCore?.enabled, init: initVariablesCore }, { condition: settings.novelDraw?.enabled, init: initNovelDraw }, + { condition: settings.tts?.enabled, init: initTts }, { condition: true, init: initStreamingGeneration }, { condition: true, init: initButtonCollapse } ]; diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..eb55dae --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "ES2022", + "checkJs": true, + "allowJs": true, + "noEmit": true, + "lib": ["DOM", "ES2022"] + }, + "include": ["**/*.js"] +} diff --git a/libs/pixi.min.js b/libs/pixi.min.js new file mode 100644 index 0000000..954ac10 --- /dev/null +++ b/libs/pixi.min.js @@ -0,0 +1,1162 @@ +/*! + * pixi.js - v7.3.2 + * Compiled Fri, 20 Oct 2023 15:28:31 UTC + * + * pixi.js is licensed under the MIT License. + * http://www.opensource.org/licenses/mit-license + */var PIXI=function(_){"use strict";var _e=(s=>(s[s.WEBGL_LEGACY=0]="WEBGL_LEGACY",s[s.WEBGL=1]="WEBGL",s[s.WEBGL2=2]="WEBGL2",s))(_e||{}),or=(s=>(s[s.UNKNOWN=0]="UNKNOWN",s[s.WEBGL=1]="WEBGL",s[s.CANVAS=2]="CANVAS",s))(or||{}),Zi=(s=>(s[s.COLOR=16384]="COLOR",s[s.DEPTH=256]="DEPTH",s[s.STENCIL=1024]="STENCIL",s))(Zi||{}),H=(s=>(s[s.NORMAL=0]="NORMAL",s[s.ADD=1]="ADD",s[s.MULTIPLY=2]="MULTIPLY",s[s.SCREEN=3]="SCREEN",s[s.OVERLAY=4]="OVERLAY",s[s.DARKEN=5]="DARKEN",s[s.LIGHTEN=6]="LIGHTEN",s[s.COLOR_DODGE=7]="COLOR_DODGE",s[s.COLOR_BURN=8]="COLOR_BURN",s[s.HARD_LIGHT=9]="HARD_LIGHT",s[s.SOFT_LIGHT=10]="SOFT_LIGHT",s[s.DIFFERENCE=11]="DIFFERENCE",s[s.EXCLUSION=12]="EXCLUSION",s[s.HUE=13]="HUE",s[s.SATURATION=14]="SATURATION",s[s.COLOR=15]="COLOR",s[s.LUMINOSITY=16]="LUMINOSITY",s[s.NORMAL_NPM=17]="NORMAL_NPM",s[s.ADD_NPM=18]="ADD_NPM",s[s.SCREEN_NPM=19]="SCREEN_NPM",s[s.NONE=20]="NONE",s[s.SRC_OVER=0]="SRC_OVER",s[s.SRC_IN=21]="SRC_IN",s[s.SRC_OUT=22]="SRC_OUT",s[s.SRC_ATOP=23]="SRC_ATOP",s[s.DST_OVER=24]="DST_OVER",s[s.DST_IN=25]="DST_IN",s[s.DST_OUT=26]="DST_OUT",s[s.DST_ATOP=27]="DST_ATOP",s[s.ERASE=26]="ERASE",s[s.SUBTRACT=28]="SUBTRACT",s[s.XOR=29]="XOR",s))(H||{}),Lt=(s=>(s[s.POINTS=0]="POINTS",s[s.LINES=1]="LINES",s[s.LINE_LOOP=2]="LINE_LOOP",s[s.LINE_STRIP=3]="LINE_STRIP",s[s.TRIANGLES=4]="TRIANGLES",s[s.TRIANGLE_STRIP=5]="TRIANGLE_STRIP",s[s.TRIANGLE_FAN=6]="TRIANGLE_FAN",s))(Lt||{}),A=(s=>(s[s.RGBA=6408]="RGBA",s[s.RGB=6407]="RGB",s[s.RG=33319]="RG",s[s.RED=6403]="RED",s[s.RGBA_INTEGER=36249]="RGBA_INTEGER",s[s.RGB_INTEGER=36248]="RGB_INTEGER",s[s.RG_INTEGER=33320]="RG_INTEGER",s[s.RED_INTEGER=36244]="RED_INTEGER",s[s.ALPHA=6406]="ALPHA",s[s.LUMINANCE=6409]="LUMINANCE",s[s.LUMINANCE_ALPHA=6410]="LUMINANCE_ALPHA",s[s.DEPTH_COMPONENT=6402]="DEPTH_COMPONENT",s[s.DEPTH_STENCIL=34041]="DEPTH_STENCIL",s))(A||{}),Ie=(s=>(s[s.TEXTURE_2D=3553]="TEXTURE_2D",s[s.TEXTURE_CUBE_MAP=34067]="TEXTURE_CUBE_MAP",s[s.TEXTURE_2D_ARRAY=35866]="TEXTURE_2D_ARRAY",s[s.TEXTURE_CUBE_MAP_POSITIVE_X=34069]="TEXTURE_CUBE_MAP_POSITIVE_X",s[s.TEXTURE_CUBE_MAP_NEGATIVE_X=34070]="TEXTURE_CUBE_MAP_NEGATIVE_X",s[s.TEXTURE_CUBE_MAP_POSITIVE_Y=34071]="TEXTURE_CUBE_MAP_POSITIVE_Y",s[s.TEXTURE_CUBE_MAP_NEGATIVE_Y=34072]="TEXTURE_CUBE_MAP_NEGATIVE_Y",s[s.TEXTURE_CUBE_MAP_POSITIVE_Z=34073]="TEXTURE_CUBE_MAP_POSITIVE_Z",s[s.TEXTURE_CUBE_MAP_NEGATIVE_Z=34074]="TEXTURE_CUBE_MAP_NEGATIVE_Z",s))(Ie||{}),k=(s=>(s[s.UNSIGNED_BYTE=5121]="UNSIGNED_BYTE",s[s.UNSIGNED_SHORT=5123]="UNSIGNED_SHORT",s[s.UNSIGNED_SHORT_5_6_5=33635]="UNSIGNED_SHORT_5_6_5",s[s.UNSIGNED_SHORT_4_4_4_4=32819]="UNSIGNED_SHORT_4_4_4_4",s[s.UNSIGNED_SHORT_5_5_5_1=32820]="UNSIGNED_SHORT_5_5_5_1",s[s.UNSIGNED_INT=5125]="UNSIGNED_INT",s[s.UNSIGNED_INT_10F_11F_11F_REV=35899]="UNSIGNED_INT_10F_11F_11F_REV",s[s.UNSIGNED_INT_2_10_10_10_REV=33640]="UNSIGNED_INT_2_10_10_10_REV",s[s.UNSIGNED_INT_24_8=34042]="UNSIGNED_INT_24_8",s[s.UNSIGNED_INT_5_9_9_9_REV=35902]="UNSIGNED_INT_5_9_9_9_REV",s[s.BYTE=5120]="BYTE",s[s.SHORT=5122]="SHORT",s[s.INT=5124]="INT",s[s.FLOAT=5126]="FLOAT",s[s.FLOAT_32_UNSIGNED_INT_24_8_REV=36269]="FLOAT_32_UNSIGNED_INT_24_8_REV",s[s.HALF_FLOAT=36193]="HALF_FLOAT",s))(k||{}),D=(s=>(s[s.FLOAT=0]="FLOAT",s[s.INT=1]="INT",s[s.UINT=2]="UINT",s))(D||{}),zt=(s=>(s[s.NEAREST=0]="NEAREST",s[s.LINEAR=1]="LINEAR",s))(zt||{}),Wt=(s=>(s[s.CLAMP=33071]="CLAMP",s[s.REPEAT=10497]="REPEAT",s[s.MIRRORED_REPEAT=33648]="MIRRORED_REPEAT",s))(Wt||{}),Ut=(s=>(s[s.OFF=0]="OFF",s[s.POW2=1]="POW2",s[s.ON=2]="ON",s[s.ON_MANUAL=3]="ON_MANUAL",s))(Ut||{}),bt=(s=>(s[s.NPM=0]="NPM",s[s.UNPACK=1]="UNPACK",s[s.PMA=2]="PMA",s[s.NO_PREMULTIPLIED_ALPHA=0]="NO_PREMULTIPLIED_ALPHA",s[s.PREMULTIPLY_ON_UPLOAD=1]="PREMULTIPLY_ON_UPLOAD",s[s.PREMULTIPLIED_ALPHA=2]="PREMULTIPLIED_ALPHA",s))(bt||{}),kt=(s=>(s[s.NO=0]="NO",s[s.YES=1]="YES",s[s.AUTO=2]="AUTO",s[s.BLEND=0]="BLEND",s[s.CLEAR=1]="CLEAR",s[s.BLIT=2]="BLIT",s))(kt||{}),Qi=(s=>(s[s.AUTO=0]="AUTO",s[s.MANUAL=1]="MANUAL",s))(Qi||{}),At=(s=>(s.LOW="lowp",s.MEDIUM="mediump",s.HIGH="highp",s))(At||{}),ht=(s=>(s[s.NONE=0]="NONE",s[s.SCISSOR=1]="SCISSOR",s[s.STENCIL=2]="STENCIL",s[s.SPRITE=3]="SPRITE",s[s.COLOR=4]="COLOR",s))(ht||{}),na=(s=>(s[s.RED=1]="RED",s[s.GREEN=2]="GREEN",s[s.BLUE=4]="BLUE",s[s.ALPHA=8]="ALPHA",s))(na||{}),at=(s=>(s[s.NONE=0]="NONE",s[s.LOW=2]="LOW",s[s.MEDIUM=4]="MEDIUM",s[s.HIGH=8]="HIGH",s))(at||{}),Gt=(s=>(s[s.ELEMENT_ARRAY_BUFFER=34963]="ELEMENT_ARRAY_BUFFER",s[s.ARRAY_BUFFER=34962]="ARRAY_BUFFER",s[s.UNIFORM_BUFFER=35345]="UNIFORM_BUFFER",s))(Gt||{});const aa={createCanvas:(s,t)=>{const e=document.createElement("canvas");return e.width=s,e.height=t,e},getCanvasRenderingContext2D:()=>CanvasRenderingContext2D,getWebGLRenderingContext:()=>WebGLRenderingContext,getNavigator:()=>navigator,getBaseUrl:()=>{var s;return(s=document.baseURI)!=null?s:window.location.href},getFontFaceSet:()=>document.fonts,fetch:(s,t)=>fetch(s,t),parseXML:s=>new DOMParser().parseFromString(s,"text/xml")},O={ADAPTER:aa,RESOLUTION:1,CREATE_IMAGE_BITMAP:!1,ROUND_PIXELS:!1};var hr=/iPhone/i,oa=/iPod/i,ha=/iPad/i,la=/\biOS-universal(?:.+)Mac\b/i,lr=/\bAndroid(?:.+)Mobile\b/i,ua=/Android/i,$e=/(?:SD4930UR|\bSilk(?:.+)Mobile\b)/i,Ji=/Silk/i,se=/Windows Phone/i,ca=/\bWindows(?:.+)ARM\b/i,da=/BlackBerry/i,fa=/BB10/i,pa=/Opera Mini/i,ma=/\b(CriOS|Chrome)(?:.+)Mobile/i,ga=/Mobile(?:.+)Firefox\b/i,_a=function(s){return typeof s!="undefined"&&s.platform==="MacIntel"&&typeof s.maxTouchPoints=="number"&&s.maxTouchPoints>1&&typeof MSStream=="undefined"};function fu(s){return function(t){return t.test(s)}}function va(s){var t={userAgent:"",platform:"",maxTouchPoints:0};!s&&typeof navigator!="undefined"?t={userAgent:navigator.userAgent,platform:navigator.platform,maxTouchPoints:navigator.maxTouchPoints||0}:typeof s=="string"?t.userAgent=s:s&&s.userAgent&&(t={userAgent:s.userAgent,platform:s.platform,maxTouchPoints:s.maxTouchPoints||0});var e=t.userAgent,i=e.split("[FBAN");typeof i[1]!="undefined"&&(e=i[0]),i=e.split("Twitter"),typeof i[1]!="undefined"&&(e=i[0]);var r=fu(e),n={apple:{phone:r(hr)&&!r(se),ipod:r(oa),tablet:!r(hr)&&(r(ha)||_a(t))&&!r(se),universal:r(la),device:(r(hr)||r(oa)||r(ha)||r(la)||_a(t))&&!r(se)},amazon:{phone:r($e),tablet:!r($e)&&r(Ji),device:r($e)||r(Ji)},android:{phone:!r(se)&&r($e)||!r(se)&&r(lr),tablet:!r(se)&&!r($e)&&!r(lr)&&(r(Ji)||r(ua)),device:!r(se)&&(r($e)||r(Ji)||r(lr)||r(ua))||r(/\bokhttp\b/i)},windows:{phone:r(se),tablet:r(ca),device:r(se)||r(ca)},other:{blackberry:r(da),blackberry10:r(fa),opera:r(pa),firefox:r(ga),chrome:r(ma),device:r(da)||r(fa)||r(pa)||r(ga)||r(ma)},any:!1,phone:!1,tablet:!1};return n.any=n.apple.device||n.android.device||n.windows.device||n.other.device,n.phone=n.apple.phone||n.android.phone||n.windows.phone,n.tablet=n.apple.tablet||n.android.tablet||n.windows.tablet,n}var ya;const $t=((ya=va.default)!=null?ya:va)(globalThis.navigator);O.RETINA_PREFIX=/@([0-9\.]+)x/,O.FAIL_IF_MAJOR_PERFORMANCE_CAVEAT=!1;var ur=typeof globalThis!="undefined"?globalThis:typeof window!="undefined"?window:typeof global!="undefined"?global:typeof self!="undefined"?self:{};function He(s){return s&&s.__esModule&&Object.prototype.hasOwnProperty.call(s,"default")?s.default:s}function Gm(s){return s&&Object.prototype.hasOwnProperty.call(s,"default")?s.default:s}function $m(s){return s&&Object.prototype.hasOwnProperty.call(s,"default")&&Object.keys(s).length===1?s.default:s}function Hm(s){if(s.__esModule)return s;var t=s.default;if(typeof t=="function"){var e=function i(){if(this instanceof i){var r=[null];r.push.apply(r,arguments);var n=Function.bind.apply(t,r);return new n}return t.apply(this,arguments)};e.prototype=t.prototype}else e={};return Object.defineProperty(e,"__esModule",{value:!0}),Object.keys(s).forEach(function(i){var r=Object.getOwnPropertyDescriptor(s,i);Object.defineProperty(e,i,r.get?r:{enumerable:!0,get:function(){return s[i]}})}),e}var cr={exports:{}},Vm=cr.exports;(function(s){"use strict";var t=Object.prototype.hasOwnProperty,e="~";function i(){}Object.create&&(i.prototype=Object.create(null),new i().__proto__||(e=!1));function r(h,l,u){this.fn=h,this.context=l,this.once=u||!1}function n(h,l,u,c,d){if(typeof u!="function")throw new TypeError("The listener must be a function");var f=new r(u,c||h,d),p=e?e+l:l;return h._events[p]?h._events[p].fn?h._events[p]=[h._events[p],f]:h._events[p].push(f):(h._events[p]=f,h._eventsCount++),h}function a(h,l){--h._eventsCount===0?h._events=new i:delete h._events[l]}function o(){this._events=new i,this._eventsCount=0}o.prototype.eventNames=function(){var l=[],u,c;if(this._eventsCount===0)return l;for(c in u=this._events)t.call(u,c)&&l.push(e?c.slice(1):c);return Object.getOwnPropertySymbols?l.concat(Object.getOwnPropertySymbols(u)):l},o.prototype.listeners=function(l){var u=e?e+l:l,c=this._events[u];if(!c)return[];if(c.fn)return[c.fn];for(var d=0,f=c.length,p=new Array(f);d80*e){o=l=s[0],h=u=s[1];for(var p=e;pl&&(l=c),d>u&&(u=d);f=Math.max(l-o,u-h),f=f!==0?32767/f:0}return ni(n,a,e,o,h,f,0),a}function xa(s,t,e,i,r){var n,a;if(r===pr(s,t,e,i)>0)for(n=t;n=t;n-=i)a=Ea(n,s[n],s[n+1],a);return a&&is(a,a.next)&&(oi(a),a=a.next),a}function Re(s,t){if(!s)return s;t||(t=s);var e=s,i;do if(i=!1,!e.steiner&&(is(e,e.next)||rt(e.prev,e,e.next)===0)){if(oi(e),e=t=e.prev,e===e.next)break;i=!0}else e=e.next;while(i||e!==t);return t}function ni(s,t,e,i,r,n,a){if(s){!a&&n&&Au(s,i,r,n);for(var o=s,h,l;s.prev!==s.next;){if(h=s.prev,l=s.next,n?gu(s,i,r,n):mu(s)){t.push(h.i/e|0),t.push(s.i/e|0),t.push(l.i/e|0),oi(s),s=l.next,o=l.next;continue}if(s=l,s===o){a?a===1?(s=_u(Re(s),t,e),ni(s,t,e,i,r,n,2)):a===2&&vu(s,t,e,i,r,n):ni(Re(s),t,e,i,r,n,1);break}}}}function mu(s){var t=s.prev,e=s,i=s.next;if(rt(t,e,i)>=0)return!1;for(var r=t.x,n=e.x,a=i.x,o=t.y,h=e.y,l=i.y,u=rn?r>a?r:a:n>a?n:a,f=o>h?o>l?o:l:h>l?h:l,p=i.next;p!==t;){if(p.x>=u&&p.x<=d&&p.y>=c&&p.y<=f&&Xe(r,o,n,h,a,l,p.x,p.y)&&rt(p.prev,p,p.next)>=0)return!1;p=p.next}return!0}function gu(s,t,e,i){var r=s.prev,n=s,a=s.next;if(rt(r,n,a)>=0)return!1;for(var o=r.x,h=n.x,l=a.x,u=r.y,c=n.y,d=a.y,f=oh?o>l?o:l:h>l?h:l,g=u>c?u>d?u:d:c>d?c:d,y=dr(f,p,t,e,i),b=dr(m,g,t,e,i),v=s.prevZ,x=s.nextZ;v&&v.z>=y&&x&&x.z<=b;){if(v.x>=f&&v.x<=m&&v.y>=p&&v.y<=g&&v!==r&&v!==a&&Xe(o,u,h,c,l,d,v.x,v.y)&&rt(v.prev,v,v.next)>=0||(v=v.prevZ,x.x>=f&&x.x<=m&&x.y>=p&&x.y<=g&&x!==r&&x!==a&&Xe(o,u,h,c,l,d,x.x,x.y)&&rt(x.prev,x,x.next)>=0))return!1;x=x.nextZ}for(;v&&v.z>=y;){if(v.x>=f&&v.x<=m&&v.y>=p&&v.y<=g&&v!==r&&v!==a&&Xe(o,u,h,c,l,d,v.x,v.y)&&rt(v.prev,v,v.next)>=0)return!1;v=v.prevZ}for(;x&&x.z<=b;){if(x.x>=f&&x.x<=m&&x.y>=p&&x.y<=g&&x!==r&&x!==a&&Xe(o,u,h,c,l,d,x.x,x.y)&&rt(x.prev,x,x.next)>=0)return!1;x=x.nextZ}return!0}function _u(s,t,e){var i=s;do{var r=i.prev,n=i.next.next;!is(r,n)&&ba(r,i,i.next,n)&&ai(r,n)&&ai(n,r)&&(t.push(r.i/e|0),t.push(i.i/e|0),t.push(n.i/e|0),oi(i),oi(i.next),i=s=n),i=i.next}while(i!==s);return Re(i)}function vu(s,t,e,i,r,n){var a=s;do{for(var o=a.next.next;o!==a.prev;){if(a.i!==o.i&&Iu(a,o)){var h=Ta(a,o);a=Re(a,a.next),h=Re(h,h.next),ni(a,t,e,i,r,n,0),ni(h,t,e,i,r,n,0);return}o=o.next}a=a.next}while(a!==s)}function yu(s,t,e,i){var r=[],n,a,o,h,l;for(n=0,a=t.length;n=e.next.y&&e.next.y!==e.y){var o=e.x+(r-e.y)*(e.next.x-e.x)/(e.next.y-e.y);if(o<=i&&o>n&&(n=o,a=e.x=e.x&&e.x>=l&&i!==e.x&&Xe(ra.x||e.x===a.x&&Eu(a,e)))&&(a=e,c=d)),e=e.next;while(e!==h);return a}function Eu(s,t){return rt(s.prev,s,t.prev)<0&&rt(t.next,s,s.next)<0}function Au(s,t,e,i){var r=s;do r.z===0&&(r.z=dr(r.x,r.y,t,e,i)),r.prevZ=r.prev,r.nextZ=r.next,r=r.next;while(r!==s);r.prevZ.nextZ=null,r.prevZ=null,wu(r)}function wu(s){var t,e,i,r,n,a,o,h,l=1;do{for(e=s,s=null,n=null,a=0;e;){for(a++,i=e,o=0,t=0;t0||h>0&&i;)o!==0&&(h===0||!i||e.z<=i.z)?(r=e,e=e.nextZ,o--):(r=i,i=i.nextZ,h--),n?n.nextZ=r:s=r,r.prevZ=n,n=r;e=i}n.nextZ=null,l*=2}while(a>1);return s}function dr(s,t,e,i,r){return s=(s-e)*r|0,t=(t-i)*r|0,s=(s|s<<8)&16711935,s=(s|s<<4)&252645135,s=(s|s<<2)&858993459,s=(s|s<<1)&1431655765,t=(t|t<<8)&16711935,t=(t|t<<4)&252645135,t=(t|t<<2)&858993459,t=(t|t<<1)&1431655765,s|t<<1}function Su(s){var t=s,e=s;do(t.x=(s-a)*(n-o)&&(s-a)*(i-o)>=(e-a)*(t-o)&&(e-a)*(n-o)>=(r-a)*(i-o)}function Iu(s,t){return s.next.i!==t.i&&s.prev.i!==t.i&&!Ru(s,t)&&(ai(s,t)&&ai(t,s)&&Cu(s,t)&&(rt(s.prev,s,t.prev)||rt(s,t.prev,t))||is(s,t)&&rt(s.prev,s,s.next)>0&&rt(t.prev,t,t.next)>0)}function rt(s,t,e){return(t.y-s.y)*(e.x-t.x)-(t.x-s.x)*(e.y-t.y)}function is(s,t){return s.x===t.x&&s.y===t.y}function ba(s,t,e,i){var r=rs(rt(s,t,e)),n=rs(rt(s,t,i)),a=rs(rt(e,i,s)),o=rs(rt(e,i,t));return!!(r!==n&&a!==o||r===0&&ss(s,e,t)||n===0&&ss(s,i,t)||a===0&&ss(e,s,i)||o===0&&ss(e,t,i))}function ss(s,t,e){return t.x<=Math.max(s.x,e.x)&&t.x>=Math.min(s.x,e.x)&&t.y<=Math.max(s.y,e.y)&&t.y>=Math.min(s.y,e.y)}function rs(s){return s>0?1:s<0?-1:0}function Ru(s,t){var e=s;do{if(e.i!==s.i&&e.next.i!==s.i&&e.i!==t.i&&e.next.i!==t.i&&ba(e,e.next,s,t))return!0;e=e.next}while(e!==s);return!1}function ai(s,t){return rt(s.prev,s,s.next)<0?rt(s,t,s.next)>=0&&rt(s,s.prev,t)>=0:rt(s,t,s.prev)<0||rt(s,s.next,t)<0}function Cu(s,t){var e=s,i=!1,r=(s.x+t.x)/2,n=(s.y+t.y)/2;do e.y>n!=e.next.y>n&&e.next.y!==e.y&&r<(e.next.x-e.x)*(n-e.y)/(e.next.y-e.y)+e.x&&(i=!i),e=e.next;while(e!==s);return i}function Ta(s,t){var e=new fr(s.i,s.x,s.y),i=new fr(t.i,t.x,t.y),r=s.next,n=t.prev;return s.next=t,t.prev=s,e.next=r,r.prev=e,i.next=e,e.prev=i,n.next=i,i.prev=n,i}function Ea(s,t,e,i){var r=new fr(s,t,e);return i?(r.next=i.next,r.prev=i,i.next.prev=r,i.next=r):(r.prev=r,r.next=r),r}function oi(s){s.next.prev=s.prev,s.prev.next=s.next,s.prevZ&&(s.prevZ.nextZ=s.nextZ),s.nextZ&&(s.nextZ.prevZ=s.prevZ)}function fr(s,t,e){this.i=s,this.x=t,this.y=e,this.prev=null,this.next=null,this.z=0,this.prevZ=null,this.nextZ=null,this.steiner=!1}es.deviation=function(s,t,e,i){var r=t&&t.length,n=r?t[0]*e:s.length,a=Math.abs(pr(s,0,n,e));if(r)for(var o=0,h=t.length;o0&&(i+=s[r-1].length,e.holes.push(i))}return e};var Pu=ts.exports,Aa=He(Pu),hi={},ns={exports:{}};/*! https://mths.be/punycode v1.3.2 by @mathias */var zm=ns.exports;(function(s,t){(function(e){var i=t&&!t.nodeType&&t,r=s&&!s.nodeType&&s,n=typeof ur=="object"&&ur;(n.global===n||n.window===n||n.self===n)&&(e=n);var a,o=2147483647,h=36,l=1,u=26,c=38,d=700,f=72,p=128,m="-",g=/^xn--/,y=/[^\x20-\x7E]/,b=/[\x2E\u3002\uFF0E\uFF61]/g,v={overflow:"Overflow: input needs wider integers to process","not-basic":"Illegal input >= 0x80 (not a basic code point)","invalid-input":"Invalid input"},x=h-l,E=Math.floor,M=String.fromCharCode,S;function w(P){throw RangeError(v[P])}function F(P,C){for(var K=P.length,Q=[];K--;)Q[K]=C(P[K]);return Q}function G(P,C){var K=P.split("@"),Q="";K.length>1&&(Q=K[0]+"@",P=K[1]),P=P.replace(b,".");var J=P.split("."),gt=F(J,C).join(".");return Q+gt}function Y(P){for(var C=[],K=0,Q=P.length,J,gt;K=55296&&J<=56319&&K65535&&(C-=65536,K+=M(C>>>10&1023|55296),C=56320|C&1023),K+=M(C),K}).join("")}function T(P){return P-48<10?P-22:P-65<26?P-65:P-97<26?P-97:h}function I(P,C){return P+22+75*(P<26)-((C!=0)<<5)}function $(P,C,K){var Q=0;for(P=K?E(P/d):P>>1,P+=E(P/C);P>x*u>>1;Q+=h)P=E(P/x);return E(Q+(x+1)*P/(P+c))}function W(P){var C=[],K=P.length,Q,J=0,gt=p,ut=f,_t,xt,Ct,vt,st,ct,dt,te,ee;for(_t=P.lastIndexOf(m),_t<0&&(_t=0),xt=0;xt<_t;++xt)P.charCodeAt(xt)>=128&&w("not-basic"),C.push(P.charCodeAt(xt));for(Ct=_t>0?_t+1:0;Ct=K&&w("invalid-input"),dt=T(P.charCodeAt(Ct++)),(dt>=h||dt>E((o-J)/st))&&w("overflow"),J+=dt*st,te=ct<=ut?l:ct>=ut+u?u:ct-ut,!(dtE(o/ee)&&w("overflow"),st*=ee;Q=C.length+1,ut=$(J-vt,Q,vt==0),E(J/Q)>o-gt&&w("overflow"),gt+=E(J/Q),J%=Q,C.splice(J++,0,gt)}return N(C)}function V(P){var C,K,Q,J,gt,ut,_t,xt,Ct,vt,st,ct=[],dt,te,ee,ji;for(P=Y(P),dt=P.length,C=p,K=0,gt=f,ut=0;ut=C&&st<_t&&(_t=st);for(te=Q+1,_t-C>E((o-K)/te)&&w("overflow"),K+=(_t-C)*te,C=_t,ut=0;uto&&w("overflow"),st==C){for(xt=K,Ct=h;vt=Ct<=gt?l:Ct>=gt+u?u:Ct-gt,!(xt0&&o>a&&(o=a);for(var h=0;h=0?(c=l.substr(0,u),d=l.substr(u+1)):(c=l,d=""),f=decodeURIComponent(c),p=decodeURIComponent(d),Mu(r,f)?Array.isArray(r[f])?r[f].push(p):r[f]=[r[f],p]:r[f]=p}return r},qm=He(Ia),ui=function(s){switch(typeof s){case"string":return s;case"boolean":return s?"true":"false";case"number":return isFinite(s)?s:"";default:return""}},Ra=function(s,t,e,i){return t=t||"&",e=e||"=",s===null&&(s=void 0),typeof s=="object"?Object.keys(s).map(function(r){var n=encodeURIComponent(ui(r))+e;return Array.isArray(s[r])?s[r].map(function(a){return n+encodeURIComponent(ui(a))}).join(t):n+encodeURIComponent(ui(s[r]))}).join(t):i?encodeURIComponent(ui(i))+e+encodeURIComponent(ui(s)):""},Km=He(Ra),Du,Ou,Zm=li.decode=Ou=li.parse=Ia,Qm=li.encode=Du=li.stringify=Ra,Bu=wa,Yt=Sa,Fu=hi.parse=ci,Nu=hi.resolve=Wu,Jm=hi.resolveObject=Yu,Lu=hi.format=zu,tg=hi.Url=Mt;function Mt(){this.protocol=null,this.slashes=null,this.auth=null,this.host=null,this.port=null,this.hostname=null,this.hash=null,this.search=null,this.query=null,this.pathname=null,this.path=null,this.href=null}var Uu=/^([a-z0-9.+-]+:)/i,ku=/:[0-9]*$/,Gu=/^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/,$u=["<",">",'"',"`"," ","\r",` +`," "],Hu=["{","}","|","\\","^","`"].concat($u),mr=["'"].concat(Hu),Ca=["%","/","?",";","#"].concat(mr),Pa=["/","?","#"],Vu=255,Ma=/^[+a-z0-9A-Z_-]{0,63}$/,Xu=/^([+a-z0-9A-Z_-]{0,63})(.*)$/,ju={javascript:!0,"javascript:":!0},gr={javascript:!0,"javascript:":!0},je={http:!0,https:!0,ftp:!0,gopher:!0,file:!0,"http:":!0,"https:":!0,"ftp:":!0,"gopher:":!0,"file:":!0},_r=li;function ci(s,t,e){if(s&&Yt.isObject(s)&&s instanceof Mt)return s;var i=new Mt;return i.parse(s,t,e),i}Mt.prototype.parse=function(s,t,e){if(!Yt.isString(s))throw new TypeError("Parameter 'url' must be a string, not "+typeof s);var i=s.indexOf("?"),r=i!==-1&&i127?E+="x":E+=x[M];if(!E.match(Ma)){var w=b.slice(0,f),F=b.slice(f+1),G=x.match(Xu);G&&(w.push(G[1]),F.unshift(G[2])),F.length&&(o="/"+F.join(".")+o),this.hostname=w.join(".");break}}}this.hostname.length>Vu?this.hostname="":this.hostname=this.hostname.toLowerCase(),y||(this.hostname=Bu.toASCII(this.hostname));var Y=this.port?":"+this.port:"",N=this.hostname||"";this.host=N+Y,this.href+=this.host,y&&(this.hostname=this.hostname.substr(1,this.hostname.length-2),o[0]!=="/"&&(o="/"+o))}if(!ju[u])for(var f=0,v=mr.length;f0?e.host.split("@"):!1;E&&(e.auth=E.shift(),e.host=e.hostname=E.shift())}return e.search=s.search,e.query=s.query,(!Yt.isNull(e.pathname)||!Yt.isNull(e.search))&&(e.path=(e.pathname?e.pathname:"")+(e.search?e.search:"")),e.href=e.format(),e}if(!b.length)return e.pathname=null,e.search?e.path="/"+e.search:e.path=null,e.href=e.format(),e;for(var M=b.slice(-1)[0],S=(e.host||s.host||b.length>1)&&(M==="."||M==="..")||M==="",w=0,F=b.length;F>=0;F--)M=b[F],M==="."?b.splice(F,1):M===".."?(b.splice(F,1),w++):w&&(b.splice(F,1),w--);if(!g&&!y)for(;w--;w)b.unshift("..");g&&b[0]!==""&&(!b[0]||b[0].charAt(0)!=="/")&&b.unshift(""),S&&b.join("/").substr(-1)!=="/"&&b.push("");var G=b[0]===""||b[0]&&b[0].charAt(0)==="/";if(x){e.hostname=e.host=G?"":b.length?b.shift():"";var E=e.host&&e.host.indexOf("@")>0?e.host.split("@"):!1;E&&(e.auth=E.shift(),e.host=e.hostname=E.shift())}return g=g||e.host&&b.length,g&&!G&&b.unshift(""),b.length?e.pathname=b.join("/"):(e.pathname=null,e.path=null),(!Yt.isNull(e.pathname)||!Yt.isNull(e.search))&&(e.path=(e.pathname?e.pathname:"")+(e.search?e.search:"")),e.auth=s.auth||e.auth,e.slashes=e.slashes||s.slashes,e.href=e.format(),e},Mt.prototype.parseHost=function(){var s=this.host,t=ku.exec(s);t&&(t=t[0],t!==":"&&(this.port=t.substr(1)),s=s.substr(0,s.length-t.length)),s&&(this.hostname=s)};const Da={};function Oa(s,t,e=3){if(Da[t])return;let i=new Error().stack;typeof i=="undefined"?console.warn("PixiJS Deprecation Warning: ",`${t} +Deprecated since v${s}`):(i=i.split(` +`).splice(e).join(` +`),console.groupCollapsed?(console.groupCollapsed("%cPixiJS Deprecation Warning: %c%s","color:#614108;background:#fffbe6","font-weight:normal;color:#614108;background:#fffbe6",`${t} +Deprecated since v${s}`),console.warn(i),console.groupEnd()):(console.warn("PixiJS Deprecation Warning: ",`${t} +Deprecated since v${s}`),console.warn(i))),Da[t]=!0}const qu={get parse(){return Fu},get format(){return Lu},get resolve(){return Nu}};function Ht(s){if(typeof s!="string")throw new TypeError(`Path must be a string. Received ${JSON.stringify(s)}`)}function di(s){return s.split("?")[0].split("#")[0]}function Ku(s){return s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Zu(s,t,e){return s.replace(new RegExp(Ku(t),"g"),e)}function Qu(s,t){let e="",i=0,r=-1,n=0,a=-1;for(let o=0;o<=s.length;++o){if(o2){const h=e.lastIndexOf("/");if(h!==e.length-1){h===-1?(e="",i=0):(e=e.slice(0,h),i=e.length-1-e.lastIndexOf("/")),r=o,n=0;continue}}else if(e.length===2||e.length===1){e="",i=0,r=o,n=0;continue}}t&&(e.length>0?e+="/..":e="..",i=2)}else e.length>0?e+=`/${s.slice(r+1,o)}`:e=s.slice(r+1,o),i=o-r-1;r=o,n=0}else a===46&&n!==-1?++n:n=-1}return e}const lt={toPosix(s){return Zu(s,"\\","/")},isUrl(s){return/^https?:/.test(this.toPosix(s))},isDataUrl(s){return/^data:([a-z]+\/[a-z0-9-+.]+(;[a-z0-9-.!#$%*+.{}|~`]+=[a-z0-9-.!#$%*+.{}()_|~`]+)*)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s<>]*?)$/i.test(s)},isBlobUrl(s){return s.startsWith("blob:")},hasProtocol(s){return/^[^/:]+:/.test(this.toPosix(s))},getProtocol(s){Ht(s),s=this.toPosix(s);const t=/^file:\/\/\//.exec(s);if(t)return t[0];const e=/^[^/:]+:\/{0,2}/.exec(s);return e?e[0]:""},toAbsolute(s,t,e){if(Ht(s),this.isDataUrl(s)||this.isBlobUrl(s))return s;const i=di(this.toPosix(t!=null?t:O.ADAPTER.getBaseUrl())),r=di(this.toPosix(e!=null?e:this.rootname(i)));return s=this.toPosix(s),s.startsWith("/")?lt.join(r,s.slice(1)):this.isAbsolute(s)?s:this.join(i,s)},normalize(s){if(Ht(s),s.length===0)return".";if(this.isDataUrl(s)||this.isBlobUrl(s))return s;s=this.toPosix(s);let t="";const e=s.startsWith("/");this.hasProtocol(s)&&(t=this.rootname(s),s=s.slice(t.length));const i=s.endsWith("/");return s=Qu(s,!1),s.length>0&&i&&(s+="/"),e?`/${s}`:t+s},isAbsolute(s){return Ht(s),s=this.toPosix(s),this.hasProtocol(s)?!0:s.startsWith("/")},join(...s){var t;if(s.length===0)return".";let e;for(let i=0;i0)if(e===void 0)e=r;else{const n=(t=s[i-1])!=null?t:"";this.extname(n)?e+=`/../${r}`:e+=`/${r}`}}return e===void 0?".":this.normalize(e)},dirname(s){if(Ht(s),s.length===0)return".";s=this.toPosix(s);let t=s.charCodeAt(0);const e=t===47;let i=-1,r=!0;const n=this.getProtocol(s),a=s;s=s.slice(n.length);for(let o=s.length-1;o>=1;--o)if(t=s.charCodeAt(o),t===47){if(!r){i=o;break}}else r=!1;return i===-1?e?"/":this.isUrl(a)?n+s:n:e&&i===1?"//":n+s.slice(0,i)},rootname(s){Ht(s),s=this.toPosix(s);let t="";if(s.startsWith("/")?t="/":t=this.getProtocol(s),this.isUrl(s)){const e=s.indexOf("/",t.length);e!==-1?t=s.slice(0,e):t=s,t.endsWith("/")||(t+="/")}return t},basename(s,t){Ht(s),t&&Ht(t),s=di(this.toPosix(s));let e=0,i=-1,r=!0,n;if(t!==void 0&&t.length>0&&t.length<=s.length){if(t.length===s.length&&t===s)return"";let a=t.length-1,o=-1;for(n=s.length-1;n>=0;--n){const h=s.charCodeAt(n);if(h===47){if(!r){e=n+1;break}}else o===-1&&(r=!1,o=n+1),a>=0&&(h===t.charCodeAt(a)?--a===-1&&(i=n):(a=-1,i=o))}return e===i?i=o:i===-1&&(i=s.length),s.slice(e,i)}for(n=s.length-1;n>=0;--n)if(s.charCodeAt(n)===47){if(!r){e=n+1;break}}else i===-1&&(r=!1,i=n+1);return i===-1?"":s.slice(e,i)},extname(s){Ht(s),s=di(this.toPosix(s));let t=-1,e=0,i=-1,r=!0,n=0;for(let a=s.length-1;a>=0;--a){const o=s.charCodeAt(a);if(o===47){if(!r){e=a+1;break}continue}i===-1&&(r=!1,i=a+1),o===46?t===-1?t=a:n!==1&&(n=1):t!==-1&&(n=-1)}return t===-1||i===-1||n===0||n===1&&t===i-1&&t===e+1?"":s.slice(t,i)},parse(s){Ht(s);const t={root:"",dir:"",base:"",ext:"",name:""};if(s.length===0)return t;s=di(this.toPosix(s));let e=s.charCodeAt(0);const i=this.isAbsolute(s);let r;const n="";t.root=this.rootname(s),i||this.hasProtocol(s)?r=1:r=0;let a=-1,o=0,h=-1,l=!0,u=s.length-1,c=0;for(;u>=r;--u){if(e=s.charCodeAt(u),e===47){if(!l){o=u+1;break}continue}h===-1&&(l=!1,h=u+1),e===46?a===-1?a=u:c!==1&&(c=1):a!==-1&&(c=-1)}return a===-1||h===-1||c===0||c===1&&a===h-1&&a===o+1?h!==-1&&(o===0&&i?t.base=t.name=s.slice(1,h):t.base=t.name=s.slice(o,h)):(o===0&&i?(t.name=s.slice(1,a),t.base=s.slice(1,h)):(t.name=s.slice(o,a),t.base=s.slice(o,h)),t.ext=s.slice(a,h)),t.dir=this.dirname(s),n&&(t.dir=n+t.dir),t},sep:"/",delimiter:":"};let vr;async function Ba(){return vr!=null||(vr=(async()=>{var s;const t=document.createElement("canvas").getContext("webgl");if(!t)return bt.UNPACK;const e=await new Promise(a=>{const o=document.createElement("video");o.onloadeddata=()=>a(o),o.onerror=()=>a(null),o.autoplay=!1,o.crossOrigin="anonymous",o.preload="auto",o.src="data:video/webm;base64,GkXfo59ChoEBQveBAULygQRC84EIQoKEd2VibUKHgQJChYECGFOAZwEAAAAAAAHTEU2bdLpNu4tTq4QVSalmU6yBoU27i1OrhBZUrmtTrIHGTbuMU6uEElTDZ1OsggEXTbuMU6uEHFO7a1OsggG97AEAAAAAAABZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAVSalmoCrXsYMPQkBNgIRMYXZmV0GETGF2ZkSJiEBEAAAAAAAAFlSua8yuAQAAAAAAAEPXgQFzxYgAAAAAAAAAAZyBACK1nIN1bmSIgQCGhVZfVlA5g4EBI+ODhAJiWgDglLCBArqBApqBAlPAgQFVsIRVuYEBElTDZ9Vzc9JjwItjxYgAAAAAAAAAAWfInEWjh0VOQ09ERVJEh49MYXZjIGxpYnZweC12cDlnyKJFo4hEVVJBVElPTkSHlDAwOjAwOjAwLjA0MDAwMDAwMAAAH0O2dcfngQCgwqGggQAAAIJJg0IAABAAFgA4JBwYSgAAICAAEb///4r+AAB1oZ2mm+6BAaWWgkmDQgAAEAAWADgkHBhKAAAgIABIQBxTu2uRu4+zgQC3iveBAfGCAXHwgQM=",o.load()});if(!e)return bt.UNPACK;const i=t.createTexture();t.bindTexture(t.TEXTURE_2D,i);const r=t.createFramebuffer();t.bindFramebuffer(t.FRAMEBUFFER,r),t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,i,0),t.pixelStorei(t.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!1),t.pixelStorei(t.UNPACK_COLORSPACE_CONVERSION_WEBGL,t.NONE),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e);const n=new Uint8Array(4);return t.readPixels(0,0,1,1,t.RGBA,t.UNSIGNED_BYTE,n),t.deleteFramebuffer(r),t.deleteTexture(i),(s=t.getExtension("WEBGL_lose_context"))==null||s.loseContext(),n[0]<=n[3]?bt.PMA:bt.UNPACK})()),vr}function Ju(){}function tc(){}let yr;function Fa(){return typeof yr=="undefined"&&(yr=function(){var s;const t={stencil:!0,failIfMajorPerformanceCaveat:O.FAIL_IF_MAJOR_PERFORMANCE_CAVEAT};try{if(!O.ADAPTER.getWebGLRenderingContext())return!1;const e=O.ADAPTER.createCanvas();let i=e.getContext("webgl",t)||e.getContext("experimental-webgl",t);const r=!!((s=i==null?void 0:i.getContextAttributes())!=null&&s.stencil);if(i){const n=i.getExtension("WEBGL_lose_context");n&&n.loseContext()}return i=null,r}catch(e){return!1}}()),yr}var ec={grad:.9,turn:360,rad:360/(2*Math.PI)},re=function(s){return typeof s=="string"?s.length>0:typeof s=="number"},ft=function(s,t,e){return t===void 0&&(t=0),e===void 0&&(e=Math.pow(10,t)),Math.round(e*s)/e+0},Dt=function(s,t,e){return t===void 0&&(t=0),e===void 0&&(e=1),s>e?e:s>t?s:t},Na=function(s){return(s=isFinite(s)?s%360:0)>0?s:s+360},La=function(s){return{r:Dt(s.r,0,255),g:Dt(s.g,0,255),b:Dt(s.b,0,255),a:Dt(s.a)}},xr=function(s){return{r:ft(s.r),g:ft(s.g),b:ft(s.b),a:ft(s.a,3)}},ic=/^#([0-9a-f]{3,8})$/i,as=function(s){var t=s.toString(16);return t.length<2?"0"+t:t},Ua=function(s){var t=s.r,e=s.g,i=s.b,r=s.a,n=Math.max(t,e,i),a=n-Math.min(t,e,i),o=a?n===t?(e-i)/a:n===e?2+(i-t)/a:4+(t-e)/a:0;return{h:60*(o<0?o+6:o),s:n?a/n*100:0,v:n/255*100,a:r}},ka=function(s){var t=s.h,e=s.s,i=s.v,r=s.a;t=t/360*6,e/=100,i/=100;var n=Math.floor(t),a=i*(1-e),o=i*(1-(t-n)*e),h=i*(1-(1-t+n)*e),l=n%6;return{r:255*[i,o,a,a,h,i][l],g:255*[h,i,i,o,a,a][l],b:255*[a,a,h,i,i,o][l],a:r}},Ga=function(s){return{h:Na(s.h),s:Dt(s.s,0,100),l:Dt(s.l,0,100),a:Dt(s.a)}},$a=function(s){return{h:ft(s.h),s:ft(s.s),l:ft(s.l),a:ft(s.a,3)}},Ha=function(s){return ka((e=(t=s).s,{h:t.h,s:(e*=((i=t.l)<50?i:100-i)/100)>0?2*e/(i+e)*100:0,v:i+e,a:t.a}));var t,e,i},fi=function(s){return{h:(t=Ua(s)).h,s:(r=(200-(e=t.s))*(i=t.v)/100)>0&&r<200?e*i/100/(r<=100?r:200-r)*100:0,l:r/2,a:t.a};var t,e,i,r},sc=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,rc=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,nc=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,ac=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,br={string:[[function(s){var t=ic.exec(s);return t?(s=t[1]).length<=4?{r:parseInt(s[0]+s[0],16),g:parseInt(s[1]+s[1],16),b:parseInt(s[2]+s[2],16),a:s.length===4?ft(parseInt(s[3]+s[3],16)/255,2):1}:s.length===6||s.length===8?{r:parseInt(s.substr(0,2),16),g:parseInt(s.substr(2,2),16),b:parseInt(s.substr(4,2),16),a:s.length===8?ft(parseInt(s.substr(6,2),16)/255,2):1}:null:null},"hex"],[function(s){var t=nc.exec(s)||ac.exec(s);return t?t[2]!==t[4]||t[4]!==t[6]?null:La({r:Number(t[1])/(t[2]?100/255:1),g:Number(t[3])/(t[4]?100/255:1),b:Number(t[5])/(t[6]?100/255:1),a:t[7]===void 0?1:Number(t[7])/(t[8]?100:1)}):null},"rgb"],[function(s){var t=sc.exec(s)||rc.exec(s);if(!t)return null;var e,i,r=Ga({h:(e=t[1],i=t[2],i===void 0&&(i="deg"),Number(e)*(ec[i]||1)),s:Number(t[3]),l:Number(t[4]),a:t[5]===void 0?1:Number(t[5])/(t[6]?100:1)});return Ha(r)},"hsl"]],object:[[function(s){var t=s.r,e=s.g,i=s.b,r=s.a,n=r===void 0?1:r;return re(t)&&re(e)&&re(i)?La({r:Number(t),g:Number(e),b:Number(i),a:Number(n)}):null},"rgb"],[function(s){var t=s.h,e=s.s,i=s.l,r=s.a,n=r===void 0?1:r;if(!re(t)||!re(e)||!re(i))return null;var a=Ga({h:Number(t),s:Number(e),l:Number(i),a:Number(n)});return Ha(a)},"hsl"],[function(s){var t=s.h,e=s.s,i=s.v,r=s.a,n=r===void 0?1:r;if(!re(t)||!re(e)||!re(i))return null;var a=function(o){return{h:Na(o.h),s:Dt(o.s,0,100),v:Dt(o.v,0,100),a:Dt(o.a)}}({h:Number(t),s:Number(e),v:Number(i),a:Number(n)});return ka(a)},"hsv"]]},Va=function(s,t){for(var e=0;e=.5},s.prototype.toHex=function(){return t=xr(this.rgba),e=t.r,i=t.g,r=t.b,a=(n=t.a)<1?as(ft(255*n)):"","#"+as(e)+as(i)+as(r)+a;var t,e,i,r,n,a},s.prototype.toRgb=function(){return xr(this.rgba)},s.prototype.toRgbString=function(){return t=xr(this.rgba),e=t.r,i=t.g,r=t.b,(n=t.a)<1?"rgba("+e+", "+i+", "+r+", "+n+")":"rgb("+e+", "+i+", "+r+")";var t,e,i,r,n},s.prototype.toHsl=function(){return $a(fi(this.rgba))},s.prototype.toHslString=function(){return t=$a(fi(this.rgba)),e=t.h,i=t.s,r=t.l,(n=t.a)<1?"hsla("+e+", "+i+"%, "+r+"%, "+n+")":"hsl("+e+", "+i+"%, "+r+"%)";var t,e,i,r,n},s.prototype.toHsv=function(){return t=Ua(this.rgba),{h:ft(t.h),s:ft(t.s),v:ft(t.v),a:ft(t.a,3)};var t},s.prototype.invert=function(){return qt({r:255-(t=this.rgba).r,g:255-t.g,b:255-t.b,a:t.a});var t},s.prototype.saturate=function(t){return t===void 0&&(t=.1),qt(Tr(this.rgba,t))},s.prototype.desaturate=function(t){return t===void 0&&(t=.1),qt(Tr(this.rgba,-t))},s.prototype.grayscale=function(){return qt(Tr(this.rgba,-1))},s.prototype.lighten=function(t){return t===void 0&&(t=.1),qt(ja(this.rgba,t))},s.prototype.darken=function(t){return t===void 0&&(t=.1),qt(ja(this.rgba,-t))},s.prototype.rotate=function(t){return t===void 0&&(t=15),this.hue(this.hue()+t)},s.prototype.alpha=function(t){return typeof t=="number"?qt({r:(e=this.rgba).r,g:e.g,b:e.b,a:t}):ft(this.rgba.a,3);var e},s.prototype.hue=function(t){var e=fi(this.rgba);return typeof t=="number"?qt({h:t,s:e.s,l:e.l,a:e.a}):ft(e.h)},s.prototype.isEqual=function(t){return this.toHex()===qt(t).toHex()},s}(),qt=function(s){return s instanceof os?s:new os(s)},za=[],oc=function(s){s.forEach(function(t){za.indexOf(t)<0&&(t(os,br),za.push(t))})},ig=function(){return new os({r:255*Math.random(),g:255*Math.random(),b:255*Math.random()})};function hc(s,t){var e={white:"#ffffff",bisque:"#ffe4c4",blue:"#0000ff",cadetblue:"#5f9ea0",chartreuse:"#7fff00",chocolate:"#d2691e",coral:"#ff7f50",antiquewhite:"#faebd7",aqua:"#00ffff",azure:"#f0ffff",whitesmoke:"#f5f5f5",papayawhip:"#ffefd5",plum:"#dda0dd",blanchedalmond:"#ffebcd",black:"#000000",gold:"#ffd700",goldenrod:"#daa520",gainsboro:"#dcdcdc",cornsilk:"#fff8dc",cornflowerblue:"#6495ed",burlywood:"#deb887",aquamarine:"#7fffd4",beige:"#f5f5dc",crimson:"#dc143c",cyan:"#00ffff",darkblue:"#00008b",darkcyan:"#008b8b",darkgoldenrod:"#b8860b",darkkhaki:"#bdb76b",darkgray:"#a9a9a9",darkgreen:"#006400",darkgrey:"#a9a9a9",peachpuff:"#ffdab9",darkmagenta:"#8b008b",darkred:"#8b0000",darkorchid:"#9932cc",darkorange:"#ff8c00",darkslateblue:"#483d8b",gray:"#808080",darkslategray:"#2f4f4f",darkslategrey:"#2f4f4f",deeppink:"#ff1493",deepskyblue:"#00bfff",wheat:"#f5deb3",firebrick:"#b22222",floralwhite:"#fffaf0",ghostwhite:"#f8f8ff",darkviolet:"#9400d3",magenta:"#ff00ff",green:"#008000",dodgerblue:"#1e90ff",grey:"#808080",honeydew:"#f0fff0",hotpink:"#ff69b4",blueviolet:"#8a2be2",forestgreen:"#228b22",lawngreen:"#7cfc00",indianred:"#cd5c5c",indigo:"#4b0082",fuchsia:"#ff00ff",brown:"#a52a2a",maroon:"#800000",mediumblue:"#0000cd",lightcoral:"#f08080",darkturquoise:"#00ced1",lightcyan:"#e0ffff",ivory:"#fffff0",lightyellow:"#ffffe0",lightsalmon:"#ffa07a",lightseagreen:"#20b2aa",linen:"#faf0e6",mediumaquamarine:"#66cdaa",lemonchiffon:"#fffacd",lime:"#00ff00",khaki:"#f0e68c",mediumseagreen:"#3cb371",limegreen:"#32cd32",mediumspringgreen:"#00fa9a",lightskyblue:"#87cefa",lightblue:"#add8e6",midnightblue:"#191970",lightpink:"#ffb6c1",mistyrose:"#ffe4e1",moccasin:"#ffe4b5",mintcream:"#f5fffa",lightslategray:"#778899",lightslategrey:"#778899",navajowhite:"#ffdead",navy:"#000080",mediumvioletred:"#c71585",powderblue:"#b0e0e6",palegoldenrod:"#eee8aa",oldlace:"#fdf5e6",paleturquoise:"#afeeee",mediumturquoise:"#48d1cc",mediumorchid:"#ba55d3",rebeccapurple:"#663399",lightsteelblue:"#b0c4de",mediumslateblue:"#7b68ee",thistle:"#d8bfd8",tan:"#d2b48c",orchid:"#da70d6",mediumpurple:"#9370db",purple:"#800080",pink:"#ffc0cb",skyblue:"#87ceeb",springgreen:"#00ff7f",palegreen:"#98fb98",red:"#ff0000",yellow:"#ffff00",slateblue:"#6a5acd",lavenderblush:"#fff0f5",peru:"#cd853f",palevioletred:"#db7093",violet:"#ee82ee",teal:"#008080",slategray:"#708090",slategrey:"#708090",aliceblue:"#f0f8ff",darkseagreen:"#8fbc8f",darkolivegreen:"#556b2f",greenyellow:"#adff2f",seagreen:"#2e8b57",seashell:"#fff5ee",tomato:"#ff6347",silver:"#c0c0c0",sienna:"#a0522d",lavender:"#e6e6fa",lightgreen:"#90ee90",orange:"#ffa500",orangered:"#ff4500",steelblue:"#4682b4",royalblue:"#4169e1",turquoise:"#40e0d0",yellowgreen:"#9acd32",salmon:"#fa8072",saddlebrown:"#8b4513",sandybrown:"#f4a460",rosybrown:"#bc8f8f",darksalmon:"#e9967a",lightgoldenrodyellow:"#fafad2",snow:"#fffafa",lightgrey:"#d3d3d3",lightgray:"#d3d3d3",dimgray:"#696969",dimgrey:"#696969",olivedrab:"#6b8e23",olive:"#808000"},i={};for(var r in e)i[e[r]]=r;var n={};s.prototype.toName=function(a){if(!(this.rgba.a||this.rgba.r||this.rgba.g||this.rgba.b))return"transparent";var o,h,l=i[this.toHex()];if(l)return l;if(a!=null&&a.closest){var u=this.toRgb(),c=1/0,d="black";if(!n.length)for(var f in e)n[f]=new s(e[f]).toRgb();for(var p in e){var m=(o=u,h=n[p],Math.pow(o.r-h.r,2)+Math.pow(o.g-h.g,2)+Math.pow(o.b-h.b,2));mt in s?lc(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,dc=(s,t)=>{for(var e in t||(t={}))uc.call(t,e)&&Ya(s,e,t[e]);if(Wa)for(var e of Wa(t))cc.call(t,e)&&Ya(s,e,t[e]);return s};oc([hc]);const ze=class ir{constructor(t=16777215){this._value=null,this._components=new Float32Array(4),this._components.fill(1),this._int=16777215,this.value=t}get red(){return this._components[0]}get green(){return this._components[1]}get blue(){return this._components[2]}get alpha(){return this._components[3]}setValue(t){return this.value=t,this}set value(t){if(t instanceof ir)this._value=this.cloneSource(t._value),this._int=t._int,this._components.set(t._components);else{if(t===null)throw new Error("Cannot set PIXI.Color#value to null");(this._value===null||!this.isSourceEqual(this._value,t))&&(this.normalize(t),this._value=this.cloneSource(t))}}get value(){return this._value}cloneSource(t){return typeof t=="string"||typeof t=="number"||t instanceof Number||t===null?t:Array.isArray(t)||ArrayBuffer.isView(t)?t.slice(0):typeof t=="object"&&t!==null?dc({},t):t}isSourceEqual(t,e){const i=typeof t;if(i!==typeof e)return!1;if(i==="number"||i==="string"||t instanceof Number)return t===e;if(Array.isArray(t)&&Array.isArray(e)||ArrayBuffer.isView(t)&&ArrayBuffer.isView(e))return t.length!==e.length?!1:t.every((r,n)=>r===e[n]);if(t!==null&&e!==null){const r=Object.keys(t),n=Object.keys(e);return r.length!==n.length?!1:r.every(a=>t[a]===e[a])}return t===e}toRgba(){const[t,e,i,r]=this._components;return{r:t,g:e,b:i,a:r}}toRgb(){const[t,e,i]=this._components;return{r:t,g:e,b:i}}toRgbaString(){const[t,e,i]=this.toUint8RgbArray();return`rgba(${t},${e},${i},${this.alpha})`}toUint8RgbArray(t){const[e,i,r]=this._components;return t=t!=null?t:[],t[0]=Math.round(e*255),t[1]=Math.round(i*255),t[2]=Math.round(r*255),t}toRgbArray(t){t=t!=null?t:[];const[e,i,r]=this._components;return t[0]=e,t[1]=i,t[2]=r,t}toNumber(){return this._int}toLittleEndianNumber(){const t=this._int;return(t>>16)+(t&65280)+((t&255)<<16)}multiply(t){const[e,i,r,n]=ir.temp.setValue(t)._components;return this._components[0]*=e,this._components[1]*=i,this._components[2]*=r,this._components[3]*=n,this.refreshInt(),this._value=null,this}premultiply(t,e=!0){return e&&(this._components[0]*=t,this._components[1]*=t,this._components[2]*=t),this._components[3]=t,this.refreshInt(),this._value=null,this}toPremultiplied(t,e=!0){if(t===1)return(255<<24)+this._int;if(t===0)return e?0:this._int;let i=this._int>>16&255,r=this._int>>8&255,n=this._int&255;return e&&(i=i*t+.5|0,r=r*t+.5|0,n=n*t+.5|0),(t*255<<24)+(i<<16)+(r<<8)+n}toHex(){const t=this._int.toString(16);return`#${"000000".substring(0,6-t.length)+t}`}toHexa(){const t=Math.round(this._components[3]*255).toString(16);return this.toHex()+"00".substring(0,2-t.length)+t}setAlpha(t){return this._components[3]=this._clamp(t),this}round(t){const[e,i,r]=this._components;return this._components[0]=Math.round(e*t)/t,this._components[1]=Math.round(i*t)/t,this._components[2]=Math.round(r*t)/t,this.refreshInt(),this._value=null,this}toArray(t){t=t!=null?t:[];const[e,i,r,n]=this._components;return t[0]=e,t[1]=i,t[2]=r,t[3]=n,t}normalize(t){let e,i,r,n;if((typeof t=="number"||t instanceof Number)&&t>=0&&t<=16777215){const a=t;e=(a>>16&255)/255,i=(a>>8&255)/255,r=(a&255)/255,n=1}else if((Array.isArray(t)||t instanceof Float32Array)&&t.length>=3&&t.length<=4)t=this._clamp(t),[e,i,r,n=1]=t;else if((t instanceof Uint8Array||t instanceof Uint8ClampedArray)&&t.length>=3&&t.length<=4)t=this._clamp(t,0,255),[e,i,r,n=255]=t,e/=255,i/=255,r/=255,n/=255;else if(typeof t=="string"||typeof t=="object"){if(typeof t=="string"){const o=ir.HEX_PATTERN.exec(t);o&&(t=`#${o[2]}`)}const a=qt(t);a.isValid()&&({r:e,g:i,b:r,a:n}=a.rgba,e/=255,i/=255,r/=255)}if(e!==void 0)this._components[0]=e,this._components[1]=i,this._components[2]=r,this._components[3]=n,this.refreshInt();else throw new Error(`Unable to convert color ${t}`)}refreshInt(){this._clamp(this._components);const[t,e,i]=this._components;this._int=(t*255<<16)+(e*255<<8)+(i*255|0)}_clamp(t,e=0,i=1){return typeof t=="number"?Math.min(Math.max(t,e),i):(t.forEach((r,n)=>{t[n]=Math.min(Math.max(r,e),i)}),t)}};ze.shared=new ze,ze.temp=new ze,ze.HEX_PATTERN=/^(#|0x)?(([a-f0-9]{3}){1,2}([a-f0-9]{2})?)$/i;let Z=ze;function fc(s,t=[]){return Z.shared.setValue(s).toRgbArray(t)}function qa(s){return Z.shared.setValue(s).toHex()}function pc(s){return Z.shared.setValue(s).toNumber()}function Ka(s){return Z.shared.setValue(s).toNumber()}function mc(){const s=[],t=[];for(let i=0;i<32;i++)s[i]=i,t[i]=i;s[H.NORMAL_NPM]=H.NORMAL,s[H.ADD_NPM]=H.ADD,s[H.SCREEN_NPM]=H.SCREEN,t[H.NORMAL]=H.NORMAL_NPM,t[H.ADD]=H.ADD_NPM,t[H.SCREEN]=H.SCREEN_NPM;const e=[];return e.push(t),e.push(s),e}const Ar=mc();function wr(s,t){return Ar[t?1:0][s]}function gc(s,t,e,i=!0){return Z.shared.setValue(s).premultiply(t,i).toArray(e!=null?e:new Float32Array(4))}function _c(s,t){return Z.shared.setValue(s).toPremultiplied(t)}function vc(s,t,e,i=!0){return Z.shared.setValue(s).premultiply(t,i).toArray(e!=null?e:new Float32Array(4))}const Za=/^\s*data:(?:([\w-]+)\/([\w+.-]+))?(?:;charset=([\w-]+))?(?:;(base64))?,(.*)/i;function Qa(s,t=null){const e=s*6;if(t=t||new Uint16Array(e),t.length!==e)throw new Error(`Out buffer length is incorrect, got ${t.length} and expected ${e}`);for(let i=0,r=0;i>>1,s|=s>>>2,s|=s>>>4,s|=s>>>8,s|=s>>>16,s+1}function Sr(s){return!(s&s-1)&&!!s}function Ir(s){let t=(s>65535?1:0)<<4;s>>>=t;let e=(s>255?1:0)<<3;return s>>>=e,t|=e,e=(s>15?1:0)<<2,s>>>=e,t|=e,e=(s>3?1:0)<<1,s>>>=e,t|=e,t|s>>1}function Ce(s,t,e){const i=s.length;let r;if(t>=i||e===0)return;e=t+e>i?i-t:e;const n=i-e;for(r=t;rt in s?Sc(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,oo=(s,t)=>{for(var e in t||(t={}))Cc.call(t,e)&&ao(s,e,t[e]);if(no)for(var e of no(t))Pc.call(t,e)&&ao(s,e,t[e]);return s},Mc=(s,t)=>Ic(s,Rc(t)),R=(s=>(s.Renderer="renderer",s.Application="application",s.RendererSystem="renderer-webgl-system",s.RendererPlugin="renderer-webgl-plugin",s.CanvasRendererSystem="renderer-canvas-system",s.CanvasRendererPlugin="renderer-canvas-plugin",s.Asset="asset",s.LoadParser="load-parser",s.ResolveParser="resolve-parser",s.CacheParser="cache-parser",s.DetectionParser="detection-parser",s))(R||{});const Mr=s=>{if(typeof s=="function"||typeof s=="object"&&s.extension){const t=typeof s.extension!="object"?{type:s.extension}:s.extension;s=Mc(oo({},t),{ref:s})}if(typeof s=="object")s=oo({},s);else throw new Error("Invalid extension type");return typeof s.type=="string"&&(s.type=[s.type]),s},ho=(s,t)=>{var e;return(e=Mr(s).priority)!=null?e:t},L={_addHandlers:{},_removeHandlers:{},_queue:{},remove(...s){return s.map(Mr).forEach(t=>{t.type.forEach(e=>{var i,r;return(r=(i=this._removeHandlers)[e])==null?void 0:r.call(i,t)})}),this},add(...s){return s.map(Mr).forEach(t=>{t.type.forEach(e=>{const i=this._addHandlers,r=this._queue;i[e]?i[e](t):(r[e]=r[e]||[],r[e].push(t))})}),this},handle(s,t,e){const i=this._addHandlers,r=this._removeHandlers;i[s]=t,r[s]=e;const n=this._queue;return n[s]&&(n[s].forEach(a=>t(a)),delete n[s]),this},handleByMap(s,t){return this.handle(s,e=>{t[e.name]=e.ref},e=>{delete t[e.name]})},handleByList(s,t,e=-1){return this.handle(s,i=>{t.includes(i.ref)||(t.push(i.ref),t.sort((r,n)=>ho(n,e)-ho(r,e)))},i=>{const r=t.indexOf(i.ref);r!==-1&&t.splice(r,1)})}};class ls{constructor(t){typeof t=="number"?this.rawBinaryData=new ArrayBuffer(t):t instanceof Uint8Array?this.rawBinaryData=t.buffer:this.rawBinaryData=t,this.uint32View=new Uint32Array(this.rawBinaryData),this.float32View=new Float32Array(this.rawBinaryData)}get int8View(){return this._int8View||(this._int8View=new Int8Array(this.rawBinaryData)),this._int8View}get uint8View(){return this._uint8View||(this._uint8View=new Uint8Array(this.rawBinaryData)),this._uint8View}get int16View(){return this._int16View||(this._int16View=new Int16Array(this.rawBinaryData)),this._int16View}get uint16View(){return this._uint16View||(this._uint16View=new Uint16Array(this.rawBinaryData)),this._uint16View}get int32View(){return this._int32View||(this._int32View=new Int32Array(this.rawBinaryData)),this._int32View}view(t){return this[`${t}View`]}destroy(){this.rawBinaryData=null,this._int8View=null,this._uint8View=null,this._int16View=null,this._uint16View=null,this._int32View=null,this.uint32View=null,this.float32View=null}static sizeOf(t){switch(t){case"int8":case"uint8":return 1;case"int16":case"uint16":return 2;case"int32":case"uint32":case"float32":return 4;default:throw new Error(`${t} isn't a valid view type`)}}}const Dc=["precision mediump float;","void main(void){","float test = 0.1;","%forloop%","gl_FragColor = vec4(0.0);","}"].join(` +`);function Oc(s){let t="";for(let e=0;e0&&(t+=` +else `),e=0;--i){const r=us[i];if(r.test&&r.test(s,e))return new r(s,t)}throw new Error("Unrecognized source type to auto-detect Resource")}class St{constructor(t){this.items=[],this._name=t,this._aliasCount=0}emit(t,e,i,r,n,a,o,h){if(arguments.length>8)throw new Error("max arguments reached");const{name:l,items:u}=this;this._aliasCount++;for(let c=0,d=u.length;c0&&this.items.length>1&&(this._aliasCount=0,this.items=this.items.slice(0))}add(t){return t[this._name]&&(this.ensureNonAliasedItems(),this.remove(t),this.items.push(t)),this}remove(t){const e=this.items.indexOf(t);return e!==-1&&(this.ensureNonAliasedItems(),this.items.splice(e,1)),this}contains(t){return this.items.includes(t)}removeAll(){return this.ensureNonAliasedItems(),this.items.length=0,this}destroy(){this.removeAll(),this.items=null,this._name=null}get empty(){return this.items.length===0}get name(){return this._name}}Object.defineProperties(St.prototype,{dispatch:{value:St.prototype.emit},run:{value:St.prototype.emit}});class We{constructor(t=0,e=0){this._width=t,this._height=e,this.destroyed=!1,this.internal=!1,this.onResize=new St("setRealSize"),this.onUpdate=new St("update"),this.onError=new St("onError")}bind(t){this.onResize.add(t),this.onUpdate.add(t),this.onError.add(t),(this._width||this._height)&&this.onResize.emit(this._width,this._height)}unbind(t){this.onResize.remove(t),this.onUpdate.remove(t),this.onError.remove(t)}resize(t,e){(t!==this._width||e!==this._height)&&(this._width=t,this._height=e,this.onResize.emit(t,e))}get valid(){return!!this._width&&!!this._height}update(){this.destroyed||this.onUpdate.emit()}load(){return Promise.resolve(this)}get width(){return this._width}get height(){return this._height}style(t,e,i){return!1}dispose(){}destroy(){this.destroyed||(this.destroyed=!0,this.dispose(),this.onError.removeAll(),this.onError=null,this.onResize.removeAll(),this.onResize=null,this.onUpdate.removeAll(),this.onUpdate=null)}static test(t,e){return!1}}class mi extends We{constructor(t,e){var i;const{width:r,height:n}=e||{};if(!r||!n)throw new Error("BufferResource width or height invalid");super(r,n),this.data=t,this.unpackAlignment=(i=e.unpackAlignment)!=null?i:4}upload(t,e,i){const r=t.gl;r.pixelStorei(r.UNPACK_ALIGNMENT,this.unpackAlignment),r.pixelStorei(r.UNPACK_PREMULTIPLY_ALPHA_WEBGL,e.alphaMode===bt.UNPACK);const n=e.realWidth,a=e.realHeight;return i.width===n&&i.height===a?r.texSubImage2D(e.target,0,0,0,n,a,e.format,i.type,this.data):(i.width=n,i.height=a,r.texImage2D(e.target,0,i.internalFormat,n,a,0,e.format,i.type,this.data)),!0}dispose(){this.data=null}static test(t){return t===null||t instanceof Int8Array||t instanceof Uint8Array||t instanceof Uint8ClampedArray||t instanceof Int16Array||t instanceof Uint16Array||t instanceof Int32Array||t instanceof Uint32Array||t instanceof Float32Array}}var Bc=Object.defineProperty,uo=Object.getOwnPropertySymbols,Fc=Object.prototype.hasOwnProperty,Nc=Object.prototype.propertyIsEnumerable,co=(s,t,e)=>t in s?Bc(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,Lc=(s,t)=>{for(var e in t||(t={}))Fc.call(t,e)&&co(s,e,t[e]);if(uo)for(var e of uo(t))Nc.call(t,e)&&co(s,e,t[e]);return s};const Uc={scaleMode:zt.NEAREST,alphaMode:bt.NPM},kr=class ei extends Ve{constructor(t=null,e=null){super(),e=Object.assign({},ei.defaultOptions,e);const{alphaMode:i,mipmap:r,anisotropicLevel:n,scaleMode:a,width:o,height:h,wrapMode:l,format:u,type:c,target:d,resolution:f,resourceOptions:p}=e;t&&!(t instanceof We)&&(t=Ur(t,p),t.internal=!0),this.resolution=f||O.RESOLUTION,this.width=Math.round((o||0)*this.resolution)/this.resolution,this.height=Math.round((h||0)*this.resolution)/this.resolution,this._mipmap=r,this.anisotropicLevel=n,this._wrapMode=l,this._scaleMode=a,this.format=u,this.type=c,this.target=d,this.alphaMode=i,this.uid=ve(),this.touched=0,this.isPowerOfTwo=!1,this._refreshPOT(),this._glTextures={},this.dirtyId=0,this.dirtyStyleId=0,this.cacheId=null,this.valid=o>0&&h>0,this.textureCacheIds=[],this.destroyed=!1,this.resource=null,this._batchEnabled=0,this._batchLocation=0,this.parentTextureArray=null,this.setResource(t)}get realWidth(){return Math.round(this.width*this.resolution)}get realHeight(){return Math.round(this.height*this.resolution)}get mipmap(){return this._mipmap}set mipmap(t){this._mipmap!==t&&(this._mipmap=t,this.dirtyStyleId++)}get scaleMode(){return this._scaleMode}set scaleMode(t){this._scaleMode!==t&&(this._scaleMode=t,this.dirtyStyleId++)}get wrapMode(){return this._wrapMode}set wrapMode(t){this._wrapMode!==t&&(this._wrapMode=t,this.dirtyStyleId++)}setStyle(t,e){let i;return t!==void 0&&t!==this.scaleMode&&(this.scaleMode=t,i=!0),e!==void 0&&e!==this.mipmap&&(this.mipmap=e,i=!0),i&&this.dirtyStyleId++,this}setSize(t,e,i){return i=i||this.resolution,this.setRealSize(t*i,e*i,i)}setRealSize(t,e,i){return this.resolution=i||this.resolution,this.width=Math.round(t)/this.resolution,this.height=Math.round(e)/this.resolution,this._refreshPOT(),this.update(),this}_refreshPOT(){this.isPowerOfTwo=Sr(this.realWidth)&&Sr(this.realHeight)}setResolution(t){const e=this.resolution;return e===t?this:(this.resolution=t,this.valid&&(this.width=Math.round(this.width*e)/t,this.height=Math.round(this.height*e)/t,this.emit("update",this)),this._refreshPOT(),this)}setResource(t){if(this.resource===t)return this;if(this.resource)throw new Error("Resource can be set only once");return t.bind(this),this.resource=t,this}update(){this.valid?(this.dirtyId++,this.dirtyStyleId++,this.emit("update",this)):this.width>0&&this.height>0&&(this.valid=!0,this.emit("loaded",this),this.emit("update",this))}onError(t){this.emit("error",this,t)}destroy(){this.resource&&(this.resource.unbind(this),this.resource.internal&&this.resource.destroy(),this.resource=null),this.cacheId&&(delete wt[this.cacheId],delete Tt[this.cacheId],this.cacheId=null),this.valid=!1,this.dispose(),ei.removeFromCache(this),this.textureCacheIds=null,this.destroyed=!0,this.emit("destroyed",this),this.removeAllListeners()}dispose(){this.emit("dispose",this)}castToBaseTexture(){return this}static from(t,e,i=O.STRICT_TEXTURE_CACHE){const r=typeof t=="string";let n=null;if(r)n=t;else{if(!t._pixiId){const o=(e==null?void 0:e.pixiIdPrefix)||"pixiid";t._pixiId=`${o}_${ve()}`}n=t._pixiId}let a=wt[n];if(r&&i&&!a)throw new Error(`The cacheId "${n}" does not exist in BaseTextureCache.`);return a||(a=new ei(t,e),a.cacheId=n,ei.addToCache(a,n)),a}static fromBuffer(t,e,i,r){t=t||new Float32Array(e*i*4);const n=new mi(t,Lc({width:e,height:i},r==null?void 0:r.resourceOptions));let a,o;return t instanceof Float32Array?(a=A.RGBA,o=k.FLOAT):t instanceof Int32Array?(a=A.RGBA_INTEGER,o=k.INT):t instanceof Uint32Array?(a=A.RGBA_INTEGER,o=k.UNSIGNED_INT):t instanceof Int16Array?(a=A.RGBA_INTEGER,o=k.SHORT):t instanceof Uint16Array?(a=A.RGBA_INTEGER,o=k.UNSIGNED_SHORT):t instanceof Int8Array?(a=A.RGBA,o=k.BYTE):(a=A.RGBA,o=k.UNSIGNED_BYTE),n.internal=!0,new ei(n,Object.assign({},Uc,{type:o,format:a},r))}static addToCache(t,e){e&&(t.textureCacheIds.includes(e)||t.textureCacheIds.push(e),wt[e]&&wt[e]!==t&&console.warn(`BaseTexture added to the cache with an id [${e}] that already had an entry`),wt[e]=t)}static removeFromCache(t){if(typeof t=="string"){const e=wt[t];if(e){const i=e.textureCacheIds.indexOf(t);return i>-1&&e.textureCacheIds.splice(i,1),delete wt[t],e}}else if(t!=null&&t.textureCacheIds){for(let e=0;e1){for(let c=0;c(s[s.POLY=0]="POLY",s[s.RECT=1]="RECT",s[s.CIRC=2]="CIRC",s[s.ELIP=3]="ELIP",s[s.RREC=4]="RREC",s))(pt||{});class q{constructor(t=0,e=0){this.x=0,this.y=0,this.x=t,this.y=e}clone(){return new q(this.x,this.y)}copyFrom(t){return this.set(t.x,t.y),this}copyTo(t){return t.set(this.x,this.y),t}equals(t){return t.x===this.x&&t.y===this.y}set(t=0,e=t){return this.x=t,this.y=e,this}}const ds=[new q,new q,new q,new q];class j{constructor(t=0,e=0,i=0,r=0){this.x=Number(t),this.y=Number(e),this.width=Number(i),this.height=Number(r),this.type=pt.RECT}get left(){return this.x}get right(){return this.x+this.width}get top(){return this.y}get bottom(){return this.y+this.height}static get EMPTY(){return new j(0,0,0,0)}clone(){return new j(this.x,this.y,this.width,this.height)}copyFrom(t){return this.x=t.x,this.y=t.y,this.width=t.width,this.height=t.height,this}copyTo(t){return t.x=this.x,t.y=this.y,t.width=this.width,t.height=this.height,t}contains(t,e){return this.width<=0||this.height<=0?!1:t>=this.x&&t=this.y&&et.right?t.right:this.right)<=w)return!1;const F=this.yt.bottom?t.bottom:this.bottom)>F}const i=this.left,r=this.right,n=this.top,a=this.bottom;if(r<=i||a<=n)return!1;const o=ds[0].set(t.left,t.top),h=ds[1].set(t.left,t.bottom),l=ds[2].set(t.right,t.top),u=ds[3].set(t.right,t.bottom);if(l.x<=o.x||h.y<=o.y)return!1;const c=Math.sign(e.a*e.d-e.b*e.c);if(c===0||(e.apply(o,o),e.apply(h,h),e.apply(l,l),e.apply(u,u),Math.max(o.x,h.x,l.x,u.x)<=i||Math.min(o.x,h.x,l.x,u.x)>=r||Math.max(o.y,h.y,l.y,u.y)<=n||Math.min(o.y,h.y,l.y,u.y)>=a))return!1;const d=c*(h.y-o.y),f=c*(o.x-h.x),p=d*i+f*n,m=d*r+f*n,g=d*i+f*a,y=d*r+f*a;if(Math.max(p,m,g,y)<=d*o.x+f*o.y||Math.min(p,m,g,y)>=d*u.x+f*u.y)return!1;const b=c*(o.y-l.y),v=c*(l.x-o.x),x=b*i+v*n,E=b*r+v*n,M=b*i+v*a,S=b*r+v*a;return!(Math.max(x,E,M,S)<=b*o.x+v*o.y||Math.min(x,E,M,S)>=b*u.x+v*u.y)}pad(t=0,e=t){return this.x-=t,this.y-=e,this.width+=t*2,this.height+=e*2,this}fit(t){const e=Math.max(this.x,t.x),i=Math.min(this.x+this.width,t.x+t.width),r=Math.max(this.y,t.y),n=Math.min(this.y+this.height,t.y+t.height);return this.x=e,this.width=Math.max(i-e,0),this.y=r,this.height=Math.max(n-r,0),this}ceil(t=1,e=.001){const i=Math.ceil((this.x+this.width-e)*t)/t,r=Math.ceil((this.y+this.height-e)*t)/t;return this.x=Math.floor((this.x+e)*t)/t,this.y=Math.floor((this.y+e)*t)/t,this.width=i-this.x,this.height=r-this.y,this}enlarge(t){const e=Math.min(this.x,t.x),i=Math.max(this.x+this.width,t.x+t.width),r=Math.min(this.y,t.y),n=Math.max(this.y+this.height,t.y+t.height);return this.x=e,this.width=i-e,this.y=r,this.height=n-r,this}}class fs{constructor(t=0,e=0,i=0){this.x=t,this.y=e,this.radius=i,this.type=pt.CIRC}clone(){return new fs(this.x,this.y,this.radius)}contains(t,e){if(this.radius<=0)return!1;const i=this.radius*this.radius;let r=this.x-t,n=this.y-e;return r*=r,n*=n,r+n<=i}getBounds(){return new j(this.x-this.radius,this.y-this.radius,this.radius*2,this.radius*2)}}class ps{constructor(t=0,e=0,i=0,r=0){this.x=t,this.y=e,this.width=i,this.height=r,this.type=pt.ELIP}clone(){return new ps(this.x,this.y,this.width,this.height)}contains(t,e){if(this.width<=0||this.height<=0)return!1;let i=(t-this.x)/this.width,r=(e-this.y)/this.height;return i*=i,r*=r,i+r<=1}getBounds(){return new j(this.x-this.width,this.y-this.height,this.width,this.height)}}class Pe{constructor(...t){let e=Array.isArray(t[0])?t[0]:t;if(typeof e[0]!="number"){const i=[];for(let r=0,n=e.length;re!=u>e&&t<(l-o)*((e-h)/(u-h))+o&&(i=!i)}return i}}class ms{constructor(t=0,e=0,i=0,r=0,n=20){this.x=t,this.y=e,this.width=i,this.height=r,this.radius=n,this.type=pt.RREC}clone(){return new ms(this.x,this.y,this.width,this.height,this.radius)}contains(t,e){if(this.width<=0||this.height<=0)return!1;if(t>=this.x&&t<=this.x+this.width&&e>=this.y&&e<=this.y+this.height){const i=Math.max(0,Math.min(this.radius,Math.min(this.width,this.height)/2));if(e>=this.y+i&&e<=this.y+this.height-i||t>=this.x+i&&t<=this.x+this.width-i)return!0;let r=t-(this.x+i),n=e-(this.y+i);const a=i*i;if(r*r+n*n<=a||(r=t-(this.x+this.width-i),r*r+n*n<=a)||(n=e-(this.y+this.height-i),r*r+n*n<=a)||(r=t-(this.x+i),r*r+n*n<=a))return!0}return!1}}class tt{constructor(t=1,e=0,i=0,r=1,n=0,a=0){this.array=null,this.a=t,this.b=e,this.c=i,this.d=r,this.tx=n,this.ty=a}fromArray(t){this.a=t[0],this.b=t[1],this.c=t[3],this.d=t[4],this.tx=t[2],this.ty=t[5]}set(t,e,i,r,n,a){return this.a=t,this.b=e,this.c=i,this.d=r,this.tx=n,this.ty=a,this}toArray(t,e){this.array||(this.array=new Float32Array(9));const i=e||this.array;return t?(i[0]=this.a,i[1]=this.b,i[2]=0,i[3]=this.c,i[4]=this.d,i[5]=0,i[6]=this.tx,i[7]=this.ty,i[8]=1):(i[0]=this.a,i[1]=this.c,i[2]=this.tx,i[3]=this.b,i[4]=this.d,i[5]=this.ty,i[6]=0,i[7]=0,i[8]=1),i}apply(t,e){e=e||new q;const i=t.x,r=t.y;return e.x=this.a*i+this.c*r+this.tx,e.y=this.b*i+this.d*r+this.ty,e}applyInverse(t,e){e=e||new q;const i=1/(this.a*this.d+this.c*-this.b),r=t.x,n=t.y;return e.x=this.d*i*r+-this.c*i*n+(this.ty*this.c-this.tx*this.d)*i,e.y=this.a*i*n+-this.b*i*r+(-this.ty*this.a+this.tx*this.b)*i,e}translate(t,e){return this.tx+=t,this.ty+=e,this}scale(t,e){return this.a*=t,this.d*=e,this.c*=t,this.b*=e,this.tx*=t,this.ty*=e,this}rotate(t){const e=Math.cos(t),i=Math.sin(t),r=this.a,n=this.c,a=this.tx;return this.a=r*e-this.b*i,this.b=r*i+this.b*e,this.c=n*e-this.d*i,this.d=n*i+this.d*e,this.tx=a*e-this.ty*i,this.ty=a*i+this.ty*e,this}append(t){const e=this.a,i=this.b,r=this.c,n=this.d;return this.a=t.a*e+t.b*r,this.b=t.a*i+t.b*n,this.c=t.c*e+t.d*r,this.d=t.c*i+t.d*n,this.tx=t.tx*e+t.ty*r+this.tx,this.ty=t.tx*i+t.ty*n+this.ty,this}setTransform(t,e,i,r,n,a,o,h,l){return this.a=Math.cos(o+l)*n,this.b=Math.sin(o+l)*n,this.c=-Math.sin(o-h)*a,this.d=Math.cos(o-h)*a,this.tx=t-(i*this.a+r*this.c),this.ty=e-(i*this.b+r*this.d),this}prepend(t){const e=this.tx;if(t.a!==1||t.b!==0||t.c!==0||t.d!==1){const i=this.a,r=this.c;this.a=i*t.a+this.b*t.c,this.b=i*t.b+this.b*t.d,this.c=r*t.a+this.d*t.c,this.d=r*t.b+this.d*t.d}return this.tx=e*t.a+this.ty*t.c+t.tx,this.ty=e*t.b+this.ty*t.d+t.ty,this}decompose(t){const e=this.a,i=this.b,r=this.c,n=this.d,a=t.pivot,o=-Math.atan2(-r,n),h=Math.atan2(i,e),l=Math.abs(o+h);return l<1e-5||Math.abs(_i-l)<1e-5?(t.rotation=h,t.skew.x=t.skew.y=0):(t.rotation=0,t.skew.x=o,t.skew.y=h),t.scale.x=Math.sqrt(e*e+i*i),t.scale.y=Math.sqrt(r*r+n*n),t.position.x=this.tx+(a.x*e+a.y*r),t.position.y=this.ty+(a.x*i+a.y*n),t}invert(){const t=this.a,e=this.b,i=this.c,r=this.d,n=this.tx,a=t*r-e*i;return this.a=r/a,this.b=-e/a,this.c=-i/a,this.d=t/a,this.tx=(i*this.ty-r*n)/a,this.ty=-(t*this.ty-e*n)/a,this}identity(){return this.a=1,this.b=0,this.c=0,this.d=1,this.tx=0,this.ty=0,this}clone(){const t=new tt;return t.a=this.a,t.b=this.b,t.c=this.c,t.d=this.d,t.tx=this.tx,t.ty=this.ty,t}copyTo(t){return t.a=this.a,t.b=this.b,t.c=this.c,t.d=this.d,t.tx=this.tx,t.ty=this.ty,t}copyFrom(t){return this.a=t.a,this.b=t.b,this.c=t.c,this.d=t.d,this.tx=t.tx,this.ty=t.ty,this}static get IDENTITY(){return new tt}static get TEMP_MATRIX(){return new tt}}const Me=[1,1,0,-1,-1,-1,0,1,1,1,0,-1,-1,-1,0,1],De=[0,1,1,1,0,-1,-1,-1,0,1,1,1,0,-1,-1,-1],Oe=[0,-1,-1,-1,0,1,1,1,0,1,1,1,0,-1,-1,-1],Be=[1,1,0,-1,-1,-1,0,1,-1,-1,0,1,1,1,0,-1],$r=[],go=[],gs=Math.sign;function Xc(){for(let s=0;s<16;s++){const t=[];$r.push(t);for(let e=0;e<16;e++){const i=gs(Me[s]*Me[e]+Oe[s]*De[e]),r=gs(De[s]*Me[e]+Be[s]*De[e]),n=gs(Me[s]*Oe[e]+Oe[s]*Be[e]),a=gs(De[s]*Oe[e]+Be[s]*Be[e]);for(let o=0;o<16;o++)if(Me[o]===i&&De[o]===r&&Oe[o]===n&&Be[o]===a){t.push(o);break}}}for(let s=0;s<16;s++){const t=new tt;t.set(Me[s],De[s],Oe[s],Be[s],0,0),go.push(t)}}Xc();const et={E:0,SE:1,S:2,SW:3,W:4,NW:5,N:6,NE:7,MIRROR_VERTICAL:8,MAIN_DIAGONAL:10,MIRROR_HORIZONTAL:12,REVERSE_DIAGONAL:14,uX:s=>Me[s],uY:s=>De[s],vX:s=>Oe[s],vY:s=>Be[s],inv:s=>s&8?s&15:-s&7,add:(s,t)=>$r[s][t],sub:(s,t)=>$r[s][et.inv(t)],rotate180:s=>s^4,isVertical:s=>(s&3)===2,byDirection:(s,t)=>Math.abs(s)*2<=Math.abs(t)?t>=0?et.S:et.N:Math.abs(t)*2<=Math.abs(s)?s>0?et.E:et.W:t>0?s>0?et.SE:et.SW:s>0?et.NE:et.NW,matrixAppendRotationInv:(s,t,e=0,i=0)=>{const r=go[et.inv(t)];r.tx=e,r.ty=i,s.append(r)}};class oe{constructor(t,e,i=0,r=0){this._x=i,this._y=r,this.cb=t,this.scope=e}clone(t=this.cb,e=this.scope){return new oe(t,e,this._x,this._y)}set(t=0,e=t){return(this._x!==t||this._y!==e)&&(this._x=t,this._y=e,this.cb.call(this.scope)),this}copyFrom(t){return(this._x!==t.x||this._y!==t.y)&&(this._x=t.x,this._y=t.y,this.cb.call(this.scope)),this}copyTo(t){return t.set(this._x,this._y),t}equals(t){return t.x===this._x&&t.y===this._y}get x(){return this._x}set x(t){this._x!==t&&(this._x=t,this.cb.call(this.scope))}get y(){return this._y}set y(t){this._y!==t&&(this._y=t,this.cb.call(this.scope))}}const Hr=class{constructor(){this.worldTransform=new tt,this.localTransform=new tt,this.position=new oe(this.onChange,this,0,0),this.scale=new oe(this.onChange,this,1,1),this.pivot=new oe(this.onChange,this,0,0),this.skew=new oe(this.updateSkew,this,0,0),this._rotation=0,this._cx=1,this._sx=0,this._cy=0,this._sy=1,this._localID=0,this._currentLocalID=0,this._worldID=0,this._parentID=0}onChange(){this._localID++}updateSkew(){this._cx=Math.cos(this._rotation+this.skew.y),this._sx=Math.sin(this._rotation+this.skew.y),this._cy=-Math.sin(this._rotation-this.skew.x),this._sy=Math.cos(this._rotation-this.skew.x),this._localID++}updateLocalTransform(){const t=this.localTransform;this._localID!==this._currentLocalID&&(t.a=this._cx*this.scale.x,t.b=this._sx*this.scale.x,t.c=this._cy*this.scale.y,t.d=this._sy*this.scale.y,t.tx=this.position.x-(this.pivot.x*t.a+this.pivot.y*t.c),t.ty=this.position.y-(this.pivot.x*t.b+this.pivot.y*t.d),this._currentLocalID=this._localID,this._parentID=-1)}updateTransform(t){const e=this.localTransform;if(this._localID!==this._currentLocalID&&(e.a=this._cx*this.scale.x,e.b=this._sx*this.scale.x,e.c=this._cy*this.scale.y,e.d=this._sy*this.scale.y,e.tx=this.position.x-(this.pivot.x*e.a+this.pivot.y*e.c),e.ty=this.position.y-(this.pivot.x*e.b+this.pivot.y*e.d),this._currentLocalID=this._localID,this._parentID=-1),this._parentID!==t._worldID){const i=t.worldTransform,r=this.worldTransform;r.a=e.a*i.a+e.b*i.c,r.b=e.a*i.b+e.b*i.d,r.c=e.c*i.a+e.d*i.c,r.d=e.c*i.b+e.d*i.d,r.tx=e.tx*i.a+e.ty*i.c+i.tx,r.ty=e.tx*i.b+e.ty*i.d+i.ty,this._parentID=t._worldID,this._worldID++}}setFromMatrix(t){t.decompose(this),this._localID++}get rotation(){return this._rotation}set rotation(t){this._rotation!==t&&(this._rotation=t,this.updateSkew())}};Hr.IDENTITY=new Hr;let _s=Hr;var jc=`varying vec2 vTextureCoord; + +uniform sampler2D uSampler; + +void main(void){ + gl_FragColor *= texture2D(uSampler, vTextureCoord); +}`,zc=`attribute vec2 aVertexPosition; +attribute vec2 aTextureCoord; + +uniform mat3 projectionMatrix; + +varying vec2 vTextureCoord; + +void main(void){ + gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); + vTextureCoord = aTextureCoord; +} +`;function _o(s,t,e){const i=s.createShader(t);return s.shaderSource(i,e),s.compileShader(i),i}function Vr(s){const t=new Array(s);for(let e=0;es.type==="float"&&s.size===1&&!s.isArray,code:s=>` + if(uv["${s}"] !== ud["${s}"].value) + { + ud["${s}"].value = uv["${s}"] + gl.uniform1f(ud["${s}"].location, uv["${s}"]) + } + `},{test:(s,t)=>(s.type==="sampler2D"||s.type==="samplerCube"||s.type==="sampler2DArray")&&s.size===1&&!s.isArray&&(t==null||t.castToBaseTexture!==void 0),code:s=>`t = syncData.textureCount++; + + renderer.texture.bind(uv["${s}"], t); + + if(ud["${s}"].value !== t) + { + ud["${s}"].value = t; + gl.uniform1i(ud["${s}"].location, t); +; // eslint-disable-line max-len + }`},{test:(s,t)=>s.type==="mat3"&&s.size===1&&!s.isArray&&t.a!==void 0,code:s=>` + gl.uniformMatrix3fv(ud["${s}"].location, false, uv["${s}"].toArray(true)); + `,codeUbo:s=>` + var ${s}_matrix = uv.${s}.toArray(true); + + data[offset] = ${s}_matrix[0]; + data[offset+1] = ${s}_matrix[1]; + data[offset+2] = ${s}_matrix[2]; + + data[offset + 4] = ${s}_matrix[3]; + data[offset + 5] = ${s}_matrix[4]; + data[offset + 6] = ${s}_matrix[5]; + + data[offset + 8] = ${s}_matrix[6]; + data[offset + 9] = ${s}_matrix[7]; + data[offset + 10] = ${s}_matrix[8]; + `},{test:(s,t)=>s.type==="vec2"&&s.size===1&&!s.isArray&&t.x!==void 0,code:s=>` + cv = ud["${s}"].value; + v = uv["${s}"]; + + if(cv[0] !== v.x || cv[1] !== v.y) + { + cv[0] = v.x; + cv[1] = v.y; + gl.uniform2f(ud["${s}"].location, v.x, v.y); + }`,codeUbo:s=>` + v = uv.${s}; + + data[offset] = v.x; + data[offset+1] = v.y; + `},{test:s=>s.type==="vec2"&&s.size===1&&!s.isArray,code:s=>` + cv = ud["${s}"].value; + v = uv["${s}"]; + + if(cv[0] !== v[0] || cv[1] !== v[1]) + { + cv[0] = v[0]; + cv[1] = v[1]; + gl.uniform2f(ud["${s}"].location, v[0], v[1]); + } + `},{test:(s,t)=>s.type==="vec4"&&s.size===1&&!s.isArray&&t.width!==void 0,code:s=>` + cv = ud["${s}"].value; + v = uv["${s}"]; + + if(cv[0] !== v.x || cv[1] !== v.y || cv[2] !== v.width || cv[3] !== v.height) + { + cv[0] = v.x; + cv[1] = v.y; + cv[2] = v.width; + cv[3] = v.height; + gl.uniform4f(ud["${s}"].location, v.x, v.y, v.width, v.height) + }`,codeUbo:s=>` + v = uv.${s}; + + data[offset] = v.x; + data[offset+1] = v.y; + data[offset+2] = v.width; + data[offset+3] = v.height; + `},{test:(s,t)=>s.type==="vec4"&&s.size===1&&!s.isArray&&t.red!==void 0,code:s=>` + cv = ud["${s}"].value; + v = uv["${s}"]; + + if(cv[0] !== v.red || cv[1] !== v.green || cv[2] !== v.blue || cv[3] !== v.alpha) + { + cv[0] = v.red; + cv[1] = v.green; + cv[2] = v.blue; + cv[3] = v.alpha; + gl.uniform4f(ud["${s}"].location, v.red, v.green, v.blue, v.alpha) + }`,codeUbo:s=>` + v = uv.${s}; + + data[offset] = v.red; + data[offset+1] = v.green; + data[offset+2] = v.blue; + data[offset+3] = v.alpha; + `},{test:(s,t)=>s.type==="vec3"&&s.size===1&&!s.isArray&&t.red!==void 0,code:s=>` + cv = ud["${s}"].value; + v = uv["${s}"]; + + if(cv[0] !== v.red || cv[1] !== v.green || cv[2] !== v.blue || cv[3] !== v.a) + { + cv[0] = v.red; + cv[1] = v.green; + cv[2] = v.blue; + + gl.uniform3f(ud["${s}"].location, v.red, v.green, v.blue) + }`,codeUbo:s=>` + v = uv.${s}; + + data[offset] = v.red; + data[offset+1] = v.green; + data[offset+2] = v.blue; + `},{test:s=>s.type==="vec4"&&s.size===1&&!s.isArray,code:s=>` + cv = ud["${s}"].value; + v = uv["${s}"]; + + if(cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3]) + { + cv[0] = v[0]; + cv[1] = v[1]; + cv[2] = v[2]; + cv[3] = v[3]; + + gl.uniform4f(ud["${s}"].location, v[0], v[1], v[2], v[3]) + }`}],Wc={float:` + if (cv !== v) + { + cu.value = v; + gl.uniform1f(location, v); + }`,vec2:` + if (cv[0] !== v[0] || cv[1] !== v[1]) + { + cv[0] = v[0]; + cv[1] = v[1]; + + gl.uniform2f(location, v[0], v[1]) + }`,vec3:` + if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2]) + { + cv[0] = v[0]; + cv[1] = v[1]; + cv[2] = v[2]; + + gl.uniform3f(location, v[0], v[1], v[2]) + }`,vec4:` + if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3]) + { + cv[0] = v[0]; + cv[1] = v[1]; + cv[2] = v[2]; + cv[3] = v[3]; + + gl.uniform4f(location, v[0], v[1], v[2], v[3]); + }`,int:` + if (cv !== v) + { + cu.value = v; + + gl.uniform1i(location, v); + }`,ivec2:` + if (cv[0] !== v[0] || cv[1] !== v[1]) + { + cv[0] = v[0]; + cv[1] = v[1]; + + gl.uniform2i(location, v[0], v[1]); + }`,ivec3:` + if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2]) + { + cv[0] = v[0]; + cv[1] = v[1]; + cv[2] = v[2]; + + gl.uniform3i(location, v[0], v[1], v[2]); + }`,ivec4:` + if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3]) + { + cv[0] = v[0]; + cv[1] = v[1]; + cv[2] = v[2]; + cv[3] = v[3]; + + gl.uniform4i(location, v[0], v[1], v[2], v[3]); + }`,uint:` + if (cv !== v) + { + cu.value = v; + + gl.uniform1ui(location, v); + }`,uvec2:` + if (cv[0] !== v[0] || cv[1] !== v[1]) + { + cv[0] = v[0]; + cv[1] = v[1]; + + gl.uniform2ui(location, v[0], v[1]); + }`,uvec3:` + if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2]) + { + cv[0] = v[0]; + cv[1] = v[1]; + cv[2] = v[2]; + + gl.uniform3ui(location, v[0], v[1], v[2]); + }`,uvec4:` + if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3]) + { + cv[0] = v[0]; + cv[1] = v[1]; + cv[2] = v[2]; + cv[3] = v[3]; + + gl.uniform4ui(location, v[0], v[1], v[2], v[3]); + }`,bool:` + if (cv !== v) + { + cu.value = v; + gl.uniform1i(location, v); + }`,bvec2:` + if (cv[0] != v[0] || cv[1] != v[1]) + { + cv[0] = v[0]; + cv[1] = v[1]; + + gl.uniform2i(location, v[0], v[1]); + }`,bvec3:` + if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2]) + { + cv[0] = v[0]; + cv[1] = v[1]; + cv[2] = v[2]; + + gl.uniform3i(location, v[0], v[1], v[2]); + }`,bvec4:` + if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3]) + { + cv[0] = v[0]; + cv[1] = v[1]; + cv[2] = v[2]; + cv[3] = v[3]; + + gl.uniform4i(location, v[0], v[1], v[2], v[3]); + }`,mat2:"gl.uniformMatrix2fv(location, false, v)",mat3:"gl.uniformMatrix3fv(location, false, v)",mat4:"gl.uniformMatrix4fv(location, false, v)",sampler2D:` + if (cv !== v) + { + cu.value = v; + + gl.uniform1i(location, v); + }`,samplerCube:` + if (cv !== v) + { + cu.value = v; + + gl.uniform1i(location, v); + }`,sampler2DArray:` + if (cv !== v) + { + cu.value = v; + + gl.uniform1i(location, v); + }`},Yc={float:"gl.uniform1fv(location, v)",vec2:"gl.uniform2fv(location, v)",vec3:"gl.uniform3fv(location, v)",vec4:"gl.uniform4fv(location, v)",mat4:"gl.uniformMatrix4fv(location, false, v)",mat3:"gl.uniformMatrix3fv(location, false, v)",mat2:"gl.uniformMatrix2fv(location, false, v)",int:"gl.uniform1iv(location, v)",ivec2:"gl.uniform2iv(location, v)",ivec3:"gl.uniform3iv(location, v)",ivec4:"gl.uniform4iv(location, v)",uint:"gl.uniform1uiv(location, v)",uvec2:"gl.uniform2uiv(location, v)",uvec3:"gl.uniform3uiv(location, v)",uvec4:"gl.uniform4uiv(location, v)",bool:"gl.uniform1iv(location, v)",bvec2:"gl.uniform2iv(location, v)",bvec3:"gl.uniform3iv(location, v)",bvec4:"gl.uniform4iv(location, v)",sampler2D:"gl.uniform1iv(location, v)",samplerCube:"gl.uniform1iv(location, v)",sampler2DArray:"gl.uniform1iv(location, v)"};function qc(s,t){var e;const i=[` + var v = null; + var cv = null; + var cu = null; + var t = 0; + var gl = renderer.gl; + `];for(const r in s.uniforms){const n=t[r];if(!n){((e=s.uniforms[r])==null?void 0:e.group)===!0&&(s.uniforms[r].ubo?i.push(` + renderer.shader.syncUniformBufferGroup(uv.${r}, '${r}'); + `):i.push(` + renderer.shader.syncUniformGroup(uv.${r}, syncData); + `));continue}const a=s.uniforms[r];let o=!1;for(let h=0;h=_e.WEBGL2&&(t=s.getContext("webgl2",{})),t||(t=s.getContext("webgl",{})||s.getContext("experimental-webgl",{}),t?t.getExtension("WEBGL_draw_buffers"):t=null),vi=t}return vi}let vs;function Kc(){if(!vs){vs=At.MEDIUM;const s=xo();if(s&&s.getShaderPrecisionFormat){const t=s.getShaderPrecisionFormat(s.FRAGMENT_SHADER,s.HIGH_FLOAT);t&&(vs=t.precision?At.HIGH:At.MEDIUM)}}return vs}function bo(s,t){const e=s.getShaderSource(t).split(` +`).map((l,u)=>`${u}: ${l}`),i=s.getShaderInfoLog(t),r=i.split(` +`),n={},a=r.map(l=>parseFloat(l.replace(/^ERROR\: 0\:([\d]+)\:.*$/,"$1"))).filter(l=>l&&!n[l]?(n[l]=!0,!0):!1),o=[""];a.forEach(l=>{e[l-1]=`%c${e[l-1]}%c`,o.push("background: #FF0000; color:#FFFFFF; font-size: 10px","font-size: 10px")});const h=e.join(` +`);o[0]=h,console.error(i),console.groupCollapsed("click to view full shader code"),console.warn(...o),console.groupEnd()}function Zc(s,t,e,i){s.getProgramParameter(t,s.LINK_STATUS)||(s.getShaderParameter(e,s.COMPILE_STATUS)||bo(s,e),s.getShaderParameter(i,s.COMPILE_STATUS)||bo(s,i),console.error("PixiJS Error: Could not initialize shader."),s.getProgramInfoLog(t)!==""&&console.warn("PixiJS Warning: gl.getProgramInfoLog()",s.getProgramInfoLog(t)))}const Qc={float:1,vec2:2,vec3:3,vec4:4,int:1,ivec2:2,ivec3:3,ivec4:4,uint:1,uvec2:2,uvec3:3,uvec4:4,bool:1,bvec2:2,bvec3:3,bvec4:4,mat2:4,mat3:9,mat4:16,sampler2D:1};function To(s){return Qc[s]}let ys=null;const Eo={FLOAT:"float",FLOAT_VEC2:"vec2",FLOAT_VEC3:"vec3",FLOAT_VEC4:"vec4",INT:"int",INT_VEC2:"ivec2",INT_VEC3:"ivec3",INT_VEC4:"ivec4",UNSIGNED_INT:"uint",UNSIGNED_INT_VEC2:"uvec2",UNSIGNED_INT_VEC3:"uvec3",UNSIGNED_INT_VEC4:"uvec4",BOOL:"bool",BOOL_VEC2:"bvec2",BOOL_VEC3:"bvec3",BOOL_VEC4:"bvec4",FLOAT_MAT2:"mat2",FLOAT_MAT3:"mat3",FLOAT_MAT4:"mat4",SAMPLER_2D:"sampler2D",INT_SAMPLER_2D:"sampler2D",UNSIGNED_INT_SAMPLER_2D:"sampler2D",SAMPLER_CUBE:"samplerCube",INT_SAMPLER_CUBE:"samplerCube",UNSIGNED_INT_SAMPLER_CUBE:"samplerCube",SAMPLER_2D_ARRAY:"sampler2DArray",INT_SAMPLER_2D_ARRAY:"sampler2DArray",UNSIGNED_INT_SAMPLER_2D_ARRAY:"sampler2DArray"};function Ao(s,t){if(!ys){const e=Object.keys(Eo);ys={};for(let i=0;i0&&(e+=` +else `),ithis.size&&this.flush(),this._vertexCount+=t.vertexData.length/2,this._indexCount+=t.indices.length,this._bufferedTextures[this._bufferSize]=t._texture.baseTexture,this._bufferedElements[this._bufferSize++]=t)}buildTexturesAndDrawCalls(){const{_bufferedTextures:t,maxTextures:e}=this,i=jt._textureArrayPool,r=this.renderer.batch,n=this._tempBoundTextures,a=this.renderer.textureGC.count;let o=++X._globalBatch,h=0,l=i[0],u=0;r.copyBoundTextures(n,e);for(let c=0;c=e&&(r.boundArray(l,n,o,e),this.buildDrawCalls(l,u,c),u=c,l=i[++h],++o),d._batchEnabled=o,d.touched=a,l.elements[l.count++]=d)}l.count>0&&(r.boundArray(l,n,o,e),this.buildDrawCalls(l,u,this._bufferSize),++h,++o);for(let c=0;c0);for(let m=0;m=0;--r)t[r]=i[r]||null,t[r]&&(t[r]._batchLocation=r)}boundArray(t,e,i,r){const{elements:n,ids:a,count:o}=t;let h=0;for(let l=0;l=0&&c=_e.WEBGL2&&(i=t.getContext("webgl2",e)),i)this.webGLVersion=2;else if(this.webGLVersion=1,i=t.getContext("webgl",e)||t.getContext("experimental-webgl",e),!i)throw new Error("This browser does not support WebGL. Try using the canvas renderer");return this.gl=i,this.getExtensions(),this.gl}getExtensions(){const{gl:t}=this,e={loseContext:t.getExtension("WEBGL_lose_context"),anisotropicFiltering:t.getExtension("EXT_texture_filter_anisotropic"),floatTextureLinear:t.getExtension("OES_texture_float_linear"),s3tc:t.getExtension("WEBGL_compressed_texture_s3tc"),s3tc_sRGB:t.getExtension("WEBGL_compressed_texture_s3tc_srgb"),etc:t.getExtension("WEBGL_compressed_texture_etc"),etc1:t.getExtension("WEBGL_compressed_texture_etc1"),pvrtc:t.getExtension("WEBGL_compressed_texture_pvrtc")||t.getExtension("WEBKIT_WEBGL_compressed_texture_pvrtc"),atc:t.getExtension("WEBGL_compressed_texture_atc"),astc:t.getExtension("WEBGL_compressed_texture_astc")};this.webGLVersion===1?Object.assign(this.extensions,e,{drawBuffers:t.getExtension("WEBGL_draw_buffers"),depthTexture:t.getExtension("WEBGL_depth_texture"),vertexArrayObject:t.getExtension("OES_vertex_array_object")||t.getExtension("MOZ_OES_vertex_array_object")||t.getExtension("WEBKIT_OES_vertex_array_object"),uint32ElementIndex:t.getExtension("OES_element_index_uint"),floatTexture:t.getExtension("OES_texture_float"),floatTextureLinear:t.getExtension("OES_texture_float_linear"),textureHalfFloat:t.getExtension("OES_texture_half_float"),textureHalfFloatLinear:t.getExtension("OES_texture_half_float_linear")}):this.webGLVersion===2&&Object.assign(this.extensions,e,{colorBufferFloat:t.getExtension("EXT_color_buffer_float")})}handleContextLost(t){t.preventDefault(),setTimeout(()=>{this.gl.isContextLost()&&this.extensions.loseContext&&this.extensions.loseContext.restoreContext()},0)}handleContextRestored(){this.renderer.runners.contextChange.emit(this.gl)}destroy(){const t=this.renderer.view;this.renderer=null,t.removeEventListener!==void 0&&(t.removeEventListener("webglcontextlost",this.handleContextLost),t.removeEventListener("webglcontextrestored",this.handleContextRestored)),this.gl.useProgram(null),this.extensions.loseContext&&this.extensions.loseContext.loseContext()}postrender(){this.renderer.objectRenderer.renderingToScreen&&this.gl.flush()}validateContext(t){const e=t.getContextAttributes(),i="WebGL2RenderingContext"in globalThis&&t instanceof globalThis.WebGL2RenderingContext;i&&(this.webGLVersion=2),e&&!e.stencil&&console.warn("Provided WebGL context does not have a stencil buffer, masks may not render correctly");const r=i||!!t.getExtension("OES_element_index_uint");this.supports.uint32Indices=r,r||console.warn("Provided WebGL context does not support 32 index buffer, complex graphics may not render correctly")}}Ei.defaultOptions={context:null,antialias:!1,premultipliedAlpha:!0,preserveDrawingBuffer:!1,powerPreference:"default"},Ei.extension={type:R.RendererSystem,name:"context"},L.add(Ei);class Ts{constructor(t,e){if(this.width=Math.round(t),this.height=Math.round(e),!this.width||!this.height)throw new Error("Framebuffer width or height is zero");this.stencil=!1,this.depth=!1,this.dirtyId=0,this.dirtyFormat=0,this.dirtySize=0,this.depthTexture=null,this.colorTextures=[],this.glFramebuffers={},this.disposeRunner=new St("disposeFramebuffer"),this.multisample=at.NONE}get colorTexture(){return this.colorTextures[0]}addColorTexture(t=0,e){return this.colorTextures[t]=e||new X(null,{scaleMode:zt.NEAREST,resolution:1,mipmap:Ut.OFF,width:this.width,height:this.height}),this.dirtyId++,this.dirtyFormat++,this}addDepthTexture(t){return this.depthTexture=t||new X(null,{scaleMode:zt.NEAREST,resolution:1,width:this.width,height:this.height,mipmap:Ut.OFF,format:A.DEPTH_COMPONENT,type:k.UNSIGNED_SHORT}),this.dirtyId++,this.dirtyFormat++,this}enableDepth(){return this.depth=!0,this.dirtyId++,this.dirtyFormat++,this}enableStencil(){return this.stencil=!0,this.dirtyId++,this.dirtyFormat++,this}resize(t,e){if(t=Math.round(t),e=Math.round(e),!t||!e)throw new Error("Framebuffer width and height must not be zero");if(!(t===this.width&&e===this.height)){this.width=t,this.height=e,this.dirtyId++,this.dirtySize++;for(let i=0;i{const r=this.source;this.url=r.src;const n=()=>{this.destroyed||(r.onload=null,r.onerror=null,this.update(),this._load=null,this.createBitmap?e(this.process()):e(this))};r.complete&&r.src?n():(r.onload=n,r.onerror=a=>{i(a),this.onError.emit(a)})}),this._load)}process(){const t=this.source;if(this._process!==null)return this._process;if(this.bitmap!==null||!globalThis.createImageBitmap)return Promise.resolve(this);const e=globalThis.createImageBitmap,i=!t.crossOrigin||t.crossOrigin==="anonymous";return this._process=fetch(t.src,{mode:i?"cors":"no-cors"}).then(r=>r.blob()).then(r=>e(r,0,0,t.width,t.height,{premultiplyAlpha:this.alphaMode===null||this.alphaMode===bt.UNPACK?"premultiply":"none"})).then(r=>this.destroyed?Promise.reject():(this.bitmap=r,this.update(),this._process=null,Promise.resolve(this))),this._process}upload(t,e,i){if(typeof this.alphaMode=="number"&&(e.alphaMode=this.alphaMode),!this.createBitmap)return super.upload(t,e,i);if(!this.bitmap&&(this.process(),!this.bitmap))return!1;if(super.upload(t,e,i,this.bitmap),!this.preserveBitmap){let r=!0;const n=e._glTextures;for(const a in n){const o=n[a];if(o!==i&&o.dirtyId!==e.dirtyId){r=!1;break}}r&&(this.bitmap.close&&this.bitmap.close(),this.bitmap=null)}return!0}dispose(){this.source.onload=null,this.source.onerror=null,super.dispose(),this.bitmap&&(this.bitmap.close(),this.bitmap=null),this._process=null,this._load=null}static test(t){return typeof HTMLImageElement!="undefined"&&(typeof t=="string"||t instanceof HTMLImageElement)}}class qr{constructor(){this.x0=0,this.y0=0,this.x1=1,this.y1=0,this.x2=1,this.y2=1,this.x3=0,this.y3=1,this.uvsFloat32=new Float32Array(8)}set(t,e,i){const r=e.width,n=e.height;if(i){const a=t.width/2/r,o=t.height/2/n,h=t.x/r+a,l=t.y/n+o;i=et.add(i,et.NW),this.x0=h+a*et.uX(i),this.y0=l+o*et.uY(i),i=et.add(i,2),this.x1=h+a*et.uX(i),this.y1=l+o*et.uY(i),i=et.add(i,2),this.x2=h+a*et.uX(i),this.y2=l+o*et.uY(i),i=et.add(i,2),this.x3=h+a*et.uX(i),this.y3=l+o*et.uY(i)}else this.x0=t.x/r,this.y0=t.y/n,this.x1=(t.x+t.width)/r,this.y1=t.y/n,this.x2=(t.x+t.width)/r,this.y2=(t.y+t.height)/n,this.x3=t.x/r,this.y3=(t.y+t.height)/n;this.uvsFloat32[0]=this.x0,this.uvsFloat32[1]=this.y0,this.uvsFloat32[2]=this.x1,this.uvsFloat32[3]=this.y1,this.uvsFloat32[4]=this.x2,this.uvsFloat32[5]=this.y2,this.uvsFloat32[6]=this.x3,this.uvsFloat32[7]=this.y3}}const Co=new qr;function Es(s){s.destroy=function(){},s.on=function(){},s.once=function(){},s.emit=function(){}}class B extends Ve{constructor(t,e,i,r,n,a,o){if(super(),this.noFrame=!1,e||(this.noFrame=!0,e=new j(0,0,1,1)),t instanceof B&&(t=t.baseTexture),this.baseTexture=t,this._frame=e,this.trim=r,this.valid=!1,this.destroyed=!1,this._uvs=Co,this.uvMatrix=null,this.orig=i||e,this._rotate=Number(n||0),n===!0)this._rotate=2;else if(this._rotate%2!==0)throw new Error("attempt to use diamond-shaped UVs. If you are sure, set rotation manually");this.defaultAnchor=a?new q(a.x,a.y):new q(0,0),this.defaultBorders=o,this._updateID=0,this.textureCacheIds=[],t.valid?this.noFrame?t.valid&&this.onBaseTextureUpdated(t):this.frame=e:t.once("loaded",this.onBaseTextureUpdated,this),this.noFrame&&t.on("update",this.onBaseTextureUpdated,this)}update(){this.baseTexture.resource&&this.baseTexture.resource.update()}onBaseTextureUpdated(t){if(this.noFrame){if(!this.baseTexture.valid)return;this._frame.width=t.width,this._frame.height=t.height,this.valid=!0,this.updateUvs()}else this.frame=this._frame;this.emit("update",this)}destroy(t){if(this.baseTexture){if(t){const{resource:e}=this.baseTexture;e!=null&&e.url&&Tt[e.url]&&B.removeFromCache(e.url),this.baseTexture.destroy()}this.baseTexture.off("loaded",this.onBaseTextureUpdated,this),this.baseTexture.off("update",this.onBaseTextureUpdated,this),this.baseTexture=null}this._frame=null,this._uvs=null,this.trim=null,this.orig=null,this.valid=!1,B.removeFromCache(this),this.textureCacheIds=null,this.destroyed=!0,this.emit("destroyed",this),this.removeAllListeners()}clone(){var t;const e=this._frame.clone(),i=this._frame===this.orig?e:this.orig.clone(),r=new B(this.baseTexture,!this.noFrame&&e,i,(t=this.trim)==null?void 0:t.clone(),this.rotate,this.defaultAnchor,this.defaultBorders);return this.noFrame&&(r._frame=e),r}updateUvs(){this._uvs===Co&&(this._uvs=new qr),this._uvs.set(this._frame,this.baseTexture,this.rotate),this._updateID++}static from(t,e={},i=O.STRICT_TEXTURE_CACHE){const r=typeof t=="string";let n=null;if(r)n=t;else if(t instanceof X){if(!t.cacheId){const o=(e==null?void 0:e.pixiIdPrefix)||"pixiid";t.cacheId=`${o}-${ve()}`,X.addToCache(t,t.cacheId)}n=t.cacheId}else{if(!t._pixiId){const o=(e==null?void 0:e.pixiIdPrefix)||"pixiid";t._pixiId=`${o}_${ve()}`}n=t._pixiId}let a=Tt[n];if(r&&i&&!a)throw new Error(`The cacheId "${n}" does not exist in TextureCache.`);return!a&&!(t instanceof X)?(e.resolution||(e.resolution=Kt(t)),a=new B(new X(t,e)),a.baseTexture.cacheId=n,X.addToCache(a.baseTexture,n),B.addToCache(a,n)):!a&&t instanceof X&&(a=new B(t),B.addToCache(a,n)),a}static fromURL(t,e){const i=Object.assign({autoLoad:!1},e==null?void 0:e.resourceOptions),r=B.from(t,Object.assign({resourceOptions:i},e),!1),n=r.baseTexture.resource;return r.baseTexture.valid?Promise.resolve(r):n.load().then(()=>Promise.resolve(r))}static fromBuffer(t,e,i,r){return new B(X.fromBuffer(t,e,i,r))}static fromLoader(t,e,i,r){const n=new X(t,Object.assign({scaleMode:X.defaultOptions.scaleMode,resolution:Kt(e)},r)),{resource:a}=n;a instanceof Yr&&(a.url=e);const o=new B(n);return i||(i=e),X.addToCache(o.baseTexture,i),B.addToCache(o,i),i!==e&&(X.addToCache(o.baseTexture,e),B.addToCache(o,e)),o.baseTexture.valid?Promise.resolve(o):new Promise(h=>{o.baseTexture.once("loaded",()=>h(o))})}static addToCache(t,e){e&&(t.textureCacheIds.includes(e)||t.textureCacheIds.push(e),Tt[e]&&Tt[e]!==t&&console.warn(`Texture added to the cache with an id [${e}] that already had an entry`),Tt[e]=t)}static removeFromCache(t){if(typeof t=="string"){const e=Tt[t];if(e){const i=e.textureCacheIds.indexOf(t);return i>-1&&e.textureCacheIds.splice(i,1),delete Tt[t],e}}else if(t!=null&&t.textureCacheIds){for(let e=0;ethis.baseTexture.width,o=i+n>this.baseTexture.height;if(a||o){const h=a&&o?"and":"or",l=`X: ${e} + ${r} = ${e+r} > ${this.baseTexture.width}`,u=`Y: ${i} + ${n} = ${i+n} > ${this.baseTexture.height}`;throw new Error(`Texture Error: frame does not fit inside the base Texture dimensions: ${l} ${h} ${u}`)}this.valid=r&&n&&this.baseTexture.valid,!this.trim&&!this.rotate&&(this.orig=t),this.valid&&this.updateUvs()}get rotate(){return this._rotate}set rotate(t){this._rotate=t,this.valid&&this.updateUvs()}get width(){return this.orig.width}get height(){return this.orig.height}castToBaseTexture(){return this.baseTexture}static get EMPTY(){return B._EMPTY||(B._EMPTY=new B(new X),Es(B._EMPTY),Es(B._EMPTY.baseTexture)),B._EMPTY}static get WHITE(){if(!B._WHITE){const t=O.ADAPTER.createCanvas(16,16),e=t.getContext("2d");t.width=16,t.height=16,e.fillStyle="white",e.fillRect(0,0,16,16),B._WHITE=new B(X.from(t)),Es(B._WHITE),Es(B._WHITE.baseTexture)}return B._WHITE}}class xe extends B{constructor(t,e){super(t,e),this.valid=!0,this.filterFrame=null,this.filterPoolKey=null,this.updateUvs()}get framebuffer(){return this.baseTexture.framebuffer}get multisample(){return this.framebuffer.multisample}set multisample(t){this.framebuffer.multisample=t}resize(t,e,i=!0){const r=this.baseTexture.resolution,n=Math.round(t*r)/r,a=Math.round(e*r)/r;this.valid=n>0&&a>0,this._frame.width=this.orig.width=n,this._frame.height=this.orig.height=a,i&&this.baseTexture.resize(n,a),this.updateUvs()}setResolution(t){const{baseTexture:e}=this;e.resolution!==t&&(e.setResolution(t),this.resize(e.width,e.height,!1))}static create(t){return new xe(new Wr(t))}}class Kr{constructor(t){this.texturePool={},this.textureOptions=t||{},this.enableFullScreen=!1,this._pixelsWidth=0,this._pixelsHeight=0}createTexture(t,e,i=at.NONE){const r=new Wr(Object.assign({width:t,height:e,resolution:1,multisample:i},this.textureOptions));return new xe(r)}getOptimalTexture(t,e,i=1,r=at.NONE){let n;t=Math.max(Math.ceil(t*i-1e-6),1),e=Math.max(Math.ceil(e*i-1e-6),1),!this.enableFullScreen||t!==this._pixelsWidth||e!==this._pixelsHeight?(t=pi(t),e=pi(e),n=((t&65535)<<16|e&65535)>>>0,r>1&&(n+=r*4294967296)):n=r>1?-r:-1,this.texturePool[n]||(this.texturePool[n]=[]);let a=this.texturePool[n].pop();return a||(a=this.createTexture(t,e,r)),a.filterPoolKey=n,a.setResolution(i),a}getFilterTexture(t,e,i){const r=this.getOptimalTexture(t.width,t.height,e||t.resolution,i||at.NONE);return r.filterFrame=t.filterFrame,r}returnTexture(t){const e=t.filterPoolKey;t.filterFrame=null,this.texturePool[e].push(t)}returnFilterTexture(t){this.returnTexture(t)}clear(t){if(t=t!==!1,t)for(const e in this.texturePool){const i=this.texturePool[e];if(i)for(let r=0;r0&&t.height>0;for(const e in this.texturePool){if(!(Number(e)<0))continue;const i=this.texturePool[e];if(i)for(let r=0;r1&&(u=this.getOptimalFilterTexture(l.width,l.height,e.resolution),u.filterFrame=l.filterFrame),i[c].apply(this,l,u,kt.CLEAR,e);const d=l;l=u,u=d}i[c].apply(this,l,h.renderTexture,kt.BLEND,e),c>1&&e.multisample>1&&this.returnFilterTexture(e.renderTexture),this.returnFilterTexture(l),this.returnFilterTexture(u)}e.clear(),this.statePool.push(e)}bindAndClear(t,e=kt.CLEAR){const{renderTexture:i,state:r}=this.renderer;if(t===this.defaultFilterStack[this.defaultFilterStack.length-1].renderTexture?this.renderer.projection.transform=this.activeState.transform:this.renderer.projection.transform=null,t!=null&&t.filterFrame){const a=this.tempRect;a.x=0,a.y=0,a.width=t.filterFrame.width,a.height=t.filterFrame.height,i.bind(t,t.filterFrame,a)}else t!==this.defaultFilterStack[this.defaultFilterStack.length-1].renderTexture?i.bind(t):this.renderer.renderTexture.bind(t,this.activeState.bindingSourceFrame,this.activeState.bindingDestinationFrame);const n=r.stateId&1||this.forceClear;(e===kt.CLEAR||e===kt.BLIT&&n)&&this.renderer.framebuffer.clear(0,0,0,0)}applyFilter(t,e,i,r){const n=this.renderer;n.state.set(t.state),this.bindAndClear(i,r),t.uniforms.uSampler=e,t.uniforms.filterGlobals=this.globalUniforms,n.shader.bind(t),t.legacy=!!t.program.attributeData.aTextureCoord,t.legacy?(this.quadUv.map(e._frame,e.filterFrame),n.geometry.bind(this.quadUv),n.geometry.draw(Lt.TRIANGLES)):(n.geometry.bind(this.quad),n.geometry.draw(Lt.TRIANGLE_STRIP))}calculateSpriteMatrix(t,e){const{sourceFrame:i,destinationFrame:r}=this.activeState,{orig:n}=e._texture,a=t.set(r.width,0,0,r.height,i.x,i.y),o=e.worldTransform.copyTo(tt.TEMP_MATRIX);return o.invert(),a.prepend(o),a.scale(1/n.width,1/n.height),a.translate(e.anchor.x,e.anchor.y),a}destroy(){this.renderer=null,this.texturePool.clear(!1)}getOptimalFilterTexture(t,e,i=1,r=at.NONE){return this.texturePool.getOptimalTexture(t,e,i,r)}getFilterTexture(t,e,i){if(typeof t=="number"){const n=t;t=e,e=n}t=t||this.activeState.renderTexture;const r=this.texturePool.getOptimalTexture(t.width,t.height,e||t.resolution,i||at.NONE);return r.filterFrame=t.filterFrame,r}returnFilterTexture(t){this.texturePool.returnTexture(t)}emptyPool(){this.texturePool.clear(!0)}resize(){this.texturePool.setScreenSize(this.renderer.view)}transformAABB(t,e){const i=As[0],r=As[1],n=As[2],a=As[3];i.set(e.left,e.top),r.set(e.left,e.bottom),n.set(e.right,e.top),a.set(e.right,e.bottom),t.apply(i,i),t.apply(r,r),t.apply(n,n),t.apply(a,a);const o=Math.min(i.x,r.x,n.x,a.x),h=Math.min(i.y,r.y,n.y,a.y),l=Math.max(i.x,r.x,n.x,a.x),u=Math.max(i.y,r.y,n.y,a.y);e.x=o,e.y=h,e.width=l-o,e.height=u-h}roundFrame(t,e,i,r,n){if(!(t.width<=0||t.height<=0||i.width<=0||i.height<=0)){if(n){const{a,b:o,c:h,d:l}=n;if((Math.abs(o)>1e-4||Math.abs(h)>1e-4)&&(Math.abs(a)>1e-4||Math.abs(l)>1e-4))return}n=n?Qr.copyFrom(n):Qr.identity(),n.translate(-i.x,-i.y).scale(r.width/i.width,r.height/i.height).translate(r.x,r.y),this.transformAABB(n,t),t.ceil(e),this.transformAABB(n.invert(),t)}}}Jr.extension={type:R.RendererSystem,name:"filter"},L.add(Jr);class Do{constructor(t){this.framebuffer=t,this.stencil=null,this.dirtyId=-1,this.dirtyFormat=-1,this.dirtySize=-1,this.multisample=at.NONE,this.msaaBuffer=null,this.blitFramebuffer=null,this.mipLevel=0}}const od=new j;class tn{constructor(t){this.renderer=t,this.managedFramebuffers=[],this.unknownFramebuffer=new Ts(10,10),this.msaaSamples=null}contextChange(){this.disposeAll(!0);const t=this.gl=this.renderer.gl;if(this.CONTEXT_UID=this.renderer.CONTEXT_UID,this.current=this.unknownFramebuffer,this.viewport=new j,this.hasMRT=!0,this.writeDepthTexture=!0,this.renderer.context.webGLVersion===1){let e=this.renderer.context.extensions.drawBuffers,i=this.renderer.context.extensions.depthTexture;O.PREFER_ENV===_e.WEBGL_LEGACY&&(e=null,i=null),e?t.drawBuffers=r=>e.drawBuffersWEBGL(r):(this.hasMRT=!1,t.drawBuffers=()=>{}),i||(this.writeDepthTexture=!1)}else this.msaaSamples=t.getInternalformatParameter(t.RENDERBUFFER,t.RGBA8,t.SAMPLES)}bind(t,e,i=0){const{gl:r}=this;if(t){const n=t.glFramebuffers[this.CONTEXT_UID]||this.initFramebuffer(t);this.current!==t&&(this.current=t,r.bindFramebuffer(r.FRAMEBUFFER,n.framebuffer)),n.mipLevel!==i&&(t.dirtyId++,t.dirtyFormat++,n.mipLevel=i),n.dirtyId!==t.dirtyId&&(n.dirtyId=t.dirtyId,n.dirtyFormat!==t.dirtyFormat?(n.dirtyFormat=t.dirtyFormat,n.dirtySize=t.dirtySize,this.updateFramebuffer(t,i)):n.dirtySize!==t.dirtySize&&(n.dirtySize=t.dirtySize,this.resizeFramebuffer(t)));for(let a=0;a>i,o=e.height>>i,h=a/e.width;this.setViewport(e.x*h,e.y*h,a,o)}else{const a=t.width>>i,o=t.height>>i;this.setViewport(0,0,a,o)}}else this.current&&(this.current=null,r.bindFramebuffer(r.FRAMEBUFFER,null)),e?this.setViewport(e.x,e.y,e.width,e.height):this.setViewport(0,0,this.renderer.width,this.renderer.height)}setViewport(t,e,i,r){const n=this.viewport;t=Math.round(t),e=Math.round(e),i=Math.round(i),r=Math.round(r),(n.width!==i||n.height!==r||n.x!==t||n.y!==e)&&(n.x=t,n.y=e,n.width=i,n.height=r,this.gl.viewport(t,e,i,r))}get size(){return this.current?{x:0,y:0,width:this.current.width,height:this.current.height}:{x:0,y:0,width:this.renderer.width,height:this.renderer.height}}clear(t,e,i,r,n=Zi.COLOR|Zi.DEPTH){const{gl:a}=this;a.clearColor(t,e,i,r),a.clear(n)}initFramebuffer(t){const{gl:e}=this,i=new Do(e.createFramebuffer());return i.multisample=this.detectSamples(t.multisample),t.glFramebuffers[this.CONTEXT_UID]=i,this.managedFramebuffers.push(t),t.disposeRunner.add(this),i}resizeFramebuffer(t){const{gl:e}=this,i=t.glFramebuffers[this.CONTEXT_UID];if(i.stencil){e.bindRenderbuffer(e.RENDERBUFFER,i.stencil);let a;this.renderer.context.webGLVersion===1?a=e.DEPTH_STENCIL:t.depth&&t.stencil?a=e.DEPTH24_STENCIL8:t.depth?a=e.DEPTH_COMPONENT24:a=e.STENCIL_INDEX8,i.msaaBuffer?e.renderbufferStorageMultisample(e.RENDERBUFFER,i.multisample,a,t.width,t.height):e.renderbufferStorage(e.RENDERBUFFER,a,t.width,t.height)}const r=t.colorTextures;let n=r.length;e.drawBuffers||(n=Math.min(n,1));for(let a=0;a1&&this.canMultisampleFramebuffer(t)?r.msaaBuffer=r.msaaBuffer||i.createRenderbuffer():r.msaaBuffer&&(i.deleteRenderbuffer(r.msaaBuffer),r.msaaBuffer=null,r.blitFramebuffer&&(r.blitFramebuffer.dispose(),r.blitFramebuffer=null));const o=[];for(let h=0;h1&&i.drawBuffers(o),t.depthTexture&&this.writeDepthTexture){const h=t.depthTexture;this.renderer.texture.bind(h,0),i.framebufferTexture2D(i.FRAMEBUFFER,i.DEPTH_ATTACHMENT,i.TEXTURE_2D,h._glTextures[this.CONTEXT_UID].texture,e)}if((t.stencil||t.depth)&&!(t.depthTexture&&this.writeDepthTexture)){r.stencil=r.stencil||i.createRenderbuffer();let h,l;this.renderer.context.webGLVersion===1?(h=i.DEPTH_STENCIL_ATTACHMENT,l=i.DEPTH_STENCIL):t.depth&&t.stencil?(h=i.DEPTH_STENCIL_ATTACHMENT,l=i.DEPTH24_STENCIL8):t.depth?(h=i.DEPTH_ATTACHMENT,l=i.DEPTH_COMPONENT24):(h=i.STENCIL_ATTACHMENT,l=i.STENCIL_INDEX8),i.bindRenderbuffer(i.RENDERBUFFER,r.stencil),r.msaaBuffer?i.renderbufferStorageMultisample(i.RENDERBUFFER,r.multisample,l,t.width,t.height):i.renderbufferStorage(i.RENDERBUFFER,l,t.width,t.height),i.framebufferRenderbuffer(i.FRAMEBUFFER,h,i.RENDERBUFFER,r.stencil)}else r.stencil&&(i.deleteRenderbuffer(r.stencil),r.stencil=null)}canMultisampleFramebuffer(t){return this.renderer.context.webGLVersion!==1&&t.colorTextures.length<=1&&!t.depthTexture}detectSamples(t){const{msaaSamples:e}=this;let i=at.NONE;if(t<=1||e===null)return i;for(let r=0;r=0&&this.managedFramebuffers.splice(n,1),t.disposeRunner.remove(this),e||(r.deleteFramebuffer(i.framebuffer),i.msaaBuffer&&r.deleteRenderbuffer(i.msaaBuffer),i.stencil&&r.deleteRenderbuffer(i.stencil)),i.blitFramebuffer&&this.disposeFramebuffer(i.blitFramebuffer,e)}disposeAll(t){const e=this.managedFramebuffers;this.managedFramebuffers=[];for(let i=0;ii.createVertexArrayOES(),t.bindVertexArray=r=>i.bindVertexArrayOES(r),t.deleteVertexArray=r=>i.deleteVertexArrayOES(r)):(this.hasVao=!1,t.createVertexArray=()=>null,t.bindVertexArray=()=>null,t.deleteVertexArray=()=>null)}if(e.webGLVersion!==2){const i=t.getExtension("ANGLE_instanced_arrays");i?(t.vertexAttribDivisor=(r,n)=>i.vertexAttribDivisorANGLE(r,n),t.drawElementsInstanced=(r,n,a,o,h)=>i.drawElementsInstancedANGLE(r,n,a,o,h),t.drawArraysInstanced=(r,n,a,o)=>i.drawArraysInstancedANGLE(r,n,a,o)):this.hasInstance=!1}this.canUseUInt32ElementIndex=e.webGLVersion===2||!!e.extensions.uint32ElementIndex}bind(t,e){e=e||this.renderer.shader.shader;const{gl:i}=this;let r=t.glVertexArrayObjects[this.CONTEXT_UID],n=!1;r||(this.managedGeometries[t.id]=t,t.disposeRunner.add(this),t.glVertexArrayObjects[this.CONTEXT_UID]=r={},n=!0);const a=r[e.program.id]||this.initGeometryVao(t,e,n);this._activeGeometry=t,this._activeVao!==a&&(this._activeVao=a,this.hasVao?i.bindVertexArray(a):this.activateVao(t,e.program)),this.updateBuffers()}reset(){this.unbind()}updateBuffers(){const t=this._activeGeometry,e=this.renderer.buffer;for(let i=0;i0?this.maskStack[this.maskStack.length-1]._colorMask:15;i!==e&&this.renderer.gl.colorMask((i&1)!==0,(i&2)!==0,(i&4)!==0,(i&8)!==0)}destroy(){this.renderer=null}}rn.extension={type:R.RendererSystem,name:"mask"},L.add(rn);class No{constructor(t){this.renderer=t,this.maskStack=[],this.glConst=0}getStackLength(){return this.maskStack.length}setMaskStack(t){const{gl:e}=this.renderer,i=this.getStackLength();this.maskStack=t;const r=this.getStackLength();r!==i&&(r===0?e.disable(this.glConst):(e.enable(this.glConst),this._useCurrent()))}_useCurrent(){}destroy(){this.renderer=null,this.maskStack=null}}const Lo=new tt,Uo=[],ko=class sr extends No{constructor(t){super(t),this.glConst=O.ADAPTER.getWebGLRenderingContext().SCISSOR_TEST}getStackLength(){const t=this.maskStack[this.maskStack.length-1];return t?t._scissorCounter:0}calcScissorRect(t){var e;if(t._scissorRectLocal)return;const i=t._scissorRect,{maskObject:r}=t,{renderer:n}=this,a=n.renderTexture,o=r.getBounds(!0,(e=Uo.pop())!=null?e:new j);this.roundFrameToPixels(o,a.current?a.current.resolution:n.resolution,a.sourceFrame,a.destinationFrame,n.projection.transform),i&&o.fit(i),t._scissorRectLocal=o}static isMatrixRotated(t){if(!t)return!1;const{a:e,b:i,c:r,d:n}=t;return(Math.abs(i)>1e-4||Math.abs(r)>1e-4)&&(Math.abs(e)>1e-4||Math.abs(n)>1e-4)}testScissor(t){const{maskObject:e}=t;if(!e.isFastRect||!e.isFastRect()||sr.isMatrixRotated(e.worldTransform)||sr.isMatrixRotated(this.renderer.projection.transform))return!1;this.calcScissorRect(t);const i=t._scissorRectLocal;return i.width>0&&i.height>0}roundFrameToPixels(t,e,i,r,n){sr.isMatrixRotated(n)||(n=n?Lo.copyFrom(n):Lo.identity(),n.translate(-i.x,-i.y).scale(r.width/i.width,r.height/i.height).translate(r.x,r.y),this.renderer.filter.transformAABB(n,t),t.fit(r),t.x=Math.round(t.x*e),t.y=Math.round(t.y*e),t.width=Math.round(t.width*e),t.height=Math.round(t.height*e))}push(t){t._scissorRectLocal||this.calcScissorRect(t);const{gl:e}=this.renderer;t._scissorRect||e.enable(e.SCISSOR_TEST),t._scissorCounter++,t._scissorRect=t._scissorRectLocal,this._useCurrent()}pop(t){const{gl:e}=this.renderer;t&&Uo.push(t._scissorRectLocal),this.getStackLength()>0?this._useCurrent():e.disable(e.SCISSOR_TEST)}_useCurrent(){const t=this.maskStack[this.maskStack.length-1]._scissorRect;let e;this.renderer.renderTexture.current?e=t.y:e=this.renderer.height-t.height-t.y,this.renderer.gl.scissor(t.x,e,t.width,t.height)}};ko.extension={type:R.RendererSystem,name:"scissor"};let Go=ko;L.add(Go);class nn extends No{constructor(t){super(t),this.glConst=O.ADAPTER.getWebGLRenderingContext().STENCIL_TEST}getStackLength(){const t=this.maskStack[this.maskStack.length-1];return t?t._stencilCounter:0}push(t){const e=t.maskObject,{gl:i}=this.renderer,r=t._stencilCounter;r===0&&(this.renderer.framebuffer.forceStencil(),i.clearStencil(0),i.clear(i.STENCIL_BUFFER_BIT),i.enable(i.STENCIL_TEST)),t._stencilCounter++;const n=t._colorMask;n!==0&&(t._colorMask=0,i.colorMask(!1,!1,!1,!1)),i.stencilFunc(i.EQUAL,r,4294967295),i.stencilOp(i.KEEP,i.KEEP,i.INCR),e.renderable=!0,e.render(this.renderer),this.renderer.batch.flush(),e.renderable=!1,n!==0&&(t._colorMask=n,i.colorMask((n&1)!==0,(n&2)!==0,(n&4)!==0,(n&8)!==0)),this._useCurrent()}pop(t){const e=this.renderer.gl;if(this.getStackLength()===0)e.disable(e.STENCIL_TEST);else{const i=this.maskStack.length!==0?this.maskStack[this.maskStack.length-1]:null,r=i?i._colorMask:15;r!==0&&(i._colorMask=0,e.colorMask(!1,!1,!1,!1)),e.stencilOp(e.KEEP,e.KEEP,e.DECR),t.renderable=!0,t.render(this.renderer),this.renderer.batch.flush(),t.renderable=!1,r!==0&&(i._colorMask=r,e.colorMask((r&1)!==0,(r&2)!==0,(r&4)!==0,(r&8)!==0)),this._useCurrent()}}_useCurrent(){const t=this.renderer.gl;t.stencilFunc(t.EQUAL,this.getStackLength(),4294967295),t.stencilOp(t.KEEP,t.KEEP,t.KEEP)}}nn.extension={type:R.RendererSystem,name:"stencil"},L.add(nn);class an{constructor(t){this.renderer=t,this.plugins={}}init(){const t=this.rendererPlugins;for(const e in t)this.plugins[e]=new t[e](this.renderer)}destroy(){for(const t in this.plugins)this.plugins[t].destroy(),this.plugins[t]=null}}an.extension={type:[R.RendererSystem,R.CanvasRendererSystem],name:"_plugin"},L.add(an);class on{constructor(t){this.renderer=t,this.destinationFrame=null,this.sourceFrame=null,this.defaultFrame=null,this.projectionMatrix=new tt,this.transform=null}update(t,e,i,r){this.destinationFrame=t||this.destinationFrame||this.defaultFrame,this.sourceFrame=e||this.sourceFrame||t,this.calculateProjection(this.destinationFrame,this.sourceFrame,i,r),this.transform&&this.projectionMatrix.append(this.transform);const n=this.renderer;n.globalUniforms.uniforms.projectionMatrix=this.projectionMatrix,n.globalUniforms.update(),n.shader.shader&&n.shader.syncUniformGroup(n.shader.shader.uniforms.globals)}calculateProjection(t,e,i,r){const n=this.projectionMatrix,a=r?-1:1;n.identity(),n.a=1/e.width*2,n.d=a*(1/e.height*2),n.tx=-1-e.x*n.a,n.ty=-a-e.y*n.d}setTransform(t){}destroy(){this.renderer=null}}on.extension={type:R.RendererSystem,name:"projection"},L.add(on);var $o=Object.getOwnPropertySymbols,ud=Object.prototype.hasOwnProperty,cd=Object.prototype.propertyIsEnumerable,dd=(s,t)=>{var e={};for(var i in s)ud.call(s,i)&&t.indexOf(i)<0&&(e[i]=s[i]);if(s!=null&&$o)for(var i of $o(s))t.indexOf(i)<0&&cd.call(s,i)&&(e[i]=s[i]);return e};const fd=new _s,Ho=new j;class hn{constructor(t){this.renderer=t,this._tempMatrix=new tt}generateTexture(t,e){var i;const r=e||{},{region:n}=r,a=dd(r,["region"]),o=(n==null?void 0:n.copyTo(Ho))||t.getLocalBounds(Ho,!0),h=a.resolution||this.renderer.resolution;o.width=Math.max(o.width,1/h),o.height=Math.max(o.height,1/h),a.width=o.width,a.height=o.height,a.resolution=h,(i=a.multisample)!=null||(a.multisample=this.renderer.multisample);const l=xe.create(a);this._tempMatrix.tx=-o.x,this._tempMatrix.ty=-o.y;const u=t.transform;return t.transform=fd,this.renderer.render(t,{renderTexture:l,transform:this._tempMatrix,skipUpdateTransform:!!t.parent,blit:!0}),t.transform=u,l}destroy(){}}hn.extension={type:[R.RendererSystem,R.CanvasRendererSystem],name:"textureGenerator"},L.add(hn);const Ne=new j,Ai=new j;class ln{constructor(t){this.renderer=t,this.defaultMaskStack=[],this.current=null,this.sourceFrame=new j,this.destinationFrame=new j,this.viewportFrame=new j}contextChange(){var t;const e=(t=this.renderer)==null?void 0:t.gl.getContextAttributes();this._rendererPremultipliedAlpha=!!(e&&e.alpha&&e.premultipliedAlpha)}bind(t=null,e,i){const r=this.renderer;this.current=t;let n,a,o;t?(n=t.baseTexture,o=n.resolution,e||(Ne.width=t.frame.width,Ne.height=t.frame.height,e=Ne),i||(Ai.x=t.frame.x,Ai.y=t.frame.y,Ai.width=e.width,Ai.height=e.height,i=Ai),a=n.framebuffer):(o=r.resolution,e||(Ne.width=r._view.screen.width,Ne.height=r._view.screen.height,e=Ne),i||(i=Ne,i.width=e.width,i.height=e.height));const h=this.viewportFrame;h.x=i.x*o,h.y=i.y*o,h.width=i.width*o,h.height=i.height*o,t||(h.y=r.view.height-(h.y+h.height)),h.ceil(),this.renderer.framebuffer.bind(a,h),this.renderer.projection.update(i,e,o,!a),t?this.renderer.mask.setMaskStack(n.maskStack):this.renderer.mask.setMaskStack(this.defaultMaskStack),this.sourceFrame.copyFrom(e),this.destinationFrame.copyFrom(i)}clear(t,e){const i=this.current?this.current.baseTexture.clear:this.renderer.background.backgroundColor,r=Z.shared.setValue(t||i);(this.current&&this.current.baseTexture.alphaMode>0||!this.current&&this._rendererPremultipliedAlpha)&&r.premultiply(r.alpha);const n=this.destinationFrame,a=this.current?this.current.baseTexture:this.renderer._view.screen,o=n.width!==a.width||n.height!==a.height;if(o){let{x:h,y:l,width:u,height:c}=this.viewportFrame;h=Math.round(h),l=Math.round(l),u=Math.round(u),c=Math.round(c),this.renderer.gl.enable(this.renderer.gl.SCISSOR_TEST),this.renderer.gl.scissor(h,l,u,c)}this.renderer.framebuffer.clear(r.red,r.green,r.blue,r.alpha,e),o&&this.renderer.scissor.pop()}resize(){this.bind(null)}reset(){this.bind(null)}destroy(){this.renderer=null}}ln.extension={type:R.RendererSystem,name:"renderTexture"},L.add(ln);class pd{}class Vo{constructor(t,e){this.program=t,this.uniformData=e,this.uniformGroups={},this.uniformDirtyGroups={},this.uniformBufferBindings={}}destroy(){this.uniformData=null,this.uniformGroups=null,this.uniformDirtyGroups=null,this.uniformBufferBindings=null,this.program=null}}function md(s,t){const e={},i=t.getProgramParameter(s,t.ACTIVE_ATTRIBUTES);for(let r=0;rl>u?1:-1);for(let l=0;l({data:n,offset:0,dataLen:0,dirty:0}));let e=0,i=0,r=0;for(let n=0;n1&&(e=Math.max(e,16)*a.data.size),a.dataLen=e,i%e!==0&&i<16){const o=i%e%16;i+=o,r+=o}i+e>16?(r=Math.ceil(r/16)*16,a.offset=r,r+=e,i=e):(a.offset=r,i+=e,r+=e)}return r=Math.ceil(r/16)*16,{uboElements:t,size:r}}function Wo(s,t){const e=[];for(const i in s)t[i]&&e.push(t[i]);return e.sort((i,r)=>i.index-r.index),e}function Yo(s,t){if(!s.autoManage)return{size:0,syncFunc:_d};const e=Wo(s.uniforms,t),{uboElements:i,size:r}=zo(e),n=[` + var v = null; + var v2 = null; + var cv = null; + var t = 0; + var gl = renderer.gl + var index = 0; + var data = buffer.data; + `];for(let a=0;a1){const c=To(o.data.type),d=Math.max(jo[o.data.type]/16,1),f=c/d,p=(4-f%4)%4;n.push(` + cv = ud.${l}.value; + v = uv.${l}; + offset = ${o.offset/4}; + + t = 0; + + for(var i=0; i < ${o.data.size*d}; i++) + { + for(var j = 0; j < ${f}; j++) + { + data[offset++] = v[t++]; + } + offset += ${p}; + } + + `)}else{const c=vd[o.data.type];n.push(` + cv = ud.${l}.value; + v = uv.${l}; + offset = ${o.offset/4}; + ${c}; + `)}}return n.push(` + renderer.buffer.update(buffer); + `),{size:r,syncFunc:new Function("ud","uv","renderer","syncData","buffer",n.join(` +`))}}let yd=0;const Ss={textureCount:0,uboCount:0};class un{constructor(t){this.destroyed=!1,this.renderer=t,this.systemCheck(),this.gl=null,this.shader=null,this.program=null,this.cache={},this._uboCache={},this.id=yd++}systemCheck(){if(!So())throw new Error("Current environment does not allow unsafe-eval, please use @pixi/unsafe-eval module to enable support.")}contextChange(t){this.gl=t,this.reset()}bind(t,e){t.disposeRunner.add(this),t.uniforms.globals=this.renderer.globalUniforms;const i=t.program,r=i.glPrograms[this.renderer.CONTEXT_UID]||this.generateProgram(t);return this.shader=t,this.program!==i&&(this.program=i,this.gl.useProgram(r.program)),e||(Ss.textureCount=0,Ss.uboCount=0,this.syncUniformGroup(t.uniformGroup,Ss)),r}setUniforms(t){const e=this.shader.program,i=e.glPrograms[this.renderer.CONTEXT_UID];e.syncUniforms(i.uniformData,t,this.renderer)}syncUniformGroup(t,e){const i=this.getGlProgram();(!t.static||t.dirtyId!==i.uniformDirtyGroups[t.id])&&(i.uniformDirtyGroups[t.id]=t.dirtyId,this.syncUniforms(t,i,e))}syncUniforms(t,e,i){(t.syncUniforms[this.shader.program.id]||this.createSyncGroups(t))(e.uniformData,t.uniforms,this.renderer,i)}createSyncGroups(t){const e=this.getSignature(t,this.shader.program.uniformData,"u");return this.cache[e]||(this.cache[e]=qc(t,this.shader.program.uniformData)),t.syncUniforms[this.shader.program.id]=this.cache[e],t.syncUniforms[this.shader.program.id]}syncUniformBufferGroup(t,e){const i=this.getGlProgram();if(!t.static||t.dirtyId!==0||!i.uniformGroups[t.id]){t.dirtyId=0;const r=i.uniformGroups[t.id]||this.createSyncBufferGroup(t,i,e);t.buffer.update(),r(i.uniformData,t.uniforms,this.renderer,Ss,t.buffer)}this.renderer.buffer.bindBufferBase(t.buffer,i.uniformBufferBindings[e])}createSyncBufferGroup(t,e,i){const{gl:r}=this.renderer;this.renderer.buffer.bind(t.buffer);const n=this.gl.getUniformBlockIndex(e.program,i);e.uniformBufferBindings[i]=this.shader.uniformBindCount,r.uniformBlockBinding(e.program,n,this.shader.uniformBindCount),this.shader.uniformBindCount++;const a=this.getSignature(t,this.shader.program.uniformData,"ubo");let o=this._uboCache[a];if(o||(o=this._uboCache[a]=Yo(t,this.shader.program.uniformData)),t.autoManage){const h=new Float32Array(o.size/4);t.buffer.update(h)}return e.uniformGroups[t.id]=o.syncFunc,e.uniformGroups[t.id]}getSignature(t,e,i){const r=t.uniforms,n=[`${i}-`];for(const a in r)n.push(a),e[a]&&n.push(e[a].type);return n.join("-")}getGlProgram(){return this.shader?this.shader.program.glPrograms[this.renderer.CONTEXT_UID]:null}generateProgram(t){const e=this.gl,i=t.program,r=Xo(e,i);return i.glPrograms[this.renderer.CONTEXT_UID]=r,r}reset(){this.program=null,this.shader=null}disposeShader(t){this.shader===t&&(this.shader=null)}destroy(){this.renderer=null,this.destroyed=!0}}un.extension={type:R.RendererSystem,name:"shader"},L.add(un);class wi{constructor(t){this.renderer=t}run(t){const{renderer:e}=this;e.runners.init.emit(e.options),t.hello&&console.log(`PixiJS 7.3.2 - ${e.rendererLogId} - https://pixijs.com`),e.resize(e.screen.width,e.screen.height)}destroy(){}}wi.defaultOptions={hello:!1},wi.extension={type:[R.RendererSystem,R.CanvasRendererSystem],name:"startup"},L.add(wi);function xd(s,t=[]){return t[H.NORMAL]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.ADD]=[s.ONE,s.ONE],t[H.MULTIPLY]=[s.DST_COLOR,s.ONE_MINUS_SRC_ALPHA,s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.SCREEN]=[s.ONE,s.ONE_MINUS_SRC_COLOR,s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.OVERLAY]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.DARKEN]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.LIGHTEN]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.COLOR_DODGE]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.COLOR_BURN]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.HARD_LIGHT]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.SOFT_LIGHT]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.DIFFERENCE]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.EXCLUSION]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.HUE]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.SATURATION]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.COLOR]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.LUMINOSITY]=[s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.NONE]=[0,0],t[H.NORMAL_NPM]=[s.SRC_ALPHA,s.ONE_MINUS_SRC_ALPHA,s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.ADD_NPM]=[s.SRC_ALPHA,s.ONE,s.ONE,s.ONE],t[H.SCREEN_NPM]=[s.SRC_ALPHA,s.ONE_MINUS_SRC_COLOR,s.ONE,s.ONE_MINUS_SRC_ALPHA],t[H.SRC_IN]=[s.DST_ALPHA,s.ZERO],t[H.SRC_OUT]=[s.ONE_MINUS_DST_ALPHA,s.ZERO],t[H.SRC_ATOP]=[s.DST_ALPHA,s.ONE_MINUS_SRC_ALPHA],t[H.DST_OVER]=[s.ONE_MINUS_DST_ALPHA,s.ONE],t[H.DST_IN]=[s.ZERO,s.SRC_ALPHA],t[H.DST_OUT]=[s.ZERO,s.ONE_MINUS_SRC_ALPHA],t[H.DST_ATOP]=[s.ONE_MINUS_DST_ALPHA,s.SRC_ALPHA],t[H.XOR]=[s.ONE_MINUS_DST_ALPHA,s.ONE_MINUS_SRC_ALPHA],t[H.SUBTRACT]=[s.ONE,s.ONE,s.ONE,s.ONE,s.FUNC_REVERSE_SUBTRACT,s.FUNC_ADD],t}const bd=0,Td=1,Ed=2,Ad=3,wd=4,Sd=5,qo=class Qn{constructor(){this.gl=null,this.stateId=0,this.polygonOffset=0,this.blendMode=H.NONE,this._blendEq=!1,this.map=[],this.map[bd]=this.setBlend,this.map[Td]=this.setOffset,this.map[Ed]=this.setCullFace,this.map[Ad]=this.setDepthTest,this.map[wd]=this.setFrontFace,this.map[Sd]=this.setDepthMask,this.checks=[],this.defaultState=new Zt,this.defaultState.blend=!0}contextChange(t){this.gl=t,this.blendModes=xd(t),this.set(this.defaultState),this.reset()}set(t){if(t=t||this.defaultState,this.stateId!==t.data){let e=this.stateId^t.data,i=0;for(;e;)e&1&&this.map[i].call(this,!!(t.data&1<>1,i++;this.stateId=t.data}for(let e=0;et.systems[n]),r=[...i,...Object.keys(t.systems).filter(n=>!i.includes(n))];for(const n of r)this.addSystem(t.systems[n],n)}addRunners(...t){t.forEach(e=>{this.runners[e]=new St(e)})}addSystem(t,e){const i=new t(this);if(this[e])throw new Error(`Whoops! The name "${e}" is already in use`);this[e]=i,this._systemsHash[e]=i;for(const r in this.runners)this.runners[r].add(i);return this}emitWithCustomOptions(t,e){const i=Object.keys(this._systemsHash);t.items.forEach(r=>{const n=i.find(a=>this._systemsHash[a]===r);r[t.name](e[n])})}destroy(){Object.values(this.runners).forEach(t=>{t.destroy()}),this._systemsHash={}}}const Si=class rr{constructor(t){this.renderer=t,this.count=0,this.checkCount=0,this.maxIdle=rr.defaultMaxIdle,this.checkCountMax=rr.defaultCheckCountMax,this.mode=rr.defaultMode}postrender(){this.renderer.objectRenderer.renderingToScreen&&(this.count++,this.mode!==Qi.MANUAL&&(this.checkCount++,this.checkCount>this.checkCountMax&&(this.checkCount=0,this.run())))}run(){const t=this.renderer.texture,e=t.managedTextures;let i=!1;for(let r=0;rthis.maxIdle&&(t.destroyTexture(n,!0),e[r]=null,i=!0)}if(i){let r=0;for(let n=0;n=0;r--)this.unload(t.children[r])}destroy(){this.renderer=null}};Si.defaultMode=Qi.AUTO,Si.defaultMaxIdle=3600,Si.defaultCheckCountMax=600,Si.extension={type:R.RendererSystem,name:"textureGC"};let be=Si;L.add(be);class Is{constructor(t){this.texture=t,this.width=-1,this.height=-1,this.dirtyId=-1,this.dirtyStyleId=-1,this.mipmap=!1,this.wrapMode=33071,this.type=k.UNSIGNED_BYTE,this.internalFormat=A.RGBA,this.samplerType=0}}function Id(s){let t;return"WebGL2RenderingContext"in globalThis&&s instanceof globalThis.WebGL2RenderingContext?t={[s.RGB]:D.FLOAT,[s.RGBA]:D.FLOAT,[s.ALPHA]:D.FLOAT,[s.LUMINANCE]:D.FLOAT,[s.LUMINANCE_ALPHA]:D.FLOAT,[s.R8]:D.FLOAT,[s.R8_SNORM]:D.FLOAT,[s.RG8]:D.FLOAT,[s.RG8_SNORM]:D.FLOAT,[s.RGB8]:D.FLOAT,[s.RGB8_SNORM]:D.FLOAT,[s.RGB565]:D.FLOAT,[s.RGBA4]:D.FLOAT,[s.RGB5_A1]:D.FLOAT,[s.RGBA8]:D.FLOAT,[s.RGBA8_SNORM]:D.FLOAT,[s.RGB10_A2]:D.FLOAT,[s.RGB10_A2UI]:D.FLOAT,[s.SRGB8]:D.FLOAT,[s.SRGB8_ALPHA8]:D.FLOAT,[s.R16F]:D.FLOAT,[s.RG16F]:D.FLOAT,[s.RGB16F]:D.FLOAT,[s.RGBA16F]:D.FLOAT,[s.R32F]:D.FLOAT,[s.RG32F]:D.FLOAT,[s.RGB32F]:D.FLOAT,[s.RGBA32F]:D.FLOAT,[s.R11F_G11F_B10F]:D.FLOAT,[s.RGB9_E5]:D.FLOAT,[s.R8I]:D.INT,[s.R8UI]:D.UINT,[s.R16I]:D.INT,[s.R16UI]:D.UINT,[s.R32I]:D.INT,[s.R32UI]:D.UINT,[s.RG8I]:D.INT,[s.RG8UI]:D.UINT,[s.RG16I]:D.INT,[s.RG16UI]:D.UINT,[s.RG32I]:D.INT,[s.RG32UI]:D.UINT,[s.RGB8I]:D.INT,[s.RGB8UI]:D.UINT,[s.RGB16I]:D.INT,[s.RGB16UI]:D.UINT,[s.RGB32I]:D.INT,[s.RGB32UI]:D.UINT,[s.RGBA8I]:D.INT,[s.RGBA8UI]:D.UINT,[s.RGBA16I]:D.INT,[s.RGBA16UI]:D.UINT,[s.RGBA32I]:D.INT,[s.RGBA32UI]:D.UINT,[s.DEPTH_COMPONENT16]:D.FLOAT,[s.DEPTH_COMPONENT24]:D.FLOAT,[s.DEPTH_COMPONENT32F]:D.FLOAT,[s.DEPTH_STENCIL]:D.FLOAT,[s.DEPTH24_STENCIL8]:D.FLOAT,[s.DEPTH32F_STENCIL8]:D.FLOAT}:t={[s.RGB]:D.FLOAT,[s.RGBA]:D.FLOAT,[s.ALPHA]:D.FLOAT,[s.LUMINANCE]:D.FLOAT,[s.LUMINANCE_ALPHA]:D.FLOAT,[s.DEPTH_STENCIL]:D.FLOAT},t}function Rd(s){let t;return"WebGL2RenderingContext"in globalThis&&s instanceof globalThis.WebGL2RenderingContext?t={[k.UNSIGNED_BYTE]:{[A.RGBA]:s.RGBA8,[A.RGB]:s.RGB8,[A.RG]:s.RG8,[A.RED]:s.R8,[A.RGBA_INTEGER]:s.RGBA8UI,[A.RGB_INTEGER]:s.RGB8UI,[A.RG_INTEGER]:s.RG8UI,[A.RED_INTEGER]:s.R8UI,[A.ALPHA]:s.ALPHA,[A.LUMINANCE]:s.LUMINANCE,[A.LUMINANCE_ALPHA]:s.LUMINANCE_ALPHA},[k.BYTE]:{[A.RGBA]:s.RGBA8_SNORM,[A.RGB]:s.RGB8_SNORM,[A.RG]:s.RG8_SNORM,[A.RED]:s.R8_SNORM,[A.RGBA_INTEGER]:s.RGBA8I,[A.RGB_INTEGER]:s.RGB8I,[A.RG_INTEGER]:s.RG8I,[A.RED_INTEGER]:s.R8I},[k.UNSIGNED_SHORT]:{[A.RGBA_INTEGER]:s.RGBA16UI,[A.RGB_INTEGER]:s.RGB16UI,[A.RG_INTEGER]:s.RG16UI,[A.RED_INTEGER]:s.R16UI,[A.DEPTH_COMPONENT]:s.DEPTH_COMPONENT16},[k.SHORT]:{[A.RGBA_INTEGER]:s.RGBA16I,[A.RGB_INTEGER]:s.RGB16I,[A.RG_INTEGER]:s.RG16I,[A.RED_INTEGER]:s.R16I},[k.UNSIGNED_INT]:{[A.RGBA_INTEGER]:s.RGBA32UI,[A.RGB_INTEGER]:s.RGB32UI,[A.RG_INTEGER]:s.RG32UI,[A.RED_INTEGER]:s.R32UI,[A.DEPTH_COMPONENT]:s.DEPTH_COMPONENT24},[k.INT]:{[A.RGBA_INTEGER]:s.RGBA32I,[A.RGB_INTEGER]:s.RGB32I,[A.RG_INTEGER]:s.RG32I,[A.RED_INTEGER]:s.R32I},[k.FLOAT]:{[A.RGBA]:s.RGBA32F,[A.RGB]:s.RGB32F,[A.RG]:s.RG32F,[A.RED]:s.R32F,[A.DEPTH_COMPONENT]:s.DEPTH_COMPONENT32F},[k.HALF_FLOAT]:{[A.RGBA]:s.RGBA16F,[A.RGB]:s.RGB16F,[A.RG]:s.RG16F,[A.RED]:s.R16F},[k.UNSIGNED_SHORT_5_6_5]:{[A.RGB]:s.RGB565},[k.UNSIGNED_SHORT_4_4_4_4]:{[A.RGBA]:s.RGBA4},[k.UNSIGNED_SHORT_5_5_5_1]:{[A.RGBA]:s.RGB5_A1},[k.UNSIGNED_INT_2_10_10_10_REV]:{[A.RGBA]:s.RGB10_A2,[A.RGBA_INTEGER]:s.RGB10_A2UI},[k.UNSIGNED_INT_10F_11F_11F_REV]:{[A.RGB]:s.R11F_G11F_B10F},[k.UNSIGNED_INT_5_9_9_9_REV]:{[A.RGB]:s.RGB9_E5},[k.UNSIGNED_INT_24_8]:{[A.DEPTH_STENCIL]:s.DEPTH24_STENCIL8},[k.FLOAT_32_UNSIGNED_INT_24_8_REV]:{[A.DEPTH_STENCIL]:s.DEPTH32F_STENCIL8}}:t={[k.UNSIGNED_BYTE]:{[A.RGBA]:s.RGBA,[A.RGB]:s.RGB,[A.ALPHA]:s.ALPHA,[A.LUMINANCE]:s.LUMINANCE,[A.LUMINANCE_ALPHA]:s.LUMINANCE_ALPHA},[k.UNSIGNED_SHORT_5_6_5]:{[A.RGB]:s.RGB},[k.UNSIGNED_SHORT_4_4_4_4]:{[A.RGBA]:s.RGBA},[k.UNSIGNED_SHORT_5_5_5_1]:{[A.RGBA]:s.RGBA}},t}class cn{constructor(t){this.renderer=t,this.boundTextures=[],this.currentLocation=-1,this.managedTextures=[],this._unknownBoundTextures=!1,this.unknownTexture=new X,this.hasIntegerTextures=!1}contextChange(){const t=this.gl=this.renderer.gl;this.CONTEXT_UID=this.renderer.CONTEXT_UID,this.webGLVersion=this.renderer.context.webGLVersion,this.internalFormats=Rd(t),this.samplerTypes=Id(t);const e=t.getParameter(t.MAX_TEXTURE_IMAGE_UNITS);this.boundTextures.length=e;for(let r=0;r=0;--n){const a=e[n];a&&a._glTextures[r].samplerType!==D.FLOAT&&this.renderer.texture.unbind(a)}}initTexture(t){const e=new Is(this.gl.createTexture());return e.dirtyId=-1,t._glTextures[this.CONTEXT_UID]=e,this.managedTextures.push(t),t.on("dispose",this.destroyTexture,this),e}initTextureType(t,e){var i,r,n;e.internalFormat=(r=(i=this.internalFormats[t.type])==null?void 0:i[t.format])!=null?r:t.format,e.samplerType=(n=this.samplerTypes[e.internalFormat])!=null?n:D.FLOAT,this.webGLVersion===2&&t.type===k.HALF_FLOAT?e.type=this.gl.HALF_FLOAT:e.type=t.type}updateTexture(t){var e;const i=t._glTextures[this.CONTEXT_UID];if(!i)return;const r=this.renderer;if(this.initTextureType(t,i),(e=t.resource)!=null&&e.upload(r,t,i))i.samplerType!==D.FLOAT&&(this.hasIntegerTextures=!0);else{const n=t.realWidth,a=t.realHeight,o=r.gl;(i.width!==n||i.height!==a||i.dirtyId<0)&&(i.width=n,i.height=a,o.texImage2D(t.target,0,i.internalFormat,n,a,0,t.format,i.type,null))}t.dirtyStyleId!==i.dirtyStyleId&&this.updateTextureStyle(t),i.dirtyId=t.dirtyId}destroyTexture(t,e){const{gl:i}=this;if(t=t.castToBaseTexture(),t._glTextures[this.CONTEXT_UID]&&(this.unbind(t),i.deleteTexture(t._glTextures[this.CONTEXT_UID].texture),t.off("dispose",this.destroyTexture,this),delete t._glTextures[this.CONTEXT_UID],!e)){const r=this.managedTextures.indexOf(t);r!==-1&&Ce(this.managedTextures,r,1)}}updateTextureStyle(t){var e;const i=t._glTextures[this.CONTEXT_UID];i&&((t.mipmap===Ut.POW2||this.webGLVersion!==2)&&!t.isPowerOfTwo?i.mipmap=!1:i.mipmap=t.mipmap>=1,this.webGLVersion!==2&&!t.isPowerOfTwo?i.wrapMode=Wt.CLAMP:i.wrapMode=t.wrapMode,(e=t.resource)!=null&&e.style(this.renderer,t,i)||this.setStyle(t,i),i.dirtyStyleId=t.dirtyStyleId)}setStyle(t,e){const i=this.gl;if(e.mipmap&&t.mipmap!==Ut.ON_MANUAL&&i.generateMipmap(t.target),i.texParameteri(t.target,i.TEXTURE_WRAP_S,e.wrapMode),i.texParameteri(t.target,i.TEXTURE_WRAP_T,e.wrapMode),e.mipmap){i.texParameteri(t.target,i.TEXTURE_MIN_FILTER,t.scaleMode===zt.LINEAR?i.LINEAR_MIPMAP_LINEAR:i.NEAREST_MIPMAP_NEAREST);const r=this.renderer.context.extensions.anisotropicFiltering;if(r&&t.anisotropicLevel>0&&t.scaleMode===zt.LINEAR){const n=Math.min(t.anisotropicLevel,i.getParameter(r.MAX_TEXTURE_MAX_ANISOTROPY_EXT));i.texParameterf(t.target,r.TEXTURE_MAX_ANISOTROPY_EXT,n)}}else i.texParameteri(t.target,i.TEXTURE_MIN_FILTER,t.scaleMode===zt.LINEAR?i.LINEAR:i.NEAREST);i.texParameteri(t.target,i.TEXTURE_MAG_FILTER,t.scaleMode===zt.LINEAR?i.LINEAR:i.NEAREST)}destroy(){this.renderer=null}}cn.extension={type:R.RendererSystem,name:"texture"},L.add(cn);class dn{constructor(t){this.renderer=t}contextChange(){this.gl=this.renderer.gl,this.CONTEXT_UID=this.renderer.CONTEXT_UID}bind(t){const{gl:e,CONTEXT_UID:i}=this,r=t._glTransformFeedbacks[i]||this.createGLTransformFeedback(t);e.bindTransformFeedback(e.TRANSFORM_FEEDBACK,r)}unbind(){const{gl:t}=this;t.bindTransformFeedback(t.TRANSFORM_FEEDBACK,null)}beginTransformFeedback(t,e){const{gl:i,renderer:r}=this;e&&r.shader.bind(e),i.beginTransformFeedback(t)}endTransformFeedback(){const{gl:t}=this;t.endTransformFeedback()}createGLTransformFeedback(t){const{gl:e,renderer:i,CONTEXT_UID:r}=this,n=e.createTransformFeedback();t._glTransformFeedbacks[r]=n,e.bindTransformFeedback(e.TRANSFORM_FEEDBACK,n);for(let a=0;at in s?Cd(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,Rs=(s,t)=>{for(var e in t||(t={}))Pd.call(t,e)&&Jo(s,e,t[e]);if(Qo)for(var e of Qo(t))Md.call(t,e)&&Jo(s,e,t[e]);return s};O.PREFER_ENV=_e.WEBGL2,O.STRICT_TEXTURE_CACHE=!1,O.RENDER_OPTIONS=Rs(Rs(Rs(Rs({},Ei.defaultOptions),Ti.defaultOptions),Ii.defaultOptions),wi.defaultOptions),Object.defineProperties(O,{WRAP_MODE:{get(){return X.defaultOptions.wrapMode},set(s){X.defaultOptions.wrapMode=s}},SCALE_MODE:{get(){return X.defaultOptions.scaleMode},set(s){X.defaultOptions.scaleMode=s}},MIPMAP_TEXTURES:{get(){return X.defaultOptions.mipmap},set(s){X.defaultOptions.mipmap=s}},ANISOTROPIC_LEVEL:{get(){return X.defaultOptions.anisotropicLevel},set(s){X.defaultOptions.anisotropicLevel=s}},FILTER_RESOLUTION:{get(){return yt.defaultResolution},set(s){yt.defaultResolution=s}},FILTER_MULTISAMPLE:{get(){return yt.defaultMultisample},set(s){yt.defaultMultisample=s}},SPRITE_MAX_TEXTURES:{get(){return ye.defaultMaxTextures},set(s){ye.defaultMaxTextures=s}},SPRITE_BATCH_SIZE:{get(){return ye.defaultBatchSize},set(s){ye.defaultBatchSize=s}},CAN_UPLOAD_SAME_BUFFER:{get(){return ye.canUploadSameBuffer},set(s){ye.canUploadSameBuffer=s}},GC_MODE:{get(){return be.defaultMode},set(s){be.defaultMode=s}},GC_MAX_IDLE:{get(){return be.defaultMaxIdle},set(s){be.defaultMaxIdle=s}},GC_MAX_CHECK_COUNT:{get(){return be.defaultCheckCountMax},set(s){be.defaultCheckCountMax=s}},PRECISION_VERTEX:{get(){return Qt.defaultVertexPrecision},set(s){Qt.defaultVertexPrecision=s}},PRECISION_FRAGMENT:{get(){return Qt.defaultFragmentPrecision},set(s){Qt.defaultFragmentPrecision=s}}});var le=(s=>(s[s.INTERACTION=50]="INTERACTION",s[s.HIGH=25]="HIGH",s[s.NORMAL=0]="NORMAL",s[s.LOW=-25]="LOW",s[s.UTILITY=-50]="UTILITY",s))(le||{});class fn{constructor(t,e=null,i=0,r=!1){this.next=null,this.previous=null,this._destroyed=!1,this.fn=t,this.context=e,this.priority=i,this.once=r}match(t,e=null){return this.fn===t&&this.context===e}emit(t){this.fn&&(this.context?this.fn.call(this.context,t):this.fn(t));const e=this.next;return this.once&&this.destroy(!0),this._destroyed&&(this.next=null),e}connect(t){this.previous=t,t.next&&(t.next.previous=this),this.next=t.next,t.next=this}destroy(t=!1){this._destroyed=!0,this.fn=null,this.context=null,this.previous&&(this.previous.next=this.next),this.next&&(this.next.previous=this.previous);const e=this.next;return this.next=t?null:e,this.previous=null,e}}const th=class Pt{constructor(){this.autoStart=!1,this.deltaTime=1,this.lastTime=-1,this.speed=1,this.started=!1,this._requestId=null,this._maxElapsedMS=100,this._minElapsedMS=0,this._protected=!1,this._lastFrame=-1,this._head=new fn(null,null,1/0),this.deltaMS=1/Pt.targetFPMS,this.elapsedMS=1/Pt.targetFPMS,this._tick=t=>{this._requestId=null,this.started&&(this.update(t),this.started&&this._requestId===null&&this._head.next&&(this._requestId=requestAnimationFrame(this._tick)))}}_requestIfNeeded(){this._requestId===null&&this._head.next&&(this.lastTime=performance.now(),this._lastFrame=this.lastTime,this._requestId=requestAnimationFrame(this._tick))}_cancelIfNeeded(){this._requestId!==null&&(cancelAnimationFrame(this._requestId),this._requestId=null)}_startIfPossible(){this.started?this._requestIfNeeded():this.autoStart&&this.start()}add(t,e,i=le.NORMAL){return this._addListener(new fn(t,e,i))}addOnce(t,e,i=le.NORMAL){return this._addListener(new fn(t,e,i,!0))}_addListener(t){let e=this._head.next,i=this._head;if(!e)t.connect(i);else{for(;e;){if(t.priority>e.priority){t.connect(i);break}i=e,e=e.next}t.previous||t.connect(i)}return this._startIfPossible(),this}remove(t,e){let i=this._head.next;for(;i;)i.match(t,e)?i=i.destroy():i=i.next;return this._head.next||this._cancelIfNeeded(),this}get count(){if(!this._head)return 0;let t=0,e=this._head;for(;e=e.next;)t++;return t}start(){this.started||(this.started=!0,this._requestIfNeeded())}stop(){this.started&&(this.started=!1,this._cancelIfNeeded())}destroy(){if(!this._protected){this.stop();let t=this._head.next;for(;t;)t=t.destroy(!0);this._head.destroy(),this._head=null}}update(t=performance.now()){let e;if(t>this.lastTime){if(e=this.elapsedMS=t-this.lastTime,e>this._maxElapsedMS&&(e=this._maxElapsedMS),e*=this.speed,this._minElapsedMS){const n=t-this._lastFrame|0;if(n{this._ticker.stop()},this.start=()=>{this._ticker.start()},this._ticker=null,this.ticker=t.sharedTicker?mt.shared:new mt,t.autoStart&&this.start()}static destroy(){if(this._ticker){const t=this._ticker;this.ticker=null,t.destroy()}}}pn.extension=R.Application,L.add(pn);const eh=[];L.handleByList(R.Renderer,eh);function ih(s){for(const t of eh)if(t.test(s))return new t(s);throw new Error("Unable to auto-detect a suitable renderer.")}var Dd=`attribute vec2 aVertexPosition; +attribute vec2 aTextureCoord; + +uniform mat3 projectionMatrix; + +varying vec2 vTextureCoord; + +void main(void) +{ + gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); + vTextureCoord = aTextureCoord; +}`,Od=`attribute vec2 aVertexPosition; + +uniform mat3 projectionMatrix; + +varying vec2 vTextureCoord; + +uniform vec4 inputSize; +uniform vec4 outputFrame; + +vec4 filterVertexPosition( void ) +{ + vec2 position = aVertexPosition * max(outputFrame.zw, vec2(0.)) + outputFrame.xy; + + return vec4((projectionMatrix * vec3(position, 1.0)).xy, 0.0, 1.0); +} + +vec2 filterTextureCoord( void ) +{ + return aVertexPosition * (outputFrame.zw * inputSize.zw); +} + +void main(void) +{ + gl_Position = filterVertexPosition(); + vTextureCoord = filterTextureCoord(); +} +`;const sh=Dd,mn=Od;class gn{constructor(t){this.renderer=t}contextChange(t){let e;if(this.renderer.context.webGLVersion===1){const i=t.getParameter(t.FRAMEBUFFER_BINDING);t.bindFramebuffer(t.FRAMEBUFFER,null),e=t.getParameter(t.SAMPLES),t.bindFramebuffer(t.FRAMEBUFFER,i)}else{const i=t.getParameter(t.DRAW_FRAMEBUFFER_BINDING);t.bindFramebuffer(t.DRAW_FRAMEBUFFER,null),e=t.getParameter(t.SAMPLES),t.bindFramebuffer(t.DRAW_FRAMEBUFFER,i)}e>=at.HIGH?this.multisample=at.HIGH:e>=at.MEDIUM?this.multisample=at.MEDIUM:e>=at.LOW?this.multisample=at.LOW:this.multisample=at.NONE}destroy(){}}gn.extension={type:R.RendererSystem,name:"_multisample"},L.add(gn);class Bd{constructor(t){this.buffer=t||null,this.updateID=-1,this.byteLength=-1,this.refCount=0}}class _n{constructor(t){this.renderer=t,this.managedBuffers={},this.boundBufferBases={}}destroy(){this.renderer=null}contextChange(){this.disposeAll(!0),this.gl=this.renderer.gl,this.CONTEXT_UID=this.renderer.CONTEXT_UID}bind(t){const{gl:e,CONTEXT_UID:i}=this,r=t._glBuffers[i]||this.createGLBuffer(t);e.bindBuffer(t.type,r.buffer)}unbind(t){const{gl:e}=this;e.bindBuffer(t,null)}bindBufferBase(t,e){const{gl:i,CONTEXT_UID:r}=this;if(this.boundBufferBases[e]!==t){const n=t._glBuffers[r]||this.createGLBuffer(t);this.boundBufferBases[e]=t,i.bindBufferBase(i.UNIFORM_BUFFER,e,n.buffer)}}bindBufferRange(t,e,i){const{gl:r,CONTEXT_UID:n}=this;i=i||0;const a=t._glBuffers[n]||this.createGLBuffer(t);r.bindBufferRange(r.UNIFORM_BUFFER,e||0,a.buffer,i*256,256)}update(t){const{gl:e,CONTEXT_UID:i}=this,r=t._glBuffers[i]||this.createGLBuffer(t);if(t._updateID!==r.updateID)if(r.updateID=t._updateID,e.bindBuffer(t.type,r.buffer),r.byteLength>=t.data.byteLength)e.bufferSubData(t.type,0,t.data);else{const n=t.static?e.STATIC_DRAW:e.DYNAMIC_DRAW;r.byteLength=t.data.byteLength,e.bufferData(t.type,t.data,n)}}dispose(t,e){if(!this.managedBuffers[t.id])return;delete this.managedBuffers[t.id];const i=t._glBuffers[this.CONTEXT_UID],r=this.gl;t.disposeRunner.remove(this),i&&(e||r.deleteBuffer(i.buffer),delete t._glBuffers[this.CONTEXT_UID])}disposeAll(t){const e=Object.keys(this.managedBuffers);for(let i=0;ie.resource).filter(e=>e).map(e=>e.load());return this._load=Promise.all(t).then(()=>{const{realWidth:e,realHeight:i}=this.items[0];return this.resize(e,i),this.update(),Promise.resolve(this)}),this._load}}class rh extends yn{constructor(t,e){const{width:i,height:r}=e||{};let n,a;Array.isArray(t)?(n=t,a=t.length):a=t,super(a,{width:i,height:r}),n&&this.initFromArray(n,e)}addBaseTextureAt(t,e){if(t.resource)this.addResourceAt(t.resource,e);else throw new Error("ArrayResource does not support RenderTexture");return this}bind(t){super.bind(t),t.target=Ie.TEXTURE_2D_ARRAY}upload(t,e,i){const{length:r,itemDirtyIds:n,items:a}=this,{gl:o}=t;i.dirtyId<0&&o.texImage3D(o.TEXTURE_2D_ARRAY,0,i.internalFormat,this._width,this._height,r,0,e.format,i.type,null);for(let h=0;h0)if(t.resource)this.addResourceAt(t.resource,e);else throw new Error("CubeResource does not support copying of renderTexture.");else t.target=Ie.TEXTURE_CUBE_MAP_POSITIVE_X+e,t.parentTextureArray=this.baseTexture,this.items[e]=t;return t.valid&&!this.valid&&this.resize(t.realWidth,t.realHeight),this.items[e]=t,this}upload(t,e,i){const r=this.itemDirtyIds;for(let n=0;n{if(this.url===null){t(this);return}try{const i=await O.ADAPTER.fetch(this.url,{mode:this.crossOrigin?"cors":"no-cors"});if(this.destroyed)return;const r=await i.blob();if(this.destroyed)return;const n=await createImageBitmap(r,{premultiplyAlpha:this.alphaMode===null||this.alphaMode===bt.UNPACK?"premultiply":"none"});if(this.destroyed){n.close();return}this.source=n,this.update(),t(this)}catch(i){if(this.destroyed)return;e(i),this.onError.emit(i)}}),this._load)}upload(t,e,i){return this.source instanceof ImageBitmap?(typeof this.alphaMode=="number"&&(e.alphaMode=this.alphaMode),super.upload(t,e,i)):(this.load(),!1)}dispose(){this.ownsImageBitmap&&this.source instanceof ImageBitmap&&this.source.close(),super.dispose(),this._load=null}static test(t){return!!globalThis.createImageBitmap&&typeof ImageBitmap!="undefined"&&(typeof t=="string"||t instanceof ImageBitmap)}static get EMPTY(){var t;return Le._EMPTY=(t=Le._EMPTY)!=null?t:O.ADAPTER.createCanvas(0,0),Le._EMPTY}}const xn=class nr extends he{constructor(t,e){e=e||{},super(O.ADAPTER.createCanvas()),this._width=0,this._height=0,this.svg=t,this.scale=e.scale||1,this._overrideWidth=e.width,this._overrideHeight=e.height,this._resolve=null,this._crossorigin=e.crossorigin,this._load=null,e.autoLoad!==!1&&this.load()}load(){return this._load?this._load:(this._load=new Promise(t=>{if(this._resolve=()=>{this.update(),t(this)},nr.SVG_XML.test(this.svg.trim())){if(!btoa)throw new Error("Your browser doesn't support base64 conversions.");this.svg=`data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(this.svg)))}`}this._loadSvg()}),this._load)}_loadSvg(){const t=new Image;he.crossOrigin(t,this.svg,this._crossorigin),t.src=this.svg,t.onerror=e=>{this._resolve&&(t.onerror=null,this.onError.emit(e))},t.onload=()=>{if(!this._resolve)return;const e=t.width,i=t.height;if(!e||!i)throw new Error("The SVG image must have width and height defined (in pixels), canvas API needs them.");let r=e*this.scale,n=i*this.scale;(this._overrideWidth||this._overrideHeight)&&(r=this._overrideWidth||this._overrideHeight/i*e,n=this._overrideHeight||this._overrideWidth/e*i),r=Math.round(r),n=Math.round(n);const a=this.source;a.width=r,a.height=n,a._pixiId=`canvas_${ve()}`,a.getContext("2d").drawImage(t,0,0,e,i,0,0,r,n),this._resolve(),this._resolve=null}}static getSize(t){const e=nr.SVG_SIZE.exec(t),i={};return e&&(i[e[1]]=Math.round(parseFloat(e[3])),i[e[5]]=Math.round(parseFloat(e[7]))),i}dispose(){super.dispose(),this._resolve=null,this._crossorigin=null}static test(t,e){return e==="svg"||typeof t=="string"&&t.startsWith("data:image/svg+xml")||typeof t=="string"&&nr.SVG_XML.test(t)}};xn.SVG_XML=/^(<\?xml[^?]+\?>)?\s*()]*-->)?\s*\]*(?:\s(width|height)=('|")(\d*(?:\.\d+)?)(?:px)?('|"))[^>]*(?:\s(width|height)=('|")(\d*(?:\.\d+)?)(?:px)?('|"))[^>]*>/i;let Ms=xn;const bn=class ta extends he{constructor(t,e){if(e=e||{},!(t instanceof HTMLVideoElement)){const i=document.createElement("video");e.autoLoad!==!1&&i.setAttribute("preload","auto"),e.playsinline!==!1&&(i.setAttribute("webkit-playsinline",""),i.setAttribute("playsinline","")),e.muted===!0&&(i.setAttribute("muted",""),i.muted=!0),e.loop===!0&&i.setAttribute("loop",""),e.autoPlay!==!1&&i.setAttribute("autoplay",""),typeof t=="string"&&(t=[t]);const r=t[0].src||t[0];he.crossOrigin(i,r,e.crossorigin);for(let n=0;n{this.valid?e(this):(this._resolve=e,this._reject=i,t.load())}),this._load}_onError(t){this.source.removeEventListener("error",this._onError,!0),this.onError.emit(t),this._reject&&(this._reject(t),this._reject=null,this._resolve=null)}_isSourcePlaying(){const t=this.source;return!t.paused&&!t.ended}_isSourceReady(){return this.source.readyState>2}_onPlayStart(){this.valid||this._onCanPlay(),this._configureAutoUpdate()}_onPlayStop(){this._configureAutoUpdate()}_onSeeked(){this._autoUpdate&&!this._isSourcePlaying()&&(this._msToNextUpdate=0,this.update(),this._msToNextUpdate=0)}_onCanPlay(){const t=this.source;t.removeEventListener("canplay",this._onCanPlay),t.removeEventListener("canplaythrough",this._onCanPlay);const e=this.valid;this._msToNextUpdate=0,this.update(),this._msToNextUpdate=0,!e&&this._resolve&&(this._resolve(this),this._resolve=null,this._reject=null),this._isSourcePlaying()?this._onPlayStart():this.autoPlay&&t.play()}dispose(){this._configureAutoUpdate();const t=this.source;t&&(t.removeEventListener("play",this._onPlayStart),t.removeEventListener("pause",this._onPlayStop),t.removeEventListener("seeked",this._onSeeked),t.removeEventListener("canplay",this._onCanPlay),t.removeEventListener("canplaythrough",this._onCanPlay),t.removeEventListener("error",this._onError,!0),t.pause(),t.src="",t.load()),super.dispose()}get autoUpdate(){return this._autoUpdate}set autoUpdate(t){t!==this._autoUpdate&&(this._autoUpdate=t,this._configureAutoUpdate())}get updateFPS(){return this._updateFPS}set updateFPS(t){t!==this._updateFPS&&(this._updateFPS=t,this._configureAutoUpdate())}_configureAutoUpdate(){this._autoUpdate&&this._isSourcePlaying()?!this._updateFPS&&this.source.requestVideoFrameCallback?(this._isConnectedToTicker&&(mt.shared.remove(this.update,this),this._isConnectedToTicker=!1,this._msToNextUpdate=0),this._videoFrameRequestCallbackHandle===null&&(this._videoFrameRequestCallbackHandle=this.source.requestVideoFrameCallback(this._videoFrameRequestCallback))):(this._videoFrameRequestCallbackHandle!==null&&(this.source.cancelVideoFrameCallback(this._videoFrameRequestCallbackHandle),this._videoFrameRequestCallbackHandle=null),this._isConnectedToTicker||(mt.shared.add(this.update,this),this._isConnectedToTicker=!0,this._msToNextUpdate=0)):(this._videoFrameRequestCallbackHandle!==null&&(this.source.cancelVideoFrameCallback(this._videoFrameRequestCallbackHandle),this._videoFrameRequestCallbackHandle=null),this._isConnectedToTicker&&(mt.shared.remove(this.update,this),this._isConnectedToTicker=!1,this._msToNextUpdate=0))}static test(t,e){return globalThis.HTMLVideoElement&&t instanceof HTMLVideoElement||ta.TYPES.includes(e)}};bn.TYPES=["mp4","m4v","webm","ogg","ogv","h264","avi","mov"],bn.MIME_TYPES={ogv:"video/ogg",mov:"video/quicktime",m4v:"video/mp4"};let Tn=bn;us.push(Le,Yr,nh,Tn,Ms,mi,oh,rh);class Fd{constructor(){this._glTransformFeedbacks={},this.buffers=[],this.disposeRunner=new St("disposeTransformFeedback")}bindBuffer(t,e){this.buffers[t]=e}destroy(){this.disposeRunner.emit(this,!1)}}const Nd="7.3.2";class Ri{constructor(){this.minX=1/0,this.minY=1/0,this.maxX=-1/0,this.maxY=-1/0,this.rect=null,this.updateID=-1}isEmpty(){return this.minX>this.maxX||this.minY>this.maxY}clear(){this.minX=1/0,this.minY=1/0,this.maxX=-1/0,this.maxY=-1/0}getRectangle(t){return this.minX>this.maxX||this.minY>this.maxY?j.EMPTY:(t=t||new j(0,0,1,1),t.x=this.minX,t.y=this.minY,t.width=this.maxX-this.minX,t.height=this.maxY-this.minY,t)}addPoint(t){this.minX=Math.min(this.minX,t.x),this.maxX=Math.max(this.maxX,t.x),this.minY=Math.min(this.minY,t.y),this.maxY=Math.max(this.maxY,t.y)}addPointMatrix(t,e){const{a:i,b:r,c:n,d:a,tx:o,ty:h}=t,l=i*e.x+n*e.y+o,u=r*e.x+a*e.y+h;this.minX=Math.min(this.minX,l),this.maxX=Math.max(this.maxX,l),this.minY=Math.min(this.minY,u),this.maxY=Math.max(this.maxY,u)}addQuad(t){let e=this.minX,i=this.minY,r=this.maxX,n=this.maxY,a=t[0],o=t[1];e=ar?a:r,n=o>n?o:n,a=t[2],o=t[3],e=ar?a:r,n=o>n?o:n,a=t[4],o=t[5],e=ar?a:r,n=o>n?o:n,a=t[6],o=t[7],e=ar?a:r,n=o>n?o:n,this.minX=e,this.minY=i,this.maxX=r,this.maxY=n}addFrame(t,e,i,r,n){this.addFrameMatrix(t.worldTransform,e,i,r,n)}addFrameMatrix(t,e,i,r,n){const a=t.a,o=t.b,h=t.c,l=t.d,u=t.tx,c=t.ty;let d=this.minX,f=this.minY,p=this.maxX,m=this.maxY,g=a*e+h*i+u,y=o*e+l*i+c;d=gp?g:p,m=y>m?y:m,g=a*r+h*i+u,y=o*r+l*i+c,d=gp?g:p,m=y>m?y:m,g=a*e+h*n+u,y=o*e+l*n+c,d=gp?g:p,m=y>m?y:m,g=a*r+h*n+u,y=o*r+l*n+c,d=gp?g:p,m=y>m?y:m,this.minX=d,this.minY=f,this.maxX=p,this.maxY=m}addVertexData(t,e,i){let r=this.minX,n=this.minY,a=this.maxX,o=this.maxY;for(let h=e;ha?l:a,o=u>o?u:o}this.minX=r,this.minY=n,this.maxX=a,this.maxY=o}addVertices(t,e,i,r){this.addVerticesMatrix(t.worldTransform,e,i,r)}addVerticesMatrix(t,e,i,r,n=0,a=n){const o=t.a,h=t.b,l=t.c,u=t.d,c=t.tx,d=t.ty;let f=this.minX,p=this.minY,m=this.maxX,g=this.maxY;for(let y=i;yr?t.maxX:r,this.maxY=t.maxY>n?t.maxY:n}addBoundsMask(t,e){const i=t.minX>e.minX?t.minX:e.minX,r=t.minY>e.minY?t.minY:e.minY,n=t.maxXl?n:l,this.maxY=a>u?a:u}}addBoundsMatrix(t,e){this.addFrameMatrix(e,t.minX,t.minY,t.maxX,t.maxY)}addBoundsArea(t,e){const i=t.minX>e.x?t.minX:e.x,r=t.minY>e.y?t.minY:e.y,n=t.maxXl?n:l,this.maxY=a>u?a:u}}pad(t=0,e=t){this.isEmpty()||(this.minX-=t,this.maxX+=t,this.minY-=e,this.maxY+=e)}addFramePad(t,e,i,r,n,a){t-=n,e-=a,i+=n,r+=a,this.minX=this.minXi?this.maxX:i,this.minY=this.minYr?this.maxY:r}}class it extends Ve{constructor(){super(),this.tempDisplayObjectParent=null,this.transform=new _s,this.alpha=1,this.visible=!0,this.renderable=!0,this.cullable=!1,this.cullArea=null,this.parent=null,this.worldAlpha=1,this._lastSortedIndex=0,this._zIndex=0,this.filterArea=null,this.filters=null,this._enabledFilters=null,this._bounds=new Ri,this._localBounds=null,this._boundsID=0,this._boundsRect=null,this._localBoundsRect=null,this._mask=null,this._maskRefCount=0,this._destroyed=!1,this.isSprite=!1,this.isMask=!1}static mixin(t){const e=Object.keys(t);for(let i=0;i1)for(let e=0;ethis.children.length)throw new Error(`${t}addChildAt: The index ${e} supplied is out of bounds ${this.children.length}`);return t.parent&&t.parent.removeChild(t),t.parent=this,this.sortDirty=!0,t.transform._parentID=-1,this.children.splice(e,0,t),this._boundsID++,this.onChildrenChange(e),t.emit("added",this),this.emit("childAdded",t,this,e),t}swapChildren(t,e){if(t===e)return;const i=this.getChildIndex(t),r=this.getChildIndex(e);this.children[i]=e,this.children[r]=t,this.onChildrenChange(i=this.children.length)throw new Error(`The index ${e} supplied is out of bounds ${this.children.length}`);const i=this.getChildIndex(t);Ce(this.children,i,1),this.children.splice(e,0,t),this.onChildrenChange(e)}getChildAt(t){if(t<0||t>=this.children.length)throw new Error(`getChildAt: Index (${t}) does not exist.`);return this.children[t]}removeChild(...t){if(t.length>1)for(let e=0;e0&&n<=r){a=this.children.splice(i,n);for(let o=0;o1&&this.children.sort(Ud),this.sortDirty=!1}updateTransform(){this.sortableChildren&&this.sortDirty&&this.sortChildren(),this._boundsID++,this.transform.updateTransform(this.parent.transform),this.worldAlpha=this.alpha*this.parent.worldAlpha;for(let t=0,e=this.children.length;t0&&e.height>0))return;let i,r;this.cullArea?(i=this.cullArea,r=this.worldTransform):this._render!==ea.prototype._render&&(i=this.getBounds(!0));const n=t.projection.transform;if(n&&(r?(r=Ld.copyFrom(r),r.prepend(n)):r=n),i&&e.intersects(i,r))this._render(t);else if(this.cullArea)return;for(let a=0,o=this.children.length;a=r&&Ci.x=n&&Ci.y=e&&(a=s-o-1),h=h.replace("%value%",t[a].toString()),r+=h,r+=` +`}return i=i.replace("%blur%",r),i=i.replace("%size%",s.toString()),i}const jd=` + attribute vec2 aVertexPosition; + + uniform mat3 projectionMatrix; + + uniform float strength; + + varying vec2 vBlurTexCoords[%size%]; + + uniform vec4 inputSize; + uniform vec4 outputFrame; + + vec4 filterVertexPosition( void ) + { + vec2 position = aVertexPosition * max(outputFrame.zw, vec2(0.)) + outputFrame.xy; + + return vec4((projectionMatrix * vec3(position, 1.0)).xy, 0.0, 1.0); + } + + vec2 filterTextureCoord( void ) + { + return aVertexPosition * (outputFrame.zw * inputSize.zw); + } + + void main(void) + { + gl_Position = filterVertexPosition(); + + vec2 textureCoord = filterTextureCoord(); + %blur% + }`;function zd(s,t){const e=Math.ceil(s/2);let i=jd,r="",n;t?n="vBlurTexCoords[%index%] = textureCoord + vec2(%sampleIndex% * strength, 0.0);":n="vBlurTexCoords[%index%] = textureCoord + vec2(0.0, %sampleIndex% * strength);";for(let a=0;a 0.0) { + c.rgb /= c.a; + } + + vec4 result; + + result.r = (m[0] * c.r); + result.r += (m[1] * c.g); + result.r += (m[2] * c.b); + result.r += (m[3] * c.a); + result.r += m[4]; + + result.g = (m[5] * c.r); + result.g += (m[6] * c.g); + result.g += (m[7] * c.b); + result.g += (m[8] * c.a); + result.g += m[9]; + + result.b = (m[10] * c.r); + result.b += (m[11] * c.g); + result.b += (m[12] * c.b); + result.b += (m[13] * c.a); + result.b += m[14]; + + result.a = (m[15] * c.r); + result.a += (m[16] * c.g); + result.a += (m[17] * c.b); + result.a += (m[18] * c.a); + result.a += m[19]; + + vec3 rgb = mix(c.rgb, result.rgb, uAlpha); + + // Premultiply alpha again. + rgb *= result.a; + + gl_FragColor = vec4(rgb, result.a); +} +`;class Os extends yt{constructor(){const t={m:new Float32Array([1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0]),uAlpha:1};super(mn,Wd,t),this.alpha=1}_loadMatrix(t,e=!1){let i=t;e&&(this._multiply(i,this.uniforms.m,t),i=this._colorMatrix(i)),this.uniforms.m=i}_multiply(t,e,i){return t[0]=e[0]*i[0]+e[1]*i[5]+e[2]*i[10]+e[3]*i[15],t[1]=e[0]*i[1]+e[1]*i[6]+e[2]*i[11]+e[3]*i[16],t[2]=e[0]*i[2]+e[1]*i[7]+e[2]*i[12]+e[3]*i[17],t[3]=e[0]*i[3]+e[1]*i[8]+e[2]*i[13]+e[3]*i[18],t[4]=e[0]*i[4]+e[1]*i[9]+e[2]*i[14]+e[3]*i[19]+e[4],t[5]=e[5]*i[0]+e[6]*i[5]+e[7]*i[10]+e[8]*i[15],t[6]=e[5]*i[1]+e[6]*i[6]+e[7]*i[11]+e[8]*i[16],t[7]=e[5]*i[2]+e[6]*i[7]+e[7]*i[12]+e[8]*i[17],t[8]=e[5]*i[3]+e[6]*i[8]+e[7]*i[13]+e[8]*i[18],t[9]=e[5]*i[4]+e[6]*i[9]+e[7]*i[14]+e[8]*i[19]+e[9],t[10]=e[10]*i[0]+e[11]*i[5]+e[12]*i[10]+e[13]*i[15],t[11]=e[10]*i[1]+e[11]*i[6]+e[12]*i[11]+e[13]*i[16],t[12]=e[10]*i[2]+e[11]*i[7]+e[12]*i[12]+e[13]*i[17],t[13]=e[10]*i[3]+e[11]*i[8]+e[12]*i[13]+e[13]*i[18],t[14]=e[10]*i[4]+e[11]*i[9]+e[12]*i[14]+e[13]*i[19]+e[14],t[15]=e[15]*i[0]+e[16]*i[5]+e[17]*i[10]+e[18]*i[15],t[16]=e[15]*i[1]+e[16]*i[6]+e[17]*i[11]+e[18]*i[16],t[17]=e[15]*i[2]+e[16]*i[7]+e[17]*i[12]+e[18]*i[17],t[18]=e[15]*i[3]+e[16]*i[8]+e[17]*i[13]+e[18]*i[18],t[19]=e[15]*i[4]+e[16]*i[9]+e[17]*i[14]+e[18]*i[19]+e[19],t}_colorMatrix(t){const e=new Float32Array(t);return e[4]/=255,e[9]/=255,e[14]/=255,e[19]/=255,e}brightness(t,e){const i=[t,0,0,0,0,0,t,0,0,0,0,0,t,0,0,0,0,0,1,0];this._loadMatrix(i,e)}tint(t,e){const[i,r,n]=Z.shared.setValue(t).toArray(),a=[i,0,0,0,0,0,r,0,0,0,0,0,n,0,0,0,0,0,1,0];this._loadMatrix(a,e)}greyscale(t,e){const i=[t,t,t,0,0,t,t,t,0,0,t,t,t,0,0,0,0,0,1,0];this._loadMatrix(i,e)}blackAndWhite(t){const e=[.3,.6,.1,0,0,.3,.6,.1,0,0,.3,.6,.1,0,0,0,0,0,1,0];this._loadMatrix(e,t)}hue(t,e){t=(t||0)/180*Math.PI;const i=Math.cos(t),r=Math.sin(t),n=Math.sqrt,a=1/3,o=n(a),h=i+(1-i)*a,l=a*(1-i)-o*r,u=a*(1-i)+o*r,c=a*(1-i)+o*r,d=i+a*(1-i),f=a*(1-i)-o*r,p=a*(1-i)-o*r,m=a*(1-i)+o*r,g=i+a*(1-i),y=[h,l,u,0,0,c,d,f,0,0,p,m,g,0,0,0,0,0,1,0];this._loadMatrix(y,e)}contrast(t,e){const i=(t||0)+1,r=-.5*(i-1),n=[i,0,0,0,r,0,i,0,0,r,0,0,i,0,r,0,0,0,1,0];this._loadMatrix(n,e)}saturate(t=0,e){const i=t*2/3+1,r=(i-1)*-.5,n=[i,r,r,0,0,r,i,r,0,0,r,r,i,0,0,0,0,0,1,0];this._loadMatrix(n,e)}desaturate(){this.saturate(-1)}negative(t){const e=[-1,0,0,1,0,0,-1,0,1,0,0,0,-1,1,0,0,0,0,1,0];this._loadMatrix(e,t)}sepia(t){const e=[.393,.7689999,.18899999,0,0,.349,.6859999,.16799999,0,0,.272,.5339999,.13099999,0,0,0,0,0,1,0];this._loadMatrix(e,t)}technicolor(t){const e=[1.9125277891456083,-.8545344976951645,-.09155508482755585,0,11.793603434377337,-.3087833385928097,1.7658908555458428,-.10601743074722245,0,-70.35205161461398,-.231103377548616,-.7501899197440212,1.847597816108189,0,30.950940869491138,0,0,0,1,0];this._loadMatrix(e,t)}polaroid(t){const e=[1.438,-.062,-.062,0,0,-.122,1.378,-.122,0,0,-.016,-.016,1.483,0,0,0,0,0,1,0];this._loadMatrix(e,t)}toBGR(t){const e=[0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0];this._loadMatrix(e,t)}kodachrome(t){const e=[1.1285582396593525,-.3967382283601348,-.03992559172921793,0,63.72958762196502,-.16404339962244616,1.0835251566291304,-.05498805115633132,0,24.732407896706203,-.16786010706155763,-.5603416277695248,1.6014850761964943,0,35.62982807460946,0,0,0,1,0];this._loadMatrix(e,t)}browni(t){const e=[.5997023498159715,.34553243048391263,-.2708298674538042,0,47.43192855600873,-.037703249837783157,.8609577587992641,.15059552388459913,0,-36.96841498319127,.24113635128153335,-.07441037908422492,.44972182064877153,0,-7.562075277591283,0,0,0,1,0];this._loadMatrix(e,t)}vintage(t){const e=[.6279345635605994,.3202183420819367,-.03965408211312453,0,9.651285835294123,.02578397704808868,.6441188644374771,.03259127616149294,0,7.462829176470591,.0466055556782719,-.0851232987247891,.5241648018700465,0,5.159190588235296,0,0,0,1,0];this._loadMatrix(e,t)}colorTone(t,e,i,r,n){t=t||.2,e=e||.15,i=i||16770432,r=r||3375104;const a=Z.shared,[o,h,l]=a.setValue(i).toArray(),[u,c,d]=a.setValue(r).toArray(),f=[.3,.59,.11,0,0,o,h,l,t,0,u,c,d,e,0,o-u,h-c,l-d,0,0];this._loadMatrix(f,n)}night(t,e){t=t||.1;const i=[t*-2,-t,0,0,0,-t,0,t,0,0,0,t,t*2,0,0,0,0,0,1,0];this._loadMatrix(i,e)}predator(t,e){const i=[11.224130630493164*t,-4.794486999511719*t,-2.8746118545532227*t,0*t,.40342438220977783*t,-3.6330697536468506*t,9.193157196044922*t,-2.951810836791992*t,0*t,-1.316135048866272*t,-3.2184197902679443*t,-4.2375030517578125*t,7.476448059082031*t,0*t,.8044459223747253*t,0,0,0,1,0];this._loadMatrix(i,e)}lsd(t){const e=[2,-.4,.5,0,0,-.5,2,-.4,0,0,-.4,-.5,3,0,0,0,0,0,1,0];this._loadMatrix(e,t)}reset(){const t=[1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0];this._loadMatrix(t,!1)}get matrix(){return this.uniforms.m}set matrix(t){this.uniforms.m=t}get alpha(){return this.uniforms.uAlpha}set alpha(t){this.uniforms.uAlpha=t}}Os.prototype.grayscale=Os.prototype.greyscale;var Yd=`varying vec2 vFilterCoord; +varying vec2 vTextureCoord; + +uniform vec2 scale; +uniform mat2 rotation; +uniform sampler2D uSampler; +uniform sampler2D mapSampler; + +uniform highp vec4 inputSize; +uniform vec4 inputClamp; + +void main(void) +{ + vec4 map = texture2D(mapSampler, vFilterCoord); + + map -= 0.5; + map.xy = scale * inputSize.zw * (rotation * map.xy); + + gl_FragColor = texture2D(uSampler, clamp(vec2(vTextureCoord.x + map.x, vTextureCoord.y + map.y), inputClamp.xy, inputClamp.zw)); +} +`,qd=`attribute vec2 aVertexPosition; + +uniform mat3 projectionMatrix; +uniform mat3 filterMatrix; + +varying vec2 vTextureCoord; +varying vec2 vFilterCoord; + +uniform vec4 inputSize; +uniform vec4 outputFrame; + +vec4 filterVertexPosition( void ) +{ + vec2 position = aVertexPosition * max(outputFrame.zw, vec2(0.)) + outputFrame.xy; + + return vec4((projectionMatrix * vec3(position, 1.0)).xy, 0.0, 1.0); +} + +vec2 filterTextureCoord( void ) +{ + return aVertexPosition * (outputFrame.zw * inputSize.zw); +} + +void main(void) +{ + gl_Position = filterVertexPosition(); + vTextureCoord = filterTextureCoord(); + vFilterCoord = ( filterMatrix * vec3( vTextureCoord, 1.0) ).xy; +} +`;class fh extends yt{constructor(t,e){const i=new tt;t.renderable=!1,super(qd,Yd,{mapSampler:t._texture,filterMatrix:i,scale:{x:1,y:1},rotation:new Float32Array([1,0,0,1])}),this.maskSprite=t,this.maskMatrix=i,e==null&&(e=20),this.scale=new q(e,e)}apply(t,e,i,r){this.uniforms.filterMatrix=t.calculateSpriteMatrix(this.maskMatrix,this.maskSprite),this.uniforms.scale.x=this.scale.x,this.uniforms.scale.y=this.scale.y;const n=this.maskSprite.worldTransform,a=Math.sqrt(n.a*n.a+n.b*n.b),o=Math.sqrt(n.c*n.c+n.d*n.d);a!==0&&o!==0&&(this.uniforms.rotation[0]=n.a/a,this.uniforms.rotation[1]=n.b/a,this.uniforms.rotation[2]=n.c/o,this.uniforms.rotation[3]=n.d/o),t.applyFilter(this,e,i,r)}get map(){return this.uniforms.mapSampler}set map(t){this.uniforms.mapSampler=t}}var Kd=`varying vec2 v_rgbNW; +varying vec2 v_rgbNE; +varying vec2 v_rgbSW; +varying vec2 v_rgbSE; +varying vec2 v_rgbM; + +varying vec2 vFragCoord; +uniform sampler2D uSampler; +uniform highp vec4 inputSize; + + +/** + Basic FXAA implementation based on the code on geeks3d.com with the + modification that the texture2DLod stuff was removed since it's + unsupported by WebGL. + + -- + + From: + https://github.com/mitsuhiko/webgl-meincraft + + Copyright (c) 2011 by Armin Ronacher. + + Some rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * The names of the contributors may not be used to endorse or + promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FXAA_REDUCE_MIN +#define FXAA_REDUCE_MIN (1.0/ 128.0) +#endif +#ifndef FXAA_REDUCE_MUL +#define FXAA_REDUCE_MUL (1.0 / 8.0) +#endif +#ifndef FXAA_SPAN_MAX +#define FXAA_SPAN_MAX 8.0 +#endif + +//optimized version for mobile, where dependent +//texture reads can be a bottleneck +vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 inverseVP, + vec2 v_rgbNW, vec2 v_rgbNE, + vec2 v_rgbSW, vec2 v_rgbSE, + vec2 v_rgbM) { + vec4 color; + vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; + vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; + vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; + vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; + vec4 texColor = texture2D(tex, v_rgbM); + vec3 rgbM = texColor.xyz; + vec3 luma = vec3(0.299, 0.587, 0.114); + float lumaNW = dot(rgbNW, luma); + float lumaNE = dot(rgbNE, luma); + float lumaSW = dot(rgbSW, luma); + float lumaSE = dot(rgbSE, luma); + float lumaM = dot(rgbM, luma); + float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); + float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); + + mediump vec2 dir; + dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); + dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); + + float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * + (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); + + float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); + dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), + max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), + dir * rcpDirMin)) * inverseVP; + + vec3 rgbA = 0.5 * ( + texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + + texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); + vec3 rgbB = rgbA * 0.5 + 0.25 * ( + texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + + texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); + + float lumaB = dot(rgbB, luma); + if ((lumaB < lumaMin) || (lumaB > lumaMax)) + color = vec4(rgbA, texColor.a); + else + color = vec4(rgbB, texColor.a); + return color; +} + +void main() { + + vec4 color; + + color = fxaa(uSampler, vFragCoord, inputSize.zw, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); + + gl_FragColor = color; +} +`,Zd=` +attribute vec2 aVertexPosition; + +uniform mat3 projectionMatrix; + +varying vec2 v_rgbNW; +varying vec2 v_rgbNE; +varying vec2 v_rgbSW; +varying vec2 v_rgbSE; +varying vec2 v_rgbM; + +varying vec2 vFragCoord; + +uniform vec4 inputSize; +uniform vec4 outputFrame; + +vec4 filterVertexPosition( void ) +{ + vec2 position = aVertexPosition * max(outputFrame.zw, vec2(0.)) + outputFrame.xy; + + return vec4((projectionMatrix * vec3(position, 1.0)).xy, 0.0, 1.0); +} + +void texcoords(vec2 fragCoord, vec2 inverseVP, + out vec2 v_rgbNW, out vec2 v_rgbNE, + out vec2 v_rgbSW, out vec2 v_rgbSE, + out vec2 v_rgbM) { + v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP; + v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP; + v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP; + v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP; + v_rgbM = vec2(fragCoord * inverseVP); +} + +void main(void) { + + gl_Position = filterVertexPosition(); + + vFragCoord = aVertexPosition * outputFrame.zw; + + texcoords(vFragCoord, inputSize.zw, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); +} +`;class ph extends yt{constructor(){super(Zd,Kd)}}var Qd=`precision highp float; + +varying vec2 vTextureCoord; +varying vec4 vColor; + +uniform float uNoise; +uniform float uSeed; +uniform sampler2D uSampler; + +float rand(vec2 co) +{ + return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); +} + +void main() +{ + vec4 color = texture2D(uSampler, vTextureCoord); + float randomValue = rand(gl_FragCoord.xy * uSeed); + float diff = (randomValue - 0.5) * uNoise; + + // Un-premultiply alpha before applying the color matrix. See issue #3539. + if (color.a > 0.0) { + color.rgb /= color.a; + } + + color.r += diff; + color.g += diff; + color.b += diff; + + // Premultiply alpha again. + color.rgb *= color.a; + + gl_FragColor = color; +} +`;class mh extends yt{constructor(t=.5,e=Math.random()){super(mn,Qd,{uNoise:0,uSeed:0}),this.noise=t,this.seed=e}get noise(){return this.uniforms.uNoise}set noise(t){this.uniforms.uNoise=t}get seed(){return this.uniforms.uSeed}set seed(t){this.uniforms.uSeed=t}}const En={AlphaFilter:ch,BlurFilter:dh,BlurFilterPass:Ds,ColorMatrixFilter:Os,DisplacementFilter:fh,FXAAFilter:ph,NoiseFilter:mh};Object.entries(En).forEach(([s,t])=>{Object.defineProperty(En,s,{get(){return Oa("7.1.0",`filters.${s} has moved to ${s}`),t}})});let Jd=class{constructor(){this.interactionFrequency=10,this._deltaTime=0,this._didMove=!1,this.tickerAdded=!1,this._pauseUpdate=!0}init(t){this.removeTickerListener(),this.events=t,this.interactionFrequency=10,this._deltaTime=0,this._didMove=!1,this.tickerAdded=!1,this._pauseUpdate=!0}get pauseUpdate(){return this._pauseUpdate}set pauseUpdate(t){this._pauseUpdate=t}addTickerListener(){this.tickerAdded||!this.domElement||(mt.system.add(this.tickerUpdate,this,le.INTERACTION),this.tickerAdded=!0)}removeTickerListener(){this.tickerAdded&&(mt.system.remove(this.tickerUpdate,this),this.tickerAdded=!1)}pointerMoved(){this._didMove=!0}update(){if(!this.domElement||this._pauseUpdate)return;if(this._didMove){this._didMove=!1;return}const t=this.events.rootPointerEvent;this.events.supportsTouchEvents&&t.pointerType==="touch"||globalThis.document.dispatchEvent(new PointerEvent("pointermove",{clientX:t.clientX,clientY:t.clientY}))}tickerUpdate(t){this._deltaTime+=t,!(this._deltaTimei.priority-r.priority)}dispatchEvent(t,e){t.propagationStopped=!1,t.propagationImmediatelyStopped=!1,this.propagate(t,e),this.dispatch.emit(e||t.type,t)}mapEvent(t){if(!this.rootTarget)return;const e=this.mappingTable[t.type];if(e)for(let i=0,r=e.length;i=0;r--)if(t.currentTarget=i[r],this.notifyTarget(t,e),t.propagationStopped||t.propagationImmediatelyStopped)return}}all(t,e,i=this._allInteractiveElements){if(i.length===0)return;t.eventPhase=t.BUBBLING_PHASE;const r=Array.isArray(e)?e:[e];for(let n=i.length-1;n>=0;n--)r.forEach(a=>{t.currentTarget=i[n],this.notifyTarget(t,a)})}propagationPath(t){const e=[t];for(let i=0;i=0;c--){const d=u[c],f=this.hitTestMoveRecursive(d,this._isInteractive(e)?e:d.eventMode,i,r,n,a||n(t,i));if(f){if(f.length>0&&!f[f.length-1].parent)continue;const p=t.isInteractive();(f.length>0||p)&&(p&&this._allInteractiveElements.push(t),f.push(t)),this._hitElements.length===0&&(this._hitElements=f),o=!0}}}const h=this._isInteractive(e),l=t.isInteractive();return h&&l&&this._allInteractiveElements.push(t),a||this._hitElements.length>0?null:o?this._hitElements:h&&!n(t,i)&&r(t,i)?l?[t]:[]:null}hitTestRecursive(t,e,i,r,n){if(this._interactivePrune(t)||n(t,i))return null;if((t.eventMode==="dynamic"||e==="dynamic")&&(Te.pauseUpdate=!1),t.interactiveChildren&&t.children){const h=t.children;for(let l=h.length-1;l>=0;l--){const u=h[l],c=this.hitTestRecursive(u,this._isInteractive(e)?e:u.eventMode,i,r,n);if(c){if(c.length>0&&!c[c.length-1].parent)continue;const d=t.isInteractive();return(c.length>0||d)&&c.push(t),c}}}const a=this._isInteractive(e),o=t.isInteractive();return a&&r(t,i)?o?[t]:[]:null}_isInteractive(t){return t==="static"||t==="dynamic"}_interactivePrune(t){return!!(!t||t.isMask||!t.visible||!t.renderable||t.eventMode==="none"||t.eventMode==="passive"&&!t.interactiveChildren||t.isMask)}hitPruneFn(t,e){var i;if(t.hitArea&&(t.worldTransform.applyInverse(e,An),!t.hitArea.contains(An.x,An.y)))return!0;if(t._mask){const r=t._mask.isMaskData?t._mask.maskObject:t._mask;if(r&&!((i=r.containsPoint)!=null&&i.call(r,e)))return!0}return!1}hitTestFn(t,e){return t.eventMode==="passive"?!1:t.hitArea?!0:t.containsPoint?t.containsPoint(e):!1}notifyTarget(t,e){var i,r;e=e!=null?e:t.type;const n=`on${e}`;(r=(i=t.currentTarget)[n])==null||r.call(i,t);const a=t.eventPhase===t.CAPTURING_PHASE||t.eventPhase===t.AT_TARGET?`${e}capture`:e;this.notifyListeners(t,a),t.eventPhase===t.AT_TARGET&&this.notifyListeners(t,e)}mapPointerDown(t){if(!(t instanceof Bt)){console.warn("EventBoundary cannot map a non-pointer event as a pointer event");return}const e=this.createPointerEvent(t);if(this.dispatchEvent(e,"pointerdown"),e.pointerType==="touch")this.dispatchEvent(e,"touchstart");else if(e.pointerType==="mouse"||e.pointerType==="pen"){const r=e.button===2;this.dispatchEvent(e,r?"rightdown":"mousedown")}const i=this.trackingData(t.pointerId);i.pressTargetsByButton[t.button]=e.composedPath(),this.freeEvent(e)}mapPointerMove(t){var e,i,r;if(!(t instanceof Bt)){console.warn("EventBoundary cannot map a non-pointer event as a pointer event");return}this._allInteractiveElements.length=0,this._hitElements.length=0,this._isPointerMoveEvent=!0;const n=this.createPointerEvent(t);this._isPointerMoveEvent=!1;const a=n.pointerType==="mouse"||n.pointerType==="pen",o=this.trackingData(t.pointerId),h=this.findMountedTarget(o.overTargets);if(((e=o.overTargets)==null?void 0:e.length)>0&&h!==n.target){const c=t.type==="mousemove"?"mouseout":"pointerout",d=this.createPointerEvent(t,c,h);if(this.dispatchEvent(d,"pointerout"),a&&this.dispatchEvent(d,"mouseout"),!n.composedPath().includes(h)){const f=this.createPointerEvent(t,"pointerleave",h);for(f.eventPhase=f.AT_TARGET;f.target&&!n.composedPath().includes(f.target);)f.currentTarget=f.target,this.notifyTarget(f),a&&this.notifyTarget(f,"mouseleave"),f.target=f.target.parent;this.freeEvent(f)}this.freeEvent(d)}if(h!==n.target){const c=t.type==="mousemove"?"mouseover":"pointerover",d=this.clonePointerEvent(n,c);this.dispatchEvent(d,"pointerover"),a&&this.dispatchEvent(d,"mouseover");let f=h==null?void 0:h.parent;for(;f&&f!==this.rootTarget.parent&&f!==n.target;)f=f.parent;if(!f||f===this.rootTarget.parent){const p=this.clonePointerEvent(n,"pointerenter");for(p.eventPhase=p.AT_TARGET;p.target&&p.target!==h&&p.target!==this.rootTarget.parent;)p.currentTarget=p.target,this.notifyTarget(p),a&&this.notifyTarget(p,"mouseenter"),p.target=p.target.parent;this.freeEvent(p)}this.freeEvent(d)}const l=[],u=(i=this.enableGlobalMoveEvents)!=null?i:!0;this.moveOnAll?l.push("pointermove"):this.dispatchEvent(n,"pointermove"),u&&l.push("globalpointermove"),n.pointerType==="touch"&&(this.moveOnAll?l.splice(1,0,"touchmove"):this.dispatchEvent(n,"touchmove"),u&&l.push("globaltouchmove")),a&&(this.moveOnAll?l.splice(1,0,"mousemove"):this.dispatchEvent(n,"mousemove"),u&&l.push("globalmousemove"),this.cursor=(r=n.target)==null?void 0:r.cursor),l.length>0&&this.all(n,l),this._allInteractiveElements.length=0,this._hitElements.length=0,o.overTargets=n.composedPath(),this.freeEvent(n)}mapPointerOver(t){var e;if(!(t instanceof Bt)){console.warn("EventBoundary cannot map a non-pointer event as a pointer event");return}const i=this.trackingData(t.pointerId),r=this.createPointerEvent(t),n=r.pointerType==="mouse"||r.pointerType==="pen";this.dispatchEvent(r,"pointerover"),n&&this.dispatchEvent(r,"mouseover"),r.pointerType==="mouse"&&(this.cursor=(e=r.target)==null?void 0:e.cursor);const a=this.clonePointerEvent(r,"pointerenter");for(a.eventPhase=a.AT_TARGET;a.target&&a.target!==this.rootTarget.parent;)a.currentTarget=a.target,this.notifyTarget(a),n&&this.notifyTarget(a,"mouseenter"),a.target=a.target.parent;i.overTargets=r.composedPath(),this.freeEvent(r),this.freeEvent(a)}mapPointerOut(t){if(!(t instanceof Bt)){console.warn("EventBoundary cannot map a non-pointer event as a pointer event");return}const e=this.trackingData(t.pointerId);if(e.overTargets){const i=t.pointerType==="mouse"||t.pointerType==="pen",r=this.findMountedTarget(e.overTargets),n=this.createPointerEvent(t,"pointerout",r);this.dispatchEvent(n),i&&this.dispatchEvent(n,"mouseout");const a=this.createPointerEvent(t,"pointerleave",r);for(a.eventPhase=a.AT_TARGET;a.target&&a.target!==this.rootTarget.parent;)a.currentTarget=a.target,this.notifyTarget(a),i&&this.notifyTarget(a,"mouseleave"),a.target=a.target.parent;e.overTargets=null,this.freeEvent(n),this.freeEvent(a)}this.cursor=null}mapPointerUp(t){if(!(t instanceof Bt)){console.warn("EventBoundary cannot map a non-pointer event as a pointer event");return}const e=performance.now(),i=this.createPointerEvent(t);if(this.dispatchEvent(i,"pointerup"),i.pointerType==="touch")this.dispatchEvent(i,"touchend");else if(i.pointerType==="mouse"||i.pointerType==="pen"){const o=i.button===2;this.dispatchEvent(i,o?"rightup":"mouseup")}const r=this.trackingData(t.pointerId),n=this.findMountedTarget(r.pressTargetsByButton[t.button]);let a=n;if(n&&!i.composedPath().includes(n)){let o=n;for(;o&&!i.composedPath().includes(o);){if(i.currentTarget=o,this.notifyTarget(i,"pointerupoutside"),i.pointerType==="touch")this.notifyTarget(i,"touchendoutside");else if(i.pointerType==="mouse"||i.pointerType==="pen"){const h=i.button===2;this.notifyTarget(i,h?"rightupoutside":"mouseupoutside")}o=o.parent}delete r.pressTargetsByButton[t.button],a=o}if(a){const o=this.clonePointerEvent(i,"click");o.target=a,o.path=null,r.clicksByButton[t.button]||(r.clicksByButton[t.button]={clickCount:0,target:o.target,timeStamp:e});const h=r.clicksByButton[t.button];if(h.target===o.target&&e-h.timeStamp<200?++h.clickCount:h.clickCount=1,h.target=o.target,h.timeStamp=e,o.detail=h.clickCount,o.pointerType==="mouse"){const l=o.button===2;this.dispatchEvent(o,l?"rightclick":"click")}else o.pointerType==="touch"&&this.dispatchEvent(o,"tap");this.dispatchEvent(o,"pointertap"),this.freeEvent(o)}this.freeEvent(i)}mapPointerUpOutside(t){if(!(t instanceof Bt)){console.warn("EventBoundary cannot map a non-pointer event as a pointer event");return}const e=this.trackingData(t.pointerId),i=this.findMountedTarget(e.pressTargetsByButton[t.button]),r=this.createPointerEvent(t);if(i){let n=i;for(;n;)r.currentTarget=n,this.notifyTarget(r,"pointerupoutside"),r.pointerType==="touch"?this.notifyTarget(r,"touchendoutside"):(r.pointerType==="mouse"||r.pointerType==="pen")&&this.notifyTarget(r,r.button===2?"rightupoutside":"mouseupoutside"),n=n.parent;delete e.pressTargetsByButton[t.button]}this.freeEvent(r)}mapWheel(t){if(!(t instanceof Ue)){console.warn("EventBoundary cannot map a non-wheel event as a wheel event");return}const e=this.createWheelEvent(t);this.dispatchEvent(e),this.freeEvent(e)}findMountedTarget(t){if(!t)return null;let e=t[0];for(let i=1;it in s?sf(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,af=(s,t)=>{for(var e in t||(t={}))rf.call(t,e)&&vh(s,e,t[e]);if(_h)for(var e of _h(t))nf.call(t,e)&&vh(s,e,t[e]);return s};const of=1,hf={touchstart:"pointerdown",touchend:"pointerup",touchendoutside:"pointerupoutside",touchmove:"pointermove",touchcancel:"pointercancel"},wn=class ia{constructor(t){this.supportsTouchEvents="ontouchstart"in globalThis,this.supportsPointerEvents=!!globalThis.PointerEvent,this.domElement=null,this.resolution=1,this.renderer=t,this.rootBoundary=new gh(null),Te.init(this),this.autoPreventDefault=!0,this.eventsAdded=!1,this.rootPointerEvent=new Bt(null),this.rootWheelEvent=new Ue(null),this.cursorStyles={default:"inherit",pointer:"pointer"},this.features=new Proxy(af({},ia.defaultEventFeatures),{set:(e,i,r)=>(i==="globalMove"&&(this.rootBoundary.enableGlobalMoveEvents=r),e[i]=r,!0)}),this.onPointerDown=this.onPointerDown.bind(this),this.onPointerMove=this.onPointerMove.bind(this),this.onPointerUp=this.onPointerUp.bind(this),this.onPointerOverOut=this.onPointerOverOut.bind(this),this.onWheel=this.onWheel.bind(this)}static get defaultEventMode(){return this._defaultEventMode}init(t){var e,i;const{view:r,resolution:n}=this.renderer;this.setTargetElement(r),this.resolution=n,ia._defaultEventMode=(e=t.eventMode)!=null?e:"auto",Object.assign(this.features,(i=t.eventFeatures)!=null?i:{}),this.rootBoundary.enableGlobalMoveEvents=this.features.globalMove}resolutionChange(t){this.resolution=t}destroy(){this.setTargetElement(null),this.renderer=null}setCursor(t){t=t||"default";let e=!0;if(globalThis.OffscreenCanvas&&this.domElement instanceof OffscreenCanvas&&(e=!1),this.currentCursor===t)return;this.currentCursor=t;const i=this.cursorStyles[t];if(i)switch(typeof i){case"string":e&&(this.domElement.style.cursor=i);break;case"function":i(t);break;case"object":e&&Object.assign(this.domElement.style,i);break}else e&&typeof t=="string"&&!Object.prototype.hasOwnProperty.call(this.cursorStyles,t)&&(this.domElement.style.cursor=t)}get pointer(){return this.rootPointerEvent}onPointerDown(t){if(!this.features.click)return;this.rootBoundary.rootTarget=this.renderer.lastObjectRendered;const e=this.normalizeToPointerData(t);this.autoPreventDefault&&e[0].isNormalized&&(t.cancelable||!("cancelable"in t))&&t.preventDefault();for(let i=0,r=e.length;i0&&(e=t.composedPath()[0]);const i=e!==this.domElement?"outside":"",r=this.normalizeToPointerData(t);for(let n=0,a=r.length;n{this._isMobileAccessibility=!0,this.activate(),this.destroyTouchHook()}),document.body.appendChild(t),this._hookDiv=t}destroyTouchHook(){this._hookDiv&&(document.body.removeChild(this._hookDiv),this._hookDiv=null)}activate(){var t;this._isActive||(this._isActive=!0,globalThis.document.addEventListener("mousemove",this._onMouseMove,!0),globalThis.removeEventListener("keydown",this._onKeyDown,!1),this.renderer.on("postrender",this.update,this),(t=this.renderer.view.parentNode)==null||t.appendChild(this.div))}deactivate(){var t;!this._isActive||this._isMobileAccessibility||(this._isActive=!1,globalThis.document.removeEventListener("mousemove",this._onMouseMove,!0),globalThis.addEventListener("keydown",this._onKeyDown,!1),this.renderer.off("postrender",this.update),(t=this.div.parentNode)==null||t.removeChild(this.div))}updateAccessibleObjects(t){if(!t.visible||!t.accessibleChildren)return;t.accessible&&t.isInteractive()&&(t._accessibleActive||this.addChild(t),t.renderId=this.renderId);const e=t.children;if(e)for(let i=0;i title : ${t.title}
tabIndex: ${t.tabIndex}`}capHitArea(t){t.x<0&&(t.width+=t.x,t.x=0),t.y<0&&(t.height+=t.y,t.y=0);const{width:e,height:i}=this.renderer;t.x+t.width>e&&(t.width=e-t.x),t.y+t.height>i&&(t.height=i-t.y)}addChild(t){let e=this.pool.pop();e||(e=document.createElement("button"),e.style.width=`${Fs}px`,e.style.height=`${Fs}px`,e.style.backgroundColor=this.debug?"rgba(255,255,255,0.5)":"transparent",e.style.position="absolute",e.style.zIndex=Th.toString(),e.style.borderStyle="none",navigator.userAgent.toLowerCase().includes("chrome")?e.setAttribute("aria-live","off"):e.setAttribute("aria-live","polite"),navigator.userAgent.match(/rv:.*Gecko\//)?e.setAttribute("aria-relevant","additions"):e.setAttribute("aria-relevant","text"),e.addEventListener("click",this._onClick.bind(this)),e.addEventListener("focus",this._onFocus.bind(this)),e.addEventListener("focusout",this._onFocusOut.bind(this))),e.style.pointerEvents=t.accessiblePointerEvents,e.type=t.accessibleType,t.accessibleTitle&&t.accessibleTitle!==null?e.title=t.accessibleTitle:(!t.accessibleHint||t.accessibleHint===null)&&(e.title=`displayObject ${t.tabIndex}`),t.accessibleHint&&t.accessibleHint!==null&&e.setAttribute("aria-label",t.accessibleHint),this.debug&&this.updateDebugHTML(e),t._accessibleActive=!0,t._accessibleDiv=e,e.displayObject=t,this.children.push(t),this.div.appendChild(t._accessibleDiv),t._accessibleDiv.tabIndex=t.tabIndex}_dispatchEvent(t,e){const{displayObject:i}=t.target,r=this.renderer.events.rootBoundary,n=Object.assign(new Ye(r),{target:i});r.rootTarget=this.renderer.lastObjectRendered,e.forEach(a=>r.dispatchEvent(n,a))}_onClick(t){this._dispatchEvent(t,["click","pointertap","tap"])}_onFocus(t){t.target.getAttribute("aria-live")||t.target.setAttribute("aria-live","assertive"),this._dispatchEvent(t,["mouseover"])}_onFocusOut(t){t.target.getAttribute("aria-live")||t.target.setAttribute("aria-live","polite"),this._dispatchEvent(t,["mouseout"])}_onKeyDown(t){t.keyCode===lf&&this.activate()}_onMouseMove(t){t.movementX===0&&t.movementY===0||this.deactivate()}destroy(){this.destroyTouchHook(),this.div=null,globalThis.document.removeEventListener("mousemove",this._onMouseMove,!0),globalThis.removeEventListener("keydown",this._onKeyDown),this.pool=null,this.children=null,this.renderer=null}}Sn.extension={name:"accessibility",type:[R.RendererPlugin,R.CanvasRendererPlugin]},L.add(Sn);const Ah=class sa{constructor(t){this.stage=new It,t=Object.assign({forceCanvas:!1},t),this.renderer=ih(t),sa._plugins.forEach(e=>{e.init.call(this,t)})}render(){this.renderer.render(this.stage)}get view(){var t;return(t=this.renderer)==null?void 0:t.view}get screen(){var t;return(t=this.renderer)==null?void 0:t.screen}destroy(t,e){const i=sa._plugins.slice(0);i.reverse(),i.forEach(r=>{r.destroy.call(this)}),this.stage.destroy(e),this.stage=null,this.renderer.destroy(t),this.renderer=null}};Ah._plugins=[];let wh=Ah;L.handleByList(R.Application,wh._plugins);class In{static init(t){Object.defineProperty(this,"resizeTo",{set(e){globalThis.removeEventListener("resize",this.queueResize),this._resizeTo=e,e&&(globalThis.addEventListener("resize",this.queueResize),this.resize())},get(){return this._resizeTo}}),this.queueResize=()=>{this._resizeTo&&(this.cancelResize(),this._resizeId=requestAnimationFrame(()=>this.resize()))},this.cancelResize=()=>{this._resizeId&&(cancelAnimationFrame(this._resizeId),this._resizeId=null)},this.resize=()=>{if(!this._resizeTo)return;this.cancelResize();let e,i;if(this._resizeTo===globalThis.window)e=globalThis.innerWidth,i=globalThis.innerHeight;else{const{clientWidth:r,clientHeight:n}=this._resizeTo;e=r,i=n}this.renderer.resize(e,i),this.render()},this._resizeId=null,this._resizeTo=null,this.resizeTo=t.resizeTo||null}static destroy(){globalThis.removeEventListener("resize",this.queueResize),this.cancelResize(),this.cancelResize=null,this.queueResize=null,this.resizeTo=null,this.resize=null}}In.extension=R.Application,L.add(In);const Sh={loader:R.LoadParser,resolver:R.ResolveParser,cache:R.CacheParser,detection:R.DetectionParser};L.handle(R.Asset,s=>{const t=s.ref;Object.entries(Sh).filter(([e])=>!!t[e]).forEach(([e,i])=>{var r;return L.add(Object.assign(t[e],{extension:(r=t[e].extension)!=null?r:i}))})},s=>{const t=s.ref;Object.keys(Sh).filter(e=>!!t[e]).forEach(e=>L.remove(t[e]))});class mf{constructor(t,e=!1){this._loader=t,this._assetList=[],this._isLoading=!1,this._maxConcurrent=1,this.verbose=e}add(t){t.forEach(e=>{this._assetList.push(e)}),this.verbose&&console.log("[BackgroundLoader] assets: ",this._assetList),this._isActive&&!this._isLoading&&this._next()}async _next(){if(this._assetList.length&&this._isActive){this._isLoading=!0;const t=[],e=Math.min(this._assetList.length,this._maxConcurrent);for(let i=0;i(Array.isArray(s)||(s=[s]),t?s.map(i=>typeof i=="string"||e?t(i):i):s),Ns=(s,t)=>{const e=t.split("?")[1];return e&&(s+=`?${e}`),s};function Ih(s,t,e,i,r){const n=t[e];for(let a=0;a{const a=n.substring(1,n.length-1).split(",");r.push(a)}),Ih(s,r,0,e,i)}else i.push(s);return i}const Mi=s=>!Array.isArray(s);let gf=class{constructor(){this._parsers=[],this._cache=new Map,this._cacheMap=new Map}reset(){this._cacheMap.clear(),this._cache.clear()}has(t){return this._cache.has(t)}get(t){return this._cache.get(t)}set(t,e){const i=Ft(t);let r;for(let o=0;o{r[o]=e}));const n=Object.keys(r),a={cacheKeys:n,keys:i};if(i.forEach(o=>{this._cacheMap.set(o,a)}),n.forEach(o=>{this._cache.has(o)&&this._cache.get(o),this._cache.set(o,r[o])}),e instanceof B){const o=e;i.forEach(h=>{o.baseTexture!==B.EMPTY.baseTexture&&X.addToCache(o.baseTexture,h),B.addToCache(o,h)})}}remove(t){if(!this._cacheMap.has(t))return;const e=this._cacheMap.get(t);e.cacheKeys.forEach(i=>{this._cache.delete(i)}),e.keys.forEach(i=>{this._cacheMap.delete(i)})}get parsers(){return this._parsers}};const Ee=new gf;var _f=Object.defineProperty,vf=Object.defineProperties,yf=Object.getOwnPropertyDescriptors,Ch=Object.getOwnPropertySymbols,xf=Object.prototype.hasOwnProperty,bf=Object.prototype.propertyIsEnumerable,Ph=(s,t,e)=>t in s?_f(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,Tf=(s,t)=>{for(var e in t||(t={}))xf.call(t,e)&&Ph(s,e,t[e]);if(Ch)for(var e of Ch(t))bf.call(t,e)&&Ph(s,e,t[e]);return s},Ef=(s,t)=>vf(s,yf(t));class Af{constructor(){this._parsers=[],this._parsersValidated=!1,this.parsers=new Proxy(this._parsers,{set:(t,e,i)=>(this._parsersValidated=!1,t[e]=i,!0)}),this.promiseCache={}}reset(){this._parsersValidated=!1,this.promiseCache={}}_getLoadPromiseAndParser(t,e){const i={promise:null,parser:null};return i.promise=(async()=>{var r,n;let a=null,o=null;if(e.loadParser&&(o=this._parserHash[e.loadParser]),!o){for(let h=0;h({alias:[l],src:l})),o=a.length,h=a.map(async l=>{const u=lt.toAbsolute(l.src);if(!r[l.src])try{this.promiseCache[u]||(this.promiseCache[u]=this._getLoadPromiseAndParser(u,l)),r[l.src]=await this.promiseCache[u].promise,e&&e(++i/o)}catch(c){throw delete this.promiseCache[u],delete r[l.src],new Error(`[Loader.load] Failed to load ${u}. +${c}`)}});return await Promise.all(h),n?r[a[0].src]:r}async unload(t){const e=Ft(t,i=>({alias:[i],src:i})).map(async i=>{var r,n;const a=lt.toAbsolute(i.src),o=this.promiseCache[a];if(o){const h=await o.promise;delete this.promiseCache[a],(n=(r=o.parser)==null?void 0:r.unload)==null||n.call(r,h,i,this)}});await Promise.all(e)}_validateParsers(){this._parsersValidated=!0,this._parserHash=this._parsers.filter(t=>t.name).reduce((t,e)=>(t[e.name],Ef(Tf({},t),{[e.name]:e})),{})}}var Nt=(s=>(s[s.Low=0]="Low",s[s.Normal=1]="Normal",s[s.High=2]="High",s))(Nt||{});const wf=".json",Sf="application/json",Mh={extension:{type:R.LoadParser,priority:Nt.Low},name:"loadJson",test(s){return ke(s,Sf)||ce(s,wf)},async load(s){return await(await O.ADAPTER.fetch(s)).json()}};L.add(Mh);const If=".txt",Rf="text/plain",Dh={name:"loadTxt",extension:{type:R.LoadParser,priority:Nt.Low},test(s){return ke(s,Rf)||ce(s,If)},async load(s){return await(await O.ADAPTER.fetch(s)).text()}};L.add(Dh);var Cf=Object.defineProperty,Pf=Object.defineProperties,Mf=Object.getOwnPropertyDescriptors,Oh=Object.getOwnPropertySymbols,Df=Object.prototype.hasOwnProperty,Of=Object.prototype.propertyIsEnumerable,Bh=(s,t,e)=>t in s?Cf(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,Bf=(s,t)=>{for(var e in t||(t={}))Df.call(t,e)&&Bh(s,e,t[e]);if(Oh)for(var e of Oh(t))Of.call(t,e)&&Bh(s,e,t[e]);return s},Ff=(s,t)=>Pf(s,Mf(t));const Nf=["normal","bold","100","200","300","400","500","600","700","800","900"],Lf=[".ttf",".otf",".woff",".woff2"],Uf=["font/ttf","font/otf","font/woff","font/woff2"],kf=/^(--|-?[A-Z_])[0-9A-Z_-]*$/i;function Fh(s){const t=lt.extname(s),e=lt.basename(s,t).replace(/(-|_)/g," ").toLowerCase().split(" ").map(n=>n.charAt(0).toUpperCase()+n.slice(1));let i=e.length>0;for(const n of e)if(!n.match(kf)){i=!1;break}let r=e.join(" ");return i||(r=`"${r.replace(/[\\"]/g,"\\$&")}"`),r}const Gf=/^[0-9A-Za-z%:/?#\[\]@!\$&'()\*\+,;=\-._~]*$/;function $f(s){return Gf.test(s)?s:encodeURI(s)}const Nh={extension:{type:R.LoadParser,priority:Nt.Low},name:"loadWebFont",test(s){return ke(s,Uf)||ce(s,Lf)},async load(s,t){var e,i,r,n,a,o;const h=O.ADAPTER.getFontFaceSet();if(h){const l=[],u=(i=(e=t.data)==null?void 0:e.family)!=null?i:Fh(s),c=(a=(n=(r=t.data)==null?void 0:r.weights)==null?void 0:n.filter(f=>Nf.includes(f)))!=null?a:["normal"],d=(o=t.data)!=null?o:{};for(let f=0;fO.ADAPTER.getFontFaceSet().delete(t))}};L.add(Nh);let Lh=0,Rn;const Hf="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+ip1sAAAAASUVORK5CYII=",Vf={id:"checkImageBitmap",code:` + async function checkImageBitmap() + { + try + { + if (typeof createImageBitmap !== 'function') return false; + + const response = await fetch('${Hf}'); + const imageBlob = await response.blob(); + const imageBitmap = await createImageBitmap(imageBlob); + + return imageBitmap.width === 1 && imageBitmap.height === 1; + } + catch (e) + { + return false; + } + } + checkImageBitmap().then((result) => { self.postMessage(result); }); + `},Xf={id:"loadImageBitmap",code:` + async function loadImageBitmap(url) + { + const response = await fetch(url); + + if (!response.ok) + { + throw new Error(\`[WorkerManager.loadImageBitmap] Failed to fetch \${url}: \` + + \`\${response.status} \${response.statusText}\`); + } + + const imageBlob = await response.blob(); + const imageBitmap = await createImageBitmap(imageBlob); + + return imageBitmap; + } + self.onmessage = async (event) => + { + try + { + const imageBitmap = await loadImageBitmap(event.data.data[0]); + + self.postMessage({ + data: imageBitmap, + uuid: event.data.uuid, + id: event.data.id, + }, [imageBitmap]); + } + catch(e) + { + self.postMessage({ + error: e, + uuid: event.data.uuid, + id: event.data.id, + }); + } + };`};let Cn,jf=class{constructor(){this._initialized=!1,this._createdWorkers=0,this.workerPool=[],this.queue=[],this.resolveHash={}}isImageBitmapSupported(){return this._isImageBitmapSupported!==void 0?this._isImageBitmapSupported:(this._isImageBitmapSupported=new Promise(t=>{const e=URL.createObjectURL(new Blob([Vf.code],{type:"application/javascript"})),i=new Worker(e);i.addEventListener("message",r=>{i.terminate(),URL.revokeObjectURL(e),t(r.data)})}),this._isImageBitmapSupported)}loadImageBitmap(t){return this._run("loadImageBitmap",[t])}async _initWorkers(){this._initialized||(this._initialized=!0)}getWorker(){Rn===void 0&&(Rn=navigator.hardwareConcurrency||4);let t=this.workerPool.pop();return!t&&this._createdWorkers{this.complete(e.data),this.returnWorker(e.target),this.next()})),t}returnWorker(t){this.workerPool.push(t)}complete(t){t.error!==void 0?this.resolveHash[t.uuid].reject(t.error):this.resolveHash[t.uuid].resolve(t.data),this.resolveHash[t.uuid]=null}async _run(t,e){await this._initWorkers();const i=new Promise((r,n)=>{this.queue.push({id:t,arguments:e,resolve:r,reject:n})});return this.next(),i}next(){if(!this.queue.length)return;const t=this.getWorker();if(!t)return;const e=this.queue.pop(),i=e.id;this.resolveHash[Lh]={resolve:e.resolve,reject:e.reject},t.postMessage({data:e.arguments,uuid:Lh++,id:i})}};const Uh=new jf;function qe(s,t,e){s.resource.internal=!0;const i=new B(s),r=()=>{delete t.promiseCache[e],Ee.has(e)&&Ee.remove(e)};return i.baseTexture.once("destroyed",()=>{e in t.promiseCache&&(console.warn("[Assets] A BaseTexture managed by Assets was destroyed instead of unloaded! Use Assets.unload() instead of destroying the BaseTexture."),r())}),i.once("destroyed",()=>{s.destroyed||(console.warn("[Assets] A Texture managed by Assets was destroyed instead of unloaded! Use Assets.unload() instead of destroying the Texture."),r())}),i}var zf=Object.defineProperty,kh=Object.getOwnPropertySymbols,Wf=Object.prototype.hasOwnProperty,Yf=Object.prototype.propertyIsEnumerable,Gh=(s,t,e)=>t in s?zf(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,$h=(s,t)=>{for(var e in t||(t={}))Wf.call(t,e)&&Gh(s,e,t[e]);if(kh)for(var e of kh(t))Yf.call(t,e)&&Gh(s,e,t[e]);return s};const qf=[".jpeg",".jpg",".png",".webp",".avif"],Kf=["image/jpeg","image/png","image/webp","image/avif"];async function Hh(s){const t=await O.ADAPTER.fetch(s);if(!t.ok)throw new Error(`[loadImageBitmap] Failed to fetch ${s}: ${t.status} ${t.statusText}`);const e=await t.blob();return await createImageBitmap(e)}const Di={name:"loadTextures",extension:{type:R.LoadParser,priority:Nt.High},config:{preferWorkers:!0,preferCreateImageBitmap:!0,crossOrigin:"anonymous"},test(s){return ke(s,Kf)||ce(s,qf)},async load(s,t,e){var i,r;const n=globalThis.createImageBitmap&&this.config.preferCreateImageBitmap;let a;n?this.config.preferWorkers&&await Uh.isImageBitmapSupported()?a=await Uh.loadImageBitmap(s):a=await Hh(s):a=await new Promise((l,u)=>{const c=new Image;c.crossOrigin=this.config.crossOrigin,c.src=s,c.complete?l(c):(c.onload=()=>l(c),c.onerror=d=>u(d))});const o=$h({},t.data);(i=o.resolution)!=null||(o.resolution=Kt(s)),n&&((r=o.resourceOptions)==null?void 0:r.ownsImageBitmap)===void 0&&(o.resourceOptions=$h({},o.resourceOptions),o.resourceOptions.ownsImageBitmap=!0);const h=new X(a,o);return h.resource.src=s,qe(h,e,s)},unload(s){s.destroy(!0)}};L.add(Di);var Zf=Object.defineProperty,Vh=Object.getOwnPropertySymbols,Qf=Object.prototype.hasOwnProperty,Jf=Object.prototype.propertyIsEnumerable,Xh=(s,t,e)=>t in s?Zf(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,tp=(s,t)=>{for(var e in t||(t={}))Qf.call(t,e)&&Xh(s,e,t[e]);if(Vh)for(var e of Vh(t))Jf.call(t,e)&&Xh(s,e,t[e]);return s};const ep=".svg",ip="image/svg+xml",jh={extension:{type:R.LoadParser,priority:Nt.High},name:"loadSVG",test(s){return ke(s,ip)||ce(s,ep)},async testParse(s){return Ms.test(s)},async parse(s,t,e){var i;const r=new Ms(s,(i=t==null?void 0:t.data)==null?void 0:i.resourceOptions);await r.load();const n=new X(r,tp({resolution:Kt(s)},t==null?void 0:t.data));return n.resource.src=t.src,qe(n,e,t.src)},async load(s,t){return(await O.ADAPTER.fetch(s)).text()},unload:Di.unload};L.add(jh);var sp=Object.defineProperty,zh=Object.getOwnPropertySymbols,rp=Object.prototype.hasOwnProperty,np=Object.prototype.propertyIsEnumerable,Wh=(s,t,e)=>t in s?sp(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,Yh=(s,t)=>{for(var e in t||(t={}))rp.call(t,e)&&Wh(s,e,t[e]);if(zh)for(var e of zh(t))np.call(t,e)&&Wh(s,e,t[e]);return s};const ap=[".mp4",".m4v",".webm",".ogv"],op=["video/mp4","video/webm","video/ogg"],qh={name:"loadVideo",extension:{type:R.LoadParser,priority:Nt.High},config:{defaultAutoPlay:!0},test(s){return ke(s,op)||ce(s,ap)},async load(s,t,e){var i;let r;const n=await(await O.ADAPTER.fetch(s)).blob(),a=URL.createObjectURL(n);try{const o=Yh({autoPlay:this.config.defaultAutoPlay},(i=t==null?void 0:t.data)==null?void 0:i.resourceOptions),h=new Tn(a,o);await h.load();const l=new X(h,Yh({alphaMode:await Ba(),resolution:Kt(s)},t==null?void 0:t.data));l.resource.src=s,r=qe(l,e,s),r.baseTexture.once("destroyed",()=>{URL.revokeObjectURL(a)})}catch(o){throw URL.revokeObjectURL(a),o}return r},unload(s){s.destroy(!0)}};L.add(qh);var hp=Object.defineProperty,lp=Object.defineProperties,up=Object.getOwnPropertyDescriptors,Kh=Object.getOwnPropertySymbols,cp=Object.prototype.hasOwnProperty,dp=Object.prototype.propertyIsEnumerable,Zh=(s,t,e)=>t in s?hp(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,Ke=(s,t)=>{for(var e in t||(t={}))cp.call(t,e)&&Zh(s,e,t[e]);if(Kh)for(var e of Kh(t))dp.call(t,e)&&Zh(s,e,t[e]);return s},Qh=(s,t)=>lp(s,up(t));class fp{constructor(){this._defaultBundleIdentifierOptions={connector:"-",createBundleAssetId:(t,e)=>`${t}${this._bundleIdConnector}${e}`,extractAssetIdFromBundle:(t,e)=>e.replace(`${t}${this._bundleIdConnector}`,"")},this._bundleIdConnector=this._defaultBundleIdentifierOptions.connector,this._createBundleAssetId=this._defaultBundleIdentifierOptions.createBundleAssetId,this._extractAssetIdFromBundle=this._defaultBundleIdentifierOptions.extractAssetIdFromBundle,this._assetMap={},this._preferredOrder=[],this._parsers=[],this._resolverHash={},this._bundles={}}setBundleIdentifier(t){var e,i,r;if(this._bundleIdConnector=(e=t.connector)!=null?e:this._bundleIdConnector,this._createBundleAssetId=(i=t.createBundleAssetId)!=null?i:this._createBundleAssetId,this._extractAssetIdFromBundle=(r=t.extractAssetIdFromBundle)!=null?r:this._extractAssetIdFromBundle,this._extractAssetIdFromBundle("foo",this._createBundleAssetId("foo","bar"))!=="bar")throw new Error("[Resolver] GenerateBundleAssetId are not working correctly")}prefer(...t){t.forEach(e=>{this._preferredOrder.push(e),e.priority||(e.priority=Object.keys(e.params))}),this._resolverHash={}}set basePath(t){this._basePath=t}get basePath(){return this._basePath}set rootPath(t){this._rootPath=t}get rootPath(){return this._rootPath}get parsers(){return this._parsers}reset(){this.setBundleIdentifier(this._defaultBundleIdentifierOptions),this._assetMap={},this._preferredOrder=[],this._resolverHash={},this._rootPath=null,this._basePath=null,this._manifest=null,this._bundles={},this._defaultSearchParams=null}setDefaultSearchParams(t){if(typeof t=="string")this._defaultSearchParams=t;else{const e=t;this._defaultSearchParams=Object.keys(e).map(i=>`${encodeURIComponent(i)}=${encodeURIComponent(e[i])}`).join("&")}}getAlias(t){const{alias:e,name:i,src:r,srcs:n}=t;return Ft(e||i||r||n,a=>{var o;return typeof a=="string"?a:Array.isArray(a)?a.map(h=>{var l,u;return(u=(l=h==null?void 0:h.src)!=null?l:h==null?void 0:h.srcs)!=null?u:h}):a!=null&&a.src||a!=null&&a.srcs?(o=a.src)!=null?o:a.srcs:a},!0)}addManifest(t){this._manifest,this._manifest=t,t.bundles.forEach(e=>{this.addBundle(e.name,e.assets)})}addBundle(t,e){const i=[];Array.isArray(e)?e.forEach(r=>{var n,a;const o=(n=r.src)!=null?n:r.srcs,h=(a=r.alias)!=null?a:r.name;let l;if(typeof h=="string"){const u=this._createBundleAssetId(t,h);i.push(u),l=[h,u]}else{const u=h.map(c=>this._createBundleAssetId(t,c));i.push(...u),l=[...h,...u]}this.add(Qh(Ke({},r),{alias:l,src:o}))}):Object.keys(e).forEach(r=>{var n;const a=[r,this._createBundleAssetId(t,r)];if(typeof e[r]=="string")this.add({alias:a,src:e[r]});else if(Array.isArray(e[r]))this.add({alias:a,src:e[r]});else{const o=e[r],h=(n=o.src)!=null?n:o.srcs;this.add(Qh(Ke({},o),{alias:a,src:Array.isArray(h)?h:[h]}))}i.push(...a)}),this._bundles[t]=i}add(t,e,i,r,n){const a=[];typeof t=="string"||Array.isArray(t)&&typeof t[0]=="string"?a.push({alias:t,src:e,data:i,format:r,loadParser:n}):Array.isArray(t)?a.push(...t):a.push(t);let o;Ft(a).forEach(h=>{const{src:l,srcs:u}=h;let{data:c,format:d,loadParser:f}=h;const p=Ft(l||u).map(y=>typeof y=="string"?Rh(y):Array.isArray(y)?y:[y]),m=this.getAlias(h),g=[];p.forEach(y=>{y.forEach(b=>{var v,x,E;let M={};if(typeof b!="object"){M.src=b;for(let S=0;S{this._assetMap[y]=g})})}resolveBundle(t){const e=Mi(t);t=Ft(t);const i={};return t.forEach(r=>{const n=this._bundles[r];if(n){const a=this.resolve(n),o={};for(const h in a){const l=a[h];o[this._extractAssetIdFromBundle(r,h)]=l}i[r]=o}}),e?i[t[0]]:i}resolveUrl(t){const e=this.resolve(t);if(typeof t!="string"){const i={};for(const r in e)i[r]=e[r].src;return i}return e.src}resolve(t){const e=Mi(t);t=Ft(t);const i={};return t.forEach(r=>{var n;if(!this._resolverHash[r])if(this._assetMap[r]){let a=this._assetMap[r];const o=a[0],h=this._getPreferredOrder(a);h==null||h.priority.forEach(l=>{h.params[l].forEach(u=>{const c=a.filter(d=>d[l]?d[l]===u:!1);c.length&&(a=c)})}),this._resolverHash[r]=(n=a[0])!=null?n:o}else this._resolverHash[r]=this.buildResolvedAsset({alias:[r],src:r},{});i[r]=this._resolverHash[r]}),e?i[t[0]]:i}hasKey(t){return!!this._assetMap[t]}hasBundle(t){return!!this._bundles[t]}_getPreferredOrder(t){for(let e=0;en.params.format.includes(i.format));if(r)return r}return this._preferredOrder[0]}_appendDefaultSearchParams(t){if(!this._defaultSearchParams)return t;const e=/\?/.test(t)?"&":"?";return`${t}${e}${this._defaultSearchParams}`}buildResolvedAsset(t,e){var i;const{aliases:r,data:n,loadParser:a,format:o}=e;return(this._basePath||this._rootPath)&&(t.src=lt.toAbsolute(t.src,this._basePath,this._rootPath)),t.alias=(i=r!=null?r:t.alias)!=null?i:[t.src],t.src=this._appendDefaultSearchParams(t.src),t.data=Ke(Ke({},n||{}),t.data),t.loadParser=a!=null?a:t.loadParser,t.format=o!=null?o:lt.extname(t.src).slice(1),t.srcs=t.src,t.name=t.alias,t}}class Jh{constructor(){this._detections=[],this._initialized=!1,this.resolver=new fp,this.loader=new Af,this.cache=Ee,this._backgroundLoader=new mf(this.loader),this._backgroundLoader.active=!0,this.reset()}async init(t={}){var e,i,r;if(this._initialized)return;if(this._initialized=!0,t.defaultSearchParams&&this.resolver.setDefaultSearchParams(t.defaultSearchParams),t.basePath&&(this.resolver.basePath=t.basePath),t.bundleIdentifier&&this.resolver.setBundleIdentifier(t.bundleIdentifier),t.manifest){let h=t.manifest;typeof h=="string"&&(h=await this.load(h)),this.resolver.addManifest(h)}const n=(i=(e=t.texturePreference)==null?void 0:e.resolution)!=null?i:1,a=typeof n=="number"?[n]:n,o=await this._detectFormats({preferredFormats:(r=t.texturePreference)==null?void 0:r.format,skipDetections:t.skipDetections,detections:this._detections});this.resolver.prefer({params:{format:o,resolution:a}}),t.preferences&&this.setPreferences(t.preferences)}add(t,e,i,r,n){this.resolver.add(t,e,i,r,n)}async load(t,e){this._initialized||await this.init();const i=Mi(t),r=Ft(t).map(o=>{if(typeof o!="string"){const h=this.resolver.getAlias(o);return h.some(l=>!this.resolver.hasKey(l))&&this.add(o),Array.isArray(h)?h[0]:h}return this.resolver.hasKey(o)||this.add({alias:o,src:o}),o}),n=this.resolver.resolve(r),a=await this._mapLoadToResolve(n,e);return i?a[r[0]]:a}addBundle(t,e){this.resolver.addBundle(t,e)}async loadBundle(t,e){this._initialized||await this.init();let i=!1;typeof t=="string"&&(i=!0,t=[t]);const r=this.resolver.resolveBundle(t),n={},a=Object.keys(r);let o=0,h=0;const l=()=>{e==null||e(++o/h)},u=a.map(c=>{const d=r[c];return h+=Object.keys(d).length,this._mapLoadToResolve(d,l).then(f=>{n[c]=f})});return await Promise.all(u),i?n[t[0]]:n}async backgroundLoad(t){this._initialized||await this.init(),typeof t=="string"&&(t=[t]);const e=this.resolver.resolve(t);this._backgroundLoader.add(Object.values(e))}async backgroundLoadBundle(t){this._initialized||await this.init(),typeof t=="string"&&(t=[t]);const e=this.resolver.resolveBundle(t);Object.values(e).forEach(i=>{this._backgroundLoader.add(Object.values(i))})}reset(){this.resolver.reset(),this.loader.reset(),this.cache.reset(),this._initialized=!1}get(t){if(typeof t=="string")return Ee.get(t);const e={};for(let i=0;i{const l=n[o.src],u=[o.src];o.alias&&u.push(...o.alias),a[r[h]]=l,Ee.set(u,l)}),a}async unload(t){this._initialized||await this.init();const e=Ft(t).map(r=>typeof r!="string"?r.src:r),i=this.resolver.resolve(e);await this._unloadFromResolved(i)}async unloadBundle(t){this._initialized||await this.init(),t=Ft(t);const e=this.resolver.resolveBundle(t),i=Object.keys(e).map(r=>this._unloadFromResolved(e[r]));await Promise.all(i)}async _unloadFromResolved(t){const e=Object.values(t);e.forEach(i=>{Ee.remove(i.src)}),await this.loader.unload(e)}async _detectFormats(t){let e=[];t.preferredFormats&&(e=Array.isArray(t.preferredFormats)?t.preferredFormats:[t.preferredFormats]);for(const i of t.detections)t.skipDetections||await i.test()?e=await i.add(e):t.skipDetections||(e=await i.remove(e));return e=e.filter((i,r)=>e.indexOf(i)===r),e}get detections(){return this._detections}get preferWorkers(){return Di.config.preferWorkers}set preferWorkers(t){this.setPreferences({preferWorkers:t})}setPreferences(t){this.loader.parsers.forEach(e=>{e.config&&Object.keys(e.config).filter(i=>i in t).forEach(i=>{e.config[i]=t[i]})})}}const Oi=new Jh;L.handleByList(R.LoadParser,Oi.loader.parsers).handleByList(R.ResolveParser,Oi.resolver.parsers).handleByList(R.CacheParser,Oi.cache.parsers).handleByList(R.DetectionParser,Oi.detections);const tl={extension:R.CacheParser,test:s=>Array.isArray(s)&&s.every(t=>t instanceof B),getCacheableAssets:(s,t)=>{const e={};return s.forEach(i=>{t.forEach((r,n)=>{e[i+(n===0?"":n+1)]=r})}),e}};L.add(tl);async function el(s){if("Image"in globalThis)return new Promise(t=>{const e=new Image;e.onload=()=>{t(!0)},e.onerror=()=>{t(!1)},e.src=s});if("createImageBitmap"in globalThis&&"fetch"in globalThis){try{const t=await(await fetch(s)).blob();await createImageBitmap(t)}catch(t){return!1}return!0}return!1}const il={extension:{type:R.DetectionParser,priority:1},test:async()=>el("data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A="),add:async s=>[...s,"avif"],remove:async s=>s.filter(t=>t!=="avif")};L.add(il);const sl={extension:{type:R.DetectionParser,priority:0},test:async()=>el("data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAAAAAAfQ//73v/+BiOh/AAA="),add:async s=>[...s,"webp"],remove:async s=>s.filter(t=>t!=="webp")};L.add(sl);const rl=["png","jpg","jpeg"],nl={extension:{type:R.DetectionParser,priority:-1},test:()=>Promise.resolve(!0),add:async s=>[...s,...rl],remove:async s=>s.filter(t=>!rl.includes(t))};L.add(nl);const pp="WorkerGlobalScope"in globalThis&&globalThis instanceof globalThis.WorkerGlobalScope;function Pn(s){return pp?!1:document.createElement("video").canPlayType(s)!==""}const al={extension:{type:R.DetectionParser,priority:0},test:async()=>Pn("video/webm"),add:async s=>[...s,"webm"],remove:async s=>s.filter(t=>t!=="webm")};L.add(al);const ol={extension:{type:R.DetectionParser,priority:0},test:async()=>Pn("video/mp4"),add:async s=>[...s,"mp4","m4v"],remove:async s=>s.filter(t=>t!=="mp4"&&t!=="m4v")};L.add(ol);const hl={extension:{type:R.DetectionParser,priority:0},test:async()=>Pn("video/ogg"),add:async s=>[...s,"ogv"],remove:async s=>s.filter(t=>t!=="ogv")};L.add(hl);const ll={extension:R.ResolveParser,test:Di.test,parse:s=>{var t,e;return{resolution:parseFloat((e=(t=O.RETINA_PREFIX.exec(s))==null?void 0:t[1])!=null?e:"1"),format:lt.extname(s).slice(1),src:s}}};L.add(ll);var Et=(s=>(s[s.COMPRESSED_RGB_S3TC_DXT1_EXT=33776]="COMPRESSED_RGB_S3TC_DXT1_EXT",s[s.COMPRESSED_RGBA_S3TC_DXT1_EXT=33777]="COMPRESSED_RGBA_S3TC_DXT1_EXT",s[s.COMPRESSED_RGBA_S3TC_DXT3_EXT=33778]="COMPRESSED_RGBA_S3TC_DXT3_EXT",s[s.COMPRESSED_RGBA_S3TC_DXT5_EXT=33779]="COMPRESSED_RGBA_S3TC_DXT5_EXT",s[s.COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT=35917]="COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT",s[s.COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT=35918]="COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT",s[s.COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT=35919]="COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT",s[s.COMPRESSED_SRGB_S3TC_DXT1_EXT=35916]="COMPRESSED_SRGB_S3TC_DXT1_EXT",s[s.COMPRESSED_R11_EAC=37488]="COMPRESSED_R11_EAC",s[s.COMPRESSED_SIGNED_R11_EAC=37489]="COMPRESSED_SIGNED_R11_EAC",s[s.COMPRESSED_RG11_EAC=37490]="COMPRESSED_RG11_EAC",s[s.COMPRESSED_SIGNED_RG11_EAC=37491]="COMPRESSED_SIGNED_RG11_EAC",s[s.COMPRESSED_RGB8_ETC2=37492]="COMPRESSED_RGB8_ETC2",s[s.COMPRESSED_RGBA8_ETC2_EAC=37496]="COMPRESSED_RGBA8_ETC2_EAC",s[s.COMPRESSED_SRGB8_ETC2=37493]="COMPRESSED_SRGB8_ETC2",s[s.COMPRESSED_SRGB8_ALPHA8_ETC2_EAC=37497]="COMPRESSED_SRGB8_ALPHA8_ETC2_EAC",s[s.COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2=37494]="COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2",s[s.COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2=37495]="COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2",s[s.COMPRESSED_RGB_PVRTC_4BPPV1_IMG=35840]="COMPRESSED_RGB_PVRTC_4BPPV1_IMG",s[s.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG=35842]="COMPRESSED_RGBA_PVRTC_4BPPV1_IMG",s[s.COMPRESSED_RGB_PVRTC_2BPPV1_IMG=35841]="COMPRESSED_RGB_PVRTC_2BPPV1_IMG",s[s.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG=35843]="COMPRESSED_RGBA_PVRTC_2BPPV1_IMG",s[s.COMPRESSED_RGB_ETC1_WEBGL=36196]="COMPRESSED_RGB_ETC1_WEBGL",s[s.COMPRESSED_RGB_ATC_WEBGL=35986]="COMPRESSED_RGB_ATC_WEBGL",s[s.COMPRESSED_RGBA_ATC_EXPLICIT_ALPHA_WEBGL=35986]="COMPRESSED_RGBA_ATC_EXPLICIT_ALPHA_WEBGL",s[s.COMPRESSED_RGBA_ATC_INTERPOLATED_ALPHA_WEBGL=34798]="COMPRESSED_RGBA_ATC_INTERPOLATED_ALPHA_WEBGL",s[s.COMPRESSED_RGBA_ASTC_4x4_KHR=37808]="COMPRESSED_RGBA_ASTC_4x4_KHR",s))(Et||{});const Bi={33776:.5,33777:.5,33778:1,33779:1,35916:.5,35917:.5,35918:1,35919:1,37488:.5,37489:.5,37490:1,37491:1,37492:.5,37496:1,37493:.5,37497:1,37494:.5,37495:.5,35840:.5,35842:.5,35841:.25,35843:.25,36196:.5,35986:.5,35986:1,34798:1,37808:1};let de,Ze;function ul(){Ze={s3tc:de.getExtension("WEBGL_compressed_texture_s3tc"),s3tc_sRGB:de.getExtension("WEBGL_compressed_texture_s3tc_srgb"),etc:de.getExtension("WEBGL_compressed_texture_etc"),etc1:de.getExtension("WEBGL_compressed_texture_etc1"),pvrtc:de.getExtension("WEBGL_compressed_texture_pvrtc")||de.getExtension("WEBKIT_WEBGL_compressed_texture_pvrtc"),atc:de.getExtension("WEBGL_compressed_texture_atc"),astc:de.getExtension("WEBGL_compressed_texture_astc")}}const cl={extension:{type:R.DetectionParser,priority:2},test:async()=>{const s=O.ADAPTER.createCanvas().getContext("webgl");return s?(de=s,!0):!1},add:async s=>{Ze||ul();const t=[];for(const e in Ze)Ze[e]&&t.push(e);return[...t,...s]},remove:async s=>(Ze||ul(),s.filter(t=>!(t in Ze)))};L.add(cl);class dl extends mi{constructor(t,e={width:1,height:1,autoLoad:!0}){let i,r;typeof t=="string"?(i=t,r=new Uint8Array):(i=null,r=t),super(r,e),this.origin=i,this.buffer=r?new ls(r):null,this._load=null,this.loaded=!1,this.origin!==null&&e.autoLoad!==!1&&this.load(),this.origin===null&&this.buffer&&(this._load=Promise.resolve(this),this.loaded=!0,this.onBlobLoaded(this.buffer.rawBinaryData))}onBlobLoaded(t){}load(){return this._load?this._load:(this._load=fetch(this.origin).then(t=>t.blob()).then(t=>t.arrayBuffer()).then(t=>(this.data=new Uint32Array(t),this.buffer=new ls(t),this.loaded=!0,this.onBlobLoaded(t),this.update(),this)),this._load)}}class Ae extends dl{constructor(t,e){super(t,e),this.format=e.format,this.levels=e.levels||1,this._width=e.width,this._height=e.height,this._extension=Ae._formatToExtension(this.format),(e.levelBuffers||this.buffer)&&(this._levelBuffers=e.levelBuffers||Ae._createLevelBuffers(t instanceof Uint8Array?t:this.buffer.uint8View,this.format,this.levels,4,4,this.width,this.height))}upload(t,e,i){const r=t.gl;if(!t.context.extensions[this._extension])throw new Error(`${this._extension} textures are not supported on the current machine`);if(!this._levelBuffers)return!1;r.pixelStorei(r.UNPACK_ALIGNMENT,4);for(let n=0,a=this.levels;n=33776&&t<=33779)return"s3tc";if(t>=37488&&t<=37497)return"etc";if(t>=35840&&t<=35843)return"pvrtc";if(t>=36196)return"etc1";if(t>=35986&&t<=34798)return"atc";throw new Error("Invalid (compressed) texture format given!")}static _createLevelBuffers(t,e,i,r,n,a,o){const h=new Array(i);let l=t.byteOffset,u=a,c=o,d=u+r-1&~(r-1),f=c+n-1&~(n-1),p=d*f*Bi[e];for(let m=0;m1?u:d,levelHeight:i>1?c:f,levelBuffer:new Uint8Array(t.buffer,l,p)},l+=p,u=u>>1||1,c=c>>1||1,d=u+r-1&~(r-1),f=c+n-1&~(n-1),p=d*f*Bi[e];return h}}const Mn=4,Ls=124,mp=32,fl=20,gp=542327876,Us={SIZE:1,FLAGS:2,HEIGHT:3,WIDTH:4,MIPMAP_COUNT:7,PIXEL_FORMAT:19},_p={SIZE:0,FLAGS:1,FOURCC:2,RGB_BITCOUNT:3,R_BIT_MASK:4,G_BIT_MASK:5,B_BIT_MASK:6,A_BIT_MASK:7},ks={DXGI_FORMAT:0,RESOURCE_DIMENSION:1,MISC_FLAG:2,ARRAY_SIZE:3,MISC_FLAGS2:4},vp=1,yp=2,xp=4,bp=64,Tp=512,Ep=131072,Ap=827611204,wp=861165636,Sp=894720068,Ip=808540228,Rp=4,Cp={[Ap]:Et.COMPRESSED_RGBA_S3TC_DXT1_EXT,[wp]:Et.COMPRESSED_RGBA_S3TC_DXT3_EXT,[Sp]:Et.COMPRESSED_RGBA_S3TC_DXT5_EXT},Pp={70:Et.COMPRESSED_RGBA_S3TC_DXT1_EXT,71:Et.COMPRESSED_RGBA_S3TC_DXT1_EXT,73:Et.COMPRESSED_RGBA_S3TC_DXT3_EXT,74:Et.COMPRESSED_RGBA_S3TC_DXT3_EXT,76:Et.COMPRESSED_RGBA_S3TC_DXT5_EXT,77:Et.COMPRESSED_RGBA_S3TC_DXT5_EXT,72:Et.COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT,75:Et.COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT,78:Et.COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT};function pl(s){const t=new Uint32Array(s);if(t[0]!==gp)throw new Error("Invalid DDS file magic word");const e=new Uint32Array(s,0,Ls/Uint32Array.BYTES_PER_ELEMENT),i=e[Us.HEIGHT],r=e[Us.WIDTH],n=e[Us.MIPMAP_COUNT],a=new Uint32Array(s,Us.PIXEL_FORMAT*Uint32Array.BYTES_PER_ELEMENT,mp/Uint32Array.BYTES_PER_ELEMENT),o=a[vp];if(o&xp){const h=a[_p.FOURCC];if(h!==Ip){const b=Cp[h],v=Mn+Ls,x=new Uint8Array(s,v);return[new Ae(x,{format:b,width:r,height:i,levels:n})]}const l=Mn+Ls,u=new Uint32Array(t.buffer,l,fl/Uint32Array.BYTES_PER_ELEMENT),c=u[ks.DXGI_FORMAT],d=u[ks.RESOURCE_DIMENSION],f=u[ks.MISC_FLAG],p=u[ks.ARRAY_SIZE],m=Pp[c];if(m===void 0)throw new Error(`DDSParser cannot parse texture data with DXGI format ${c}`);if(f===Rp)throw new Error("DDSParser does not support cubemap textures");if(d===6)throw new Error("DDSParser does not supported 3D texture data");const g=new Array,y=Mn+Ls+fl;if(p===1)g.push(new Uint8Array(s,y));else{const b=Bi[m];let v=0,x=r,E=i;for(let S=0;S>>1,E=E>>>1}let M=y;for(let S=0;Snew Ae(b,{format:m,width:r,height:i,levels:n}))}throw o&bp?new Error("DDSParser does not support uncompressed texture data."):o&Tp?new Error("DDSParser does not supported YUV uncompressed texture data."):o&Ep?new Error("DDSParser does not support single-channel (lumninance) texture data!"):o&yp?new Error("DDSParser does not support single-channel (alpha) texture data!"):new Error("DDSParser failed to load a texture file due to an unknown reason!")}const ml=[171,75,84,88,32,49,49,187,13,10,26,10],Mp=67305985,Xt={FILE_IDENTIFIER:0,ENDIANNESS:12,GL_TYPE:16,GL_TYPE_SIZE:20,GL_FORMAT:24,GL_INTERNAL_FORMAT:28,GL_BASE_INTERNAL_FORMAT:32,PIXEL_WIDTH:36,PIXEL_HEIGHT:40,PIXEL_DEPTH:44,NUMBER_OF_ARRAY_ELEMENTS:48,NUMBER_OF_FACES:52,NUMBER_OF_MIPMAP_LEVELS:56,BYTES_OF_KEY_VALUE_DATA:60},Dn=64,On={[k.UNSIGNED_BYTE]:1,[k.UNSIGNED_SHORT]:2,[k.INT]:4,[k.UNSIGNED_INT]:4,[k.FLOAT]:4,[k.HALF_FLOAT]:8},gl={[A.RGBA]:4,[A.RGB]:3,[A.RG]:2,[A.RED]:1,[A.LUMINANCE]:1,[A.LUMINANCE_ALPHA]:2,[A.ALPHA]:1},_l={[k.UNSIGNED_SHORT_4_4_4_4]:2,[k.UNSIGNED_SHORT_5_5_5_1]:2,[k.UNSIGNED_SHORT_5_6_5]:2};function vl(s,t,e=!1){const i=new DataView(t);if(!Dp(s,i))return null;const r=i.getUint32(Xt.ENDIANNESS,!0)===Mp,n=i.getUint32(Xt.GL_TYPE,r),a=i.getUint32(Xt.GL_FORMAT,r),o=i.getUint32(Xt.GL_INTERNAL_FORMAT,r),h=i.getUint32(Xt.PIXEL_WIDTH,r),l=i.getUint32(Xt.PIXEL_HEIGHT,r)||1,u=i.getUint32(Xt.PIXEL_DEPTH,r)||1,c=i.getUint32(Xt.NUMBER_OF_ARRAY_ELEMENTS,r)||1,d=i.getUint32(Xt.NUMBER_OF_FACES,r),f=i.getUint32(Xt.NUMBER_OF_MIPMAP_LEVELS,r),p=i.getUint32(Xt.BYTES_OF_KEY_VALUE_DATA,r);if(l===0||u!==1)throw new Error("Only 2D textures are supported");if(d!==1)throw new Error("CubeTextures are not supported by KTXLoader yet!");if(c!==1)throw new Error("WebGL does not support array textures");const m=4,g=4,y=h+3&-4,b=l+3&-4,v=new Array(c);let x=h*l;n===0&&(x=y*b);let E;if(n!==0?On[n]?E=On[n]*gl[a]:E=_l[n]:E=Bi[o],E===void 0)throw new Error("Unable to resolve the pixel format stored in the *.ktx file!");const M=e?Bp(i,p,r):null;let S=x*E,w=h,F=l,G=y,Y=b,N=Dn+p;for(let T=0;T1||n!==0?w:G,levelHeight:f>1||n!==0?F:Y,levelBuffer:new Uint8Array(t,$,S)},$+=S}N+=I+4,N=N%4!==0?N+4-N%4:N,w=w>>1||1,F=F>>1||1,G=w+m-1&~(m-1),Y=F+g-1&~(g-1),S=G*Y*E}return n!==0?{uncompressed:v.map(T=>{let I=T[0].levelBuffer,$=!1;return n===k.FLOAT?I=new Float32Array(T[0].levelBuffer.buffer,T[0].levelBuffer.byteOffset,T[0].levelBuffer.byteLength/4):n===k.UNSIGNED_INT?($=!0,I=new Uint32Array(T[0].levelBuffer.buffer,T[0].levelBuffer.byteOffset,T[0].levelBuffer.byteLength/4)):n===k.INT&&($=!0,I=new Int32Array(T[0].levelBuffer.buffer,T[0].levelBuffer.byteOffset,T[0].levelBuffer.byteLength/4)),{resource:new mi(I,{width:T[0].levelWidth,height:T[0].levelHeight}),type:n,format:$?Op(a):a}}),kvData:M}:{compressed:v.map(T=>new Ae(null,{format:o,width:h,height:l,levels:f,levelBuffers:T})),kvData:M}}function Dp(s,t){for(let e=0;et-r){console.error("KTXLoader: keyAndValueByteSize out of bounds");break}let h=0;for(;ht in s?Fp(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,Up=(s,t)=>{for(var e in t||(t={}))Np.call(t,e)&&xl(s,e,t[e]);if(yl)for(var e of yl(t))Lp.call(t,e)&&xl(s,e,t[e]);return s};const bl={extension:{type:R.LoadParser,priority:Nt.High},name:"loadDDS",test(s){return ce(s,".dds")},async load(s,t,e){const i=await(await O.ADAPTER.fetch(s)).arrayBuffer(),r=pl(i).map(n=>{const a=new X(n,Up({mipmap:Ut.OFF,alphaMode:bt.NO_PREMULTIPLIED_ALPHA,resolution:Kt(s)},t.data));return qe(a,e,s)});return r.length===1?r[0]:r},unload(s){Array.isArray(s)?s.forEach(t=>t.destroy(!0)):s.destroy(!0)}};L.add(bl);var kp=Object.defineProperty,Tl=Object.getOwnPropertySymbols,Gp=Object.prototype.hasOwnProperty,$p=Object.prototype.propertyIsEnumerable,El=(s,t,e)=>t in s?kp(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,Hp=(s,t)=>{for(var e in t||(t={}))Gp.call(t,e)&&El(s,e,t[e]);if(Tl)for(var e of Tl(t))$p.call(t,e)&&El(s,e,t[e]);return s};const Al={extension:{type:R.LoadParser,priority:Nt.High},name:"loadKTX",test(s){return ce(s,".ktx")},async load(s,t,e){const i=await(await O.ADAPTER.fetch(s)).arrayBuffer(),{compressed:r,uncompressed:n,kvData:a}=vl(s,i),o=r!=null?r:n,h=Hp({mipmap:Ut.OFF,alphaMode:bt.NO_PREMULTIPLIED_ALPHA,resolution:Kt(s)},t.data),l=o.map(u=>{var c;o===n&&Object.assign(h,{type:u.type,format:u.format});const d=(c=u.resource)!=null?c:u,f=new X(d,h);return f.ktxKeyValueData=a,qe(f,e,s)});return l.length===1?l[0]:l},unload(s){Array.isArray(s)?s.forEach(t=>t.destroy(!0)):s.destroy(!0)}};L.add(Al);const wl={extension:R.ResolveParser,test:s=>{const t=lt.extname(s).slice(1);return["basis","ktx","dds"].includes(t)},parse:s=>{var t,e,i,r;const n=lt.extname(s).slice(1);if(n==="ktx"){const a=[".s3tc.ktx",".s3tc_sRGB.ktx",".etc.ktx",".etc1.ktx",".pvrt.ktx",".atc.ktx",".astc.ktx"];if(a.some(o=>s.endsWith(o)))return{resolution:parseFloat((e=(t=O.RETINA_PREFIX.exec(s))==null?void 0:t[1])!=null?e:"1"),format:a.find(o=>s.endsWith(o)),src:s}}return{resolution:parseFloat((r=(i=O.RETINA_PREFIX.exec(s))==null?void 0:i[1])!=null?r:"1"),format:n,src:s}}};L.add(wl);const Gs=new j,Vp=4,Sl=class Yi{constructor(t){this.renderer=t,this._rendererPremultipliedAlpha=!1}contextChange(){var t;const e=(t=this.renderer)==null?void 0:t.gl.getContextAttributes();this._rendererPremultipliedAlpha=!!(e&&e.alpha&&e.premultipliedAlpha)}async image(t,e,i,r){const n=new Image;return n.src=await this.base64(t,e,i,r),n}async base64(t,e,i,r){const n=this.canvas(t,r);if(n.toBlob!==void 0)return new Promise((a,o)=>{n.toBlob(h=>{if(!h){o(new Error("ICanvas.toBlob failed!"));return}const l=new FileReader;l.onload=()=>a(l.result),l.onerror=o,l.readAsDataURL(h)},e,i)});if(n.toDataURL!==void 0)return n.toDataURL(e,i);if(n.convertToBlob!==void 0){const a=await n.convertToBlob({type:e,quality:i});return new Promise((o,h)=>{const l=new FileReader;l.onload=()=>o(l.result),l.onerror=h,l.readAsDataURL(a)})}throw new Error("Extract.base64() requires ICanvas.toDataURL, ICanvas.toBlob, or ICanvas.convertToBlob to be implemented")}canvas(t,e){const{pixels:i,width:r,height:n,flipY:a,premultipliedAlpha:o}=this._rawPixels(t,e);a&&Yi._flipY(i,r,n),o&&Yi._unpremultiplyAlpha(i);const h=new Ja(r,n,1),l=new ImageData(new Uint8ClampedArray(i.buffer),r,n);return h.context.putImageData(l,0,0),h.canvas}pixels(t,e){const{pixels:i,width:r,height:n,flipY:a,premultipliedAlpha:o}=this._rawPixels(t,e);return a&&Yi._flipY(i,r,n),o&&Yi._unpremultiplyAlpha(i),i}_rawPixels(t,e){const i=this.renderer;if(!i)throw new Error("The Extract has already been destroyed");let r,n=!1,a=!1,o,h=!1;t&&(t instanceof xe?o=t:(o=i.generateTexture(t,{region:e,resolution:i.resolution,multisample:i.multisample}),h=!0,e&&(Gs.width=e.width,Gs.height=e.height,e=Gs)));const l=i.gl;if(o){if(r=o.baseTexture.resolution,e=e!=null?e:o.frame,n=!1,a=o.baseTexture.alphaMode>0&&o.baseTexture.format===A.RGBA,!h){i.renderTexture.bind(o);const f=o.framebuffer.glFramebuffers[i.CONTEXT_UID];f.blitFramebuffer&&i.framebuffer.bind(f.blitFramebuffer)}}else r=i.resolution,e||(e=Gs,e.width=i.width/r,e.height=i.height/r),n=!0,a=this._rendererPremultipliedAlpha,i.renderTexture.bind();const u=Math.max(Math.round(e.width*r),1),c=Math.max(Math.round(e.height*r),1),d=new Uint8Array(Vp*u*c);return l.readPixels(Math.round(e.x*r),Math.round(e.y*r),u,c,l.RGBA,l.UNSIGNED_BYTE,d),h&&(o==null||o.destroy(!0)),{pixels:d,width:u,height:c,flipY:n,premultipliedAlpha:a}}destroy(){this.renderer=null}static _flipY(t,e,i){const r=e<<2,n=i>>1,a=new Uint8Array(r);for(let o=0;o=0&&o>=0&&r>=0&&n>=0)){t.length=0;return}const h=Math.ceil(2.3*Math.sqrt(a+o)),l=h*8+(r?4:0)+(n?4:0);if(t.length=l,l===0)return;if(h===0){t.length=8,t[0]=t[6]=e+r,t[1]=t[3]=i+n,t[2]=t[4]=e-r,t[5]=t[7]=i-n;return}let u=0,c=h*4+(r?2:0)+2,d=c,f=l;{const p=r+a,m=n,g=e+p,y=e-p,b=i+m;if(t[u++]=g,t[u++]=b,t[--c]=b,t[--c]=y,n){const v=i-m;t[d++]=y,t[d++]=v,t[--f]=v,t[--f]=g}}for(let p=1;p0||t&&i<=0){const r=e/2;for(let n=r+r%2;n=6){Rl(e,!1);const a=[];for(let l=0;l=0&&n>=0&&a.push(e,i,e+r,i,e+r,i+n,e,i+n)},triangulate(s,t){const e=s.points,i=t.points;if(e.length===0)return;const r=i.length/2;i.push(e[0],e[1],e[2],e[3],e[6],e[7],e[4],e[5]),t.indices.push(r,r+1,r+2,r+1,r+2,r+3)}},Pl={build(s){Fi.build(s)},triangulate(s,t){Fi.triangulate(s,t)}};var Rt=(s=>(s.MITER="miter",s.BEVEL="bevel",s.ROUND="round",s))(Rt||{}),fe=(s=>(s.BUTT="butt",s.ROUND="round",s.SQUARE="square",s))(fe||{});const we={adaptive:!0,maxLength:10,minSegments:8,maxSegments:2048,epsilon:1e-4,_segmentsCount(s,t=20){if(!this.adaptive||!s||isNaN(s))return t;let e=Math.ceil(s/this.maxLength);return ethis.maxSegments&&(e=this.maxSegments),e}},Xp=we;class Fn{static curveTo(t,e,i,r,n,a){const o=a[a.length-2],h=a[a.length-1]-e,l=o-t,u=r-e,c=i-t,d=Math.abs(h*c-l*u);if(d<1e-8||n===0)return(a[a.length-2]!==t||a[a.length-1]!==e)&&a.push(t,e),null;const f=h*h+l*l,p=u*u+c*c,m=h*u+l*c,g=n*Math.sqrt(f)/d,y=n*Math.sqrt(p)/d,b=g*m/f,v=y*m/p,x=g*c+y*l,E=g*u+y*h,M=l*(y+b),S=h*(y+b),w=c*(g+v),F=u*(g+v),G=Math.atan2(S-E,M-x),Y=Math.atan2(F-E,w-x);return{cx:x+t,cy:E+e,radius:n,startAngle:G,endAngle:Y,anticlockwise:l*u>c*h}}static arc(t,e,i,r,n,a,o,h,l){const u=o-a,c=we._segmentsCount(Math.abs(u)*n,Math.ceil(Math.abs(u)/_i)*40),d=u/(c*2),f=d*2,p=Math.cos(d),m=Math.sin(d),g=c-1,y=g%1/g;for(let b=0;b<=g;++b){const v=b+y*b,x=d+a+f*v,E=Math.cos(x),M=-Math.sin(x);l.push((p*E+m*M)*n+i,(p*-M+m*E)*n+r)}}}class Ml{constructor(){this.reset()}begin(t,e,i){this.reset(),this.style=t,this.start=e,this.attribStart=i}end(t,e){this.attribSize=e-this.attribStart,this.size=t-this.start}reset(){this.style=null,this.size=0,this.start=0,this.attribStart=0,this.attribSize=0}}class $s{static curveLength(t,e,i,r,n,a,o,h){let l=0,u=0,c=0,d=0,f=0,p=0,m=0,g=0,y=0,b=0,v=0,x=t,E=e;for(let M=1;M<=10;++M)u=M/10,c=u*u,d=c*u,f=1-u,p=f*f,m=p*f,g=m*t+3*p*u*i+3*f*c*n+d*o,y=m*e+3*p*u*r+3*f*c*a+d*h,b=x-g,v=E-y,x=g,E=y,l+=Math.sqrt(b*b+v*v);return l}static curveTo(t,e,i,r,n,a,o){const h=o[o.length-2],l=o[o.length-1];o.length-=2;const u=we._segmentsCount($s.curveLength(h,l,t,e,i,r,n,a));let c=0,d=0,f=0,p=0,m=0;o.push(h,l);for(let g=1,y=0;g<=u;++g)y=g/u,c=1-y,d=c*c,f=d*c,p=y*y,m=p*y,o.push(f*h+3*d*y*t+3*c*p*i+m*n,f*l+3*d*y*e+3*c*p*r+m*a)}}function Dl(s,t,e,i,r,n,a,o){const h=s-e*r,l=t-i*r,u=s+e*n,c=t+i*n;let d,f;a?(d=i,f=-e):(d=-i,f=e);const p=h+d,m=l+f,g=u+d,y=c+f;return o.push(p,m,g,y),2}function Ge(s,t,e,i,r,n,a,o){const h=e-s,l=i-t;let u=Math.atan2(h,l),c=Math.atan2(r-s,n-t);o&&uc&&(c+=Math.PI*2);let d=u;const f=c-u,p=Math.abs(f),m=Math.sqrt(h*h+l*l),g=(15*p*Math.sqrt(m)/Math.PI>>0)+1,y=f/g;if(d+=y,o){a.push(s,t,e,i);for(let b=1,v=d;b=0&&(n.join===Rt.ROUND?d+=Ge(v,x,v-S*T,x-w*T,v-F*T,x-G*T,u,!1)+4:d+=2,u.push(v-F*I,x-G*I,v+F*T,x+G*T));continue}const gt=(-S+y)*(-w+x)-(-S+v)*(-w+b),ut=(-F+E)*(-G+x)-(-F+v)*(-G+M),_t=(z*ut-P*gt)/Q,xt=(C*gt-ot*ut)/Q,Ct=(_t-v)*(_t-v)+(xt-x)*(xt-x),vt=v+(_t-v)*T,st=x+(xt-x)*T,ct=v-(_t-v)*I,dt=x-(xt-x)*I,te=Math.min(z*z+ot*ot,P*P+C*C),ee=J?T:I,ji=te+ee*ee*m,Um=Ct<=ji;let er=n.join;if(er===Rt.MITER&&Ct/m>g&&(er=Rt.BEVEL),Um)switch(er){case Rt.MITER:{u.push(vt,st,ct,dt);break}case Rt.BEVEL:{J?u.push(vt,st,v+S*I,x+w*I,vt,st,v+F*I,x+G*I):u.push(v-S*T,x-w*T,ct,dt,v-F*T,x-G*T,ct,dt),d+=2;break}case Rt.ROUND:{J?(u.push(vt,st,v+S*I,x+w*I),d+=Ge(v,x,v+S*I,x+w*I,v+F*I,x+G*I,u,!0)+4,u.push(vt,st,v+F*I,x+G*I)):(u.push(v-S*T,x-w*T,ct,dt),d+=Ge(v,x,v-S*T,x-w*T,v-F*T,x-G*T,u,!1)+4,u.push(v-F*T,x-G*T,ct,dt));break}}else{switch(u.push(v-S*T,x-w*T,v+S*I,x+w*I),er){case Rt.MITER:{J?u.push(ct,dt,ct,dt):u.push(vt,st,vt,st),d+=2;break}case Rt.ROUND:{J?d+=Ge(v,x,v+S*I,x+w*I,v+F*I,x+G*I,u,!0)+2:d+=Ge(v,x,v-S*T,x-w*T,v-F*T,x-G*T,u,!1)+2;break}}u.push(v-F*T,x-G*T,v+F*I,x+G*I),d+=2}}y=i[(c-2)*2],b=i[(c-2)*2+1],v=i[(c-1)*2],x=i[(c-1)*2+1],S=-(b-x),w=y-v,Y=Math.sqrt(S*S+w*w),S/=Y,w/=Y,S*=p,w*=p,u.push(v-S*T,x-w*T,v+S*I,x+w*I),h||(n.cap===fe.ROUND?d+=Ge(v-S*(T-I)*.5,x-w*(T-I)*.5,v-S*T,x-w*T,v+S*I,x+w*I,u,!1)+2:n.cap===fe.SQUARE&&(d+=Dl(v,x,S,w,T,I,!1,u)));const $=t.indices,W=we.epsilon*we.epsilon;for(let V=f;V0&&(this.invalidate(),this.clearDirty++,this.graphicsData.length=0),this}drawShape(t,e=null,i=null,r=null){const n=new Li(t,e,i,r);return this.graphicsData.push(n),this.dirty++,this}drawHole(t,e=null){if(!this.graphicsData.length)return null;const i=new Li(t,null,null,e),r=this.graphicsData[this.graphicsData.length-1];return i.lineStyle=r.lineStyle,r.holes.push(i),this.dirty++,this}destroy(){super.destroy();for(let t=0;t0&&(i=this.batches[this.batches.length-1],r=i.style);for(let h=this.shapeIndex;h65535;this.indicesUint16&&this.indices.length===this.indicesUint16.length&&o===this.indicesUint16.BYTES_PER_ELEMENT>2?this.indicesUint16.set(this.indices):this.indicesUint16=o?new Uint32Array(this.indices):new Uint16Array(this.indices),this.batchable=this.isBatchable(),this.batchable?this.packBatches():this.buildDrawCalls()}_compareStyles(t,e){return!(!t||!e||t.texture.baseTexture!==e.texture.baseTexture||t.color+t.alpha!==e.color+e.alpha||!!t.native!=!!e.native)}validateBatching(){if(this.dirty===this.cacheDirty||!this.graphicsData.length)return!1;for(let t=0,e=this.graphicsData.length;t65535*2)return!1;const t=this.batches;for(let e=0;e0&&(r=Ni.pop(),r||(r=new cs,r.texArray=new bs),this.drawCalls.push(r)),r.start=u,r.size=0,r.texArray.count=0,r.type=l),m.touched=1,m._batchEnabled=t,m._batchLocation=n,m.wrapMode=Wt.REPEAT,r.texArray.elements[r.texArray.count++]=m,n++)),r.size+=d.size,u+=d.size,o=m._batchLocation,this.addColors(e,p.color,p.alpha,d.attribSize,d.attribStart),this.addTextureIds(i,o,d.attribSize,d.attribStart)}X._globalBatch=t,this.packAttributes()}packAttributes(){const t=this.points,e=this.uvs,i=this.colors,r=this.textureIds,n=new ArrayBuffer(t.length*3*4),a=new Float32Array(n),o=new Uint32Array(n);let h=0;for(let l=0;l0&&t.alpha>0;return i?(t.matrix&&(t.matrix=t.matrix.clone(),t.matrix.invert()),Object.assign(this._lineStyle,{visible:i},t)):this._lineStyle.reset(),this}startPoly(){if(this.currentPath){const t=this.currentPath.points,e=this.currentPath.points.length;e>2&&(this.drawShape(this.currentPath),this.currentPath=new Pe,this.currentPath.closeStroke=!1,this.currentPath.points.push(t[e-2],t[e-1]))}else this.currentPath=new Pe,this.currentPath.closeStroke=!1}finishPoly(){this.currentPath&&(this.currentPath.points.length>2?(this.drawShape(this.currentPath),this.currentPath=null):this.currentPath.points.length=0)}moveTo(t,e){return this.startPoly(),this.currentPath.points[0]=t,this.currentPath.points[1]=e,this}lineTo(t,e){this.currentPath||this.moveTo(0,0);const i=this.currentPath.points,r=i[i.length-2],n=i[i.length-1];return(r!==t||n!==e)&&i.push(t,e),this}_initCurve(t=0,e=0){this.currentPath?this.currentPath.points.length===0&&(this.currentPath.points=[t,e]):this.moveTo(t,e)}quadraticCurveTo(t,e,i,r){this._initCurve();const n=this.currentPath.points;return n.length===0&&this.moveTo(0,0),Hs.curveTo(t,e,i,r,n),this}bezierCurveTo(t,e,i,r,n,a){return this._initCurve(),$s.curveTo(t,e,i,r,n,a,this.currentPath.points),this}arcTo(t,e,i,r,n){this._initCurve(t,e);const a=this.currentPath.points,o=Fn.curveTo(t,e,i,r,n,a);if(o){const{cx:h,cy:l,radius:u,startAngle:c,endAngle:d,anticlockwise:f}=o;this.arc(h,l,u,c,d,f)}return this}arc(t,e,i,r,n,a=!1){if(r===n)return this;if(!a&&n<=r?n+=_i:a&&r<=n&&(r+=_i),n-r===0)return this;const o=t+Math.cos(r)*i,h=e+Math.sin(r)*i,l=this._geometry.closePointEps;let u=this.currentPath?this.currentPath.points:null;if(u){const c=Math.abs(u[u.length-2]-o),d=Math.abs(u[u.length-1]-h);c0;return i?(t.matrix&&(t.matrix=t.matrix.clone(),t.matrix.invert()),Object.assign(this._fillStyle,{visible:i},t)):this._fillStyle.reset(),this}endFill(){return this.finishPoly(),this._fillStyle.reset(),this}drawRect(t,e,i,r){return this.drawShape(new j(t,e,i,r))}drawRoundedRect(t,e,i,r,n){return this.drawShape(new ms(t,e,i,r,n))}drawCircle(t,e,i){return this.drawShape(new fs(t,e,i))}drawEllipse(t,e,i,r){return this.drawShape(new ps(t,e,i,r))}drawPolygon(...t){let e,i=!0;const r=t[0];r.points?(i=r.closeStroke,e=r.points):Array.isArray(t[0])?e=t[0]:e=t;const n=new Pe(e);return n.closeStroke=i,this.drawShape(n),this}drawShape(t){return this._holeMode?this._geometry.drawHole(t,this._matrix):this._geometry.drawShape(t,this._fillStyle.clone(),this._lineStyle.clone(),this._matrix),this}clear(){return this._geometry.clear(),this._lineStyle.reset(),this._fillStyle.reset(),this._boundsID++,this._matrix=null,this._holeMode=!1,this.currentPath=null,this}isFastRect(){const t=this._geometry.graphicsData;return t.length===1&&t[0].shape.type===pt.RECT&&!t[0].matrix&&!t[0].holes.length&&!(t[0].lineStyle.visible&&t[0].lineStyle.width)}_render(t){this.finishPoly();const e=this._geometry;e.updateBatches(),e.batchable?(this.batchDirty!==e.batchDirty&&this._populateBatches(),this._renderBatched(t)):(t.batch.flush(),this._renderDirect(t))}_populateBatches(){const t=this._geometry,e=this.blendMode,i=t.batches.length;this.batchTint=-1,this._transformID=-1,this.batchDirty=t.batchDirty,this.batches.length=i,this.vertexData=new Float32Array(t.points);for(let r=0;r0){const p=h.x-t[d].x,m=h.y-t[d].y,g=Math.sqrt(p*p+m*m);h=t[d],o+=g/l}else o=d/(u-1);n[f]=o,n[f+1]=0,n[f+2]=o,n[f+3]=1}let c=0;for(let d=0;d0?this.textureScale*this._width/2:this._width/2;for(let l=0;l1&&(d=1);const f=Math.sqrt(r*r+n*n);f<1e-6?(r=0,n=0):(r/=f,n/=f,r*=h,n*=h),a[c]=u.x+r,a[c+1]=u.y+n,a[c+2]=u.x-r,a[c+3]=u.y-n,e=u}this.buffers[0].update()}update(){this.textureScale>0?this.build():this.updateVertices()}}class Gl extends Je{constructor(t,e,i){const r=new Ul(t.width,t.height,e,i),n=new ti(B.WHITE);super(r,n),this.texture=t,this.autoResize=!0}textureUpdated(){this._textureID=this.shader.texture._updateID;const t=this.geometry,{width:e,height:i}=this.shader.texture;this.autoResize&&(t.width!==e||t.height!==i)&&(t.width=this.shader.texture.width,t.height=this.shader.texture.height,t.build())}set texture(t){this.shader.texture!==t&&(this.shader.texture=t,this._textureID=-1,t.baseTexture.valid?this.textureUpdated():t.once("update",this.textureUpdated,this))}get texture(){return this.shader.texture}_render(t){this._textureID!==this.shader.texture._updateID&&this.textureUpdated(),super._render(t)}destroy(t){this.shader.texture.off("update",this.textureUpdated,this),super.destroy(t)}}const js=10;class Kp extends Gl{constructor(t,e,i,r,n){var a,o,h,l,u,c,d,f;super(B.WHITE,4,4),this._origWidth=t.orig.width,this._origHeight=t.orig.height,this._width=this._origWidth,this._height=this._origHeight,this._leftWidth=(o=e!=null?e:(a=t.defaultBorders)==null?void 0:a.left)!=null?o:js,this._rightWidth=(l=r!=null?r:(h=t.defaultBorders)==null?void 0:h.right)!=null?l:js,this._topHeight=(c=i!=null?i:(u=t.defaultBorders)==null?void 0:u.top)!=null?c:js,this._bottomHeight=(f=n!=null?n:(d=t.defaultBorders)==null?void 0:d.bottom)!=null?f:js,this.texture=t}textureUpdated(){this._textureID=this.shader.texture._updateID,this._refresh()}get vertices(){return this.geometry.getBuffer("aVertexPosition").data}set vertices(t){this.geometry.getBuffer("aVertexPosition").data=t}updateHorizontalVertices(){const t=this.vertices,e=this._getMinScale();t[9]=t[11]=t[13]=t[15]=this._topHeight*e,t[17]=t[19]=t[21]=t[23]=this._height-this._bottomHeight*e,t[25]=t[27]=t[29]=t[31]=this._height}updateVerticalVertices(){const t=this.vertices,e=this._getMinScale();t[2]=t[10]=t[18]=t[26]=this._leftWidth*e,t[4]=t[12]=t[20]=t[28]=this._width-this._rightWidth*e,t[6]=t[14]=t[22]=t[30]=this._width}_getMinScale(){const t=this._leftWidth+this._rightWidth,e=this._width>t?1:this._width/t,i=this._topHeight+this._bottomHeight,r=this._height>i?1:this._height/i;return Math.min(e,r)}get width(){return this._width}set width(t){this._width=t,this._refresh()}get height(){return this._height}set height(t){this._height=t,this._refresh()}get leftWidth(){return this._leftWidth}set leftWidth(t){this._leftWidth=t,this._refresh()}get rightWidth(){return this._rightWidth}set rightWidth(t){this._rightWidth=t,this._refresh()}get topHeight(){return this._topHeight}set topHeight(t){this._topHeight=t,this._refresh()}get bottomHeight(){return this._bottomHeight}set bottomHeight(t){this._bottomHeight=t,this._refresh()}_refresh(){const t=this.texture,e=this.geometry.buffers[1].data;this._origWidth=t.orig.width,this._origHeight=t.orig.height;const i=1/this._origWidth,r=1/this._origHeight;e[0]=e[8]=e[16]=e[24]=0,e[1]=e[3]=e[5]=e[7]=0,e[6]=e[14]=e[22]=e[30]=1,e[25]=e[27]=e[29]=e[31]=1,e[2]=e[10]=e[18]=e[26]=i*this._leftWidth,e[4]=e[12]=e[20]=e[28]=1-i*this._rightWidth,e[9]=e[11]=e[13]=e[15]=r*this._topHeight,e[17]=e[19]=e[21]=e[23]=1-r*this._bottomHeight,this.updateHorizontalVertices(),this.updateVerticalVertices(),this.geometry.buffers[0].update(),this.geometry.buffers[1].update()}}class Zp extends Je{constructor(t=B.EMPTY,e,i,r,n){const a=new ki(e,i,r);a.getBuffer("aVertexPosition").static=!1;const o=new ti(t);super(a,o,null,n),this.autoUpdate=!0}get vertices(){return this.geometry.getBuffer("aVertexPosition").data}set vertices(t){this.geometry.getBuffer("aVertexPosition").data=t}_render(t){this.autoUpdate&&this.geometry.getBuffer("aVertexPosition").update(),super._render(t)}}class Qp extends Je{constructor(t,e,i=0){const r=new kl(t.height,e,i),n=new ti(t);i>0&&(t.baseTexture.wrapMode=Wt.REPEAT),super(r,n),this.autoUpdate=!0}_render(t){const e=this.geometry;(this.autoUpdate||e._width!==this.shader.texture.height)&&(e._width=this.shader.texture.height,e.update()),super._render(t)}}class Jp extends It{constructor(t=1500,e,i=16384,r=!1){super();const n=16384;i>n&&(i=n),this._properties=[!1,!0,!1,!1,!1],this._maxSize=t,this._batchSize=i,this._buffers=null,this._bufferUpdateIDs=[],this._updateID=0,this.interactiveChildren=!1,this.blendMode=H.NORMAL,this.autoResize=r,this.roundPixels=!0,this.baseTexture=null,this.setProperties(e),this._tintColor=new Z(0),this.tintRgb=new Float32Array(3),this.tint=16777215}setProperties(t){t&&(this._properties[0]="vertices"in t||"scale"in t?!!t.vertices||!!t.scale:this._properties[0],this._properties[1]="position"in t?!!t.position:this._properties[1],this._properties[2]="rotation"in t?!!t.rotation:this._properties[2],this._properties[3]="uvs"in t?!!t.uvs:this._properties[3],this._properties[4]="tint"in t||"alpha"in t?!!t.tint||!!t.alpha:this._properties[4])}updateTransform(){this.displayObjectUpdateTransform()}get tint(){return this._tintColor.value}set tint(t){this._tintColor.setValue(t),this._tintColor.toRgbArray(this.tintRgb)}render(t){!this.visible||this.worldAlpha<=0||!this.children.length||!this.renderable||(this.baseTexture||(this.baseTexture=this.children[0]._texture.baseTexture,this.baseTexture.valid||this.baseTexture.once("update",()=>this.onChildrenChange(0))),t.batch.setObjectRenderer(t.plugins.particle),t.plugins.particle.render(this))}onChildrenChange(t){const e=Math.floor(t/this._batchSize);for(;this._bufferUpdateIDs.lengthi&&!t.autoResize&&(a=i);let o=t._buffers;o||(o=t._buffers=this.generateBuffers(t));const h=e[0]._texture.baseTexture,l=h.alphaMode>0;this.state.blendMode=wr(t.blendMode,l),n.state.set(this.state);const u=n.gl,c=t.worldTransform.copyTo(this.tempMatrix);c.prepend(n.globalUniforms.uniforms.projectionMatrix),this.shader.uniforms.translationMatrix=c.toArray(!0),this.shader.uniforms.uColor=Z.shared.setValue(t.tintRgb).premultiply(t.worldAlpha,l).toArray(this.shader.uniforms.uColor),this.shader.uniforms.uSampler=h,this.renderer.shader.bind(this.shader);let d=!1;for(let f=0,p=0;fr&&(m=r),p>=o.length&&o.push(this._generateOneMoreBuffer(t));const g=o[p];g.uploadDynamic(e,f,m);const y=t._bufferUpdateIDs[p]||0;d=d||g._updateID0);r[a]=l,r[a+n]=l,r[a+n*2]=l,r[a+n*3]=l,a+=n*4}}destroy(){super.destroy(),this.shader&&(this.shader.destroy(),this.shader=null),this.tempMatrix=null}}Hn.extension={name:"particle",type:R.RendererPlugin},L.add(Hn);var Gi=(s=>(s[s.LINEAR_VERTICAL=0]="LINEAR_VERTICAL",s[s.LINEAR_HORIZONTAL=1]="LINEAR_HORIZONTAL",s))(Gi||{});const zs={willReadFrequently:!0},Jt=class U{static get experimentalLetterSpacingSupported(){let t=U._experimentalLetterSpacingSupported;if(t!==void 0){const e=O.ADAPTER.getCanvasRenderingContext2D().prototype;t=U._experimentalLetterSpacingSupported="letterSpacing"in e||"textLetterSpacing"in e}return t}constructor(t,e,i,r,n,a,o,h,l){this.text=t,this.style=e,this.width=i,this.height=r,this.lines=n,this.lineWidths=a,this.lineHeight=o,this.maxLineWidth=h,this.fontProperties=l}static measureText(t,e,i,r=U._canvas){i=i==null?e.wordWrap:i;const n=e.toFontString(),a=U.measureFont(n);a.fontSize===0&&(a.fontSize=e.fontSize,a.ascent=e.fontSize);const o=r.getContext("2d",zs);o.font=n;const h=(i?U.wordWrap(t,e,r):t).split(/(?:\r\n|\r|\n)/),l=new Array(h.length);let u=0;for(let p=0;p0&&(r?n-=e:n+=(U.graphemeSegmenter(t).length-1)*e),n}static wordWrap(t,e,i=U._canvas){const r=i.getContext("2d",zs);let n=0,a="",o="";const h=Object.create(null),{letterSpacing:l,whiteSpace:u}=e,c=U.collapseSpaces(u),d=U.collapseNewlines(u);let f=!c;const p=e.wordWrapWidth+l,m=U.tokenize(t);for(let g=0;gp)if(a!==""&&(o+=U.addLine(a),a="",n=0),U.canBreakWords(y,e.breakWords)){const v=U.wordWrapSplit(y);for(let x=0;xp&&(o+=U.addLine(a),f=!1,a="",n=0),a+=E,n+=w}}else{a.length>0&&(o+=U.addLine(a),a="",n=0);const v=g===m.length-1;o+=U.addLine(y,!v),f=!1,a="",n=0}else b+n>p&&(f=!1,o+=U.addLine(a),a="",n=0),(a.length>0||!U.isBreakingSpace(y)||f)&&(a+=y,n+=b)}return o+=U.addLine(a,!1),o}static addLine(t,e=!0){return t=U.trimRight(t),t=e?`${t} +`:t,t}static getFromCache(t,e,i,r){let n=i[t];return typeof n!="number"&&(n=U._measureText(t,e,r)+e,i[t]=n),n}static collapseSpaces(t){return t==="normal"||t==="pre-line"}static collapseNewlines(t){return t==="normal"}static trimRight(t){if(typeof t!="string")return"";for(let e=t.length-1;e>=0;e--){const i=t[e];if(!U.isBreakingSpace(i))break;t=t.slice(0,-1)}return t}static isNewline(t){return typeof t!="string"?!1:U._newlines.includes(t.charCodeAt(0))}static isBreakingSpace(t,e){return typeof t!="string"?!1:U._breakingSpaces.includes(t.charCodeAt(0))}static tokenize(t){const e=[];let i="";if(typeof t!="string")return e;for(let r=0;ro;--d){for(let m=0;m{if(typeof(Intl==null?void 0:Intl.Segmenter)=="function"){const s=new Intl.Segmenter;return t=>[...s.segment(t)].map(e=>e.segment)}return s=>[...s]})(),Jt.experimentalLetterSpacing=!1,Jt._fonts={},Jt._newlines=[10,13],Jt._breakingSpaces=[9,32,8192,8193,8194,8195,8196,8197,8198,8200,8201,8202,8287,12288];let pe=Jt;const im=["serif","sans-serif","monospace","cursive","fantasy","system-ui"],Hl=class qi{constructor(t){this.styleID=0,this.reset(),Xn(this,t,t)}clone(){const t={};return Xn(t,this,qi.defaultStyle),new qi(t)}reset(){Xn(this,qi.defaultStyle,qi.defaultStyle)}get align(){return this._align}set align(t){this._align!==t&&(this._align=t,this.styleID++)}get breakWords(){return this._breakWords}set breakWords(t){this._breakWords!==t&&(this._breakWords=t,this.styleID++)}get dropShadow(){return this._dropShadow}set dropShadow(t){this._dropShadow!==t&&(this._dropShadow=t,this.styleID++)}get dropShadowAlpha(){return this._dropShadowAlpha}set dropShadowAlpha(t){this._dropShadowAlpha!==t&&(this._dropShadowAlpha=t,this.styleID++)}get dropShadowAngle(){return this._dropShadowAngle}set dropShadowAngle(t){this._dropShadowAngle!==t&&(this._dropShadowAngle=t,this.styleID++)}get dropShadowBlur(){return this._dropShadowBlur}set dropShadowBlur(t){this._dropShadowBlur!==t&&(this._dropShadowBlur=t,this.styleID++)}get dropShadowColor(){return this._dropShadowColor}set dropShadowColor(t){const e=Vn(t);this._dropShadowColor!==e&&(this._dropShadowColor=e,this.styleID++)}get dropShadowDistance(){return this._dropShadowDistance}set dropShadowDistance(t){this._dropShadowDistance!==t&&(this._dropShadowDistance=t,this.styleID++)}get fill(){return this._fill}set fill(t){const e=Vn(t);this._fill!==e&&(this._fill=e,this.styleID++)}get fillGradientType(){return this._fillGradientType}set fillGradientType(t){this._fillGradientType!==t&&(this._fillGradientType=t,this.styleID++)}get fillGradientStops(){return this._fillGradientStops}set fillGradientStops(t){sm(this._fillGradientStops,t)||(this._fillGradientStops=t,this.styleID++)}get fontFamily(){return this._fontFamily}set fontFamily(t){this.fontFamily!==t&&(this._fontFamily=t,this.styleID++)}get fontSize(){return this._fontSize}set fontSize(t){this._fontSize!==t&&(this._fontSize=t,this.styleID++)}get fontStyle(){return this._fontStyle}set fontStyle(t){this._fontStyle!==t&&(this._fontStyle=t,this.styleID++)}get fontVariant(){return this._fontVariant}set fontVariant(t){this._fontVariant!==t&&(this._fontVariant=t,this.styleID++)}get fontWeight(){return this._fontWeight}set fontWeight(t){this._fontWeight!==t&&(this._fontWeight=t,this.styleID++)}get letterSpacing(){return this._letterSpacing}set letterSpacing(t){this._letterSpacing!==t&&(this._letterSpacing=t,this.styleID++)}get lineHeight(){return this._lineHeight}set lineHeight(t){this._lineHeight!==t&&(this._lineHeight=t,this.styleID++)}get leading(){return this._leading}set leading(t){this._leading!==t&&(this._leading=t,this.styleID++)}get lineJoin(){return this._lineJoin}set lineJoin(t){this._lineJoin!==t&&(this._lineJoin=t,this.styleID++)}get miterLimit(){return this._miterLimit}set miterLimit(t){this._miterLimit!==t&&(this._miterLimit=t,this.styleID++)}get padding(){return this._padding}set padding(t){this._padding!==t&&(this._padding=t,this.styleID++)}get stroke(){return this._stroke}set stroke(t){const e=Vn(t);this._stroke!==e&&(this._stroke=e,this.styleID++)}get strokeThickness(){return this._strokeThickness}set strokeThickness(t){this._strokeThickness!==t&&(this._strokeThickness=t,this.styleID++)}get textBaseline(){return this._textBaseline}set textBaseline(t){this._textBaseline!==t&&(this._textBaseline=t,this.styleID++)}get trim(){return this._trim}set trim(t){this._trim!==t&&(this._trim=t,this.styleID++)}get whiteSpace(){return this._whiteSpace}set whiteSpace(t){this._whiteSpace!==t&&(this._whiteSpace=t,this.styleID++)}get wordWrap(){return this._wordWrap}set wordWrap(t){this._wordWrap!==t&&(this._wordWrap=t,this.styleID++)}get wordWrapWidth(){return this._wordWrapWidth}set wordWrapWidth(t){this._wordWrapWidth!==t&&(this._wordWrapWidth=t,this.styleID++)}toFontString(){const t=typeof this.fontSize=="number"?`${this.fontSize}px`:this.fontSize;let e=this.fontFamily;Array.isArray(this.fontFamily)||(e=this.fontFamily.split(","));for(let i=e.length-1;i>=0;i--){let r=e[i].trim();!/([\"\'])[^\'\"]+\1/.test(r)&&!im.includes(r)&&(r=`"${r}"`),e[i]=r}return`${this.fontStyle} ${this.fontVariant} ${this.fontWeight} ${t} ${e.join(",")}`}};Hl.defaultStyle={align:"left",breakWords:!1,dropShadow:!1,dropShadowAlpha:1,dropShadowAngle:Math.PI/6,dropShadowBlur:0,dropShadowColor:"black",dropShadowDistance:5,fill:"black",fillGradientType:Gi.LINEAR_VERTICAL,fillGradientStops:[],fontFamily:"Arial",fontSize:26,fontStyle:"normal",fontVariant:"normal",fontWeight:"normal",leading:0,letterSpacing:0,lineHeight:0,lineJoin:"miter",miterLimit:10,padding:0,stroke:"black",strokeThickness:0,textBaseline:"alphabetic",trim:!1,whiteSpace:"pre",wordWrap:!1,wordWrapWidth:100};let me=Hl;function Vn(s){const t=Z.shared,e=i=>{const r=t.setValue(i);return r.alpha===1?r.toHex():r.toRgbaString()};return Array.isArray(s)?s.map(e):e(s)}function sm(s,t){if(!Array.isArray(s)||!Array.isArray(t)||s.length!==t.length)return!1;for(let e=0;e0&&p>m&&(g=(m+p)/2);const y=m+d,b=i.lineHeight*(f+1);let v=y;f+10}}function nm(s,t){var e;let i=!1;if((e=s==null?void 0:s._textures)!=null&&e.length){for(let r=0;r{this.queue&&this.prepareItems()},this.registerFindHook(um),this.registerFindHook(cm),this.registerFindHook(nm),this.registerFindHook(am),this.registerFindHook(om),this.registerUploadHook(hm),this.registerUploadHook(lm)}upload(t){return new Promise(e=>{t&&this.add(t),this.queue.length?(this.completes.push(e),this.ticking||(this.ticking=!0,mt.system.addOnce(this.tick,this,le.UTILITY))):e()})}tick(){setTimeout(this.delayedTick,0)}prepareItems(){for(this.limiter.beginFrame();this.queue.length&&this.limiter.allowedToUpload();){const t=this.queue[0];let e=!1;if(t&&!t._destroyed){for(let i=0,r=this.uploadHooks.length;i=0;e--)this.add(t.children[e]);return this}destroy(){this.ticking&&mt.system.remove(this.tick,this),this.ticking=!1,this.addHooks=null,this.uploadHooks=null,this.renderer=null,this.completes=null,this.queue=null,this.limiter=null,this.uploadHookHelper=null}};jl.uploadsPerFrame=4;let Ws=jl;Object.defineProperties(O,{UPLOADS_PER_FRAME:{get(){return Ws.uploadsPerFrame},set(s){Ws.uploadsPerFrame=s}}});function zl(s,t){return t instanceof X?(t._glTextures[s.CONTEXT_UID]||s.texture.bind(t),!0):!1}function dm(s,t){if(!(t instanceof Gn))return!1;const{geometry:e}=t;t.finishPoly(),e.updateBatches();const{batches:i}=e;for(let r=0;r=this._durations[this.currentFrame];)r-=this._durations[this.currentFrame]*n,this._currentTime+=n;this._currentTime+=r/this._durations[this.currentFrame]}else this._currentTime+=e;this._currentTime<0&&!this.loop?(this.gotoAndStop(0),this.onComplete&&this.onComplete()):this._currentTime>=this._textures.length&&!this.loop?(this.gotoAndStop(this._textures.length-1),this.onComplete&&this.onComplete()):i!==this.currentFrame&&(this.loop&&this.onLoop&&(this.animationSpeed>0&&this.currentFramei)&&this.onLoop(),this.updateTexture())}updateTexture(){const t=this.currentFrame;this._previousFrame!==t&&(this._previousFrame=t,this._texture=this._textures[t],this._textureID=-1,this._textureTrimmedID=-1,this._cachedTint=16777215,this.uvs=this._texture._uvs.uvsFloat32,this.updateAnchor&&this._anchor.copyFrom(this._texture.defaultAnchor),this.onFrameChange&&this.onFrameChange(this.currentFrame))}destroy(t){this.stop(),super.destroy(t),this.onComplete=null,this.onFrameChange=null,this.onLoop=null}static fromFrames(t){const e=[];for(let i=0;ithis.totalFrames-1)throw new Error(`[AnimatedSprite]: Invalid frame index value ${t}, expected to be between 0 and totalFrames ${this.totalFrames}.`);const e=this.currentFrame;this._currentTime=t,e!==this.currentFrame&&this.updateTexture()}get playing(){return this._playing}get autoUpdate(){return this._autoUpdate}set autoUpdate(t){t!==this._autoUpdate&&(this._autoUpdate=t,!this._autoUpdate&&this._isConnectedToTicker?(mt.shared.remove(this.update,this),this._isConnectedToTicker=!1):this._autoUpdate&&!this._isConnectedToTicker&&this._playing&&(mt.shared.add(this.update,this),this._isConnectedToTicker=!0))}}const $i=new q;class Wn extends ue{constructor(t,e=100,i=100){super(t),this.tileTransform=new _s,this._width=e,this._height=i,this.uvMatrix=this.texture.uvMatrix||new ws(t),this.pluginName="tilingSprite",this.uvRespectAnchor=!1}get clampMargin(){return this.uvMatrix.clampMargin}set clampMargin(t){this.uvMatrix.clampMargin=t,this.uvMatrix.update(!0)}get tileScale(){return this.tileTransform.scale}set tileScale(t){this.tileTransform.scale.copyFrom(t)}get tilePosition(){return this.tileTransform.position}set tilePosition(t){this.tileTransform.position.copyFrom(t)}_onTextureUpdate(){this.uvMatrix&&(this.uvMatrix.texture=this._texture),this._cachedTint=16777215}_render(t){const e=this._texture;!e||!e.valid||(this.tileTransform.updateLocalTransform(),this.uvMatrix.update(),t.batch.setObjectRenderer(t.plugins[this.pluginName]),t.plugins[this.pluginName].render(this))}_calculateBounds(){const t=this._width*-this._anchor._x,e=this._height*-this._anchor._y,i=this._width*(1-this._anchor._x),r=this._height*(1-this._anchor._y);this._bounds.addFrame(this.transform,t,e,i,r)}getLocalBounds(t){return this.children.length===0?(this._bounds.minX=this._width*-this._anchor._x,this._bounds.minY=this._height*-this._anchor._y,this._bounds.maxX=this._width*(1-this._anchor._x),this._bounds.maxY=this._height*(1-this._anchor._y),t||(this._localBoundsRect||(this._localBoundsRect=new j),t=this._localBoundsRect),this._bounds.getRectangle(t)):super.getLocalBounds.call(this,t)}containsPoint(t){this.worldTransform.applyInverse(t,$i);const e=this._width,i=this._height,r=-e*this.anchor._x;if($i.x>=r&&$i.x=n&&$i.y1?Vt.from(gm,mm,e):Vt.from(Wl,_m,e)}render(t){const e=this.renderer,i=this.quad;let r=i.vertices;r[0]=r[6]=t._width*-t.anchor.x,r[1]=r[3]=t._height*-t.anchor.y,r[2]=r[4]=t._width*(1-t.anchor.x),r[5]=r[7]=t._height*(1-t.anchor.y);const n=t.uvRespectAnchor?t.anchor.x:0,a=t.uvRespectAnchor?t.anchor.y:0;r=i.uvs,r[0]=r[6]=-n,r[1]=r[3]=-a,r[2]=r[4]=1-n,r[5]=r[7]=1-a,i.invalidate();const o=t._texture,h=o.baseTexture,l=h.alphaMode>0,u=t.tileTransform.localTransform,c=t.uvMatrix;let d=h.isPowerOfTwo&&o.frame.width===h.width&&o.frame.height===h.height;d&&(h._glTextures[e.CONTEXT_UID]?d=h.wrapMode!==Wt.CLAMP:h.wrapMode===Wt.CLAMP&&(h.wrapMode=Wt.REPEAT));const f=d?this.simpleShader:this.shader,p=o.width,m=o.height,g=t._width,y=t._height;qs.set(u.a*p/g,u.b*p/y,u.c*m/g,u.d*m/y,u.tx/g,u.ty/y),qs.invert(),d?qs.prepend(c.mapCoord):(f.uniforms.uMapCoord=c.mapCoord.toArray(!0),f.uniforms.uClampFrame=c.uClampFrame,f.uniforms.uClampOffset=c.uClampOffset),f.uniforms.uTransform=qs.toArray(!0),f.uniforms.uColor=Z.shared.setValue(t.tint).premultiply(t.worldAlpha,l).toArray(f.uniforms.uColor),f.uniforms.translationMatrix=t.transform.worldTransform.toArray(!0),f.uniforms.uSampler=o,e.shader.bind(f),e.geometry.bind(i),this.state.blendMode=wr(t.blendMode,l),e.state.set(this.state),e.geometry.draw(this.renderer.gl.TRIANGLES,6,0)}}Yn.extension={name:"tilingSprite",type:R.RendererPlugin},L.add(Yn);const Yl=class Ki{constructor(t,e,i=null){this.linkedSheets=[],this._texture=t instanceof B?t:null,this.baseTexture=t instanceof X?t:this._texture.baseTexture,this.textures={},this.animations={},this.data=e;const r=this.baseTexture.resource;this.resolution=this._updateResolution(i||(r?r.url:null)),this._frames=this.data.frames,this._frameKeys=Object.keys(this._frames),this._batchIndex=0,this._callback=null}_updateResolution(t=null){const{scale:e}=this.data.meta;let i=Kt(t,null);return i===null&&(i=parseFloat(e!=null?e:"1")),i!==1&&this.baseTexture.setResolution(i),i}parse(){return new Promise(t=>{this._callback=t,this._batchIndex=0,this._frameKeys.length<=Ki.BATCH_SIZE?(this._processFrames(0),this._processAnimations(),this._parseComplete()):this._nextBatch()})}_processFrames(t){let e=t;const i=Ki.BATCH_SIZE;for(;e-t{this._batchIndex*Ki.BATCH_SIZE{i[r]=t}),Object.keys(t.textures).forEach(r=>{i[r]=t.textures[r]}),!e){const r=lt.dirname(s[0]);t.linkedSheets.forEach((n,a)=>{const o=ql([`${r}/${t.data.meta.related_multi_packs[a]}`],n,!0);Object.assign(i,o)})}return i}const Kl={extension:R.Asset,cache:{test:s=>s instanceof qn,getCacheableAssets:(s,t)=>ql(s,t,!1)},resolver:{test:s=>{const t=s.split("?")[0].split("."),e=t.pop(),i=t.pop();return e==="json"&&ym.includes(i)},parse:s=>{var t,e;const i=s.split(".");return{resolution:parseFloat((e=(t=O.RETINA_PREFIX.exec(s))==null?void 0:t[1])!=null?e:"1"),format:i[i.length-2],src:s}}},loader:{name:"spritesheetLoader",extension:{type:R.LoadParser,priority:Nt.Normal},async testParse(s,t){return lt.extname(t.src).toLowerCase()===".json"&&!!s.frames},async parse(s,t,e){var i,r;let n=lt.dirname(t.src);n&&n.lastIndexOf("/")!==n.length-1&&(n+="/");let a=n+s.meta.image;a=Ns(a,t.src);const o=(await e.load([a]))[a],h=new qn(o.baseTexture,s,t.src);await h.parse();const l=(i=s==null?void 0:s.meta)==null?void 0:i.related_multi_packs;if(Array.isArray(l)){const u=[];for(const d of l){if(typeof d!="string")continue;let f=n+d;(r=t.data)!=null&&r.ignoreMultiPack||(f=Ns(f,t.src),u.push(e.load({src:f,data:{ignoreMultiPack:!0}})))}const c=await Promise.all(u);h.linkedSheets=c,c.forEach(d=>{d.linkedSheets=[h].concat(h.linkedSheets.filter(f=>f!==d))})}return h},unload(s){s.destroy(!0)}}};L.add(Kl);class Hi{constructor(){this.info=[],this.common=[],this.page=[],this.char=[],this.kerning=[],this.distanceField=[]}}class Vi{static test(t){return typeof t=="string"&&t.startsWith("info face=")}static parse(t){const e=t.match(/^[a-z]+\s+.+$/gm),i={info:[],common:[],page:[],char:[],chars:[],kerning:[],kernings:[],distanceField:[]};for(const n in e){const a=e[n].match(/^[a-z]+/gm)[0],o=e[n].match(/[a-zA-Z]+=([^\s"']+|"([^"]*)")/gm),h={};for(const l in o){const u=o[l].split("="),c=u[0],d=u[1].replace(/"/gm,""),f=parseFloat(d),p=isNaN(f)?d:f;h[c]=p}i[a].push(h)}const r=new Hi;return i.info.forEach(n=>r.info.push({face:n.face,size:parseInt(n.size,10)})),i.common.forEach(n=>r.common.push({lineHeight:parseInt(n.lineHeight,10)})),i.page.forEach(n=>r.page.push({id:parseInt(n.id,10),file:n.file})),i.char.forEach(n=>r.char.push({id:parseInt(n.id,10),page:parseInt(n.page,10),x:parseInt(n.x,10),y:parseInt(n.y,10),width:parseInt(n.width,10),height:parseInt(n.height,10),xoffset:parseInt(n.xoffset,10),yoffset:parseInt(n.yoffset,10),xadvance:parseInt(n.xadvance,10)})),i.kerning.forEach(n=>r.kerning.push({first:parseInt(n.first,10),second:parseInt(n.second,10),amount:parseInt(n.amount,10)})),i.distanceField.forEach(n=>r.distanceField.push({distanceRange:parseInt(n.distanceRange,10),fieldType:n.fieldType})),r}}class Ks{static test(t){const e=t;return typeof t!="string"&&"getElementsByTagName"in t&&e.getElementsByTagName("page").length&&e.getElementsByTagName("info")[0].getAttribute("face")!==null}static parse(t){const e=new Hi,i=t.getElementsByTagName("info"),r=t.getElementsByTagName("common"),n=t.getElementsByTagName("page"),a=t.getElementsByTagName("char"),o=t.getElementsByTagName("kerning"),h=t.getElementsByTagName("distanceField");for(let l=0;l")?Ks.test(O.ADAPTER.parseXML(t)):!1}static parse(t){return Ks.parse(O.ADAPTER.parseXML(t))}}const Kn=[Vi,Ks,Zs];function Zl(s){for(let t=0;tt in s?Em(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,Am=(s,t)=>{for(var e in t||(t={}))Jl.call(t,e)&&eu(s,e,t[e]);if(Js)for(var e of Js(t))tu.call(t,e)&&eu(s,e,t[e]);return s},wm=(s,t)=>{var e={};for(var i in s)Jl.call(s,i)&&t.indexOf(i)<0&&(e[i]=s[i]);if(s!=null&&Js)for(var i of Js(s))t.indexOf(i)<0&&tu.call(s,i)&&(e[i]=s[i]);return e};const Se=class ie{constructor(t,e,i){var r,n;const[a]=t.info,[o]=t.common,[h]=t.page,[l]=t.distanceField,u=Kt(h.file),c={};this._ownsTextures=i,this.font=a.face,this.size=a.size,this.lineHeight=o.lineHeight/u,this.chars={},this.pageTextures=c;for(let d=0;d=l-N*o){if(g===0)throw new Error(`[BitmapFont] textureHeight ${l}px is too small (fontFamily: '${d.fontFamily}', fontSize: ${d.fontSize}px, char: '${F}')`);--w,y=null,b=null,v=null,g=0,m=0,x=0;continue}if(x=Math.max(N+G.fontProperties.descent,x),T*o+m>=f){if(m===0)throw new Error(`[BitmapFont] textureWidth ${h}px is too small (fontFamily: '${d.fontFamily}', fontSize: ${d.fontSize}px, char: '${F}')`);--w,g+=x*o,g=Math.ceil(g),m=0,x=0;continue}bm(y,b,G,m,g,o,d);const I=Qs(G.text);p.char.push({id:I,page:M.length-1,x:m/o,y:g/o,width:T,height:N,xoffset:0,yoffset:0,xadvance:Y-(d.dropShadow?d.dropShadowDistance:0)-(d.stroke?d.strokeThickness:0)}),m+=(T+2*a)*o,m=Math.ceil(m)}if(!(i!=null&&i.skipKerning))for(let w=0,F=c.length;w 0.99) {\r + alpha = 1.0;\r + }\r +\r + // Gamma correction for coverage-like alpha\r + float luma = dot(uColor.rgb, vec3(0.299, 0.587, 0.114));\r + float gamma = mix(1.0, 1.0 / 2.2, luma);\r + float coverage = pow(uColor.a * alpha, gamma); \r +\r + // NPM Textures, NPM outputs\r + gl_FragColor = vec4(uColor.rgb, coverage);\r +}\r +`,Im=`// Mesh material default fragment\r +attribute vec2 aVertexPosition;\r +attribute vec2 aTextureCoord;\r +\r +uniform mat3 projectionMatrix;\r +uniform mat3 translationMatrix;\r +uniform mat3 uTextureMatrix;\r +\r +varying vec2 vTextureCoord;\r +\r +void main(void)\r +{\r + gl_Position = vec4((projectionMatrix * translationMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);\r +\r + vTextureCoord = (uTextureMatrix * vec3(aTextureCoord, 1.0)).xy;\r +}\r +`;const iu=[],su=[],ru=[],nu=class du extends It{constructor(t,e={}){super();const{align:i,tint:r,maxWidth:n,letterSpacing:a,fontName:o,fontSize:h}=Object.assign({},du.styleDefaults,e);if(!ge.available[o])throw new Error(`Missing BitmapFont "${o}"`);this._activePagesMeshData=[],this._textWidth=0,this._textHeight=0,this._align=i,this._tintColor=new Z(r),this._font=void 0,this._fontName=o,this._fontSize=h,this.text=t,this._maxWidth=n,this._maxLineHeight=0,this._letterSpacing=a,this._anchor=new oe(()=>{this.dirty=!0},this,0,0),this._roundPixels=O.ROUND_PIXELS,this.dirty=!0,this._resolution=O.RESOLUTION,this._autoResolution=!0,this._textureCache={}}updateText(){var t;const e=ge.available[this._fontName],i=this.fontSize,r=i/e.size,n=new q,a=[],o=[],h=[],l=this._text.replace(/(?:\r\n|\r)/g,` +`)||" ",u=Ql(l),c=this._maxWidth*e.size/i,d=e.distanceFieldType==="none"?iu:su;let f=null,p=0,m=0,g=0,y=-1,b=0,v=0,x=0,E=0;for(let N=0;N0&&n.x>c&&(++v,Ce(a,1+y-v,1+N-y),N=y,y=-1,o.push(b),h.push(a.length>0?a[a.length-1].prevSpaces:0),m=Math.max(m,b),g++,n.x=0,n.y+=e.lineHeight,f=null,E=0)}const M=u[u.length-1];M!=="\r"&&M!==` +`&&(/(?:\s)/.test(M)&&(p=b),o.push(p),m=Math.max(m,p),h.push(-1));const S=[];for(let N=0;N<=g;N++){let T=0;this._align==="right"?T=m-o[N]:this._align==="center"?T=(m-o[N])/2:this._align==="justify"&&(T=h[N]<0?0:(m-o[N])/h[N]),S.push(T)}const w=a.length,F={},G=[],Y=this._activePagesMeshData;d.push(...Y);for(let N=0;N6*I)||T.vertices.lengthe[r.mesh.texture.baseTexture.uid]).forEach(r=>{r.mesh.texture=B.EMPTY});for(const r in e)e[r].destroy(),delete e[r];this._font=null,this._tintColor=null,this._textureCache=null,super.destroy(t)}};nu.styleDefaults={align:"left",tint:16777215,maxWidth:0,letterSpacing:0};let Rm=nu;const Cm=[".xml",".fnt"],au={extension:{type:R.LoadParser,priority:Nt.Normal},name:"loadBitmapFont",test(s){return Cm.includes(lt.extname(s).toLowerCase())},async testParse(s){return Vi.test(s)||Zs.test(s)},async parse(s,t,e){const i=Vi.test(s)?Vi.parse(s):Zs.parse(s),{src:r}=t,{page:n}=i,a=[];for(let l=0;lo[l]);return ge.install(i,h,!0)},async load(s,t){return(await O.ADAPTER.fetch(s)).text()},unload(s){s.destroy()}};L.add(au);var Pm=Object.defineProperty,Mm=Object.defineProperties,Dm=Object.getOwnPropertyDescriptors,ou=Object.getOwnPropertySymbols,Om=Object.prototype.hasOwnProperty,Bm=Object.prototype.propertyIsEnumerable,hu=(s,t,e)=>t in s?Pm(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,Fm=(s,t)=>{for(var e in t||(t={}))Om.call(t,e)&&hu(s,e,t[e]);if(ou)for(var e of ou(t))Bm.call(t,e)&&hu(s,e,t[e]);return s},Nm=(s,t)=>Mm(s,Dm(t));const Zn=class si extends me{constructor(){super(...arguments),this._fonts=[],this._overrides=[],this._stylesheet="",this.fontsDirty=!1}static from(t){return new si(Object.keys(si.defaultOptions).reduce((e,i)=>Nm(Fm({},e),{[i]:t[i]}),{}))}cleanFonts(){this._fonts.length>0&&(this._fonts.forEach(t=>{URL.revokeObjectURL(t.src),t.refs--,t.refs===0&&(t.fontFace&&document.fonts.delete(t.fontFace),delete si.availableFonts[t.originalUrl])}),this.fontFamily="Arial",this._fonts.length=0,this.styleID++,this.fontsDirty=!0)}loadFont(t,e={}){const{availableFonts:i}=si;if(i[t]){const r=i[t];return this._fonts.push(r),r.refs++,this.styleID++,this.fontsDirty=!0,Promise.resolve()}return O.ADAPTER.fetch(t).then(r=>r.blob()).then(async r=>new Promise((n,a)=>{const o=URL.createObjectURL(r),h=new FileReader;h.onload=()=>n([o,h.result]),h.onerror=a,h.readAsDataURL(r)})).then(async([r,n])=>{const a=Object.assign({family:lt.basename(t,lt.extname(t)),weight:"normal",style:"normal",display:"auto",src:r,dataSrc:n,refs:1,originalUrl:t,fontFace:null},e);i[t]=a,this._fonts.push(a),this.styleID++;const o=new FontFace(a.family,`url(${a.src})`,{weight:a.weight,style:a.style,display:a.display});a.fontFace=o,await o.load(),document.fonts.add(o),await document.fonts.ready,this.styleID++,this.fontsDirty=!0})}addOverride(...t){const e=t.filter(i=>!this._overrides.includes(i));e.length>0&&(this._overrides.push(...e),this.styleID++)}removeOverride(...t){const e=t.filter(i=>this._overrides.includes(i));e.length>0&&(this._overrides=this._overrides.filter(i=>!e.includes(i)),this.styleID++)}toCSS(t){return[`transform: scale(${t})`,"transform-origin: top left","display: inline-block",`color: ${this.normalizeColor(this.fill)}`,`font-size: ${this.fontSize}px`,`font-family: ${this.fontFamily}`,`font-weight: ${this.fontWeight}`,`font-style: ${this.fontStyle}`,`font-variant: ${this.fontVariant}`,`letter-spacing: ${this.letterSpacing}px`,`text-align: ${this.align}`,`padding: ${this.padding}px`,`white-space: ${this.whiteSpace}`,...this.lineHeight?[`line-height: ${this.lineHeight}px`]:[],...this.wordWrap?[`word-wrap: ${this.breakWords?"break-all":"break-word"}`,`max-width: ${this.wordWrapWidth}px`]:[],...this.strokeThickness?[`-webkit-text-stroke-width: ${this.strokeThickness}px`,`-webkit-text-stroke-color: ${this.normalizeColor(this.stroke)}`,`text-stroke-width: ${this.strokeThickness}px`,`text-stroke-color: ${this.normalizeColor(this.stroke)}`,"paint-order: stroke"]:[],...this.dropShadow?[this.dropShadowToCSS()]:[],...this._overrides].join(";")}toGlobalCSS(){return this._fonts.reduce((t,e)=>`${t} + @font-face { + font-family: "${e.family}"; + src: url('${e.dataSrc}'); + font-weight: ${e.weight}; + font-style: ${e.style}; + font-display: ${e.display}; + }`,this._stylesheet)}get stylesheet(){return this._stylesheet}set stylesheet(t){this._stylesheet!==t&&(this._stylesheet=t,this.styleID++)}normalizeColor(t){return Array.isArray(t)&&(t=Ka(t)),typeof t=="number"?qa(t):t}dropShadowToCSS(){let t=this.normalizeColor(this.dropShadowColor);const e=this.dropShadowAlpha,i=Math.round(Math.cos(this.dropShadowAngle)*this.dropShadowDistance),r=Math.round(Math.sin(this.dropShadowAngle)*this.dropShadowDistance);t.startsWith("#")&&e<1&&(t+=(e*255|0).toString(16).padStart(2,"0"));const n=`${i}px ${r}px`;return this.dropShadowBlur>0?`text-shadow: ${n} ${this.dropShadowBlur}px ${t}`:`text-shadow: ${n} ${t}`}reset(){Object.assign(this,si.defaultOptions)}onBeforeDraw(){const{fontsDirty:t}=this;return this.fontsDirty=!1,this.isSafari&&this._fonts.length>0&&t?new Promise(e=>setTimeout(e,100)):Promise.resolve()}get isSafari(){const{userAgent:t}=O.ADAPTER.getNavigator();return/^((?!chrome|android).)*safari/i.test(t)}set fillGradientStops(t){console.warn("[HTMLTextStyle] fillGradientStops is not supported by HTMLText")}get fillGradientStops(){return super.fillGradientStops}set fillGradientType(t){console.warn("[HTMLTextStyle] fillGradientType is not supported by HTMLText")}get fillGradientType(){return super.fillGradientType}set miterLimit(t){console.warn("[HTMLTextStyle] miterLimit is not supported by HTMLText")}get miterLimit(){return super.miterLimit}set trim(t){console.warn("[HTMLTextStyle] trim is not supported by HTMLText")}get trim(){return super.trim}set textBaseline(t){console.warn("[HTMLTextStyle] textBaseline is not supported by HTMLText")}get textBaseline(){return super.textBaseline}set leading(t){console.warn("[HTMLTextStyle] leading is not supported by HTMLText")}get leading(){return super.leading}set lineJoin(t){console.warn("[HTMLTextStyle] lineJoin is not supported by HTMLText")}get lineJoin(){return super.lineJoin}};Zn.availableFonts={},Zn.defaultOptions={align:"left",breakWords:!1,dropShadow:!1,dropShadowAlpha:1,dropShadowAngle:Math.PI/6,dropShadowBlur:0,dropShadowColor:"black",dropShadowDistance:5,fill:"black",fontFamily:"Arial",fontSize:26,fontStyle:"normal",fontVariant:"normal",fontWeight:"normal",letterSpacing:0,lineHeight:0,padding:0,stroke:"black",strokeThickness:0,whiteSpace:"normal",wordWrap:!1,wordWrapWidth:100};let tr=Zn;const Xi=class ri extends ue{constructor(t="",e={}){var i;super(B.EMPTY),this._text=null,this._style=null,this._autoResolution=!0,this.localStyleID=-1,this.dirty=!1,this._updateID=0,this.ownsStyle=!1;const r=new Image,n=B.from(r,{scaleMode:O.SCALE_MODE,resourceOptions:{autoLoad:!1}});n.orig=new j,n.trim=new j,this.texture=n;const a="http://www.w3.org/2000/svg",o="http://www.w3.org/1999/xhtml",h=document.createElementNS(a,"svg"),l=document.createElementNS(a,"foreignObject"),u=document.createElementNS(o,"div"),c=document.createElementNS(o,"style");l.setAttribute("width","10000"),l.setAttribute("height","10000"),l.style.overflow="hidden",h.appendChild(l),this.maxWidth=ri.defaultMaxWidth,this.maxHeight=ri.defaultMaxHeight,this._domElement=u,this._styleElement=c,this._svgRoot=h,this._foreignObject=l,this._foreignObject.appendChild(c),this._foreignObject.appendChild(u),this._image=r,this._loadImage=new Image,this._autoResolution=ri.defaultAutoResolution,this._resolution=(i=ri.defaultResolution)!=null?i:O.RESOLUTION,this.text=t,this.style=e}measureText(t){var e,i;const{text:r,style:n,resolution:a}=Object.assign({text:this._text,style:this._style,resolution:this._resolution},t);Object.assign(this._domElement,{innerHTML:r,style:n.toCSS(a)}),this._styleElement.textContent=n.toGlobalCSS(),document.body.appendChild(this._svgRoot);const o=this._domElement.getBoundingClientRect();this._svgRoot.remove();const{width:h,height:l}=o,u=Math.min(this.maxWidth,Math.ceil(h)),c=Math.min(this.maxHeight,Math.ceil(l));return this._svgRoot.setAttribute("width",u.toString()),this._svgRoot.setAttribute("height",c.toString()),r!==this._text&&(this._domElement.innerHTML=this._text),n!==this._style&&(Object.assign(this._domElement,{style:(e=this._style)==null?void 0:e.toCSS(a)}),this._styleElement.textContent=(i=this._style)==null?void 0:i.toGlobalCSS()),{width:u+n.padding*2,height:c+n.padding*2}}async updateText(t=!0){const{style:e,_image:i,_loadImage:r}=this;if(this.localStyleID!==e.styleID&&(this.dirty=!0,this.localStyleID=e.styleID),!this.dirty&&t)return;const{width:n,height:a}=this.measureText();i.width=r.width=Math.ceil(Math.max(1,n)),i.height=r.height=Math.ceil(Math.max(1,a)),this._updateID++;const o=this._updateID;await new Promise(h=>{r.onload=async()=>{if(o/gi,"

").replace(/
/gi,"
").replace(/ /gi," ")}};Xi.defaultDestroyOptions={texture:!0,children:!1,baseTexture:!0},Xi.defaultMaxWidth=2024,Xi.defaultMaxHeight=2024,Xi.defaultAutoResolution=!0;let Lm=Xi;return _.ALPHA_MODES=bt,_.AbstractMultiResource=yn,_.AccessibilityManager=Sn,_.AlphaFilter=ch,_.AnimatedSprite=Ys,_.Application=wh,_.ArrayResource=rh,_.Assets=Oi,_.AssetsClass=Jh,_.Attribute=gi,_.BLEND_MODES=H,_.BUFFER_BITS=Zi,_.BUFFER_TYPE=Gt,_.BackgroundSystem=Ti,_.BaseImageResource=he,_.BasePrepare=Ws,_.BaseRenderTexture=Wr,_.BaseTexture=X,_.BatchDrawCall=cs,_.BatchGeometry=Gr,_.BatchRenderer=ye,_.BatchShaderGenerator=Io,_.BatchSystem=zr,_.BatchTextureArray=bs,_.BitmapFont=ge,_.BitmapFontData=Hi,_.BitmapText=Rm,_.BlobResource=dl,_.BlurFilter=dh,_.BlurFilterPass=Ds,_.Bounds=Ri,_.BrowserAdapter=aa,_.Buffer=nt,_.BufferResource=mi,_.BufferSystem=_n,_.CLEAR_MODES=kt,_.COLOR_MASK_BITS=na,_.Cache=Ee,_.CanvasResource=nh,_.Circle=fs,_.Color=Z,_.ColorMatrixFilter=Os,_.CompressedTextureResource=Ae,_.Container=It,_.ContextSystem=Ei,_.CountLimiter=Xl,_.CubeResource=oh,_.DEG_TO_RAD=mo,_.DRAW_MODES=Lt,_.DisplacementFilter=fh,_.DisplayObject=it,_.ENV=_e,_.Ellipse=ps,_.EventBoundary=gh,_.EventSystem=Bs,_.ExtensionType=R,_.Extract=Il,_.FORMATS=A,_.FORMATS_TO_COMPONENTS=gl,_.FXAAFilter=ph,_.FederatedDisplayObject=xh,_.FederatedEvent=Ye,_.FederatedMouseEvent=Pi,_.FederatedPointerEvent=Bt,_.FederatedWheelEvent=Ue,_.FillStyle=Ui,_.Filter=yt,_.FilterState=Mo,_.FilterSystem=Jr,_.Framebuffer=Ts,_.FramebufferSystem=tn,_.GC_MODES=Qi,_.GLFramebuffer=Do,_.GLProgram=Vo,_.GLTexture=Is,_.GRAPHICS_CURVES=Xp,_.GenerateTextureSystem=hn,_.Geometry=ae,_.GeometrySystem=sn,_.Graphics=Gn,_.GraphicsData=Li,_.GraphicsGeometry=Bl,_.HTMLText=Lm,_.HTMLTextStyle=tr,_.IGLUniformData=pd,_.INSTALLED=us,_.INTERNAL_FORMATS=Et,_.INTERNAL_FORMAT_TO_BYTES_PER_PIXEL=Bi,_.ImageBitmapResource=Le,_.ImageResource=Yr,_.LINE_CAP=fe,_.LINE_JOIN=Rt,_.LineStyle=Xs,_.LoaderParserPriority=Nt,_.MASK_TYPES=ht,_.MIPMAP_MODES=Ut,_.MSAA_QUALITY=at,_.MaskData=Fo,_.MaskSystem=rn,_.Matrix=tt,_.Mesh=Je,_.MeshBatchUvs=Fl,_.MeshGeometry=ki,_.MeshMaterial=ti,_.MultisampleSystem=gn,_.NineSlicePlane=Kp,_.NoiseFilter=mh,_.ObjectRenderer=xi,_.ObjectRendererSystem=vn,_.ObservablePoint=oe,_.PI_2=_i,_.PRECISION=At,_.ParticleContainer=Jp,_.ParticleRenderer=Hn,_.PlaneGeometry=Ul,_.PluginSystem=an,_.Point=q,_.Polygon=Pe,_.Prepare=zn,_.Program=Qt,_.ProjectionSystem=on,_.Quad=Po,_.QuadUv=Zr,_.RAD_TO_DEG=po,_.RENDERER_TYPE=or,_.Rectangle=j,_.RenderTexture=xe,_.RenderTexturePool=Kr,_.RenderTextureSystem=ln,_.Renderer=Ps,_.ResizePlugin=In,_.Resource=We,_.RopeGeometry=kl,_.RoundedRectangle=ms,_.Runner=St,_.SAMPLER_TYPES=D,_.SCALE_MODES=zt,_.SHAPES=pt,_.SVGResource=Ms,_.ScissorSystem=Go,_.Shader=Vt,_.ShaderSystem=un,_.SimpleMesh=Zp,_.SimplePlane=Gl,_.SimpleRope=Qp,_.Sprite=ue,_.SpriteMaskFilter=Bo,_.Spritesheet=qn,_.StartupSystem=wi,_.State=Zt,_.StateSystem=Ko,_.StencilSystem=nn,_.SystemManager=Zo,_.TARGETS=Ie,_.TEXT_GRADIENT=Gi,_.TYPES=k,_.TYPES_TO_BYTES_PER_COMPONENT=On,_.TYPES_TO_BYTES_PER_PIXEL=_l,_.TemporaryDisplayObject=hh,_.Text=jn,_.TextFormat=Vi,_.TextMetrics=pe,_.TextStyle=me,_.Texture=B,_.TextureGCSystem=be,_.TextureMatrix=ws,_.TextureSystem=cn,_.TextureUvs=qr,_.Ticker=mt,_.TickerPlugin=pn,_.TilingSprite=Wn,_.TilingSpriteRenderer=Yn,_.TimeLimiter=pm,_.Transform=_s,_.TransformFeedback=Fd,_.TransformFeedbackSystem=dn,_.UPDATE_PRIORITY=le,_.UniformGroup=Ot,_.VERSION=Nd,_.VideoResource=Tn,_.ViewSystem=Ii,_.ViewableBuffer=ls,_.WRAP_MODES=Wt,_.XMLFormat=Ks,_.XMLStringFormat=Zs,_.accessibleTarget=bh,_.autoDetectFormat=Zl,_.autoDetectRenderer=ih,_.autoDetectResource=Ur,_.cacheTextureArray=tl,_.checkDataUrl=ke,_.checkExtension=ce,_.checkMaxIfStatementsInShader=lo,_.convertToList=Ft,_.copySearchParams=Ns,_.createStringVariations=Rh,_.createTexture=qe,_.createUBOElements=zo,_.curves=we,_.defaultFilterVertex=mn,_.defaultVertex=sh,_.detectAvif=il,_.detectCompressedTextures=cl,_.detectDefaults=nl,_.detectMp4=ol,_.detectOgv=hl,_.detectWebm=al,_.detectWebp=sl,_.extensions=L,_.filters=En,_.generateProgram=Xo,_.generateUniformBufferSync=Yo,_.getFontFamilyName=Fh,_.getTestContext=xo,_.getUBOData=Wo,_.graphicsUtils=Wp,_.groupD8=et,_.isMobile=$t,_.isSingleItem=Mi,_.loadBitmapFont=au,_.loadDDS=bl,_.loadImageBitmap=Hh,_.loadJson=Mh,_.loadKTX=Al,_.loadSVG=jh,_.loadTextures=Di,_.loadTxt=Dh,_.loadVideo=qh,_.loadWebFont=Nh,_.parseDDS=pl,_.parseKTX=vl,_.resolveCompressedTextureUrl=wl,_.resolveTextureUrl=ll,_.settings=O,_.spritesheetAsset=Kl,_.uniformParsers=Fe,_.unsafeEvalSupported=So,_.utils=wc,_}({}); +//# sourceMappingURL=pixi.min.js.map diff --git a/modules/control-audio.js b/modules/control-audio.js index cf1b1d9..ab903e1 100644 --- a/modules/control-audio.js +++ b/modules/control-audio.js @@ -1,268 +1,268 @@ -"use strict"; - -import { extension_settings } from "../../../../extensions.js"; -import { eventSource, event_types } from "../../../../../script.js"; -import { SlashCommandParser } from "../../../../slash-commands/SlashCommandParser.js"; -import { SlashCommand } from "../../../../slash-commands/SlashCommand.js"; -import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from "../../../../slash-commands/SlashCommandArgument.js"; - -const AudioHost = (() => { - /** @typedef {{ audio: HTMLAudioElement|null, currentUrl: string }} AudioInstance */ - /** @type {Record<'primary'|'secondary', AudioInstance>} */ - const instances = { - primary: { audio: null, currentUrl: "" }, - secondary: { audio: null, currentUrl: "" }, - }; - - /** - * @param {('primary'|'secondary')} area - * @returns {HTMLAudioElement} - */ - function getOrCreate(area) { - const inst = instances[area] || (instances[area] = { audio: null, currentUrl: "" }); - if (!inst.audio) { - inst.audio = new Audio(); - inst.audio.preload = "auto"; - try { inst.audio.crossOrigin = "anonymous"; } catch { } - } - return inst.audio; - } - - /** - * @param {string} url - * @param {boolean} loop - * @param {('primary'|'secondary')} area - * @param {number} volume10 1-10 - */ - async function playUrl(url, loop = false, area = 'primary', volume10 = 5) { - const u = String(url || "").trim(); - if (!/^https?:\/\//i.test(u)) throw new Error("仅支持 http/https 链接"); - const a = getOrCreate(area); - a.loop = !!loop; - - let v = Number(volume10); - if (!Number.isFinite(v)) v = 5; - v = Math.max(1, Math.min(10, v)); - try { a.volume = v / 10; } catch { } - - const inst = instances[area]; - if (inst.currentUrl && u === inst.currentUrl) { - if (a.paused) await a.play(); - return `继续播放: ${u}`; - } - - inst.currentUrl = u; - if (a.src !== u) { - a.src = u; - try { await a.play(); } - catch (e) { throw new Error("播放失败"); } - } else { - try { a.currentTime = 0; await a.play(); } catch { } - } - return `播放: ${u}`; - } - - /** - * @param {('primary'|'secondary')} area - */ - function stop(area = 'primary') { - const inst = instances[area]; - if (inst?.audio) { - try { inst.audio.pause(); } catch { } - } - return "已停止"; - } - - /** - * @param {('primary'|'secondary')} area - */ - function getCurrentUrl(area = 'primary') { - const inst = instances[area]; - return inst?.currentUrl || ""; - } - - function reset() { - for (const key of /** @type {('primary'|'secondary')[]} */(['primary','secondary'])) { - const inst = instances[key]; - if (inst.audio) { - try { inst.audio.pause(); } catch { } - try { inst.audio.removeAttribute('src'); inst.audio.load(); } catch { } - } - inst.currentUrl = ""; - } - } - - function stopAll() { - for (const key of /** @type {('primary'|'secondary')[]} */(['primary','secondary'])) { - const inst = instances[key]; - if (inst?.audio) { - try { inst.audio.pause(); } catch { } - } - } - return "已全部停止"; - } - - /** - * 清除指定实例:停止并移除 src,清空 currentUrl - * @param {('primary'|'secondary')} area - */ - function clear(area = 'primary') { - const inst = instances[area]; - if (inst?.audio) { - try { inst.audio.pause(); } catch { } - try { inst.audio.removeAttribute('src'); inst.audio.load(); } catch { } - } - inst.currentUrl = ""; - return "已清除"; - } - - return { playUrl, stop, stopAll, clear, getCurrentUrl, reset }; -})(); - -let registeredCommand = null; -let chatChangedHandler = null; -let isRegistered = false; -let globalStateChangedHandler = null; - -function registerSlash() { - if (isRegistered) return; - try { - registeredCommand = SlashCommand.fromProps({ - name: "xbaudio", - callback: async (args, value) => { - try { - const action = String(args.play || "").toLowerCase(); - const mode = String(args.mode || "loop").toLowerCase(); - const rawArea = args.area; - const hasArea = typeof rawArea !== 'undefined' && rawArea !== null && String(rawArea).trim() !== ''; - const area = hasArea && String(rawArea).toLowerCase() === 'secondary' ? 'secondary' : 'primary'; - const volumeArg = args.volume; - let volume = Number(volumeArg); - if (!Number.isFinite(volume)) volume = 5; - const url = String(value || "").trim(); - const loop = mode === "loop"; - - if (url.toLowerCase() === "list") { - return AudioHost.getCurrentUrl(area) || ""; - } - - if (action === "off") { - if (hasArea) { - return AudioHost.stop(area); - } - return AudioHost.stopAll(); - } - - if (action === "clear") { - if (hasArea) { - return AudioHost.clear(area); - } - AudioHost.reset(); - return "已全部清除"; - } - - if (action === "on" || (!action && url)) { - return await AudioHost.playUrl(url, loop, area, volume); - } - - if (!url && !action) { - const cur = AudioHost.getCurrentUrl(area); - return cur ? `当前播放(${area}): ${cur}` : "未在播放。用法: /xbaudio [play=on] [mode=loop] [area=primary/secondary] [volume=5] URL | /xbaudio list | /xbaudio play=off (未指定 area 将关闭全部)"; - } - - return "用法: /xbaudio play=off | /xbaudio play=off area=primary/secondary | /xbaudio play=clear | /xbaudio play=clear area=primary/secondary | /xbaudio [play=on] [mode=loop/once] [area=primary/secondary] [volume=1-10] URL | /xbaudio list (默认: play=on mode=loop area=primary volume=5;未指定 area 的 play=off 关闭全部;未指定 area 的 play=clear 清除全部)"; - } catch (e) { - return `错误: ${e.message || e}`; - } - }, - namedArgumentList: [ - SlashCommandNamedArgument.fromProps({ name: "play", description: "on/off/clear 或留空以默认播放", typeList: [ARGUMENT_TYPE.STRING], enumList: ["on", "off", "clear"] }), - SlashCommandNamedArgument.fromProps({ name: "mode", description: "once/loop", typeList: [ARGUMENT_TYPE.STRING], enumList: ["once", "loop"] }), - SlashCommandNamedArgument.fromProps({ name: "area", description: "primary/secondary (play=off 未指定 area 关闭全部)", typeList: [ARGUMENT_TYPE.STRING], enumList: ["primary", "secondary"] }), - SlashCommandNamedArgument.fromProps({ name: "volume", description: "音量 1-10(默认 5)", typeList: [ARGUMENT_TYPE.NUMBER] }), - ], - unnamedArgumentList: [ - SlashCommandArgument.fromProps({ description: "音频URL (http/https) 或 list", typeList: [ARGUMENT_TYPE.STRING] }), - ], - helpString: "播放网络音频。示例: /xbaudio https://files.catbox.moe/0ryoa5.mp3 (默认: play=on mode=loop area=primary volume=5) | /xbaudio area=secondary volume=8 https://files.catbox.moe/0ryoa5.mp3 | /xbaudio list | /xbaudio play=off (未指定 area 关闭全部) | /xbaudio play=off area=primary | /xbaudio play=clear (未指定 area 清除全部)", - }); - SlashCommandParser.addCommandObject(registeredCommand); - if (event_types?.CHAT_CHANGED) { - chatChangedHandler = () => { try { AudioHost.reset(); } catch { } }; - eventSource.on(event_types.CHAT_CHANGED, chatChangedHandler); - } - isRegistered = true; - } catch (e) { - console.error("[LittleWhiteBox][audio] 注册斜杠命令失败", e); - } -} - -function unregisterSlash() { - if (!isRegistered) return; - try { - if (chatChangedHandler && event_types?.CHAT_CHANGED) { - try { eventSource.removeListener(event_types.CHAT_CHANGED, chatChangedHandler); } catch { } - } - chatChangedHandler = null; - try { - const map = SlashCommandParser.commands || {}; - Object.keys(map).forEach((k) => { if (map[k] === registeredCommand) delete map[k]; }); - } catch { } - } finally { - registeredCommand = null; - isRegistered = false; - } -} - -function enableFeature() { - registerSlash(); -} - -function disableFeature() { - try { AudioHost.reset(); } catch { } - unregisterSlash(); -} - -export function initControlAudio() { - try { - try { - const enabled = !!(extension_settings?.LittleWhiteBox?.audio?.enabled ?? true); - if (enabled) enableFeature(); else disableFeature(); - } catch { enableFeature(); } - - const bind = () => { - const cb = document.getElementById('xiaobaix_audio_enabled'); - if (!cb) { setTimeout(bind, 200); return; } - const applyState = () => { - const input = /** @type {HTMLInputElement} */(cb); - const enabled = !!(input && input.checked); - if (enabled) enableFeature(); else disableFeature(); - }; - cb.addEventListener('change', applyState); - applyState(); - }; - bind(); - - // 监听扩展全局开关,关闭时强制停止并清理两个实例 - try { - if (!globalStateChangedHandler) { - globalStateChangedHandler = (e) => { - try { - const enabled = !!(e && e.detail && e.detail.enabled); - if (!enabled) { - try { AudioHost.reset(); } catch { } - unregisterSlash(); - } else { - // 重新根据子开关状态应用 - const audioEnabled = !!(extension_settings?.LittleWhiteBox?.audio?.enabled ?? true); - if (audioEnabled) enableFeature(); else disableFeature(); - } - } catch { } - }; - document.addEventListener('xiaobaixEnabledChanged', globalStateChangedHandler); - } - } catch { } - } catch (e) { - console.error("[LittleWhiteBox][audio] 初始化失败", e); - } -} +"use strict"; + +import { extension_settings } from "../../../../extensions.js"; +import { eventSource, event_types } from "../../../../../script.js"; +import { SlashCommandParser } from "../../../../slash-commands/SlashCommandParser.js"; +import { SlashCommand } from "../../../../slash-commands/SlashCommand.js"; +import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from "../../../../slash-commands/SlashCommandArgument.js"; + +const AudioHost = (() => { + /** @typedef {{ audio: HTMLAudioElement|null, currentUrl: string }} AudioInstance */ + /** @type {Record<'primary'|'secondary', AudioInstance>} */ + const instances = { + primary: { audio: null, currentUrl: "" }, + secondary: { audio: null, currentUrl: "" }, + }; + + /** + * @param {('primary'|'secondary')} area + * @returns {HTMLAudioElement} + */ + function getOrCreate(area) { + const inst = instances[area] || (instances[area] = { audio: null, currentUrl: "" }); + if (!inst.audio) { + inst.audio = new Audio(); + inst.audio.preload = "auto"; + try { inst.audio.crossOrigin = "anonymous"; } catch { } + } + return inst.audio; + } + + /** + * @param {string} url + * @param {boolean} loop + * @param {('primary'|'secondary')} area + * @param {number} volume10 1-10 + */ + async function playUrl(url, loop = false, area = 'primary', volume10 = 5) { + const u = String(url || "").trim(); + if (!/^https?:\/\//i.test(u)) throw new Error("仅支持 http/https 链接"); + const a = getOrCreate(area); + a.loop = !!loop; + + let v = Number(volume10); + if (!Number.isFinite(v)) v = 5; + v = Math.max(1, Math.min(10, v)); + try { a.volume = v / 10; } catch { } + + const inst = instances[area]; + if (inst.currentUrl && u === inst.currentUrl) { + if (a.paused) await a.play(); + return `继续播放: ${u}`; + } + + inst.currentUrl = u; + if (a.src !== u) { + a.src = u; + try { await a.play(); } + catch (e) { throw new Error("播放失败"); } + } else { + try { a.currentTime = 0; await a.play(); } catch { } + } + return `播放: ${u}`; + } + + /** + * @param {('primary'|'secondary')} area + */ + function stop(area = 'primary') { + const inst = instances[area]; + if (inst?.audio) { + try { inst.audio.pause(); } catch { } + } + return "已停止"; + } + + /** + * @param {('primary'|'secondary')} area + */ + function getCurrentUrl(area = 'primary') { + const inst = instances[area]; + return inst?.currentUrl || ""; + } + + function reset() { + for (const key of /** @type {('primary'|'secondary')[]} */(['primary','secondary'])) { + const inst = instances[key]; + if (inst.audio) { + try { inst.audio.pause(); } catch { } + try { inst.audio.removeAttribute('src'); inst.audio.load(); } catch { } + } + inst.currentUrl = ""; + } + } + + function stopAll() { + for (const key of /** @type {('primary'|'secondary')[]} */(['primary','secondary'])) { + const inst = instances[key]; + if (inst?.audio) { + try { inst.audio.pause(); } catch { } + } + } + return "已全部停止"; + } + + /** + * 清除指定实例:停止并移除 src,清空 currentUrl + * @param {('primary'|'secondary')} area + */ + function clear(area = 'primary') { + const inst = instances[area]; + if (inst?.audio) { + try { inst.audio.pause(); } catch { } + try { inst.audio.removeAttribute('src'); inst.audio.load(); } catch { } + } + inst.currentUrl = ""; + return "已清除"; + } + + return { playUrl, stop, stopAll, clear, getCurrentUrl, reset }; +})(); + +let registeredCommand = null; +let chatChangedHandler = null; +let isRegistered = false; +let globalStateChangedHandler = null; + +function registerSlash() { + if (isRegistered) return; + try { + registeredCommand = SlashCommand.fromProps({ + name: "xbaudio", + callback: async (args, value) => { + try { + const action = String(args.play || "").toLowerCase(); + const mode = String(args.mode || "loop").toLowerCase(); + const rawArea = args.area; + const hasArea = typeof rawArea !== 'undefined' && rawArea !== null && String(rawArea).trim() !== ''; + const area = hasArea && String(rawArea).toLowerCase() === 'secondary' ? 'secondary' : 'primary'; + const volumeArg = args.volume; + let volume = Number(volumeArg); + if (!Number.isFinite(volume)) volume = 5; + const url = String(value || "").trim(); + const loop = mode === "loop"; + + if (url.toLowerCase() === "list") { + return AudioHost.getCurrentUrl(area) || ""; + } + + if (action === "off") { + if (hasArea) { + return AudioHost.stop(area); + } + return AudioHost.stopAll(); + } + + if (action === "clear") { + if (hasArea) { + return AudioHost.clear(area); + } + AudioHost.reset(); + return "已全部清除"; + } + + if (action === "on" || (!action && url)) { + return await AudioHost.playUrl(url, loop, area, volume); + } + + if (!url && !action) { + const cur = AudioHost.getCurrentUrl(area); + return cur ? `当前播放(${area}): ${cur}` : "未在播放。用法: /xbaudio [play=on] [mode=loop] [area=primary/secondary] [volume=5] URL | /xbaudio list | /xbaudio play=off (未指定 area 将关闭全部)"; + } + + return "用法: /xbaudio play=off | /xbaudio play=off area=primary/secondary | /xbaudio play=clear | /xbaudio play=clear area=primary/secondary | /xbaudio [play=on] [mode=loop/once] [area=primary/secondary] [volume=1-10] URL | /xbaudio list (默认: play=on mode=loop area=primary volume=5;未指定 area 的 play=off 关闭全部;未指定 area 的 play=clear 清除全部)"; + } catch (e) { + return `错误: ${e.message || e}`; + } + }, + namedArgumentList: [ + SlashCommandNamedArgument.fromProps({ name: "play", description: "on/off/clear 或留空以默认播放", typeList: [ARGUMENT_TYPE.STRING], enumList: ["on", "off", "clear"] }), + SlashCommandNamedArgument.fromProps({ name: "mode", description: "once/loop", typeList: [ARGUMENT_TYPE.STRING], enumList: ["once", "loop"] }), + SlashCommandNamedArgument.fromProps({ name: "area", description: "primary/secondary (play=off 未指定 area 关闭全部)", typeList: [ARGUMENT_TYPE.STRING], enumList: ["primary", "secondary"] }), + SlashCommandNamedArgument.fromProps({ name: "volume", description: "音量 1-10(默认 5)", typeList: [ARGUMENT_TYPE.NUMBER] }), + ], + unnamedArgumentList: [ + SlashCommandArgument.fromProps({ description: "音频URL (http/https) 或 list", typeList: [ARGUMENT_TYPE.STRING] }), + ], + helpString: "播放网络音频。示例: /xbaudio https://files.catbox.moe/0ryoa5.mp3 (默认: play=on mode=loop area=primary volume=5) | /xbaudio area=secondary volume=8 https://files.catbox.moe/0ryoa5.mp3 | /xbaudio list | /xbaudio play=off (未指定 area 关闭全部) | /xbaudio play=off area=primary | /xbaudio play=clear (未指定 area 清除全部)", + }); + SlashCommandParser.addCommandObject(registeredCommand); + if (event_types?.CHAT_CHANGED) { + chatChangedHandler = () => { try { AudioHost.reset(); } catch { } }; + eventSource.on(event_types.CHAT_CHANGED, chatChangedHandler); + } + isRegistered = true; + } catch (e) { + console.error("[LittleWhiteBox][audio] 注册斜杠命令失败", e); + } +} + +function unregisterSlash() { + if (!isRegistered) return; + try { + if (chatChangedHandler && event_types?.CHAT_CHANGED) { + try { eventSource.removeListener(event_types.CHAT_CHANGED, chatChangedHandler); } catch { } + } + chatChangedHandler = null; + try { + const map = SlashCommandParser.commands || {}; + Object.keys(map).forEach((k) => { if (map[k] === registeredCommand) delete map[k]; }); + } catch { } + } finally { + registeredCommand = null; + isRegistered = false; + } +} + +function enableFeature() { + registerSlash(); +} + +function disableFeature() { + try { AudioHost.reset(); } catch { } + unregisterSlash(); +} + +export function initControlAudio() { + try { + try { + const enabled = !!(extension_settings?.LittleWhiteBox?.audio?.enabled ?? true); + if (enabled) enableFeature(); else disableFeature(); + } catch { enableFeature(); } + + const bind = () => { + const cb = document.getElementById('xiaobaix_audio_enabled'); + if (!cb) { setTimeout(bind, 200); return; } + const applyState = () => { + const input = /** @type {HTMLInputElement} */(cb); + const enabled = !!(input && input.checked); + if (enabled) enableFeature(); else disableFeature(); + }; + cb.addEventListener('change', applyState); + applyState(); + }; + bind(); + + // 监听扩展全局开关,关闭时强制停止并清理两个实例 + try { + if (!globalStateChangedHandler) { + globalStateChangedHandler = (e) => { + try { + const enabled = !!(e && e.detail && e.detail.enabled); + if (!enabled) { + try { AudioHost.reset(); } catch { } + unregisterSlash(); + } else { + // 重新根据子开关状态应用 + const audioEnabled = !!(extension_settings?.LittleWhiteBox?.audio?.enabled ?? true); + if (audioEnabled) enableFeature(); else disableFeature(); + } + } catch { } + }; + document.addEventListener('xiaobaixEnabledChanged', globalStateChangedHandler); + } + } catch { } + } catch (e) { + console.error("[LittleWhiteBox][audio] 初始化失败", e); + } +} diff --git a/modules/debug-panel/debug-panel.html b/modules/debug-panel/debug-panel.html index 2c1c06c..a65743c 100644 --- a/modules/debug-panel/debug-panel.html +++ b/modules/debug-panel/debug-panel.html @@ -358,8 +358,11 @@ - \ No newline at end of file + diff --git a/modules/debug-panel/debug-panel.js b/modules/debug-panel/debug-panel.js index 4e9aad5..41341f3 100644 --- a/modules/debug-panel/debug-panel.js +++ b/modules/debug-panel/debug-panel.js @@ -3,6 +3,7 @@ // ═══════════════════════════════════════════════════════════════════════════ import { extensionFolderPath } from "../../core/constants.js"; +import { postToIframe, isTrustedMessage } from "../../core/iframe-messaging.js"; const STORAGE_EXPANDED_KEY = "xiaobaix_debug_panel_pos_v2"; const STORAGE_MINI_KEY = "xiaobaix_debug_panel_minipos_v2"; @@ -455,7 +456,7 @@ async function getDebugSnapshot() { } function postToFrame(msg) { - try { iframeEl?.contentWindow?.postMessage({ source: "LittleWhiteBox-DebugHost", ...msg }, "*"); } catch {} + try { postToIframe(iframeEl, { ...msg }, "LittleWhiteBox-DebugHost"); } catch {} } async function sendSnapshotToFrame() { @@ -488,9 +489,11 @@ async function handleAction(action) { function bindMessageListener() { if (messageListenerBound) return; messageListenerBound = true; + // eslint-disable-next-line no-restricted-syntax window.addEventListener("message", async (e) => { + // Guarded by isTrustedMessage (origin + source). + if (!isTrustedMessage(e, iframeEl, "LittleWhiteBox-DebugFrame")) return; const msg = e?.data; - if (!msg || msg.source !== "LittleWhiteBox-DebugFrame") return; if (msg.type === "FRAME_READY") { frameReady = true; await sendSnapshotToFrame(); } else if (msg.type === "XB_DEBUG_ACTION") await handleAction(msg); else if (msg.type === "CLOSE_PANEL") closeDebugPanel(); @@ -511,7 +514,9 @@ function updateMiniBadge(logs) { const newMax = maxLogId(logs); if (newMax > lastLogId && !isExpanded) { miniBtnEl.classList.remove("flash"); - void miniBtnEl.offsetWidth; + // Force reflow to restart animation. + // eslint-disable-next-line no-unused-expressions + miniBtnEl.offsetWidth; miniBtnEl.classList.add("flash"); } lastLogId = newMax; diff --git a/modules/fourth-wall/fourth-wall.html b/modules/fourth-wall/fourth-wall.html index 5f107a8..1178c9d 100644 --- a/modules/fourth-wall/fourth-wall.html +++ b/modules/fourth-wall/fourth-wall.html @@ -577,11 +577,15 @@ let defaultVoiceKey = 'female_1'; ══════════════════════════════════════════════════════════════════════════════ */ function escapeHtml(text) { - return String(text || '').replace(/&/g, '&').replace(//g, '>').replace(/\n/g, '
'); + return escapeHtmlText(text).replace(/\n/g, '
'); +} + +function escapeHtmlText(text) { + return String(text || '').replace(/[&<>\"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '\"': '"', '\'': ''' }[c])); } function renderThinking(text) { - return String(text || '').replace(/&/g, '&').replace(//g, '>') + return escapeHtmlText(text) .replace(/\*\*([^*]+)\*\*/g, '$1').replace(/^[\\*\\-]\\s+/gm, '• ').replace(/\n/g, '
'); } @@ -606,8 +610,12 @@ function generateUUID() { }); } +const PARENT_ORIGIN = (() => { + try { return new URL(document.referrer).origin; } catch { return window.location.origin; } +})(); + function postToParent(payload) { - window.parent.postMessage({ source: 'LittleWhiteBox-FourthWall', ...payload }, '*'); + window.parent.postMessage({ source: 'LittleWhiteBox-FourthWall', ...payload }, PARENT_ORIGIN); } function getEmotionIcon(emotion) { @@ -856,7 +864,7 @@ function hydrateVoiceSlots(container) { function renderContent(text) { if (!text) return ''; - let html = String(text).replace(/&/g, '&').replace(//g, '>'); + let html = escapeHtmlText(text); html = html.replace(/\[(?:img|图片)\s*:\s*([^\]]+)\]/gi, (_, inner) => { const tags = parseImageToken(inner); @@ -915,7 +923,7 @@ function renderMessages() { const isEditing = editingIndex === idx; const timeStr = formatTimeDisplay(msg.ts); const bubbleContent = isEditing - ? `` + ? `` : renderContent(msg.content); const actions = isEditing ? `
` @@ -1116,7 +1124,9 @@ function regenerate() { 消息处理 ══════════════════════════════════════════════════════════════════════════════ */ +// Guarded by origin/source check. window.addEventListener('message', event => { + if (event.origin !== PARENT_ORIGIN || event.source !== window.parent) return; const data = event.data; if (!data || data.source !== 'LittleWhiteBox') return; @@ -1125,7 +1135,7 @@ window.addEventListener('message', event => { source: 'LittleWhiteBox-FourthWall', type: 'PONG', pingId: data.pingId - }, '*'); + }, PARENT_ORIGIN); return; } @@ -1313,4 +1323,4 @@ document.addEventListener('DOMContentLoaded', async () => { }); - \ No newline at end of file + diff --git a/modules/fourth-wall/fourth-wall.js b/modules/fourth-wall/fourth-wall.js index 617091f..5d286bd 100644 --- a/modules/fourth-wall/fourth-wall.js +++ b/modules/fourth-wall/fourth-wall.js @@ -2,8 +2,7 @@ // 次元壁模块 - 主控制器 // ════════════════════════════════════════════════════════════════════════════ import { extension_settings, getContext, saveMetadataDebounced } from "../../../../../extensions.js"; -import { saveSettingsDebounced, chat_metadata } from "../../../../../../script.js"; -import { executeSlashCommand } from "../../core/slash-command.js"; +import { saveSettingsDebounced, chat_metadata, default_user_avatar, default_avatar } from "../../../../../../script.js"; import { EXT_ID, extensionFolderPath } from "../../core/constants.js"; import { createModuleEvents, event_types } from "../../core/event-manager.js"; import { xbLog } from "../../core/debug-core.js"; @@ -19,6 +18,7 @@ import { DEFAULT_META_PROTOCOL } from "./fw-prompt.js"; import { initMessageEnhancer, cleanupMessageEnhancer } from "./fw-message-enhancer.js"; +import { postToIframe, isTrustedMessage, getTrustedOrigin } from "../../core/iframe-messaging.js"; // ════════════════════════════════════════════════════════════════════════════ // 常量 // ════════════════════════════════════════════════════════════════════════════ @@ -41,7 +41,6 @@ let streamTimerId = null; let floatBtnResizeHandler = null; let suppressFloatBtnClickUntil = 0; let currentLoadedChatId = null; -let isFullscreen = false; let lastCommentaryTime = 0; let commentaryBubbleEl = null; let commentaryBubbleTimer = null; @@ -157,7 +156,7 @@ function getAvatarUrls() { const ch = Array.isArray(ctx.characters) ? ctx.characters[chId] : null; let char = ch?.avatar || (typeof default_avatar !== 'undefined' ? default_avatar : ''); if (char && !/^(data:|blob:|https?:)/i.test(char)) { - char = /[\/]/.test(char) ? char.replace(/^\/+/, '') : `characters/${char}`; + char = String(char).includes('/') ? char.replace(/^\/+/, '') : `characters/${char}`; } return { user: toAbsUrl(user), char: toAbsUrl(char) }; } @@ -209,14 +208,14 @@ function postToFrame(payload) { pendingFrameMessages.push(payload); return; } - iframe.contentWindow.postMessage({ source: 'LittleWhiteBox', ...payload }, '*'); + postToIframe(iframe, payload, 'LittleWhiteBox'); } function flushPendingMessages() { if (!frameReady) return; const iframe = document.getElementById('xiaobaix-fourth-wall-iframe'); if (!iframe?.contentWindow) return; - pendingFrameMessages.forEach(p => iframe.contentWindow.postMessage({ source: 'LittleWhiteBox', ...p }, '*')); + pendingFrameMessages.forEach(p => postToIframe(iframe, p, 'LittleWhiteBox')); pendingFrameMessages = []; } @@ -268,7 +267,7 @@ function checkIframeHealth() { recoverIframe('contentWindow 不存在'); return; } - win.postMessage({ source: 'LittleWhiteBox', type: 'PING', pingId }, '*'); + win.postMessage({ source: 'LittleWhiteBox', type: 'PING', pingId }, getTrustedOrigin()); } catch (e) { recoverIframe('无法访问 iframe: ' + e.message); return; @@ -314,8 +313,9 @@ function recoverIframe(reason) { // ════════════════════════════════════════════════════════════════════════════ function handleFrameMessage(event) { + const iframe = document.getElementById('xiaobaix-fourth-wall-iframe'); + if (!isTrustedMessage(event, iframe, 'LittleWhiteBox-FourthWall')) return; const data = event.data; - if (!data || data.source !== 'LittleWhiteBox-FourthWall') return; const store = getFWStore(); const settings = getSettings(); @@ -463,11 +463,22 @@ async function startGeneration(data) { promptTemplates: getSettings().fourthWallPromptTemplates }); - const top64 = b64UrlEncode(`user={${msg1}};assistant={${msg2}};user={${msg3}};assistant={${msg4}}`); - const nonstreamArg = data.settings.stream ? '' : ' nonstream=true'; - const cmd = `/xbgenraw id=${STREAM_SESSION_ID} top64="${top64}"${nonstreamArg} ""`; - - await executeSlashCommand(cmd); + const gen = window.xiaobaixStreamingGeneration; + if (!gen?.xbgenrawCommand) throw new Error('xbgenraw 模块不可用'); + + const topMessages = [ + { role: 'user', content: msg1 }, + { role: 'assistant', content: msg2 }, + { role: 'user', content: msg3 }, + ]; + + await gen.xbgenrawCommand({ + id: STREAM_SESSION_ID, + top64: b64UrlEncode(JSON.stringify(topMessages)), + bottomassistant: msg4, + nonstream: data.settings.stream ? 'false' : 'true', + as: 'user', + }, ''); if (data.settings.stream) { startStreamingPoll(); @@ -620,11 +631,24 @@ async function generateCommentary(targetText, type) { if (!built) return null; const { msg1, msg2, msg3, msg4 } = built; - const top64 = b64UrlEncode(`user={${msg1}};assistant={${msg2}};user={${msg3}};assistant={${msg4}}`); + + const gen = window.xiaobaixStreamingGeneration; + if (!gen?.xbgenrawCommand) return null; + + const topMessages = [ + { role: 'user', content: msg1 }, + { role: 'assistant', content: msg2 }, + { role: 'user', content: msg3 }, + ]; try { - const cmd = `/xbgenraw id=xb8 nonstream=true top64="${top64}" ""`; - const result = await executeSlashCommand(cmd); + const result = await gen.xbgenrawCommand({ + id: 'xb8', + top64: b64UrlEncode(JSON.stringify(topMessages)), + bottomassistant: msg4, + nonstream: 'true', + as: 'user', + }, ''); return extractMsg(result) || null; } catch { return null; @@ -771,14 +795,14 @@ function createOverlay() { $overlay.on('click', '.fw-backdrop', hideOverlay); document.body.appendChild($overlay[0]); + // Guarded by isTrustedMessage (origin + source). + // eslint-disable-next-line no-restricted-syntax window.addEventListener('message', handleFrameMessage); document.addEventListener('fullscreenchange', () => { if (!document.fullscreenElement) { - isFullscreen = false; postToFrame({ type: 'FULLSCREEN_STATE', isFullscreen: false }); } else { - isFullscreen = true; postToFrame({ type: 'FULLSCREEN_STATE', isFullscreen: true }); } }); @@ -809,7 +833,6 @@ function showOverlay() { function hideOverlay() { $('#xiaobaix-fourth-wall-overlay').hide(); if (document.fullscreenElement) document.exitFullscreen().catch(() => {}); - isFullscreen = false; // ═══════════════════════════ 新增:移除可见性监听 ═══════════════════════════ if (visibilityHandler) { @@ -826,12 +849,10 @@ function toggleFullscreen() { if (document.fullscreenElement) { document.exitFullscreen().then(() => { - isFullscreen = false; postToFrame({ type: 'FULLSCREEN_STATE', isFullscreen: false }); }).catch(() => {}); } else if (overlay.requestFullscreen) { overlay.requestFullscreen().then(() => { - isFullscreen = true; postToFrame({ type: 'FULLSCREEN_STATE', isFullscreen: true }); }).catch(() => {}); } diff --git a/modules/fourth-wall/fw-message-enhancer.js b/modules/fourth-wall/fw-message-enhancer.js index 86acf80..c3d04f9 100644 --- a/modules/fourth-wall/fw-message-enhancer.js +++ b/modules/fourth-wall/fw-message-enhancer.js @@ -258,6 +258,8 @@ function injectStyles() { function enhanceMessageContent(container) { if (!container) return; + // Rewrites already-rendered message HTML; no new HTML source is introduced here. + // eslint-disable-next-line no-unsanitized/property const html = container.innerHTML; let enhanced = html; let hasChanges = false; @@ -283,7 +285,11 @@ function enhanceMessageContent(container) { return createVoiceBubbleHTML(txt, ''); }); - if (hasChanges) container.innerHTML = enhanced; + if (hasChanges) { + // Replaces existing message HTML with enhanced tokens only. + // eslint-disable-next-line no-unsanitized/property + container.innerHTML = enhanced; + } hydrateImageSlots(container); hydrateVoiceSlots(container); @@ -317,6 +323,8 @@ function hydrateImageSlots(container) { slot.dataset.observed = '1'; if (!slot.dataset.loaded && !slot.dataset.loading && !slot.querySelector('img')) { + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property slot.innerHTML = `
滚动加载
`; } @@ -325,18 +333,26 @@ function hydrateImageSlots(container) { } async function loadImage(slot, tags) { + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property slot.innerHTML = `
检查缓存...
`; try { const base64 = await generateImage(tags, (status, position, delay) => { switch (status) { case 'queued': + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property slot.innerHTML = `
排队中 #${position}
`; break; case 'generating': + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property slot.innerHTML = `
生成中${position > 0 ? ` (${position} 排队)` : ''}...
`; break; case 'waiting': + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property slot.innerHTML = `
排队中 #${position} (${delay}s)
`; break; } @@ -349,12 +365,16 @@ async function loadImage(slot, tags) { slot.dataset.loading = ''; if (err.message === '队列已清空') { + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property slot.innerHTML = `
滚动加载
`; slot.dataset.loading = ''; slot.dataset.observed = ''; return; } + // Template-only UI markup with escaped error text. + // eslint-disable-next-line no-unsanitized/property slot.innerHTML = `
${escapeHtml(err?.message || '失败')}
`; bindRetryButton(slot); } @@ -369,12 +389,16 @@ function renderImage(slot, base64, fromCache) { img.className = 'xb-generated-img'; img.onclick = () => window.open(img.src, '_blank'); + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property slot.innerHTML = ''; slot.appendChild(img); if (fromCache) { const badge = document.createElement('span'); badge.className = 'xb-img-badge'; + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property badge.innerHTML = ''; slot.appendChild(badge); } diff --git a/modules/iframe-renderer.js b/modules/iframe-renderer.js index 175f250..63f49a5 100644 --- a/modules/iframe-renderer.js +++ b/modules/iframe-renderer.js @@ -1,11 +1,12 @@ import { extension_settings, getContext } from "../../../../extensions.js"; import { createModuleEvents, event_types } from "../core/event-manager.js"; -import { EXT_ID, extensionFolderPath } from "../core/constants.js"; +import { EXT_ID } from "../core/constants.js"; import { xbLog, CacheRegistry } from "../core/debug-core.js"; import { replaceXbGetVarInString } from "./variables/var-commands.js"; import { executeSlashCommand } from "../core/slash-command.js"; import { default_user_avatar, default_avatar } from "../../../../../script.js"; import { getIframeBaseScript, getWrapperScript } from "../core/wrapper-inline.js"; +import { postToIframe, getIframeTargetOrigin, getTrustedOrigin } from "../core/iframe-messaging.js"; const MODULE_ID = 'iframeRenderer'; const events = createModuleEvents(MODULE_ID); @@ -20,7 +21,6 @@ const BLOB_CACHE_LIMIT = 32; let lastApplyTs = 0; let pendingHeight = null; let pendingRec = null; -let hideStyleInjected = false; CacheRegistry.register(MODULE_ID, { name: 'Blob URL 缓存', @@ -46,7 +46,6 @@ function ensureHideCodeStyle(enable) { const old = document.getElementById(id); if (!enable) { old?.remove(); - hideStyleInjected = false; return; } if (old) return; @@ -57,7 +56,6 @@ function ensureHideCodeStyle(enable) { .xiaobaix-active .mes_text pre.xb-show { display: block !important; } `; document.head.appendChild(hideCodeStyle); - hideStyleInjected = true; } function setActiveClass(enable) { @@ -253,7 +251,7 @@ function resolveAvatarUrls() { const ch = Array.isArray(ctx.characters) ? ctx.characters[chId] : null; let char = ch?.avatar || default_avatar; if (char && !/^(data:|blob:|https?:)/i.test(char)) { - char = /[\/]/.test(char) ? char.replace(/^\/+/, '') : `characters/${char}`; + char = String(char).includes('/') ? char.replace(/^\/+/, '') : `characters/${char}`; } return { user: toAbsUrl(user), char: toAbsUrl(char) }; } @@ -310,28 +308,30 @@ function handleIframeMessage(event) { } if (data && data.type === 'runCommand') { + const replyOrigin = (typeof event.origin === 'string' && event.origin) ? event.origin : getTrustedOrigin(); executeSlashCommand(data.command) .then(result => event.source.postMessage({ source: 'xiaobaix-host', type: 'commandResult', id: data.id, result - }, '*')) + }, replyOrigin)) .catch(err => event.source.postMessage({ source: 'xiaobaix-host', type: 'commandError', id: data.id, error: err.message || String(err) - }, '*')); + }, replyOrigin)); return; } if (data && data.type === 'getAvatars') { + const replyOrigin = (typeof event.origin === 'string' && event.origin) ? event.origin : getTrustedOrigin(); try { const urls = resolveAvatarUrls(); - event.source?.postMessage({ source: 'xiaobaix-host', type: 'avatars', urls }, '*'); + event.source?.postMessage({ source: 'xiaobaix-host', type: 'avatars', urls }, replyOrigin); } catch (e) { - event.source?.postMessage({ source: 'xiaobaix-host', type: 'avatars', urls: { user: '', char: '' } }, '*'); + event.source?.postMessage({ source: 'xiaobaix-host', type: 'avatars', urls: { user: '', char: '' } }, replyOrigin); } return; } @@ -383,7 +383,10 @@ export function renderHtmlInIframe(htmlContent, container, preElement) { preElement.style.display = 'none'; registerIframeMapping(iframe, wrapper); - try { iframe.contentWindow?.postMessage({ type: 'probe' }, '*'); } catch (e) {} + try { + const targetOrigin = getIframeTargetOrigin(iframe); + postToIframe(iframe, { type: 'probe' }, null, targetOrigin); + } catch (e) {} preElement.dataset.xbFinal = 'true'; preElement.dataset.xbHash = originalHash; @@ -667,6 +670,7 @@ export function initRenderer() { }); if (!messageListenerBound) { + // eslint-disable-next-line no-restricted-syntax -- message bridge for iframe renderers. window.addEventListener('message', handleIframeMessage); messageListenerBound = true; } diff --git a/modules/immersive-mode.js b/modules/immersive-mode.js index 11ef879..a293c07 100644 --- a/modules/immersive-mode.js +++ b/modules/immersive-mode.js @@ -657,18 +657,6 @@ function cleanup() { }; } -function attachResizeObserverTo(el) { - if (!el) return; - - if (!resizeObs) { - resizeObs = new ResizeObserver(() => { }); - } - - if (resizeObservedEl) detachResizeObserver(); - resizeObservedEl = el; - resizeObs.observe(el); -} - function detachResizeObserver() { if (resizeObs && resizeObservedEl) { resizeObs.unobserve(resizeObservedEl); diff --git a/modules/message-preview.js b/modules/message-preview.js index 128a394..c7ab633 100644 --- a/modules/message-preview.js +++ b/modules/message-preview.js @@ -1,650 +1,669 @@ -import { extension_settings, getContext } from "../../../../extensions.js"; -import { saveSettingsDebounced, eventSource, event_types } from "../../../../../script.js"; -import { EXT_ID } from "../core/constants.js"; - -const C = { MAX_HISTORY: 10, CHECK: 200, DEBOUNCE: 300, CLEAN: 300000, TARGET: "/api/backends/chat-completions/generate", TIMEOUT: 30, ASSOC_DELAY: 1000, REQ_WINDOW: 30000 }; -const S = { active: false, isPreview: false, isLong: false, isHistoryUiBound: false, previewData: null, previewIds: new Set(), interceptedIds: [], history: [], listeners: [], resolve: null, reject: null, sendBtnWasDisabled: false, longPressTimer: null, longPressDelay: 1000, chatLenBefore: 0, restoreLong: null, cleanTimer: null, previewAbort: null, tailAPI: null, genEndedOff: null, cleanupFallback: null, pendingPurge: false }; - -const $q = (sel) => $(sel); -const ON = (e, c) => eventSource.on(e, c); -const OFF = (e, c) => eventSource.removeListener(e, c); -const now = () => Date.now(); -const geEnabled = () => { try { return ("isXiaobaixEnabled" in window) ? !!window.isXiaobaixEnabled : true; } catch { return true; } }; -const debounce = (fn, w) => { let t; return (...a) => { clearTimeout(t); t = setTimeout(() => fn(...a), w); }; }; -const safeJson = (t) => { try { return JSON.parse(t); } catch { return null; } }; - -const readText = async (b) => { try { if (!b) return ""; if (typeof b === "string") return b; if (b instanceof Blob) return await b.text(); if (b instanceof URLSearchParams) return b.toString(); if (typeof b === "object" && typeof b.text === "function") return await b.text(); } catch { } return ""; }; - -function isSafeBody(body) { if (!body) return true; return (typeof body === "string" || body instanceof Blob || body instanceof URLSearchParams || body instanceof ArrayBuffer || ArrayBuffer.isView(body) || (typeof FormData !== "undefined" && body instanceof FormData)); } - -async function safeReadBodyFromInput(input, options) { try { if (input instanceof Request) return await readText(input.clone()); const body = options?.body; if (!isSafeBody(body)) return ""; return await readText(body); } catch { return ""; } } - -const isGen = (u) => String(u || "").includes(C.TARGET); -const isTarget = async (input, opt = {}) => { try { const url = input instanceof Request ? input.url : input; if (!isGen(url)) return false; const text = await safeReadBodyFromInput(input, opt); return text ? text.includes('"messages"') : true; } catch { return input instanceof Request ? isGen(input.url) : isGen(input); } }; -const getSettings = () => { const d = extension_settings[EXT_ID] || (extension_settings[EXT_ID] = {}); d.preview = d.preview || { enabled: false, timeoutSeconds: C.TIMEOUT }; d.recorded = d.recorded || { enabled: true }; d.preview.timeoutSeconds = C.TIMEOUT; return d; }; - -function injectPreviewModalStyles() { - if (document.getElementById('message-preview-modal-styles')) return; - const style = document.createElement('style'); - style.id = 'message-preview-modal-styles'; - style.textContent = ` - .mp-overlay{position:fixed;inset:0;background:none;z-index:9999;display:flex;align-items:center;justify-content:center;pointer-events:none} - .mp-modal{ - width:clamp(360px,55vw,860px); - max-width:95vw; - background:var(--SmartThemeBlurTintColor); - border:2px solid var(--SmartThemeBorderColor); - border-radius:10px; - box-shadow:0 8px 16px var(--SmartThemeShadowColor); - pointer-events:auto; - display:flex; - flex-direction:column; - height:80vh; - max-height:calc(100vh - 60px); - resize:both; - overflow:hidden; - } - .mp-header{display:flex;justify-content:space-between;padding:10px 14px;border-bottom:1px solid var(--SmartThemeBorderColor);font-weight:600;cursor:move;flex-shrink:0} - .mp-body{height:60vh;overflow:auto;padding:10px;flex:1;min-height:160px} - .mp-footer{display:flex;gap:8px;justify-content:flex-end;padding:12px 14px;border-top:1px solid var(--SmartThemeBorderColor);flex-shrink:0} - .mp-close{cursor:pointer} - .mp-btn{cursor:pointer;border:1px solid var(--SmartThemeBorderColor);background:var(--SmartThemeBlurTintColor);padding:6px 10px;border-radius:6px} - .mp-search-input{padding:4px 8px;border:1px solid var(--SmartThemeBorderColor);border-radius:4px;background:var(--SmartThemeShadowColor);color:inherit;font-size:12px;width:120px} - .mp-search-btn{padding:4px 6px;font-size:12px;min-width:24px;text-align:center} - .mp-search-info{font-size:12px;opacity:.8;white-space:nowrap} - .message-preview-container{height:100%} - .message-preview-content-box{height:100%;overflow:auto} - .mp-highlight{background-color:yellow;color:black;padding:1px 2px;border-radius:2px} - .mp-highlight.current{background-color:orange;font-weight:bold} - @media (max-width:999px){ - .mp-overlay{position:absolute;inset:0;align-items:flex-start} - .mp-modal{width:100%;max-width:100%;max-height:100%;margin:0;border-radius:10px 10px 0 0;height:100vh;resize:none} - .mp-header{padding:8px 14px} - .mp-body{padding:8px} - .mp-footer{padding:8px 14px;flex-wrap:wrap;gap:6px} - .mp-search-input{width:150px} - } - `; - document.head.appendChild(style); -} - -function setupModalDrag(modal, overlay, header) { - modal.style.position = 'absolute'; - modal.style.left = '50%'; - modal.style.top = '50%'; - modal.style.transform = 'translate(-50%, -50%)'; - - let dragging = false, sx = 0, sy = 0, sl = 0, st = 0; - - function onDown(e) { - if (!(e instanceof PointerEvent) || e.button !== 0) return; - dragging = true; - const overlayRect = overlay.getBoundingClientRect(); - const rect = modal.getBoundingClientRect(); - modal.style.left = (rect.left - overlayRect.left) + 'px'; - modal.style.top = (rect.top - overlayRect.top) + 'px'; - modal.style.transform = ''; - sx = e.clientX; sy = e.clientY; - sl = parseFloat(modal.style.left) || 0; - st = parseFloat(modal.style.top) || 0; - window.addEventListener('pointermove', onMove, { passive: true }); - window.addEventListener('pointerup', onUp, { once: true }); - e.preventDefault(); - } - - function onMove(e) { - if (!dragging) return; - const dx = e.clientX - sx, dy = e.clientY - sy; - let nl = sl + dx, nt = st + dy; - const maxLeft = (overlay.clientWidth || overlay.getBoundingClientRect().width) - modal.offsetWidth; - const maxTop = (overlay.clientHeight || overlay.getBoundingClientRect().height) - modal.offsetHeight; - nl = Math.max(0, Math.min(maxLeft, nl)); - nt = Math.max(0, Math.min(maxTop, nt)); - modal.style.left = nl + 'px'; - modal.style.top = nt + 'px'; - } - - function onUp() { - dragging = false; - window.removeEventListener('pointermove', onMove); - } - - header.addEventListener('pointerdown', onDown); -} - -function createMovableModal(title, content) { - injectPreviewModalStyles(); - const overlay = document.createElement('div'); - overlay.className = 'mp-overlay'; - const modal = document.createElement('div'); - modal.className = 'mp-modal'; - const header = document.createElement('div'); - header.className = 'mp-header'; - header.innerHTML = `${title}`; - const body = document.createElement('div'); - body.className = 'mp-body'; - body.innerHTML = content; - const footer = document.createElement('div'); - footer.className = 'mp-footer'; - footer.innerHTML = ` - - - - - - - - `; - modal.appendChild(header); - modal.appendChild(body); - modal.appendChild(footer); - overlay.appendChild(modal); - setupModalDrag(modal, overlay, header); - - let searchResults = []; - let currentIndex = -1; - const searchInput = footer.querySelector('.mp-search-input'); - const searchInfo = footer.querySelector('#mp-search-info'); - const prevBtn = footer.querySelector('#mp-search-prev'); - const nextBtn = footer.querySelector('#mp-search-next'); - - function clearHighlights() { body.querySelectorAll('.mp-highlight').forEach(el => { el.outerHTML = el.innerHTML; }); } - function performSearch(query) { - clearHighlights(); - searchResults = []; - currentIndex = -1; - if (!query.trim()) { searchInfo.textContent = ''; return; } - const walker = document.createTreeWalker(body, NodeFilter.SHOW_TEXT, null, false); - const nodes = []; - let node; - while (node = walker.nextNode()) { nodes.push(node); } - const regex = new RegExp(query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'); - nodes.forEach(textNode => { - const text = textNode.textContent; - if (!text || !regex.test(text)) return; - let html = text; - let offset = 0; - regex.lastIndex = 0; - const matches = [...text.matchAll(regex)]; - matches.forEach((m) => { - const start = m.index + offset; - const end = start + m[0].length; - const before = html.slice(0, start); - const mid = html.slice(start, end); - const after = html.slice(end); - const span = `${mid}`; - html = before + span + after; - offset += span.length - m[0].length; - searchResults.push({}); - }); - const parent = textNode.parentElement; - parent.innerHTML = parent.innerHTML.replace(text, html); - }); - updateSearchInfo(); - if (searchResults.length > 0) { currentIndex = 0; highlightCurrent(); } - } - function updateSearchInfo() { if (!searchResults.length) searchInfo.textContent = searchInput.value.trim() ? '无结果' : ''; else searchInfo.textContent = `${currentIndex + 1}/${searchResults.length}`; } - function highlightCurrent() { - body.querySelectorAll('.mp-highlight.current').forEach(el => el.classList.remove('current')); - if (currentIndex >= 0 && currentIndex < searchResults.length) { - const el = body.querySelector(`.mp-highlight[data-search-index="${currentIndex}"]`); - if (el) { el.classList.add('current'); el.scrollIntoView({ behavior: 'smooth', block: 'center' }); } - } - } - function navigateSearch(direction) { - if (!searchResults.length) return; - if (direction === 'next') currentIndex = (currentIndex + 1) % searchResults.length; - else currentIndex = currentIndex <= 0 ? searchResults.length - 1 : currentIndex - 1; - updateSearchInfo(); - highlightCurrent(); - } - let searchTimeout; - searchInput.addEventListener('input', (e) => { clearTimeout(searchTimeout); searchTimeout = setTimeout(() => performSearch(e.target.value), 250); }); - searchInput.addEventListener('keydown', (e) => { if (e.key === 'Enter') { e.preventDefault(); if (e.shiftKey) navigateSearch('prev'); else navigateSearch('next'); } else if (e.key === 'Escape') { searchInput.value = ''; performSearch(''); } }); - prevBtn.addEventListener('click', () => navigateSearch('prev')); - nextBtn.addEventListener('click', () => navigateSearch('next')); - footer.querySelector('#mp-focus-search')?.addEventListener('click', () => { searchInput.focus(); if (searchInput.value) navigateSearch('next'); }); - - const close = () => overlay.remove(); - header.querySelector('.mp-close').addEventListener('click', close); - footer.querySelector('#mp-close').addEventListener('click', close); - footer.querySelector('#mp-toggle-format').addEventListener('click', (e) => { - const box = body.querySelector(".message-preview-content-box"); - const f = box?.querySelector(".mp-state-formatted"); - const r = box?.querySelector(".mp-state-raw"); - if (!(f && r)) return; - const showRaw = r.style.display === "none"; - r.style.display = showRaw ? "block" : "none"; - f.style.display = showRaw ? "none" : "block"; - e.currentTarget.textContent = showRaw ? "切换整理格式" : "切换原始格式"; - searchInput.value = ""; - clearHighlights(); - searchInfo.textContent = ""; - searchResults = []; - currentIndex = -1; - }); - - document.body.appendChild(overlay); - return { overlay, modal, body, close }; -} - -const MIRROR = { MERGE: "merge", MERGE_TOOLS: "merge_tools", SEMI: "semi", SEMI_TOOLS: "semi_tools", STRICT: "strict", STRICT_TOOLS: "strict_tools", SINGLE: "single" }; -const roleMap = { system: { label: "SYSTEM:", color: "#F7E3DA" }, user: { label: "USER:", color: "#F0ADA7" }, assistant: { label: "ASSISTANT:", color: "#6BB2CC" } }; -const colorXml = (t) => (typeof t === "string" ? t.replace(/<([^>]+)>/g, '<$1>') : t); -const getNames = (req) => { const n = { charName: String(req?.char_name || ""), userName: String(req?.user_name || ""), groupNames: Array.isArray(req?.group_names) ? req.group_names.map(String) : [] }; n.startsWithGroupName = (m) => n.groupNames.some((g) => String(m || "").startsWith(`${g}: `)); return n; }; -const toText = (m) => { const c = m?.content; if (typeof c === "string") return c; if (Array.isArray(c)) return c.map((p) => p?.type === "text" ? String(p.text || "") : p?.type === "image_url" ? "[image]" : p?.type === "video_url" ? "[video]" : typeof p === "string" ? p : (typeof p?.content === "string" ? p.content : "")).filter(Boolean).join("\n\n"); return String(c || ""); }; -const applyName = (m, n) => { const { role, name } = m; let t = toText(m); if (role === "system" && name === "example_assistant") { if (n.charName && !t.startsWith(`${n.charName}: `) && !n.startsWithGroupName(t)) t = `${n.charName}: ${t}`; } else if (role === "system" && name === "example_user") { if (n.userName && !t.startsWith(`${n.userName}: `)) t = `${n.userName}: ${t}`; } else if (name && role !== "system" && !t.startsWith(`${name}: `)) t = `${name}: ${t}`; return { ...m, content: t, name: undefined }; }; -function mergeMessages(messages, names, { strict = false, placeholders = false, single = false, tools = false } = {}) { - if (!Array.isArray(messages)) return []; - let mapped = messages.map((m) => applyName({ ...m }, names)).map((x) => { const m = { ...x }; if (!tools) { if (m.role === "tool") m.role = "user"; delete m.tool_calls; delete m.tool_call_id; } if (single) { if (m.role === "assistant") { const t = String(m.content || ""); if (names.charName && !t.startsWith(`${names.charName}: `) && !names.startsWithGroupName(t)) m.content = `${names.charName}: ${t}`; } if (m.role === "user") { const t = String(m.content || ""); if (names.userName && !t.startsWith(`${names.userName}: `)) m.content = `${names.userName}: ${t}`; } m.role = "user"; } return m; }); - const squash = (arr) => { const out = []; for (const m of arr) { if (out.length && out[out.length - 1].role === m.role && String(m.content || "").length && m.role !== "tool") out[out.length - 1].content += `\n\n${m.content}`; else out.push(m); } return out; }; - let sq = squash(mapped); - if (strict) { for (let i = 0; i < sq.length; i++) if (i > 0 && sq[i].role === "system") sq[i].role = "user"; if (placeholders) { if (!sq.length) sq.push({ role: "user", content: "[Start a new chat]" }); else if (sq[0].role === "system" && (sq.length === 1 || sq[1].role !== "user")) sq.splice(1, 0, { role: "user", content: "[Start a new chat]" }); else if (sq[0].role !== "system" && sq[0].role !== "user") sq.unshift({ role: "user", content: "[Start a new chat]" }); } return squash(sq); } - if (!sq.length) sq.push({ role: "user", content: "[Start a new chat]" }); - return sq; -} -function mirror(requestData) { - try { - let type = String(requestData?.custom_prompt_post_processing || "").toLowerCase(); - const source = String(requestData?.chat_completion_source || "").toLowerCase(); - if (source === "perplexity") type = MIRROR.STRICT; - const names = getNames(requestData || {}), src = Array.isArray(requestData?.messages) ? JSON.parse(JSON.stringify(requestData.messages)) : []; - const mk = (o) => mergeMessages(src, names, o); - switch (type) { - case MIRROR.MERGE: return mk({ strict: false }); - case MIRROR.MERGE_TOOLS: return mk({ strict: false, tools: true }); - case MIRROR.SEMI: return mk({ strict: true }); - case MIRROR.SEMI_TOOLS: return mk({ strict: true, tools: true }); - case MIRROR.STRICT: return mk({ strict: true, placeholders: true }); - case MIRROR.STRICT_TOOLS: return mk({ strict: true, placeholders: true, tools: true }); - case MIRROR.SINGLE: return mk({ strict: true, single: true }); - default: return src; - } - } catch { return Array.isArray(requestData?.messages) ? requestData.messages : []; } -} -const finalMsgs = (d) => { try { if (d?.requestData?.messages) return mirror(d.requestData); if (Array.isArray(d?.messages)) return d.messages; return []; } catch { return Array.isArray(d?.messages) ? d.messages : []; } }; -const formatPreview = (d) => { - const msgs = finalMsgs(d); - let out = `↓酒馆日志↓(${msgs.length})\n${"-".repeat(30)}\n`; - msgs.forEach((m, i) => { - const txt = m.content || ""; - const rm = roleMap[m.role] || { label: `${String(m.role || "").toUpperCase()}:`, color: "#FFF" }; - out += `
${rm.label}
`; - out += /<[^>]+>/g.test(txt) ? `
${colorXml(txt)}
` : `
${txt}
`; - }); - return out; -}; -const stripTop = (o) => { try { if (!o || typeof o !== "object") return o; if (Array.isArray(o)) return o; const messages = Array.isArray(o.messages) ? JSON.parse(JSON.stringify(o.messages)) : undefined; return typeof messages !== "undefined" ? { messages } : {}; } catch { return {}; } }; -const formatRaw = (d) => { try { const hasReq = Array.isArray(d?.requestData?.messages), hasMsgs = !hasReq && Array.isArray(d?.messages); let obj; if (hasReq) { const req = JSON.parse(JSON.stringify(d.requestData)); try { req.messages = mirror(req); } catch { } obj = req; } else if (hasMsgs) { const fake = { ...(d || {}), messages: d.messages }; let mm = null; try { mm = mirror(fake); } catch { } obj = { ...(d || {}), messages: mm || d.messages }; } else obj = d?.requestData ?? d; obj = stripTop(obj); return colorXml(JSON.stringify(obj, null, 2)); } catch { try { return colorXml(String(d)); } catch { return ""; } } }; -const buildPreviewHtml = (d) => { const formatted = formatPreview(d), raw = formatRaw(d); return `
${formatted}
`; }; -const openPopup = async (html, title) => { createMovableModal(title, html); }; -const displayPreview = async (d) => { try { await openPopup(buildPreviewHtml(d), "消息拦截"); } catch { toastr.error("显示拦截失败"); } }; - -const pushHistory = (r) => { S.history.unshift(r); if (S.history.length > C.MAX_HISTORY) S.history.length = C.MAX_HISTORY; }; -const extractUser = (ms) => { if (!Array.isArray(ms)) return ""; for (let i = ms.length - 1; i >= 0; i--) if (ms[i]?.role === "user") return ms[i].content || ""; return ""; }; - -async function recordReal(input, options) { - try { - const url = input instanceof Request ? input.url : input; - const body = await safeReadBodyFromInput(input, options); - if (!body) return; - const data = safeJson(body) || {}, ctx = getContext(); - pushHistory({ url, method: options?.method || (input instanceof Request ? input.method : "POST"), requestData: data, messages: data.messages || [], model: data.model || "Unknown", timestamp: now(), messageId: ctx.chat?.length || 0, characterName: ctx.characters?.[ctx.characterId]?.name || "Unknown", userInput: extractUser(data.messages || []), isRealRequest: true }); - setTimeout(() => { if (S.history[0] && !S.history[0].associatedMessageId) S.history[0].associatedMessageId = ctx.chat?.length || 0; }, C.ASSOC_DELAY); - } catch { } -} - -const findRec = (id) => { - if (!S.history.length) return null; - const preds = [(r) => r.associatedMessageId === id, (r) => r.messageId === id, (r) => r.messageId === id - 1, (r) => Math.abs(r.messageId - id) <= 1]; - for (const p of preds) { const m = S.history.find(p); if (m) return m; } - const cs = S.history.filter((r) => r.messageId <= id + 2); - return cs.length ? cs.sort((a, b) => b.messageId - a.messageId)[0] : S.history[0]; -}; - -// Improved purgePreviewArtifacts - follows SillyTavern's batch delete pattern -async function purgePreviewArtifacts() { - try { - if (!S.pendingPurge) return; - S.pendingPurge = false; - const ctx = getContext(); - const chat = Array.isArray(ctx.chat) ? ctx.chat : []; - const start = Math.max(0, Number(S.chatLenBefore) || 0); - if (start >= chat.length) return; - - // 1. Remove DOM elements (following SillyTavern's pattern from #dialogue_del_mes_ok) - const $chat = $('#chat'); - $chat.find(`.mes[mesid="${start}"]`).nextAll('.mes').addBack().remove(); - - // 2. Truncate chat array - chat.length = start; - - // 3. Update last_mes class - $('#chat .mes').removeClass('last_mes'); - $('#chat .mes').last().addClass('last_mes'); - - // 4. Save chat and emit MESSAGE_DELETED event (critical for other plugins) - ctx.saveChat?.(); - await eventSource.emit(event_types.MESSAGE_DELETED, start); - } catch (e) { - console.error('[message-preview] purgePreviewArtifacts error', e); - } -} - - - -function oneShotOnLast(ev, handler) { - const wrapped = (...args) => { - try { handler(...args); } finally { off(); } - }; - let off = () => { }; - if (typeof eventSource.makeLast === "function") { - eventSource.makeLast(ev, wrapped); - off = () => { - try { eventSource.removeListener?.(ev, wrapped); } catch { } - try { eventSource.off?.(ev, wrapped); } catch { } - }; - } else if (S.tailAPI?.onLast) { - const disposer = S.tailAPI.onLast(ev, wrapped); - off = () => { try { disposer?.(); } catch { } }; - } else { - eventSource.on(ev, wrapped); - off = () => { try { eventSource.removeListener?.(ev, wrapped); } catch { } }; - } - return off; -} - -function installEventSourceTail(es) { - if (!es || es.__lw_tailInstalled) return es?.__lw_tailAPI || null; - const SYM = { MW_STACK: Symbol.for("lwbox.es.emitMiddlewareStack"), BASE: Symbol.for("lwbox.es.emitBase"), ORIG_DESC: Symbol.for("lwbox.es.emit.origDesc"), COMPOSED: Symbol.for("lwbox.es.emit.composed"), ID: Symbol.for("lwbox.middleware.identity") }; - const getFnFromDesc = (d) => { try { if (typeof d?.value === "function") return d.value; if (typeof d?.get === "function") { const v = d.get.call(es); if (typeof v === "function") return v; } } catch { } return es.emit?.bind?.(es) || es.emit; }; - const compose = (base, stack) => stack.reduce((acc, mw) => mw(acc), base); - const tails = new Map(); - const addTail = (ev, fn) => { if (typeof fn !== "function") return () => { }; const arr = tails.get(ev) || []; arr.push(fn); tails.set(ev, arr); return () => { const a = tails.get(ev); if (!a) return; const i = a.indexOf(fn); if (i >= 0) a.splice(i, 1); }; }; - const runTails = (ev, args) => { const arr = tails.get(ev); if (!arr?.length) return; for (const h of arr.slice()) { try { h(...args); } catch (e) { } } }; - const makeTailMw = () => { const mw = (next) => function patchedEmit(ev, ...args) { let r; try { r = next.call(this, ev, ...args); } catch (e) { queueMicrotask(() => runTails(ev, args)); throw e; } if (r && typeof r.then === "function") r.finally(() => runTails(ev, args)); else queueMicrotask(() => runTails(ev, args)); return r; }; Object.defineProperty(mw, SYM.ID, { value: true }); return Object.freeze(mw); }; - const ensureAccessor = () => { try { const d = Object.getOwnPropertyDescriptor(es, "emit"); if (!es[SYM.ORIG_DESC]) es[SYM.ORIG_DESC] = d || null; es[SYM.BASE] ||= getFnFromDesc(d); Object.defineProperty(es, "emit", { configurable: true, enumerable: d?.enumerable ?? true, get() { return reapply(); }, set(v) { if (typeof v === "function") { es[SYM.BASE] = v; queueMicrotask(reapply); } } }); } catch { } }; - const reapply = () => { try { const base = es[SYM.BASE] || getFnFromDesc(Object.getOwnPropertyDescriptor(es, "emit")) || es.emit.bind(es); const stack = es[SYM.MW_STACK] || (es[SYM.MW_STACK] = []); let idx = stack.findIndex((m) => m && m[SYM.ID]); if (idx === -1) { stack.push(makeTailMw()); idx = stack.length - 1; } if (idx !== stack.length - 1) { const mw = stack[idx]; stack.splice(idx, 1); stack.push(mw); } const composed = compose(base, stack) || base; if (!es[SYM.COMPOSED] || es[SYM.COMPOSED]._base !== base || es[SYM.COMPOSED]._stack !== stack) { composed._base = base; composed._stack = stack; es[SYM.COMPOSED] = composed; } return es[SYM.COMPOSED]; } catch { return es.emit; } }; - ensureAccessor(); - queueMicrotask(reapply); - const api = { onLast: (e, h) => addTail(e, h), removeLast: (e, h) => { const a = tails.get(e); if (!a) return; const i = a.indexOf(h); if (i >= 0) a.splice(i, 1); }, uninstall() { try { const s = es[SYM.MW_STACK]; const i = Array.isArray(s) ? s.findIndex((m) => m && m[SYM.ID]) : -1; if (i >= 0) s.splice(i, 1); const orig = es[SYM.ORIG_DESC]; if (orig) { try { Object.defineProperty(es, "emit", orig); } catch { Object.defineProperty(es, "emit", { configurable: true, enumerable: true, writable: true, value: es[SYM.BASE] || es.emit }); } } else { Object.defineProperty(es, "emit", { configurable: true, enumerable: true, writable: true, value: es[SYM.BASE] || es.emit }); } } catch { } delete es.__lw_tailInstalled; delete es.__lw_tailAPI; tails.clear(); } }; - Object.defineProperty(es, "__lw_tailInstalled", { value: true }); - Object.defineProperty(es, "__lw_tailAPI", { value: api }); - return api; -} - -let __installed = false; -const MW_KEY = Symbol.for("lwbox.fetchMiddlewareStack"); -const BASE_KEY = Symbol.for("lwbox.fetchBase"); -const ORIG_KEY = Symbol.for("lwbox.fetch.origDesc"); -const CMP_KEY = Symbol.for("lwbox.fetch.composed"); -const ID = Symbol.for("lwbox.middleware.identity"); -const getFetchFromDesc = (d) => { try { if (typeof d?.value === "function") return d.value; if (typeof d?.get === "function") { const v = d.get.call(window); if (typeof v === "function") return v; } } catch { } return globalThis.fetch; }; -const compose = (base, stack) => stack.reduce((acc, mw) => mw(acc), base); -const withTimeout = (p, ms = 200) => { try { return Promise.race([p, new Promise((r) => setTimeout(r, ms))]); } catch { return p; } }; -const ensureAccessor = () => { try { const d = Object.getOwnPropertyDescriptor(window, "fetch"); if (!window[ORIG_KEY]) window[ORIG_KEY] = d || null; window[BASE_KEY] ||= getFetchFromDesc(d); Object.defineProperty(window, "fetch", { configurable: true, enumerable: d?.enumerable ?? true, get() { return reapply(); }, set(v) { if (typeof v === "function") { window[BASE_KEY] = v; queueMicrotask(reapply); } } }); } catch { } }; -const reapply = () => { try { const base = window[BASE_KEY] || getFetchFromDesc(Object.getOwnPropertyDescriptor(window, "fetch")); const stack = window[MW_KEY] || (window[MW_KEY] = []); let idx = stack.findIndex((m) => m && m[ID]); if (idx === -1) { stack.push(makeMw()); idx = stack.length - 1; } if (idx !== window[MW_KEY].length - 1) { const mw = window[MW_KEY][idx]; window[MW_KEY].splice(idx, 1); window[MW_KEY].push(mw); } const composed = compose(base, stack) || base; if (!window[CMP_KEY] || window[CMP_KEY]._base !== base || window[CMP_KEY]._stack !== stack) { composed._base = base; composed._stack = stack; window[CMP_KEY] = composed; } return window[CMP_KEY]; } catch { return globalThis.fetch; } }; -function makeMw() { - const mw = (next) => async function f(input, options = {}) { - try { - if (await isTarget(input, options)) { - if (S.isPreview || S.isLong) { - const url = input instanceof Request ? input.url : input; - return interceptPreview(url, options).catch(() => new Response(JSON.stringify({ error: { message: "拦截失败,请手动中止消息生成。" } }), { status: 200, headers: { "Content-Type": "application/json" } })); - } else { try { await withTimeout(recordReal(input, options)); } catch { } } - } - } catch { } - return Reflect.apply(next, this, arguments); - }; - Object.defineProperty(mw, ID, { value: true, enumerable: false }); - return Object.freeze(mw); -} -function installFetch() { - if (__installed) return; __installed = true; - try { - window[MW_KEY] ||= []; - window[BASE_KEY] ||= getFetchFromDesc(Object.getOwnPropertyDescriptor(window, "fetch")); - ensureAccessor(); - if (!window[MW_KEY].some((m) => m && m[ID])) window[MW_KEY].push(makeMw()); - else { - const i = window[MW_KEY].findIndex((m) => m && m[ID]); - if (i !== window[MW_KEY].length - 1) { - const mw = window[MW_KEY][i]; - window[MW_KEY].splice(i, 1); - window[MW_KEY].push(mw); - } - } - queueMicrotask(reapply); - window.addEventListener("pageshow", reapply, { passive: true }); - document.addEventListener("visibilitychange", () => { if (document.visibilityState === "visible") reapply(); }, { passive: true }); - window.addEventListener("focus", reapply, { passive: true }); - } catch { } -} -function uninstallFetch() { - if (!__installed) return; - try { - const s = window[MW_KEY]; - const i = Array.isArray(s) ? s.findIndex((m) => m && m[ID]) : -1; - if (i >= 0) s.splice(i, 1); - const others = Array.isArray(window[MW_KEY]) && window[MW_KEY].length; - const orig = window[ORIG_KEY]; - if (!others) { - if (orig) { - try { Object.defineProperty(window, "fetch", orig); } - catch { Object.defineProperty(window, "fetch", { configurable: true, enumerable: true, writable: true, value: window[BASE_KEY] || globalThis.fetch }); } - } else { - Object.defineProperty(window, "fetch", { configurable: true, enumerable: true, writable: true, value: window[BASE_KEY] || globalThis.fetch }); - } - } else { - reapply(); - } - } catch { } - __installed = false; -} -const setupFetch = () => { if (!S.active) { installFetch(); S.active = true; } }; -const restoreFetch = () => { if (S.active) { uninstallFetch(); S.active = false; } }; -const updateFetchState = () => { const st = getSettings(), need = (st.preview.enabled || st.recorded.enabled); if (need && !S.active) setupFetch(); if (!need && S.active) restoreFetch(); }; - -async function interceptPreview(url, options) { - const body = await safeReadBodyFromInput(url, options); - const data = safeJson(body) || {}; - const userInput = extractUser(data?.messages || []); - const ctx = getContext(); - - if (S.isLong) { - const chat = Array.isArray(ctx.chat) ? ctx.chat : []; - let start = chat.length; - if (chat.length > 0 && chat[chat.length - 1]?.is_user === true) start = chat.length - 1; - S.chatLenBefore = start; - S.pendingPurge = true; - oneShotOnLast(event_types.GENERATION_ENDED, () => setTimeout(() => purgePreviewArtifacts(), 0)); - } - - S.previewData = { url, method: options?.method || "POST", requestData: data, messages: data?.messages || [], model: data?.model || "Unknown", timestamp: now(), userInput, isPreview: true }; - if (S.isLong) { setTimeout(() => { displayPreview(S.previewData); }, 100); } else if (S.resolve) { S.resolve({ success: true, data: S.previewData }); S.resolve = S.reject = null; } - const payload = S.isLong ? { choices: [{ message: { content: "【小白X】已拦截消息" }, finish_reason: "stop" }], intercepted: true } : { choices: [{ message: { content: "" }, finish_reason: "stop" }] }; - return new Response(JSON.stringify(payload), { status: 200, headers: { "Content-Type": "application/json" } }); -} - -const addHistoryButtonsDebounced = debounce(() => { - const set = getSettings(); if (!set.recorded.enabled || !geEnabled()) return; - $(".mes_history_preview").remove(); - $("#chat .mes").each(function () { - const id = parseInt($(this).attr("mesid")), isUser = $(this).attr("is_user") === "true"; - if (id <= 0 || isUser) return; - const btn = $(`
`).on("click", (e) => { e.preventDefault(); e.stopPropagation(); showHistoryPreview(id); }); - if (window.registerButtonToSubContainer && window.registerButtonToSubContainer(id, btn[0])) return; - $(this).find(".flex-container.flex1.alignitemscenter").append(btn); - }); -}, C.DEBOUNCE); - -const disableSend = (dis = true) => { - const $b = $q("#send_but"); - if (dis) { S.sendBtnWasDisabled = $b.prop("disabled"); $b.prop("disabled", true).off("click.preview-block").on("click.preview-block", (e) => { e.preventDefault(); e.stopImmediatePropagation(); return false; }); } - else { $b.prop("disabled", S.sendBtnWasDisabled).off("click.preview-block"); S.sendBtnWasDisabled = false; } -}; -const triggerSend = () => { - const $b = $q("#send_but"), $t = $q("#send_textarea"), txt = String($t.val() || ""); if (!txt.trim()) return false; - const was = $b.prop("disabled"); $b.prop("disabled", false); $b[0].dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, view: window })); if (was) $b.prop("disabled", true); return true; -}; - -async function showPreview() { - let toast = null, backup = null; - try { - const set = getSettings(); if (!set.preview.enabled || !geEnabled()) return toastr.warning("消息拦截功能未启用"); - const text = String($q("#send_textarea").val() || "").trim(); if (!text) return toastr.error("请先输入消息内容"); - - backup = text; disableSend(true); - const ctx = getContext(); - S.chatLenBefore = Array.isArray(ctx.chat) ? ctx.chat.length : 0; - S.isPreview = true; S.previewData = null; S.previewIds.clear(); S.previewAbort = new AbortController(); - S.pendingPurge = true; - - const endHandler = () => { - try { if (S.genEndedOff) { S.genEndedOff(); S.genEndedOff = null; } } catch { } - if (S.pendingPurge) { - setTimeout(() => purgePreviewArtifacts(), 0); - } - }; - - S.genEndedOff = oneShotOnLast(event_types.GENERATION_ENDED, endHandler); - clearTimeout(S.cleanupFallback); - S.cleanupFallback = setTimeout(() => { - try { if (S.genEndedOff) { S.genEndedOff(); S.genEndedOff = null; } } catch { } - purgePreviewArtifacts(); - }, 1500); - - toast = toastr.info(`正在拦截请求...(${set.preview.timeoutSeconds}秒超时)`, "消息拦截", { timeOut: 0, tapToDismiss: false }); - - if (!triggerSend()) throw new Error("无法触发发送事件"); - - const res = await waitIntercept().catch((e) => ({ success: false, error: e?.message || e })); - if (toast) { toastr.clear(toast); toast = null; } - if (res.success) { await displayPreview(res.data); toastr.success("拦截成功!", "", { timeOut: 3000 }); } - else toastr.error(`拦截失败: ${res.error}`, "", { timeOut: 5000 }); - } catch (e) { - if (toast) toastr.clear(toast); toastr.error(`拦截异常: ${e.message}`, "", { timeOut: 5000 }); - } finally { - try { S.previewAbort?.abort("拦截结束"); } catch { } S.previewAbort = null; - if (S.resolve) S.resolve({ success: false, error: "拦截已取消" }); S.resolve = S.reject = null; - clearTimeout(S.cleanupFallback); S.cleanupFallback = null; - S.isPreview = false; S.previewData = null; - disableSend(false); if (backup) $q("#send_textarea").val(backup); - } -} - -async function showHistoryPreview(messageId) { - try { - const set = getSettings(); if (!set.recorded.enabled || !geEnabled()) return; - const rec = findRec(messageId); - if (rec?.messages?.length || rec?.requestData?.messages?.length) await openPopup(buildPreviewHtml({ ...rec, isHistoryPreview: true, targetMessageId: messageId }), `消息历史查看 - 第 ${messageId + 1} 条消息`); - else toastr.warning(`未找到第 ${messageId + 1} 条消息的API请求记录`); - } catch { toastr.error("查看历史消息失败"); } -} - -const cleanupMemory = () => { - if (S.history.length > C.MAX_HISTORY) S.history = S.history.slice(0, C.MAX_HISTORY); - S.previewIds.clear(); S.previewData = null; $(".mes_history_preview").each(function () { if (!$(this).closest(".mes").length) $(this).remove(); }); - if (!S.isLong) S.interceptedIds = []; -}; - -function onLast(ev, handler) { - if (typeof eventSource.makeLast === "function") { eventSource.makeLast(ev, handler); S.listeners.push({ e: ev, h: handler, off: () => { } }); return; } - if (S.tailAPI?.onLast) { const off = S.tailAPI.onLast(ev, handler); S.listeners.push({ e: ev, h: handler, off }); return; } - const tail = (...args) => queueMicrotask(() => { try { handler(...args); } catch { } }); - eventSource.on(ev, tail); - S.listeners.push({ e: ev, h: tail, off: () => eventSource.removeListener?.(ev, tail) }); -} - -const addEvents = () => { - removeEvents(); - [ - { e: event_types.MESSAGE_RECEIVED, h: addHistoryButtonsDebounced }, - { e: event_types.CHARACTER_MESSAGE_RENDERED, h: addHistoryButtonsDebounced }, - { e: event_types.USER_MESSAGE_RENDERED, h: addHistoryButtonsDebounced }, - { e: event_types.CHAT_CHANGED, h: () => { S.history = []; setTimeout(addHistoryButtonsDebounced, C.CHECK); } }, - { e: event_types.MESSAGE_RECEIVED, h: (messageId) => setTimeout(() => { const r = S.history.find((x) => !x.associatedMessageId && now() - x.timestamp < C.REQ_WINDOW); if (r) r.associatedMessageId = messageId; }, 100) }, - ].forEach(({ e, h }) => onLast(e, h)); - const late = (payload) => { - try { - const ctx = getContext(); - pushHistory({ - url: C.TARGET, method: "POST", requestData: payload, messages: payload?.messages || [], model: payload?.model || "Unknown", - timestamp: now(), messageId: ctx.chat?.length || 0, characterName: ctx.characters?.[ctx.characterId]?.name || "Unknown", - userInput: extractUser(payload?.messages || []), isRealRequest: true, source: "settings_ready", - }); - } catch { } - queueMicrotask(() => updateFetchState()); - }; - if (typeof eventSource.makeLast === "function") { eventSource.makeLast(event_types.CHAT_COMPLETION_SETTINGS_READY, late); S.listeners.push({ e: event_types.CHAT_COMPLETION_SETTINGS_READY, h: late, off: () => { } }); } - else if (S.tailAPI?.onLast) { const off = S.tailAPI.onLast(event_types.CHAT_COMPLETION_SETTINGS_READY, late); S.listeners.push({ e: event_types.CHAT_COMPLETION_SETTINGS_READY, h: late, off }); } - else { ON(event_types.CHAT_COMPLETION_SETTINGS_READY, late); S.listeners.push({ e: event_types.CHAT_COMPLETION_SETTINGS_READY, h: late, off: () => OFF(event_types.CHAT_COMPLETION_SETTINGS_READY, late) }); queueMicrotask(() => { try { OFF(event_types.CHAT_COMPLETION_SETTINGS_READY, late); } catch { } try { ON(event_types.CHAT_COMPLETION_SETTINGS_READY, late); } catch { } }); } -}; -const removeEvents = () => { S.listeners.forEach(({ e, h, off }) => { if (typeof off === "function") { try { off(); } catch { } } else { try { OFF(e, h); } catch { } } }); S.listeners = []; }; - -const toggleLong = () => { - S.isLong = !S.isLong; - const $b = $q("#message_preview_btn"); - if (S.isLong) { - $b.css("color", "red"); - toastr.info("持续拦截已开启", "", { timeOut: 2000 }); - } else { - $b.css("color", ""); - S.pendingPurge = false; - toastr.info("持续拦截已关闭", "", { timeOut: 2000 }); - } -}; -const bindBtn = () => { - const $b = $q("#message_preview_btn"); - $b.on("mousedown touchstart", () => { S.longPressTimer = setTimeout(() => toggleLong(), S.longPressDelay); }); - $b.on("mouseup touchend mouseleave", () => { if (S.longPressTimer) { clearTimeout(S.longPressTimer); S.longPressTimer = null; } }); - $b.on("click", () => { if (S.longPressTimer) { clearTimeout(S.longPressTimer); S.longPressTimer = null; return; } if (!S.isLong) showPreview(); }); -}; - -const waitIntercept = () => new Promise((resolve, reject) => { - const t = setTimeout(() => { if (S.resolve) { S.resolve({ success: false, error: `等待超时 (${getSettings().preview.timeoutSeconds}秒)` }); S.resolve = S.reject = null; } }, getSettings().preview.timeoutSeconds * 1000); - S.resolve = (v) => { clearTimeout(t); resolve(v); }; S.reject = (e) => { clearTimeout(t); reject(e); }; -}); - -function cleanup() { - removeEvents(); restoreFetch(); disableSend(false); - $(".mes_history_preview").remove(); $("#message_preview_btn").remove(); cleanupMemory(); - Object.assign(S, { resolve: null, reject: null, isPreview: false, isLong: false, interceptedIds: [], chatLenBefore: 0, sendBtnWasDisabled: false, pendingPurge: false }); - if (S.longPressTimer) { clearTimeout(S.longPressTimer); S.longPressTimer = null; } - if (S.restoreLong) { try { S.restoreLong(); } catch { } S.restoreLong = null; } - if (S.genEndedOff) { try { S.genEndedOff(); } catch { } S.genEndedOff = null; } - if (S.cleanupFallback) { clearTimeout(S.cleanupFallback); S.cleanupFallback = null; } -} - -function initMessagePreview() { - try { - cleanup(); S.tailAPI = installEventSourceTail(eventSource); - const set = getSettings(); - const btn = $(`
`); - $("#send_but").before(btn); bindBtn(); - $("#xiaobaix_preview_enabled").prop("checked", set.preview.enabled).on("change", function () { - if (!geEnabled()) return; set.preview.enabled = $(this).prop("checked"); saveSettingsDebounced(); - $("#message_preview_btn").toggle(set.preview.enabled); - if (set.preview.enabled) { if (!S.cleanTimer) S.cleanTimer = setInterval(cleanupMemory, C.CLEAN); } - else { if (S.cleanTimer) { clearInterval(S.cleanTimer); S.cleanTimer = null; } } - updateFetchState(); - if (!set.preview.enabled && set.recorded.enabled) { addEvents(); addHistoryButtonsDebounced(); } - }); - $("#xiaobaix_recorded_enabled").prop("checked", set.recorded.enabled).on("change", function () { - if (!geEnabled()) return; set.recorded.enabled = $(this).prop("checked"); saveSettingsDebounced(); - if (set.recorded.enabled) { addEvents(); addHistoryButtonsDebounced(); } - else { $(".mes_history_preview").remove(); S.history.length = 0; if (!set.preview.enabled) removeEvents(); } - updateFetchState(); - }); - if (!set.preview.enabled) $("#message_preview_btn").hide(); - updateFetchState(); if (set.recorded.enabled) addHistoryButtonsDebounced(); - if (set.preview.enabled || set.recorded.enabled) addEvents(); - if (window.registerModuleCleanup) window.registerModuleCleanup("messagePreview", cleanup); - if (set.preview.enabled) S.cleanTimer = setInterval(cleanupMemory, C.CLEAN); - } catch { toastr.error("模块初始化失败"); } -} - -window.addEventListener("beforeunload", cleanup); -window.messagePreviewCleanup = cleanup; - -export { initMessagePreview, addHistoryButtonsDebounced, cleanup }; \ No newline at end of file +import { extension_settings, getContext } from "../../../../extensions.js"; +import { saveSettingsDebounced, eventSource, event_types } from "../../../../../script.js"; +import { EXT_ID } from "../core/constants.js"; + +const C = { MAX_HISTORY: 10, CHECK: 200, DEBOUNCE: 300, CLEAN: 300000, TARGET: "/api/backends/chat-completions/generate", TIMEOUT: 30, ASSOC_DELAY: 1000, REQ_WINDOW: 30000 }; +const S = { active: false, isPreview: false, isLong: false, isHistoryUiBound: false, previewData: null, previewIds: new Set(), interceptedIds: [], history: [], listeners: [], resolve: null, reject: null, sendBtnWasDisabled: false, longPressTimer: null, longPressDelay: 1000, chatLenBefore: 0, restoreLong: null, cleanTimer: null, previewAbort: null, tailAPI: null, genEndedOff: null, cleanupFallback: null, pendingPurge: false }; + +const $q = (sel) => $(sel); +const ON = (e, c) => eventSource.on(e, c); +const OFF = (e, c) => eventSource.removeListener(e, c); +const now = () => Date.now(); +const geEnabled = () => { try { return ("isXiaobaixEnabled" in window) ? !!window.isXiaobaixEnabled : true; } catch { return true; } }; +const debounce = (fn, w) => { let t; return (...a) => { clearTimeout(t); t = setTimeout(() => fn(...a), w); }; }; +const safeJson = (t) => { try { return JSON.parse(t); } catch { return null; } }; + +const readText = async (b) => { try { if (!b) return ""; if (typeof b === "string") return b; if (b instanceof Blob) return await b.text(); if (b instanceof URLSearchParams) return b.toString(); if (typeof b === "object" && typeof b.text === "function") return await b.text(); } catch { } return ""; }; + +function isSafeBody(body) { if (!body) return true; return (typeof body === "string" || body instanceof Blob || body instanceof URLSearchParams || body instanceof ArrayBuffer || ArrayBuffer.isView(body) || (typeof FormData !== "undefined" && body instanceof FormData)); } + +async function safeReadBodyFromInput(input, options) { try { if (input instanceof Request) return await readText(input.clone()); const body = options?.body; if (!isSafeBody(body)) return ""; return await readText(body); } catch { return ""; } } + +const isGen = (u) => String(u || "").includes(C.TARGET); +const isTarget = async (input, opt = {}) => { try { const url = input instanceof Request ? input.url : input; if (!isGen(url)) return false; const text = await safeReadBodyFromInput(input, opt); return text ? text.includes('"messages"') : true; } catch { return input instanceof Request ? isGen(input.url) : isGen(input); } }; +const getSettings = () => { const d = extension_settings[EXT_ID] || (extension_settings[EXT_ID] = {}); d.preview = d.preview || { enabled: false, timeoutSeconds: C.TIMEOUT }; d.recorded = d.recorded || { enabled: true }; d.preview.timeoutSeconds = C.TIMEOUT; return d; }; + +function injectPreviewModalStyles() { + if (document.getElementById('message-preview-modal-styles')) return; + const style = document.createElement('style'); + style.id = 'message-preview-modal-styles'; + style.textContent = ` + .mp-overlay{position:fixed;inset:0;background:none;z-index:9999;display:flex;align-items:center;justify-content:center;pointer-events:none} + .mp-modal{ + width:clamp(360px,55vw,860px); + max-width:95vw; + background:var(--SmartThemeBlurTintColor); + border:2px solid var(--SmartThemeBorderColor); + border-radius:10px; + box-shadow:0 8px 16px var(--SmartThemeShadowColor); + pointer-events:auto; + display:flex; + flex-direction:column; + height:80vh; + max-height:calc(100vh - 60px); + resize:both; + overflow:hidden; + } + .mp-header{display:flex;justify-content:space-between;padding:10px 14px;border-bottom:1px solid var(--SmartThemeBorderColor);font-weight:600;cursor:move;flex-shrink:0} + .mp-body{height:60vh;overflow:auto;padding:10px;flex:1;min-height:160px} + .mp-footer{display:flex;gap:8px;justify-content:flex-end;padding:12px 14px;border-top:1px solid var(--SmartThemeBorderColor);flex-shrink:0} + .mp-close{cursor:pointer} + .mp-btn{cursor:pointer;border:1px solid var(--SmartThemeBorderColor);background:var(--SmartThemeBlurTintColor);padding:6px 10px;border-radius:6px} + .mp-search-input{padding:4px 8px;border:1px solid var(--SmartThemeBorderColor);border-radius:4px;background:var(--SmartThemeShadowColor);color:inherit;font-size:12px;width:120px} + .mp-search-btn{padding:4px 6px;font-size:12px;min-width:24px;text-align:center} + .mp-search-info{font-size:12px;opacity:.8;white-space:nowrap} + .message-preview-container{height:100%} + .message-preview-content-box{height:100%;overflow:auto} + .mp-highlight{background-color:yellow;color:black;padding:1px 2px;border-radius:2px} + .mp-highlight.current{background-color:orange;font-weight:bold} + @media (max-width:999px){ + .mp-overlay{position:absolute;inset:0;align-items:flex-start} + .mp-modal{width:100%;max-width:100%;max-height:100%;margin:0;border-radius:10px 10px 0 0;height:100vh;resize:none} + .mp-header{padding:8px 14px} + .mp-body{padding:8px} + .mp-footer{padding:8px 14px;flex-wrap:wrap;gap:6px} + .mp-search-input{width:150px} + } + `; + document.head.appendChild(style); +} + +function setupModalDrag(modal, overlay, header) { + modal.style.position = 'absolute'; + modal.style.left = '50%'; + modal.style.top = '50%'; + modal.style.transform = 'translate(-50%, -50%)'; + + let dragging = false, sx = 0, sy = 0, sl = 0, st = 0; + + function onDown(e) { + if (!(e instanceof PointerEvent) || e.button !== 0) return; + dragging = true; + const overlayRect = overlay.getBoundingClientRect(); + const rect = modal.getBoundingClientRect(); + modal.style.left = (rect.left - overlayRect.left) + 'px'; + modal.style.top = (rect.top - overlayRect.top) + 'px'; + modal.style.transform = ''; + sx = e.clientX; sy = e.clientY; + sl = parseFloat(modal.style.left) || 0; + st = parseFloat(modal.style.top) || 0; + window.addEventListener('pointermove', onMove, { passive: true }); + window.addEventListener('pointerup', onUp, { once: true }); + e.preventDefault(); + } + + function onMove(e) { + if (!dragging) return; + const dx = e.clientX - sx, dy = e.clientY - sy; + let nl = sl + dx, nt = st + dy; + const maxLeft = (overlay.clientWidth || overlay.getBoundingClientRect().width) - modal.offsetWidth; + const maxTop = (overlay.clientHeight || overlay.getBoundingClientRect().height) - modal.offsetHeight; + nl = Math.max(0, Math.min(maxLeft, nl)); + nt = Math.max(0, Math.min(maxTop, nt)); + modal.style.left = nl + 'px'; + modal.style.top = nt + 'px'; + } + + function onUp() { + dragging = false; + window.removeEventListener('pointermove', onMove); + } + + header.addEventListener('pointerdown', onDown); +} + +function createMovableModal(title, content) { + injectPreviewModalStyles(); + const overlay = document.createElement('div'); + overlay.className = 'mp-overlay'; + const modal = document.createElement('div'); + modal.className = 'mp-modal'; + const header = document.createElement('div'); + header.className = 'mp-header'; + // Template-only UI markup (title is escaped by caller). + // eslint-disable-next-line no-unsanitized/property + header.innerHTML = `${title}`; + const body = document.createElement('div'); + body.className = 'mp-body'; + // Content is already escaped before building the preview. + // eslint-disable-next-line no-unsanitized/property + body.innerHTML = content; + const footer = document.createElement('div'); + footer.className = 'mp-footer'; + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property + footer.innerHTML = ` + + + + + + + + `; + modal.appendChild(header); + modal.appendChild(body); + modal.appendChild(footer); + overlay.appendChild(modal); + setupModalDrag(modal, overlay, header); + + let searchResults = []; + let currentIndex = -1; + const searchInput = footer.querySelector('.mp-search-input'); + const searchInfo = footer.querySelector('#mp-search-info'); + const prevBtn = footer.querySelector('#mp-search-prev'); + const nextBtn = footer.querySelector('#mp-search-next'); + + function clearHighlights() { + body.querySelectorAll('.mp-highlight').forEach(el => { + // Controlled markup generated locally. + // eslint-disable-next-line no-unsanitized/property + el.outerHTML = el.innerHTML; + }); + } + function performSearch(query) { + clearHighlights(); + searchResults = []; + currentIndex = -1; + if (!query.trim()) { searchInfo.textContent = ''; return; } + const walker = document.createTreeWalker(body, NodeFilter.SHOW_TEXT, null, false); + const nodes = []; + let node; + while ((node = walker.nextNode())) { nodes.push(node); } + const regex = new RegExp(query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'); + nodes.forEach(textNode => { + const text = textNode.textContent; + if (!text || !regex.test(text)) return; + let html = text; + let offset = 0; + regex.lastIndex = 0; + const matches = [...text.matchAll(regex)]; + matches.forEach((m) => { + const start = m.index + offset; + const end = start + m[0].length; + const before = html.slice(0, start); + const mid = html.slice(start, end); + const after = html.slice(end); + const span = `${mid}`; + html = before + span + after; + offset += span.length - m[0].length; + searchResults.push({}); + }); + const parent = textNode.parentElement; + // Controlled markup generated locally. + // eslint-disable-next-line no-unsanitized/property + parent.innerHTML = parent.innerHTML.replace(text, html); + }); + updateSearchInfo(); + if (searchResults.length > 0) { currentIndex = 0; highlightCurrent(); } + } + function updateSearchInfo() { if (!searchResults.length) searchInfo.textContent = searchInput.value.trim() ? '无结果' : ''; else searchInfo.textContent = `${currentIndex + 1}/${searchResults.length}`; } + function highlightCurrent() { + body.querySelectorAll('.mp-highlight.current').forEach(el => el.classList.remove('current')); + if (currentIndex >= 0 && currentIndex < searchResults.length) { + const el = body.querySelector(`.mp-highlight[data-search-index="${currentIndex}"]`); + if (el) { el.classList.add('current'); el.scrollIntoView({ behavior: 'smooth', block: 'center' }); } + } + } + function navigateSearch(direction) { + if (!searchResults.length) return; + if (direction === 'next') currentIndex = (currentIndex + 1) % searchResults.length; + else currentIndex = currentIndex <= 0 ? searchResults.length - 1 : currentIndex - 1; + updateSearchInfo(); + highlightCurrent(); + } + let searchTimeout; + searchInput.addEventListener('input', (e) => { clearTimeout(searchTimeout); searchTimeout = setTimeout(() => performSearch(e.target.value), 250); }); + searchInput.addEventListener('keydown', (e) => { if (e.key === 'Enter') { e.preventDefault(); if (e.shiftKey) navigateSearch('prev'); else navigateSearch('next'); } else if (e.key === 'Escape') { searchInput.value = ''; performSearch(''); } }); + prevBtn.addEventListener('click', () => navigateSearch('prev')); + nextBtn.addEventListener('click', () => navigateSearch('next')); + footer.querySelector('#mp-focus-search')?.addEventListener('click', () => { searchInput.focus(); if (searchInput.value) navigateSearch('next'); }); + + const close = () => overlay.remove(); + header.querySelector('.mp-close').addEventListener('click', close); + footer.querySelector('#mp-close').addEventListener('click', close); + footer.querySelector('#mp-toggle-format').addEventListener('click', (e) => { + const box = body.querySelector(".message-preview-content-box"); + const f = box?.querySelector(".mp-state-formatted"); + const r = box?.querySelector(".mp-state-raw"); + if (!(f && r)) return; + const showRaw = r.style.display === "none"; + r.style.display = showRaw ? "block" : "none"; + f.style.display = showRaw ? "none" : "block"; + e.currentTarget.textContent = showRaw ? "切换整理格式" : "切换原始格式"; + searchInput.value = ""; + clearHighlights(); + searchInfo.textContent = ""; + searchResults = []; + currentIndex = -1; + }); + + document.body.appendChild(overlay); + return { overlay, modal, body, close }; +} + +const MIRROR = { MERGE: "merge", MERGE_TOOLS: "merge_tools", SEMI: "semi", SEMI_TOOLS: "semi_tools", STRICT: "strict", STRICT_TOOLS: "strict_tools", SINGLE: "single" }; +const roleMap = { system: { label: "SYSTEM:", color: "#F7E3DA" }, user: { label: "USER:", color: "#F0ADA7" }, assistant: { label: "ASSISTANT:", color: "#6BB2CC" } }; +const escapeHtml = (v) => String(v ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", "\"": """, "'": "'" }[c])); +const colorXml = (t) => { + const safe = escapeHtml(t); + return safe.replace(/<([^&]+?)>/g, '<$1>'); +}; +const getNames = (req) => { const n = { charName: String(req?.char_name || ""), userName: String(req?.user_name || ""), groupNames: Array.isArray(req?.group_names) ? req.group_names.map(String) : [] }; n.startsWithGroupName = (m) => n.groupNames.some((g) => String(m || "").startsWith(`${g}: `)); return n; }; +const toText = (m) => { const c = m?.content; if (typeof c === "string") return c; if (Array.isArray(c)) return c.map((p) => p?.type === "text" ? String(p.text || "") : p?.type === "image_url" ? "[image]" : p?.type === "video_url" ? "[video]" : typeof p === "string" ? p : (typeof p?.content === "string" ? p.content : "")).filter(Boolean).join("\n\n"); return String(c || ""); }; +const applyName = (m, n) => { const { role, name } = m; let t = toText(m); if (role === "system" && name === "example_assistant") { if (n.charName && !t.startsWith(`${n.charName}: `) && !n.startsWithGroupName(t)) t = `${n.charName}: ${t}`; } else if (role === "system" && name === "example_user") { if (n.userName && !t.startsWith(`${n.userName}: `)) t = `${n.userName}: ${t}`; } else if (name && role !== "system" && !t.startsWith(`${name}: `)) t = `${name}: ${t}`; return { ...m, content: t, name: undefined }; }; +function mergeMessages(messages, names, { strict = false, placeholders = false, single = false, tools = false } = {}) { + if (!Array.isArray(messages)) return []; + let mapped = messages.map((m) => applyName({ ...m }, names)).map((x) => { const m = { ...x }; if (!tools) { if (m.role === "tool") m.role = "user"; delete m.tool_calls; delete m.tool_call_id; } if (single) { if (m.role === "assistant") { const t = String(m.content || ""); if (names.charName && !t.startsWith(`${names.charName}: `) && !names.startsWithGroupName(t)) m.content = `${names.charName}: ${t}`; } if (m.role === "user") { const t = String(m.content || ""); if (names.userName && !t.startsWith(`${names.userName}: `)) m.content = `${names.userName}: ${t}`; } m.role = "user"; } return m; }); + const squash = (arr) => { const out = []; for (const m of arr) { if (out.length && out[out.length - 1].role === m.role && String(m.content || "").length && m.role !== "tool") out[out.length - 1].content += `\n\n${m.content}`; else out.push(m); } return out; }; + let sq = squash(mapped); + if (strict) { for (let i = 0; i < sq.length; i++) if (i > 0 && sq[i].role === "system") sq[i].role = "user"; if (placeholders) { if (!sq.length) sq.push({ role: "user", content: "[Start a new chat]" }); else if (sq[0].role === "system" && (sq.length === 1 || sq[1].role !== "user")) sq.splice(1, 0, { role: "user", content: "[Start a new chat]" }); else if (sq[0].role !== "system" && sq[0].role !== "user") sq.unshift({ role: "user", content: "[Start a new chat]" }); } return squash(sq); } + if (!sq.length) sq.push({ role: "user", content: "[Start a new chat]" }); + return sq; +} +function mirror(requestData) { + try { + let type = String(requestData?.custom_prompt_post_processing || "").toLowerCase(); + const source = String(requestData?.chat_completion_source || "").toLowerCase(); + if (source === "perplexity") type = MIRROR.STRICT; + const names = getNames(requestData || {}), src = Array.isArray(requestData?.messages) ? JSON.parse(JSON.stringify(requestData.messages)) : []; + const mk = (o) => mergeMessages(src, names, o); + switch (type) { + case MIRROR.MERGE: return mk({ strict: false }); + case MIRROR.MERGE_TOOLS: return mk({ strict: false, tools: true }); + case MIRROR.SEMI: return mk({ strict: true }); + case MIRROR.SEMI_TOOLS: return mk({ strict: true, tools: true }); + case MIRROR.STRICT: return mk({ strict: true, placeholders: true }); + case MIRROR.STRICT_TOOLS: return mk({ strict: true, placeholders: true, tools: true }); + case MIRROR.SINGLE: return mk({ strict: true, single: true }); + default: return src; + } + } catch { return Array.isArray(requestData?.messages) ? requestData.messages : []; } +} +const finalMsgs = (d) => { try { if (d?.requestData?.messages) return mirror(d.requestData); if (Array.isArray(d?.messages)) return d.messages; return []; } catch { return Array.isArray(d?.messages) ? d.messages : []; } }; +const formatPreview = (d) => { + const msgs = finalMsgs(d); + let out = `↓酒馆日志↓(${msgs.length})\n${"-".repeat(30)}\n`; + msgs.forEach((m, i) => { + const txt = String(m.content || ""); + const safeTxt = escapeHtml(txt); + const rm = roleMap[m.role] || { label: `${String(m.role || "").toUpperCase()}:`, color: "#FFF" }; + out += `
${rm.label}
`; + out += /<[^>]+>/g.test(txt) ? `
${colorXml(txt)}
` : `
${safeTxt}
`; + }); + return out; +}; +const stripTop = (o) => { try { if (!o || typeof o !== "object") return o; if (Array.isArray(o)) return o; const messages = Array.isArray(o.messages) ? JSON.parse(JSON.stringify(o.messages)) : undefined; return typeof messages !== "undefined" ? { messages } : {}; } catch { return {}; } }; +const formatRaw = (d) => { try { const hasReq = Array.isArray(d?.requestData?.messages), hasMsgs = !hasReq && Array.isArray(d?.messages); let obj; if (hasReq) { const req = JSON.parse(JSON.stringify(d.requestData)); try { req.messages = mirror(req); } catch { } obj = req; } else if (hasMsgs) { const fake = { ...(d || {}), messages: d.messages }; let mm = null; try { mm = mirror(fake); } catch { } obj = { ...(d || {}), messages: mm || d.messages }; } else obj = d?.requestData ?? d; obj = stripTop(obj); return colorXml(JSON.stringify(obj, null, 2)); } catch { try { return colorXml(String(d)); } catch { return ""; } } }; +const buildPreviewHtml = (d) => { const formatted = formatPreview(d), raw = formatRaw(d); return `
${formatted}
`; }; +const openPopup = async (html, title) => { createMovableModal(title, html); }; +const displayPreview = async (d) => { try { await openPopup(buildPreviewHtml(d), "消息拦截"); } catch { toastr.error("显示拦截失败"); } }; + +const pushHistory = (r) => { S.history.unshift(r); if (S.history.length > C.MAX_HISTORY) S.history.length = C.MAX_HISTORY; }; +const extractUser = (ms) => { if (!Array.isArray(ms)) return ""; for (let i = ms.length - 1; i >= 0; i--) if (ms[i]?.role === "user") return ms[i].content || ""; return ""; }; + +async function recordReal(input, options) { + try { + const url = input instanceof Request ? input.url : input; + const body = await safeReadBodyFromInput(input, options); + if (!body) return; + const data = safeJson(body) || {}, ctx = getContext(); + pushHistory({ url, method: options?.method || (input instanceof Request ? input.method : "POST"), requestData: data, messages: data.messages || [], model: data.model || "Unknown", timestamp: now(), messageId: ctx.chat?.length || 0, characterName: ctx.characters?.[ctx.characterId]?.name || "Unknown", userInput: extractUser(data.messages || []), isRealRequest: true }); + setTimeout(() => { if (S.history[0] && !S.history[0].associatedMessageId) S.history[0].associatedMessageId = ctx.chat?.length || 0; }, C.ASSOC_DELAY); + } catch { } +} + +const findRec = (id) => { + if (!S.history.length) return null; + const preds = [(r) => r.associatedMessageId === id, (r) => r.messageId === id, (r) => r.messageId === id - 1, (r) => Math.abs(r.messageId - id) <= 1]; + for (const p of preds) { const m = S.history.find(p); if (m) return m; } + const cs = S.history.filter((r) => r.messageId <= id + 2); + return cs.length ? cs.sort((a, b) => b.messageId - a.messageId)[0] : S.history[0]; +}; + +// Improved purgePreviewArtifacts - follows SillyTavern's batch delete pattern +async function purgePreviewArtifacts() { + try { + if (!S.pendingPurge) return; + S.pendingPurge = false; + const ctx = getContext(); + const chat = Array.isArray(ctx.chat) ? ctx.chat : []; + const start = Math.max(0, Number(S.chatLenBefore) || 0); + if (start >= chat.length) return; + + // 1. Remove DOM elements (following SillyTavern's pattern from #dialogue_del_mes_ok) + const $chat = $('#chat'); + $chat.find(`.mes[mesid="${start}"]`).nextAll('.mes').addBack().remove(); + + // 2. Truncate chat array + chat.length = start; + + // 3. Update last_mes class + $('#chat .mes').removeClass('last_mes'); + $('#chat .mes').last().addClass('last_mes'); + + // 4. Save chat and emit MESSAGE_DELETED event (critical for other plugins) + ctx.saveChat?.(); + await eventSource.emit(event_types.MESSAGE_DELETED, start); + } catch (e) { + console.error('[message-preview] purgePreviewArtifacts error', e); + } +} + + + +function oneShotOnLast(ev, handler) { + const wrapped = (...args) => { + try { handler(...args); } finally { off(); } + }; + let off = () => { }; + if (typeof eventSource.makeLast === "function") { + eventSource.makeLast(ev, wrapped); + off = () => { + try { eventSource.removeListener?.(ev, wrapped); } catch { } + try { eventSource.off?.(ev, wrapped); } catch { } + }; + } else if (S.tailAPI?.onLast) { + const disposer = S.tailAPI.onLast(ev, wrapped); + off = () => { try { disposer?.(); } catch { } }; + } else { + eventSource.on(ev, wrapped); + off = () => { try { eventSource.removeListener?.(ev, wrapped); } catch { } }; + } + return off; +} + +function installEventSourceTail(es) { + if (!es || es.__lw_tailInstalled) return es?.__lw_tailAPI || null; + const SYM = { MW_STACK: Symbol.for("lwbox.es.emitMiddlewareStack"), BASE: Symbol.for("lwbox.es.emitBase"), ORIG_DESC: Symbol.for("lwbox.es.emit.origDesc"), COMPOSED: Symbol.for("lwbox.es.emit.composed"), ID: Symbol.for("lwbox.middleware.identity") }; + const getFnFromDesc = (d) => { try { if (typeof d?.value === "function") return d.value; if (typeof d?.get === "function") { const v = d.get.call(es); if (typeof v === "function") return v; } } catch { } return es.emit?.bind?.(es) || es.emit; }; + const compose = (base, stack) => stack.reduce((acc, mw) => mw(acc), base); + const tails = new Map(); + const addTail = (ev, fn) => { if (typeof fn !== "function") return () => { }; const arr = tails.get(ev) || []; arr.push(fn); tails.set(ev, arr); return () => { const a = tails.get(ev); if (!a) return; const i = a.indexOf(fn); if (i >= 0) a.splice(i, 1); }; }; + const runTails = (ev, args) => { const arr = tails.get(ev); if (!arr?.length) return; for (const h of arr.slice()) { try { h(...args); } catch (e) { } } }; + const makeTailMw = () => { const mw = (next) => function patchedEmit(ev, ...args) { let r; try { r = next.call(this, ev, ...args); } catch (e) { queueMicrotask(() => runTails(ev, args)); throw e; } if (r && typeof r.then === "function") r.finally(() => runTails(ev, args)); else queueMicrotask(() => runTails(ev, args)); return r; }; Object.defineProperty(mw, SYM.ID, { value: true }); return Object.freeze(mw); }; + const ensureAccessor = () => { try { const d = Object.getOwnPropertyDescriptor(es, "emit"); if (!es[SYM.ORIG_DESC]) es[SYM.ORIG_DESC] = d || null; es[SYM.BASE] ||= getFnFromDesc(d); Object.defineProperty(es, "emit", { configurable: true, enumerable: d?.enumerable ?? true, get() { return reapply(); }, set(v) { if (typeof v === "function") { es[SYM.BASE] = v; queueMicrotask(reapply); } } }); } catch { } }; + const reapply = () => { try { const base = es[SYM.BASE] || getFnFromDesc(Object.getOwnPropertyDescriptor(es, "emit")) || es.emit.bind(es); const stack = es[SYM.MW_STACK] || (es[SYM.MW_STACK] = []); let idx = stack.findIndex((m) => m && m[SYM.ID]); if (idx === -1) { stack.push(makeTailMw()); idx = stack.length - 1; } if (idx !== stack.length - 1) { const mw = stack[idx]; stack.splice(idx, 1); stack.push(mw); } const composed = compose(base, stack) || base; if (!es[SYM.COMPOSED] || es[SYM.COMPOSED]._base !== base || es[SYM.COMPOSED]._stack !== stack) { composed._base = base; composed._stack = stack; es[SYM.COMPOSED] = composed; } return es[SYM.COMPOSED]; } catch { return es.emit; } }; + ensureAccessor(); + queueMicrotask(reapply); + const api = { onLast: (e, h) => addTail(e, h), removeLast: (e, h) => { const a = tails.get(e); if (!a) return; const i = a.indexOf(h); if (i >= 0) a.splice(i, 1); }, uninstall() { try { const s = es[SYM.MW_STACK]; const i = Array.isArray(s) ? s.findIndex((m) => m && m[SYM.ID]) : -1; if (i >= 0) s.splice(i, 1); const orig = es[SYM.ORIG_DESC]; if (orig) { try { Object.defineProperty(es, "emit", orig); } catch { Object.defineProperty(es, "emit", { configurable: true, enumerable: true, writable: true, value: es[SYM.BASE] || es.emit }); } } else { Object.defineProperty(es, "emit", { configurable: true, enumerable: true, writable: true, value: es[SYM.BASE] || es.emit }); } } catch { } delete es.__lw_tailInstalled; delete es.__lw_tailAPI; tails.clear(); } }; + Object.defineProperty(es, "__lw_tailInstalled", { value: true }); + Object.defineProperty(es, "__lw_tailAPI", { value: api }); + return api; +} + +let __installed = false; +const MW_KEY = Symbol.for("lwbox.fetchMiddlewareStack"); +const BASE_KEY = Symbol.for("lwbox.fetchBase"); +const ORIG_KEY = Symbol.for("lwbox.fetch.origDesc"); +const CMP_KEY = Symbol.for("lwbox.fetch.composed"); +const ID = Symbol.for("lwbox.middleware.identity"); +const getFetchFromDesc = (d) => { try { if (typeof d?.value === "function") return d.value; if (typeof d?.get === "function") { const v = d.get.call(window); if (typeof v === "function") return v; } } catch { } return globalThis.fetch; }; +const compose = (base, stack) => stack.reduce((acc, mw) => mw(acc), base); +const withTimeout = (p, ms = 200) => { try { return Promise.race([p, new Promise((r) => setTimeout(r, ms))]); } catch { return p; } }; +const ensureAccessor = () => { try { const d = Object.getOwnPropertyDescriptor(window, "fetch"); if (!window[ORIG_KEY]) window[ORIG_KEY] = d || null; window[BASE_KEY] ||= getFetchFromDesc(d); Object.defineProperty(window, "fetch", { configurable: true, enumerable: d?.enumerable ?? true, get() { return reapply(); }, set(v) { if (typeof v === "function") { window[BASE_KEY] = v; queueMicrotask(reapply); } } }); } catch { } }; +const reapply = () => { try { const base = window[BASE_KEY] || getFetchFromDesc(Object.getOwnPropertyDescriptor(window, "fetch")); const stack = window[MW_KEY] || (window[MW_KEY] = []); let idx = stack.findIndex((m) => m && m[ID]); if (idx === -1) { stack.push(makeMw()); idx = stack.length - 1; } if (idx !== window[MW_KEY].length - 1) { const mw = window[MW_KEY][idx]; window[MW_KEY].splice(idx, 1); window[MW_KEY].push(mw); } const composed = compose(base, stack) || base; if (!window[CMP_KEY] || window[CMP_KEY]._base !== base || window[CMP_KEY]._stack !== stack) { composed._base = base; composed._stack = stack; window[CMP_KEY] = composed; } return window[CMP_KEY]; } catch { return globalThis.fetch; } }; +function makeMw() { + const mw = (next) => async function f(input, options = {}) { + try { + if (await isTarget(input, options)) { + if (S.isPreview || S.isLong) { + const url = input instanceof Request ? input.url : input; + return interceptPreview(url, options).catch(() => new Response(JSON.stringify({ error: { message: "拦截失败,请手动中止消息生成。" } }), { status: 200, headers: { "Content-Type": "application/json" } })); + } else { try { await withTimeout(recordReal(input, options)); } catch { } } + } + } catch { } + return Reflect.apply(next, this, arguments); + }; + Object.defineProperty(mw, ID, { value: true, enumerable: false }); + return Object.freeze(mw); +} +function installFetch() { + if (__installed) return; __installed = true; + try { + window[MW_KEY] ||= []; + window[BASE_KEY] ||= getFetchFromDesc(Object.getOwnPropertyDescriptor(window, "fetch")); + ensureAccessor(); + if (!window[MW_KEY].some((m) => m && m[ID])) window[MW_KEY].push(makeMw()); + else { + const i = window[MW_KEY].findIndex((m) => m && m[ID]); + if (i !== window[MW_KEY].length - 1) { + const mw = window[MW_KEY][i]; + window[MW_KEY].splice(i, 1); + window[MW_KEY].push(mw); + } + } + queueMicrotask(reapply); + window.addEventListener("pageshow", reapply, { passive: true }); + document.addEventListener("visibilitychange", () => { if (document.visibilityState === "visible") reapply(); }, { passive: true }); + window.addEventListener("focus", reapply, { passive: true }); + } catch { } +} +function uninstallFetch() { + if (!__installed) return; + try { + const s = window[MW_KEY]; + const i = Array.isArray(s) ? s.findIndex((m) => m && m[ID]) : -1; + if (i >= 0) s.splice(i, 1); + const others = Array.isArray(window[MW_KEY]) && window[MW_KEY].length; + const orig = window[ORIG_KEY]; + if (!others) { + if (orig) { + try { Object.defineProperty(window, "fetch", orig); } + catch { Object.defineProperty(window, "fetch", { configurable: true, enumerable: true, writable: true, value: window[BASE_KEY] || globalThis.fetch }); } + } else { + Object.defineProperty(window, "fetch", { configurable: true, enumerable: true, writable: true, value: window[BASE_KEY] || globalThis.fetch }); + } + } else { + reapply(); + } + } catch { } + __installed = false; +} +const setupFetch = () => { if (!S.active) { installFetch(); S.active = true; } }; +const restoreFetch = () => { if (S.active) { uninstallFetch(); S.active = false; } }; +const updateFetchState = () => { const st = getSettings(), need = (st.preview.enabled || st.recorded.enabled); if (need && !S.active) setupFetch(); if (!need && S.active) restoreFetch(); }; + +async function interceptPreview(url, options) { + const body = await safeReadBodyFromInput(url, options); + const data = safeJson(body) || {}; + const userInput = extractUser(data?.messages || []); + const ctx = getContext(); + + if (S.isLong) { + const chat = Array.isArray(ctx.chat) ? ctx.chat : []; + let start = chat.length; + if (chat.length > 0 && chat[chat.length - 1]?.is_user === true) start = chat.length - 1; + S.chatLenBefore = start; + S.pendingPurge = true; + oneShotOnLast(event_types.GENERATION_ENDED, () => setTimeout(() => purgePreviewArtifacts(), 0)); + } + + S.previewData = { url, method: options?.method || "POST", requestData: data, messages: data?.messages || [], model: data?.model || "Unknown", timestamp: now(), userInput, isPreview: true }; + if (S.isLong) { setTimeout(() => { displayPreview(S.previewData); }, 100); } else if (S.resolve) { S.resolve({ success: true, data: S.previewData }); S.resolve = S.reject = null; } + const payload = S.isLong ? { choices: [{ message: { content: "【小白X】已拦截消息" }, finish_reason: "stop" }], intercepted: true } : { choices: [{ message: { content: "" }, finish_reason: "stop" }] }; + return new Response(JSON.stringify(payload), { status: 200, headers: { "Content-Type": "application/json" } }); +} + +const addHistoryButtonsDebounced = debounce(() => { + const set = getSettings(); if (!set.recorded.enabled || !geEnabled()) return; + $(".mes_history_preview").remove(); + $("#chat .mes").each(function () { + const id = parseInt($(this).attr("mesid")), isUser = $(this).attr("is_user") === "true"; + if (id <= 0 || isUser) return; + const btn = $(`
`).on("click", (e) => { e.preventDefault(); e.stopPropagation(); showHistoryPreview(id); }); + if (window.registerButtonToSubContainer && window.registerButtonToSubContainer(id, btn[0])) return; + $(this).find(".flex-container.flex1.alignitemscenter").append(btn); + }); +}, C.DEBOUNCE); + +const disableSend = (dis = true) => { + const $b = $q("#send_but"); + if (dis) { S.sendBtnWasDisabled = $b.prop("disabled"); $b.prop("disabled", true).off("click.preview-block").on("click.preview-block", (e) => { e.preventDefault(); e.stopImmediatePropagation(); return false; }); } + else { $b.prop("disabled", S.sendBtnWasDisabled).off("click.preview-block"); S.sendBtnWasDisabled = false; } +}; +const triggerSend = () => { + const $b = $q("#send_but"), $t = $q("#send_textarea"), txt = String($t.val() || ""); if (!txt.trim()) return false; + const was = $b.prop("disabled"); $b.prop("disabled", false); $b[0].dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, view: window })); if (was) $b.prop("disabled", true); return true; +}; + +async function showPreview() { + let toast = null, backup = null; + try { + const set = getSettings(); if (!set.preview.enabled || !geEnabled()) return toastr.warning("消息拦截功能未启用"); + const text = String($q("#send_textarea").val() || "").trim(); if (!text) return toastr.error("请先输入消息内容"); + + backup = text; disableSend(true); + const ctx = getContext(); + S.chatLenBefore = Array.isArray(ctx.chat) ? ctx.chat.length : 0; + S.isPreview = true; S.previewData = null; S.previewIds.clear(); S.previewAbort = new AbortController(); + S.pendingPurge = true; + + const endHandler = () => { + try { if (S.genEndedOff) { S.genEndedOff(); S.genEndedOff = null; } } catch { } + if (S.pendingPurge) { + setTimeout(() => purgePreviewArtifacts(), 0); + } + }; + + S.genEndedOff = oneShotOnLast(event_types.GENERATION_ENDED, endHandler); + clearTimeout(S.cleanupFallback); + S.cleanupFallback = setTimeout(() => { + try { if (S.genEndedOff) { S.genEndedOff(); S.genEndedOff = null; } } catch { } + purgePreviewArtifacts(); + }, 1500); + + toast = toastr.info(`正在拦截请求...(${set.preview.timeoutSeconds}秒超时)`, "消息拦截", { timeOut: 0, tapToDismiss: false }); + + if (!triggerSend()) throw new Error("无法触发发送事件"); + + const res = await waitIntercept().catch((e) => ({ success: false, error: e?.message || e })); + if (toast) { toastr.clear(toast); toast = null; } + if (res.success) { await displayPreview(res.data); toastr.success("拦截成功!", "", { timeOut: 3000 }); } + else toastr.error(`拦截失败: ${res.error}`, "", { timeOut: 5000 }); + } catch (e) { + if (toast) toastr.clear(toast); toastr.error(`拦截异常: ${e.message}`, "", { timeOut: 5000 }); + } finally { + try { S.previewAbort?.abort("拦截结束"); } catch { } S.previewAbort = null; + if (S.resolve) S.resolve({ success: false, error: "拦截已取消" }); S.resolve = S.reject = null; + clearTimeout(S.cleanupFallback); S.cleanupFallback = null; + S.isPreview = false; S.previewData = null; + disableSend(false); if (backup) $q("#send_textarea").val(backup); + } +} + +async function showHistoryPreview(messageId) { + try { + const set = getSettings(); if (!set.recorded.enabled || !geEnabled()) return; + const rec = findRec(messageId); + if (rec?.messages?.length || rec?.requestData?.messages?.length) await openPopup(buildPreviewHtml({ ...rec, isHistoryPreview: true, targetMessageId: messageId }), `消息历史查看 - 第 ${messageId + 1} 条消息`); + else toastr.warning(`未找到第 ${messageId + 1} 条消息的API请求记录`); + } catch { toastr.error("查看历史消息失败"); } +} + +const cleanupMemory = () => { + if (S.history.length > C.MAX_HISTORY) S.history = S.history.slice(0, C.MAX_HISTORY); + S.previewIds.clear(); S.previewData = null; $(".mes_history_preview").each(function () { if (!$(this).closest(".mes").length) $(this).remove(); }); + if (!S.isLong) S.interceptedIds = []; +}; + +function onLast(ev, handler) { + if (typeof eventSource.makeLast === "function") { eventSource.makeLast(ev, handler); S.listeners.push({ e: ev, h: handler, off: () => { } }); return; } + if (S.tailAPI?.onLast) { const off = S.tailAPI.onLast(ev, handler); S.listeners.push({ e: ev, h: handler, off }); return; } + const tail = (...args) => queueMicrotask(() => { try { handler(...args); } catch { } }); + eventSource.on(ev, tail); + S.listeners.push({ e: ev, h: tail, off: () => eventSource.removeListener?.(ev, tail) }); +} + +const addEvents = () => { + removeEvents(); + [ + { e: event_types.MESSAGE_RECEIVED, h: addHistoryButtonsDebounced }, + { e: event_types.CHARACTER_MESSAGE_RENDERED, h: addHistoryButtonsDebounced }, + { e: event_types.USER_MESSAGE_RENDERED, h: addHistoryButtonsDebounced }, + { e: event_types.CHAT_CHANGED, h: () => { S.history = []; setTimeout(addHistoryButtonsDebounced, C.CHECK); } }, + { e: event_types.MESSAGE_RECEIVED, h: (messageId) => setTimeout(() => { const r = S.history.find((x) => !x.associatedMessageId && now() - x.timestamp < C.REQ_WINDOW); if (r) r.associatedMessageId = messageId; }, 100) }, + ].forEach(({ e, h }) => onLast(e, h)); + const late = (payload) => { + try { + const ctx = getContext(); + pushHistory({ + url: C.TARGET, method: "POST", requestData: payload, messages: payload?.messages || [], model: payload?.model || "Unknown", + timestamp: now(), messageId: ctx.chat?.length || 0, characterName: ctx.characters?.[ctx.characterId]?.name || "Unknown", + userInput: extractUser(payload?.messages || []), isRealRequest: true, source: "settings_ready", + }); + } catch { } + queueMicrotask(() => updateFetchState()); + }; + if (typeof eventSource.makeLast === "function") { eventSource.makeLast(event_types.CHAT_COMPLETION_SETTINGS_READY, late); S.listeners.push({ e: event_types.CHAT_COMPLETION_SETTINGS_READY, h: late, off: () => { } }); } + else if (S.tailAPI?.onLast) { const off = S.tailAPI.onLast(event_types.CHAT_COMPLETION_SETTINGS_READY, late); S.listeners.push({ e: event_types.CHAT_COMPLETION_SETTINGS_READY, h: late, off }); } + else { ON(event_types.CHAT_COMPLETION_SETTINGS_READY, late); S.listeners.push({ e: event_types.CHAT_COMPLETION_SETTINGS_READY, h: late, off: () => OFF(event_types.CHAT_COMPLETION_SETTINGS_READY, late) }); queueMicrotask(() => { try { OFF(event_types.CHAT_COMPLETION_SETTINGS_READY, late); } catch { } try { ON(event_types.CHAT_COMPLETION_SETTINGS_READY, late); } catch { } }); } +}; +const removeEvents = () => { S.listeners.forEach(({ e, h, off }) => { if (typeof off === "function") { try { off(); } catch { } } else { try { OFF(e, h); } catch { } } }); S.listeners = []; }; + +const toggleLong = () => { + S.isLong = !S.isLong; + const $b = $q("#message_preview_btn"); + if (S.isLong) { + $b.css("color", "red"); + toastr.info("持续拦截已开启", "", { timeOut: 2000 }); + } else { + $b.css("color", ""); + S.pendingPurge = false; + toastr.info("持续拦截已关闭", "", { timeOut: 2000 }); + } +}; +const bindBtn = () => { + const $b = $q("#message_preview_btn"); + $b.on("mousedown touchstart", () => { S.longPressTimer = setTimeout(() => toggleLong(), S.longPressDelay); }); + $b.on("mouseup touchend mouseleave", () => { if (S.longPressTimer) { clearTimeout(S.longPressTimer); S.longPressTimer = null; } }); + $b.on("click", () => { if (S.longPressTimer) { clearTimeout(S.longPressTimer); S.longPressTimer = null; return; } if (!S.isLong) showPreview(); }); +}; + +const waitIntercept = () => new Promise((resolve, reject) => { + const t = setTimeout(() => { if (S.resolve) { S.resolve({ success: false, error: `等待超时 (${getSettings().preview.timeoutSeconds}秒)` }); S.resolve = S.reject = null; } }, getSettings().preview.timeoutSeconds * 1000); + S.resolve = (v) => { clearTimeout(t); resolve(v); }; S.reject = (e) => { clearTimeout(t); reject(e); }; +}); + +function cleanup() { + removeEvents(); restoreFetch(); disableSend(false); + $(".mes_history_preview").remove(); $("#message_preview_btn").remove(); cleanupMemory(); + Object.assign(S, { resolve: null, reject: null, isPreview: false, isLong: false, interceptedIds: [], chatLenBefore: 0, sendBtnWasDisabled: false, pendingPurge: false }); + if (S.longPressTimer) { clearTimeout(S.longPressTimer); S.longPressTimer = null; } + if (S.restoreLong) { try { S.restoreLong(); } catch { } S.restoreLong = null; } + if (S.genEndedOff) { try { S.genEndedOff(); } catch { } S.genEndedOff = null; } + if (S.cleanupFallback) { clearTimeout(S.cleanupFallback); S.cleanupFallback = null; } +} + +function initMessagePreview() { + try { + cleanup(); S.tailAPI = installEventSourceTail(eventSource); + const set = getSettings(); + const btn = $(`
`); + $("#send_but").before(btn); bindBtn(); + $("#xiaobaix_preview_enabled").prop("checked", set.preview.enabled).on("change", function () { + if (!geEnabled()) return; set.preview.enabled = $(this).prop("checked"); saveSettingsDebounced(); + $("#message_preview_btn").toggle(set.preview.enabled); + if (set.preview.enabled) { if (!S.cleanTimer) S.cleanTimer = setInterval(cleanupMemory, C.CLEAN); } + else { if (S.cleanTimer) { clearInterval(S.cleanTimer); S.cleanTimer = null; } } + updateFetchState(); + if (!set.preview.enabled && set.recorded.enabled) { addEvents(); addHistoryButtonsDebounced(); } + }); + $("#xiaobaix_recorded_enabled").prop("checked", set.recorded.enabled).on("change", function () { + if (!geEnabled()) return; set.recorded.enabled = $(this).prop("checked"); saveSettingsDebounced(); + if (set.recorded.enabled) { addEvents(); addHistoryButtonsDebounced(); } + else { $(".mes_history_preview").remove(); S.history.length = 0; if (!set.preview.enabled) removeEvents(); } + updateFetchState(); + }); + if (!set.preview.enabled) $("#message_preview_btn").hide(); + updateFetchState(); if (set.recorded.enabled) addHistoryButtonsDebounced(); + if (set.preview.enabled || set.recorded.enabled) addEvents(); + if (window.registerModuleCleanup) window.registerModuleCleanup("messagePreview", cleanup); + if (set.preview.enabled) S.cleanTimer = setInterval(cleanupMemory, C.CLEAN); + } catch { toastr.error("模块初始化失败"); } +} + +window.addEventListener("beforeunload", cleanup); +window.messagePreviewCleanup = cleanup; + +export { initMessagePreview, addHistoryButtonsDebounced, cleanup }; diff --git a/modules/novel-draw/cloud-presets.js b/modules/novel-draw/cloud-presets.js index cb22244..beb71f0 100644 --- a/modules/novel-draw/cloud-presets.js +++ b/modules/novel-draw/cloud-presets.js @@ -489,6 +489,8 @@ function createModal() { const overlay = document.createElement('div'); overlay.className = 'cloud-presets-overlay'; + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property overlay.innerHTML = `
@@ -584,6 +586,8 @@ function renderPage() { const start = (currentPage - 1) * ITEMS_PER_PAGE; const pageItems = filteredPresets.slice(start, start + ITEMS_PER_PAGE); + // Escaped fields are used in the template. + // eslint-disable-next-line no-unsanitized/property grid.innerHTML = pageItems.map(p => `
@@ -609,24 +613,34 @@ function renderPage() { btn.disabled = true; const origHtml = btn.innerHTML; + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property btn.innerHTML = ' 导入中'; try { const data = await downloadPreset(url); if (onImportCallback) await onImportCallback(data); btn.classList.add('success'); + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property btn.innerHTML = ' 成功'; setTimeout(() => { btn.classList.remove('success'); + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property btn.innerHTML = origHtml; btn.disabled = false; }, 2000); } catch (err) { console.error('[CloudPresets]', err); btn.classList.add('error'); + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property btn.innerHTML = ' 失败'; setTimeout(() => { btn.classList.remove('error'); + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property btn.innerHTML = origHtml; btn.disabled = false; }, 2000); diff --git a/modules/novel-draw/floating-panel.js b/modules/novel-draw/floating-panel.js index f9c37dd..fe273db 100644 --- a/modules/novel-draw/floating-panel.js +++ b/modules/novel-draw/floating-panel.js @@ -1,15 +1,18 @@ -// floating-panel.js +// floating-panel.js +/** + * NovelDraw 画图按钮面板 - 支持楼层按钮和悬浮按钮双模式 + */ -import { +import { openNovelDrawSettings, generateAndInsertImages, getSettings, saveSettings, - isModuleEnabled, findLastAIMessageId, classifyError, - ErrorType, + isGenerating, } from './novel-draw.js'; +import { registerToToolbar, removeFromToolbar } from '../../widgets/message-toolbar.js'; // ═══════════════════════════════════════════════════════════════════════════ // 常量 @@ -28,7 +31,6 @@ const FloatState = { ERROR: 'error', }; -// 尺寸预设 const SIZE_OPTIONS = [ { value: 'default', label: '跟随预设', width: null, height: null }, { value: '832x1216', label: '832 × 1216 竖图', width: 832, height: 1216 }, @@ -42,159 +44,101 @@ const SIZE_OPTIONS = [ // 状态 // ═══════════════════════════════════════════════════════════════════════════ -let floatEl = null; -let dragState = null; -let currentState = FloatState.IDLE; -let currentResult = { success: 0, total: 0, error: null, startTime: 0 }; -let autoResetTimer = null; -let cooldownRafId = null; -let cooldownEndTime = 0; +// 楼层按钮状态 +const panelMap = new Map(); +const pendingCallbacks = new Map(); +let floorObserver = null; -let $cache = {}; +// 悬浮按钮状态 +let floatingEl = null; +let floatingDragState = null; +let floatingState = FloatState.IDLE; +let floatingResult = { success: 0, total: 0, error: null, startTime: 0 }; +let floatingAutoResetTimer = null; +let floatingCooldownRafId = null; +let floatingCooldownEndTime = 0; +let $floatingCache = {}; -function cacheDOM() { - if (!floatEl) return; - $cache = { - capsule: floatEl.querySelector('.nd-capsule'), - statusIcon: floatEl.querySelector('#nd-status-icon'), - statusText: floatEl.querySelector('#nd-status-text'), - detailResult: floatEl.querySelector('#nd-detail-result'), - detailErrorRow: floatEl.querySelector('#nd-detail-error-row'), - detailError: floatEl.querySelector('#nd-detail-error'), - detailTime: floatEl.querySelector('#nd-detail-time'), - presetSelect: floatEl.querySelector('#nd-preset-select'), - sizeSelect: floatEl.querySelector('#nd-size-select'), - autoToggle: floatEl.querySelector('#nd-auto-toggle'), - }; -} +// 通用状态 +let stylesInjected = false; // ═══════════════════════════════════════════════════════════════════════════ -// 样式 - 精致简约 +// 样式 - 统一样式(楼层+悬浮共用) // ═══════════════════════════════════════════════════════════════════════════ const STYLES = ` -/* ═══════════════════════════════════════════════════════════════════════════ - 设计令牌 (Design Tokens) - ═══════════════════════════════════════════════════════════════════════════ */ :root { - /* 胶囊尺寸 */ - --nd-w: 74px; --nd-h: 34px; - - /* 颜色系统 */ + --nd-bg: rgba(0, 0, 0, 0.55); --nd-bg-solid: rgba(24, 24, 28, 0.98); - --nd-bg-card: rgba(0, 0, 0, 0.35); - --nd-bg-hover: rgba(255, 255, 255, 0.06); + --nd-bg-hover: rgba(0, 0, 0, 0.7); --nd-bg-active: rgba(255, 255, 255, 0.1); - - --nd-border-subtle: rgba(255, 255, 255, 0.08); - --nd-border-default: rgba(255, 255, 255, 0.12); + --nd-border: rgba(255, 255, 255, 0.08); --nd-border-hover: rgba(255, 255, 255, 0.2); - - --nd-text-primary: rgba(255, 255, 255, 0.92); + --nd-border-subtle: rgba(255, 255, 255, 0.08); + --nd-text-primary: rgba(255, 255, 255, 0.85); --nd-text-secondary: rgba(255, 255, 255, 0.65); - --nd-text-muted: rgba(255, 255, 255, 0.5); - - /* 语义色 */ - --nd-accent: #d4a574; + --nd-text-muted: rgba(255, 255, 255, 0.45); + --nd-text-dim: rgba(255, 255, 255, 0.25); --nd-success: #3ecf8e; --nd-warning: #f0b429; --nd-error: #f87171; --nd-info: #60a5fa; - - /* 阴影 */ - --nd-shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.25); - --nd-shadow-md: 0 4px 16px rgba(0, 0, 0, 0.35); - --nd-shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.5); - - /* 圆角 */ + --nd-shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.5); --nd-radius-sm: 6px; --nd-radius-md: 10px; --nd-radius-lg: 14px; - --nd-radius-full: 9999px; - - /* 过渡 */ - --nd-transition-fast: 0.15s ease; - --nd-transition-normal: 0.25s ease; } /* ═══════════════════════════════════════════════════════════════════════════ - 悬浮容器 + 楼层按钮样式 ═══════════════════════════════════════════════════════════════════════════ */ -.nd-float { - position: fixed; - z-index: 10000; +.nd-float { + position: relative; user-select: none; - will-change: transform; - contain: layout style; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; } -/* ═══════════════════════════════════════════════════════════════════════════ - 胶囊主体 - ═══════════════════════════════════════════════════════════════════════════ */ .nd-capsule { - width: var(--nd-w); + width: 74px; height: var(--nd-h); - background: var(--nd-bg-solid); - border: 1px solid var(--nd-border-default); + background: var(--nd-bg); + border: 1px solid var(--nd-border); border-radius: 17px; - box-shadow: var(--nd-shadow-md); + backdrop-filter: blur(16px); + -webkit-backdrop-filter: blur(16px); position: relative; overflow: hidden; - transition: border-color var(--nd-transition-normal), - box-shadow var(--nd-transition-normal), - background var(--nd-transition-normal); - touch-action: none; - cursor: grab; + transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1); } -.nd-capsule:active { cursor: grabbing; } - .nd-float:hover .nd-capsule { + background: var(--nd-bg-hover); border-color: var(--nd-border-hover); - box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45); } -/* 状态边框 */ -.nd-float.working .nd-capsule { - border-color: rgba(240, 180, 41, 0.5); -} -.nd-float.cooldown .nd-capsule { - border-color: rgba(96, 165, 250, 0.6); - background: rgba(96, 165, 250, 0.06); -} -.nd-float.success .nd-capsule { - border-color: rgba(62, 207, 142, 0.6); - background: rgba(62, 207, 142, 0.06); -} -.nd-float.partial .nd-capsule { - border-color: rgba(240, 180, 41, 0.6); - background: rgba(240, 180, 41, 0.06); -} -.nd-float.error .nd-capsule { - border-color: rgba(248, 113, 113, 0.6); - background: rgba(248, 113, 113, 0.06); +.nd-float.working .nd-capsule { border-color: rgba(240, 180, 41, 0.5); } +.nd-float.cooldown .nd-capsule { border-color: rgba(96, 165, 250, 0.6); background: rgba(96, 165, 250, 0.1); } +.nd-float.success .nd-capsule { border-color: rgba(62, 207, 142, 0.6); background: rgba(62, 207, 142, 0.1); } +.nd-float.partial .nd-capsule { border-color: rgba(240, 180, 41, 0.6); background: rgba(240, 180, 41, 0.1); } +.nd-float.error .nd-capsule { border-color: rgba(248, 113, 113, 0.6); background: rgba(248, 113, 113, 0.1); } + +.nd-inner { + display: grid; + width: 100%; + height: 100%; + grid-template-areas: "s"; + pointer-events: none; } -/* ═══════════════════════════════════════════════════════════════════════════ - 胶囊内层 - ═══════════════════════════════════════════════════════════════════════════ */ -.nd-inner { - display: grid; - width: 100%; - height: 100%; - grid-template-areas: "s"; - pointer-events: none; -} - -.nd-layer { - grid-area: s; - display: flex; - align-items: center; - width: 100%; - height: 100%; - transition: opacity 0.2s, transform 0.2s; - pointer-events: auto; +.nd-layer { + grid-area: s; + display: flex; + align-items: center; + width: 100%; + height: 100%; + transition: opacity 0.2s, transform 0.2s; + pointer-events: auto; } .nd-layer-idle { opacity: 1; transform: translateY(0); } @@ -204,12 +148,11 @@ const STYLES = ` .nd-float.success .nd-layer-idle, .nd-float.partial .nd-layer-idle, .nd-float.error .nd-layer-idle { - opacity: 0; - transform: translateY(-100%); + opacity: 0; + transform: translateY(-100%); pointer-events: none; } -/* 绘制按钮 */ .nd-btn-draw { flex: 1; height: 100%; @@ -221,13 +164,12 @@ const STYLES = ` justify-content: center; position: relative; color: var(--nd-text-primary); - transition: background var(--nd-transition-fast); + transition: background 0.15s; font-size: 16px; } -.nd-btn-draw:hover { background: var(--nd-bg-hover); } -.nd-btn-draw:active { background: var(--nd-bg-active); } +.nd-btn-draw:hover { background: rgba(255, 255, 255, 0.12); } +.nd-btn-draw:active { transform: scale(0.92); } -/* 自动模式指示点 */ .nd-auto-dot { position: absolute; top: 7px; @@ -241,21 +183,12 @@ const STYLES = ` transform: scale(0); transition: all 0.2s; } -.nd-float.auto-on .nd-auto-dot { - opacity: 1; - transform: scale(1); -} +.nd-float.auto-on .nd-auto-dot { opacity: 1; transform: scale(1); } -/* 分隔线 */ -.nd-sep { - width: 1px; - height: 14px; - background: var(--nd-border-subtle); -} +.nd-sep { width: 1px; height: 12px; background: var(--nd-border); } -/* 菜单按钮 */ .nd-btn-menu { - width: 28px; + width: 24px; height: 100%; border: none; background: transparent; @@ -263,21 +196,17 @@ const STYLES = ` display: flex; align-items: center; justify-content: center; - color: var(--nd-text-muted); + color: var(--nd-text-dim); font-size: 8px; - transition: all var(--nd-transition-fast); -} -.nd-btn-menu:hover { - background: var(--nd-bg-hover); - color: var(--nd-text-secondary); + opacity: 0.6; + transition: opacity 0.25s, transform 0.25s; } +.nd-float:hover .nd-btn-menu { opacity: 1; } +.nd-btn-menu:hover { background: rgba(255, 255, 255, 0.12); color: var(--nd-text-muted); } .nd-arrow { transition: transform 0.2s; } .nd-float.expanded .nd-arrow { transform: rotate(180deg); } -/* ═══════════════════════════════════════════════════════════════════════════ - 工作状态层 - ═══════════════════════════════════════════════════════════════════════════ */ .nd-layer-active { opacity: 0; transform: translateY(100%); @@ -305,69 +234,42 @@ const STYLES = ` .nd-float.partial .nd-layer-active { color: var(--nd-warning); } .nd-float.error .nd-layer-active { color: var(--nd-error); } -.nd-spin { - display: inline-block; - animation: nd-spin 1.5s linear infinite; - will-change: transform; -} +.nd-spin { display: inline-block; animation: nd-spin 1.5s linear infinite; } @keyframes nd-spin { to { transform: rotate(360deg); } } -.nd-countdown { - font-variant-numeric: tabular-nums; - min-width: 36px; - text-align: center; -} +.nd-countdown { font-variant-numeric: tabular-nums; min-width: 36px; text-align: center; } -/* ═══════════════════════════════════════════════════════════════════════════ - 详情气泡 - ═══════════════════════════════════════════════════════════════════════════ */ +/* 详情弹窗 - 向下展开(楼层按钮用) */ .nd-detail { position: absolute; - bottom: calc(100% + 10px); - left: 50%; - transform: translateX(-50%) translateY(4px); - background: var(--nd-bg-solid); - border: 1px solid var(--nd-border-default); - border-radius: var(--nd-radius-md); + top: calc(100% + 8px); + right: 0; + background: rgba(18, 18, 22, 0.98); + border: 1px solid var(--nd-border); + border-radius: 12px; padding: 12px 16px; font-size: 12px; color: var(--nd-text-secondary); white-space: nowrap; box-shadow: var(--nd-shadow-lg); + backdrop-filter: blur(20px); + -webkit-backdrop-filter: blur(20px); opacity: 0; visibility: hidden; - transition: opacity var(--nd-transition-fast), transform var(--nd-transition-fast); - z-index: 10; -} - -.nd-detail::after { - content: ''; - position: absolute; - bottom: -6px; - left: 50%; - transform: translateX(-50%); - border: 6px solid transparent; - border-top-color: var(--nd-bg-solid); + transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); + z-index: 100; + transform: translateY(-6px) scale(0.96); + transform-origin: top right; } .nd-float.show-detail .nd-detail { opacity: 1; visibility: visible; - transform: translateX(-50%) translateY(0); -} - -.nd-detail-row { - display: flex; - align-items: center; - gap: 10px; - padding: 3px 0; -} -.nd-detail-row + .nd-detail-row { - margin-top: 6px; - padding-top: 8px; - border-top: 1px solid var(--nd-border-subtle); + transform: translateY(0) scale(1); } +.nd-detail-row { display: flex; align-items: center; gap: 10px; padding: 3px 0; } +.nd-detail-row + .nd-detail-row { margin-top: 6px; padding-top: 8px; border-top: 1px solid var(--nd-border-subtle); } .nd-detail-icon { opacity: 0.6; font-size: 13px; } .nd-detail-label { color: var(--nd-text-muted); } .nd-detail-value { margin-left: auto; font-weight: 600; color: var(--nd-text-primary); } @@ -375,27 +277,25 @@ const STYLES = ` .nd-detail-value.warning { color: var(--nd-warning); } .nd-detail-value.error { color: var(--nd-error); } -/* ═══════════════════════════════════════════════════════════════════════════ - 菜单面板 - 核心重构 - ═══════════════════════════════════════════════════════════════════════════ */ +/* 菜单 - 向下展开(楼层按钮用) */ .nd-menu { position: absolute; - bottom: calc(100% + 10px); + top: calc(100% + 8px); right: 0; width: 190px; - background: var(--nd-bg-solid); - border: 1px solid var(--nd-border-default); - border-radius: var(--nd-radius-lg); + background: rgba(18, 18, 22, 0.96); + border: 1px solid var(--nd-border); + border-radius: 12px; padding: 10px; box-shadow: var(--nd-shadow-lg); + backdrop-filter: blur(20px); + -webkit-backdrop-filter: blur(20px); opacity: 0; visibility: hidden; - transform: translateY(6px) scale(0.98); - transform-origin: bottom right; - transition: opacity var(--nd-transition-fast), - transform 0.15s cubic-bezier(0.34, 1.56, 0.64, 1), - visibility var(--nd-transition-fast); - z-index: 5; + transform: translateY(-6px) scale(0.96); + transform-origin: top right; + transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); + z-index: 100; } .nd-float.expanded .nd-menu { @@ -404,92 +304,60 @@ const STYLES = ` transform: translateY(0) scale(1); } -/* ═══════════════════════════════════════════════════════════════════════════ - 参数卡片 - ═══════════════════════════════════════════════════════════════════════════ */ .nd-card { - background: var(--nd-bg-card); - border: 1px solid var(--nd-border-subtle); - border-radius: var(--nd-radius-md); - overflow: hidden; - box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.02); + background: transparent; + border: none; + border-radius: 0; + overflow: visible; } .nd-row { display: flex; align-items: center; - padding: 2px 0; + gap: 10px; + padding: 6px 2px; + min-height: 36px; } -/* 标签 - 提升可读性 */ .nd-label { - width: 36px; - padding-left: 10px; - font-size: 10px; - font-weight: 500; + font-size: 11px; color: var(--nd-text-muted); - letter-spacing: 0.2px; + width: 32px; flex-shrink: 0; + padding: 0; } -/* 选择框 - 统一风格 */ .nd-select { flex: 1; min-width: 0; - border: none; - background: transparent; + background: rgba(255, 255, 255, 0.06); + border: 1px solid var(--nd-border-subtle); color: var(--nd-text-primary); - font-size: 12px; - padding: 10px 8px; + font-size: 11px; + min-height: 32px; + border-radius: 6px; + padding: 6px 8px; + margin: 0; + box-sizing: border-box; outline: none; cursor: pointer; - transition: color var(--nd-transition-fast); text-align: center; text-align-last: center; - margin: 0; - line-height: 1.2; + transition: border-color 0.2s; + vertical-align: middle; + -webkit-appearance: none; + -moz-appearance: none; + appearance: none; } +.nd-select:hover { border-color: rgba(255, 255, 255, 0.2); } +.nd-select:focus { border-color: rgba(255, 255, 255, 0.3); } +.nd-select option { background: #1a1a1e; color: #eee; text-align: left; } +.nd-select.size { font-family: "SF Mono", "Menlo", "Consolas", monospace; font-size: 11px; } -.nd-select:hover { color: #fff; } -.nd-select:focus { color: #fff; } +.nd-inner-sep { display: none; } -.nd-select option { - background: #1a1a1e; - color: #eee; - padding: 8px; - text-align: left; -} +.nd-controls { display: flex; align-items: center; gap: 8px; margin-top: 10px; } -/* 尺寸选择框 - 等宽字体,白色文字 */ -.nd-select.size { - font-family: "SF Mono", "Menlo", "Consolas", "Liberation Mono", monospace; - font-size: 11px; - letter-spacing: -0.2px; -} - -/* 内部分隔线 */ -.nd-inner-sep { - height: 1px; - background: linear-gradient( - 90deg, - transparent 8px, - var(--nd-border-subtle) 8px, - var(--nd-border-subtle) calc(100% - 8px), - transparent calc(100% - 8px) - ); -} - -/* ═══════════════════════════════════════════════════════════════════════════ - 控制栏 - ═══════════════════════════════════════════════════════════════════════════ */ -.nd-controls { - display: flex; - align-items: center; - gap: 8px; - margin-top: 10px; -} - -/* 自动开关 */ .nd-auto { flex: 1; display: flex; @@ -500,18 +368,10 @@ const STYLES = ` border: 1px solid var(--nd-border-subtle); border-radius: var(--nd-radius-sm); cursor: pointer; - transition: all var(--nd-transition-fast); -} - -.nd-auto:hover { - background: var(--nd-bg-hover); - border-color: var(--nd-border-default); -} - -.nd-auto.on { - background: rgba(62, 207, 142, 0.08); - border-color: rgba(62, 207, 142, 0.3); + transition: all 0.15s; } +.nd-auto:hover { background: rgba(255, 255, 255, 0.08); } +.nd-auto.on { background: rgba(62, 207, 142, 0.08); border-color: rgba(62, 207, 142, 0.3); } .nd-dot { width: 7px; @@ -519,29 +379,13 @@ const STYLES = ` border-radius: 50%; background: rgba(255, 255, 255, 0.2); transition: all 0.2s; - flex-shrink: 0; } +.nd-auto.on .nd-dot { background: var(--nd-success); box-shadow: 0 0 8px rgba(62, 207, 142, 0.5); } -.nd-auto.on .nd-dot { - background: var(--nd-success); - box-shadow: 0 0 8px rgba(62, 207, 142, 0.5); -} +.nd-auto-text { font-size: 12px; color: var(--nd-text-muted); } +.nd-auto:hover .nd-auto-text { color: var(--nd-text-secondary); } +.nd-auto.on .nd-auto-text { color: rgba(62, 207, 142, 0.95); } -.nd-auto-text { - font-size: 12px; - color: var(--nd-text-muted); - transition: color var(--nd-transition-fast); -} - -.nd-auto:hover .nd-auto-text { - color: var(--nd-text-secondary); -} - -.nd-auto.on .nd-auto-text { - color: rgba(62, 207, 142, 0.95); -} - -/* 设置按钮 */ .nd-gear { width: 36px; height: 36px; @@ -554,23 +398,73 @@ const STYLES = ` align-items: center; justify-content: center; font-size: 14px; - transition: all var(--nd-transition-fast); - flex-shrink: 0; + transition: all 0.15s; +} +.nd-gear:hover { background: rgba(255, 255, 255, 0.08); color: var(--nd-text-secondary); } + +/* ═══════════════════════════════════════════════════════════════════════════ + 悬浮按钮样式(固定定位,可拖拽) + ═══════════════════════════════════════════════════════════════════════════ */ +.nd-floating-global { + position: fixed; + z-index: 10000; + user-select: none; + will-change: transform; } -.nd-gear:hover { - background: var(--nd-bg-hover); - border-color: var(--nd-border-default); - color: var(--nd-text-secondary); +.nd-floating-global .nd-capsule { + background: var(--nd-bg-solid); + box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35); + touch-action: none; + cursor: grab; } -.nd-gear:active { - background: var(--nd-bg-active); +.nd-floating-global .nd-capsule:active { cursor: grabbing; } + +/* 悬浮按钮的详情和菜单向上展开 */ +.nd-floating-global .nd-detail { + top: auto; + bottom: calc(100% + 10px); + transform: translateY(4px) scale(0.96); + transform-origin: bottom right; } + +.nd-floating-global.show-detail .nd-detail { + transform: translateY(0) scale(1); +} + +.nd-floating-global .nd-detail::after { + content: ''; + position: absolute; + top: auto; + bottom: -6px; + left: 50%; + transform: translateX(-50%); + border: 6px solid transparent; + border-top-color: rgba(18, 18, 22, 0.98); + border-bottom-color: transparent; +} + +.nd-floating-global .nd-menu { + top: auto; + bottom: calc(100% + 10px); + transform: translateY(6px) scale(0.98); + transform-origin: bottom right; +} + +.nd-floating-global.expanded .nd-menu { + transform: translateY(0) scale(1); +} + +/* 悬浮按钮箭头向上 */ +.nd-floating-global .nd-arrow { transform: rotate(180deg); } +.nd-floating-global.expanded .nd-arrow { transform: rotate(0deg); } `; function injectStyles() { - if (document.getElementById('nd-float-styles')) return; + if (stylesInjected) return; + stylesInjected = true; + const el = document.createElement('style'); el.id = 'nd-float-styles'; el.textContent = STYLES; @@ -578,15 +472,535 @@ function injectStyles() { } // ═══════════════════════════════════════════════════════════════════════════ -// 位置管理 +// 通用工具函数 // ═══════════════════════════════════════════════════════════════════════════ -function getPosition() { +function createEl(tag, className, text) { + const el = document.createElement(tag); + if (className) el.className = className; + if (text !== undefined) el.textContent = text; + return el; +} + +function fillPresetSelect(selectEl) { + if (!selectEl) return; + const settings = getSettings(); + const presets = settings.paramsPresets || []; + const currentId = settings.selectedParamsPresetId; + selectEl.replaceChildren(); + presets.forEach(p => { + const opt = document.createElement('option'); + opt.value = p.id; + opt.textContent = p.name || '未命名'; + if (p.id === currentId) opt.selected = true; + selectEl.appendChild(opt); + }); +} + +function fillSizeSelect(selectEl) { + if (!selectEl) return; + const settings = getSettings(); + const current = settings.overrideSize || 'default'; + selectEl.replaceChildren(); + SIZE_OPTIONS.forEach(opt => { + const option = document.createElement('option'); + option.value = opt.value; + option.textContent = opt.label; + if (opt.value === current) option.selected = true; + selectEl.appendChild(option); + }); +} + +// ═══════════════════════════════════════════════════════════════════════════ +// ▼▼▼ 楼层按钮逻辑 ▼▼▼ +// ═══════════════════════════════════════════════════════════════════════════ + +function createFloorPanelData(messageId) { + return { + messageId, + root: null, + state: FloatState.IDLE, + result: { success: 0, total: 0, error: null, startTime: 0 }, + autoResetTimer: null, + cooldownRafId: null, + cooldownEndTime: 0, + $cache: {}, + _cleanup: null, + }; +} + +function createFloorPanelElement(messageId) { + const settings = getSettings(); + const isAuto = settings.mode === 'auto'; + + const root = document.createElement('div'); + root.className = `nd-float${isAuto ? ' auto-on' : ''}`; + root.dataset.messageId = messageId; + + const capsule = createEl('div', 'nd-capsule'); + const inner = createEl('div', 'nd-inner'); + const layerIdle = createEl('div', 'nd-layer nd-layer-idle'); + const drawBtn = createEl('button', 'nd-btn-draw'); + drawBtn.title = '点击生成配图'; + drawBtn.appendChild(createEl('span', '', '🎨')); + drawBtn.appendChild(createEl('span', 'nd-auto-dot')); + const sep = createEl('div', 'nd-sep'); + const menuBtn = createEl('button', 'nd-btn-menu'); + menuBtn.title = '展开菜单'; + menuBtn.appendChild(createEl('span', 'nd-arrow', '▼')); + layerIdle.append(drawBtn, sep, menuBtn); + + const layerActive = createEl('div', 'nd-layer nd-layer-active'); + layerActive.append( + createEl('span', 'nd-status-icon', '⏳'), + createEl('span', 'nd-status-text', '分析') + ); + + inner.append(layerIdle, layerActive); + capsule.appendChild(inner); + + const detail = createEl('div', 'nd-detail'); + const detailRowResult = createEl('div', 'nd-detail-row'); + detailRowResult.append( + createEl('span', 'nd-detail-icon', '📊'), + createEl('span', 'nd-detail-label', '结果'), + createEl('span', 'nd-detail-value nd-result', '-') + ); + const detailRowError = createEl('div', 'nd-detail-row nd-error-row'); + detailRowError.style.display = 'none'; + detailRowError.append( + createEl('span', 'nd-detail-icon', '💡'), + createEl('span', 'nd-detail-label', '原因'), + createEl('span', 'nd-detail-value error nd-error', '-') + ); + const detailRowTime = createEl('div', 'nd-detail-row'); + detailRowTime.append( + createEl('span', 'nd-detail-icon', '⏱'), + createEl('span', 'nd-detail-label', '耗时'), + createEl('span', 'nd-detail-value nd-time', '-') + ); + detail.append(detailRowResult, detailRowError, detailRowTime); + + const menu = createEl('div', 'nd-menu'); + const card = createEl('div', 'nd-card'); + const rowPreset = createEl('div', 'nd-row'); + rowPreset.appendChild(createEl('span', 'nd-label', '预设')); + const presetSelect = createEl('select', 'nd-select nd-preset-select'); + fillPresetSelect(presetSelect); + rowPreset.appendChild(presetSelect); + const innerSep = createEl('div', 'nd-inner-sep'); + const rowSize = createEl('div', 'nd-row'); + rowSize.appendChild(createEl('span', 'nd-label', '尺寸')); + const sizeSelect = createEl('select', 'nd-select size nd-size-select'); + fillSizeSelect(sizeSelect); + rowSize.appendChild(sizeSelect); + card.append(rowPreset, innerSep, rowSize); + + const controls = createEl('div', 'nd-controls'); + const autoToggle = createEl('div', `nd-auto${isAuto ? ' on' : ''} nd-auto-toggle`); + autoToggle.append( + createEl('span', 'nd-dot'), + createEl('span', 'nd-auto-text', '自动配图') + ); + const settingsBtn = createEl('button', 'nd-gear nd-settings-btn', '⚙'); + settingsBtn.title = '打开设置'; + controls.append(autoToggle, settingsBtn); + + menu.append(card, controls); + + root.append(capsule, detail, menu); + return root; +} + +function cacheFloorDOM(panelData) { + const el = panelData.root; + if (!el) return; + + panelData.$cache = { + statusIcon: el.querySelector('.nd-status-icon'), + statusText: el.querySelector('.nd-status-text'), + result: el.querySelector('.nd-result'), + errorRow: el.querySelector('.nd-error-row'), + error: el.querySelector('.nd-error'), + time: el.querySelector('.nd-time'), + presetSelect: el.querySelector('.nd-preset-select'), + sizeSelect: el.querySelector('.nd-size-select'), + autoToggle: el.querySelector('.nd-auto-toggle'), + }; +} + +function setFloorState(messageId, state, data = {}) { + const panelData = panelMap.get(messageId); + if (!panelData?.root) return; + + const el = panelData.root; + panelData.state = state; + + if (panelData.autoResetTimer) { + clearTimeout(panelData.autoResetTimer); + panelData.autoResetTimer = null; + } + if (state !== FloatState.COOLDOWN && panelData.cooldownRafId) { + cancelAnimationFrame(panelData.cooldownRafId); + panelData.cooldownRafId = null; + panelData.cooldownEndTime = 0; + } + + el.classList.remove('working', 'cooldown', 'success', 'partial', 'error', 'show-detail'); + + const { statusIcon, statusText } = panelData.$cache; + + switch (state) { + case FloatState.IDLE: + panelData.result = { success: 0, total: 0, error: null, startTime: 0 }; + break; + case FloatState.LLM: + el.classList.add('working'); + panelData.result.startTime = Date.now(); + if (statusIcon) { statusIcon.textContent = '⏳'; statusIcon.className = 'nd-status-icon nd-spin'; } + if (statusText) statusText.textContent = '分析'; + break; + case FloatState.GEN: + el.classList.add('working'); + if (statusIcon) { statusIcon.textContent = '🎨'; statusIcon.className = 'nd-status-icon nd-spin'; } + if (statusText) statusText.textContent = `${data.current || 0}/${data.total || 0}`; + panelData.result.total = data.total || 0; + break; + case FloatState.COOLDOWN: + el.classList.add('cooldown'); + if (statusIcon) { statusIcon.textContent = '⏳'; statusIcon.className = 'nd-status-icon nd-spin'; } + startFloorCooldownTimer(panelData, data.duration); + break; + case FloatState.SUCCESS: + el.classList.add('success'); + if (statusIcon) { statusIcon.textContent = '✓'; statusIcon.className = 'nd-status-icon'; } + if (statusText) statusText.textContent = `${data.success}/${data.total}`; + panelData.result.success = data.success; + panelData.result.total = data.total; + panelData.autoResetTimer = setTimeout(() => setFloorState(messageId, FloatState.IDLE), AUTO_RESET_DELAY); + break; + case FloatState.PARTIAL: + el.classList.add('partial'); + if (statusIcon) { statusIcon.textContent = '⚠'; statusIcon.className = 'nd-status-icon'; } + if (statusText) statusText.textContent = `${data.success}/${data.total}`; + panelData.result.success = data.success; + panelData.result.total = data.total; + panelData.autoResetTimer = setTimeout(() => setFloorState(messageId, FloatState.IDLE), AUTO_RESET_DELAY); + break; + case FloatState.ERROR: + el.classList.add('error'); + if (statusIcon) { statusIcon.textContent = '✗'; statusIcon.className = 'nd-status-icon'; } + if (statusText) statusText.textContent = data.error?.label || '错误'; + panelData.result.error = data.error; + panelData.autoResetTimer = setTimeout(() => setFloorState(messageId, FloatState.IDLE), AUTO_RESET_DELAY); + break; + } +} + +function startFloorCooldownTimer(panelData, duration) { + panelData.cooldownEndTime = Date.now() + duration; + + function tick() { + if (!panelData.cooldownEndTime) return; + const remaining = Math.max(0, panelData.cooldownEndTime - Date.now()); + const statusText = panelData.$cache?.statusText; + if (statusText) { + statusText.textContent = `${(remaining / 1000).toFixed(1)}s`; + statusText.className = 'nd-status-text nd-countdown'; + } + if (remaining <= 0) { + panelData.cooldownRafId = null; + panelData.cooldownEndTime = 0; + return; + } + panelData.cooldownRafId = requestAnimationFrame(tick); + } + + panelData.cooldownRafId = requestAnimationFrame(tick); +} + +function updateFloorDetailPopup(messageId) { + const panelData = panelMap.get(messageId); + if (!panelData?.root) return; + + const { result: resultEl, errorRow, error: errorEl, time: timeEl } = panelData.$cache; + const { result, state } = panelData; + + const elapsed = result.startTime + ? ((Date.now() - result.startTime) / 1000).toFixed(1) + : '-'; + + if (state === FloatState.SUCCESS || state === FloatState.PARTIAL) { + if (resultEl) { + resultEl.textContent = `${result.success}/${result.total} 成功`; + resultEl.className = `nd-detail-value ${state === FloatState.SUCCESS ? 'success' : 'warning'}`; + } + if (errorRow) errorRow.style.display = state === FloatState.PARTIAL ? 'flex' : 'none'; + if (errorEl && state === FloatState.PARTIAL) { + errorEl.textContent = `${result.total - result.success} 张失败`; + } + } else if (state === FloatState.ERROR) { + if (resultEl) { + resultEl.textContent = '生成失败'; + resultEl.className = 'nd-detail-value error'; + } + if (errorRow) errorRow.style.display = 'flex'; + if (errorEl) errorEl.textContent = result.error?.desc || '未知错误'; + } + + if (timeEl) timeEl.textContent = `${elapsed}s`; +} + +async function handleFloorDrawClick(messageId) { + const panelData = panelMap.get(messageId); + if (!panelData || panelData.state !== FloatState.IDLE) return; + + if (isGenerating()) { + toastr?.info?.('已有任务进行中,请等待完成'); + return; + } + + try { + await generateAndInsertImages({ + messageId, + onStateChange: (state, data) => { + switch (state) { + case 'llm': setFloorState(messageId, FloatState.LLM); break; + case 'gen': setFloorState(messageId, FloatState.GEN, data); break; + case 'progress': setFloorState(messageId, FloatState.GEN, data); break; + case 'cooldown': setFloorState(messageId, FloatState.COOLDOWN, data); break; + case 'success': + if (data.aborted && data.success === 0) { + setFloorState(messageId, FloatState.IDLE); + } else if (data.aborted || data.success < data.total) { + setFloorState(messageId, FloatState.PARTIAL, data); + } else { + setFloorState(messageId, FloatState.SUCCESS, data); + } + break; + } + } + }); + } catch (e) { + console.error('[NovelDraw]', e); + if (e.message === '已取消' || e.message?.includes('已有任务进行中')) { + setFloorState(messageId, FloatState.IDLE); + if (e.message?.includes('已有任务进行中')) toastr?.info?.(e.message); + } else { + setFloorState(messageId, FloatState.ERROR, { error: classifyError(e) }); + } + } +} + +async function handleFloorAbort(messageId) { + try { + const { abortGeneration } = await import('./novel-draw.js'); + if (abortGeneration()) { + setFloorState(messageId, FloatState.IDLE); + toastr?.info?.('已中止'); + } + } catch (e) { + console.error('[NovelDraw] 中止失败:', e); + } +} + +function bindFloorPanelEvents(panelData) { + const { messageId, root: el } = panelData; + + el.querySelector('.nd-btn-draw')?.addEventListener('click', (e) => { + e.stopPropagation(); + handleFloorDrawClick(messageId); + }); + + el.querySelector('.nd-btn-menu')?.addEventListener('click', (e) => { + e.stopPropagation(); + el.classList.remove('show-detail'); + if (!el.classList.contains('expanded')) { + refreshFloorPresetSelect(messageId); + refreshFloorSizeSelect(messageId); + } + el.classList.toggle('expanded'); + }); + + el.querySelector('.nd-layer-active')?.addEventListener('click', (e) => { + e.stopPropagation(); + const state = panelData.state; + if ([FloatState.LLM, FloatState.GEN, FloatState.COOLDOWN].includes(state)) { + handleFloorAbort(messageId); + } else if ([FloatState.SUCCESS, FloatState.PARTIAL, FloatState.ERROR].includes(state)) { + updateFloorDetailPopup(messageId); + el.classList.toggle('show-detail'); + } + }); + + panelData.$cache.presetSelect?.addEventListener('change', (e) => { + const settings = getSettings(); + settings.selectedParamsPresetId = e.target.value; + saveSettings(settings); + updateAllPresetSelects(); + }); + + panelData.$cache.sizeSelect?.addEventListener('change', (e) => { + const settings = getSettings(); + settings.overrideSize = e.target.value; + saveSettings(settings); + updateAllSizeSelects(); + }); + + panelData.$cache.autoToggle?.addEventListener('click', () => { + const settings = getSettings(); + settings.mode = settings.mode === 'auto' ? 'manual' : 'auto'; + saveSettings(settings); + updateAutoModeUI(); + }); + + el.querySelector('.nd-settings-btn')?.addEventListener('click', (e) => { + e.stopPropagation(); + el.classList.remove('expanded'); + openNovelDrawSettings(); + }); + + const closeMenu = (e) => { + if (!el.contains(e.target)) { + el.classList.remove('expanded', 'show-detail'); + } + }; + document.addEventListener('click', closeMenu, { passive: true }); + + panelData._cleanup = () => { + document.removeEventListener('click', closeMenu); + }; +} + +function refreshFloorPresetSelect(messageId) { + const data = panelMap.get(messageId); + const select = data?.$cache?.presetSelect; + fillPresetSelect(select); +} + +function refreshFloorSizeSelect(messageId) { + const data = panelMap.get(messageId); + const select = data?.$cache?.sizeSelect; + fillSizeSelect(select); +} + +function mountFloorPanel(messageEl, messageId) { + if (panelMap.has(messageId)) { + const existing = panelMap.get(messageId); + if (existing.root?.isConnected) return existing; + existing._cleanup?.(); + panelMap.delete(messageId); + } + + injectStyles(); + + const panelData = createFloorPanelData(messageId); + const panel = createFloorPanelElement(messageId); + panelData.root = panel; + + const success = registerToToolbar(messageId, panel, { + position: 'right', + id: `novel-draw-${messageId}` + }); + + if (!success) return null; + + cacheFloorDOM(panelData); + bindFloorPanelEvents(panelData); + + panelMap.set(messageId, panelData); + return panelData; +} + +function setupFloorObserver() { + if (floorObserver) return; + + floorObserver = new IntersectionObserver((entries) => { + const toMount = []; + + for (const entry of entries) { + if (!entry.isIntersecting) continue; + + const el = entry.target; + const mid = Number(el.getAttribute('mesid')); + + if (pendingCallbacks.has(mid)) { + toMount.push({ el, mid }); + pendingCallbacks.delete(mid); + floorObserver.unobserve(el); + } + } + + if (toMount.length > 0) { + requestAnimationFrame(() => { + for (const { el, mid } of toMount) { + mountFloorPanel(el, mid); + } + }); + } + }, { rootMargin: '300px' }); +} + +export function ensureNovelDrawPanel(messageEl, messageId, options = {}) { + const settings = getSettings(); + if (settings.showFloorButton === false) return null; + + const { force = false } = options; + + injectStyles(); + + if (panelMap.has(messageId)) { + const existing = panelMap.get(messageId); + if (existing.root?.isConnected) return existing; + existing._cleanup?.(); + panelMap.delete(messageId); + } + + if (force) { + return mountFloorPanel(messageEl, messageId); + } + + const rect = messageEl.getBoundingClientRect(); + if (rect.top < window.innerHeight + 500 && rect.bottom > -500) { + return mountFloorPanel(messageEl, messageId); + } + + setupFloorObserver(); + pendingCallbacks.set(messageId, true); + floorObserver.observe(messageEl); + + return null; +} + +export function setStateForMessage(messageId, state, data = {}) { + let panelData = panelMap.get(messageId); + + if (!panelData?.root?.isConnected) { + const messageEl = document.querySelector(`.mes[mesid="${messageId}"]`); + if (messageEl) { + panelData = ensureNovelDrawPanel(messageEl, messageId, { force: true }); + } + } + + if (panelData) { + setFloorState(messageId, state, data); + } + + if (floatingEl && messageId === findLastAIMessageId()) { + setFloatingState(state, data); + } +} + +// ═══════════════════════════════════════════════════════════════════════════ +// ▼▼▼ 悬浮按钮逻辑 ▼▼▼ +// ═══════════════════════════════════════════════════════════════════════════ + +function getFloatingPosition() { try { const raw = localStorage.getItem(FLOAT_POS_KEY); if (raw) return JSON.parse(raw); } catch {} - + const debug = document.getElementById('xiaobaix-debug-mini'); if (debug) { const r = debug.getBoundingClientRect(); @@ -595,276 +1009,255 @@ function getPosition() { return { left: window.innerWidth - 110, top: window.innerHeight - 80 }; } -function savePosition() { - if (!floatEl) return; - const r = floatEl.getBoundingClientRect(); +function saveFloatingPosition() { + if (!floatingEl) return; + const r = floatingEl.getBoundingClientRect(); try { - localStorage.setItem(FLOAT_POS_KEY, JSON.stringify({ - left: Math.round(r.left), - top: Math.round(r.top) + localStorage.setItem(FLOAT_POS_KEY, JSON.stringify({ + left: Math.round(r.left), + top: Math.round(r.top) })); } catch {} } -function applyPosition() { - if (!floatEl) return; - const pos = getPosition(); - const w = floatEl.offsetWidth || 77; - const h = floatEl.offsetHeight || 34; - floatEl.style.left = `${Math.max(0, Math.min(pos.left, window.innerWidth - w))}px`; - floatEl.style.top = `${Math.max(0, Math.min(pos.top, window.innerHeight - h))}px`; +function applyFloatingPosition() { + if (!floatingEl) return; + const pos = getFloatingPosition(); + const w = floatingEl.offsetWidth || 77; + const h = floatingEl.offsetHeight || 34; + floatingEl.style.left = `${Math.max(0, Math.min(pos.left, window.innerWidth - w))}px`; + floatingEl.style.top = `${Math.max(0, Math.min(pos.top, window.innerHeight - h))}px`; } -// ═══════════════════════════════════════════════════════════════════════════ -// 倒计时 -// ═══════════════════════════════════════════════════════════════════════════ - -function clearCooldownTimer() { - if (cooldownRafId) { - cancelAnimationFrame(cooldownRafId); - cooldownRafId = null; +function clearFloatingCooldownTimer() { + if (floatingCooldownRafId) { + cancelAnimationFrame(floatingCooldownRafId); + floatingCooldownRafId = null; } - cooldownEndTime = 0; + floatingCooldownEndTime = 0; } -function startCooldownTimer(duration) { - clearCooldownTimer(); - cooldownEndTime = Date.now() + duration; - +function startFloatingCooldownTimer(duration) { + clearFloatingCooldownTimer(); + floatingCooldownEndTime = Date.now() + duration; + function tick() { - if (!cooldownEndTime) return; - updateCooldownDisplay(); - const remaining = cooldownEndTime - Date.now(); - if (remaining <= -100) { - clearCooldownTimer(); + if (!floatingCooldownEndTime) return; + const remaining = Math.max(0, floatingCooldownEndTime - Date.now()); + const statusText = $floatingCache.statusText; + if (statusText) { + statusText.textContent = `${(remaining / 1000).toFixed(1)}s`; + statusText.className = 'nd-status-text nd-countdown'; + } + if (remaining <= 0) { + clearFloatingCooldownTimer(); return; } - cooldownRafId = requestAnimationFrame(tick); + floatingCooldownRafId = requestAnimationFrame(tick); } - - cooldownRafId = requestAnimationFrame(tick); + + floatingCooldownRafId = requestAnimationFrame(tick); } -function updateCooldownDisplay() { - const { statusText } = $cache; - if (!statusText) return; - const remaining = Math.max(0, cooldownEndTime - Date.now()); - const seconds = (remaining / 1000).toFixed(1); - statusText.textContent = `${seconds}s`; - statusText.className = 'nd-countdown'; -} +function setFloatingState(state, data = {}) { + if (!floatingEl) return; -// ═══════════════════════════════════════════════════════════════════════════ -// 状态管理 -// ═══════════════════════════════════════════════════════════════════════════ + floatingState = state; -const STATE_CONFIG = { - [FloatState.IDLE]: { cls: '', icon: '', text: '', spinning: false }, - [FloatState.LLM]: { cls: 'working', icon: '⏳', text: '分析', spinning: true }, - [FloatState.GEN]: { cls: 'working', icon: '🎨', text: '', spinning: true }, - [FloatState.COOLDOWN]: { cls: 'cooldown', icon: '⏳', text: '', spinning: true }, - [FloatState.SUCCESS]: { cls: 'success', icon: '✓', text: '', spinning: false }, - [FloatState.PARTIAL]: { cls: 'partial', icon: '⚠', text: '', spinning: false }, - [FloatState.ERROR]: { cls: 'error', icon: '✗', text: '', spinning: false }, -}; - -function setState(state, data = {}) { - if (!floatEl) return; - - currentState = state; - - if (autoResetTimer) { - clearTimeout(autoResetTimer); - autoResetTimer = null; + if (floatingAutoResetTimer) { + clearTimeout(floatingAutoResetTimer); + floatingAutoResetTimer = null; } - + if (state !== FloatState.COOLDOWN) { - clearCooldownTimer(); + clearFloatingCooldownTimer(); } - - floatEl.classList.remove('working', 'cooldown', 'success', 'partial', 'error', 'show-detail'); - - const cfg = STATE_CONFIG[state]; - if (cfg.cls) floatEl.classList.add(cfg.cls); - - const { statusIcon, statusText } = $cache; + + floatingEl.classList.remove('working', 'cooldown', 'success', 'partial', 'error', 'show-detail'); + + const { statusIcon, statusText } = $floatingCache; if (!statusIcon || !statusText) return; - - statusIcon.textContent = cfg.icon; - statusIcon.className = cfg.spinning ? 'nd-spin' : ''; - statusText.className = ''; - + switch (state) { case FloatState.IDLE: - currentResult = { success: 0, total: 0, error: null, startTime: 0 }; + floatingResult = { success: 0, total: 0, error: null, startTime: 0 }; break; case FloatState.LLM: - currentResult.startTime = Date.now(); - statusText.textContent = cfg.text; + floatingEl.classList.add('working'); + floatingResult.startTime = Date.now(); + statusIcon.textContent = '⏳'; + statusIcon.className = 'nd-status-icon nd-spin'; + statusText.textContent = '分析'; break; case FloatState.GEN: + floatingEl.classList.add('working'); + statusIcon.textContent = '🎨'; + statusIcon.className = 'nd-status-icon nd-spin'; statusText.textContent = `${data.current || 0}/${data.total || 0}`; - currentResult.total = data.total || 0; + floatingResult.total = data.total || 0; break; case FloatState.COOLDOWN: - startCooldownTimer(data.duration); + floatingEl.classList.add('cooldown'); + statusIcon.textContent = '⏳'; + statusIcon.className = 'nd-status-icon nd-spin'; + startFloatingCooldownTimer(data.duration); break; case FloatState.SUCCESS: - case FloatState.PARTIAL: + floatingEl.classList.add('success'); + statusIcon.textContent = '✓'; + statusIcon.className = 'nd-status-icon'; statusText.textContent = `${data.success}/${data.total}`; - currentResult.success = data.success; - currentResult.total = data.total; - autoResetTimer = setTimeout(() => setState(FloatState.IDLE), AUTO_RESET_DELAY); + floatingResult.success = data.success; + floatingResult.total = data.total; + floatingAutoResetTimer = setTimeout(() => setFloatingState(FloatState.IDLE), AUTO_RESET_DELAY); + break; + case FloatState.PARTIAL: + floatingEl.classList.add('partial'); + statusIcon.textContent = '⚠'; + statusIcon.className = 'nd-status-icon'; + statusText.textContent = `${data.success}/${data.total}`; + floatingResult.success = data.success; + floatingResult.total = data.total; + floatingAutoResetTimer = setTimeout(() => setFloatingState(FloatState.IDLE), AUTO_RESET_DELAY); break; case FloatState.ERROR: + floatingEl.classList.add('error'); + statusIcon.textContent = '✗'; + statusIcon.className = 'nd-status-icon'; statusText.textContent = data.error?.label || '错误'; - currentResult.error = data.error; - autoResetTimer = setTimeout(() => setState(FloatState.IDLE), AUTO_RESET_DELAY); + floatingResult.error = data.error; + floatingAutoResetTimer = setTimeout(() => setFloatingState(FloatState.IDLE), AUTO_RESET_DELAY); break; } } -function updateProgress(current, total) { - if (currentState !== FloatState.GEN || !$cache.statusText) return; - $cache.statusText.textContent = `${current}/${total}`; -} - -function updateDetailPopup() { - const { detailResult, detailErrorRow, detailError, detailTime } = $cache; +function updateFloatingDetailPopup() { + const { detailResult, detailErrorRow, detailError, detailTime } = $floatingCache; if (!detailResult) return; - - const elapsed = currentResult.startTime - ? ((Date.now() - currentResult.startTime) / 1000).toFixed(1) + + const elapsed = floatingResult.startTime + ? ((Date.now() - floatingResult.startTime) / 1000).toFixed(1) : '-'; - - const isSuccess = currentState === FloatState.SUCCESS; - const isPartial = currentState === FloatState.PARTIAL; - const isError = currentState === FloatState.ERROR; - - if (isSuccess || isPartial) { - detailResult.textContent = `${currentResult.success}/${currentResult.total} 成功`; - detailResult.className = `nd-detail-value ${isSuccess ? 'success' : 'warning'}`; - detailErrorRow.style.display = isPartial ? 'flex' : 'none'; - if (isPartial) detailError.textContent = `${currentResult.total - currentResult.success} 张失败`; - } else if (isError) { + + if (floatingState === FloatState.SUCCESS || floatingState === FloatState.PARTIAL) { + detailResult.textContent = `${floatingResult.success}/${floatingResult.total} 成功`; + detailResult.className = `nd-detail-value ${floatingState === FloatState.SUCCESS ? 'success' : 'warning'}`; + detailErrorRow.style.display = floatingState === FloatState.PARTIAL ? 'flex' : 'none'; + if (floatingState === FloatState.PARTIAL) { + detailError.textContent = `${floatingResult.total - floatingResult.success} 张失败`; + } + } else if (floatingState === FloatState.ERROR) { detailResult.textContent = '生成失败'; detailResult.className = 'nd-detail-value error'; detailErrorRow.style.display = 'flex'; - detailError.textContent = currentResult.error?.desc || '未知错误'; + detailError.textContent = floatingResult.error?.desc || '未知错误'; } - + detailTime.textContent = `${elapsed}s`; } -// ═══════════════════════════════════════════════════════════════════════════ -// 拖拽与点击 -// ═══════════════════════════════════════════════════════════════════════════ - -function onPointerDown(e) { +function onFloatingPointerDown(e) { if (e.button !== 0) return; - - dragState = { + + floatingDragState = { startX: e.clientX, startY: e.clientY, - startLeft: floatEl.getBoundingClientRect().left, - startTop: floatEl.getBoundingClientRect().top, + startLeft: floatingEl.getBoundingClientRect().left, + startTop: floatingEl.getBoundingClientRect().top, pointerId: e.pointerId, moved: false, originalTarget: e.target }; - + try { e.currentTarget.setPointerCapture(e.pointerId); } catch {} e.preventDefault(); } -function onPointerMove(e) { - if (!dragState || dragState.pointerId !== e.pointerId) return; - - const dx = e.clientX - dragState.startX; - const dy = e.clientY - dragState.startY; - - if (!dragState.moved && (Math.abs(dx) > 3 || Math.abs(dy) > 3)) { - dragState.moved = true; +function onFloatingPointerMove(e) { + if (!floatingDragState || floatingDragState.pointerId !== e.pointerId) return; + + const dx = e.clientX - floatingDragState.startX; + const dy = e.clientY - floatingDragState.startY; + + if (!floatingDragState.moved && (Math.abs(dx) > 3 || Math.abs(dy) > 3)) { + floatingDragState.moved = true; } - - if (dragState.moved) { - const w = floatEl.offsetWidth || 88; - const h = floatEl.offsetHeight || 36; - floatEl.style.left = `${Math.max(0, Math.min(dragState.startLeft + dx, window.innerWidth - w))}px`; - floatEl.style.top = `${Math.max(0, Math.min(dragState.startTop + dy, window.innerHeight - h))}px`; + + if (floatingDragState.moved) { + const w = floatingEl.offsetWidth || 88; + const h = floatingEl.offsetHeight || 36; + floatingEl.style.left = `${Math.max(0, Math.min(floatingDragState.startLeft + dx, window.innerWidth - w))}px`; + floatingEl.style.top = `${Math.max(0, Math.min(floatingDragState.startTop + dy, window.innerHeight - h))}px`; } - + e.preventDefault(); } -function onPointerUp(e) { - if (!dragState || dragState.pointerId !== e.pointerId) return; - - const { moved, originalTarget } = dragState; - +function onFloatingPointerUp(e) { + if (!floatingDragState || floatingDragState.pointerId !== e.pointerId) return; + + const { moved, originalTarget } = floatingDragState; + try { e.currentTarget.releasePointerCapture(e.pointerId); } catch {} - dragState = null; - + floatingDragState = null; + if (moved) { - savePosition(); + saveFloatingPosition(); } else { - routeClick(originalTarget); + routeFloatingClick(originalTarget); } } -function routeClick(target) { - if (target.closest('#nd-btn-draw')) { - handleDrawClick(); - } else if (target.closest('#nd-btn-menu')) { - floatEl.classList.remove('show-detail'); - if (!floatEl.classList.contains('expanded')) { - refreshPresetSelect(); - refreshSizeSelect(); +function routeFloatingClick(target) { + if (target.closest('.nd-btn-draw')) { + handleFloatingDrawClick(); + } else if (target.closest('.nd-btn-menu')) { + floatingEl.classList.remove('show-detail'); + if (!floatingEl.classList.contains('expanded')) { + refreshFloatingPresetSelect(); + refreshFloatingSizeSelect(); } - floatEl.classList.toggle('expanded'); - } else if (target.closest('#nd-layer-active')) { - - if ([FloatState.LLM, FloatState.GEN, FloatState.COOLDOWN].includes(currentState)) { - - handleAbort(); - } else if ([FloatState.SUCCESS, FloatState.PARTIAL, FloatState.ERROR].includes(currentState)) { - - updateDetailPopup(); - floatEl.classList.toggle('show-detail'); + floatingEl.classList.toggle('expanded'); + } else if (target.closest('.nd-layer-active')) { + if ([FloatState.LLM, FloatState.GEN, FloatState.COOLDOWN].includes(floatingState)) { + handleFloatingAbort(); + } else if ([FloatState.SUCCESS, FloatState.PARTIAL, FloatState.ERROR].includes(floatingState)) { + updateFloatingDetailPopup(); + floatingEl.classList.toggle('show-detail'); } } } -// ═══════════════════════════════════════════════════════════════════════════ -// 核心操作 -// ═══════════════════════════════════════════════════════════════════════════ +async function handleFloatingDrawClick() { + if (floatingState !== FloatState.IDLE) return; -async function handleDrawClick() { - if (currentState !== FloatState.IDLE) return; // 非空闲状态不处理 - const messageId = findLastAIMessageId(); if (messageId < 0) { toastr?.warning?.('没有可配图的AI消息'); return; } - + + if (isGenerating()) { + toastr?.info?.('已有任务进行中,请等待完成'); + return; + } + try { await generateAndInsertImages({ messageId, onStateChange: (state, data) => { switch (state) { - case 'llm': setState(FloatState.LLM); break; - case 'gen': setState(FloatState.GEN, data); break; - case 'progress': setState(FloatState.GEN, data); break; - case 'cooldown': setState(FloatState.COOLDOWN, data); break; + case 'llm': setFloatingState(FloatState.LLM); break; + case 'gen': setFloatingState(FloatState.GEN, data); break; + case 'progress': setFloatingState(FloatState.GEN, data); break; + case 'cooldown': setFloatingState(FloatState.COOLDOWN, data); break; case 'success': - // ▼ 修改:中止时也显示结果 if (data.aborted && data.success === 0) { - setState(FloatState.IDLE); + setFloatingState(FloatState.IDLE); } else if (data.aborted || data.success < data.total) { - setState(FloatState.PARTIAL, data); + setFloatingState(FloatState.PARTIAL, data); } else { - setState(FloatState.SUCCESS, data); + setFloatingState(FloatState.SUCCESS, data); } break; } @@ -872,20 +1265,20 @@ async function handleDrawClick() { }); } catch (e) { console.error('[NovelDraw]', e); - // ▼ 修改:中止不显示错误 - if (e.message === '已取消') { - setState(FloatState.IDLE); + if (e.message === '已取消' || e.message?.includes('已有任务进行中')) { + setFloatingState(FloatState.IDLE); + if (e.message?.includes('已有任务进行中')) toastr?.info?.(e.message); } else { - setState(FloatState.ERROR, { error: classifyError(e) }); + setFloatingState(FloatState.ERROR, { error: classifyError(e) }); } } } -async function handleAbort() { +async function handleFloatingAbort() { try { const { abortGeneration } = await import('./novel-draw.js'); if (abortGeneration()) { - setState(FloatState.IDLE); + setFloatingState(FloatState.IDLE); toastr?.info?.('已中止'); } } catch (e) { @@ -893,207 +1286,279 @@ async function handleAbort() { } } +function refreshFloatingPresetSelect() { + fillPresetSelect($floatingCache.presetSelect); +} + +function refreshFloatingSizeSelect() { + fillSizeSelect($floatingCache.sizeSelect); +} + +function cacheFloatingDOM() { + if (!floatingEl) return; + $floatingCache = { + capsule: floatingEl.querySelector('.nd-capsule'), + statusIcon: floatingEl.querySelector('.nd-status-icon'), + statusText: floatingEl.querySelector('.nd-status-text'), + detailResult: floatingEl.querySelector('.nd-result'), + detailErrorRow: floatingEl.querySelector('.nd-error-row'), + detailError: floatingEl.querySelector('.nd-error'), + detailTime: floatingEl.querySelector('.nd-time'), + presetSelect: floatingEl.querySelector('.nd-preset-select'), + sizeSelect: floatingEl.querySelector('.nd-size-select'), + autoToggle: floatingEl.querySelector('.nd-auto-toggle'), + }; +} + +function handleFloatingOutsideClick(e) { + if (floatingEl && !floatingEl.contains(e.target)) { + floatingEl.classList.remove('expanded', 'show-detail'); + } +} + +function createFloatingButton() { + if (floatingEl) return; + + const settings = getSettings(); + if (settings.showFloatingButton !== true) return; + + injectStyles(); + + const isAuto = settings.mode === 'auto'; + + floatingEl = document.createElement('div'); + floatingEl.className = `nd-float nd-floating-global${isAuto ? ' auto-on' : ''}`; + floatingEl.id = 'nd-floating-global'; + + const detail = createEl('div', 'nd-detail'); + const detailRowResult = createEl('div', 'nd-detail-row'); + detailRowResult.append( + createEl('span', 'nd-detail-icon', '📊'), + createEl('span', 'nd-detail-label', '结果'), + createEl('span', 'nd-detail-value nd-result', '-') + ); + const detailRowError = createEl('div', 'nd-detail-row nd-error-row'); + detailRowError.style.display = 'none'; + detailRowError.append( + createEl('span', 'nd-detail-icon', '💡'), + createEl('span', 'nd-detail-label', '原因'), + createEl('span', 'nd-detail-value error nd-error', '-') + ); + const detailRowTime = createEl('div', 'nd-detail-row'); + detailRowTime.append( + createEl('span', 'nd-detail-icon', '⏱'), + createEl('span', 'nd-detail-label', '耗时'), + createEl('span', 'nd-detail-value nd-time', '-') + ); + detail.append(detailRowResult, detailRowError, detailRowTime); + + const menu = createEl('div', 'nd-menu'); + const card = createEl('div', 'nd-card'); + const rowPreset = createEl('div', 'nd-row'); + rowPreset.appendChild(createEl('span', 'nd-label', '预设')); + const presetSelect = createEl('select', 'nd-select nd-preset-select'); + fillPresetSelect(presetSelect); + rowPreset.appendChild(presetSelect); + const innerSep = createEl('div', 'nd-inner-sep'); + const rowSize = createEl('div', 'nd-row'); + rowSize.appendChild(createEl('span', 'nd-label', '尺寸')); + const sizeSelect = createEl('select', 'nd-select size nd-size-select'); + fillSizeSelect(sizeSelect); + rowSize.appendChild(sizeSelect); + card.append(rowPreset, innerSep, rowSize); + + const controls = createEl('div', 'nd-controls'); + const autoToggle = createEl('div', `nd-auto${isAuto ? ' on' : ''} nd-auto-toggle`); + autoToggle.append( + createEl('span', 'nd-dot'), + createEl('span', 'nd-auto-text', '自动配图') + ); + const settingsBtn = createEl('button', 'nd-gear nd-settings-btn', '⚙'); + settingsBtn.title = '打开设置'; + controls.append(autoToggle, settingsBtn); + menu.append(card, controls); + + const capsule = createEl('div', 'nd-capsule'); + const inner = createEl('div', 'nd-inner'); + const layerIdle = createEl('div', 'nd-layer nd-layer-idle'); + const drawBtn = createEl('button', 'nd-btn-draw'); + drawBtn.title = '点击为最后一条AI消息生成配图'; + drawBtn.appendChild(createEl('span', '', '🎨')); + drawBtn.appendChild(createEl('span', 'nd-auto-dot')); + const sep = createEl('div', 'nd-sep'); + const menuBtn = createEl('button', 'nd-btn-menu'); + menuBtn.title = '展开菜单'; + menuBtn.appendChild(createEl('span', 'nd-arrow', '▲')); + layerIdle.append(drawBtn, sep, menuBtn); + const layerActive = createEl('div', 'nd-layer nd-layer-active'); + layerActive.append( + createEl('span', 'nd-status-icon', '⏳'), + createEl('span', 'nd-status-text', '分析') + ); + inner.append(layerIdle, layerActive); + capsule.appendChild(inner); + + floatingEl.append(detail, menu, capsule); + + document.body.appendChild(floatingEl); + cacheFloatingDOM(); + applyFloatingPosition(); + + const capsuleEl = $floatingCache.capsule; + if (capsuleEl) { + capsuleEl.addEventListener('pointerdown', onFloatingPointerDown, { passive: false }); + capsuleEl.addEventListener('pointermove', onFloatingPointerMove, { passive: false }); + capsuleEl.addEventListener('pointerup', onFloatingPointerUp, { passive: false }); + capsuleEl.addEventListener('pointercancel', onFloatingPointerUp, { passive: false }); + } + + $floatingCache.presetSelect?.addEventListener('change', (e) => { + const settings = getSettings(); + settings.selectedParamsPresetId = e.target.value; + saveSettings(settings); + updateAllPresetSelects(); + }); + + $floatingCache.sizeSelect?.addEventListener('change', (e) => { + const settings = getSettings(); + settings.overrideSize = e.target.value; + saveSettings(settings); + updateAllSizeSelects(); + }); + + $floatingCache.autoToggle?.addEventListener('click', () => { + const settings = getSettings(); + settings.mode = settings.mode === 'auto' ? 'manual' : 'auto'; + saveSettings(settings); + updateAutoModeUI(); + }); + + floatingEl.querySelector('.nd-settings-btn')?.addEventListener('click', () => { + floatingEl.classList.remove('expanded'); + openNovelDrawSettings(); + }); + + document.addEventListener('click', handleFloatingOutsideClick, { passive: true }); + window.addEventListener('resize', applyFloatingPosition); +} + +function destroyFloatingButton() { + clearFloatingCooldownTimer(); + + if (floatingAutoResetTimer) { + clearTimeout(floatingAutoResetTimer); + floatingAutoResetTimer = null; + } + + window.removeEventListener('resize', applyFloatingPosition); + document.removeEventListener('click', handleFloatingOutsideClick); + + floatingEl?.remove(); + floatingEl = null; + floatingDragState = null; + floatingState = FloatState.IDLE; + $floatingCache = {}; +} + // ═══════════════════════════════════════════════════════════════════════════ -// 预设与尺寸管理 +// 全局更新函数 // ═══════════════════════════════════════════════════════════════════════════ -function buildPresetOptions() { - const settings = getSettings(); - const presets = settings.paramsPresets || []; - const currentId = settings.selectedParamsPresetId; - return presets.map(p => - `` - ).join(''); +function updateAllPresetSelects() { + panelMap.forEach((data) => { + fillPresetSelect(data.$cache?.presetSelect); + }); + fillPresetSelect($floatingCache.presetSelect); } -function buildSizeOptions() { - const settings = getSettings(); - const current = settings.overrideSize || 'default'; - return SIZE_OPTIONS.map(opt => - `` - ).join(''); -} - -function refreshPresetSelect() { - if (!$cache.presetSelect) return; - $cache.presetSelect.innerHTML = buildPresetOptions(); -} - -function refreshSizeSelect() { - if (!$cache.sizeSelect) return; - $cache.sizeSelect.innerHTML = buildSizeOptions(); -} - -function handlePresetChange(e) { - const presetId = e.target.value; - if (!presetId) return; - const settings = getSettings(); - settings.selectedParamsPresetId = presetId; - saveSettings(settings); -} - -function handleSizeChange(e) { - const value = e.target.value; - const settings = getSettings(); - settings.overrideSize = value; - saveSettings(settings); +function updateAllSizeSelects() { + panelMap.forEach((data) => { + fillSizeSelect(data.$cache?.sizeSelect); + }); + fillSizeSelect($floatingCache.sizeSelect); } export function updateAutoModeUI() { - if (!floatEl) return; const isAuto = getSettings().mode === 'auto'; - floatEl.classList.toggle('auto-on', isAuto); - $cache.autoToggle?.classList.toggle('on', isAuto); -} -function handleAutoToggle() { - const settings = getSettings(); - settings.mode = settings.mode === 'auto' ? 'manual' : 'auto'; - saveSettings(settings); - updateAutoModeUI(); -} - -// ═══════════════════════════════════════════════════════════════════════════ -// 创建与销毁 -// ═══════════════════════════════════════════════════════════════════════════ - -export function createFloatingPanel() { - if (floatEl) return; - - injectStyles(); - - const settings = getSettings(); - const isAuto = settings.mode === 'auto'; - - floatEl = document.createElement('div'); - floatEl.className = `nd-float${isAuto ? ' auto-on' : ''}`; - floatEl.id = 'nd-floating-panel'; - - floatEl.innerHTML = ` - -
-
- 📊 - 结果 - - -
- -
- - 耗时 - - -
-
- - -
- -
-
- 预设 - -
-
-
- 尺寸 - -
-
- - -
-
- - 自动配图 -
- -
-
- - -
-
-
- -
- -
-
- - 分析 -
-
-
- `; - - document.body.appendChild(floatEl); - cacheDOM(); - applyPosition(); - bindEvents(); - - window.addEventListener('resize', applyPosition); -} - -function bindEvents() { - const capsule = $cache.capsule; - if (!capsule) return; - - capsule.addEventListener('pointerdown', onPointerDown, { passive: false }); - capsule.addEventListener('pointermove', onPointerMove, { passive: false }); - capsule.addEventListener('pointerup', onPointerUp, { passive: false }); - capsule.addEventListener('pointercancel', onPointerUp, { passive: false }); - - $cache.presetSelect?.addEventListener('change', handlePresetChange); - $cache.sizeSelect?.addEventListener('change', handleSizeChange); - $cache.autoToggle?.addEventListener('click', handleAutoToggle); - - floatEl.querySelector('#nd-settings-btn')?.addEventListener('click', () => { - floatEl.classList.remove('expanded'); - openNovelDrawSettings(); + panelMap.forEach((data) => { + if (!data.root) return; + data.root.classList.toggle('auto-on', isAuto); + data.$cache.autoToggle?.classList.toggle('on', isAuto); }); - - document.addEventListener('click', handleOutsideClick, { passive: true }); + + if (floatingEl) { + floatingEl.classList.toggle('auto-on', isAuto); + $floatingCache.autoToggle?.classList.toggle('on', isAuto); + } } -function handleOutsideClick(e) { - if (floatEl && !floatEl.contains(e.target)) { - floatEl.classList.remove('expanded', 'show-detail'); +export function refreshPresetSelectAll() { + updateAllPresetSelects(); +} + +// ═══════════════════════════════════════════════════════════════════════════ +// 按钮显示控制 +// ═══════════════════════════════════════════════════════════════════════════ + +export function updateButtonVisibility(showFloor, showFloating) { + if (showFloating && !floatingEl) { + createFloatingButton(); + } else if (!showFloating && floatingEl) { + destroyFloatingButton(); + } + + if (!showFloor) { + panelMap.forEach((data, messageId) => { + if (data.autoResetTimer) clearTimeout(data.autoResetTimer); + if (data.cooldownRafId) cancelAnimationFrame(data.cooldownRafId); + data._cleanup?.(); + if (data.root) removeFromToolbar(messageId, data.root); + }); + panelMap.clear(); + pendingCallbacks.clear(); + floorObserver?.disconnect(); + floorObserver = null; + } +} + +// ═══════════════════════════════════════════════════════════════════════════ +// 初始化与清理 +// ═══════════════════════════════════════════════════════════════════════════ + +export function initFloatingPanel() { + const settings = getSettings(); + + if (settings.showFloatingButton === true) { + createFloatingButton(); } } export function destroyFloatingPanel() { - clearCooldownTimer(); - - if (autoResetTimer) { - clearTimeout(autoResetTimer); - autoResetTimer = null; - } - - window.removeEventListener('resize', applyPosition); - document.removeEventListener('click', handleOutsideClick); - - floatEl?.remove(); - floatEl = null; - dragState = null; - currentState = FloatState.IDLE; - $cache = {}; + panelMap.forEach((data, messageId) => { + if (data.autoResetTimer) clearTimeout(data.autoResetTimer); + if (data.cooldownRafId) cancelAnimationFrame(data.cooldownRafId); + data._cleanup?.(); + if (data.root) removeFromToolbar(messageId, data.root); + }); + panelMap.clear(); + pendingCallbacks.clear(); + + floorObserver?.disconnect(); + floorObserver = null; + + destroyFloatingButton(); } // ═══════════════════════════════════════════════════════════════════════════ // 导出 // ═══════════════════════════════════════════════════════════════════════════ -export { FloatState, setState, updateProgress, refreshPresetSelect, SIZE_OPTIONS }; +export { + FloatState, + refreshPresetSelectAll as refreshPresetSelect, + SIZE_OPTIONS, + createFloatingButton, + destroyFloatingButton, + setFloatingState, +}; diff --git a/modules/novel-draw/gallery-cache.js b/modules/novel-draw/gallery-cache.js index 0a053c0..4f6f65d 100644 --- a/modules/novel-draw/gallery-cache.js +++ b/modules/novel-draw/gallery-cache.js @@ -53,10 +53,6 @@ function invalidateCache(slotId) { // 工具函数 // ═══════════════════════════════════════════════════════════════════════════ -function escapeHtml(str) { - return String(str || '').replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); -} - function getChatCharacterName() { const ctx = getContext(); if (ctx.groupId) return String(ctx.groups?.[ctx.groupId]?.id ?? 'group'); @@ -558,6 +554,8 @@ function createGalleryOverlay() { const overlay = document.createElement('div'); overlay.id = 'nd-gallery-overlay'; + // Template-only UI markup. + // eslint-disable-next-line no-unsanitized/property overlay.innerHTML = ``; document.body.appendChild(overlay); @@ -612,6 +610,8 @@ function renderGallery() { const reversedPreviews = previews.slice().reverse(); const thumbsContainer = document.getElementById('nd-gallery-thumbs'); + // Generated from local preview data only. + // eslint-disable-next-line no-unsanitized/property thumbsContainer.innerHTML = reversedPreviews.map((p, i) => { const src = p.savedUrl || `data:image/png;base64,${p.base64}`; const originalIndex = previews.length - 1 - i; diff --git a/modules/novel-draw/image-live-effect.js b/modules/novel-draw/image-live-effect.js new file mode 100644 index 0000000..5ba96cc --- /dev/null +++ b/modules/novel-draw/image-live-effect.js @@ -0,0 +1,331 @@ +// image-live-effect.js +// Live Photo - 柔和分区 + 亮度感知 + +import { extensionFolderPath } from "../../core/constants.js"; + +let PIXI = null; +let pixiLoading = null; +const activeEffects = new Map(); + +async function ensurePixi() { + if (PIXI) return PIXI; + if (pixiLoading) return pixiLoading; + + pixiLoading = new Promise((resolve, reject) => { + if (window.PIXI) { PIXI = window.PIXI; resolve(PIXI); return; } + const script = document.createElement('script'); + script.src = `${extensionFolderPath}/libs/pixi.min.js`; + script.onload = () => { PIXI = window.PIXI; resolve(PIXI); }; + script.onerror = () => reject(new Error('PixiJS 加载失败')); + // eslint-disable-next-line no-unsanitized/method + document.head.appendChild(script); + }); + return pixiLoading; +} + +// ═══════════════════════════════════════════════════════════════════════════ +// 着色器 - 柔和分区 + 亮度感知 +// ═══════════════════════════════════════════════════════════════════════════ + +const VERTEX_SHADER = ` +attribute vec2 aVertexPosition; +attribute vec2 aTextureCoord; +uniform mat3 projectionMatrix; +varying vec2 vTextureCoord; +void main() { + gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); + vTextureCoord = aTextureCoord; +}`; + +const FRAGMENT_SHADER = ` +precision highp float; +varying vec2 vTextureCoord; +uniform sampler2D uSampler; +uniform float uTime; +uniform float uIntensity; + +float hash(vec2 p) { + return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); +} + +float noise(vec2 p) { + vec2 i = floor(p); + vec2 f = fract(p); + f = f * f * (3.0 - 2.0 * f); + return mix( + mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), + mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), + f.y + ); +} + +float zone(float v, float start, float end) { + return smoothstep(start, start + 0.08, v) * (1.0 - smoothstep(end - 0.08, end, v)); +} + +float skinDetect(vec4 color) { + float brightness = dot(color.rgb, vec3(0.299, 0.587, 0.114)); + float warmth = color.r - color.b; + return smoothstep(0.3, 0.6, brightness) * smoothstep(0.0, 0.15, warmth); +} + +void main() { + vec2 uv = vTextureCoord; + float v = uv.y; + float u = uv.x; + float centerX = abs(u - 0.5); + + vec4 baseColor = texture2D(uSampler, uv); + float skin = skinDetect(baseColor); + + vec2 offset = vec2(0.0); + + // ═══════════════════════════════════════════════════════════════════════ + // 🛡️ 头部保护 (Y: 0 ~ 0.30) + // ═══════════════════════════════════════════════════════════════════════ + float headLock = 1.0 - smoothstep(0.0, 0.30, v); + float headDampen = mix(1.0, 0.05, headLock); + + // ═══════════════════════════════════════════════════════════════════════ + // 🫁 全局呼吸 + // ═══════════════════════════════════════════════════════════════════════ + float breath = sin(uTime * 0.8) * 0.004; + offset += (uv - 0.5) * breath * headDampen; + + // ═══════════════════════════════════════════════════════════════════════ + // 👙 胸部区域 (Y: 0.35 ~ 0.55) - 呼吸起伏 + // ═══════════════════════════════════════════════════════════════════════ + float chestZone = zone(v, 0.35, 0.55); + float chestCenter = 1.0 - smoothstep(0.0, 0.35, centerX); + float chestStrength = chestZone * chestCenter; + + float breathRhythm = sin(uTime * 1.0) * 0.6 + sin(uTime * 2.0) * 0.4; + + // 纵向起伏 + float chestY = breathRhythm * 0.010 * (1.0 + skin * 0.7); + offset.y += chestY * chestStrength * uIntensity; + + // 横向微扩 + float chestX = breathRhythm * 0.005 * (u - 0.5); + offset.x += chestX * chestStrength * uIntensity * (1.0 + skin * 0.4); + + // ═══════════════════════════════════════════════════════════════════════ + // 🍑 腰臀区域 (Y: 0.55 ~ 0.75) - 轻微摇摆 + // ═══════════════════════════════════════════════════════════════════════ + float hipZone = zone(v, 0.55, 0.75); + float hipCenter = 1.0 - smoothstep(0.0, 0.4, centerX); + float hipStrength = hipZone * hipCenter; + + // 左右轻晃 + float hipSway = sin(uTime * 0.6) * 0.008; + offset.x += hipSway * hipStrength * uIntensity * (1.0 + skin * 0.4); + + // 微弱弹动 + float hipBounce = sin(uTime * 1.0 + 0.3) * 0.006; + offset.y += hipBounce * hipStrength * uIntensity * (1.0 + skin * 0.6); + + // ═══════════════════════════════════════════════════════════════════════ + // 👗 底部区域 (Y: 0.75+) - 轻微飘动 + // ═══════════════════════════════════════════════════════════════════════ + float bottomZone = smoothstep(0.73, 0.80, v); + float bottomStrength = bottomZone * (v - 0.75) * 2.5; + + float bottomWave = sin(uTime * 1.2 + u * 5.0) * 0.012; + offset.x += bottomWave * bottomStrength * uIntensity; + + // ═══════════════════════════════════════════════════════════════════════ + // 🌊 环境流动 - 极轻微 + // ═══════════════════════════════════════════════════════════════════════ + float ambient = noise(uv * 2.5 + uTime * 0.15) * 0.003; + offset.x += ambient * headDampen * uIntensity; + offset.y += noise(uv * 3.0 - uTime * 0.12) * 0.002 * headDampen * uIntensity; + + // ═══════════════════════════════════════════════════════════════════════ + // 应用偏移 + // ═══════════════════════════════════════════════════════════════════════ + vec2 finalUV = clamp(uv + offset, 0.001, 0.999); + + gl_FragColor = texture2D(uSampler, finalUV); +}`; + +// ═══════════════════════════════════════════════════════════════════════════ +// Live 效果类 +// ═══════════════════════════════════════════════════════════════════════════ + +class ImageLiveEffect { + constructor(container, imageSrc) { + this.container = container; + this.imageSrc = imageSrc; + this.app = null; + this.sprite = null; + this.filter = null; + this.canvas = null; + this.running = false; + this.destroyed = false; + this.startTime = Date.now(); + this.intensity = 1.0; + this._boundAnimate = this.animate.bind(this); + } + + async init() { + const wrap = this.container.querySelector('.xb-nd-img-wrap'); + const img = this.container.querySelector('img'); + if (!wrap || !img) return false; + + const rect = img.getBoundingClientRect(); + this.width = Math.round(rect.width); + this.height = Math.round(rect.height); + if (this.width < 50 || this.height < 50) return false; + + try { + this.app = new PIXI.Application({ + width: this.width, + height: this.height, + backgroundAlpha: 0, + resolution: 1, + autoDensity: true, + }); + + this.canvas = document.createElement('div'); + this.canvas.className = 'xb-nd-live-canvas'; + this.canvas.style.cssText = 'position:absolute;top:0;left:0;width:100%;height:100%;z-index:1;pointer-events:none;'; + this.app.view.style.cssText = 'width:100%;height:100%;display:block;'; + this.canvas.appendChild(this.app.view); + wrap.appendChild(this.canvas); + + const texture = await this.loadTexture(this.imageSrc); + if (!texture || this.destroyed) { this.destroy(); return false; } + + this.sprite = new PIXI.Sprite(texture); + this.sprite.width = this.width; + this.sprite.height = this.height; + + this.filter = new PIXI.Filter(VERTEX_SHADER, FRAGMENT_SHADER, { + uTime: 0, + uIntensity: this.intensity, + }); + this.sprite.filters = [this.filter]; + this.app.stage.addChild(this.sprite); + + img.style.opacity = '0'; + this.container.classList.add('mode-live'); + this.start(); + return true; + } catch (e) { + console.error('[Live] init error:', e); + this.destroy(); + return false; + } + } + + loadTexture(src) { + return new Promise((resolve) => { + if (this.destroyed) { resolve(null); return; } + try { + const texture = PIXI.Texture.from(src); + if (texture.baseTexture.valid) resolve(texture); + else { + texture.baseTexture.once('loaded', () => resolve(texture)); + texture.baseTexture.once('error', () => resolve(null)); + } + } catch { resolve(null); } + }); + } + + start() { + if (this.running || this.destroyed) return; + this.running = true; + this.app.ticker.add(this._boundAnimate); + } + + stop() { + this.running = false; + this.app?.ticker?.remove(this._boundAnimate); + } + + animate() { + if (this.destroyed || !this.filter) return; + this.filter.uniforms.uTime = (Date.now() - this.startTime) / 1000; + } + + setIntensity(value) { + this.intensity = Math.max(0, Math.min(2, value)); + if (this.filter) this.filter.uniforms.uIntensity = this.intensity; + } + + destroy() { + if (this.destroyed) return; + this.destroyed = true; + this.stop(); + this.container?.classList.remove('mode-live'); + const img = this.container?.querySelector('img'); + if (img) img.style.opacity = ''; + this.canvas?.remove(); + this.app?.destroy(true, { children: true, texture: false }); + this.app = null; + this.sprite = null; + this.filter = null; + this.canvas = null; + } +} + +// ═══════════════════════════════════════════════════════════════════════════ +// API +// ═══════════════════════════════════════════════════════════════════════════ + +export async function toggleLiveEffect(container) { + const existing = activeEffects.get(container); + const btn = container.querySelector('.xb-nd-live-btn'); + + if (existing) { + existing.destroy(); + activeEffects.delete(container); + btn?.classList.remove('active'); + return false; + } + + btn?.classList.add('loading'); + + try { + await ensurePixi(); + const img = container.querySelector('img'); + if (!img?.src) { btn?.classList.remove('loading'); return false; } + + const effect = new ImageLiveEffect(container, img.src); + const success = await effect.init(); + btn?.classList.remove('loading'); + + if (success) { + activeEffects.set(container, effect); + btn?.classList.add('active'); + return true; + } + return false; + } catch (e) { + console.error('[Live] failed:', e); + btn?.classList.remove('loading'); + return false; + } +} + +export function destroyLiveEffect(container) { + const effect = activeEffects.get(container); + if (effect) { + effect.destroy(); + activeEffects.delete(container); + container.querySelector('.xb-nd-live-btn')?.classList.remove('active'); + } +} + +export function destroyAllLiveEffects() { + activeEffects.forEach(e => e.destroy()); + activeEffects.clear(); +} + +export function isLiveActive(container) { + return activeEffects.has(container); +} + +export function getEffect(container) { + return activeEffects.get(container); +} diff --git a/modules/novel-draw/novel-draw.html b/modules/novel-draw/novel-draw.html index 2ef88d9..fc33863 100644 --- a/modules/novel-draw/novel-draw.html +++ b/modules/novel-draw/novel-draw.html @@ -65,6 +65,13 @@ body { display: flex; background: var(--bg-input); border: 1px solid var(--border); border-radius: 16px; padding: 2px; } +.header-toggles { display: flex; gap: 6px; margin-right: 8px; } +.header-toggle { + display: flex; align-items: center; gap: 4px; padding: 4px 8px; + background: var(--bg-input); border: 1px solid var(--border); border-radius: 12px; + font-size: 11px; color: var(--text-secondary); cursor: pointer; transition: all 0.15s; +} +.header-toggle input { accent-color: var(--accent); } .header-mode button { padding: 6px 14px; border: none; border-radius: 14px; background: transparent; color: var(--text-secondary); @@ -210,6 +217,7 @@ select.input { cursor: pointer; } border: 1px solid rgba(212, 165, 116, 0.2); border-radius: 8px; font-size: 12px; color: var(--text-secondary); line-height: 1.6; } +.tip-text { display: flex; flex-direction: column; gap: 4px; } .tip-box i { color: var(--accent); flex-shrink: 0; margin-top: 2px; } .gallery-char-section { margin-bottom: 16px; } .gallery-char-header { @@ -363,6 +371,16 @@ select.input { cursor: pointer; }
未启用
+
+ + +
@@ -410,7 +428,11 @@ select.input { cursor: pointer; }
-
聊天界面点击悬浮球 🎨 即可为最后一条AI消息生成配图。开启自动模式后,AI回复时会自动配图。
+
+
消息楼层按钮的 🎨 为对应消息生成配图。
+
悬浮按钮的 🎨 仅作用于最后一条AI消息。
+
开启自动模式后,AI回复时会自动配图。
+
@@ -662,7 +684,7 @@ select.input { cursor: pointer; }
@@ -829,7 +851,9 @@ let state = { paramsPresets: [], llmApi: { provider: 'st', url: '', key: '', model: '', modelCache: [] }, useStream: true, - characterTags: [] + characterTags: [], + showFloorButton: true, + showFloatingButton: false }; let gallerySummary = {}; @@ -845,8 +869,11 @@ let modalData = { slotId: null, images: [], currentIndex: 0, charName: null }; const $ = id => document.getElementById(id); const $$ = sel => document.querySelectorAll(sel); +const PARENT_ORIGIN = (() => { + try { return new URL(document.referrer).origin; } catch { return window.location.origin; } +})(); function postToParent(payload) { - window.parent.postMessage({ source: 'NovelDraw-Frame', ...payload }, '*'); + window.parent.postMessage({ source: 'NovelDraw-Frame', ...payload }, PARENT_ORIGIN); } function escapeHtml(str) { @@ -1256,6 +1283,8 @@ function getCurrentLlmModel() { function applyStateToUI() { updateBadge(state.enabled); updateModeButtons(state.mode); + $('nd_show_floor').checked = state.showFloorButton !== false; + $('nd_show_floating').checked = state.showFloatingButton === true; $('nd_api_key').value = state.apiKey || ''; $('nd_timeout').value = Math.round((state.timeout > 0 ? state.timeout : DEFAULTS.timeout) / 1000); @@ -1384,7 +1413,9 @@ function collectParamsPreset() { // 消息处理 // ═══════════════════════════════════════════════════════════════════════════ +// Guarded by origin/source check. window.addEventListener('message', event => { + if (event.origin !== PARENT_ORIGIN || event.source !== window.parent) return; const data = event.data; if (!data || data.source !== 'LittleWhiteBox-NovelDraw') return; @@ -1483,6 +1514,22 @@ document.addEventListener('DOMContentLoaded', () => { updateModeButtons(state.mode); postToParent({ type: 'SAVE_MODE', mode: state.mode }); })); + + $('nd_show_floor').addEventListener('change', () => { + postToParent({ + type: 'SAVE_BUTTON_MODE', + showFloorButton: $('nd_show_floor').checked, + showFloatingButton: $('nd_show_floating').checked + }); + }); + + $('nd_show_floating').addEventListener('change', () => { + postToParent({ + type: 'SAVE_BUTTON_MODE', + showFloorButton: $('nd_show_floor').checked, + showFloatingButton: $('nd_show_floating').checked + }); + }); // ═══════════════════════════════════════════════════════════════════════ // 关闭按钮 @@ -1717,4 +1764,4 @@ document.addEventListener('DOMContentLoaded', () => { }); - \ No newline at end of file + diff --git a/modules/novel-draw/novel-draw.js b/modules/novel-draw/novel-draw.js index 1a223e4..9c54b3a 100644 --- a/modules/novel-draw/novel-draw.js +++ b/modules/novel-draw/novel-draw.js @@ -29,6 +29,7 @@ import { parsePresetData, destroyCloudPresets } from './cloud-presets.js'; +import { postToIframe, isTrustedMessage } from "../../core/iframe-messaging.js"; // ═══════════════════════════════════════════════════════════════════════════ // 常量 @@ -42,7 +43,7 @@ const CONFIG_VERSION = 4; const MAX_SEED = 0xFFFFFFFF; const API_TEST_TIMEOUT = 15000; const PLACEHOLDER_REGEX = /\[image:([a-z0-9\-_]+)\]/gi; -const INITIAL_RENDER_MESSAGE_LIMIT = 10; +const INITIAL_RENDER_MESSAGE_LIMIT = 1; const events = createModuleEvents(MODULE_KEY); @@ -86,6 +87,8 @@ const DEFAULT_SETTINGS = { useWorldInfo: false, characterTags: [], overrideSize: 'default', + showFloorButton: true, + showFloatingButton: false, }; // ═══════════════════════════════════════════════════════════════════════════ @@ -102,6 +105,7 @@ let settingsCache = null; let settingsLoaded = false; let generationAbortController = null; let messageObserver = null; +let ensureNovelDrawPanelRef = null; // ═══════════════════════════════════════════════════════════════════════════ // 样式 @@ -176,6 +180,13 @@ function ensureStyles() { .xb-nd-edit-input:focus{border-color:rgba(212,165,116,0.5);outline:none} .xb-nd-edit-input.scene{border-color:rgba(212,165,116,0.3)} .xb-nd-edit-input.char{border-color:rgba(147,197,253,0.3)} +.xb-nd-live-btn{position:absolute;bottom:10px;right:10px;z-index:5;padding:4px 8px;background:rgba(0,0,0,0.75);border:none;border-radius:12px;color:rgba(255,255,255,0.7);font-size:10px;font-weight:700;letter-spacing:0.5px;cursor:pointer;opacity:0.7;transition:all 0.2s;user-select:none} +.xb-nd-live-btn:hover{opacity:1;background:rgba(0,0,0,0.85)} +.xb-nd-live-btn.active{background:rgba(62,207,142,0.9);color:#fff;opacity:1;box-shadow:0 0 10px rgba(62,207,142,0.5)} +.xb-nd-live-btn.loading{pointer-events:none;opacity:0.5} +.xb-nd-img.mode-live .xb-nd-img-wrap>img{opacity:0!important;pointer-events:none} +.xb-nd-live-canvas{border-radius:10px;overflow:hidden} +.xb-nd-live-canvas canvas{display:block;border-radius:10px} `; document.head.appendChild(style); } @@ -263,7 +274,7 @@ function abortGeneration() { } function isGenerating() { - return generationAbortController !== null; + return autoBusy || generationAbortController !== null; } // ═══════════════════════════════════════════════════════════════════════════ @@ -769,6 +780,7 @@ function buildImageHtml({ slotId, imgId, url, tags, positive, messageId, state = ${displayVersion} / ${historyCount}
`; + const liveBtn = ``; const menuBusy = isBusy ? ' busy' : ''; const menuHtml = `
@@ -786,6 +798,7 @@ ${indicator}
${navPill} + ${liveBtn}
${menuHtml}
-
自动触发
+
总结设置
-
+