workbenchEditor.tsx 14 KB

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