less copy protection, more size visualization
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

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