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.
 
 
 

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