From 290f94150fab675b22bebbef91ee3ef9a100963e Mon Sep 17 00:00:00 2001 From: "Val Erastov (xibyte)" Date: Thu, 19 Mar 2020 20:46:59 -0700 Subject: [PATCH] implement flipping right --- .../sketcher/components/ConstraintEditor.jsx | 45 ++++++++++--------- web/app/sketcher/constr/ANConstraints.js | 36 +++++++-------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/web/app/sketcher/components/ConstraintEditor.jsx b/web/app/sketcher/components/ConstraintEditor.jsx index ca2526cb..579d7402 100644 --- a/web/app/sketcher/components/ConstraintEditor.jsx +++ b/web/app/sketcher/components/ConstraintEditor.jsx @@ -9,6 +9,8 @@ import Window from "ui/components/Window"; import Field from "ui/components/controls/Field"; import Label from "../../../../modules/ui/components/controls/Label"; import {SketcherAppContext} from "./SketcherApp"; +import {EMPTY_OBJECT} from "../../../../modules/gems/objects"; +import identity from 'lodash'; export function ConstraintEditor() { @@ -53,7 +55,7 @@ export function ConstraintEditor() { Object.keys(constraint.schema.constants).map(name => { const val = values[name]; if (val !== undefined) { - constraint.constants[name] = val; + constraint.updateConstant(name, val + ''); } }); onApply(); @@ -65,27 +67,30 @@ export function ConstraintEditor() { - {Object.keys(constraint.schema.constants) + {constraint.constantKeys .filter(key => !constraint.schema.constants[key].readOnly) - .sort().map(name => - - { - (() => { - const def = constraint.schema.constants[name]; - const val = values[name]; - if (def.type === 'number') { - return setValue(name, value)}/> - } else if (def.type === 'boolean') { - return setValue(name, value)}/> - } else { - return {val}; + .sort().map(name => { + const def = constraint.schema.constants[name]; + const presentation = def.presentation || EMPTY_OBJECT; + const onChange = value => setValue(name, (presentation.transformIn||identity)(value)); + + const val = (presentation.transformOut||identity)(values[name]); + const type = presentation.type || def.type; + return + + { + (() => { + if (type === 'number') { + return + } else if (type === 'boolean') { + return + } else { + return {val}; + } + })() } - - })() - - } - - )} + + })} diff --git a/web/app/sketcher/constr/ANConstraints.js b/web/app/sketcher/constr/ANConstraints.js index 154a1867..b8546b1a 100644 --- a/web/app/sketcher/constr/ANConstraints.js +++ b/web/app/sketcher/constr/ANConstraints.js @@ -637,22 +637,20 @@ export const ConstraintDefinitions = { angle: { type: 'number', description: 'line angle', - readOnly: true, initialValue: ([segment1, segment2]) => { const a1 = segment1.params.ang.get(); const a2 = segment2.params.ang.get(); const ang = makeAngle0_360(a2 - a1); return Math.abs(180 - ang) > Math.min(Math.abs(360 - ang), Math.abs(0 - ang)) ? 180 : 0; }, - transform: degree => degree * DEG_RAD + transform: degree => degree * DEG_RAD, + presentation: { + label: 'flip', + type: 'boolean', + transformOut: value => value === '180', + transformIn: value => value ? '180' : '0', + } }, - flip: { - type: 'boolean', - description: 'flips the ends', - initialValue: () => { - return false; - }, - } }, defineParamsScope: (objs, cb) => { @@ -660,12 +658,6 @@ export const ConstraintDefinitions = { }, collectPolynomials: (polynomials, params, constants) => { - if (constants.flip) { - constants = { - ...constants, - angle: (constants.angle === 0 ? 180 : 0) - }; - } ConstraintDefinitions.AngleBetween.collectPolynomials(polynomials, params, constants); } @@ -1001,13 +993,12 @@ export class AlgNumConstraint { initConstants() { if (this.schema.constants) { this.constants = {}; - Object.keys(this.schema.constants).map(name => { + this.constantKeys.map(name => { let val = this.schema.constants[name].initialValue(this.objects); if (typeof val === 'number') { val = val.toFixed(2) + ''; } - this.constants[name] = val; - + this.updateConstant(name, val); }); } } @@ -1033,9 +1024,16 @@ export class AlgNumConstraint { initialGuess() { if (this.schema.initialGuess) { - this.schema.initialGuess(this.params); + this.schema.initialGuess(this.params, this.resolvedConstants); } } + get constantKeys() { + return Object.keys(this.schema.constants); + } + + updateConstant(key, value) { + this.constants[key] = value + ''; // only string are allowed here + } }