mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-15 12:53:52 +01:00
implement flipping right
This commit is contained in:
parent
f6845a6140
commit
290f94150f
2 changed files with 42 additions and 39 deletions
|
|
@ -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() {
|
|||
|
||||
<Stack>
|
||||
|
||||
{Object.keys(constraint.schema.constants)
|
||||
{constraint.constantKeys
|
||||
.filter(key => !constraint.schema.constants[key].readOnly)
|
||||
.sort().map(name => <Field key={name}>
|
||||
<Label>{name}</Label>
|
||||
{
|
||||
(() => {
|
||||
const def = constraint.schema.constants[name];
|
||||
const val = values[name];
|
||||
if (def.type === 'number') {
|
||||
return <NumberControl value={val} onChange={value => setValue(name, value)}/>
|
||||
} else if (def.type === 'boolean') {
|
||||
return <CheckboxControl value={val} onChange={value => setValue(name, value)}/>
|
||||
} else {
|
||||
return <span>{val}</span>;
|
||||
.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 <Field key={presentation.label||name}>
|
||||
<Label>{name}</Label>
|
||||
{
|
||||
(() => {
|
||||
if (type === 'number') {
|
||||
return <NumberControl value={val} onChange={onChange}/>
|
||||
} else if (type === 'boolean') {
|
||||
return <CheckboxControl value={val} onChange={onChange}/>
|
||||
} else {
|
||||
return <span>{val}</span>;
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
})()
|
||||
|
||||
}
|
||||
|
||||
</Field>)}
|
||||
</Field>
|
||||
})}
|
||||
|
||||
|
||||
<ButtonGroup>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue