PNet/js/PElems/Node.ts

41 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-06-27 09:19:06 +02:00
import { svgNodes, update } from "..";
2023-06-22 17:47:04 +02:00
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));
2023-06-27 09:19:06 +02:00
this.renderedElement.onClick((event) => {
if (this.marks < this.capacity) this.marks++
update();
})
2023-06-22 17:47:04 +02:00
}
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();
}
}
}