Skip to content

Commit

Permalink
Add for each node
Browse files Browse the repository at this point in the history
  • Loading branch information
grifdail committed Nov 8, 2024
1 parent 510012e commit 2054035
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 70 deletions.
8 changes: 4 additions & 4 deletions src/Hooks/useTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,15 @@ export const useTree = create<TreeStore>()(
newNodeData.graph = get().editedGraph;
newNodeData.label = name;
ports.forEach((element, index) => {
newNodeData.dataOutputs[index.toString()] = {
id: index.toString(),
label: element.label,
newNodeData.dataOutputs[element.id] = {
id: element.id,
label: element.label || element.id,
type: element.type,
defaultValue: createDefaultValue(element.type),
};
});
newNodeData.pairedNode = pairedNode;
newNodeData.settings.blackboardData = ports;
newNodeData.settings.blackboardData = Object.fromEntries(ports.map((port) => [port.id, port]));
console.log(newNodeData);
set((state) => ({ nodes: { ...state.nodes, [newNodeData.id]: newNodeData } }));
},
Expand Down
58 changes: 0 additions & 58 deletions src/Nodes/Array/Closest.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/Nodes/Array/FindBest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const createIndexNode = ({ id, positionX, positionY, selectedType }: NodeData):
{
key: `${id}-index`,
type: "number",
label: "index",
id: "index",
},
{
key: `${id}-value`,
type: selectedType,
label: "value",
id: "value",
},
],
"Find best index",
Expand Down
3 changes: 2 additions & 1 deletion src/Nodes/Array/GenerateArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const createIndexNode = ({ id, positionX, positionY }: NodeData): void => {
{
key: `${id}-index`,
type: "number",
label: "index",
id: "index",
},
],
"Generate Array index",
Expand Down Expand Up @@ -51,6 +51,7 @@ export const GenerateArray: NodeDefinition = {
],
canBeExecuted: false,
availableTypes: CommonTypes,
defaultType: "number",
onChangeType: changeTypeGenerator(["value"], [], [], ["array"]),
contextMenu: {
"Create the index node": createIndexNode,
Expand Down
4 changes: 2 additions & 2 deletions src/Nodes/Nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ import { RegularMaterial } from "./3D/RegularMaterial";
import { Render3D } from "./3D/Render3D";
import { TextureMaterial } from "./3D/TextureMaterial";
import { WireframeMaterial } from "./3D/WireframeMaterial";
import { Closest } from "./Array/Closest";
import { Count } from "./Array/Count";
import { FindBest } from "./Array/FindBest";
import { GenerateArray } from "./Array/GenerateArray";
Expand Down Expand Up @@ -162,6 +161,7 @@ import { Sqrt } from "./Math/Sqrt";
import { Step } from "./Math/Step";
import { ShaderMaterial } from "./Shaders/ShaderMaterial";
import { Blackboard } from "./System/Blackboard";
import { ForEachNode } from "./System/ForEach";
import { CrossProduct } from "./Vector/CrossProduct";
import { FocalLength } from "./Vector/FocalLength";
import { ScaleAdd } from "./Vector/ScaleAdd";
Expand Down Expand Up @@ -378,10 +378,10 @@ export const Nodes: Array<NodeDefinition> = [
StaticArray,
Count,
Sum,
Closest,
FindBest,
Slice,
GenerateArray,
ForEachNode,

//Misc
AnimationCurve,
Expand Down
3 changes: 2 additions & 1 deletion src/Nodes/System/Blackboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { PortType } from "../../Types/PortType";

export type BlackboardPortData = {
key: string;
label: string;
label?: string;
type: PortType;
id: string;
};

export const Blackboard: NodeDefinition = {
Expand Down
40 changes: 40 additions & 0 deletions src/Nodes/System/ForEach.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { IconAssembly } from "@tabler/icons-react";
import { NodeDefinition } from "../../Types/NodeDefinition";
import { CommonTypes, PortType } from "../../Types/PortType";
import { changeTypeGenerator } from "../../Utils/changeTypeGenerator";

export const ForEachNode: NodeDefinition = {
id: "ForEach",
description: "Execute an instruction for each element of an array",
featureLevel: 10,
icon: IconAssembly,
tags: ["Control", "Array"],
dataInputs: [{ id: "array", type: "array-number", defaultValue: [] }],
dataOutputs: [
{ id: "value", type: "number", defaultValue: 0 },
{ id: "index", type: "number", defaultValue: 10 },
],
executeOutputs: ["loop"],
settings: [],
canBeExecuted: true,
availableTypes: CommonTypes,
defaultType: "number",
onChangeType: changeTypeGenerator([], ["value"], ["array"]),
getData: (portId, nodeData, context) => {
if (portId === "value") {
return context.blackboard[`${nodeData.id}-value`] || 0;
} else {
return context.blackboard[`${nodeData.id}-index`] || 0;
}
},
execute: (data, context) => {
var array = context.getInputValue(data, "array", `array-${data.selectedType}` as PortType) as any[];
for (var i = 0; i < array.length; i++) {
context.blackboard[`${data.id}-index`] = i;
context.blackboard[`${data.id}-value`] = array[i];
if (data.execOutputs.loop) {
context.execute(data.execOutputs.loop);
}
}
},
};
4 changes: 3 additions & 1 deletion src/Types/NodeDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export type ContextMenuData = {
[key: string]: (node: NodeData, tree: TreeStore) => void;
};

export type PortChangeFunction = (node: NodeData, type: PortType) => void;

export type NodeDefinition = {
hideInLibrary?: boolean;
IsUnique?: boolean;
Expand All @@ -37,7 +39,7 @@ export type NodeDefinition = {
onSettingChange?: (node: NodeData, settingId: string, value: any, tree: TreeStore) => void;
availableTypes?: PortType[];
defaultType?: PortType;
onChangeType?: (node: NodeData, type: PortType) => void;
onChangeType?: PortChangeFunction;
onCreate?: (node: NodeData) => void;
featureLevel?: number;
};
Expand Down
10 changes: 9 additions & 1 deletion src/Utils/changeTypeGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useTree } from "../Hooks/useTree";
import { NodeData } from "../Types/NodeData";
import { PortChangeFunction } from "../Types/NodeDefinition";
import { PortType } from "../Types/PortType";
import { convertTypeValue } from "./convertTypeValue";

export function changeTypeGenerator(inputs: string[], outputs: string[], arrayInput: string[] = [], arrayOutput: string[] = []) {
export function changeTypeGenerator(inputs: string[], outputs: string[], arrayInput: string[] = [], arrayOutput: string[] = [], blackboard?: PortChangeFunction): PortChangeFunction {
return (node: NodeData, type: PortType) => {
inputs.forEach((key) => {
node.dataInputs[key].ownValue = convertTypeValue(node.dataInputs[key].ownValue, node.dataInputs[key].type, type);
Expand All @@ -18,5 +20,11 @@ export function changeTypeGenerator(inputs: string[], outputs: string[], arrayIn
arrayOutput.forEach((key) => {
node.dataOutputs[key].type = `array-${type}` as PortType;
});
if (blackboard) {
var tree = useTree.getState();
Object.values(tree.nodes)
.filter((n) => n.pairedNode === node.id)
.forEach((node) => tree.dangerouselyUpdateNode(node.id, () => blackboard(node, type)));
}
};
}

0 comments on commit 2054035

Please sign in to comment.