Skip to content

Commit

Permalink
ExporterElement can be a parent. Drag and drop on exporters working.
Browse files Browse the repository at this point in the history
  • Loading branch information
billbudge committed Dec 23, 2024
1 parent e8e84a0 commit d7cdd8c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 91 deletions.
66 changes: 17 additions & 49 deletions examples/functioncharts/files/Library.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,20 @@
"typeString": "[,](Iterator)",
"x": 171.6781520843506,
"y": 745.774974822998,
"width": 402.5672550201416,
"width": 435.6688175201416,
"height": 263.3163833618164,
"name": "Iterator",
"nodes": [
{
"type": "exporter",
"id": 23,
"typeString": "[,[vv(v),[,v(?)v(v)]](return)]",
"typeString": "[,[vv(v),[,v(?)v(v)]()(res)](return)]",
"x": 25.325830459594727,
"y": 106.81869888305664,
"instancer": 0,
"inner": {
"type": "instance",
"id": 97,
"id": 24,
"typeString": "[vv(v),[,v(?)v(v)]()(res)](return)",
"x": 235.14444732666016,
"y": 861.9030570983887,
Expand All @@ -198,29 +198,29 @@
{
"type": "exporter",
"id": 25,
"typeString": "[,[vv(e),[,v(?)v(v)]](throw)]",
"typeString": "[,[vv(e),[,v(?)v(v)]()(res)](throw)]",
"x": 24.886804580688477,
"y": 185.3163833618164,
"instancer": 0,
"inner": {
"type": "instance",
"id": 98,
"id": 102,
"typeString": "[vv(e),[,v(?)v(v)]()(res)](throw)",
"x": 213.59529876708984,
"y": 934.5873908996582,
"x": 208.91951751708984,
"y": 937.1147346496582,
"instancer": 90
}
},
{
"type": "exporter",
"id": 27,
"typeString": "[,[vv,[,v(?)v(v)]](next)]",
"typeString": "[,[vv,[,v(?)v(v)]()(res)](next)]",
"x": 24.045297622680664,
"y": 24.806217193603516,
"instancer": 0,
"inner": {
"type": "instance",
"id": 96,
"id": 28,
"typeString": "[vv,[,v(?)v(v)]()(res)](next)",
"x": 208.45709228515625,
"y": 780.6296195983887,
Expand All @@ -236,8 +236,8 @@
"typeString": "[v,](entries)",
"x": 838.4532958865166,
"y": 1247.4500312805176,
"width": 501.0915496945381,
"height": 199.38124084472656,
"width": 613.0915496945381,
"height": 232.68202209472656,
"name": "Array.entries",
"nodes": [
{
Expand All @@ -250,16 +250,16 @@
{
"type": "exporter",
"id": 31,
"typeString": "[,[,[vv,[,v(?)v(v)]][vv(v),[,v(?)v(v)]][vv(e),[,v(?)v(v)]]](Iterator)](iter)",
"x": 45.07796937227249,
"y": 11.381240844726562,
"typeString": "[,[,[vv,[,v(?)v(v)]()(res)]()(next)[vv(v),[,v(?)v(v)]()(res)]()(return)[vv(e),[,v(?)v(v)]()(res)]()(throw)](Iterator)]( )",
"x": 15.234219372272491,
"y": 44.68202209472656,
"instancer": 0,
"inner": {
"type": "instance",
"id": 100,
"id": 103,
"typeString": "[,[vv,[,v(?)v(v)]()(res)]()(next)[vv(v),[,v(?)v(v)]()(res)]()(return)[vv(e),[,v(?)v(v)]()(res)]()(throw)](Iterator)",
"x": 867.9211883544922,
"y": 1280.3296394348145,
"x": 870.5227508544922,
"y": 1279.6343269348145,
"instancer": 22
}
}
Expand Down Expand Up @@ -786,38 +786,6 @@
}
],
"wires": []
},
{
"type": "instance",
"id": 96,
"typeString": "[vv,[,v(?)v(v)]()(res)](next)",
"x": 208.45709228515625,
"y": 780.6296195983887,
"instancer": 80
},
{
"type": "instance",
"id": 97,
"typeString": "[vv(v),[,v(?)v(v)]()(res)](return)",
"x": 235.14444732666016,
"y": 861.9030570983887,
"instancer": 85
},
{
"type": "instance",
"id": 98,
"typeString": "[vv(e),[,v(?)v(v)]()(res)](throw)",
"x": 213.59529876708984,
"y": 934.5873908996582,
"instancer": 90
},
{
"type": "instance",
"id": 100,
"typeString": "[,[vv,[,v(?)v(v)]()(res)]()(next)[vv(v),[,v(?)v(v)]()(res)]()(return)[vv(e),[,v(?)v(v)]()(res)]()(throw)](Iterator)",
"x": 867.9211883544922,
"y": 1280.3296394348145,
"instancer": 22
}
],
"wires": []
Expand Down
62 changes: 34 additions & 28 deletions examples/functioncharts/functioncharts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ abstract class NodeBase<T extends NodeTemplate> implements DataContextObject, Re
set y(value: number) { this.template.y.set(this, value); }

// Derived properties, managed by the FunctionchartContext.
parent: Functionchart | undefined;
parent: ElementParentTypes | undefined;
globalPosition = defaultPoint;

private _type: Type;
Expand Down Expand Up @@ -664,6 +664,7 @@ export class Functionchart extends NodeBase<FunctionchartTemplate> {
}

export type ElementTypes = Element | Pseudoelement;
export type ElementParentTypes = Functionchart | ExporterElement;
export type NodeTypes = ElementTypes | Functionchart;
export type InstancerTypes = Functionchart | InstancerElement;
export type AllTypes = NodeTypes | Wire;
Expand Down Expand Up @@ -877,11 +878,12 @@ export class FunctionchartContext extends EventBase<Change, ChangeEvents>

getContainingFunctionchart(items: AllTypes[]) : Functionchart {
let owner = getLowestCommonAncestor<AllTypes>(...items);
if (!owner)
return this.functionchart; // |items| not in the functionchart yet.
if (!(owner instanceof Functionchart))
owner = owner.parent; // single item, not a functionchart.
return owner as Functionchart;
while (owner && !(owner instanceof Functionchart))
owner = owner.parent;
if (owner instanceof Functionchart)
return owner;

return this.functionchart;
}

forInWires(dst: NodeTypes, visitor: WireVisitor) {
Expand Down Expand Up @@ -1249,7 +1251,7 @@ export class FunctionchartContext extends EventBase<Change, ChangeEvents>
}

connectInput(node: NodeTypes, pin: number) {
const parent = node.parent!,
const parent = node.parent as Functionchart,
p = this.layoutEngine.inputPinToPoint(node, pin),
wire = this.newWire(undefined, 0, node, pin);
p.x -= 32;
Expand Down Expand Up @@ -1287,7 +1289,7 @@ export class FunctionchartContext extends EventBase<Change, ChangeEvents>
}

connectOutput(node: NodeTypes, pin: number) {
const parent = node.parent!,
const parent = node.parent as Functionchart,
p = this.layoutEngine.outputPinToPoint(node, pin),
wire = this.newWire(node, pin, undefined, 0);
p.x += 32;
Expand Down Expand Up @@ -1359,13 +1361,15 @@ export class FunctionchartContext extends EventBase<Change, ChangeEvents>
const scope = getLowestCommonAncestor<AllTypes>(item, definition);
return scope === definition || // recursive
scope === definitionScope; // within scope of definition.
}
}
return true;
}

isValidFunctionInstance(instance: FunctionInstance) : boolean {
const parent = instance.parent;
if (!parent)
let parent = instance.parent;
if (parent instanceof ExporterElement)
parent = parent.parent;
if (!parent || !(parent instanceof Functionchart))
return false;
return this.canAddItem(instance, parent);
}
Expand Down Expand Up @@ -1653,7 +1657,7 @@ export class FunctionchartContext extends EventBase<Change, ChangeEvents>
}

