sequence node drawing v1

This commit is contained in:
ari committed 2022-07-10 13:37:11 -04:00
1 parent 1d7b6e96c1
commit edb3dafe12
7 files changed
+70 -58

No files matched your search

@@ -1,9 +1,8 @@
import { DAGDims } from "./types"; import { DAGDims } from "./types";
export const NODE_WIDTH = 170 as const; export const NODE_WIDTH = 200 as const;
export const NODE_HEIGHT = 70 as const; export const NODE_HEIGHT = 80 as const;
export const CIRCLE_SIZE = 40 as const; export const CIRCLE_SIZE = 40 as const;
export const DISTANCE_FACTOR = 1.25 as const;
export const DEFAULT_DIMS: DAGDims = { export const DEFAULT_DIMS: DAGDims = {
trigger: { trigger: {
@@ -22,7 +21,10 @@ export const DEFAULT_DIMS: DAGDims = {
height: CIRCLE_SIZE, height: CIRCLE_SIZE,
width: CIRCLE_SIZE, width: CIRCLE_SIZE,
}, },
distanceFactor: DISTANCE_FACTOR, distanceFactor: {
node: 2,
collection: 1.25,
},
position: { position: {
x: 50, x: 50,
y: 50, y: 50,
@@ -5,22 +5,17 @@ export const moveAlong = (
by: keyof DAGEntities, by: keyof DAGEntities,
startPoint: XYPosition, startPoint: XYPosition,
increment: number, increment: number,
dims: DAGDims, dims: DAGDims
direction: "under" | "next" = "next"
): XYPosition => { ): XYPosition => {
const moveOnY = { const moveOnY = {
y: startPoint.y + dims[by].height * dims.distanceFactor * increment, y: startPoint.y + dims[by].height * dims.distanceFactor.node * increment,
x: startPoint.x, x: startPoint.x,
}; };
const moveOnX = { const moveOnX = {
x: startPoint.x + dims[by].width * dims.distanceFactor * increment, x: startPoint.x + dims[by].width * dims.distanceFactor.node * increment,
y: startPoint.y, y: startPoint.y,
}; };
if (direction === "next") { return !dims.flipped ? moveOnX : moveOnY;
return dims.flipped ? moveOnX : moveOnY;
} else {
return !dims.flipped ? moveOnX : moveOnY;
}
}; };
export const moveFromTo = ( export const moveFromTo = (
@@ -31,34 +26,13 @@ export const moveFromTo = (
): XYPosition => { ): XYPosition => {
if (!dims.flipped) { if (!dims.flipped) {
return { return {
x: startPoint.x + dims[from].width * dims.distanceFactor, x: startPoint.x + dims[from].width * dims.distanceFactor.collection,
y: startPoint.y + (dims[from].height - dims[to].height) / 2, y: startPoint.y + (dims[from].height - dims[to].height) / 2,
}; };
} else { } else {
return { return {
y: startPoint.y + dims[from].height * dims.distanceFactor, y: startPoint.y + dims[from].height * dims.distanceFactor.collection,
x: startPoint.x + (dims[from].width - dims[to].width) / 2, x: startPoint.x + (dims[from].width - dims[to].width) / 2,
}; };
} }
}; };
export const computeSize = (
by: keyof DAGEntities,
dims: DAGDims,
count: number,
direction: "under" | "next" = "next"
): Size => {
const moveOnY = {
height: dims[by].height * count,
width: dims[by].width * dims.distanceFactor,
};
const moveOnX = {
width: dims[by].width * count,
height: dims[by].height * dims.distanceFactor,
};
if (direction === "next") {
return dims.flipped ? moveOnX : moveOnY;
} else {
return !dims.flipped ? moveOnX : moveOnY;
}
};
@@ -3,6 +3,7 @@ import {
ElementMaker, ElementMaker,
ElementMakerBaseProps, ElementMakerBaseProps,
ElementMakerOutput, ElementMakerOutput,
LastNode,
} from "./types"; } from "./types";
import { AutomationTrigger } from "types/automations/triggers"; import { AutomationTrigger } from "types/automations/triggers";
import * as distance from "./distance"; import * as distance from "./distance";
@@ -13,6 +14,7 @@ import {
} from "types/automations"; } from "types/automations";
import { AutomationCondition } from "types/automations/conditions"; import { AutomationCondition } from "types/automations/conditions";
import { CollectionNodeMaker } from "../nodes/CollectionNode"; import { CollectionNodeMaker } from "../nodes/CollectionNode";
import { SequenceNodeMaker } from "../nodes/SequenceNode";
export const makeAutomationNodes = ( export const makeAutomationNodes = (
automation: AutomationActionData, automation: AutomationActionData,
@@ -72,9 +74,7 @@ export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
nodes: nodes.map((node, index) => ({ nodes: nodes.map((node, index) => ({
enabled: node.enabled ?? true, enabled: node.enabled ?? true,
label: getDescriptionFromAutomationNode(node, namer, true), label: getDescriptionFromAutomationNode(node, namer, true),
...stateUpdater.createNodeActions("trigger", index, { ...stateUpdater.createNodeActions("trigger", index, {}),
flipped: dims.flipped,
}),
})), })),
}) })
); );
@@ -101,9 +101,7 @@ export const makeConditionNodes: ElementMaker<AutomationCondition> = (
nodes: nodes.map((node, index) => ({ nodes: nodes.map((node, index) => ({
enabled: node.enabled ?? true, enabled: node.enabled ?? true,
label: getDescriptionFromAutomationNode(node, namer, true), label: getDescriptionFromAutomationNode(node, namer, true),
...stateUpdater.createNodeActions("condition", index, { ...stateUpdater.createNodeActions("condition", index, {}),
flipped: dims.flipped,
}),
})), })),
}) })
); );
@@ -129,14 +127,46 @@ export const makeConditionNodes: ElementMaker<AutomationCondition> = (
export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = ( export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
nodes, nodes,
{ nodeId, elementData, dims } { nodeId, lastNodeId, elementData, dims, position, namer, stateUpdater }
) => { ) => {
let lastNode: LastNode = {
nodeId: lastNodeId ?? nodeId,
pos: elementData.nodes[elementData.nodes.length - 1].position,
size: dims.node,
};
nodes.forEach((node, nodeIndex) => {
const element = SequenceNodeMaker.makeElement(
{
id: `${nodeId}-${nodeIndex}`,
position: distance.moveAlong("node", position, nodeIndex, dims),
},
dims,
{
color: "green",
enabled: node.enabled ?? true,
hasInput: true,
label: getDescriptionFromAutomationNode(node, namer, true),
...stateUpdater.createNodeActions("sequence", nodeIndex, {
includeAdd: true,
flipped: dims.flipped,
}),
}
);
elementData.nodes.push(element);
elementData.edges.push({
id: `${lastNode.nodeId}->${element.id}`,
target: element.id,
source: lastNode.nodeId,
});
lastNode = {
nodeId: element.id,
pos: element.position,
size: dims.node,
};
});
return { return {
elementData, elementData,
lastNode: { lastNode,
nodeId,
pos: elementData.nodes[elementData.nodes.length - 1].position,
size: dims.node,
},
}; };
}; };
@@ -20,7 +20,10 @@ export type DAGEntities = {
}; };
export type DAGDims = DAGEntities & { export type DAGDims = DAGEntities & {
position: XYPosition; position: XYPosition;
distanceFactor: number; distanceFactor: {
node: number;
collection: number;
};
flipped: boolean; flipped: boolean;
}; };
export type ElementMakerBaseProps = { export type ElementMakerBaseProps = {
@@ -1,5 +1,7 @@
.collection-nodes--wrap { .collection-nodes--wrap {
--padding: 1.5em; --padding: 1.5em;
--child-height: calc(var(--node-height) - 2*var(--padding));
--child-width: calc(var(--node-width) - 2*var(--padding));
min-width: var(--node-width); min-width: var(--node-width);
min-height: var(--node-height); min-height: var(--node-height);
@@ -10,6 +12,8 @@
flex-direction: column; flex-direction: column;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
/* outline: 1px dotted white; */
} }
.collection-nodes { .collection-nodes {
@@ -18,11 +22,11 @@
flex-wrap: wrap; flex-wrap: wrap;
flex-direction: row; flex-direction: row;
align-items: center; align-items: center;
gap: 1.5em; gap: 0em 1em;
max-width: var(--node-width); max-width: var(--child-width);
max-height: var(--node-height); max-height: var(--child-height);
border-color: var(--node-color); border-color: var(--child-color);
border-style: solid; border-style: solid;
border-width: 1px; border-width: 1px;
border-radius: 10px; border-radius: 10px;
@@ -26,8 +26,8 @@ export const CollectionNode: FC<CollectionNodeProps> = ({
style={ style={
{ {
"--node-color": nodeColor, "--node-color": nodeColor,
"--node-height": `calc(${height}px - 2*var(--padding))`, "--node-height": `${height}px`,
"--node-width": `calc(${width}px - 2*var(--padding))`, "--node-width": `${width}px`,
} as any } as any
} }
> >
@@ -4,9 +4,8 @@
justify-content: center; justify-content: center;
align-items: center; align-items: center;
cursor: default; cursor: default;
/* outline-width: 1px;
outline-style: dotted; /* outline: 1px dotted white; */
outline-color: white; */
} }
.sequence-node--inner { .sequence-node--inner {