less copy protection, more size visualization
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

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