less copy protection, more size visualization
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

634 line
19 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. "kilometers",
  14. "feet",
  15. "miles",
  16. ],
  17. area: [
  18. "cm^2",
  19. "meters^2"
  20. ],
  21. mass: [
  22. "kilograms"
  23. ]
  24. }
  25. const config = {
  26. height: math.unit(1500, "meters"),
  27. minLineSize: 50,
  28. maxLineSize: 250
  29. }
  30. const entities = {
  31. }
  32. function constrainRel(coords) {
  33. return {
  34. x: Math.min(Math.max(coords.x, 0), 1),
  35. y: Math.min(Math.max(coords.y, 0), 1)
  36. }
  37. }
  38. function snapRel(coords) {
  39. return constrainRel({
  40. x: coords.x,
  41. y: altHeld ? coords.y : (Math.abs(coords.y - 1) < 0.05 ? 1 : coords.y)
  42. });
  43. }
  44. function adjustAbs(coords, oldHeight, newHeight) {
  45. return { x: coords.x, y: 1 + (coords.y - 1) * math.divide(oldHeight, newHeight) };
  46. }
  47. function rel2abs(coords) {
  48. const canvasWidth = document.querySelector("#display").clientWidth - 50;
  49. const canvasHeight = document.querySelector("#display").clientHeight - 50;
  50. return { x: coords.x * canvasWidth, y: coords.y * canvasHeight };
  51. }
  52. function abs2rel(coords) {
  53. const canvasWidth = document.querySelector("#display").clientWidth - 50;
  54. const canvasHeight = document.querySelector("#display").clientHeight - 50;
  55. return { x: coords.x / canvasWidth, y: coords.y / canvasHeight };
  56. }
  57. function updateEntityElement(entity, element) {
  58. const position = rel2abs({ x: element.dataset.x, y: element.dataset.y });
  59. const view = element.dataset.view;
  60. element.style.left = position.x + "px";
  61. element.style.top = position.y + "px";
  62. const canvasHeight = document.querySelector("#display").clientHeight;
  63. const pixels = math.divide(entity.views[view].height, config.height) * (canvasHeight - 100);
  64. element.style.setProperty("--height", pixels + "px");
  65. element.querySelector(".entity-name").innerText = entity.name;
  66. const bottomName = document.querySelector("#bottom-name-" + element.dataset.key);
  67. let entX = document.querySelector("#entities").getBoundingClientRect().x;
  68. bottomName.style.left = position.x + entX + "px";
  69. bottomName.style.top = "95vh";
  70. bottomName.innerText = entity.name;
  71. }
  72. function updateSizes() {
  73. drawScale();
  74. Object.entries(entities).forEach(([key, entity]) => {
  75. const element = document.querySelector("#entity-" + key);
  76. updateEntityElement(entity, element);
  77. });
  78. }
  79. function drawScale() {
  80. function drawTicks(/** @type {CanvasRenderingContext2D} */ ctx, pixelsPer, heightPer) {
  81. let total = heightPer.clone();
  82. total.value = 0;
  83. for (let y = ctx.canvas.clientHeight - 50; y >= 50; y -= pixelsPer) {
  84. drawTick(ctx, 50, y, total);
  85. total = math.add(total, heightPer);
  86. }
  87. }
  88. function drawTick(/** @type {CanvasRenderingContext2D} */ ctx, x, y, value) {
  89. const oldStroke = ctx.strokeStyle;
  90. const oldFill = ctx.fillStyle;
  91. ctx.beginPath();
  92. ctx.moveTo(x, y);
  93. ctx.lineTo(x + 20, y);
  94. ctx.strokeStyle = "#000000";
  95. ctx.stroke();
  96. ctx.beginPath();
  97. ctx.moveTo(x + 20, y);
  98. ctx.lineTo(ctx.canvas.clientWidth - 70, y);
  99. ctx.strokeStyle = "#aaaaaa";
  100. ctx.stroke();
  101. ctx.beginPath();
  102. ctx.moveTo(ctx.canvas.clientWidth - 70, y);
  103. ctx.lineTo(ctx.canvas.clientWidth - 50, y);
  104. ctx.strokeStyle = "#000000";
  105. ctx.stroke();
  106. const oldFont = ctx.font;
  107. ctx.font = 'normal 24pt coda';
  108. ctx.fillStyle = "#dddddd";
  109. ctx.beginPath();
  110. ctx.fillText(value.format({ precision: 3 }), x + 20, y + 35);
  111. ctx.font = oldFont;
  112. ctx.strokeStyle = oldStroke;
  113. ctx.fillStyle = oldFill;
  114. }
  115. const canvas = document.querySelector("#display");
  116. /** @type {CanvasRenderingContext2D} */
  117. const ctx = canvas.getContext("2d");
  118. let pixelsPer = (ctx.canvas.clientHeight - 100) / config.height.value;
  119. let heightPer = config.height.clone();
  120. heightPer.value = 1;
  121. if (pixelsPer < config.minLineSize) {
  122. heightPer.value /= pixelsPer / config.minLineSize;
  123. pixelsPer = config.minLineSize;
  124. }
  125. if (pixelsPer > config.maxLineSize) {
  126. heightPer.value /= pixelsPer / config.maxLineSize;
  127. pixelsPer = config.maxLineSize;
  128. }
  129. ctx.clearRect(0, 0, canvas.width, canvas.height);
  130. ctx.scale(1, 1);
  131. ctx.canvas.width = canvas.clientWidth;
  132. ctx.canvas.height = canvas.clientHeight;
  133. ctx.beginPath();
  134. ctx.moveTo(50, 50);
  135. ctx.lineTo(50, ctx.canvas.clientHeight - 50);
  136. ctx.stroke();
  137. ctx.beginPath();
  138. ctx.moveTo(ctx.canvas.clientWidth - 50, 50);
  139. ctx.lineTo(ctx.canvas.clientWidth - 50, ctx.canvas.clientHeight - 50);
  140. ctx.stroke();
  141. drawTicks(ctx, pixelsPer, heightPer);
  142. }
  143. function makeEntity(name, author, views) {
  144. const entityTemplate = {
  145. name: name,
  146. author: author,
  147. scale: 1,
  148. views: views,
  149. init: function () {
  150. Object.entries(this.views).forEach(([viewKey, view]) => {
  151. view.parent = this;
  152. if (this.defaultView === undefined) {
  153. this.defaultView = viewKey;
  154. }
  155. Object.entries(view.attributes).forEach(([key, val]) => {
  156. Object.defineProperty(
  157. view,
  158. key,
  159. {
  160. get: function() {
  161. return math.multiply(Math.pow(this.parent.scale, this.attributes[key].power), this.attributes[key].base);
  162. },
  163. set: function(value) {
  164. const newScale = Math.pow(math.divide(value, this.attributes[key].base), 1 / this.attributes[key].power);
  165. this.parent.scale = newScale;
  166. }
  167. }
  168. )
  169. });
  170. });
  171. delete this.init;
  172. return this;
  173. }
  174. }.init();
  175. return entityTemplate;
  176. }
  177. function clickDown(target, x, y) {
  178. clicked = target;
  179. const rect = target.getBoundingClientRect();
  180. let entX = document.querySelector("#entities").getBoundingClientRect().x;
  181. let entY = document.querySelector("#entities").getBoundingClientRect().y;
  182. dragOffsetX = x - rect.left + entX;
  183. dragOffsetY = y - rect.top + entY;
  184. clickTimeout = setTimeout(() => { dragging = true }, 200)
  185. }
  186. // could we make this actually detect the menu area?
  187. function hoveringInDeleteArea(e) {
  188. return e.clientY < document.body.clientHeight / 10;
  189. }
  190. function clickUp(e) {
  191. clearTimeout(clickTimeout);
  192. if (clicked) {
  193. if (dragging) {
  194. dragging = false;
  195. if (hoveringInDeleteArea(e)) {
  196. removeEntity(clicked);
  197. document.querySelector("#menubar").classList.remove("hover-delete");
  198. }
  199. } else {
  200. select(clicked);
  201. }
  202. clicked = null;
  203. }
  204. }
  205. function deselect() {
  206. if (selected) {
  207. selected.classList.remove("selected");
  208. }
  209. selected = null;
  210. clearViewList();
  211. clearEntityOptions();
  212. clearViewOptions();
  213. }
  214. function select(target) {
  215. deselect();
  216. selected = target;
  217. selectedEntity = entities[target.dataset.key];
  218. selected.classList.add("selected");
  219. configViewList(selectedEntity, target.dataset.view);
  220. configEntityOptions(selectedEntity, target.dataset.view);
  221. configViewOptions(selectedEntity, target.dataset.view);
  222. }
  223. function configViewList(entity, selectedView) {
  224. const list = document.querySelector("#entity-view");
  225. list.innerHTML = "";
  226. list.style.display = "block";
  227. Object.keys(entity.views).forEach(view => {
  228. const option = document.createElement("option");
  229. option.innerText = entity.views[view].name;
  230. option.value = view;
  231. if (view === selectedView) {
  232. option.selected = true;
  233. }
  234. list.appendChild(option);
  235. });
  236. }
  237. function clearViewList() {
  238. const list = document.querySelector("#entity-view");
  239. list.innerHTML = "";
  240. list.style.display = "none";
  241. }
  242. function updateWorldOptions(entity, view) {
  243. const heightInput = document.querySelector("#options-height-value");
  244. const heightSelect = document.querySelector("#options-height-unit");
  245. const converted = config.height.to(heightSelect.value);
  246. heightInput.value = math.round(converted.value, 3);
  247. }
  248. function configEntityOptions(entity, view) {
  249. const holder = document.querySelector("#options-entity");
  250. holder.innerHTML = "";
  251. const scaleLabel = document.createElement("div");
  252. scaleLabel.classList.add("options-label");
  253. scaleLabel.innerText = "Scale";
  254. const scaleRow = document.createElement("div");
  255. scaleRow.classList.add("options-row");
  256. const scaleInput = document.createElement("input");
  257. scaleInput.classList.add("options-field-numeric");
  258. scaleInput.id = "options-entity-scale";
  259. scaleInput.addEventListener("input", e => {
  260. entity.scale = e.target.value;
  261. updateSizes();
  262. updateEntityOptions(entity, view);
  263. updateViewOptions(entity, view);
  264. });
  265. scaleInput.setAttribute("min", 1);
  266. scaleInput.setAttribute("type", "number");
  267. scaleInput.value = entity.scale;
  268. scaleRow.appendChild(scaleInput);
  269. holder.appendChild(scaleLabel);
  270. holder.appendChild(scaleRow);
  271. const nameLabel = document.createElement("div");
  272. nameLabel.classList.add("options-label");
  273. nameLabel.innerText = "Name";
  274. const nameRow = document.createElement("div");
  275. nameRow.classList.add("options-row");
  276. const nameInput = document.createElement("input");
  277. nameInput.classList.add("options-field-text");
  278. nameInput.value = entity.name;
  279. nameInput.addEventListener("input", e => {
  280. entity.name = e.target.value;
  281. updateSizes();
  282. })
  283. nameRow.appendChild(nameInput);
  284. holder.appendChild(nameLabel);
  285. holder.appendChild(nameRow);
  286. }
  287. function updateEntityOptions(entity, view) {
  288. const scaleInput = document.querySelector("#options-entity-scale");
  289. scaleInput.value = entity.scale;
  290. }
  291. function clearEntityOptions() {
  292. const holder = document.querySelector("#options-entity");
  293. holder.innerHTML = "";
  294. }
  295. function configViewOptions(entity, view) {
  296. const holder = document.querySelector("#options-view");
  297. holder.innerHTML = "";
  298. Object.entries(entity.views[view].attributes).forEach(([key, val]) => {
  299. const label = document.createElement("div");
  300. label.classList.add("options-label");
  301. label.innerText = val.name;
  302. holder.appendChild(label);
  303. const row = document.createElement("div");
  304. row.classList.add("options-row");
  305. holder.appendChild(row);
  306. const input = document.createElement("input");
  307. input.classList.add("options-field-numeric");
  308. input.id = "options-view-" + key + "-input";
  309. input.setAttribute("type", "number");
  310. input.setAttribute("min", 1);
  311. input.value = entity.views[view][key].value;
  312. const select = document.createElement("select");
  313. select.id = "options-view-" + key + "-select"
  314. unitChoices[val.type].forEach(name => {
  315. const option = document.createElement("option");
  316. option.innerText = name;
  317. select.appendChild(option);
  318. });
  319. input.addEventListener("input", e => {
  320. entity.views[view][key] = math.unit(input.value, select.value);
  321. updateSizes();
  322. updateEntityOptions(entity, view);
  323. updateViewOptions(entity, view, key);
  324. });
  325. select.addEventListener("input", e => {
  326. entity.views[view][key] = math.unit(input.value, select.value);
  327. updateSizes();
  328. updateEntityOptions(entity, view);
  329. updateViewOptions(entity, view, key);
  330. });
  331. row.appendChild(input);
  332. row.appendChild(select);
  333. });
  334. }
  335. function updateViewOptions(entity, view, changed) {
  336. Object.entries(entity.views[view].attributes).forEach(([key, val]) => {
  337. if (key != changed) {
  338. const input = document.querySelector("#options-view-" + key + "-input");
  339. const select = document.querySelector("#options-view-" + key + "-select");
  340. const currentUnit = select.value;
  341. const convertedAmount = entity.views[view][key].to(currentUnit);
  342. input.value = math.round(convertedAmount.value, 5);
  343. }
  344. });
  345. }
  346. function clearViewOptions() {
  347. const holder = document.querySelector("#options-view");
  348. holder.innerHTML = "";
  349. }
  350. // this is a crime against humanity, and also stolen from
  351. // stack overflow
  352. // https://stackoverflow.com/questions/38487569/click-through-png-image-only-if-clicked-coordinate-is-transparent
  353. const testCanvas = document.createElement("canvas");
  354. testCanvas.id = "test-canvas";
  355. const testCtx = testCanvas.getContext("2d");
  356. function testClick(event) {
  357. const target = event.target;
  358. // Get click coordinates
  359. var x = event.clientX - target.getBoundingClientRect().x,
  360. y = event.clientY - target.getBoundingClientRect().y,
  361. w = testCtx.canvas.width = target.width,
  362. h = testCtx.canvas.height = target.height,
  363. alpha;
  364. // Draw image to canvas
  365. // and read Alpha channel value
  366. testCtx.drawImage(target, 0, 0, w, h);
  367. alpha = testCtx.getImageData(x, y, 1, 1).data[3]; // [0]R [1]G [2]B [3]A
  368. // If pixel is transparent,
  369. // retrieve the element underneath and trigger it's click event
  370. if (alpha === 0) {
  371. const oldDisplay = target.style.display;
  372. target.style.display = "none";
  373. const newTarget = document.elementFromPoint(event.clientX, event.clientY);
  374. newTarget.dispatchEvent(new MouseEvent(event.type, {
  375. "clientX": event.clientX,
  376. "clientY": event.clientY
  377. }));
  378. target.style.display = oldDisplay;
  379. } else {
  380. clickDown(target.parentElement, event.clientX, event.clientY);
  381. }
  382. }
  383. function removeEntity(element) {
  384. delete entities[element.dataset.key];
  385. element.parentElement.removeChild(element);
  386. }
  387. function displayEntity(entity, view, x, y) {
  388. const box = document.createElement("div");
  389. box.classList.add("entity-box");
  390. const img = document.createElement("img");
  391. img.classList.add("entity-image");
  392. const nameTag = document.createElement("div");
  393. nameTag.classList.add("entity-name");
  394. nameTag.innerText = entity.name;
  395. box.appendChild(img);
  396. box.appendChild(nameTag);
  397. img.src = entity.views[view].image.source;
  398. box.dataset.x = x;
  399. box.dataset.y = y;
  400. img.addEventListener("mousedown", e => { testClick(e); e.stopPropagation() });
  401. img.addEventListener("touchstart", e => {
  402. const fakeEvent = {
  403. target: e.target,
  404. clientX: e.touches[0].clientX,
  405. clientY: e.touches[0].clientY
  406. };
  407. testClick(fakeEvent);});
  408. box.id = "entity-" + entityIndex;
  409. box.dataset.key = entityIndex;
  410. box.dataset.view = view;
  411. entities[entityIndex] = entity;
  412. entity.index = entityIndex;
  413. const world = document.querySelector("#entities");
  414. world.appendChild(box);
  415. const bottomName = document.createElement("div");
  416. bottomName.classList.add("bottom-name");
  417. bottomName.id = "bottom-name-" + entityIndex;
  418. bottomName.innerText = entity.name;
  419. bottomName.addEventListener("click", () => select(box));
  420. world.appendChild(bottomName);
  421. entityIndex += 1;
  422. updateEntityElement(entity, box);
  423. }
  424. document.addEventListener("DOMContentLoaded", () => {
  425. const stuff = [makeFen()].concat(makeBuildings())
  426. let x = 0.2;
  427. stuff.forEach(entity => {
  428. displayEntity(entity, entity.defaultView, x, 1);
  429. x += 0.7 / stuff.length;
  430. })
  431. window.addEventListener("wheel", e => {
  432. const dir = e.deltaY < 0 ? 0.9 : 1.1;
  433. config.height = math.multiply(config.height, dir);
  434. updateSizes();
  435. updateWorldOptions();
  436. })
  437. document.querySelector("body").appendChild(testCtx.canvas);
  438. updateSizes();
  439. document.querySelector("#options-height-value").addEventListener("input", e => {
  440. updateWorldHeight();
  441. })
  442. document.querySelector("#options-height-unit").addEventListener("input", e => {
  443. updateWorldHeight();
  444. })
  445. world.addEventListener("mousedown", e => deselect());
  446. document.addEventListener("mouseup", e => clickUp(e));
  447. document.addEventListener("touchend", e => {
  448. const fakeEvent = {
  449. target: e.target,
  450. clientX: e.touches[0].clientX,
  451. clientY: e.touches[0].clientY
  452. };
  453. clickUp(fakeEvent);});
  454. document.querySelector("#entity-view").addEventListener("input", e => {
  455. selected.dataset.view = e.target.value
  456. selected.querySelector(".entity-image").src = entities[selected.dataset.key].views[e.target.value].image.source;
  457. updateSizes();
  458. updateEntityOptions(entities[selected.dataset.key], e.target.value);
  459. updateViewOptions(entities[selected.dataset.key], e.target.value);
  460. });
  461. clearViewList();
  462. });
  463. window.addEventListener("resize", () => {
  464. updateSizes();
  465. })
  466. document.addEventListener("mousemove", (e) => {
  467. if (clicked) {
  468. const position = snapRel(abs2rel({ x: e.clientX - dragOffsetX, y: e.clientY - dragOffsetY }));
  469. clicked.dataset.x = position.x;
  470. clicked.dataset.y = position.y;
  471. updateEntityElement(entities[clicked.dataset.key], clicked);
  472. if (hoveringInDeleteArea(e)) {
  473. document.querySelector("#menubar").classList.add("hover-delete");
  474. } else {
  475. document.querySelector("#menubar").classList.remove("hover-delete");
  476. }
  477. }
  478. });
  479. document.addEventListener("touchmove", (e) => {
  480. if (clicked) {
  481. e.preventDefault();
  482. let x = e.touches[0].clientX;
  483. let y = e.touches[0].clientY;
  484. const position = snapRel(abs2rel({ x: x - dragOffsetX, y: y - dragOffsetY }));
  485. clicked.dataset.x = position.x;
  486. clicked.dataset.y = position.y;
  487. updateEntityElement(entities[clicked.dataset.key], clicked);
  488. }
  489. }, {passive: false});
  490. function updateWorldHeight() {
  491. const value = Math.max(1, document.querySelector("#options-height-value").value);
  492. const unit = document.querySelector("#options-height-unit").value;
  493. const oldHeight = config.height;
  494. config.height = math.unit(value + " " + unit)
  495. Object.entries(entities).forEach(([key, entity]) => {
  496. const element = document.querySelector("#entity-" + key);
  497. const newPosition = adjustAbs({ x: element.dataset.x, y: element.dataset.y }, oldHeight, config.height);
  498. element.dataset.x = newPosition.x;
  499. element.dataset.y = newPosition.y;
  500. });
  501. updateSizes();
  502. }