import { Button, Color, Graphics, ImageAsset, Label, Layers, Node, resources, Sprite, SpriteFrame, Texture2D, UITransform, Widget } from 'cc'; declare const wx: any; export const UI_LAYER = Layers.Enum.UI_2D; export function hex(code: string): Color { const c = new Color(); Color.fromHEX(c, code); return c; } export interface BoxStyle { fill?: string; stroke?: string; strokeWidth?: number; radius?: number; /** 3D 底部厚边(实体按键/木牌立体感) */ depth?: number; depthColor?: string; /** 柔和落影偏移 */ shadow?: number; shadowColor?: string; /** 顶部高光细线 */ highlight?: string; /** 内侧虚线手缝线装饰 */ stitchColor?: string; stitchInset?: number; /** 四角黄铜铆钉装饰 */ rivets?: boolean; rivetColor?: string; } export function createUINode(name: string, w: number, h: number, parent?: Node): Node { const node = new Node(name); node.layer = UI_LAYER; const ui = node.addComponent(UITransform); ui.setContentSize(w, h); if (parent) parent.addChild(node); return node; } function drawDashedRect(g: Graphics, x: number, y: number, w: number, h: number, color: string): void { g.strokeColor = hex(color); g.lineWidth = 1.6; const dash = 7; const gap = 5; const drawEdge = (x1: number, y1: number, x2: number, y2: number) => { const dist = Math.hypot(x2 - x1, y2 - y1); const ux = (x2 - x1) / dist; const uy = (y2 - y1) / dist; let pos = 4; while (pos < dist - 4) { const end = Math.min(dist - 4, pos + dash); g.moveTo(x1 + ux * pos, y1 + uy * pos); g.lineTo(x1 + ux * end, y1 + uy * end); pos = end + gap; } }; drawEdge(x, y + h, x + w, y + h); drawEdge(x + w, y + h, x + w, y); drawEdge(x + w, y, x, y); drawEdge(x, y, x, y + h); g.stroke(); } export function drawPadlockIcon( g: Graphics, scale = 1, bodyColor = '#d6b67d', shackleColor = '#ecd2a2', keyholeColor = '#46311f', ): void { g.clear(); const s = scale; const r = 3.7 * s; const archY = 3.2 * s; // 锁梁(U 型圆弧,手动采样保证各端方向一致) g.strokeColor = hex(shackleColor); g.lineWidth = Math.max(1.5, 2.0 * s); g.moveTo(-r, 0.2 * s); g.lineTo(-r, archY); const steps = 12; for (let i = 1; i <= steps; i++) { const t = Math.PI - (Math.PI * i) / steps; g.lineTo(Math.cos(t) * r, archY + Math.sin(t) * r); } g.lineTo(r, 0.2 * s); g.stroke(); // 锁身(倒角金属块) const bw = 11.4 * s; const bh = 8.6 * s; g.fillColor = hex(bodyColor); g.roundRect(-bw / 2, -6.8 * s, bw, bh, 2.2 * s); g.fill(); // 锁孔 g.fillColor = hex(keyholeColor); g.circle(0, -1.7 * s, 1.45 * s); g.fill(); g.rect(-0.68 * s, -4.6 * s, 1.36 * s, 2.7 * s); g.fill(); } export function drawEnvelopeIcon(g: Graphics, scale = 1, color = '#fff2cf', sealColor = '#d45a52'): void { g.clear(); const s = scale; const w = 22 * s; const h = 15 * s; g.strokeColor = hex(color); g.lineWidth = Math.max(1.6, 2.1 * s); g.roundRect(-w / 2, -h / 2, w, h, 2.4 * s); g.stroke(); g.moveTo(-w / 2 + 1.5 * s, h / 2 - 1.5 * s); g.lineTo(0, -1.2 * s); g.lineTo(w / 2 - 1.5 * s, h / 2 - 1.5 * s); g.stroke(); g.fillColor = hex(sealColor); g.circle(0, -1.6 * s, 3.0 * s); g.fill(); } export function drawSpoolIcon(g: Graphics, scale = 1, woodColor = '#f4d694', threadColor = '#93be76'): void { g.clear(); const s = scale; // 绕线身 g.fillColor = hex(threadColor); g.roundRect(-7 * s, -7 * s, 14 * s, 14 * s, 2.5 * s); g.fill(); // 几道柔和丝线横纹 g.strokeColor = hex('#eef7dc99'); g.lineWidth = 1.3 * s; for (const ry of [-3.4, 0, 3.4]) { g.moveTo(-6.2 * s, ry * s); g.lineTo(6.2 * s, ry * s); } g.stroke(); // 上下木挡片 g.fillColor = hex(woodColor); g.roundRect(-10.5 * s, 6.2 * s, 21 * s, 4.4 * s, 2 * s); g.roundRect(-10.5 * s, -10.6 * s, 21 * s, 4.4 * s, 2 * s); g.fill(); } export function drawGalleryFrameIcon(g: Graphics, scale = 1, frameColor = '#fff0c8', accentColor = '#e6b14c'): void { g.clear(); const s = scale; const w = 20 * s; const h = 22 * s; g.strokeColor = hex(frameColor); g.lineWidth = Math.max(1.8, 2.2 * s); g.roundRect(-w / 2, -h / 2, w, h, 3 * s); g.stroke(); // 内绣绷圆环 + 小花点缀 g.strokeColor = hex(accentColor); g.lineWidth = 1.6 * s; g.circle(0, 1.5 * s, 5.4 * s); g.stroke(); g.fillColor = hex(accentColor); g.circle(0, 1.5 * s, 2.2 * s); g.fill(); g.strokeColor = hex(frameColor); g.lineWidth = 1.5 * s; g.moveTo(-5.5 * s, -6.8 * s); g.lineTo(5.5 * s, -6.8 * s); g.stroke(); } export function drawRoundedBox(g: Graphics, w: number, h: number, style: BoxStyle): void { g.clear(); const r = Math.min(style.radius ?? 16, w / 2, h / 2); const x = -w / 2; const y = -h / 2; const depth = style.depth ?? 0; const shadow = style.shadow ?? 0; // 1. 柔和地面投影 if (shadow > 0) { g.fillColor = hex(style.shadowColor ?? '#1b211438'); const sy = y - depth - shadow; if (r > 0) g.roundRect(x + 1, sy, w - 2, h, r); else g.rect(x + 1, sy, w - 2, h); g.fill(); } // 2. 实体 3D 底座厚边 if (depth > 0) { g.fillColor = hex(style.depthColor ?? '#3b4530'); if (r > 0) g.roundRect(x, y - depth, w, h, r); else g.rect(x, y - depth, w, h); g.fill(); } // 3. 主面板填充 if (style.fill) { g.fillColor = hex(style.fill); if (r > 0) g.roundRect(x, y, w, h, r); else g.rect(x, y, w, h); g.fill(); } // 4. 顶部微光高光带(让按钮/木牌有温润手作光泽) if (style.highlight && w > 24 && h > 16) { const inset = Math.max(4, Math.min(r * 0.65, 16)); g.strokeColor = hex(style.highlight); g.lineWidth = 2; g.moveTo(x + inset, y + h - 2.5); g.lineTo(x + w - inset, y + h - 2.5); g.stroke(); } // 5. 外描边 if (style.stroke) { g.strokeColor = hex(style.stroke); g.lineWidth = style.strokeWidth ?? 2; if (r > 0) g.roundRect(x, y, w, h, r); else g.rect(x, y, w, h); g.stroke(); } // 6. 内侧手缝虚线 if (style.stitchColor) { const inset = style.stitchInset ?? 8; if (w > inset * 2 + 16 && h > inset * 2 + 16) { drawDashedRect(g, x + inset, y + inset, w - inset * 2, h - inset * 2, style.stitchColor); } } // 7. 四角黄铜铆钉 if (style.rivets && w > 48 && h > 40) { const pad = Math.max(12, Math.min(18, r * 0.65)); const pts = [ [x + pad, y + h - pad], [x + w - pad, y + h - pad], [x + pad, y + pad], [x + w - pad, y + pad], ]; for (const [rx, ry] of pts) { g.fillColor = hex('#2c1e1255'); g.circle(rx, ry - 1.2, 3.6); g.fill(); g.fillColor = hex(style.rivetColor ?? '#d9ad62'); g.circle(rx, ry, 3.2); g.fill(); g.fillColor = hex('#fff2cfcc'); g.circle(rx - 0.9, ry + 0.9, 1.1); g.fill(); } } } export function createPanel(name: string, w: number, h: number, style: BoxStyle, parent?: Node): { node: Node; graphics: Graphics } { const node = createUINode(name, w, h, parent); const graphics = node.addComponent(Graphics); drawRoundedBox(graphics, w, h, style); return { node, graphics }; } export interface LabelStyle { size?: number; lineHeight?: number; color?: string; bold?: boolean; align?: number; verticalAlign?: number; overflow?: number; } export function createLabel(name: string, text: string, w: number, h: number, style: LabelStyle = {}, parent?: Node): Label { const node = createUINode(name, w, h, parent); const label = node.addComponent(Label); label.string = text; label.fontSize = style.size ?? 24; label.lineHeight = style.lineHeight ?? Math.round((style.size ?? 24) * 1.25); label.color = hex(style.color ?? '#302e22'); label.isBold = Boolean(style.bold); label.horizontalAlign = style.align ?? Label.HorizontalAlign.CENTER; label.verticalAlign = style.verticalAlign ?? Label.VerticalAlign.CENTER; if (style.overflow !== undefined) label.overflow = style.overflow; return label; } export interface ButtonSpec { name: string; text: string; w: number; h: number; fill: string; stroke?: string; depth?: number; depthColor?: string; shadow?: number; highlight?: string; stitchColor?: string; textColor?: string; fontSize?: number; radius?: number; bold?: boolean; onClick: () => void; } export interface StyledButton { node: Node; button: Button; label: Label; graphics: Graphics; setStyle: (fill: string, stroke: string | undefined, textColor: string, depthColor?: string) => void; setEnabled: (enabled: boolean) => void; } export function createButton(spec: ButtonSpec, parent?: Node): StyledButton { const radius = spec.radius ?? Math.min(22, spec.h / 2); const depth = spec.depth ?? 5; const shadow = spec.shadow ?? 3; const highlight = spec.highlight ?? '#ffffff55'; const defaultDepth = spec.depthColor ?? spec.stroke ?? '#3c4530'; const { node, graphics } = createPanel( spec.name, spec.w, spec.h, { fill: spec.fill, stroke: spec.stroke, strokeWidth: 2, radius, depth, depthColor: defaultDepth, shadow, highlight, stitchColor: spec.stitchColor, stitchInset: 6, }, parent, ); const label = createLabel( 'Label', spec.text, spec.w - 14, spec.h, { size: spec.fontSize ?? 24, color: spec.textColor ?? '#302e22', bold: spec.bold ?? true, overflow: Label.Overflow.SHRINK, }, node, ); const button = node.addComponent(Button); button.transition = Button.Transition.SCALE; button.zoomScale = 0.93; node.on(Button.EventType.CLICK, () => { if (button.interactable) spec.onClick(); }); return { node, button, label, graphics, setStyle(fill: string, stroke: string | undefined, textColor: string, depthColor?: string) { drawRoundedBox(graphics, spec.w, spec.h, { fill, stroke, strokeWidth: 2, radius, depth, depthColor: depthColor ?? stroke ?? defaultDepth, shadow, highlight, stitchColor: spec.stitchColor, stitchInset: 6, }); label.color = hex(textColor); }, setEnabled(enabled: boolean) { button.interactable = enabled; const base = label.color.clone(); base.a = enabled ? 255 : 115; label.color = base; }, }; } export interface DockButtonSpec { name: string; icon: string; text: string; w: number; h: number; fill: string; stroke: string; depthColor: string; iconBg: string; iconColor: string; textColor: string; onClick: () => void; } export interface DockButton extends StyledButton { iconLabel: Label; iconBgGraphics: Graphics; setDockStyle: (fill: string, stroke: string, depthColor: string, iconBg: string, iconColor: string, textColor: string) => void; } /** 底部游戏工具栏专用:上图标徽章 + 下短文字 + 3D 实体木/陶按压底座 */ export function createDockButton(spec: DockButtonSpec, parent?: Node): DockButton { const radius = 20; const { node, graphics } = createPanel( spec.name, spec.w, spec.h, { fill: spec.fill, stroke: spec.stroke, strokeWidth: 2, radius, depth: 6, depthColor: spec.depthColor, shadow: 4, highlight: '#fffdf466', }, parent, ); const badge = createPanel('IconBadge', 44, 44, { fill: spec.iconBg, radius: 22 }, node); badge.node.setPosition(0, 14, 0); const iconLabel = createLabel('Icon', spec.icon, 44, 44, { size: 25, color: spec.iconColor, bold: true }, badge.node); const label = createLabel( 'Label', spec.text, spec.w - 10, 26, { size: 18, color: spec.textColor, bold: true, overflow: Label.Overflow.SHRINK }, node, ); label.node.setPosition(0, -25, 0); const button = node.addComponent(Button); button.transition = Button.Transition.SCALE; button.zoomScale = 0.91; node.on(Button.EventType.CLICK, () => { if (button.interactable) spec.onClick(); }); const applyDock = (fill: string, stroke: string, depthColor: string, iconBg: string, iconColor: string, textColor: string) => { drawRoundedBox(graphics, spec.w, spec.h, { fill, stroke, strokeWidth: 2, radius, depth: 6, depthColor, shadow: 4, highlight: '#fffdf466', }); drawRoundedBox(badge.graphics, 44, 44, { fill: iconBg, radius: 22 }); iconLabel.color = hex(iconColor); label.color = hex(textColor); }; return { node, button, label, graphics, iconLabel, iconBgGraphics: badge.graphics, setStyle(fill: string, stroke: string | undefined, textColor: string, depthColor?: string) { applyDock(fill, stroke ?? spec.stroke, depthColor ?? stroke ?? spec.depthColor, spec.iconBg, spec.iconColor, textColor); }, setDockStyle: applyDock, setEnabled(enabled: boolean) { button.interactable = enabled; const tc = label.color.clone(); tc.a = enabled ? 255 : 110; label.color = tc; const ic = iconLabel.color.clone(); ic.a = enabled ? 255 : 110; iconLabel.color = ic; }, }; } function makeOffscreenCanvas(w: number, h: number): any { const c = typeof wx !== 'undefined' && typeof wx.createCanvas === 'function' ? wx.createCanvas() : document.createElement('canvas'); c.width = w; c.height = h; return c; } function canvasToSpriteFrame(canvas: any): SpriteFrame { const img = new ImageAsset(canvas); const tex = new Texture2D(); tex.image = img; const sf = new SpriteFrame(); sf.texture = tex; return sf; } let cachedPlayBg: SpriteFrame | null = null; let cachedLobbyBg: SpriteFrame | null = null; /** 生成对局场景全屏「手作刺绣工坊桌面」纹理(苔绿麻布台垫 + 暖光斜照 + 木桌边沿 + 角落小道具) */ export function createAtelierTableSpriteFrame(): SpriteFrame { if (cachedPlayBg) return cachedPlayBg; const W = 750; const H = 1334; const canvas = makeOffscreenCanvas(W, H); const ctx: CanvasRenderingContext2D = canvas.getContext('2d'); // 1. 基底:苔藓绿手作亚麻桌垫渐变(模拟从左上方洒下的自然窗光) const baseGrad = ctx.createRadialGradient(210, 260, 40, W * 0.52, H * 0.48, 860); baseGrad.addColorStop(0, '#728562'); baseGrad.addColorStop(0.42, '#5c6e4f'); baseGrad.addColorStop(0.82, '#47573d'); baseGrad.addColorStop(1, '#36432e'); ctx.fillStyle = baseGrad; ctx.fillRect(0, 0, W, H); // 2. 亚麻细密织纹 ctx.save(); ctx.strokeStyle = 'rgba(255, 248, 226, 0.045)'; ctx.lineWidth = 1; for (let x = 0; x < W; x += 6) { ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, H); ctx.stroke(); } ctx.strokeStyle = 'rgba(20, 28, 16, 0.05)'; for (let y = 0; y < H; y += 6) { ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(W, y); ctx.stroke(); } ctx.restore(); // 3. 左右极细原木桌沿(带来真实桌面的边界感,但绝不挤占空间) const drawWoodTrim = (x0: number, w: number) => { const g = ctx.createLinearGradient(x0, 0, x0 + w, 0); g.addColorStop(0, '#6e492b'); g.addColorStop(0.5, '#8c603c'); g.addColorStop(1, '#583920'); ctx.fillStyle = g; ctx.fillRect(x0, 0, w, H); }; drawWoodTrim(0, 10); drawWoodTrim(W - 10, 10); // 4. 内侧细金线压边(刺绣桌垫滚边) ctx.save(); ctx.strokeStyle = 'rgba(244, 224, 174, 0.24)'; ctx.lineWidth = 2; ctx.setLineDash([8, 6]); ctx.strokeRect(20, 20, W - 40, H - 40); ctx.restore(); // 5. 柔和斜射日光斑(氛围层次) ctx.save(); const sunGrad = ctx.createLinearGradient(0, 0, 560, 620); sunGrad.addColorStop(0, 'rgba(255, 245, 212, 0.14)'); sunGrad.addColorStop(0.5, 'rgba(255, 245, 212, 0.03)'); sunGrad.addColorStop(1, 'rgba(255, 245, 212, 0)'); ctx.fillStyle = sunGrad; ctx.beginPath(); ctx.moveTo(0, 0); ctx.lineTo(520, 0); ctx.lineTo(750, 480); ctx.lineTo(750, 720); ctx.lineTo(0, 240); ctx.closePath(); ctx.fill(); ctx.restore(); // 6. 绣绷底座大柔影(让中央木绣绷宛如真实搁在桌面上) ctx.save(); const hoopShadow = ctx.createRadialGradient(W / 2, 654, 240, W / 2, 668, 368); hoopShadow.addColorStop(0, 'rgba(18, 25, 14, 0.42)'); hoopShadow.addColorStop(0.72, 'rgba(18, 25, 14, 0.18)'); hoopShadow.addColorStop(1, 'rgba(18, 25, 14, 0)'); ctx.fillStyle = hoopShadow; ctx.beginPath(); ctx.arc(W / 2, 658, 368, 0, Math.PI * 2); ctx.fill(); ctx.restore(); // 7. 桌面四角低调手作装饰(角落暗纹藤叶 + 右下角小木线轴剪影,绝不抢戏) ctx.save(); ctx.strokeStyle = 'rgba(240, 224, 182, 0.14)'; ctx.lineWidth = 2; const drawCornerLeaf = (cx: number, cy: number, sx: number, sy: number) => { ctx.save(); ctx.translate(cx, cy); ctx.scale(sx, sy); ctx.beginPath(); ctx.moveTo(0, 48); ctx.quadraticCurveTo(6, 6, 48, 0); ctx.stroke(); ctx.beginPath(); ctx.ellipse(22, 16, 10, 4.5, -0.5, 0, Math.PI * 2); ctx.stroke(); ctx.beginPath(); ctx.ellipse(14, 30, 9, 4, -1.0, 0, Math.PI * 2); ctx.stroke(); ctx.restore(); }; drawCornerLeaf(32, 232, 1, 1); drawCornerLeaf(W - 32, 232, -1, 1); drawCornerLeaf(32, 1142, 1, -1); drawCornerLeaf(W - 32, 1142, -1, -1); ctx.restore(); // 8. 底部深暗脚垫渐变(托举底部木质工具盘) const bottomGrad = ctx.createLinearGradient(0, H - 240, 0, H); bottomGrad.addColorStop(0, 'rgba(24, 31, 19, 0)'); bottomGrad.addColorStop(1, 'rgba(22, 29, 17, 0.42)'); ctx.fillStyle = bottomGrad; ctx.fillRect(0, H - 240, W, 240); cachedPlayBg = canvasToSpriteFrame(canvas); return cachedPlayBg; } /** 生成大厅场景全屏「温暖原木手作书案」纹理(暖橡木桌底 + 苔绿亚麻桌旗 + 阳光光晕) */ export function createLobbyDeskSpriteFrame(): SpriteFrame { if (cachedLobbyBg) return cachedLobbyBg; const W = 750; const H = 1334; const canvas = makeOffscreenCanvas(W, H); const ctx: CanvasRenderingContext2D = canvas.getContext('2d'); // 1. 暖杏色手工木案渐变 const deskGrad = ctx.createLinearGradient(0, 0, 0, H); deskGrad.addColorStop(0, '#627354'); deskGrad.addColorStop(0.24, '#56674a'); deskGrad.addColorStop(0.242, '#eadbc0'); deskGrad.addColorStop(0.55, '#f2e6cf'); deskGrad.addColorStop(1, '#dfd0b1'); ctx.fillStyle = deskGrad; ctx.fillRect(0, 0, W, H); // 2. 顶部苔绿工坊幕布区域(给游戏标题与今日绣绷展示台营造景深) const topCloth = ctx.createRadialGradient(W * 0.35, 110, 30, W * 0.5, 190, 520); topCloth.addColorStop(0, '#6f825e'); topCloth.addColorStop(0.55, '#546548'); topCloth.addColorStop(1, '#3f4e36'); ctx.fillStyle = topCloth; ctx.beginPath(); ctx.moveTo(0, 0); ctx.lineTo(W, 0); ctx.lineTo(W, 338); ctx.quadraticCurveTo(W / 2, 372, 0, 338); ctx.closePath(); ctx.fill(); // 3. 顶部幕布金线滚边与波浪收口 ctx.save(); ctx.strokeStyle = 'rgba(244, 221, 168, 0.55)'; ctx.lineWidth = 3; ctx.beginPath(); ctx.moveTo(0, 338); ctx.quadraticCurveTo(W / 2, 372, W, 338); ctx.stroke(); ctx.strokeStyle = 'rgba(255, 245, 218, 0.25)'; ctx.lineWidth = 1.6; ctx.setLineDash([7, 6]); ctx.beginPath(); ctx.moveTo(18, 326); ctx.quadraticCurveTo(W / 2, 358, W - 18, 326); ctx.stroke(); ctx.restore(); // 4. 下半部分亚麻纸/木纹细肌理 ctx.save(); ctx.strokeStyle = 'rgba(138, 112, 74, 0.045)'; ctx.lineWidth = 1; for (let y = 360; y < H; y += 7) { ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(W, y); ctx.stroke(); } ctx.strokeStyle = 'rgba(255, 250, 238, 0.14)'; for (let x = 0; x < W; x += 9) { ctx.beginPath(); ctx.moveTo(x, 360); ctx.lineTo(x, H); ctx.stroke(); } ctx.restore(); // 5. 左右原木桌框边条 const drawSideWood = (x0: number, w: number) => { const g = ctx.createLinearGradient(x0, 0, x0 + w, 0); g.addColorStop(0, '#7c5534'); g.addColorStop(0.5, '#9d7048'); g.addColorStop(1, '#684528'); ctx.fillStyle = g; ctx.fillRect(x0, 0, w, H); }; drawSideWood(0, 8); drawSideWood(W - 8, 8); // 6. 顶部柔光粒子光晕(手作温暖感) ctx.save(); const glow = ctx.createRadialGradient(140, 80, 10, 140, 80, 260); glow.addColorStop(0, 'rgba(255, 244, 206, 0.18)'); glow.addColorStop(1, 'rgba(255, 244, 206, 0)'); ctx.fillStyle = glow; ctx.fillRect(0, 0, 420, 340); ctx.restore(); cachedLobbyBg = canvasToSpriteFrame(canvas); return cachedLobbyBg; } /** 创建全屏纹理背景精灵 */ export function createBackdropSprite(name: string, frame: SpriteFrame, parent: Node): Node { const node = createUINode(name, 750, 1334, parent); const sp = node.addComponent(Sprite); sp.sizeMode = Sprite.SizeMode.CUSTOM; sp.spriteFrame = frame; stretchToParent(node); return node; } const uiSpriteCache = new Map(); /** 从 assets/resources/ 异步加载 Meowa 生成的精绘游戏 UI 精灵(自动兼容 SpriteFrame 与 ImageAsset) */ export function loadUiSprite(resourcePath: string, sprite: Sprite): void { const cached = uiSpriteCache.get(resourcePath); if (cached) { if (sprite.isValid) sprite.spriteFrame = cached; return; } resources.load(`${resourcePath}/spriteFrame`, SpriteFrame, (err, sf) => { if (!err && sf) { uiSpriteCache.set(resourcePath, sf); if (sprite.isValid) sprite.spriteFrame = sf; return; } resources.load(resourcePath, ImageAsset, (imgErr, img) => { if (imgErr || !img) return; const tex = new Texture2D(); tex.image = img; const fallbackSf = new SpriteFrame(); fallbackSf.texture = tex; uiSpriteCache.set(resourcePath, fallbackSf); if (sprite.isValid) sprite.spriteFrame = fallbackSf; }); }); } /** 创建加载 Meowa 美术贴图的精灵节点 */ export function createArtSprite(name: string, resourcePath: string, w: number, h: number, parent: Node): { node: Node; sprite: Sprite } { const node = createUINode(name, w, h, parent); const sprite = node.addComponent(Sprite); sprite.sizeMode = Sprite.SizeMode.CUSTOM; loadUiSprite(resourcePath, sprite); return { node, sprite }; } export interface ArtButtonSpec { name: string; resourcePath: string; text?: string; w: number; h: number; textColor?: string; fontSize?: number; textOffsetY?: number; bold?: boolean; onClick: () => void; } export interface ArtButton { node: Node; button: Button; sprite: Sprite; label?: Label; setEnabled: (enabled: boolean) => void; } /** 创建基于 Meowa 手绘贴图的实体按压按钮 */ export function createArtButton(spec: ArtButtonSpec, parent: Node): ArtButton { const { node, sprite } = createArtSprite(spec.name, spec.resourcePath, spec.w, spec.h, parent); let label: Label | undefined; if (spec.text !== undefined) { label = createLabel( 'Label', spec.text, spec.w - 28, spec.h - 8, { size: spec.fontSize ?? 26, color: spec.textColor ?? '#3b240a', bold: spec.bold ?? true, overflow: Label.Overflow.SHRINK, }, node, ); if (spec.textOffsetY) label.node.setPosition(0, spec.textOffsetY, 0); } const button = node.addComponent(Button); button.transition = Button.Transition.SCALE; button.zoomScale = 0.92; node.on(Button.EventType.CLICK, () => { if (button.interactable) spec.onClick(); }); const res: ArtButton = { node, button, sprite, label, setEnabled(enabled: boolean) { button.interactable = enabled; sprite.color = enabled ? hex('#ffffff') : hex('#c2b8a8'); if (res.label) { const c = res.label.color.clone(); c.a = enabled ? 255 : 155; res.label.color = c; } }, }; return res; } export function stretchToParent(node: Node): Widget { const widget = node.addComponent(Widget); widget.isAlignTop = true; widget.isAlignBottom = true; widget.isAlignLeft = true; widget.isAlignRight = true; widget.top = 0; widget.bottom = 0; widget.left = 0; widget.right = 0; widget.alignMode = Widget.AlignMode.ON_WINDOW_RESIZE; return widget; }