Home Reference Source

cables_dev/cables_ui/src/ui/components/canvas/canvasui.js

  1. import { ele } from "cables-shared-client";
  2. import userSettings from "../usersettings.js";
  3.  
  4. export default class CanvasUi
  5. {
  6. constructor(cg)
  7. {
  8. this._cg = cg;
  9.  
  10. this.isCanvasFocussed = false;
  11. this.minimized = false;
  12.  
  13. this._elCanvasIconbarContainer = this._elCanvasIconbarContainer || ele.byId("canvasicons");
  14. this._elCanvasIconbar = this._elCanvasIconbar || ele.byId("canvasIconBar");
  15. this._elcanvasCtxSwitcher = this._elcanvasCtxSwitcher || ele.byId("canvasCtxSwitcher");
  16. this._elCanvasInfoSize = this._elCanvasInfoSize || ele.byId("canvasInfoSize");
  17. this._elSplitterPatch = this._elSplitterPatch || ele.byId("splitterPatch");
  18. this._elCanvasInfoFps = this._elCanvasInfoFps || document.getElementById("canvasInfoFPS");
  19. this._elCtxSwitcher = this._elCtxSwitcher || document.getElementById("canvasCtxSwitcher");
  20.  
  21. this._elCanvasInfoMs = this._elCanvasInfoMs || document.getElementById("canvasInfoMS");
  22. this._elInfoVersion = ele.byId("canvasInfoVersion");
  23.  
  24. this._elCanvasInfoSizeOverlay = ele.byId("canvasInfoOverlay");
  25.  
  26.  
  27. this._elCanvasIconbarContainer.addEventListener("click", () =>
  28. {
  29. this.canvasEle.focus();
  30. });
  31.  
  32. this._elCanvasInfoSize.addEventListener("pointerenter", () =>
  33. {
  34. this._elCanvasInfoSizeOverlay.style.top = this._elCanvasInfoSize.getBoundingClientRect().y + 30 + "px";
  35. this._elCanvasInfoSizeOverlay.style.left = this._elCanvasInfoSize.getBoundingClientRect().x + "px";
  36. // this._elCanvasInfoSizeOverlay.innerHTML = "";
  37. this._elCanvasInfoSizeOverlay.classList.remove("hidden");
  38. });
  39. this._elCanvasInfoSize.addEventListener("pointerleave", () =>
  40. {
  41. this._elCanvasInfoSizeOverlay.classList.add("hidden");
  42. });
  43.  
  44. if (this._elInfoVersion)
  45. {
  46. if (this._cg.glVersion == 1)
  47. {
  48. this._elCanvasInfoVer = this._elCanvasInfoVer || document.getElementById("canvasInfoVersion");
  49. this._elCanvasInfoVer.innerHTML = "WebGL 1";
  50. }
  51. else this._elInfoVersion.remove();
  52. }
  53.  
  54. this.canvasEle = this._cg.canvas;
  55.  
  56. cg.on("resize", () =>
  57. {
  58. this.updateSizeDisplay();
  59. });
  60.  
  61.  
  62. cg.fpsCounter.on("performance", (perf) =>
  63. {
  64. const p = CABLES.UI.uiProfiler.start("[canvasUi] on performance");
  65.  
  66. // if (this.isCanvasFocussed)
  67. // {
  68. if (this._oldFps != perf.fps) this._elCanvasInfoFps.innerHTML = perf.fps + " FPS";
  69. this._oldFps = perf.fps;
  70.  
  71. if (this._cg.profileData)
  72. {
  73. let ms = ((Math.round(this._cg.profileData.profileOnAnimFrameOps * 100) / 100) || "0.0") + "ms";
  74.  
  75. if (window.gui && gui.patchView.patchRenderer.vizLayer && gui.patchView.patchRenderer.vizLayer.renderMs > 3)
  76. {
  77. ms += " vizLayer: " + Math.round(gui.patchView.patchRenderer.vizLayer.renderMs) + "ms";
  78. }
  79.  
  80. if (this._oldMs != ms) this._elCanvasInfoMs.innerHTML = ms;
  81. this._oldMs = ms;
  82. }
  83. // }
  84.  
  85. p.finish();
  86. });
  87.  
  88. this.canvasEle.setAttribute("tabindex", 3);
  89.  
  90. this.canvasEle.addEventListener("focus", () =>
  91. {
  92. const p = CABLES.UI.uiProfiler.start("[canvasUi] on focus");
  93.  
  94. this.showCanvasModal(true);
  95. gui.canvasManager.setCurrentCanvas(this.canvasEle);
  96. p.finish();
  97. });
  98.  
  99. document.body.addEventListener("mousedown",
  100. (e) =>
  101. {
  102. if (this.isCanvasFocussed &&
  103. !e.target.classList.contains("item") &&
  104. !e.target.classList.contains("icon") &&
  105. e.target != this.canvasEle
  106. ) this.showCanvasModal(false);
  107. },
  108. true);
  109. }
  110.  
  111. get canvasMode()
  112. {
  113. return gui._canvasMode;
  114. }
  115.  
  116. updateCanvasIconBar()
  117. {
  118. if (!this._elCanvasIconbarContainer) return;
  119.  
  120. const perf = CABLES.UI.uiProfiler.start("[canvasUi] updateCanvasIconBar");
  121.  
  122. const splitterPatchRect = this._elSplitterPatch.getBoundingClientRect();
  123. const bodyRect = document.body.getBoundingClientRect();
  124.  
  125.  
  126. perf.finish();
  127. }
  128.  
  129. updateSizeDisplay()
  130. {
  131. if (!gui.corePatch().cgl) return;
  132.  
  133. const canvas = gui.canvasManager.currentCanvas();
  134.  
  135. const ctx = gui.canvasManager.currentContext();
  136.  
  137. this._elCanvasInfoAspect = this._elCanvasInfoAspect || document.getElementById("canvasInfoAspect");
  138.  
  139. let sizeStr = canvas.width + "x" + canvas.height;
  140. if (ctx.pixelDensity != 1) sizeStr += " (" + Math.round(ctx.pixelDensity * 100) / 100 + "x)";
  141.  
  142. gui.canvasManager.updateCanvasUi();
  143.  
  144.  
  145. if (this._oldSizeStr != sizeStr) this._elCanvasInfoSize.innerHTML = sizeStr;
  146. this._oldSizeStr = sizeStr;
  147.  
  148. this.updateIconState();
  149.  
  150. let str = "<table>";
  151. str += "<tr><td>Canvas API</td><td>" + ctx.getGApiName() + "</td></tr>";
  152. str += "<tr><td>Canvas id</td><td>" + canvas.id + "</td></tr>";
  153. str += "<tr><td>Canvas CSS Size:</td><td><code>" + canvas.clientWidth + "&nbsp;x&nbsp;" + canvas.clientHeight + "</td></tr>";
  154. str += "<tr><td>Canvas Pixel Size:</td><td><code>" + canvas.width + " x " + canvas.height + "</td></tr>";
  155. str += "<tr><td>Device Pixel Ratio/Density:</td><td><code>" + window.devicePixelRatio + "</td></tr>";
  156. str += "<tr><td>Canvas Pixel Ratio/Density:</td><td><code>" + ctx.pixelDensity + "</td></tr>";
  157. str += "</table>";
  158. this._elCanvasInfoSizeOverlay.innerHTML = str;
  159.  
  160. return sizeStr;
  161. }
  162.  
  163. updateIconState()
  164. {
  165. const act = userSettings.get("overlaysShow");
  166. const icon = ele.byId("canvUitoggleOverlay");
  167. if (icon)
  168. if (act)icon.style.backgroundColor = "var(--color-special)";
  169. else icon.style.backgroundColor = "var(--color-07)";
  170. }
  171.  
  172. showCanvasModal(_show)
  173. {
  174. if (userSettings.get("hideCanvasUi")) return;
  175.  
  176. const perf = CABLES.UI.uiProfiler.start("[canvasUi] showCanvasModal");
  177.  
  178. this._elCanvasModalDarkener = this._elCanvasModalDarkener || document.getElementById("canvasmodal");
  179.  
  180. this.updateSizeDisplay();
  181. this.updateCanvasIconBar();
  182.  
  183. this.isCanvasFocussed = _show;
  184. if (this.isCanvasFocussed) this._elCanvasIconbar.classList.remove("hidden");
  185. else this._elCanvasIconbar.classList.add("hidden");
  186.  
  187.  
  188.  
  189.  
  190. if (_show)
  191. {
  192. if (gui.canvasManager.mode == gui.canvasManager.CANVASMODE_PATCHBG)
  193.  
  194. {
  195. ele.hide(this._elCanvasModalDarkener);
  196. }
  197. else
  198. {
  199. if (!this._showing) ele.show(this._elCanvasModalDarkener);
  200. }
  201.  
  202. // if (!this._showing) ele.show(this._elCanvasIconbarContainer);
  203.  
  204.  
  205.  
  206. // const sizeStr = this.getCanvasSizeString();
  207.  
  208. // if (sizeStr != this._oldSizeStr) this._elCanvasInfoSize.innerHTML = sizeStr;
  209. // this._oldSizeStr = sizeStr;
  210. }
  211. else
  212. {
  213. setTimeout(() =>
  214. {
  215. // ele.hide(this._elCanvasIconbarContainer);
  216. ele.hide(this._elCanvasModalDarkener);
  217. }, 100);
  218. }
  219.  
  220. this._showing = _show;
  221.  
  222. perf.finish();
  223. }
  224. }