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(tileset): animated tiles get an authoring surface
yhh02c50181a8d4
diff --git a/desktop/src/panels/TilesetEditor.tsx b/desktop/src/panels/TilesetEditor.tsxindex ec5dc45e7..9f2a50e1f 100644--- a/desktop/src/panels/TilesetEditor.tsx+++ b/desktop/src/panels/TilesetEditor.tsx@@ -17,10 +17,10 @@ import {useEffect, useRef, useState, useSyncExternalStore,type CSSProperties, type MouseEvent as ReactMouseEvent,} from 'react';-import { Save, Plus, Trash2 } from 'lucide-react';+import { Save, Plus, Trash2, X } from 'lucide-react';import {TB_N, TB_E, TB_S, TB_W, TB_NE, TB_SE, TB_SW, TB_NW,- type TilesetAsset,+ type TilesetAsset, type TilesetAnimFrame,} from 'esengine';import { Segmented } from '@/components/Segmented';import { Select } from '@/components/Select';@@ -40,6 +40,35 @@ function rowsFor(height: number, tileH: number, margin: number, spacing: number)const TERRAIN_COLORS = ['#4caf50', '#d6884c', '#4c8fd6', '#b14cd6', '#d6c64c', '#d64c6e'];+/** Loops the frame strip at each frame's own duration; falls back to the target tile. */+function AnimPreview({ frames, fallback, thumb }: {+ frames: TilesetAnimFrame[];+ fallback: number;+ thumb: (tile: number) => CSSProperties;+}) {+ const [i, setI] = useState(0);+ useEffect(() => {+ setI(0);+ if (frames.length < 2) return;+ let idx = 0;+ let live = true;+ let t: ReturnType<typeof setTimeout>;+ const tick = () => {+ if (!live) return;+ idx = (idx + 1) % frames.length;+ setI(idx);+ t = setTimeout(tick, frames[idx].durationMs || 120);+ };+ t = setTimeout(tick, frames[0].durationMs || 120);+ return () => {+ live = false;+ clearTimeout(t);+ };+ }, [frames]);+ const tile = frames.length ? frames[Math.min(i, frames.length - 1)].tile : fallback;+ return <span className="ts-fthumb ts-apreview" style={thumb(tile)} title="Preview" />;+}+/** A grid-geometry number field that commits on blur/Enter (one undo step per edit). */function GridField(props: { label: string; value: number; min?: number; onCommit: (n: number) => void }) {const [text, setText] = useState(String(props.value));@@ -159,7 +188,8 @@ export function TilesetEditor() {const [natural, setNatural] = useState<{ w: number; h: number } | null>(null);const [zoom, setZoom] = useState(2);- const [mode, setMode] = useState<'collision' | 'terrain'>('collision');+ const [mode, setMode] = useState<'collision' | 'terrain' | 'animation'>('collision');+ const [animTile, setAnimTile] = useState<number | null>(null);const [shape, setShape] = useState<'box' | 'polygon'>('box');const [polyTile, setPolyTile] = useState<number | null>(null);const [activeSet, setActiveSet] = useState(0);@@ -217,6 +247,25 @@ export function TilesetEditor() {else TilesetCommands.setTileTerrain(id, activeSet, 0);};+ // ── animation authoring ──+ const animFrames: TilesetAnimFrame[] = animTile != null ? asset.tiles[animTile]?.animation ?? [] : [];+ const setFrames = (frames: TilesetAnimFrame[]) => {+ if (animTile != null) TilesetCommands.setTileAnimation(animTile, frames);+ };+ /** Atlas crop for a tile id at thumbnail size (same math as the cell layout). */+ const THUMB = 26;+ const thumb = (tile: number): CSSProperties => {+ if (!texUrl || !natural || tile < 1) return {};+ const c = (tile - 1) % cols;+ const r = Math.floor((tile - 1) / cols);+ const s = THUMB / tw;+ return {+ backgroundImage: `url(${texUrl})`,+ backgroundPosition: `${-(mg + c * (tw + sp)) * s}px ${-(mg + r * (th + sp)) * s}px`,+ backgroundSize: `${natural.w * s}px ${natural.h * s}px`,+ };+ };+const cells = [];if (texUrl && natural) {for (let row = 0; row < rows; row++) {@@ -253,6 +302,21 @@ export function TilesetEditor() {}}/>,);+ } else if (mode === 'animation') {+ const hasAnim = !!asset.tiles[id]?.animation?.length;+ cells.push(+ <div+ key={id}+ className={'ts-cell ts-acell' + (hasAnim ? ' has-anim' : '') + (animTile === id ? ' is-target' : '')}+ style={{ left, top, width: w, height: h }}+ title={animTile == null ? `#${id} — click to edit its animation` : `#${id} — click to append as a frame`}+ onPointerDown={(e) => {+ e.preventDefault();+ if (animTile == null) setAnimTile(id);+ else setFrames([...animFrames, { tile: id, durationMs: 120 }]);+ }}+ />,+ );} else {const tt = tileTerrain(id);const showZones = hovered === id || tt != null;@@ -310,6 +374,7 @@ export function TilesetEditor() {options={[{ value: 'collision', label: 'Collision' },{ value: 'terrain', label: 'Terrain' },+ { value: 'animation', label: 'Animation' },]}/>{mode === 'collision' && (@@ -338,6 +403,56 @@ export function TilesetEditor() {</button></div>+ {mode === 'animation' && (+ <div className="ts-anims">+ {animTile == null ? (+ <span className="ts-ahint">Click a tile in the atlas to edit its animation — animated tiles carry a ▶ mark</span>+ ) : (+ <>+ <AnimPreview frames={animFrames} fallback={animTile} thumb={thumb} />+ <span className="ts-astat">#{animTile}</span>+ <span className="ts-sep" />+ {animFrames.map((f, i) => (+ <span key={`${i}-${f.tile}`} className="ts-frame">+ <span className="ts-fthumb" style={thumb(f.tile)} title={`#${f.tile}`} />+ <input+ key={`${animTile}-${i}-${f.durationMs}`}+ className="ts-fdur"+ defaultValue={f.durationMs}+ title="Frame duration (ms)"+ spellCheck={false}+ onBlur={(e) => {+ const n = parseInt(e.target.value, 10);+ if (Number.isFinite(n) && n > 0 && n !== f.durationMs)+ setFrames(animFrames.map((g, j) => (j === i ? { ...g, durationMs: n } : g)));+ }}+ onKeyDown={(e) => {+ if (e.key === 'Enter') (e.target as HTMLInputElement).blur();+ }}+ />+ <button+ type="button" className="ts-fx" title="Remove frame"+ onClick={() => setFrames(animFrames.filter((_, j) => j !== i))}+ >+ <X size={11} strokeWidth={2.2} />+ </button>+ </span>+ ))}+ <span className="ts-ahint">+ {animFrames.length === 0 ? 'Click atlas tiles to add frames' : 'Click atlas tiles to append frames'}+ </span>+ <span className="ts-grow" />+ {animFrames.length > 0 && (+ <button type="button" className="ts-trm" title="Clear animation" onClick={() => setFrames([])}>+ <Trash2 size={13} />+ </button>+ )}+ <button type="button" className="ts-terrain" onClick={() => setAnimTile(null)}>Done</button>+ </>+ )}+ </div>+ )}+{mode === 'terrain' && (<div className="ts-terrains">{terrains.map((t, i) => (diff --git a/desktop/src/theme/tileset.css b/desktop/src/theme/tileset.cssindex 784a000a3..b725c5834 100644--- a/desktop/src/theme/tileset.css+++ b/desktop/src/theme/tileset.css@@ -219,6 +219,30 @@.ts-pe-pt:hover { fill: #f0645a; }.ts-pe-hint { color: var(--text-faint); font-size: var(--fs-xs); text-align: center; }+/* Animation mode: the frame strip under the toolbar + atlas cell markers. */+.ts-anims {+ display: flex;+ align-items: center;+ gap: 6px;+ flex-wrap: wrap;+ padding: 6px 10px;+ border-bottom: 1px solid var(--line);+ background: var(--srf-2);+ flex: 0 0 auto;+}+.ts-ahint { color: var(--text-faint); font-size: var(--fs-xs); }+.ts-astat { font-family: var(--mono); color: var(--text-dim); font-size: var(--fs-xs); }+.ts-frame { display: inline-flex; align-items: center; gap: 3px; background: var(--srf-3); border: 1px solid var(--line); border-radius: var(--r-sm); padding: 2px; }+.ts-fthumb { width: 26px; height: 26px; border-radius: var(--r-xs); background-color: var(--srf-0); image-rendering: pixelated; flex: none; }+.ts-apreview { width: 32px; height: 32px; border: 1px solid var(--star-line); }+.ts-fdur { width: 44px; background: var(--inset); border: 1px solid var(--line); border-radius: var(--r-xs); color: var(--text); padding: 1px 3px; font-family: var(--mono); font-size: var(--fs-2xs); }+.ts-fdur:focus { outline: none; border-color: var(--acc); }+.ts-fx { display: grid; place-items: center; width: 16px; height: 16px; color: var(--text-mute); border-radius: var(--r-xs); transition: color var(--t-fast), background var(--t-fast); }+.ts-fx:hover { color: var(--error); background: var(--srf-4); }+.ts-acell.has-anim { border-color: var(--star-line); box-shadow: inset 0 0 0 1px var(--star-line); }+.ts-acell.has-anim::after { content: '▶'; position: absolute; right: 2px; bottom: 1px; font-size: 8px; line-height: 1; color: var(--star-hi); }+.ts-acell.is-target { border-color: var(--star); box-shadow: inset 0 0 0 1px var(--star); }+.ts-empty,.ts-warn {display: flex;diff --git a/desktop/src/tileset/TilesetCommands.ts b/desktop/src/tileset/TilesetCommands.tsindex 859fa9f1d..a46594be0 100644--- a/desktop/src/tileset/TilesetCommands.ts+++ b/desktop/src/tileset/TilesetCommands.ts@@ -8,7 +8,7 @@*/import { serializeTileset } from 'esengine';-import type { TilesetAsset, TilesetTerrain, TerrainMode } from 'esengine';+import type { TilesetAsset, TilesetTerrain, TerrainMode, TilesetAnimFrame } from 'esengine';import { TilesetDocument } from './TilesetDocument';import { Toasts } from '@/store/Toasts';@@ -116,6 +116,26 @@ export const TilesetCommands = {});},+ /**+ * Replace a tile's animation frame list as ONE undo step. An empty (or+ * all-invalid) list clears the animation; frames are sanitized to positive+ * integer tile ids and durations.+ */+ setTileAnimation(id: number, frames: TilesetAnimFrame[]): void {+ if (id <= 0) return;+ TilesetDocument.edit('Edit Tile Animation', (a) => {+ const clean = frames+ .filter((f) => f.tile > 0 && f.durationMs > 0)+ .map((f) => ({ tile: Math.floor(f.tile), durationMs: Math.round(f.durationMs) }));+ if (clean.length > 0) {+ a.tiles[id] = { ...(a.tiles[id] ?? {}), animation: clean };+ } else if (a.tiles[id]?.animation) {+ delete a.tiles[id].animation;+ pruneEmpty(a, id);+ }+ });+ },+/** Persist the open tileset to its file. */async save(): Promise<void> {const asset = TilesetDocument.asset;