vector2LineComponent.tsx 4.0 KB

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