Home Reference Source

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

  1. import { utils } from "cables";
  2. import { UserSettings, userSettings } from "../components/usersettings.js";
  3. import GlRect from "../gldraw/glrect.js";
  4. import GlRectInstancer from "../gldraw/glrectinstancer.js";
  5. import { gui } from "../gui.js";
  6. import GlOp from "./glop.js";
  7. import { UiOp } from "../core_extend_op.js";
  8. export default class GlArea
  9. {
  10. /** @type {GlRect} */
  11. #rectBg = null;
  12. /** @type {GlRect} */
  13. #rectResize = null;
  14. /** @type {GlOp} */
  15. #glop;
  16. #id = utils.shortId();
  17. #instancer;
  18. _w = 300;
  19. _h = 200;
  20. _visible = true;
  21. /** @type {GlOp} */
  22. #glOpScopeEnd;
  23. /**
  24. * @param {GlRectInstancer} instancer
  25. * @param {GlOp} glop
  26. */
  27. constructor(instancer, glop)
  28. {
  29. this.#instancer = instancer;
  30. this.#glop = glop;
  31. this.#rectBg = this.#instancer.createRect({ "name": "glarea bg", "interactive": false, "draggable": false });
  32. this.#rectBg.setSize(this._w, this._h);
  33. // this.#rectBg.setBorder(1);
  34. this._updateColor();
  35. /**
  36. * @type {Number}
  37. */
  38. this.resizeCornerSize = 15;
  39. /**
  40. * @type {GlRect}
  41. */
  42. this.#rectResize = this.#instancer.createRect({ "name": "glarea resize", "interactive": true, "draggable": true });
  43. this.#rectResize.setShape(GlRect.SHAPE_TRIANGLE_BOTTOM);
  44. this.#rectResize.setSize(this.resizeCornerSize, this.resizeCornerSize);
  45. this.#rectResize.setColor(0, 0, 0, 0.2);
  46. this.#rectResize.setPosition(200 - this.resizeCornerSize, 200 - this.resizeCornerSize);
  47. this.#rectResize.draggable = true;
  48. this.#rectResize.draggableMove = true;
  49. this.#rectResize.on(GlRect.EVENT_POINTER_HOVER, (rect) =>
  50. {
  51. this.#glop.glPatch.hoverResizeRect = rect;
  52. });
  53. this.#rectResize.on(GlRect.EVENT_POINTER_UNHOVER, (rect) =>
  54. {
  55. if (this.#glop.glPatch.hoverResizeRect == rect) this.#glop.glPatch.hoverResizeRect = null;
  56. });
  57. this.#glop.on(GlOp.EVENT_DRAG, () =>
  58. {
  59. this.update();
  60. });
  61. this.#rectResize.on(GlRect.EVENT_DRAG, (_e) =>
  62. {
  63. this._w = this.#rectResize.x - this.#glop.x + this.#rectResize.w / 2;
  64. this._h = this.#rectResize.y - this.#glop.y + this.#rectResize.h / 2;
  65. if (userSettings.get(UserSettings.PREF_SNAPTOGRID))
  66. {
  67. this._w = this.#glop.glPatch.snap.snapX(this._w);
  68. this._h = this.#glop.glPatch.snap.snapY(this._h);
  69. }
  70. gui.savedState.setUnSaved("resizeGlArea", this.#glop.op.getSubPatch());
  71. if (this.#glOpScopeEnd)
  72. {
  73. this.#glOpScopeEnd.op.setPos(this.#rectBg.x, this.#rectResize.y + this.#rectResize.h / 2 - this.#glOpScopeEnd.h);
  74. }
  75. this.update();
  76. });
  77. if (this.#glop.op.uiAttribs.area)
  78. {
  79. if (this.#glop.op.uiAttribs.area.id) this.#id = this.#glop.op.uiAttribs.area.id;
  80. this._w = this.#glop.op.uiAttribs.area.w;
  81. this._h = this.#glop.op.uiAttribs.area.h;
  82. }
  83. this.update();
  84. }
  85. /**
  86. * @param {boolean} v
  87. */
  88. set visible(v)
  89. {
  90. this._visible = v;
  91. this.update();
  92. }
  93. minsize = 20;
  94. update()
  95. {
  96. if (this.#rectBg)
  97. {
  98. if (this.#glop.op.uiAttribs.areaCollapsed && this._h != this.minsize)
  99. {
  100. this.#glop.op.uiAttribs.area.origW = this._w;
  101. this.#glop.op.uiAttribs.area.origH = this._h;
  102. this._w = this.minsize;
  103. this._h = this.minsize;
  104. this.#rectBg.visible = false;
  105. this.#rectResize.visible = false;
  106. }
  107. if (!this.#glop.op.uiAttribs.areaCollapsed)
  108. {
  109. if (this._h == this.minsize && !this.#glop.op.uiAttribs.areaCollapsed)
  110. {
  111. this.#rectBg.visible = true;
  112. this.#rectResize.visible = true;
  113. this._w = this.#glop.op.uiAttribs.area.origW;
  114. this._h = this.#glop.op.uiAttribs.area.origH;
  115. }
  116. this.#rectBg.visible = this._visible;
  117. this.#rectResize.visible = this._visible;
  118. if (!this._visible) return;
  119. this.#rectBg.setPosition(
  120. this.#glop.x,
  121. this.#glop.y,
  122. 0.2);
  123. if (this.#glOpScopeEnd)
  124. {
  125. this._h = this.#glOpScopeEnd.y - this.#glop.y + this.#glOpScopeEnd.h;
  126. this.#rectBg.setSize(
  127. this._w,
  128. this._h);
  129. this.#rectResize.setPosition(this.#rectResize.x, this.#glOpScopeEnd.y + this.#glOpScopeEnd.h);
  130. }
  131. else
  132. this.#rectBg.setSize(this._w, this._h);
  133. this.#rectResize.setPosition(
  134. this.#glop.x + this._w - this.#rectResize.w,
  135. this.#glop.y + this._h - this.#rectResize.h,
  136. -0.1
  137. );
  138. }
  139. }
  140. if (!this.#glOpScopeEnd && this.#glop.getUiAttribs().scopeArea)
  141. {
  142. if (this.#glop.op.tempData.scopeAreaEndOp)
  143. this.#glOpScopeEnd = this.#glop.glPatch.getGlOp(this.#glop.op.tempData.scopeAreaEndOp);
  144. }
  145. if (!this.#glop.op.uiAttribs.areaCollapsed)
  146. {
  147. this.#glop.op.setUiAttrib({ "area": { "w": this._w, "h": this._h, "id": this.#id } });
  148. this.updateChildOps();
  149. }
  150. }
  151. updateChildOps()
  152. {
  153. const childs = this.#glop.glPatch._getGlOpsInRect(this.#glop.x, this.#glop.y, this.#glop.x + this._w, this.#glop.y + this._h);
  154. const opChilds = [];
  155. let changed = false;
  156. for (let i = 0; i < childs.length; i++)
  157. {
  158. if (childs[i].op)
  159. {
  160. opChilds.push(childs[i].op);
  161. if (childs[i].op.attribs.area != this.#id)
  162. {
  163. childs[i].op.attribs.area = this.#id;
  164. changed = true;
  165. }
  166. }
  167. }
  168. const currentChilds = gui.corePatch().getOpsByArea(this.#id);
  169. for (let i = 0; i < currentChilds.length; i++)
  170. {
  171. if (opChilds.indexOf(currentChilds[i]) == -1)
  172. {
  173. delete currentChilds[i].attribs.area;
  174. changed = true;
  175. }
  176. }
  177. if (changed)
  178. {
  179. this.#glop?.op.emitEvent("areaChildrenChange", childs);
  180. this.#glOpScopeEnd?.op.emitEvent("areaChildrenChange", childs);
  181. }
  182. }
  183. /**
  184. * @private
  185. */
  186. _updateColor()
  187. {
  188. // if (this._glop.opUiAttribs.color)
  189. // {
  190. // const cols = chroma.hex(this._glop.opUiAttribs.color).gl();
  191. // cols[3] = 0.1;
  192. // this._rectBg.setColorArray(cols);
  193. // }
  194. // else
  195. this.#rectBg.setColor(0, 0, 0, 0.08);
  196. }
  197. dispose()
  198. {
  199. this.#rectBg.dispose();
  200. this.#rectResize.dispose();
  201. this.#rectBg = null;
  202. this.#rectResize = null;
  203. return null;
  204. }
  205. }