master
howard 2021-04-14 15:46:30 -07:00
parent 9d85a61c6b
commit 33f1a20281
7 changed files with 80 additions and 154 deletions

View File

@ -194,8 +194,10 @@ export class Scene {
} }
clearSelection() { clearSelection() {
for (let x = 0; x < this.selected.length; x++) { for (let x = 0, obj; x < this.selected.length; x++) {
const obj = this.selected[x] obj = this.selected[x]
if (obj.userData.type == 'sketch') continue
if (obj.userData.type == 'plane') { if (obj.userData.type == 'plane') {
obj.material.opacity = 0.05 obj.material.opacity = 0.05
obj.children[0].material.color.set(color['planeBorder']) obj.children[0].material.color.set(color['planeBorder'])
@ -217,81 +219,9 @@ export class Scene {
} }
} }
this.obj3d.dispatchEvent({ type: 'change' })
} }
hover(obj) {
if (typeof obj == 'object' && !this.selected.includes(obj)) {
if (obj.userData.type == 'plane') {
obj.material.opacity = 0.02
obj.children[0].material.color.set(color['planeBorder'])
} else {
if (obj.userData.type == 'mesh') {
obj.material.emissive.set(color.emissive)
}
obj.material.color.set(color[obj.userData.type])
}
}
if (typeof obj == 'object' && !this.selected.includes(obj)) {
if (obj.userData.type == 'plane') {
obj.material.opacity = 0.02
obj.children[0].material.color.set(color['planeBorder'])
} else {
if (obj.userData.type == 'mesh') {
obj.material.emissive.set(color.emissive)
}
obj.material.color.set(color[obj.userData.type])
}
}
obj.material.color.set(color[obj.userData.type])
if (obj.userData.type == 'mesh') {
obj.material.emissive.set(color.emissive)
} else if (obj.userData.type == 'plane') {
obj.material.opacity = 0.02
obj.children[0].material.color.set(color['planeBorder'])
}
if (obj.userData.type == 'selpoint') {
obj.visible = false
}
if (typeof obj == 'object') {
if (obj.userData.type == 'plane') {
obj.material.opacity = 0.06
obj.children[0].material.color.set(hoverColor['planeBorder'])
} else {
if (obj.userData.type == 'mesh') {
obj.material.emissive.set(hoverColor.emissive)
}
obj.material.color.set(hoverColor[obj.userData.type])
}
}
}
subtract(m1, m2, op) { subtract(m1, m2, op) {
let bspA = CSG.fromMesh(m1) let bspA = CSG.fromMesh(m1)
let bspB = CSG.fromMesh(m2) let bspB = CSG.fromMesh(m2)
@ -432,7 +362,6 @@ async function addSketch() {
sketch.obj3d.addEventListener('change', this.render); sketch.obj3d.addEventListener('change', this.render);
console.log('render')
this.store.dispatch({ type: 'rx-sketch', obj: sketch }) this.store.dispatch({ type: 'rx-sketch', obj: sketch })
sketch.activate() sketch.activate()

View File

@ -151,7 +151,7 @@ class Sketch {
window.addEventListener('keydown', this.onKeyPress) window.addEventListener('keydown', this.onKeyPress)
this.canvas.addEventListener('pointerdown', this.onPick) this.canvas.addEventListener('pointerdown', this.onPick)
this.canvas.addEventListener('pointermove', this.onHover) this.canvas.addEventListener('pointermove', this.onHover)
this.store.dispatch({ type: 'set-active-sketch', sketch: this.obj3d.name }) this.store.dispatch({ type: 'set-active-sketch', activeSketchId: this.obj3d.name })
this.setDimLines() this.setDimLines()
@ -203,10 +203,14 @@ class Sketch {
onKeyPress(e) { onKeyPress(e) {
switch (e.key) { this.command(e.key)
}
command(key) {
switch (key) {
case 'Escape': case 'Escape':
drawClear.call(this) drawClear.call(this)
this.mode = ""
document.activeElement.blur() document.activeElement.blur()
break; break;
case 'l': case 'l':
@ -214,22 +218,35 @@ class Sketch {
drawClear.call(this) drawClear.call(this)
} }
this.mode = "line" this.mode = "line"
this.canvas.addEventListener('pointerdown', this.drawOnClick1, {once:true}) this.canvas.addEventListener('pointerdown', this.drawOnClick1, { once: true })
break; break;
case 'a': case 'a':
this.mode = "arc" this.mode = "arc"
this.canvas.addEventListener('pointerdown', this.drawOnClick1, {once:true}) this.canvas.addEventListener('pointerdown', this.drawOnClick1, { once: true })
break;
case 'd':
if (this.mode != '') {
drawClear.call(this)
}
this.mode = ""
this.drawDimension()
break; break;
case 'p': case 'p':
this.mode = "point" this.mode = "point"
this.canvas.addEventListener('pointerdown', this.drawOnClick1, {once:true}) this.canvas.addEventListener('pointerdown', this.drawOnClick1, { once: true })
break;
case 'd':
drawClear.call(this)
this.drawDimension()
break;
case 'c':
drawClear.call(this)
setCoincident.call(this)
break;
case 'v':
drawClear.call(this)
setOrdinate.call(this, 0)
break;
case 'h':
drawClear.call(this)
setOrdinate.call(this, 1)
break;
case 't':
drawClear.call(this)
setTangent.call(this)
break; break;
case 'Delete': case 'Delete':
this.deleteSelected() this.deleteSelected()
@ -237,34 +254,6 @@ class Sketch {
case 'Backspace': case 'Backspace':
this.deleteSelected() this.deleteSelected()
break; break;
case 'c':
if (this.mode != '') {
drawClear.call(this)
}
setCoincident.call(this)
this.mode = ""
break;
case 'v':
if (this.mode != '') {
drawClear.call(this)
}
setOrdinate.call(this, 0)
this.mode = ""
break;
case 'h':
if (this.mode != '') {
drawClear.call(this)
}
setOrdinate.call(this, 1)
this.mode = ""
break;
case 't':
if (this.mode != '') {
drawClear.call(this)
}
setTangent.call(this)
this.mode = ""
break;
case 'z': case 'z':
var string = JSON.stringify(this.toJSON()); var string = JSON.stringify(this.toJSON());
window.string = string; window.string = string;
@ -298,7 +287,7 @@ class Sketch {
this.updateOtherBuffers() this.updateOtherBuffers()
this.selected = [] this.selected = []
this.obj3d.dispatchEvent({ type: 'change' }) this.scene.render()
} }
delete(obj) { delete(obj) {

View File

@ -36,7 +36,7 @@ export async function setCoincident() {
} }
this.selected = [] this.selected = []
this.obj3d.dispatchEvent({ type: 'change' }) this.scene.render()
} }
@ -67,7 +67,7 @@ export async function setOrdinate(dir = 0) {
this.updateBoundingSpheres() this.updateBoundingSpheres()
this.selected = [] this.selected = []
this.obj3d.dispatchEvent({ type: 'change' }) this.scene.render()
} }
export async function setTangent() { export async function setTangent() {
@ -104,7 +104,7 @@ export async function setTangent() {
this.updateBoundingSpheres() this.updateBoundingSpheres()
this.selected = [] this.selected = []
this.obj3d.dispatchEvent({ type: 'change' }) this.scene.render()
} }

View File

@ -127,19 +127,20 @@ export function drawClear() {
if (this.mode == "") return if (this.mode == "") return
if (['line', 'arc'].includes(this.mode)) { if (['line', 'arc'].includes(this.mode)) {
this.canvas.removeEventListener('pointerdown', this.drawOnClick1)
this.canvas.removeEventListener('pointermove', this.drawPreClick2);
this.canvas.removeEventListener('pointerdown', this.drawOnClick2);
this.canvas.removeEventListener('pointermove', this.drawPreClick3);
this.canvas.removeEventListener('pointerdown', this.drawOnClick3);
this.delete(this.obj3d.children[this.updatePoint]) this.delete(this.obj3d.children[this.updatePoint])
this.obj3d.dispatchEvent({ type: 'change' })
this.subsequent = false
this.toPush = []
} }
this.canvas.removeEventListener('pointerdown', this.drawOnClick1)
this.canvas.removeEventListener('pointermove', this.drawPreClick2);
this.canvas.removeEventListener('pointerdown', this.drawOnClick2);
this.canvas.removeEventListener('pointermove', this.drawPreClick3);
this.canvas.removeEventListener('pointerdown', this.drawOnClick3);
this.scene.render()
this.subsequent = false
this.toPush = []
this.mode = ""
} }

View File

@ -8,12 +8,11 @@ import { FaCube, FaEdit } from 'react-icons/fa'
import { BsBoxArrowUp } from 'react-icons/bs' import { BsBoxArrowUp } from 'react-icons/bs'
import { MdDone, MdSave, MdFolder } from 'react-icons/md' import { MdDone, MdSave, MdFolder } from 'react-icons/md'
import * as Icon from "./icons"; import * as Icon from "./icons";
import { setCoincident, setOrdinate, setTangent } from '../constraintEvents'
export const NavBar = () => { export const NavBar = () => {
const dispatch = useDispatch() const dispatch = useDispatch()
const treeEntries = useSelector(state => state.treeEntries) const treeEntriesById = useSelector(state => state.treeEntries.byId)
const activeSketchId = useSelector(state => state.treeEntries.activeSketchId) const activeSketchId = useSelector(state => state.treeEntries.activeSketchId)
@ -26,8 +25,14 @@ export const NavBar = () => {
forceUpdate() forceUpdate()
} }
const extrude = () => { const extrude = () => {
console.log(treeEntries.tree[activeSketchId]) // sc.extrude(treeEntriesById[activeSketchId])
sc.extrude(treeEntries.byId[activeSketchId]) sc.extrude(sc.activeSketch)
}
const addSketch = () => {
sc.addSketch()
console.log(!!sc.activeSketch,'state')
forceUpdate()
} }
useEffect(() => { useEffect(() => {
@ -41,29 +46,33 @@ export const NavBar = () => {
} }
}, [activeSketchId]) }, [activeSketchId])
useEffect(() => {
console.log(treeEntriesById)
}, [treeEntriesById])
const btnz = [ const btnz = [
[MdDone, () => { [MdDone, () => {
treeEntries.byId[activeSketchId].deactivate() // treeEntriesById[activeSketchId].deactivate()
// dispatch({ type: 'update-descendents', sketch}) // dispatch({ type: 'update-descendents', sketch})
sc.activeSketch = null sc.activeSketch.deactivate()
sc.render() sc.render()
forceUpdate() forceUpdate()
// sc.activeDim = this.activeSketch.obj3d.children[1].children // sc.activeDim = this.activeSketch.obj3d.children[1].children
}, 'Finish'], }, 'Finish'],
[Icon.Extrude, extrude, 'Extrude [e]'], [Icon.Extrude, extrude, 'Extrude [e]'],
[Icon.Dimension, () => sc.activeSketch.drawDimension(), 'Dimension [d]'], [Icon.Dimension, () => sc.activeSketch.command('d'), 'Dimension [d]'],
[Icon.Line, () => sc.extrude(treeEntries.byId[activeSketchId]), 'Line [l]'], [Icon.Line, () => sc.activeSketch.command('l'), 'Line [l]'],
[Icon.Arc, () => sc.extrude(treeEntries.byId[activeSketchId]), 'Arc [a]'], [Icon.Arc, () => sc.activeSketch.command('a'), 'Arc [a]'],
[Icon.Coincident, () => setCoincident.call(sc.activeSketch), 'Coincident [c]'], [Icon.Coincident, () => sc.activeSketch.command('c'), 'Coincident [c]'],
[Icon.Vertical, () => setOrdinate.call(sc.activeSketch, 0), 'Vertical [v]'], [Icon.Vertical, () => sc.activeSketch.command('v'), 'Vertical [v]'],
[Icon.Horizontal, () => setOrdinate.call(sc.activeSketch, 1), 'Horizontal [h]'], [Icon.Horizontal, () => sc.activeSketch.command('h'), 'Horizontal [h]'],
[Icon.Tangent, () => setTangent.call(sc.activeSketch), 'Tangent [t]'], [Icon.Tangent, () => sc.activeSketch.command('t'), 'Tangent [t]'],
] ]
const btnz2 = [ const btnz2 = [
[FaEdit, sc.addSketch, 'Sketch [s]'] [FaEdit, addSketch, 'Sketch [s]']
, ,
[Icon.Extrude, extrude, 'Extrude [e]'], [Icon.Extrude, extrude, 'Extrude [e]'],
[Icon.Union, () => boolOp('u'), 'Union'], [Icon.Union, () => boolOp('u'), 'Union'],
@ -78,7 +87,7 @@ export const NavBar = () => {
return <div className='topNav flex justify-center items-center bg-gray-700'> return <div className='topNav flex justify-center items-center bg-gray-700'>
{ {
activeSketchId ? activeSketchId?
btnz.map(([Icon, fcn, txt, shortcut], idx) => ( btnz.map(([Icon, fcn, txt, shortcut], idx) => (
<Icon className="btn w-auto h-full p-3.5" tooltip={txt} <Icon className="btn w-auto h-full p-3.5" tooltip={txt}
onClick={fcn} key={idx} onClick={fcn} key={idx}

View File

@ -39,8 +39,8 @@ export function reducer(state = {}, action) {
case 'set-active-sketch': case 'set-active-sketch':
return update(state, { return update(state, {
treeEntries: { treeEntries: {
visible: { [action.sketch]: { $set: true } }, visible: { [action.activeSketchId]: { $set: true } },
activeSketchId: { $set: action.sketch }, activeSketchId: { $set: action.activeSketchId },
}, },
}) })
case 'exit-sketch': case 'exit-sketch':

View File

@ -29,8 +29,6 @@ const TreeEntry = ({ entId }) => {
const treeEntries = useSelector(state => state.treeEntries.byId) const treeEntries = useSelector(state => state.treeEntries.byId)
const dispatch = useDispatch() const dispatch = useDispatch()
const activeSketchId = useSelector(state => state.treeEntries.activeSketchId)
// const activeSketchId = treeEntries.activeSketchId
const visible = useSelector(state => state.treeEntries.visible[entId]) const visible = useSelector(state => state.treeEntries.visible[entId])
@ -52,7 +50,7 @@ const TreeEntry = ({ entId }) => {
onDoubleClick={() => { onDoubleClick={() => {
if (obj3d.userData.type == 'sketch') { if (obj3d.userData.type == 'sketch') {
activeSketchId && treeEntries[activeSketchId].deactivate() sc.activeSketch && sc.activeSketch.deactivate()
sketch.activate() sketch.activate()
sc.clearSelection() sc.clearSelection()
sc.activeSketch = sketch; sc.activeSketch = sketch;
@ -66,17 +64,17 @@ const TreeEntry = ({ entId }) => {
}} }}
onPointerLeave={() => { onPointerLeave={() => {
if (visible & obj3d.userData.type == 'sketch') return if (visible & obj3d.userData.type == 'sketch') return
if (sc.selected.includes(obj3d) || activeSketchId == obj3d.name) return if (sc.selected.includes(obj3d) || sc.activeSketch && sc.activeSketch.name == obj3d.name) return
sc.setHover(obj3d, 0) sc.setHover(obj3d, 0)
sc.render() sc.render()
}} }}
onClick={() => { onClick={() => {
if (obj3d.userData.type == 'mesh') { // if (obj3d.userData.type == 'mesh') {
sc.selected.push( sc.selected.push(
obj3d obj3d
) )
sc.render() sc.render()
} // }
}} }}
> >
<Icon className='h-full w-auto p-1.5' /> <Icon className='h-full w-auto p-1.5' />