-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1247 lines (1122 loc) · 39.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>Transmissions</title>
<style>
body,
html {
margin: 0;
padding: 0;
overflow: hidden;
width: 100%;
height: 100%;
}
#top-left-nav {
position: fixed;
top: 10px;
left: 10px;
display: flex;
align-items: center;
}
.top-right-nav {
position: fixed;
top: 10px;
right: 10px;
display: flex;
align-items: center;
}
.center-nav {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: flex;
align-items: center;
justify-content: center;
}
.ui-base {
display: flex;
align-items: center;
justify-content: center;
height: 38px;
background-color: lightgray;
border: 1px solid darkgray;
border-radius: 5px;
}
.mr-10 {
margin-right: 10px;
}
.ui-shadow {
box-shadow: 3px 3px 15px rgba(0, 0, 0, 0.25);
}
#changeMapButton:before {
background: url("mapicon.png");
width: 20px;
height: 19px;
background-size: cover;
display: block;
content: "";
}
#changeMapButton {
padding: 0 10px;
user-select: none;
cursor: pointer;
font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS",
sans-serif;
}
#addButton {
width: 38px;
cursor: pointer;
user-select: none; /* Prevent text selection */
}
/* Style for the pencil icon in edit mode */
#addButton.edit-mode:before {
content: "✓"; /* Pencil icon */
font-size: 20px;
}
/* Style for the checkmark icon (add mode) */
#addButton.add-mode:before {
content: "✓"; /* Checkmark icon */
font-size: 20px;
}
/* Style for the plus icon (default) */
#addButton:not(.edit-mode):not(.add-mode):before {
background: url("transmitter.svg");
width: 25px;
height: 25px;
background-size: cover;
display: block;
content: "";
}
#notification {
padding: 0 15px;
user-select: none;
cursor: pointer;
font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS",
sans-serif;
}
#bottom-left-nav {
position: absolute;
bottom: 20px;
left: 10px;
width: 150px;
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 10px;
}
#deleteButton {
display: none;
width: 38px;
cursor: pointer;
user-select: none;
}
#deleteButton:before {
content: "❌"; /* Trash can icon */
font-size: 20px;
}
#radiusSlider {
display: none;
}
#audioURLInput {
display: none;
padding: 0 10px;
box-sizing: border-box;
}
#deleteButton.visible,
#audioURLInput.visible,
#radiusSlider.visible {
display: flex;
}
#bottom-right-nav {
position: absolute;
bottom: 20px;
right: 10px;
display: flex;
flex-direction: row;
align-items: flex-end;
gap: 0px;
}
#uploadButton,
#downloadButton {
display: flex;
width: 38px;
cursor: pointer;
user-select: none;
}
#downloadButton:before {
content: "💾"; /* Down arrow icon */
font-size: 20px;
}
#uploadButton:before {
background: url("upload.svg");
width: 20px;
height: 20px;
background-size: cover;
content: "";
}
#downloadButton.visible,
#uploadButton.visible {
display: flex;
}
#gameURLInput {
display: none;
padding: 0 10px;
box-sizing: border-box;
}
#defaultElement {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
overflow: hidden;
}
#mapContainer {
position: relative;
width: 100%;
height: 100%;
}
#mapCanvas {
width: 100%;
height: 100%;
}
#circleContainer {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 200px; /* Adjust as needed */
height: 200px; /* Adjust as needed */
}
.circle {
position: absolute;
border: 1.5px solid #ff0000; /* Adjust the border color and width as needed */
border-radius: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div id="mapContainer">
<canvas id="mapCanvas"></canvas>
<div id="top-left-nav">
<div
id="addButton"
class="ui-base ui-shadow mr-10"
title="Place a transmitter"
></div>
<div
id="notification"
class="ui-base ui-shadow mr-10"
title="Place a transmitter"
>
Add Transmitter
</div>
</div>
<div id="circleContainer"></div>
<div id="map-nav" class="center-nav">
<div
id="changeMapButton"
class="ui-base ui-shadow"
title="Change map image"
></div>
</div>
<div id="bottom-left-nav">
<div
id="deleteButton"
class="ui-base ui-shadow mr-10"
title="Delete transmitter"
></div>
<input
type="text"
id="audioURLInput"
class="ui-base ui-shadow mr-10"
placeholder="Enter audio URL"
/>
<input
type="range"
min="20"
max="500"
value="200"
id="radiusSlider"
class="ui-shadow"
/>
</div>
<div id="bottom-right-nav">
<input
type="text"
id="gameURLInput"
class="ui-base ui-shadow mr-10"
placeholder="Load from drive URL"
/>
<div
id="downloadButton"
class="ui-base ui-shadow mr-10"
title="Save game to file"
></div>
<div
id="uploadButton"
class="ui-base ui-shadow mr-10"
title="Load game from file"
></div>
</div>
</div>
<input
type="file"
id="uploadFileInput"
style="display: none"
accept="application/json"
/>
<input
type="file"
id="mapFileInput"
style="display: none"
accept="image/*"
/>
<script>
// --- Constants ---
const DEFAULT_TRANSMITTER_RADIUS = 10;
const DEFAULT_TRANSMITTER_COLOR = "red";
const AVATAR_MOVE_STEP = 10;
// --- App State ---
const appState = {
addTransmitterMode: false,
editTransmitterMode: false,
notificationText: "Add Transmitter", // Text to display in the notification
mapState: {
image: new Image(),
scale: 1,
canvasWidth: 0,
canvasHeight: 0,
translateX: 0,
translateY: 0,
isDragging: false, // Map drag
minScale: 1,
maxScale: 3,
dragStartX: 0, // Start X position for the initial drag
dragStartY: 0, // Start Y position for the initial drag
},
transmitterState: {
selectedTransmitter: null,
isDraggingTransmitter: false, // Circle drag
currentSessionTransmitter: null,
placedTransmitters: [],
},
};
// --- Map State ---
appState.mapState = {
image: new Image(),
scale: 1,
translateX: 0,
translateY: 0,
isDragging: false, // Map drag
minScale: 1,
maxScale: 3,
dragStartX: 0, // Start X position for the initial drag
dragStartY: 0, // Start Y position for the initial drag
};
document.addEventListener("DOMContentLoaded", function () {
const splashCircle = document.getElementById("circleContainer");
const maxRadius = 150; // Set the radius of the largest circle
const numCircles = maxRadius / 10; // Adjust the number of circles as needed
if (appState.mapState.image.src === "") {
for (let i = 0; i < numCircles; i++) {
const radius = i * ((maxRadius - 30) / numCircles) + 30; // ((maxRadius + 60) / numCircles) * (i + 1); // i * ((120 - 60) / numCircles) + 60;
const circle = document.createElement("div");
circle.className = "circle";
circle.style.width = `${radius * 2 * 0.0625}rem`;
circle.style.height = `${radius * 2 * 0.0625}rem`;
circle.style.opacity = 1 - i / numCircles;
circle.style.left = "50%";
circle.style.top = "50%";
splashCircle.appendChild(circle);
}
} else {
splashCircle.style.display = "none";
}
});
// --- Transmitter State ---
appState.transmitterState = {
selectedTransmitter: null,
isDraggingTransmitter: false, // Circle drag
currentSessionTransmitter: null,
placedTransmitters: [],
};
// --- Avatar State ---
const avatarState = {
image: new Image(),
width: 36,
height: 45,
x: 0,
y: 0,
};
// --- DOM Elements ---
const canvas = document.getElementById("mapCanvas");
const ctx = canvas.getContext("2d");
const addButton = document.getElementById("addButton");
const notification = document.getElementById("notification");
const radiusSlider = document.getElementById("radiusSlider");
const audioURLInput = document.getElementById("audioURLInput");
const deleteButton = document.getElementById("deleteButton");
const downloadButton = document.getElementById("downloadButton");
const uploadButton = document.getElementById("uploadButton");
const uploadFileInput = document.getElementById("uploadFileInput");
// --- Initialization ---
const mapImageKey = "mapImageSrc";
const mapImageSrc = localStorage.getItem(mapImageKey);
console.log("Initial mapImageSrc from localStorage:", mapImageSrc);
function loadAvatar() {
avatarState.image.src = "lilguy.png";
avatarState.image.onload = () => {
avatarState.x =
appState.mapState.image.width / 2 - avatarState.width * 0.5;
avatarState.y =
appState.mapState.image.height / 2 - avatarState.height * 0.5;
console.log(
"canvas.width:",
canvas.width,
"canvas.height:",
canvas.height
);
console.log(
"window.innerWidth:",
window.innerWidth,
"window.innerHeight:",
window.innerHeight
);
console.log(
"Avatar loaded and positioned at:",
avatarState.x,
avatarState.y
);
console.log("appState.mapState.scale:", appState.mapState.scale);
console.log(
"appState.mapState.translateX:",
appState.mapState.translateX,
"appState.mapState.translateY:",
appState.mapState.translateY
);
draw();
};
}
function updateUIVisibility() {
const isVisible = appState.mapState.image.src !== "";
console.log("updateUIVisibility - isVisible:", isVisible);
addButton.style.display = isVisible ? "flex" : "none";
notification.style.display = isVisible ? "flex" : "none";
const mapNav = document.getElementById("map-nav");
if (isVisible) {
mapNav.classList.remove("center-nav");
mapNav.classList.add("top-right-nav");
} else {
mapNav.classList.remove("top-right-nav");
mapNav.classList.add("center-nav");
}
const bottomRightNav = document.getElementById("bottom-right-nav");
bottomRightNav.style.display =
appState.addTransmitterMode || appState.editTransmitterMode
? "none"
: "flex";
}
function removeSplashCircle(mapImageSrc) {
const splashCircle = document.getElementById("circleContainer");
splashCircle.style.display = "none";
}
function loadMapImage() {
console.log("Map image loaded successfully");
removeSplashCircle();
resizeCanvas();
calculateMinScale();
centerImage();
updateUIVisibility();
draw();
loadAvatar();
}
if (mapImageSrc) {
console.log("Loading map image from localStorage:", mapImageSrc);
appState.mapState.image.src = mapImageSrc;
console.log(
"appState.mapState.image.src set to:",
appState.mapState.image.src
);
console.log("appState set to:", appState);
appState.mapState.image.onload = () => {
console.log("Map image loaded successfully");
loadMapImage();
};
appState.mapState.image.onerror = (error) => {
console.error("Error loading map image:", error);
localStorage.removeItem(mapImageKey); // Clear the invalid image from localStorage
showMapUploadDialog(); // Prompt the user to upload a new map image
};
} else {
console.log(
"No map image found in localStorage, showing upload dialog"
);
showMapUploadDialog();
}
document
.getElementById("mapFileInput")
.addEventListener("change", (event) => {
const input = event.target;
const file = input.files[0];
if (!file) {
console.error("No file selected");
return;
}
const reader = new FileReader();
reader.onload = (ev) => {
const mapImageSrc = ev.target.result;
console.log("Map image loaded from file input:", mapImageSrc);
localStorage.setItem(mapImageKey, mapImageSrc);
appState.mapState.image.src = mapImageSrc;
console.log(
"appState.mapState.image.src set to:",
appState.mapState.image.src
);
console.log("appState set to:", appState);
appState.mapState.image.onload = () => {
loadMapImage();
};
appState.mapState.image.onerror = (error) => {
console.error("Error loading map image from file input:", error);
localStorage.removeItem(mapImageKey); // Clear the invalid image from localStorage
showMapUploadDialog(); // Prompt the user to upload a new map image
};
};
// Ensure the file variable is defined and passed correctly
if (file) {
console.log("File selected:", file);
reader.readAsDataURL(file);
} else {
console.error("No file selected for upload.");
}
});
updateUIVisibility();
document
.getElementById("changeMapButton")
.addEventListener("click", () => {
console.log("Change map button clicked");
document.getElementById("mapFileInput").click();
});
function showMapUploadDialog() {
console.log("Showing map upload dialog");
document.getElementById("mapFileInput").click();
}
// --- Helper Functions ---
function playTransmitterAudio(transmitter) {
if (transmitter.audioURL) {
const audio = new Audio(transmitter.audioURL);
transmitter.audio = audio;
transmitter.audio.loop = true;
transmitter.audio.volume = transmitter.distanceToAvatar;
transmitter.audio.play().catch((error) => {
console.error("Error playing audio:", error);
});
}
}
function stopTransmitterAudio(transmitter) {
if (transmitter.audio) {
transmitter.audio.pause();
transmitter.audio = null;
}
}
function resizeCanvas() {
const gameArea = document.getElementById("mapCanvas");
canvas.width = gameArea.clientWidth;
canvas.height = gameArea.clientHeight;
// Save canvas dimensions in appState
appState.mapState.canvasWidth = canvas.width;
appState.mapState.canvasHeight = canvas.height;
}
function calculateMinScale() {
const canvasAspectRatio = canvas.width / canvas.height;
const imageAspectRatio =
appState.mapState.image.width / appState.mapState.image.height;
appState.mapState.minScale =
canvasAspectRatio > imageAspectRatio
? canvas.width / appState.mapState.image.width
: canvas.height / appState.mapState.image.height;
appState.mapState.scale = appState.mapState.minScale;
}
function centerImage() {
appState.mapState.translateX =
(canvas.width -
appState.mapState.image.width * appState.mapState.scale) /
2;
appState.mapState.translateY =
(canvas.height -
appState.mapState.image.height * appState.mapState.scale) /
2;
}
function getEventCoordinates(event) {
const clientX = event.clientX - appState.mapState.translateX;
const clientY = event.clientY - appState.mapState.translateY;
const x = clientX / appState.mapState.scale;
const y = clientY / appState.mapState.scale;
return { x: x, y: y };
}
function createTransmitter(x, y) {
return {
x: x,
y: y,
radius: parseInt(radiusSlider.value),
fillStyle: DEFAULT_TRANSMITTER_COLOR,
audioURL: "",
audio: null,
distanceToAvatar: 0,
};
}
function handleAudioURLChange(event) {
console.log("handleAudioURLChange called");
console.log("event.target.value:", event.target.value);
console.log(
"appState.editTransmitterMode:",
appState.editTransmitterMode
);
console.log(
"appState.transmitterState.selectedTransmitter:",
appState.transmitterState.selectedTransmitter
);
if (appState.transmitterState.selectedTransmitter) {
appState.transmitterState.selectedTransmitter.audioURL =
event.target.value;
console.log("Audio URL:", event.target.value);
} else {
console.log("No transmitter selected");
}
}
function deleteSelectedTransmitter() {
if (appState.transmitterState.selectedTransmitter) {
stopTransmitterAudio(appState.transmitterState.selectedTransmitter);
appState.transmitterState.placedTransmitters =
appState.transmitterState.placedTransmitters.filter(
(transmitter) =>
transmitter !== appState.transmitterState.selectedTransmitter
);
appState.transmitterState.selectedTransmitter = null;
appState.transmitterState.currentSessionTransmitter = null;
}
// set appstate.addTransmitterMode to false
appState.addTransmitterMode = false;
appState.editTransmitterMode = false;
// Update UI elements
addButton.classList.remove("edit-mode");
radiusSlider.classList.remove("visible");
audioURLInput.classList.remove("visible");
deleteButton.classList.remove("visible");
addButton.classList.remove("add-mode");
updateNotificationVisibility();
draw();
}
// --- Drawing Functions ---
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.translate(
appState.mapState.translateX,
appState.mapState.translateY
);
ctx.scale(appState.mapState.scale, appState.mapState.scale);
drawMap();
drawTransmitters();
drawAvatar();
ctx.restore();
}
function drawMap() {
ctx.drawImage(appState.mapState.image, 0, 0);
}
function drawTransmitters() {
appState.transmitterState.placedTransmitters.forEach(
(transmitter, index) => {
drawTransmitter(transmitter, index);
const distance = calculateDistance(
avatarState.x,
avatarState.y,
transmitter.x,
transmitter.y,
transmitter.radius
);
transmitter.distanceToAvatar = distance;
if (transmitter.audio) {
transmitter.audio.volume = distance;
}
}
);
}
function calculateDistance(x1, y1, x2, y2, radius) {
const distance = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
if (distance > radius) {
return 0;
}
return 1 - distance / radius;
}
function drawConcentricCircles(
ctx,
centerX,
centerY,
radius,
color,
numCircles,
lineWidth
) {
for (let i = 0; i < numCircles; i++) {
const currRadius = (radius / numCircles) * (i + 1);
ctx.beginPath();
ctx.arc(transmitter.x, transmitter.y, currRadius, 0, 2 * Math.PI);
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth; // Set the line width
ctx.stroke();
}
}
function drawTransmitter(transmitter, seed) {
// Style Parameters
const colors = [
"#DC143C", // Crimson
"#32CD32", // LimeGreen
"#00BFFF", // DeepSkyBlue
"#8B008B", // DarkMagenta
"#FF69B4", // HotPink
"#8A2BE2", // BlueViolet
"#006400", // DarkGreen
"#FF8C00", // DarkOrange
"#4682B4", // SteelBlue
"#B8860B", // DarkGoldenRod
];
//const seed = appState.transmitterState.placedTransmitters.length;
const colorIndex = seed % colors.length;
const circleColor = "#DC143C"; //colors[colorIndex];
const numCircles = transmitter.radius / 12; // Number of concentric circles
const lineWidth = 2; // Set the desired line width
// Function to convert hex color to rgba
function hexToRgba(hex, alpha) {
const bigint = parseInt(hex.slice(1), 16);
const r = (bigint >> 16) & 255;
const g = (bigint >> 8) & 255;
const b = bigint & 255;
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
// Draw concentric circles
for (let i = 0; i < numCircles; i++) {
const radius = (transmitter.radius / numCircles) * (i + 1);
const opacity = 1 - i / numCircles;
const newColor = hexToRgba(circleColor, opacity);
ctx.beginPath();
ctx.arc(transmitter.x, transmitter.y, radius, 0, 2 * Math.PI);
ctx.strokeStyle = newColor;
ctx.lineWidth = lineWidth; // Set the line width
ctx.stroke();
}
}
function drawAvatar() {
const aw = avatarState.width / appState.mapState.scale;
const ah = avatarState.height / appState.mapState.scale;
const x = avatarState.x - aw / 2;
const y = avatarState.y - ah / 2;
ctx.drawImage(avatarState.image, x, y, aw, ah);
}
// --- Event Handlers ---
function handleWheel(event) {
event.preventDefault();
const scaleFactor = event.deltaY > 0 ? 0.9 : 1.1;
const newScale = Math.max(
appState.mapState.minScale,
Math.min(
appState.mapState.maxScale,
appState.mapState.scale * scaleFactor
)
);
const { x: mouseX, y: mouseY } = getEventCoordinates(event);
appState.mapState.translateX -=
mouseX * (newScale - appState.mapState.scale);
appState.mapState.translateY -=
mouseY * (newScale - appState.mapState.scale);
appState.mapState.scale = newScale;
// Ensure image covers viewport
const scaledWidth =
appState.mapState.image.width * appState.mapState.scale;
const scaledHeight =
appState.mapState.image.height * appState.mapState.scale;
if (appState.mapState.translateX > 0) appState.mapState.translateX = 0;
if (appState.mapState.translateY > 0) appState.mapState.translateY = 0;
if (appState.mapState.translateX + scaledWidth < canvas.width)
appState.mapState.translateX = canvas.width - scaledWidth;
if (appState.mapState.translateY + scaledHeight < canvas.height)
appState.mapState.translateY = canvas.height - scaledHeight;
// If image smaller than viewport in both dimensions, center it.
if (scaledWidth < canvas.width && scaledHeight < canvas.height) {
centerImage();
}
draw();
}
function isEventTargetingUI(event) {
return (
event.target === addButton ||
event.target === notification ||
event.target === radiusSlider
);
}
function handleMouseDown(event) {
const { x, y } = getEventCoordinates(event);
if (isEventTargetingUI(event)) {
return; // Ignore clicks on UI elements
}
// Check if a placed transmitter is clicked
appState.transmitterState.selectedTransmitter = null;
for (
let i = appState.transmitterState.placedTransmitters.length - 1;
i >= 0;
i--
) {
const transmitter = appState.transmitterState.placedTransmitters[i];
if (isInsideCircle({ x, y }, transmitter)) {
appState.transmitterState.selectedTransmitter = transmitter;
appState.transmitterState.isDraggingTransmitter = true;
break;
}
}
// Check if we clicked currentSessionTransmitter
if (
!appState.transmitterState.selectedTransmitter &&
appState.transmitterState.currentSessionTransmitter &&
isInsideCircle(
{ x, y },
appState.transmitterState.currentSessionTransmitter
)
) {
appState.transmitterState.selectedTransmitter =
appState.transmitterState.currentSessionTransmitter;
appState.transmitterState.isDraggingTransmitter = true;
}
if (appState.transmitterState.isDraggingTransmitter) {
// If a transmitter is selected, enter edit mode
enterEditTransmitterMode(
appState.transmitterState.selectedTransmitter
);
} else {
appState.mapState.isDragging = true;
appState.mapState.dragStartX =
event.clientX - appState.mapState.translateX;
appState.mapState.dragStartY =
event.clientY - appState.mapState.translateY;
}
}
function handleMouseMove(event) {
const { x, y } = getEventCoordinates(event);
if (appState.transmitterState.isDraggingTransmitter) {
moveTransmitter(appState.transmitterState.selectedTransmitter, x, y);
return;
}
if (!appState.mapState.isDragging) return;
appState.mapState.translateX =
event.clientX - appState.mapState.dragStartX;
appState.mapState.translateY =
event.clientY - appState.mapState.dragStartY;
// Ensure image covers viewport
const scaledWidth =
appState.mapState.image.width * appState.mapState.scale;
const scaledHeight =
appState.mapState.image.height * appState.mapState.scale;
if (appState.mapState.translateX > 0) appState.mapState.translateX = 0;
if (appState.mapState.translateY > 0) appState.mapState.translateY = 0;
if (appState.mapState.translateX + scaledWidth < canvas.width)
appState.mapState.translateX = canvas.width - scaledWidth;
if (appState.mapState.translateY + scaledHeight < canvas.height)
appState.mapState.translateY = canvas.height - scaledHeight;
draw();
}
function isInsideCircle(point, circle) {
const circleTop = circle.y - circle.radius;
const circleBottom = circle.y + circle.radius;
const circleLeft = circle.x - circle.radius;
const circleRight = circle.x + circle.radius;
return (
point.x >= circleLeft &&
point.x <= circleRight &&
point.y >= circleTop &&
point.y <= circleBottom
);
}
function handleMouseUp(event) {
if (appState.transmitterState.isDraggingTransmitter) {
appState.transmitterState.isDraggingTransmitter = false;
} else if (appState.addTransmitterMode) {
const { x, y } = getEventCoordinates(event);
// If there's a current session transmitter, update its position
if (appState.transmitterState.currentSessionTransmitter) {
moveTransmitter(
appState.transmitterState.currentSessionTransmitter,
x,
y
);
}
draw();
} else if (appState.editTransmitterMode && !isEventTargetingUI(event)) {
exitEditTransmitterMode();
}
appState.mapState.isDragging = false;
}
function handleMouseLeave() {
appState.mapState.isDragging = false;
appState.transmitterState.isDraggingTransmitter = false;
}
function handleResize() {
resizeCanvas();
calculateMinScale();
if (appState.mapState.scale < appState.mapState.minScale) {
appState.mapState.scale = appState.mapState.minScale;
}
centerImage();
draw();
}
function handleKeyDown(event) {
if (event.key === "Enter") {
if (appState.addTransmitterMode) {
// Exit addTransmitterMode without pushing the current session transmitter again
appState.transmitterState.currentSessionTransmitter = null; // Reset current session transmitter
draw();
toggleAddTransmitterMode();
}
if (appState.editTransmitterMode) {
exitEditTransmitterMode();
}
} else {
moveAvatar(event);
}
}
function handleRadiusChange(event) {
if (
appState.editTransmitterMode &&
appState.transmitterState.selectedTransmitter
) {
appState.transmitterState.selectedTransmitter.radius = parseInt(
event.target.value