Files
DOSSIER/src/commands/cmds/revcloud.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

133 lines
5.2 KiB
TypeScript

// Revisionswolke (Nutzer-Wunsch: „Keine Revisionswolken/Markup-Werkzeuge").
// Schritte (wie `polyline.ts`, aber ohne dessen Bogen-/Tangenten-Unteroptionen
// — JEDES Segment wird automatisch zu Beulen, s. `cmd_draw.rs::revision_cloud`):
// 1) „Startpunkt:" → Punkt
// 2) „Nächster Punkt ( Schliessen ):" → Punkt … Enter beendet OFFEN (≥2
// Punkte); Klick nahe dem Startpunkt ODER die Option „Schliessen"
// committet als RING (≥3 Punkte).
// Beulengrösse (Ziel-Bogenlänge) per Option, zyklisch, persistiert über
// Aufrufe hinweg (wie `lastSides` bei `polygon.ts`).
import { segmentHud } from "../../tools/types";
import type {
CmdOption,
Command,
CommandContext,
CommandResult,
CommandState,
ToolDraft,
Vec2,
} from "../types";
import { createMutation, createPreview } from "../createCore";
const EPS = 1e-6;
const CLOSE_HIT = 0.15; // Modell-Meter: Klick nahe Startpunkt schliesst den Zug
const ARC_SIZE_PRESETS = [0.2, 0.3, 0.5, 0.8, 1.2];
let lastArcSize = 0.3;
const nextArcSize = (): number =>
ARC_SIZE_PRESETS[(ARC_SIZE_PRESETS.indexOf(lastArcSize) + 1) % ARC_SIZE_PRESETS.length];
const ARC_SIZE_OPTION: CmdOption = { id: "arcSize", labelKey: "cmd.revcloud.arcSize" };
const CLOSE: CmdOption = { id: "close", labelKey: "cmd.revcloud.close" };
const segLen = (a: Vec2, b: Vec2): number => Math.hypot(b.x - a.x, b.y - a.y);
interface RevcloudIdle extends CommandState {
phase: "start";
}
interface RevcloudDrawing extends CommandState {
phase: "next";
points: Vec2[];
cursor: Vec2 | null;
}
type RevcloudState = RevcloudIdle | RevcloudDrawing;
/** Vorschau: die tatsächliche Beulen-Kontur (tesselliert), inkl. Gummiband zum Cursor. */
function revcloudDraft(ctx: CommandContext, points: Vec2[], cursor: Vec2 | null): ToolDraft {
// Beulen-Kontur im Kern (`cmd_draw.rs`): je Segment gleich lange Bögen der Ziel-Bogenlänge, tesselliert.
const all = cursor ? [...points, cursor] : points;
const draft: ToolDraft = { preview: createPreview(ctx, { cmd: "revcloud", arcSize: lastArcSize }, all).shapes, vertices: points };
const last = points[points.length - 1];
if (cursor && last && segLen(last, cursor) >= EPS) draft.hud = segmentHud(last, cursor);
return draft;
}
/** Commit (Kern): Revisionswolke als Polylinie mit Bögen (weniger als 2 Punkte → nichts). */
const appendRevcloud = (points: Vec2[], closed: boolean, ctx: CommandContext) =>
createMutation(ctx, { cmd: "revcloud", pts: points, closed, arcSize: lastArcSize });
const idle = (): [CommandState, CommandResult] => [
{ phase: "start", lastPoint: null } as RevcloudIdle,
{ draft: null, done: true },
];
export const revcloudCommand: Command = {
name: "revcloud",
labelKey: "cmd.revcloud.label",
prompt: (s) => ((s as RevcloudState).phase === "next" ? "cmd.revcloud.next" : "cmd.revcloud.start"),
accepts: () => ["point", "option"],
options: (s) => {
const st = s as RevcloudState;
const base = [{ ...ARC_SIZE_OPTION, value: `${lastArcSize}m` }];
return st.phase === "next" && st.points.length >= 3 ? [CLOSE, ...base] : base;
},
init: (): RevcloudIdle => ({ phase: "start", lastPoint: null }),
onInput: (state, input, ctx): [CommandState, CommandResult] => {
const s = state as RevcloudState;
if (input.kind === "option") {
if (input.id === "arcSize") {
lastArcSize = nextArcSize();
return [
s,
{ draft: s.phase === "next" ? revcloudDraft(ctx, s.points, s.cursor) : null },
];
}
if (input.id === "close" && s.phase === "next" && s.points.length >= 3) {
const pts = s.points;
return [idle()[0], { draft: null, done: true, commit: appendRevcloud(pts, true, ctx) }];
}
return [s, { draft: null }];
}
if (input.kind !== "point") {
return [s, { draft: s.phase === "next" ? revcloudDraft(ctx, s.points, s.cursor) : null }];
}
const pt = input.point;
if (s.phase !== "next") {
const ns: RevcloudDrawing = { phase: "next", points: [pt], cursor: pt, lastPoint: pt };
return [ns, { draft: revcloudDraft(ctx, [pt], pt) }];
}
// Klick nahe Startpunkt (≥3 Punkte) → schliessen (committet als Ring).
if (s.points.length >= 3 && segLen(s.points[0], pt) < CLOSE_HIT) {
const pts = s.points;
return [idle()[0], { draft: null, done: true, commit: appendRevcloud(pts, true, ctx) }];
}
const points = [...s.points, pt];
const ns: RevcloudDrawing = { phase: "next", points, cursor: pt, lastPoint: pt };
return [ns, { draft: revcloudDraft(ctx, points, pt) }];
},
onMove: (state, point, _snap, ctx): [CommandState, CommandResult] => {
const s = state as RevcloudState;
if (s.phase !== "next") return [s, { draft: null }];
const ns: RevcloudDrawing = { ...s, cursor: point };
return [ns, { draft: revcloudDraft(ctx, s.points, point) }];
},
// Enter/Space/Rechtsklick: Zug als OFFENE Wolke beenden (≥2 Punkte).
onConfirm: (state, ctx): [CommandState, CommandResult] => {
const s = state as RevcloudState;
if (s.phase === "next" && s.points.length >= 2) {
const pts = s.points;
return [idle()[0], { draft: null, done: true, commit: appendRevcloud(pts, false, ctx) }];
}
return idle();
},
onCancel: (): [CommandState, CommandResult] => idle(),
};