previewMeshControlComponent.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import * as React from "react";
  2. import { GlobalState } from '../../globalState';
  3. import { Color3, Color4 } from 'babylonjs/Maths/math.color';
  4. import { PreviewMeshType } from './previewMeshType';
  5. import { DataStorage } from '../../dataStorage';
  6. import { OptionsLineComponent } from '../../sharedComponents/optionsLineComponent';
  7. import * as ReactDOM from 'react-dom';
  8. const popUpIcon: string = require("./svgs/popOut.svg");
  9. const colorPicker: string = require("./svgs/colorPicker.svg");
  10. const pauseIcon: string = require("./svgs/pauseIcon.svg");
  11. const playIcon: string = require("./svgs/playIcon.svg");
  12. interface IPreviewMeshControlComponent {
  13. globalState: GlobalState;
  14. togglePreviewAreaComponent: () => void;
  15. }
  16. export class PreviewMeshControlComponent extends React.Component<IPreviewMeshControlComponent> {
  17. private colorInputRef: React.RefObject<HTMLInputElement>;
  18. constructor(props: IPreviewMeshControlComponent) {
  19. super(props);
  20. this.colorInputRef = React.createRef();
  21. }
  22. changeMeshType(newOne: PreviewMeshType) {
  23. if (this.props.globalState.previewMeshType === newOne) {
  24. return;
  25. }
  26. this.props.globalState.previewMeshType = newOne;
  27. this.props.globalState.onPreviewCommandActivated.notifyObservers();
  28. DataStorage.StoreNumber("PreviewMeshType", newOne);
  29. this.forceUpdate();
  30. }
  31. useCustomMesh(evt: any) {
  32. var files: File[] = evt.target.files;
  33. if (files && files.length) {
  34. let file = files[0];
  35. this.props.globalState.previewMeshFile = file;
  36. this.props.globalState.previewMeshType = PreviewMeshType.Custom;
  37. this.props.globalState.onPreviewCommandActivated.notifyObservers();
  38. this.forceUpdate();
  39. }
  40. (ReactDOM.findDOMNode(this.refs["file-picker"]) as HTMLInputElement).value = "";
  41. }
  42. onPopUp() {
  43. this.props.togglePreviewAreaComponent();
  44. }
  45. changeAnimation() {
  46. this.props.globalState.rotatePreview = !this.props.globalState.rotatePreview;
  47. this.props.globalState.onAnimationCommandActivated.notifyObservers();
  48. this.forceUpdate();
  49. }
  50. changeBackground(value: string) {
  51. const newColor = Color3.FromHexString(value);
  52. DataStorage.StoreNumber("BackgroundColorR", newColor.r);
  53. DataStorage.StoreNumber("BackgroundColorG", newColor.g);
  54. DataStorage.StoreNumber("BackgroundColorB", newColor.b);
  55. const newBackgroundColor = Color4.FromColor3(newColor, 1.0);
  56. this.props.globalState.backgroundColor = newBackgroundColor;
  57. this.props.globalState.onPreviewBackgroundChanged.notifyObservers();
  58. }
  59. changeBackgroundClick() {
  60. this.colorInputRef.current?.click();
  61. }
  62. render() {
  63. var meshTypeOptions = [
  64. { label: "Cube", value: PreviewMeshType.Box },
  65. { label: "Cylinder", value: PreviewMeshType.Cylinder },
  66. { label: "Plane", value: PreviewMeshType.Plane },
  67. { label: "Shader ball", value: PreviewMeshType.ShaderBall },
  68. { label: "Sphere", value: PreviewMeshType.Sphere },
  69. { label: "Load...", value: PreviewMeshType.Custom + 1 }
  70. ];
  71. if (this.props.globalState.previewMeshType === PreviewMeshType.Custom) {
  72. meshTypeOptions.splice(0, 0, {
  73. label: "Custom", value: PreviewMeshType.Custom
  74. });
  75. }
  76. return (
  77. <div id="preview-mesh-bar">
  78. <OptionsLineComponent label="" options={meshTypeOptions} target={this.props.globalState}
  79. propertyName="previewMeshType"
  80. noDirectUpdate={true}
  81. onSelect={(value: any) => {
  82. if (value !== PreviewMeshType.Custom + 1) {
  83. this.changeMeshType(value);
  84. } else {
  85. (ReactDOM.findDOMNode(this.refs["file-picker"]) as HTMLElement).click();
  86. }
  87. }} />
  88. <div style={{
  89. display: "none"
  90. }} title="Preview with a custom mesh" >
  91. <input ref="file-picker" id="file-picker" type="file" onChange={evt => this.useCustomMesh(evt)} accept=".gltf, .glb, .babylon, .obj"/>
  92. </div>
  93. <div
  94. title="Turn-table animation"
  95. onClick={() => this.changeAnimation()} className="button" id="play-button">
  96. {this.props.globalState.rotatePreview ? <img src={pauseIcon} alt=""/> : <img src={playIcon} alt=""/>}
  97. </div>
  98. <div
  99. id="color-picker-button"
  100. title="Background color"
  101. className={"button align"}
  102. onClick={_ => this.changeBackgroundClick()}
  103. >
  104. <img src={colorPicker} alt=""/>
  105. <label htmlFor="color-picker" id="color-picker-label">
  106. </label>
  107. <input ref={this.colorInputRef} id="color-picker" type="color" onChange={evt => this.changeBackground(evt.target.value)} />
  108. </div>
  109. <div
  110. title="Open preview in new window" id="preview-new-window"
  111. onClick={() => this.onPopUp()} className="button">
  112. <img src={popUpIcon} alt=""/>
  113. </div>
  114. </div>
  115. );
  116. }
  117. }