guiNode.ts 6.5 KB

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