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): terrain-tool atlas clicks deep-link to the Tileset Editor's Terrain tab

esengine6e59c0e1b388
diff --git a/desktop/src/panels/TilemapPainter.tsx b/desktop/src/panels/TilemapPainter.tsx
index dd34497c7..3d8cc0bb2 100644
--- a/desktop/src/panels/TilemapPainter.tsx
+++ b/desktop/src/panels/TilemapPainter.tsx
@@ -28,6 +28,7 @@ import { buildStampGhost } from '@/tools/tileStampGhost';
import { colsFor, rowsFor, TERRAIN_COLORS } from '@/tools/tileMath';
import { loadTilesetAsset } from '@/tileset/loadTileset';
import { openTileset } from '@/tileset/openTileset';
+import { useTilesetView } from '@/tileset/tilesetView';
import { createTilemapFromTileset } from '@/tilemap/createTilemap';
import { layerTilesetRefs } from '@/tilemap/layerTilesetModel';
import { AnimPreview, tileThumbStyle, type TileAtlas } from '@/tools/tileThumb';
@@ -165,7 +166,15 @@ export function TilemapPainter() {
const texUrl = asset ? `estella://project/${ProjectStore.assetInfo(asset.texture)?.path ?? ''}` : null;
const [natural, setNatural] = useState<{ w: number; h: number } | null>(null);
- useEffect(() => setNatural(null), [texUrl]);
+ // On texture change adopt the size straight from the element when it is
+ // already complete — a cache-hit image can finish loading before React
+ // attaches onLoad, which would leave the palette collapsed at 0×0.
+ const atlasImgRef = useRef<HTMLImageElement | null>(null);
+ useEffect(() => {
+ const img = atlasImgRef.current;
+ if (img && img.complete && img.naturalWidth > 0) setNatural({ w: img.naturalWidth, h: img.naturalHeight });
+ else setNatural(null);
+ }, [texUrl]);
// Publish the active tileset's atlas layout for the viewport's WYSIWYG brush ghost.
// The painter is the one place that loads the atlas image, so it owns this geometry;
@@ -369,9 +378,14 @@ export function TilemapPainter() {
title={`#${id}`}
onPointerDown={(e) => {
e.preventDefault();
- // Picking a tile IS a brush action — leave the terrain tool so the
- // atlas (kept visible for context) never dead-ends.
- if (tool === 'terrain') setTool('brush');
+ // With the terrain tool, an atlas click means "edit this tileset's
+ // terrain" — deep-link into the Tileset Editor's Terrain tab
+ // instead of silently switching paint tools.
+ if (tool === 'terrain') {
+ useTilesetView.getState().setMode('terrain');
+ if (tilesetPath) void openTileset(tilesetPath);
+ return;
+ }
dragAnchor.current = { c: col, r: row };
setSel({ c0: col, r0: row, c1: col, r1: row });
}}
@@ -610,7 +624,10 @@ export function TilemapPainter() {
<div className="tp-warn">
{t('tile.noTerrains')}
{tilesetPath && (
- <button type="button" className="tp-warn-cta" onClick={() => void openTileset(tilesetPath)}>
+ <button
+ type="button" className="tp-warn-cta"
+ onClick={() => { useTilesetView.getState().setMode('terrain'); void openTileset(tilesetPath); }}
+ >
{t('tile.openTilesetEditor')}
</button>
)}
@@ -692,6 +709,7 @@ export function TilemapPainter() {
style={{ width: natural?.w ?? 0, height: natural?.h ?? 0, transform: `scale(${zoom})`, transformOrigin: '0 0' }}
>
<img
+ ref={atlasImgRef}
className="tp-img" src={texUrl} alt="" draggable={false}
onLoad={(e) => setNatural({ w: e.currentTarget.naturalWidth, h: e.currentTarget.naturalHeight })}
/>
diff --git a/desktop/src/panels/TilesetEditor.tsx b/desktop/src/panels/TilesetEditor.tsx
index 8079ceb1c..69fa2e94c 100644
--- a/desktop/src/panels/TilesetEditor.tsx
+++ b/desktop/src/panels/TilesetEditor.tsx
@@ -30,6 +30,7 @@ import { DirtyDot } from '@/components/DirtyDot';
import { GridField } from '@/components/GridField';
import { TilesetDocument } from '@/tileset/TilesetDocument';
import { TilesetCommands, type TileCollisionMods } from '@/tileset/TilesetCommands';
+import { useTilesetView } from '@/tileset/tilesetView';
import { ProjectStore } from '@/project/ProjectStore';
import { colsFor, rowsFor, TERRAIN_COLORS } from '@/tools/tileMath';
import { AnimPreview, tileThumbStyle, type TileAtlas } from '@/tools/tileThumb';
@@ -174,7 +175,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' | 'animation' | 'properties'>('collision');
+ const mode = useTilesetView((s) => s.mode);
+ const setMode = useTilesetView((s) => s.setMode);
const [animTile, setAnimTile] = useState<number | null>(null);
const [propTile, setPropTile] = useState<number | null>(null);
const [shape, setShape] = useState<'box' | 'polygon' | 'circle'>('box');
@@ -207,7 +209,15 @@ export function TilesetEditor() {
const wangDragRef = useRef(wangDrag);
wangDragRef.current = wangDrag;
- useEffect(() => setNatural(null), [texUrl]);
+ // On texture change adopt the size straight from the element when it is
+ // already complete — a cache-hit image can finish loading before React
+ // attaches onLoad, which would leave the stage collapsed at 0×0.
+ const imgRef = useRef<HTMLImageElement | null>(null);
+ useEffect(() => {
+ const img = imgRef.current;
+ if (img && img.complete && img.naturalWidth > 0) setNatural({ w: img.naturalWidth, h: img.naturalHeight });
+ else setNatural(null);
+ }, [texUrl]);
if (!asset) {
return (
@@ -834,6 +844,7 @@ export function TilesetEditor() {
) : (
<div className="ts-stage" style={{ width: (natural?.w ?? 0) * zoom, height: (natural?.h ?? 0) * zoom }}>
<img
+ ref={imgRef}
className="ts-img" src={texUrl} alt="" draggable={false}
onLoad={(e) => setNatural({ w: e.currentTarget.naturalWidth, h: e.currentTarget.naturalHeight })}
/>
diff --git a/desktop/src/tileset/tilesetView.ts b/desktop/src/tileset/tilesetView.ts
new file mode 100644
index 000000000..107a5dfad
--- /dev/null
+++ b/desktop/src/tileset/tilesetView.ts
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file tilesetView.ts
+ * @brief The Tileset Editor's view state (the active authoring mode tab), lifted out
+ * of the component so other surfaces can deep-link into a specific tab — e.g.
+ * the painter's terrain tool opens the editor directly on the Terrain tab.
+ * Editor-session state — never serialized.
+ */
+import { create } from 'zustand';
+
+export type TilesetEditMode = 'collision' | 'terrain' | 'animation' | 'properties';
+
+export const useTilesetView = create<{
+ mode: TilesetEditMode;
+ setMode: (mode: TilesetEditMode) => void;
+}>((set) => ({
+ mode: 'collision',
+ setMode: (mode) => set({ mode }),
+}));