2023-03-05 07:34:56 +11:00
|
|
|
import { useState, useEffect } from 'react'
|
2023-10-10 06:43:25 +11:00
|
|
|
import { create } from 'react-modal-promise'
|
2023-03-05 07:34:56 +11:00
|
|
|
import { toolTips, useStore } from '../../useStore'
|
2023-10-11 13:36:54 +11:00
|
|
|
import { BinaryPart, Program, Value, VariableDeclarator } from '../../lang/wasm'
|
2023-03-05 07:34:56 +11:00
|
|
|
import {
|
|
|
|
getNodePathFromSourceRange,
|
|
|
|
getNodeFromPath,
|
|
|
|
} from '../../lang/queryAst'
|
|
|
|
import { isSketchVariablesLinked } from '../../lang/std/sketchConstraints'
|
|
|
|
import {
|
|
|
|
TransformInfo,
|
2023-03-07 15:45:59 +11:00
|
|
|
transformSecondarySketchLinesTagFirst,
|
2023-03-05 07:34:56 +11:00
|
|
|
getTransformInfos,
|
2023-04-06 12:45:56 +10:00
|
|
|
ConstraintType,
|
2023-10-11 13:36:54 +11:00
|
|
|
PathToNodeMap,
|
2023-03-05 07:34:56 +11:00
|
|
|
} from '../../lang/std/sketchcombos'
|
2023-10-10 06:43:25 +11:00
|
|
|
import { GetInfoModal } from '../SetHorVertDistanceModal'
|
2023-04-06 12:45:56 +10:00
|
|
|
import { createLiteral, createVariableDeclaration } from '../../lang/modifyAst'
|
2023-04-02 17:20:11 +10:00
|
|
|
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
2023-10-11 13:36:54 +11:00
|
|
|
import { kclManager } from 'lang/KclSinglton'
|
|
|
|
import { Selections } from 'useStore'
|
2023-03-07 15:45:59 +11:00
|
|
|
|
2023-10-10 06:43:25 +11:00
|
|
|
const getModalInfo = create(GetInfoModal as any)
|
2023-03-05 07:34:56 +11:00
|
|
|
|
2023-09-16 01:23:11 -04:00
|
|
|
type ButtonType =
|
|
|
|
| 'setHorzDistance'
|
|
|
|
| 'setVertDistance'
|
|
|
|
| 'alignEndsHorizontally'
|
|
|
|
| 'alignEndsVertically'
|
|
|
|
|
|
|
|
const buttonLabels: Record<ButtonType, string> = {
|
|
|
|
setHorzDistance: 'Set Horizontal Distance',
|
|
|
|
setVertDistance: 'Set Vertical Distance',
|
|
|
|
alignEndsHorizontally: 'Align Ends Horizontally',
|
|
|
|
alignEndsVertically: 'Align Ends Vertically',
|
|
|
|
}
|
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
/*
|
2023-04-05 15:08:46 +10:00
|
|
|
export const SetHorzVertDistance = ({
|
2023-04-06 12:45:56 +10:00
|
|
|
buttonType,
|
2023-03-05 07:34:56 +11:00
|
|
|
}: {
|
2023-09-16 01:23:11 -04:00
|
|
|
buttonType: ButtonType
|
2023-03-05 07:34:56 +11:00
|
|
|
}) => {
|
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 constraint: ConstraintType =
|
|
|
|
buttonType === 'setHorzDistance' || buttonType === 'setVertDistance'
|
|
|
|
? buttonType
|
|
|
|
: buttonType === 'alignEndsHorizontally'
|
|
|
|
? 'setVertDistance'
|
|
|
|
: 'setHorzDistance'
|
2023-03-05 07:34:56 +11:00
|
|
|
const [enable, setEnable] = useState(false)
|
|
|
|
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
|
|
|
|
useEffect(() => {
|
2023-10-11 13:36:54 +11:00
|
|
|
const { transforms, enabled } = horzVertDistanceInfo({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
})
|
|
|
|
setTransformInfos(transforms)
|
|
|
|
setEnable(enabled)
|
2023-03-05 07:34:56 +11:00
|
|
|
}, [guiMode, selectionRanges])
|
|
|
|
if (guiMode.mode !== 'sketch') return null
|
|
|
|
|
2023-04-06 12:45:56 +10:00
|
|
|
const isAlign =
|
|
|
|
buttonType === 'alignEndsHorizontally' ||
|
|
|
|
buttonType === 'alignEndsVertically'
|
|
|
|
|
2023-03-05 07:34:56 +11:00
|
|
|
return (
|
|
|
|
<button
|
2023-03-07 15:45:59 +11:00
|
|
|
onClick={async () => {
|
2023-10-11 13:36:54 +11:00
|
|
|
if (!transformInfos) return
|
2023-04-14 07:49:36 +10:00
|
|
|
const { modifiedAst, tagInfo, valueUsedInTransform, pathToNodeMap } =
|
|
|
|
transformSecondarySketchLinesTagFirst({
|
2023-10-11 13:36:54 +11:00
|
|
|
ast: JSON.parse(JSON.stringify(kclManager.ast)),
|
2023-04-14 07:49:36 +10:00
|
|
|
selectionRanges,
|
|
|
|
transformInfos,
|
2023-10-11 13:36:54 +11:00
|
|
|
programMemory: kclManager.programMemory,
|
2023-04-14 07:49:36 +10:00
|
|
|
})
|
2023-10-10 06:43:25 +11:00
|
|
|
const {
|
|
|
|
segName,
|
|
|
|
value,
|
|
|
|
valueNode,
|
|
|
|
variableName,
|
|
|
|
newVariableInsertIndex,
|
|
|
|
sign,
|
|
|
|
}: {
|
|
|
|
segName: string
|
|
|
|
value: number
|
|
|
|
valueNode: Value
|
|
|
|
variableName?: string
|
|
|
|
newVariableInsertIndex: number
|
|
|
|
sign: number
|
|
|
|
} = await (!isAlign &&
|
|
|
|
getModalInfo({
|
2023-04-14 07:49:36 +10:00
|
|
|
segName: tagInfo?.tag,
|
|
|
|
isSegNameEditable: !tagInfo?.isTagExisting,
|
|
|
|
value: valueUsedInTransform,
|
|
|
|
initialVariableName:
|
|
|
|
constraint === 'setHorzDistance' ? 'xDis' : 'yDis',
|
2023-10-10 06:43:25 +11:00
|
|
|
} as any))
|
|
|
|
if (segName === tagInfo?.tag && value === valueUsedInTransform) {
|
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-14 07:49:36 +10:00
|
|
|
})
|
2023-10-10 06:43:25 +11:00
|
|
|
} else {
|
|
|
|
let finalValue = isAlign
|
|
|
|
? createLiteral(0)
|
|
|
|
: removeDoubleNegatives(valueNode as BinaryPart, sign, variableName)
|
|
|
|
// transform again but forcing certain values
|
|
|
|
const { modifiedAst: _modifiedAst, pathToNodeMap } =
|
2023-03-07 15:45:59 +11:00
|
|
|
transformSecondarySketchLinesTagFirst({
|
2023-10-11 13:36:54 +11:00
|
|
|
ast: kclManager.ast,
|
2023-03-07 15:45:59 +11:00
|
|
|
selectionRanges,
|
|
|
|
transformInfos,
|
2023-10-11 13:36:54 +11:00
|
|
|
programMemory: kclManager.programMemory,
|
2023-04-14 07:49:36 +10:00
|
|
|
forceSegName: segName,
|
|
|
|
forceValueUsedInTransform: finalValue,
|
2023-03-07 15:45:59 +11:00
|
|
|
})
|
2023-04-14 07:49:36 +10:00
|
|
|
if (variableName) {
|
|
|
|
const newBody = [..._modifiedAst.body]
|
|
|
|
newBody.splice(
|
|
|
|
newVariableInsertIndex,
|
|
|
|
0,
|
|
|
|
createVariableDeclaration(variableName, valueNode)
|
|
|
|
)
|
|
|
|
_modifiedAst.body = newBody
|
2023-03-07 15:45:59 +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-14 07:49:36 +10:00
|
|
|
})
|
2023-03-07 15:45:59 +11:00
|
|
|
}
|
|
|
|
}}
|
2023-03-05 07:34:56 +11:00
|
|
|
disabled={!enable}
|
2023-09-16 01:23:11 -04:00
|
|
|
title={buttonLabels[buttonType]}
|
2023-03-05 07:34:56 +11:00
|
|
|
>
|
2023-09-16 01:23:11 -04:00
|
|
|
{buttonLabels[buttonType]}
|
2023-03-05 07:34:56 +11:00
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
2023-10-11 13:36:54 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
export function horzVertDistanceInfo({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
constraint: 'setHorzDistance' | 'setVertDistance'
|
|
|
|
}) {
|
|
|
|
const paths = selectionRanges.codeBasedSelections.map(({ range }) =>
|
|
|
|
getNodePathFromSourceRange(kclManager.ast, range)
|
|
|
|
)
|
|
|
|
const nodes = paths.map(
|
|
|
|
(pathToNode) => getNodeFromPath<Value>(kclManager.ast, pathToNode).node
|
|
|
|
)
|
|
|
|
const varDecs = paths.map(
|
|
|
|
(pathToNode) =>
|
|
|
|
getNodeFromPath<VariableDeclarator>(
|
|
|
|
kclManager.ast,
|
|
|
|
pathToNode,
|
|
|
|
'VariableDeclarator'
|
|
|
|
)?.node
|
|
|
|
)
|
|
|
|
const primaryLine = varDecs[0]
|
|
|
|
const secondaryVarDecs = varDecs.slice(1)
|
|
|
|
const isOthersLinkedToPrimary = secondaryVarDecs.every((secondary) =>
|
|
|
|
isSketchVariablesLinked(secondary, primaryLine, kclManager.ast)
|
|
|
|
)
|
|
|
|
const isAllTooltips = nodes.every(
|
|
|
|
(node) =>
|
|
|
|
node?.type === 'CallExpression' &&
|
|
|
|
[
|
|
|
|
...toolTips,
|
|
|
|
'startSketchAt', // TODO probably a better place for this to live
|
|
|
|
].includes(node.callee.name as any)
|
|
|
|
)
|
|
|
|
|
|
|
|
const theTransforms = getTransformInfos(
|
|
|
|
{
|
|
|
|
...selectionRanges,
|
|
|
|
codeBasedSelections: selectionRanges.codeBasedSelections.slice(1),
|
|
|
|
},
|
|
|
|
kclManager.ast,
|
|
|
|
constraint
|
|
|
|
)
|
|
|
|
const _enableEqual =
|
|
|
|
secondaryVarDecs.length === 1 &&
|
|
|
|
isAllTooltips &&
|
|
|
|
isOthersLinkedToPrimary &&
|
|
|
|
theTransforms.every(Boolean)
|
|
|
|
return { enabled: _enableEqual, transforms: theTransforms }
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function applyConstraintHorzVertDistance({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
// TODO align will always be false (covered by synconous applyConstraintHorzVertAlign), remove it
|
|
|
|
isAlign = false,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
constraint: 'setHorzDistance' | 'setVertDistance'
|
|
|
|
isAlign?: boolean
|
|
|
|
}): Promise<{
|
|
|
|
modifiedAst: Program
|
|
|
|
pathToNodeMap: PathToNodeMap
|
|
|
|
}> {
|
|
|
|
const transformInfos = horzVertDistanceInfo({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
}).transforms
|
|
|
|
const { modifiedAst, tagInfo, valueUsedInTransform, pathToNodeMap } =
|
|
|
|
transformSecondarySketchLinesTagFirst({
|
|
|
|
ast: JSON.parse(JSON.stringify(kclManager.ast)),
|
|
|
|
selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
})
|
|
|
|
const {
|
|
|
|
segName,
|
|
|
|
value,
|
|
|
|
valueNode,
|
|
|
|
variableName,
|
|
|
|
newVariableInsertIndex,
|
|
|
|
sign,
|
|
|
|
}: {
|
|
|
|
segName: string
|
|
|
|
value: number
|
|
|
|
valueNode: Value
|
|
|
|
variableName?: string
|
|
|
|
newVariableInsertIndex: number
|
|
|
|
sign: number
|
|
|
|
} = await (!isAlign &&
|
|
|
|
getModalInfo({
|
|
|
|
segName: tagInfo?.tag,
|
|
|
|
isSegNameEditable: !tagInfo?.isTagExisting,
|
|
|
|
value: valueUsedInTransform,
|
|
|
|
initialVariableName: constraint === 'setHorzDistance' ? 'xDis' : 'yDis',
|
|
|
|
} as any))
|
|
|
|
if (segName === tagInfo?.tag && value === valueUsedInTransform) {
|
|
|
|
return {
|
|
|
|
modifiedAst,
|
|
|
|
pathToNodeMap,
|
|
|
|
}
|
|
|
|
// TODO handle cursor stuff
|
|
|
|
// kclManager.updateAst(modifiedAst, true, {
|
|
|
|
// callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
|
|
|
|
// })
|
|
|
|
} else {
|
|
|
|
let finalValue = isAlign
|
|
|
|
? createLiteral(0)
|
|
|
|
: removeDoubleNegatives(valueNode as BinaryPart, sign, variableName)
|
|
|
|
// transform again but forcing certain values
|
|
|
|
const { modifiedAst: _modifiedAst, pathToNodeMap } =
|
|
|
|
transformSecondarySketchLinesTagFirst({
|
|
|
|
ast: kclManager.ast,
|
|
|
|
selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
forceSegName: segName,
|
|
|
|
forceValueUsedInTransform: finalValue,
|
|
|
|
})
|
|
|
|
if (variableName) {
|
|
|
|
const newBody = [..._modifiedAst.body]
|
|
|
|
newBody.splice(
|
|
|
|
newVariableInsertIndex,
|
|
|
|
0,
|
|
|
|
createVariableDeclaration(variableName, valueNode)
|
|
|
|
)
|
|
|
|
_modifiedAst.body = newBody
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
modifiedAst: _modifiedAst,
|
|
|
|
pathToNodeMap,
|
|
|
|
}
|
|
|
|
// TODO handle cursor stuff
|
|
|
|
// kclManager.updateAst(_modifiedAst, true, {
|
|
|
|
// callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
|
|
|
|
// })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function applyConstraintHorzVertAlign({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
}: {
|
|
|
|
selectionRanges: Selections
|
|
|
|
constraint: 'setHorzDistance' | 'setVertDistance'
|
|
|
|
}): {
|
|
|
|
modifiedAst: Program
|
|
|
|
pathToNodeMap: PathToNodeMap
|
|
|
|
} {
|
|
|
|
const transformInfos = horzVertDistanceInfo({
|
|
|
|
selectionRanges,
|
|
|
|
constraint,
|
|
|
|
}).transforms
|
|
|
|
let finalValue = createLiteral(0)
|
|
|
|
const { modifiedAst, pathToNodeMap } = transformSecondarySketchLinesTagFirst({
|
|
|
|
ast: kclManager.ast,
|
|
|
|
selectionRanges,
|
|
|
|
transformInfos,
|
|
|
|
programMemory: kclManager.programMemory,
|
|
|
|
forceValueUsedInTransform: finalValue,
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
modifiedAst: modifiedAst,
|
|
|
|
pathToNodeMap,
|
|
|
|
}
|
|
|
|
// TODO handle cursor stuff
|
|
|
|
// kclManager.updateAst(_modifiedAst, true, {
|
|
|
|
// callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
|
|
|
|
// })
|
|
|
|
}
|