From 2fc68e7c82d4382c9c75a755ba5b00f185a0d69d Mon Sep 17 00:00:00 2001 From: Kurt Hutten Date: Sat, 8 Apr 2023 14:16:49 +1000 Subject: [PATCH] Change perpendicular-distance constraint to something more intuitive to setting parallel distance (#110) * some clean up * Change perpendicular-distance constraint to something more intuitive to setting parallel distance * delete * add back * force rename * change name back * try renaming again --- src/Toolbar.tsx | 4 +- src/components/AvailableVarsHelpers.tsx | 1 + src/components/Toolbar/Intersect.tsx | 56 ++++++++++--- ...etAngleBetween.tsx => SetAngleBetween.tsx} | 0 ...{SetAngleLength.tsx => setAngleLength.tsx} | 0 src/lang/queryAst.ts | 84 ++++++++++++++++++- src/lang/std/sketch.ts | 2 +- src/lang/std/sketchConstraints.test.ts | 4 +- src/lang/std/sketchConstraints.ts | 15 +++- src/lang/std/sketchcombos.ts | 3 +- 10 files changed, 146 insertions(+), 23 deletions(-) rename src/components/Toolbar/{setAngleBetween.tsx => SetAngleBetween.tsx} (100%) rename src/components/Toolbar/{SetAngleLength.tsx => setAngleLength.tsx} (100%) diff --git a/src/Toolbar.tsx b/src/Toolbar.tsx index 7ee7bfa4d..b8b38a779 100644 --- a/src/Toolbar.tsx +++ b/src/Toolbar.tsx @@ -7,10 +7,10 @@ import { EqualLength } from './components/Toolbar/EqualLength' import { EqualAngle } from './components/Toolbar/EqualAngle' import { Intersect } from './components/Toolbar/Intersect' import { SetHorzVertDistance } from './components/Toolbar/SetHorzVertDistance' -import { SetAngleLength } from './components/Toolbar/SetAngleLength' +import { SetAngleLength } from './components/Toolbar/setAngleLength' import { ConvertToVariable } from './components/Toolbar/ConvertVariable' import { SetAbsDistance } from './components/Toolbar/SetAbsDistance' -import { SetAngleBetween } from './components/Toolbar/setAngleBetween' +import { SetAngleBetween } from './components/Toolbar/SetAngleBetween' export const Toolbar = () => { const { diff --git a/src/components/AvailableVarsHelpers.tsx b/src/components/AvailableVarsHelpers.tsx index 6653a1626..2dec7d9be 100644 --- a/src/components/AvailableVarsHelpers.tsx +++ b/src/components/AvailableVarsHelpers.tsx @@ -79,6 +79,7 @@ function stringSplice(str: string, index: number, count: number, add: string) { return str.slice(0, index) + (add || '') + str.slice(index + count) } +// what a terriable name export function useCalc({ value, initialVariableName: valueName = '', diff --git a/src/components/Toolbar/Intersect.tsx b/src/components/Toolbar/Intersect.tsx index 28a79506e..8983b3978 100644 --- a/src/components/Toolbar/Intersect.tsx +++ b/src/components/Toolbar/Intersect.tsx @@ -9,6 +9,7 @@ import { import { getNodePathFromSourceRange, getNodeFromPath, + isLinesParallelAndConstrained, } from '../../lang/queryAst' import { isSketchVariablesLinked } from '../../lang/std/sketchConstraints' import { @@ -17,10 +18,7 @@ import { getTransformInfos, } from '../../lang/std/sketchcombos' import { GetInfoModal } from '../SetHorVertDistanceModal' -import { - createIdentifier, - createVariableDeclaration, -} from '../../lang/modifyAst' +import { createVariableDeclaration } from '../../lang/modifyAst' import { removeDoubleNegatives } from '../AvailableVarsHelpers' const getModalInfo = create(GetInfoModal as any) @@ -37,9 +35,45 @@ export const Intersect = () => { ) const [enable, setEnable] = useState(false) const [transformInfos, setTransformInfos] = useState() + const [forecdSelectionRanges, setForcedSelectionRanges] = + useState() useEffect(() => { if (!ast) return - const paths = selectionRanges.codeBasedSelections.map(({ range }) => + if (selectionRanges.codeBasedSelections.length < 2) { + setEnable(false) + setForcedSelectionRanges({ ...selectionRanges }) + return + } + + const previousSegment = + selectionRanges.codeBasedSelections.length > 1 && + isLinesParallelAndConstrained( + ast, + programMemory, + selectionRanges.codeBasedSelections[0], + selectionRanges.codeBasedSelections[1] + ) + const shouldUsePreviousSegment = + selectionRanges.codeBasedSelections?.[1]?.type !== 'line-end' && + previousSegment && + previousSegment.isParallelAndConstrained + console.log(shouldUsePreviousSegment) + + const _forcedSelectionRanges: typeof selectionRanges = { + ...selectionRanges, + codeBasedSelections: [ + selectionRanges.codeBasedSelections?.[0], + shouldUsePreviousSegment + ? { + range: previousSegment.sourceRange, + type: 'line-end', + } + : selectionRanges.codeBasedSelections?.[1], + ], + } + setForcedSelectionRanges(_forcedSelectionRanges) + + const paths = _forcedSelectionRanges.codeBasedSelections.map(({ range }) => getNodePathFromSourceRange(ast, range) ) const nodes = paths.map( @@ -70,7 +104,8 @@ export const Intersect = () => { const theTransforms = getTransformInfos( { ...selectionRanges, - codeBasedSelections: selectionRanges.codeBasedSelections.slice(1), + codeBasedSelections: + _forcedSelectionRanges.codeBasedSelections.slice(1), }, ast, 'intersect' @@ -81,7 +116,8 @@ export const Intersect = () => { secondaryVarDecs.length === 1 && isAllTooltips && isOthersLinkedToPrimary && - theTransforms.every(Boolean) + theTransforms.every(Boolean) && + _forcedSelectionRanges?.codeBasedSelections?.[1]?.type === 'line-end' setEnable(_enableEqual) }, [guiMode, selectionRanges]) if (guiMode.mode !== 'sketch') return null @@ -89,11 +125,11 @@ export const Intersect = () => { return (