this repo has no description
1import { NodeDefinition } from "../Nodes/Nodes";
2
3export class Node{
4 name: string;
5 id: string;
6 typeId: string;
7 x: number;
8 y: number;
9 w: number;
10 h: number;
11 inputs: NodeIO[];
12 outputs: NodeIO[];
13 selected: boolean;
14 statics: NodeStatic[];
15 onStaticsUpdate: ( node: Node ) => Promise<void>;
16
17 constructor( pos: [ number, number ], node: NodeDefinition, id: string ){
18 this.name = node.name;
19 this.id = id;
20 this.typeId = node.typeId!;
21 this.x = pos[0];
22 this.y = pos[1];
23
24 this.inputs = node.inputs ? node.inputs.map(( x, indx ) => {
25 return {
26 name: x.name,
27 type: x.type,
28 connections: [],
29 parent: this,
30 index: indx
31 }
32 }) : [];
33
34 this.outputs = node.outputs ? node.outputs.map(( x, indx ) => {
35 return {
36 name: x.name,
37 type: x.type,
38 connections: [],
39 parent: this,
40 index: indx
41 }
42 }) : [];
43
44 this.w = node.w || 200;
45 this.h = 50 + Math.max(this.outputs.length, this.inputs.length) * 30;
46
47 this.selected = false;
48 this.statics = node.statics!,
49 this.onStaticsUpdate = node.onStaticsUpdate!;
50 }
51
52 updateSize(){
53 this.h = 50 + Math.max(this.outputs.length, this.inputs.length) * 30;
54 }
55}
56
57export interface NodeIO{
58 name: string,
59 type: NodeType,
60 connections: NodeIO[],
61 parent: Node,
62 index: number
63}
64
65export enum NodeType{
66 Label = 0,
67
68 String = 1,
69 Float = 2,
70 Int = 3,
71 Boolean = 4,
72 Flow = 5,
73
74 AnyTypeA = 6,
75 AnyTypeB = 7,
76 AnyTypeC = 8,
77
78 OSCAddress = 9,
79 ParameterList = 10
80}
81
82let NodeIOCastTable: any = {
83 1: { 2: true, 3: true, 4: true }, // Strings -> Floats, Ints, Bools
84 2: { 1: true, 3: true }, // Floats -> Strings, Ints
85 3: { 1: true, 2: true, 4: true }, // Ints -> Strings, Floats, Bools
86 4: { 1: true, 2: true, 3: true } // Bools -> Strings, Ints, Floats
87};
88
89export let NodeIOCanCast = ( input: NodeType | null, output: NodeType | null ): boolean => {
90 if(input === output)return true;
91 if(!input || !output)return false;
92
93 let CastFrom = NodeIOCastTable[input];
94 if(!CastFrom)return false;
95
96 let CastTo = CastFrom[output];
97 if(!CastTo)return false;
98
99 return true;
100}
101
102export let NodeIOResolveAnyTypes = ( nodeio: NodeIO ): NodeType | null => {
103 if(nodeio.type > 0 && nodeio.type < 6){
104 // It's a base type
105 return nodeio.type;
106 }
107
108 // It's an "AnyType" value and we should resolve it,
109 // it also means it's an input as "AnyType" is not valid on outputs
110 let type = nodeio.type;
111
112 // Check if we have any connections
113 if(nodeio.connections.length > 0){
114 // We do, lets copy the type of the first input
115 return nodeio.connections[0].type;
116 }
117
118 // Check if there are any others of the same "AnyType"
119 let other = nodeio.parent.inputs.filter(x => x !== nodeio).find(x => x.type === type);
120 if(other){
121 // There are others with the same type, lets copy that type
122 // Does other have any connections
123
124 if(other.connections.length > 0){
125 return other.connections[0].type;
126 }
127 }
128
129 // We can't resolve it yet
130 return null;
131}
132
133export let NodeIOLinkColours = ( nodeio: NodeIO, output?: NodeIO ) => {
134 let cols: any = {
135 1: '#ffff9f',
136 2: '#cda0cb',
137 3: '#7ecaca',
138 4: '#8bc0a2',
139 5: '#edeae3'
140 }
141
142 let type = NodeIOResolveAnyTypes(nodeio);
143 if(output){
144 let outputType = NodeIOResolveAnyTypes(output);
145 let startType = type ? cols[type] : '#fff5';
146
147 if(type !== outputType){
148 let endType = outputType ? cols[outputType] : '#fff5';
149
150 return [ startType, endType ];
151 } else{
152 return [ startType, startType ];
153 }
154 }
155
156 return type ? cols[type] : '#fff5';
157}
158
159export interface NodeStatic{
160 name: string,
161 type: NodeType,
162 value: any
163}