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
fix(tilemap): wang corner authoring — quadrant targets, stroke painting
esengineaa4df79f9a24
diff --git a/desktop/src/i18n/messages/tile.ts b/desktop/src/i18n/messages/tile.tsindex d3036428b..2c8956410 100644--- a/desktop/src/i18n/messages/tile.ts+++ b/desktop/src/i18n/messages/tile.ts@@ -173,7 +173,7 @@ export const tileMessages = defineMessages({'tile.terrain.delete': { en: 'Delete terrain', zh: '删除地形' },// — Tileset editor: wang (corner) terrain authoring —- 'tile.wang.cornerTip': { en: 'Paint the active color on the {dir} corner (right-click clears)', zh: '在{dir}角绘制当前颜色(右键清除)' },+ 'tile.wang.cornerTip': { en: 'Paint the active color on the {dir} corner — drag for a stroke, right button erases', zh: '在{dir}角绘制当前颜色——拖动连续绘制,右键擦除' },'tile.wang.pickColor': { en: 'Paint with "{name}"', zh: '使用「{name}」绘制' },'tile.wang.recolor': { en: 'Change color', zh: '更改颜色' },'tile.wang.addColor': { en: 'Add color', zh: '添加颜色' },diff --git a/desktop/src/panels/TilesetEditor.tsx b/desktop/src/panels/TilesetEditor.tsxindex e4d073d5b..8079ceb1c 100644--- a/desktop/src/panels/TilesetEditor.tsx+++ b/desktop/src/panels/TilesetEditor.tsx@@ -48,13 +48,14 @@ const ZONES: { gx: number; gy: number; bit: number; corner: boolean; dir: string{ gx: 0, gy: 0, bit: TB_NW, corner: true, dir: t('tile.dir.northWest') },];-// Wang corner dots, placed at the cell's 4 corners. Index order matches the tile's-// `corners` array: [top-left, top-right, bottom-right, bottom-left].-const WANG_CORNERS: { i: number; gx: number; gy: number; dir: string }[] = [- { i: 0, gx: 0, gy: 0, dir: t('tile.dir.northWest') },- { i: 1, gx: 1, gy: 0, dir: t('tile.dir.northEast') },- { i: 2, gx: 1, gy: 1, dir: t('tile.dir.southEast') },- { i: 3, gx: 0, gy: 1, dir: t('tile.dir.southWest') },+// Wang corner quadrants: each quarter of the cell is the (large, non-overlapping)+// click target for its corner. Index order matches the tile's `corners` array:+// [top-left, top-right, bottom-right, bottom-left].+const WANG_CORNERS: { i: number; dir: string }[] = [+ { i: 0, dir: t('tile.dir.northWest') },+ { i: 1, dir: t('tile.dir.northEast') },+ { i: 2, dir: t('tile.dir.southEast') },+ { i: 3, dir: t('tile.dir.southWest') },];/** A focused per-tile collision-polygon editor: a magnified tile + click-to-add /@@ -200,6 +201,11 @@ export function TilesetEditor() {const [stampDrag, setStampDrag] = useState<{ ids: Set<number>; on: boolean; kind: 'circle' | 'preset' } | null>(null);const stampDragRef = useRef(stampDrag);stampDragRef.current = stampDrag;+ // Live wang stroke: painted (tile, corner) cells + the color (0 = erasing),+ // committed as one undo step on release.+ const [wangDrag, setWangDrag] = useState<{ keys: Set<string>; cells: { id: number; corner: number }[]; color: number } | null>(null);+ const wangDragRef = useRef(wangDrag);+ wangDragRef.current = wangDrag;useEffect(() => setNatural(null), [texUrl]);@@ -258,6 +264,11 @@ export function TilesetEditor() {}setStampDrag(null);}+ const wd = wangDragRef.current;+ if (wd) {+ TilesetCommands.paintWangCorners(wd.cells, activeSet, wd.color);+ setWangDrag(null);+ }};const growStampDrag = (id: number) => {const sd = stampDragRef.current;@@ -286,9 +297,22 @@ export function TilesetEditor() {if (tileTerrain(id)) TilesetCommands.setTileTerrain(id, null, 0);else TilesetCommands.setTileTerrain(id, activeSet, 0);};- // Wang authoring: assign the active color to one of a tile's 4 corners (right-click clears).- const setCorner = (id: number, corner: number, color: number) =>- TilesetCommands.setTileWangCorner(id, activeSet, corner, color);+ // Wang authoring: strokes of (tile, corner) cells, same drag-paint feel as+ // collision. Left paints the active color, right (or right-drag) erases.+ const startWangDrag = (id: number, corner: number, color: number) =>+ setWangDrag({ keys: new Set([`${id}:${corner}`]), cells: [{ id, corner }], color });+ const growWangDrag = (id: number, corner: number) => {+ const wd = wangDragRef.current;+ if (!wd || wd.keys.has(`${id}:${corner}`)) return;+ const keys = new Set(wd.keys);+ keys.add(`${id}:${corner}`);+ setWangDrag({ keys, cells: [...wd.cells, { id, corner }], color: wd.color });+ };+ // The corner's value as displayed — the live stroke wins over the asset.+ const wangCornerOf = (id: number, corner: number): number => {+ if (wangDrag?.keys.has(`${id}:${corner}`)) return wangDrag.color;+ return tileTerrain(id)?.corners?.[corner] ?? 0;+ };// ── animation authoring ──const animFrames: TilesetAnimFrame[] = animTile != null ? asset.tiles[animTile]?.animation ?? [] : [];@@ -415,6 +439,10 @@ export function TilesetEditor() {const showZones = hovered === id || tt != null;const cellStyle: CSSProperties = { left, top, width: w, height: h };if (tt && !isWang) (cellStyle as Record<string, string | number>)['--tcolor'] = terrainColor;+ if (isWang) {+ const active = terrain?.colors?.[activeColor - 1]?.color;+ if (active) (cellStyle as Record<string, string | number>)['--wactive'] = active;+ }cells.push(<divkey={id}@@ -425,21 +453,34 @@ export function TilesetEditor() {onPointerLeave={() => setHovered((cur) => (cur === id ? null : cur))}>{isWang ? (- // A colored dot at each of the tile's 4 corners — click paints the active- // color, right-click clears. The dots ARE the "circle in the corner" UI.- (showZones || tt) && terrain && WANG_CORNERS.map((c) => {- const cv = tt?.corners?.[c.i] ?? 0;+ // Each quarter of the cell paints its corner — large hit areas that+ // never overlap a neighbor's. The colored quarter-disc in the corner+ // is the "circle in the corner" visual; matching adjacent tiles+ // compose full circles across cell borders. Drag paints a stroke+ // (one undo step), right button — or right-drag — erases.+ (showZones || tt || wangDrag) && terrain && WANG_CORNERS.map((c) => {+ const cv = wangCornerOf(id, c.i);const col = cv > 0 ? terrain.colors?.[cv - 1]?.color : undefined;return (<buttonkey={c.i}type="button"- className={'ts-wcorner' + (cv > 0 ? ' is-on' : '')}- style={{ left: `${c.gx * 100}%`, top: `${c.gy * 100}%`, ...(col ? { background: col } : {}) }}+ className={'ts-wquad ts-wquad-' + c.i + (cv > 0 ? ' is-on' : '')}+ style={col ? ({ '--wc': col } as CSSProperties) : undefined}aria-label={t('tile.zone.aria', { dir: c.dir })}title={t('tile.wang.cornerTip', { dir: c.dir })}- onClick={() => setCorner(id, c.i, activeColor)}- onContextMenu={(e) => { e.preventDefault(); setCorner(id, c.i, 0); }}+ onPointerDown={(e) => {+ if (e.button !== 0 && e.button !== 2) return;+ e.preventDefault();+ startWangDrag(id, c.i, e.button === 2 ? 0 : activeColor);+ }}+ onPointerEnter={() => growWangDrag(id, c.i)}+ onContextMenu={(e) => e.preventDefault()}+ onClick={(e) => {+ // Keyboard activation only (detail 0) — pointer strokes+ // commit on release via commitDrag.+ if (e.detail === 0) TilesetCommands.paintWangCorners([{ id, corner: c.i }], activeSet, activeColor);+ }}/>);})@@ -787,8 +828,7 @@ export function TilesetEditor() {</div>)}- <div className="ts-canvas" onPointerUp={mode === 'collision' ? commitDrag : undefined}- onPointerLeave={mode === 'collision' ? commitDrag : undefined}>+ <div className="ts-canvas" onPointerUp={commitDrag} onPointerLeave={commitDrag}>{!texUrl ? (<div className="ts-warn">{t('tile.texNotFound', { ref: String(asset.texture) || t('tile.refEmpty') })}</div>) : (diff --git a/desktop/src/theme/tileset.css b/desktop/src/theme/tileset.cssindex 4635678aa..a25ca7091 100644--- a/desktop/src/theme/tileset.css+++ b/desktop/src/theme/tileset.css@@ -253,23 +253,49 @@.ts-zone-c { left: 33.34%; top: 33.34%; }.ts-zone-c:hover { background: rgba(255, 255, 255, 0.1); }-/* Wang (corner) authoring: a colored dot at each of a tile's 4 corners — the "circle in- the corner" UI. Empty = faint ring; assigned = filled with the terrain color (inline). */-.ts-wcorner {+/* Wang (corner) authoring: each quarter of the cell is the click target for its+ corner — large hit areas that never overlap a neighbor's. The corner's color+ draws as a quarter-disc anchored in the cell corner (the "circle in the+ corner"); matching adjacent tiles compose full circles across cell borders. */+.ts-wquad {position: absolute;- width: 11px;- height: 11px;+ width: 50%;+ height: 50%;margin: 0;padding: 0;- transform: translate(-50%, -50%);- border-radius: 50%;- border: 1px solid rgba(255, 255, 255, 0.55);- background: rgba(18, 19, 24, 0.5);- box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5);+ border: 0;+ background: transparent;cursor: pointer;}-.ts-wcorner.is-on { border-color: rgba(255, 255, 255, 0.9); }-.ts-wcorner:hover { border-color: var(--star); transform: translate(-50%, -50%) scale(1.15); }+.ts-wquad-0 { left: 0; top: 0; }+.ts-wquad-1 { right: 0; top: 0; }+.ts-wquad-2 { right: 0; bottom: 0; }+.ts-wquad-3 { left: 0; bottom: 0; }+.ts-wquad::before {+ content: '';+ position: absolute;+ width: 58%;+ height: 58%;+ background: rgba(18, 19, 24, 0.4);+ border: 1px solid rgba(255, 255, 255, 0.45);+ opacity: 0.65;+}+/* Anchor the disc into the cell corner; round + outline only the inner arc. */+.ts-wquad-0::before { left: 0; top: 0; border-bottom-right-radius: 100%; border-top: 0; border-left: 0; }+.ts-wquad-1::before { right: 0; top: 0; border-bottom-left-radius: 100%; border-top: 0; border-right: 0; }+.ts-wquad-2::before { right: 0; bottom: 0; border-top-left-radius: 100%; border-bottom: 0; border-right: 0; }+.ts-wquad-3::before { left: 0; bottom: 0; border-top-right-radius: 100%; border-bottom: 0; border-left: 0; }+.ts-wquad.is-on::before { background: var(--wc); border-color: rgba(255, 255, 255, 0.9); opacity: 1; }+/* Hovering an empty quadrant previews the active color about to be painted. */+.ts-wquad:hover::before {+ width: 70%;+ height: 70%;+ border-color: var(--star);+ opacity: 1;+}+.ts-wquad:hover:not(.is-on)::before {+ background: color-mix(in srgb, var(--wactive, transparent) 55%, transparent);+}/* Wang color palette (below the terrain editor row): swatch · name · picker · remove. */.ts-wcolors {diff --git a/desktop/src/tileset/TilesetCommands.ts b/desktop/src/tileset/TilesetCommands.tsindex 51f0ba4ba..8d3edd88f 100644--- a/desktop/src/tileset/TilesetCommands.ts+++ b/desktop/src/tileset/TilesetCommands.ts@@ -226,21 +226,24 @@ export const TilesetCommands = {},/**- * Assign one corner of a tile's wang membership (corner 0-3 = TL/TR/BR/BL, color 0 = none)- * as ONE undo step. Clearing every corner drops the membership.+ * Paint one wang color onto many (tile, corner) cells (corner 0-3 = TL/TR/BR/BL,+ * color 0 = erase) as ONE undo step — a whole brush stroke. A tile whose corners+ * all clear drops its membership.*/- setTileWangCorner(id: number, set: number, corner: number, color: number): void {- if (id <= 0 || corner < 0 || corner > 3) return;- TilesetDocument.edit('Edit Tile Corner', (a) => {- const existing = a.tiles[id]?.terrain;- const corners = existing && existing.set === set && existing.corners- ? [...existing.corners] : [0, 0, 0, 0];- corners[corner] = color & 0xff;- if (corners.every((c) => c === 0)) {- if (a.tiles[id]?.terrain) { delete a.tiles[id].terrain; pruneEmpty(a, id); }- return;+ paintWangCorners(cells: { id: number; corner: number }[], set: number, color: number): void {+ TilesetDocument.edit('Paint Tile Corners', (a) => {+ for (const { id, corner } of cells) {+ if (id <= 0 || corner < 0 || corner > 3) continue;+ const existing = a.tiles[id]?.terrain;+ const corners = existing && existing.set === set && existing.corners+ ? [...existing.corners] : [0, 0, 0, 0];+ corners[corner] = color & 0xff;+ if (corners.every((c) => c === 0)) {+ if (a.tiles[id]?.terrain) { delete a.tiles[id].terrain; pruneEmpty(a, id); }+ continue;+ }+ a.tiles[id] = { ...(a.tiles[id] ?? {}), terrain: { set, corners } };}- a.tiles[id] = { ...(a.tiles[id] ?? {}), terrain: { set, corners } };});},diff --git a/desktop/tests/tileset-commands.test.ts b/desktop/tests/tileset-commands.test.tsindex ec75fbbe2..ee832282a 100644--- a/desktop/tests/tileset-commands.test.ts+++ b/desktop/tests/tileset-commands.test.ts@@ -128,3 +128,37 @@ describe('TilesetCommands — probability', () => {expect(tiles()[3].probability).toBe(0.2);});});++describe('TilesetCommands — wang corner strokes', () => {+ beforeEach(() => {+ EditorHistory.clear();+ TilesetDocument.open(freshTileset(), 'a.estileset');+ TilesetCommands.addTerrain('blend', 'wang');+ TilesetCommands.addWangColor(0); // color 1+ TilesetCommands.addWangColor(0); // color 2+ });++ it('paints a whole stroke across tiles and corners as one undo step', () => {+ TilesetCommands.paintWangCorners(+ [{ id: 5, corner: 0 }, { id: 5, corner: 1 }, { id: 6, corner: 3 }], 0, 1,+ );+ expect(tiles()[5].terrain).toEqual({ set: 0, corners: [1, 1, 0, 0] });+ expect(tiles()[6].terrain).toEqual({ set: 0, corners: [0, 0, 0, 1] });+ EditorHistory.undo();+ expect(tiles()[5]).toBeUndefined();+ expect(tiles()[6]).toBeUndefined();+ });++ it('erasing (color 0) every corner drops the membership and prunes the tile', () => {+ TilesetCommands.paintWangCorners([{ id: 5, corner: 0 }, { id: 5, corner: 2 }], 0, 2);+ TilesetCommands.paintWangCorners([{ id: 5, corner: 0 }, { id: 5, corner: 2 }], 0, 0);+ expect(tiles()[5]).toBeUndefined();+ EditorHistory.undo();+ expect(tiles()[5].terrain).toEqual({ set: 0, corners: [2, 0, 2, 0] });+ });++ it('ignores out-of-range corners and non-positive ids', () => {+ TilesetCommands.paintWangCorners([{ id: 0, corner: 0 }, { id: 5, corner: 4 }, { id: 5, corner: -1 }], 0, 1);+ expect(tiles()[5]).toBeUndefined();+ });+});diff --git a/docs/astro/src/content/docs/guides/tilemap.mdx b/docs/astro/src/content/docs/guides/tilemap.mdxindex 31e521944..f96cdd602 100644--- a/docs/astro/src/content/docs/guides/tilemap.mdx+++ b/docs/astro/src/content/docs/guides/tilemap.mdx@@ -210,8 +210,10 @@ of two models:paints colors on a **half-cell corner grid** — click between four tiles andthe resolver re-tiles all of them by exact corner match (nearest-mismatchfallback), producing smooth marching-squares boundaries. The tileset editor- authors the color palette (add / rename / recolor / remove) and per-tile- corner dots; the painter lists the colors to paint with.+ authors the color palette (add / rename / recolor / remove) and the per-tile+ corners: each quarter of a tile is the click target for its corner — click or+ drag a stroke to paint the active color, right button erases — and the+ painter lists the colors to paint with.The painter's terrain brush dispatches on the set's model automatically. Ablended-island corner-Wang demo ships in `examples/`.diff --git a/docs/astro/src/content/docs/zh-cn/guides/tilemap.mdx b/docs/astro/src/content/docs/zh-cn/guides/tilemap.mdxindex bf430f39a..1515becdc 100644--- a/docs/astro/src/content/docs/zh-cn/guides/tilemap.mdx+++ b/docs/astro/src/content/docs/zh-cn/guides/tilemap.mdx@@ -188,7 +188,8 @@ tilemap.setChunkTiles(layer, chunkX, chunkY, tiles, 32, 32);(草、沙、水……),每个瓦片给自己的四个**角**各指定一种颜色。一个集就能混合多种地形,画笔在**半格角网格**上绘制颜色——点在四块瓦片之间,解析器按角色精确匹配重排这四块(最近失配兜底),得到平滑的 marching-squares 边界。图集编辑器负责- 颜色调色板(添加 / 重命名 / 改色 / 删除)与逐瓦片角点;画笔面板列出可绘制的颜色。+ 颜色调色板(添加 / 重命名 / 改色 / 删除)与逐瓦片的角:瓦片的每个四分之一区域就是+ 对应角的点击目标——单击或拖动笔画绘制当前颜色,右键擦除;画笔面板列出可绘制的颜色。画笔的地形工具按地形集的模型自动分派。`examples/` 里有一个角 Wang 混合海岛演示。