propertyTabComponent.tsx 8.8 KB

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