Files
DOSSIER/src/commands/cmds/bezier.ts
T
Claude 61263efe13 [K4] Zeichenbefehle im Kern: Elemente, Vorschauen, Gruppen-Zuordnung
Alle Erzeuge-Befehle bauen ihre Elemente im Kern: cmd_create.rs (Wand, Decke,
Raum, Treppe, Öffnung, Dach, Stütze, Extrusion), cmd_draw.rs (2D-Formen,
Texte, Bemassung, autodim), cmd_solid.rs (solid, solidbool, voidbox, terrain,
Deckenöffnung, Dachhof, Schnittlinie). Vorschauen über doc_create_preview(_again)
ohne JSON je Mausschritt, Abfragen über doc_create_query; Gruppen-Zuordnung als
`group` im Request ersetzt activeGroupOps. TS hält nur die Zustandsautomaten
(commands/createCore.ts). Ersetzte TS-Geometrie und alte Werkzeug-Commits in
tools/tools.ts gelöscht. Gleichheit alt/neu mit zeitweiligen Zufallstests
geprüft (s. TODO.md). Die drei Gruppen hängen über den gemeinsamen Dispatcher
zusammen und sind deshalb ein Commit.
2026-10-29 15:21:04 +00:00

146 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Bezier — klassisches Kontrollpunkt-Werkzeug (manuell gesetzte Griffe, kein
// Auto-Glätten wie `spline`). Jedes Segment braucht 3 Klicks nach dem Anker:
// 1) „Startpunkt:" → Punkt (erster Anker)
// 2) „Griff 1:" → Punkt (Kontrollpunkt 1 dieses Segments)
// 3) „Griff 2:" → Punkt (Kontrollpunkt 2 dieses Segments)
// 4) „Endpunkt:" → Punkt (Segment-Ende, = Anker des nächsten Segments)
// Nach ≥1 fertigem Segment kann „Fertig" (Enter) den Zug abschliessen; jeder
// weitere Anker startet automatisch ein neues Segment (Schritte 2–4).
import type {
Command,
CommandContext,
CommandResult,
CommandState,
CmdOption,
DraftShape,
ToolDraft,
Vec2,
} from "../types";
import { createMutation, createPreview } from "../createCore";
interface BezierStart extends CommandState {
phase: "start";
}
interface BezierC1 extends CommandState {
phase: "c1";
pts: Vec2[]; // abgeschlossene Punkte (Anker·Griff·Griff·Anker·…)
anchor: Vec2; // letzter Anker (= pts[pts.length-1])
cursor: Vec2 | null;
}
interface BezierC2 extends CommandState {
phase: "c2";
pts: Vec2[];
anchor: Vec2;
c1: Vec2;
cursor: Vec2 | null;
}
interface BezierEnd extends CommandState {
phase: "end";
pts: Vec2[];
anchor: Vec2;
c1: Vec2;
c2: Vec2;
cursor: Vec2 | null;
}
type BezierState = BezierStart | BezierC1 | BezierC2 | BezierEnd;
const FINISH: CmdOption = { id: "finish", labelKey: "cmd.bezier.finish" };
/** Vorschau: abgeschlossene Kurve (tessellierte Segmente) + Gummiband-Linien des laufenden Segments. */
function bezierDraft(ctx: CommandContext, pts: Vec2[], rubber: Vec2[]): ToolDraft {
// Bezier-Kette ab 4 Punkten im Kern (`cmd_draw.rs`, 24 Schritte je Kurve), Gummiband dazu.
const preview: DraftShape[] = pts.length >= 4 ? createPreview(ctx, { cmd: "bezier" }, pts).shapes : [];
for (let i = 0; i < rubber.length - 1; i++) preview.push({ kind: "line", a: rubber[i], b: rubber[i + 1] });
return { preview, vertices: pts.length > 0 ? [pts[0]] : [] };
}
/** Commit (Kern): Bezier-Kette (mindestens 4 Punkte). */
const appendBezier = (pts: Vec2[], ctx: CommandContext) => createMutation(ctx, { cmd: "bezier", pts });
const idle = (): [CommandState, CommandResult] => [
{ phase: "start", lastPoint: null } as BezierStart,
{ draft: null, done: true },
];
const finish = (pts: Vec2[], ctx: CommandContext): [CommandState, CommandResult] => [
{ phase: "start", lastPoint: null } as BezierStart,
{ draft: null, done: true, commit: appendBezier(pts, ctx) },
];
export const bezierCommand: Command = {
name: "bezier",
labelKey: "cmd.bezier.label",
prompt: (s) => {
const phase = (s as BezierState).phase;
return phase === "c1"
? "cmd.bezier.c1"
: phase === "c2"
? "cmd.bezier.c2"
: phase === "end"
? "cmd.bezier.end"
: "cmd.bezier.start";
},
accepts: () => ["point", "option"],
options: (s) => ((s as BezierState).phase === "c1" && (s as BezierC1).pts.length >= 4 ? [FINISH] : []),
init: (): BezierStart => ({ phase: "start", lastPoint: null }),
onInput: (state, input, ctx): [CommandState, CommandResult] => {
const s = state as BezierState;
if (input.kind === "option") {
if (input.id === "finish" && s.phase === "c1" && s.pts.length >= 4) return finish(s.pts, ctx);
return [s, { draft: null }];
}
if (input.kind !== "point") return [s, { draft: null }];
const pt = input.point;
if (s.phase === "start") {
const ns: BezierC1 = { phase: "c1", pts: [pt], anchor: pt, cursor: pt, lastPoint: pt };
return [ns, { draft: bezierDraft(ctx, ns.pts, [pt]) }];
}
if (s.phase === "c1") {
const ns: BezierC2 = { phase: "c2", pts: s.pts, anchor: s.anchor, c1: pt, cursor: pt, lastPoint: pt };
return [ns, { draft: bezierDraft(ctx, ns.pts, [s.anchor, pt]) }];
}
if (s.phase === "c2") {
const ns: BezierEnd = {
phase: "end",
pts: s.pts,
anchor: s.anchor,
c1: s.c1,
c2: pt,
cursor: pt,
lastPoint: pt,
};
return [ns, { draft: bezierDraft(ctx, ns.pts, [s.anchor, s.c1, pt]) }];
}
// phase "end": Segment abschliessen, direkt das nächste Segment beginnen.
const pts = [...s.pts, s.c1, s.c2, pt];
const ns: BezierC1 = { phase: "c1", pts, anchor: pt, cursor: pt, lastPoint: pt };
return [ns, { draft: bezierDraft(ctx, pts, [pt]) }];
},
onMove: (state, point, _snap, ctx): [CommandState, CommandResult] => {
const s = state as BezierState;
if (s.phase === "start") return [s, { draft: null }];
if (s.phase === "c1") {
const ns: BezierC1 = { ...s, cursor: point };
return [ns, { draft: bezierDraft(ctx, s.pts, [s.anchor, point]) }];
}
if (s.phase === "c2") {
const ns: BezierC2 = { ...s, cursor: point };
return [ns, { draft: bezierDraft(ctx, s.pts, [s.anchor, s.c1, point]) }];
}
const ns: BezierEnd = { ...s, cursor: point };
// Endschritt: die tatsächliche Bezier-Kurve bis zum Cursor live anzeigen.
return [ns, { draft: bezierDraft(ctx, [...s.pts, s.c1, s.c2, point], []) }];
},
onConfirm: (state, ctx): [CommandState, CommandResult] => {
const s = state as BezierState;
if (s.phase === "c1" && s.pts.length >= 4) return finish(s.pts, ctx);
return idle();
},
onCancel: (): [CommandState, CommandResult] => idle(),
};