[149] Baum-Beschriftung mit Griff verschiebbar (Tree.labelOffset)

Versatz des linken Zeilenrands ab Stammmitte im Rust-Modell, dritter Griff bei gezeigter Beschriftung.
Tests plan::trees::beschriftung_mit_art_name, treeGrips.test.ts.
This commit is contained in:
Claude committed 2026-11-07 13:39:42 +00:00
1 parent 243aa19d25
commit f1116c3705
7 files changed
+101 -11

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
import { describe, expect, it } from "vitest";
import type { Tree } from "../model/types";
import { treeGripPatch, treeGripPoints } from "./treeGrips";
const base = {
id: "b",
type: "tree",
floorId: "eg",
categoryCode: "13",
position: { x: 2, y: 3 },
trunkDiameter: 0.4,
crownDiameter: 6,
crownBase: 3,
height: 12,
shape: "round",
status: "new",
foliage: "deciduous",
} as unknown as Tree;
describe("[149] Baum-Griffe", () => {
it("Beschriftungs-Griff nur mit gezeigter Beschriftung, Vorgabe rechts neben dem Stamm", () => {
expect(treeGripPoints(base, 100)).toHaveLength(2);
const t = { ...base, showLabel: true, species: "Quercus" };
const g = treeGripPoints(t, 100)[2];
expect(g.x).toBeCloseTo(2.3, 12);
expect(g.y).toBe(3);
expect(treeGripPoints({ ...t, labelOffset: { x: -1, y: 2 } }, 100)[2]).toEqual({ x: 1, y: 5 });
});
it("Ziehen: Stammmitte, Kronendurchmesser, Beschriftungs-Versatz ab Stammmitte (Kern-Test plan::trees)", () => {
const t = { ...base, showLabel: true, species: "Quercus" };
expect(treeGripPatch(t, 0, { x: 5, y: 5 })).toEqual({ position: { x: 5, y: 5 } });
expect(treeGripPatch(t, 1, { x: 2, y: 6 })).toEqual({ crownDiameter: 6 });
const patch = treeGripPatch(t, 2, { x: 0, y: 7 });
expect(patch).toEqual({ labelOffset: { x: -2, y: 4 } });
});
});
+28
View File
@@ -0,0 +1,28 @@
// [149] Griffe eines Baums im Grundriss: Stammmitte (verschieben), Kronenrand (Durchmesser) und — mit gezeigter
// Beschriftung — linker Rand der Beschriftung (`labelOffset` ab Stammmitte, Kern `plan/trees.rs::tree_label`).
//
// Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
import type { Tree, Vec2 } from "../model/types";
/** Beschriftung sichtbar (wie der Kern: `showLabel` und eine nicht leere Art). */
function labelShown(tr: Tree): boolean {
return tr.showLabel === true && (tr.species ?? "").trim() !== "";
}
/** Griff-Punkte: Stammmitte, Kronenrand, ggf. Beschriftung (Vorgabe: 1 mm Papier rechts neben dem Stamm). */
export function treeGripPoints(tr: Tree, scaleDenominator: number): Vec2[] {
const pts: Vec2[] = [tr.position, { x: tr.position.x + tr.crownDiameter / 2, y: tr.position.y }];
if (labelShown(tr)) {
const o = tr.labelOffset ?? { x: tr.trunkDiameter / 2 + scaleDenominator / 1000, y: 0 };
pts.push({ x: tr.position.x + o.x, y: tr.position.y + o.y });
}
return pts;
}
/** Änderung beim Ziehen des Griffs `index` an `pt`. */
export function treeGripPatch(tr: Tree, index: number, pt: Vec2): Partial<Tree> {
if (index === 0) return { position: { x: pt.x, y: pt.y } };
if (index === 1) return { crownDiameter: Math.max(0.2, 2 * Math.hypot(pt.x - tr.position.x, pt.y - tr.position.y)) };
return { labelOffset: { x: pt.x - tr.position.x, y: pt.y - tr.position.y } };
}
+5 -9
View File
@@ -40,6 +40,7 @@ import type { CommandEngine } from "../commands/engine";
import { single } from "./selection";
import { getState as storeState } from "./appStore";
import { patchItem } from "../core";
import { treeGripPatch, treeGripPoints } from "./treeGrips";
import type { Selection } from "./selection";
export interface GripEditFieldsState {
@@ -170,10 +171,8 @@ export function useGripEditing(opts: {
// [149] Baum: Griff in der Stammmitte (verschieben) und am Kronenrand (Kronendurchmesser).
const selectedTreeId = single(selection, "trees");
const selectedTree = selectedTreeId ? (project.trees ?? []).find((x) => x.id === selectedTreeId) : undefined;
const treeGrips = (tr: NonNullable<typeof selectedTree>): Vec2[] => [
tr.position,
{ x: tr.position.x + tr.crownDiameter / 2, y: tr.position.y },
];
// Mit gezeigter Beschriftung dazu ihr Griff (`labelOffset`, s. `treeGrips.ts`).
const treeGrips = (tr: NonNullable<typeof selectedTree>): Vec2[] => treeGripPoints(tr, scaleDenominator);
const selectedRoomId = single(selection, "rooms");
const selectedCeilingId = single(selection, "ceilings");
const selectedDrawingId = single(selection, "drawings");
@@ -852,14 +851,11 @@ export function useGripEditing(opts: {
else if (st) moveStairBy(st.id, sub(pt, stairMoveHandle(st)));
return;
}
// [149] Baum: Stammmitte bzw. Kronenrand live, ein Undo-Schritt je Zug (gleicher coalesceKey).
// [149] Baum: Stammmitte, Kronenrand bzw. Beschriftung live, ein Undo-Schritt je Zug (gleicher coalesceKey).
if (selectedTree) {
const tr = selectedTree;
if (tr.locked) return;
const patch =
index === 0
? { position: { x: pt.x, y: pt.y } }
: { crownDiameter: Math.max(0.2, 2 * Math.hypot(pt.x - tr.position.x, pt.y - tr.position.y)) };
const patch = treeGripPatch(tr, index, pt);
storeState().applyChange((p) => patchItem(p, "trees", tr.id, patch), `treeGrip:${tr.id}:${index}`);
return;
}