Skip to content

Commit

Permalink
Add constraint propagation to point 3d params, #417
Browse files Browse the repository at this point in the history
  • Loading branch information
cocopon committed Mar 10, 2023
1 parent e2dcaa0 commit f7c2a48
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 76 deletions.
56 changes: 16 additions & 40 deletions packages/core/src/input-binding/point-2d/plugin-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,66 +143,42 @@ describe(Point2dInputPlugin.id, () => {
{
params: {
params: {
x: {min: -1, max: 1, step: 0.1},
y: {min: -2, max: 2, step: 0.2},
},
},
expected: {
x: {min: -1, max: 1, step: 0.1},
y: {min: -2, max: 2, step: 0.2},
},
},
{
params: {
params: {
min: -1,
max: 1,
step: 0.1,
...{min: -1, max: 1, step: 0.1},
x: {min: -2, max: 2, step: 0.2},
},
},
expected: {
x: {min: -2, max: 2, step: 0.2},
y: {min: -1, max: 1, step: 0.1},
},
expected: [
{min: -2, max: 2, step: 0.2},
{min: -1, max: 1, step: 0.1},
],
},
{
params: {
params: {
min: -1,
max: 1,
step: 0.1,
...{min: -1, max: 1, step: 0.1},
y: {min: -2, max: 2, step: 0.2},
},
},
expected: {
x: {min: -1, max: 1, step: 0.1},
y: {min: -2, max: 2, step: 0.2},
},
expected: [
{min: -1, max: 1, step: 0.1},
{min: -2, max: 2, step: 0.2},
],
},
].forEach(({params, expected}) => {
describe(`when params=${JSON.stringify(params)}`, () => {
it('should propagate dimension params', () => {
const doc = createTestWindow().document;
const p = {x: 12, y: 34, hello: 'world'};
const obj = {p: p};
const c = createInputBindingController(Point2dInputPlugin, {
document: doc,
params: params.params,
target: new BindingTarget(obj, 'p'),
target: new BindingTarget({p: {x: 12, y: 34}}, 'p'),
}) as InputBindingController;

const constraint = getPoint2dConstraint(c.value);

const xp = getDimensionProps(
constraint.components[0] as Constraint<number>,
);
assert.deepStrictEqual(xp, expected.x);

const yp = getDimensionProps(
constraint.components[1] as Constraint<number>,
);
assert.deepStrictEqual(yp, expected.y);
const comps = getPoint2dConstraint(c.value).components;
[0, 1].forEach((i) => {
const p = getDimensionProps(comps[i] as Constraint<number>);
assert.deepStrictEqual(p, expected[i]);
});
});
});
});
Expand Down
16 changes: 2 additions & 14 deletions packages/core/src/input-binding/point-2d/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,8 @@ function createConstraint(
return new PointNdConstraint({
assembly: Point2dAssembly,
components: [
createDimensionConstraint(
{
...params,
...params.x,
},
initialValue.x,
),
createDimensionConstraint(
{
...params,
...params.y,
},
initialValue.y,
),
createDimensionConstraint({...params, ...params.x}, initialValue.x),
createDimensionConstraint({...params, ...params.y}, initialValue.y),
],
});
}
Expand Down
103 changes: 84 additions & 19 deletions packages/core/src/input-binding/point-3d/plugin-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as assert from 'assert';
import {describe, it} from 'mocha';

import {InputBindingController} from '../../blade/binding/controller/input-binding';
import {BindingTarget} from '../../common/binding/target';
import {InputBindingValue} from '../../common/binding/value/input-binding';
import {findConstraint} from '../../common/constraint/composite';
import {Constraint} from '../../common/constraint/constraint';
import {StepConstraint} from '../../common/constraint/step';
import {ComplexValue} from '../../common/model/complex-value';
import {getBoundValue} from '../../common/model/test-util';
Expand All @@ -15,6 +17,23 @@ import {createInputBindingController} from '../plugin';
import {Point3d} from './model/point-3d';
import {Point3dInputPlugin} from './plugin';

function getPoint3dConstraint(
v: InputBindingValue<unknown>,
): PointNdConstraint<Point3d> {
return (getBoundValue(v) as ComplexValue<unknown>)
.constraint as PointNdConstraint<Point3d>;
}

function getDimensionProps(c: Constraint<number>) {
const [min, max] = findNumberRange(c);
const sc = findConstraint(c, StepConstraint);
return {
max: max,
min: min,
step: sc?.step,
};
}

describe(Point3dInputPlugin.id, () => {
it('should have right number of text views', () => {
const doc = createTestWindow().document;
Expand All @@ -34,13 +53,9 @@ describe(Point3dInputPlugin.id, () => {
document: doc,
params: {z: {step: 1}},
target: new BindingTarget({foo: {x: 12, y: 34, z: 56}}, 'foo'),
});
}) as InputBindingController;

const cs = (
getBoundValue(
c?.value as InputBindingValue<unknown>,
) as ComplexValue<unknown>
).constraint as PointNdConstraint<Point3d>;
const cs = getPoint3dConstraint(c.value);
const zc = cs.components[2];
if (!zc) {
assert.fail('Unexpected constraint');
Expand All @@ -60,19 +75,69 @@ describe(Point3dInputPlugin.id, () => {
},
},
target: new BindingTarget({foo: {x: 12, y: 34, z: 56}}, 'foo'),
});
}) as InputBindingController;

