Home Reference Source

cables_dev/cables_ui/src/ui/commands/cmd_renderer.js

  1. import { ele } from "cables-shared-client";
  2. import CanvasLens from "../components/canvas/canvaslens.js";
  3. import ModalDialog from "../dialogs/modaldialog.js";
  4. import Gui, { gui } from "../gui.js";
  5. const CABLES_CMD_RENDERER = {};
  6. const rendererCommands =
  7. {
  8. /** @type {import("./commands.js").commandObject[]} */
  9. "commands": [],
  10. "functions": CABLES_CMD_RENDERER
  11. };
  12. export default rendererCommands;
  13. export { CABLES_CMD_RENDERER as CmdRenderer };
  14. CABLES_CMD_RENDERER.screenshot = function ()
  15. {
  16. gui.canvasManager.currentContext().saveScreenshot();
  17. gui.corePatch().resume();
  18. };
  19. CABLES_CMD_RENDERER.maximizeCanvas = function ()
  20. {
  21. gui.toggleMaximizeCanvas();
  22. };
  23. CABLES_CMD_RENDERER.resetSize = function ()
  24. {
  25. gui.rendererWidth = 640;
  26. gui.rendererHeight = 360;
  27. gui.setLayout();
  28. };
  29. CABLES_CMD_RENDERER.canvasMagnifier = function ()
  30. {
  31. gui.canvasMagnifier = new CanvasLens();
  32. };
  33. CABLES_CMD_RENDERER.scrollingPage = function ()
  34. {
  35. if (ele.byId("testcontent").innerHTML == "")
  36. {
  37. document.body.classList.add("scrollPage");
  38. let str = "";
  39. for (let i = 0; i < 1000; i++)
  40. {
  41. str += "- long page...<br/>";
  42. }
  43. str += "<div style=\"position:fixed;bottom:50px;z-index:99999;border-radius:10px;left:40%;cursor:pointer;background-color:#07F78C;color:#000;padding:20px;\" class=\"button-small\" onclick=\"CABLES.CMD.RENDERER.scrollingPage();\">exit scrollmode<div>";
  44. ele.byId("testcontent").innerHTML = str;
  45. }
  46. else
  47. {
  48. document.body.scrollTo({
  49. "top": 0,
  50. "behaviour": "smooth"
  51. });
  52. document.body.classList.remove("scrollPage");
  53. ele.byId("testcontent").innerHTML = "";
  54. }
  55. };
  56. CABLES_CMD_RENDERER.aspect = function (a = 0)
  57. {
  58. if (!a)
  59. {
  60. new ModalDialog({
  61. "prompt": true,
  62. "title": "Change Aspect Ratio of Renderer",
  63. "text": "Enter an aspect ratio, e.g.: 16:9 or 0.22",
  64. "promptValue": gui.corePatch().cgl.canvasScale,
  65. "promptOk": (r) =>
  66. {
  67. if (r.indexOf(":") >= 0)
  68. {
  69. const parts = r.split(":");
  70. const s = parseInt(parts[0]) / parseInt(parts[1]);
  71. CABLES_CMD_RENDERER.aspect(s);
  72. }
  73. else
  74. {
  75. const s = parseFloat(r);
  76. CABLES_CMD_RENDERER.aspect(s);
  77. }
  78. }
  79. });
  80. return;
  81. }
  82. const nh = gui.rendererWidth * 1 / a;
  83. if (nh < window.innerHeight * 0.6)
  84. {
  85. gui.rendererHeight = nh;
  86. }
  87. else
  88. {
  89. gui.rendererHeight = window.innerHeight * 0.6;
  90. gui.rendererWidth = gui.rendererHeight * a;
  91. }
  92. gui.emitEvent(Gui.EVENT_RESIZE_CANVAS);
  93. gui.setLayout();
  94. gui.canvasManager.getCanvasUiBar().updateCanvasIconBar();
  95. };
  96. CABLES_CMD_RENDERER.scaleCanvas = function ()
  97. {
  98. const p = new ModalDialog({
  99. "prompt": true,
  100. "title": "Change Scale of Renderer",
  101. "text": "Enter a new scale",
  102. "promptValue": gui.corePatch().cgl.canvasScale || 1,
  103. "promptOk": (r) =>
  104. {
  105. const s = parseFloat(r);
  106. gui.corePatch().cgl.canvasScale = s;
  107. gui.setLayout();
  108. }
  109. });
  110. };
  111. CABLES_CMD_RENDERER.changeSize = function ()
  112. {
  113. let str = "Enter a new size:";
  114. if (gui.canvasManager.getCanvasUiBar())
  115. gui.canvasManager.getCanvasUiBar().showCanvasModal(false);
  116. const p = new ModalDialog({
  117. "prompt": true,
  118. "title": "Change Canvas size",
  119. "text": str,
  120. "promptValue": Math.round(gui.rendererWidth) + " x " + Math.round(gui.rendererHeight),
  121. "promptOk": (r) =>
  122. {
  123. const matches = r.match(/\d+/g);
  124. if (matches.length > 0)
  125. {
  126. gui.canvasManager.setSize(matches[0], matches[1]);
  127. if (gui.canvasManager.mode != gui.canvasManager.CANVASMODE_POPOUT)
  128. {
  129. gui.rendererWidth = matches[0];
  130. gui.rendererHeight = matches[1];
  131. }
  132. else
  133. {
  134. gui.canvasManager.subWindow.resizeTo(matches[0], matches[1]);
  135. }
  136. gui.setLayout();
  137. }
  138. }
  139. });
  140. };
  141. CABLES_CMD_RENDERER.popoutCanvas = function ()
  142. {
  143. gui.canvasManager.popOut();
  144. };
  145. rendererCommands.commands.push(
  146. {
  147. "cmd": "save screenshot",
  148. "category": "canvas",
  149. "func": CABLES_CMD_RENDERER.screenshot,
  150. "icon": "image"
  151. },
  152. {
  153. "cmd": "maximize canvas",
  154. "category": "canvas",
  155. "func": CABLES_CMD_RENDERER.maximizeCanvas,
  156. "icon": "canvas_max",
  157. "infotext": "renderer_maximize"
  158. },
  159. {
  160. "cmd": "change canvas size",
  161. "category": "canvas",
  162. "func": CABLES_CMD_RENDERER.changeSize,
  163. "icon": "resize_canvas"
  164. },
  165. {
  166. "cmd": "reset canvas size",
  167. "category": "canvas",
  168. "func": CABLES_CMD_RENDERER.resetSize,
  169. "icon": "reset_render_size"
  170. },
  171. {
  172. "cmd": "set canvas aspect ratio",
  173. "category": "canvas",
  174. "func": CABLES_CMD_RENDERER.aspect,
  175. "icon": "canvas_max"
  176. },
  177. {
  178. "cmd": "scale canvas",
  179. "category": "canvas",
  180. "func": CABLES_CMD_RENDERER.scaleCanvas,
  181. "icon": "scale_canvas"
  182. },
  183. {
  184. "cmd": "canvas magnifier",
  185. "category": "canvas",
  186. "func": CABLES_CMD_RENDERER.canvasMagnifier,
  187. "icon": "picker"
  188. },
  189. {
  190. "cmd": "canvas window",
  191. "category": "canvas",
  192. "func": CABLES_CMD_RENDERER.popoutCanvas,
  193. "icon": "external"
  194. },
  195. {
  196. "cmd": "Simulate Scrolling Page",
  197. "category": "canvas",
  198. "func": CABLES_CMD_RENDERER.scrollingPage
  199. },
  200. {
  201. "cmd": "Maximize renderer",
  202. "category": "ui",
  203. "func": CABLES_CMD_RENDERER.maximizeCanvas,
  204. "icon": "canvas_max",
  205. "hotkey": "CMD + ENTER"
  206. }
  207. );