Spaces:
Running
Running
File size: 9,838 Bytes
16ab111 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
import React, { useEffect, useRef, useState, useMemo } from "react";
import { Play, Pause } from "lucide-react";
import { cn } from "@/lib/utils";
import URDFManipulator from "urdf-loader/src/urdf-manipulator-element.js";
import { useUrdf } from "@/hooks/useUrdf";
import {
createUrdfViewer,
setupMeshLoader,
setupJointHighlighting,
setupModelLoading,
} from "@/lib/urdfViewerHelpers";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
// Register the URDFManipulator as a custom element if it hasn't been already
if (typeof window !== "undefined" && !customElements.get("urdf-viewer")) {
customElements.define("urdf-viewer", URDFManipulator);
}
// Extend the interface for the URDF viewer element to include background property
interface URDFViewerElement extends HTMLElement {
background?: string;
setJointValue?: (jointName: string, value: number) => void;
}
interface URDFViewerProps {
hasAnimation?: boolean;
headerHeight?: number;
}
const URDFViewer: React.FC<URDFViewerProps> = ({
hasAnimation = false,
headerHeight = 0,
}) => {
const { theme } = useTheme();
const isDarkMode = theme === "dark";
const containerRef = useRef<HTMLDivElement>(null);
const [highlightedJoint, setHighlightedJoint] = useState<string | null>(null);
const {
registerUrdfProcessor,
alternativeUrdfModels,
isDefaultModel,
currentAnimationConfig,
} = useUrdf();
// Add state for animation control
const [isAnimating, setIsAnimating] = useState<boolean>(isDefaultModel);
const [showAnimationControl, setShowAnimationControl] = useState<boolean>(
isDefaultModel || hasAnimation
);
const cleanupAnimationRef = useRef<(() => void) | null>(null);
const viewerRef = useRef<URDFViewerElement | null>(null);
const hasInitializedRef = useRef<boolean>(false);
// Add state for custom URDF path
const [customUrdfPath, setCustomUrdfPath] = useState<string | null>(null);
const [urlModifierFunc, setUrlModifierFunc] = useState<
((url: string) => string) | null
>(null);
const packageRef = useRef<string>("");
// Implement UrdfProcessor interface for drag and drop
const urdfProcessor = useMemo(
() => ({
loadUrdf: (urdfPath: string) => {
setCustomUrdfPath(urdfPath);
},
setUrlModifierFunc: (func: (url: string) => string) => {
setUrlModifierFunc(() => func);
},
getPackage: () => {
return packageRef.current;
},
}),
[]
);
// Register the URDF processor with the global drag and drop context
useEffect(() => {
registerUrdfProcessor(urdfProcessor);
}, [registerUrdfProcessor, urdfProcessor]);
// Effect to update animation control visibility based on props
useEffect(() => {
// Show animation controls for default model or when hasAnimation is true
setShowAnimationControl(isDefaultModel || hasAnimation);
}, [isDefaultModel, hasAnimation]);
// Toggle animation function
const toggleAnimation = () => {
setIsAnimating((prev) => !prev);
};
// Main effect to create and setup the viewer only once
useEffect(() => {
if (!containerRef.current) return;
// Create and configure the URDF viewer element
const viewer = createUrdfViewer(containerRef.current, isDarkMode);
viewerRef.current = viewer; // Store reference to the viewer
// Setup mesh loading function
setupMeshLoader(viewer, urlModifierFunc);
// Determine which URDF to load
const urdfPath = isDefaultModel
? "/urdf/T12/urdf/T12.URDF"
: customUrdfPath || "";
// Setup model loading if a path is available
let cleanupModelLoading = () => {};
if (urdfPath) {
cleanupModelLoading = setupModelLoading(
viewer,
urdfPath,
packageRef.current,
setCustomUrdfPath,
alternativeUrdfModels
);
}
// Setup joint highlighting
const cleanupJointHighlighting = setupJointHighlighting(
viewer,
setHighlightedJoint
);
// Setup animation event handler for the default model or when hasAnimation is true
const onModelProcessed = () => {
hasInitializedRef.current = true;
if (isAnimating && "setJointValue" in viewer) {
// Clear any existing animation
if (cleanupAnimationRef.current) {
cleanupAnimationRef.current();
cleanupAnimationRef.current = null;
}
// Use the appropriate animation based on whether it's the default model or hasAnimation
if (isDefaultModel) {
// Start the hexapod animation when it's the default model
cleanupAnimationRef.current = animateHexapodRobot(
viewer as import("@/lib/urdfAnimationHelpers").URDFViewerElement
);
} else if (hasAnimation) {
// Use the custom animation config from the context if available, otherwise fall back to cassieWalkingConfig
const animationConfig = currentAnimationConfig || cassieWalkingConfig;
// Start the animation using the selected configuration
cleanupAnimationRef.current = animateRobot(
viewer as import("@/lib/urdfAnimationHelpers").URDFViewerElement,
animationConfig
);
}
}
};
viewer.addEventListener("urdf-processed", onModelProcessed);
// Return cleanup function
return () => {
if (cleanupAnimationRef.current) {
cleanupAnimationRef.current();
cleanupAnimationRef.current = null;
}
hasInitializedRef.current = false;
cleanupJointHighlighting();
cleanupModelLoading();
viewer.removeEventListener("urdf-processed", onModelProcessed);
};
}, [
isDefaultModel,
customUrdfPath,
urlModifierFunc,
hasAnimation,
currentAnimationConfig,
]);
// Separate effect to handle theme changes without recreating the viewer
useEffect(() => {
if (!viewerRef.current) return;
// Update only the visual aspects based on theme
if (viewerRef.current.background !== undefined) {
if (isDarkMode) {
viewerRef.current.background = "#1f2937"; // Dark background
} else {
viewerRef.current.background = "#e0e7ff"; // Light background
}
}
}, [isDarkMode]);
// Effect to handle animation toggling after initial load
useEffect(() => {
if (!viewerRef.current || !hasInitializedRef.current) return;
// Only manage animation if viewer has setJointValue (required for animation)
if (!("setJointValue" in viewerRef.current)) return;
if (isAnimating) {
if (!cleanupAnimationRef.current) {
// Only start animation if it's not already running
if (isDefaultModel) {
cleanupAnimationRef.current = animateHexapodRobot(
viewerRef.current as import("@/lib/urdfAnimationHelpers").URDFViewerElement
);
} else if (hasAnimation) {
// Use the custom animation config from the context if available, otherwise fall back to cassieWalkingConfig
const animationConfig = currentAnimationConfig;
// Start animation using the selected configuration
cleanupAnimationRef.current = animateRobot(
viewerRef.current as import("@/lib/urdfAnimationHelpers").URDFViewerElement,
animationConfig
);
}
}
} else {
if (cleanupAnimationRef.current) {
// Just cancel the animation frame without resetting anything
cleanupAnimationRef.current();
cleanupAnimationRef.current = null;
}
}
}, [isAnimating, isDefaultModel, hasAnimation, currentAnimationConfig]);
return (
<div
className={cn(
"w-full h-full transition-all duration-300 ease-in-out relative",
isDarkMode
? "bg-gradient-to-br from-gray-900 to-gray-800"
: "bg-gradient-to-br from-blue-50 to-indigo-50"
)}
>
<div ref={containerRef} className="w-full h-full" />
{/* Control buttons container in top right, with vertical padding for header */}
<div
className="absolute right-4 flex items-center space-x-2 z-10"
style={{ top: `${headerHeight + 16}px` }}
>
{/* ModeToggle button */}
<ModeToggle />
{/* Animation control button with icon - show for both default model and when animation is available */}
{showAnimationControl && (
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<button
onClick={toggleAnimation}
className={cn(
"p-2.5 rounded-full shadow-md transition-all duration-300",
isDarkMode
? "bg-gray-700 hover:bg-gray-600 text-white"
: "bg-white/80 hover:bg-white text-gray-800"
)}
aria-label={
isAnimating ? "Stop Animation" : "Start Animation"
}
>
{isAnimating ? (
<Pause className="h-5 w-5" />
) : (
<Play className="h-5 w-5" />
)}
</button>
</TooltipTrigger>
<TooltipContent side="bottom">
<p className="font-mono text-xs">
{isAnimating ? "Stop Animation" : "Start Animation"}
</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
)}
</div>
{/* Joint highlight indicator */}
{highlightedJoint && (
<div className="absolute bottom-4 right-4 bg-black/70 text-white px-3 py-2 rounded-md text-sm font-mono z-10">
Joint: {highlightedJoint}
</div>
)}
</div>
);
};
export default URDFViewer;
|