less copy protection, more size visualization
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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