three.cad/src/react/reducer.js

127 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-03-26 13:38:49 +08:00
2021-03-29 10:23:24 +08:00
2021-04-17 13:58:54 +08:00
import { DepTree } from './depTree'
2021-04-07 12:21:09 +08:00
import update from 'immutability-helper'
2021-04-15 16:36:54 +08:00
import { combineReducers } from 'redux';
const defaultState = {
byId: {},
allIds: [],
tree: {},
order: {},
visible: {},
activeSketchId: ""
2021-04-07 12:21:09 +08:00
}
2021-04-06 12:52:19 +08:00
2021-04-17 17:09:35 +08:00
let cache
2021-04-17 13:58:54 +08:00
2021-04-15 16:36:54 +08:00
export function treeEntries(state = defaultState, action) {
2021-03-29 10:23:24 +08:00
switch (action.type) {
2021-03-29 18:27:34 +08:00
case 'rx-sketch':
2021-04-06 12:52:19 +08:00
return update(state, {
2021-04-15 16:36:54 +08:00
byId: { [action.obj.obj3d.name]: { $set: action.obj } },
allIds: { $push: [action.obj.obj3d.name] },
tree: { [action.obj.obj3d.name]: { $set: {} } },
order: { [action.obj.obj3d.name]: { $set: state.allIds.length } },
visible: { [action.obj.obj3d.name]: { $set: true } },
2021-04-06 12:52:19 +08:00
})
2021-04-11 15:57:39 +08:00
case 'set-entry-visibility': {
return update(state, {
2021-04-15 16:36:54 +08:00
visible: { $merge: action.obj },
2021-04-11 15:57:39 +08:00
})
}
2021-03-30 13:13:13 +08:00
case 'set-active-sketch':
2021-04-17 17:09:35 +08:00
cache = JSON.stringify(state.byId[action.activeSketchId])
2021-04-06 12:52:19 +08:00
return update(state, {
2021-04-15 16:36:54 +08:00
visible: { [action.activeSketchId]: { $set: true } },
activeSketchId: { $set: action.activeSketchId },
2021-04-06 12:52:19 +08:00
})
2021-04-17 13:58:54 +08:00
case 'finish-sketch':
return update(state, {
activeSketchId: { $set: "" },
visible: { [state.activeSketchId]: { $set: false } },
})
case 'cancel-sketch':
const sketch = sc.loadSketch(cache)
const deletedObj = sc.obj3d.children.splice(state.order[state.activeSketchId] + 1, 1,
sketch.obj3d
)[0]
deletedObj.traverse((obj) => {
if (obj.geometry) obj.geometry.dispose()
if (obj.material) obj.material.dispose()
})
2021-04-11 15:57:39 +08:00
return update(state, {
2021-04-15 16:36:54 +08:00
activeSketchId: { $set: "" },
2021-04-17 13:58:54 +08:00
byId: { [state.activeSketchId]: { $set: sketch } },
2021-04-15 16:36:54 +08:00
visible: { [state.activeSketchId]: { $set: false } },
2021-04-11 15:57:39 +08:00
})
2021-03-29 18:27:34 +08:00
case 'rx-extrusion':
2021-04-06 12:52:19 +08:00
return update(state, {
2021-04-15 16:36:54 +08:00
byId: {
[action.mesh.name]: { $set: action.mesh }
},
allIds: { $push: [action.mesh.name] },
tree: {
2021-04-17 07:03:30 +08:00
[action.sketchId]: { [action.mesh.name]: { $set: true } },
2021-04-15 16:36:54 +08:00
[action.mesh.name]: { $set: {} }
},
order: { [action.mesh.name]: { $set: state.allIds.length } },
visible: {
[action.mesh.name]: { $set: true }
2021-03-29 18:27:34 +08:00
}
2021-04-06 12:52:19 +08:00
})
2021-04-06 13:40:47 +08:00
case 'rx-boolean':
2021-04-06 12:52:19 +08:00
2021-04-06 13:40:47 +08:00
return update(state, {
2021-04-15 16:36:54 +08:00
byId: {
[action.mesh.name]: { $set: action.mesh }
},
allIds: { $push: [action.mesh.name] },
tree: {
[action.deps[0]]: { [action.mesh.name]: { $set: true } },
[action.deps[1]]: { [action.mesh.name]: { $set: true } },
[action.mesh.name]: { $set: {} }
},
order: { [action.mesh.name]: { $set: state.allIds.length } }
2021-04-06 13:40:47 +08:00
})
2021-04-06 12:52:19 +08:00
case 'delete-node':
2021-04-15 16:36:54 +08:00
const depTree = new DepTree(state)
2021-04-06 12:52:19 +08:00
const obj = depTree.deleteNode(action.id)
2021-04-15 16:36:54 +08:00
return update(state, { $merge: obj })
2021-04-06 12:52:19 +08:00
2021-04-01 06:03:35 +08:00
case 'restore-state':
return action.state
2021-03-29 10:23:24 +08:00
default:
return state
}
}
2021-04-17 13:58:54 +08:00
export function ui(state = { dialog: {} }, action) {
2021-04-17 11:54:01 +08:00
switch (action.type) {
case 'set-dialog':
return update(state, {
dialog: { $set: { target: action.target, action: action.action } },
})
case 'clear-dialog':
return update(state, {
dialog: { $set: {} },
})
default:
return state
}
}
2021-03-29 18:27:34 +08:00
2021-04-15 16:36:54 +08:00
export const reducer = combineReducers({
2021-04-17 11:54:01 +08:00
ui,
2021-04-15 16:36:54 +08:00
treeEntries
})