cables_dev/cables_ui/src/ui/glpatch/gluicanvas.js
- import { Texture } from "cables-corelibs";
- import GlCanvas from "../gldraw/glcanvas.js";
- import { gui } from "../gui.js";
- import GlPatch from "./glpatch.js";
- import GlPatchAPI from "./patchapi.js";
- import { UiPatch } from "../core_extend_patch.js";
- export default class GlUiCanvas extends GlCanvas
- {
-
- constructor(_patch, parentEle)
- {
- super(_patch, parentEle);
- if (!this.cgl || !this.cgl.gl) return;
- this.cgl.name = "ui";
- this.glPatch = new GlPatch(this.cgl);
- this.patchApi = new GlPatchAPI(_patch, this.glPatch);
- this.patchApi.reset();
- this.cgl.on("resize", () =>
- {
- this.glPatch.emitEvent(GlPatch.EVENT_RESIZE, this.width * window.devicePixelRatio, this.height * window.devicePixelRatio);
- });
- this.cgl.on("beginFrame", () =>
- {
- this.glPatch.vizLayer.renderVizLayer(false);
- });
- this.setSize(100, 100);
- gui.on("canvasModeChange", (mode) =>
- {
- this.canvas.classList.toggle("gluiPatchBg", mode == gui.canvasManager.CANVASMODE_PATCHBG);
- });
- this.glPatch.on(GlPatch.EVENT_PAUSED, () =>
- {
-
- });
- this.glPatch.on(GlPatch.EVENT_RESUMED, () =>
- {
- this.cgl.setSize(this.width, this.height);
-
- });
- this.canvas.oncontextmenu = (e) => { e.preventDefault(); e.stopPropagation(); };
- this.canvas.addEventListener("pointermove", (_e) =>
- {
- this.glPatch.needsRedraw = true;
- }, { "passive": true });
- this.canvas.addEventListener("pointerdown", (_e) =>
- {
- this.glPatch.needsRedraw = true;
- }, { "passive": true });
- this.canvas.addEventListener("pointerup", (_e) =>
- {
- this.glPatch.needsRedraw = true;
- }, { "passive": true });
-
- this.canvas.addEventListener("wheel", (event) => { if (event.ctrlKey) event.preventDefault(); }, { "passive": false });
- this.canvas.addEventListener("wheel", (_event) =>
- {
- this.activityHigh();
- }, { "passive": true });
- this.parentResized();
- this.activityHigh();
- requestAnimationFrame(() =>
- {
-
-
- this.render();
- });
- }
- parentResized()
- {
- if (!this.glPatch) return;
- this.setSize(this._parentEle.clientWidth, this._parentEle.clientHeight);
- this.glPatch.needsRedraw = true;
- this.glPatch.emitEvent(GlPatch.EVENT_RESIZE, this._parentEle.clientWidth, this._parentEle.clientHeight);
- this._lastTime = 0;
- }
- render()
- {
- const startTime = performance.now();
- this.glPatch.updateTime();
- requestAnimationFrame(() => { this.render(); });
- if (this.targetFps != 0 && !this.glPatch.mouseState.mouseOverCanvas && performance.now() - this._lastTime < 1000 / this.targetFps) return;
- const cgl = this.cgl;
- cgl.perfProfiler = gui.corePatch().perfProfiler;
- cgl.name = "ui ";
- cgl.doGlQueryTiming = true;
- cgl.renderStart(cgl);
- if (cgl.lastMesh)cgl.lastMesh.unBind();
- if (this._oldTargetFps != this.targetFps) this._oldTargetFps = this.targetFps;
- if (!this._inited)
- {
- for (let i = 0; i <= 8; i++) this.cgl.setTexture(i, Texture.getEmptyTexture(this.cgl).tex);
- this._inited = true;
- }
- if (this._firstTime) this._firstTime = false;
- if (this.glPatch.debugData)
- this.glPatch.debugData.targetFps = this.targetFps;
- this.glPatch.render(this.width, this.height);
- if (this.glPatch.isAnimated) this.activityHigh();
- cgl.renderEnd(cgl);
- this._lastTime = performance.now();
- gui.corePatch().perfProfiler.setDuration("ui cpu", performance.now() - startTime);
- }
- }