mudlet-map-renderer 0.0.25 → 0.1.1-konva

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,172 +0,0 @@
1
- const paper = require("paper");
2
- const { PathFinder } = require("../reader/PathFinder");
3
-
4
- let selectionStyle = function (item) {
5
- let style = {
6
- strokeColor: new paper.Color(180 / 255, 93 / 255, 60 / 255, 0.9),
7
- };
8
- if (item.closed) {
9
- style.fillColor = new paper.Color(
10
- new paper.Gradient([[item.fillColor, 0.38], new paper.Color(1, 1, 1)], false),
11
- item.bounds.topCenter,
12
- item.bounds.bottomCenter
13
- );
14
- }
15
- return style;
16
- };
17
-
18
- paper.Item.prototype.select = function (styleFunction) {
19
- this.mapSelected = !this.mapSelected;
20
- if (this.mapSelected && styleFunction !== undefined) {
21
- let style = styleFunction(this);
22
- this.orgStyle = {};
23
- for (const key in style) {
24
- this.orgStyle[key] = this[key];
25
- }
26
- this.style = style;
27
- } else {
28
- this.style = this.orgStyle;
29
- }
30
- };
31
-
32
- class Controls {
33
- constructor(renderer, reader, element, paperScope) {
34
- this.renderer = renderer;
35
- this.reader = reader;
36
- this.element = element;
37
- this.scope = paperScope;
38
- this.view = paperScope.view;
39
- this.element.onwheel = (event) => this.zoom(event);
40
- this.activateDrag();
41
- this.renderer.emitter.addEventListener("roomClick", (event) => this.selectRoom(event.detail));
42
- this.renderer.emitter.addEventListener("backgroundClick", () => this.deselectRoom());
43
- this.renderer.emitter.addEventListener("areaArrowClick", (event) => this.goToRoomArea(event.detail));
44
-
45
- let bounds = this.renderer.getBounds();
46
-
47
- this.view.center = bounds.center;
48
- this.view.zoom = Math.min(this.view.size.width / bounds.width, this.view.size.height / bounds.height);
49
- this.view.minZoom = this.view.zoom;
50
-
51
- this.pathFinder = new PathFinder(reader);
52
- }
53
-
54
- zoom(event) {
55
- event.preventDefault();
56
- let oldZoom = this.view.zoom;
57
- this.deltaZoom(event.deltaY > 0 ? 0.9 : 1.1);
58
- let viewPos = this.view.viewToProject(new paper.Point(event.offsetX, event.offsetY));
59
- let zoomScale = oldZoom / this.view.zoom;
60
- let centerAdjust = viewPos.subtract(this.view.center);
61
- let offset = viewPos.subtract(centerAdjust.multiply(zoomScale)).subtract(this.view.center);
62
- this.view.center = this.view.center.add(offset);
63
- }
64
-
65
- setZoom(value) {
66
- this.view.zoom = value;
67
- this.view.zoom = Math.min(Math.max(this.view.zoom, this.view.minZoom), 50);
68
- this.element.dispatchEvent(new CustomEvent("zoom", { detail: this.view }));
69
- }
70
-
71
- deltaZoom(delta) {
72
- this.setZoom(this.view.zoom * delta);
73
- }
74
-
75
- activateDrag() {
76
- let toolPan = new paper.Tool();
77
- toolPan.activate();
78
- toolPan.onMouseDrag = (event) => {
79
- this.toggleOptimizedDrag(true);
80
- this.element.style.cursor = "all-scroll";
81
- let delta = event.downPoint.subtract(event.point);
82
- this.view.translate(delta.negate());
83
- this.isDrag = true;
84
- this.element.dispatchEvent(new CustomEvent("drag", { detail: this.view }));
85
- };
86
- toolPan.onMouseDown = () => {
87
- this.isDrag = false;
88
- this.element.dispatchEvent(new CustomEvent("drag", { detail: this.view }));
89
- };
90
- toolPan.onMouseUp = () => {
91
- this.isDrag = false;
92
- this.element.style.cursor = "default";
93
- this.toggleOptimizedDrag(false);
94
- };
95
- }
96
-
97
- toggleOptimizedDrag(state) {
98
- if (!this.renderer.settings.optimizeDrag) {
99
- return;
100
- }
101
- if (state) {
102
- if (!this.isDrag) {
103
- this.renderer.linkLayer.visible = false;
104
- this.renderer.roomLayer.visible = false;
105
- this.renderer.rasterLayer.visible = true;
106
- }
107
- } else {
108
- this.renderer.linkLayer.visible = true;
109
- this.renderer.roomLayer.visible = true;
110
- this.renderer.rasterLayer.visible = false;
111
- }
112
- }
113
-
114
- selectRoom(room) {
115
- if (this.isDrag) {
116
- return false;
117
- }
118
- this.deselectRoom();
119
- this.renderer.renderPosition(room.id);
120
- room.render.select(selectionStyle);
121
- room.exitsRenders.forEach((render) => render.select(selectionStyle));
122
- this.selected = room;
123
-
124
- this.element.dispatchEvent(new CustomEvent("roomSelected", { detail: room }));
125
- }
126
-
127
- deselectRoom() {
128
- if (this.isDrag) {
129
- return false;
130
- }
131
- this.renderer.clearPosition();
132
- this.renderer.clearHighlight();
133
- if (this.selected !== undefined) {
134
- this.selected.render.select();
135
- this.selected.exitsRenders.forEach((render) => render.select());
136
- delete this.selected;
137
- this.element.dispatchEvent(new CustomEvent("roomDeselected"));
138
- }
139
- }
140
-
141
- centerRoom(id) {
142
- let room = this.renderer.area.getRoomById(id);
143
- if (room !== undefined) {
144
- this.centerOnItem(room.render);
145
- this.selectRoom(room);
146
- }
147
- }
148
-
149
- centerOnItem(item) {
150
- this.view.center = item.localToGlobal(item.position);
151
- }
152
-
153
- goToRoomArea(id) {
154
- let destRoom = this.reader.getRoomById(id);
155
- this.element.dispatchEvent(new CustomEvent("goToArea", { detail: destRoom }));
156
- }
157
-
158
- move(x, y) {
159
- this.view.translate(new paper.Point(x * 50, y * 50).negate());
160
- }
161
-
162
- renderPath(from, to, color) {
163
- let rooms = this.pathFinder.path(from, to)?.map(number => parseInt(number));
164
- if (rooms) {
165
- return this.renderer.renderPath(rooms, color);
166
- }
167
- }
168
- }
169
-
170
- module.exports = {
171
- Controls: Controls,
172
- };