Lengths and angles should be set with |abs| values (#93)
* Lengths and angles should be set with |abs| values * clean up
This commit is contained in:
@ -1,7 +1,16 @@
|
||||
import { useEffect, useState, useRef } from 'react'
|
||||
import { abstractSyntaxTree, Value } from '../lang/abstractSyntaxTree'
|
||||
import {
|
||||
abstractSyntaxTree,
|
||||
BinaryPart,
|
||||
Value,
|
||||
} from '../lang/abstractSyntaxTree'
|
||||
import { executor } from '../lang/executor'
|
||||
import { findUniqueName } from '../lang/modifyAst'
|
||||
import {
|
||||
createIdentifier,
|
||||
createLiteral,
|
||||
createUnaryExpression,
|
||||
findUniqueName,
|
||||
} from '../lang/modifyAst'
|
||||
import { findAllPreviousVariables, PrevVariable } from '../lang/queryAst'
|
||||
import { lexer } from '../lang/tokeniser'
|
||||
import { useStore } from '../useStore'
|
||||
@ -238,3 +247,32 @@ export const CreateNewVariable = ({
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export function removeDoubleNegatives(
|
||||
valueNode: BinaryPart,
|
||||
sign: number,
|
||||
variableName?: string
|
||||
): BinaryPart {
|
||||
let finValue: BinaryPart = variableName
|
||||
? createIdentifier(variableName)
|
||||
: valueNode
|
||||
if (sign === -1) finValue = createUnaryExpression(finValue)
|
||||
if (
|
||||
finValue.type === 'UnaryExpression' &&
|
||||
finValue.operator === '-' &&
|
||||
finValue.argument.type === 'UnaryExpression' &&
|
||||
finValue.argument.operator === '-'
|
||||
) {
|
||||
finValue = finValue.argument.argument
|
||||
}
|
||||
if (
|
||||
finValue.type === 'UnaryExpression' &&
|
||||
finValue.operator === '-' &&
|
||||
finValue.argument.type === 'Literal' &&
|
||||
typeof finValue.argument.value === 'number' &&
|
||||
finValue.argument.value < 0
|
||||
) {
|
||||
finValue = createLiteral(-finValue.argument.value)
|
||||
}
|
||||
return finValue
|
||||
}
|
||||
|
@ -478,13 +478,17 @@ function LineRender({
|
||||
setBaseColor('orange')
|
||||
return
|
||||
}
|
||||
const level = getConstraintLevelFromSourceRange(sourceRange, ast)
|
||||
if (level === 'free') {
|
||||
try {
|
||||
const level = getConstraintLevelFromSourceRange(sourceRange, ast)
|
||||
if (level === 'free') {
|
||||
setBaseColor('orange')
|
||||
} else if (level === 'partial') {
|
||||
setBaseColor('IndianRed')
|
||||
} else if (level === 'full') {
|
||||
setBaseColor('lightgreen')
|
||||
}
|
||||
} catch (e) {
|
||||
setBaseColor('orange')
|
||||
} else if (level === 'partial') {
|
||||
setBaseColor('IndianRed')
|
||||
} else if (level === 'full') {
|
||||
setBaseColor('lightgreen')
|
||||
}
|
||||
}, [guiMode, ast, sourceRange])
|
||||
|
||||
|
@ -19,6 +19,7 @@ export const SetAngleLengthModal = ({
|
||||
isOpen: boolean
|
||||
onResolve: (a: {
|
||||
value: string
|
||||
sign: number
|
||||
valueNode: Value
|
||||
variableName?: string
|
||||
newVariableInsertIndex: number
|
||||
@ -27,7 +28,8 @@ export const SetAngleLengthModal = ({
|
||||
value: number
|
||||
valueName: string
|
||||
}) => {
|
||||
const [value, setValue] = useState(String(initialValue))
|
||||
const [sign, setSign] = useState(Math.sign(Number(initialValue)))
|
||||
const [value, setValue] = useState(String(initialValue * sign))
|
||||
const [shouldCreateVariable, setShouldCreateVariable] = useState(false)
|
||||
|
||||
const {
|
||||
@ -39,7 +41,10 @@ export const SetAngleLengthModal = ({
|
||||
setNewVariableName,
|
||||
inputRef,
|
||||
newVariableInsertIndex,
|
||||
} = useCalc({ value, initialVariableName: valueName })
|
||||
} = useCalc({
|
||||
value,
|
||||
initialVariableName: valueName,
|
||||
})
|
||||
|
||||
return (
|
||||
<Transition appear show={isOpen} as={Fragment}>
|
||||
@ -87,7 +92,13 @@ export const SetAngleLengthModal = ({
|
||||
>
|
||||
{valueName} Value
|
||||
</label>
|
||||
<div className="mt-1">
|
||||
<div className="mt-1 flex">
|
||||
<button
|
||||
className="border border-gray-300 px-2"
|
||||
onClick={() => setSign(-sign)}
|
||||
>
|
||||
{sign > 0 ? '+' : '-'}
|
||||
</button>
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="text"
|
||||
@ -121,6 +132,7 @@ export const SetAngleLengthModal = ({
|
||||
valueNode &&
|
||||
onResolve({
|
||||
value,
|
||||
sign,
|
||||
valueNode,
|
||||
newVariableInsertIndex,
|
||||
variableName: shouldCreateVariable
|
||||
|
@ -25,6 +25,7 @@ export const GetInfoModal = ({
|
||||
valueNode: Value
|
||||
variableName?: string
|
||||
newVariableInsertIndex: number
|
||||
sign: number
|
||||
}) => void
|
||||
onReject: (a: any) => void
|
||||
segName: string
|
||||
@ -32,8 +33,9 @@ export const GetInfoModal = ({
|
||||
value: number
|
||||
initialVariableName: string
|
||||
}) => {
|
||||
const [sign, setSign] = useState(Math.sign(Number(initialValue)))
|
||||
const [segName, setSegName] = useState(initialSegName)
|
||||
const [value, setValue] = useState(String(initialValue))
|
||||
const [value, setValue] = useState(String(Math.abs(initialValue)))
|
||||
const [shouldCreateVariable, setShouldCreateVariable] = useState(false)
|
||||
|
||||
const {
|
||||
@ -45,7 +47,7 @@ export const GetInfoModal = ({
|
||||
newVariableName,
|
||||
isNewVariableNameUnique,
|
||||
newVariableInsertIndex,
|
||||
} = useCalc({ value, initialVariableName })
|
||||
} = useCalc({ value: value, initialVariableName })
|
||||
|
||||
return (
|
||||
<Transition appear show={isOpen} as={Fragment}>
|
||||
@ -93,7 +95,13 @@ export const GetInfoModal = ({
|
||||
>
|
||||
Distance
|
||||
</label>
|
||||
<div className="mt-1">
|
||||
<div className="mt-1 flex">
|
||||
<button
|
||||
className="border border-gray-300 px-2 mr-1"
|
||||
onClick={() => setSign(-sign)}
|
||||
>
|
||||
{sign > 0 ? '+' : '-'}
|
||||
</button>
|
||||
<input
|
||||
type="text"
|
||||
name="val"
|
||||
@ -145,6 +153,7 @@ export const GetInfoModal = ({
|
||||
value,
|
||||
valueNode,
|
||||
newVariableInsertIndex,
|
||||
sign,
|
||||
variableName: shouldCreateVariable
|
||||
? newVariableName
|
||||
: undefined,
|
||||
|
@ -1,7 +1,11 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { create } from 'react-modal-promise'
|
||||
import { toolTips, useStore } from '../../useStore'
|
||||
import { Value, VariableDeclarator } from '../../lang/abstractSyntaxTree'
|
||||
import {
|
||||
BinaryPart,
|
||||
Value,
|
||||
VariableDeclarator,
|
||||
} from '../../lang/abstractSyntaxTree'
|
||||
import {
|
||||
getNodePathFromSourceRange,
|
||||
getNodeFromPath,
|
||||
@ -17,6 +21,7 @@ import {
|
||||
createIdentifier,
|
||||
createVariableDeclaration,
|
||||
} from '../../lang/modifyAst'
|
||||
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
||||
|
||||
const getModalInfo = create(GetInfoModal as any)
|
||||
|
||||
@ -95,12 +100,14 @@ export const Intersect = () => {
|
||||
valueNode,
|
||||
variableName,
|
||||
newVariableInsertIndex,
|
||||
sign,
|
||||
}: {
|
||||
segName: string
|
||||
value: number
|
||||
valueNode: Value
|
||||
variableName?: string
|
||||
newVariableInsertIndex: number
|
||||
sign: number
|
||||
} = await getModalInfo({
|
||||
segName: tagInfo?.tag,
|
||||
isSegNameEditable: !tagInfo?.isTagExisting,
|
||||
@ -111,6 +118,11 @@ export const Intersect = () => {
|
||||
updateAst(modifiedAst)
|
||||
} else {
|
||||
// transform again but forcing certain values
|
||||
const finalValue = removeDoubleNegatives(
|
||||
valueNode as BinaryPart,
|
||||
sign,
|
||||
variableName
|
||||
)
|
||||
const { modifiedAst: _modifiedAst } =
|
||||
transformSecondarySketchLinesTagFirst({
|
||||
ast,
|
||||
@ -118,9 +130,7 @@ export const Intersect = () => {
|
||||
transformInfos,
|
||||
programMemory,
|
||||
forceSegName: segName,
|
||||
forceValueUsedInTransform: variableName
|
||||
? createIdentifier(variableName)
|
||||
: valueNode,
|
||||
forceValueUsedInTransform: finalValue,
|
||||
})
|
||||
if (variableName) {
|
||||
const newBody = [..._modifiedAst.body]
|
||||
|
@ -5,7 +5,6 @@ import { Value } from '../../lang/abstractSyntaxTree'
|
||||
import {
|
||||
getNodePathFromSourceRange,
|
||||
getNodeFromPath,
|
||||
findAllPreviousVariables,
|
||||
} from '../../lang/queryAst'
|
||||
import {
|
||||
TransformInfo,
|
||||
@ -13,10 +12,8 @@ import {
|
||||
transformAstSketchLines,
|
||||
} from '../../lang/std/sketchcombos'
|
||||
import { SetAngleLengthModal } from '../SetAngleLengthModal'
|
||||
import {
|
||||
createIdentifier,
|
||||
createVariableDeclaration,
|
||||
} from '../../lang/modifyAst'
|
||||
import { createVariableDeclaration } from '../../lang/modifyAst'
|
||||
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
||||
|
||||
const getModalInfo = create(SetAngleLengthModal as any)
|
||||
|
||||
@ -71,11 +68,16 @@ export const SetAngleLength = ({
|
||||
referenceSegName: '',
|
||||
})
|
||||
try {
|
||||
const { valueNode, variableName, newVariableInsertIndex } =
|
||||
const { valueNode, variableName, newVariableInsertIndex, sign } =
|
||||
await getModalInfo({
|
||||
value: valueUsedInTransform,
|
||||
valueName: angleOrLength === 'setAngle' ? 'angle' : 'length',
|
||||
} as any)
|
||||
const finalValue = removeDoubleNegatives(
|
||||
valueNode,
|
||||
sign,
|
||||
variableName
|
||||
)
|
||||
|
||||
const { modifiedAst: _modifiedAst } = transformAstSketchLines({
|
||||
ast: JSON.parse(JSON.stringify(ast)),
|
||||
@ -83,9 +85,7 @@ export const SetAngleLength = ({
|
||||
transformInfos,
|
||||
programMemory,
|
||||
referenceSegName: '',
|
||||
forceValueUsedInTransform: variableName
|
||||
? createIdentifier(variableName)
|
||||
: valueNode,
|
||||
forceValueUsedInTransform: finalValue,
|
||||
})
|
||||
if (variableName) {
|
||||
const newBody = [..._modifiedAst.body]
|
||||
|
@ -1,7 +1,11 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { create } from 'react-modal-promise'
|
||||
import { toolTips, useStore } from '../../useStore'
|
||||
import { Value, VariableDeclarator } from '../../lang/abstractSyntaxTree'
|
||||
import {
|
||||
BinaryPart,
|
||||
Value,
|
||||
VariableDeclarator,
|
||||
} from '../../lang/abstractSyntaxTree'
|
||||
import {
|
||||
getNodePathFromSourceRange,
|
||||
getNodeFromPath,
|
||||
@ -17,6 +21,7 @@ import {
|
||||
createIdentifier,
|
||||
createVariableDeclaration,
|
||||
} from '../../lang/modifyAst'
|
||||
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
||||
|
||||
const getModalInfo = create(GetInfoModal as any)
|
||||
|
||||
@ -99,12 +104,14 @@ export const SetHorzDistance = ({
|
||||
valueNode,
|
||||
variableName,
|
||||
newVariableInsertIndex,
|
||||
sign,
|
||||
}: {
|
||||
segName: string
|
||||
value: number
|
||||
valueNode: Value
|
||||
variableName?: string
|
||||
newVariableInsertIndex: number
|
||||
sign: number
|
||||
} = await getModalInfo({
|
||||
segName: tagInfo?.tag,
|
||||
isSegNameEditable: !tagInfo?.isTagExisting,
|
||||
@ -115,6 +122,12 @@ export const SetHorzDistance = ({
|
||||
if (segName === tagInfo?.tag && value === valueUsedInTransform) {
|
||||
updateAst(modifiedAst)
|
||||
} else {
|
||||
const finalValue = removeDoubleNegatives(
|
||||
valueNode as BinaryPart,
|
||||
sign,
|
||||
variableName
|
||||
)
|
||||
console.log(finalValue)
|
||||
// transform again but forcing certain values
|
||||
const { modifiedAst: _modifiedAst } =
|
||||
transformSecondarySketchLinesTagFirst({
|
||||
@ -123,9 +136,7 @@ export const SetHorzDistance = ({
|
||||
transformInfos,
|
||||
programMemory,
|
||||
forceSegName: segName,
|
||||
forceValueUsedInTransform: variableName
|
||||
? createIdentifier(variableName)
|
||||
: valueNode,
|
||||
forceValueUsedInTransform: finalValue,
|
||||
})
|
||||
if (variableName) {
|
||||
const newBody = [..._modifiedAst.body]
|
||||
|
Reference in New Issue
Block a user