diff --git a/src/components/Brick.jsx b/src/components/Brick.jsx index 1adcba9..7e2a1b7 100644 --- a/src/components/Brick.jsx +++ b/src/components/Brick.jsx @@ -5,6 +5,7 @@ import { base, createGeometry, knobSize, + outlineWidth, } from "../utils"; import { Vector3, Box3, BackSide } from "three"; import { useSelect } from "@react-three/drei"; @@ -15,17 +16,10 @@ export const Brick = ({ dimensions = { x: 1, z: 1 }, rotation = 0, translation = { x: 0, z: 0 }, - // onClick = () => {}, bricksBoundBox = { current: [] }, uID = "", mouseMove = () => {}, }) => { - // const [{}, set] = useControls(() => ({ - // Delete: button((get) => { - // deleteSelectedBrick(); - // }), - // })); - const brickRef = useRef(); const { height, width, depth } = getMeasurementsFromDimensions(dimensions); @@ -36,11 +30,11 @@ export const Brick = ({ const outlineGeometry = useMemo(() => { return createGeometry({ - width: width + 2.6, - height: height + 2.6, - depth: depth + 2.6, + width: width + outlineWidth * 2, + height: height + outlineWidth * 2, + depth: depth + outlineWidth * 2, dimensions, - knobDim: knobSize + 1.2, + knobDim: knobSize + outlineWidth, }); }, [width, height, depth, dimensions]); @@ -82,20 +76,21 @@ export const Brick = ({ }; }, [uID, bricksBoundBox]); - const compansateX = - dimensions.x % 2 === 0 ? dimensions.x / 2 : (dimensions.x - 1) / 2; - const compansateZ = - dimensions.z % 2 === 0 ? dimensions.z / 2 : (dimensions.z - 1) / 2; + const compansate = { + x: dimensions.x % 2 === 0 ? dimensions.x / 2 : (dimensions.x - 1) / 2, + z: dimensions.z % 2 === 0 ? dimensions.z / 2 : (dimensions.z - 1) / 2, + }; - const offsetX = - Math.sign(translation.x) < 0 - ? Math.max(translation.x, -compansateX) - : Math.min(translation.x, compansateX); - - const offsetZ = - Math.sign(translation.z) < 0 - ? Math.max(translation.z, -compansateZ) - : Math.min(translation.z, compansateZ); + const offset = { + x: + Math.sign(translation.x) < 0 + ? Math.max(translation.x, -compansate.x) + : Math.min(translation.x, compansate.x), + z: + Math.sign(translation.z) < 0 + ? Math.max(translation.z, -compansate.z) + : Math.min(translation.z, compansate.z), + }; const selected = useSelect().map((sel) => sel.userData.uID); const isSelected = !!selected.find((sel) => sel === uID); @@ -110,11 +105,18 @@ export const Brick = ({ diff --git a/src/components/BrickOutline.jsx b/src/components/BrickOutline.jsx new file mode 100644 index 0000000..52f3c5b --- /dev/null +++ b/src/components/BrickOutline.jsx @@ -0,0 +1,62 @@ +import { useSelect } from "@react-three/drei"; +import React, { useMemo } from "react"; + +const OutlineMesh = ({ meshesData }) => { + console.log(meshesData); + return <>; +}; + +const BrickOutline = () => { + const selected = useSelect().map((sel) => sel.userData); + + const selectedMeshes = useMemo(() => { + const meshesAccrodingToType = {}; + + for (let i = 0; i < selected.length; i++) { + const currentSelected = selected[i]; + meshesAccrodingToType[currentSelected.type] = meshesAccrodingToType[ + currentSelected.type + ] + ? [...meshesAccrodingToType[currentSelected.type], currentSelected] + : [currentSelected]; + } + + return meshesAccrodingToType; + }, [selected]); + + // const compansate = { + // x: dimensions.x % 2 === 0 ? dimensions.x / 2 : (dimensions.x - 1) / 2, + // z: dimensions.z % 2 === 0 ? dimensions.z / 2 : (dimensions.z - 1) / 2, + // }; + + // const offset = { + // x: + // Math.sign(translation.x) < 0 + // ? Math.max(translation.x, -compansate.x) + // : Math.min(translation.x, compansate.x), + // z: + // Math.sign(translation.z) < 0 + // ? Math.max(translation.z, -compansate.z) + // : Math.min(translation.z, compansate.z), + // }; + + return ( + <> + {/* + + */} + {Object.entries(selectedMeshes).map(([key, value]) => ( + + ))} + + ); +}; + +export default BrickOutline; diff --git a/src/components/DeleteBrick.jsx b/src/components/DeleteBrick.jsx new file mode 100644 index 0000000..cd3c9a1 --- /dev/null +++ b/src/components/DeleteBrick.jsx @@ -0,0 +1,13 @@ +import { useSelect } from "@react-three/drei"; +import React from "react"; +import { useDeleteShortcut } from "../utils"; + +const DeleteBrick = ({ setBricks }) => { + const selected = useSelect().map((sel) => sel.userData.uID); + + useDeleteShortcut(selected, setBricks); + + return <>; +}; + +export default DeleteBrick; diff --git a/src/components/Scene.jsx b/src/components/Scene.jsx index af36def..842c4e2 100644 --- a/src/components/Scene.jsx +++ b/src/components/Scene.jsx @@ -9,6 +9,8 @@ import { } from "../utils"; import { button, useControls } from "leva"; import { Select } from "@react-three/drei"; +import DeleteBrick from "./DeleteBrick"; +import BrickOutline from "./BrickOutline"; let t; @@ -205,6 +207,8 @@ export const Scene = () => { /> ); })} + + diff --git a/src/utils/constants.js b/src/utils/constants.js index a7c87e5..e7867d0 100644 --- a/src/utils/constants.js +++ b/src/utils/constants.js @@ -1,5 +1,6 @@ export const base = 25; export const knobSize = 7; +export const outlineWidth = 1.3; export const bricks = [ { x: 1, z: 1 }, diff --git a/src/utils/hooks.js b/src/utils/hooks.js index 1311e6e..0aeb969 100644 --- a/src/utils/hooks.js +++ b/src/utils/hooks.js @@ -43,3 +43,32 @@ export const useAnchorShorcuts = (anchorX, anchorZ, set) => { return null; }; + +export const useDeleteShortcut = (selected, setBricks) => { + const deleteSelectedBricks = () => { + console.log("DELETE"); + setBricks((bricks) => + bricks.filter((brick) => { + const selectedClone = [...selected]; + const uID = brick.uID; + let should = true; + for (let i = 0; i < selectedClone.length; i++) { + const selectedUID = selectedClone[i]; + if (uID === selectedUID) { + should = false; + selectedClone.splice(i, 1); + } + } + return should; + }) + ); + }; + + useKeyboardShortcut(["Delete"], deleteSelectedBricks, { + overrideSystem: true, + ignoreInputFields: false, + repeatOnHold: false, + }); + + return null; +};