less copy protection, more size visualization
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

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