propertyTabComponent.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import * as React from "react";
  2. import { GlobalState } from '../../globalState';
  3. import { Nullable } from 'babylonjs/types';
  4. import { ButtonLineComponent } from '../../sharedComponents/buttonLineComponent';
  5. import { LineContainerComponent } from '../../sharedComponents/lineContainerComponent';
  6. import { StringTools } from '../../stringTools';
  7. import { FileButtonLineComponent } from '../../sharedComponents/fileButtonLineComponent';
  8. import { Tools } from 'babylonjs/Misc/tools';
  9. import { SerializationTools } from '../../serializationTools';
  10. import { CheckBoxLineComponent } from '../../sharedComponents/checkBoxLineComponent';
  11. import { DataStorage } from '../../dataStorage';
  12. import { GraphNode } from '../../diagram/graphNode';
  13. import { SliderLineComponent } from '../../sharedComponents/sliderLineComponent';
  14. import { GraphFrame } from '../../diagram/graphFrame';
  15. import { TextLineComponent } from '../../sharedComponents/textLineComponent';
  16. import { Engine } from 'babylonjs/Engines/engine';
  17. import { FramePropertyTabComponent } from '../../diagram/properties/framePropertyComponent';
  18. require("./propertyTab.scss");
  19. interface IPropertyTabComponentProps {
  20. globalState: GlobalState;
  21. }
  22. export class PropertyTabComponent extends React.Component<IPropertyTabComponentProps, { currentNode: Nullable<GraphNode>, currentFrame: Nullable<GraphFrame> }> {
  23. constructor(props: IPropertyTabComponentProps) {
  24. super(props)
  25. this.state = { currentNode: null, currentFrame: null };
  26. }
  27. componentDidMount() {
  28. this.props.globalState.onSelectionChangedObservable.add(selection => {
  29. if (selection instanceof GraphNode) {
  30. this.setState({ currentNode: selection, currentFrame: null });
  31. } else if (selection instanceof GraphFrame) {
  32. this.setState({ currentNode: null, currentFrame: selection });
  33. } else {
  34. this.setState({ currentNode: null, currentFrame: null });
  35. }
  36. });
  37. }
  38. load(file: File) {
  39. Tools.ReadFile(file, (data) => {
  40. let decoder = new TextDecoder("utf-8");
  41. SerializationTools.Deserialize(JSON.parse(decoder.decode(data)), this.props.globalState);
  42. }, undefined, true);
  43. }
  44. save() {
  45. let json = SerializationTools.Serialize(this.props.globalState.nodeMaterial, this.props.globalState);
  46. StringTools.DownloadAsFile(this.props.globalState.hostDocument, json, "nodeMaterial.json");
  47. }
  48. customSave() {
  49. this.props.globalState.onLogRequiredObservable.notifyObservers({message: "Saving your material to Babylon.js snippet server...", isError: false});
  50. this.props.globalState.customSave!.action(SerializationTools.Serialize(this.props.globalState.nodeMaterial, this.props.globalState)).then(() => {
  51. this.props.globalState.onLogRequiredObservable.notifyObservers({message: "Material saved successfully", isError: false});
  52. }).catch(err => {
  53. this.props.globalState.onLogRequiredObservable.notifyObservers({message: err, isError: true});
  54. });
  55. }
  56. render() {
  57. if (this.state.currentNode) {
  58. return (
  59. <div id="propertyTab">
  60. <div id="header">
  61. <img id="logo" src="https://www.babylonjs.com/Assets/logo-babylonjs-social-twitter.png" />
  62. <div id="title">
  63. NODE MATERIAL EDITOR
  64. </div>
  65. </div>
  66. {this.state.currentNode.renderProperties()}
  67. </div>
  68. );
  69. }
  70. if (this.state.currentFrame) {
  71. return (
  72. <FramePropertyTabComponent globalState={this.props.globalState} frame={this.state.currentFrame}/>
  73. );
  74. }
  75. let gridSize = DataStorage.ReadNumber("GridSize", 20);
  76. return (
  77. <div id="propertyTab">
  78. <div id="header">
  79. <img id="logo" src="https://www.babylonjs.com/Assets/logo-babylonjs-social-twitter.png" />
  80. <div id="title">
  81. NODE MATERIAL EDITOR
  82. </div>
  83. </div>
  84. <div>
  85. <LineContainerComponent title="GENERAL">
  86. <TextLineComponent label="Version" value={Engine.Version}/>
  87. <TextLineComponent label="Help" value="doc.babylonjs.com" underline={true} onLink={() => window.open('https://doc.babylonjs.com/how_to/node_material', '_blank')}/>
  88. <ButtonLineComponent label="Reset to default" onClick={() => {
  89. this.props.globalState.nodeMaterial!.setToDefault();
  90. this.props.globalState.onResetRequiredObservable.notifyObservers();
  91. }} />
  92. </LineContainerComponent>
  93. <LineContainerComponent title="UI">
  94. <ButtonLineComponent label="Zoom to fit" onClick={() => {
  95. this.props.globalState.onZoomToFitRequiredObservable.notifyObservers();
  96. }} />
  97. <ButtonLineComponent label="Reorganize" onClick={() => {
  98. this.props.globalState.onReOrganizedRequiredObservable.notifyObservers();
  99. }} />
  100. </LineContainerComponent>
  101. <LineContainerComponent title="OPTIONS">
  102. <CheckBoxLineComponent label="Embed textures when saving"
  103. isSelected={() => DataStorage.ReadBoolean("EmbedTextures", true)}
  104. onSelect={(value: boolean) => {
  105. DataStorage.StoreBoolean("EmbedTextures", value);
  106. }}
  107. />
  108. <SliderLineComponent label="Grid size" minimum={0} maximum={100} step={5}
  109. decimalCount={0}
  110. directValue={gridSize}
  111. onChange={value => {
  112. DataStorage.StoreNumber("GridSize", value);
  113. this.props.globalState.onGridSizeChanged.notifyObservers();
  114. this.forceUpdate();
  115. }}
  116. />
  117. <CheckBoxLineComponent label="Show grid"
  118. isSelected={() => DataStorage.ReadBoolean("ShowGrid", true)}
  119. onSelect={(value: boolean) => {
  120. DataStorage.StoreBoolean("ShowGrid", value);
  121. this.props.globalState.onGridSizeChanged.notifyObservers();
  122. }}
  123. />
  124. </LineContainerComponent>
  125. <LineContainerComponent title="FILE">
  126. <FileButtonLineComponent label="Load" onClick={(file) => this.load(file)} accept=".json" />
  127. <ButtonLineComponent label="Save" onClick={() => {
  128. this.save();
  129. }} />
  130. {
  131. this.props.globalState.customSave &&
  132. <ButtonLineComponent label={this.props.globalState.customSave!.label} onClick={() => {
  133. this.customSave();
  134. }} />
  135. }
  136. <ButtonLineComponent label="Generate code" onClick={() => {
  137. StringTools.DownloadAsFile(this.props.globalState.hostDocument, this.props.globalState.nodeMaterial!.generateCode(), "code.txt");
  138. }} />
  139. <ButtonLineComponent label="Export shaders" onClick={() => {
  140. StringTools.DownloadAsFile(this.props.globalState.hostDocument, this.props.globalState.nodeMaterial!.compiledShaders, "shaders.txt");
  141. }} />
  142. </LineContainerComponent>
  143. </div>
  144. </div>
  145. );
  146. }
  147. }