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.

fix(editor): the gizmo moves things, not every pixel of the thing

yhh64f3d5200aa6
diff --git a/desktop/src/i18n/messages/viewport.ts b/desktop/src/i18n/messages/viewport.ts
index c1674c041..9cd34cfc5 100644
--- a/desktop/src/i18n/messages/viewport.ts
+++ b/desktop/src/i18n/messages/viewport.ts
@@ -14,12 +14,12 @@ export const viewportMessages = defineMessages({
// — HUD hint line (active transform tool) —
'vp.hint.select': {
- en: 'Click to select · Shift adds · drag empty to box-select',
- zh: '点击选择 · Shift 加选 · 拖动空白处框选',
+ en: 'Click to select · Shift adds · drag a body to move · drag empty to box-select',
+ zh: '点击选择 · Shift 加选 · 拖动实体移动 · 拖动空白处框选',
},
'vp.hint.move': {
- en: 'Drag a gizmo axis or the body · Alt-drag duplicates · arrows nudge',
- zh: '拖动 Gizmo 轴或其主体 · Alt 拖动创建副本 · 方向键微移',
+ en: 'Drag a gizmo axis or its center · Alt-drag duplicates · arrows nudge',
+ zh: '拖动 Gizmo 轴或中心方块 · Alt 拖动创建副本 · 方向键微移',
},
'vp.hint.rotate': { en: 'Drag the ring to rotate the selection', zh: '拖动圆环旋转所选' },
'vp.hint.scale': {
diff --git a/desktop/src/tools/transformTools.ts b/desktop/src/tools/transformTools.ts
index 37134b961..e423c8b9b 100644
--- a/desktop/src/tools/transformTools.ts
+++ b/desktop/src/tools/transformTools.ts
@@ -53,11 +53,23 @@ interface Drag {
pivotWorld: Pt;
pivotClient: Pt; // canvas-relative
downWorld: Pt;
+ /** Where the press landed, in window-client px — what the slop is measured from. */
+ downClient: Pt;
startAngle: number; // rotate: cursor screen-angle around the pivot
startDist: number; // scale: cursor screen-distance from the pivot
/** Local-frame angle (world radians) for axis-constrained move; 0 = world axes. */
angleRad: number;
targets: Target[];
+ /**
+ * Whether this drag may write yet.
+ *
+ * Grabbing a handle is unambiguous, so a handle drag is armed at once. A drag
+ * that started on an entity's BODY is not: the same press is how you select,
+ * and applying from the first pixel meant a click that wobbled moved the thing
+ * you were only trying to pick. It arms when the press clears the click slop —
+ * the same threshold that already decides click-versus-drag.
+ */
+ armed: boolean;
}
interface MarqueeState {
@@ -232,6 +244,7 @@ function beginDrag(
p: PointerInput,
cur: Pt,
angleRad = 0,
+ armed = true,
): Drag {
const label = kind === 'rotate' ? 'Rotate' : kind === 'scale' ? 'Scale' : 'Move';
const downWorld = ViewportController.canvasToWorld(p.clientX, p.clientY) ?? { x: 0, y: 0 };
@@ -246,6 +259,7 @@ function beginDrag(
pivotWorld,
pivotClient,
downWorld,
+ downClient: { x: p.clientX, y: p.clientY },
startAngle: Math.atan2(cur.y - pivotClient.y, cur.x - pivotClient.x),
// The grab point's screen distance from the pivot. Scale is now delta-based off
// this (not a ratio), so it needs no floor and can't blow up when you grab near
@@ -253,6 +267,7 @@ function beginDrag(
startDist: Math.hypot(cur.x - pivotClient.x, cur.y - pivotClient.y),
angleRad,
targets,
+ armed,
};
}
@@ -379,6 +394,10 @@ function makeTransformTool(mode: ToolMode): EditorTool {
if (handle) {
drag = beginDrag(kind, handle.axis, captureTargets(ids, kind), pivotWorld, pc, p, cur, localAngle);
ed.setActiveGizmoAxis(handle.axis); // light up the grabbed handle
+ // Alt-duplicate rides the handle too, now that the body no longer
+ // transforms — otherwise Alt+drag would have nowhere to happen in the
+ // transform tools. Deferred to the first real move, as it always was.
+ altPending = p.alt ? ids : null;
ctx.capture(p.pointerId);
return true;
}
@@ -403,6 +422,20 @@ function makeTransformTool(mode: ToolMode): EditorTool {
pendingClick = { downX: p.clientX, downY: p.clientY, stack };
const ids = [...useSelection.getState().selectedIds];
+ // A transform tool's gizmo is the drag surface; the body is only for
+ // picking. Dragging the body used to transform freely on both axes at
+ // once, which made every click a potential nudge — press to select,
+ // travel a pixel, and the thing you were aiming at had moved (or, in the
+ // rotate/scale tools, spun or resized). The handles say which axis you
+ // meant; the body says nothing, so it no longer answers.
+ //
+ // The select tool keeps it: it shows no gizmo, so a body drag is the only
+ // move it has, and it arms only past the click slop.
+ if (mode !== 'select') {
+ ctx.capture(p.pointerId);
+ return true;
+ }
+
// Drag the originals for now; an Alt-drag defers its clone to the first real
// move (onPointerMove), so a bare Alt-click leaves nothing behind.
const targets = captureTargets(ids);
@@ -410,7 +443,7 @@ function makeTransformTool(mode: ToolMode): EditorTool {
altPending = p.alt ? ids : null;
if (!targets.length || !pivotWorld) return false;
const pc = ViewportController.worldToClient(pivotWorld.x, pivotWorld.y) ?? cur;
- drag = beginDrag(kind, 'xy', targets, pivotWorld, pc, p, cur);
+ drag = beginDrag(kind, 'xy', targets, pivotWorld, pc, p, cur, 0, false);
ctx.capture(p.pointerId);
return true;
}
@@ -423,20 +456,30 @@ function makeTransformTool(mode: ToolMode): EditorTool {
},
onPointerMove(p) {
+ const past = (from: Pt): boolean =>
+ Math.abs(p.clientX - from.x) > CLICK_SLOP || Math.abs(p.clientY - from.y) > CLICK_SLOP;
+
// A press that travels past the slop is a drag, not a click — disarm the
// click-through cycle so releasing won't advance the selection.
- if (pendingClick && (Math.abs(p.clientX - pendingClick.downX) > CLICK_SLOP || Math.abs(p.clientY - pendingClick.downY) > CLICK_SLOP)) {
- // Past the slop: this is a drag, not a click. If it's a deferred Alt-drag,
- // clone NOW and retarget the drag onto the fresh copies (the originals stay).
- if (altPending && drag) {
+ if (pendingClick && past({ x: pendingClick.downX, y: pendingClick.downY })) {
+ pendingClick = null;
+ cycle = null;
+ }
+ // Measured from the DRAG's own press, not the pick's: a handle grab sets no
+ // pendingClick (it must not cycle the selection), and Alt-duplicate has to
+ // work when you drag an axis.
+ if (drag && past(drag.downClient)) {
+ // A deferred Alt-drag clones NOW and retargets onto the fresh copies, so a
+ // bare Alt-click leaves nothing behind.
+ if (altPending) {
const dup = altDuplicateTargets(altPending);
if (dup.targets.length) drag.targets = dup.targets;
+ altPending = null;
}
- altPending = null;
- pendingClick = null;
- cycle = null;
+ // And the intent is a drag, so a body drag may write from here.
+ drag.armed = true;
}
- if (drag) {
+ if (drag?.armed) {
const origin = canvasOrigin();
const cur: Pt = origin
? { x: p.clientX - origin.left, y: p.clientY - origin.top }
@@ -469,8 +512,12 @@ function makeTransformTool(mode: ToolMode): EditorTool {
SceneStore.resume();
drag = null;
useEditorStore.getState().setActiveGizmoAxis(null);
- // A bare click (no drag) on overlapping entities walks to the next one down.
- const stepped = pendingClick && stepCycle(pendingClick.stack, cycle, p.clientX, p.clientY);
+ }
+ // A bare click (no drag) on overlapping entities walks to the next one down.
+ // Outside the `drag` branch: a press on a body in a transform tool no longer
+ // starts one, and click-through has to keep working without it.
+ if (pendingClick) {
+ const stepped = stepCycle(pendingClick.stack, cycle, p.clientX, p.clientY);
pendingClick = null;
if (stepped) {
useSelection.getState().select(stepped.pick);
diff --git a/desktop/tests/transform-tool.test.ts b/desktop/tests/transform-tool.test.ts
index 03821443f..3a7c42ff0 100644
--- a/desktop/tests/transform-tool.test.ts
+++ b/desktop/tests/transform-tool.test.ts
@@ -100,18 +100,46 @@ describe('empty space → marquee box-select', () => {
});
describe('entity pick → select + move', () => {
- it('selects the clicked entity and moves it by the world delta', () => {
+ it('selects the clicked entity, and the SELECT tool moves it by the world delta', () => {
h.pick.entity = 7;
h.pos.set(7, { x: 100, y: 100 });
- const t = TRANSFORM_TOOLS.move;
+ const t = TRANSFORM_TOOLS.select;
expect(t.onPointerDown(ev(100, 100), ctx)).toBe(true);
expect(useSelection.getState().selectedId).toBe(7);
- t.onPointerMove(ev(130, 100), ctx); // +30 in x
+ t.onPointerMove(ev(130, 100), ctx); // +30 in x, past the slop
t.onPointerUp(ev(130, 100), ctx);
expect(h.calls.setXY.at(-1)).toEqual([7, 130, 100]);
expect(h.calls.commit).toBe(1);
});
+ it('a transform tool selects on a body press but does not transform', () => {
+ // The gizmo handles say which axis you meant; the body says nothing. Dragging
+ // it used to transform freely, so any click that wobbled nudged the thing you
+ // were only trying to pick.
+ h.pick.entity = 7;
+ h.pos.set(7, { x: 100, y: 100 });
+ for (const mode of ['move', 'rotate', 'scale'] as const) {
+ h.calls.setXY = [];
+ useSelection.getState().select(null);
+ const t = TRANSFORM_TOOLS[mode];
+ expect(t.onPointerDown(ev(100, 100), ctx)).toBe(true);
+ expect(useSelection.getState().selectedId).toBe(7);
+ t.onPointerMove(ev(160, 140), ctx); // well past the slop
+ t.onPointerUp(ev(160, 140), ctx);
+ expect(h.calls.setXY).toEqual([]);
+ }
+ });
+
+ it('the select tool does not nudge on a click that only wobbles', () => {
+ h.pick.entity = 7;
+ h.pos.set(7, { x: 100, y: 100 });
+ const t = TRANSFORM_TOOLS.select;
+ t.onPointerDown(ev(100, 100), ctx);
+ t.onPointerMove(ev(102, 101), ctx); // inside the click slop
+ t.onPointerUp(ev(102, 101), ctx);
+ expect(h.calls.setXY).toEqual([]);
+ });
+
it('Shift-click toggles selection without starting a drag', () => {
useSelection.getState().select(7);
h.pick.entity = 9;
@@ -123,7 +151,7 @@ describe('entity pick → select + move', () => {
it('Alt-drag duplicates on the first move and moves the copy', () => {
h.pick.entity = 7;
h.pos.set(7, { x: 100, y: 100 });
- const t = TRANSFORM_TOOLS.move;
+ const t = TRANSFORM_TOOLS.select;
t.onPointerDown(ev(100, 100, { alt: true }), ctx);
expect(h.calls.dup).toEqual([]); // deferred — no clone until the drag actually moves
t.onPointerMove(ev(140, 100), ctx); // past the slop → clone NOW, retarget onto the copy
@@ -133,10 +161,22 @@ describe('entity pick → select + move', () => {
expect(h.calls.setXY.at(-1)).toEqual([107, 140, 100]); // copy tracks the cursor from the original's start
});
+ it('Alt rides the gizmo handle, now that the body does not transform', () => {
+ useSelection.getState().select(7);
+ h.pos.set(7, { x: 100, y: 100 });
+ const t = TRANSFORM_TOOLS.move;
+ // Grab the x arrow: the pivot is the entity, world == client px in this harness.
+ t.onPointerDown(ev(100 + GIZMO.axisLen - 5, 100, { alt: true }), ctx);
+ expect(h.calls.dup).toEqual([]); // still deferred
+ t.onPointerMove(ev(100 + GIZMO.axisLen + 40, 100), ctx);
+ expect(h.calls.dup).toEqual([107]);
+ t.onPointerUp(ev(100 + GIZMO.axisLen + 40, 100), ctx);
+ });
+
it('a bare Alt-click leaves no duplicate (no drag past the slop)', () => {
h.pick.entity = 7;
h.pos.set(7, { x: 100, y: 100 });
- const t = TRANSFORM_TOOLS.move;
+ const t = TRANSFORM_TOOLS.select;
t.onPointerDown(ev(100, 100, { alt: true }), ctx);
t.onPointerUp(ev(100, 100), ctx); // released in place — never a drag
expect(h.calls.dup).toEqual([]); // the old bug stacked a copy on the original here