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): complete the on-canvas gizmo handles - size (vec2) + cone angle

yhhb333f301cf2a
diff --git a/desktop/src/engine/ViewportController.ts b/desktop/src/engine/ViewportController.ts
index 83dce2b31..938ecb063 100644
--- a/desktop/src/engine/ViewportController.ts
+++ b/desktop/src/engine/ViewportController.ts
@@ -474,7 +474,7 @@ export const ViewportController = {
*/
getColliderGizmo(
id: EntityId,
- ): { kind: 'box'; pts: Array<{ x: number; y: number }>; handle: { x: number; y: number } | null } | { kind: 'circle'; cx: number; cy: number; r: number; handle: { x: number; y: number } | null } | null {
+ ): { kind: 'box'; pts: Array<{ x: number; y: number }>; handle: { x: number; y: number } | null; sizeHandle: { x: number; y: number } | null } | { kind: 'circle'; cx: number; cy: number; r: number; handle: { x: number; y: number } | null; sizeHandle: { x: number; y: number } | null } | null {
const world = EngineHost.world;
if (!world || !world.valid(id) || !world.has(id, Transform)) return null;
const t = world.get(id, Transform);
@@ -496,8 +496,9 @@ export const ViewportController = {
this.worldToClient(wx, wy),
);
if (screen.some((p) => !p)) return null;
- // Box halfExtents is a vec2 (corner handle) — a follow-up; no radius handle here.
- return { kind: 'box', pts: screen.map((p) => ({ x: p!.x, y: p!.y })), handle: null };
+ const bs = screen.map((p) => ({ x: p!.x, y: p!.y }));
+ // Size handle at the +hw,+hh corner (obbCorners index 2) — drag = halfExtents.
+ return { kind: 'box', pts: bs, handle: null, sizeHandle: bs[2] };
}
const cc = world.get(id, CircleCollider) as { radius: number; offset: { x: number; y: number } };
@@ -507,7 +508,7 @@ export const ViewportController = {
if (!center || !edge) return null;
// Radius handle at the top of the circle (drag = radius, in physics meters).
const handle = this.worldToClient(c.x, c.y + cc.radius * ppu);
- return { kind: 'circle', cx: center.x, cy: center.y, r: Math.hypot(edge.x - center.x, edge.y - center.y), handle: handle ?? null };
+ return { kind: 'circle', cx: center.x, cy: center.y, r: Math.hypot(edge.x - center.x, edge.y - center.y), handle: handle ?? null, sizeHandle: null };
},
/** Ids of entities carrying a ParticleEmitter — the emitter-gizmo set. */
@@ -533,7 +534,7 @@ export const ViewportController = {
*/
getParticleEmitterGizmo(
id: EntityId,
- ): { cx: number; cy: number; kind: 'point' | 'circle' | 'poly'; r: number; pts: Array<{ x: number; y: number }>; on: boolean; handle: { x: number; y: number } | null } | null {
+ ): { cx: number; cy: number; kind: 'point' | 'circle' | 'poly'; r: number; pts: Array<{ x: number; y: number }>; on: boolean; handle: { x: number; y: number } | null; sizeHandle: { x: number; y: number } | null; angleHandle: { x: number; y: number } | null } | null {
const world = EngineHost.world;
if (!world || !world.valid(id) || !world.has(id, ParticleEmitter) || !world.has(id, Transform)) return null;
const t = world.get(id, Transform);
@@ -559,7 +560,7 @@ export const ViewportController = {
const r = edge ? Math.hypot(edge.x - center.x, edge.y - center.y) : 0;
// Radius handle at the top of the ring (drag to resize shapeRadius).
const handle = this.worldToClient(t.worldPosition.x, t.worldPosition.y + p.shapeRadius);
- return { cx: center.x, cy: center.y, kind: 'circle', r, pts: [], on, handle: handle ?? null };
+ return { cx: center.x, cy: center.y, kind: 'circle', r, pts: [], on, handle: handle ?? null, sizeHandle: null, angleHandle: null };
}
case 2: { // Rectangle — oriented spawn box of shapeSize
const corners = obbCorners({
@@ -567,7 +568,9 @@ export const ViewportController = {
hw: Math.abs(p.shapeSize.x) * 0.5, hh: Math.abs(p.shapeSize.y) * 0.5, rot,
}).map(([wx, wy]) => this.worldToClient(wx, wy));
if (corners.some((s) => !s)) return null;
- return { cx: center.x, cy: center.y, kind: 'poly', r: 0, pts: corners.map((s) => ({ x: s!.x, y: s!.y })), on, handle: null };
+ const cs = corners.map((s) => ({ x: s!.x, y: s!.y }));
+ // Size handle at the +halfW,+halfH corner (obbCorners index 2) — drag = shapeSize.
+ return { cx: center.x, cy: center.y, kind: 'poly', r: 0, pts: cs, on, handle: null, sizeHandle: cs[2], angleHandle: null };
}
case 3: { // Cone — aim wedge: local up (0,1) swept ±shapeAngle/2, out to shapeRadius
const half = Math.max(0, p.shapeAngle) * 0.5 * (Math.PI / 180);
@@ -581,13 +584,16 @@ export const ViewportController = {
if (!s) return null;
pts.push({ x: s.x, y: s.y });
}
- // Radius handle at the wedge's forward tip (drag to resize shapeRadius/reach).
+ // Radius handle at the wedge's forward tip (drag to resize shapeRadius/reach);
+ // angle handle at the +half-angle edge (drag to widen/narrow shapeAngle).
const tip = toWorld(0, rad);
const handleS = this.worldToClient(tip.x, tip.y);
- return { cx: center.x, cy: center.y, kind: 'poly', r: 0, pts, on, handle: handleS ?? null };
+ const edge = toWorld(Math.sin(half) * rad, Math.cos(half) * rad);
+ const angleS = this.worldToClient(edge.x, edge.y);
+ return { cx: center.x, cy: center.y, kind: 'poly', r: 0, pts, on, handle: handleS ?? null, sizeHandle: null, angleHandle: angleS ?? null };
}
default: // Point (0) — a marker at the emitter (the clickable icon)
- return { cx: center.x, cy: center.y, kind: 'point', r: 0, pts: [], on, handle: null };
+ return { cx: center.x, cy: center.y, kind: 'point', r: 0, pts: [], on, handle: null, sizeHandle: null, angleHandle: null };
}
},
};
diff --git a/desktop/src/panels/Viewport.tsx b/desktop/src/panels/Viewport.tsx
index 32223e2c0..77bd613eb 100644
--- a/desktop/src/panels/Viewport.tsx
+++ b/desktop/src/panels/Viewport.tsx
@@ -66,6 +66,68 @@ function startRadiusHandleDrag(
window.addEventListener('pointerup', onUp);
}
+// Drag a box corner handle → a vec2 size field. Same channel as the radius drag; the
+// cursor is un-rotated into the entity's local frame so the corner's |local| gives the
+// half-extents. `fullSize` writes the full size (emitter shapeSize = 2× half); else the
+// half-extents (collider halfExtents). `ppu` maps world px → the field's units.
+function startSizeHandleDrag(
+ rt: number, component: string, field: string, ppu: number, fullSize: boolean, e: ReactPointerEvent,
+): void {
+ if (e.button !== 0) return;
+ const src = SceneModel.sourceFor(rt);
+ const center = ViewportController.getEntityWorldXY(rt);
+ if (src == null || !center) return;
+ e.stopPropagation();
+ const rot = ViewportController.getEntityWorldAngleRad(rt);
+ const cos = Math.cos(rot), sin = Math.sin(rot);
+ SceneCommands.beginGesture(`${component} size`);
+ const onMove = (ev: PointerEvent) => {
+ const w = ViewportController.canvasToWorld(ev.clientX, ev.clientY);
+ if (!w) return;
+ const dx = w.x - center.x, dy = w.y - center.y;
+ const lx = dx * cos + dy * sin; // un-rotate into the box's local frame
+ const ly = -dx * sin + dy * cos;
+ const k = (fullSize ? 2 : 1) / (ppu || 1);
+ SceneCommands.setField(src, component, field, 'vec2', [Math.abs(lx) * k, Math.abs(ly) * k]);
+ };
+ const onUp = () => {
+ SceneCommands.endGesture();
+ window.removeEventListener('pointermove', onMove);
+ window.removeEventListener('pointerup', onUp);
+ };
+ window.addEventListener('pointermove', onMove);
+ window.addEventListener('pointerup', onUp);
+}
+
+// Drag a cone's edge handle → its spread angle (ParticleEmitter.shapeAngle, degrees).
+// The cursor's angle off local +Y is the half-angle; shapeAngle is the full spread.
+function startAngleHandleDrag(rt: number, e: ReactPointerEvent): void {
+ if (e.button !== 0) return;
+ const src = SceneModel.sourceFor(rt);
+ const center = ViewportController.getEntityWorldXY(rt);
+ if (src == null || !center) return;
+ e.stopPropagation();
+ const rot = ViewportController.getEntityWorldAngleRad(rt);
+ const cos = Math.cos(rot), sin = Math.sin(rot);
+ SceneCommands.beginGesture('Cone angle');
+ const onMove = (ev: PointerEvent) => {
+ const w = ViewportController.canvasToWorld(ev.clientX, ev.clientY);
+ if (!w) return;
+ const dx = w.x - center.x, dy = w.y - center.y;
+ const lx = dx * cos + dy * sin;
+ const ly = -dx * sin + dy * cos;
+ const halfDeg = Math.abs(Math.atan2(lx, ly)) * (180 / Math.PI);
+ SceneCommands.setField(src, 'ParticleEmitter', 'shapeAngle', 'number', Math.min(180, halfDeg * 2));
+ };
+ const onUp = () => {
+ SceneCommands.endGesture();
+ window.removeEventListener('pointermove', onMove);
+ window.removeEventListener('pointerup', onUp);
+ };
+ window.addEventListener('pointermove', onMove);
+ window.addEventListener('pointerup', onUp);
+}
+
// The interactive transform gizmo, drawn from the origin (= the selection pivot, the
// wrapper is translated there each frame). Its geometry mirrors the hit zones in
// gizmo.ts (GIZMO constants) so the handles a user aims at are the handles the tool
@@ -569,7 +631,7 @@ export function Viewport() {
if (poly) poly.style.opacity = '0';
if (circ) circ.style.opacity = '0';
}
- // Circle-collider radius handle (box halfExtents is a vec2 — a follow-up).
+ // Collider drag handles: circle radius (cl-handle) / box corner size (cl-size-handle).
const chnd = svg.querySelector('.cl-handle') as SVGCircleElement | null;
if (chnd) {
if (cg && cg.kind === 'circle' && cg.handle) {
@@ -580,6 +642,16 @@ export function Viewport() {
chnd.style.display = 'none';
}
}
+ const csz = svg.querySelector('.cl-size-handle') as SVGCircleElement | null;
+ if (csz) {
+ if (cg && cg.kind === 'box' && cg.sizeHandle) {
+ csz.setAttribute('cx', String(cg.sizeHandle.x));
+ csz.setAttribute('cy', String(cg.sizeHandle.y));
+ csz.style.display = '';
+ } else {
+ csz.style.display = 'none';
+ }
+ }
}
// Light2D gizmos — icon at the light, dashed reach circle (Point/Spot), direction
@@ -676,18 +748,23 @@ export function Viewport() {
if (poly) poly.style.opacity = '0';
if (circ) circ.style.opacity = '0';
}
- // Radius drag-handle (Circle/Cone): shown only when the emitter is on and
- // exposes one; hidden (display:none) otherwise so it can't grab pointers.
- const hnd = svg ? (svg.querySelector('.pe-handle') as SVGCircleElement | null) : null;
- if (hnd) {
- if (pg && pg.on && pg.handle) {
- hnd.setAttribute('cx', String(pg.handle.x));
- hnd.setAttribute('cy', String(pg.handle.y));
- hnd.style.display = '';
+ // Drag handles (radius Circle/Cone, size Rect, angle Cone): each shown only
+ // when the emitter is on and exposes it, else display:none so it can't grab
+ // pointers. The shape SVG spans the viewport so handle px are absolute.
+ const placePe = (cls: string, pt: { x: number; y: number } | null | undefined) => {
+ const el = svg ? (svg.querySelector(cls) as SVGCircleElement | null) : null;
+ if (!el) return;
+ if (pg && pg.on && pt) {
+ el.setAttribute('cx', String(pt.x));
+ el.setAttribute('cy', String(pt.y));
+ el.style.display = '';
} else {
- hnd.style.display = 'none';
+ el.style.display = 'none';
}
- }
+ };
+ placePe('.pe-handle', pg?.handle);
+ placePe('.pe-size-handle', pg?.sizeHandle);
+ placePe('.pe-angle-handle', pg?.angleHandle);
}
PerfMonitor.mark('gizmo.update', perfT0);
};
@@ -983,6 +1060,14 @@ export function Viewport() {
style={{ display: 'none' }}
onPointerDown={(e) => startRadiusHandleDrag(id, 'CircleCollider', 'radius', ViewportController.colliderPixelsPerUnit(), e)}
/>
+ <circle
+ className="cl-size-handle"
+ cx="0"
+ cy="0"
+ r="5"
+ style={{ display: 'none' }}
+ onPointerDown={(e) => startSizeHandleDrag(id, 'BoxCollider', 'halfExtents', ViewportController.colliderPixelsPerUnit(), false, e)}
+ />
</svg>
))}
@@ -1035,6 +1120,22 @@ export function Viewport() {
style={{ display: 'none' }}
onPointerDown={(e) => startRadiusHandleDrag(id, 'ParticleEmitter', 'shapeRadius', 1, e)}
/>
+ <circle
+ className="pe-size-handle"
+ cx="0"
+ cy="0"
+ r="5"
+ style={{ display: 'none' }}
+ onPointerDown={(e) => startSizeHandleDrag(id, 'ParticleEmitter', 'shapeSize', 1, true, e)}
+ />
+ <circle
+ className="pe-angle-handle"
+ cx="0"
+ cy="0"
+ r="5"
+ style={{ display: 'none' }}
+ onPointerDown={(e) => startAngleHandleDrag(id, e)}
+ />
</svg>
))}
diff --git a/desktop/src/theme/app.css b/desktop/src/theme/app.css
index 5435f42ca..72a7d9a1d 100644
--- a/desktop/src/theme/app.css
+++ b/desktop/src/theme/app.css
@@ -781,14 +781,18 @@ body.thumb-capture .viewport__play-stop {
stroke-width: 1.5;
opacity: 0;
}
-/* Draggable radius handle (circle collider) — write the radius (meters) on canvas. */
-.viewport__collider-gizmo .cl-handle {
+/* Draggable handles — circle radius / box corner size (meters), written on canvas. */
+.viewport__collider-gizmo .cl-handle,
+.viewport__collider-gizmo .cl-size-handle {
fill: #00dc82;
stroke: #1a1a1a;
stroke-width: 1.5;
pointer-events: auto;
cursor: ew-resize;
}
+.viewport__collider-gizmo .cl-size-handle {
+ cursor: nwse-resize;
+}
/* Particle-emitter gizmo: a clickable icon at the emitter + its spawn-shape overlay.
The icon wrapper is translate()'d to the emitter; the shape SVG spans the viewport
@@ -839,15 +843,25 @@ body.thumb-capture .viewport__play-stop {
stroke-width: 1.5;
opacity: 0;
}
-/* Draggable radius handle (Circle/Cone) — write shapeRadius on canvas. */
-.viewport__particle-shape .pe-handle {
+/* Draggable handles — radius (Circle/Cone), size corner (Rect), angle edge (Cone). */
+.viewport__particle-shape .pe-handle,
+.viewport__particle-shape .pe-size-handle,
+.viewport__particle-shape .pe-angle-handle {
fill: #ffb454;
stroke: #1a1a1a;
stroke-width: 1.5;
pointer-events: auto;
cursor: ew-resize;
}
-.viewport__particle-shape .pe-handle:hover {
+.viewport__particle-shape .pe-size-handle {
+ cursor: nwse-resize;
+}
+.viewport__particle-shape .pe-angle-handle {
+ cursor: crosshair;
+}
+.viewport__particle-shape .pe-handle:hover,
+.viewport__particle-shape .pe-size-handle:hover,
+.viewport__particle-shape .pe-angle-handle:hover {
fill: #ffd08a;
}