/* Canonical floor-plan wall/transition renderer, shared with the existing editor. */ (() => { 'use strict'; function normalizeTransitionType(type) { return (type || "").toLowerCase().replace(/[^a-z0-9]/g, ""); } function isStairTransition(type) { return normalizeTransitionType(type) === "stairsup" || normalizeTransitionType(type) === "stairsdown"; } function isLadderTransition(type) { return ["ladder", "ladders"].includes(normalizeTransitionType(type)); } function isInsideLocationTransition(type) { return isStairTransition(type) || isLadderTransition(type); } function isUnrestrictedTransition(type) { return ["hiddenpassage", "openpassage", "secretpassage"].includes(normalizeTransitionType(type)); } function isSecretRouteTransition(type) { return ["hiddenpassage", "secretpassage"].includes(normalizeTransitionType(type)); } function isVisibleEntranceTransition(type) { return [ "door", "doubledoor", "frenchdoor", "frenchdoors", "slidingdoor", "slidingdoors", "exteriordoor", "archway", "openpassage", "stairsup", "stairsdown" ].includes(normalizeTransitionType(type)); } function create(editor,{readBlock=block=>({x:Number(block.style.getPropertyValue('--x')),y:Number(block.style.getPropertyValue('--y')),w:Number(block.style.getPropertyValue('--w')),h:Number(block.style.getPropertyValue('--h'))})}={}) { function subtractRange(segments, start, end) { return segments.flatMap(segment => { const clippedStart = Math.max(segment.start, start); const clippedEnd = Math.min(segment.end, end); if (clippedStart >= clippedEnd) return [segment]; return [ { start: segment.start, end: clippedStart }, { start: clippedEnd, end: segment.end } ].filter(next => next.end > next.start); }); } function renderWallSegment(block, side, start, end, total) { if (end <= start || total <= 0) return; const segment = document.createElement("span"); segment.dataset.wallSegment = side; segment.className = `floor-plan-wall-segment floor-plan-wall-segment--${side}`; segment.style.setProperty("--wall-start", `${(start / total) * 100}%`); segment.style.setProperty("--wall-length", `${((end - start) / total) * 100}%`); block.append(segment); } function visibleEdgeSegments(item, visibleBlocks, side) { const axisStart = side === "east" || side === "west" ? item.y : item.x; const axisLength = side === "east" || side === "west" ? item.h : item.w; let segments = [{ start: 0, end: axisLength }]; visibleBlocks.forEach(other => { if (item === other || !item.block.dataset.locationId || item.block.dataset.locationId !== other.block.dataset.locationId) return; let touches = false; let overlapStart = 0; let overlapEnd = 0; if (side === "east" && item.x + item.w === other.x) { touches = true; overlapStart = Math.max(item.y, other.y) - axisStart; overlapEnd = Math.min(item.y + item.h, other.y + other.h) - axisStart; } else if (side === "west" && other.x + other.w === item.x) { touches = true; overlapStart = Math.max(item.y, other.y) - axisStart; overlapEnd = Math.min(item.y + item.h, other.y + other.h) - axisStart; } else if (side === "south" && item.y + item.h === other.y) { touches = true; overlapStart = Math.max(item.x, other.x) - axisStart; overlapEnd = Math.min(item.x + item.w, other.x + other.w) - axisStart; } else if (side === "north" && other.y + other.h === item.y) { touches = true; overlapStart = Math.max(item.x, other.x) - axisStart; overlapEnd = Math.min(item.x + item.w, other.x + other.w) - axisStart; } if (touches && overlapStart < overlapEnd) { segments = subtractRange(segments, overlapStart, overlapEnd); } }); return segments; } function refreshWallSegments(visibleBlocks) { visibleBlocks.forEach(item => { item.block.querySelectorAll("[data-wall-segment]").forEach(segment => segment.remove()); ["north", "east", "south", "west"].forEach(side => { const total = side === "east" || side === "west" ? item.h : item.w; visibleEdgeSegments(item, visibleBlocks, side).forEach(segment => { renderWallSegment(item.block, side, segment.start, segment.end, total); }); }); }); } function refreshSharedWalls(activePanel) { const visibleBlocks = [...activePanel.querySelectorAll("[data-block]")].map(block => ({ block, ...readBlock(block) })); refreshWallSegments(visibleBlocks); return visibleBlocks; } function locationCenter(blocksForLocation) { if (blocksForLocation.length === 0) return null; const minX = Math.min(...blocksForLocation.map(item => item.x)); const minY = Math.min(...blocksForLocation.map(item => item.y)); const maxX = Math.max(...blocksForLocation.map(item => item.x + item.w)); const maxY = Math.max(...blocksForLocation.map(item => item.y + item.h)); return { x: (minX + maxX) / 2, y: (minY + maxY) / 2 }; } function refreshHiddenLocationStyles(visibleBlocks) { const visibleEntranceLocationIds = new Set(); editor.querySelectorAll("[data-transition-marker]").forEach(marker => { if (!isVisibleEntranceTransition(marker.dataset.transitionKind)) return; if (marker.dataset.fromLocationId) visibleEntranceLocationIds.add(marker.dataset.fromLocationId); if (marker.dataset.toLocationId) visibleEntranceLocationIds.add(marker.dataset.toLocationId); }); visibleBlocks.forEach(item => { const locationId = item.block.dataset.locationId; const isHidden = !!locationId && !visibleEntranceLocationIds.has(locationId); item.block.classList.toggle("is-hidden-location", isHidden); const baseTitle = item.block.dataset.locationTitle || item.block.dataset.locationPath || item.block.dataset.locationName || ""; item.block.title = isHidden ? `${baseTitle}${baseTitle ? "\n" : ""}Hidden location: no visible entrance` : baseTitle; }); } function renderSecretRoute(activePanel, marker, fromCenter, toCenter) { if (!fromCenter || !toCenter) return; const dx = toCenter.x - fromCenter.x; const dy = toCenter.y - fromCenter.y; const length = Math.sqrt(dx * dx + dy * dy); if (length <= 0) return; const route = document.createElement("span"); route.className = "floor-plan-secret-route"; route.dataset.secretRoute = marker.dataset.transitionMarker || ""; route.style.setProperty("--route-x", fromCenter.x); route.style.setProperty("--route-y", fromCenter.y); route.style.setProperty("--route-length", length); route.style.setProperty("--route-angle", `${Math.atan2(dy, dx)}rad`); route.title = marker.title || ""; route.setAttribute("aria-hidden", "true"); activePanel.querySelector("[data-floor-grid]")?.append(route); } function sharedTransitionEdge(fromBlocks, toBlocks) { let best = null; fromBlocks.forEach(from => { toBlocks.forEach(to => { if (from.x + from.w === to.x || to.x + to.w === from.x) { const start = Math.max(from.y, to.y); const end = Math.min(from.y + from.h, to.y + to.h); const length = end - start; if (length > 0 && (!best || length > best.length)) { best = { length, orientation: "vertical", start, end, x: from.x + from.w === to.x ? to.x : from.x, y: start + length / 2 }; } } if (from.y + from.h === to.y || to.y + to.h === from.y) { const start = Math.max(from.x, to.x); const end = Math.min(from.x + from.w, to.x + to.w); const length = end - start; if (length > 0 && (!best || length > best.length)) { best = { length, orientation: "horizontal", start, end, x: start + length / 2, y: from.y + from.h === to.y ? to.y : from.y }; } } }); }); return best; } function positionWithinLocation(blocksForLocation, position) { const center = locationCenter(blocksForLocation); if (!center) return null; const minX = Math.min(...blocksForLocation.map(item => item.x)); const minY = Math.min(...blocksForLocation.map(item => item.y)); const maxX = Math.max(...blocksForLocation.map(item => item.x + item.w)); const maxY = Math.max(...blocksForLocation.map(item => item.y + item.h)); const inset = 0.35; const normalized = (position || "Centre").toLowerCase().replace(/\s+/g, ""); const x = normalized.includes("west") ? minX + inset : normalized.includes("east") ? maxX - inset : center.x; const y = normalized.includes("north") ? minY + inset : normalized.includes("south") ? maxY - inset : center.y; return { x, y }; } function positionTransitions(activePanel, visibleBlocks) { const markerPositions = []; activePanel.querySelectorAll("[data-secret-route]").forEach(route => route.remove()); activePanel.querySelectorAll("[data-transition-marker]").forEach(marker => { const fromBlocks = visibleBlocks.filter(item => item.block.dataset.locationId === marker.dataset.fromLocationId); const toBlocks = visibleBlocks.filter(item => item.block.dataset.locationId === marker.dataset.toLocationId); const fromCenter = locationCenter(fromBlocks); const toCenter = locationCenter(toBlocks); if (isSecretRouteTransition(marker.dataset.transitionKind)) { marker.hidden = true; if (fromCenter && toCenter) { renderSecretRoute(activePanel, marker, fromCenter, toCenter); } return; } if (fromBlocks.length === 0 || (!isInsideLocationTransition(marker.dataset.transitionKind) && toBlocks.length === 0)) { marker.hidden = true; return; } const insidePlacement = isInsideLocationTransition(marker.dataset.transitionKind) ? positionWithinLocation(fromBlocks, marker.dataset.positionWithinLocation) : null; const sharedEdge = insidePlacement ? null : sharedTransitionEdge(fromBlocks, toBlocks); marker.hidden = false; marker.classList.toggle("is-shared-edge", !!sharedEdge); marker.classList.toggle("is-inside-location", !!insidePlacement); marker.classList.toggle("is-horizontal", sharedEdge?.orientation === "horizontal"); marker.classList.toggle("is-vertical", sharedEdge?.orientation === "vertical"); const pairKey = [marker.dataset.fromLocationId, marker.dataset.toLocationId].sort().join("-"); const key = insidePlacement ? `${pairKey}:inside:${normalizeTransitionType(marker.dataset.transitionKind)}:${marker.dataset.positionWithinLocation || "Centre"}:${insidePlacement.x}:${insidePlacement.y}` : sharedEdge ? `${pairKey}:${sharedEdge.orientation}:${sharedEdge.x}:${sharedEdge.y}:${sharedEdge.start}:${sharedEdge.end}` : `${pairKey}:fallback`; markerPositions.push({ marker, insidePlacement, sharedEdge, fromCenter, toCenter, key }); }); const groups = markerPositions.reduce((map, item) => { if (!map.has(item.key)) map.set(item.key, []); map.get(item.key).push(item); return map; }, new Map()); groups.forEach(group => { group.forEach((item, index) => { const fraction = (index + 1) / (group.length + 1); let x; let y; if (item.insidePlacement) { x = item.insidePlacement.x; y = item.insidePlacement.y; } else if (item.sharedEdge?.orientation === "vertical") { x = item.sharedEdge.x; y = item.sharedEdge.start + item.sharedEdge.length * fraction; } else if (item.sharedEdge?.orientation === "horizontal") { x = item.sharedEdge.start + item.sharedEdge.length * fraction; y = item.sharedEdge.y; } else { x = item.fromCenter.x + (item.toCenter.x - item.fromCenter.x) * fraction; y = item.fromCenter.y + (item.toCenter.y - item.fromCenter.y) * fraction; } item.marker.style.setProperty("--transition-x", x); item.marker.style.setProperty("--transition-y", y); }); }); } return {refreshSharedWalls,refreshHiddenLocationStyles,positionTransitions,locationCenter}; } window.PlotDirectorFloorPlanRenderer={create,normalizeTransitionType,isStairTransition,isLadderTransition,isInsideLocationTransition,isUnrestrictedTransition,isSecretRouteTransition,isVisibleEntranceTransition}; })();