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

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