Skip to content

Commit

Permalink
Implement for loop in trace
Browse files Browse the repository at this point in the history
  • Loading branch information
spaaaacccee committed Nov 8, 2023
1 parent 1a154f6 commit ee79ee0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
34 changes: 27 additions & 7 deletions client/src/components/renderer/parser/parse.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { Dictionary as Dict } from "lodash";
import { Dictionary as Dict, flatMap, range } from "lodash";
import {
ComponentDefinition,
ComponentDefinitionMap,
IntrinsicComponentMap,
ParsedComponent,
ParsedComponentDefinition,
TraceComponent,
} from "protocol";
import { applyScope } from "./applyScope";
import { Context, Key } from "./Context";
import { normalize } from "./normalize";

function parseFor(component: TraceComponent<string, Dict<any>>) {
const { $for, ...rest } = component;
if ($for) {
const { $let = "i", $from = 0, $to = 1, $step = 1 } = $for;
return range($from, $to, $step).map((i) =>
applyScope(normalize({ [$let]: i }), normalize(rest as any))
);
} else {
return [component];
}
}

/**
* A parser for a list of Components
* @param definition a list of Components
Expand All @@ -20,11 +34,17 @@ export function parse<T extends IntrinsicComponentMap>(
components: ComponentDefinitionMap,
context: Context<T[Key<T>]> = {}
): ParsedComponentDefinition<T> {
return definition.flatMap((component) => {
const { $ } = component;
const scoped = applyScope(normalize(context), normalize(component) as any);
return $ in components
? parse(components[$], components, scoped)
: [scoped as ParsedComponent<Key<T>, T[Key<T>]>];
return definition.flatMap((c) => {
const { $ } = c;
const c2 = parseFor(c);
return flatMap(c2, (component) => {
const scoped = applyScope(
normalize(context),
normalize(component) as any
);
return $ in components
? parse(components[$], components, scoped)
: [scoped as ParsedComponent<Key<T>, T[Key<T>]>];
});
});
}
2 changes: 1 addition & 1 deletion internal-renderers/src/d2-renderer/D2RendererOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const defaultD2RendererOptions: D2RendererOptions = {
height: 64,
},
tileSubdivision: 0,
refreshInterval: 1000 / 60,
refreshInterval: 1000 / 30,
animationDuration: 150,
debounceInterval: 75,
backgroundColor: "#ffffff",
Expand Down
3 changes: 1 addition & 2 deletions resources/traces/v1.0.5/grid-astar.trace.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"version": "1.0.5",
"render": {
"context": {},
"components": {
"tile": [
{
Expand All @@ -15,7 +14,7 @@
]
},
"views": {
"main": { "renderer": "2d-pixi", "components": [{ "$": "tile" }] }
"main": { "components": [{ "$": "tile" }] }
},
"path": {
"pivot": { "x": "{{$.event.x + 0.5}}", "y": "{{$.event.y + 0.5}}" },
Expand Down
24 changes: 24 additions & 0 deletions resources/traces/v1.0.5/loop.trace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: 1.0.5
render:
components:
tile:
- $: rect
width: 1
height: 1
x: "{{$.event.x + $.i}}"
y: "{{$.event.y}}"
fill: "{{$.color[$.event.type]}}"
$for:
$from: 0 # Optional, default 0
$to: 10 # Optional, default 1
$step: 2 # Optional, default 1
$let: i # Optional, default "i"
views:
main:
components:
- $: tile
events:
- type: source
id: 1736
x: 8
y: 15
6 changes: 1 addition & 5 deletions resources/traces/v1.0.5/tile.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@
]
},
"views": {
"main": { "renderer": "2d-pixi", "components": [{ "$": "tileboard" }] },
"someOtherView": {
"renderer": "2d-pixi",
"components": [{ "$": "tileboard" }]
}
"main": { "components": [{ "$": "tileboard" }] }
}
},
"events": [
Expand Down

0 comments on commit ee79ee0

Please sign in to comment.