const cs = (
getBoundValue(
c?.value as InputBindingValue<unknown>,
) as ComplexValue<unknown>
).constraint as PointNdConstraint<Point3d>;
const zc = cs.components[2];
if (!zc) {
assert.fail('Unexpected constraint');
}
const [min, max] = findNumberRange(zc);
assert.strictEqual(min, -123);
assert.strictEqual(max, 456);
const cs = getPoint3dConstraint(c.value);
const zp = getDimensionProps(cs.components[2] as Constraint<number>);
assert.deepStrictEqual([zp.min, zp.max], [-123, 456]);
});

[
{
params: {
params: {
...{min: -1, max: 1, step: 0.1},
x: {min: -2, max: 2, step: 0.2},
},
},
expected: [
{min: -2, max: 2, step: 0.2},
{min: -1, max: 1, step: 0.1},
{min: -1, max: 1, step: 0.1},
],
},
{
params: {
params: {
...{min: -1, max: 1, step: 0.1},
y: {min: -2, max: 2, step: 0.2},
},
},
expected: [
{min: -1, max: 1, step: 0.1},
{min: -2, max: 2, step: 0.2},
{min: -1, max: 1, step: 0.1},
],
},
{
params: {
params: {
...{min: -1, max: 1, step: 0.1},
z: {min: -2, max: 2, step: 0.2},
},
},
expected: [
{min: -1, max: 1, step: 0.1},
{min: -1, max: 1, step: 0.1},
{min: -2, max: 2, step: 0.2},
],
},
].forEach(({params, expected}) => {
describe(`when params=${JSON.stringify(params)}`, () => {
it('should propagate dimension params', () => {
const doc = createTestWindow().document;
const c = createInputBindingController(Point3dInputPlugin, {
document: doc,
params: params.params,
target: new BindingTarget({p: {x: 12, y: 34, z: 56}}, 'p'),
}) as InputBindingController;

const comps = getPoint3dConstraint(c.value).components;
[0, 1, 2].forEach((i) => {
const p = getDimensionProps(comps[i] as Constraint<number>);
assert.deepStrictEqual(p, expected[i]);
});
});
});
});
});
9 changes: 6 additions & 3 deletions packages/core/src/input-binding/point-3d/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ function createConstraint(
return new PointNdConstraint({
assembly: Point3dAssembly,
components: [
createDimensionConstraint(params.x, initialValue.x),
createDimensionConstraint(params.y, initialValue.y),
createDimensionConstraint(params.z, initialValue.z),
createDimensionConstraint({...params, ...params.x}, initialValue.x),
createDimensionConstraint({...params, ...params.y}, initialValue.y),
createDimensionConstraint({...params, ...params.z}, initialValue.z),
],
});
}
Expand Down Expand Up @@ -75,7 +75,10 @@ export const Point3dInputPlugin: InputBindingPlugin<
return null;
}
const result = parseRecord<Point3dInputParams>(params, (p) => ({
max: p.optional.number,
min: p.optional.number,
readonly: p.optional.constant(false),
step: p.optional.number,
x: p.optional.custom(parsePointDimensionParams),
y: p.optional.custom(parsePointDimensionParams),
z: p.optional.custom(parsePointDimensionParams),
Expand Down

0 comments on commit f7c2a48

Please sign in to comment.