less copy protection, more size visualization
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

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