replaceNode(node: NodeTypes, newNode: NodeTypes) {
const parent = node.parent,
const parent = node.parent as Functionchart,
newType = newNode.type;
// Add newNode so that both nodes are present as we rewire them.
this.addItem(newNode, parent);
Expand Down Expand Up @@ -1721,7 +1725,7 @@ export class FunctionchartContext extends EventBase<Change, ChangeEvents>

elements.forEach(element => {
if (element instanceof InstancerElement || element instanceof ExporterElement) return;
const parent = element.parent!;
const parent = element.parent as Functionchart;
selection.delete(element);
const exporter = self.exportElement(element),
index = parent.nodes.indexOf(element);
Expand Down Expand Up @@ -1917,7 +1921,7 @@ export class FunctionchartContext extends EventBase<Change, ChangeEvents>
}
}

private insertElement(element: ElementTypes, parent: Functionchart) {
private insertElement(element: ElementTypes, parent: ElementParentTypes) {
this.nodes.add(element);
element.parent = parent;
this.updateGlobalPosition(element);
Expand Down Expand Up @@ -1971,18 +1975,20 @@ export class FunctionchartContext extends EventBase<Change, ChangeEvents>
this.derivedInfoNeedsUpdate = true; // Removal might break a cycle, making an unsortable graph sortable.
}

private insertItem(item: AllTypes, parent: Functionchart) {
if (item instanceof Wire) {
if (parent && this.nodes.has(parent)) {
this.insertWire(item, parent);
}
} else if (item instanceof Functionchart) {
if (parent && this.nodes.has(parent)) {
this.insertFunctionchart(item, parent);
private insertItem(item: AllTypes, parent: ElementParentTypes) {
if (!this.nodes.has(parent)) return;
if (parent instanceof Functionchart) {
if (item instanceof Wire) {
this.insertWire(item, parent as Functionchart);
} else if (item instanceof Functionchart) {
this.insertFunctionchart(item, parent as Functionchart);
} else {
this.insertElement(item, parent);
}
} else {
if (parent && this.nodes.has(parent)) {
} else if (parent instanceof ExporterElement) {
if (item instanceof Element) {
this.insertElement(item, parent);
parent.typeString = item.type.toExporterType().typeString;
}
}
}
Expand Down Expand Up @@ -2014,14 +2020,14 @@ export class FunctionchartContext extends EventBase<Change, ChangeEvents>
this.updateGlobalPosition(owner); // Update any derived properties.
this.derivedInfoNeedsUpdate = true;
}
elementInserted(owner: Functionchart, prop: ChildPropertyTypes, index: number) : void {
elementInserted(owner: ElementParentTypes, prop: ChildPropertyTypes, index: number) : void {
if (this.nodes.has(owner)) {
const value: AllTypes = prop.get(owner).get(index) as AllTypes;
this.insertItem(value, owner);
this.onElementInserted(owner, prop, index);
}
}
elementRemoved(owner: Functionchart, prop: ChildPropertyTypes, index: number, oldValue: AllTypes) : void {
elementRemoved(owner: ElementParentTypes, prop: ChildPropertyTypes, index: number, oldValue: AllTypes) : void {
if (this.nodes.has(owner)) {
this.removeItem(oldValue);
this.onElementRemoved(owner, prop, index, oldValue);
Expand Down Expand Up @@ -2065,15 +2071,15 @@ export class FunctionchartContext extends EventBase<Change, ChangeEvents>
return this.onChanged(change);
}
private onElementInserted(
owner: Functionchart, prop: ChildPropertyTypes, index: number) :
owner: ElementParentTypes, prop: ChildPropertyTypes, index: number) :
Change {
const change: Change =
{ type: 'elementInserted', item: owner, prop: prop, index: index, oldValue: undefined };
super.onEvent('elementInserted', change);
return this.onChanged(change);
}
private onElementRemoved(
owner: Functionchart, prop: ChildPropertyTypes, index: number, oldValue: AllTypes ) :
owner: ElementParentTypes, prop: ChildPropertyTypes, index: number, oldValue: AllTypes ) :
Change {
const change: Change =
{ type: 'elementRemoved', item: owner, prop: prop, index: index, oldValue: oldValue };
Expand Down
34 changes: 20 additions & 14 deletions out/examples/functioncharts/functioncharts.js

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

0 comments on commit d7cdd8c

Please sign in to comment.