workbenchEditor.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import * as React from "react";
  2. import { GlobalState } from "./globalState";
  3. import { GuiListComponent } from "./components/guiList/guiListComponent";
  4. import { PropertyTabComponent } from "./components/propertyTab/propertyTabComponent";
  5. import { Portal } from "./portal";
  6. import { LogComponent } from "./components/log/logComponent";
  7. import { DataStorage } from "babylonjs/Misc/dataStorage";
  8. import { Nullable } from "babylonjs/types";
  9. import { GUINodeTools } from "./guiNodeTools";
  10. import { IEditorData } from "./nodeLocationInfo";
  11. import { WorkbenchComponent } from "./diagram/workbench";
  12. import { GUINode } from "./diagram/guiNode";
  13. import { _TypeStore } from "babylonjs/Misc/typeStore";
  14. import { MessageDialogComponent } from "./sharedComponents/messageDialog";
  15. require("./main.scss");
  16. interface IGraphEditorProps {
  17. globalState: GlobalState;
  18. }
  19. interface IGraphEditorState {
  20. showPreviewPopUp: boolean;
  21. }
  22. export class WorkbenchEditor extends React.Component<IGraphEditorProps, IGraphEditorState> {
  23. private _workbenchCanvas: WorkbenchComponent;
  24. private _startX: number;
  25. private _moveInProgress: boolean;
  26. private _leftWidth = DataStorage.ReadNumber("LeftWidth", 200);
  27. private _rightWidth = DataStorage.ReadNumber("RightWidth", 300);
  28. private _onWidgetKeyUpPointer: any;
  29. private _popUpWindow: Window;
  30. componentDidMount() {
  31. if (this.props.globalState.hostDocument) {
  32. this._workbenchCanvas = this.refs["workbenchCanvas"] as WorkbenchComponent;
  33. }
  34. if (navigator.userAgent.indexOf("Mobile") !== -1) {
  35. ((this.props.globalState.hostDocument || document).querySelector(".blocker") as HTMLElement).style.visibility = "visible";
  36. }
  37. }
  38. componentWillUnmount() {
  39. if (this.props.globalState.hostDocument) {
  40. this.props.globalState.hostDocument!.removeEventListener("keyup", this._onWidgetKeyUpPointer, false);
  41. }
  42. }
  43. constructor(props: IGraphEditorProps) {
  44. super(props);
  45. this.state = {
  46. showPreviewPopUp: false,
  47. };
  48. this.props.globalState.hostDocument!.addEventListener(
  49. "keydown",
  50. (evt) => {
  51. if ((evt.keyCode === 46 || evt.keyCode === 8) && !this.props.globalState.blockKeyboardEvents) {
  52. // Delete
  53. }
  54. if (!evt.ctrlKey || this.props.globalState.blockKeyboardEvents) {
  55. return;
  56. }
  57. if (evt.key === "c") {
  58. // Copy
  59. let selectedItems = this._workbenchCanvas.selectedGuiNodes;
  60. if (!selectedItems.length) {
  61. return;
  62. }
  63. let selectedItem = selectedItems[0] as GUINode;
  64. if (!selectedItem.guiControl) {
  65. return;
  66. }
  67. } else if (evt.key === "v") {
  68. // Paste
  69. }
  70. },
  71. false
  72. );
  73. }
  74. pasteSelection(copiedNodes: GUINode[], currentX: number, currentY: number, selectNew = false) {
  75. //let originalNode: Nullable<GUINode> = null;
  76. let newNodes: GUINode[] = [];
  77. // Copy to prevent recursive side effects while creating nodes.
  78. copiedNodes = copiedNodes.slice();
  79. // Cancel selection
  80. this.props.globalState.onSelectionChangedObservable.notifyObservers(null);
  81. // Create new nodes
  82. for (var node of copiedNodes) {
  83. let block = node.guiControl;
  84. if (!block) {
  85. continue;
  86. }
  87. }
  88. return newNodes;
  89. }
  90. zoomToFit() {
  91. this._workbenchCanvas.zoomToFit();
  92. }
  93. showWaitScreen() {
  94. this.props.globalState.hostDocument.querySelector(".wait-screen")?.classList.remove("hidden");
  95. }
  96. hideWaitScreen() {
  97. this.props.globalState.hostDocument.querySelector(".wait-screen")?.classList.add("hidden");
  98. }
  99. reOrganize(editorData: Nullable<IEditorData> = null, isImportingAFrame = false) {
  100. this.showWaitScreen();
  101. this._workbenchCanvas._isLoading = true; // Will help loading large graphes
  102. setTimeout(() => {
  103. if (!editorData || !editorData.locations) {
  104. this._workbenchCanvas.distributeGraph();
  105. } else {
  106. // Locations
  107. for (var location of editorData.locations) {
  108. for (var node of this._workbenchCanvas.nodes) {
  109. if (node.guiControl && node.guiControl.uniqueId === location.blockId) {
  110. node.x = location.x;
  111. node.y = location.y;
  112. node.cleanAccumulation();
  113. break;
  114. }
  115. }
  116. }
  117. }
  118. this._workbenchCanvas._isLoading = false;
  119. for (var node of this._workbenchCanvas.nodes) {
  120. }
  121. this.hideWaitScreen();
  122. });
  123. }
  124. onPointerDown(evt: React.PointerEvent<HTMLDivElement>) {
  125. this._startX = evt.clientX;
  126. this._moveInProgress = true;
  127. evt.currentTarget.setPointerCapture(evt.pointerId);
  128. }
  129. onPointerUp(evt: React.PointerEvent<HTMLDivElement>) {
  130. this._moveInProgress = false;
  131. evt.currentTarget.releasePointerCapture(evt.pointerId);
  132. }
  133. resizeColumns(evt: React.PointerEvent<HTMLDivElement>, forLeft = true) {
  134. if (!this._moveInProgress) {
  135. return;
  136. }
  137. const deltaX = evt.clientX - this._startX;
  138. const rootElement = evt.currentTarget.ownerDocument!.getElementById("gui-editor-workbench-root") as HTMLDivElement;
  139. if (forLeft) {
  140. this._leftWidth += deltaX;
  141. this._leftWidth = Math.max(150, Math.min(400, this._leftWidth));
  142. DataStorage.WriteNumber("LeftWidth", this._leftWidth);
  143. } else {
  144. this._rightWidth -= deltaX;
  145. this._rightWidth = Math.max(250, Math.min(500, this._rightWidth));
  146. DataStorage.WriteNumber("RightWidth", this._rightWidth);
  147. }
  148. rootElement.style.gridTemplateColumns = this.buildColumnLayout();
  149. this._startX = evt.clientX;
  150. }
  151. buildColumnLayout() {
  152. return `${this._leftWidth}px 4px calc(100% - ${this._leftWidth + 8 + this._rightWidth}px) 4px ${this._rightWidth}px`;
  153. }
  154. emitNewBlock(event: React.DragEvent<HTMLDivElement>) {
  155. var data = event.dataTransfer.getData("babylonjs-gui-node") as string;
  156. let guiElement = GUINodeTools.CreateControlFromString (data);
  157. let newGuiNode = this._workbenchCanvas.appendBlock(guiElement);
  158. /*let x = event.clientX; // - event.currentTarget.offsetLeft - this._workbenchCanvas.x;
  159. let y = event.clientY; // - event.currentTarget.offsetTop - this._workbenchCanvas.y - 20;
  160. newGuiNode.x += (x - newGuiNode.x);
  161. newGuiNode.y += y - newGuiNode.y;
  162. //newGuiNode.cleanAccumulation();*/
  163. this.props.globalState.onSelectionChangedObservable.notifyObservers(null);
  164. this.props.globalState.onSelectionChangedObservable.notifyObservers(newGuiNode);
  165. this.forceUpdate();
  166. }
  167. handlePopUp = () => {
  168. this.setState({
  169. showPreviewPopUp: true,
  170. });
  171. this.props.globalState.hostWindow.addEventListener("beforeunload", this.handleClosingPopUp);
  172. };
  173. handleClosingPopUp = () => {
  174. this._popUpWindow.close();
  175. };
  176. createPopupWindow = (title: string, windowVariableName: string, width = 500, height = 500): Window | null => {
  177. const windowCreationOptionsList = {
  178. width: width,
  179. height: height,
  180. top: (this.props.globalState.hostWindow.innerHeight - width) / 2 + window.screenY,
  181. left: (this.props.globalState.hostWindow.innerWidth - height) / 2 + window.screenX,
  182. };
  183. var windowCreationOptions = Object.keys(windowCreationOptionsList)
  184. .map((key) => key + "=" + (windowCreationOptionsList as any)[key])
  185. .join(",");
  186. const popupWindow = this.props.globalState.hostWindow.open("", title, windowCreationOptions);
  187. if (!popupWindow) {
  188. return null;
  189. }
  190. const parentDocument = popupWindow.document;
  191. parentDocument.title = title;
  192. parentDocument.body.style.width = "100%";
  193. parentDocument.body.style.height = "100%";
  194. parentDocument.body.style.margin = "0";
  195. parentDocument.body.style.padding = "0";
  196. let parentControl = parentDocument.createElement("div");
  197. parentControl.style.width = "100%";
  198. parentControl.style.height = "100%";
  199. parentControl.style.margin = "0";
  200. parentControl.style.padding = "0";
  201. parentControl.style.display = "grid";
  202. parentControl.style.gridTemplateRows = "40px auto";
  203. parentControl.id = "gui-editor-workbench-root";
  204. parentControl.className = "right-panel";
  205. popupWindow.document.body.appendChild(parentControl);
  206. this.copyStyles(this.props.globalState.hostWindow.document, parentDocument);
  207. (this as any)[windowVariableName] = popupWindow;
  208. this._popUpWindow = popupWindow;
  209. return popupWindow;
  210. };
  211. copyStyles = (sourceDoc: HTMLDocument, targetDoc: HTMLDocument) => {
  212. const styleContainer = [];
  213. for (var index = 0; index < sourceDoc.styleSheets.length; index++) {
  214. var styleSheet: any = sourceDoc.styleSheets[index];
  215. try {
  216. if (styleSheet.href) {
  217. // for <link> elements loading CSS from a URL
  218. const newLinkEl = sourceDoc.createElement("link");
  219. newLinkEl.rel = "stylesheet";
  220. newLinkEl.href = styleSheet.href;
  221. targetDoc.head!.appendChild(newLinkEl);
  222. styleContainer.push(newLinkEl);
  223. } else if (styleSheet.cssRules) {
  224. // for <style> elements
  225. const newStyleEl = sourceDoc.createElement("style");
  226. for (var cssRule of styleSheet.cssRules) {
  227. newStyleEl.appendChild(sourceDoc.createTextNode(cssRule.cssText));
  228. }
  229. targetDoc.head!.appendChild(newStyleEl);
  230. styleContainer.push(newStyleEl);
  231. }
  232. } catch (e) {
  233. console.log(e);
  234. }
  235. }
  236. };
  237. fixPopUpStyles = (document: Document) => {
  238. const previewContainer = document.getElementById("preview");
  239. if (previewContainer) {
  240. previewContainer.style.height = "auto";
  241. previewContainer.style.gridRow = "1";
  242. }
  243. const previewConfigBar = document.getElementById("preview-config-bar");
  244. if (previewConfigBar) {
  245. previewConfigBar.style.gridRow = "2";
  246. }
  247. const newWindowButton = document.getElementById("preview-new-window");
  248. if (newWindowButton) {
  249. newWindowButton.style.display = "none";
  250. }
  251. const previewMeshBar = document.getElementById("preview-mesh-bar");
  252. if (previewMeshBar) {
  253. previewMeshBar.style.gridTemplateColumns = "auto 1fr 40px 40px";
  254. }
  255. };
  256. render() {
  257. return (
  258. <Portal globalState={this.props.globalState}>
  259. <div
  260. id="gui-editor-workbench-root"
  261. style={{
  262. gridTemplateColumns: this.buildColumnLayout(),
  263. }}
  264. onMouseMove={(evt) => {
  265. // this._mouseLocationX = evt.pageX;
  266. // this._mouseLocationY = evt.pageY;
  267. }}
  268. onMouseDown={(evt) => {
  269. if ((evt.target as HTMLElement).nodeName === "INPUT") {
  270. return;
  271. }
  272. this.props.globalState.blockKeyboardEvents = false;
  273. }}
  274. >
  275. {/* Node creation menu */}
  276. <GuiListComponent globalState={this.props.globalState} />
  277. <div id="leftGrab" onPointerDown={(evt) => this.onPointerDown(evt)} onPointerUp={(evt) => this.onPointerUp(evt)} onPointerMove={(evt) => this.resizeColumns(evt)}></div>
  278. {/* The gui workbench diagram */}
  279. <div
  280. className="diagram-container"
  281. onDrop={(event) => {
  282. this.emitNewBlock(event);
  283. }}
  284. onDragOver={(event) => {
  285. event.preventDefault();
  286. }}
  287. >
  288. <WorkbenchComponent ref={"workbenchCanvas"} globalState={this.props.globalState} />
  289. </div>
  290. <div id="rightGrab" onPointerDown={(evt) => this.onPointerDown(evt)} onPointerUp={(evt) => this.onPointerUp(evt)} onPointerMove={(evt) => this.resizeColumns(evt, false)}></div>
  291. {/* Property tab */}
  292. <div className="right-panel">
  293. <PropertyTabComponent globalState={this.props.globalState} />
  294. </div>
  295. <LogComponent globalState={this.props.globalState} />
  296. </div>
  297. <MessageDialogComponent globalState={this.props.globalState} />
  298. <div className="blocker">Node Material Editor runs only on desktop</div>
  299. <div className="wait-screen hidden">Processing...please wait</div>
  300. </Portal>
  301. );
  302. }
  303. }