Skip to content

Commit 1fed124

Browse files
committed
port descriptions and rendering
1 parent 13eece1 commit 1fed124

File tree

5 files changed

+101
-32
lines changed

5 files changed

+101
-32
lines changed

index.html

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,11 @@
203203
{ name: "b", type: "float32" }
204204
],
205205
outputs: [
206-
{ name: "sum", type: "float32" }
206+
{
207+
name: "sum",
208+
type: "float32",
209+
description: "The sum of a and b"
210+
}
207211
],
208212
});
209213
graph.addNode(node);
@@ -221,8 +225,8 @@
221225

222226
var sumNode = new FlowNode({
223227
position: {
224-
x: 850,
225-
y: 600
228+
x: 750,
229+
y: 700
226230
},
227231
title: "Add",
228232
info: "I add two numbers",
@@ -231,14 +235,14 @@
231235
{ name: "b", type: "float32" }
232236
],
233237
outputs: [
234-
{ name: "sum", type: "float32" }
238+
{ name: "sum", type: "float32", description: "The sum of a and b" }
235239
],
236240
});
237241

238242

239243
var aNode = new FlowNode({
240244
position: {
241-
x: 550,
245+
x: 450,
242246
y: 500
243247
},
244248
title: "Number",
@@ -256,7 +260,7 @@
256260

257261
var bNode = new FlowNode({
258262
position: {
259-
x: 550,
263+
x: 450,
260264
y: 650
261265
},
262266
title: "Number",
@@ -280,17 +284,17 @@
280284

281285
const arrNode = new FlowNode({
282286
position: {
283-
x: 1050,
284-
y: 600
287+
x: 950,
288+
y: 700
285289
},
286290
title: "Add",
287291
subTitle: "Array Inputs",
288292
info: "This node contains a port that can take multiple input nodes at once, which can be useful for operations that can operate on [0,n] data sizes",
289293
inputs: [
290-
{ name: "numbers", type: "float32", array: true },
294+
{ name: "numbers", type: "float32", array: true, description: "All numbers to add together" },
291295
],
292296
outputs: [
293-
{ name: "sum", type: "float32" }
297+
{ name: "sum", type: "float32", description: "The sum of all connected nodes to our 'numbers' port" }
294298
],
295299
});
296300

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@elicdavis/node-flow",
33
"description": "build node graphs",
44
"author": "Eli C Davis",
5-
"version": "0.1.1",
5+
"version": "0.1.2",
66
"license": "MIT",
77
"keywords": [
88
"nodes",

src/elements/base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export abstract class RenderElementBase implements IRenderElement {
107107
this.doRender(ctx, offsetPosition, graphScale, elementSize);
108108
}
109109

110+
// todo: Remove limitations argument?
110111
abstract calcSize(ctx: CanvasRenderingContext2D, out: Vector2, limitations: Vector2): void;
111112

112113
protected maxLimitations(out: Vector2): void {

src/elements/text.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class TextElement extends RenderElementBase {
3535

3636
doRender(ctx: CanvasRenderingContext2D, position: Vector2, graphScale: number, scaledFillableSpace: Vector2): void {
3737
const scale = Zero();
38-
this.calcSize(ctx, scale);
38+
this.calcSize(ctx, scale, scaledFillableSpace);
3939
ScaleVector(scale, graphScale);
4040

4141
const justifiedPosition = Zero();
@@ -77,10 +77,38 @@ export class TextElement extends RenderElementBase {
7777
default:
7878
throw new Error("unimplemented justification: " + this.#align);
7979
}
80-
this.#text.render(ctx, graphScale, justifiedPosition);
80+
81+
if (scaledFillableSpace.x <= 0) {
82+
this.#text.render(ctx, graphScale, justifiedPosition);
83+
} else {
84+
const eles = this.#text.breakIntoLines(ctx, scaledFillableSpace.x);
85+
86+
const tempSize = { x: 0, y: 0 };
87+
for (let i = 0; i < eles.length; i++) {
88+
eles[i].render(ctx, graphScale, justifiedPosition);
89+
eles[i].size(ctx, 1, tempSize)
90+
justifiedPosition.y += tempSize.y
91+
}
92+
}
8193
}
8294

83-
calcSize(ctx: CanvasRenderingContext2D, out: Vector2): void {
84-
this.#text.size(ctx, 1, out);
95+
calcSize(ctx: CanvasRenderingContext2D, out: Vector2, limitations: Vector2): void {
96+
97+
if (limitations.x <= 0) {
98+
this.#text.size(ctx, 1, out);
99+
return;
100+
}
101+
102+
const eles = this.#text.breakIntoLines(ctx, limitations.x);
103+
104+
const tempSize = { x: 0, y: 0 };
105+
out.x = 0;
106+
out.y = 0;
107+
for (let i = 0; i < eles.length; i++) {
108+
eles[i].size(ctx, 1, tempSize)
109+
out.x = Math.max(tempSize.x, out.x);
110+
out.y += tempSize.y
111+
}
112+
85113
}
86114
}

src/port.ts

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { Camera } from "./camera";
22
import { Connection } from './connection';
3+
import { RenderElementBase } from "./elements/base";
4+
import { ContainerRenderElement, AlignItems } from './elements/container';
5+
import { TextAlign, TextElement } from "./elements/text";
36
import { FlowNode } from "./node";
47
import { PassSubsystem } from "./pass/subsystem";
5-
import { TextAlign } from "./styles/canvasTextAlign";
6-
import { Box, BoxIntersection, InBox } from "./types/box";
8+
import { FontStyle } from "./styles/text";
9+
import { Box, InBox } from "./types/box";
10+
import { Text } from "./types/text";
711
import { Vector2 } from "./types/vector2";
8-
import { Color, HSV, HSV2RGB, RgbToHex } from "./utils/color";
12+
import { Color, HSV, HSV2RGB, RgbToHex } from './utils/color';
913

1014
export enum PortType {
1115
Input = "INPUT",
@@ -25,6 +29,7 @@ type ConnectionChangeCallback = (connection: Connection, connectionIndex: number
2529
export interface PortConfig {
2630
name?: string;
2731
type?: string;
32+
description?: string;
2833
array?: boolean;
2934
emptyStyle?: PortStyle;
3035
filledStyle?: PortStyle;
@@ -64,9 +69,11 @@ export class Port {
6469

6570
#dataType: string;
6671

72+
#dataTypePopupElement: RenderElementBase;
73+
6774
#onConnectionAdded: Array<ConnectionChangeCallback>;
6875

69-
#onConnectionRemoved: Array<ConnectionChangeCallback>;;
76+
#onConnectionRemoved: Array<ConnectionChangeCallback>;
7077

7178
constructor(node: FlowNode, portType: PortType, config?: PortConfig) {
7279
this.#node = node;
@@ -98,6 +105,43 @@ export class Port {
98105
if (config?.onConnectionRemoved) {
99106
this.#onConnectionRemoved.push(config?.onConnectionRemoved);
100107
}
108+
109+
const containerElements = new Array<RenderElementBase>();
110+
let dataTypeDisplay = this.#dataType;
111+
if (config?.array === true) {
112+
dataTypeDisplay = "Array<" + dataTypeDisplay + ">"
113+
}
114+
containerElements.push(new TextElement(
115+
new Text(dataTypeDisplay, {
116+
color: "white",
117+
}),
118+
{
119+
Align: TextAlign.Center,
120+
}
121+
));
122+
123+
const description = config?.description;
124+
if (description && description !== "") {
125+
containerElements.push(new TextElement(
126+
new Text(description, { color: "white", style: FontStyle.Italic }),
127+
{
128+
Align: TextAlign.Center,
129+
Padding: { Top: 8 },
130+
MaxWidth: 300
131+
}
132+
));
133+
}
134+
135+
this.#dataTypePopupElement = new ContainerRenderElement(
136+
containerElements,
137+
{
138+
BackgroundColor: "rgba(0, 0, 0, 0.75)",
139+
Border: {
140+
Radius: 6,
141+
},
142+
Padding: 13,
143+
}
144+
);
101145
}
102146

103147
addConnection(connection: Connection): void {
@@ -183,20 +227,12 @@ export class Port {
183227
const xPos = position.x;
184228
const yPos = position.y;
185229

230+
// TODO: Make it so we're not creating a new lambda each frame
186231
postProcess.queue(() => {
187-
ctx.textAlign = TextAlign.Center;
188-
189-
const padding = 13;
190-
const measurement = ctx.measureText(this.#dataType)
191-
const w = measurement.width + (padding * camera.zoom);
192-
193-
ctx.beginPath();
194-
ctx.fillStyle = "rgba(0, 0, 0, 0.75)";
195-
ctx.roundRect(xPos - w / 2, yPos + (scaledRadius * 3) - (padding * camera.zoom), w, (padding * 2) * camera.zoom, 4 * camera.zoom);
196-
ctx.fill();
197-
198-
ctx.fillStyle = "#FFFFFF";
199-
ctx.fillText(this.#dataType, xPos, yPos + (scaledRadius * 3));
232+
const size = { x: 0, y: 0 }
233+
this.#dataTypePopupElement.calcSize(ctx, size, { x: -1, y: -1 });
234+
// size.x = Math.min(150, size.x)
235+
this.#dataTypePopupElement.render(ctx, { x: xPos - (size.x / 2), y: yPos }, 1, size)
200236
})
201237
}
202238

0 commit comments

Comments
 (0)