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(editor): editable circle-collision shapes (drag centre + radius)

yhh7ee87102889e
diff --git a/desktop/src/i18n/messages/tile.ts b/desktop/src/i18n/messages/tile.ts
index 084d51ea3..0865cbca3 100644
--- a/desktop/src/i18n/messages/tile.ts
+++ b/desktop/src/i18n/messages/tile.ts
@@ -160,6 +160,11 @@ export const tileMessages = defineMessages({
en: 'Click to add a vertex · drag a vertex to move · click it to delete · takes effect at ≥3 points',
zh: '点击添加顶点 · 拖动顶点移动 · 点击顶点删除 · ≥3 个顶点时生效',
},
+ 'tile.ce.title': { en: 'Collision circle · #{id}', zh: '碰撞圆形 · #{id}' },
+ 'tile.ce.hint': {
+ en: 'Drag the centre to move · drag the edge handle to resize',
+ zh: '拖动圆心移动 · 拖动边缘手柄调整半径',
+ },
// — Tileset editor: terrain authoring —
'tile.zone.member': { en: 'Tile belongs to this terrain', zh: '瓦片属于此地形' },
diff --git a/desktop/src/panels/TilesetEditor.tsx b/desktop/src/panels/TilesetEditor.tsx
index 9330e2044..fff93c4dc 100644
--- a/desktop/src/panels/TilesetEditor.tsx
+++ b/desktop/src/panels/TilesetEditor.tsx
@@ -16,7 +16,7 @@
import {
useEffect, useRef, useState, useSyncExternalStore,
- type CSSProperties, type MouseEvent as ReactMouseEvent,
+ type CSSProperties, type MouseEvent as ReactMouseEvent, type PointerEvent as ReactPointerEvent,
} from 'react';
import { Plus, Trash2, X, ArrowUp } from 'lucide-react';
import {
@@ -167,6 +167,100 @@ function PolygonEditor(props: {
);
}
+/** A focused per-tile circle-collision editor: drag the centre dot or the radius handle on
+ * a magnified tile (live preview, one undo step on release). Modifiers (one-way / sensor /
+ * material) are preserved across the edit, and Clear removes the shape. Mirrors the
+ * {@link PolygonEditor} gesture so the two shapes fine-tune the same way. */
+function CircleEditor(props: {
+ asset: TilesetAsset; texUrl: string; natural: { w: number; h: number };
+ tileId: number; cols: number; onClose: () => void;
+}) {
+ const { asset, texUrl, natural, tileId, cols, onClose } = props;
+ const win = usePanelWindow();
+ const { tileWidth: tw, tileHeight: th, margin: mg, spacing: sp } = asset;
+ const col = (tileId - 1) % cols;
+ const row = Math.floor((tileId - 1) / cols);
+ const tileX = mg + col * (tw + sp);
+ const tileY = mg + row * (th + sp);
+ const SIZE = 260;
+ const sx = SIZE / tw;
+ const sy = SIZE / th;
+ const existing = asset.tiles[tileId]?.collision;
+ // Keep the shape's modifiers so a resize doesn't drop a one-way / sensor / material.
+ const mods: TileCollisionMods = existing
+ ? { oneWay: existing.oneWay, sensor: existing.sensor, density: existing.density, friction: existing.friction, restitution: existing.restitution }
+ : {};
+ const [c, setC] = useState(() => existing?.type === 'circle'
+ ? { cx: existing.cx, cy: existing.cy, r: existing.r }
+ : { cx: tw / 2, cy: th / 2, r: Math.min(tw, th) / 2 });
+ const cRef = useRef(c);
+ cRef.current = c;
+ const stageRef = useRef<HTMLDivElement>(null);
+ const drag = useRef<'center' | 'radius' | null>(null);
+
+ const clamp = (n: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, n));
+ const round1 = (n: number) => Math.round(n * 10) / 10;
+ const at = (clientX: number, clientY: number): [number, number] => {
+ const r = stageRef.current!.getBoundingClientRect();
+ return [((clientX - r.left) / r.width) * tw, ((clientY - r.top) / r.height) * th];
+ };
+
+ useEffect(() => {
+ const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose(); };
+ win.addEventListener('keydown', onKey);
+ return () => win.removeEventListener('keydown', onKey);
+ }, [onClose, win]);
+
+ const start = (kind: 'center' | 'radius') => (e: ReactPointerEvent) => {
+ e.stopPropagation();
+ drag.current = kind;
+ try { (e.target as Element).setPointerCapture(e.pointerId); } catch { /* capture is optional */ }
+ };
+ const move = (e: ReactPointerEvent) => {
+ if (!drag.current) return;
+ const [px, py] = at(e.clientX, e.clientY);
+ if (drag.current === 'center') setC((s) => ({ ...s, cx: round1(clamp(px, 0, tw)), cy: round1(clamp(py, 0, th)) }));
+ else setC((s) => ({ ...s, r: round1(clamp(Math.hypot(px - cRef.current.cx, py - cRef.current.cy), 0.5, Math.max(tw, th))) }));
+ };
+ const end = (e: ReactPointerEvent) => {
+ if (!drag.current) return;
+ drag.current = null;
+ try { (e.target as Element).releasePointerCapture(e.pointerId); } catch { /* not captured */ }
+ TilesetCommands.setTileCircle(tileId, cRef.current.cx, cRef.current.cy, cRef.current.r, mods);
+ };
+ const HR = Math.max(1.4, tw * 0.07);
+
+ return (
+ <div className="ts-pe-backdrop" onPointerDown={(e) => { if (e.target === e.currentTarget) onClose(); }}>
+ <div className="ts-pe">
+ <div className="ts-pe-head">
+ <span>{t('tile.ce.title', { id: tileId })}</span>
+ <span className="ts-grow" />
+ <button type="button" onClick={() => { TilesetCommands.setTileCircle(tileId, 0, 0, 0); onClose(); }}>{t('tile.pe.clear')}</button>
+ <button type="button" onClick={onClose}>{t('tile.done')}</button>
+ </div>
+ <div
+ ref={stageRef}
+ className="ts-pe-stage"
+ style={{
+ width: SIZE, height: SIZE,
+ backgroundImage: `url(${texUrl})`,
+ backgroundPosition: `-${tileX * sx}px -${tileY * sy}px`,
+ backgroundSize: `${natural.w * sx}px ${natural.h * sy}px`,
+ }}
+ >
+ <svg className="ts-pe-svg" viewBox={`0 0 ${tw} ${th}`} width={SIZE} height={SIZE}>
+ <circle className="ts-pe-poly" cx={c.cx} cy={c.cy} r={c.r} style={{ vectorEffect: 'non-scaling-stroke' }} />
+ <circle className="ts-pe-pt" cx={c.cx + c.r} cy={c.cy} r={HR} onPointerDown={start('radius')} onPointerMove={move} onPointerUp={end} />
+ <circle className="ts-pe-pt" cx={c.cx} cy={c.cy} r={HR} onPointerDown={start('center')} onPointerMove={move} onPointerUp={end} />
+ </svg>
+ </div>
+ <div className="ts-pe-hint">{t('tile.ce.hint')}</div>
+ </div>
+ </div>
+ );
+}
+
/** Key/value entry row for the tile-properties bar; commits on + or Enter. */
function AddPropRow({ onAdd }: { onAdd: (k: string, v: string) => void }) {
const [k, setK] = useState('');
@@ -223,6 +317,7 @@ export function TilesetEditor() {
const [propTile, setPropTile] = useState<number | null>(null);
const [shape, setShape] = useState<'box' | 'polygon' | 'circle'>('box');
const [polyTile, setPolyTile] = useState<number | null>(null);
+ const [circleTile, setCircleTile] = useState<number | null>(null);
// In polygon mode, an active preset stamps that canned slope/half-tile on click;
// null = freeform (click opens the vertex editor). See slopePresets.
const [activePreset, setActivePreset] = useState<SlopePreset | null>(null);
@@ -431,7 +526,10 @@ export function TilesetEditor() {
title={t('tile.cell.circleTip', { id })}
onPointerDown={(e) => {
e.preventDefault();
- setStampDrag({ ids: new Set([id]), on: !circleOf(id), kind: 'circle' });
+ // A circled tile opens the focused editor (drag centre/radius); an empty
+ // one stamps a fitted disc, still drag-paintable across cells.
+ if (circleOf(id)) setCircleTile(id);
+ else setStampDrag({ ids: new Set([id]), on: true, kind: 'circle' });
}}
onPointerEnter={() => growStampDrag(id)}
>
@@ -913,6 +1011,13 @@ export function TilesetEditor() {
onClose={() => setPolyTile(null)}
/>
)}
+
+ {mode === 'collision' && shape === 'circle' && circleTile != null && texUrl && natural && (
+ <CircleEditor
+ asset={asset} texUrl={texUrl} natural={natural} tileId={circleTile} cols={cols}
+ onClose={() => setCircleTile(null)}
+ />
+ )}
</div>
);
}