three.cad/src/react/navBar.jsx

96 lines
2.9 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'
2021-04-16 08:05:40 +08:00
import { FaEdit } from 'react-icons/fa'
2021-04-14 18:19:51 +08:00
import { MdDone, MdSave, MdFolder } from 'react-icons/md'
2021-04-07 12:21:09 +08:00
import * as Icon from "./icons";
2021-04-16 08:05:40 +08:00
export const NavBar = ({ setDialog }) => {
2021-04-07 12:21:09 +08:00
const dispatch = useDispatch()
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-17 07:03:30 +08:00
sc.boolOp(m1, m2, code)
2021-04-13 09:37:16 +08:00
sc.render()
forceUpdate()
}
2021-04-16 08:05:40 +08:00
const extrude = () => { setDialog(true) }
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
sc.activeSketch.deactivate()
2021-04-16 08:05:40 +08:00
// dispatch({ type: 'update-descendents', sketch})
2021-04-14 04:39:33 +08:00
sc.render()
forceUpdate()
}, '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-17 07:03:30 +08:00
[FaEdit, addSketch, 'Sketch [s]'],
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>
}