[d565449] | 1 | import Konva from "konva";
|
---|
| 2 |
|
---|
| 3 | export default class MapShape extends Konva.Shape {
|
---|
| 4 | constructor(config, layer, blockSize, snap) {
|
---|
| 5 | if (new.target === MapShape) {
|
---|
| 6 | throw new Error("Cannot instantiate abstract class BaseShape directly.");
|
---|
| 7 | }
|
---|
| 8 | super(config);
|
---|
| 9 | this.layer = layer;
|
---|
| 10 | this.blockSize = blockSize;
|
---|
| 11 | this._type = "";
|
---|
| 12 | this.snappable = snap;
|
---|
| 13 | this._info = {};
|
---|
| 14 | this.modalEventName = "";
|
---|
| 15 | this.infoText = null;
|
---|
| 16 |
|
---|
| 17 | this.shadowForStrokeEnabled(false);
|
---|
| 18 | this.on("mouseover", () => (document.body.style.cursor = "pointer"));
|
---|
| 19 | this.on("mouseout", () => (document.body.style.cursor = "default"));
|
---|
| 20 | // this.on("dblclick", (e) => {
|
---|
| 21 | // this.moveToTop();
|
---|
| 22 | // this.getLayer()
|
---|
| 23 | // .find("Transformer")
|
---|
| 24 | // .forEach((t) => t.moveToTop());
|
---|
| 25 | // });
|
---|
| 26 |
|
---|
| 27 | if (snap) {
|
---|
| 28 | this.on("dragend", this.snapToGrid.bind(this));
|
---|
| 29 | this.on('dblclick', this.snapToGrid.bind(this))
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | this.on("transform", () => {
|
---|
| 33 | const scaleX = this.scaleX();
|
---|
| 34 | const scaleY = this.scaleY();
|
---|
| 35 | this.strokeWidth(1 / Math.max(scaleX, scaleY));
|
---|
| 36 | });
|
---|
| 37 |
|
---|
| 38 | this.on('dragend', () => {
|
---|
| 39 | if (this.infoText) {
|
---|
| 40 | this.updateTextPosition();
|
---|
| 41 | }
|
---|
| 42 | });
|
---|
| 43 |
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | loadInfo(attrs) {
|
---|
| 47 | console.log("Abstract function");
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | initText() {
|
---|
| 52 | this.textOffsetX = 0;
|
---|
| 53 | this.textOffsetY = -30;
|
---|
| 54 | this.infoText = new Konva.Text({
|
---|
| 55 | x: this.x() + this.textOffsetX,
|
---|
| 56 | y: this.y() + this.textOffsetY,
|
---|
| 57 | text: this._info.name,
|
---|
| 58 | fontSize: 12,
|
---|
| 59 | fontFamily: 'Verdana',
|
---|
| 60 | fill: 'white',
|
---|
| 61 | });
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | updateTextPosition() {
|
---|
| 65 | if (this.infoText) {
|
---|
| 66 | this.infoText.x(this.x() + this.textOffsetX);
|
---|
| 67 | this.infoText.y(this.y() + this.textOffsetY);
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | displayName(layer) {
|
---|
| 72 | if (this.infoText) {
|
---|
| 73 | this.infoText.text(this._info.name);
|
---|
| 74 | layer.add(this.infoText);
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | _sceneFunc(context) {
|
---|
| 79 | let width = this.width();
|
---|
| 80 | let height = this.height();
|
---|
| 81 | context.beginPath();
|
---|
| 82 | context.rect(0, 0, width, height)
|
---|
| 83 | context.closePath();
|
---|
| 84 | context.fillStrokeShape(this);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | clearText() {
|
---|
| 88 | if (this.infoText !== null) {
|
---|
| 89 | this.infoText.remove()
|
---|
| 90 | console.log("cleared text")
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | snapToGrid() {
|
---|
| 96 | this.position({
|
---|
| 97 | x: Math.round(this.x() / this.blockSize) * this.blockSize,
|
---|
| 98 | y: Math.round(this.y() / this.blockSize) * this.blockSize,
|
---|
| 99 | });
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | saveShapeDetails() {
|
---|
| 103 | console.log("This shape does not contain information");
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 | get info() {
|
---|
| 108 | return this._info;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | set info(formData) {
|
---|
| 112 | this._info = formData;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | get type() {
|
---|
| 116 | return this._type;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | set type(type) {
|
---|
| 120 | this._type = type
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | } |
---|