vector3LineComponent.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import * as React from "react";
  2. import { Vector3 } from "babylonjs/Maths/math.vector";
  3. import { Observable } from "babylonjs/Misc/observable";
  4. import { NumericInputComponent } from "./numericInputComponent";
  5. import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
  6. import { faMinus, faPlus } from "@fortawesome/free-solid-svg-icons";
  7. import { PropertyChangedEvent } from "../../propertyChangedEvent";
  8. import { SliderLineComponent } from './sliderLineComponent';
  9. import { Tools } from 'babylonjs/Misc/tools';
  10. interface IVector3LineComponentProps {
  11. label: string;
  12. target: any;
  13. propertyName: string;
  14. step?: number;
  15. onChange?: (newvalue: Vector3) => void;
  16. useEuler?: boolean,
  17. onPropertyChangedObservable?: Observable<PropertyChangedEvent>;
  18. }
  19. export class Vector3LineComponent extends React.Component<IVector3LineComponentProps, { isExpanded: boolean, value: Vector3 }> {
  20. static defaultProps = {
  21. step: 0.001, // cm
  22. };
  23. private _localChange = false;
  24. constructor(props: IVector3LineComponentProps) {
  25. super(props);
  26. this.state = { isExpanded: false, value: this.getCurrentValue().clone() }
  27. }
  28. getCurrentValue() {
  29. return this.props.target[this.props.propertyName];
  30. }
  31. shouldComponentUpdate(nextProps: IVector3LineComponentProps, nextState: { isExpanded: boolean, value: Vector3 }) {
  32. const nextPropsValue = this.getCurrentValue();
  33. if (!nextPropsValue.equals(nextState.value) || this._localChange) {
  34. nextState.value = nextPropsValue.clone();
  35. this._localChange = false;
  36. return true;
  37. }
  38. return false;
  39. }
  40. switchExpandState() {
  41. this._localChange = true;
  42. this.setState({ isExpanded: !this.state.isExpanded });
  43. }
  44. raiseOnPropertyChanged(previousValue: Vector3) {
  45. if (this.props.onChange) {
  46. this.props.onChange(this.state.value);
  47. }
  48. if (!this.props.onPropertyChangedObservable) {
  49. return;
  50. }
  51. this.props.onPropertyChangedObservable.notifyObservers({
  52. object: this.props.target,
  53. property: this.props.propertyName,
  54. value: this.state.value,
  55. initialValue: previousValue
  56. });
  57. }
  58. updateVector3() {
  59. const store = this.props.target[this.props.propertyName].clone();
  60. this.props.target[this.props.propertyName] = this.state.value;
  61. this.setState({ value: store });
  62. this.raiseOnPropertyChanged(store);
  63. }
  64. updateStateX(value: number) {
  65. this._localChange = true;
  66. this.state.value.x = value;
  67. this.updateVector3();
  68. }
  69. updateStateY(value: number) {
  70. this._localChange = true;
  71. this.state.value.y = value;
  72. this.updateVector3();
  73. }
  74. updateStateZ(value: number) {
  75. this._localChange = true;
  76. this.state.value.z = value;
  77. this.updateVector3();
  78. }
  79. render() {
  80. const chevron = this.state.isExpanded ? <FontAwesomeIcon icon={faMinus} /> : <FontAwesomeIcon icon={faPlus} />
  81. return (
  82. <div className="vector3Line">
  83. <div className="firstLine">
  84. <div className="label">
  85. {this.props.label}
  86. </div>
  87. <div className="vector">
  88. {
  89. !this.props.useEuler &&
  90. `X: ${this.state.value.x.toFixed(2)}, Y: ${this.state.value.y.toFixed(2)}, Z: ${this.state.value.z.toFixed(2)}`
  91. }
  92. {
  93. this.props.useEuler &&
  94. `X: ${Tools.ToDegrees(this.state.value.x).toFixed(2)}, Y: ${Tools.ToDegrees(this.state.value.y).toFixed(2)}, Z: ${Tools.ToDegrees(this.state.value.z).toFixed(2)}`
  95. }
  96. </div>
  97. <div className="expand hoverIcon" onClick={() => this.switchExpandState()} title="Expand">
  98. {chevron}
  99. </div>
  100. </div>
  101. {
  102. this.state.isExpanded && !this.props.useEuler &&
  103. <div className="secondLine">
  104. <NumericInputComponent label="x" step={this.props.step} value={this.state.value.x} onChange={value => this.updateStateX(value)} />
  105. <NumericInputComponent label="y" step={this.props.step} value={this.state.value.y} onChange={value => this.updateStateY(value)} />
  106. <NumericInputComponent label="z" step={this.props.step} value={this.state.value.z} onChange={value => this.updateStateZ(value)} />
  107. </div>
  108. }
  109. {
  110. this.state.isExpanded && this.props.useEuler &&
  111. <div className="secondLine">
  112. <SliderLineComponent label="x" minimum={0} maximum={360} step={0.1} directValue={Tools.ToDegrees(this.state.value.x)} onChange={value => this.updateStateX(Tools.ToRadians(value))} />
  113. <SliderLineComponent label="y" minimum={0} maximum={360} step={0.1} directValue={Tools.ToDegrees(this.state.value.y)} onChange={value => this.updateStateY(Tools.ToRadians(value))} />
  114. <SliderLineComponent label="z" minimum={0} maximum={360} step={0.1} directValue={Tools.ToDegrees(this.state.value.z)} onChange={value => this.updateStateZ(Tools.ToRadians(value))} />
  115. </div>
  116. }
  117. </div>
  118. );
  119. }
  120. }