Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | import keyBy from "lodash/keyBy"; import uniq from "lodash/uniq"; import React, { FC, useEffect, useLayoutEffect, useRef, useState } from "react"; import { useAppSelector } from "store/hooks"; import { fontSelectors } from "store/features/entities/entitiesState"; import { loadFont, drawFrame, drawText, FontData } from "./TextPreviewHelper"; import { assetURL } from "shared/lib/helpers/assets"; interface MenuPreviewProps { items: string[]; layout: "dialogue" | "menu"; } export const MenuPreview: FC<MenuPreviewProps> = ({ items, layout }) => { const uiVersion = useAppSelector((state) => state.editor.uiVersion); const fonts = useAppSelector((state) => fontSelectors.selectAll(state)); const fontsLookup = useAppSelector((state) => fontSelectors.selectEntities(state) ); const defaultFontId = useAppSelector( (state) => state.project.present.settings.defaultFontId || fonts[0]?.id ); const [frameImage, setFrameImage] = useState<HTMLImageElement>(); const [cursorImage, setCursorImage] = useState<HTMLImageElement>(); const [fontsData, setFontsData] = useState<Record<string, FontData>>({}); const [drawn, setDrawn] = useState<boolean>(false); const ref = useRef<HTMLCanvasElement>(null); const frameAsset = { id: "frame", name: "Window Frame", filename: `frame.png`, _v: uiVersion, }; const frameAssetURL = assetURL("ui", frameAsset); const cursorAsset = { id: "cursor", name: "Window Cursor", filename: `cursor.png`, _v: uiVersion, }; const cursorAssetURL = assetURL("ui", cursorAsset); useEffect(() => { async function fetchData() { const usedFontIds = uniq( items.flatMap((item) => ([] as string[]).concat( defaultFontId, (String(item).match(/(!F:[0-9a-f-]+!)/g) || []) // Add fonts referenced in text .map((id) => id.substring(3).replace(/!$/, "")) ) ) ); const usedFonts = usedFontIds.map((id) => fontsLookup[id] || fonts[0]); const usedFontData = await Promise.all(usedFonts.map(loadFont)); setFontsData(keyBy(usedFontData, "id")); } fetchData(); }, [defaultFontId, fonts, fontsLookup, items]); // Load frame image useEffect(() => { const img = new Image(); img.src = frameAssetURL; img.onload = () => { setFrameImage(img); }; }, [frameAssetURL]); // Load cursor image useEffect(() => { const img = new Image(); img.src = cursorAssetURL; img.onload = () => { setCursorImage(img); }; }, [cursorAssetURL]); useLayoutEffect(() => { Iif (ref.current && frameImage && cursorImage) { const canvas = ref.current; const ctx = canvas.getContext("2d"); // eslint-disable-next-line no-self-assign canvas.width = canvas.width; Iif (ctx) { const tileWidth = layout === "dialogue" ? 20 : 10; const tileHeight = (layout === "dialogue" ? Math.min(items.length, 4) : items.length) + 2; canvas.width = tileWidth * 8; canvas.height = tileHeight * 8; drawFrame(ctx, frameImage, tileWidth, tileHeight); items.forEach((item, i) => { const x = layout === "dialogue" ? 16 + 9 * 8 * Math.floor(i / 4) : 16; const y = layout === "dialogue" ? 8 + (i % 4) * 8 : 8 + i * 8; drawText( ctx, item || `Item ${i + 1}`, x, y, Infinity, fontsData, defaultFontId, fonts[0]?.id ); }); ctx.drawImage(cursorImage, 8, 8); } setDrawn(true); } }, [ ref, frameImage, fontsData, defaultFontId, fonts, layout, items, cursorImage, ]); return ( <canvas ref={ref} width={160} height={48} style={{ width: layout === "dialogue" ? 240 : 120, imageRendering: "pixelated", boxShadow: "5px 5px 10px 0px rgba(0,0,0,0.5)", borderRadius: 4, opacity: drawn ? 1 : 0, }} /> ); }; |