guiNode.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { GlobalState } from "../globalState";
  2. import { Nullable } from "babylonjs/types";
  3. import { Observer } from "babylonjs/Misc/observable";
  4. import { WorkbenchComponent, FramePortData } from "./workbench";
  5. import { Control } from "babylonjs-gui/2D/controls/control";
  6. import { Vector2 } from "babylonjs/Maths/math.vector";
  7. import { Container } from "babylonjs-gui/2D/controls/container";
  8. export class GUINode {
  9. private _x = 0;
  10. private _y = 0;
  11. private _gridAlignedX = 0;
  12. private _gridAlignedY = 0;
  13. private _globalState: GlobalState;
  14. private _onSelectionChangedObserver: Nullable<Observer<Nullable<GUINode | FramePortData>>>;
  15. private _onSelectionBoxMovedObserver: Nullable<Observer<ClientRect | DOMRect>>;
  16. private _onUpdateRequiredObserver: Nullable<Observer<void>>;
  17. private _ownerCanvas: WorkbenchComponent;
  18. private _isSelected: boolean;
  19. private _isVisible = true;
  20. private _enclosingFrameId = -1;
  21. public children: GUINode[] = [];
  22. public get isVisible() {
  23. return this._isVisible;
  24. }
  25. public set isVisible(value: boolean) {
  26. this._isVisible = value;
  27. }
  28. public get gridAlignedX() {
  29. return this._gridAlignedX;
  30. }
  31. public get gridAlignedY() {
  32. return this._gridAlignedY;
  33. }
  34. public get x() {
  35. return this._x;
  36. }
  37. public set x(value: number) {
  38. if (this._x === value) {
  39. return;
  40. }
  41. this._x = value;
  42. this._gridAlignedX = this._ownerCanvas.getGridPosition(value);
  43. }
  44. public get y() {
  45. return this._y;
  46. }
  47. public set y(value: number) {
  48. if (this._y === value) {
  49. return;
  50. }
  51. this._y = value;
  52. this._gridAlignedY = this._ownerCanvas.getGridPosition(value);
  53. }
  54. public get width() {
  55. return this.guiControl.widthInPixels;
  56. }
  57. public get height() {
  58. return this.guiControl.heightInPixels;
  59. }
  60. public get id() {
  61. return this.guiControl.uniqueId;
  62. }
  63. public get name() {
  64. return this.guiControl.name;
  65. }
  66. public get isSelected() {
  67. return this._isSelected;
  68. }
  69. public get enclosingFrameId() {
  70. return this._enclosingFrameId;
  71. }
  72. public set enclosingFrameId(value: number) {
  73. this._enclosingFrameId = value;
  74. }
  75. public set isSelected(value: boolean) {
  76. this._isSelected = value;
  77. if (value) {
  78. this._globalState.onSelectionChangedObservable.notifyObservers(this);
  79. }
  80. }
  81. public constructor(globalState: GlobalState, public guiControl: Control) {
  82. this._globalState = globalState;
  83. this._ownerCanvas = this._globalState.workbench;
  84. this.x = guiControl.leftInPixels;
  85. this.y = guiControl.topInPixels;
  86. guiControl.onPointerUpObservable.add((evt) => {
  87. this.clicked = false;
  88. console.log("up");
  89. });
  90. guiControl.onPointerDownObservable.add((evt) => {
  91. if (!this._ownerCanvas.isUp) return;
  92. this.clicked = true;
  93. this.isSelected = true;
  94. console.log("down");
  95. this._ownerCanvas.isUp = false;
  96. });
  97. guiControl.onPointerEnterObservable.add((evt) => {
  98. this._ownerCanvas.isOverGUINode = true;
  99. console.log("in");
  100. });
  101. guiControl.onPointerOutObservable.add((evt) => {
  102. this._ownerCanvas.isOverGUINode = false;
  103. console.log("out");
  104. });
  105. this._onSelectionBoxMovedObserver = this._globalState.onSelectionBoxMoved.add((rect1) => {});
  106. }
  107. public cleanAccumulation(useCeil = false) {
  108. this.x = this._ownerCanvas.getGridPosition(this.x, useCeil);
  109. this.y = this._ownerCanvas.getGridPosition(this.y, useCeil);
  110. }
  111. public clicked: boolean;
  112. public _onMove(evt: Vector2, startPos: Vector2, ignorClick: boolean = false) {
  113. if (!this.clicked && !ignorClick) return false;
  114. console.log("moving");
  115. let newX = (evt.x - startPos.x) ;// / this._ownerCanvas.zoom;
  116. let newY = (evt.y - startPos.y) ;// / this._ownerCanvas.zoom;
  117. this.x += newX;
  118. this.y += newY;
  119. this.children.forEach((child) => {
  120. child._onMove(evt, startPos, true);
  121. });
  122. return true;
  123. //evt.stopPropagation();
  124. }
  125. public updateVisual() {
  126. this.guiControl.leftInPixels = this.x;
  127. this.guiControl.topInPixels = this.y;
  128. }
  129. private _isContainer() {
  130. switch (this.guiControl.typeName) {
  131. case "Button":
  132. case "StackPanel":
  133. case "Rectangle":
  134. case "Ellipse":
  135. return true;
  136. default:
  137. return false;
  138. }
  139. }
  140. public addGui(childNode: GUINode) {
  141. if (!this._isContainer) return;
  142. this.children.push(childNode);
  143. (this.guiControl as Container).addControl(childNode.guiControl);
  144. //adjust the position to be relative
  145. //childNode.x = this.x - childNode.x;
  146. //childNode.y = this.y - childNode.y;
  147. }
  148. public dispose() {
  149. // notify frame observers that this node is being deleted
  150. this._globalState.onGuiNodeRemovalObservable.notifyObservers(this);
  151. if (this._onSelectionChangedObserver) {
  152. this._globalState.onSelectionChangedObservable.remove(this._onSelectionChangedObserver);
  153. }
  154. if (this._onUpdateRequiredObserver) {
  155. this._globalState.onUpdateRequiredObservable.remove(this._onUpdateRequiredObserver);
  156. }
  157. if (this._onSelectionBoxMovedObserver) {
  158. this._globalState.onSelectionBoxMoved.remove(this._onSelectionBoxMovedObserver);
  159. }
  160. this.guiControl.dispose();
  161. }
  162. }