Skip to content

Commit

Permalink
refactor(scenegraph): update toHiccup() impls
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 18, 2020
1 parent 62472f5 commit 1bdc0f1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
30 changes: 30 additions & 0 deletions packages/scenegraph/src/hiccup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { deref, IToHiccup } from "@thi.ng/api";
import { isFunction } from "@thi.ng/checks";
import type { ANode } from "./anode";

/**
* `IToHiccup` implementation Node2D/3D
*
* @param node
* @param ctx
*
* @internal
*/
export const toHiccup = <T extends ANode<T> & IToHiccup>(
node: T,
ctx?: any
) => {
const body = isFunction(node.body) ? node.body(ctx) : deref(node.body);
return node.enabled && node.display
? node.children.length
? [
"g",
{},
node.body ? ["g", { transform: node.mat }, body] : undefined,
...node.children.map((c) => c.toHiccup(ctx)),
]
: body
? ["g", { transform: node.mat }, body]
: undefined
: undefined;
};
24 changes: 6 additions & 18 deletions packages/scenegraph/src/node2.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { deref, ICopy, IToHiccup, Nullable } from "@thi.ng/api";
import { isFunction, isNumber } from "@thi.ng/checks";
import { ICopy, IToHiccup, Nullable } from "@thi.ng/api";
import { isNumber } from "@thi.ng/checks";
import { invert23, mulM23, mulV23, transform23 } from "@thi.ng/matrices";
import { ReadonlyVec, set2, Vec } from "@thi.ng/vectors";
import { ANode } from "./anode";
import type { ISceneNode } from "./api";
import { toHiccup } from "./hiccup";

export class Node2D extends ANode<Node2D>
export class Node2D
extends ANode<Node2D>
implements ICopy<Node2D>, ISceneNode<Node2D>, IToHiccup {
translate: Vec;
rotate: number;
Expand Down Expand Up @@ -70,20 +72,6 @@ export class Node2D extends ANode<Node2D>
* @param ctx - arbitrary user data
*/
toHiccup(ctx?: any): any {
const body = isFunction(this.body) ? this.body(ctx) : deref(this.body);
return this.enabled && this.display
? this.children.length
? [
"g",
{},
this.body
? ["g", { transform: this.mat }, body]
: undefined,
...this.children.map((c) => c.toHiccup(ctx)),
]
: body
? ["g", { transform: this.mat }, body]
: undefined
: undefined;
return toHiccup<Node2D>(this, ctx);
}
}
24 changes: 6 additions & 18 deletions packages/scenegraph/src/node3.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { deref, ICopy, IToHiccup, Nullable } from "@thi.ng/api";
import { isFunction, isNumber } from "@thi.ng/checks";
import { ICopy, IToHiccup, Nullable } from "@thi.ng/api";
import { isNumber } from "@thi.ng/checks";
import { invert44, mulM44, mulV344, transform44 } from "@thi.ng/matrices";
import { ReadonlyVec, set3, Vec } from "@thi.ng/vectors";
import { ANode } from "./anode";
import type { ISceneNode } from "./api";
import { toHiccup } from "./hiccup";

export class Node3D extends ANode<Node3D>
export class Node3D
extends ANode<Node3D>
implements ICopy<Node3D>, ISceneNode<Node3D>, IToHiccup {
translate: Vec;
rotate: Vec;
Expand Down Expand Up @@ -69,20 +71,6 @@ export class Node3D extends ANode<Node3D>
* @param ctx - arbitrary user data
*/
toHiccup(ctx?: any): any {
const body = isFunction(this.body) ? this.body(ctx) : deref(this.body);
return this.enabled && this.display
? this.children.length
? [
"g",
{},
this.body
? ["g", { transform: this.mat }, body]
: undefined,
...this.children.map((c) => c.toHiccup(ctx)),
]
: body
? ["g", { transform: this.mat }, body]
: undefined
: undefined;
return toHiccup<Node3D>(this, ctx);
}
}

0 comments on commit 1bdc0f1

Please sign in to comment.