mudmapper.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. var ctx;
  2. var canvas;
  3. var lastX=0;
  4. var lastY=0;
  5. var dragStart = null;
  6. var dragged = false;
  7. var scaleFactor = 1.1;
  8. var roomSize = {width: 64, height: 64};
  9. var spacing = 32;
  10. var rooms = [
  11. {"id":0,"x":16,"y":16,"name":"temple"},
  12. {"id":1,"x":16,"y":112,"name":"street"},
  13. {"id":2,"x":112,"y":112,"name":"street"},
  14. {"id":3,"x":208,"y":112,"name":"street"},
  15. {"id":4,"x":208,"y":208,"name":"guild"},
  16. {"id":5,"x":304,"y":112,"name":"street"},
  17. {"id":6,"x":400,"y":112,"name":"street"},
  18. {"id":7,"x":208,"y":16,"name":"shop"},
  19. {"id":8,"x":112,"y":208,"name":"street"},
  20. {"id":9,"x":112,"y":304,"name":"old well"},
  21. {"id":10,"x":208,"y":304,"name":"bank"},
  22. {"id":11,"x":112,"y":400,"name":"post office"},
  23. {"id":12,"x":16,"y":304,"name":"street"},
  24. {"id":13,"x":16,"y":400,"name":"street"},
  25. {"id":14,"x":16,"y":496,"name":"street"},
  26. {"id":15,"x":16,"y":592,"name":"street"},
  27. {"id":16,"x":16,"y":688,"name":"south gate"},
  28. {"id":17,"x":-80,"y":304,"name":"street"},
  29. {"id":18,"x":-176,"y":304,"name":"street"},
  30. {"id":19,"x":-176,"y":400,"name":"cemetary"},
  31. {"id":21,"x":-176,"y":112,"name":"street"},
  32. {"id":23,"x":-368,"y":112,"name":"street"},
  33. {"id":24,"x":-464,"y":112,"name":"west gate"},
  34. {"id":25,"x":-368,"y":16,"name":"street"},
  35. {"id":26,"x":-272,"y":-80,"name":"street"},
  36. {"id":27,"x":-368,"y":-80,"name":"mikes"},
  37. {"id":28,"x":-176,"y":16,"name":"merchantrow"},
  38. {"id":29,"x":-80,"y":16,"name":"smithy"},
  39. {"id":30,"x":-80,"y":-80,"name":"forge"},
  40. {"id":31,"x":-176,"y":-80,"name":"armourer"},
  41. {"id":32,"x":-80,"y":112,"name":"street"},
  42. {"id":33,"x":-272,"y":112,"name":"street"},
  43. ];
  44. var loadedRooms = JSON.parse(localStorage.getItem("map-rooms"));
  45. if(loadedRooms != null) {
  46. rooms = loadedRooms;
  47. }
  48. var links = [
  49. {"start":0,"end":1,"exit":"s","enter":"n"},
  50. {"start":1,"end":2,"exit":"e","enter":"w"},
  51. {"start":3,"end":2,"exit":"w","enter":"e"},
  52. {"start":4,"end":3,"exit":"n","enter":"s"},
  53. {"start":3,"end":5,"exit":"e","enter":"w"},
  54. {"start":5,"end":6,"exit":"e","enter":"w"},
  55. {"start":3,"end":7,"exit":"n","enter":"s"},
  56. {"start":2,"end":8,"exit":"s","enter":"n"},
  57. {"start":8,"end":9,"exit":"s","enter":"n"},
  58. {"start":9,"end":10,"exit":"e","enter":"w"},
  59. {"start":9,"end":11,"exit":"s","enter":"n"},
  60. {"start":9,"end":12,"exit":"w","enter":"e"},
  61. {"start":12,"end":13,"exit":"s","enter":"n"},
  62. {"start":13,"end":14,"exit":"s","enter":"n"},
  63. {"start":14,"end":15,"exit":"s","enter":"n"},
  64. {"start":15,"end":16,"exit":"s","enter":"n"},
  65. {"start":12,"end":17,"exit":"w","enter":"e"},
  66. {"start":17,"end":18,"exit":"w","enter":"e"},
  67. {"start":18,"end":19,"exit":"s","enter":"n"},
  68. {"start":23,"end":24,"exit":"w","enter":"e"},
  69. {"start":23,"end":25,"exit":"n","enter":"s"},
  70. {"start":25,"end":26,"exit":"ne","enter":"sw"},
  71. {"start":25,"end":27,"exit":"n","enter":"s"},
  72. {"start":26,"end":28,"exit":"se","enter":"nw"},
  73. {"start":28,"end":29,"exit":"e","enter":"w"},
  74. {"start":29,"end":30,"exit":"n","enter":"s"},
  75. {"start":30,"end":31,"exit":"w","enter":"e"},
  76. {"start":31,"end":28,"exit":"s","enter":"n"},
  77. {"start":28,"end":21,"exit":"s","enter":"n"},
  78. {"start":21,"end":32,"exit":"e","enter":"w"},
  79. {"start":32,"end":29,"exit":"n","enter":"s"},
  80. {"start":32,"end":1,"exit":"e","enter":"w"},
  81. {"start":23,"end":33,"exit":"e","enter":"w"},
  82. {"start":33,"end":21,"exit":"e","enter":"w"},
  83. ];
  84. var loadedLinks = JSON.parse(localStorage.getItem("map-links"));
  85. if(loadedLinks != null) {
  86. links = loadedLinks;
  87. }
  88. var activeRoom = rooms[0];
  89. var movingRoom = false;
  90. var roomToMove = null;
  91. var moveLink = false;
  92. var linkToMove = null;
  93. function resizeCanvas() {
  94. // look up the size the canvas is being displayed
  95. const width = canvas.clientWidth;
  96. const height = canvas.clientHeight;
  97. // If its resolution does not match change it
  98. if (canvas.width !== width || canvas.height !== height) {
  99. canvas.width = width;
  100. canvas.height = height;
  101. return true;
  102. }
  103. return false;
  104. }
  105. function clearMap() {
  106. var p1 = ctx.transformedPoint(0,0);
  107. var p2 = ctx.transformedPoint(canvas.width,canvas.height);
  108. ctx.clearRect(p1.x,p1.y,p2.x-p1.x,p2.y-p1.y);
  109. ctx.save();
  110. ctx.setTransform(1,0,0,1,0,0);
  111. ctx.clearRect(0,0,canvas.width,canvas.height);
  112. ctx.restore();
  113. }
  114. function drawRoom(room) {
  115. if(room.id == activeRoom.id) {
  116. ctx.fillStyle = "#FFFFFF";
  117. } else {
  118. ctx.fillStyle = "#DDDDDD";
  119. }
  120. ctx.strokeStyle = "#000000";
  121. ctx.fillRect(room.x,room.y,roomSize.width,roomSize.height);
  122. ctx.strokeRect(room.x,room.y,roomSize.width,roomSize.height);
  123. ctx.fillStyle = "#000000";
  124. ctx.fillText(room.name, room.x + 5, room.y + 11);
  125. }
  126. function getRoomExit(room, direction) {
  127. var exit = {x:0, y:0};
  128. var exitOffset = {x:0, y:0};
  129. switch(direction) {
  130. case "n":
  131. exitOffset.x = roomSize.width / 2;
  132. exitOffset.y = 0;
  133. break;
  134. case "s":
  135. exitOffset.x = roomSize.width / 2;
  136. exitOffset.y = roomSize.height;
  137. break;
  138. case "e":
  139. exitOffset.x = roomSize.width;
  140. exitOffset.y = roomSize.height / 2;
  141. break;
  142. case "w":
  143. exitOffset.x = 0;
  144. exitOffset.y = roomSize.height / 2;
  145. break;
  146. case "nw":
  147. exitOffset.x = 0;
  148. exitOffset.y = 0;
  149. break;
  150. case "ne":
  151. exitOffset.x = roomSize.width;
  152. exitOffset.y = 0;
  153. break;
  154. case "sw":
  155. exitOffset.x = 0;
  156. exitOffset.y = roomSize.height;
  157. break;
  158. case "se":
  159. exitOffset.x = roomSize.width;
  160. exitOffset.y = roomSize.height;
  161. break;
  162. }
  163. exit.x = exitOffset.x + room.x;
  164. exit.y = exitOffset.y + room.y;
  165. return exit;
  166. }
  167. function getWiggle(start, end, exit, enter) {
  168. var wiggle = {start: {x: 0, y: 0}, end: {x:0, y:0}};
  169. switch(exit) {
  170. case "n":
  171. wiggle.start.x = -16;
  172. wiggle.start.y = -16;
  173. break;
  174. case "s":
  175. wiggle.start.x = -16;
  176. wiggle.start.y = 16;
  177. break;
  178. case "e":
  179. wiggle.start.x = 16;
  180. wiggle.start.y = -16;
  181. break;
  182. case "w":
  183. wiggle.start.x = -16;
  184. wiggle.start.y = -16;
  185. break;
  186. case "ne":
  187. wiggle.start.x = 0;
  188. wiggle.start.y = -16;
  189. break;
  190. case "nw":
  191. wiggle.start.x = 0;
  192. wiggle.start.y = -16;
  193. break;
  194. case "se":
  195. wiggle.start.x = 16;
  196. wiggle.start.y = 0;
  197. break;
  198. case "sw":
  199. wiggle.start.x = -16;
  200. wiggle.start.y = 0;
  201. break;
  202. }
  203. switch(enter) {
  204. case "n":
  205. wiggle.end.x = 16;
  206. wiggle.end.y = -16;
  207. break;
  208. case "s":
  209. wiggle.end.x = 16;
  210. wiggle.end.y = 16;
  211. break;
  212. case "e":
  213. wiggle.end.x = 16;
  214. wiggle.end.y = 16;
  215. break;
  216. case "w":
  217. wiggle.end.x = -16;
  218. wiggle.end.y = 16;
  219. break;
  220. case "ne":
  221. wiggle.end.x = 16;
  222. wiggle.end.y = 0;
  223. break;
  224. case "se":
  225. wiggle.end.x = 0;
  226. wiggle.end.y = 16;
  227. break;
  228. case "nw":
  229. wiggle.end.x = -16;
  230. wiggle.end.y = 0;
  231. break;
  232. case "sw":
  233. wiggle.end.x = 0;
  234. wiggle.end.y = 16;
  235. break;
  236. }
  237. return wiggle;
  238. }
  239. function getRoomById(roomId) {
  240. for(var roomIndex in rooms) {
  241. var room = rooms[roomIndex];
  242. if(room.id == roomId) {
  243. return room;
  244. }
  245. }
  246. }
  247. function drawLink(link, rooms) {
  248. var startRoom = getRoomById(link.start);
  249. var endRoom = getRoomById(link.end);
  250. if(startRoom == null || endRoom == null) {
  251. //TODO: delete link
  252. return;
  253. }
  254. var start = getRoomExit(startRoom, link.exit);
  255. var end = getRoomExit(endRoom, link.enter);
  256. var wiggle = getWiggle(start, end, link.exit, link.enter);
  257. ctx.beginPath();
  258. ctx.moveTo(start.x,start.y);
  259. ctx.bezierCurveTo(start.x+wiggle.start.x,start.y+wiggle.start.y,end.x+wiggle.end.x,end.y+wiggle.end.y,end.x,end.y);
  260. ctx.stroke();
  261. }
  262. function drawLinkToPoint(link, point) {
  263. var startRoom = getRoomById(link.start);
  264. var endRoom = getRoomById(link.end);
  265. if(startRoom == null || endRoom == null) {
  266. //TODO: delete link
  267. return;
  268. }
  269. var start = getRoomExit(startRoom, link.exit);
  270. var end = getRoomExit(endRoom, link.enter);
  271. var wiggle = getWiggle(start, end, link.exit, link.enter);
  272. ctx.beginPath();
  273. ctx.moveTo(start.x,start.y);
  274. ctx.bezierCurveTo(start.x+wiggle.start.x,start.y+wiggle.start.y,end.x+wiggle.end.x,end.y+wiggle.end.y,end.x,end.y);
  275. ctx.stroke();
  276. }
  277. function getOppositeDir(dir) {
  278. switch(dir) {
  279. case "n": return "s";
  280. case "s": return "n";
  281. case "e": return "w";
  282. case "w": return "e";
  283. case "ne": return "sw";
  284. case "nw": return "se";
  285. case "se": return "nw";
  286. case "sw": return "ne";
  287. case "u": return "d";
  288. case "d": return "u";
  289. }
  290. }
  291. function drawMap() {
  292. clearMap();
  293. rooms.map(room => drawRoom(room));
  294. links.map(link => drawLink(link, rooms));
  295. if(moveLink) {
  296. drawLink(linkToMove, rooms);
  297. }
  298. }
  299. function handleMouseDown(evt) {
  300. if(movingRoom) {
  301. movingRoom = false;
  302. localStorage.setItem("map-rooms", JSON.stringify(rooms));
  303. return;
  304. }
  305. if(moveLink && activeRoom.id != linkToMove.start) {
  306. moveLink = false;
  307. links.push(linkToMove);
  308. localStorage.setItem("map-links", JSON.stringify(links));
  309. return;
  310. }
  311. document.body.style.mozUserSelect = 'none';
  312. document.body.style.webkitUserSelect = 'none';
  313. document.body.style.userSelect = 'none';
  314. lastX = evt.offsetX || (evt.pageX - canvas.offsetLeft);
  315. lastY = evt.offsetY || (evt.pageY - canvas.offsetTop);
  316. dragStart = ctx.transformedPoint(lastX,lastY);
  317. dragged = false;
  318. }
  319. function checkRoomHover(room, mouseTransform) {
  320. if(mouseTransform.x >= room.x && mouseTransform.x <= room.x + roomSize.width &&
  321. mouseTransform.y >= room.y && mouseTransform.y <= room.y + roomSize.height)
  322. {
  323. setContextToRoom(room);
  324. drawMap();
  325. }
  326. }
  327. function handleMouseMove(evt) {
  328. if(movingRoom) {
  329. newX = evt.offsetX || (evt.pageX - canvas.offsetLeft);
  330. newY = evt.offsetY || (evt.pageY - canvas.offsetTop);
  331. var pt = ctx.transformedPoint(newX - (roomSize.width / 2),newY - (roomSize.height / 2));
  332. roomToMove.x = pt.x;
  333. roomToMove.y = pt.y;
  334. drawMap();
  335. return;
  336. }
  337. if(moveLink) {
  338. linkToMove.end = activeRoom.id;
  339. }
  340. setContextToWorld();
  341. var mouseTransform = ctx.transformedPoint(event.clientX, event.clientY);
  342. rooms.map(room => checkRoomHover(room, mouseTransform));
  343. lastX = evt.offsetX || (evt.pageX - canvas.offsetLeft);
  344. lastY = evt.offsetY || (evt.pageY - canvas.offsetTop);
  345. if (dragStart){
  346. dragged = true;
  347. var pt = ctx.transformedPoint(lastX,lastY);
  348. ctx.translate(pt.x-dragStart.x,pt.y-dragStart.y);
  349. drawMap();
  350. }
  351. hideContextMenu();
  352. }
  353. function handleMouseUp(evt) {
  354. dragStart = null;
  355. dragged = false;
  356. }
  357. function zoomMap(clicks){
  358. var pt = ctx.transformedPoint(lastX,lastY);
  359. ctx.translate(pt.x,pt.y);
  360. var factor = Math.pow(scaleFactor,clicks);
  361. ctx.scale(factor,factor);
  362. ctx.translate(-pt.x,-pt.y);
  363. drawMap();
  364. }
  365. function handleScroll(evt){
  366. var delta = evt.wheelDelta ? evt.wheelDelta/40 : evt.detail ? -evt.detail : 0;
  367. if (delta) {
  368. zoomMap(delta);
  369. }
  370. return evt.preventDefault() && false;
  371. };
  372. function showContextMenu(x, y) {
  373. var menu = document.getElementsByClassName("context-menu-container")[0];
  374. menu.style.display="block";
  375. menu.style.top = y-15 + "px";
  376. menu.style.left = x-15 + "px";
  377. }
  378. function hideContextMenu() {
  379. var menu = document.getElementsByClassName("context-menu-container")[0];
  380. menu.style.display="none";
  381. }
  382. function setContextToWorld() {
  383. document.getElementById("world-menu").style.display= "block";
  384. document.getElementById("room-menu").style.display= "none";
  385. }
  386. function setContextToRoom(room) {
  387. activeRoom = room;
  388. document.getElementById("world-menu").style.display= "none";
  389. var roomMenu = document.getElementById("room-menu");
  390. roomMenu.style.display= "block";
  391. roomMenu.getElementsByClassName("title")[0].innerHTML = room.name;
  392. }
  393. function handleContextMenu(event) {
  394. event.preventDefault();
  395. showContextMenu(event.clientX, event.clientY);
  396. dragStart = null;
  397. dragged = false;
  398. }
  399. function handleResize(event) {
  400. var canvas = document.getElementById("mapper");
  401. var ctx = canvas.getContext("2d");
  402. ctx.setTransform(1,0,0,1,0,0);
  403. resizeCanvas();
  404. drawMap();
  405. }
  406. function setActiveRoom(input) {
  407. for(var linkId in links) {
  408. var link = links[linkId];
  409. if(activeRoom.id == link.start && input == link.exit) {
  410. activeRoom = getRoomById(link.end);
  411. return true;
  412. }
  413. if(activeRoom.id == link.end && input == link.enter) {
  414. activeRoom = getRoomById(link.start);
  415. return true;
  416. }
  417. }
  418. return false;
  419. }
  420. function getNewRoomIndex() {
  421. var newIndex = 0;
  422. for(var roomId in rooms) {
  423. if(rooms[roomId].id > newIndex) {
  424. newIndex = rooms[roomId].id;
  425. }
  426. }
  427. return newIndex + 1;
  428. }
  429. function setOrCreateActiveRoom(input) {
  430. if(setActiveRoom(input)) {
  431. return;
  432. }
  433. var newRoom = {id: getNewRoomIndex(), x: activeRoom.x, y: activeRoom.y, name:"newroom"};
  434. if(input.indexOf("n") != -1) {
  435. newRoom.y = activeRoom.y - (spacing + roomSize.height);
  436. }
  437. if(input.indexOf("s") != -1) {
  438. newRoom.y = activeRoom.y + (spacing + roomSize.height);
  439. }
  440. if(input.indexOf("w") != -1) {
  441. newRoom.x = activeRoom.x - (spacing + roomSize.width);
  442. }
  443. if(input.indexOf("e") != -1) {
  444. newRoom.x = activeRoom.x + (spacing + roomSize.width);
  445. }
  446. rooms.push(newRoom);
  447. links.push({start:activeRoom.id, end:newRoom.id, exit:input, enter:getOppositeDir(input)})
  448. activeRoom = newRoom;
  449. localStorage.setItem("map-rooms", JSON.stringify(rooms));
  450. localStorage.setItem("map-links", JSON.stringify(links));
  451. }
  452. function keyboardListener(event) {
  453. var key = event.keyCode;
  454. switch(key) {
  455. case 78:
  456. inputBuffer = "n";
  457. break;
  458. case 83:
  459. inputBuffer = "s";
  460. break;
  461. case 69:
  462. inputBuffer += "e";
  463. break;
  464. case 87:
  465. inputBuffer += "w";
  466. break;
  467. case 38:
  468. setActiveRoom("n");
  469. inputBuffer = "";
  470. break;
  471. case 40:
  472. setActiveRoom("s");
  473. inputBuffer = "";
  474. break;
  475. case 39:
  476. setActiveRoom("e");
  477. inputBuffer = "";
  478. break;
  479. case 37:
  480. setActiveRoom("w");
  481. inputBuffer = "";
  482. break;
  483. case 13:
  484. if(inputBuffer != "") {
  485. setOrCreateActiveRoom(inputBuffer);
  486. }
  487. inputBuffer = "";
  488. break;
  489. default:
  490. inputBuffer = "";
  491. console.warn("Unhandled keypress: ", key);
  492. break;
  493. }
  494. drawMap();
  495. }
  496. function showModal(modalId) {
  497. var modal = document.getElementById(modalId);
  498. modal.parentNode.style.display = "block";
  499. modal.style.display = "block";
  500. }
  501. function closeModal(modalId) {
  502. var modal = document.getElementById(modalId);
  503. modal.parentNode.style.display = "none";
  504. modal.style.display = "none";
  505. }
  506. window.addEventListener("resize", handleResize, false);
  507. document.addEventListener("DOMContentLoaded", function(event) {
  508. document.getElementById("reset-view").addEventListener('click', function(event) {
  509. var canvas = document.getElementById("mapper");
  510. var ctx = canvas.getContext("2d");
  511. ctx.setTransform(1,0,0,1,0,0);
  512. lastX=canvas.width/2;
  513. lastY=canvas.height/2;
  514. hideContextMenu();
  515. resizeCanvas();
  516. drawMap();
  517. });
  518. document.getElementById("delete-room").addEventListener('click', function(event) {
  519. var roomToDelete = rooms.indexOf(activeRoom);
  520. rooms.splice(roomToDelete, 1);
  521. for(var linkId = links.length - 1; linkId >= 0; linkId--) {
  522. var link = links[linkId];
  523. if(activeRoom.id == link.start || activeRoom.id == link.end) {
  524. links.splice(linkId, 1);
  525. }
  526. }
  527. localStorage.setItem("map-rooms", JSON.stringify(rooms));
  528. localStorage.setItem("map-links", JSON.stringify(links));
  529. hideContextMenu();
  530. drawMap();
  531. });
  532. document.getElementById("rename-room").addEventListener('click', function(event) {
  533. var newName = prompt("Please enter the new room name:", activeRoom.name);
  534. activeRoom.name = newName;
  535. localStorage.setItem("map-rooms", JSON.stringify(rooms));
  536. localStorage.setItem("map-links", JSON.stringify(links));
  537. hideContextMenu();
  538. drawMap();
  539. });
  540. document.getElementById("move-room").addEventListener('click', function(event) {
  541. movingRoom = true;
  542. roomToMove = activeRoom;
  543. hideContextMenu();
  544. drawMap();
  545. });
  546. var modalCloseButtons = document.getElementsByClassName("modal-close");
  547. for(var i = 0; i < modalCloseButtons.length; i++) {
  548. modalCloseButtons[i].addEventListener("click", function(event) {
  549. this.parentNode.style.display = "none";
  550. this.parentNode.parentNode.style.display = "none";
  551. });
  552. }
  553. document.getElementById("manage-links").addEventListener('click', function(event) {
  554. var modal = document.getElementById("link-editor-modal");
  555. modal.style.display = "block";
  556. modal.parentNode.style.display = "block";
  557. hideContextMenu();
  558. });
  559. document.getElementById("add-link").addEventListener('click', function(event) {
  560. var linkSourceId = activeRoom.id;
  561. moveLink = true;
  562. var exitDir = prompt("Please enter the exit direction (n, s, e, w):");
  563. var oppositeDir = getOppositeDir(exitDir);
  564. linkToMove = {start:linkSourceId, end:null, exit:exitDir, enter:oppositeDir};
  565. hideContextMenu();
  566. });
  567. document.getElementById("export-map").addEventListener('click', function(event) {
  568. showModal("export-modal");
  569. var exportData = {"rooms": rooms, "links": links, "version": 1};
  570. var exportText = JSON.stringify(exportData);
  571. var exportField = document.getElementById("export-text");
  572. exportField.value = btoa(exportText);
  573. hideContextMenu();
  574. });
  575. document.getElementById("import-map").addEventListener('click', function(event) {
  576. showModal("import-modal");
  577. document.getElementById("import-text").value = "";
  578. hideContextMenu();
  579. });
  580. document.getElementById("load-import-data").addEventListener("click", function(event) {
  581. var newData = document.getElementById("import-text").value;
  582. var potentialLoad = null;
  583. try {
  584. potentialLoad = JSON.parse(atob(newData));
  585. } catch (exception) {
  586. console.error("Invalid import data", exception);
  587. }
  588. if(potentialLoad != null) {
  589. rooms = potentialLoad.rooms;
  590. links = potentialLoad.links;
  591. localStorage.setItem("map-rooms", JSON.stringify(rooms));
  592. localStorage.setItem("map-links", JSON.stringify(links));
  593. drawMap();
  594. closeModal("import-modal");
  595. }
  596. });
  597. canvas = document.getElementById("mapper");
  598. canvas.addEventListener('mousedown',handleMouseDown,false);
  599. canvas.addEventListener('mousemove',handleMouseMove,false);
  600. canvas.addEventListener('mouseup',handleMouseUp, false);
  601. canvas.addEventListener('DOMMouseScroll',handleScroll,false);
  602. canvas.addEventListener('mousewheel',handleScroll,false);
  603. canvas.addEventListener('contextmenu', handleContextMenu, false);
  604. ctx = canvas.getContext('2d');
  605. attachTransforms(ctx);
  606. lastX=canvas.width/2;
  607. lastY=canvas.height/2;
  608. resizeCanvas();
  609. drawMap();
  610. });
  611. var inputBuffer = "";
  612. window.addEventListener("keydown", keyboardListener);