rewrite selection system
This commit is contained in:
@@ -15,3 +15,6 @@ export type { AnyNodeType, AnyNodeId } from "./types";
|
||||
|
||||
// Camera
|
||||
export { CameraSchema } from "./camera";
|
||||
|
||||
// Zones
|
||||
export type { Zone, ZonePolygon } from "./zone";
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import dedent from "dedent";
|
||||
import { z } from "zod";
|
||||
import { objectId } from "./base";
|
||||
import { LevelNode } from "./nodes/level";
|
||||
|
||||
// Polygon boundary for zone area - array of [x, z] coordinates
|
||||
export const ZonePolygon = z.array(z.tuple([z.number(), z.number()]));
|
||||
|
||||
export const ZoneSchema = z
|
||||
.object({
|
||||
id: objectId("zone"),
|
||||
object: z.literal("zone").default("zone"),
|
||||
levelId: LevelNode.shape.id, // Required - must be attached to a level
|
||||
name: z.string(),
|
||||
// Polygon boundary - array of [x, z] coordinates defining the zone
|
||||
polygon: ZonePolygon,
|
||||
// Visual styling
|
||||
color: z.string().default("#3b82f6"), // Default blue
|
||||
metadata: z.json().optional().default({}),
|
||||
})
|
||||
.describe(
|
||||
dedent`
|
||||
Zone schema - a polygon zone attached to a level
|
||||
- object: "zone"
|
||||
- id: zone id
|
||||
- levelId: level this zone is attached to
|
||||
- name: zone name
|
||||
- polygon: array of [x, z] points defining the zone boundary
|
||||
- color: hex color for visual styling
|
||||
- metadata: zone metadata (optional)
|
||||
`,
|
||||
);
|
||||
|
||||
export type Zone = z.infer<typeof ZoneSchema>;
|
||||
export type ZonePolygon = z.infer<typeof ZonePolygon>;
|
||||
@@ -29,7 +29,7 @@ const Viewer: React.FC<ViewerProps> = ({ children }) => {
|
||||
return renderer;
|
||||
}}
|
||||
shadows
|
||||
camera={{ position: [3, 3, 3], fov: 50 }}
|
||||
camera={{ position: [50, 50, 50], fov: 50 }}
|
||||
>
|
||||
<color attach="background" args={["#ececec"]} />
|
||||
|
||||
|
||||
@@ -1,25 +1,60 @@
|
||||
"use client";
|
||||
|
||||
import { BuildingNode, LevelNode } from "@pascal-app/core";
|
||||
import { BuildingNode, ItemNode, LevelNode, Zone } from "@pascal-app/core";
|
||||
import { create } from "zustand";
|
||||
|
||||
type SelectionPath = {
|
||||
buildingId: BuildingNode["id"] | null;
|
||||
levelId: LevelNode["id"] | null;
|
||||
zoneId: Zone["id"] | null;
|
||||
selectedIds: ItemNode["id"][]; // For items/assets (multi-select)
|
||||
};
|
||||
|
||||
type ViewerState = {
|
||||
selection: SelectionPath;
|
||||
levelMode: "stacked" | "exploded" | "solo" | "manual";
|
||||
|
||||
// Actions
|
||||
setLevelMode: (mode: "stacked" | "exploded" | "solo" | "manual") => void;
|
||||
|
||||
currentBuildingId: BuildingNode["id"] | null;
|
||||
setCurrentBuildingId: (id: BuildingNode["id"] | null) => void;
|
||||
currentLevelId: LevelNode["id"] | null;
|
||||
setCurrentLevelId: (id: LevelNode["id"] | null) => void;
|
||||
// Smart selection update
|
||||
setSelection: (updates: Partial<SelectionPath>) => void;
|
||||
resetSelection: () => void;
|
||||
};
|
||||
|
||||
const useViewer = create<ViewerState>()((set, get) => ({
|
||||
selection: { buildingId: null, levelId: null, zoneId: null, selectedIds: [] },
|
||||
levelMode: "stacked",
|
||||
setLevelMode: (mode) => set({ levelMode: mode }),
|
||||
currentLevelId: null,
|
||||
setCurrentLevelId: (id) => set({ currentLevelId: id }),
|
||||
currentBuildingId: null,
|
||||
setCurrentBuildingId: (id) => set({ currentBuildingId: id }),
|
||||
|
||||
setSelection: (updates) =>
|
||||
set((state) => {
|
||||
const newSelection = { ...state.selection, ...updates };
|
||||
|
||||
// Hierarchy Guard: If we change a high-level parent, reset the children
|
||||
if (updates.buildingId !== undefined) {
|
||||
newSelection.levelId = null;
|
||||
newSelection.zoneId = null;
|
||||
newSelection.selectedIds = [];
|
||||
} else if (updates.levelId !== undefined) {
|
||||
newSelection.zoneId = null;
|
||||
newSelection.selectedIds = [];
|
||||
} else if (updates.zoneId !== undefined) {
|
||||
newSelection.selectedIds = [];
|
||||
}
|
||||
|
||||
return { selection: newSelection };
|
||||
}),
|
||||
|
||||
resetSelection: () =>
|
||||
set({
|
||||
selection: {
|
||||
buildingId: null,
|
||||
levelId: null,
|
||||
zoneId: null,
|
||||
selectedIds: [],
|
||||
},
|
||||
}),
|
||||
}));
|
||||
|
||||
export default useViewer;
|
||||
|
||||
Reference in New Issue
Block a user