Home Reference Source

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

  1. import { Logger, ele, Events } from "cables-shared-client";
  2. import { Anim, Op, Patch } from "cables";
  3. import { idleCallbackSoon, logStack } from "cables/src/core/utils.js";
  4. import { BoundingBox } from "cables-corelibs/cg/cg_boundingbox.js";
  5. import { CglContext } from "cables-corelibs/cgl/cgl_state.js";
  6. import GlRectInstancer from "../gldraw/glrectinstancer.js";
  7. import GlTextWriter from "../gldraw/gltextwriter.js";
  8. import GlText from "../gldraw/gltext.js";
  9. import GlDragLine from "./gldragline.js";
  10. import GlSelectionArea from "./glselectionarea.js";
  11. import GlViewBox from "./glviewbox.js";
  12. import GlOp from "./glop.js";
  13. import MouseState from "./mousestate.js";
  14. import GlCursor from "./glcursor.js";
  15. import ShakeDetector from "./shakedetect.js";
  16. import VizLayer from "./vizlayer.js";
  17. import { GuiText } from "../text.js";
  18. import Gui, { gui } from "../gui.js";
  19. import Snap from "./snap.js";
  20. import gluiconfig from "./gluiconfig.js";
  21. import { updateHoverToolTip, hideToolTip } from "../elements/tooltips.js";
  22. import { notify } from "../elements/notification.js";
  23. import { UserSettings, userSettings } from "../components/usersettings.js";
  24. import { CmdOps } from "../commands/cmd_op.js";
  25. import { CmdPatch } from "../commands/cmd_patch.js";
  26. import GlLink from "./gllink.js";
  27. import { DomEvents } from "../theme.js";
  28. import { GlSplineDrawer } from "../gldraw/glsplinedrawer.js";
  29. import { InputBindings } from "../inputbindings.js";
  30. import GlPatchAPI from "./patchapi.js";
  31. import { UiOp } from "../core_extend_op.js";
  32. import { GlLineDrawer } from "../gldraw/gllinedrawer.js";
  33. import undo from "../utils/undo.js";
  34. let idleSoon = null;
  35. const SELECTION_UNDO_DELAY_MS = 300;
  36. let lastUpdate = 0;
  37. /** Renders the patchfield. */
  38. export default class GlPatch extends Events
  39. {
  40. static USERPREF_GLPATCH_CABLE_WIDTH = "glcablewidth";
  41. static EVENT_MOUSE_META_SCROLL = "metascroll";
  42. static EVENT_MOUSE_UP_OVER_OP = "mouseUpOverOp";
  43. static EVENT_MOUSE_UP_OVER_PORT = "mouseUpOverPort";
  44. static EVENT_MOUSE_DOWN_OVER_PORT = "mouseDownOverPort";
  45. static EVENT_MOUSE_DRAG_LINK = "mouseDragLink";
  46. static EVENT_MOUSE_UP = "mouseup";
  47. static EVENT_SELECTED_OPS_CHANGED = "selectedOpsChanged";
  48. static EVENT_SPACE_DOWN = "spacedown";
  49. static EVENT_SPACE_UP = "spaceup";
  50. static EVENT_MOUSE_MOVE = "mousemove";
  51. static EVENT_MOUSE_LEAVE = "mouseleave";
  52. static EVENT_MOUSE_ENTER = "mouseenter";
  53. static EVENT_MOUSE_DOWN = "mousedown";
  54. static EVENT_PAUSED = "paused";
  55. static EVENT_RESUMED = "resumed";
  56. static EVENT_RESIZE = "resize";
  57. static EVENT_META_SCROLL = "META_SCROLL";
  58. #cgl = null;
  59. /** @type {string[]} */
  60. #selectionUndoIds = [];
  61. #selectionUndoTimeout = null;
  62. #selectionUndoIndex = -1;
  63. #selectionUndoLength = 0;
  64. hoverPort = null;
  65. paused = false;
  66. pauseTimeOut = null;
  67. blueprint = false;
  68. isAnimated = false;
  69. #numSelectedGlOps = 0;
  70. /** @type {import("../gldraw/glrect.js").default} */
  71. hoverResizeRect = null;
  72. _mouseLeaveButtons = 0;
  73. /** @type {number[]} */
  74. _cutLine = [];
  75. cutLineActive = false;
  76. /** @type {Object<string,GlOp>} */
  77. _glOpz = {};
  78. /** @type {string | any[]} */
  79. _hoverOps = [];
  80. /** @type {string[]} */
  81. _ignoreNonExistError = [];
  82. _hoverOpLongStartTime = 0;
  83. /** @type {GlPatchAPI} */
  84. _patchAPI = null;
  85. debugData = null;
  86. greyOut = false;
  87. _greyOutRect = null;
  88. startLinkButtonDrag = null;
  89. _time = 0;
  90. frameCount = 0;
  91. _viewZoom = 0;
  92. needsRedraw = false;
  93. /** @type {Object<string,GlOp>} */
  94. _selectedGlOps = {};
  95. /** @type {Object<string,GlLink>} */
  96. links = {};
  97. _dropInCircleLink = null;
  98. _dropInCircleRect = null;
  99. #cachedNumSelectedOps = 0;
  100. _cachedFirstSelectedOp = null;
  101. _showingOpCursor = false;
  102. _fpsStartTime = 0;
  103. cacheOIRxa = 0;
  104. cacheOIRya = 0;
  105. cacheOIRxb = 0;
  106. cacheOIRyb = 0;
  107. cacheOIRops = null;
  108. _subpatchoprect = null;
  109. suggestionTeaser = null;
  110. /** @type {Object<String,GlLineDrawer>} */
  111. #splineDrawers = null;
  112. #selectionArea = null;
  113. #dropInOpBorder = null;
  114. /** @type {GlRectInstancer} */
  115. #rectInstancer = null;
  116. #overlaySplines = null;
  117. #numSelOpsTimeout = null;
  118. #textWriter = null;
  119. #lastMouseX = -1;
  120. #lastMouseY = -1;
  121. /** @type {GlLink} */
  122. #hoverLink;
  123. #lines;
  124. /**
  125. * @param {CglContext} cgl
  126. */
  127. constructor(cgl)
  128. {
  129. super();
  130. this.logEvents(false, "glpatch");
  131. this._log = new Logger("glpatch");
  132. if (!cgl) this._log.error("[glpatch] need cgl");
  133. this.#cgl = cgl;
  134. this.mouseState = new MouseState(cgl.canvas);
  135. this._timeStart = performance.now();
  136. this.vizFlowMode = userSettings.get(UserSettings.PREF_GLFLOWMODE) || 0;
  137. this.#overlaySplines = new GlSplineDrawer(cgl, "overlaysplines");
  138. this.#overlaySplines.zPos = 0.5;
  139. this.#splineDrawers = { "0": new GlSplineDrawer(cgl, "patchCableSplines_0") };
  140. this.viewBox = new GlViewBox(cgl, this);
  141. this.#rectInstancer = new GlRectInstancer(cgl, { "name": "mainrects", "initNum": 1000, "hoverWhenButton": true });
  142. this.#lines = new GlSplineDrawer(cgl, "links");
  143. this._overLayRects = new GlRectInstancer(cgl, { "name": "overlayrects" });
  144. this.#rectInstancer.hoverWhenButton;
  145. this._overLayRects.hoverWhenButton = false;
  146. this.#textWriter = new GlTextWriter(cgl, { "name": "mainText", "initNum": 1000 });
  147. this._textWriterOverlay = new GlTextWriter(cgl, { "name": "textoverlay" });
  148. /** @type {string} */
  149. this._currentSubpatch = "0";
  150. this.#selectionArea = new GlSelectionArea(this._overLayRects);
  151. this.portDragLine = new GlDragLine(this.#overlaySplines, this);
  152. // if (userSettings.get(UserSettings.PREF_DEVINFOS))
  153. // {
  154. // CABLES.UI.showDevInfos = true;
  155. // const idx = this.#overlaySplines.getSplineIndex();
  156. // this.#overlaySplines.setSpline(idx, [-1000000, 0, 0, 1000000, 0, 0]);
  157. // this.#overlaySplines.setSplineColor(idx, [0.25, 0.25, 0.25, 1.0]);
  158. // const idx2 = this.#overlaySplines.getSplineIndex();
  159. // this.#overlaySplines.setSpline(idx2, [0, -1000000, 0, 0, 1000000, 0]);
  160. // this.#overlaySplines.setSplineColor(idx2, [0.25, 0.25, 0.25, 1.0]);
  161. // }
  162. this.cablesHoverText = new GlText(this.#textWriter, "");
  163. this.cablesHoverText.setPosition(0, 0);
  164. this.cablesHoverText.setColor(1, 1, 1, 0);
  165. this._eleDropOp = ele.byId("drop-op-cursor");
  166. this._subpatchAnimFade = new Anim({ "defaultEasing": Anim.EASING_CUBIC_OUT });
  167. this._subpatchAnimOutX = new Anim({ "defaultEasing": Anim.EASING_CUBIC_OUT });
  168. this._subpatchAnimOutY = new Anim({ "defaultEasing": Anim.EASING_CUBIC_OUT });
  169. this._subpatchAnimOutW = new Anim({ "defaultEasing": Anim.EASING_CUBIC_OUT });
  170. this._subpatchAnimOutH = new Anim({ "defaultEasing": Anim.EASING_CUBIC_OUT });
  171. this._focusRectAnim = new Anim({ "defaultEasing": Anim.EASING_CUBIC_OUT });
  172. this._focusRect = this._overLayRects.createRect({ "name": "focusrect", "interactive": false });
  173. this._focusRect.setSize(1, 1);
  174. this._focusRect.setShape(4);
  175. this._focusRect.setColor(0, 1, 1, 1);
  176. this._focusRect.visible = false;
  177. this._glCursors = {};
  178. this._localGlCursor = new GlCursor(this, this._overLayRects);
  179. this._localGlCursor.setColor(1, 1, 1, 1);
  180. this._glCursors[0] = this._localGlCursor;
  181. this._glSelectionAreas = {};
  182. this.opShakeDetector = new ShakeDetector();
  183. this.opShakeDetector.on("shake", () => { if (gui.patchView.getSelectedOps().length === 1)gui.patchView.unlinkSelectedOps(); });
  184. this.snap = new Snap(cgl, this, this.#rectInstancer);
  185. // this._redrawFlash = this._overLayRects.createRect({ "name": "redrawflash", "interactive": false });
  186. // this._redrawFlash.setSize(50, 5);
  187. // this._redrawFlash.setColor(0, 1, 0, 1);
  188. this._fadeOutRectAnim = new Anim({ "defaultEasing": Anim.EASING_LINEAR });
  189. this._fadeOutRect = this._overLayRects.createRect({ "name": "fadeoutrect", "interactive": false });
  190. this._fadeOutRect.setSize(100000000, 100000000);
  191. this._fadeOutRect.setPosition(-50000000, -50000000);
  192. this._fadeOutRect.setColor(0, 0, 0, 0.0);
  193. this._fadeOutRect.visible = true;
  194. this.#dropInOpBorder = this._overLayRects.createRect({ "interactive": false, "name": "dropinborder" });
  195. this.#dropInOpBorder.setSize(100, 100);
  196. this.#dropInOpBorder.setColor(1, 0, 0, 1);
  197. this.#dropInOpBorder.visible = false;
  198. cgl.canvas.addEventListener(DomEvents.POINTER_MOVE, this._onCanvasMouseMove.bind(this), { "passive": false });
  199. cgl.canvas.addEventListener(DomEvents.POINTER_UP, this.#onCanvasMouseUp.bind(this), { "passive": false });
  200. cgl.canvas.addEventListener(DomEvents.POINTER_DOWN, this._onCanvasMouseDown.bind(this), { "passive": false });
  201. cgl.canvas.addEventListener(DomEvents.POINTER_LEAVE, this._onCanvasMouseLeave.bind(this), { "passive": false });
  202. cgl.canvas.addEventListener(DomEvents.POINTER_ENTER, this._onCanvasMouseEnter.bind(this), { "passive": false });
  203. cgl.canvas.addEventListener(DomEvents.POINTER_DBL_CLICK, this.#onCanvasDblClick.bind(this), { "passive": false });
  204. cgl.canvas.addEventListener(DomEvents.TOUCH_FOCUS, this.isFocused.bind(this));
  205. cgl.canvas.addEventListener(DomEvents.TOUCH_BLUR, this.isFocused.bind(this));
  206. gui.on(Gui.EVENT_THEMECHANGED, this.updateTheme.bind(this));
  207. gui.keys.key("1", "Left", "down", null, { "ignoreInput": true }, () =>
  208. {
  209. gui.inputBindings.exec(InputBindings.KEY_1);
  210. });
  211. gui.keys.key("2", "Left", "down", null, { "ignoreInput": true }, () =>
  212. {
  213. gui.inputBindings.exec(InputBindings.KEY_2);
  214. });
  215. gui.keys.key("3", "Left", "down", null, { "ignoreInput": true }, () =>
  216. {
  217. gui.inputBindings.exec(InputBindings.KEY_3);
  218. });
  219. gui.keys.key("4", "Left", "down", null, { "ignoreInput": true }, () =>
  220. {
  221. gui.inputBindings.exec(InputBindings.KEY_4);
  222. });
  223. gui.keys.key("5", "Left", "down", null, { "ignoreInput": true }, () =>
  224. {
  225. gui.inputBindings.exec(InputBindings.KEY_5);
  226. });
  227. gui.keys.key("6", "Left", "down", null, { "ignoreInput": true }, () =>
  228. {
  229. gui.inputBindings.exec(InputBindings.KEY_6);
  230. });
  231. gui.keys.key("7", "Left", "down", null, { "ignoreInput": true }, () =>
  232. {
  233. gui.inputBindings.exec(InputBindings.KEY_7);
  234. });
  235. gui.keys.key("8", "Left", "down", null, { "ignoreInput": true }, () =>
  236. {
  237. gui.inputBindings.exec(InputBindings.KEY_8);
  238. });
  239. gui.keys.key("9", "Left", "down", null, { "ignoreInput": true }, () =>
  240. {
  241. gui.inputBindings.exec(InputBindings.KEY_9);
  242. });
  243. gui.keys.key("0", "Left", "down", null, { "ignoreInput": true }, () =>
  244. {
  245. gui.inputBindings.exec(InputBindings.KEY_0);
  246. });
  247. gui.keys.key("ArrowLeft", "Left", "down", cgl.canvas.id, { "cmdCtrl": true }, () =>
  248. {
  249. this.viewBox.keyScrollX(-1);
  250. });
  251. gui.keys.key("ArrowRight", "Left", "down", cgl.canvas.id, { "cmdCtrl": true }, () =>
  252. {
  253. this.viewBox.keyScrollX(1);
  254. });
  255. gui.keys.key("ArrowUp", "Left", "down", cgl.canvas.id, { "cmdCtrl": true }, () =>
  256. {
  257. this.viewBox.keyScrollY(-1);
  258. });
  259. gui.keys.key("ArrowDown", "Left", "down", cgl.canvas.id, { "cmdCtrl": true }, () =>
  260. {
  261. this.viewBox.keyScrollY(1);
  262. });
  263. gui.keys.key("ArrowUp", "", "down", cgl.canvas.id, { "ignoreInput": true }, () =>
  264. {
  265. gui.patchView.cursorNavOps(0, -1);
  266. });
  267. gui.keys.key("ArrowDown", "", "down", cgl.canvas.id, { "ignoreInput": true }, () =>
  268. {
  269. gui.patchView.cursorNavOps(0, 1);
  270. });
  271. gui.keys.key("ArrowLeft", "", "down", cgl.canvas.id, { "ignoreInput": true }, () =>
  272. {
  273. gui.patchView.cursorNavOps(-1, 0);
  274. });
  275. gui.keys.key("ArrowRight", "", "down", cgl.canvas.id, { "ignoreInput": true }, () =>
  276. {
  277. gui.patchView.cursorNavOps(1, 0);
  278. });
  279. gui.keys.key(["Delete", "Backspace"], "Delete selected ops", "down", cgl.canvas.id, {}, this._onKeyDelete.bind(this));
  280. gui.keys.key("f", "Toggle flow visualization", "down", cgl.canvas.id, {}, (_e) =>
  281. {
  282. let fm = this.vizFlowMode || 0;
  283. fm++;
  284. if (fm == 3)fm = 0;
  285. const modes = ["Off", "Highlight Active", "Show Dataflow"];
  286. notify("Flow Visualization: ", modes[fm]);
  287. userSettings.set(UserSettings.PREF_GLFLOWMODE, fm);
  288. });
  289. gui.keys.key(" ", "Drag left mouse button to pan patch", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) => { this._spacePressed = true; this.emitEvent(GlPatch.EVENT_SPACE_DOWN); });
  290. gui.keys.key(" ", "", "up", cgl.canvas.id, { "displayGroup": "editor" }, (_e) => { this._spacePressed = false; this.emitEvent(GlPatch.EVENT_SPACE_UP); });
  291. gui.keys.key("e", "Edit op code", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) => { CmdOps.editOp(true); });
  292. gui.keys.key("c", "Center Selected Ops", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) =>
  293. {
  294. this.viewBox.centerSelectedOps();
  295. if (gui.patchView.getSelectedOps().length == 1)
  296. this.focusOpAnim(gui.patchView.getSelectedOps()[0].id);
  297. });
  298. gui.keys.key("x", "Unlink selected ops", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) => { gui.patchView.unlinkSelectedOps(); });
  299. gui.keys.key("x", "Unlink selected ops first ports only", "down", cgl.canvas.id, { "shiftKey": true, "displayGroup": "editor" }, (_e) => { gui.patchView.unlinkSelectedOps(true); });
  300. gui.keys.key("u", "Goto parent subpatch", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) => { CmdPatch.gotoParentSubpatch(); });
  301. gui.keys.key("a", "Select all ops in current subpatch", "down", cgl.canvas.id, { "cmdCtrl": true, "displayGroup": "editor" }, (_e) => { gui.patchView.selectAllOpsSubPatch(this._currentSubpatch); });
  302. gui.keys.key("a", "Align selected ops", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) =>
  303. {
  304. for (let j = 0; j < 20; j++)
  305. for (const i in this._selectedGlOps)
  306. {
  307. if (this._selectedGlOps[i].op)
  308. this._selectedGlOps[i].op.setUiAttribs(
  309. {
  310. "translate":
  311. {
  312. "x": this.snap.snapOpX(this._selectedGlOps[i].op.uiAttribs.translate.x, this._selectedGlOps[i].op, 1000),
  313. "y": this._selectedGlOps[i].op.uiAttribs.translate.y
  314. } });
  315. gui.patchView.testCollision(this._selectedGlOps[i].op);
  316. }
  317. });
  318. gui.keys.key("a", "Compress selected ops vertically", "down", cgl.canvas.id, { "shiftKey": true, "displayGroup": "editor" }, (_e) => { gui.patchView.compressSelectedOps(gui.patchView.getSelectedOps()); });
  319. gui.keys.key("p", "previous op", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) => { gui.opHistory.back(); });
  320. gui.keys.key("p", "previous op", "down", cgl.canvas.id, { "displayGroup": "editor", "shiftKey": true }, (_e) => { gui.opHistory.forward(); });
  321. // gui.keys.key("i", "Navigate op history forward", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) => { gui.opHistory.forward(); });
  322. // gui.keys.key("j", "Navigate op history back", "down", cgl.canvas.id, { "shiftKey": true, "displayGroup": "editor" }, (_e) => { gui.opHistory.back(); });
  323. // gui.keys.key("k", "Navigate op history forward", "down", cgl.canvas.id, { "shiftKey": true, "displayGroup": "editor" }, (_e) => { gui.opHistory.forward(); });
  324. gui.keys.key("d", "Disable Op", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) => { this.toggleOpsEnable(); });
  325. gui.keys.key("d", "Duplicate selected ops", "down", cgl.canvas.id, { "cmdCtrl": true, "displayGroup": "editor" }, (_e) => { gui.patchView.duplicateSelectedOps(); });
  326. // gui.keys.key("d", "Temporary unlink op", "down", cgl.canvas.id, { "shiftKey": true, "displayGroup": "editor" }, (_e) => { gui.patchView.tempUnlinkOp(); });
  327. gui.keys.key("!", "debug", "down", cgl.canvas.id, { "shiftKey": true, "displayGroup": "editor" }, (_e) => { this._cycleDebug(); });
  328. gui.keys.key("+", "Zoom In", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) => { this.zoomStep(-1); });
  329. gui.keys.key("=", "Zoom In", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) => { this.zoomStep(-1); });
  330. gui.keys.key("-", "Zoom Out", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) => { this.zoomStep(1); });
  331. gui.keys.key("t", "Set Title", "down", cgl.canvas.id, { "displayGroup": "editor" }, (_e) => { CmdPatch.setOpTitle(); });
  332. gui.keys.key("y", "Cut Cables", "down", cgl.canvas.id, { "displayGroup": "editor" }, () =>
  333. {
  334. if (!this.cutLineActive) this._cutLine = [];
  335. this.cutLineActive = true;
  336. });
  337. gui.keys.key("y", "Cut Cables", "up", cgl.canvas.id, { "displayGroup": "editor" }, () =>
  338. {
  339. this.cutLineActive = false;
  340. this.#overlaySplines.setSpline(this._cutLineIdx, [0, 0, 0, 0, 0, 0]);
  341. });
  342. gui.on(Gui.EVENT_UILOADED, () =>
  343. {
  344. this.snap.update();
  345. // update remote cursor positions
  346. gui.on("netCursorPos", (msg) =>
  347. {
  348. if (!this._glCursors[msg.clientId]) this._glCursors[msg.clientId] = new GlCursor(this, this._overLayRects, msg.clientId);
  349. if (msg.hasOwnProperty("subpatch"))
  350. this._glCursors[msg.clientId].setSubpatch(msg.subpatch);
  351. this._glCursors[msg.clientId].setPosition(msg.x, msg.y);
  352. });
  353. gui.on("netSelectionArea", (msg) =>
  354. {
  355. if (!this._glSelectionAreas[msg.clientId]) this._glSelectionAreas[msg.clientId] = new GlSelectionArea(this._overLayRects);
  356. const area = this._glSelectionAreas[msg.clientId];
  357. if (msg.hide)
  358. {
  359. area.hideArea();
  360. }
  361. else
  362. {
  363. if (msg.color) area.setColor([msg.color.r, msg.color.g, msg.color.b, gui.theme.colors_patch.patchSelectionArea[3]]);
  364. area.setPos(msg.x, msg.y, 1000);
  365. area.setSize(msg.sizeX, msg.sizeY);
  366. }
  367. });
  368. // jump to client position, aka. follow mode
  369. gui.on("netGotoPos", (msg) =>
  370. {
  371. if (msg.hasOwnProperty("scrollX") && msg.hasOwnProperty("scrollY"))
  372. {
  373. if (msg.hasOwnProperty("subpatch"))
  374. {
  375. /*
  376. * set subpatch and revert greyout-state to what is appropriate for this
  377. * client in multiplayer session
  378. */
  379. if (msg.subpatch !== this.getCurrentSubPatch())
  380. {
  381. const mpGreyOut = gui.patchView.patchRenderer.greyOut;
  382. gui.patchView.setCurrentSubPatch(msg.subpatch, () =>
  383. {
  384. gui.patchView.patchRenderer.greyOut = mpGreyOut;
  385. });
  386. }
  387. }
  388. if (msg.hasOwnProperty("zoom"))
  389. {
  390. if (gui.patchView.patchRenderer.viewBox)
  391. gui.patchView.patchRenderer.viewBox.animateZoom(msg.zoom);
  392. }
  393. if (gui.patchView.patchRenderer.viewBox) gui.patchView.patchRenderer.viewBox.scrollTo(-msg.scrollX, msg.scrollY);
  394. else this.center(msg.scrollX, msg.scrollY);
  395. }
  396. });
  397. // remove client on connection lost
  398. gui.on("netClientRemoved", (msg) =>
  399. {
  400. if (this._glCursors[msg.clientId]) this._glCursors[msg.clientId].visible = false;
  401. });
  402. gui.on("netLeaveSession", (msg) =>
  403. {
  404. if (msg.clients)
  405. {
  406. msg.clients.forEach((/** @type {{ clientId: string | number; }} */ client) =>
  407. {
  408. if (this._glCursors[client.clientId]) this._glCursors[client.clientId].visible = false;
  409. });
  410. }
  411. });
  412. });
  413. gui.on("restrictionChange", (r) =>
  414. {
  415. gui.patchView.patchRenderer.greyOut = r === Gui.RESTRICT_MODE_FOLLOWER;
  416. this._updateGreyout();
  417. });
  418. this.vizLayer = new VizLayer(this);
  419. userSettings.on(UserSettings.EVENT_CHANGE, (key, value) =>
  420. {
  421. this.vizFlowMode = userSettings.get(UserSettings.PREF_GLFLOWMODE);
  422. this.updateVizFlowMode();
  423. this.updateCableWidth();
  424. });
  425. this.snap.update();
  426. this._cablesHoverButtonRect = undefined;
  427. }
  428. get lastMouseX()
  429. {
  430. return this.#lastMouseX;
  431. }
  432. get _lastMouseX()
  433. {
  434. console.warn("_lastmouse should not be used");
  435. logStack();
  436. return this.#lastMouseX;
  437. }
  438. get lastMouseY()
  439. {
  440. return this.#lastMouseY;
  441. }
  442. get _lastMouseY()
  443. {
  444. console.warn("_lastmouse should not be used");
  445. logStack();
  446. return this.#lastMouseY;
  447. }
  448. get textWriter()
  449. { return this.#textWriter; }
  450. get name() { return "glpatch"; }
  451. get time() { return this._time; }
  452. set patchAPI(api) { this._patchAPI = api; }
  453. get patchAPI() { return this._patchAPI; }
  454. get rectDrawer() { return this.#rectInstancer; }
  455. get selectedGlOps() { return this._selectedGlOps; }
  456. get subPatch() { return this._currentSubpatch; }
  457. get isAreaSelecting() { return this.#selectionArea.active; }
  458. get cgl() { return this.#cgl; }
  459. updateVizFlowMode()
  460. {
  461. for (let i in this._glOpz)
  462. this._glOpz[i].updateVizFlowMode();
  463. }
  464. updateCursor()
  465. {
  466. let cur = "auto";
  467. if (this.greyOut)
  468. {
  469. cur = "not-allowed";
  470. }
  471. else
  472. {
  473. if (this.viewBox.cursor)cur = this.viewBox.cursor;
  474. else if (this.#isHoveringResize()) cur = "nwse-resize";
  475. else if (this.#isHoveringPort() || this.#isHoveringLinkButton() || this._hoverOps.length > 0) cur = "pointer";
  476. else if (this._spacePressed) cur = "grabbing";
  477. }
  478. this.#cgl.setCursor(cur);
  479. this._cursor = cur;
  480. }
  481. #isHoveringResize()
  482. {
  483. const rect = this.hoverResizeRect;
  484. if (!rect || rect.disposed || !rect.visible) return false;
  485. return rect.isHovering() || rect.isDragging;
  486. }
  487. #isHoveringPort()
  488. {
  489. return this.hoverPort && !this.hoverPort.disposed && this.hoverPort.hovering;
  490. }
  491. #isHoveringLinkButton()
  492. {
  493. const rect = this._cablesHoverButtonRect;
  494. return rect && !rect.disposed && rect.visible;
  495. }
  496. // setCursor(c)
  497. // {
  498. // this._cursor = c;
  499. // }
  500. _removeDropInRect()
  501. {
  502. this.#dropInOpBorder.visible = false;
  503. }
  504. /**
  505. * @param {MouseEvent} e
  506. */
  507. _onCanvasMouseMove(e)
  508. {
  509. if (this.startLinkButtonDrag)
  510. this.startLinkButtonDrag.startDragging(e);
  511. this._dropInCircleLink = this._dropInCircleRect = null;
  512. if (e.shiftKey) this._pressedShiftKey = true;
  513. else this._pressedShiftKey = false;
  514. if (e.ctrlKey) this._pressedCtrlKey = true;
  515. else this._pressedCtrlKey = false;
  516. this.emitEvent(GlPatch.EVENT_MOUSE_MOVE, e);
  517. if (this._dropInCircleRect)
  518. {
  519. let visible = false;
  520. if (gui.patchView.getSelectedOps().length == 1)
  521. {
  522. for (const i in this.selectedGlOps)
  523. {
  524. if (this.selectedGlOps[i].isHovering() && this.selectedGlOps[i].isDragging)
  525. {
  526. visible = true;
  527. const border = 5;
  528. this.#dropInOpBorder.setSize(this._selectedGlOps[i].w + border * 2, this._selectedGlOps[i].h + border * 2);
  529. this.#dropInOpBorder.setPosition(this._selectedGlOps[i].x - border, this._selectedGlOps[i].y - border);
  530. this.#dropInOpBorder.setColorArray(this._dropInCircleRect.color);
  531. this.#dropInOpBorder.setOpacity(0.35);
  532. }
  533. }
  534. }
  535. else visible = false;
  536. this.#dropInOpBorder.visible = visible;
  537. }
  538. else this.#dropInOpBorder.visible = false;
  539. if (this.debugData)
  540. {
  541. this.debugData._onCanvasMouseMove = this.debugData._onCanvasMouseMove || 0;
  542. this.debugData._onCanvasMouseMove++;
  543. }
  544. this.profileMouseEvents = this.profileMouseEvents || 0;
  545. this.profileMouseEvents++;
  546. }
  547. _cycleDebug()
  548. {
  549. this._debugRenderStyle = this._debugRenderStyle || 0;
  550. this._debugRenderStyle++;
  551. if (this._debugRenderStyle > 3) this._debugRenderStyle = 0;
  552. for (let i in this.#splineDrawers) this.#splineDrawers[i].setDebugRenderer(this._debugRenderStyle);
  553. this.#rectInstancer.setDebugRenderer(this._debugRenderStyle);
  554. this._overLayRects.setDebugRenderer(this._debugRenderStyle);
  555. this.#textWriter.setDebugRenderer(this._debugRenderStyle);
  556. this._textWriterOverlay.setDebugRenderer(this._debugRenderStyle);
  557. /*
  558. * if (this._debugRenderStyle == 3) this.clear = false;
  559. * else this.clear = true;
  560. */
  561. }
  562. /**
  563. * @param {MouseEvent} e
  564. */
  565. #onCanvasDblClick(e)
  566. {
  567. let isOverSubPatchOp = false;
  568. let isOverArea = false;
  569. if (this._hoverOps.length > 0)
  570. {
  571. isOverSubPatchOp = this._hoverOps[0].op && this._hoverOps[0].op.isSubPatchOp();
  572. isOverArea = this._hoverOps[0].op.uiAttribs.hasArea;
  573. }
  574. if (isOverArea)
  575. {
  576. const hoverOp = this._hoverOps[0].op;
  577. const colPort = hoverOp.getPortByName("Collapse");
  578. if (colPort)colPort._onTriggered();
  579. }
  580. else
  581. if (isOverSubPatchOp)
  582. {
  583. const hoverOp = this._hoverOps[0].op;
  584. gui.patchView.setCurrentSubPatch(hoverOp.patchId.get());
  585. gui.patchView.updateSubPatchBreadCrumb(hoverOp.patchId.get());
  586. }
  587. else
  588. {
  589. gui.inputBindings.exec(InputBindings.MOUSE_PATCH_DBL_CLICK);
  590. }
  591. e.preventDefault();
  592. }
  593. /**
  594. * @param {PointerEvent} e
  595. */
  596. _onCanvasMouseLeave(e)
  597. {
  598. if (e.pointerType == "touch") return;
  599. if (this._pauseMouseUntilButtonUp)
  600. {
  601. this._pauseMouseUntilButtonUp = false;
  602. return;
  603. }
  604. if (this.#selectionArea.active)
  605. {
  606. this.#selectionArea.hideArea();
  607. }
  608. hideToolTip();
  609. this._lastButton = 0;
  610. this._mouseLeaveButtons = e.buttons;
  611. this.emitEvent(GlPatch.EVENT_MOUSE_LEAVE, e);
  612. }
  613. /**
  614. * @param {PointerEvent} e
  615. */
  616. _onCanvasMouseEnter(e)
  617. {
  618. if (e.pointerType == "touch") return;
  619. if (this._mouseLeaveButtons != e.buttons && e.pointerType == "mouse")
  620. {
  621. // reentering with mouse down already - basically block all interaction
  622. this._pauseMouseUntilButtonUp = true;
  623. // this._log.log("reenter with different buttons!");
  624. return;
  625. }
  626. this.emitEvent(GlPatch.EVENT_MOUSE_ENTER, e);
  627. if (e.buttons == 0 && this._mouseLeaveButtons != e.buttons)
  628. {
  629. // when left while button down but re-entering button up...
  630. this._lastButton = 0;
  631. this.#onCanvasMouseUp(e);
  632. }
  633. }
  634. _updateGreyout()
  635. {
  636. if (this.greyOut && !this._greyOutRect)
  637. {
  638. this._greyOutRect = this._overLayRects.createRect({ "interactive": false, "name": "greyoutrect" });
  639. this._greyOutRect.setColor(
  640. gui.theme.colors_patch.background[0],
  641. gui.theme.colors_patch.background[1],
  642. gui.theme.colors_patch.background[2],
  643. 0.5);
  644. this._greyOutRect.setSize(20000000, 20000000);
  645. this._greyOutRect.setPosition(-10000000, -10000000, gluiconfig.zPosGreyOutRect);
  646. }
  647. else if (!this.greyOut && this._greyOutRect)
  648. {
  649. this._greyOutRect.dispose();
  650. this._greyOutRect = null;
  651. }
  652. /*
  653. * if (this.greyOutBlue && this._greyOutRect)
  654. * {
  655. * this._greyOutRect.setColor(
  656. * gui.theme.colors_patch.background[0] * 0.8,
  657. * gui.theme.colors_patch.background[1] * 1.5,
  658. * gui.theme.colors_patch.background[2] * 2.5,
  659. * 0.25);
  660. * }
  661. */
  662. }
  663. /**
  664. * @param {PointerEvent} e
  665. */
  666. _onCanvasMouseDown(e)
  667. {
  668. if (!e.pointerType) return;
  669. this._removeDropInRect();
  670. // this._onCanvasMouseMove(e);
  671. this.viewBox._onCanvasMouseMove(e);
  672. if (this.mouseState.buttonLeft && !this.isMouseOverOp() && gui.longPressConnector.isActive()) gui.longPressConnector.longPressCancel();
  673. try { this.#cgl.canvas.setPointerCapture(e.pointerId); }
  674. catch (er) { this._log.log(er); }
  675. this.emitEvent(GlPatch.EVENT_MOUSE_DOWN, e);
  676. this.#rectInstancer.mouseDown(e);
  677. this._canvasMouseDown = true;
  678. this._canvasMouseDownSelecting = this.mouseState.buttonStateForSelecting;
  679. }
  680. /**
  681. * @param {PointerEvent} e
  682. */
  683. #onCanvasMouseUp(e)
  684. {
  685. this.linkStartedDragging = false;
  686. this.startLinkButtonDrag = null;
  687. if (e.button == MouseState.BUTTON_RIGHT && !this.isMouseOverOp() && !this.#hoverLink)
  688. gui.inputBindings.exec(InputBindings.MOUSE_PATCH_RIGHT_CLICK);
  689. if (!this.portDragLine.isActive)
  690. {
  691. if (this._pauseMouseUntilButtonUp)
  692. {
  693. this._pauseMouseUntilButtonUp = false;
  694. return;
  695. }
  696. if (!this._canvasMouseDown) return;
  697. }
  698. this._canvasMouseDown = false;
  699. const perf = gui.uiProfiler.start("[glpatch] _onCanvasMouseUp");
  700. this._removeDropInRect();
  701. this.#rectInstancer.mouseUp(e);
  702. try { this.#cgl.canvas.releasePointerCapture(e.pointerId); }
  703. catch (er) { this._log.log(er); }
  704. // gui.longPressConnector.longPressCancel();
  705. this.#rectInstancer.interactive = true;
  706. if (!this.#selectionArea.active && !this.portDragLine.isActive && this._canvasMouseDownSelecting && !this.mouseState.buttonStateForSelecting)
  707. {
  708. if ((gui.patchView.getSelectedOps().length == 0) || (this._hoverOps.length == 0))
  709. {
  710. this.unselectAll();
  711. gui.showInfo(GuiText.patch);
  712. gui.patchView.showDefaultPanel();
  713. }
  714. }
  715. if (this.#selectionArea.active) this.#selectionArea.hideArea();
  716. this.emitEvent(GlPatch.EVENT_MOUSE_UP, e);
  717. if (this._canvasMouseDownSelecting && !this.mouseState.buttonStateForSelecting) this._canvasMouseDownSelecting = false;
  718. if (this._dropInCircleLink)
  719. {
  720. if (gui.patchView.getSelectedOps().length == 1)
  721. {
  722. for (const i in this.selectedGlOps)
  723. {
  724. if (this.selectedGlOps[i].isHovering()) // && this.selectedGlOps[i].isDragging
  725. {
  726. const coord = this.screenToPatchCoord(e.offsetX, e.offsetY);
  727. gui.patchView.insertOpInLink(this._dropInCircleLink.link, this.selectedGlOps[i].op, coord[0], coord[1]);
  728. this._selectedGlOps[i].op.setUiAttribs(
  729. {
  730. "translate":
  731. {
  732. "x": this.snap.snapOpX(this._selectedGlOps[i].op.uiAttribs.translate.x, this._selectedGlOps[i].op, 100),
  733. "y": coord[1]
  734. } });
  735. return;
  736. }
  737. }
  738. }
  739. }
  740. perf.finish();
  741. this._dropInCircleLink = this._dropInCircleRect = null;
  742. this.#selectionArea.mouseUp();
  743. }
  744. /**
  745. * @param {KeyboardEvent} e
  746. */
  747. _onKeyDelete(e)
  748. {
  749. gui.patchView.deleteSelectedOps();
  750. gui.patchView.showDefaultPanel();
  751. if (e.stopPropagation) e.stopPropagation();
  752. if (e.preventDefault) e.preventDefault();
  753. }
  754. isFocused()
  755. {
  756. const isf = document.activeElement == this.#cgl.canvas;
  757. this.wasFocussed = isf;
  758. return isf;
  759. }
  760. isMouseOverOp()
  761. {
  762. return this._hoverOps.length > 0;
  763. }
  764. /**
  765. * @param {number} x
  766. * @param {number} y
  767. */
  768. center(x, y)
  769. {
  770. if (x === undefined) this.viewBox.centerSelectedOps();
  771. else this.viewBox.animateScrollTo(x, y, 0, false);
  772. }
  773. get lineDrawer()
  774. {
  775. return this.#lines;
  776. }
  777. /**
  778. * @param {string } linkId
  779. */
  780. deleteLink(linkId)
  781. {
  782. const l = this.links[linkId];
  783. if (l)
  784. {
  785. delete this.links[linkId];
  786. l.dispose();
  787. }
  788. else
  789. {
  790. this._log.log("could not find link to remove!!", linkId);
  791. }
  792. }
  793. /**
  794. * @param {string} opid
  795. */
  796. deleteOp(opid) // should work th opid...
  797. {
  798. const glop = this._glOpz[opid];
  799. if (!glop)
  800. {
  801. this._log.warn("could not find op to delete", opid);
  802. return;
  803. }
  804. if (this._selectedGlOps[opid]) this.unSelectOpId(opid);
  805. delete this._glOpz[opid];
  806. glop.dispose();
  807. }
  808. toggleOpsEnable()
  809. {
  810. let willDisable = true;
  811. const ops = gui.patchView.getSelectedOps();
  812. for (let i = 0; i < ops.length; i++)
  813. {
  814. if (ops[i].uiAttribs.disabled)willDisable = false;
  815. }
  816. for (let i = 0; i < ops.length; i++)
  817. {
  818. ops[i].setUiAttribs({ "disabled": willDisable });
  819. }
  820. gui.opParams.refresh();
  821. }
  822. /**
  823. * @param {GlLink} l
  824. */
  825. addLink(l)
  826. {
  827. if (this.links[l.id]) this.links[l.id].dispose();
  828. this.links[l.id] = l;
  829. }
  830. /**
  831. * @param {string} opid
  832. */
  833. focusOpAnim(opid)
  834. {
  835. this._focusRectOp = this._glOpz[opid];
  836. this._focusRectAnim.clear();
  837. this._focusRectAnim.setValue(this._time, 0);
  838. this._focusRectAnim.setValue(this._time + 0.5, 1);
  839. }
  840. /**
  841. * @param {string} opid
  842. */
  843. focusOp(opid)
  844. {
  845. gui.opParams.show(opid);
  846. this.focusOpAnim(opid);
  847. }
  848. /**
  849. * @param {string} opid
  850. * @param {import("../components/patchview.js").GotoOpOptions} options
  851. */
  852. gotoOp(opid, options = {})
  853. {
  854. if (!options.hasOwnProperty("showParams"))options.showParams = true;
  855. if (!options.hasOwnProperty("center"))options.center = true;
  856. if (!options.hasOwnProperty("focusAnim"))options.focusAnim = true;
  857. if (!options.hasOwnProperty("unselectAll"))options.unselectAll = true;
  858. if (!options.hasOwnProperty("zoom"))options.zoom = true;
  859. const op = gui.corePatch().getOpById(opid);
  860. if (!op)
  861. {
  862. console.log("unknown goto op");
  863. return;
  864. }
  865. if (op.getSubPatch() != gui.patchView.getCurrentSubPatch())
  866. {
  867. gui.patchView.setCurrentSubPatch(op.getSubPatch(), () =>
  868. {
  869. this.gotoOp(opid, options);
  870. });
  871. return;
  872. }
  873. if (options.unselectAll) gui.patchView.unselectAllOps();
  874. if (options.center)
  875. {
  876. this.setSelectedOpById(opid);
  877. this.viewBox.centerSelectedOps(false, options.zoom);
  878. }
  879. if (options.focusAnim) this.focusOpAnim(opid);
  880. if (options.showParams) gui.opParams.show(opid);
  881. gui.patchView.focus();
  882. }
  883. /**
  884. * @param {boolean} _x
  885. * @param {boolean} _y
  886. */
  887. cursorNavOps(_x, _y)
  888. {
  889. const ops = gui.patchView.getSelectedOps();
  890. if (ops.length == 0) return;
  891. }
  892. /**
  893. * @param {UiOp} op
  894. * @param {boolean} fromDeserialize
  895. */
  896. addOp(op, fromDeserialize = false)
  897. {
  898. if (!op) this._log.error("no op at addop", op);
  899. if (!fromDeserialize && !op.uiAttribs.hasOwnProperty("subPatch")) op.uiAttribs.subPatch = this._currentSubpatch;
  900. let glOp = this._glOpz[op.id];
  901. if (!glOp)
  902. {
  903. glOp = new GlOp(this, this.#rectInstancer, op);
  904. this._glOpz[op.id] = glOp;
  905. }
  906. else
  907. {
  908. glOp.opUiAttribs = op.uiAttribs || {};
  909. }
  910. op.on(Op.EVENT_PORT_REMOVE, () => { glOp.refreshPorts(); });
  911. op.on(Op.EVENT_PORT_ADD, () => { glOp.refreshPorts(); });
  912. op.on("onEnabledChange", () => { glOp.update(); });
  913. op.on(Op.EVENT_UIATTR_CHANGE,
  914. (newAttribs) =>
  915. {
  916. glOp.setUiAttribs(newAttribs, op.uiAttribs);
  917. });
  918. if (!op.uiAttribs.translate && op.uiAttribs.createdLocally)
  919. {
  920. if (CABLES.UI.OPSELECT.newOpPos.y === 0 && CABLES.UI.OPSELECT.newOpPos.x === 0)
  921. op.uiAttr({ "translate": { "x": Snap.snapOpPosX(this.viewBox.mousePatchX), "y": Snap.snapOpPosY(this.viewBox.mousePatchY) } });
  922. else
  923. {
  924. if (!CABLES.UI.OPSELECT.newOpPos.noSnap)
  925. {
  926. CABLES.UI.OPSELECT.newOpPos.x = Snap.snapOpPosX(CABLES.UI.OPSELECT.newOpPos.x);
  927. CABLES.UI.OPSELECT.newOpPos.y = Snap.snapOpPosY(CABLES.UI.OPSELECT.newOpPos.y);
  928. }
  929. op.uiAttr({ "translate": { "x": CABLES.UI.OPSELECT.newOpPos.x, "y": CABLES.UI.OPSELECT.newOpPos.y } });
  930. }
  931. }
  932. glOp.setTitle(op.uiAttribs.title, this.#textWriter);
  933. if (!fromDeserialize)
  934. {
  935. glOp.update();
  936. this.unselectAll();
  937. if (gui.finishedLoading() && op.uiAttribs.subPatch == this.getCurrentSubPatch())
  938. {
  939. this.selectOpId(op.id);
  940. gui.opParams.show(op.id);
  941. }
  942. if (op.uiAttribs.translate && op.uiAttribs.createdLocally)
  943. {
  944. glOp.updatePosition();
  945. }
  946. }
  947. delete op.uiAttribs.createdLocally;
  948. this.portDragLine.stop();
  949. }
  950. /**
  951. * @param {number} x
  952. * @param {number} y
  953. */
  954. screenToPatchCoord(x, y)
  955. {
  956. return this.viewBox.screenToPatchCoord(x, y);
  957. }
  958. _drawCursor()
  959. {
  960. const drawGlCursor = userSettings.get(UserSettings.PREF_GLPATCH_CURSOR);
  961. /*
  962. * if (drawGlCursor) this._cgl.setCursor("none");
  963. * else
  964. * {
  965. * if (this._cursor == CABLES.GLGUI.CURSOR_HAND) this._cgl.setCursor("move");
  966. * else if (this._cursor == CABLES.GLGUI.CURSOR_POINTER) this._cgl.setCursor("pointer");
  967. * else this._cgl.setCursor("auto");
  968. * }
  969. */
  970. this._localGlCursor.visible = drawGlCursor;
  971. const a = this.viewBox.screenToPatchCoord(0, 0);
  972. const b = this.viewBox.screenToPatchCoord(20, 20);
  973. const z = (b[0] - a[0]);
  974. if (drawGlCursor)
  975. {
  976. this._localGlCursor.setSize(z, z);
  977. this._localGlCursor.setPosition(this.viewBox.mousePatchX, this.viewBox.mousePatchY);
  978. }
  979. for (const i in this._glCursors)
  980. {
  981. if (this._glCursors[i].visible) this._glCursors[i].setSize(z, z);
  982. }
  983. }
  984. updateTime()
  985. {
  986. this._time = (performance.now() - this._timeStart) / 1000;
  987. }
  988. /**
  989. * @param {number} resX
  990. * @param {number} resY
  991. */
  992. render(resX, resY)
  993. {
  994. if (!gui || !gui.canvasManager) return;
  995. if (!this.#selectionUndoTimeout)
  996. {
  997. this.#selectionUndoIndex = undo.getIndex();
  998. this.#selectionUndoLength = undo.getCommands().length;
  999. }
  1000. if (gui.canvasManager.mode == gui.canvasManager.CANVASMODE_PATCHBG)
  1001. {
  1002. this.#cgl.gl.clearColor(0, 0, 0, 0);
  1003. }
  1004. else
  1005. {
  1006. this.#cgl.gl.clearColor(
  1007. gui.theme.colors_patch.background[0],
  1008. gui.theme.colors_patch.background[1],
  1009. gui.theme.colors_patch.background[2],
  1010. gui.theme.colors_patch.background[3]);
  1011. }
  1012. this.updateSubPatchOpAnim();
  1013. this.#cgl.gl.clear(this.#cgl.gl.COLOR_BUFFER_BIT | this.#cgl.gl.DEPTH_BUFFER_BIT);
  1014. if (Object.keys(this._glOpz).length != gui.corePatch().ops.length)
  1015. {
  1016. for (let j = 0; j < gui.corePatch().ops.length; j++)
  1017. {
  1018. if (!this._glOpz[gui.corePatch().ops[j].id] && this._ignoreNonExistError.indexOf(gui.corePatch().ops[j].id) == -1)
  1019. {
  1020. this._ignoreNonExistError.push(gui.corePatch().ops[j].id);
  1021. this._log.warn("missing glop in glpatch: ", gui.corePatch().ops[j].name);
  1022. }
  1023. }
  1024. }
  1025. this.hasFocus = ele.hasFocus(this.#cgl.canvas);
  1026. if (this.debugData) this.debugData.splineUpdate = 0;
  1027. for (const i in this._glOpz)
  1028. {
  1029. this._glOpz[i].updateIfNeeded();
  1030. }
  1031. this.frameCount++;
  1032. this.isAnimated = false;
  1033. for (const i in this._glCursors)
  1034. this._glCursors[i].updateAnim();
  1035. this.snap.render(this._canvasMouseDown);
  1036. this._fadeOutRect.visible = !this._fadeOutRectAnim.isFinished(this._time);
  1037. if (this.suggestionTeaser) this.suggestionTeaser.setPos(this.viewBox.mouseX + 10, this.viewBox.mouseY + 30);
  1038. /*
  1039. * if (this._fadeOutRect.visible)
  1040. * {
  1041. * this.isAnimated = true;
  1042. * const v = this._fadeOutRectAnim.getValue(this._time);
  1043. */
  1044. // this.subPatchAnim = 1.0 - this._fadeOutRectAnim.getValue(this._time);
  1045. /*
  1046. * this._fadeOutRect.setColor(
  1047. * gui.theme.colors_patch.background[0],
  1048. * gui.theme.colors_patch.background[1],
  1049. * gui.theme.colors_patch.background[2],
  1050. * v);
  1051. * }
  1052. */
  1053. this._focusRect.visible = !this._focusRectAnim.isFinished(this._time);
  1054. if (this._focusRect.visible)
  1055. {
  1056. this.isAnimated = true;
  1057. const v = 1.0 - this._focusRectAnim.getValue(this._time);
  1058. const dist = 20;
  1059. if (this._focusRectOp)
  1060. {
  1061. this._focusRect.setPosition(this._focusRectOp.x - v * dist, this._focusRectOp.y - v * dist);
  1062. this._focusRect.setSize(this._focusRectOp.w + v * 2 * dist, this._focusRectOp.h + v * 2 * dist);
  1063. this._focusRect.setColor(1, 1, 1, v);
  1064. }
  1065. else
  1066. {
  1067. this._log.log("no focusrectop");
  1068. }
  1069. }
  1070. this.#cgl.pushDepthTest(true);
  1071. this.#cgl.pushDepthWrite(true);
  1072. // this._showRedrawFlash++;
  1073. // this._redrawFlash.setPosition(0, this._showRedrawFlash % 30, 1000);
  1074. this.viewBox.update();
  1075. this._patchAPI.updateFlowModeActivity(this.vizFlowMode);
  1076. this.viewBox.setSize(resX, resY);
  1077. const starttime = performance.now();
  1078. this.mouseMove(this.viewBox.mousePatchX, this.viewBox.mousePatchY);
  1079. this._drawCursor();
  1080. this.portDragLine.setPosition(this.viewBox.mousePatchX, this.viewBox.mousePatchY);
  1081. const perf = gui.uiProfiler.start("[glpatch] render");
  1082. // this._splineDrawer.render(resX, resY, this.viewBox.scrollXZoom, this.viewBox.scrollYZoom, this.viewBox.zoom, this.viewBox.mouseX, this.viewBox.mouseY);
  1083. this.getSplineDrawer(this._currentSubpatch).render(resX, resY, this.viewBox.scrollXZoom, this.viewBox.scrollYZoom, this.viewBox.zoom, this.viewBox.mouseX, this.viewBox.mouseY);
  1084. this.#rectInstancer.render(resX, resY, this.viewBox.scrollXZoom, this.viewBox.scrollYZoom, this.viewBox.zoom);
  1085. this.#textWriter.render(resX, resY, this.viewBox.scrollXZoom, this.viewBox.scrollYZoom, this.viewBox.zoom);
  1086. this.#overlaySplines.render(resX, resY, this.viewBox.scrollXZoom, this.viewBox.scrollYZoom, this.viewBox.zoom);
  1087. this.#cgl.popDepthTest();
  1088. this.#cgl.pushDepthTest(false);
  1089. this._overLayRects.render(resX, resY, this.viewBox.scrollXZoom, this.viewBox.scrollYZoom, this.viewBox.zoom);
  1090. this.#cgl.popDepthTest();
  1091. this.#cgl.pushDepthTest(true);
  1092. this._textWriterOverlay.render(resX, resY, -0.98, 0.94, 600);
  1093. this.#cgl.pushDepthTest(false);
  1094. gui.longPressConnector.glRender(this, this.#cgl, resX, resY, this.viewBox.scrollXZoom, this.viewBox.scrollYZoom, this.viewBox.zoom, this.viewBox.mouseX, this.viewBox.mouseY);
  1095. this.#cgl.popDepthTest();
  1096. if (this._showingOpCursor)
  1097. {
  1098. this._eleDropOp.style.top = this.viewBox.mouseY - 12 + "px";
  1099. this._eleDropOp.style.left = this.viewBox.mouseX - 37 + "px";
  1100. }
  1101. this.needsRedraw = false;
  1102. if (this.debugData)
  1103. {
  1104. if (performance.now() - this._fpsStartTime > 1000)
  1105. {
  1106. this._fpsStartTime = performance.now();
  1107. this.debugData.mouseMovePS = this.profileMouseEvents;
  1108. this.profileMouseEvents = 0;
  1109. }
  1110. this.debugData["glpatch.allowDragging"] = this.allowDragging;
  1111. this.debugData.rects = this.#rectInstancer.getNumRects();
  1112. this.debugData["text rects"] = this.#textWriter.rectDrawer.getNumRects();
  1113. this.debugData.viewZoom = this.viewBox.zoom;
  1114. this.debugData.viewScroll = this.viewBox.scrollX + "," + this.viewBox.scrollY;
  1115. this.debugData._mousePatchX = Math.round(this.viewBox.mousePatchX * 100) / 100;
  1116. this.debugData._mousePatchY = Math.round(this.viewBox.mousePatchY * 100) / 100;
  1117. this.debugData.mouse_isDragging = this.mouseState.isDragging;
  1118. this.debugData.rectInstancer = JSON.stringify(this.#rectInstancer.getDebug(), null, 2);
  1119. this.debugData.renderMs = Math.round((performance.now() - starttime) * 10) / 10;
  1120. }
  1121. if (this.#cgl.profileData && this.debugData)
  1122. {
  1123. this.debugData.glPrimitives = this.#cgl.profileData.profileMeshNumElements;
  1124. this.debugData.glUpdateAttribs = this.#cgl.profileData.profileMeshAttributes;
  1125. for (let i in this.#cgl.profileData.profileSingleMeshAttribute)
  1126. this.debugData["glUpdateAttribs " + i] = this.#cgl.profileData.profileSingleMeshAttribute[i];
  1127. this.#cgl.profileData.clear();
  1128. }
  1129. this.#cgl.popDepthTest();
  1130. this.#cgl.popDepthWrite();
  1131. this._updateGreyout();
  1132. perf.finish();
  1133. this.updateCursor();
  1134. this.#cgl.profileData.clearGlQuery();
  1135. }
  1136. /**
  1137. * @param {number} x
  1138. * @param {number} y
  1139. * @param {MouseEvent} [e]
  1140. */
  1141. mouseMove(x, y, e)
  1142. {
  1143. if (this.cutLineActive)
  1144. {
  1145. if (
  1146. this.viewBox.mousePatchX == this._cutLine[this._cutLine.length - 3] &&
  1147. this.viewBox.mousePatchY == this._cutLine[this._cutLine.length - 2]) return;
  1148. for (let i in this.links)
  1149. {
  1150. if (this.links[i].collideLine(
  1151. this._cutLine[this._cutLine.length - 3], this._cutLine[this._cutLine.length - 2], this.viewBox.mousePatchX, this.viewBox.mousePatchY))
  1152. {
  1153. this.links[i].unlink();
  1154. }
  1155. }
  1156. this._cutLine.push(this.viewBox.mousePatchX, this.viewBox.mousePatchY, 0);
  1157. if (!this._cutLineIdx)
  1158. this._cutLineIdx = this.#overlaySplines.getSplineIndex();
  1159. this.#overlaySplines.setSpline(this._cutLineIdx, [...this._cutLine]); // copy dat array
  1160. this.#overlaySplines.setSplineColor(this._cutLineIdx, [1, 0, 0, 1]);
  1161. return;
  1162. }
  1163. if (this._oldMouseMoveX == x && this._oldMouseMoveY == y) return;
  1164. this._oldMouseMoveX = x;
  1165. this._oldMouseMoveY = y;
  1166. if (!this.portDragLine.isActive)
  1167. if (this._pauseMouseUntilButtonUp) return;
  1168. let allowSelectionArea = !this.portDragLine.isActive;
  1169. if (this.#selectionArea.active)allowSelectionArea = true;
  1170. this.#rectInstancer.mouseMove(x, y, this.mouseState.getButton(), e);
  1171. if (this.#rectInstancer.isDragging()) return;
  1172. /*
  1173. * if (!this.mouseState.isDragging)
  1174. * else this._hoverOps = [];
  1175. */
  1176. let preId = -1;
  1177. if (this._hoverOps.length) preId = this._hoverOps[0].id;
  1178. this._hoverOps = this._getGlOpsInRect(x, y, x + 1, y + 1);
  1179. if (this._hoverOps.length && this._hoverOps[0].id != preId) this._hoverOpLongStartTime = performance.now();
  1180. if (!this._hoverOps.length) this._hoverOpLongStartTime = 0;
  1181. if (this.mouseState.isButtonDown())
  1182. {
  1183. }
  1184. else
  1185. {
  1186. this._hoverDragOp = null;
  1187. }
  1188. if (this._cablesHoverButtonRect && this._cablesHoverButtonRect.isHovering()) allowSelectionArea = false;
  1189. if (this.#selectionArea.h == 0 && this._hoverOps.length > 0) allowSelectionArea = false;
  1190. if (this._lastButton == 1 && this.mouseState.buttonLeft) this.#selectionArea.hideArea();
  1191. if (gui.longPressConnector.isActive())
  1192. {
  1193. const ops = this._getGlOpsInRect(x, y, x + 1, y + 1);
  1194. if (ops.length > 0 && this._focusRectAnim.isFinished(this._time) && gui.longPressConnector.getStartOp().id != ops[0].id) this.focusOpAnim(ops[0].id);
  1195. }
  1196. if (this.mouseState.buttonStateForSelecting && allowSelectionArea && this.mouseState.isDragging && this.mouseState.mouseOverCanvas)
  1197. {
  1198. if (this.#rectInstancer.interactive)
  1199. if (this._pressedShiftKey || this._pressedCtrlKey) this.#selectionArea.previousOps = gui.patchView.getSelectedOps();
  1200. this.#rectInstancer.interactive = false;
  1201. this.#selectionArea.setPos(this.#lastMouseX, this.#lastMouseY);
  1202. this.#selectionArea.setSize((x - this.#lastMouseX), (y - this.#lastMouseY));
  1203. this._selectOpsInRect(x, y, this.#lastMouseX, this.#lastMouseY);
  1204. if (this._pressedShiftKey)
  1205. {
  1206. for (let i = 0; i < this.#selectionArea.previousOps.length; i++)
  1207. {
  1208. this.#selectionArea.previousOps[i].selected = true;
  1209. this.selectOpId(this.#selectionArea.previousOps[i].id);
  1210. }
  1211. }
  1212. if (this._pressedCtrlKey)
  1213. {
  1214. let unselIds = [];
  1215. for (let i in this._selectedGlOps)
  1216. unselIds.push(this._selectedGlOps[i].id);
  1217. for (let i = 0; i < this.#selectionArea.previousOps.length; i++)
  1218. {
  1219. this.#selectionArea.previousOps[i].selected = true;
  1220. this.selectOpId(this.#selectionArea.previousOps[i].id);
  1221. }
  1222. for (let i = 0; i < unselIds.length; i++)
  1223. this.unSelectOpId(unselIds[i]);
  1224. }
  1225. // gui.emitEvent("drawSelectionArea", this.#lastMouseX, this.#lastMouseY, (x - this.#lastMouseX), (y - this.#lastMouseY));
  1226. gui.patchView.showSelectedOpsPanel();
  1227. this._updateNumberOfSelectedOps();
  1228. }
  1229. else
  1230. {
  1231. this.#numSelectedGlOps = -1;
  1232. if (this.#selectionArea.isVisible())
  1233. {
  1234. this.#selectionArea.previousOps = [];
  1235. this.#selectionArea.hideArea();
  1236. }
  1237. this.#lastMouseX = x;
  1238. this.#lastMouseY = y;
  1239. }
  1240. this._lastButton = this.mouseState.getButton();
  1241. }
  1242. _updateNumberOfSelectedOps()
  1243. {
  1244. if (this.#numSelOpsTimeout) clearTimeout(this.#numSelOpsTimeout);
  1245. this.#numSelOpsTimeout = setTimeout(() =>
  1246. {
  1247. this.#numSelOpsTimeout = null;
  1248. const numSelectedOps = Object.keys(this._selectedGlOps).length;
  1249. const changedNumOps = this.#numSelectedGlOps != numSelectedOps;
  1250. this.#numSelectedGlOps = numSelectedOps;
  1251. if (changedNumOps) this.emitEvent(GlPatch.EVENT_SELECTED_OPS_CHANGED, numSelectedOps);
  1252. }, 20);
  1253. }
  1254. /**
  1255. * @param {number} xa
  1256. * @param {number} ya
  1257. * @param {number} xb
  1258. * @param {number} yb
  1259. */
  1260. _getGlOpsInRect(xa, ya, xb, yb)
  1261. {
  1262. if (this.cacheOIRxa == xa && this.cacheOIRya == ya && this.cacheOIRxb == xb && this.cacheOIRyb == yb)
  1263. return this.cacheOIRops;
  1264. const perf = gui.uiProfiler.start("[glpatch] ops in rect");
  1265. const x = Math.min(xa, xb);
  1266. const y = Math.min(ya, yb);
  1267. const x2 = Math.max(xa, xb);
  1268. const y2 = Math.max(ya, yb);
  1269. const ops = [];
  1270. const cops = gui.corePatch().getSubPatchOps();
  1271. for (let j = 0; j < cops.length; j++)
  1272. {
  1273. // for (const i in this._glOpz)
  1274. if (cops[j])
  1275. {
  1276. const glop = this._glOpz[cops[j].id];
  1277. if (!glop || !glop.visible) continue;
  1278. if (glop.x + glop.w >= x && // glop. right edge past r2 left
  1279. glop.x <= x2 && // glop. left edge past r2 right
  1280. glop.y + glop.h >= y && // glop. top edge past r2 bottom
  1281. glop.y <= y2) // r1 bottom edge past r2 top
  1282. {
  1283. ops.push(glop);
  1284. }
  1285. }
  1286. // else console.log("no c op");
  1287. }
  1288. perf.finish();
  1289. this.cacheOIRxa = xa;
  1290. this.cacheOIRya = ya;
  1291. this.cacheOIRxb = xb;
  1292. this.cacheOIRyb = yb;
  1293. this.cacheOIRops = ops;
  1294. return ops;
  1295. }
  1296. unselectAll()
  1297. {
  1298. const perf = gui.uiProfiler.start("[glpatch] unselectAll");
  1299. for (const i in this._glOpz) if (this._glOpz[i].selected) this._glOpz[i].selected = false;
  1300. this._selectedGlOps = {};
  1301. this.#cachedNumSelectedOps = 0;
  1302. this._cachedFirstSelectedOp = null;
  1303. this._updateNumberOfSelectedOps();
  1304. perf.finish();
  1305. }
  1306. /**
  1307. * @param {UiOp|Op} op
  1308. */
  1309. getGlOp(op)
  1310. {
  1311. return this._glOpz[op.id];
  1312. }
  1313. /**
  1314. * @param {String} id
  1315. */
  1316. setSelectedOpById(id)
  1317. {
  1318. this.unselectAll();
  1319. if (this._glOpz[id] && !this._glOpz[id].isInCurrentSubPatch()) this.setCurrentSubPatch(this._glOpz[id].getSubPatch());
  1320. this.selectOpId(id);
  1321. }
  1322. getNumSelectedOps()
  1323. {
  1324. return this.#cachedNumSelectedOps;
  1325. }
  1326. getOnlySelectedOp()
  1327. {
  1328. if (this.#cachedNumSelectedOps == 1 && this._cachedFirstSelectedOp) return this._cachedFirstSelectedOp.op;
  1329. }
  1330. isDraggingOps()
  1331. {
  1332. if (this._cachedFirstSelectedOp) return this._cachedFirstSelectedOp.isDragging;
  1333. else return false;
  1334. }
  1335. /**
  1336. * @param {String} id
  1337. */
  1338. selectOpId(id)
  1339. {
  1340. if (this._glOpz[id] && !this._selectedGlOps[id])
  1341. {
  1342. this._selectedGlOps[id] = this._glOpz[id];
  1343. this.#cachedNumSelectedOps++;
  1344. if (this.#cachedNumSelectedOps == 1) this._cachedFirstSelectedOp = this._glOpz[id];
  1345. this._glOpz[id].selected = true;
  1346. gui.corePatch().getOpById(id).setUiAttrib({ "selected": true });
  1347. }
  1348. if (performance.now() - lastUpdate > 30 && gui.patchView.getSelectedOps().length > 1)
  1349. {
  1350. lastUpdate = performance.now();
  1351. idleSoon = idleCallbackSoon(idleSoon, () =>
  1352. {
  1353. gui.patchView.showSelectedOpsPanel();
  1354. });
  1355. }
  1356. }
  1357. /**
  1358. * @param {String} id
  1359. */
  1360. unSelectOpId(id)
  1361. {
  1362. if (this._glOpz[id])
  1363. {
  1364. delete this._selectedGlOps[id];
  1365. this._glOpz[id].selected = false;
  1366. this.#cachedNumSelectedOps--;
  1367. }
  1368. }
  1369. selectionChanged()
  1370. {
  1371. clearTimeout(this.#selectionUndoTimeout);
  1372. this.#selectionUndoTimeout = setTimeout(() => { this.#recordSelectionUndo(); }, SELECTION_UNDO_DELAY_MS);
  1373. }
  1374. #recordSelectionUndo()
  1375. {
  1376. this.#selectionUndoTimeout = null;
  1377. const before = this.#selectionUndoIds;
  1378. const after = gui.patchView.getSelectedOps().map((op) => op.id).sort();
  1379. const changedByOtherAction = undo.getIndex() != this.#selectionUndoIndex || undo.getCommands().length != this.#selectionUndoLength;
  1380. this.#selectionUndoIds = after;
  1381. if (changedByOtherAction || before.join() == after.join()) return;
  1382. undo.add({
  1383. "title": "Select ops",
  1384. "undo": () => { this.#restoreSelection(before); },
  1385. "redo": () => { this.#restoreSelection(after); }
  1386. });
  1387. }
  1388. /**
  1389. * @param {string[]} ids
  1390. */
  1391. #restoreSelection(ids)
  1392. {
  1393. gui.patchView.unselectAllOps();
  1394. for (const id of ids) gui.patchView.selectOpId(id);
  1395. this.#selectionUndoIds = ids;
  1396. }
  1397. updateSubPatchOpAnim()
  1398. {
  1399. if (this._subpatchoprect)
  1400. {
  1401. this._subpatchoprect.setPosition(this._subpatchAnimOutX.getValue(this._time), this._subpatchAnimOutY.getValue(this._time), gluiconfig.zPosGlRectSelected);
  1402. this._subpatchoprect.setSize(this._subpatchAnimOutW.getValue(this._time), this._subpatchAnimOutH.getValue(this._time));
  1403. this._subpatchoprect.setOpacity(this._subpatchAnimFade.getValue(this._time));
  1404. }
  1405. }
  1406. /**
  1407. * @param {BoundingBox} bounds
  1408. * @param {Function} next
  1409. */
  1410. subPatchOpAnimStart(bounds, next)
  1411. {
  1412. this._subpatchAnimFade = new Anim({ "defaultEasing": Anim.EASING_CUBIC_OUT });
  1413. this._subpatchAnimOutX = new Anim({ "defaultEasing": Anim.EASING_CUBIC_OUT });
  1414. this._subpatchAnimOutY = new Anim({ "defaultEasing": Anim.EASING_CUBIC_OUT });
  1415. this._subpatchAnimOutW = new Anim({ "defaultEasing": Anim.EASING_CUBIC_OUT });
  1416. this._subpatchAnimOutH = new Anim({ "defaultEasing": Anim.EASING_CUBIC_OUT });
  1417. /*
  1418. * this._subpatchAnimFade.clear();
  1419. * this._subpatchAnimFade.setValue(this._time, 0.0);
  1420. * this._subpatchAnimFade.setValue(this._time + 0.25, 1.0);
  1421. */
  1422. if (this._subpatchoprect) this._subpatchoprect.dispose();
  1423. this._subpatchoprect = this._overLayRects.createRect({ "name": "subpatchoprect", "interactive": false });
  1424. let col = gui.theme.colors_patch.opBgRect;
  1425. this._subpatchoprect.setColorArray(col);
  1426. let dur = 0.4;
  1427. this._subpatchAnimFade.setValue(this._time, 0.0);
  1428. this._subpatchAnimFade.setValue(this._time + dur, 0.4, () =>
  1429. {
  1430. if (next)
  1431. {
  1432. this.paused = true;
  1433. next();
  1434. }
  1435. });
  1436. this._subpatchoprect.setBorder(gluiconfig.subPatchOpBorder);
  1437. this._subpatchAnimOutX.setValue(this._time, bounds.minX - gui.theme.patch.selectedOpBorderX / 2);
  1438. this._subpatchAnimOutY.setValue(this._time, bounds.minY - gui.theme.patch.selectedOpBorderY / 2);
  1439. this._subpatchAnimOutW.setValue(this._time, bounds.size[0] + gui.theme.patch.selectedOpBorderX);
  1440. this._subpatchAnimOutH.setValue(this._time, bounds.size[1] + gui.theme.patch.selectedOpBorderY);
  1441. this.updateSubPatchOpAnim();
  1442. }
  1443. /**
  1444. * @param {string | number} opid
  1445. */
  1446. subPatchOpAnimEnd(opid)
  1447. {
  1448. const glop = this._glOpz[opid];
  1449. this.#subPatchOpAnimEndRect(glop.op.uiAttribs.translate.x, glop.op.uiAttribs.translate.y, glop.w, glop.h);
  1450. }
  1451. /**
  1452. * @param {BoundingBox} bounds
  1453. */
  1454. subPatchOpAnimEndBounds(bounds)
  1455. {
  1456. this.#subPatchOpAnimEndRect(
  1457. bounds.minX - gui.theme.patch.selectedOpBorderX / 2,
  1458. bounds.minY - gui.theme.patch.selectedOpBorderY / 2,
  1459. bounds.size[0] + gui.theme.patch.selectedOpBorderX,
  1460. bounds.size[1] + gui.theme.patch.selectedOpBorderY);
  1461. }
  1462. /**
  1463. * @param {number} x
  1464. * @param {number} y
  1465. * @param {number} w
  1466. * @param {number} h
  1467. */
  1468. #subPatchOpAnimEndRect(x, y, w, h)
  1469. {
  1470. // clearTimeout(this.pauseTimeOut);
  1471. this.paused = false;
  1472. const dur = 0.25;
  1473. this._subpatchAnimOutX.clear();
  1474. this._subpatchAnimOutX.setValue(this._time, this._subpatchoprect.x);
  1475. this._subpatchAnimOutX.setValue(this._time + dur, x);
  1476. this._subpatchAnimOutY.clear();
  1477. this._subpatchAnimOutY.setValue(this._time, this._subpatchoprect.y);
  1478. this._subpatchAnimOutY.setValue(this._time + dur, y);
  1479. this._subpatchAnimOutW.clear();
  1480. this._subpatchAnimOutW.setValue(this._time, this._subpatchoprect.w);
  1481. this._subpatchAnimOutW.setValue(this._time + dur, w);
  1482. this._subpatchAnimOutH.clear();
  1483. this._subpatchAnimOutH.setValue(this._time, this._subpatchoprect.h);
  1484. this._subpatchAnimOutH.setValue(this._time + dur, h);
  1485. this._subpatchAnimFade.clear();
  1486. this._subpatchAnimFade.setValue(this._time, 1);
  1487. this._subpatchAnimFade.setValue(this._time + dur / 2, 1.0);
  1488. this._subpatchAnimFade.setValue(this._time + dur, 0.0, () =>
  1489. {
  1490. this._subpatchoprect.dispose();
  1491. this._subpatchoprect = null;
  1492. });
  1493. }
  1494. /**
  1495. * @param {number} xa
  1496. * @param {number} ya
  1497. * @param {number} xb
  1498. * @param {number} yb
  1499. */
  1500. _selectOpsInRect(xa, ya, xb, yb)
  1501. {
  1502. const ops = this._getGlOpsInRect(xa, ya, xb, yb);
  1503. const perf = gui.uiProfiler.start("[glpatch] _selectOpsInRect");
  1504. const opIds = [];
  1505. for (let i = 0; i < ops.length; i++)
  1506. opIds.push(ops[i].id);
  1507. for (let i in this._selectedGlOps)
  1508. {
  1509. if (opIds.indexOf(i) == -1)
  1510. {
  1511. this._selectedGlOps[i].selected = false;
  1512. delete this._selectedGlOps[i];
  1513. }
  1514. }
  1515. this.#cachedNumSelectedOps = Object.keys(this._selectedGlOps).length;
  1516. for (let i = 0; i < ops.length; i++)
  1517. {
  1518. ops[i].selected = true;
  1519. this.selectOpId(ops[i].id);
  1520. }
  1521. perf.finish();
  1522. }
  1523. getZoomForAllOps()
  1524. {
  1525. // this._log.log(this.getOpBounds());
  1526. return 1200;
  1527. }
  1528. /**
  1529. * @param {Op[]} ops
  1530. */
  1531. getOpBounds(ops)
  1532. {
  1533. return gui.patchView.getOpBounds(ops);
  1534. }
  1535. dispose()
  1536. {
  1537. for (const i in this._glOpz)
  1538. {
  1539. this._glOpz[i].dispose();
  1540. delete this._glOpz[i];
  1541. }
  1542. for (let i in this.links)
  1543. {
  1544. this.links[i].dispose();
  1545. }
  1546. this.links = {};
  1547. this._glOpz = {};
  1548. if (this.#rectInstancer) this.#rectInstancer.dispose();
  1549. if (this.#lines) this.#lines.dispose();
  1550. }
  1551. reset()
  1552. {
  1553. }
  1554. /**
  1555. * @param {string} opid
  1556. */
  1557. getOp(opid)
  1558. {
  1559. return this._glOpz[opid];
  1560. }
  1561. // setDrawableColorByType(e, t, diff)
  1562. // {
  1563. // if (!e) return;
  1564. // diff = diff || gluiconfig.colorMulInActive;
  1565. // let col = [0, 0, 0, 0];
  1566. // console.log("color types", gui.theme.colors);
  1567. // if (!gui.theme.colors.types) return;
  1568. // if (t == Port.TYPE_NUMBER) if (gui.theme.colors.types.num) col = [gui.theme.colors.types.num[0] * diff, gui.theme.colors.types.num[1] * diff, gui.theme.colors.types.num[2] * diff, 1]; else col = [0.7, 0.7, 0.7, 1];
  1569. // else if (t == Port.TYPE_TRIGGER) if (gui.theme.colors.types.trigger) col = [gui.theme.colors.types.trigger[0] * diff, gui.theme.colors.types.trigger[1] * diff, gui.theme.colors.types.trigger[2] * diff, 1]; else col = [0.7, 0.7, 0.7, 1];
  1570. // else if (t == Port.TYPE_OBJECT) if (gui.theme.colors.types.obj) col = [gui.theme.colors.types.obj[0] * diff, gui.theme.colors.types.obj[1] * diff, gui.theme.colors.types.obj[2] * diff, 1]; else col = [0.7, 0.7, 0.7, 1];
  1571. // else if (t == Port.TYPE_ARRAY) if (gui.theme.colors.types.arr) col = [gui.theme.colors.types.arr[0] * diff, gui.theme.colors.types.arr[1] * diff, gui.theme.colors.types.arr[2] * diff, 1]; else col = [0.7, 0.7, 0.7, 1];
  1572. // else if (t == Port.TYPE_STRING) if (gui.theme.colors.types.str) col = [gui.theme.colors.types.str[0] * diff, gui.theme.colors.types.str[1] * diff, gui.theme.colors.types.str[2] * diff, 1]; else col = [0.7, 0.7, 0.7, 1];
  1573. // else if (t == Port.TYPE_DYNAMIC) if (gui.theme.colors.types.dynamic) col = [gui.theme.colors.types.dynamic[0] * diff, gui.theme.colors.types.dynamic[1] * diff, gui.theme.colors.types.dynamic[2] * diff, 1]; else col = [0.7, 0.7, 0.7, 1];
  1574. // else this._log.warn("unknown port color");
  1575. // e.setColor(col[0], col[1], col[2], col[3]);
  1576. // }
  1577. isDraggingPort()
  1578. {
  1579. return this.portDragLine.isActive;
  1580. }
  1581. get dragLine()
  1582. {
  1583. return this.portDragLine;
  1584. }
  1585. get allowDragging()
  1586. {
  1587. return this.#rectInstancer.allowDragging;
  1588. }
  1589. set allowDragging(b)
  1590. {
  1591. this.#rectInstancer.allowDragging = b;
  1592. }
  1593. /**
  1594. * @param {string} opid
  1595. * @param {string} portname
  1596. */
  1597. getConnectedGlPorts(opid, portname)
  1598. {
  1599. const op = this.getOp(opid);
  1600. return op.getGlPortsLinkedToPort(opid, portname);
  1601. }
  1602. focus()
  1603. {
  1604. this.#cgl.canvas.focus();
  1605. }
  1606. /**
  1607. * @param {ClipboardEvent} e
  1608. */
  1609. cut(e)
  1610. {
  1611. gui.patchView.clipboardCutOps(e);
  1612. }
  1613. /**
  1614. * @param {ClipboardEvent} e
  1615. */
  1616. copy(e)
  1617. {
  1618. /*
  1619. * todo play copy indicator anim
  1620. * for (const i in selectedOps) selectedOps[i].oprect.showCopyAnim();
  1621. */
  1622. gui.patchView.clipboardCopyOps(e);
  1623. }
  1624. /**
  1625. * @param {ClipboardEvent} e
  1626. */
  1627. paste(e)
  1628. {
  1629. gui.patchView.clipboardPaste(e, this._currentSubpatch, this.viewBox.mousePatchX, this.viewBox.mousePatchY, (/** @type {string | any[]} */ ops, /** @type {any} */ _focusSubpatchop) =>
  1630. {
  1631. this.unselectAll();
  1632. for (let i = 0; i < ops.length; i++)
  1633. {
  1634. this.selectOpId(ops[i].id);
  1635. }
  1636. if (ops[ops.length - 1])gui.opParams.show(ops[ops.length - 1].id);
  1637. if (ops.length == 1) this.focusOpAnim(ops[0].id);
  1638. });
  1639. }
  1640. getCurrentSubPatch()
  1641. {
  1642. if (this._currentSubpatch === undefined)
  1643. {
  1644. this._log.warn("current subpatch undefined");
  1645. this.setCurrentSubPatch(Patch.DEFAULT_SUBPATCHID);
  1646. }
  1647. return this._currentSubpatch;
  1648. }
  1649. /**
  1650. * @param {string} sub
  1651. * @param {function} [next]
  1652. */
  1653. setCurrentSubPatch(sub, next)
  1654. {
  1655. if (this._currentSubpatch == sub)
  1656. {
  1657. for (const i in this._glOpz)
  1658. this._glOpz[i].updateVisible();
  1659. if (next)next();
  1660. return;
  1661. }
  1662. if (sub === undefined)
  1663. {
  1664. this._log.warn("current subpatch undefined");
  1665. return;
  1666. // throw new Error("current subpatch undefined");
  1667. }
  1668. this.unselectAll();
  1669. if (sub != "0" && sub != this._currentSubpatch) this._log.log("set subpatch: ", sub);
  1670. this._currentSubpatch = sub;
  1671. /*
  1672. * this._fadeOutRectAnim.clear();
  1673. * this._fadeOutRectAnim.setValue(this._time, 0);
  1674. * this._fadeOutRectAnim.setValue(this._time + timeGrey, 1.0);
  1675. * this._fadeOutRectAnim.setValue(this._time + timeGrey + 2, 1);
  1676. * this._fadeOutRectAnim.setValue(this._time + timeVisibleAgain, 0);
  1677. */
  1678. gui.patchView.updateSubPatchBreadCrumb(sub);
  1679. /*
  1680. * setTimeout(() =>
  1681. * {
  1682. */
  1683. for (const i in this._glOpz)
  1684. {
  1685. this._glOpz[i].updateVisible();
  1686. }
  1687. for (const i in this.links)
  1688. {
  1689. this.links[i].updateVisible();
  1690. }
  1691. this.restoreSubPatchViewBox(sub, next);
  1692. this.snap.update();
  1693. }
  1694. /**
  1695. * @param {string | number} sub
  1696. * @param {Function} [next]
  1697. */
  1698. restoreSubPatchViewBox(sub, next)
  1699. {
  1700. const dur = 0.3;
  1701. const timeGrey = dur * 1.5;
  1702. const timeVisibleAgain = dur * 3.0;
  1703. this.viewBox.animSwitchSubPatch(dur, sub, timeGrey, timeVisibleAgain, next);
  1704. }
  1705. storeSubPatchViewBox()
  1706. {
  1707. this.viewBox.storeCurrentSubPatch();
  1708. }
  1709. /**
  1710. * @param {import("cables-shared-client").SerializedPatchUi} dataUi
  1711. */
  1712. serialize(dataUi)
  1713. {
  1714. this.viewBox.serialize(dataUi);
  1715. }
  1716. /**
  1717. * @param {import("cables-shared-client").SerializedPatch} proj
  1718. */
  1719. setProject(proj)
  1720. {
  1721. this.viewBox.deSerialize(proj.ui);
  1722. }
  1723. get spacePressed()
  1724. {
  1725. return this._spacePressed;
  1726. }
  1727. pause()
  1728. {
  1729. this.#cgl.canvas.style["background-color"] = "rgba(61,61,61,1)";
  1730. this.paused = true;
  1731. this.emitEvent(GlPatch.EVENT_PAUSED);
  1732. }
  1733. resume()
  1734. {
  1735. this.#cgl.canvas.style["background-color"] = "transparent";
  1736. this.paused = false;
  1737. this.emitEvent(GlPatch.EVENT_RESUMED);
  1738. }
  1739. /**
  1740. * @param {number} _x
  1741. * @param {number} _y
  1742. * @param {number} _w
  1743. * @param {number} _h
  1744. */
  1745. setSize(_x, _y, _w, _h)
  1746. {
  1747. }
  1748. /**
  1749. * @param {number} s
  1750. */
  1751. zoomStep(s)
  1752. {
  1753. this.viewBox.zoomStep(s);
  1754. }
  1755. /**
  1756. * @param {boolean} show
  1757. */
  1758. showOpCursor(show)
  1759. {
  1760. if (this._showingOpCursor != show)
  1761. {
  1762. this._showingOpCursor = show;
  1763. if (show) ele.show(this._eleDropOp);
  1764. else ele.hide(this._eleDropOp);
  1765. }
  1766. }
  1767. pauseInteraction()
  1768. {
  1769. this.#cgl.canvas.style["pointer-events"] = "none";
  1770. this.vizLayer.pauseInteraction();
  1771. }
  1772. resumeInteraction()
  1773. {
  1774. this.#cgl.canvas.style["pointer-events"] = "initial";
  1775. this.vizLayer.resumeInteraction();
  1776. }
  1777. /**
  1778. * @param {string|number} subpatchId
  1779. * @returns {GlSplineDrawer} splinedrawer
  1780. */
  1781. getSplineDrawer(subpatchId)
  1782. {
  1783. if (this.#splineDrawers.hasOwnProperty(subpatchId)) return this.#splineDrawers[subpatchId];
  1784. else
  1785. {
  1786. this.#splineDrawers[subpatchId] = new GlSplineDrawer(this.#cgl, "patchCableSplines_" + subpatchId);
  1787. this.#splineDrawers[subpatchId].width = userSettings.get(GlPatch.USERPREF_GLPATCH_CABLE_WIDTH) || gui.theme.patch.cablesWidth;
  1788. return this.#splineDrawers[subpatchId];
  1789. }
  1790. }
  1791. /**
  1792. * @param {MouseEvent} e
  1793. * @param {GlLink} link
  1794. */
  1795. setHoverLink(e, link)
  1796. {
  1797. this.#hoverLink = link;
  1798. if (link && link.link && e)
  1799. {
  1800. clearTimeout(this._ttTImeout);
  1801. updateHoverToolTip(e, link.link.portOut, link);
  1802. }
  1803. if (!link)
  1804. {
  1805. clearTimeout(this._ttTImeout);
  1806. this._ttTImeout = setTimeout(() =>
  1807. {
  1808. hideToolTip();
  1809. }, 100);
  1810. }
  1811. }
  1812. updateTheme()
  1813. {
  1814. this.#selectionArea.updateTheme();
  1815. for (let i in this._glOpz)
  1816. this._glOpz[i].updateTheme();
  1817. this.updateCableWidth();
  1818. }
  1819. updateCableWidth()
  1820. {
  1821. for (const i in this.#splineDrawers)
  1822. {
  1823. this.#splineDrawers[i].width = userSettings.get(GlPatch.USERPREF_GLPATCH_CABLE_WIDTH) || gui.theme.patch.cablesWidth;
  1824. }
  1825. }
  1826. /**
  1827. * @param {string} ns
  1828. */
  1829. static getOpNamespaceColor(ns)
  1830. {
  1831. // make static util thing...
  1832. const parts = ns.split(".");
  1833. const nss = parts[0] + "." + parts[1];
  1834. if (!gui.theme.colors_namespaces) return [1, 1, 1, 1];
  1835. if (gui.theme.colors_namespaces[nss]) return gui.theme.colors_namespaces[nss];
  1836. else return gui.theme.colors_namespaces.unknown || [1, 0, 0, 1];
  1837. }
  1838. get splineDrawers()// todo: delete, was debug
  1839. {
  1840. return this.#splineDrawers;
  1841. }
  1842. }