vector4LineComponent.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import * as React from "react";
  2. import { Vector4 } from "babylonjs/Maths/math";
  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. interface IVector4LineComponentProps {
  9. label: string;
  10. target?: any;
  11. propertyName?: string;
  12. value?: Vector4;
  13. step?: number;
  14. onChange?: (newvalue: Vector4) => void;
  15. onPropertyChangedObservable?: Observable<PropertyChangedEvent>;
  16. }
  17. export class Vector4LineComponent extends React.Component<IVector4LineComponentProps, { isExpanded: boolean, value: Vector4 }> {
  18. static defaultProps = {
  19. step: 0.001, // cm
  20. };
  21. private _localChange = false;
  22. constructor(props: IVector4LineComponentProps) {
  23. super(props);
  24. this.state = { isExpanded: false, value: (this.props.value || this.props.target[this.props.propertyName!]).clone() }
  25. }
  26. shouldComponentUpdate(nextProps: IVector4LineComponentProps, nextState: { isExpanded: boolean, value: Vector4 }) {
  27. const nextPropsValue = nextProps.value || nextProps.target[nextProps.propertyName!];
  28. if (!nextPropsValue.equals(nextState.value) || this._localChange) {
  29. nextState.value = nextPropsValue.clone();
  30. this._localChange = false;
  31. return true;
  32. }
  33. return false;
  34. }
  35. switchExpandState() {
  36. this._localChange = true;
  37. this.setState({ isExpanded: !this.state.isExpanded });
  38. }
  39. raiseOnPropertyChanged(previousValue: Vector4) {
  40. if (this.props.onChange) {
  41. this.props.onChange(this.state.value);
  42. }
  43. if (!this.props.onPropertyChangedObservable || !this.props.propertyName) {
  44. return;
  45. }
  46. this.props.onPropertyChangedObservable.notifyObservers({
  47. object: this.props.target,
  48. property: this.props.propertyName,
  49. value: this.state.value,
  50. initialValue: previousValue
  51. });
  52. }
  53. updateVector4() {
  54. const store = (this.props.value || this.props.target[this.props.propertyName!]).clone();
  55. if (this.props.value) {
  56. this.props.value.copyFrom(this.state.value);
  57. } else {
  58. this.props.target[this.props.propertyName!] = this.state.value;
  59. }
  60. this.forceUpdate();
  61. this.raiseOnPropertyChanged(store);
  62. }
  63. updateStateX(value: number) {
  64. this._localChange = true;
  65. this.state.value.x = value;
  66. this.updateVector4();
  67. }
  68. updateStateY(value: number) {
  69. this._localChange = true;
  70. this.state.value.y = value;
  71. this.updateVector4();
  72. }
  73. updateStateZ(value: number) {
  74. this._localChange = true;
  75. this.state.value.z = value;
  76. this.updateVector4();
  77. }
  78. updateStateW(value: number) {
  79. this._localChange = true;
  80. this.state.value.w = value;
  81. this.updateVector4();
  82. }
  83. render() {
  84. const chevron = this.state.isExpanded ? <FontAwesomeIcon icon={faMinus} /> : <FontAwesomeIcon icon={faPlus} />
  85. return (
  86. <div className="vector3Line">
  87. <div className="firstLine">
  88. <div className="label">
  89. {this.props.label}
  90. </div>
  91. <div className="vector">
  92. {`X: ${this.state.value.x.toFixed(2)}, Y: ${this.state.value.y.toFixed(2)}, Z: ${this.state.value.z.toFixed(2)}, W: ${this.state.value.w.toFixed(2)}`}
  93. </div>
  94. <div className="expand hoverIcon" onClick={() => this.switchExpandState()} title="Expand">
  95. {chevron}
  96. </div>
  97. </div>
  98. {
  99. this.state.isExpanded &&
  100. <div className="secondLine">
  101. <NumericInputComponent label="x" step={this.props.step} value={this.state.value.x} onChange={value => this.updateStateX(value)} />
  102. <NumericInputComponent label="y" step={this.props.step} value={this.state.value.y} onChange={value => this.updateStateY(value)} />
  103. <NumericInputComponent label="z" step={this.props.step} value={this.state.value.z} onChange={value => this.updateStateZ(value)} />
  104. <NumericInputComponent label="w" step={this.props.step} value={this.state.value.w} onChange={value => this.updateStateW(value)} />
  105. </div>
  106. }
  107. </div>
  108. );
  109. }
  110. }