// Dock-Panel „Objekte" ([253]) — Palette der Standard-Objektbibliothek 2D: Gruppen (Sanitär, Küche, Tische, …) // mit Vorschau-Symbol je Objekt. Klick wählt das Objekt und startet den Befehl `libobj` (Einfügepunkt klicken, // R = drehen, M = spiegeln). Der Katalog kommt aus dem Rust-Kern; die Vorschau zeichnet der Kern (grob). import { useEffect, useMemo, useRef, useState } from "react"; import { t } from "../i18n"; import type { TranslationKey } from "../i18n"; import { usePanelHostAction } from "./host"; import { setActiveLibObject } from "../commands/cmds/libobj"; import { createPaletteDrag, currentPlanDropTarget } from "../state/planDropTarget"; import { libCatalog, libDefaultParams, libGeometry, libGeometryToPolys, libGroups, polysBounds, type LibObjectDef, } from "../model/furnitureLib"; const SIZE = 44; /** Vorschau: Linien des Objekts (grob) als kleines SVG, Plan-y nach oben. */ function Preview({ def }: { def: LibObjectDef }) { const polys = useMemo( () => libGeometryToPolys(libGeometry(def.id, libDefaultParams(def), "grob", 1), (p) => ({ x: p.x, y: -p.y })), [def], ); const b = polysBounds(polys); if (!b) return ; const w = Math.max(b.maxX - b.minX, 1e-3); const h = Math.max(b.maxY - b.minY, 1e-3); const pad = 3; const k = (SIZE - 2 * pad) / Math.max(w, h); const tx = (x: number) => pad + (x - b.minX) * k + ((SIZE - 2 * pad) - w * k) / 2; const ty = (y: number) => pad + (y - b.minY) * k + ((SIZE - 2 * pad) - h * k) / 2; return ( {polys.map((pl, i) => { const d = pl.pts.map((p, j) => `${j ? "L" : "M"}${tx(p.x).toFixed(2)} ${ty(p.y).toFixed(2)}`).join(" ") + (pl.closed ? "Z" : ""); return ; })} ); } const OPEN_KEY = "cad.objlib.open"; /** Gemerkter Zustand der Gruppen (offen/zu) je Gruppen-Schlüssel; leer, wenn nichts gespeichert ist. */ function loadOpen(): Record { try { const raw = localStorage.getItem(OPEN_KEY); const v: unknown = raw ? JSON.parse(raw) : null; return v && typeof v === "object" ? (v as Record) : {}; } catch { return {}; } } export function ObjectLibraryPanel() { const [open, setOpen] = useState>(loadOpen); const setGroupOpen = (g: string, v: boolean) => setOpen((o) => { const next = { ...o, [g]: v }; try { localStorage.setItem(OPEN_KEY, JSON.stringify(next)); } catch { /* Speicher gesperrt: der Zustand gilt nur für diese Sitzung */ } return next; }); const onRunCommand = usePanelHostAction("onRunCommand"); const catalog = useMemo(() => libCatalog(), []); const groups = useMemo(() => libGroups(), []); const place = (def: LibObjectDef) => { setActiveLibObject(def.id, libDefaultParams(def)); onRunCommand("libobj"); }; // Ziehen in den Plan: Zeigerdruck startet den Befehl wie ein Klick; Bewegen über der Zeichenfläche führt die // Vorschau nach, Loslassen dort setzt den Punkt, Loslassen daneben oder Esc bricht ab. Ohne Ziehen bleibt es ein Klick. const drag = useRef(createPaletteDrag(currentPlanDropTarget)).current; useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") drag.abort(); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [drag]); const mods = (e: React.PointerEvent) => ({ shift: e.shiftKey, ctrl: e.ctrlKey || e.metaKey, alt: e.altKey }); if (catalog.objects.length === 0) { return {t("lib.empty")}; } return ( {t("lib.hint")} {groups.map((g) => ( { const v = e.currentTarget.open; if (v !== (open[g] ?? g === groups[0])) setGroupOpen(g, v); }} style={{ marginBottom: 4 }}> {t(`lib.g.${g}` as TranslationKey)} {catalog.objects .filter((o) => o.group === g) .map((o) => { const name = t(`lib.o.${o.id.replace(/^lib:/, "")}` as TranslationKey); return ( { if (e.button !== 0) return; place(o); if (e.pointerType === "touch") return; // Touch scrollt das Panel: nur Klick try { e.currentTarget.setPointerCapture(e.pointerId); } catch { /* ohne Capture: Ziehen entfällt, der Klick-Weg bleibt */ } drag.down(e.clientX, e.clientY); }} onPointerMove={(e) => drag.move(e.clientX, e.clientY, mods(e))} onPointerUp={(e) => { drag.up(e.clientX, e.clientY, mods(e)); if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId); }} onPointerCancel={() => drag.abort()} // Tastatur (Enter/Leertaste, detail 0): Zeigerdruck hat den Befehl schon gestartet. onClick={(e) => { if (e.detail === 0) place(o); }} style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: 2, padding: "4px 2px", border: "1px solid var(--border)", borderRadius: 4, background: "var(--field-bg)", color: "var(--ink)", cursor: "pointer", font: "inherit", }} > {name} ); })} ))} ); } /** Tab-Symbol: Stuhl in der Draufsicht. */ export function ObjectLibraryTabIcon() { return ( ); }