three.cad/src/react/navBar.jsx

111 lines
3.5 KiB
React
Raw Normal View History

2021-04-07 12:21:09 +08:00
2021-04-08 06:50:53 +08:00
import React, { useEffect, useReducer } from 'react';
2021-04-07 12:21:09 +08:00
import { useDispatch, useSelector } from 'react-redux'
import { FaCube, FaEdit } from 'react-icons/fa'
2021-04-14 18:19:51 +08:00
import { BsBoxArrowUp } from 'react-icons/bs'
import { MdDone, MdSave, MdFolder } from 'react-icons/md'
2021-04-07 12:21:09 +08:00
import * as Icon from "./icons";
2021-04-15 16:36:54 +08:00
export const NavBar = ({setDialog}) => {
2021-04-07 12:21:09 +08:00
const dispatch = useDispatch()
2021-04-15 06:46:30 +08:00
const treeEntriesById = useSelector(state => state.treeEntries.byId)
2021-04-11 15:57:39 +08:00
const activeSketchId = useSelector(state => state.treeEntries.activeSketchId)
2021-04-07 12:21:09 +08:00
2021-04-13 09:37:16 +08:00
const boolOp = (code) => {
if (sc.selected.length != 2 || !sc.selected.every(e => e.userData.type == 'mesh')) return
const [m1, m2] = sc.selected
2021-04-15 13:32:20 +08:00
const mesh = sc.boolOp(m1, m2, code)
2021-04-13 09:37:16 +08:00
dispatch({ type: 'rx-boolean', mesh, deps: [m1.name, m2.name] })
sc.render()
forceUpdate()
}
const extrude = () => {
2021-04-15 16:36:54 +08:00
setDialog('dd')
// if (sc.activeSketch) {
// sc.extrude(sc.activeSketch)
// } else if (sc.selected.length === 1 && sc.selected[0].userData.type == 'sketch') {
// sc.extrude(treeEntriesById[sc.selected[0].name])
// } else {
// console.log('invalid selection')
// }
2021-04-15 06:46:30 +08:00
}
const addSketch = () => {
sc.addSketch()
2021-04-15 16:36:54 +08:00
console.log(!!sc.activeSketch, 'state')
2021-04-15 06:46:30 +08:00
forceUpdate()
2021-04-13 09:37:16 +08:00
}
2021-04-07 12:21:09 +08:00
useEffect(() => {
if (!activeSketchId) {
sc.canvas.addEventListener('pointermove', sc.onHover)
sc.canvas.addEventListener('pointerdown', sc.onPick)
return () => {
sc.canvas.removeEventListener('pointermove', sc.onHover)
sc.canvas.removeEventListener('pointerdown', sc.onPick)
}
}
}, [activeSketchId])
2021-04-15 16:36:54 +08:00
// useEffect(() => {
// console.log(treeEntriesById)
// }, [treeEntriesById])
2021-04-15 06:46:30 +08:00
2021-04-07 12:21:09 +08:00
const btnz = [
2021-04-14 04:39:33 +08:00
[MdDone, () => {
2021-04-15 06:46:30 +08:00
// treeEntriesById[activeSketchId].deactivate()
2021-04-14 04:39:33 +08:00
// dispatch({ type: 'update-descendents', sketch})
2021-04-15 06:46:30 +08:00
sc.activeSketch.deactivate()
2021-04-14 04:39:33 +08:00
sc.render()
forceUpdate()
// sc.activeDim = this.activeSketch.obj3d.children[1].children
}, 'Finish'],
2021-04-14 07:49:56 +08:00
[Icon.Extrude, extrude, 'Extrude [e]'],
2021-04-15 06:46:30 +08:00
[Icon.Dimension, () => sc.activeSketch.command('d'), 'Dimension [d]'],
[Icon.Line, () => sc.activeSketch.command('l'), 'Line [l]'],
[Icon.Arc, () => sc.activeSketch.command('a'), 'Arc [a]'],
[Icon.Coincident, () => sc.activeSketch.command('c'), 'Coincident [c]'],
[Icon.Vertical, () => sc.activeSketch.command('v'), 'Vertical [v]'],
[Icon.Horizontal, () => sc.activeSketch.command('h'), 'Horizontal [h]'],
[Icon.Tangent, () => sc.activeSketch.command('t'), 'Tangent [t]'],
2021-04-14 04:39:33 +08:00
]
const btnz2 = [
2021-04-15 06:46:30 +08:00
[FaEdit, addSketch, 'Sketch [s]']
2021-04-14 04:39:33 +08:00
,
2021-04-14 07:49:56 +08:00
[Icon.Extrude, extrude, 'Extrude [e]'],
2021-04-14 04:39:33 +08:00
[Icon.Union, () => boolOp('u'), 'Union'],
[Icon.Subtract, () => boolOp('s'), 'Subtract'],
[Icon.Intersect, () => boolOp('i'), 'Intersect'],
2021-04-14 07:49:56 +08:00
[MdSave, () => boolOp('i'), 'Save [ctrl+s]'],
[MdFolder, () => boolOp('i'), 'Load'],
[Icon.Stl, () => boolOp('i'), 'Export STL'],
2021-04-07 12:21:09 +08:00
]
const [_, forceUpdate] = useReducer(x => x + 1, 0);
2021-04-11 07:16:08 +08:00
return <div className='topNav flex justify-center items-center bg-gray-700'>
2021-04-07 12:21:09 +08:00
{
2021-04-15 16:36:54 +08:00
activeSketchId ?
2021-04-14 04:39:33 +08:00
btnz.map(([Icon, fcn, txt, shortcut], idx) => (
<Icon className="btn w-auto h-full p-3.5" tooltip={txt}
onClick={fcn} key={idx}
/>
))
:
btnz2.map(([Icon, fcn, txt, shortcut], idx) => (
<Icon className="btn w-auto h-full p-3.5" tooltip={txt}
onClick={fcn} key={idx}
/>
))
2021-04-07 12:21:09 +08:00
}
</div>
}