alignVertically/Horizontally (#106)
This commit is contained in:
@ -10,14 +10,22 @@ import {
|
||||
TransformInfo,
|
||||
getTransformInfos,
|
||||
transformAstSketchLines,
|
||||
ConstraintType,
|
||||
} from '../../lang/std/sketchcombos'
|
||||
import { SetAngleLengthModal } from '../SetAngleLengthModal'
|
||||
import { createVariableDeclaration } from '../../lang/modifyAst'
|
||||
import {
|
||||
createIdentifier,
|
||||
createVariableDeclaration,
|
||||
} from '../../lang/modifyAst'
|
||||
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
||||
|
||||
const getModalInfo = create(SetAngleLengthModal as any)
|
||||
|
||||
export const SetAbsDistance = ({ disType }: { disType: 'xAbs' | 'yAbs' }) => {
|
||||
export const SetAbsDistance = ({
|
||||
buttonType,
|
||||
}: {
|
||||
buttonType: 'xAbs' | 'yAbs' | 'snapToYAxis' | 'snapToXAxis'
|
||||
}) => {
|
||||
const {
|
||||
guiMode,
|
||||
selectionRanges: selections,
|
||||
@ -31,6 +39,12 @@ export const SetAbsDistance = ({ disType }: { disType: 'xAbs' | 'yAbs' }) => {
|
||||
selectionRanges: s.selectionRanges,
|
||||
programMemory: s.programMemory,
|
||||
}))
|
||||
const disType: ConstraintType =
|
||||
buttonType === 'xAbs' || buttonType === 'yAbs'
|
||||
? buttonType
|
||||
: buttonType === 'snapToYAxis'
|
||||
? 'xAbs'
|
||||
: 'yAbs'
|
||||
const [enableAngLen, setEnableAngLen] = useState(false)
|
||||
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
|
||||
useEffect(() => {
|
||||
@ -69,6 +83,8 @@ export const SetAbsDistance = ({ disType }: { disType: 'xAbs' | 'yAbs' }) => {
|
||||
}, [guiMode, selections])
|
||||
if (guiMode.mode !== 'sketch') return null
|
||||
|
||||
const isAlign = buttonType === 'snapToYAxis' || buttonType === 'snapToXAxis'
|
||||
|
||||
return (
|
||||
<button
|
||||
onClick={async () => {
|
||||
@ -83,11 +99,14 @@ export const SetAbsDistance = ({ disType }: { disType: 'xAbs' | 'yAbs' }) => {
|
||||
try {
|
||||
let forceVal = valueUsedInTransform || 0
|
||||
const { valueNode, variableName, newVariableInsertIndex, sign } =
|
||||
await getModalInfo({
|
||||
value: forceVal,
|
||||
valueName: disType === 'yAbs' ? 'yDis' : 'xDis',
|
||||
} as any)
|
||||
let finalValue = removeDoubleNegatives(valueNode, sign, variableName)
|
||||
await (!isAlign &&
|
||||
getModalInfo({
|
||||
value: forceVal,
|
||||
valueName: disType === 'yAbs' ? 'yDis' : 'xDis',
|
||||
} as any))
|
||||
let finalValue = isAlign
|
||||
? createIdentifier('_0')
|
||||
: removeDoubleNegatives(valueNode, sign, variableName)
|
||||
|
||||
const { modifiedAst: _modifiedAst } = transformAstSketchLines({
|
||||
ast: JSON.parse(JSON.stringify(ast)),
|
||||
@ -117,7 +136,7 @@ export const SetAbsDistance = ({ disType }: { disType: 'xAbs' | 'yAbs' }) => {
|
||||
}`}
|
||||
disabled={!enableAngLen}
|
||||
>
|
||||
{disType}
|
||||
{buttonType}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
@ -15,17 +15,22 @@ import {
|
||||
TransformInfo,
|
||||
transformSecondarySketchLinesTagFirst,
|
||||
getTransformInfos,
|
||||
ConstraintType,
|
||||
} from '../../lang/std/sketchcombos'
|
||||
import { GetInfoModal } from '../SetHorVertDistanceModal'
|
||||
import { createVariableDeclaration } from '../../lang/modifyAst'
|
||||
import { createLiteral, createVariableDeclaration } from '../../lang/modifyAst'
|
||||
import { removeDoubleNegatives } from '../AvailableVarsHelpers'
|
||||
|
||||
const getModalInfo = create(GetInfoModal as any)
|
||||
|
||||
export const SetHorzVertDistance = ({
|
||||
horOrVert,
|
||||
buttonType,
|
||||
}: {
|
||||
horOrVert: 'setHorzDistance' | 'setVertDistance'
|
||||
buttonType:
|
||||
| 'setHorzDistance'
|
||||
| 'setVertDistance'
|
||||
| 'alignEndsHorizontally'
|
||||
| 'alignEndsVertically'
|
||||
}) => {
|
||||
const { guiMode, selectionRanges, ast, programMemory, updateAst } = useStore(
|
||||
(s) => ({
|
||||
@ -36,6 +41,12 @@ export const SetHorzVertDistance = ({
|
||||
programMemory: s.programMemory,
|
||||
})
|
||||
)
|
||||
const constraint: ConstraintType =
|
||||
buttonType === 'setHorzDistance' || buttonType === 'setVertDistance'
|
||||
? buttonType
|
||||
: buttonType === 'alignEndsHorizontally'
|
||||
? 'setVertDistance'
|
||||
: 'setHorzDistance'
|
||||
const [enable, setEnable] = useState(false)
|
||||
const [transformInfos, setTransformInfos] = useState<TransformInfo[]>()
|
||||
useEffect(() => {
|
||||
@ -74,7 +85,7 @@ export const SetHorzVertDistance = ({
|
||||
codeBasedSelections: selectionRanges.codeBasedSelections.slice(1),
|
||||
},
|
||||
ast,
|
||||
horOrVert
|
||||
constraint
|
||||
)
|
||||
setTransformInfos(theTransforms)
|
||||
|
||||
@ -87,6 +98,10 @@ export const SetHorzVertDistance = ({
|
||||
}, [guiMode, selectionRanges])
|
||||
if (guiMode.mode !== 'sketch') return null
|
||||
|
||||
const isAlign =
|
||||
buttonType === 'alignEndsHorizontally' ||
|
||||
buttonType === 'alignEndsVertically'
|
||||
|
||||
return (
|
||||
<button
|
||||
onClick={async () => {
|
||||
@ -112,21 +127,24 @@ export const SetHorzVertDistance = ({
|
||||
variableName?: string
|
||||
newVariableInsertIndex: number
|
||||
sign: number
|
||||
} = await getModalInfo({
|
||||
segName: tagInfo?.tag,
|
||||
isSegNameEditable: !tagInfo?.isTagExisting,
|
||||
value: valueUsedInTransform,
|
||||
initialVariableName:
|
||||
horOrVert === 'setHorzDistance' ? 'xDis' : 'yDis',
|
||||
} as any)
|
||||
} = await (!isAlign &&
|
||||
getModalInfo({
|
||||
segName: tagInfo?.tag,
|
||||
isSegNameEditable: !tagInfo?.isTagExisting,
|
||||
value: valueUsedInTransform,
|
||||
initialVariableName:
|
||||
constraint === 'setHorzDistance' ? 'xDis' : 'yDis',
|
||||
} as any))
|
||||
if (segName === tagInfo?.tag && value === valueUsedInTransform) {
|
||||
updateAst(modifiedAst)
|
||||
} else {
|
||||
const finalValue = removeDoubleNegatives(
|
||||
valueNode as BinaryPart,
|
||||
sign,
|
||||
variableName
|
||||
)
|
||||
let finalValue = isAlign
|
||||
? createLiteral(0)
|
||||
: removeDoubleNegatives(
|
||||
valueNode as BinaryPart,
|
||||
sign,
|
||||
variableName
|
||||
)
|
||||
// transform again but forcing certain values
|
||||
const { modifiedAst: _modifiedAst } =
|
||||
transformSecondarySketchLinesTagFirst({
|
||||
@ -155,7 +173,7 @@ export const SetHorzVertDistance = ({
|
||||
}`}
|
||||
disabled={!enable}
|
||||
>
|
||||
{horOrVert}
|
||||
{buttonType}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user