Home Reference Source

cables_dev/cables_ui/src/ui/gldraw/glspline.js

  1. import GlRect from "./glrect.js";
  2. import { GlSplineDrawer } from "./glsplinedrawer.js";
  3. export default class GlSpline
  4. {
  5. #splineIdx = -1;
  6. /** @type {GlSplineDrawer} */
  7. #splineDrawer = null;
  8. /** @type {Array<number>} */
  9. #points = [0, 0, 0, 10, 10, 0];
  10. /** @type {GlRect} */
  11. #parentRect;
  12. /** @type {String} */
  13. #name = "unknown spline";
  14. #disposed = false;
  15. /**
  16. * @param {GlSplineDrawer} splineDrawer
  17. * @param {string} name
  18. * @param {Object} options
  19. */
  20. constructor(splineDrawer, name, options = {})
  21. {
  22. this.#name = name;
  23. this.#splineDrawer = splineDrawer;
  24. this.#splineIdx = this.#splineDrawer.getSplineIndex(this.#name);
  25. this.#parentRect = null;
  26. splineDrawer.on(GlSplineDrawer.EVENT_CLEARED, () =>
  27. {
  28. this.dispose();
  29. });
  30. }
  31. /**
  32. * @param {GlRect} r
  33. */
  34. setParentRect(r)
  35. {
  36. if (this.checkDisposed()) return;
  37. if (this.#parentRect) this.#parentRect.off(this.rebuild.bind(this));
  38. this.#parentRect = r;
  39. if (this.#parentRect) this.#parentRect.on(GlRect.EVENT_POSITIONCHANGED, this.rebuild.bind(this));
  40. this.rebuild();
  41. }
  42. getDrawer()
  43. {
  44. return this.#splineDrawer;
  45. }
  46. /**
  47. * @param {Array<number>} p
  48. */
  49. setPoints(p)
  50. {
  51. if (this.checkDisposed()) return;
  52. this.#points = p;
  53. this.rebuild();
  54. }
  55. rebuild()
  56. {
  57. if (this.checkDisposed()) return;
  58. const finalPoints = [];
  59. let x = 0, y = 0, z = 0;
  60. if (this.#parentRect)
  61. {
  62. x = this.#parentRect.x;
  63. y = this.#parentRect.y;
  64. z = this.#parentRect.z;
  65. }
  66. for (let i = 0; i < this.#points.length; i += 3)
  67. {
  68. finalPoints[i + 0] = this.#points[i + 0] + x;
  69. finalPoints[i + 1] = this.#points[i + 1] + y;
  70. finalPoints[i + 2] = this.#points[i + 2] + z;
  71. }
  72. this.#splineDrawer.setSpline(this.#splineIdx, finalPoints);
  73. }
  74. /**
  75. * @param {number} r
  76. * @param {number} g
  77. * @param {number} b
  78. * @param {number} a=1
  79. */
  80. setColor(r, g, b, a = 1)
  81. {
  82. if (this.checkDisposed()) return;
  83. this.#splineDrawer.setSplineColor(this.#splineIdx, [r, g, b, a]);
  84. }
  85. /**
  86. * @param {number[]} arr
  87. */
  88. setColorArray(arr)
  89. {
  90. if (this.checkDisposed()) return;
  91. this.#splineDrawer.setSplineColor(this.#splineIdx, arr);
  92. }
  93. checkDisposed()
  94. {
  95. if (this.#disposed)console.log("disposed object...", this);
  96. return this.#disposed;
  97. }
  98. dispose()
  99. {
  100. this.#disposed = true;
  101. this.#splineDrawer.deleteSpline(this.#splineIdx);
  102. this.#splineIdx = -1;
  103. return null;
  104. }
  105. getNumPoints()
  106. {
  107. return this.#points.length / 3;
  108. }
  109. }