Home Reference Source

cables_dev/cables_ui/src/ui/dialogs/canv_curveeditor.js

  1. import { Logger, ele } from "cables-shared-client";
  2. import { utils } from "cables";
  3. import CanvasPointEditor from "./canv_pointeditor.js";
  4. /**
  5. * gradient editor dialog
  6. */
  7. export class CurveEditor extends CanvasPointEditor
  8. {
  9. #log = new Logger("curveeditor");
  10. /**
  11. * @param {any} opid
  12. * @param {any} portname
  13. * @param {import("./canv_pointeditor.js").CanvasPointEditorOptions} options
  14. */
  15. constructor(opid, portname, options)
  16. {
  17. super(opid, portname, options);
  18. this._opId = opid;
  19. this._portName = portname;
  20. this.options.template = "CurveEditor";
  21. this.on("open", () =>
  22. {
  23. });
  24. }
  25. updateOpenerBackground()
  26. {
  27. this.openerEle.style.background = CurveEditor.getCssGradientString(this.keys);
  28. }
  29. /**
  30. * @param {import("./canv_pointeditor.js").KeyObject[]} keys
  31. */
  32. static getCssGradientString(keys)
  33. {
  34. if (!keys) return "";
  35. let str = "linear-gradient(";
  36. str += "90deg";
  37. for (let i = 0; i < keys.length; i++)
  38. {
  39. const r = Math.floor((1 - keys[i].posy) * 255);
  40. str += ",rgba(" + r + ", " + r + ", " + r + ", " + 255 + ") " + Math.floor((keys[i].pos) * 100) + "% ";
  41. }
  42. str += ")";
  43. return str;
  44. }
  45. updateCanvas()
  46. {
  47. if (!this.ctx)
  48. {
  49. const canvas = ele.byId(this.id + "Canvas");
  50. if (!canvas)
  51. {
  52. this.#log.error("[gradienteditor] no canvas found");
  53. return;
  54. }
  55. this.ctx = canvas.getContext("2d");
  56. }
  57. let keys = [];
  58. if (this.keys.length == 0) keys.push({
  59. "posy": 0.5,
  60. "pos": 0,
  61. "r": 0,
  62. "g": 0,
  63. "b": 0,
  64. "a": 1
  65. });
  66. else keys = [{
  67. "posy": this.keys[0].posy,
  68. "pos": 0,
  69. "r": this.keys[0].r,
  70. "g": this.keys[0].g,
  71. "b": this.keys[0].b,
  72. "a": this.keys[0].a
  73. }].concat(this.keys);
  74. const last = keys[keys.length - 1];
  75. keys.push({
  76. "posy": last.posy,
  77. "pos": 1,
  78. "r": last.r,
  79. "g": last.g,
  80. "b": last.b,
  81. "a": last.a,
  82. });
  83. this.ctx.fillStyle = "#444444";
  84. this.ctx.fillRect(0, 0, this.width, this.height);
  85. this.ctx.lineWidth = 2;
  86. this.ctx.strokeStyle = "#999999";
  87. if (this.port.tempData.curveanim)
  88. {
  89. this.ctx.beginPath();
  90. const num = 100;
  91. for (let i = 0; i < num; i++)
  92. {
  93. const y = (1 - this.port.tempData.curveanim.getValue(i / num)) * this.height;
  94. if (i == 0) this.ctx.moveTo((i / num) * this.width, y);
  95. else this.ctx.lineTo((i / num) * this.width, y);
  96. }
  97. this.ctx.stroke();
  98. }
  99. else
  100. {
  101. this.ctx.beginPath();
  102. for (let i = 0; i < keys.length; i++)
  103. {
  104. const key = keys[i];
  105. if (i == 0) this.ctx.moveTo(key.pos * this.width, key.posy * this.height - 1);
  106. else this.ctx.lineTo(key.pos * this.width, key.posy * this.height - 1);
  107. }
  108. this.ctx.stroke();
  109. }
  110. if (this._opId && this._portName)
  111. {
  112. const keyData = [];
  113. for (let i = 0; i < keys.length; i++)
  114. {
  115. keyData[i] =
  116. {
  117. "pos": keys[i].pos,
  118. "posy": keys[i].posy,
  119. "r": keys[i].r,
  120. "g": keys[i].g,
  121. "b": keys[i].b,
  122. "a": keys[i].a
  123. };
  124. }
  125. this.port.set(JSON.stringify({ "keys": keyData }));
  126. }
  127. this.updateOpenerBackground();
  128. }
  129. }