2023-04-05 15:08:46 +10:00
|
|
|
import { useState, useEffect } from 'react'
|
|
|
|
import { create } from 'react-modal-promise'
|
|
|
|
import { toolTips, useStore } from '../../useStore'
|
2023-07-13 16:57:22 +10:00
|
|
|
import { Value } from '../../lang/abstractSyntaxTreeTypes'
|
2023-04-05 15:08:46 +10:00
|
|
|
import {
|
|
|
|
getNodePathFromSourceRange,
|
|
|
|
getNodeFromPath,
|
|
|
|
} from '../../lang/queryAst'
|
|
|
|
import {
|
|
|
|
TransformInfo,
|
|
|
|
getTransformInfos,
|
|
|
|
transformAstSketchLines,
|
2023-04-06 12:45:56 +10:00
|
|
|
ConstraintType,
|
2023-04-05 15:08:46 +10:00
|
|
|
} from '../../lang/std/sketchcombos'
|
|
|
|
import { SetAngleLengthModal } from '../SetAngleLengthModal'
|
2023-04-06 12:45:56 +10:00
|
|
|
import {
|
|
|
|
createIdentifier,
|
|
|
|
createVariableDeclaration,
|
|
|
|
} from '../../lang/modifyAst'
|
2023-04-05 15:08:46 +10:00
|
|
|
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
2023-04-14 07:49:36 +10:00
|
|
|
import { updateCursors } from '../../lang/util'
|
2023-04-05 15:08:46 +10:00
|
|
|
|
|
|
|
const getModalInfo = create(SetAngleLengthModal as any)
|
|
|
|
|
2023-04-06 12:45:56 +10:00
|
|
|
export const SetAbsDistance = ({
|
|
|
|
buttonType,
|
|
|
|
}: {
|
|
|
|
buttonType: 'xAbs' | 'yAbs' | 'snapToYAxis' | 'snapToXAxis'
|
|
|
|
}) => {
|
2023-04-14 07:49:36 +10:00
|
|
|
const { guiMode, selectionRanges, ast, programMemory, updateAst, setCursor } =
|
|
|
|
useStore((s) => ({
|
|
|
|
guiMode: s.guiMode,
|
|
|
|
ast: s.ast,
|
|
|
|
updateAst: s.updateAst,
|
|
|
|
selectionRanges: s.selectionRanges,
|
|
|
|
programMemory: s.programMemory,
|
|
|
|
setCursor: s.setCursor,
|
|
|
|
}))
|
2023-04-06 12:45:56 +10:00
|
|
|
const disType: ConstraintType =
|
|
|
|
buttonType === 'xAbs' || buttonType === 'yAbs'
|
|
|
|
? buttonType
|
|
|
|
: buttonType === 'snapToYAxis'
|
|
|
|
? 'xAbs'
|
|
|
|
: 'yAbs'
|
2023-04-05 15:08:46 +10:00
|
|
|
const [enableAngLen, setEnableAngLen] = useState(false)
|
|
|
|
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
|
|
|
|
useEffect(() => {
|
|
|
|
if (!ast) return
|
2023-04-14 07:49:36 +10:00
|
|
|
const paths = selectionRanges.codeBasedSelections.map(({ range }) =>
|
2023-04-05 15:08:46 +10:00
|
|
|
getNodePathFromSourceRange(ast, range)
|
|
|
|
)
|
|
|
|
const nodes = paths.map(
|
|
|
|
(pathToNode) =>
|
|
|
|
getNodeFromPath<Value>(ast, pathToNode, 'CallExpression').node
|
|
|
|
)
|
|
|
|
const isAllTooltips = nodes.every(
|
|
|
|
(node) =>
|
|
|
|
node?.type === 'CallExpression' &&
|
|
|
|
toolTips.includes(node.callee.name as any)
|
|
|
|
)
|
|
|
|
|
2023-04-14 07:49:36 +10:00
|
|
|
const theTransforms = getTransformInfos(selectionRanges, ast, disType)
|
2023-04-05 15:08:46 +10:00
|
|
|
setTransformInfos(theTransforms)
|
|
|
|
|
|
|
|
const enableY =
|
|
|
|
disType === 'yAbs' &&
|
2023-04-14 07:49:36 +10:00
|
|
|
selectionRanges.otherSelections.length === 1 &&
|
|
|
|
selectionRanges.otherSelections[0] === 'x-axis' // select the x axis to set the distance from it i.e. y
|
2023-04-05 15:08:46 +10:00
|
|
|
const enableX =
|
|
|
|
disType === 'xAbs' &&
|
2023-04-14 07:49:36 +10:00
|
|
|
selectionRanges.otherSelections.length === 1 &&
|
|
|
|
selectionRanges.otherSelections[0] === 'y-axis' // select the y axis to set the distance from it i.e. x
|
2023-04-05 15:08:46 +10:00
|
|
|
|
|
|
|
const _enableHorz =
|
|
|
|
isAllTooltips &&
|
|
|
|
theTransforms.every(Boolean) &&
|
2023-04-14 07:49:36 +10:00
|
|
|
selectionRanges.codeBasedSelections.length === 1 &&
|
2023-04-05 15:08:46 +10:00
|
|
|
(enableX || enableY)
|
|
|
|
setEnableAngLen(_enableHorz)
|
2023-04-14 07:49:36 +10:00
|
|
|
}, [guiMode, selectionRanges])
|
2023-04-05 15:08:46 +10:00
|
|
|
if (guiMode.mode !== 'sketch') return null
|
|
|
|
|
2023-04-06 12:45:56 +10:00
|
|
|
const isAlign = buttonType === 'snapToYAxis' || buttonType === 'snapToXAxis'
|
|
|
|
|
2023-04-05 15:08:46 +10:00
|
|
|
return (
|
|
|
|
<button
|
|
|
|
onClick={async () => {
|
|
|
|
if (!(transformInfos && ast)) return
|
|
|
|
const { valueUsedInTransform } = transformAstSketchLines({
|
|
|
|
ast: JSON.parse(JSON.stringify(ast)),
|
2023-04-14 07:49:36 +10:00
|
|
|
selectionRanges: selectionRanges,
|
2023-04-05 15:08:46 +10:00
|
|
|
transformInfos,
|
|
|
|
programMemory,
|
|
|
|
referenceSegName: '',
|
|
|
|
})
|
|
|
|
try {
|
|
|
|
let forceVal = valueUsedInTransform || 0
|
|
|
|
const { valueNode, variableName, newVariableInsertIndex, sign } =
|
2023-04-06 12:45:56 +10:00
|
|
|
await (!isAlign &&
|
|
|
|
getModalInfo({
|
|
|
|
value: forceVal,
|
|
|
|
valueName: disType === 'yAbs' ? 'yDis' : 'xDis',
|
|
|
|
} as any))
|
|
|
|
let finalValue = isAlign
|
|
|
|
? createIdentifier('_0')
|
|
|
|
: removeDoubleNegatives(valueNode, sign, variableName)
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2023-04-14 07:49:36 +10:00
|
|
|
const { modifiedAst: _modifiedAst, pathToNodeMap } =
|
|
|
|
transformAstSketchLines({
|
|
|
|
ast: JSON.parse(JSON.stringify(ast)),
|
|
|
|
selectionRanges: selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory,
|
|
|
|
referenceSegName: '',
|
|
|
|
forceValueUsedInTransform: finalValue,
|
|
|
|
})
|
2023-04-05 15:08:46 +10:00
|
|
|
if (variableName) {
|
|
|
|
const newBody = [..._modifiedAst.body]
|
|
|
|
newBody.splice(
|
|
|
|
newVariableInsertIndex,
|
|
|
|
0,
|
|
|
|
createVariableDeclaration(variableName, valueNode)
|
|
|
|
)
|
|
|
|
_modifiedAst.body = newBody
|
|
|
|
}
|
|
|
|
|
2023-09-15 04:35:48 -07:00
|
|
|
updateAst(_modifiedAst, true, {
|
2023-04-14 07:49:36 +10:00
|
|
|
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
|
|
|
|
})
|
2023-04-05 15:08:46 +10:00
|
|
|
} catch (e) {
|
|
|
|
console.log('e', e)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
disabled={!enableAngLen}
|
|
|
|
>
|
2023-04-06 12:45:56 +10:00
|
|
|
{buttonType}
|
2023-04-05 15:08:46 +10:00
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|