2023-04-05 15:08:46 +10:00
|
|
|
import { useState, useEffect } from 'react'
|
2023-10-10 06:43:25 +11:00
|
|
|
import { create } from 'react-modal-promise'
|
2023-04-05 15:08:46 +10:00
|
|
|
import { toolTips, useStore } from '../../useStore'
|
2023-10-10 06:43:25 +11:00
|
|
|
import { Value } from '../../lang/wasm'
|
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'
|
2023-10-10 06:43:25 +11:00
|
|
|
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-10-11 13:36:54 +11:00
|
|
|
import { kclManager } from 'lang/KclSinglton'
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2023-10-10 06:43:25 +11:00
|
|
|
const getModalInfo = create(SetAngleLengthModal as any)
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2023-09-16 01:23:11 -04:00
|
|
|
type ButtonType = 'xAbs' | 'yAbs' | 'snapToYAxis' | 'snapToXAxis'
|
|
|
|
|
|
|
|
const buttonLabels: Record<ButtonType, string> = {
|
|
|
|
xAbs: 'Set distance from X Axis',
|
|
|
|
yAbs: 'Set distance from Y Axis',
|
|
|
|
snapToYAxis: 'Snap To Y Axis',
|
|
|
|
snapToXAxis: 'Snap To X Axis',
|
|
|
|
}
|
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
/*
|
2023-09-16 01:23:11 -04:00
|
|
|
export const SetAbsDistance = ({ buttonType }: { buttonType: ButtonType }) => {
|
2023-10-11 13:36:54 +11:00
|
|
|
const { guiMode, selectionRanges, setCursor } = useStore((s) => ({
|
|
|
|
guiMode: s.guiMode,
|
|
|
|
selectionRanges: s.selectionRanges,
|
|
|
|
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(() => {
|
2023-04-14 07:49:36 +10:00
|
|
|
const paths = selectionRanges.codeBasedSelections.map(({ range }) =>
|
2023-10-11 13:36:54 +11:00
|
|
|
getNodePathFromSourceRange(kclManager.ast, range)
|
2023-04-05 15:08:46 +10:00
|
|
|
)
|
|
|
|
const nodes = paths.map(
|
|
|
|
(pathToNode) =>
|
2023-10-11 13:36:54 +11:00
|
|
|
getNodeFromPath<Value>(kclManager.ast, pathToNode, 'CallExpression')
|
|
|
|
.node
|
2023-04-05 15:08:46 +10:00
|
|
|
)
|
|
|
|
const isAllTooltips = nodes.every(
|
|
|
|
(node) =>
|
|
|
|
node?.type === 'CallExpression' &&
|
|
|
|
toolTips.includes(node.callee.name as any)
|
|
|
|
)
|
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
const theTransforms = getTransformInfos(
|
|
|
|
selectionRanges,
|
|
|
|
kclManager.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 () => {
|
2023-10-11 13:36:54 +11:00
|
|
|
if (!transformInfos) return
|
2023-04-05 15:08:46 +10:00
|
|
|
const { valueUsedInTransform } = transformAstSketchLines({
|
2023-10-11 13:36:54 +11:00
|
|
|
ast: JSON.parse(JSON.stringify(kclManager.ast)),
|
2023-04-14 07:49:36 +10:00
|
|
|
selectionRanges: selectionRanges,
|
2023-04-05 15:08:46 +10:00
|
|
|
transformInfos,
|
2023-10-11 13:36:54 +11:00
|
|
|
programMemory: kclManager.programMemory,
|
2023-04-05 15:08:46 +10:00
|
|
|
referenceSegName: '',
|
|
|
|
})
|
|
|
|
try {
|
2023-10-10 06:43:25 +11:00
|
|
|
let forceVal = valueUsedInTransform || 0
|
|
|
|
const { valueNode, variableName, newVariableInsertIndex, sign } =
|
|
|
|
await (!isAlign &&
|
|
|
|
getModalInfo({
|
2023-04-06 12:45:56 +10:00
|
|
|
value: forceVal,
|
|
|
|
valueName: disType === 'yAbs' ? 'yDis' : 'xDis',
|
2023-10-10 06:43:25 +11:00
|
|
|
} as any))
|
|
|
|
let finalValue = isAlign
|
|
|
|
? createIdentifier('_0')
|
|
|
|
: removeDoubleNegatives(valueNode, sign, variableName)
|
2023-04-05 15:08:46 +10:00
|
|
|
|
2023-10-10 06:43:25 +11:00
|
|
|
const { modifiedAst: _modifiedAst, pathToNodeMap } =
|
|
|
|
transformAstSketchLines({
|
2023-10-11 13:36:54 +11:00
|
|
|
ast: JSON.parse(JSON.stringify(kclManager.ast)),
|
2023-10-10 06:43:25 +11:00
|
|
|
selectionRanges: selectionRanges,
|
|
|
|
transformInfos,
|
2023-10-11 13:36:54 +11:00
|
|
|
programMemory: kclManager.programMemory,
|
2023-10-10 06:43:25 +11:00
|
|
|
referenceSegName: '',
|
|
|
|
forceValueUsedInTransform: finalValue,
|
2023-10-06 16:34:21 -04:00
|
|
|
})
|
2023-10-10 06:43:25 +11:00
|
|
|
if (variableName) {
|
|
|
|
const newBody = [..._modifiedAst.body]
|
|
|
|
newBody.splice(
|
|
|
|
newVariableInsertIndex,
|
|
|
|
0,
|
|
|
|
createVariableDeclaration(variableName, valueNode)
|
|
|
|
)
|
|
|
|
_modifiedAst.body = newBody
|
2023-10-06 16:34:21 -04:00
|
|
|
}
|
2023-10-10 06:43:25 +11:00
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
kclManager.updateAst(_modifiedAst, true, {
|
2023-10-10 06:43:25 +11:00
|
|
|
callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
|
|
|
|
})
|
2023-04-05 15:08:46 +10:00
|
|
|
} catch (e) {
|
2023-09-25 17:28:03 +10:00
|
|
|
console.log('error', e)
|
2023-04-05 15:08:46 +10:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
disabled={!enableAngLen}
|
2023-09-16 01:23:11 -04:00
|
|
|
title={buttonLabels[buttonType]}
|
2023-04-05 15:08:46 +10:00
|
|
|
>
|
2023-09-16 01:23:11 -04:00
|
|
|
{buttonLabels[buttonType]}
|
2023-04-05 15:08:46 +10:00
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
2023-10-11 13:36:54 +11:00
|
|
|
*/
|