|
| 1 | +import React from "react"; |
| 2 | +import { Box, SxProps, Theme, Divider } from "@mui/material"; |
| 3 | +import { useFiber } from "@metacell/geppetto-meta-ui/3d-canvas/Canvas3D"; |
| 4 | +import { RootState } from "@react-three/fiber"; |
| 5 | + |
| 6 | +const toolbarBaseStyles: SxProps<Theme> = { |
| 7 | + display: "flex", |
| 8 | + flexDirection: "column", |
| 9 | + alignItems: "center", |
| 10 | + backgroundColor: "#f0f0f0", |
| 11 | +}; |
| 12 | + |
| 13 | +const toolbarButtonBaseStyles: React.CSSProperties = { |
| 14 | + padding: "0.75rem", |
| 15 | + fontSize: "1.25rem", |
| 16 | + color: "#666", |
| 17 | + display: "flex", |
| 18 | + alignItems: "center", |
| 19 | + justifyContent: "center", |
| 20 | + cursor: "pointer", |
| 21 | +}; |
| 22 | + |
| 23 | +const CanvasIdContext = React.createContext<string | undefined>(undefined); |
| 24 | +const useCanvasId = () => React.useContext(CanvasIdContext); |
| 25 | + |
| 26 | +export const Toolbar3D = ({ |
| 27 | + children, |
| 28 | + sx, |
| 29 | + canvasId, |
| 30 | +}: { |
| 31 | + children: React.ReactNode; |
| 32 | + sx?: SxProps<Theme>; |
| 33 | + canvasId?: string; |
| 34 | +}) => { |
| 35 | + return ( |
| 36 | + <CanvasIdContext.Provider value={canvasId}> |
| 37 | + <Box sx={{ ...toolbarBaseStyles, ...sx }}>{children}</Box> |
| 38 | + </CanvasIdContext.Provider> |
| 39 | + ); |
| 40 | +}; |
| 41 | + |
| 42 | +export const Toolbar3DButton = ({ |
| 43 | + icon, |
| 44 | + tooltip, |
| 45 | + onClick, |
| 46 | + style, |
| 47 | +}: { |
| 48 | + icon: React.ReactNode; |
| 49 | + tooltip: string; |
| 50 | + onClick: (fiber: RootState) => void; |
| 51 | + style?: React.CSSProperties; |
| 52 | +}) => { |
| 53 | + const canvasId = useCanvasId(); |
| 54 | + const fiber = useFiber(canvasId ?? "default"); |
| 55 | + |
| 56 | + return ( |
| 57 | + <div |
| 58 | + style={{ ...toolbarButtonBaseStyles, ...style }} |
| 59 | + title={tooltip} |
| 60 | + onClick={() => fiber && onClick(fiber)} |
| 61 | + > |
| 62 | + {icon} |
| 63 | + </div> |
| 64 | + ); |
| 65 | +}; |
| 66 | + |
| 67 | +export const Toolbar3DSeparator = ({ |
| 68 | + style, |
| 69 | + variant = "horizontal", |
| 70 | +}: { |
| 71 | + style?: React.CSSProperties; |
| 72 | + variant?: "horizontal" | "vertical"; |
| 73 | +}) => { |
| 74 | + return ( |
| 75 | + <Divider |
| 76 | + style={{ ...style }} |
| 77 | + orientation={variant === "horizontal" ? "horizontal" : "vertical"} |
| 78 | + /> |
| 79 | + ); |
| 80 | +}; |
0 commit comments