Skip to content

Commit

Permalink
Localized palette edits.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Sep 17, 2023
1 parent 7a0fcac commit 0dac9ab
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 111 deletions.
4 changes: 2 additions & 2 deletions src/components/output/OutputView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import Pointer from '../../input/Pointer';
import Place from '../../output/Place';
import moveOutput, { addStageContent } from '../palette/editOutput';
import { getPlace } from '../../output/getPlace';
import { getOrCreatePlace } from '../../output/getOrCreatePlace';
import { SvelteComponent, afterUpdate, beforeUpdate } from 'svelte';
import Placement from '../../input/Placement';
import { toExpression } from '../../parser/parseExpression';
Expand Down Expand Up @@ -446,7 +446,7 @@
? renderedFocus
: // If there's selected output, it's the first output selected, and it has a place
$selectedOutput && $selectedOutput.length > 0
? getPlace(
? getOrCreatePlace(
project,
$locale,
$selectedOutput[0],
Expand Down
20 changes: 14 additions & 6 deletions src/components/palette/BindColor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import type OutputProperty from '../../edit/OutputProperty';
import { getProject, getSelectedOutput } from '../project/Contexts';
import { Projects } from '../../db/Database';
import type Bind from '../../nodes/Bind';
export let property: OutputProperty;
export let values: OutputPropertyValueSet;
Expand All @@ -17,9 +18,15 @@
let project = getProject();
let selectedOutput = getSelectedOutput();
$: lightness = getColorValue('lightness') ?? 0;
$: chroma = getColorValue('chroma') ?? 0;
$: hue = getColorValue('hue') ?? 0;
$: lightness = $project
? getColorValue($project.shares.output.Color.inputs[0]) ?? 0
: 0;
$: chroma = $project
? getColorValue($project.shares.output.Color.inputs[1]) ?? 0
: 0;
$: hue = $project
? getColorValue($project.shares.output.Color.inputs[1]) ?? 0
: 0;
// Whenever the slider value changes, revise the Evaluates to match the new value.
function handleChange(l: number, c: number, h: number) {
Expand Down Expand Up @@ -51,19 +58,20 @@
);
}
function getColorValue(name: string) {
function getColorValue(bind: Bind) {
if ($project === undefined) return undefined;
// The value of this facet on every value selected.
const facets = values.values.map((val) => {
if ($project && val.expression instanceof Evaluate) {
const mapping = val.expression.getMappingFor(
name,
bind,
$project.getNodeContext(val.expression)
);
const number =
mapping && mapping.given instanceof NumberLiteral
? mapping.given.getValue().toNumber() *
(name === 'lightness' && mapping.given.isPercent()
(bind === $project.shares.output.Color.inputs[0] &&
mapping.given.isPercent()
? 0.01
: 1)
: undefined;
Expand Down
12 changes: 6 additions & 6 deletions src/components/palette/PlaceEditor.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script lang="ts">
import { getFirstName } from '@locale/Locale';
import TextField from '../widgets/TextField.svelte';
import Evaluate from '../../nodes/Evaluate';
import type Project from '@models/Project';
Expand All @@ -12,6 +11,7 @@
import { Projects, locale, locales } from '../../db/Database';
import { tick } from 'svelte';
import Button from '../widgets/Button.svelte';
import type Bind from '../../nodes/Bind';
export let project: Project;
export let place: Evaluate;
Expand All @@ -25,7 +25,7 @@
return !num.isNaN();
}
async function handleChange(dimension: string, value: string) {
async function handleChange(dimension: Bind, value: string) {
if (place === undefined) return;
if (value.length > 0 && !valid(value)) return;
Expand Down Expand Up @@ -56,11 +56,11 @@
</script>

<div class="place">
{project.shares.output.Place.names.getSymbolicName()}{#each [getFirstName($locale.output.Place.x.names), getFirstName($locale.output.Place.y.names), getFirstName($locale.output.Place.z.names)] as dimension, index}
{@const given = place?.getMappingFor(
{project.shares.output.Place.names.getSymbolicName()}{#each project.shares.output.Place.inputs as dimension, index}
{@const given = place?.getInput(
dimension,
project.getNodeContext(place)
)?.given}
)}
<!-- Get the measurement literal, if there is one -->
{@const value =
given instanceof Expression ? getNumber(given) : undefined}
Expand All @@ -70,7 +70,7 @@
text={`${value}`}
validator={valid}
{editable}
placeholder={getFirstName(dimension)}
placeholder={dimension.names.getNames()[0]}
description={$locale.ui.palette.field.coordinate}
changed={(value) => handleChange(dimension, value)}
bind:view={views[index]}
Expand Down
15 changes: 5 additions & 10 deletions src/components/palette/VelocityEditor.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script lang="ts">
import { getFirstName } from '@locale/Locale';
import TextField from '../widgets/TextField.svelte';
import type Evaluate from '../../nodes/Evaluate';
import type Project from '@models/Project';
Expand All @@ -11,6 +10,7 @@
import Expression from '../../nodes/Expression';
import { Projects, locale, locales } from '../../db/Database';
import { tick } from 'svelte';
import type Bind from '../../nodes/Bind';
export let project: Project;
export let velocity: Evaluate;
Expand All @@ -23,11 +23,7 @@
return !num.isNaN();
}
async function handleChange(
dimension: string,
index: number,
value: string
) {
async function handleChange(dimension: Bind, index: number, value: string) {
if (velocity === undefined) return;
if (value.length > 0 && !valid(value)) return;
Expand Down Expand Up @@ -58,12 +54,11 @@
</script>

<div class="place">
{project.shares.output.Velocity.names.getSymbolicName()}{#each project.shares.output.Velocity.inputs.map( (input) => input.getPreferredName($locales) ) as dimension, index}
{@const mapping = velocity?.getMappingFor(
{project.shares.output.Velocity.names.getSymbolicName()}{#each project.shares.output.Velocity.inputs as dimension, index}
{@const given = velocity?.getInput(
dimension,
project.getNodeContext(velocity)
)}
{@const given = mapping?.given}
<!-- Get the measurement literal, if there is one -->
{@const value =
given instanceof Expression ? getNumber(given) : undefined}
Expand All @@ -73,7 +68,7 @@
text={`${value}`}
validator={valid}
{editable}
placeholder={getFirstName(dimension)}
placeholder={dimension.names.getNames()[0]}
description={$locale.ui.palette.field.coordinate}
changed={(value) => handleChange(dimension, index, value)}
bind:view={views[index]}
Expand Down
127 changes: 73 additions & 54 deletions src/components/palette/editOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
STAGE_SYMBOL,
} from '../../parser/Symbols';
import { toExpression } from '../../parser/parseExpression';
import { getPlaceExpression } from '../../output/getOrCreatePlace';

export function getNumber(given: Expression): number | undefined {
const measurement =
Expand Down Expand Up @@ -54,65 +55,83 @@ export default function moveOutput(
evaluates.map((evaluate) => {
const ctx = project.getNodeContext(evaluate);

const given = evaluate.getMappingFor('place', ctx);
const given = getPlaceExpression(project, evaluate, ctx);
const place =
given &&
given.given instanceof Evaluate &&
given.given.is(PlaceType, ctx)
? given.given
: given &&
given.given instanceof Bind &&
given.given.value instanceof Evaluate &&
given.given.value.is(PlaceType, ctx)
? given.given.value
given instanceof Evaluate && given.is(PlaceType, ctx)
? given
: undefined;

const x = place?.getMappingFor('x', ctx)?.given;
const y = place?.getMappingFor('y', ctx)?.given;
const z = place?.getMappingFor('z', ctx)?.given;
const x = place?.getInput(
project.shares.output.Place.inputs[0],
ctx
);
const y = place?.getInput(
project.shares.output.Place.inputs[1],
ctx
);
const z = place?.getInput(
project.shares.output.Place.inputs[2],
ctx
);

const xValue = x instanceof Expression ? getNumber(x) : undefined;
const yValue = y instanceof Expression ? getNumber(y) : undefined;
const zValue = z instanceof Expression ? getNumber(z) : undefined;

const bind = evaluate.is(project.shares.output.Phrase, ctx)
? project.shares.output.Phrase.inputs[3]
: evaluate.is(project.shares.output.Group, ctx)
? project.shares.output.Phrase.inputs[4]
: undefined;

return [
evaluate,
evaluate.withBindAs(
'place',
Evaluate.make(
Reference.make(
PlaceType.names.getPreferredNameString(locales),
PlaceType
),
[
// If coordinate is computed, and not a literal, don't change it.
x instanceof Expression && xValue === undefined
? x
: NumberLiteral.make(
relative
? new Decimal(xValue ?? 0)
.add(horizontal)
.toNumber()
: horizontal,
Unit.create(['m'])
),
y instanceof Expression && yValue === undefined
? y
: NumberLiteral.make(
relative
? new Decimal(yValue ?? 0)
.add(vertical)
.toNumber()
: vertical,
Unit.create(['m'])
bind === undefined
? evaluate
: evaluate.withBindAs(
bind,
Evaluate.make(
Reference.make(
PlaceType.names.getPreferredNameString(
locales
),
z instanceof Expression && zValue !== undefined
? z
: NumberLiteral.make(0, Unit.create(['m'])),
]
),
ctx
),
PlaceType
),
[
// If coordinate is computed, and not a literal, don't change it.
x instanceof Expression &&
xValue === undefined
? x
: NumberLiteral.make(
relative
? new Decimal(xValue ?? 0)
.add(horizontal)
.toNumber()
: horizontal,
Unit.create(['m'])
),
y instanceof Expression &&
yValue === undefined
? y
: NumberLiteral.make(
relative
? new Decimal(yValue ?? 0)
.add(vertical)
.toNumber()
: vertical,
Unit.create(['m'])
),
z instanceof Expression &&
zValue !== undefined
? z
: NumberLiteral.make(
0,
Unit.create(['m'])
),
]
),
ctx
),
];
})
);
Expand Down Expand Up @@ -231,12 +250,12 @@ export function addStageContent(

if (stage) {
const context = project.getNodeContext(stage);
const list = stage.getExpressionFor(
StageType.inputs[0].getNames()[0],
context
);
if (list instanceof ListLiteral) {
reviseContent(database, project, list, [...list.values, content]);
const content = stage.getInput(StageType.inputs[0], context);
if (content instanceof ListLiteral) {
reviseContent(database, project, content, [
...content.values,
content,
]);
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/edit/OutputExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,18 @@ export default class OutputExpression {
}

withPropertyUnset(name: string): Evaluate {
return this.node.withBindAs(
name,
undefined,
this.project.getNodeContext(this.node)
);
// Find the bind corresponding to the given name.

const context = this.project.getNodeContext(this.node);
const fun = this.node.getFunction(context);
const bind = fun?.inputs.find((bind) => bind.hasName(name));

return bind
? this.node.withBindAs(
bind,
undefined,
this.project.getNodeContext(this.node)
)
: this.node;
}
}
24 changes: 16 additions & 8 deletions src/models/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,14 +806,22 @@ export default class Project {
name: string,
value: Expression | undefined
): [Evaluate, Evaluate | undefined][] {
return evaluates.map((evaluate) => [
evaluate,
evaluate.withBindAs(
name,
value?.clone(),
this.getNodeContext(evaluate)
),
]);
return evaluates.map((evaluate) => {
// Find the bind corresponding to the name.
const context = this.getNodeContext(evaluate);
const fun = evaluate.getFunction(context);
const bind = fun?.inputs.find((bind) => bind.hasName(name));
return bind
? [
evaluate,
evaluate.withBindAs(
bind,
value?.clone(),
this.getNodeContext(evaluate)
),
]
: [evaluate, evaluate];
});
}

/** Get all the languages used in the project */
Expand Down
Loading

0 comments on commit 0dac9ab

Please sign in to comment.