Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support folders in Parameters #268

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@
"homepage": "https://awatif.co",
"devDependencies": {
"@storybook/html-vite": "^7.1.1",
"@tweakpane/core": "^1.1.9",
"@tweakpane/core": "^2.0.1",
"@types/jest": "^29.5.3",
"@types/three": "^0.154.0",
"autoprefixer": "^10.4.14",
"chromatic": "^6.20.0",
"daisyui": "^3.5.0",
"jest": "^29.6.2",
"postcss": "^8.4.27",
"storybook": "^7.1.1",
"tailwindcss": "^3.3.3",
"ts-jest": "^29.1.1",
"vite": "^4.4.7",
"vite-plugin-solid": "^2.7.0",
"jest": "^29.6.2",
"ts-jest": "^29.1.1"
"vite-plugin-solid": "^2.7.0"
},
"dependencies": {
"@supabase/supabase-js": "^2.31.0",
"mathjs": "^11.9.1",
"monaco-editor": "^0.40.0",
"solid-js": "^1.7.8",
"three": "^0.155.0",
"tweakpane": "^3.1.10"
"tweakpane": "^4.0.1"
}
}
4 changes: 3 additions & 1 deletion src/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ export const analysisResults = analyzing(nodes, elements, assignments);`;

<Parameters
parameters={parameters()}
onChange={(e) => solveModel({ key: e.presetKey || "", value: e.value })}
onChange={(e) =>
solveModel({ key: (e.target as any).tag, value: e.value })
}
/>
</Layouter>
);
Expand Down
106 changes: 82 additions & 24 deletions src/Parameters/Parameters.stories-dev.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,88 @@
import { Meta, StoryObj } from "@storybook/html";
import { Parameters } from "./Parameters";
import { ComponentProps } from "solid-js";

export const Default: StoryObj = {};
type Args = ComponentProps<typeof Parameters>;

export const Empty: StoryObj = {
args: {
parameters: {},
},
};

export const Default: StoryObj = {
args: {
parameters: {
width: {
value: 5,
min: 1,
max: 10,
step: 1,
},
height: {
value: 50,
min: 0,
max: 100,
step: 5,
label: "another height",
},
},
},
};

export const WithFolders: StoryObj<Args> = {
args: {
parameters: {
"folder-1/width": {
value: 5,
min: 1,
max: 10,
step: 1,
},
"folder-1/height": {
value: 50,
min: 0,
max: 100,
step: 5,
label: "another height",
},
"folder-2/width": {
value: 9,
min: 1,
max: 10,
step: 1,
},
},
},
};

export const WithFoldersAndWithout: StoryObj<Args> = {
args: {
parameters: {
"folder-1/width": {
value: 5,
min: 1,
max: 10,
step: 1,
},
width: {
value: 9,
min: 1,
max: 10,
step: 1,
},
"folder-2/height": {
value: 50,
min: 0,
max: 100,
step: 5,
label: "another height",
},
},
},
};

export default {
title: "Parameters",
render: () => (
<Parameters
parameters={{
width: {
value: 5,
min: 1,
max: 10,
step: 1,
},
height: {
value: 50,
min: 0,
max: 100,
step: 5,
label: "another height",
},
toggle: {
value: true,
},
}}
/>
),
} as Meta;
render: (props) => <Parameters {...props} />,
} as Meta<Args>;
71 changes: 48 additions & 23 deletions src/Parameters/Parameters.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,71 @@
import { createEffect } from "solid-js";
import { Pane, TpChangeEvent } from "tweakpane";
import { FolderApi, Pane, TpChangeEvent } from "tweakpane";

type Parameter = {
value: unknown;
value: number;
min?: number;
max?: number;
step?: number;
label?: string;
};
export type ParametersType = Record<string, Parameter>;

export type SettingsProps = {
type ParametersProps = {
parameters: ParametersType;
onChange?: (ev: TpChangeEvent<unknown>) => void;
onChange?: (e: TpChangeEvent<unknown>) => void;
};

export function Parameters(props: SettingsProps) {
export function Parameters(props: ParametersProps) {
let pane = new Pane({ title: "Parameters" });
let inputs: any[] = [];
const folders = new Map<string, FolderApi>();

createEffect(() => {
pane.hidden = Object.keys(props.parameters).length == 0 ? true : false;

inputs.forEach((i) => pane.remove(i));

Object.keys(props.parameters).forEach((key) => {
const parameter = props.parameters[key];
inputs.push(
pane.addInput(parameter, "value", {
min: parameter.min,
max: parameter.max,
step: parameter.step,
label: parameter.label || key,
presetKey: key,
})
);
const parameters = props.parameters;

pane.hidden = Object.keys(parameters).length === 0;

pane.children.forEach((c) => pane.remove(c));
folders.clear();

const tweakParams = convertToTweakParams(parameters);
Object.entries(parameters).forEach(([key, parameter]) => {
const bindingOptions = {
min: parameter.min,
max: parameter.max,
step: parameter.step,
label: parameter.label || key,
tag: key,
};

const folderKey = getFolderKey(key);
if (folderKey) {
if (!folders.get(folderKey))
folders.set(folderKey, pane.addFolder({ title: folderKey }));

folders.get(folderKey)?.addBinding(tweakParams, key, bindingOptions);
} else {
pane.addBinding(tweakParams, key, bindingOptions);
}
});
});

pane.on("change", (ev) => {
if (props.onChange) props.onChange(ev);
pane.on("change", (e) => {
if (props.onChange) props.onChange(e);
});

return <div class="absolute bottom-0 right-5 w-[19rem]">{pane.element}</div>;
}

const convertToTweakParams = (parameters: ParametersType) =>
Object.entries(parameters).reduce(
(tweakParams: Record<string, number>, [key, parameter]) => {
tweakParams[key] = parameter.value;
return tweakParams;
},
{}
);

const getFolderKey = (key: string) => {
const splitList = key.split("/");
return splitList.length === 1 ? undefined : splitList[0];
};
26 changes: 13 additions & 13 deletions src/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@ export type SettingsProps = {
export function Settings(props: SettingsProps) {
let pane = new Pane({ title: "Settings", expanded: false });

pane.addInput(props.settings, "gridSize", { label: "grid size", min: 1 });
pane.addInput(props.settings, "displayScale", {
pane.addBinding(props.settings, "gridSize", { label: "grid size", min: 1 });
pane.addBinding(props.settings, "displayScale", {
label: "display scale",
min: -10,
max: 10,
step: 1,
});
pane.addInput(props.settings, "nodes");
pane.addInput(props.settings, "elements");
pane.addInput(props.settings, "nodesIndices", { label: "nodes indices" });
pane.addInput(props.settings, "elementsIndices", {
pane.addBinding(props.settings, "nodes");
pane.addBinding(props.settings, "elements");
pane.addBinding(props.settings, "nodesIndices", { label: "nodes indices" });
pane.addBinding(props.settings, "elementsIndices", {
label: "elements indices",
});
pane.addInput(props.settings, "supports");
pane.addInput(props.settings, "loads");
pane.addInput(props.settings, "deformedShape", { label: "deformed shape" });
pane.addInput(props.settings, "elementResults", {
pane.addBinding(props.settings, "supports");
pane.addBinding(props.settings, "loads");
pane.addBinding(props.settings, "deformedShape", { label: "deformed shape" });
pane.addBinding(props.settings, "elementResults", {
options: {
none: "none",
normal: "normal",
},
label: "element results",
});
pane.addInput(props.settings, "nodeResults", {
pane.addBinding(props.settings, "nodeResults", {
options: {
none: "none",
deformation: "deformation",
Expand All @@ -56,8 +56,8 @@ export function Settings(props: SettingsProps) {
label: "node results",
});

pane.on("change", (ev) => {
if (props.onChange) props.onChange(ev);
pane.on("change", (e) => {
if (props.onChange) props.onChange(e);
});

createEffect(() => {
Expand Down