less copy protection, more size visualization
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

925 řádky
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. }
  335. function configViewOptions(entity, view) {
  336. const holder = document.querySelector("#options-view");
  337. holder.innerHTML = "";
  338. Object.entries(entity.views[view].attributes).forEach(([key, val]) => {
  339. const label = document.createElement("div");
  340. label.classList.add("options-label");
  341. label.innerText = val.name;
  342. holder.appendChild(label);
  343. const row = document.createElement("div");
  344. row.classList.add("options-row");
  345. holder.appendChild(row);
  346. const input = document.createElement("input");
  347. input.classList.add("options-field-numeric");
  348. input.id = "options-view-" + key + "-input";
  349. input.setAttribute("type", "number");
  350. input.setAttribute("min", 1);
  351. input.value = entity.views[view][key].value;
  352. const select = document.createElement("select");
  353. select.id = "options-view-" + key + "-select"
  354. unitChoices[val.type].forEach(name => {
  355. const option = document.createElement("option");
  356. option.innerText = name;
  357. select.appendChild(option);
  358. });
  359. input.addEventListener("input", e => {
  360. const value = input.value == 0 ? 1 : input.value;
  361. entity.views[view][key] = math.unit(value, select.value);
  362. if (config.autoFit) {
  363. fitWorld();
  364. }
  365. updateSizes();
  366. updateEntityOptions(entity, view);
  367. updateViewOptions(entity, view, key);
  368. });
  369. select.setAttribute("oldUnit", select.value);
  370. select.addEventListener("input", e => {
  371. const value = input.value == 0 ? 1 : input.value;
  372. const oldUnit = select.getAttribute("oldUnit");
  373. entity.views[view][key] = math.unit(value, oldUnit).to(select.value);
  374. input.value = entity.views[view][key].toNumber(select.value);
  375. select.setAttribute("oldUnit", select.value);
  376. if (config.autoFit) {
  377. fitWorld();
  378. }
  379. updateSizes();
  380. updateEntityOptions(entity, view);
  381. updateViewOptions(entity, view, key);
  382. });
  383. row.appendChild(input);
  384. row.appendChild(select);
  385. });
  386. }
  387. function updateViewOptions(entity, view, changed) {
  388. Object.entries(entity.views[view].attributes).forEach(([key, val]) => {
  389. if (key != changed) {
  390. const input = document.querySelector("#options-view-" + key + "-input");
  391. const select = document.querySelector("#options-view-" + key + "-select");
  392. const currentUnit = select.value;
  393. const convertedAmount = entity.views[view][key].toNumber(currentUnit);
  394. input.value = math.round(convertedAmount, 5);
  395. }
  396. });
  397. }
  398. function clearViewOptions() {
  399. const holder = document.querySelector("#options-view");
  400. holder.innerHTML = "";
  401. }
  402. // this is a crime against humanity, and also stolen from
  403. // stack overflow
  404. // https://stackoverflow.com/questions/38487569/click-through-png-image-only-if-clicked-coordinate-is-transparent
  405. const testCanvas = document.createElement("canvas");
  406. testCanvas.id = "test-canvas";
  407. const testCtx = testCanvas.getContext("2d");
  408. function testClick(event) {
  409. // oh my god I can't believe I'm doing this
  410. const target = event.target;
  411. if (navigator.userAgent.indexOf("Firefox") != -1) {
  412. clickDown(target.parentElement, event.clientX, event.clientY);
  413. return;
  414. }
  415. // Get click coordinates
  416. let w = target.width;
  417. let h = target.height;
  418. let ratioW = 1, ratioH = 1;
  419. // Limit the size of the canvas so that very large images don't cause problems)
  420. if (w > 4000) {
  421. ratioW = w / 4000;
  422. w /= ratioW;
  423. h /= ratioW;
  424. }
  425. if (h > 4000) {
  426. ratioH = h / 4000;
  427. w /= ratioH;
  428. h /= ratioH;
  429. }
  430. const ratio = ratioW * ratioH;
  431. var x = event.clientX - target.getBoundingClientRect().x,
  432. y = event.clientY - target.getBoundingClientRect().y,
  433. alpha;
  434. testCtx.canvas.width = w;
  435. testCtx.canvas.height = h;
  436. // Draw image to canvas
  437. // and read Alpha channel value
  438. testCtx.drawImage(target, 0, 0, w, h);
  439. alpha = testCtx.getImageData(Math.floor(x / ratio), Math.floor(y / ratio), 1, 1).data[3]; // [0]R [1]G [2]B [3]A
  440. // If pixel is transparent,
  441. // retrieve the element underneath and trigger it's click event
  442. if (alpha === 0) {
  443. const oldDisplay = target.style.display;
  444. target.style.display = "none";
  445. const newTarget = document.elementFromPoint(event.clientX, event.clientY);
  446. newTarget.dispatchEvent(new MouseEvent(event.type, {
  447. "clientX": event.clientX,
  448. "clientY": event.clientY
  449. }));
  450. target.style.display = oldDisplay;
  451. } else {
  452. clickDown(target.parentElement, event.clientX, event.clientY);
  453. }
  454. }
  455. function arrangeEntities(order) {
  456. let x = 0.1;
  457. order.forEach(key => {
  458. document.querySelector("#entity-" + key).dataset.x = x;
  459. x += 0.8 / order.length
  460. });
  461. updateSizes();
  462. }
  463. function removeAllEntities() {
  464. Object.keys(entities).forEach(key => {
  465. removeEntity(document.querySelector("#entity-" + key));
  466. });
  467. }
  468. function removeEntity(element) {
  469. delete entities[element.dataset.key];
  470. const bottomName = document.querySelector("#bottom-name-" + element.dataset.key);
  471. bottomName.parentElement.removeChild(bottomName);
  472. element.parentElement.removeChild(element);
  473. }
  474. function displayEntity(entity, view, x, y) {
  475. const box = document.createElement("div");
  476. box.classList.add("entity-box");
  477. const img = document.createElement("img");
  478. img.classList.add("entity-image");
  479. img.addEventListener("dragstart", e => {
  480. e.preventDefault();
  481. });
  482. const nameTag = document.createElement("div");
  483. nameTag.classList.add("entity-name");
  484. nameTag.innerText = entity.name;
  485. box.appendChild(img);
  486. box.appendChild(nameTag);
  487. const image = entity.views[view].image;
  488. img.src = image.source;
  489. if (image.bottom !== undefined) {
  490. img.style.setProperty("--offset", ((-1 + image.bottom) * 100) + "%")
  491. } else {
  492. img.style.setProperty("--offset", ((-1) * 100) + "%")
  493. }
  494. box.dataset.x = x;
  495. box.dataset.y = y;
  496. img.addEventListener("mousedown", e => { testClick(e); e.stopPropagation() });
  497. img.addEventListener("touchstart", e => {
  498. const fakeEvent = {
  499. target: e.target,
  500. clientX: e.touches[0].clientX,
  501. clientY: e.touches[0].clientY
  502. };
  503. testClick(fakeEvent);
  504. });
  505. box.id = "entity-" + entityIndex;
  506. box.dataset.key = entityIndex;
  507. box.dataset.view = view;
  508. entity.view = view;
  509. entities[entityIndex] = entity;
  510. entity.index = entityIndex;
  511. const world = document.querySelector("#entities");
  512. world.appendChild(box);
  513. const bottomName = document.createElement("div");
  514. bottomName.classList.add("bottom-name");
  515. bottomName.id = "bottom-name-" + entityIndex;
  516. bottomName.innerText = entity.name;
  517. bottomName.addEventListener("click", () => select(box));
  518. world.appendChild(bottomName);
  519. entityIndex += 1;
  520. updateEntityElement(entity, box);
  521. if (config.autoFit) {
  522. fitWorld();
  523. }
  524. }
  525. document.addEventListener("DOMContentLoaded", () => {
  526. prepareEntities();
  527. const stuff = availableEntities.characters.map(x => x.constructor).filter(x => {
  528. const result = x();
  529. return result.views[result.defaultView].height.toNumber("meters") < 1000;
  530. })
  531. let x = 0.2;
  532. stuff.forEach(entity => {
  533. displayEntity(entity(), entity().defaultView, x, 1);
  534. x += 0.7 / stuff.length;
  535. })
  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. fitWorld();
  547. window.addEventListener("wheel", e => {
  548. const dir = e.deltaY < 0 ? 0.9 : 1.1;
  549. config.height = math.multiply(config.height, dir);
  550. updateSizes();
  551. updateWorldOptions();
  552. })
  553. document.querySelector("body").appendChild(testCtx.canvas);
  554. updateSizes();
  555. document.querySelector("#options-height-value").addEventListener("input", e => {
  556. updateWorldHeight();
  557. })
  558. document.querySelector("#options-height-unit").addEventListener("input", e => {
  559. updateWorldHeight();
  560. })
  561. world.addEventListener("mousedown", e => deselect());
  562. document.querySelector("#display").addEventListener("mousedown", deselect);
  563. document.addEventListener("mouseup", e => clickUp(e));
  564. document.addEventListener("touchend", e => {
  565. const fakeEvent = {
  566. target: e.target,
  567. clientX: e.changedTouches[0].clientX,
  568. clientY: e.changedTouches[0].clientY
  569. };
  570. clickUp(fakeEvent);
  571. });
  572. document.querySelector("#entity-view").addEventListener("input", e => {
  573. selected.dataset.view = e.target.value;
  574. entities[selected.dataset.key].view = e.target.value;
  575. const image = entities[selected.dataset.key].views[e.target.value].image
  576. selected.querySelector(".entity-image").src = image.source;
  577. if (image.bottom !== undefined) {
  578. selected.querySelector(".entity-image").style.setProperty("--offset", ((-1 + image.bottom) * 100) + "%")
  579. } else {
  580. selected.querySelector(".entity-image").style.setProperty("--offset", ((-1) * 100) + "%")
  581. }
  582. updateSizes();
  583. updateEntityOptions(entities[selected.dataset.key], e.target.value);
  584. updateViewOptions(entities[selected.dataset.key], e.target.value);
  585. });
  586. clearViewList();
  587. document.querySelector("#menu-clear").addEventListener("click", e => {
  588. removeAllEntities();
  589. });
  590. document.querySelector("#menu-order-height").addEventListener("click", e => {
  591. const order = Object.keys(entities).sort((a, b) => {
  592. const entA = entities[a];
  593. const entB = entities[b];
  594. const viewA = document.querySelector("#entity-" + a).dataset.view;
  595. const viewB = document.querySelector("#entity-" + b).dataset.view;
  596. const heightA = entA.views[viewA].height.to("meter").value;
  597. const heightB = entB.views[viewB].height.to("meter").value;
  598. return heightA - heightB;
  599. });
  600. arrangeEntities(order);
  601. });
  602. document.querySelector("#options-world-fit").addEventListener("click", fitWorld);
  603. document.querySelector("#options-world-autofit").addEventListener("input", e => {
  604. config.autoFit = e.target.value;
  605. if (config.autoFit) {
  606. fitWorld();
  607. }
  608. });
  609. document.addEventListener("keydown", e => {
  610. if (e.key == "Delete") {
  611. if (selected) {
  612. removeEntity(selected);
  613. selected = null;
  614. }
  615. }
  616. })
  617. });
  618. function prepareEntities() {
  619. availableEntities["buildings"] = makeBuildings();
  620. availableEntities["characters"] = makeCharacters();
  621. availableEntities["objects"] = makeObjects();
  622. availableEntities["naturals"] = makeNaturals();
  623. availableEntities["vehicles"] = makeVehicles();
  624. availableEntities["characters"].sort((x,y) => {
  625. return x.name < y.name ? -1 : 1
  626. });
  627. const holder = document.querySelector("#spawners");
  628. const categorySelect = document.createElement("select");
  629. categorySelect.id = "category-picker";
  630. holder.appendChild(categorySelect);
  631. Object.entries(availableEntities).forEach(([category, entityList]) => {
  632. const select = document.createElement("select");
  633. select.id = "create-entity-" + category;
  634. for (let i = 0; i < entityList.length; i++) {
  635. const entity = entityList[i];
  636. const option = document.createElement("option");
  637. option.value = i;
  638. option.innerText = entity.name;
  639. select.appendChild(option);
  640. };
  641. const button = document.createElement("button");
  642. button.id = "create-entity-" + category + "-button";
  643. button.innerText = "Create";
  644. button.addEventListener("click", e => {
  645. const newEntity = entityList[select.value].constructor()
  646. displayEntity(newEntity, newEntity.defaultView, 0.5, 1);
  647. });
  648. const categoryOption = document.createElement("option");
  649. categoryOption.value = category
  650. categoryOption.innerText = category;
  651. if (category == "characters") {
  652. categoryOption.selected = true;
  653. select.classList.add("category-visible");
  654. button.classList.add("category-visible");
  655. }
  656. categorySelect.appendChild(categoryOption);
  657. holder.appendChild(button);
  658. holder.appendChild(select);
  659. });
  660. categorySelect.addEventListener("input", e => {
  661. const oldSelect = document.querySelector("select.category-visible");
  662. oldSelect.classList.remove("category-visible");
  663. const oldButton = document.querySelector("button.category-visible");
  664. oldButton.classList.remove("category-visible");
  665. const newSelect = document.querySelector("#create-entity-" + e.target.value);
  666. newSelect.classList.add("category-visible");
  667. const newButton = document.querySelector("#create-entity-" + e.target.value + "-button");
  668. newButton.classList.add("category-visible");
  669. });
  670. }
  671. window.addEventListener("resize", () => {
  672. updateSizes();
  673. })
  674. document.addEventListener("mousemove", (e) => {
  675. if (clicked) {
  676. const position = snapRel(abs2rel({ x: e.clientX - dragOffsetX, y: e.clientY - dragOffsetY }));
  677. clicked.dataset.x = position.x;
  678. clicked.dataset.y = position.y;
  679. updateEntityElement(entities[clicked.dataset.key], clicked);
  680. if (hoveringInDeleteArea(e)) {
  681. document.querySelector("#menubar").classList.add("hover-delete");
  682. } else {
  683. document.querySelector("#menubar").classList.remove("hover-delete");
  684. }
  685. }
  686. });
  687. document.addEventListener("touchmove", (e) => {
  688. if (clicked) {
  689. e.preventDefault();
  690. let x = e.touches[0].clientX;
  691. let y = e.touches[0].clientY;
  692. const position = snapRel(abs2rel({ x: x - dragOffsetX, y: y - dragOffsetY }));
  693. clicked.dataset.x = position.x;
  694. clicked.dataset.y = position.y;
  695. updateEntityElement(entities[clicked.dataset.key], clicked);
  696. // what a hack
  697. // I should centralize this 'fake event' creation...
  698. if (hoveringInDeleteArea({ clientY: y })) {
  699. document.querySelector("#menubar").classList.add("hover-delete");
  700. } else {
  701. document.querySelector("#menubar").classList.remove("hover-delete");
  702. }
  703. }
  704. }, { passive: false });
  705. function fitWorld() {
  706. let max = math.unit(0, "meter");
  707. Object.entries(entities).forEach(([key, entity]) => {
  708. const view = document.querySelector("#entity-" + key).dataset.view;
  709. max = math.max(max, entity.views[view].height);
  710. });
  711. setWorldHeight(config.height, math.multiply(max, 1.1));
  712. }
  713. function updateWorldHeight() {
  714. const value = Math.max(1, document.querySelector("#options-height-value").value);
  715. const unit = document.querySelector("#options-height-unit").value;
  716. const oldHeight = config.height;
  717. setWorldHeight(oldHeight, math.unit(value, unit));
  718. }
  719. function setWorldHeight(oldHeight, newHeight) {
  720. config.height = newHeight;
  721. const unit = document.querySelector("#options-height-unit").value;
  722. document.querySelector("#options-height-value").value = config.height.toNumber(unit);
  723. Object.entries(entities).forEach(([key, entity]) => {
  724. const element = document.querySelector("#entity-" + key);
  725. const newPosition = adjustAbs({ x: element.dataset.x, y: element.dataset.y }, oldHeight, config.height);
  726. element.dataset.x = newPosition.x;
  727. element.dataset.y = newPosition.y;
  728. });
  729. updateSizes();
  730. }