[K4] Merge: Zeichenbefehle im Kern (cmd_create/cmd_draw/cmd_solid), Gruppen-Zuordnung beim Zeichnen

Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
Claude committed 2026-10-29 15:30:07 +00:00
commit 012a7e82c7
92 files changed
+4169 -4754

No files matched your search

+36 -417
View File
@@ -1,24 +1,9 @@
// Konkrete Werkzeuge (Phase 1: select / wall / line) + Registry.
// docs/design/drawing-tools.md §3, §4, §10.
// Werkzeug-Registry (docs/design/drawing-tools.md §3, §4, §10). Alle Zeichenwerkzeuge sind an
// Befehle gekoppelt (TOOL_COMMAND in App.tsx); die Elemente baut der Kern ([K4] S3,
// `src-tauri/core/src/cmd_create.rs`). Hier stehen nur noch Platzhalter (Label, Hinweis) und
// das Auswahl-Werkzeug.
import type { Ceiling, Drawing2D, Project, Vec2, Wall } from "../model/types";
import { wallTypeThickness } from "../model/types";
import { wallCorners } from "../model/geometry";
import { addItems, ceilingArea, normalizeOutline, opsMutation } from "../core";
import type { CoreOp } from "../core";
import type { DraftShape, Tool, ToolContext, ToolDraft, ToolId } from "./types";
import { uniqueId } from "./types";
const EPS = 1e-6;
const segLen = (a: Vec2, b: Vec2): number => Math.hypot(b.x - a.x, b.y - a.y);
const segAngleDeg = (a: Vec2, b: Vec2): number =>
(Math.atan2(b.y - a.y, b.x - a.x) * 180) / Math.PI;
/** Dicke des aktiven Wandtyps (Meter); Fallback, falls unbekannt. */
function activeWallThickness(ctx: ToolContext): number {
const wt = ctx.project.wallTypes.find((t) => t.id === ctx.activeWallTypeId);
return wt ? wallTypeThickness(wt) : 0.2;
}
import type { Tool, ToolId } from "./types";
// ── Select ──────────────────────────────────────────────────────────────────
// Reines Platzhalter-Werkzeug: die Auswahl-/Marquee-Logik bleibt in PlanView;
@@ -34,200 +19,32 @@ const selectTool: Tool = {
onCancel: (s) => [s, { draft: null, done: true }],
};
// ── Wall ─────────────────────────────────────────────────────────────────────
// Zeichnet eine Achs-Polylinie; jedes Segment wird ein eigenständiges Wall.
// Die Gehrung an Ecken entsteht automatisch aus der Verschneidung im Kern (`joins.rs`).
interface WallDrawing {
phase: "drawing";
points: Vec2[];
cursor: Vec2 | null;
}
type WallState = { phase: "idle" } | WallDrawing;
/** Baut die Band-Vorschau für eine Achs-Polylinie + lebendes Segment. */
function wallDraft(points: Vec2[], cursor: Vec2 | null, ctx: ToolContext): ToolDraft {
const thickness = activeWallThickness(ctx);
const preview: DraftShape[] = [];
const all = cursor ? [...points, cursor] : points;
for (let i = 0; i < all.length - 1; i++) {
const a = all[i];
const b = all[i + 1];
if (segLen(a, b) < EPS) continue;
preview.push({ kind: "poly", pts: wallCorners(a, b, thickness), closed: true });
preview.push({ kind: "line", a, b }); // Achse
}
const draft: ToolDraft = { preview, vertices: points };
// HUD am lebenden Segment: Länge + Winkel.
if (cursor && points.length > 0) {
const a = points[points.length - 1];
if (segLen(a, cursor) >= EPS) {
draft.hud = {
at: cursor,
text: `${segLen(a, cursor).toFixed(2)} m · ${Math.abs(
((segAngleDeg(a, cursor) + 360) % 360),
).toFixed(0)}°`,
};
}
}
return draft;
}
/** Ops: hängt je Achssegment ein Wall des aktiven Typs an. */
function appendWalls(pts: Vec2[], ctx: ToolContext): CoreOp[] {
const height =
ctx.level.floorHeight && ctx.level.floorHeight > 0 ? ctx.level.floorHeight : 2.6;
const newWalls: Wall[] = [];
for (let i = 0; i < pts.length - 1; i++) {
const a = pts[i];
const b = pts[i + 1];
if (segLen(a, b) < EPS) continue;
newWalls.push({
id: uniqueId("W"),
type: "wall",
floorId: ctx.level.id,
categoryCode: ctx.defaultCategoryCode,
start: a,
end: b,
wallTypeId: ctx.activeWallTypeId,
height,
});
}
return addItems("walls", newWalls);
}
// ── Wall / Ceiling ───────────────────────────────────────────────────────────
// An die gekoppelten Befehle `wall`/`ceiling` (TOOL_COMMAND in App.tsx) gebunden: Klick und
// getipptes „wall“ münden in denselben Befehl, die Elemente baut der Kern ([K4] S3,
// `cmd_create.rs`). Diese Tool-Objekte sind reine Platzhalter (wie window/door).
const wallTool: Tool = {
id: "wall",
labelKey: "tool.wall",
floorOnly: true,
hintKey: (s) =>
(s as WallState).phase === "drawing" ? "tool.wall.nextPoint" : "tool.wall.firstPoint",
hintKey: () => "tool.wall.firstPoint",
init: () => ({ phase: "idle" }),
onClick: (state, p, ctx) => {
const s = state as WallState;
const points = s.phase === "drawing" ? [...s.points, p.point] : [p.point];
const next: WallState = { phase: "drawing", points, cursor: p.point };
return [next, { draft: wallDraft(points, p.point, ctx) }];
},
onMove: (state, p, ctx) => {
const s = state as WallState;
if (s.phase !== "drawing") return [s, { draft: { preview: [], vertices: [] } }];
const next: WallState = { ...s, cursor: p.point };
return [next, { draft: wallDraft(s.points, p.point, ctx) }];
},
onCommitGesture: (state, ctx) => {
const s = state as WallState;
if (s.phase !== "drawing" || s.points.length < 2) {
return [{ phase: "idle" }, { draft: null, done: true }];
}
const pts = s.points;
return [
{ phase: "idle" },
{ draft: null, done: true, commit: opsMutation(() => appendWalls(pts, ctx)) },
];
},
onCancel: () => [{ phase: "idle" }, { draft: null, done: true }],
onUndoPoint: (state, ctx) => {
const s = state as WallState;
if (s.phase === "drawing" && s.points.length > 1) {
const points = s.points.slice(0, -1);
const next: WallState = { phase: "drawing", points, cursor: s.cursor };
return [next, { draft: wallDraft(points, s.cursor, ctx) }];
}
return [{ phase: "idle" }, { draft: null, done: true }];
},
onClick: (s) => [s, { draft: null }],
onMove: (s) => [s, { draft: null }],
onCommitGesture: (s) => [s, { draft: null, done: true }],
onCancel: (s) => [s, { draft: null, done: true }],
};
// ── Ceiling ────────────────────────────────────────────────────────────────
// Zeichnet einen GESCHLOSSENEN Umriss (wie eine geschlossene Polylinie) und
// committet daraus eine `Ceiling`. Der reguläre Zeichenpfad läuft über den
// gekoppelten `ceiling`-Befehl (TOOL_COMMAND); dieses Legacy-Tool ist eine
// vollständige, funktionsfähige Alternative (kein Stub).
interface CeilDrawing {
phase: "drawing";
points: Vec2[];
cursor: Vec2 | null;
}
type CeilState = { phase: "idle" } | CeilDrawing;
function ceilDraft(points: Vec2[], cursor: Vec2 | null): ToolDraft {
const all = cursor ? [...points, cursor] : points;
const preview: DraftShape[] = [{ kind: "poly", pts: all, closed: all.length >= 3 }];
const draft: ToolDraft = { preview, vertices: points };
if (cursor && points.length > 0) {
const a = points[points.length - 1];
if (segLen(a, cursor) >= EPS) {
draft.hud = {
at: cursor,
text: `${segLen(a, cursor).toFixed(2)} m · ${Math.abs(
(segAngleDeg(a, cursor) + 360) % 360,
).toFixed(0)}°`,
};
}
}
return draft;
}
function appendCeiling(project: Project, pts: Vec2[], ctx: ToolContext): CoreOp[] {
const outline = normalizeOutline(pts);
if (!outline || ceilingArea(outline) < 1e-4) return [];
const hasCat = project.layers.some((c) => c.code === "30");
const c: Ceiling = {
id: uniqueId("C"),
type: "ceiling",
floorId: ctx.level.id,
categoryCode: hasCat ? "30" : ctx.defaultCategoryCode,
outline,
wallTypeId: ctx.activeWallTypeId,
};
return addItems("ceilings", [c]);
}
const ceilingTool: Tool = {
id: "ceiling",
labelKey: "tool.ceiling",
floorOnly: true,
hintKey: (s) =>
(s as CeilState).phase === "drawing"
? "tool.ceiling.nextPoint"
: "tool.ceiling.firstPoint",
hintKey: () => "tool.ceiling.firstPoint",
init: () => ({ phase: "idle" }),
onClick: (state, p, ctx) => {
const s = state as CeilState;
if (s.phase !== "drawing") {
return [{ phase: "drawing", points: [p.point], cursor: p.point }, { draft: ceilDraft([p.point], p.point) }];
}
// Klick auf den Startpunkt schließt den Umriss (ab 3 Punkten) und committet.
if (s.points.length >= 3 && segLen(p.point, s.points[0]) < EPS) {
const pts = s.points;
return [{ phase: "idle" }, { draft: null, done: true, commit: opsMutation((proj) => appendCeiling(proj, pts, ctx)) }];
}
const points = [...s.points, p.point];
return [{ phase: "drawing", points, cursor: p.point }, { draft: ceilDraft(points, p.point) }];
},
onMove: (state, p) => {
const s = state as CeilState;
if (s.phase !== "drawing") return [s, { draft: { preview: [], vertices: [] } }];
return [{ ...s, cursor: p.point }, { draft: ceilDraft(s.points, p.point) }];
},
onCommitGesture: (state, ctx) => {
const s = state as CeilState;
if (s.phase !== "drawing" || s.points.length < 3) {
return [{ phase: "idle" }, { draft: null, done: true }];
}
const pts = s.points;
return [{ phase: "idle" }, { draft: null, done: true, commit: opsMutation((proj) => appendCeiling(proj, pts, ctx)) }];
},
onCancel: () => [{ phase: "idle" }, { draft: null, done: true }],
onUndoPoint: (state) => {
const s = state as CeilState;
if (s.phase === "drawing" && s.points.length > 1) {
const points = s.points.slice(0, -1);
return [{ phase: "drawing", points, cursor: s.cursor }, { draft: ceilDraft(points, s.cursor) }];
}
return [{ phase: "idle" }, { draft: null, done: true }];
},
onClick: (s) => [s, { draft: null }],
onMove: (s) => [s, { draft: null }],
onCommitGesture: (s) => [s, { draft: null, done: true }],
onCancel: (s) => [s, { draft: null, done: true }],
};
// ── Window / Door (Öffnungen) ─────────────────────────────────────────────────
@@ -293,123 +110,19 @@ const roomTool: Tool = {
onCancel: (s) => [s, { draft: null, done: true }],
};
// ── Line ─────────────────────────────────────────────────────────────────────
// Zwei-Klick-Strecke → Drawing2D{shape:"line"}.
interface LineDrawing {
phase: "drawing";
a: Vec2;
cursor: Vec2 | null;
}
type LineState = { phase: "idle" } | LineDrawing;
function lineDraft(a: Vec2, cursor: Vec2 | null): ToolDraft {
const preview: DraftShape[] = [];
if (cursor && segLen(a, cursor) >= EPS) preview.push({ kind: "line", a, b: cursor });
const draft: ToolDraft = { preview, vertices: [a] };
if (cursor && segLen(a, cursor) >= EPS) {
draft.hud = {
at: cursor,
text: `${segLen(a, cursor).toFixed(2)} m · ${Math.abs(
(segAngleDeg(a, cursor) + 360) % 360,
).toFixed(0)}°`,
};
}
return draft;
}
function appendLine(a: Vec2, b: Vec2, ctx: ToolContext): CoreOp[] {
const d: Drawing2D = {
id: uniqueId("dr2d"),
type: "drawing2d",
levelId: ctx.level.id,
categoryCode: ctx.defaultCategoryCode,
geom: { shape: "line", a, b },
// Kein expliziter Linienstil → erbt Farbe/Strichstärke aus der aktiven Ebene
// (Kategorie). Ein Linienstil-Picker kann das später überschreiben.
};
return addItems("drawings2d", [d]);
}
// ── Line / Polyline / Rectangle ──────────────────────────────────────────────
// Gekoppelt an die Befehle `line`/`polyline`/`rect` (TOOL_COMMAND); reine Platzhalter.
const lineTool: Tool = {
id: "line",
labelKey: "tool.line",
hintKey: (s) =>
(s as LineState).phase === "drawing" ? "tool.line.secondPoint" : "tool.line.firstPoint",
hintKey: () => "tool.line.firstPoint",
init: () => ({ phase: "idle" }),
onClick: (state, p, ctx) => {
const s = state as LineState;
if (s.phase !== "drawing") {
return [{ phase: "drawing", a: p.point, cursor: p.point }, { draft: lineDraft(p.point, p.point) }];
}
// Zweiter Klick: Strecke abschließen (Null-Strecke verwerfen).
if (segLen(s.a, p.point) < EPS) {
return [{ phase: "idle" }, { draft: null, done: true }];
}
const a = s.a;
const b = p.point;
return [
{ phase: "idle" },
{ draft: null, done: true, commit: opsMutation(() => appendLine(a, b, ctx)) },
];
},
onMove: (state, p) => {
const s = state as LineState;
if (s.phase !== "drawing") return [s, { draft: { preview: [], vertices: [] } }];
return [{ ...s, cursor: p.point }, { draft: lineDraft(s.a, p.point) }];
},
// Linie braucht genau zwei Punkte; Doppelklick/Enter bricht den offenen
// Entwurf ab (kein gültiges zweites Ende gesetzt).
onCommitGesture: () => [{ phase: "idle" }, { draft: null, done: true }],
onCancel: () => [{ phase: "idle" }, { draft: null, done: true }],
onClick: (s) => [s, { draft: null }],
onMove: (s) => [s, { draft: null }],
onCommitGesture: (s) => [s, { draft: null, done: true }],
onCancel: (s) => [s, { draft: null, done: true }],
};
// ── Polyline ─────────────────────────────────────────────────────────────────
// Mehrteilige 2D-Polylinie → Drawing2D{shape:"polyline"}. Abschluss per Doppel-/
// Rechtsklick/Enter (offen) ODER Klick auf den Startpunkt (geschlossen).
interface PolyDrawing {
phase: "drawing";
points: Vec2[];
cursor: Vec2 | null;
}
type PolyState = { phase: "idle" } | PolyDrawing;
function polyDraft(points: Vec2[], cursor: Vec2 | null): ToolDraft {
const preview: DraftShape[] = [];
const all = cursor ? [...points, cursor] : points;
for (let i = 0; i < all.length - 1; i++) {
if (segLen(all[i], all[i + 1]) >= EPS) preview.push({ kind: "line", a: all[i], b: all[i + 1] });
}
const draft: ToolDraft = { preview, vertices: points };
if (cursor && points.length > 0) {
const a = points[points.length - 1];
if (segLen(a, cursor) >= EPS) {
draft.hud = {
at: cursor,
text: `${segLen(a, cursor).toFixed(2)} m · ${Math.abs(
(segAngleDeg(a, cursor) + 360) % 360,
).toFixed(0)}°`,
};
}
}
return draft;
}
function appendPolyline(pts: Vec2[], closed: boolean, ctx: ToolContext): CoreOp[] {
if (pts.length < 2) return [];
const d: Drawing2D = {
id: uniqueId("dr2d"),
type: "drawing2d",
levelId: ctx.level.id,
categoryCode: ctx.defaultCategoryCode,
geom: { shape: "polyline", pts, closed },
// Kein expliziter Linienstil → erbt Farbe/Strichstärke aus der aktiven Ebene
// (Kategorie). Ein Linienstil-Picker kann das später überschreiben.
};
return addItems("drawings2d", [d]);
}
// Konstruktionslinie: Platzhalter-Werkzeug, gekoppelt an den `xline`-Befehl
// (zwei Punkte legen die Richtung fest, Rendering/Snapping verlängern weit
// darüber hinaus). Nicht floorOnly (freies 2D-Element).
@@ -440,117 +153,23 @@ const rayTool: Tool = {
const polylineTool: Tool = {
id: "polyline",
labelKey: "tool.polyline",
hintKey: (s) =>
(s as PolyState).phase === "drawing" ? "tool.polyline.nextPoint" : "tool.polyline.firstPoint",
hintKey: () => "tool.polyline.firstPoint",
init: () => ({ phase: "idle" }),
onClick: (state, p, ctx) => {
const s = state as PolyState;
if (s.phase !== "drawing") {
return [{ phase: "drawing", points: [p.point], cursor: p.point }, { draft: polyDraft([p.point], p.point) }];
}
// Klick auf den Startpunkt schließt die Polylinie (ab 3 Punkten).
if (s.points.length >= 3 && segLen(p.point, s.points[0]) < EPS) {
const pts = s.points;
return [{ phase: "idle" }, { draft: null, done: true, commit: opsMutation(() => appendPolyline(pts, true, ctx)) }];
}
const points = [...s.points, p.point];
return [{ phase: "drawing", points, cursor: p.point }, { draft: polyDraft(points, p.point) }];
},
onMove: (state, p) => {
const s = state as PolyState;
if (s.phase !== "drawing") return [s, { draft: { preview: [], vertices: [] } }];
return [{ ...s, cursor: p.point }, { draft: polyDraft(s.points, p.point) }];
},
onCommitGesture: (state, ctx) => {
const s = state as PolyState;
if (s.phase !== "drawing" || s.points.length < 2) {
return [{ phase: "idle" }, { draft: null, done: true }];
}
const pts = s.points;
return [{ phase: "idle" }, { draft: null, done: true, commit: opsMutation(() => appendPolyline(pts, false, ctx)) }];
},
onCancel: () => [{ phase: "idle" }, { draft: null, done: true }],
onUndoPoint: (state) => {
const s = state as PolyState;
if (s.phase === "drawing" && s.points.length > 1) {
const points = s.points.slice(0, -1);
return [{ phase: "drawing", points, cursor: s.cursor }, { draft: polyDraft(points, s.cursor) }];
}
return [{ phase: "idle" }, { draft: null, done: true }];
},
onClick: (s) => [s, { draft: null }],
onMove: (s) => [s, { draft: null }],
onCommitGesture: (s) => [s, { draft: null, done: true }],
onCancel: (s) => [s, { draft: null, done: true }],
};
// ── Rectangle ────────────────────────────────────────────────────────────────
// Zwei Ecken (Klick-Klick) → Drawing2D{shape:"rect"} (achsparallel, min/max sortiert).
interface RectDrawing {
phase: "drawing";
p0: Vec2;
cursor: Vec2 | null;
}
type RectState = { phase: "idle" } | RectDrawing;
function rectCorners(a: Vec2, b: Vec2): Vec2[] {
const minX = Math.min(a.x, b.x), maxX = Math.max(a.x, b.x);
const minY = Math.min(a.y, b.y), maxY = Math.max(a.y, b.y);
return [
{ x: minX, y: minY },
{ x: maxX, y: minY },
{ x: maxX, y: maxY },
{ x: minX, y: maxY },
];
}
function rectDraft(p0: Vec2, cursor: Vec2 | null): ToolDraft {
const preview: DraftShape[] = [];
if (cursor) preview.push({ kind: "poly", pts: rectCorners(p0, cursor), closed: true });
const draft: ToolDraft = { preview, vertices: [p0] };
if (cursor) {
const w = Math.abs(cursor.x - p0.x);
const h = Math.abs(cursor.y - p0.y);
draft.hud = { at: cursor, text: `${w.toFixed(2)} × ${h.toFixed(2)} m` };
}
return draft;
}
function appendRect(a: Vec2, b: Vec2, ctx: ToolContext): CoreOp[] {
const min = { x: Math.min(a.x, b.x), y: Math.min(a.y, b.y) };
const max = { x: Math.max(a.x, b.x), y: Math.max(a.y, b.y) };
if (Math.abs(max.x - min.x) < EPS || Math.abs(max.y - min.y) < EPS) return [];
const d: Drawing2D = {
id: uniqueId("dr2d"),
type: "drawing2d",
levelId: ctx.level.id,
categoryCode: ctx.defaultCategoryCode,
geom: { shape: "rect", min, max },
// Kein expliziter Linienstil → erbt Farbe/Strichstärke aus der aktiven Ebene
// (Kategorie). Ein Linienstil-Picker kann das später überschreiben.
};
return addItems("drawings2d", [d]);
}
const rectTool: Tool = {
id: "rect",
labelKey: "tool.rect",
hintKey: (s) =>
(s as RectState).phase === "drawing" ? "tool.rect.secondCorner" : "tool.rect.firstCorner",
hintKey: () => "tool.rect.firstCorner",
init: () => ({ phase: "idle" }),
onClick: (state, p, ctx) => {
const s = state as RectState;
if (s.phase !== "drawing") {
return [{ phase: "drawing", p0: p.point, cursor: p.point }, { draft: rectDraft(p.point, p.point) }];
}
const a = s.p0;
const b = p.point;
return [{ phase: "idle" }, { draft: null, done: true, commit: opsMutation(() => appendRect(a, b, ctx)) }];
},
onMove: (state, p) => {
const s = state as RectState;
if (s.phase !== "drawing") return [s, { draft: { preview: [], vertices: [] } }];
return [{ ...s, cursor: p.point }, { draft: rectDraft(s.p0, p.point) }];
},
onCommitGesture: () => [{ phase: "idle" }, { draft: null, done: true }],
onCancel: () => [{ phase: "idle" }, { draft: null, done: true }],
onClick: (s) => [s, { draft: null }],
onMove: (s) => [s, { draft: null }],
onCommitGesture: (s) => [s, { draft: null, done: true }],
onCancel: (s) => [s, { draft: null, done: true }],
};
// Kreis: Platzhalter-Werkzeug (analog window/door), gekoppelt an den