propertyTabComponent.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import * as React from "react";
  2. import { GlobalState } from '../../globalState';
  3. import { Nullable } from 'babylonjs/types';
  4. import { DefaultNodeModel } from '../../components/diagram/defaultNodeModel';
  5. import { ButtonLineComponent } from '../../sharedComponents/buttonLineComponent';
  6. import { LineContainerComponent } from '../../sharedComponents/lineContainerComponent';
  7. import { StringTools } from '../../stringTools';
  8. import { FileButtonLineComponent } from '../../sharedComponents/fileButtonLineComponent';
  9. import { Tools } from 'babylonjs/Misc/tools';
  10. import { SerializationTools } from '../../serializationTools';
  11. import { CheckBoxLineComponent } from '../../sharedComponents/checkBoxLineComponent';
  12. import { DataStorage } from '../../dataStorage';
  13. require("./propertyTab.scss");
  14. interface IPropertyTabComponentProps {
  15. globalState: GlobalState;
  16. }
  17. export class PropertyTabComponent extends React.Component<IPropertyTabComponentProps, { currentNode: Nullable<DefaultNodeModel> }> {
  18. constructor(props: IPropertyTabComponentProps) {
  19. super(props)
  20. this.state = { currentNode: null };
  21. }
  22. componentDidMount() {
  23. this.props.globalState.onSelectionChangedObservable.add(block => {
  24. this.setState({ currentNode: block });
  25. });
  26. }
  27. load(file: File) {
  28. Tools.ReadFile(file, (data) => {
  29. let decoder = new TextDecoder("utf-8");
  30. SerializationTools.Deserialize(JSON.parse(decoder.decode(data)), this.props.globalState);
  31. }, undefined, true);
  32. }
  33. save() {
  34. let json = SerializationTools.Serialize(this.props.globalState.nodeMaterial, this.props.globalState);
  35. StringTools.DownloadAsFile(this.props.globalState.hostDocument, json, "nodeMaterial.json");
  36. }
  37. customSave() {
  38. this.props.globalState.onLogRequiredObservable.notifyObservers({message: "Saving your material to Babylon.js snippet server...", isError: false});
  39. this.props.globalState.customSave!.action(SerializationTools.Serialize(this.props.globalState.nodeMaterial, this.props.globalState)).then(() => {
  40. this.props.globalState.onLogRequiredObservable.notifyObservers({message: "Material saved successfully", isError: false});
  41. }).catch(err => {
  42. this.props.globalState.onLogRequiredObservable.notifyObservers({message: err, isError: true});
  43. });
  44. }
  45. render() {
  46. if (this.state.currentNode) {
  47. return (
  48. <div id="propertyTab">
  49. <div id="header">
  50. <img id="logo" src="https://www.babylonjs.com/Assets/logo-babylonjs-social-twitter.png" />
  51. <div id="title">
  52. NODE MATERIAL EDITOR
  53. </div>
  54. </div>
  55. {this.state.currentNode.renderProperties(this.props.globalState)}
  56. </div>
  57. );
  58. }
  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. <div>
  68. <LineContainerComponent title="GENERAL">
  69. <ButtonLineComponent label="Reset to default" onClick={() => {
  70. this.props.globalState.nodeMaterial!.setToDefault();
  71. this.props.globalState.onResetRequiredObservable.notifyObservers(null);
  72. }} />
  73. </LineContainerComponent>
  74. <LineContainerComponent title="UI">
  75. <ButtonLineComponent label="Zoom to fit" onClick={() => {
  76. this.props.globalState.onZoomToFitRequiredObservable.notifyObservers();
  77. }} />
  78. <ButtonLineComponent label="Reorganize" onClick={() => {
  79. this.props.globalState.onReOrganizedRequiredObservable.notifyObservers();
  80. }} />
  81. </LineContainerComponent>
  82. <LineContainerComponent title="OPTIONS">
  83. <CheckBoxLineComponent label="Embed textures when saving"
  84. isSelected={() => DataStorage.ReadBoolean("EmbedTextures", true)}
  85. onSelect={(value: boolean) => {
  86. DataStorage.StoreBoolean("EmbedTextures", value);
  87. }}
  88. />
  89. </LineContainerComponent>
  90. <LineContainerComponent title="FILE">
  91. <FileButtonLineComponent label="Load" onClick={(file) => this.load(file)} accept=".json" />
  92. <ButtonLineComponent label="Save" onClick={() => {
  93. this.save();
  94. }} />
  95. {
  96. this.props.globalState.customSave &&
  97. <ButtonLineComponent label={this.props.globalState.customSave!.label} onClick={() => {
  98. this.customSave();
  99. }} />
  100. }
  101. <ButtonLineComponent label="Generate code" onClick={() => {
  102. StringTools.DownloadAsFile(this.props.globalState.hostDocument, this.props.globalState.nodeMaterial!.generateCode(), "code.txt");
  103. }} />
  104. <ButtonLineComponent label="Export shaders" onClick={() => {
  105. StringTools.DownloadAsFile(this.props.globalState.hostDocument, this.props.globalState.nodeMaterial!.compiledShaders, "shaders.txt");
  106. }} />
  107. </LineContainerComponent>
  108. </div>
  109. </div>
  110. );
  111. }
  112. }