Home Reference Source

cables_dev/cables_ui/src/ui/glpatch/gluicanvas.js

  1. import { Texture } from "cables-corelibs";
  2. import GlCanvas from "../gldraw/glcanvas.js";
  3. import { gui } from "../gui.js";
  4. import GlPatch from "./glpatch.js";
  5. import GlPatchAPI from "./patchapi.js";
  6. import { UiPatch } from "../core_extend_patch.js";
  7. /** Canvas of the patchfield ({@link GlPatch}). */
  8. export default class GlUiCanvas extends GlCanvas
  9. {
  10. /**
  11. * @param {UiPatch} _patch
  12. * @param {HTMLElement} parentEle
  13. */
  14. constructor(_patch, parentEle)
  15. {
  16. super(_patch, parentEle);
  17. if (!this.cgl || !this.cgl.gl) return;
  18. this.cgl.name = "ui";
  19. this.glPatch = new GlPatch(this.cgl);
  20. this.patchApi = new GlPatchAPI(_patch, this.glPatch);
  21. this.patchApi.reset();
  22. this.cgl.on("resize", () =>
  23. {
  24. this.glPatch.emitEvent(GlPatch.EVENT_RESIZE, this.width * window.devicePixelRatio, this.height * window.devicePixelRatio);
  25. });
  26. this.cgl.on("beginFrame", () =>
  27. {
  28. this.glPatch.vizLayer.renderVizLayer(false);
  29. });
  30. this.setSize(100, 100);
  31. gui.on("canvasModeChange", (mode) =>
  32. {
  33. this.canvas.classList.toggle("gluiPatchBg", mode == gui.canvasManager.CANVASMODE_PATCHBG);
  34. });
  35. this.glPatch.on(GlPatch.EVENT_PAUSED, () =>
  36. {
  37. // this.patch.pause();
  38. });
  39. this.glPatch.on(GlPatch.EVENT_RESUMED, () =>
  40. {
  41. this.cgl.setSize(this.width, this.height);
  42. // this.patch.resume();
  43. });
  44. this.canvas.oncontextmenu = (e) => { e.preventDefault(); e.stopPropagation(); };
  45. this.canvas.addEventListener("pointermove", (_e) =>
  46. {
  47. this.glPatch.needsRedraw = true;
  48. }, { "passive": true });
  49. this.canvas.addEventListener("pointerdown", (_e) =>
  50. {
  51. this.glPatch.needsRedraw = true;
  52. }, { "passive": true });
  53. this.canvas.addEventListener("pointerup", (_e) =>
  54. {
  55. this.glPatch.needsRedraw = true;
  56. }, { "passive": true });
  57. // this is for disabling touchpad "pinch with two fingers" on macs, which would zoom in html
  58. this.canvas.addEventListener("wheel", (event) => { if (event.ctrlKey) event.preventDefault(); }, { "passive": false });
  59. this.canvas.addEventListener("wheel", (_event) =>
  60. {
  61. this.activityHigh();
  62. }, { "passive": true });
  63. this.parentResized();
  64. this.activityHigh();
  65. requestAnimationFrame(() =>
  66. {
  67. // if (gui.corePatch().cgl) this.cgl.doGlQueryTiming = gui.corePatch().cgl.doGlQueryTiming;
  68. // gui.corePatch().perfProfiler.setDuration("glui")
  69. this.render();
  70. });
  71. }
  72. parentResized()
  73. {
  74. if (!this.glPatch) return;
  75. this.setSize(this._parentEle.clientWidth, this._parentEle.clientHeight);
  76. this.glPatch.needsRedraw = true;
  77. this.glPatch.emitEvent(GlPatch.EVENT_RESIZE, this._parentEle.clientWidth, this._parentEle.clientHeight);
  78. this._lastTime = 0;
  79. }
  80. render()
  81. {
  82. const startTime = performance.now();
  83. this.glPatch.updateTime();
  84. requestAnimationFrame(() => { this.render(); });
  85. if (this.targetFps != 0 && !this.glPatch.mouseState.mouseOverCanvas && performance.now() - this._lastTime < 1000 / this.targetFps) return;
  86. const cgl = this.cgl;
  87. cgl.perfProfiler = gui.corePatch().perfProfiler;
  88. cgl.name = "ui ";
  89. cgl.doGlQueryTiming = true;
  90. cgl.renderStart(cgl);
  91. if (cgl.lastMesh)cgl.lastMesh.unBind();
  92. if (this._oldTargetFps != this.targetFps) this._oldTargetFps = this.targetFps;
  93. if (!this._inited)
  94. {
  95. for (let i = 0; i <= 8; i++) this.cgl.setTexture(i, Texture.getEmptyTexture(this.cgl).tex);
  96. this._inited = true;
  97. }
  98. if (this._firstTime) this._firstTime = false;
  99. if (this.glPatch.debugData)
  100. this.glPatch.debugData.targetFps = this.targetFps;
  101. this.glPatch.render(this.width, this.height);
  102. if (this.glPatch.isAnimated) this.activityHigh();
  103. cgl.renderEnd(cgl);
  104. this._lastTime = performance.now();
  105. gui.corePatch().perfProfiler.setDuration("ui cpu", performance.now() - startTime);
  106. }
  107. }