2023-03-10 08:48:50 +11:00
|
|
|
import { useState, useEffect } from 'react'
|
|
|
|
import { create } from 'react-modal-promise'
|
|
|
|
import { toolTips, useStore } from '../../useStore'
|
|
|
|
import { Value } from '../../lang/abstractSyntaxTree'
|
|
|
|
import {
|
|
|
|
getNodePathFromSourceRange,
|
|
|
|
getNodeFromPath,
|
|
|
|
} from '../../lang/queryAst'
|
|
|
|
import {
|
|
|
|
TransformInfo,
|
|
|
|
getTransformInfos,
|
|
|
|
transformAstSketchLines,
|
|
|
|
} from '../../lang/std/sketchcombos'
|
2023-03-13 09:22:05 +11:00
|
|
|
import { SetAngleLengthModal } from '../SetAngleLengthModal'
|
2023-04-02 17:20:11 +10:00
|
|
|
import { createVariableDeclaration } from '../../lang/modifyAst'
|
|
|
|
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
2023-03-10 08:48:50 +11:00
|
|
|
|
|
|
|
const getModalInfo = create(SetAngleLengthModal as any)
|
|
|
|
|
|
|
|
export const SetAngleLength = ({
|
|
|
|
angleOrLength,
|
|
|
|
}: {
|
|
|
|
angleOrLength: 'setAngle' | 'setLength'
|
|
|
|
}) => {
|
|
|
|
const { guiMode, selectionRanges, ast, programMemory, updateAst } = useStore(
|
|
|
|
(s) => ({
|
|
|
|
guiMode: s.guiMode,
|
|
|
|
ast: s.ast,
|
|
|
|
updateAst: s.updateAst,
|
|
|
|
selectionRanges: s.selectionRanges,
|
|
|
|
programMemory: s.programMemory,
|
|
|
|
})
|
|
|
|
)
|
2023-04-01 16:47:00 +11:00
|
|
|
const [enableAngLen, setEnableAngLen] = useState(false)
|
2023-03-10 08:48:50 +11:00
|
|
|
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
|
|
|
|
useEffect(() => {
|
|
|
|
if (!ast) return
|
|
|
|
const paths = selectionRanges.map((selectionRange) =>
|
|
|
|
getNodePathFromSourceRange(ast, selectionRange)
|
|
|
|
)
|
|
|
|
const nodes = paths.map(
|
2023-04-01 16:47:00 +11:00
|
|
|
(pathToNode) =>
|
|
|
|
getNodeFromPath<Value>(ast, pathToNode, 'CallExpression').node
|
2023-03-10 08:48:50 +11:00
|
|
|
)
|
|
|
|
const isAllTooltips = nodes.every(
|
|
|
|
(node) =>
|
|
|
|
node?.type === 'CallExpression' &&
|
|
|
|
toolTips.includes(node.callee.name as any)
|
|
|
|
)
|
|
|
|
|
|
|
|
const theTransforms = getTransformInfos(selectionRanges, ast, angleOrLength)
|
|
|
|
setTransformInfos(theTransforms)
|
|
|
|
|
|
|
|
const _enableHorz = isAllTooltips && theTransforms.every(Boolean)
|
2023-04-01 16:47:00 +11:00
|
|
|
setEnableAngLen(_enableHorz)
|
2023-03-10 08:48:50 +11:00
|
|
|
}, [guiMode, selectionRanges])
|
|
|
|
if (guiMode.mode !== 'sketch') return null
|
|
|
|
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
onClick={async () => {
|
|
|
|
if (!(transformInfos && ast)) return
|
|
|
|
const { modifiedAst, valueUsedInTransform } = transformAstSketchLines({
|
|
|
|
ast: JSON.parse(JSON.stringify(ast)),
|
|
|
|
selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory,
|
|
|
|
referenceSegName: '',
|
|
|
|
})
|
|
|
|
try {
|
2023-04-02 17:20:11 +10:00
|
|
|
const { valueNode, variableName, newVariableInsertIndex, sign } =
|
2023-03-13 09:22:05 +11:00
|
|
|
await getModalInfo({
|
|
|
|
value: valueUsedInTransform,
|
|
|
|
valueName: angleOrLength === 'setAngle' ? 'angle' : 'length',
|
|
|
|
} as any)
|
2023-04-02 17:20:11 +10:00
|
|
|
const finalValue = removeDoubleNegatives(
|
|
|
|
valueNode,
|
|
|
|
sign,
|
|
|
|
variableName
|
|
|
|
)
|
2023-03-10 08:48:50 +11:00
|
|
|
|
|
|
|
const { modifiedAst: _modifiedAst } = transformAstSketchLines({
|
|
|
|
ast: JSON.parse(JSON.stringify(ast)),
|
|
|
|
selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory,
|
|
|
|
referenceSegName: '',
|
2023-04-02 17:20:11 +10:00
|
|
|
forceValueUsedInTransform: finalValue,
|
2023-03-10 08:48:50 +11:00
|
|
|
})
|
|
|
|
if (variableName) {
|
|
|
|
const newBody = [..._modifiedAst.body]
|
|
|
|
newBody.splice(
|
2023-03-13 09:22:05 +11:00
|
|
|
newVariableInsertIndex,
|
2023-03-10 08:48:50 +11:00
|
|
|
0,
|
|
|
|
createVariableDeclaration(variableName, valueNode)
|
|
|
|
)
|
|
|
|
_modifiedAst.body = newBody
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAst(_modifiedAst)
|
|
|
|
} catch (e) {
|
|
|
|
console.log('e', e)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
className={`border m-1 px-1 rounded text-xs ${
|
2023-04-01 16:47:00 +11:00
|
|
|
enableAngLen ? 'bg-gray-50 text-gray-800' : 'bg-gray-200 text-gray-400'
|
2023-03-10 08:48:50 +11:00
|
|
|
}`}
|
2023-04-01 16:47:00 +11:00
|
|
|
disabled={!enableAngLen}
|
2023-03-10 08:48:50 +11:00
|
|
|
>
|
|
|
|
{angleOrLength}
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|