less copy protection, more size visualization
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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