PNet/js/PElems/Node.ts
2023-06-27 09:19:06 +02:00

41 lines
No EOL
1.1 KiB
TypeScript

import { svgNodes, update } from "..";
import { SVGNode } from "../graphics/SVGNode";
import { Positionable } from "../types";
import { Edge } from "./Edge";
export class Node implements Positionable {
renderedElement: SVGNode;
x: number = 0;
y: number = 0;
name: string;
capacity = 1;
marks = 0;
updateList: Edge[] = [];
constructor(name, x, y) {
this.name = name;
this.x = x;
this.y = y;
this.renderedElement = new SVGNode(this);
this.renderedElement.appendIt(svgNodes);
this.renderedElement.onMove((event) => this.updatePosition(event.x, event.y));
this.renderedElement.onClick((event) => {
if (this.marks < this.capacity) this.marks++
update();
})
}
updatePoints() {
this.renderedElement.setMarks(this.marks, this.capacity);
}
updatePosition(x: number, y: number) {
this.x = x;
this.y = y;
this.renderedElement.translate(x, y);
for (const elem of this.updateList) {
elem.updatePosition();
}
}
}