A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat: Add font selector and fix Text component inspector
esenginee5c9d3c1e459
diff --git a/.vscode/tasks.json b/.vscode/tasks.jsonindex 10eed98c6..41da42710 100644--- a/.vscode/tasks.json+++ b/.vscode/tasks.json@@ -18,6 +18,16 @@},"problemMatcher": ["$gcc"]},+ {+ "label": "build-sdk",+ "type": "shell",+ "command": "pnpm",+ "args": ["build"],+ "options": {+ "cwd": "${workspaceFolder}/sdk"+ },+ "problemMatcher": ["$tsc"]+ },{"label": "build-editor","type": "shell",@@ -26,6 +36,7 @@"options": {"cwd": "${workspaceFolder}/editor"},+ "dependsOn": ["build-sdk"],"problemMatcher": ["$tsc"]},{diff --git a/editor/src/bounds/TextBoundsProvider.ts b/editor/src/bounds/TextBoundsProvider.tsindex d862b5a22..e298fc5b3 100644--- a/editor/src/bounds/TextBoundsProvider.ts+++ b/editor/src/bounds/TextBoundsProvider.ts@@ -22,20 +22,52 @@ export const textBoundsProvider: BoundsProvider = {const fontSize = data.fontSize ?? 24;const fontFamily = data.fontFamily ?? 'Arial';const lineHeight = data.lineHeight ?? 1.2;+ const maxWidthLimit = data.maxWidth ?? 0;const ctx = getContext();ctx.font = `${fontSize}px ${fontFamily}`;- const lines = content.split('\n');+ const lines = wrapText(ctx, content, maxWidthLimit);let maxWidth = 0;for (const line of lines) {maxWidth = Math.max(maxWidth, ctx.measureText(line).width);}- const padding = fontSize * 0.2;return {- width: Math.ceil(maxWidth) + padding * 2,- height: Math.ceil(lines.length * fontSize * lineHeight) + padding * 2+ width: Math.ceil(maxWidth),+ height: Math.ceil(lines.length * fontSize * lineHeight)};}};++function wrapText(ctx: CanvasRenderingContext2D, text: string, maxWidth: number): string[] {+ if (maxWidth <= 0) {+ return text.split('\n');+ }++ const result: string[] = [];+ const paragraphs = text.split('\n');++ for (const paragraph of paragraphs) {+ const words = paragraph.split(' ');+ let currentLine = '';++ for (const word of words) {+ const testLine = currentLine ? `${currentLine} ${word}` : word;+ const metrics = ctx.measureText(testLine);++ if (metrics.width > maxWidth && currentLine) {+ result.push(currentLine);+ currentLine = word;+ } else {+ currentLine = testLine;+ }+ }++ if (currentLine) {+ result.push(currentLine);+ }+ }++ return result.length > 0 ? result : [''];+}diff --git a/editor/src/property/editors.ts b/editor/src/property/editors.tsindex e8ab9ac4e..67273a0c4 100644--- a/editor/src/property/editors.ts+++ b/editor/src/property/editors.ts@@ -819,6 +819,98 @@ function createTextureEditor(};}+// =============================================================================+// Font Editor+// =============================================================================++const AVAILABLE_FONTS = [+ 'Arial',+ 'Helvetica',+ 'Times New Roman',+ 'Georgia',+ 'Verdana',+ 'Tahoma',+ 'Trebuchet MS',+ 'Courier New',+ 'Lucida Console',+ 'Impact',+ 'Comic Sans MS',+ 'Microsoft YaHei',+ 'SimHei',+ 'SimSun',+ 'KaiTi',+ 'FangSong',+];++function getAvailableFonts(): string[] {+ const testString = 'mmmmmmmmmmlli';+ const testSize = '72px';+ const baseline = 'monospace';++ const canvas = document.createElement('canvas');+ const ctx = canvas.getContext('2d')!;+ ctx.font = `${testSize} ${baseline}`;+ const baselineWidth = ctx.measureText(testString).width;++ const available: string[] = [];+ for (const font of AVAILABLE_FONTS) {+ ctx.font = `${testSize} "${font}", ${baseline}`;+ const width = ctx.measureText(testString).width;+ if (width !== baselineWidth) {+ available.push(font);+ }+ }++ return available.length > 0 ? available : ['Arial', 'sans-serif'];+}++function createFontEditor(+ container: HTMLElement,+ ctx: PropertyEditorContext+): PropertyEditorInstance {+ const { value, onChange } = ctx;++ const wrapper = document.createElement('div');+ wrapper.className = 'es-font-editor';++ const select = document.createElement('select');+ select.className = 'es-input es-input-select';++ const fonts = getAvailableFonts();+ const currentFont = String(value ?? 'Arial');++ if (!fonts.includes(currentFont)) {+ fonts.unshift(currentFont);+ }++ for (const font of fonts) {+ const option = document.createElement('option');+ option.value = font;+ option.textContent = font;+ option.style.fontFamily = font;+ if (font === currentFont) {+ option.selected = true;+ }+ select.appendChild(option);+ }++ select.addEventListener('change', () => {+ onChange(select.value);+ });++ wrapper.appendChild(select);+ container.appendChild(wrapper);++ return {+ update(v: unknown) {+ select.value = String(v ?? 'Arial');+ },+ dispose() {+ wrapper.remove();+ },+ };+}+// =============================================================================// Register All Editors// =============================================================================@@ -834,4 +926,5 @@ export function registerBuiltinEditors(): void {registerPropertyEditor('enum', createEnumEditor);registerPropertyEditor('euler', createEulerEditor);registerPropertyEditor('texture', createTextureEditor);+ registerPropertyEditor('font', createFontEditor);}diff --git a/editor/src/schemas/ComponentSchemas.ts b/editor/src/schemas/ComponentSchemas.tsindex a12de6832..956cffffd 100644--- a/editor/src/schemas/ComponentSchemas.ts+++ b/editor/src/schemas/ComponentSchemas.ts@@ -100,27 +100,29 @@ export const TextSchema: ComponentSchema = {category: 'builtin',properties: [{ name: 'content', type: 'string' },+ { name: 'fontFamily', type: 'font' },{ name: 'fontSize', type: 'number', min: 8, max: 200 },- { name: 'fontFamily', type: 'string' },{ name: 'color', type: 'color' },{name: 'align',type: 'enum',options: [- { label: 'Left', value: 'left' },- { label: 'Center', value: 'center' },- { label: 'Right', value: 'right' },+ { label: 'Left', value: 0 },+ { label: 'Center', value: 1 },+ { label: 'Right', value: 2 },],},{name: 'baseline',type: 'enum',options: [- { label: 'Top', value: 'top' },- { label: 'Middle', value: 'middle' },- { label: 'Bottom', value: 'bottom' },+ { label: 'Top', value: 0 },+ { label: 'Middle', value: 1 },+ { label: 'Bottom', value: 2 },],},+ { name: 'maxWidth', type: 'number', min: 0 },+ { name: 'lineHeight', type: 'number', min: 0.5, max: 3, step: 0.1 },],};@@ -176,6 +178,11 @@ function registerUserComponent(defaults: Record<string, unknown>,isTag: boolean): void {+ const existing = schemaRegistry.get(name);+ if (existing?.category === 'builtin') {+ return;+ }+const schema: ComponentSchema = {name,category: isTag ? 'tag' : 'script',