Skip to content

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.

feat(tilemap): layer panel (new/opacity) + palette collision/terrain/anim badges

esenginef6044ebe4146
diff --git a/desktop/src/panels/TilemapPainter.tsx b/desktop/src/panels/TilemapPainter.tsx
index abea3071e..e291a8404 100644
--- a/desktop/src/panels/TilemapPainter.tsx
+++ b/desktop/src/panels/TilemapPainter.tsx
@@ -26,6 +26,8 @@ import { MOD_LABEL } from '@/commands/keybinding';
import { buildStampGhost } from '@/tools/tileStampGhost';
import { colsFor, rowsFor, TERRAIN_COLORS } from '@/tools/tileMath';
import { loadTilesetAsset } from '@/tileset/loadTileset';
+import { createTilemapFromTileset } from '@/tilemap/createTilemap';
+import { AnimPreview, tileThumbStyle, type TileAtlas } from '@/tools/tileThumb';
import { IconButton } from '@/components/IconButton';
const TOOLS: { id: PaintTool; icon: typeof Brush; label: string }[] = [
@@ -235,7 +237,22 @@ export function TilemapPainter() {
const layers = SceneModel.entityOrder()
.map((id) => ({ id, e: SceneModel.entityBySource(id) }))
.filter((L) => L.e?.components.some((c) => c.type === 'TilemapLayer'))
- .map((L) => ({ id: L.id, name: L.e!.name, hidden: SceneModel.isHidden(L.id), locked: SceneModel.isLocked(L.id) }));
+ .map((L) => {
+ const data = L.e!.components.find((c) => c.type === 'TilemapLayer')!.data as { opacity?: number };
+ return {
+ id: L.id,
+ name: L.e!.name,
+ hidden: SceneModel.isHidden(L.id),
+ locked: SceneModel.isLocked(L.id),
+ opacity: typeof data.opacity === 'number' ? data.opacity : 1,
+ };
+ });
+
+ // New layer: another TilemapLayer entity on the active tileset (a sibling paint
+ // target). Opacity edits coalesce into one undo step per slider drag.
+ const addLayer = () => { if (tilesetPath) void createTilemapFromTileset(tilesetPath); };
+ const setLayerOpacity = (id: number, v: number) =>
+ SceneCommands.setField(id, 'TilemapLayer', 'opacity', 'number', v);
const commitSel = (r: SelRect) => {
const w = r.c1 - r.c0 + 1;
@@ -306,11 +323,18 @@ export function TilemapPainter() {
? ProjectStore.listAssets('tileset').filter((a) => !selectedTilemapTilesetRefs(selectedId).includes(a.ref))
: [];
+ const atlas: TileAtlas | null = texUrl && natural
+ ? { url: texUrl, naturalW: natural.w, naturalH: natural.h, cols, tileW: tw, tileH: th, margin: mg, spacing: sp }
+ : null;
const cells = [];
if (texUrl && natural) {
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const id = row * cols + col + activeFirstId;
+ // asset.tiles is keyed by the tileset-local 1-based id (== TilesetEditor's).
+ const meta = asset?.tiles[row * cols + col + 1];
+ const anim = meta?.animation;
+ const ter = meta?.terrain;
cells.push(
<div
key={id}
@@ -325,7 +349,25 @@ export function TilemapPainter() {
onPointerEnter={() => {
if (dragAnchor.current) setSel(normRect(dragAnchor.current, { c: col, r: row }));
}}
- />,
+ >
+ {anim && anim.length > 1 && atlas && (
+ <AnimPreview
+ frames={anim}
+ fallback={row * cols + col + 1}
+ className="tp-cell-anim"
+ thumb={(t) => tileThumbStyle(atlas, t, tw)}
+ />
+ )}
+ {meta?.collision && <span className="tp-badge tp-badge-col" title="Has collision" />}
+ {ter != null && (
+ <span
+ className="tp-badge tp-badge-ter"
+ style={{ background: TERRAIN_COLORS[ter.set % TERRAIN_COLORS.length] }}
+ title="Terrain member"
+ />
+ )}
+ {anim && anim.length > 0 && <span className="tp-badge tp-badge-anim" title="Animated" />}
+ </div>,
);
}
}
@@ -333,8 +375,11 @@ export function TilemapPainter() {
return (
<div className="tp-panel">
- {layers.length > 1 && (
+ {layers.length > 0 && (
<div className="tp-layers">
+ <IconButton size="sm" title="New layer on this tileset" disabled={!tilesetPath} onClick={addLayer}>
+ <Plus size={14} />
+ </IconButton>
{layers.map((L) => (
<span key={L.id} className={'tp-layer' + (L.id === selectedId ? ' is-active' : '')}>
<button
@@ -349,6 +394,13 @@ export function TilemapPainter() {
>
{L.name}
</button>
+ <input
+ className="tp-layer-op" type="range" min={0} max={1} step={0.05} value={L.opacity}
+ title={`Opacity ${Math.round(L.opacity * 100)}%`}
+ onPointerDown={() => SceneCommands.beginGesture('Layer opacity')}
+ onChange={(e) => setLayerOpacity(L.id, Number(e.target.value))}
+ onPointerUp={() => SceneCommands.endGesture()}
+ />
<button
type="button" className="tp-layer-lock" title={L.locked ? 'Unlock' : 'Lock'}
onClick={() => SceneCommands.setEntityLocked(L.id, !L.locked)}
diff --git a/desktop/src/panels/TilesetEditor.tsx b/desktop/src/panels/TilesetEditor.tsx
index ef10ea71c..b6640f67f 100644
--- a/desktop/src/panels/TilesetEditor.tsx
+++ b/desktop/src/panels/TilesetEditor.tsx
@@ -29,35 +29,7 @@ import { TilesetDocument } from '@/tileset/TilesetDocument';
import { TilesetCommands } from '@/tileset/TilesetCommands';
import { ProjectStore } from '@/project/ProjectStore';
import { colsFor, rowsFor, TERRAIN_COLORS } from '@/tools/tileMath';
-
-/** 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" />;
-}
+import { AnimPreview, tileThumbStyle, type TileAtlas } from '@/tools/tileThumb';
/** 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 }) {
@@ -242,19 +214,12 @@ export function TilesetEditor() {
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). */
+ /** Atlas crop for a tile id at thumbnail size (shared with the painter palette). */
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 atlas: TileAtlas | null = texUrl && natural
+ ? { url: texUrl, naturalW: natural.w, naturalH: natural.h, cols, tileW: tw, tileH: th, margin: mg, spacing: sp }
+ : null;
+ const thumb = (tile: number): CSSProperties => tileThumbStyle(atlas, tile, THUMB);
const cells = [];
if (texUrl && natural) {
@@ -399,7 +364,7 @@ export function TilesetEditor() {
<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} />
+ <AnimPreview frames={animFrames} fallback={animTile} thumb={thumb} className="ts-fthumb ts-apreview" />
<span className="ts-astat">#{animTile}</span>
<span className="ts-sep" />
{animFrames.map((f, i) => (
diff --git a/desktop/src/theme/tilemap.css b/desktop/src/theme/tilemap.css
index 8d7dc05f4..ed3a0ab8d 100644
--- a/desktop/src/theme/tilemap.css
+++ b/desktop/src/theme/tilemap.css
@@ -71,6 +71,7 @@
}
.tp-layer.is-active .tp-layer-name { color: var(--text); }
.tp-layer-name:hover { color: var(--text); }
+.tp-layer-op { width: 44px; margin: 0 4px; align-self: center; accent-color: var(--acc); cursor: pointer; }
.tp-grow { flex: 1 1 auto; }
.tp-sep { width: 1px; align-self: stretch; margin: 2px 4px; background: var(--line); }
.tp-brush {
@@ -137,6 +138,14 @@
inside is drawn at natural size and CSS-scaled to fill it (crisp hit-testing + pixels). */
.tp-atlas-sizer { position: relative; }
.tp-atlas { position: relative; }
+/* Palette cell overlays: an animated tile loops over its static frame; corner
+ badges flag collision (red ◣), terrain membership (set-colored dot), and
+ animation (accent ◢). Live in the zoomed atlas space, so they scale with it. */
+.tp-cell-anim { position: absolute; inset: 0; pointer-events: none; image-rendering: pixelated; }
+.tp-badge { position: absolute; pointer-events: none; z-index: 1; }
+.tp-badge-col { left: 0; top: 0; border-left: 5px solid var(--error); border-bottom: 5px solid transparent; }
+.tp-badge-ter { right: 1px; top: 1px; width: 4px; height: 4px; border-radius: 50%; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.45); }
+.tp-badge-anim { right: 0; bottom: 0; border-right: 5px solid var(--star); border-top: 5px solid transparent; }
.tp-img { display: block; image-rendering: pixelated; user-select: none; }
.tp-terrains {
diff --git a/desktop/src/tools/tileThumb.tsx b/desktop/src/tools/tileThumb.tsx
new file mode 100644
index 000000000..8841be38a
--- /dev/null
+++ b/desktop/src/tools/tileThumb.tsx
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file tileThumb.tsx
+ * @brief Shared tile-thumbnail crop + animation preview — used by both the
+ * Tileset editor and the Tilemap painter's palette, which each need to
+ * show a single tile out of the atlas and loop an animated tile's frames.
+ */
+import { useEffect, useState, type CSSProperties } from 'react';
+import type { TilesetAnimFrame } from 'esengine';
+
+/** The atlas layout a tile is cropped from (a parsed `.estileset`'s grid). */
+export interface TileAtlas {
+ url: string;
+ naturalW: number;
+ naturalH: number;
+ cols: number;
+ tileW: number;
+ tileH: number;
+ margin: number;
+ spacing: number;
+}
+
+/**
+ * A CSS `background` that crops the 1-based `tile` out of `atlas`, scaled so one
+ * cell renders at `size` px. Returns `{}` for an absent atlas or an out-of-range
+ * tile so the caller can spread it unconditionally.
+ */
+export function tileThumbStyle(atlas: TileAtlas | null, tile: number, size: number): CSSProperties {
+ if (!atlas || tile < 1) return {};
+ const c = (tile - 1) % atlas.cols;
+ const r = Math.floor((tile - 1) / atlas.cols);
+ const s = size / atlas.tileW;
+ return {
+ backgroundImage: `url(${atlas.url})`,
+ backgroundPosition: `${-(atlas.margin + c * (atlas.tileW + atlas.spacing)) * s}px ${-(atlas.margin + r * (atlas.tileH + atlas.spacing)) * s}px`,
+ backgroundSize: `${atlas.naturalW * s}px ${atlas.naturalH * s}px`,
+ };
+}
+
+/**
+ * Loops an animated tile's frame strip, each frame shown for its own duration;
+ * falls back to `fallback` when there are no frames. `thumb` maps a tile id to
+ * its crop style (bind it to the atlas + size at the call site).
+ */
+export function AnimPreview({
+ frames,
+ fallback,
+ thumb,
+ className,
+}: {
+ frames: TilesetAnimFrame[];
+ fallback: number;
+ thumb: (tile: number) => CSSProperties;
+ className?: string;
+}) {
+ 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={className} style={thumb(tile)} title="Preview" />;
+}