less copy protection, more size visualization
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

821 lines
24 KiB

  1. let selected = null;
  2. let selectedEntity = null;
  3. let entityIndex = 0;
  4. let clicked = null;
  5. let dragging = false;
  6. let clickTimeout = null;
  7. let dragOffsetX = null;
  8. let dragOffsetY = null;
  9. let altHeld = false;
  10. const unitChoices = {
  11. length: [
  12. "meters",
  13. "kilometers",
  14. "feet",
  15. "miles",
  16. ],
  17. area: [
  18. "cm^2",
  19. "meters^2"
  20. ],
  21. mass: [
  22. "kilograms"
  23. ]
  24. }
  25. const config = {
  26. height: math.unit(1500, "meters"),
  27. minLineSize: 50,
  28. maxLineSize: 250,
  29. autoFit: false
  30. }
  31. const availableEntities = {
  32. }
  33. const entities = {
  34. }
  35. function constrainRel(coords) {
  36. return {
  37. x: Math.min(Math.max(coords.x, 0), 1),
  38. y: Math.min(Math.max(coords.y, 0), 1)
  39. }
  40. }
  41. function snapRel(coords) {
  42. return constrainRel({
  43. x: coords.x,
  44. y: altHeld ? coords.y : (Math.abs(coords.y - 1) < 0.05 ? 1 : coords.y)
  45. });
  46. }
  47. function adjustAbs(coords, oldHeight, newHeight) {
  48. return { x: coords.x, y: 1 + (coords.y - 1) * math.divide(oldHeight, newHeight) };
  49. }
  50. function rel2abs(coords) {
  51. const canvasWidth = document.querySelector("#display").clientWidth - 100;
  52. const canvasHeight = document.querySelector("#display").clientHeight - 50;
  53. return { x: coords.x * canvasWidth + 50, y: coords.y * canvasHeight };
  54. }
  55. function abs2rel(coords) {
  56. const canvasWidth = document.querySelector("#display").clientWidth - 100;
  57. const canvasHeight = document.querySelector("#display").clientHeight - 50;
  58. return { x: (coords.x - 50) / canvasWidth, y: coords.y / canvasHeight };
  59. }
  60. function updateEntityElement(entity, element) {
  61. const position = rel2abs({ x: element.dataset.x, y: element.dataset.y });
  62. const view = element.dataset.view;
  63. element.style.left = position.x + "px";
  64. element.style.top = position.y + "px";
  65. const canvasHeight = document.querySelector("#display").clientHeight;
  66. const pixels = math.divide(entity.views[view].height, config.height) * (canvasHeight - 100);
  67. element.style.setProperty("--height", pixels + "px");
  68. element.querySelector(".entity-name").innerText = entity.name;
  69. const bottomName = document.querySelector("#bottom-name-" + element.dataset.key);
  70. let entX = document.querySelector("#entities").getBoundingClientRect().x;
  71. bottomName.style.left = position.x + entX + "px";
  72. bottomName.style.top = "95vh";
  73. bottomName.innerText = entity.name;
  74. }
  75. function updateSizes() {
  76. drawScale();
  77. Object.entries(entities).forEach(([key, entity]) => {
  78. const element = document.querySelector("#entity-" + key);
  79. updateEntityElement(entity, element);
  80. });
  81. }
  82. function drawScale() {
  83. function drawTicks(/** @type {CanvasRenderingContext2D} */ ctx, pixelsPer, heightPer) {
  84. let total = heightPer.clone();
  85. total.value = 0;
  86. for (let y = ctx.canvas.clientHeight - 50; y >= 50; y -= pixelsPer) {
  87. drawTick(ctx, 50, y, total);
  88. total = math.add(total, heightPer);
  89. }
  90. }
  91. function drawTick(/** @type {CanvasRenderingContext2D} */ ctx, x, y, value) {
  92. const oldStroke = ctx.strokeStyle;
  93. const oldFill = ctx.fillStyle;
  94. ctx.beginPath();
  95. ctx.moveTo(x, y);
  96. ctx.lineTo(x + 20, y);
  97. ctx.strokeStyle = "#000000";
  98. ctx.stroke();
  99. ctx.beginPath();
  100. ctx.moveTo(x + 20, y);
  101. ctx.lineTo(ctx.canvas.clientWidth - 70, y);
  102. ctx.strokeStyle = "#aaaaaa";
  103. ctx.stroke();
  104. ctx.beginPath();
  105. ctx.moveTo(ctx.canvas.clientWidth - 70, y);
  106. ctx.lineTo(ctx.canvas.clientWidth - 50, y);
  107. ctx.strokeStyle = "#000000";
  108. ctx.stroke();
  109. const oldFont = ctx.font;
  110. ctx.font = 'normal 24pt coda';
  111. ctx.fillStyle = "#dddddd";
  112. ctx.beginPath();
  113. ctx.fillText(value.format({ precision: 3 }), x + 20, y + 35);
  114. ctx.font = oldFont;
  115. ctx.strokeStyle = oldStroke;
  116. ctx.fillStyle = oldFill;
  117. }
  118. const canvas = document.querySelector("#display");
  119. /** @type {CanvasRenderingContext2D} */
  120. const ctx = canvas.getContext("2d");
  121. let pixelsPer = (ctx.canvas.clientHeight - 100) / config.height.value;
  122. let heightPer = config.height.clone();
  123. heightPer.value = 1;
  124. if (pixelsPer < config.minLineSize) {
  125. heightPer.value /= pixelsPer / config.minLineSize;
  126. pixelsPer = config.minLineSize;
  127. }
  128. if (pixelsPer > config.maxLineSize) {
  129. heightPer.value /= pixelsPer / config.maxLineSize;
  130. pixelsPer = config.maxLineSize;
  131. }
  132. ctx.clearRect(0, 0, canvas.width, canvas.height);
  133. ctx.scale(1, 1);
  134. ctx.canvas.width = canvas.clientWidth;
  135. ctx.canvas.height = canvas.clientHeight;
  136. ctx.beginPath();
  137. ctx.moveTo(50, 50);
  138. ctx.lineTo(50, ctx.canvas.clientHeight - 50);
  139. ctx.stroke();
  140. ctx.beginPath();
  141. ctx.moveTo(ctx.canvas.clientWidth - 50, 50);
  142. ctx.lineTo(ctx.canvas.clientWidth - 50, ctx.canvas.clientHeight - 50);
  143. ctx.stroke();
  144. drawTicks(ctx, pixelsPer, heightPer);
  145. }
  146. function makeEntity(name, author, views) {
  147. const entityTemplate = {
  148. name: name,
  149. author: author,
  150. scale: 1,
  151. views: views,
  152. init: function () {
  153. Object.entries(this.views).forEach(([viewKey, view]) => {
  154. view.parent = this;
  155. if (this.defaultView === undefined) {
  156. this.defaultView = viewKey;
  157. }
  158. Object.entries(view.attributes).forEach(([key, val]) => {
  159. Object.defineProperty(
  160. view,
  161. key,
  162. {
  163. get: function () {
  164. return math.multiply(Math.pow(this.parent.scale, this.attributes[key].power), this.attributes[key].base);
  165. },
  166. set: function (value) {
  167. const newScale = Math.pow(math.divide(value, this.attributes[key].base), 1 / this.attributes[key].power);
  168. this.parent.scale = newScale;
  169. }
  170. }
  171. )
  172. });
  173. });
  174. delete this.init;
  175. return this;
  176. }
  177. }.init();
  178. return entityTemplate;
  179. }
  180. function clickDown(target, x, y) {
  181. clicked = target;
  182. const rect = target.getBoundingClientRect();
  183. let entX = document.querySelector("#entities").getBoundingClientRect().x;
  184. let entY = document.querySelector("#entities").getBoundingClientRect().y;
  185. dragOffsetX = x - rect.left + entX;
  186. dragOffsetY = y - rect.top + entY;
  187. clickTimeout = setTimeout(() => { dragging = true }, 200)
  188. }
  189. // could we make this actually detect the menu area?
  190. function hoveringInDeleteArea(e) {
  191. return e.clientY < document.body.clientHeight / 10;
  192. }
  193. function clickUp(e) {
  194. clearTimeout(clickTimeout);
  195. if (clicked) {
  196. if (dragging) {
  197. dragging = false;
  198. if (hoveringInDeleteArea(e)) {
  199. removeEntity(clicked);
  200. document.querySelector("#menubar").classList.remove("hover-delete");
  201. }
  202. } else {
  203. select(clicked);
  204. }
  205. clicked = null;
  206. }
  207. }
  208. function deselect() {
  209. if (selected) {
  210. selected.classList.remove("selected");
  211. }
  212. selected = null;
  213. clearViewList();
  214. clearEntityOptions();
  215. clearViewOptions();
  216. }
  217. function select(target) {
  218. deselect();
  219. selected = target;
  220. selectedEntity = entities[target.dataset.key];
  221. selected.classList.add("selected");
  222. configViewList(selectedEntity, target.dataset.view);
  223. configEntityOptions(selectedEntity, target.dataset.view);
  224. configViewOptions(selectedEntity, target.dataset.view);
  225. }
  226. function configViewList(entity, selectedView) {
  227. const list = document.querySelector("#entity-view");
  228. list.innerHTML = "";
  229. list.style.display = "block";
  230. Object.keys(entity.views).forEach(view => {
  231. const option = document.createElement("option");
  232. option.innerText = entity.views[view].name;
  233. option.value = view;
  234. if (view === selectedView) {
  235. option.selected = true;
  236. }
  237. list.appendChild(option);
  238. });
  239. }
  240. function clearViewList() {
  241. const list = document.querySelector("#entity-view");
  242. list.innerHTML = "";
  243. list.style.display = "none";
  244. }
  245. function updateWorldOptions(entity, view) {
  246. const heightInput = document.querySelector("#options-height-value");
  247. const heightSelect = document.querySelector("#options-height-unit");
  248. const converted = config.height.to(heightSelect.value);
  249. heightInput.value = math.round(converted.value, 3);
  250. }
  251. function configEntityOptions(entity, view) {
  252. const holder = document.querySelector("#options-entity");
  253. holder.innerHTML = "";
  254. const scaleLabel = document.createElement("div");
  255. scaleLabel.classList.add("options-label");
  256. scaleLabel.innerText = "Scale";
  257. const scaleRow = document.createElement("div");
  258. scaleRow.classList.add("options-row");
  259. const scaleInput = document.createElement("input");
  260. scaleInput.classList.add("options-field-numeric");
  261. scaleInput.id = "options-entity-scale";
  262. scaleInput.addEventListener("input", e => {
  263. entity.scale = e.target.value == 0 ? 1 : e.target.value;
  264. if (config.autoFit) {
  265. fitWorld();
  266. }
  267. updateSizes();
  268. updateEntityOptions(entity, view);
  269. updateViewOptions(entity, view);
  270. });
  271. scaleInput.setAttribute("min", 1);
  272. scaleInput.setAttribute("type", "number");
  273. scaleInput.value = entity.scale;
  274. scaleRow.appendChild(scaleInput);
  275. holder.appendChild(scaleLabel);
  276. holder.appendChild(scaleRow);
  277. const nameLabel = document.createElement("div");
  278. nameLabel.classList.add("options-label");
  279. nameLabel.innerText = "Name";
  280. const nameRow = document.createElement("div");
  281. nameRow.classList.add("options-row");
  282. const nameInput = document.createElement("input");
  283. nameInput.classList.add("options-field-text");
  284. nameInput.value = entity.name;
  285. nameInput.addEventListener("input", e => {
  286. entity.name = e.target.value;
  287. updateSizes();
  288. })
  289. nameRow.appendChild(nameInput);
  290. holder.appendChild(nameLabel);
  291. holder.appendChild(nameRow);
  292. }
  293. function updateEntityOptions(entity, view) {
  294. const scaleInput = document.querySelector("#options-entity-scale");
  295. scaleInput.value = entity.scale;
  296. }
  297. function clearEntityOptions() {
  298. const holder = document.querySelector("#options-entity");
  299. holder.innerHTML = "";
  300. }
  301. function configViewOptions(entity, view) {
  302. const holder = document.querySelector("#options-view");
  303. holder.innerHTML = "";
  304. Object.entries(entity.views[view].attributes).forEach(([key, val]) => {
  305. const label = document.createElement("div");
  306. label.classList.add("options-label");
  307. label.innerText = val.name;
  308. holder.appendChild(label);
  309. const row = document.createElement("div");
  310. row.classList.add("options-row");
  311. holder.appendChild(row);
  312. const input = document.createElement("input");
  313. input.classList.add("options-field-numeric");
  314. input.id = "options-view-" + key + "-input";
  315. input.setAttribute("type", "number");
  316. input.setAttribute("min", 1);
  317. input.value = entity.views[view][key].value;
  318. const select = document.createElement("select");
  319. select.id = "options-view-" + key + "-select"
  320. unitChoices[val.type].forEach(name => {
  321. const option = document.createElement("option");
  322. option.innerText = name;
  323. select.appendChild(option);
  324. });
  325. input.addEventListener("input", e => {
  326. const value = input.value == 0 ? 1 : input.value;
  327. entity.views[view][key] = math.unit(value, select.value);
  328. if (config.autoFit) {
  329. fitWorld();
  330. }
  331. updateSizes();
  332. updateEntityOptions(entity, view);
  333. updateViewOptions(entity, view, key);
  334. });
  335. select.setAttribute("oldUnit", select.value);
  336. select.addEventListener("input", e => {
  337. const value = input.value == 0 ? 1 : input.value;
  338. const oldUnit = select.getAttribute("oldUnit");
  339. entity.views[view][key] = math.unit(value, oldUnit).to(select.value);
  340. input.value = entity.views[view][key].toNumber(select.value);
  341. select.setAttribute("oldUnit", select.value);
  342. if (config.autoFit) {
  343. fitWorld();
  344. }
  345. updateSizes();
  346. updateEntityOptions(entity, view);
  347. updateViewOptions(entity, view, key);
  348. });
  349. row.appendChild(input);
  350. row.appendChild(select);
  351. });
  352. }
  353. function updateViewOptions(entity, view, changed) {
  354. Object.entries(entity.views[view].attributes).forEach(([key, val]) => {
  355. if (key != changed) {
  356. const input = document.querySelector("#options-view-" + key + "-input");
  357. const select = document.querySelector("#options-view-" + key + "-select");
  358. const currentUnit = select.value;
  359. const convertedAmount = entity.views[view][key].to(currentUnit);
  360. input.value = math.round(convertedAmount.value, 5);
  361. }
  362. });
  363. }
  364. function clearViewOptions() {
  365. const holder = document.querySelector("#options-view");
  366. holder.innerHTML = "";
  367. }
  368. // this is a crime against humanity, and also stolen from
  369. // stack overflow
  370. // https://stackoverflow.com/questions/38487569/click-through-png-image-only-if-clicked-coordinate-is-transparent
  371. const testCanvas = document.createElement("canvas");
  372. testCanvas.id = "test-canvas";
  373. const testCtx = testCanvas.getContext("2d");
  374. function testClick(event) {
  375. // oh my god I can't believe I'm doing this
  376. const target = event.target;
  377. if (navigator.userAgent.indexOf("Firefox") != -1) {
  378. clickDown(target.parentElement, event.clientX, event.clientY);
  379. return;
  380. }
  381. // Get click coordinates
  382. let w = target.width;
  383. let h = target.height;
  384. let ratioW = 1, ratioH = 1;
  385. // Limit the size of the canvas so that very large images don't cause problems)
  386. if (w > 4000) {
  387. ratioW = w / 4000;
  388. w /= ratioW;
  389. h /= ratioW;
  390. }
  391. if (h > 4000) {
  392. ratioH = h / 4000;
  393. w /= ratioH;
  394. h /= ratioH;
  395. }
  396. const ratio = ratioW * ratioH;
  397. var x = event.clientX - target.getBoundingClientRect().x,
  398. y = event.clientY - target.getBoundingClientRect().y,
  399. alpha;
  400. testCtx.canvas.width = w;
  401. testCtx.canvas.height = h;
  402. // Draw image to canvas
  403. // and read Alpha channel value
  404. testCtx.drawImage(target, 0, 0, w, h);
  405. alpha = testCtx.getImageData(Math.floor(x / ratio), Math.floor(y / ratio), 1, 1).data[3]; // [0]R [1]G [2]B [3]A
  406. // If pixel is transparent,
  407. // retrieve the element underneath and trigger it's click event
  408. if (alpha === 0) {
  409. const oldDisplay = target.style.display;
  410. target.style.display = "none";
  411. const newTarget = document.elementFromPoint(event.clientX, event.clientY);
  412. newTarget.dispatchEvent(new MouseEvent(event.type, {
  413. "clientX": event.clientX,
  414. "clientY": event.clientY
  415. }));
  416. target.style.display = oldDisplay;
  417. } else {
  418. clickDown(target.parentElement, event.clientX, event.clientY);
  419. }
  420. }
  421. function arrangeEntities(order) {
  422. let x = 0.1;
  423. order.forEach(key => {
  424. document.querySelector("#entity-" + key).dataset.x = x;
  425. x += 0.8 / order.length
  426. });
  427. updateSizes();
  428. }
  429. function removeAllEntities() {
  430. Object.keys(entities).forEach(key => {
  431. removeEntity(document.querySelector("#entity-" + key));
  432. });
  433. }
  434. function removeEntity(element) {
  435. delete entities[element.dataset.key];
  436. const bottomName = document.querySelector("#bottom-name-" + element.dataset.key);
  437. bottomName.parentElement.removeChild(bottomName);
  438. element.parentElement.removeChild(element);
  439. }
  440. function displayEntity(entity, view, x, y) {
  441. const box = document.createElement("div");
  442. box.classList.add("entity-box");
  443. const img = document.createElement("img");
  444. img.classList.add("entity-image");
  445. img.addEventListener("dragstart", e => {
  446. e.preventDefault();
  447. });
  448. const nameTag = document.createElement("div");
  449. nameTag.classList.add("entity-name");
  450. nameTag.innerText = entity.name;
  451. box.appendChild(img);
  452. box.appendChild(nameTag);
  453. const image = entity.views[view].image;
  454. img.src = image.source;
  455. if (image.bottom) {
  456. img.style.setProperty("--offset", ((-1 + image.bottom) * 100) + "%")
  457. }
  458. box.dataset.x = x;
  459. box.dataset.y = y;
  460. img.addEventListener("mousedown", e => { testClick(e); e.stopPropagation() });
  461. img.addEventListener("touchstart", e => {
  462. const fakeEvent = {
  463. target: e.target,
  464. clientX: e.touches[0].clientX,
  465. clientY: e.touches[0].clientY
  466. };
  467. testClick(fakeEvent);
  468. });
  469. box.id = "entity-" + entityIndex;
  470. box.dataset.key = entityIndex;
  471. box.dataset.view = view;
  472. entities[entityIndex] = entity;
  473. entity.index = entityIndex;
  474. const world = document.querySelector("#entities");
  475. world.appendChild(box);
  476. const bottomName = document.createElement("div");
  477. bottomName.classList.add("bottom-name");
  478. bottomName.id = "bottom-name-" + entityIndex;
  479. bottomName.innerText = entity.name;
  480. bottomName.addEventListener("click", () => select(box));
  481. world.appendChild(bottomName);
  482. entityIndex += 1;
  483. updateEntityElement(entity, box);
  484. if (config.autoFit) {
  485. fitWorld();
  486. }
  487. }
  488. document.addEventListener("DOMContentLoaded", () => {
  489. const stuff = [makeFen].concat(makeBuildings().map(x => x.constructor))
  490. let x = 0.2;
  491. stuff.forEach(entity => {
  492. displayEntity(entity(), entity().defaultView, x, 1);
  493. x += 0.7 / stuff.length;
  494. })
  495. window.addEventListener("wheel", e => {
  496. const dir = e.deltaY < 0 ? 0.9 : 1.1;
  497. config.height = math.multiply(config.height, dir);
  498. updateSizes();
  499. updateWorldOptions();
  500. })
  501. document.querySelector("body").appendChild(testCtx.canvas);
  502. updateSizes();
  503. document.querySelector("#options-height-value").addEventListener("input", e => {
  504. updateWorldHeight();
  505. })
  506. document.querySelector("#options-height-unit").addEventListener("input", e => {
  507. updateWorldHeight();
  508. })
  509. world.addEventListener("mousedown", e => deselect());
  510. document.querySelector("#display").addEventListener("mousedown", deselect);
  511. document.addEventListener("mouseup", e => clickUp(e));
  512. document.addEventListener("touchend", e => {
  513. const fakeEvent = {
  514. target: e.target,
  515. clientX: e.changedTouches[0].clientX,
  516. clientY: e.changedTouches[0].clientY
  517. };
  518. clickUp(fakeEvent);
  519. });
  520. document.querySelector("#entity-view").addEventListener("input", e => {
  521. selected.dataset.view = e.target.value
  522. const image = entities[selected.dataset.key].views[e.target.value].image
  523. selected.querySelector(".entity-image").src = image.source;
  524. if (image.bottom) {
  525. selected.querySelector(".entity-image").style.setProperty("--offset", ((-1 + image.bottom) * 100) + "%")
  526. }
  527. updateSizes();
  528. updateEntityOptions(entities[selected.dataset.key], e.target.value);
  529. updateViewOptions(entities[selected.dataset.key], e.target.value);
  530. });
  531. clearViewList();
  532. document.querySelector("#menu-clear").addEventListener("click", e => {
  533. removeAllEntities();
  534. });
  535. document.querySelector("#menu-order-height").addEventListener("click", e => {
  536. const order = Object.keys(entities).sort((a, b) => {
  537. const entA = entities[a];
  538. const entB = entities[b];
  539. const viewA = document.querySelector("#entity-" + a).dataset.view;
  540. const viewB = document.querySelector("#entity-" + b).dataset.view;
  541. const heightA = entA.views[viewA].height.to("meter").value;
  542. const heightB = entB.views[viewB].height.to("meter").value;
  543. return heightA - heightB;
  544. });
  545. arrangeEntities(order);
  546. });
  547. document.querySelector("#options-world-fit").addEventListener("click", fitWorld);
  548. document.querySelector("#options-world-autofit").addEventListener("input", e => {
  549. config.autoFit = e.target.value;
  550. if (config.autoFit) {
  551. fitWorld();
  552. }
  553. });
  554. document.addEventListener("keydown", e => {
  555. console.log(e)
  556. if (e.key == "Delete" || e.key == "Backspace") {
  557. if (selected) {
  558. removeEntity(selected);
  559. selected = null;
  560. }
  561. }
  562. })
  563. prepareEntities();
  564. });
  565. function prepareEntities() {
  566. availableEntities["buildings"] = makeBuildings();
  567. availableEntities["characters"] = makeCharacters();
  568. availableEntities["objects"] = makeObjects();
  569. availableEntities["vehicles"] = makeVehicles();
  570. const holder = document.querySelector("#spawners");
  571. Object.entries(availableEntities).forEach(([category, entityList]) => {
  572. const select = document.createElement("select");
  573. select.id = "create-entity-" + category;
  574. for (let i = 0; i < entityList.length; i++) {
  575. const entity = entityList[i];
  576. const option = document.createElement("option");
  577. option.value = i;
  578. option.innerText = entity.name;
  579. select.appendChild(option);
  580. };
  581. const button = document.createElement("button");
  582. button.innerText = "Create " + category;
  583. button.addEventListener("click", e => {
  584. const newEntity = entityList[select.value].constructor()
  585. displayEntity(newEntity, newEntity.defaultView, 0.5, 1);
  586. });
  587. holder.appendChild(select);
  588. holder.appendChild(button);
  589. });
  590. }
  591. window.addEventListener("resize", () => {
  592. updateSizes();
  593. })
  594. document.addEventListener("mousemove", (e) => {
  595. if (clicked) {
  596. const position = snapRel(abs2rel({ x: e.clientX - dragOffsetX, y: e.clientY - dragOffsetY }));
  597. clicked.dataset.x = position.x;
  598. clicked.dataset.y = position.y;
  599. updateEntityElement(entities[clicked.dataset.key], clicked);
  600. if (hoveringInDeleteArea(e)) {
  601. document.querySelector("#menubar").classList.add("hover-delete");
  602. } else {
  603. document.querySelector("#menubar").classList.remove("hover-delete");
  604. }
  605. }
  606. });
  607. document.addEventListener("touchmove", (e) => {
  608. if (clicked) {
  609. e.preventDefault();
  610. let x = e.touches[0].clientX;
  611. let y = e.touches[0].clientY;
  612. const position = snapRel(abs2rel({ x: x - dragOffsetX, y: y - dragOffsetY }));
  613. clicked.dataset.x = position.x;
  614. clicked.dataset.y = position.y;
  615. updateEntityElement(entities[clicked.dataset.key], clicked);
  616. // what a hack
  617. // I should centralize this 'fake event' creation...
  618. if (hoveringInDeleteArea({ clientY: y })) {
  619. document.querySelector("#menubar").classList.add("hover-delete");
  620. } else {
  621. document.querySelector("#menubar").classList.remove("hover-delete");
  622. }
  623. }
  624. }, { passive: false });
  625. function fitWorld() {
  626. let max = math.unit(0, "meter");
  627. Object.entries(entities).forEach(([key, entity]) => {
  628. const view = document.querySelector("#entity-" + key).dataset.view;
  629. max = math.max(max, entity.views[view].height);
  630. });
  631. setWorldHeight(config.height, math.multiply(max, 1.1));
  632. }
  633. function updateWorldHeight() {
  634. const value = Math.max(1, document.querySelector("#options-height-value").value);
  635. const unit = document.querySelector("#options-height-unit").value;
  636. const oldHeight = config.height;
  637. setWorldHeight(oldHeight, math.unit(value, unit));
  638. }
  639. function setWorldHeight(oldHeight, newHeight) {
  640. config.height = newHeight;
  641. Object.entries(entities).forEach(([key, entity]) => {
  642. const element = document.querySelector("#entity-" + key);
  643. const newPosition = adjustAbs({ x: element.dataset.x, y: element.dataset.y }, oldHeight, config.height);
  644. element.dataset.x = newPosition.x;
  645. element.dataset.y = newPosition.y;
  646. });
  647. updateSizes();
  648. }