Skip to content

Commit

Permalink
Merge pull request #37 from jpmorganchase/bind_types
Browse files Browse the repository at this point in the history
types and code hints for bind and create
  • Loading branch information
dsaffo authored Apr 2, 2024
2 parents 863ad07 + 05b77e8 commit 7fb9326
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 11 deletions.
1 change: 1 addition & 0 deletions anu-examples/examples/FirstSteps/Box.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export const box = function(engine){
camera.attachControl(true)

let box = anu.create('box', 'ourBox', {size: 2}, [{count: 2}]);

return scene;
};
2 changes: 1 addition & 1 deletion anu-examples/examples/FirstSteps/Box_With_Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const box_data = function(engine){
{
height: (d) => d.goals,
width: (d) => d.assits,
depth: (d) => d.points
depth: (d) => d.points,
},
{goals: 5, assits: 10, points: 2})

Expand Down
1 change: 0 additions & 1 deletion anu-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"@babylonjs/core": "^6.46.0",
"@babylonjs/gui": "^6.46.0",
"@babylonjs/inspector": "^6.12.3",
"@jpmorganchase/anu": "^0.0.43",
"d3": "^7.6.1",
"d3-force-3d": "^3.0.5",
"simplify-3d": "^1.0.0",
Expand Down
Empty file.
6 changes: 4 additions & 2 deletions src/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

import { Node, ActionManager, Tags, Mesh, Scene, InstancedMesh } from '@babylonjs/core';
import { Selection } from './index';
import { create } from './create';
import { create, MeshTypes } from './create';

type Property<T, K extends keyof T> = T[K];

/**
* Take a shape type, a scene, and data. For each index in the data create a new mesh for each node in the selection as the parent.
Expand All @@ -16,7 +18,7 @@ import { create } from './create';
* @returns An instance of Selection, a class containing a array of selected nodes, the scene, and the functions of the class Selection,
* or undefined if a selection could not be made.
*/
export function bind(shape: string, options?: object, data: Array<object> = [{}], scene?: Scene): Selection {
export function bind<K extends keyof MeshTypes>(shape: K, options?: Property<MeshTypes, K>, data: Array<object> = [{}], scene?: Scene): Selection {
let meshes: Node[] = [];
data.forEach((element, i) => {
var mesh = create(shape, shape, options, element, scene);
Expand Down
45 changes: 40 additions & 5 deletions src/create.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright : J.P. Morgan Chase & Co.

import { Mesh, MeshBuilder, TransformNode, Scene, Nullable, ActionManager, Tags, CreateGreasedLine, GreasedLineMeshBuilderOptions, Node, BoundingInfo, Observable, AbstractMesh } from '@babylonjs/core';
import { Mesh, MeshBuilder, TransformNode, Scene, ActionManager, Tags, CreateGreasedLine, GreasedLineMeshBuilderOptions } from '@babylonjs/core';
import { createPlaneText } from './prefabs/Text/planeText';
import { createContainer } from './prefabs/Misc/container';

Expand Down Expand Up @@ -55,6 +55,41 @@ const meshList: StringByFunc = {
container: createContainer,
};

export interface MeshTypes {
"box": Parameters<typeof MeshBuilder.CreateBox>[1],
"cot": any,
"sphere": Parameters<typeof MeshBuilder.CreateSphere>[1],
"tiledBox": Parameters<typeof MeshBuilder.CreateTiledBox>[1],
"cylinder": Parameters<typeof MeshBuilder.CreateCylinder>[1],
"capsule": Parameters<typeof MeshBuilder.CreateCapsule>[1],
"plane": Parameters<typeof MeshBuilder.CreatePlane>[1],
"tiledPlane": Parameters<typeof MeshBuilder.CreateTiledPlane>[1],
"disc": Parameters<typeof MeshBuilder.CreateDisc>[1],
"torus": Parameters<typeof MeshBuilder.CreateTorus>[1],
"torusKnot": Parameters<typeof MeshBuilder.CreateTorusKnot>[1],
"ground": Parameters<typeof MeshBuilder.CreateGround>[1],
"tiledGround": Parameters<typeof MeshBuilder.CreateTiledGround>[1],
"lines": Parameters<typeof MeshBuilder.CreateLines>[1],
"dashedLines": Parameters<typeof MeshBuilder.CreateDashedLines>[1],
"lineSystem": Parameters<typeof MeshBuilder.CreateLineSystem>[1],
"ribbon": Parameters<typeof MeshBuilder.CreateRibbon>[1],
"tube": Parameters<typeof MeshBuilder.CreateTube>[1],
"extrude": Parameters<typeof MeshBuilder.ExtrudeShape>[1],
"extrudeCustom": Parameters<typeof MeshBuilder.ExtrudeShapeCustom>[1],
"lathe": Parameters<typeof MeshBuilder.CreateLathe>[1],
"polygon": Parameters<typeof MeshBuilder.CreatePolygon>[1],
"extrudePolygon": Parameters<typeof MeshBuilder.ExtrudePolygon>[1],
"polyhedra": Parameters<typeof MeshBuilder.CreatePolyhedron>[1],
"icosphere": Parameters<typeof MeshBuilder.CreateIcoSphere>[1],
"geodesic": Parameters<typeof MeshBuilder.CreateGeodesic>[1],
"goldberg": Parameters<typeof MeshBuilder.CreateGoldberg>[1],
'planeText': Parameters<typeof createPlaneText>[1],
"greasedLine": Parameters<typeof createGL>[1],
"container": Parameters<typeof createContainer>[1],
}

type Property<T, K extends keyof T> = T[K];

/**
* Helper function to build meshes of a specified type with options optionally set with functions and data.
*
Expand All @@ -65,17 +100,17 @@ const meshList: StringByFunc = {
* @param scene The scene to create the mesh in.
* @returns A mesh object created with the passed parameters.
*/
export function create(
shape: string,
export function create<K extends keyof MeshTypes>(
shape: K,
name: string,
options: object = {},
options: Property<MeshTypes, K> = {},
data: object = {},
scene?: Scene
): Mesh {
let executedOptions: StringByAny = {};

for (let [key, value] of Object.entries(options)) {
value instanceof Function ? (executedOptions[key] = value(data)) : (executedOptions[key] = value);
value instanceof Function ? (executedOptions[key] = (value as Function)(data)) : (executedOptions[key] = value);
}

let builder: Function = meshList[shape];
Expand Down
6 changes: 4 additions & 2 deletions src/selection/bind/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

import { Node, ActionManager, Tags, Mesh, InstancedMesh } from '@babylonjs/core';
import { Selection } from '../index';
import { create } from '../../create';
import { create, MeshTypes } from '../../create';

type Property<T, K extends keyof T> = T[K];

/**
* Take a selection, a shape type, and data. For each index in the data create a new mesh for each node in the selection as the parent.
Expand All @@ -15,7 +17,7 @@ import { create } from '../../create';
* @returns An instance of Selection, a class containing a array of selected nodes, the scene, and the functions of the class Selection,
* or undefined if a selection could not be made.
*/
export function bind(this: Selection, shape: string, options: object = {}, data: Array<object> = [{}]): Selection {
export function bind<K extends keyof MeshTypes>(this: Selection, shape: K, options: Property<MeshTypes, K> = {}, data: Array<object> = [{}]): Selection {
let meshes: Node[] = [];
this.selected.forEach((node) => {
data.forEach((element, i) => {
Expand Down

0 comments on commit 7fb9326

Please sign in to comment.