PNet/js/PElems/Node.ts

47 lines
1.5 KiB
TypeScript
Raw Permalink 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) => {
2023-06-28 18:22:29 +02:00
let aws = parseInt(prompt("Gabe die maximale Anzahl an Marken ein:", this.capacity + "") || "");
if (isNaN(aws) || !isFinite(aws) || aws < 1) return;
this.capacity = aws;
aws = parseInt(prompt("Gabe die aktuelle Anzahl an Marken ein:", this.marks + "") || "");
if (isNaN(aws) || !isFinite(aws) || aws < 1) return;
this.marks = aws;
//if (this.marks < this.capacity) this.marks++
2023-06-27 09:19:06 +02:00
update();
2023-06-28 18:22:29 +02:00
});
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();
}
}
}