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 |
|
---|
9 | super(config);
|
---|
10 | this.layer = layer;
|
---|
11 | this.blockSize = blockSize;
|
---|
12 | this._type = "";
|
---|
13 | this.snappable = snap;
|
---|
14 | this._info = {};
|
---|
15 | this.eventName = "";
|
---|
16 | this.infoText = null;
|
---|
17 | this.floorNum;
|
---|
18 |
|
---|
19 | this.shadowForStrokeEnabled(false);
|
---|
20 | this.on("mouseover", () => (document.body.style.cursor = "pointer"));
|
---|
21 | this.on("mouseout", () => (document.body.style.cursor = "default"));
|
---|
22 | this.on("click", (e) => {
|
---|
23 | if(e.evt.altKey){
|
---|
24 | this.moveToTop();
|
---|
25 | this.getLayer()
|
---|
26 | .find("Transformer")
|
---|
27 | .forEach((t) => t.moveToTop());
|
---|
28 | }
|
---|
29 |
|
---|
30 | });
|
---|
31 |
|
---|
32 | if (snap) {
|
---|
33 | this.on("dragend", this.snapToGrid.bind(this));
|
---|
34 | this.on('dblclick', this.snapToGrid.bind(this));
|
---|
35 | }
|
---|
36 |
|
---|
37 | this.on("transform", () => {
|
---|
38 | const scaleX = this.scaleX();
|
---|
39 | const scaleY = this.scaleY();
|
---|
40 | this.strokeWidth(1 / Math.max(scaleX, scaleY));
|
---|
41 | });
|
---|
42 |
|
---|
43 |
|
---|
44 | this.on('dragmove', () => {
|
---|
45 | if (this.infoText) {
|
---|
46 | this.updateTextPosition();
|
---|
47 | }
|
---|
48 | });
|
---|
49 |
|
---|
50 | }
|
---|
51 |
|
---|
52 | loadInfo(attrs) {
|
---|
53 | console.log("Abstract function");
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | initText() {
|
---|
58 | this.textOffsetX = 0;
|
---|
59 | this.textOffsetY = -30;
|
---|
60 | this.infoText = new Konva.Text({
|
---|
61 | x: this.x() + this.textOffsetX,
|
---|
62 | y: this.y() + this.textOffsetY,
|
---|
63 | text: this._info.name,
|
---|
64 | fontSize: 12,
|
---|
65 | fontFamily: 'Exo',
|
---|
66 | fill: 'white',
|
---|
67 | });
|
---|
68 | }
|
---|
69 |
|
---|
70 | updateTextPosition() {
|
---|
71 | if (this.infoText) {
|
---|
72 | this.infoText.x(this.x() + this.textOffsetX);
|
---|
73 | this.infoText.y(this.y() + this.textOffsetY);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | displayName(layer) {
|
---|
78 | if (this.infoText) {
|
---|
79 | this.infoText.text(this._info.name);
|
---|
80 | layer.add(this.infoText);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | destroy() {
|
---|
86 | super.destroy();
|
---|
87 | if (this.infoText !== null) {
|
---|
88 | this.infoText.remove()
|
---|
89 | console.log("cleared text")
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | setInfo(infoObj){
|
---|
94 | this.info = infoObj;
|
---|
95 | }
|
---|
96 |
|
---|
97 | load(){
|
---|
98 | console.log("Abstract function")
|
---|
99 | }
|
---|
100 |
|
---|
101 | _sceneFunc(context) {
|
---|
102 | let width = this.width();
|
---|
103 | let height = this.height();
|
---|
104 | context.beginPath();
|
---|
105 | context.rect(0, 0, width, height)
|
---|
106 | context.closePath();
|
---|
107 | context.fillStrokeShape(this);
|
---|
108 | }
|
---|
109 |
|
---|
110 | clearText() {
|
---|
111 | if (this.infoText !== null) {
|
---|
112 | this.infoText.remove()
|
---|
113 | console.log("cleared text")
|
---|
114 | }
|
---|
115 |
|
---|
116 | }
|
---|
117 |
|
---|
118 | updateText(shapeName){
|
---|
119 | this.infoText.text = shapeName;
|
---|
120 | console.log("Updated text to : " + shapeName)
|
---|
121 | }
|
---|
122 |
|
---|
123 | snapToGrid() {
|
---|
124 | this.position({
|
---|
125 | x: Math.round(this.x() / this.blockSize) * this.blockSize,
|
---|
126 | y: Math.round(this.y() / this.blockSize) * this.blockSize,
|
---|
127 | });
|
---|
128 |
|
---|
129 | }
|
---|
130 |
|
---|
131 | saveShapeDetails() {
|
---|
132 | console.log("This shape does not contain information");
|
---|
133 | }
|
---|
134 |
|
---|
135 | onPlace(){
|
---|
136 | //this.snapToGrid();
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | get info() {
|
---|
141 | return this._info;
|
---|
142 | }
|
---|
143 |
|
---|
144 | set info(formData) {
|
---|
145 | this._info = formData;
|
---|
146 | }
|
---|
147 |
|
---|
148 | get type() {
|
---|
149 | return this._type;
|
---|
150 | }
|
---|
151 |
|
---|
152 | set type(type) {
|
---|
153 | this._type = type
|
---|
154 | }
|
---|
155 |
|
---|
156 | } |
---|