1 | import Konva from "konva";
|
---|
2 | import Factory from "../util/Factory.js";
|
---|
3 | import HttpService from "../net/HttpService.js";
|
---|
4 | import {zoomStage} from "../util/zoomStage.js";
|
---|
5 | import {addEventHandling} from "../util/addEventHandling.js";
|
---|
6 | import triggerNavigate from "../util/triggerNavigate.js";
|
---|
7 | import config from "../net/netconfig.js";
|
---|
8 |
|
---|
9 | export class MapDisplay {
|
---|
10 | constructor(containerId, floorNum) {
|
---|
11 | this.container = document.getElementById(containerId);
|
---|
12 | this.containerId = containerId;
|
---|
13 | this.stage = new Konva.Stage({
|
---|
14 | container: containerId,
|
---|
15 | width: window.innerWidth,
|
---|
16 | height: window.innerHeight,
|
---|
17 | draggable: true,
|
---|
18 | });
|
---|
19 |
|
---|
20 | this.shapes = [];
|
---|
21 | this.roomTypes = [];
|
---|
22 | this.loaded = false;
|
---|
23 | this.mainLayer = new Konva.Layer();
|
---|
24 | this.routeLayer = new Konva.Layer();
|
---|
25 | this.textLayer = new Konva.Layer();
|
---|
26 | this.stage.add(this.mainLayer);
|
---|
27 | this.stage.add(this.routeLayer);
|
---|
28 | this.stage.add(this.textLayer);
|
---|
29 |
|
---|
30 | this.floorNum = floorNum;
|
---|
31 |
|
---|
32 | this.navArrow = new Konva.Arrow({
|
---|
33 | stroke: "#bb0000",
|
---|
34 | strokeWidth: 3,
|
---|
35 | dash: [12, 7],
|
---|
36 | lineCap: "round",
|
---|
37 | tension: 10,
|
---|
38 | pointerLength: 2,
|
---|
39 | pointerWidth: 3,
|
---|
40 | });
|
---|
41 |
|
---|
42 | this.navArrow.cache();
|
---|
43 |
|
---|
44 | this.stage.on("resize", () => {
|
---|
45 | this.stage.width = window.innerWidth;
|
---|
46 | this.stage.height = window.innerHeight;
|
---|
47 | });
|
---|
48 |
|
---|
49 | this.stage.on("wheel", (e) => {
|
---|
50 | zoomStage(e, this.stage);
|
---|
51 | });
|
---|
52 | }
|
---|
53 |
|
---|
54 | clearMap() {
|
---|
55 | this.mainLayer.removeChildren();
|
---|
56 | this.shapes.forEach(shape => shape.clearText())
|
---|
57 | this.shapes = [];
|
---|
58 | }
|
---|
59 |
|
---|
60 | deserializeMap(data) {
|
---|
61 | this.clearMap();
|
---|
62 |
|
---|
63 | let dsrData = JSON.parse(data);
|
---|
64 | dsrData.forEach((shape) => {
|
---|
65 | if (shape.className !== "InfoPin") {
|
---|
66 | const renderedShape = Factory.createRenderedShape(shape.className, shape.attrs);
|
---|
67 | addEventHandling(renderedShape, this, "click");
|
---|
68 | this.shapes.push(renderedShape);
|
---|
69 | }
|
---|
70 | });
|
---|
71 | }
|
---|
72 |
|
---|
73 | displayRoomNames() {
|
---|
74 | this.shapes.forEach((shape) => {
|
---|
75 | shape.displayName(this.textLayer);
|
---|
76 | });
|
---|
77 |
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 |
|
---|
82 | loadMapN(floorData) {
|
---|
83 | if (floorData == null || floorData === "") return;
|
---|
84 |
|
---|
85 | this.deserializeMap(floorData);
|
---|
86 | this.shapes.forEach((shape) => {
|
---|
87 | this.mainLayer.add(shape);
|
---|
88 | });
|
---|
89 | this.displayRoomNames();
|
---|
90 | this.initializeRoomTypes();
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | clearRoute() {
|
---|
95 | this.routeLayer.removeChildren();
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | drawRouteNEW(nodes, offset = 0) {
|
---|
100 |
|
---|
101 | this.clearRoute();
|
---|
102 | console.log("====PATH====");
|
---|
103 | nodes.forEach((node) => console.log("NODE", node));
|
---|
104 |
|
---|
105 | let idx = offset;
|
---|
106 | let buff = [nodes[idx].coordinates.x, nodes[idx].coordinates.y];
|
---|
107 |
|
---|
108 |
|
---|
109 | ++idx;
|
---|
110 |
|
---|
111 | console.log("INIT BUFFER", buff);
|
---|
112 | console.log("INIT IDX", idx);
|
---|
113 |
|
---|
114 | const drawNextSegment = () => {
|
---|
115 |
|
---|
116 | if (idx >= nodes.length){
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | const currentNode = nodes[idx - 1];
|
---|
121 | const nextNode = nodes[idx];
|
---|
122 |
|
---|
123 | if (nextNode.floorNumber !== currentNode.floorNumber) {
|
---|
124 | triggerNavigate(nodes, idx, nextNode.floorNumber, nextNode);
|
---|
125 | return;
|
---|
126 | }
|
---|
127 |
|
---|
128 | const startX = currentNode.coordinates.x;
|
---|
129 | const startY = currentNode.coordinates.y;
|
---|
130 | const endX = nextNode.coordinates.x;
|
---|
131 | const endY = nextNode.coordinates.y;
|
---|
132 |
|
---|
133 | const numSegments = 12;
|
---|
134 |
|
---|
135 | const deltaX = (endX - startX) / numSegments;
|
---|
136 | const deltaY = (endY - startY) / numSegments;
|
---|
137 |
|
---|
138 | const drawSegment = (i) => {
|
---|
139 | const segmentX = startX + deltaX * i;
|
---|
140 | const segmentY = startY + deltaY * i;
|
---|
141 |
|
---|
142 | buff.push(segmentX, segmentY);
|
---|
143 |
|
---|
144 | let line = this.navArrow.clone({ points: [...buff] });
|
---|
145 | this.routeLayer.add(line);
|
---|
146 | this.routeLayer.draw();
|
---|
147 |
|
---|
148 | buff = [segmentX, segmentY];
|
---|
149 | };
|
---|
150 |
|
---|
151 | let segmentIdx = 1;
|
---|
152 | const interval = setInterval(() => {
|
---|
153 | drawSegment(segmentIdx);
|
---|
154 | segmentIdx++;
|
---|
155 |
|
---|
156 | if (segmentIdx > numSegments) {
|
---|
157 | clearInterval(interval);
|
---|
158 | idx++;
|
---|
159 | setTimeout(drawNextSegment, 150);
|
---|
160 | }
|
---|
161 | }, 50);
|
---|
162 | };
|
---|
163 |
|
---|
164 | drawNextSegment();
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 |
|
---|
169 | initializeRoomTypes() {
|
---|
170 | this.roomTypes = this.shapes
|
---|
171 | .filter((shape) => shape.class === "Room" && shape.info.type !== "")
|
---|
172 | .map((shape) => shape.info.type);
|
---|
173 | }
|
---|
174 |
|
---|
175 | getRoomTypes() {
|
---|
176 | return this.roomTypes;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | getShapeByName(name){
|
---|
181 | return this.shapes.find(shape => shape.info.name === name)
|
---|
182 | }
|
---|
183 |
|
---|
184 | getShapeByType(type) {
|
---|
185 | return this.shapes.filter((shape) => shape.class === type)
|
---|
186 | }
|
---|
187 |
|
---|
188 | toggleSearchRoom() {
|
---|
189 | this.toggleSearch = !this.toggleSearch;
|
---|
190 | }
|
---|
191 |
|
---|
192 | //ova e loso ne trebit vaka
|
---|
193 | highlightShape(roomName) {
|
---|
194 | let foundShape = this.shapes.filter((shape) => shape.info.name === roomName)[0];
|
---|
195 | foundShape.highlight();
|
---|
196 | }
|
---|
197 |
|
---|
198 | getMainEntrance() {
|
---|
199 | return this.shapes.filter(shape => shape.class === "Entrance").filter(el => el.info.isMainEntrance === true)[0];
|
---|
200 | }
|
---|
201 |
|
---|
202 | setFilter(filter) {
|
---|
203 | let rooms = this.getShapeByType("Room")
|
---|
204 | if (filter === "All") {
|
---|
205 | rooms.forEach((shape) => {
|
---|
206 | shape.unHighlight()
|
---|
207 | })
|
---|
208 | } else {
|
---|
209 | rooms.filter((shape) => shape.info.type === filter).forEach((shape) => {
|
---|
210 | shape.highlight()
|
---|
211 | })
|
---|
212 | rooms.filter((shape) => shape.info.type !== filter).forEach((shape) => {
|
---|
213 | shape.unHighlight()
|
---|
214 | })
|
---|
215 | }
|
---|
216 |
|
---|
217 | }
|
---|
218 | }
|
---|